Whamcloud - gitweb
d76aa06b9af708dd4e93d55de9c5b49c5fabcc84
[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-4536 LU-5242   LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  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         test_mkdir $DIR/$tdir
1401         touch $DIR/$tdir/$tfile || error "touch failed"
1402         [[ $($GETSTRIPE -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1403                 error "missing objects"
1404 }
1405 run_test 27i "$GETSTRIPE with some objects"
1406
1407 test_27j() {
1408         test_mkdir -p $DIR/d27
1409         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1410 }
1411 run_test 27j "setstripe with bad stripe offset (should return error)"
1412
1413 test_27k() { # bug 2844
1414         test_mkdir -p $DIR/d27
1415         FILE=$DIR/d27/f27k
1416         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1417         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1418         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1419         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1420         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1421         dd if=/dev/zero of=$FILE bs=4k count=1
1422         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1423         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1424 }
1425 run_test 27k "limit i_blksize for broken user apps ============="
1426
1427 test_27l() {
1428         test_mkdir -p $DIR/d27
1429         mcreate $DIR/f27l || error "creating file"
1430         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1431                 error "setstripe should have failed" || true
1432 }
1433 run_test 27l "check setstripe permissions (should return error)"
1434
1435 test_27m() {
1436         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1437                 return
1438         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1439                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1440                 return
1441         fi
1442         trap simple_cleanup_common EXIT
1443         test_mkdir -p $DIR/$tdir
1444         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1445         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1446                 error "dd should fill OST0"
1447         i=2
1448         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1449                 i=$((i + 1))
1450                 [ $i -gt 256 ] && break
1451         done
1452         i=$((i + 1))
1453         touch $DIR/$tdir/f27m_$i
1454         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1455                 error "OST0 was full but new created file still use it"
1456         i=$((i + 1))
1457         touch $DIR/$tdir/f27m_$i
1458         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1459                 error "OST0 was full but new created file still use it"
1460         simple_cleanup_common
1461 }
1462 run_test 27m "create file while OST0 was full =================="
1463
1464 sleep_maxage() {
1465         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1466         sleep $DELAY
1467 }
1468
1469 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1470 # if the OST isn't full anymore.
1471 reset_enospc() {
1472         local OSTIDX=${1:-""}
1473
1474         local list=$(comma_list $(osts_nodes))
1475         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1476
1477         do_nodes $list lctl set_param fail_loc=0
1478         sync    # initiate all OST_DESTROYs from MDS to OST
1479         sleep_maxage
1480 }
1481
1482 exhaust_precreations() {
1483         local OSTIDX=$1
1484         local FAILLOC=$2
1485         local FAILIDX=${3:-$OSTIDX}
1486         local ofacet=ost$((OSTIDX + 1))
1487
1488         test_mkdir -p -c1 $DIR/$tdir
1489         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1490         local mfacet=mds$((mdtidx + 1))
1491         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1492
1493         local OST=$(ostname_from_index $OSTIDX)
1494
1495         # on the mdt's osc
1496         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1497         local last_id=$(do_facet $mfacet lctl get_param -n \
1498                         osc.$mdtosc_proc1.prealloc_last_id)
1499         local next_id=$(do_facet $mfacet lctl get_param -n \
1500                         osc.$mdtosc_proc1.prealloc_next_id)
1501
1502         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1503         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1504
1505         test_mkdir -p $DIR/$tdir/${OST}
1506         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1507 #define OBD_FAIL_OST_ENOSPC              0x215
1508         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1509         echo "Creating to objid $last_id on ost $OST..."
1510         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1511         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1512         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1513         sleep_maxage
1514 }
1515
1516 exhaust_all_precreations() {
1517         local i
1518         for (( i=0; i < OSTCOUNT; i++ )) ; do
1519                 exhaust_precreations $i $1 -1
1520         done
1521 }
1522
1523 test_27n() {
1524         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1526         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1527         remote_ost_nodsh && skip "remote OST with nodsh" && return
1528
1529         reset_enospc
1530         rm -f $DIR/$tdir/$tfile
1531         exhaust_precreations 0 0x80000215
1532         $SETSTRIPE -c -1 $DIR/$tdir
1533         touch $DIR/$tdir/$tfile || error
1534         $GETSTRIPE $DIR/$tdir/$tfile
1535         reset_enospc
1536 }
1537 run_test 27n "create file with some full OSTs =================="
1538
1539 test_27o() {
1540         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1541         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1542         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1543         remote_ost_nodsh && skip "remote OST with nodsh" && return
1544
1545         reset_enospc
1546         rm -f $DIR/$tdir/$tfile
1547         exhaust_all_precreations 0x215
1548
1549         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1550
1551         reset_enospc
1552         rm -rf $DIR/$tdir/*
1553 }
1554 run_test 27o "create file with all full OSTs (should error) ===="
1555
1556 test_27p() {
1557         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1559         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1560         remote_ost_nodsh && skip "remote OST with nodsh" && return
1561
1562         reset_enospc
1563         rm -f $DIR/$tdir/$tfile
1564         test_mkdir -p $DIR/$tdir
1565
1566         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1567         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1568         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1569
1570         exhaust_precreations 0 0x80000215
1571         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1572         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1573         $GETSTRIPE $DIR/$tdir/$tfile
1574
1575         reset_enospc
1576 }
1577 run_test 27p "append to a truncated file with some full OSTs ==="
1578
1579 test_27q() {
1580         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1582         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1583         remote_ost_nodsh && skip "remote OST with nodsh" && return
1584
1585         reset_enospc
1586         rm -f $DIR/$tdir/$tfile
1587
1588         test_mkdir -p $DIR/$tdir
1589         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1590         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1591         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1592
1593         exhaust_all_precreations 0x215
1594
1595         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1596         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1597
1598         reset_enospc
1599 }
1600 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1601
1602 test_27r() {
1603         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1605         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1606         remote_ost_nodsh && skip "remote OST with nodsh" && return
1607
1608         reset_enospc
1609         rm -f $DIR/$tdir/$tfile
1610         exhaust_precreations 0 0x80000215
1611
1612         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1613
1614         reset_enospc
1615 }
1616 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1617
1618 test_27s() { # bug 10725
1619         test_mkdir -p $DIR/$tdir
1620         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1621         local stripe_count=0
1622         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1623         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1624                 error "stripe width >= 2^32 succeeded" || true
1625
1626 }
1627 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1628
1629 test_27t() { # bug 10864
1630         WDIR=$(pwd)
1631         WLFS=$(which lfs)
1632         cd $DIR
1633         touch $tfile
1634         $WLFS getstripe $tfile
1635         cd $WDIR
1636 }
1637 run_test 27t "check that utils parse path correctly"
1638
1639 test_27u() { # bug 4900
1640         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1641         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1642         local index
1643         local list=$(comma_list $(mdts_nodes))
1644
1645 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1646         do_nodes $list $LCTL set_param fail_loc=0x139
1647         test_mkdir -p $DIR/$tdir
1648         rm -rf $DIR/$tdir/*
1649         createmany -o $DIR/$tdir/t- 1000
1650         do_nodes $list $LCTL set_param fail_loc=0
1651
1652         TLOG=$DIR/$tfile.getstripe
1653         $GETSTRIPE $DIR/$tdir > $TLOG
1654         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1655         unlinkmany $DIR/$tdir/t- 1000
1656         [[ $OBJS -gt 0 ]] &&
1657                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1658 }
1659 run_test 27u "skip object creation on OSC w/o objects =========="
1660
1661 test_27v() { # bug 4900
1662         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1664         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1665         remote_ost_nodsh && skip "remote OST with nodsh" && return
1666
1667         exhaust_all_precreations 0x215
1668         reset_enospc
1669
1670         test_mkdir -p $DIR/$tdir
1671         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1672
1673         touch $DIR/$tdir/$tfile
1674         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1675         # all except ost1
1676         for (( i=1; i < OSTCOUNT; i++ )); do
1677                 do_facet ost$i lctl set_param fail_loc=0x705
1678         done
1679         local START=`date +%s`
1680         createmany -o $DIR/$tdir/$tfile 32
1681
1682         local FINISH=`date +%s`
1683         local TIMEOUT=`lctl get_param -n timeout`
1684         local PROCESS=$((FINISH - START))
1685         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1686                error "$FINISH - $START >= $TIMEOUT / 2"
1687         sleep $((TIMEOUT / 2 - PROCESS))
1688         reset_enospc
1689 }
1690 run_test 27v "skip object creation on slow OST ================="
1691
1692 test_27w() { # bug 10997
1693         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1694         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1695         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1696                 error "stripe size $size != 65536" || true
1697         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1698                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1699 }
1700 run_test 27w "check $SETSTRIPE -S option"
1701
1702 test_27wa() {
1703         [[ $OSTCOUNT -lt 2 ]] &&
1704                 skip_env "skipping multiple stripe count/offset test" && return
1705
1706         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1707         for i in $(seq 1 $OSTCOUNT); do
1708                 offset=$((i - 1))
1709                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1710                         error "setstripe -c $i -i $offset failed"
1711                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1712                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1713                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1714                 [ $index -ne $offset ] &&
1715                         error "stripe offset $index != $offset" || true
1716         done
1717 }
1718 run_test 27wa "check $SETSTRIPE -c -i options"
1719
1720 test_27x() {
1721         remote_ost_nodsh && skip "remote OST with nodsh" && return
1722         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1724         OFFSET=$(($OSTCOUNT - 1))
1725         OSTIDX=0
1726         local OST=$(ostname_from_index $OSTIDX)
1727
1728         test_mkdir -p $DIR/$tdir
1729         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1730         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1731         sleep_maxage
1732         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1733         for i in `seq 0 $OFFSET`; do
1734                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1735                 error "OST0 was degraded but new created file still use it"
1736         done
1737         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1738 }
1739 run_test 27x "create files while OST0 is degraded"
1740
1741 test_27y() {
1742         [[ $OSTCOUNT -lt 2 ]] &&
1743                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1744         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1745         remote_ost_nodsh && skip "remote OST with nodsh" && return
1746         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1747
1748         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1749         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1750             osc.$mdtosc.prealloc_last_id)
1751         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1752             osc.$mdtosc.prealloc_next_id)
1753         local fcount=$((last_id - next_id))
1754         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1755         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1756
1757         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1758                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1759         local OST_DEACTIVE_IDX=-1
1760         local OSC
1761         local OSTIDX
1762         local OST
1763
1764         for OSC in $MDS_OSCS; do
1765                 OST=$(osc_to_ost $OSC)
1766                 OSTIDX=$(index_from_ostuuid $OST)
1767                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1768                         OST_DEACTIVE_IDX=$OSTIDX
1769                 fi
1770                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1771                         echo $OSC "is Deactivated:"
1772                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1773                 fi
1774         done
1775
1776         OSTIDX=$(index_from_ostuuid $OST)
1777         mkdir -p $DIR/$tdir
1778         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1779
1780         for OSC in $MDS_OSCS; do
1781                 OST=$(osc_to_ost $OSC)
1782                 OSTIDX=$(index_from_ostuuid $OST)
1783                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1784                         echo $OST "is degraded:"
1785                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1786                                                 obdfilter.$OST.degraded=1
1787                 fi
1788         done
1789
1790         sleep_maxage
1791         createmany -o $DIR/$tdir/$tfile $fcount
1792
1793         for OSC in $MDS_OSCS; do
1794                 OST=$(osc_to_ost $OSC)
1795                 OSTIDX=$(index_from_ostuuid $OST)
1796                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1797                         echo $OST "is recovered from degraded:"
1798                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1799                                                 obdfilter.$OST.degraded=0
1800                 else
1801                         do_facet $SINGLEMDS lctl --device %$OSC activate
1802                 fi
1803         done
1804
1805         # all osp devices get activated, hence -1 stripe count restored
1806         local stripecnt=0
1807
1808         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1809         # devices get activated.
1810         sleep_maxage
1811         $SETSTRIPE -c -1 $DIR/$tfile
1812         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1813         rm -f $DIR/$tfile
1814         [ $stripecnt -ne $OSTCOUNT ] &&
1815                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1816         return 0
1817 }
1818 run_test 27y "create files while OST0 is degraded and the rest inactive"
1819
1820 check_seq_oid()
1821 {
1822         log "check file $1"
1823
1824         lmm_count=$($GETSTRIPE -c $1)
1825         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1826         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1827
1828         local old_ifs="$IFS"
1829         IFS=$'[:]'
1830         fid=($($LFS path2fid $1))
1831         IFS="$old_ifs"
1832
1833         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1834         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1835
1836         # compare lmm_seq and lu_fid->f_seq
1837         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1838         # compare lmm_object_id and lu_fid->oid
1839         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1840
1841         # check the trusted.fid attribute of the OST objects of the file
1842         local have_obdidx=false
1843         local stripe_nr=0
1844         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1845                 # skip lines up to and including "obdidx"
1846                 [ -z "$obdidx" ] && break
1847                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1848                 $have_obdidx || continue
1849
1850                 local ost=$((obdidx + 1))
1851                 local dev=$(ostdevname $ost)
1852                 local oid_hex
1853
1854                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1855
1856                 seq=$(echo $seq | sed -e "s/^0x//g")
1857                 if [ $seq == 0 ]; then
1858                         oid_hex=$(echo $oid)
1859                 else
1860                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1861                 fi
1862                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1863
1864                 local ff
1865                 #
1866                 # Don't unmount/remount the OSTs if we don't need to do that.
1867                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1868                 # update too, until that use mount/ll_decode_filter_fid/mount.
1869                 # Re-enable when debugfs will understand new filter_fid.
1870                 #
1871                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1872                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1873                                 $dev 2>/dev/null" | grep "parent=")
1874                 else
1875                         stop ost$ost
1876                         mount_fstype ost$ost
1877                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1878                                 $(facet_mntpt ost$ost)/$obj_file)
1879                         unmount_fstype ost$ost
1880                         start ost$ost $dev $OST_MOUNT_OPTS
1881                 fi
1882
1883                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1884
1885                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1886
1887                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1888                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1889                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1890                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1891                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1892                 local ff_pstripe
1893                 if echo $ff_parent | grep -q 'stripe='; then
1894                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1895                 else
1896                         #
1897                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1898                         # into f_ver in this case.  See the comment on
1899                         # ff_parent.
1900                         #
1901                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1902                                 sed -e 's/\]//')
1903                 fi
1904
1905                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1906                 [ $ff_pseq = $lmm_seq ] ||
1907                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1908                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1909                 [ $ff_poid = $lmm_oid ] ||
1910                         error "FF parent OID $ff_poid != $lmm_oid"
1911                 (($ff_pstripe == $stripe_nr)) ||
1912                         error "FF stripe $ff_pstripe != $stripe_nr"
1913
1914                 stripe_nr=$((stripe_nr + 1))
1915         done
1916 }
1917
1918 test_27z() {
1919         remote_ost_nodsh && skip "remote OST with nodsh" && return
1920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1921         test_mkdir -p $DIR/$tdir
1922
1923         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1924                 { error "setstripe -c -1 failed"; return 1; }
1925         # We need to send a write to every object to get parent FID info set.
1926         # This _should_ also work for setattr, but does not currently.
1927         # touch $DIR/$tdir/$tfile-1 ||
1928         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1929                 { error "dd $tfile-1 failed"; return 2; }
1930         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1931                 { error "setstripe -c -1 failed"; return 3; }
1932         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1933                 { error "dd $tfile-2 failed"; return 4; }
1934
1935         # make sure write RPCs have been sent to OSTs
1936         sync; sleep 5; sync
1937
1938         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1939         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1940 }
1941 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1942
1943 test_27A() { # b=19102
1944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1945         local restore_size=$($GETSTRIPE -S $MOUNT)
1946         local restore_count=$($GETSTRIPE -c $MOUNT)
1947         local restore_offset=$($GETSTRIPE -i $MOUNT)
1948         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1949         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1950                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1951         local default_size=$($GETSTRIPE -S $MOUNT)
1952         local default_offset=$($GETSTRIPE -i $MOUNT)
1953         local dsize=$((1024 * 1024))
1954         [ $default_size -eq $dsize ] ||
1955                 error "stripe size $default_size != $dsize"
1956         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1957         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1958 }
1959 run_test 27A "check filesystem-wide default LOV EA values"
1960
1961 test_27B() { # LU-2523
1962         test_mkdir -p $DIR/$tdir
1963         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1964         touch $DIR/$tdir/f0
1965         # open f1 with O_LOV_DELAY_CREATE
1966         # rename f0 onto f1
1967         # call setstripe ioctl on open file descriptor for f1
1968         # close
1969         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1970                 $DIR/$tdir/f0
1971
1972         rm -f $DIR/$tdir/f1
1973         # open f1 with O_LOV_DELAY_CREATE
1974         # unlink f1
1975         # call setstripe ioctl on open file descriptor for f1
1976         # close
1977         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1978
1979         # Allow multiop to fail in imitation of NFS's busted semantics.
1980         true
1981 }
1982 run_test 27B "call setstripe on open unlinked file/rename victim"
1983
1984 test_27C() { #LU-2871
1985         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1986
1987         declare -a ost_idx
1988         local index
1989         local found
1990         local i
1991         local j
1992
1993         test_mkdir -p $DIR/$tdir
1994         cd $DIR/$tdir
1995         for i in $(seq 0 $((OSTCOUNT - 1))); do
1996                 # set stripe across all OSTs starting from OST$i
1997                 $SETSTRIPE -i $i -c -1 $tfile$i
1998                 # get striping information
1999                 ost_idx=($($GETSTRIPE $tfile$i |
2000                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2001                 echo ${ost_idx[@]}
2002
2003                 # check the layout
2004                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2005                         error "${#ost_idx[@]} != $OSTCOUNT"
2006
2007                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2008                         found=0
2009                         for j in $(echo ${ost_idx[@]}); do
2010                                 if [ $index -eq $j ]; then
2011                                         found=1
2012                                         break
2013                                 fi
2014                         done
2015                         [ $found = 1 ] ||
2016                                 error "Can not find $index in ${ost_idx[@]}"
2017                 done
2018         done
2019 }
2020 run_test 27C "check full striping across all OSTs"
2021
2022 test_27D() {
2023         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2024         local POOL=${POOL:-testpool}
2025         local first_ost=0
2026         local last_ost=$(($OSTCOUNT - 1))
2027         local ost_step=1
2028         local ost_list=$(seq $first_ost $ost_step $last_ost)
2029         local ost_range="$first_ost $last_ost $ost_step"
2030
2031         mkdir -p $DIR/$tdir
2032         pool_add $POOL || error "pool_add failed"
2033         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2034         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2035                 error "llapi_layout_test failed"
2036         cleanup_pools || error "cleanup_pools failed"
2037 }
2038 run_test 27D "validate llapi_layout API"
2039
2040 # Verify that default_easize is increased from its initial value after
2041 # accessing a widely striped file.
2042 test_27E() {
2043         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2044
2045         # 72 bytes is the minimum space required to store striping
2046         # information for a file striped across one OST:
2047         # (sizeof(struct lov_user_md_v3) +
2048         #  sizeof(struct lov_user_ost_data_v1))
2049         local min_easize=72
2050         $LCTL set_param -n llite.*.default_easize $min_easize ||
2051                 error "lctl set_param failed"
2052         local easize=$($LCTL get_param -n llite.*.default_easize)
2053
2054         [ $easize -eq $min_easize ] ||
2055                 error "failed to set default_easize"
2056
2057         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2058                 error "setstripe failed"
2059         cat $DIR/$tfile
2060         rm $DIR/$tfile
2061
2062         easize=$($LCTL get_param -n llite.*.default_easize)
2063
2064         [ $easize -gt $min_easize ] ||
2065                 error "default_easize not updated"
2066 }
2067 run_test 27E "check that default extended attribute size properly increases"
2068
2069 # createtest also checks that device nodes are created and
2070 # then visible correctly (#2091)
2071 test_28() { # bug 2091
2072         test_mkdir $DIR/d28
2073         $CREATETEST $DIR/d28/ct || error
2074 }
2075 run_test 28 "create/mknod/mkdir with bad file types ============"
2076
2077 test_29() {
2078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2079         cancel_lru_locks mdc
2080         test_mkdir $DIR/d29
2081         touch $DIR/d29/foo
2082         log 'first d29'
2083         ls -l $DIR/d29
2084
2085         declare -i LOCKCOUNTORIG=0
2086         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2087                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2088         done
2089         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2090
2091         declare -i LOCKUNUSEDCOUNTORIG=0
2092         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2093                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2094         done
2095
2096         log 'second d29'
2097         ls -l $DIR/d29
2098         log 'done'
2099
2100         declare -i LOCKCOUNTCURRENT=0
2101         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2102                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2103         done
2104
2105         declare -i LOCKUNUSEDCOUNTCURRENT=0
2106         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2107                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2108         done
2109
2110         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2111                 $LCTL set_param -n ldlm.dump_namespaces ""
2112                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2113                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2114                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2115                 return 2
2116         fi
2117         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2118                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2119                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2120                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2121                 return 3
2122         fi
2123 }
2124 run_test 29 "IT_GETATTR regression  ============================"
2125
2126 test_30a() { # was test_30
2127         cp $(which ls) $DIR || cp /bin/ls $DIR
2128         $DIR/ls / || error
2129         rm $DIR/ls
2130 }
2131 run_test 30a "execute binary from Lustre (execve) =============="
2132
2133 test_30b() {
2134         cp `which ls` $DIR || cp /bin/ls $DIR
2135         chmod go+rx $DIR/ls
2136         $RUNAS $DIR/ls / || error
2137         rm $DIR/ls
2138 }
2139 run_test 30b "execute binary from Lustre as non-root ==========="
2140
2141 test_30c() { # b=22376
2142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2143         cp `which ls` $DIR || cp /bin/ls $DIR
2144         chmod a-rw $DIR/ls
2145         cancel_lru_locks mdc
2146         cancel_lru_locks osc
2147         $RUNAS $DIR/ls / || error
2148         rm -f $DIR/ls
2149 }
2150 run_test 30c "execute binary from Lustre without read perms ===="
2151
2152 test_31a() {
2153         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2154         $CHECKSTAT -a $DIR/f31 || error
2155 }
2156 run_test 31a "open-unlink file =================================="
2157
2158 test_31b() {
2159         touch $DIR/f31 || error
2160         ln $DIR/f31 $DIR/f31b || error
2161         $MULTIOP $DIR/f31b Ouc || error
2162         $CHECKSTAT -t file $DIR/f31 || error
2163 }
2164 run_test 31b "unlink file with multiple links while open ======="
2165
2166 test_31c() {
2167         touch $DIR/f31 || error
2168         ln $DIR/f31 $DIR/f31c || error
2169         multiop_bg_pause $DIR/f31 O_uc || return 1
2170         MULTIPID=$!
2171         $MULTIOP $DIR/f31c Ouc
2172         kill -USR1 $MULTIPID
2173         wait $MULTIPID
2174 }
2175 run_test 31c "open-unlink file with multiple links ============="
2176
2177 test_31d() {
2178         opendirunlink $DIR/d31d $DIR/d31d || error
2179         $CHECKSTAT -a $DIR/d31d || error
2180 }
2181 run_test 31d "remove of open directory ========================="
2182
2183 test_31e() { # bug 2904
2184         check_kernel_version 34 || return 0
2185         openfilleddirunlink $DIR/d31e || error
2186 }
2187 run_test 31e "remove of open non-empty directory ==============="
2188
2189 test_31f() { # bug 4554
2190         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2191         set -vx
2192         test_mkdir $DIR/d31f
2193         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2194         cp /etc/hosts $DIR/d31f
2195         ls -l $DIR/d31f
2196         $GETSTRIPE $DIR/d31f/hosts
2197         multiop_bg_pause $DIR/d31f D_c || return 1
2198         MULTIPID=$!
2199
2200         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2201         test_mkdir $DIR/d31f
2202         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2203         cp /etc/hosts $DIR/d31f
2204         ls -l $DIR/d31f
2205         $GETSTRIPE $DIR/d31f/hosts
2206         multiop_bg_pause $DIR/d31f D_c || return 1
2207         MULTIPID2=$!
2208
2209         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2210         wait $MULTIPID || error "first opendir $MULTIPID failed"
2211
2212         sleep 6
2213
2214         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2215         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2216         set +vx
2217 }
2218 run_test 31f "remove of open directory with open-unlink file ==="
2219
2220 test_31g() {
2221         echo "-- cross directory link --"
2222         test_mkdir -c1 $DIR/${tdir}ga
2223         test_mkdir -c1 $DIR/${tdir}gb
2224         touch $DIR/${tdir}ga/f
2225         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2226         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2227         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2228         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2229         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2230 }
2231 run_test 31g "cross directory link==============="
2232
2233 test_31h() {
2234         echo "-- cross directory link --"
2235         test_mkdir -c1 $DIR/${tdir}
2236         test_mkdir -c1 $DIR/${tdir}/dir
2237         touch $DIR/${tdir}/f
2238         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2239         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2240         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2241         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2242         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2243 }
2244 run_test 31h "cross directory link under child==============="
2245
2246 test_31i() {
2247         echo "-- cross directory link --"
2248         test_mkdir -c1 $DIR/$tdir
2249         test_mkdir -c1 $DIR/$tdir/dir
2250         touch $DIR/$tdir/dir/f
2251         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2252         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2253         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2254         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2255         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2256 }
2257 run_test 31i "cross directory link under parent==============="
2258
2259 test_31j() {
2260         test_mkdir -c1 -p $DIR/$tdir
2261         test_mkdir -c1 -p $DIR/$tdir/dir1
2262         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2263         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2264         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2265         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2266         return 0
2267 }
2268 run_test 31j "link for directory==============="
2269
2270 test_31k() {
2271         test_mkdir -c1 -p $DIR/$tdir
2272         touch $DIR/$tdir/s
2273         touch $DIR/$tdir/exist
2274         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2275         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2276         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2277         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2278         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2279         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2280         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2281         return 0
2282 }
2283 run_test 31k "link to file: the same, non-existing, dir==============="
2284
2285 test_31m() {
2286         mkdir $DIR/d31m
2287         touch $DIR/d31m/s
2288         mkdir $DIR/d31m2
2289         touch $DIR/d31m2/exist
2290         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2291         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2292         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2293         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2294         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2295         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2296         return 0
2297 }
2298 run_test 31m "link to file: the same, non-existing, dir==============="
2299
2300 test_31n() {
2301         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2302         nlink=$(stat --format=%h $DIR/$tfile)
2303         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2304         local fd=$(free_fd)
2305         local cmd="exec $fd<$DIR/$tfile"
2306         eval $cmd
2307         cmd="exec $fd<&-"
2308         trap "eval $cmd" EXIT
2309         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2310         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2311         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2312         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2313         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2314         eval $cmd
2315 }
2316 run_test 31n "check link count of unlinked file"
2317
2318 link_one() {
2319         local TEMPNAME=$(mktemp $1_XXXXXX)
2320         mlink $TEMPNAME $1 2> /dev/null &&
2321                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2322         munlink $TEMPNAME
2323 }
2324
2325 test_31o() { # LU-2901
2326         mkdir -p $DIR/$tdir
2327         for LOOP in $(seq 100); do
2328                 rm -f $DIR/$tdir/$tfile*
2329                 for THREAD in $(seq 8); do
2330                         link_one $DIR/$tdir/$tfile.$LOOP &
2331                 done
2332                 wait
2333                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2334                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2335                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2336                         break || true
2337         done
2338 }
2339 run_test 31o "duplicate hard links with same filename"
2340
2341 test_31p() {
2342         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2343
2344         mkdir $DIR/$tdir
2345         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2346         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2347
2348         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2349                 error "open unlink test1 failed"
2350         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2351                 error "open unlink test2 failed"
2352
2353         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2354                 error "test1 still exists"
2355         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2356                 error "test2 still exists"
2357 }
2358 run_test 31p "remove of open striped directory"
2359
2360 cleanup_test32_mount() {
2361         trap 0
2362         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2363 }
2364
2365 test_32a() {
2366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2367         echo "== more mountpoints and symlinks ================="
2368         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2369         trap cleanup_test32_mount EXIT
2370         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2371         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2372         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2373         cleanup_test32_mount
2374 }
2375 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2376
2377 test_32b() {
2378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2379         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2380         trap cleanup_test32_mount EXIT
2381         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2382         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2383         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2384         cleanup_test32_mount
2385 }
2386 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2387
2388 test_32c() {
2389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2390         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2391         trap cleanup_test32_mount EXIT
2392         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2393         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2394         test_mkdir -p $DIR/$tdir/d2/test_dir
2395         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2396         cleanup_test32_mount
2397 }
2398 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2399
2400 test_32d() {
2401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2402         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2403         trap cleanup_test32_mount EXIT
2404         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2405         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2406         test_mkdir -p $DIR/$tdir/d2/test_dir
2407         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2408         cleanup_test32_mount
2409 }
2410 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2411
2412 test_32e() {
2413         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2414         test_mkdir -p $DIR/d32e/tmp
2415         TMP_DIR=$DIR/d32e/tmp
2416         ln -s $DIR/d32e $TMP_DIR/symlink11
2417         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2418         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2419         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2420 }
2421 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2422
2423 test_32f() {
2424         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2425         test_mkdir -p $DIR/d32f/tmp
2426         TMP_DIR=$DIR/d32f/tmp
2427         ln -s $DIR/d32f $TMP_DIR/symlink11
2428         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2429         ls $DIR/d32f/tmp/symlink11  || error
2430         ls $DIR/d32f/symlink01 || error
2431 }
2432 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2433
2434 test_32g() {
2435         TMP_DIR=$DIR/$tdir/tmp
2436         test_mkdir -p $DIR/$tdir/tmp
2437         test_mkdir $DIR/${tdir}2
2438         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2439         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2440         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2441         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2442         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2443         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2444 }
2445 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2446
2447 test_32h() {
2448         rm -fr $DIR/$tdir $DIR/${tdir}2
2449         TMP_DIR=$DIR/$tdir/tmp
2450         test_mkdir -p $DIR/$tdir/tmp
2451         test_mkdir $DIR/${tdir}2
2452         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2453         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2454         ls $TMP_DIR/symlink12 || error
2455         ls $DIR/$tdir/symlink02  || error
2456 }
2457 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2458
2459 test_32i() {
2460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2461         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2462         trap cleanup_test32_mount EXIT
2463         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2464         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2465         touch $DIR/$tdir/test_file
2466         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2467         cleanup_test32_mount
2468 }
2469 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2470
2471 test_32j() {
2472         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2473         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2474         trap cleanup_test32_mount EXIT
2475         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2476         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2477         touch $DIR/$tdir/test_file
2478         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2479         cleanup_test32_mount
2480 }
2481 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2482
2483 test_32k() {
2484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2485         rm -fr $DIR/$tdir
2486         trap cleanup_test32_mount EXIT
2487         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2488         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2489         test_mkdir -p $DIR/$tdir/d2
2490         touch $DIR/$tdir/d2/test_file || error
2491         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2492         cleanup_test32_mount
2493 }
2494 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2495
2496 test_32l() {
2497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2498         rm -fr $DIR/$tdir
2499         trap cleanup_test32_mount EXIT
2500         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2501         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2502         test_mkdir -p $DIR/$tdir/d2
2503         touch $DIR/$tdir/d2/test_file
2504         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2505         cleanup_test32_mount
2506 }
2507 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2508
2509 test_32m() {
2510         rm -fr $DIR/d32m
2511         test_mkdir -p $DIR/d32m/tmp
2512         TMP_DIR=$DIR/d32m/tmp
2513         ln -s $DIR $TMP_DIR/symlink11
2514         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2515         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2516         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2517 }
2518 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2519
2520 test_32n() {
2521         rm -fr $DIR/d32n
2522         test_mkdir -p $DIR/d32n/tmp
2523         TMP_DIR=$DIR/d32n/tmp
2524         ln -s $DIR $TMP_DIR/symlink11
2525         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2526         ls -l $DIR/d32n/tmp/symlink11  || error
2527         ls -l $DIR/d32n/symlink01 || error
2528 }
2529 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2530
2531 test_32o() {
2532         touch $DIR/$tfile
2533         test_mkdir -p $DIR/d32o/tmp
2534         TMP_DIR=$DIR/d32o/tmp
2535         ln -s $DIR/$tfile $TMP_DIR/symlink12
2536         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2537         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2538         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2539         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2540         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2541 }
2542 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2543
2544 test_32p() {
2545     log 32p_1
2546         rm -fr $DIR/d32p
2547     log 32p_2
2548         rm -f $DIR/$tfile
2549     log 32p_3
2550         touch $DIR/$tfile
2551     log 32p_4
2552         test_mkdir -p $DIR/d32p/tmp
2553     log 32p_5
2554         TMP_DIR=$DIR/d32p/tmp
2555     log 32p_6
2556         ln -s $DIR/$tfile $TMP_DIR/symlink12
2557     log 32p_7
2558         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2559     log 32p_8
2560         cat $DIR/d32p/tmp/symlink12 || error
2561     log 32p_9
2562         cat $DIR/d32p/symlink02 || error
2563     log 32p_10
2564 }
2565 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2566
2567 cleanup_testdir_mount() {
2568         trap 0
2569         $UMOUNT -d $DIR/$tdir
2570 }
2571
2572 test_32q() {
2573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2574         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2575         trap cleanup_testdir_mount EXIT
2576         test_mkdir -p $DIR/$tdir
2577         touch $DIR/$tdir/under_the_mount
2578         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2579         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2580         cleanup_testdir_mount
2581 }
2582 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2583
2584 test_32r() {
2585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2586         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2587         trap cleanup_testdir_mount EXIT
2588         test_mkdir -p $DIR/$tdir
2589         touch $DIR/$tdir/under_the_mount
2590         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2591         ls $DIR/$tdir | grep -q under_the_mount && error || true
2592         cleanup_testdir_mount
2593 }
2594 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2595
2596 test_33aa() {
2597         rm -f $DIR/$tfile
2598         touch $DIR/$tfile
2599         chmod 444 $DIR/$tfile
2600         chown $RUNAS_ID $DIR/$tfile
2601         log 33_1
2602         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2603         log 33_2
2604 }
2605 run_test 33aa "write file with mode 444 (should return error) ===="
2606
2607 test_33a() {
2608         rm -fr $DIR/d33
2609         test_mkdir -p $DIR/d33
2610         chown $RUNAS_ID $DIR/d33
2611         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2612         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2613                 error "open RDWR" || true
2614 }
2615 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2616
2617 test_33b() {
2618         rm -fr $DIR/d33
2619         test_mkdir -p $DIR/d33
2620         chown $RUNAS_ID $DIR/d33
2621         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2622 }
2623 run_test 33b "test open file with malformed flags (No panic)"
2624
2625 test_33c() {
2626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2627         local ostnum
2628         local ostname
2629         local write_bytes
2630         local all_zeros
2631
2632         remote_ost_nodsh && skip "remote OST with nodsh" && return
2633         all_zeros=:
2634         rm -fr $DIR/d33
2635         test_mkdir -p $DIR/d33
2636         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2637
2638         sync
2639         for ostnum in $(seq $OSTCOUNT); do
2640                 # test-framework's OST numbering is one-based, while Lustre's
2641                 # is zero-based
2642                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2643                 # Parsing llobdstat's output sucks; we could grep the /proc
2644                 # path, but that's likely to not be as portable as using the
2645                 # llobdstat utility.  So we parse lctl output instead.
2646                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2647                         obdfilter/$ostname/stats |
2648                         awk '/^write_bytes/ {print $7}' )
2649                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2650                 if (( ${write_bytes:-0} > 0 ))
2651                 then
2652                         all_zeros=false
2653                         break;
2654                 fi
2655         done
2656
2657         $all_zeros || return 0
2658
2659         # Write four bytes
2660         echo foo > $DIR/d33/bar
2661         # Really write them
2662         sync
2663
2664         # Total up write_bytes after writing.  We'd better find non-zeros.
2665         for ostnum in $(seq $OSTCOUNT); do
2666                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2667                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2668                         obdfilter/$ostname/stats |
2669                         awk '/^write_bytes/ {print $7}' )
2670                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2671                 if (( ${write_bytes:-0} > 0 ))
2672                 then
2673                         all_zeros=false
2674                         break;
2675                 fi
2676         done
2677
2678         if $all_zeros
2679         then
2680                 for ostnum in $(seq $OSTCOUNT); do
2681                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2682                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2683                         do_facet ost$ostnum lctl get_param -n \
2684                                 obdfilter/$ostname/stats
2685                 done
2686                 error "OST not keeping write_bytes stats (b22312)"
2687         fi
2688 }
2689 run_test 33c "test llobdstat and write_bytes"
2690
2691 test_33d() {
2692         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2693         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2694         local MDTIDX=1
2695         local remote_dir=$DIR/$tdir/remote_dir
2696
2697         mkdir -p $DIR/$tdir
2698         $LFS mkdir -i $MDTIDX $remote_dir ||
2699                 error "create remote directory failed"
2700
2701         touch $remote_dir/$tfile
2702         chmod 444 $remote_dir/$tfile
2703         chown $RUNAS_ID $remote_dir/$tfile
2704
2705         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2706
2707         chown $RUNAS_ID $remote_dir
2708         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2709                                         error "create" || true
2710         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2711                                     error "open RDWR" || true
2712         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2713                                     error "create" || true
2714 }
2715 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2716
2717 test_33e() {
2718         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2719
2720         mkdir $DIR/$tdir
2721
2722         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2723         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2724         mkdir $DIR/$tdir/local_dir
2725
2726         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2727         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2728         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2729
2730         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2731                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2732
2733         rmdir $DIR/$tdir/* || error "rmdir failed"
2734
2735         umask 777
2736         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2737         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2738         mkdir $DIR/$tdir/local_dir
2739
2740         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2741         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2742         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2743
2744         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2745                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2746
2747         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2748
2749         umask 000
2750         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2751         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2752         mkdir $DIR/$tdir/local_dir
2753
2754         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2755         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2756         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2757
2758         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2759                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2760 }
2761 run_test 33e "mkdir and striped directory should have same mode"
2762
2763 cleanup_33f() {
2764         trap 0
2765         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2766 }
2767
2768 test_33f() {
2769         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2770
2771         mkdir $DIR/$tdir
2772         chmod go+rwx $DIR/$tdir
2773         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2774         trap cleanup_33f EXIT
2775
2776         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2777                 error "cannot create striped directory"
2778
2779         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2780                 error "cannot create files in striped directory"
2781
2782         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2783                 error "cannot remove files in striped directory"
2784
2785         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2786                 error "cannot remove striped directory"
2787
2788         cleanup_33f
2789 }
2790 run_test 33f "nonroot user can create, access, and remove a striped directory"
2791
2792 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2793 test_34a() {
2794         rm -f $DIR/f34
2795         $MCREATE $DIR/f34 || error
2796         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2797         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2798         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2799         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2800 }
2801 run_test 34a "truncate file that has not been opened ==========="
2802
2803 test_34b() {
2804         [ ! -f $DIR/f34 ] && test_34a
2805         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2806         $OPENFILE -f O_RDONLY $DIR/f34
2807         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2808         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2809 }
2810 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2811
2812 test_34c() {
2813         [ ! -f $DIR/f34 ] && test_34a
2814         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2815         $OPENFILE -f O_RDWR $DIR/f34
2816         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2817         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2818 }
2819 run_test 34c "O_RDWR opening file-with-size works =============="
2820
2821 test_34d() {
2822         [ ! -f $DIR/f34 ] && test_34a
2823         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2824         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2825         rm $DIR/f34
2826 }
2827 run_test 34d "write to sparse file ============================="
2828
2829 test_34e() {
2830         rm -f $DIR/f34e
2831         $MCREATE $DIR/f34e || error
2832         $TRUNCATE $DIR/f34e 1000 || error
2833         $CHECKSTAT -s 1000 $DIR/f34e || error
2834         $OPENFILE -f O_RDWR $DIR/f34e
2835         $CHECKSTAT -s 1000 $DIR/f34e || error
2836 }
2837 run_test 34e "create objects, some with size and some without =="
2838
2839 test_34f() { # bug 6242, 6243
2840         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2841         SIZE34F=48000
2842         rm -f $DIR/f34f
2843         $MCREATE $DIR/f34f || error
2844         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2845         dd if=$DIR/f34f of=$TMP/f34f
2846         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2847         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2848         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2849         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2850         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2851 }
2852 run_test 34f "read from a file with no objects until EOF ======="
2853
2854 test_34g() {
2855         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2856         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2857         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2858         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2859         cancel_lru_locks osc
2860         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2861                 error "wrong size after lock cancel"
2862
2863         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2864         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2865                 error "expanding truncate failed"
2866         cancel_lru_locks osc
2867         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2868                 error "wrong expanded size after lock cancel"
2869 }
2870 run_test 34g "truncate long file ==============================="
2871
2872 test_34h() {
2873         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2874         local gid=10
2875         local sz=1000
2876
2877         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2878         sync # Flush the cache so that multiop below does not block on cache
2879              # flush when getting the group lock
2880         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2881         MULTIPID=$!
2882
2883         # Since just timed wait is not good enough, let's do a sync write
2884         # that way we are sure enough time for a roundtrip + processing
2885         # passed + 2 seconds of extra margin.
2886         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2887         rm $DIR/${tfile}-1
2888         sleep 2
2889
2890         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2891                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2892                 kill -9 $MULTIPID
2893         fi
2894         wait $MULTIPID
2895         local nsz=`stat -c %s $DIR/$tfile`
2896         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2897 }
2898 run_test 34h "ftruncate file under grouplock should not block"
2899
2900 test_35a() {
2901         cp /bin/sh $DIR/f35a
2902         chmod 444 $DIR/f35a
2903         chown $RUNAS_ID $DIR/f35a
2904         $RUNAS $DIR/f35a && error || true
2905         rm $DIR/f35a
2906 }
2907 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2908
2909 test_36a() {
2910         rm -f $DIR/f36
2911         utime $DIR/f36 || error
2912 }
2913 run_test 36a "MDS utime check (mknod, utime) ==================="
2914
2915 test_36b() {
2916         echo "" > $DIR/f36
2917         utime $DIR/f36 || error
2918 }
2919 run_test 36b "OST utime check (open, utime) ===================="
2920
2921 test_36c() {
2922         rm -f $DIR/d36/f36
2923         test_mkdir $DIR/d36
2924         chown $RUNAS_ID $DIR/d36
2925         $RUNAS utime $DIR/d36/f36 || error
2926 }
2927 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2928
2929 test_36d() {
2930         [ ! -d $DIR/d36 ] && test_36c
2931         echo "" > $DIR/d36/f36
2932         $RUNAS utime $DIR/d36/f36 || error
2933 }
2934 run_test 36d "non-root OST utime check (open, utime) ==========="
2935
2936 test_36e() {
2937         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2938         test_mkdir -p $DIR/$tdir
2939         touch $DIR/$tdir/$tfile
2940         $RUNAS utime $DIR/$tdir/$tfile && \
2941                 error "utime worked, expected failure" || true
2942 }
2943 run_test 36e "utime on non-owned file (should return error) ===="
2944
2945 subr_36fh() {
2946         local fl="$1"
2947         local LANG_SAVE=$LANG
2948         local LC_LANG_SAVE=$LC_LANG
2949         export LANG=C LC_LANG=C # for date language
2950
2951         DATESTR="Dec 20  2000"
2952         test_mkdir -p $DIR/$tdir
2953         lctl set_param fail_loc=$fl
2954         date; date +%s
2955         cp /etc/hosts $DIR/$tdir/$tfile
2956         sync & # write RPC generated with "current" inode timestamp, but delayed
2957         sleep 1
2958         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2959         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2960         cancel_lru_locks osc
2961         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2962         date; date +%s
2963         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2964                 echo "BEFORE: $LS_BEFORE" && \
2965                 echo "AFTER : $LS_AFTER" && \
2966                 echo "WANT  : $DATESTR" && \
2967                 error "$DIR/$tdir/$tfile timestamps changed" || true
2968
2969         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2970 }
2971
2972 test_36f() {
2973         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2974         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2975         subr_36fh "0x80000214"
2976 }
2977 run_test 36f "utime on file racing with OST BRW write =========="
2978
2979 test_36g() {
2980         remote_ost_nodsh && skip "remote OST with nodsh" && return
2981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2982         local fmd_max_age
2983         local fmd_before
2984         local fmd_after
2985
2986         test_mkdir -p $DIR/$tdir
2987         fmd_max_age=$(do_facet ost1 \
2988                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2989                 head -n 1")
2990
2991         fmd_before=$(do_facet ost1 \
2992                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2993         touch $DIR/$tdir/$tfile
2994         sleep $((fmd_max_age + 12))
2995         fmd_after=$(do_facet ost1 \
2996                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2997
2998         echo "fmd_before: $fmd_before"
2999         echo "fmd_after: $fmd_after"
3000         [[ $fmd_after -gt $fmd_before ]] &&
3001                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3002                 error "fmd didn't expire after ping" || true
3003 }
3004 run_test 36g "filter mod data cache expiry ====================="
3005
3006 test_36h() {
3007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3008         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3009         subr_36fh "0x80000227"
3010 }
3011 run_test 36h "utime on file racing with OST BRW write =========="
3012
3013 test_36i() {
3014         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3015
3016         mkdir $DIR/$tdir
3017         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3018
3019         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3020         local new_mtime=$((mtime + 200))
3021
3022         #change Modify time of striped dir
3023         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3024                         error "change mtime failed"
3025
3026         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3027
3028         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3029 }
3030 run_test 36i "change mtime on striped directory"
3031
3032 # test_37 - duplicate with tests 32q 32r
3033
3034 test_38() {
3035         local file=$DIR/$tfile
3036         touch $file
3037         openfile -f O_DIRECTORY $file
3038         local RC=$?
3039         local ENOTDIR=20
3040         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3041         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3042 }
3043 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3044
3045 test_39() {
3046         touch $DIR/$tfile
3047         touch $DIR/${tfile}2
3048 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3049 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3050 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3051         sleep 2
3052         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3053         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3054                 echo "mtime"
3055                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3056                 echo "atime"
3057                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3058                 echo "ctime"
3059                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3060                 error "O_TRUNC didn't change timestamps"
3061         fi
3062 }
3063 run_test 39 "mtime changed on create ==========================="
3064
3065 test_39b() {
3066         test_mkdir -p -c1 $DIR/$tdir
3067         cp -p /etc/passwd $DIR/$tdir/fopen
3068         cp -p /etc/passwd $DIR/$tdir/flink
3069         cp -p /etc/passwd $DIR/$tdir/funlink
3070         cp -p /etc/passwd $DIR/$tdir/frename
3071         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3072
3073         sleep 1
3074         echo "aaaaaa" >> $DIR/$tdir/fopen
3075         echo "aaaaaa" >> $DIR/$tdir/flink
3076         echo "aaaaaa" >> $DIR/$tdir/funlink
3077         echo "aaaaaa" >> $DIR/$tdir/frename
3078
3079         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3080         local link_new=`stat -c %Y $DIR/$tdir/flink`
3081         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3082         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3083
3084         cat $DIR/$tdir/fopen > /dev/null
3085         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3086         rm -f $DIR/$tdir/funlink2
3087         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3088
3089         for (( i=0; i < 2; i++ )) ; do
3090                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3091                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3092                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3093                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3094
3095                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3096                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3097                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3098                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3099
3100                 cancel_lru_locks osc
3101                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3102         done
3103 }
3104 run_test 39b "mtime change on open, link, unlink, rename  ======"
3105
3106 # this should be set to past
3107 TEST_39_MTIME=`date -d "1 year ago" +%s`
3108
3109 # bug 11063
3110 test_39c() {
3111         touch $DIR1/$tfile
3112         sleep 2
3113         local mtime0=`stat -c %Y $DIR1/$tfile`
3114
3115         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3116         local mtime1=`stat -c %Y $DIR1/$tfile`
3117         [ "$mtime1" = $TEST_39_MTIME ] || \
3118                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3119
3120         local d1=`date +%s`
3121         echo hello >> $DIR1/$tfile
3122         local d2=`date +%s`
3123         local mtime2=`stat -c %Y $DIR1/$tfile`
3124         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3125                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3126
3127         mv $DIR1/$tfile $DIR1/$tfile-1
3128
3129         for (( i=0; i < 2; i++ )) ; do
3130                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3131                 [ "$mtime2" = "$mtime3" ] || \
3132                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3133
3134                 cancel_lru_locks osc
3135                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3136         done
3137 }
3138 run_test 39c "mtime change on rename ==========================="
3139
3140 # bug 21114
3141 test_39d() {
3142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3143         touch $DIR1/$tfile
3144
3145         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3146
3147         for (( i=0; i < 2; i++ )) ; do
3148                 local mtime=`stat -c %Y $DIR1/$tfile`
3149                 [ $mtime = $TEST_39_MTIME ] || \
3150                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3151
3152                 cancel_lru_locks osc
3153                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3154         done
3155 }
3156 run_test 39d "create, utime, stat =============================="
3157
3158 # bug 21114
3159 test_39e() {
3160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3161         touch $DIR1/$tfile
3162         local mtime1=`stat -c %Y $DIR1/$tfile`
3163
3164         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3165
3166         for (( i=0; i < 2; i++ )) ; do
3167                 local mtime2=`stat -c %Y $DIR1/$tfile`
3168                 [ $mtime2 = $TEST_39_MTIME ] || \
3169                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3170
3171                 cancel_lru_locks osc
3172                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3173         done
3174 }
3175 run_test 39e "create, stat, utime, stat ========================"
3176
3177 # bug 21114
3178 test_39f() {
3179         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3180         touch $DIR1/$tfile
3181         mtime1=`stat -c %Y $DIR1/$tfile`
3182
3183         sleep 2
3184         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3185
3186         for (( i=0; i < 2; i++ )) ; do
3187                 local mtime2=`stat -c %Y $DIR1/$tfile`
3188                 [ $mtime2 = $TEST_39_MTIME ] || \
3189                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3190
3191                 cancel_lru_locks osc
3192                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3193         done
3194 }
3195 run_test 39f "create, stat, sleep, utime, stat ================="
3196
3197 # bug 11063
3198 test_39g() {
3199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3200         echo hello >> $DIR1/$tfile
3201         local mtime1=`stat -c %Y $DIR1/$tfile`
3202
3203         sleep 2
3204         chmod o+r $DIR1/$tfile
3205
3206         for (( i=0; i < 2; i++ )) ; do
3207                 local mtime2=`stat -c %Y $DIR1/$tfile`
3208                 [ "$mtime1" = "$mtime2" ] || \
3209                         error "lost mtime: $mtime2, should be $mtime1"
3210
3211                 cancel_lru_locks osc
3212                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3213         done
3214 }
3215 run_test 39g "write, chmod, stat ==============================="
3216
3217 # bug 11063
3218 test_39h() {
3219         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3220         touch $DIR1/$tfile
3221         sleep 1
3222
3223         local d1=`date`
3224         echo hello >> $DIR1/$tfile
3225         local mtime1=`stat -c %Y $DIR1/$tfile`
3226
3227         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3228         local d2=`date`
3229         if [ "$d1" != "$d2" ]; then
3230                 echo "write and touch not within one second"
3231         else
3232                 for (( i=0; i < 2; i++ )) ; do
3233                         local mtime2=`stat -c %Y $DIR1/$tfile`
3234                         [ "$mtime2" = $TEST_39_MTIME ] || \
3235                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3236
3237                         cancel_lru_locks osc
3238                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3239                 done
3240         fi
3241 }
3242 run_test 39h "write, utime within one second, stat ============="
3243
3244 test_39i() {
3245         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3246         touch $DIR1/$tfile
3247         sleep 1
3248
3249         echo hello >> $DIR1/$tfile
3250         local mtime1=`stat -c %Y $DIR1/$tfile`
3251
3252         mv $DIR1/$tfile $DIR1/$tfile-1
3253
3254         for (( i=0; i < 2; i++ )) ; do
3255                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3256
3257                 [ "$mtime1" = "$mtime2" ] || \
3258                         error "lost mtime: $mtime2, should be $mtime1"
3259
3260                 cancel_lru_locks osc
3261                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3262         done
3263 }
3264 run_test 39i "write, rename, stat =============================="
3265
3266 test_39j() {
3267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3268         start_full_debug_logging
3269         touch $DIR1/$tfile
3270         sleep 1
3271
3272         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3273         lctl set_param fail_loc=0x80000412
3274         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3275                 error "multiop failed"
3276         local multipid=$!
3277         local mtime1=`stat -c %Y $DIR1/$tfile`
3278
3279         mv $DIR1/$tfile $DIR1/$tfile-1
3280
3281         kill -USR1 $multipid
3282         wait $multipid || error "multiop close failed"
3283
3284         for (( i=0; i < 2; i++ )) ; do
3285                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3286                 [ "$mtime1" = "$mtime2" ] ||
3287                         error "mtime is lost on close: $mtime2, " \
3288                               "should be $mtime1"
3289
3290                 cancel_lru_locks osc
3291                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3292         done
3293         lctl set_param fail_loc=0
3294         stop_full_debug_logging
3295 }
3296 run_test 39j "write, rename, close, stat ======================="
3297
3298 test_39k() {
3299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3300         touch $DIR1/$tfile
3301         sleep 1
3302
3303         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3304         local multipid=$!
3305         local mtime1=`stat -c %Y $DIR1/$tfile`
3306
3307         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3308
3309         kill -USR1 $multipid
3310         wait $multipid || error "multiop close failed"
3311
3312         for (( i=0; i < 2; i++ )) ; do
3313                 local mtime2=`stat -c %Y $DIR1/$tfile`
3314
3315                 [ "$mtime2" = $TEST_39_MTIME ] || \
3316                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3317
3318                 cancel_lru_locks osc
3319                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3320         done
3321 }
3322 run_test 39k "write, utime, close, stat ========================"
3323
3324 # this should be set to future
3325 TEST_39_ATIME=`date -d "1 year" +%s`
3326
3327 test_39l() {
3328         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3329         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3330         local atime_diff=$(do_facet $SINGLEMDS \
3331                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3332         rm -rf $DIR/$tdir
3333         mkdir -p $DIR/$tdir
3334
3335         # test setting directory atime to future
3336         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3337         local atime=$(stat -c %X $DIR/$tdir)
3338         [ "$atime" = $TEST_39_ATIME ] || \
3339                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3340
3341         # test setting directory atime from future to now
3342         local d1=$(date +%s)
3343         ls $DIR/$tdir
3344         local d2=$(date +%s)
3345
3346         cancel_lru_locks mdc
3347         atime=$(stat -c %X $DIR/$tdir)
3348         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3349                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3350
3351         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3352         sleep 3
3353
3354         # test setting directory atime when now > dir atime + atime_diff
3355         d1=$(date +%s)
3356         ls $DIR/$tdir
3357         d2=$(date +%s)
3358         cancel_lru_locks mdc
3359         atime=$(stat -c %X $DIR/$tdir)
3360         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3361                 error "atime is not updated  : $atime, should be $d2"
3362
3363         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3364         sleep 3
3365
3366         # test not setting directory atime when now < dir atime + atime_diff
3367         ls $DIR/$tdir
3368         cancel_lru_locks mdc
3369         atime=$(stat -c %X $DIR/$tdir)
3370         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3371                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3372
3373         do_facet $SINGLEMDS \
3374                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3375 }
3376 run_test 39l "directory atime update ==========================="
3377
3378 test_39m() {
3379         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3380         touch $DIR1/$tfile
3381         sleep 2
3382         local far_past_mtime=$(date -d "May 29 1953" +%s)
3383         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3384
3385         touch -m -d @$far_past_mtime $DIR1/$tfile
3386         touch -a -d @$far_past_atime $DIR1/$tfile
3387
3388         for (( i=0; i < 2; i++ )) ; do
3389                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3390                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3391                         error "atime or mtime set incorrectly"
3392
3393                 cancel_lru_locks osc
3394                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3395         done
3396 }
3397 run_test 39m "test atime and mtime before 1970"
3398
3399 test_39n() { # LU-3832
3400         local atime_diff=$(do_facet $SINGLEMDS \
3401                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3402         local atime0
3403         local atime1
3404         local atime2
3405
3406         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3407
3408         rm -rf $DIR/$tfile
3409         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3410         atime0=$(stat -c %X $DIR/$tfile)
3411
3412         sleep 5
3413         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3414         atime1=$(stat -c %X $DIR/$tfile)
3415
3416         sleep 5
3417         cancel_lru_locks mdc
3418         cancel_lru_locks osc
3419         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3420         atime2=$(stat -c %X $DIR/$tfile)
3421
3422         do_facet $SINGLEMDS \
3423                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3424
3425         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3426         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3427 }
3428 run_test 39n "check that O_NOATIME is honored"
3429
3430 test_39o() {
3431         TESTDIR=$DIR/$tdir/$tfile
3432         [ -e $TESTDIR ] && rm -rf $TESTDIR
3433         test_mkdir -p $TESTDIR
3434         cd $TESTDIR
3435         links1=2
3436         ls
3437         mkdir a b
3438         ls
3439         links2=$(stat -c %h .)
3440         [ $(($links1 + 2)) != $links2 ] &&
3441                 error "wrong links count $(($links1 + 2)) != $links2"
3442         rmdir b
3443         links3=$(stat -c %h .)
3444         [ $(($links1 + 1)) != $links3 ] &&
3445                 error "wrong links count $links1 != $links3"
3446         return 0
3447 }
3448 run_test 39o "directory cached attributes updated after create ========"
3449
3450 test_39p() {
3451         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3452         local MDTIDX=1
3453         TESTDIR=$DIR/$tdir/$tfile
3454         [ -e $TESTDIR ] && rm -rf $TESTDIR
3455         mkdir -p $TESTDIR
3456         cd $TESTDIR
3457         links1=2
3458         ls
3459         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3460         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3461         ls
3462         links2=$(stat -c %h .)
3463         [ $(($links1 + 2)) != $links2 ] &&
3464                 error "wrong links count $(($links1 + 2)) != $links2"
3465         rmdir remote_dir2
3466         links3=$(stat -c %h .)
3467         [ $(($links1 + 1)) != $links3 ] &&
3468                 error "wrong links count $links1 != $links3"
3469         return 0
3470 }
3471 run_test 39p "remote directory cached attributes updated after create ========"
3472
3473
3474 test_40() {
3475         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3476         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3477                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3478         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3479                 error "$tfile is not 4096 bytes in size"
3480 }
3481 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3482
3483 test_41() {
3484         # bug 1553
3485         small_write $DIR/f41 18
3486 }
3487 run_test 41 "test small file write + fstat ====================="
3488
3489 count_ost_writes() {
3490         lctl get_param -n osc.*.stats |
3491                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3492                         END { printf("%0.0f", writes) }'
3493 }
3494
3495 # decent default
3496 WRITEBACK_SAVE=500
3497 DIRTY_RATIO_SAVE=40
3498 MAX_DIRTY_RATIO=50
3499 BG_DIRTY_RATIO_SAVE=10
3500 MAX_BG_DIRTY_RATIO=25
3501
3502 start_writeback() {
3503         trap 0
3504         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3505         # dirty_ratio, dirty_background_ratio
3506         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3507                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3508                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3509                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3510         else
3511                 # if file not here, we are a 2.4 kernel
3512                 kill -CONT `pidof kupdated`
3513         fi
3514 }
3515
3516 stop_writeback() {
3517         # setup the trap first, so someone cannot exit the test at the
3518         # exact wrong time and mess up a machine
3519         trap start_writeback EXIT
3520         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3521         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3522                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3523                 sysctl -w vm.dirty_writeback_centisecs=0
3524                 sysctl -w vm.dirty_writeback_centisecs=0
3525                 # save and increase /proc/sys/vm/dirty_ratio
3526                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3527                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3528                 # save and increase /proc/sys/vm/dirty_background_ratio
3529                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3530                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3531         else
3532                 # if file not here, we are a 2.4 kernel
3533                 kill -STOP `pidof kupdated`
3534         fi
3535 }
3536
3537 # ensure that all stripes have some grant before we test client-side cache
3538 setup_test42() {
3539         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3540                 dd if=/dev/zero of=$i bs=4k count=1
3541                 rm $i
3542         done
3543 }
3544
3545 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3546 # file truncation, and file removal.
3547 test_42a() {
3548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3549         setup_test42
3550         cancel_lru_locks osc
3551         stop_writeback
3552         sync; sleep 1; sync # just to be safe
3553         BEFOREWRITES=`count_ost_writes`
3554         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3555         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3556         AFTERWRITES=`count_ost_writes`
3557         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3558                 error "$BEFOREWRITES < $AFTERWRITES"
3559         start_writeback
3560 }
3561 run_test 42a "ensure that we don't flush on close =============="
3562
3563 test_42b() {
3564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3565         setup_test42
3566         cancel_lru_locks osc
3567         stop_writeback
3568         sync
3569         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3570         BEFOREWRITES=$(count_ost_writes)
3571         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3572         AFTERWRITES=$(count_ost_writes)
3573         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3574                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3575         fi
3576         BEFOREWRITES=$(count_ost_writes)
3577         sync || error "sync: $?"
3578         AFTERWRITES=$(count_ost_writes)
3579         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3580                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3581         fi
3582         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3583         start_writeback
3584         return 0
3585 }
3586 run_test 42b "test destroy of file with cached dirty data ======"
3587
3588 # if these tests just want to test the effect of truncation,
3589 # they have to be very careful.  consider:
3590 # - the first open gets a {0,EOF}PR lock
3591 # - the first write conflicts and gets a {0, count-1}PW
3592 # - the rest of the writes are under {count,EOF}PW
3593 # - the open for truncate tries to match a {0,EOF}PR
3594 #   for the filesize and cancels the PWs.
3595 # any number of fixes (don't get {0,EOF} on open, match
3596 # composite locks, do smarter file size management) fix
3597 # this, but for now we want these tests to verify that
3598 # the cancellation with truncate intent works, so we
3599 # start the file with a full-file pw lock to match against
3600 # until the truncate.
3601 trunc_test() {
3602         test=$1
3603         file=$DIR/$test
3604         offset=$2
3605         cancel_lru_locks osc
3606         stop_writeback
3607         # prime the file with 0,EOF PW to match
3608         touch $file
3609         $TRUNCATE $file 0
3610         sync; sync
3611         # now the real test..
3612         dd if=/dev/zero of=$file bs=1024 count=100
3613         BEFOREWRITES=`count_ost_writes`
3614         $TRUNCATE $file $offset
3615         cancel_lru_locks osc
3616         AFTERWRITES=`count_ost_writes`
3617         start_writeback
3618 }
3619
3620 test_42c() {
3621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3622         trunc_test 42c 1024
3623         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3624             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3625         rm $file
3626 }
3627 run_test 42c "test partial truncate of file with cached dirty data"
3628
3629 test_42d() {
3630         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3631         trunc_test 42d 0
3632         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3633             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3634         rm $file
3635 }
3636 run_test 42d "test complete truncate of file with cached dirty data"
3637
3638 test_42e() { # bug22074
3639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3640         local TDIR=$DIR/${tdir}e
3641         local pagesz=$(page_size)
3642         local pages=16 # hardcoded 16 pages, don't change it.
3643         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3644         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3645         local max_dirty_mb
3646         local warmup_files
3647
3648         test_mkdir -p $DIR/${tdir}e
3649         $SETSTRIPE -c 1 $TDIR
3650         createmany -o $TDIR/f $files
3651
3652         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3653
3654         # we assume that with $OSTCOUNT files, at least one of them will
3655         # be allocated on OST0.
3656         warmup_files=$((OSTCOUNT * max_dirty_mb))
3657         createmany -o $TDIR/w $warmup_files
3658
3659         # write a large amount of data into one file and sync, to get good
3660         # avail_grant number from OST.
3661         for ((i=0; i<$warmup_files; i++)); do
3662                 idx=$($GETSTRIPE -i $TDIR/w$i)
3663                 [ $idx -ne 0 ] && continue
3664                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3665                 break
3666         done
3667         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3668         sync
3669         $LCTL get_param $proc_osc0/cur_dirty_bytes
3670         $LCTL get_param $proc_osc0/cur_grant_bytes
3671
3672         # create as much dirty pages as we can while not to trigger the actual
3673         # RPCs directly. but depends on the env, VFS may trigger flush during this
3674         # period, hopefully we are good.
3675         for ((i=0; i<$warmup_files; i++)); do
3676                 idx=$($GETSTRIPE -i $TDIR/w$i)
3677                 [ $idx -ne 0 ] && continue
3678                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3679         done
3680         $LCTL get_param $proc_osc0/cur_dirty_bytes
3681         $LCTL get_param $proc_osc0/cur_grant_bytes
3682
3683         # perform the real test
3684         $LCTL set_param $proc_osc0/rpc_stats 0
3685         for ((;i<$files; i++)); do
3686                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3687                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3688         done
3689         sync
3690         $LCTL get_param $proc_osc0/rpc_stats
3691
3692         local percent=0
3693         local have_ppr=false
3694         $LCTL get_param $proc_osc0/rpc_stats |
3695                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3696                         # skip lines until we are at the RPC histogram data
3697                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3698                         $have_ppr || continue
3699
3700                         # we only want the percent stat for < 16 pages
3701                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3702
3703                         percent=$((percent + WPCT))
3704                         if [[ $percent -gt 15 ]]; then
3705                                 error "less than 16-pages write RPCs" \
3706                                       "$percent% > 15%"
3707                                 break
3708                         fi
3709                 done
3710         rm -rf $TDIR
3711 }
3712 run_test 42e "verify sub-RPC writes are not done synchronously"
3713
3714 test_43() {
3715         test_mkdir -p $DIR/$tdir
3716         cp -p /bin/ls $DIR/$tdir/$tfile
3717         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3718         pid=$!
3719         # give multiop a chance to open
3720         sleep 1
3721
3722         $DIR/$tdir/$tfile && error || true
3723         kill -USR1 $pid
3724 }
3725 run_test 43 "execution of file opened for write should return -ETXTBSY"
3726
3727 test_43a() {
3728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3729         test_mkdir -p $DIR/$tdir
3730         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3731                         cp -p multiop $DIR/$tdir/multiop
3732         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3733                         return 1
3734         MULTIOP_PID=$!
3735         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3736         kill -USR1 $MULTIOP_PID || return 2
3737         wait $MULTIOP_PID || return 3
3738         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3739 }
3740 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3741
3742 test_43b() {
3743         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3744         test_mkdir -p $DIR/$tdir
3745         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3746                         cp -p multiop $DIR/$tdir/multiop
3747         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3748                         return 1
3749         MULTIOP_PID=$!
3750         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3751         kill -USR1 $MULTIOP_PID || return 2
3752         wait $MULTIOP_PID || return 3
3753         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3754 }
3755 run_test 43b "truncate of file being executed should return -ETXTBSY"
3756
3757 test_43c() {
3758         local testdir="$DIR/$tdir"
3759         test_mkdir -p $DIR/$tdir
3760         cp $SHELL $testdir/
3761         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3762                 ( cd $testdir && md5sum -c)
3763 }
3764 run_test 43c "md5sum of copy into lustre========================"
3765
3766 test_44() {
3767         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3768         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3769         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3770 }
3771 run_test 44 "zero length read from a sparse stripe ============="
3772
3773 test_44a() {
3774         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3775                 awk '{ print $2 }')
3776         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3777         [[ $nstripe -gt $OSTCOUNT ]] &&
3778             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3779             return
3780         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3781                 awk '{ print $2 }')
3782         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3783                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3784                         awk '{ print $2 }')
3785         fi
3786
3787         OFFSETS="0 $((stride/2)) $((stride-1))"
3788         for offset in $OFFSETS; do
3789                 for i in $(seq 0 $((nstripe-1))); do
3790                         local GLOBALOFFSETS=""
3791                         # size in Bytes
3792                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3793                         local myfn=$DIR/d44a-$size
3794                         echo "--------writing $myfn at $size"
3795                         ll_sparseness_write $myfn $size ||
3796                                 error "ll_sparseness_write"
3797                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3798                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3799                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3800
3801                         for j in $(seq 0 $((nstripe-1))); do
3802                                 # size in Bytes
3803                                 size=$((((j + $nstripe )*$stride + $offset)))
3804                                 ll_sparseness_write $myfn $size ||
3805                                         error "ll_sparseness_write"
3806                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3807                         done
3808                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3809                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3810                         rm -f $myfn
3811                 done
3812         done
3813 }
3814 run_test 44a "test sparse pwrite ==============================="
3815
3816 dirty_osc_total() {
3817         tot=0
3818         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3819                 tot=$(($tot + $d))
3820         done
3821         echo $tot
3822 }
3823 do_dirty_record() {
3824         before=`dirty_osc_total`
3825         echo executing "\"$*\""
3826         eval $*
3827         after=`dirty_osc_total`
3828         echo before $before, after $after
3829 }
3830 test_45() {
3831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3832         f="$DIR/f45"
3833         # Obtain grants from OST if it supports it
3834         echo blah > ${f}_grant
3835         stop_writeback
3836         sync
3837         do_dirty_record "echo blah > $f"
3838         [[ $before -eq $after ]] && error "write wasn't cached"
3839         do_dirty_record "> $f"
3840         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3841         do_dirty_record "echo blah > $f"
3842         [[ $before -eq $after ]] && error "write wasn't cached"
3843         do_dirty_record "sync"
3844         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3845         do_dirty_record "echo blah > $f"
3846         [[ $before -eq $after ]] && error "write wasn't cached"
3847         do_dirty_record "cancel_lru_locks osc"
3848         [[ $before -gt $after ]] ||
3849                 error "lock cancellation didn't lower dirty count"
3850         start_writeback
3851 }
3852 run_test 45 "osc io page accounting ============================"
3853
3854 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3855 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3856 # objects offset and an assert hit when an rpc was built with 1023's mapped
3857 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3858 test_46() {
3859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3860         f="$DIR/f46"
3861         stop_writeback
3862         sync
3863         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3864         sync
3865         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3866         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3867         sync
3868         start_writeback
3869 }
3870 run_test 46 "dirtying a previously written page ================"
3871
3872 # test_47 is removed "Device nodes check" is moved to test_28
3873
3874 test_48a() { # bug 2399
3875         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3876         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3877                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3878                 return
3879         check_kernel_version 34 || return 0
3880         test_mkdir -p $DIR/$tdir
3881         cd $DIR/$tdir
3882         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3883         test_mkdir $DIR/$tdir || error "recreate directory failed"
3884         touch foo || error "'touch foo' failed after recreating cwd"
3885         test_mkdir $DIR/$tdir/bar ||
3886                      error "'mkdir foo' failed after recreating cwd"
3887         if check_kernel_version 44; then
3888                 touch .foo || error "'touch .foo' failed after recreating cwd"
3889                 test_mkdir $DIR/$tdir/.bar ||
3890                               error "'mkdir .foo' failed after recreating cwd"
3891         fi
3892         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3893         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3894         cd . || error "'cd .' failed after recreating cwd"
3895         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3896         rmdir . && error "'rmdir .' worked after recreating cwd"
3897         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3898         cd .. || error "'cd ..' failed after recreating cwd"
3899 }
3900 run_test 48a "Access renamed working dir (should return errors)="
3901
3902 test_48b() { # bug 2399
3903         check_kernel_version 34 || return 0
3904         rm -rf $DIR/$tdir
3905         test_mkdir -p $DIR/$tdir
3906         cd $DIR/$tdir
3907         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3908         touch foo && error "'touch foo' worked after removing cwd"
3909         test_mkdir $DIR/$tdir/foo &&
3910                      error "'mkdir foo' worked after removing cwd"
3911         if check_kernel_version 44; then
3912                 touch .foo && error "'touch .foo' worked after removing cwd"
3913                 test_mkdir $DIR/$tdir/.foo &&
3914                               error "'mkdir .foo' worked after removing cwd"
3915         fi
3916         ls . > /dev/null && error "'ls .' worked after removing cwd"
3917         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3918         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3919         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3920         rmdir . && error "'rmdir .' worked after removing cwd"
3921         ln -s . foo && error "'ln -s .' worked after removing cwd"
3922         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3923 }
3924 run_test 48b "Access removed working dir (should return errors)="
3925
3926 test_48c() { # bug 2350
3927         check_kernel_version 36 || return 0
3928         #lctl set_param debug=-1
3929         #set -vx
3930         rm -rf $DIR/$tdir
3931         test_mkdir -p $DIR/$tdir/dir
3932         cd $DIR/$tdir/dir
3933         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3934         $TRACE touch foo && error "touch foo worked after removing cwd"
3935         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3936         if check_kernel_version 44; then
3937                 touch .foo && error "touch .foo worked after removing cwd"
3938                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3939         fi
3940         $TRACE ls . && error "'ls .' worked after removing cwd"
3941         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3942         is_patchless || ( $TRACE cd . &&
3943                         error "'cd .' worked after removing cwd" )
3944         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3945         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3946         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3947         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3948 }
3949 run_test 48c "Access removed working subdir (should return errors)"
3950
3951 test_48d() { # bug 2350
3952         check_kernel_version 36 || return 0
3953         #lctl set_param debug=-1
3954         #set -vx
3955         rm -rf $DIR/$tdir
3956         test_mkdir -p $DIR/$tdir/dir
3957         cd $DIR/$tdir/dir
3958         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3959         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3960         $TRACE touch foo && error "'touch foo' worked after removing parent"
3961         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3962         if check_kernel_version 44; then
3963                 touch .foo && error "'touch .foo' worked after removing parent"
3964                 test_mkdir .foo &&
3965                               error "mkdir .foo worked after removing parent"
3966         fi
3967         $TRACE ls . && error "'ls .' worked after removing parent"
3968         $TRACE ls .. && error "'ls ..' worked after removing parent"
3969         is_patchless || ( $TRACE cd . &&
3970                         error "'cd .' worked after recreate parent" )
3971         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3972         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3973         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3974         is_patchless || ( $TRACE cd .. &&
3975                         error "'cd ..' worked after removing parent" || true )
3976 }
3977 run_test 48d "Access removed parent subdir (should return errors)"
3978
3979 test_48e() { # bug 4134
3980         check_kernel_version 41 || return 0
3981         #lctl set_param debug=-1
3982         #set -vx
3983         rm -rf $DIR/$tdir
3984         test_mkdir -p $DIR/$tdir/dir
3985         cd $DIR/$tdir/dir
3986         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3987         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3988         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3989         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3990         # On a buggy kernel addition of "touch foo" after cd .. will
3991         # produce kernel oops in lookup_hash_it
3992         touch ../foo && error "'cd ..' worked after recreate parent"
3993         cd $DIR
3994         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3995 }
3996 run_test 48e "Access to recreated parent subdir (should return errors)"
3997
3998 test_49() { # LU-1030
3999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4000         # get ost1 size - lustre-OST0000
4001         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4002                 awk '{ print $4 }')
4003         # write 800M at maximum
4004         [[ $ost1_size -lt 2 ]] && ost1_size=2
4005         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4006
4007         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4008         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4009         local dd_pid=$!
4010
4011         # change max_pages_per_rpc while writing the file
4012         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4013         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4014         # loop until dd process exits
4015         while ps ax -opid | grep -wq $dd_pid; do
4016                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4017                 sleep $((RANDOM % 5 + 1))
4018         done
4019         # restore original max_pages_per_rpc
4020         $LCTL set_param $osc1_mppc=$orig_mppc
4021         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4022 }
4023 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4024
4025 test_50() {
4026         # bug 1485
4027         test_mkdir $DIR/$tdir
4028         cd $DIR/$tdir
4029         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4030 }
4031 run_test 50 "special situations: /proc symlinks  ==============="
4032
4033 test_51a() {    # was test_51
4034         # bug 1516 - create an empty entry right after ".." then split dir
4035         test_mkdir -c1 $DIR/$tdir
4036         touch $DIR/$tdir/foo
4037         $MCREATE $DIR/$tdir/bar
4038         rm $DIR/$tdir/foo
4039         createmany -m $DIR/$tdir/longfile 201
4040         FNUM=202
4041         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4042                 $MCREATE $DIR/$tdir/longfile$FNUM
4043                 FNUM=$(($FNUM + 1))
4044                 echo -n "+"
4045         done
4046         echo
4047         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4048 }
4049 run_test 51a "special situations: split htree with empty entry =="
4050
4051 export NUMTEST=70000
4052 test_51b() {
4053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4054         local BASE=$DIR/d${base}.${TESTSUITE}
4055
4056         # cleanup the directory
4057         rm -fr $BASE
4058
4059         test_mkdir -p -c1 $BASE
4060
4061         $LFS df
4062         $LFS df -i
4063         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4064         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4065         [[ $numfree -lt 21000 ]] &&
4066                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4067                 return
4068
4069         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4070                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4071
4072         # need to check free space for the directories as well
4073         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4074         numfree=$((blkfree / 4))
4075         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4076                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4077
4078         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4079         {
4080                 $LFS df
4081                 $LFS df -i
4082                 echo "failed" > $BASE/fnum
4083                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4084         }
4085 }
4086 run_test 51b "exceed 64k subdirectory nlink limit"
4087
4088 test_51ba() { # LU-993
4089         local BASE=$DIR/d${base}.${TESTSUITE}
4090         # unlink all but 100 subdirectories, then check it still works
4091         local LEFT=100
4092         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4093
4094         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4095         local DELETE=$((NUMTEST - LEFT))
4096
4097         # continue on to run this test even if 51b didn't finish,
4098         # just to delete the many subdirectories created.
4099         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4100
4101         # for ldiskfs the nlink count should be 1, but this is OSD specific
4102         # and so this is listed for informational purposes only
4103         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4104         unlinkmany -d $BASE/d $DELETE
4105         RC=$?
4106
4107         if [ $RC -ne 0 ]; then
4108                 if [ "$NUMPREV" == "failed" ]; then
4109                         skip "previous setup failed"
4110                         return 0
4111                 else
4112                         error "unlink of first $DELETE subdirs failed"
4113                         return $RC
4114                 fi
4115         fi
4116
4117         echo "nlink between: $(stat -c %h $BASE)"
4118         # trim the first line of ls output
4119         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4120         [ $FOUND -ne $LEFT ] &&
4121                 error "can't find subdirs: found only $FOUND/$LEFT"
4122
4123         unlinkmany -d $BASE/d $DELETE $LEFT ||
4124                 error "unlink of second $LEFT subdirs failed"
4125         # regardless of whether the backing filesystem tracks nlink accurately
4126         # or not, the nlink count shouldn't be more than "." and ".." here
4127         local AFTER=$(stat -c %h $BASE)
4128         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4129                 echo "nlink after: $AFTER"
4130 }
4131 run_test 51ba "verify nlink for many subdirectory cleanup"
4132
4133 test_51d() {
4134         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4135         [[ $OSTCOUNT -lt 3 ]] &&
4136                 skip_env "skipping test with few OSTs" && return
4137         test_mkdir -p $DIR/$tdir
4138         createmany -o $DIR/$tdir/t- 1000
4139         $GETSTRIPE $DIR/$tdir > $TMP/files
4140         for N in $(seq 0 $((OSTCOUNT - 1))); do
4141                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4142                         END { printf("%0.0f", objs) }' $TMP/files)
4143                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4144                         '($1 == '$N') { objs += 1 } \
4145                         END { printf("%0.0f", objs) }')
4146                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4147         done
4148         unlinkmany $DIR/$tdir/t- 1000
4149
4150         NLAST=0
4151         for N in $(seq 1 $((OSTCOUNT - 1))); do
4152                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4153                         error "OST $N has less objects vs OST $NLAST" \
4154                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4155                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4156                         error "OST $N has less objects vs OST $NLAST" \
4157                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4158
4159                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4160                         error "OST $N has less #0 objects vs OST $NLAST" \
4161                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4162                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4163                         error "OST $N has less #0 objects vs OST $NLAST" \
4164                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4165                 NLAST=$N
4166         done
4167 }
4168 run_test 51d "check object distribution ===================="
4169
4170 test_51e() {
4171         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4172                 skip "Only applicable to ldiskfs-based MDTs"
4173                 return
4174         fi
4175
4176         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4177         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4178
4179         touch $DIR/$tdir/d0/foo
4180         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4181                 error "file exceed 65000 nlink limit!"
4182         unlinkmany $DIR/$tdir/d0/f- 65001
4183         return 0
4184 }
4185 run_test 51e "check file nlink limit"
4186
4187 test_52a() {
4188         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4189         test_mkdir -p $DIR/$tdir
4190         touch $DIR/$tdir/foo
4191         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4192         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4193         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4194         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4195         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4196                                         error "link worked"
4197         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4198         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4199         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4200                                                      error "lsattr"
4201         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4202         cp -r $DIR/$tdir /tmp/
4203         rm -fr $DIR/$tdir || error "cleanup rm failed"
4204 }
4205 run_test 52a "append-only flag test (should return errors) ====="
4206
4207 test_52b() {
4208         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4209         test_mkdir -p $DIR/$tdir
4210         touch $DIR/$tdir/foo
4211         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4212         cat test > $DIR/$tdir/foo && error "cat test worked"
4213         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4214         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4215         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4216                                         error "link worked"
4217         echo foo >> $DIR/$tdir/foo && error "echo worked"
4218         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4219         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4220         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4221         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4222                                                         error "lsattr"
4223         chattr -i $DIR/$tdir/foo || error "chattr failed"
4224
4225         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4226 }
4227 run_test 52b "immutable flag test (should return errors) ======="
4228
4229 test_53() {
4230         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4231         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4232         remote_ost_nodsh && skip "remote OST with nodsh" && return
4233
4234         local param
4235         local param_seq
4236         local ostname
4237         local mds_last
4238         local mds_last_seq
4239         local ost_last
4240         local ost_last_seq
4241         local ost_last_id
4242         local ostnum
4243         local node
4244         local found=false
4245         local support_last_seq=true
4246
4247         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4248                 support_last_seq=false
4249
4250         # only test MDT0000
4251         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4252         local value
4253         for value in $(do_facet $SINGLEMDS \
4254                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4255                 param=$(echo ${value[0]} | cut -d "=" -f1)
4256                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4257
4258                 if $support_last_seq; then
4259                         param_seq=$(echo $param |
4260                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4261                         mds_last_seq=$(do_facet $SINGLEMDS \
4262                                        $LCTL get_param -n $param_seq)
4263                 fi
4264                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4265
4266                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4267                 node=$(facet_active_host ost$((ostnum+1)))
4268                 param="obdfilter.$ostname.last_id"
4269                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4270                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4271                         ost_last_id=$ost_last
4272
4273                         if $support_last_seq; then
4274                                 ost_last_id=$(echo $ost_last |
4275                                               awk -F':' '{print $2}' |
4276                                               sed -e "s/^0x//g")
4277                                 ost_last_seq=$(echo $ost_last |
4278                                                awk -F':' '{print $1}')
4279                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4280                         fi
4281
4282                         if [[ $ost_last_id != $mds_last ]]; then
4283                                 error "$ost_last_id != $mds_last"
4284                         else
4285                                 found=true
4286                                 break
4287                         fi
4288                 done
4289         done
4290         $found || error "can not match last_seq/last_id for $mdtosc"
4291         return 0
4292 }
4293 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4294
4295 test_54a() {
4296         $SOCKETSERVER $DIR/socket ||
4297                 error "$SOCKETSERVER $DIR/socket failed: $?"
4298         $SOCKETCLIENT $DIR/socket ||
4299                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4300         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4301 }
4302 run_test 54a "unix domain socket test =========================="
4303
4304 test_54b() {
4305         f="$DIR/f54b"
4306         mknod $f c 1 3
4307         chmod 0666 $f
4308         dd if=/dev/zero of=$f bs=$(page_size) count=1
4309 }
4310 run_test 54b "char device works in lustre ======================"
4311
4312 find_loop_dev() {
4313         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4314         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4315         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4316
4317         for i in $(seq 3 7); do
4318                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4319                 LOOPDEV=$LOOPBASE$i
4320                 LOOPNUM=$i
4321                 break
4322         done
4323 }
4324
4325 cleanup_54c() {
4326         loopdev="$DIR/loop54c"
4327
4328         trap 0
4329         $UMOUNT -d $DIR/$tdir || rc=$?
4330         losetup -d $loopdev || true
4331         losetup -d $LOOPDEV || true
4332         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4333         return $rc
4334 }
4335
4336 test_54c() {
4337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4338         loopdev="$DIR/loop54c"
4339
4340         find_loop_dev
4341         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4342         trap cleanup_54c EXIT
4343         mknod $loopdev b 7 $LOOPNUM
4344         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4345         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4346         losetup $loopdev $DIR/$tfile ||
4347                 error "can't set up $loopdev for $DIR/$tfile"
4348         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4349         test_mkdir -p $DIR/$tdir
4350         mount -t ext2 $loopdev $DIR/$tdir ||
4351                 error "error mounting $loopdev on $DIR/$tdir"
4352         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4353                 error "dd write"
4354         df $DIR/$tdir
4355         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4356                 error "dd read"
4357         cleanup_54c
4358 }
4359 run_test 54c "block device works in lustre ====================="
4360
4361 test_54d() {
4362         f="$DIR/f54d"
4363         string="aaaaaa"
4364         mknod $f p
4365         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4366 }
4367 run_test 54d "fifo device works in lustre ======================"
4368
4369 test_54e() {
4370         check_kernel_version 46 || return 0
4371         f="$DIR/f54e"
4372         string="aaaaaa"
4373         cp -aL /dev/console $f
4374         echo $string > $f || error "echo $string to $f failed"
4375 }
4376 run_test 54e "console/tty device works in lustre ======================"
4377
4378 #The test_55 used to be iopen test and it was removed by bz#24037.
4379 #run_test 55 "check iopen_connect_dentry() ======================"
4380
4381 test_56a() {    # was test_56
4382         rm -rf $DIR/$tdir
4383         $SETSTRIPE -d $DIR
4384         test_mkdir -p $DIR/$tdir/dir
4385         NUMFILES=3
4386         NUMFILESx2=$(($NUMFILES * 2))
4387         for i in $(seq 1 $NUMFILES); do
4388                 touch $DIR/$tdir/file$i
4389                 touch $DIR/$tdir/dir/file$i
4390         done
4391
4392         # test lfs getstripe with --recursive
4393         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4394         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4395                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4396         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4397         [[ $FILENUM -eq $NUMFILES ]] ||
4398                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4399         echo "$GETSTRIPE --recursive passed."
4400
4401         # test lfs getstripe with file instead of dir
4402         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4403         [[ $FILENUM -eq 1 ]] ||
4404                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4405         echo "$GETSTRIPE file1 passed."
4406
4407         #test lfs getstripe with --verbose
4408         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4409                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4410                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4411         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4412                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4413         echo "$GETSTRIPE --verbose passed."
4414
4415         #test lfs getstripe with --obd
4416         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4417                 grep -q "unknown obduuid" ||
4418                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4419
4420         [[ $OSTCOUNT -lt 2 ]] &&
4421                 skip_env "skipping other $GETSTRIPE --obd test" && return
4422
4423         OSTIDX=1
4424         OBDUUID=$(ostuuid_from_index $OSTIDX)
4425         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4426         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4427         [[ $FOUND -eq $FILENUM ]] ||
4428                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4429         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4430                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4431                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4432                 error "$GETSTRIPE --obd: should not show file on other obd"
4433         echo "$GETSTRIPE --obd passed"
4434 }
4435 run_test 56a "check $GETSTRIPE"
4436
4437 NUMFILES=3
4438 NUMDIRS=3
4439 setup_56() {
4440         local LOCAL_NUMFILES="$1"
4441         local LOCAL_NUMDIRS="$2"
4442         local MKDIR_PARAMS="$3"
4443         local DIR_STRIPE_PARAMS="$4"
4444
4445         if [ ! -d "$TDIR" ] ; then
4446                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4447                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4448                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4449                         touch $TDIR/file$i
4450                 done
4451                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4452                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4453                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4454                                 touch $TDIR/dir$i/file$j
4455                         done
4456                 done
4457         fi
4458 }
4459
4460 setup_56_special() {
4461         LOCAL_NUMFILES=$1
4462         LOCAL_NUMDIRS=$2
4463         setup_56 $1 $2
4464         if [ ! -e "$TDIR/loop1b" ] ; then
4465                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4466                         mknod $TDIR/loop${i}b b 7 $i
4467                         mknod $TDIR/null${i}c c 1 3
4468                         ln -s $TDIR/file1 $TDIR/link${i}l
4469                 done
4470                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4471                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4472                         mknod $TDIR/dir$i/null${i}c c 1 3
4473                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4474                 done
4475         fi
4476 }
4477
4478 test_56g() {
4479         $SETSTRIPE -d $DIR
4480
4481         TDIR=$DIR/${tdir}g
4482         setup_56 $NUMFILES $NUMDIRS
4483
4484         EXPECTED=$(($NUMDIRS + 2))
4485         # test lfs find with -name
4486         for i in $(seq 1 $NUMFILES) ; do
4487                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4488                 [ $NUMS -eq $EXPECTED ] ||
4489                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4490                               "found $NUMS, expected $EXPECTED"
4491         done
4492 }
4493 run_test 56g "check lfs find -name ============================="
4494
4495 test_56h() {
4496         $SETSTRIPE -d $DIR
4497
4498         TDIR=$DIR/${tdir}g
4499         setup_56 $NUMFILES $NUMDIRS
4500
4501         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4502         # test lfs find with ! -name
4503         for i in $(seq 1 $NUMFILES) ; do
4504                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4505                 [ $NUMS -eq $EXPECTED ] ||
4506                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4507                               "found $NUMS, expected $EXPECTED"
4508         done
4509 }
4510 run_test 56h "check lfs find ! -name ============================="
4511
4512 test_56i() {
4513        tdir=${tdir}i
4514        test_mkdir -p $DIR/$tdir
4515        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4516        CMD="$LFIND -ost $UUID $DIR/$tdir"
4517        OUT=$($CMD)
4518        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4519 }
4520 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4521
4522 test_56j() {
4523         TDIR=$DIR/${tdir}g
4524         setup_56_special $NUMFILES $NUMDIRS
4525
4526         EXPECTED=$((NUMDIRS + 1))
4527         CMD="$LFIND -type d $TDIR"
4528         NUMS=$($CMD | wc -l)
4529         [ $NUMS -eq $EXPECTED ] ||
4530                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4531 }
4532 run_test 56j "check lfs find -type d ============================="
4533
4534 test_56k() {
4535         TDIR=$DIR/${tdir}g
4536         setup_56_special $NUMFILES $NUMDIRS
4537
4538         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4539         CMD="$LFIND -type f $TDIR"
4540         NUMS=$($CMD | wc -l)
4541         [ $NUMS -eq $EXPECTED ] ||
4542                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4543 }
4544 run_test 56k "check lfs find -type f ============================="
4545
4546 test_56l() {
4547         TDIR=$DIR/${tdir}g
4548         setup_56_special $NUMFILES $NUMDIRS
4549
4550         EXPECTED=$((NUMDIRS + NUMFILES))
4551         CMD="$LFIND -type b $TDIR"
4552         NUMS=$($CMD | wc -l)
4553         [ $NUMS -eq $EXPECTED ] ||
4554                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4555 }
4556 run_test 56l "check lfs find -type b ============================="
4557
4558 test_56m() {
4559         TDIR=$DIR/${tdir}g
4560         setup_56_special $NUMFILES $NUMDIRS
4561
4562         EXPECTED=$((NUMDIRS + NUMFILES))
4563         CMD="$LFIND -type c $TDIR"
4564         NUMS=$($CMD | wc -l)
4565         [ $NUMS -eq $EXPECTED ] ||
4566                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4567 }
4568 run_test 56m "check lfs find -type c ============================="
4569
4570 test_56n() {
4571         TDIR=$DIR/${tdir}g
4572         setup_56_special $NUMFILES $NUMDIRS
4573
4574         EXPECTED=$((NUMDIRS + NUMFILES))
4575         CMD="$LFIND -type l $TDIR"
4576         NUMS=$($CMD | wc -l)
4577         [ $NUMS -eq $EXPECTED ] ||
4578                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4579 }
4580 run_test 56n "check lfs find -type l ============================="
4581
4582 test_56o() {
4583         TDIR=$DIR/${tdir}o
4584         setup_56 $NUMFILES $NUMDIRS
4585         utime $TDIR/file1 > /dev/null || error "utime (1)"
4586         utime $TDIR/file2 > /dev/null || error "utime (2)"
4587         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4588         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4589         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4590         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4591
4592         EXPECTED=4
4593         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4594         [ $NUMS -eq $EXPECTED ] || \
4595                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4596
4597         EXPECTED=12
4598         CMD="$LFIND -mtime 0 $TDIR"
4599         NUMS=$($CMD | wc -l)
4600         [ $NUMS -eq $EXPECTED ] ||
4601                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4602 }
4603 run_test 56o "check lfs find -mtime for old files =========================="
4604
4605 test_56p() {
4606         [ $RUNAS_ID -eq $UID ] &&
4607                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4608
4609         TDIR=$DIR/${tdir}p
4610         setup_56 $NUMFILES $NUMDIRS
4611
4612         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4613         EXPECTED=$NUMFILES
4614         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4615         NUMS=$($CMD | wc -l)
4616         [ $NUMS -eq $EXPECTED ] || \
4617                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4618
4619         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4620         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4621         NUMS=$($CMD | wc -l)
4622         [ $NUMS -eq $EXPECTED ] || \
4623                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4624 }
4625 run_test 56p "check lfs find -uid and ! -uid ==============================="
4626
4627 test_56q() {
4628         [ $RUNAS_ID -eq $UID ] &&
4629                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4630
4631         TDIR=$DIR/${tdir}q
4632         setup_56 $NUMFILES $NUMDIRS
4633
4634         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4635
4636         EXPECTED=$NUMFILES
4637         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4638         NUMS=$($CMD | wc -l)
4639         [ $NUMS -eq $EXPECTED ] ||
4640                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4641
4642         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4643         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4644         NUMS=$($CMD | wc -l)
4645         [ $NUMS -eq $EXPECTED ] ||
4646                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4647 }
4648 run_test 56q "check lfs find -gid and ! -gid ==============================="
4649
4650 test_56r() {
4651         TDIR=$DIR/${tdir}r
4652         setup_56 $NUMFILES $NUMDIRS
4653
4654         EXPECTED=12
4655         CMD="$LFIND -size 0 -type f $TDIR"
4656         NUMS=$($CMD | wc -l)
4657         [ $NUMS -eq $EXPECTED ] ||
4658                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4659         EXPECTED=0
4660         CMD="$LFIND ! -size 0 -type f $TDIR"
4661         NUMS=$($CMD | wc -l)
4662         [ $NUMS -eq $EXPECTED ] ||
4663                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4664         echo "test" > $TDIR/$tfile
4665         echo "test2" > $TDIR/$tfile.2 && sync
4666         EXPECTED=1
4667         CMD="$LFIND -size 5 -type f $TDIR"
4668         NUMS=$($CMD | wc -l)
4669         [ $NUMS -eq $EXPECTED ] ||
4670                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4671         EXPECTED=1
4672         CMD="$LFIND -size +5 -type f $TDIR"
4673         NUMS=$($CMD | wc -l)
4674         [ $NUMS -eq $EXPECTED ] ||
4675                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4676         EXPECTED=2
4677         CMD="$LFIND -size +0 -type f $TDIR"
4678         NUMS=$($CMD | wc -l)
4679         [ $NUMS -eq $EXPECTED ] ||
4680                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4681         EXPECTED=2
4682         CMD="$LFIND ! -size -5 -type f $TDIR"
4683         NUMS=$($CMD | wc -l)
4684         [ $NUMS -eq $EXPECTED ] ||
4685                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4686         EXPECTED=12
4687         CMD="$LFIND -size -5 -type f $TDIR"
4688         NUMS=$($CMD | wc -l)
4689         [ $NUMS -eq $EXPECTED ] ||
4690                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4691 }
4692 run_test 56r "check lfs find -size works =========================="
4693
4694 test_56s() { # LU-611
4695         TDIR=$DIR/${tdir}s
4696         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4697
4698         if [[ $OSTCOUNT -gt 1 ]]; then
4699                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4700                 ONESTRIPE=4
4701                 EXTRA=4
4702         else
4703                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4704                 EXTRA=0
4705         fi
4706
4707         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4708         CMD="$LFIND -stripe-count $OSTCOUNT -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         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4716         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4717         NUMS=$($CMD | wc -l)
4718         [ $NUMS -eq $EXPECTED ] || {
4719                 $GETSTRIPE -R $TDIR
4720                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4721         }
4722
4723         EXPECTED=$ONESTRIPE
4724         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4725         NUMS=$($CMD | wc -l)
4726         [ $NUMS -eq $EXPECTED ] || {
4727                 $GETSTRIPE -R $TDIR
4728                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4729         }
4730
4731         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4732         NUMS=$($CMD | wc -l)
4733         [ $NUMS -eq $EXPECTED ] || {
4734                 $GETSTRIPE -R $TDIR
4735                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4736         }
4737
4738         EXPECTED=0
4739         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4740         NUMS=$($CMD | wc -l)
4741         [ $NUMS -eq $EXPECTED ] || {
4742                 $GETSTRIPE -R $TDIR
4743                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4744         }
4745 }
4746 run_test 56s "check lfs find -stripe-count works"
4747
4748 test_56t() { # LU-611
4749         TDIR=$DIR/${tdir}t
4750         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4751
4752         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4753
4754         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4755         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4756         NUMS=$($CMD | wc -l)
4757         [ $NUMS -eq $EXPECTED ] ||
4758                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4759
4760         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4761         NUMS=$($CMD | wc -l)
4762         [ $NUMS -eq $EXPECTED ] ||
4763                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4764
4765         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4766         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4767         NUMS=$($CMD | wc -l)
4768         [ $NUMS -eq $EXPECTED ] ||
4769                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4770
4771         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4772         NUMS=$($CMD | wc -l)
4773         [ $NUMS -eq $EXPECTED ] ||
4774                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4775
4776         EXPECTED=4
4777         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4778         NUMS=$($CMD | wc -l)
4779         [ $NUMS -eq $EXPECTED ] ||
4780                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4781
4782         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4783         NUMS=$($CMD | wc -l)
4784         [ $NUMS -eq $EXPECTED ] ||
4785                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4786
4787         EXPECTED=0
4788         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4789         NUMS=$($CMD | wc -l)
4790         [ $NUMS -eq $EXPECTED ] ||
4791                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4792 }
4793 run_test 56t "check lfs find -stripe-size works"
4794
4795 test_56u() { # LU-611
4796         TDIR=$DIR/${tdir}u
4797         setup_56 $NUMFILES $NUMDIRS "-i 0"
4798
4799         if [[ $OSTCOUNT -gt 1 ]]; then
4800                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4801                 ONESTRIPE=4
4802         else
4803                 ONESTRIPE=0
4804         fi
4805
4806         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4807         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4808         NUMS=$($CMD | wc -l)
4809         [ $NUMS -eq $EXPECTED ] ||
4810                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4811
4812         EXPECTED=$ONESTRIPE
4813         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4814         NUMS=$($CMD | wc -l)
4815         [ $NUMS -eq $EXPECTED ] ||
4816                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4817
4818         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4819         NUMS=$($CMD | wc -l)
4820         [ $NUMS -eq $EXPECTED ] ||
4821                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4822
4823         EXPECTED=0
4824         # This should produce an error and not return any files
4825         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4826         NUMS=$($CMD 2>/dev/null | wc -l)
4827         [ $NUMS -eq $EXPECTED ] ||
4828                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4829
4830         if [[ $OSTCOUNT -gt 1 ]]; then
4831                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4832                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4833                 NUMS=$($CMD | wc -l)
4834                 [ $NUMS -eq $EXPECTED ] ||
4835                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4836         fi
4837 }
4838 run_test 56u "check lfs find -stripe-index works"
4839
4840 test_56v() {
4841     local MDT_IDX=0
4842
4843     TDIR=$DIR/${tdir}v
4844     rm -rf $TDIR
4845     setup_56 $NUMFILES $NUMDIRS
4846
4847     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4848     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4849
4850     for file in $($LFIND -mdt $UUID $TDIR); do
4851         file_mdt_idx=$($GETSTRIPE -M $file)
4852         [ $file_mdt_idx -eq $MDT_IDX ] ||
4853             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4854     done
4855 }
4856 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4857
4858 test_56w() {
4859         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4860                 return
4861         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4862         TDIR=$DIR/${tdir}w
4863
4864     rm -rf $TDIR || error "remove $TDIR failed"
4865     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4866
4867     local stripe_size
4868     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4869         error "$GETSTRIPE -S -d $TDIR failed"
4870     stripe_size=${stripe_size%% *}
4871
4872     local file_size=$((stripe_size * OSTCOUNT))
4873     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4874     local required_space=$((file_num * file_size))
4875     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4876     [[ $free_space -le $((required_space / 1024)) ]] &&
4877         skip_env "need at least $required_space bytes free space," \
4878                  "have $free_space kbytes" && return
4879
4880     local dd_bs=65536
4881     local dd_count=$((file_size / dd_bs))
4882
4883     # write data into the files
4884     local i
4885     local j
4886     local file
4887     for i in $(seq 1 $NUMFILES); do
4888         file=$TDIR/file$i
4889         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4890             error "write data into $file failed"
4891     done
4892     for i in $(seq 1 $NUMDIRS); do
4893         for j in $(seq 1 $NUMFILES); do
4894             file=$TDIR/dir$i/file$j
4895             yes | dd bs=$dd_bs count=$dd_count of=$file \
4896                 >/dev/null 2>&1 ||
4897                 error "write data into $file failed"
4898         done
4899     done
4900
4901     local expected=-1
4902     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4903
4904     # lfs_migrate file
4905     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4906     echo "$cmd"
4907     eval $cmd || error "$cmd failed"
4908
4909     check_stripe_count $TDIR/file1 $expected
4910
4911         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4912         then
4913                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4914                 # OST 1 if it is on OST 0. This file is small enough to
4915                 # be on only one stripe.
4916                 file=$TDIR/migr_1_ost
4917                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4918                         error "write data into $file failed"
4919                 local obdidx=$($LFS getstripe -i $file)
4920                 local oldmd5=$(md5sum $file)
4921                 local newobdidx=0
4922                 [[ $obdidx -eq 0 ]] && newobdidx=1
4923                 cmd="$LFS migrate -i $newobdidx $file"
4924                 echo $cmd
4925                 eval $cmd || error "$cmd failed"
4926                 local realobdix=$($LFS getstripe -i $file)
4927                 local newmd5=$(md5sum $file)
4928                 [[ $newobdidx -ne $realobdix ]] &&
4929                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4930                 [[ "$oldmd5" != "$newmd5" ]] &&
4931                         error "md5sum differ: $oldmd5, $newmd5"
4932         fi
4933
4934     # lfs_migrate dir
4935     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4936     echo "$cmd"
4937     eval $cmd || error "$cmd failed"
4938
4939     for j in $(seq 1 $NUMFILES); do
4940         check_stripe_count $TDIR/dir1/file$j $expected
4941     done
4942
4943     # lfs_migrate works with lfs find
4944     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4945          $LFS_MIGRATE -y -c $expected"
4946     echo "$cmd"
4947     eval $cmd || error "$cmd failed"
4948
4949     for i in $(seq 2 $NUMFILES); do
4950         check_stripe_count $TDIR/file$i $expected
4951     done
4952     for i in $(seq 2 $NUMDIRS); do
4953         for j in $(seq 1 $NUMFILES); do
4954             check_stripe_count $TDIR/dir$i/file$j $expected
4955         done
4956     done
4957 }
4958 run_test 56w "check lfs_migrate -c stripe_count works"
4959
4960 test_56x() {
4961         check_swap_layouts_support && return 0
4962         [[ $OSTCOUNT -lt 2 ]] &&
4963                 skip_env "need 2 OST, skipping test" && return
4964
4965         local dir0=$DIR/$tdir/$testnum
4966         mkdir -p $dir0 || error "creating dir $dir0"
4967
4968         local ref1=/etc/passwd
4969         local file1=$dir0/file1
4970
4971         $SETSTRIPE -c 2 $file1
4972         cp $ref1 $file1
4973         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4974         stripe=$($GETSTRIPE -c $file1)
4975         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4976         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4977
4978         # clean up
4979         rm -f $file1
4980 }
4981 run_test 56x "lfs migration support"
4982
4983 test_56y() {
4984         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4985                 skip "No HSM support on MDS of $(get_lustre_version)," \
4986                          "need 2.4.53 at least" && return
4987         local res=""
4988
4989         local dir0=$DIR/$tdir/$testnum
4990         mkdir -p $dir0 || error "creating dir $dir0"
4991         local f1=$dir0/file1
4992         local f2=$dir0/file2
4993
4994         touch $f1 || error "creating std file $f1"
4995         $MULTIOP $f2 H2c || error "creating released file $f2"
4996
4997         # a directory can be raid0, so ask only for files
4998         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4999         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5000
5001         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5002         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5003
5004         # only files can be released, so no need to force file search
5005         res=$($LFIND $dir0 -L released)
5006         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5007
5008         res=$($LFIND $dir0 \! -L released)
5009         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5010
5011 }
5012 run_test 56y "lfs find -L raid0|released"
5013
5014 test_56z() { # LU-4824
5015         # This checks to make sure 'lfs find' continues after errors
5016         # There are two classes of errors that should be caught:
5017         # - If multiple paths are provided, all should be searched even if one
5018         #   errors out
5019         # - If errors are encountered during the search, it should not terminate
5020         #   early
5021         local i
5022         test_mkdir $DIR/$tdir
5023         for i in d{0..9}; do
5024                 test_mkdir $DIR/$tdir/$i
5025         done
5026         touch $DIR/$tdir/d{0..9}/$tfile
5027         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5028                 error "$LFS find did not return an error"
5029         # Make a directory unsearchable. This should NOT be the last entry in
5030         # directory order.  Arbitrarily pick the 6th entry
5031         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5032         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5033         # The user should be able to see 10 directories and 9 files
5034         [ $count == 19 ] || error "$LFS find did not continue after error"
5035 }
5036 run_test 56z "lfs find should continue after an error"
5037
5038 test_56aa() { # LU-5937
5039         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5040
5041         mkdir $DIR/$tdir
5042         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5043
5044         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5045         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5046
5047         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5048 }
5049 run_test 56aa "lfs find --size under striped dir"
5050
5051 test_57a() {
5052         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5053         # note test will not do anything if MDS is not local
5054         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5055                 skip "Only applicable to ldiskfs-based MDTs"
5056                 return
5057         fi
5058
5059         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5060         local MNTDEV="osd*.*MDT*.mntdev"
5061         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5062         [ -z "$DEV" ] && error "can't access $MNTDEV"
5063         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5064                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5065                         error "can't access $DEV"
5066                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5067                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5068                 rm $TMP/t57a.dump
5069         done
5070 }
5071 run_test 57a "verify MDS filesystem created with large inodes =="
5072
5073 test_57b() {
5074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5075         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5076                 skip "Only applicable to ldiskfs-based MDTs"
5077                 return
5078         fi
5079
5080         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5081         local dir=$DIR/$tdir
5082
5083         local FILECOUNT=100
5084         local FILE1=$dir/f1
5085         local FILEN=$dir/f$FILECOUNT
5086
5087         rm -rf $dir || error "removing $dir"
5088         test_mkdir -p -c1 $dir || error "creating $dir"
5089         local mdtidx=$($LFS getstripe -M $dir)
5090         local mdtname=MDT$(printf %04x $mdtidx)
5091         local facet=mds$((mdtidx + 1))
5092
5093         echo "mcreating $FILECOUNT files"
5094         createmany -m $dir/f 1 $FILECOUNT || \
5095                 error "creating files in $dir"
5096
5097         # verify that files do not have EAs yet
5098         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5099         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5100
5101         sync
5102         sleep 1
5103         df $dir  #make sure we get new statfs data
5104         local MDSFREE=$(do_facet $facet \
5105                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5106         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5107         echo "opening files to create objects/EAs"
5108         local FILE
5109         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5110                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5111         done
5112
5113         # verify that files have EAs now
5114         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5115         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5116
5117         sleep 1  #make sure we get new statfs data
5118         df $dir
5119         local MDSFREE2=$(do_facet $facet \
5120                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5121         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5122         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5123                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5124                         error "MDC before $MDCFREE != after $MDCFREE2"
5125                 else
5126                         echo "MDC before $MDCFREE != after $MDCFREE2"
5127                         echo "unable to confirm if MDS has large inodes"
5128                 fi
5129         fi
5130         rm -rf $dir
5131 }
5132 run_test 57b "default LOV EAs are stored inside large inodes ==="
5133
5134 test_58() {
5135         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5136         [ -z "$(which wiretest 2>/dev/null)" ] &&
5137                         skip_env "could not find wiretest" && return
5138         wiretest
5139 }
5140 run_test 58 "verify cross-platform wire constants =============="
5141
5142 test_59() {
5143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5144         echo "touch 130 files"
5145         createmany -o $DIR/f59- 130
5146         echo "rm 130 files"
5147         unlinkmany $DIR/f59- 130
5148         sync
5149         # wait for commitment of removal
5150         wait_delete_completed
5151 }
5152 run_test 59 "verify cancellation of llog records async ========="
5153
5154 TEST60_HEAD="test_60 run $RANDOM"
5155 test_60a() {
5156         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5157         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5158         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5159                 skip_env "missing subtest run-llog.sh" && return
5160         log "$TEST60_HEAD - from kernel mode"
5161         do_facet mgs sh run-llog.sh
5162 }
5163 run_test 60a "llog sanity tests run from kernel module =========="
5164
5165 test_60b() { # bug 6411
5166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5167         dmesg > $DIR/$tfile
5168         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5169                                  /llog.test/ {
5170                                          if (marker)
5171                                                  from_marker++
5172                                          from_begin++
5173                                  }
5174                                  END {
5175                                          if (marker)
5176                                                  print from_marker
5177                                          else
5178                                                  print from_begin
5179                                  }"`
5180         [[ $LLOG_COUNT -gt 50 ]] &&
5181                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5182 }
5183 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5184
5185 test_60c() {
5186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5187         echo "create 5000 files"
5188         createmany -o $DIR/f60c- 5000
5189 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5190         lctl set_param fail_loc=0x80000137
5191         unlinkmany $DIR/f60c- 5000
5192         lctl set_param fail_loc=0
5193 }
5194 run_test 60c "unlink file when mds full"
5195
5196 test_60d() {
5197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5198         SAVEPRINTK=$(lctl get_param -n printk)
5199
5200         # verify "lctl mark" is even working"
5201         MESSAGE="test message ID $RANDOM $$"
5202         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5203         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5204
5205         lctl set_param printk=0 || error "set lnet.printk failed"
5206         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5207         MESSAGE="new test message ID $RANDOM $$"
5208         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5209         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5210         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5211
5212         lctl set_param -n printk="$SAVEPRINTK"
5213 }
5214 run_test 60d "test printk console message masking"
5215
5216 test_61() {
5217         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5218         f="$DIR/f61"
5219         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5220         cancel_lru_locks osc
5221         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5222         sync
5223 }
5224 run_test 61 "mmap() writes don't make sync hang ================"
5225
5226 # bug 2330 - insufficient obd_match error checking causes LBUG
5227 test_62() {
5228         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5229         f="$DIR/f62"
5230         echo foo > $f
5231         cancel_lru_locks osc
5232         lctl set_param fail_loc=0x405
5233         cat $f && error "cat succeeded, expect -EIO"
5234         lctl set_param fail_loc=0
5235 }
5236 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5237 # match every page all of the time.
5238 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5239
5240 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5241 # Though this test is irrelevant anymore, it helped to reveal some
5242 # other grant bugs (LU-4482), let's keep it.
5243 test_63a() {   # was test_63
5244         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5245         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5246         for i in `seq 10` ; do
5247                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5248                 sleep 5
5249                 kill $!
5250                 sleep 1
5251         done
5252
5253         rm -f $DIR/f63 || true
5254 }
5255 run_test 63a "Verify oig_wait interruption does not crash ======="
5256
5257 # bug 2248 - async write errors didn't return to application on sync
5258 # bug 3677 - async write errors left page locked
5259 test_63b() {
5260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5261         debugsave
5262         lctl set_param debug=-1
5263
5264         # ensure we have a grant to do async writes
5265         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5266         rm $DIR/$tfile
5267
5268         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5269         lctl set_param fail_loc=0x80000406
5270         $MULTIOP $DIR/$tfile Owy && \
5271                 error "sync didn't return ENOMEM"
5272         sync; sleep 2; sync     # do a real sync this time to flush page
5273         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5274                 error "locked page left in cache after async error" || true
5275         debugrestore
5276 }
5277 run_test 63b "async write errors should be returned to fsync ==="
5278
5279 test_64a () {
5280         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5281         df $DIR
5282         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5283 }
5284 run_test 64a "verify filter grant calculations (in kernel) ====="
5285
5286 test_64b () {
5287         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5288         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5289 }
5290 run_test 64b "check out-of-space detection on client ==========="
5291
5292 test_64c() {
5293         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5294 }
5295 run_test 64c "verify grant shrink ========================------"
5296
5297 # bug 1414 - set/get directories' stripe info
5298 test_65a() {
5299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5300         test_mkdir -p $DIR/$tdir
5301         touch $DIR/$tdir/f1
5302         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5303 }
5304 run_test 65a "directory with no stripe info ===================="
5305
5306 test_65b() {
5307         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5308         test_mkdir -p $DIR/$tdir
5309         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5310                                                 error "setstripe"
5311         touch $DIR/$tdir/f2
5312         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5313 }
5314 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5315
5316 test_65c() {
5317         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5318         if [[ $OSTCOUNT -gt 1 ]]; then
5319                 test_mkdir -p $DIR/$tdir
5320                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5321                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5322                 touch $DIR/$tdir/f3
5323                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5324         fi
5325 }
5326 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5327
5328 test_65d() {
5329         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5330         test_mkdir -p $DIR/$tdir
5331         if [[ $STRIPECOUNT -le 0 ]]; then
5332                 sc=1
5333         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5334 #LOV_MAX_STRIPE_COUNT is 2000
5335                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5336         else
5337                 sc=$(($STRIPECOUNT - 1))
5338         fi
5339         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5340         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5341         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5342                 error "lverify failed"
5343 }
5344 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5345
5346 test_65e() {
5347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5348         test_mkdir -p $DIR/$tdir
5349
5350         $SETSTRIPE $DIR/$tdir || error "setstripe"
5351         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5352                                         error "no stripe info failed"
5353         touch $DIR/$tdir/f6
5354         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5355 }
5356 run_test 65e "directory setstripe defaults ======================="
5357
5358 test_65f() {
5359         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5360         test_mkdir -p $DIR/${tdir}f
5361         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5362 }
5363 run_test 65f "dir setstripe permission (should return error) ==="
5364
5365 test_65g() {
5366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5367         test_mkdir -p $DIR/$tdir
5368         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5369                                                         error "setstripe"
5370         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5371         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5372                 error "delete default stripe failed"
5373 }
5374 run_test 65g "directory setstripe -d ==========================="
5375
5376 test_65h() {
5377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5378         test_mkdir -p $DIR/$tdir
5379         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5380                                                         error "setstripe"
5381         test_mkdir -p $DIR/$tdir/dd1
5382         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5383                 error "stripe info inherit failed"
5384 }
5385 run_test 65h "directory stripe info inherit ===================="
5386
5387 test_65i() { # bug6367
5388         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5389         $SETSTRIPE -S 65536 -c -1 $MOUNT
5390 }
5391 run_test 65i "set non-default striping on root directory (bug 6367)="
5392
5393 test_65ia() { # bug12836
5394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5395         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5396 }
5397 run_test 65ia "getstripe on -1 default directory striping"
5398
5399 test_65ib() { # bug12836
5400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5401         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5402 }
5403 run_test 65ib "getstripe -v on -1 default directory striping"
5404
5405 test_65ic() { # bug12836
5406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5407         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5408 }
5409 run_test 65ic "new find on -1 default directory striping"
5410
5411 test_65j() { # bug6367
5412         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5413         sync; sleep 1
5414         # if we aren't already remounting for each test, do so for this test
5415         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5416                 cleanup || error "failed to unmount"
5417                 setup
5418         fi
5419         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5420 }
5421 run_test 65j "set default striping on root directory (bug 6367)="
5422
5423 test_65k() { # bug11679
5424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5425         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5426         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5427
5428     echo "Check OST status: "
5429     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5430               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5431
5432     for OSC in $MDS_OSCS; do
5433         echo $OSC "is activate"
5434         do_facet $SINGLEMDS lctl --device %$OSC activate
5435     done
5436
5437     mkdir -p $DIR/$tdir
5438     for INACTIVE_OSC in $MDS_OSCS; do
5439         echo "Deactivate: " $INACTIVE_OSC
5440         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5441         for STRIPE_OSC in $MDS_OSCS; do
5442             OST=`osc_to_ost $STRIPE_OSC`
5443             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5444                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5445
5446             [ -f $DIR/$tdir/$IDX ] && continue
5447             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5448             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5449             RC=$?
5450             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5451         done
5452         rm -f $DIR/$tdir/*
5453         echo $INACTIVE_OSC "is Activate."
5454         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5455     done
5456 }
5457 run_test 65k "validate manual striping works properly with deactivated OSCs"
5458
5459 test_65l() { # bug 12836
5460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5461         test_mkdir -p $DIR/$tdir/test_dir
5462         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5463         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5464 }
5465 run_test 65l "lfs find on -1 stripe dir ========================"
5466
5467 # bug 2543 - update blocks count on client
5468 test_66() {
5469         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5470         COUNT=${COUNT:-8}
5471         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5472         sync; sync_all_data; sync; sync_all_data
5473         cancel_lru_locks osc
5474         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5475         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5476 }
5477 run_test 66 "update inode blocks count on client ==============="
5478
5479 LLOOP=
5480 LLITELOOPLOAD=
5481 cleanup_68() {
5482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5483         trap 0
5484         if [ ! -z "$LLOOP" ]; then
5485                 if swapon -s | grep -q $LLOOP; then
5486                         swapoff $LLOOP || error "swapoff failed"
5487                 fi
5488
5489                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5490                 rm -f $LLOOP
5491                 unset LLOOP
5492         fi
5493         if [ ! -z "$LLITELOOPLOAD" ]; then
5494                 rmmod llite_lloop
5495                 unset LLITELOOPLOAD
5496         fi
5497         rm -f $DIR/f68*
5498 }
5499
5500 meminfo() {
5501         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5502 }
5503
5504 swap_used() {
5505         swapon -s | awk '($1 == "'$1'") { print $4 }'
5506 }
5507
5508 # test case for lloop driver, basic function
5509 test_68a() {
5510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5511         [ "$UID" != 0 ] && skip_env "must run as root" && return
5512         llite_lloop_enabled || \
5513                 { skip_env "llite_lloop module disabled" && return; }
5514
5515         trap cleanup_68 EXIT
5516
5517         if ! module_loaded llite_lloop; then
5518                 if load_module llite/llite_lloop; then
5519                         LLITELOOPLOAD=yes
5520                 else
5521                         skip_env "can't find module llite_lloop"
5522                         return
5523                 fi
5524         fi
5525
5526         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5527         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5528         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5529
5530         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5531         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5532
5533         cleanup_68
5534 }
5535 run_test 68a "lloop driver - basic test ========================"
5536
5537 # excercise swapping to lustre by adding a high priority swapfile entry
5538 # and then consuming memory until it is used.
5539 test_68b() {  # was test_68
5540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5541         [ "$UID" != 0 ] && skip_env "must run as root" && return
5542         lctl get_param -n devices | grep -q obdfilter && \
5543                 skip "local OST" && return
5544
5545         grep -q llite_lloop /proc/modules
5546         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5547
5548         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5549                 skip "can't reliably test swap with TCP" && return
5550
5551         MEMTOTAL=`meminfo MemTotal`
5552         NR_BLOCKS=$((MEMTOTAL>>8))
5553         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5554
5555         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5556         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5557         mkswap $DIR/f68b
5558
5559         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5560
5561         trap cleanup_68 EXIT
5562
5563         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5564
5565         echo "before: `swapon -s | grep $LLOOP`"
5566         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5567         echo "after: `swapon -s | grep $LLOOP`"
5568         SWAPUSED=`swap_used $LLOOP`
5569
5570         cleanup_68
5571
5572         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5573 }
5574 run_test 68b "support swapping to Lustre ========================"
5575
5576 # bug5265, obdfilter oa2dentry return -ENOENT
5577 # #define OBD_FAIL_OST_ENOENT 0x217
5578 test_69() {
5579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5580         remote_ost_nodsh && skip "remote OST with nodsh" && return
5581
5582         f="$DIR/$tfile"
5583         $SETSTRIPE -c 1 -i 0 $f
5584
5585         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5586
5587         do_facet ost1 lctl set_param fail_loc=0x217
5588         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5589         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5590
5591         do_facet ost1 lctl set_param fail_loc=0
5592         $DIRECTIO write $f 0 2 || error "write error"
5593
5594         cancel_lru_locks osc
5595         $DIRECTIO read $f 0 1 || error "read error"
5596
5597         do_facet ost1 lctl set_param fail_loc=0x217
5598         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5599
5600         do_facet ost1 lctl set_param fail_loc=0
5601         rm -f $f
5602 }
5603 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5604
5605 test_71() {
5606     test_mkdir -p $DIR/$tdir
5607     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5608 }
5609 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5610
5611 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5612         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5613         [ "$RUNAS_ID" = "$UID" ] &&
5614                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5615
5616         # Check that testing environment is properly set up. Skip if not
5617         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5618                 skip_env "User $RUNAS_ID does not exist - skipping"
5619                 return 0
5620         }
5621         touch $DIR/$tfile
5622         chmod 777 $DIR/$tfile
5623         chmod ug+s $DIR/$tfile
5624         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5625                 error "$RUNAS dd $DIR/$tfile failed"
5626         # See if we are still setuid/sgid
5627         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5628                 error "S/gid is not dropped on write"
5629         # Now test that MDS is updated too
5630         cancel_lru_locks mdc
5631         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5632                 error "S/gid is not dropped on MDS"
5633         rm -f $DIR/$tfile
5634 }
5635 run_test 72a "Test that remove suid works properly (bug5695) ===="
5636
5637 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5638         local perm
5639
5640         [ "$RUNAS_ID" = "$UID" ] && \
5641                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5642         [ "$RUNAS_ID" -eq 0 ] && \
5643                 skip_env "RUNAS_ID = 0 -- skipping" && return
5644
5645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5646         # Check that testing environment is properly set up. Skip if not
5647         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5648                 skip_env "User $RUNAS_ID does not exist - skipping"
5649                 return 0
5650         }
5651         touch $DIR/${tfile}-f{g,u}
5652         test_mkdir $DIR/${tfile}-dg
5653         test_mkdir $DIR/${tfile}-du
5654         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5655         chmod g+s $DIR/${tfile}-{f,d}g
5656         chmod u+s $DIR/${tfile}-{f,d}u
5657         for perm in 777 2777 4777; do
5658                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5659                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5660                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5661                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5662         done
5663         true
5664 }
5665 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5666
5667 # bug 3462 - multiple simultaneous MDC requests
5668 test_73() {
5669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5670         test_mkdir $DIR/d73-1
5671         test_mkdir $DIR/d73-2
5672         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5673         pid1=$!
5674
5675         lctl set_param fail_loc=0x80000129
5676         $MULTIOP $DIR/d73-1/f73-2 Oc &
5677         sleep 1
5678         lctl set_param fail_loc=0
5679
5680         $MULTIOP $DIR/d73-2/f73-3 Oc &
5681         pid3=$!
5682
5683         kill -USR1 $pid1
5684         wait $pid1 || return 1
5685
5686         sleep 25
5687
5688         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5689         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5690         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5691
5692         rm -rf $DIR/d73-*
5693 }
5694 run_test 73 "multiple MDC requests (should not deadlock)"
5695
5696 test_74a() { # bug 6149, 6184
5697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5698         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5699         #
5700         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5701         # will spin in a tight reconnection loop
5702         touch $DIR/f74a
5703         $LCTL set_param fail_loc=0x8000030e
5704         # get any lock that won't be difficult - lookup works.
5705         ls $DIR/f74a
5706         $LCTL set_param fail_loc=0
5707         rm -f $DIR/f74a
5708         true
5709 }
5710 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5711
5712 test_74b() { # bug 13310
5713         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5714         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5715         #
5716         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5717         # will spin in a tight reconnection loop
5718         $LCTL set_param fail_loc=0x8000030e
5719         # get a "difficult" lock
5720         touch $DIR/f74b
5721         $LCTL set_param fail_loc=0
5722         rm -f $DIR/f74b
5723         true
5724 }
5725 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5726
5727 test_74c() {
5728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5729         #define OBD_FAIL_LDLM_NEW_LOCK
5730         $LCTL set_param fail_loc=0x319
5731         touch $DIR/$tfile && error "touch successful"
5732         $LCTL set_param fail_loc=0
5733         true
5734 }
5735 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5736
5737 num_inodes() {
5738         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5739 }
5740
5741 get_inode_slab_tunables() {
5742         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5743 }
5744
5745 set_inode_slab_tunables() {
5746         echo "lustre_inode_cache $1" > /proc/slabinfo
5747 }
5748
5749 test_76() { # Now for bug 20433, added originally in bug 1443
5750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5751         local SLAB_SETTINGS=`get_inode_slab_tunables`
5752         local CPUS=`getconf _NPROCESSORS_ONLN`
5753         # we cannot set limit below 1 which means 1 inode in each
5754         # per-cpu cache is still allowed
5755         set_inode_slab_tunables "1 1 0"
5756         cancel_lru_locks osc
5757         BEFORE_INODES=$(num_inodes)
5758         echo "before inodes: $BEFORE_INODES"
5759         local COUNT=1000
5760         [ "$SLOW" = "no" ] && COUNT=100
5761         for i in $(seq $COUNT); do
5762                 touch $DIR/$tfile
5763                 rm -f $DIR/$tfile
5764         done
5765         cancel_lru_locks osc
5766         AFTER_INODES=$(num_inodes)
5767         echo "after inodes: $AFTER_INODES"
5768         local wait=0
5769         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5770                 sleep 2
5771                 AFTER_INODES=$(num_inodes)
5772                 wait=$((wait+2))
5773                 echo "wait $wait seconds inodes: $AFTER_INODES"
5774                 if [ $wait -gt 30 ]; then
5775                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5776                 fi
5777         done
5778         set_inode_slab_tunables "$SLAB_SETTINGS"
5779 }
5780 run_test 76 "confirm clients recycle inodes properly ===="
5781
5782
5783 export ORIG_CSUM=""
5784 set_checksums()
5785 {
5786         # Note: in sptlrpc modes which enable its own bulk checksum, the
5787         # original crc32_le bulk checksum will be automatically disabled,
5788         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5789         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5790         # In this case set_checksums() will not be no-op, because sptlrpc
5791         # bulk checksum will be enabled all through the test.
5792
5793         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5794         lctl set_param -n osc.*.checksums $1
5795         return 0
5796 }
5797
5798 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5799                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5800 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5801 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5802 set_checksum_type()
5803 {
5804         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5805         log "set checksum type to $1"
5806         return 0
5807 }
5808 F77_TMP=$TMP/f77-temp
5809 F77SZ=8
5810 setup_f77() {
5811         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5812                 error "error writing to $F77_TMP"
5813 }
5814
5815 test_77a() { # bug 10889
5816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5817         $GSS && skip "could not run with gss" && return
5818         [ ! -f $F77_TMP ] && setup_f77
5819         set_checksums 1
5820         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5821         set_checksums 0
5822         rm -f $DIR/$tfile
5823 }
5824 run_test 77a "normal checksum read/write operation"
5825
5826 test_77b() { # bug 10889
5827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5828         $GSS && skip "could not run with gss" && return
5829         [ ! -f $F77_TMP ] && setup_f77
5830         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5831         $LCTL set_param fail_loc=0x80000409
5832         set_checksums 1
5833
5834         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5835                 error "dd error: $?"
5836         $LCTL set_param fail_loc=0
5837
5838         for algo in $CKSUM_TYPES; do
5839                 cancel_lru_locks osc
5840                 set_checksum_type $algo
5841                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5842                 $LCTL set_param fail_loc=0x80000408
5843                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5844                 $LCTL set_param fail_loc=0
5845         done
5846         set_checksums 0
5847         set_checksum_type $ORIG_CSUM_TYPE
5848         rm -f $DIR/$tfile
5849 }
5850 run_test 77b "checksum error on client write, read"
5851
5852 test_77d() { # bug 10889
5853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5854         $GSS && skip "could not run with gss" && return
5855         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5856         $LCTL set_param fail_loc=0x80000409
5857         set_checksums 1
5858         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5859                 error "direct write: rc=$?"
5860         $LCTL set_param fail_loc=0
5861         set_checksums 0
5862
5863         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5864         $LCTL set_param fail_loc=0x80000408
5865         set_checksums 1
5866         cancel_lru_locks osc
5867         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5868                 error "direct read: rc=$?"
5869         $LCTL set_param fail_loc=0
5870         set_checksums 0
5871 }
5872 run_test 77d "checksum error on OST direct write, read"
5873
5874 test_77f() { # bug 10889
5875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5876         $GSS && skip "could not run with gss" && return
5877         set_checksums 1
5878         for algo in $CKSUM_TYPES; do
5879                 cancel_lru_locks osc
5880                 set_checksum_type $algo
5881                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5882                 $LCTL set_param fail_loc=0x409
5883                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5884                         error "direct write succeeded"
5885                 $LCTL set_param fail_loc=0
5886         done
5887         set_checksum_type $ORIG_CSUM_TYPE
5888         set_checksums 0
5889 }
5890 run_test 77f "repeat checksum error on write (expect error)"
5891
5892 test_77g() { # bug 10889
5893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5894         $GSS && skip "could not run with gss" && return
5895         remote_ost_nodsh && skip "remote OST with nodsh" && return
5896
5897         [ ! -f $F77_TMP ] && setup_f77
5898
5899         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5900         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5901         do_facet ost1 lctl set_param fail_loc=0x8000021a
5902         set_checksums 1
5903         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5904                 error "write error: rc=$?"
5905         do_facet ost1 lctl set_param fail_loc=0
5906         set_checksums 0
5907
5908         cancel_lru_locks osc
5909         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5910         do_facet ost1 lctl set_param fail_loc=0x8000021b
5911         set_checksums 1
5912         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5913         do_facet ost1 lctl set_param fail_loc=0
5914         set_checksums 0
5915 }
5916 run_test 77g "checksum error on OST write, read"
5917
5918 test_77i() { # bug 13805
5919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5920         $GSS && skip "could not run with gss" && return
5921         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5922         lctl set_param fail_loc=0x40b
5923         remount_client $MOUNT
5924         lctl set_param fail_loc=0
5925         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5926                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5927                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5928                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5929         done
5930         remount_client $MOUNT
5931 }
5932 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5933
5934 test_77j() { # bug 13805
5935         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5936         $GSS && skip "could not run with gss" && return
5937         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5938         lctl set_param fail_loc=0x40c
5939         remount_client $MOUNT
5940         lctl set_param fail_loc=0
5941         sleep 2 # wait async osc connect to finish
5942         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5943                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5944                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5945                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5946         done
5947         remount_client $MOUNT
5948 }
5949 run_test 77j "client only supporting ADLER32"
5950
5951 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5952 rm -f $F77_TMP
5953 unset F77_TMP
5954
5955 test_78() { # bug 10901
5956         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5957         remote_ost || { skip_env "local OST" && return; }
5958
5959         NSEQ=5
5960         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5961         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5962         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5963         echo "MemTotal: $MEMTOTAL"
5964
5965         # reserve 256MB of memory for the kernel and other running processes,
5966         # and then take 1/2 of the remaining memory for the read/write buffers.
5967         if [ $MEMTOTAL -gt 512 ] ;then
5968                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5969         else
5970                 # for those poor memory-starved high-end clusters...
5971                 MEMTOTAL=$((MEMTOTAL / 2))
5972         fi
5973         echo "Mem to use for directio: $MEMTOTAL"
5974
5975         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5976         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5977         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5978         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5979                 head -n1)
5980         echo "Smallest OST: $SMALLESTOST"
5981         [[ $SMALLESTOST -lt 10240 ]] &&
5982                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5983
5984         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5985                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5986
5987         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5988         echo "File size: $F78SIZE"
5989         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5990         for i in $(seq 1 $NSEQ); do
5991                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5992                 echo directIO rdwr round $i of $NSEQ
5993                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5994         done
5995
5996         rm -f $DIR/$tfile
5997 }
5998 run_test 78 "handle large O_DIRECT writes correctly ============"
5999
6000 test_79() { # bug 12743
6001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6002         wait_delete_completed
6003
6004         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6005         BKFREE=$(calc_osc_kbytes kbytesfree)
6006         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6007
6008         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6009         DFTOTAL=`echo $STRING | cut -d, -f1`
6010         DFUSED=`echo $STRING  | cut -d, -f2`
6011         DFAVAIL=`echo $STRING | cut -d, -f3`
6012         DFFREE=$(($DFTOTAL - $DFUSED))
6013
6014         ALLOWANCE=$((64 * $OSTCOUNT))
6015
6016         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6017            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6018                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6019         fi
6020         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6021            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6022                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6023         fi
6024         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6025            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6026                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6027         fi
6028 }
6029 run_test 79 "df report consistency check ======================="
6030
6031 test_80() { # bug 10718
6032         remote_ost_nodsh && skip "remote OST with nodsh" && return
6033         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6034         # relax strong synchronous semantics for slow backends like ZFS
6035         local soc="obdfilter.*.sync_on_lock_cancel"
6036         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6037         local hosts=
6038         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6039                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6040                           facet_active_host $host; done | sort -u)
6041                 do_nodes $hosts lctl set_param $soc=never
6042         fi
6043
6044         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6045         sync; sleep 1; sync
6046         local BEFORE=`date +%s`
6047         cancel_lru_locks osc
6048         local AFTER=`date +%s`
6049         local DIFF=$((AFTER-BEFORE))
6050         if [ $DIFF -gt 1 ] ; then
6051                 error "elapsed for 1M@1T = $DIFF"
6052         fi
6053
6054         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6055
6056         rm -f $DIR/$tfile
6057 }
6058 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6059
6060 test_81a() { # LU-456
6061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6062         remote_ost_nodsh && skip "remote OST with nodsh" && return
6063         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6064         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6065         do_facet ost1 lctl set_param fail_loc=0x80000228
6066
6067         # write should trigger a retry and success
6068         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6069         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6070         RC=$?
6071         if [ $RC -ne 0 ] ; then
6072                 error "write should success, but failed for $RC"
6073         fi
6074 }
6075 run_test 81a "OST should retry write when get -ENOSPC ==============="
6076
6077 test_81b() { # LU-456
6078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6079         remote_ost_nodsh && skip "remote OST with nodsh" && return
6080         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6081         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6082         do_facet ost1 lctl set_param fail_loc=0x228
6083
6084         # write should retry several times and return -ENOSPC finally
6085         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6086         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6087         RC=$?
6088         ENOSPC=28
6089         if [ $RC -ne $ENOSPC ] ; then
6090                 error "dd should fail for -ENOSPC, but succeed."
6091         fi
6092 }
6093 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6094
6095 test_82() { # LU-1031
6096         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6097         local gid1=14091995
6098         local gid2=16022000
6099
6100         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6101         local MULTIPID1=$!
6102         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6103         local MULTIPID2=$!
6104         kill -USR1 $MULTIPID2
6105         sleep 2
6106         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6107                 error "First grouplock does not block second one"
6108         else
6109                 echo "Second grouplock blocks first one"
6110         fi
6111         kill -USR1 $MULTIPID1
6112         wait $MULTIPID1
6113         wait $MULTIPID2
6114 }
6115 run_test 82 "Basic grouplock test ==============================="
6116
6117 test_99a() {
6118         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6119                 return
6120         test_mkdir -p $DIR/d99cvsroot
6121         chown $RUNAS_ID $DIR/d99cvsroot
6122         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6123         cd $TMP
6124
6125         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6126         cd $oldPWD
6127 }
6128 run_test 99a "cvs init ========================================="
6129
6130 test_99b() {
6131         [ -z "$(which cvs 2>/dev/null)" ] &&
6132                 skip_env "could not find cvs" && return
6133         [ ! -d $DIR/d99cvsroot ] && test_99a
6134         cd /etc/init.d
6135         # some versions of cvs import exit(1) when asked to import links or
6136         # files they can't read.  ignore those files.
6137         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6138                         ! -perm +4 -printf '-I %f\n')
6139         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6140                 d99reposname vtag rtag
6141 }
6142 run_test 99b "cvs import ======================================="
6143
6144 test_99c() {
6145         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6146         [ ! -d $DIR/d99cvsroot ] && test_99b
6147         cd $DIR
6148         test_mkdir -p $DIR/d99reposname
6149         chown $RUNAS_ID $DIR/d99reposname
6150         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6151 }
6152 run_test 99c "cvs checkout ====================================="
6153
6154 test_99d() {
6155         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6156         [ ! -d $DIR/d99cvsroot ] && test_99c
6157         cd $DIR/d99reposname
6158         $RUNAS touch foo99
6159         $RUNAS cvs add -m 'addmsg' foo99
6160 }
6161 run_test 99d "cvs add =========================================="
6162
6163 test_99e() {
6164         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6165         [ ! -d $DIR/d99cvsroot ] && test_99c
6166         cd $DIR/d99reposname
6167         $RUNAS cvs update
6168 }
6169 run_test 99e "cvs update ======================================="
6170
6171 test_99f() {
6172         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6173         [ ! -d $DIR/d99cvsroot ] && test_99d
6174         cd $DIR/d99reposname
6175         $RUNAS cvs commit -m 'nomsg' foo99
6176     rm -fr $DIR/d99cvsroot
6177 }
6178 run_test 99f "cvs commit ======================================="
6179
6180 test_100() {
6181         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6182         [ "$NETTYPE" = tcp ] || \
6183                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6184                         return ; }
6185
6186         remote_ost_nodsh && skip "remote OST with nodsh" && return
6187         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6188         remote_servers || \
6189                 { skip "useless for local single node setup" && return; }
6190
6191         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6192                 [ "$PROT" != "tcp" ] && continue
6193                 RPORT=$(echo $REMOTE | cut -d: -f2)
6194                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6195
6196                 rc=0
6197                 LPORT=`echo $LOCAL | cut -d: -f2`
6198                 if [ $LPORT -ge 1024 ]; then
6199                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6200                         netstat -tna
6201                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6202                 fi
6203         done
6204         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6205 }
6206 run_test 100 "check local port using privileged port ==========="
6207
6208 function get_named_value()
6209 {
6210     local tag
6211
6212     tag=$1
6213     while read ;do
6214         line=$REPLY
6215         case $line in
6216         $tag*)
6217             echo $line | sed "s/^$tag[ ]*//"
6218             break
6219             ;;
6220         esac
6221     done
6222 }
6223
6224 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6225                    awk '/^max_cached_mb/ { print $2 }')
6226
6227 cleanup_101a() {
6228         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6229         trap 0
6230 }
6231
6232 test_101a() {
6233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6234         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6235         local s
6236         local discard
6237         local nreads=10000
6238         local cache_limit=32
6239
6240         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6241         trap cleanup_101a EXIT
6242         $LCTL set_param -n llite.*.read_ahead_stats 0
6243         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6244
6245         #
6246         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6247         #
6248         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6249         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6250
6251         discard=0
6252         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6253                 get_named_value 'read but discarded' | cut -d" " -f1); do
6254                         discard=$(($discard + $s))
6255         done
6256         cleanup_101a
6257
6258         if [[ $(($discard * 10)) -gt $nreads ]]; then
6259                 $LCTL get_param osc.*-osc*.rpc_stats
6260                 $LCTL get_param llite.*.read_ahead_stats
6261                 error "too many ($discard) discarded pages"
6262         fi
6263         rm -f $DIR/$tfile || true
6264 }
6265 run_test 101a "check read-ahead for random reads ================"
6266
6267 setup_test101bc() {
6268         test_mkdir -p $DIR/$tdir
6269         local STRIPE_SIZE=$1
6270         local FILE_LENGTH=$2
6271         STRIPE_OFFSET=0
6272
6273         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6274
6275         local list=$(comma_list $(osts_nodes))
6276         set_osd_param $list '' read_cache_enable 0
6277         set_osd_param $list '' writethrough_cache_enable 0
6278
6279         trap cleanup_test101bc EXIT
6280         # prepare the read-ahead file
6281         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6282
6283         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6284                                 count=$FILE_SIZE_MB 2> /dev/null
6285
6286 }
6287
6288 cleanup_test101bc() {
6289         trap 0
6290         rm -rf $DIR/$tdir
6291         rm -f $DIR/$tfile
6292
6293         local list=$(comma_list $(osts_nodes))
6294         set_osd_param $list '' read_cache_enable 1
6295         set_osd_param $list '' writethrough_cache_enable 1
6296 }
6297
6298 calc_total() {
6299         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6300 }
6301
6302 ra_check_101() {
6303         local READ_SIZE=$1
6304         local STRIPE_SIZE=$2
6305         local FILE_LENGTH=$3
6306         local RA_INC=1048576
6307         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6308         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6309                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6310         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6311                         get_named_value 'read but discarded' |
6312                         cut -d" " -f1 | calc_total)
6313         if [[ $DISCARD -gt $discard_limit ]]; then
6314                 $LCTL get_param llite.*.read_ahead_stats
6315                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6316         else
6317                 echo "Read-ahead success for size ${READ_SIZE}"
6318         fi
6319 }
6320
6321 test_101b() {
6322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6323         [[ $OSTCOUNT -lt 2 ]] &&
6324                 skip_env "skipping stride IO stride-ahead test" && return
6325         local STRIPE_SIZE=1048576
6326         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6327         if [ $SLOW == "yes" ]; then
6328                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6329         else
6330                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6331         fi
6332
6333         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6334
6335         # prepare the read-ahead file
6336         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6337         cancel_lru_locks osc
6338         for BIDX in 2 4 8 16 32 64 128 256
6339         do
6340                 local BSIZE=$((BIDX*4096))
6341                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6342                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6343                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6344                 $LCTL set_param -n llite.*.read_ahead_stats 0
6345                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6346                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6347                 cancel_lru_locks osc
6348                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6349         done
6350         cleanup_test101bc
6351         true
6352 }
6353 run_test 101b "check stride-io mode read-ahead ================="
6354
6355 test_101c() {
6356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6357         local STRIPE_SIZE=1048576
6358         local FILE_LENGTH=$((STRIPE_SIZE*100))
6359         local nreads=10000
6360         local osc_rpc_stats
6361
6362         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6363
6364         cancel_lru_locks osc
6365         $LCTL set_param osc.*.rpc_stats 0
6366         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6367         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6368                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6369                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6370                 local size
6371
6372                 if [ $lines -le 20 ]; then
6373                         continue
6374                 fi
6375                 for size in 1 2 4 8; do
6376                         local rpc=$(echo "$stats" |
6377                                     awk '($1 == "'$size':") {print $2; exit; }')
6378                         [ $rpc != 0 ] &&
6379                                 error "Small $((size*4))k read IO $rpc !"
6380                 done
6381                 echo "$osc_rpc_stats check passed!"
6382         done
6383         cleanup_test101bc
6384         true
6385 }
6386 run_test 101c "check stripe_size aligned read-ahead ================="
6387
6388 set_read_ahead() {
6389         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6390         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6391 }
6392
6393 test_101d() {
6394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6395         local file=$DIR/$tfile
6396         local sz_MB=${FILESIZE_101d:-500}
6397         local ra_MB=${READAHEAD_MB:-40}
6398
6399         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6400         [ $free_MB -lt $sz_MB ] &&
6401                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6402
6403         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6404         $SETSTRIPE -c -1 $file || error "setstripe failed"
6405
6406         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6407         echo Cancel LRU locks on lustre client to flush the client cache
6408         cancel_lru_locks osc
6409
6410         echo Disable read-ahead
6411         local old_READAHEAD=$(set_read_ahead 0)
6412
6413         echo Reading the test file $file with read-ahead disabled
6414         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6415
6416         echo Cancel LRU locks on lustre client to flush the client cache
6417         cancel_lru_locks osc
6418         echo Enable read-ahead with ${ra_MB}MB
6419         set_read_ahead $ra_MB
6420
6421         echo Reading the test file $file with read-ahead enabled
6422         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6423
6424         echo "read-ahead disabled time read $raOFF"
6425         echo "read-ahead enabled  time read $raON"
6426
6427         set_read_ahead $old_READAHEAD
6428         rm -f $file
6429         wait_delete_completed
6430
6431         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6432                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6433 }
6434 run_test 101d "file read with and without read-ahead enabled"
6435
6436 test_101e() {
6437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6438         local file=$DIR/$tfile
6439         local size_KB=500  #KB
6440         local count=100
6441         local bsize=1024
6442
6443         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6444         local need_KB=$((count * size_KB))
6445         [[ $free_KB -le $need_KB ]] &&
6446                 skip_env "Need free space $need_KB, have $free_KB" && return
6447
6448         echo "Creating $count ${size_KB}K test files"
6449         for ((i = 0; i < $count; i++)); do
6450                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6451         done
6452
6453         echo "Cancel LRU locks on lustre client to flush the client cache"
6454         cancel_lru_locks osc
6455
6456         echo "Reset readahead stats"
6457         $LCTL set_param -n llite.*.read_ahead_stats 0
6458
6459         for ((i = 0; i < $count; i++)); do
6460                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6461         done
6462
6463         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6464                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6465
6466         for ((i = 0; i < $count; i++)); do
6467                 rm -rf $file.$i 2>/dev/null
6468         done
6469
6470         #10000 means 20% reads are missing in readahead
6471         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6472 }
6473 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6474
6475 cleanup_test101f() {
6476     trap 0
6477     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6478     rm -rf $DIR/$tfile 2>/dev/null
6479 }
6480
6481 test_101f() {
6482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6483     local file=$DIR/$tfile
6484     local nreads=1000
6485
6486     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6487     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6488     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6489     trap cleanup_test101f EXIT
6490
6491     echo Cancel LRU locks on lustre client to flush the client cache
6492     cancel_lru_locks osc
6493
6494     echo Reset readahead stats
6495     $LCTL set_param -n llite.*.read_ahead_stats 0
6496     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6497     # readahead should read in 2M file on second read, so only miss
6498     # 2 pages.
6499     echo Random 4K reads on 2M file for 1000 times
6500     $READS -f $file -s 2097152 -b 4096 -n $nreads
6501
6502     echo checking missing pages
6503     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6504           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6505
6506     [ $miss -lt 3 ] || error "misses too much pages!"
6507     cleanup_test101f
6508 }
6509 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6510
6511 setup_test102() {
6512         test_mkdir -p $DIR/$tdir
6513         chown $RUNAS_ID $DIR/$tdir
6514         STRIPE_SIZE=65536
6515         STRIPE_OFFSET=1
6516         STRIPE_COUNT=$OSTCOUNT
6517         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6518
6519         trap cleanup_test102 EXIT
6520         cd $DIR
6521         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6522         cd $DIR/$tdir
6523         for num in 1 2 3 4; do
6524                 for count in $(seq 1 $STRIPE_COUNT); do
6525                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6526                                 local size=`expr $STRIPE_SIZE \* $num`
6527                                 local file=file"$num-$idx-$count"
6528                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6529                         done
6530                 done
6531         done
6532
6533         cd $DIR
6534         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6535 }
6536
6537 cleanup_test102() {
6538         trap 0
6539         rm -f $TMP/f102.tar
6540         rm -rf $DIR/d0.sanity/d102
6541 }
6542
6543 test_102a() {
6544         local testfile=$DIR/$tfile
6545
6546         touch $testfile
6547
6548         [ "$UID" != 0 ] && skip_env "must run as root" && return
6549         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6550                 skip_env "must have user_xattr" && return
6551
6552         [ -z "$(which setfattr 2>/dev/null)" ] &&
6553                 skip_env "could not find setfattr" && return
6554
6555         echo "set/get xattr..."
6556         setfattr -n trusted.name1 -v value1 $testfile ||
6557                 error "setfattr -n trusted.name1=value1 $testfile failed"
6558         getfattr -n trusted.name1 $testfile 2> /dev/null |
6559           grep "trusted.name1=.value1" ||
6560                 error "$testfile missing trusted.name1=value1"
6561
6562         setfattr -n user.author1 -v author1 $testfile ||
6563                 error "setfattr -n user.author1=author1 $testfile failed"
6564         getfattr -n user.author1 $testfile 2> /dev/null |
6565           grep "user.author1=.author1" ||
6566                 error "$testfile missing trusted.author1=author1"
6567
6568         echo "listxattr..."
6569         setfattr -n trusted.name2 -v value2 $testfile ||
6570                 error "$testfile unable to set trusted.name2"
6571         setfattr -n trusted.name3 -v value3 $testfile ||
6572                 error "$testfile unable to set trusted.name3"
6573         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6574             grep "trusted.name" | wc -l) -eq 3 ] ||
6575                 error "$testfile missing 3 trusted.name xattrs"
6576
6577         setfattr -n user.author2 -v author2 $testfile ||
6578                 error "$testfile unable to set user.author2"
6579         setfattr -n user.author3 -v author3 $testfile ||
6580                 error "$testfile unable to set user.author3"
6581         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6582             grep "user.author" | wc -l) -eq 3 ] ||
6583                 error "$testfile missing 3 user.author xattrs"
6584
6585         echo "remove xattr..."
6586         setfattr -x trusted.name1 $testfile ||
6587                 error "$testfile error deleting trusted.name1"
6588         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6589                 error "$testfile did not delete trusted.name1 xattr"
6590
6591         setfattr -x user.author1 $testfile ||
6592                 error "$testfile error deleting user.author1"
6593         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6594                 error "$testfile did not delete trusted.name1 xattr"
6595
6596         # b10667: setting lustre special xattr be silently discarded
6597         echo "set lustre special xattr ..."
6598         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6599                 error "$testfile allowed setting trusted.lov"
6600 }
6601 run_test 102a "user xattr test =================================="
6602
6603 test_102b() {
6604         [ -z "$(which setfattr 2>/dev/null)" ] &&
6605                 skip_env "could not find setfattr" && return
6606
6607         # b10930: get/set/list trusted.lov xattr
6608         echo "get/set/list trusted.lov xattr ..."
6609         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6610         local testfile=$DIR/$tfile
6611         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6612                 error "setstripe failed"
6613         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6614                 error "getstripe failed"
6615         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6616                 error "can't get trusted.lov from $testfile"
6617
6618         local testfile2=${testfile}2
6619         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6620                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6621
6622         $MCREATE $testfile2
6623         setfattr -n trusted.lov -v $value $testfile2
6624         local stripe_size=$($GETSTRIPE -S $testfile2)
6625         local stripe_count=$($GETSTRIPE -c $testfile2)
6626         [[ $stripe_size -eq 65536 ]] ||
6627                 error "stripe size $stripe_size != 65536"
6628         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6629                 error "stripe count $stripe_count != $STRIPECOUNT"
6630         rm -f $DIR/$tfile
6631 }
6632 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6633
6634 test_102c() {
6635         [ -z "$(which setfattr 2>/dev/null)" ] &&
6636                 skip_env "could not find setfattr" && return
6637
6638         # b10930: get/set/list lustre.lov xattr
6639         echo "get/set/list lustre.lov xattr ..."
6640         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6641         test_mkdir -p $DIR/$tdir
6642         chown $RUNAS_ID $DIR/$tdir
6643         local testfile=$DIR/$tdir/$tfile
6644         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6645                 error "setstripe failed"
6646         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6647                 error "getstripe failed"
6648         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6649         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6650
6651         local testfile2=${testfile}2
6652         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6653                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6654
6655         $RUNAS $MCREATE $testfile2
6656         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6657         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6658         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6659         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6660         [ $stripe_count -eq $STRIPECOUNT ] ||
6661                 error "stripe count $stripe_count != $STRIPECOUNT"
6662 }
6663 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6664
6665 compare_stripe_info1() {
6666         local stripe_index_all_zero=true
6667
6668         for num in 1 2 3 4; do
6669                 for count in $(seq 1 $STRIPE_COUNT); do
6670                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6671                                 local size=$((STRIPE_SIZE * num))
6672                                 local file=file"$num-$offset-$count"
6673                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6674                                 [[ $stripe_size -ne $size ]] &&
6675                                     error "$file: size $stripe_size != $size"
6676                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6677                                 # allow fewer stripes to be created, ORI-601
6678                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6679                                     error "$file: count $stripe_count != $count"
6680                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6681                                 [[ $stripe_index -ne 0 ]] &&
6682                                         stripe_index_all_zero=false
6683                         done
6684                 done
6685         done
6686         $stripe_index_all_zero &&
6687                 error "all files are being extracted starting from OST index 0"
6688         return 0
6689 }
6690
6691 find_lustre_tar() {
6692         [ -n "$(which tar 2>/dev/null)" ] &&
6693                 strings $(which tar) | grep -q "lustre" && echo tar
6694 }
6695
6696 test_102d() {
6697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6698         # b10930: tar test for trusted.lov xattr
6699         TAR=$(find_lustre_tar)
6700         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6701         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6702         setup_test102
6703         test_mkdir -p $DIR/d102d
6704         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6705         cd $DIR/d102d/$tdir
6706         compare_stripe_info1
6707 }
6708 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6709
6710 test_102f() {
6711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6712         # b10930: tar test for trusted.lov xattr
6713         TAR=$(find_lustre_tar)
6714         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6715         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6716         setup_test102
6717         test_mkdir -p $DIR/d102f
6718         cd $DIR
6719         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6720         cd $DIR/d102f/$tdir
6721         compare_stripe_info1
6722 }
6723 run_test 102f "tar copy files, not keep osts ==========="
6724
6725 grow_xattr() {
6726         local xsize=${1:-1024}  # in bytes
6727         local file=$DIR/$tfile
6728
6729         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6730                 skip "must have user_xattr" && return 0
6731         [ -z "$(which setfattr 2>/dev/null)" ] &&
6732                 skip_env "could not find setfattr" && return 0
6733         [ -z "$(which getfattr 2>/dev/null)" ] &&
6734                 skip_env "could not find getfattr" && return 0
6735
6736         touch $file
6737
6738         local value="$(generate_string $xsize)"
6739
6740         local xbig=trusted.big
6741         log "save $xbig on $file"
6742         setfattr -n $xbig -v $value $file ||
6743                 error "saving $xbig on $file failed"
6744
6745         local orig=$(get_xattr_value $xbig $file)
6746         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6747
6748         local xsml=trusted.sml
6749         log "save $xsml on $file"
6750         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6751
6752         local new=$(get_xattr_value $xbig $file)
6753         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6754
6755         log "grow $xsml on $file"
6756         setfattr -n $xsml -v "$value" $file ||
6757                 error "growing $xsml on $file failed"
6758
6759         new=$(get_xattr_value $xbig $file)
6760         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6761         log "$xbig still valid after growing $xsml"
6762
6763         rm -f $file
6764 }
6765
6766 test_102h() { # bug 15777
6767         grow_xattr 1024
6768 }
6769 run_test 102h "grow xattr from inside inode to external block"
6770
6771 test_102ha() {
6772         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6773         grow_xattr $(max_xattr_size)
6774 }
6775 run_test 102ha "grow xattr from inside inode to external inode"
6776
6777 test_102i() { # bug 17038
6778         [ -z "$(which getfattr 2>/dev/null)" ] &&
6779                 skip "could not find getfattr" && return
6780         touch $DIR/$tfile
6781         ln -s $DIR/$tfile $DIR/${tfile}link
6782         getfattr -n trusted.lov $DIR/$tfile ||
6783                 error "lgetxattr on $DIR/$tfile failed"
6784         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6785                 grep -i "no such attr" ||
6786                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6787         rm -f $DIR/$tfile $DIR/${tfile}link
6788 }
6789 run_test 102i "lgetxattr test on symbolic link ============"
6790
6791 test_102j() {
6792         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6793         TAR=$(find_lustre_tar)
6794         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6795         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6796         setup_test102 "$RUNAS"
6797         test_mkdir -p $DIR/d102j
6798         chown $RUNAS_ID $DIR/d102j
6799         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6800         cd $DIR/d102j/$tdir
6801         compare_stripe_info1 "$RUNAS"
6802 }
6803 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6804
6805 test_102k() {
6806         [ -z "$(which setfattr 2>/dev/null)" ] &&
6807                 skip "could not find setfattr" && return
6808         touch $DIR/$tfile
6809         # b22187 just check that does not crash for regular file.
6810         setfattr -n trusted.lov $DIR/$tfile
6811         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6812         local test_kdir=$DIR/d102k
6813         test_mkdir $test_kdir
6814         local default_size=`$GETSTRIPE -S $test_kdir`
6815         local default_count=`$GETSTRIPE -c $test_kdir`
6816         local default_offset=`$GETSTRIPE -i $test_kdir`
6817         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6818                 error 'dir setstripe failed'
6819         setfattr -n trusted.lov $test_kdir
6820         local stripe_size=`$GETSTRIPE -S $test_kdir`
6821         local stripe_count=`$GETSTRIPE -c $test_kdir`
6822         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6823         [ $stripe_size -eq $default_size ] ||
6824                 error "stripe size $stripe_size != $default_size"
6825         [ $stripe_count -eq $default_count ] ||
6826                 error "stripe count $stripe_count != $default_count"
6827         [ $stripe_offset -eq $default_offset ] ||
6828                 error "stripe offset $stripe_offset != $default_offset"
6829         rm -rf $DIR/$tfile $test_kdir
6830 }
6831 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6832
6833 test_102l() {
6834         [ -z "$(which getfattr 2>/dev/null)" ] &&
6835                 skip "could not find getfattr" && return
6836
6837         # LU-532 trusted. xattr is invisible to non-root
6838         local testfile=$DIR/$tfile
6839
6840         touch $testfile
6841
6842         echo "listxattr as user..."
6843         chown $RUNAS_ID $testfile
6844         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6845             grep -q "trusted" &&
6846                 error "$testfile trusted xattrs are user visible"
6847
6848         return 0;
6849 }
6850 run_test 102l "listxattr size test =================================="
6851
6852 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6853         local path=$DIR/$tfile
6854         touch $path
6855
6856         listxattr_size_check $path || error "listattr_size_check $path failed"
6857 }
6858 run_test 102m "Ensure listxattr fails on small bufffer ========"
6859
6860 cleanup_test102
6861
6862 getxattr() { # getxattr path name
6863         # Return the base64 encoding of the value of xattr name on path.
6864         local path=$1
6865         local name=$2
6866
6867         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6868         # file: $path
6869         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6870         #
6871         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6872
6873         getfattr --absolute-names --encoding=base64 --name=$name $path |
6874                 awk -F= -v name=$name '$1 == name {
6875                         print substr($0, index($0, "=") + 1);
6876         }'
6877 }
6878
6879 test_102n() { # LU-4101 mdt: protect internal xattrs
6880         local file0=$DIR/$tfile.0
6881         local file1=$DIR/$tfile.1
6882         local xattr0=$TMP/$tfile.0
6883         local xattr1=$TMP/$tfile.1
6884         local name
6885         local value
6886
6887         [ -z "$(which setfattr 2>/dev/null)" ] &&
6888                 skip "could not find setfattr" && return
6889
6890         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6891         then
6892                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6893                 return
6894         fi
6895
6896         rm -rf $file0 $file1 $xattr0 $xattr1
6897         touch $file0 $file1
6898
6899         # Get 'before' xattrs of $file1.
6900         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6901
6902         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6903                 # Try to copy xattr from $file0 to $file1.
6904                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6905
6906                 setfattr --name=trusted.$name --value="$value" $file1 ||
6907                         error "setxattr 'trusted.$name' failed"
6908
6909                 # Try to set a garbage xattr.
6910                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6911
6912                 setfattr --name=trusted.$name --value="$value" $file1 ||
6913                         error "setxattr 'trusted.$name' failed"
6914
6915                 # Try to remove the xattr from $file1. We don't care if this
6916                 # appears to succeed or fail, we just don't want there to be
6917                 # any changes or crashes.
6918                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6919         done
6920
6921         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6922         then
6923                 name="lfsck_ns"
6924                 # Try to copy xattr from $file0 to $file1.
6925                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6926
6927                 setfattr --name=trusted.$name --value="$value" $file1 ||
6928                         error "setxattr 'trusted.$name' failed"
6929
6930                 # Try to set a garbage xattr.
6931                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6932
6933                 setfattr --name=trusted.$name --value="$value" $file1 ||
6934                         error "setxattr 'trusted.$name' failed"
6935
6936                 # Try to remove the xattr from $file1. We don't care if this
6937                 # appears to succeed or fail, we just don't want there to be
6938                 # any changes or crashes.
6939                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6940         fi
6941
6942         # Get 'after' xattrs of file1.
6943         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6944
6945         if ! diff $xattr0 $xattr1; then
6946                 error "before and after xattrs of '$file1' differ"
6947         fi
6948
6949         rm -rf $file0 $file1 $xattr0 $xattr1
6950
6951         return 0
6952 }
6953 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6954
6955 test_102p() { # LU-4703 setxattr did not check ownership
6956         local testfile=$DIR/$tfile
6957
6958         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6959                 skip "MDS needs to be at least 2.5.56" && return
6960
6961         touch $testfile
6962
6963         echo "setfacl as user..."
6964         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6965         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6966
6967         echo "setfattr as user..."
6968         setfacl -m "u:$RUNAS_ID:---" $testfile
6969         $RUNAS setfattr -x system.posix_acl_access $testfile
6970         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6971 }
6972 run_test 102p "check setxattr(2) correctly fails without permission"
6973
6974 test_102q() {
6975         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
6976 }
6977 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
6978
6979 test_102r() {
6980         touch $DIR/$tfile || error "touch"
6981         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
6982         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
6983         rm $DIR/$tfile || error "rm"
6984 }
6985 run_test 102r "set EAs with empty values"
6986
6987 run_acl_subtest()
6988 {
6989     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6990     return $?
6991 }
6992
6993 test_103a() {
6994         [ "$UID" != 0 ] && skip_env "must run as root" && return
6995         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6996                 skip "must have acl enabled" && return
6997         [ -z "$(which setfacl 2>/dev/null)" ] &&
6998                 skip_env "could not find setfacl" && return
6999         $GSS && skip "could not run under gss" && return
7000
7001         gpasswd -a daemon bin                           # LU-5641
7002         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7003
7004         declare -a identity_old
7005
7006         for num in $(seq $MDSCOUNT); do
7007                 switch_identity $num true || identity_old[$num]=$?
7008         done
7009
7010         SAVE_UMASK=$(umask)
7011         umask 0022
7012         cd $DIR
7013
7014         echo "performing cp ..."
7015         run_acl_subtest cp || error "run_acl_subtest cp failed"
7016         echo "performing getfacl-noacl..."
7017         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7018         echo "performing misc..."
7019         run_acl_subtest misc || error  "misc test failed"
7020         echo "performing permissions..."
7021         run_acl_subtest permissions || error "permissions failed"
7022         echo "performing setfacl..."
7023         run_acl_subtest setfacl || error  "setfacl test failed"
7024
7025         # inheritance test got from HP
7026         echo "performing inheritance..."
7027         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7028         chmod +x make-tree || error "chmod +x failed"
7029         run_acl_subtest inheritance || error "inheritance test failed"
7030         rm -f make-tree
7031
7032         echo "LU-974 ignore umask when acl is enabled..."
7033         run_acl_subtest 974 || error "LU-974 umask test failed"
7034         if [ $MDSCOUNT -ge 2 ]; then
7035                 run_acl_subtest 974_remote ||
7036                         error "LU-974 umask test failed under remote dir"
7037         fi
7038
7039         echo "LU-2561 newly created file is same size as directory..."
7040         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7041                 run_acl_subtest 2561 || error "LU-2561 test failed"
7042         else
7043                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7044         fi
7045
7046         run_acl_subtest 4924 || error "LU-4924 test failed"
7047
7048         cd $SAVE_PWD
7049         umask $SAVE_UMASK
7050
7051         for num in $(seq $MDSCOUNT); do
7052                 if [ "${identity_old[$num]}" = 1 ]; then
7053                         switch_identity $num false || identity_old[$num]=$?
7054                 fi
7055         done
7056 }
7057 run_test 103a "acl test ========================================="
7058
7059 test_103b() {
7060         local noacl=false
7061         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7062         local mountopts=$MDS_MOUNT_OPTS
7063
7064         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7065                 noacl=true
7066         else
7067                 # stop the MDT
7068                 stop $SINGLEMDS || error "failed to stop MDT."
7069                 # remount the MDT
7070                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7071                         MDS_MOUNT_OPTS="-o noacl"
7072                 else
7073                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7074                 fi
7075                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7076                         error "failed to start MDT."
7077                 MDS_MOUNT_OPTS=$mountopts
7078         fi
7079
7080         touch $DIR/$tfile
7081         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7082
7083         if ! $noacl; then
7084                 # stop the MDT
7085                 stop $SINGLEMDS || error "failed to stop MDT."
7086                 # remount the MDT
7087                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7088                         error "failed to start MDT."
7089         fi
7090
7091         true
7092 }
7093 run_test 103b "MDS mount option 'noacl'"
7094
7095 test_103c() {
7096         mkdir -p $DIR/$tdir
7097         cp -rp $DIR/$tdir $DIR/$tdir.bak
7098
7099         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7100                 error "$DIR/$tdir shouldn't contain default ACL"
7101         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7102                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7103         true
7104 }
7105 run_test 103c "'cp -rp' won't set empty acl"
7106
7107 test_104a() {
7108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7109         touch $DIR/$tfile
7110         lfs df || error "lfs df failed"
7111         lfs df -ih || error "lfs df -ih failed"
7112         lfs df -h $DIR || error "lfs df -h $DIR failed"
7113         lfs df -i $DIR || error "lfs df -i $DIR failed"
7114         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7115         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7116
7117         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7118         lctl --device %$OSC deactivate
7119         lfs df || error "lfs df with deactivated OSC failed"
7120         lctl --device %$OSC activate
7121         # wait the osc back to normal
7122         wait_osc_import_state client ost FULL
7123
7124         lfs df || error "lfs df with reactivated OSC failed"
7125         rm -f $DIR/$tfile
7126 }
7127 run_test 104a "lfs df [-ih] [path] test ========================="
7128
7129 test_104b() {
7130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7131         [ $RUNAS_ID -eq $UID ] &&
7132                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7133         chmod 666 /dev/obd
7134         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7135                         grep "Permission denied" | wc -l)))
7136         if [ $denied_cnt -ne 0 ]; then
7137                 error "lfs check servers test failed"
7138         fi
7139 }
7140 run_test 104b "$RUNAS lfs check servers test ===================="
7141
7142 test_105a() {
7143         # doesn't work on 2.4 kernels
7144         touch $DIR/$tfile
7145         if $(flock_is_enabled); then
7146                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7147         else
7148                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7149         fi
7150         rm -f $DIR/$tfile
7151 }
7152 run_test 105a "flock when mounted without -o flock test ========"
7153
7154 test_105b() {
7155         touch $DIR/$tfile
7156         if $(flock_is_enabled); then
7157                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7158         else
7159                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7160         fi
7161         rm -f $DIR/$tfile
7162 }
7163 run_test 105b "fcntl when mounted without -o flock test ========"
7164
7165 test_105c() {
7166         touch $DIR/$tfile
7167         if $(flock_is_enabled); then
7168                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7169         else
7170                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7171         fi
7172         rm -f $DIR/$tfile
7173 }
7174 run_test 105c "lockf when mounted without -o flock test ========"
7175
7176 test_105d() { # bug 15924
7177         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7178         test_mkdir -p $DIR/$tdir
7179         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7180         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7181         $LCTL set_param fail_loc=0x80000315
7182         flocks_test 2 $DIR/$tdir
7183 }
7184 run_test 105d "flock race (should not freeze) ========"
7185
7186 test_105e() { # bug 22660 && 22040
7187         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7188         touch $DIR/$tfile
7189         flocks_test 3 $DIR/$tfile
7190 }
7191 run_test 105e "Two conflicting flocks from same process ======="
7192
7193 test_106() { #bug 10921
7194         test_mkdir -p $DIR/$tdir
7195         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7196         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7197 }
7198 run_test 106 "attempt exec of dir followed by chown of that dir"
7199
7200 test_107() {
7201         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7202         CDIR=`pwd`
7203         cd $DIR
7204
7205         local file=core
7206         rm -f $file
7207
7208         local save_pattern=$(sysctl -n kernel.core_pattern)
7209         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7210         sysctl -w kernel.core_pattern=$file
7211         sysctl -w kernel.core_uses_pid=0
7212
7213         ulimit -c unlimited
7214         sleep 60 &
7215         SLEEPPID=$!
7216
7217         sleep 1
7218
7219         kill -s 11 $SLEEPPID
7220         wait $SLEEPPID
7221         if [ -e $file ]; then
7222                 size=`stat -c%s $file`
7223                 [ $size -eq 0 ] && error "Fail to create core file $file"
7224         else
7225                 error "Fail to create core file $file"
7226         fi
7227         rm -f $file
7228         sysctl -w kernel.core_pattern=$save_pattern
7229         sysctl -w kernel.core_uses_pid=$save_uses_pid
7230         cd $CDIR
7231 }
7232 run_test 107 "Coredump on SIG"
7233
7234 test_110() {
7235         test_mkdir -p $DIR/$tdir
7236         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7237                 error "mkdir with 255 char failed"
7238         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7239                 error "mkdir with 256 char should fail, but did not"
7240         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7241                 error "create with 255 char failed"
7242         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7243                 error "create with 256 char should fail, but did not"
7244
7245         ls -l $DIR/$tdir
7246         rm -rf $DIR/$tdir
7247 }
7248 run_test 110 "filename length checking"
7249
7250 test_115() {
7251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7252         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7253                 tail -1 | cut -c11-20)
7254         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7255         echo "Starting with $OSTIO_pre threads"
7256
7257         NUMTEST=20000
7258         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7259         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7260         echo "$NUMTEST creates/unlinks"
7261         test_mkdir -p $DIR/$tdir
7262         createmany -o $DIR/$tdir/$tfile $NUMTEST
7263         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7264
7265         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7266                 tail -1 | cut -c11-20)
7267
7268         # don't return an error
7269         [ $OSTIO_post == $OSTIO_pre ] && echo \
7270             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7271             echo "This may be fine, depending on what ran before this test" &&
7272             echo "and how fast this system is." && return
7273
7274         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7275 }
7276 run_test 115 "verify dynamic thread creation===================="
7277
7278 free_min_max () {
7279         wait_delete_completed
7280         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7281         echo OST kbytes available: ${AVAIL[@]}
7282         MAXI=0; MAXV=${AVAIL[0]}
7283         MINI=0; MINV=${AVAIL[0]}
7284         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7285                 #echo OST $i: ${AVAIL[i]}kb
7286                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7287                         MAXV=${AVAIL[i]}; MAXI=$i
7288                 fi
7289                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7290                         MINV=${AVAIL[i]}; MINI=$i
7291                 fi
7292         done
7293         echo Min free space: OST $MINI: $MINV
7294         echo Max free space: OST $MAXI: $MAXV
7295 }
7296
7297 test_116a() { # was previously test_116()
7298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7299         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7300
7301         echo -n "Free space priority "
7302         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7303                 head -n1
7304         declare -a AVAIL
7305         free_min_max
7306
7307         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7308         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7309                 && return
7310         trap simple_cleanup_common EXIT
7311
7312
7313         # Check if we need to generate uneven OSTs
7314         test_mkdir -p $DIR/$tdir/OST${MINI}
7315         local FILL=$(($MINV / 4))
7316         local DIFF=$(($MAXV - $MINV))
7317         local DIFF2=$(($DIFF * 100 / $MINV))
7318
7319         local threshold=$(do_facet $SINGLEMDS \
7320                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7321         threshold=${threshold%%%}
7322         echo -n "Check for uneven OSTs: "
7323         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7324
7325         if [[ $DIFF2 -gt $threshold ]]; then
7326                 echo "ok"
7327                 echo "Don't need to fill OST$MINI"
7328         else
7329                 # generate uneven OSTs. Write 2% over the QOS threshold value
7330                 echo "no"
7331                 DIFF=$(($threshold - $DIFF2 + 2))
7332                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7333                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7334                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7335                         error "setstripe failed"
7336                 DIFF=$(($DIFF2 / 2048))
7337                 i=0
7338                 while [ $i -lt $DIFF ]; do
7339                         i=$(($i + 1))
7340                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7341                                 bs=2M count=1 2>/dev/null
7342                         echo -n .
7343                 done
7344                 echo .
7345                 sync
7346                 sleep_maxage
7347                 free_min_max
7348         fi
7349
7350         DIFF=$(($MAXV - $MINV))
7351         DIFF2=$(($DIFF * 100 / $MINV))
7352         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7353         if [[ $DIFF2 -gt $threshold ]]; then
7354                 echo "ok"
7355         else
7356                 echo "failed - QOS mode won't be used"
7357                 skip "QOS imbalance criteria not met"
7358                 simple_cleanup_common
7359                 return
7360         fi
7361
7362         MINI1=$MINI; MINV1=$MINV
7363         MAXI1=$MAXI; MAXV1=$MAXV
7364
7365         # now fill using QOS
7366         $SETSTRIPE -c 1 $DIR/$tdir
7367         FILL=$(($FILL / 200))
7368         if [ $FILL -gt 600 ]; then
7369                 FILL=600
7370         fi
7371         echo "writing $FILL files to QOS-assigned OSTs"
7372         i=0
7373         while [ $i -lt $FILL ]; do
7374                 i=$((i + 1))
7375                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7376                         count=1 2>/dev/null
7377                 echo -n .
7378         done
7379         echo "wrote $i 200k files"
7380         sync
7381         sleep_maxage
7382
7383         echo "Note: free space may not be updated, so measurements might be off"
7384         free_min_max
7385         DIFF2=$(($MAXV - $MINV))
7386         echo "free space delta: orig $DIFF final $DIFF2"
7387         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7388         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7389         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7390         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7391         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7392         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7393         [ $DIFF -gt 0 ] &&
7394                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7395
7396         # Figure out which files were written where
7397         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7398                   awk '/'$MINI1': / {print $2; exit}')
7399         echo $UUID
7400         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7401         echo "$MINC files created on smaller OST $MINI1"
7402         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7403                   awk '/'$MAXI1': / {print $2; exit}')
7404         echo $UUID
7405         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7406         echo "$MAXC files created on larger OST $MAXI1"
7407         FILL=$(($MAXC * 100 / $MINC - 100))
7408         [[ $MINC -gt 0 ]] &&
7409                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7410         [[ $MAXC -gt $MINC ]] ||
7411                 error_ignore LU-9 "stripe QOS didn't balance free space"
7412         simple_cleanup_common
7413 }
7414 run_test 116a "stripe QOS: free space balance ==================="
7415
7416 test_116b() { # LU-2093
7417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7418 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7419         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7420                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7421         [ -z "$old_rr" ] && skip "no QOS" && return 0
7422         do_facet $SINGLEMDS lctl set_param \
7423                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7424         mkdir -p $DIR/$tdir
7425         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7426         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7427         do_facet $SINGLEMDS lctl set_param fail_loc=0
7428         rm -rf $DIR/$tdir
7429         do_facet $SINGLEMDS lctl set_param \
7430                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7431 }
7432 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7433
7434 test_117() # bug 10891
7435 {
7436         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7437         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7438         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7439         lctl set_param fail_loc=0x21e
7440         > $DIR/$tfile || error "truncate failed"
7441         lctl set_param fail_loc=0
7442         echo "Truncate succeeded."
7443         rm -f $DIR/$tfile
7444 }
7445 run_test 117 "verify osd extend =========="
7446
7447 NO_SLOW_RESENDCOUNT=4
7448 export OLD_RESENDCOUNT=""
7449 set_resend_count () {
7450         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7451         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7452         lctl set_param -n $PROC_RESENDCOUNT $1
7453         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7454 }
7455
7456 # for reduce test_118* time (b=14842)
7457 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7458
7459 # Reset async IO behavior after error case
7460 reset_async() {
7461         FILE=$DIR/reset_async
7462
7463         # Ensure all OSCs are cleared
7464         $SETSTRIPE -c -1 $FILE
7465         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7466         sync
7467         rm $FILE
7468 }
7469
7470 test_118a() #bug 11710
7471 {
7472         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7473         reset_async
7474
7475         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7476         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7477         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7478
7479         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7480                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7481                 return 1;
7482         fi
7483         rm -f $DIR/$tfile
7484 }
7485 run_test 118a "verify O_SYNC works =========="
7486
7487 test_118b()
7488 {
7489         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7490         remote_ost_nodsh && skip "remote OST with nodsh" && return
7491
7492         reset_async
7493
7494         #define OBD_FAIL_OST_ENOENT 0x217
7495         set_nodes_failloc "$(osts_nodes)" 0x217
7496         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7497         RC=$?
7498         set_nodes_failloc "$(osts_nodes)" 0
7499         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7500         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7501                     grep -c writeback)
7502
7503         if [[ $RC -eq 0 ]]; then
7504                 error "Must return error due to dropped pages, rc=$RC"
7505                 return 1;
7506         fi
7507
7508         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7509                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7510                 return 1;
7511         fi
7512
7513         echo "Dirty pages not leaked on ENOENT"
7514
7515         # Due to the above error the OSC will issue all RPCs syncronously
7516         # until a subsequent RPC completes successfully without error.
7517         $MULTIOP $DIR/$tfile Ow4096yc
7518         rm -f $DIR/$tfile
7519
7520         return 0
7521 }
7522 run_test 118b "Reclaim dirty pages on fatal error =========="
7523
7524 test_118c()
7525 {
7526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7527
7528         # for 118c, restore the original resend count, LU-1940
7529         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7530                                 set_resend_count $OLD_RESENDCOUNT
7531         remote_ost_nodsh && skip "remote OST with nodsh" && return
7532
7533         reset_async
7534
7535         #define OBD_FAIL_OST_EROFS               0x216
7536         set_nodes_failloc "$(osts_nodes)" 0x216
7537
7538         # multiop should block due to fsync until pages are written
7539         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7540         MULTIPID=$!
7541         sleep 1
7542
7543         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7544                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7545         fi
7546
7547         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7548                     grep -c writeback)
7549         if [[ $WRITEBACK -eq 0 ]]; then
7550                 error "No page in writeback, writeback=$WRITEBACK"
7551         fi
7552
7553         set_nodes_failloc "$(osts_nodes)" 0
7554         wait $MULTIPID
7555         RC=$?
7556         if [[ $RC -ne 0 ]]; then
7557                 error "Multiop fsync failed, rc=$RC"
7558         fi
7559
7560         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7561         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7562                     grep -c writeback)
7563         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7564                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7565         fi
7566
7567         rm -f $DIR/$tfile
7568         echo "Dirty pages flushed via fsync on EROFS"
7569         return 0
7570 }
7571 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7572
7573 # continue to use small resend count to reduce test_118* time (b=14842)
7574 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7575
7576 test_118d()
7577 {
7578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7579         remote_ost_nodsh && skip "remote OST with nodsh" && return
7580
7581         reset_async
7582
7583         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7584         set_nodes_failloc "$(osts_nodes)" 0x214
7585         # multiop should block due to fsync until pages are written
7586         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7587         MULTIPID=$!
7588         sleep 1
7589
7590         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7591                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7592         fi
7593
7594         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7595                     grep -c writeback)
7596         if [[ $WRITEBACK -eq 0 ]]; then
7597                 error "No page in writeback, writeback=$WRITEBACK"
7598         fi
7599
7600         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7601         set_nodes_failloc "$(osts_nodes)" 0
7602
7603         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7604         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7605                     grep -c writeback)
7606         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7607                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7608         fi
7609
7610         rm -f $DIR/$tfile
7611         echo "Dirty pages gaurenteed flushed via fsync"
7612         return 0
7613 }
7614 run_test 118d "Fsync validation inject a delay of the bulk =========="
7615
7616 test_118f() {
7617         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7618         reset_async
7619
7620         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7621         lctl set_param fail_loc=0x8000040a
7622
7623         # Should simulate EINVAL error which is fatal
7624         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7625         RC=$?
7626         if [[ $RC -eq 0 ]]; then
7627                 error "Must return error due to dropped pages, rc=$RC"
7628         fi
7629
7630         lctl set_param fail_loc=0x0
7631
7632         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7633         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7634         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7635                     grep -c writeback)
7636         if [[ $LOCKED -ne 0 ]]; then
7637                 error "Locked pages remain in cache, locked=$LOCKED"
7638         fi
7639
7640         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7641                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7642         fi
7643
7644         rm -f $DIR/$tfile
7645         echo "No pages locked after fsync"
7646
7647         reset_async
7648         return 0
7649 }
7650 run_test 118f "Simulate unrecoverable OSC side error =========="
7651
7652 test_118g() {
7653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7654         reset_async
7655
7656         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7657         lctl set_param fail_loc=0x406
7658
7659         # simulate local -ENOMEM
7660         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7661         RC=$?
7662
7663         lctl set_param fail_loc=0
7664         if [[ $RC -eq 0 ]]; then
7665                 error "Must return error due to dropped pages, rc=$RC"
7666         fi
7667
7668         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7669         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7670         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7671                         grep -c writeback)
7672         if [[ $LOCKED -ne 0 ]]; then
7673                 error "Locked pages remain in cache, locked=$LOCKED"
7674         fi
7675
7676         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7677                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7678         fi
7679
7680         rm -f $DIR/$tfile
7681         echo "No pages locked after fsync"
7682
7683         reset_async
7684         return 0
7685 }
7686 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7687
7688 test_118h() {
7689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7690         remote_ost_nodsh && skip "remote OST with nodsh" && return
7691
7692         reset_async
7693
7694         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7695         set_nodes_failloc "$(osts_nodes)" 0x20e
7696         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7697         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7698         RC=$?
7699
7700         set_nodes_failloc "$(osts_nodes)" 0
7701         if [[ $RC -eq 0 ]]; then
7702                 error "Must return error due to dropped pages, rc=$RC"
7703         fi
7704
7705         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7706         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7707         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7708                     grep -c writeback)
7709         if [[ $LOCKED -ne 0 ]]; then
7710                 error "Locked pages remain in cache, locked=$LOCKED"
7711         fi
7712
7713         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7714                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7715         fi
7716
7717         rm -f $DIR/$tfile
7718         echo "No pages locked after fsync"
7719
7720         return 0
7721 }
7722 run_test 118h "Verify timeout in handling recoverables errors  =========="
7723
7724 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7725
7726 test_118i() {
7727         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7728         remote_ost_nodsh && skip "remote OST with nodsh" && return
7729
7730         reset_async
7731
7732         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7733         set_nodes_failloc "$(osts_nodes)" 0x20e
7734
7735         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7736         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7737         PID=$!
7738         sleep 5
7739         set_nodes_failloc "$(osts_nodes)" 0
7740
7741         wait $PID
7742         RC=$?
7743         if [[ $RC -ne 0 ]]; then
7744                 error "got error, but should be not, rc=$RC"
7745         fi
7746
7747         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7748         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7749         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7750         if [[ $LOCKED -ne 0 ]]; then
7751                 error "Locked pages remain in cache, locked=$LOCKED"
7752         fi
7753
7754         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7755                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7756         fi
7757
7758         rm -f $DIR/$tfile
7759         echo "No pages locked after fsync"
7760
7761         return 0
7762 }
7763 run_test 118i "Fix error before timeout in recoverable error  =========="
7764
7765 [ "$SLOW" = "no" ] && set_resend_count 4
7766
7767 test_118j() {
7768         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7769         remote_ost_nodsh && skip "remote OST with nodsh" && return
7770
7771         reset_async
7772
7773         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7774         set_nodes_failloc "$(osts_nodes)" 0x220
7775
7776         # return -EIO from OST
7777         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7778         RC=$?
7779         set_nodes_failloc "$(osts_nodes)" 0x0
7780         if [[ $RC -eq 0 ]]; then
7781                 error "Must return error due to dropped pages, rc=$RC"
7782         fi
7783
7784         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7785         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7786         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7787         if [[ $LOCKED -ne 0 ]]; then
7788                 error "Locked pages remain in cache, locked=$LOCKED"
7789         fi
7790
7791         # in recoverable error on OST we want resend and stay until it finished
7792         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7793                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7794         fi
7795
7796         rm -f $DIR/$tfile
7797         echo "No pages locked after fsync"
7798
7799         return 0
7800 }
7801 run_test 118j "Simulate unrecoverable OST side error =========="
7802
7803 test_118k()
7804 {
7805         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7806         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7807
7808         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7809         set_nodes_failloc "$(osts_nodes)" 0x20e
7810         test_mkdir -p $DIR/$tdir
7811
7812         for ((i=0;i<10;i++)); do
7813                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7814                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7815                 SLEEPPID=$!
7816                 sleep 0.500s
7817                 kill $SLEEPPID
7818                 wait $SLEEPPID
7819         done
7820
7821         set_nodes_failloc "$(osts_nodes)" 0
7822         rm -rf $DIR/$tdir
7823 }
7824 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7825
7826 test_118l()
7827 {
7828         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7829         # LU-646
7830         test_mkdir -p $DIR/$tdir
7831         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7832         rm -rf $DIR/$tdir
7833 }
7834 run_test 118l "fsync dir ========="
7835
7836 test_118m() # LU-3066
7837 {
7838         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7839         test_mkdir -p $DIR/$tdir
7840         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7841         rm -rf $DIR/$tdir
7842 }
7843 run_test 118m "fdatasync dir ========="
7844
7845 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7846
7847 test_119a() # bug 11737
7848 {
7849         BSIZE=$((512 * 1024))
7850         directio write $DIR/$tfile 0 1 $BSIZE
7851         # We ask to read two blocks, which is more than a file size.
7852         # directio will indicate an error when requested and actual
7853         # sizes aren't equeal (a normal situation in this case) and
7854         # print actual read amount.
7855         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7856         if [ "$NOB" != "$BSIZE" ]; then
7857                 error "read $NOB bytes instead of $BSIZE"
7858         fi
7859         rm -f $DIR/$tfile
7860 }
7861 run_test 119a "Short directIO read must return actual read amount"
7862
7863 test_119b() # bug 11737
7864 {
7865         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7866
7867         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7868         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7869         sync
7870         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7871                 error "direct read failed"
7872         rm -f $DIR/$tfile
7873 }
7874 run_test 119b "Sparse directIO read must return actual read amount"
7875
7876 test_119c() # bug 13099
7877 {
7878         BSIZE=1048576
7879         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7880         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7881         rm -f $DIR/$tfile
7882 }
7883 run_test 119c "Testing for direct read hitting hole"
7884
7885 test_119d() # bug 15950
7886 {
7887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7888         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7889         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7890         BSIZE=1048576
7891         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7892         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7893         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7894         lctl set_param fail_loc=0x40d
7895         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7896         pid_dio=$!
7897         sleep 1
7898         cat $DIR/$tfile > /dev/null &
7899         lctl set_param fail_loc=0
7900         pid_reads=$!
7901         wait $pid_dio
7902         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7903         sleep 2
7904         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7905         error "the read rpcs have not completed in 2s"
7906         rm -f $DIR/$tfile
7907         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7908 }
7909 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7910
7911 test_120a() {
7912         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7913         test_mkdir -p $DIR/$tdir
7914         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7915                skip "no early lock cancel on server" && return 0
7916
7917         lru_resize_disable mdc
7918         lru_resize_disable osc
7919         cancel_lru_locks mdc
7920         # asynchronous object destroy at MDT could cause bl ast to client
7921         cancel_lru_locks osc
7922
7923         stat $DIR/$tdir > /dev/null
7924         can1=$(do_facet $SINGLEMDS \
7925                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7926                awk '/ldlm_cancel/ {print $2}')
7927         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7928                awk '/ldlm_bl_callback/ {print $2}')
7929         test_mkdir -c1 $DIR/$tdir/d1
7930         can2=$(do_facet $SINGLEMDS \
7931                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7932                awk '/ldlm_cancel/ {print $2}')
7933         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7934                awk '/ldlm_bl_callback/ {print $2}')
7935         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7936         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7937         lru_resize_enable mdc
7938         lru_resize_enable osc
7939 }
7940 run_test 120a "Early Lock Cancel: mkdir test"
7941
7942 test_120b() {
7943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7944         test_mkdir $DIR/$tdir
7945         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7946                skip "no early lock cancel on server" && return 0
7947         lru_resize_disable mdc
7948         lru_resize_disable osc
7949         cancel_lru_locks mdc
7950         stat $DIR/$tdir > /dev/null
7951         can1=$(do_facet $SINGLEMDS \
7952                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7953                awk '/ldlm_cancel/ {print $2}')
7954         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7955                awk '/ldlm_bl_callback/ {print $2}')
7956         touch $DIR/$tdir/f1
7957         can2=$(do_facet $SINGLEMDS \
7958                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7959                awk '/ldlm_cancel/ {print $2}')
7960         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7961                awk '/ldlm_bl_callback/ {print $2}')
7962         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7963         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7964         lru_resize_enable mdc
7965         lru_resize_enable osc
7966 }
7967 run_test 120b "Early Lock Cancel: create test"
7968
7969 test_120c() {
7970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7971         test_mkdir -c1 $DIR/$tdir
7972         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7973                skip "no early lock cancel on server" && return 0
7974         lru_resize_disable mdc
7975         lru_resize_disable osc
7976         test_mkdir -p -c1 $DIR/$tdir/d1
7977         test_mkdir -p -c1 $DIR/$tdir/d2
7978         touch $DIR/$tdir/d1/f1
7979         cancel_lru_locks mdc
7980         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7981         can1=$(do_facet $SINGLEMDS \
7982                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7983                awk '/ldlm_cancel/ {print $2}')
7984         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7985                awk '/ldlm_bl_callback/ {print $2}')
7986         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7987         can2=$(do_facet $SINGLEMDS \
7988                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7989                awk '/ldlm_cancel/ {print $2}')
7990         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7991                awk '/ldlm_bl_callback/ {print $2}')
7992         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7993         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7994         lru_resize_enable mdc
7995         lru_resize_enable osc
7996 }
7997 run_test 120c "Early Lock Cancel: link test"
7998
7999 test_120d() {
8000         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8001         test_mkdir -p -c1 $DIR/$tdir
8002         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8003                skip "no early lock cancel on server" && return 0
8004         lru_resize_disable mdc
8005         lru_resize_disable osc
8006         touch $DIR/$tdir
8007         cancel_lru_locks mdc
8008         stat $DIR/$tdir > /dev/null
8009         can1=$(do_facet $SINGLEMDS \
8010                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8011                awk '/ldlm_cancel/ {print $2}')
8012         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8013                awk '/ldlm_bl_callback/ {print $2}')
8014         chmod a+x $DIR/$tdir
8015         can2=$(do_facet $SINGLEMDS \
8016                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8017                awk '/ldlm_cancel/ {print $2}')
8018         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8019                awk '/ldlm_bl_callback/ {print $2}')
8020         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8021         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8022         lru_resize_enable mdc
8023         lru_resize_enable osc
8024 }
8025 run_test 120d "Early Lock Cancel: setattr test"
8026
8027 test_120e() {
8028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8029         test_mkdir -p -c1 $DIR/$tdir
8030         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8031                skip "no early lock cancel on server" && return 0
8032         lru_resize_disable mdc
8033         lru_resize_disable osc
8034         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8035         cancel_lru_locks mdc
8036         cancel_lru_locks osc
8037         dd if=$DIR/$tdir/f1 of=/dev/null
8038         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8039         # XXX client can not do early lock cancel of OST lock
8040         # during unlink (LU-4206), so cancel osc lock now.
8041         cancel_lru_locks osc
8042         can1=$(do_facet $SINGLEMDS \
8043                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8044                awk '/ldlm_cancel/ {print $2}')
8045         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8046                awk '/ldlm_bl_callback/ {print $2}')
8047         unlink $DIR/$tdir/f1
8048         sleep 5
8049         can2=$(do_facet $SINGLEMDS \
8050                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8051                awk '/ldlm_cancel/ {print $2}')
8052         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8053                awk '/ldlm_bl_callback/ {print $2}')
8054         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8055         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8056         lru_resize_enable mdc
8057         lru_resize_enable osc
8058 }
8059 run_test 120e "Early Lock Cancel: unlink test"
8060
8061 test_120f() {
8062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8063         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8064                skip "no early lock cancel on server" && return 0
8065         test_mkdir -p -c1 $DIR/$tdir
8066         lru_resize_disable mdc
8067         lru_resize_disable osc
8068         test_mkdir -p -c1 $DIR/$tdir/d1
8069         test_mkdir -p -c1 $DIR/$tdir/d2
8070         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8071         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8072         cancel_lru_locks mdc
8073         cancel_lru_locks osc
8074         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8075         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8076         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8077         # XXX client can not do early lock cancel of OST lock
8078         # during rename (LU-4206), so cancel osc lock now.
8079         cancel_lru_locks osc
8080         can1=$(do_facet $SINGLEMDS \
8081                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8082                awk '/ldlm_cancel/ {print $2}')
8083         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8084                awk '/ldlm_bl_callback/ {print $2}')
8085         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8086         sleep 5
8087         can2=$(do_facet $SINGLEMDS \
8088                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8089                awk '/ldlm_cancel/ {print $2}')
8090         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8091                awk '/ldlm_bl_callback/ {print $2}')
8092         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8093         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8094         lru_resize_enable mdc
8095         lru_resize_enable osc
8096 }
8097 run_test 120f "Early Lock Cancel: rename test"
8098
8099 test_120g() {
8100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8101         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8102                skip "no early lock cancel on server" && return 0
8103         lru_resize_disable mdc
8104         lru_resize_disable osc
8105         count=10000
8106         echo create $count files
8107         test_mkdir -p $DIR/$tdir
8108         cancel_lru_locks mdc
8109         cancel_lru_locks osc
8110         t0=`date +%s`
8111
8112         can0=$(do_facet $SINGLEMDS \
8113                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8114                awk '/ldlm_cancel/ {print $2}')
8115         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8116                awk '/ldlm_bl_callback/ {print $2}')
8117         createmany -o $DIR/$tdir/f $count
8118         sync
8119         can1=$(do_facet $SINGLEMDS \
8120                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8121                awk '/ldlm_cancel/ {print $2}')
8122         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8123                awk '/ldlm_bl_callback/ {print $2}')
8124         t1=$(date +%s)
8125         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8126         echo rm $count files
8127         rm -r $DIR/$tdir
8128         sync
8129         can2=$(do_facet $SINGLEMDS \
8130                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8131                awk '/ldlm_cancel/ {print $2}')
8132         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8133                awk '/ldlm_bl_callback/ {print $2}')
8134         t2=$(date +%s)
8135         echo total: $count removes in $((t2-t1))
8136         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8137         sleep 2
8138         # wait for commitment of removal
8139         lru_resize_enable mdc
8140         lru_resize_enable osc
8141 }
8142 run_test 120g "Early Lock Cancel: performance test"
8143
8144 test_121() { #bug #10589
8145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8146         rm -rf $DIR/$tfile
8147         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8148 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8149         lctl set_param fail_loc=0x310
8150         cancel_lru_locks osc > /dev/null
8151         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8152         lctl set_param fail_loc=0
8153         [[ $reads -eq $writes ]] ||
8154                 error "read $reads blocks, must be $writes blocks"
8155 }
8156 run_test 121 "read cancel race ========="
8157
8158 test_123a() { # was test 123, statahead(bug 11401)
8159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8160         SLOWOK=0
8161         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8162             log "testing on UP system. Performance may be not as good as expected."
8163                         SLOWOK=1
8164         fi
8165
8166         rm -rf $DIR/$tdir
8167         test_mkdir -p $DIR/$tdir
8168         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8169         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8170         MULT=10
8171         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8172                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8173
8174                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8175                 lctl set_param -n llite.*.statahead_max 0
8176                 lctl get_param llite.*.statahead_max
8177                 cancel_lru_locks mdc
8178                 cancel_lru_locks osc
8179                 stime=`date +%s`
8180                 time ls -l $DIR/$tdir | wc -l
8181                 etime=`date +%s`
8182                 delta=$((etime - stime))
8183                 log "ls $i files without statahead: $delta sec"
8184                 lctl set_param llite.*.statahead_max=$max
8185
8186                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8187                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8188                 cancel_lru_locks mdc
8189                 cancel_lru_locks osc
8190                 stime=`date +%s`
8191                 time ls -l $DIR/$tdir | wc -l
8192                 etime=`date +%s`
8193                 delta_sa=$((etime - stime))
8194                 log "ls $i files with statahead: $delta_sa sec"
8195                 lctl get_param -n llite.*.statahead_stats
8196                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8197
8198                 [[ $swrong -lt $ewrong ]] &&
8199                         log "statahead was stopped, maybe too many locks held!"
8200                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8201
8202                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8203                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8204                     lctl set_param -n llite.*.statahead_max 0
8205                     lctl get_param llite.*.statahead_max
8206                     cancel_lru_locks mdc
8207                     cancel_lru_locks osc
8208                     stime=`date +%s`
8209                     time ls -l $DIR/$tdir | wc -l
8210                     etime=`date +%s`
8211                     delta=$((etime - stime))
8212                     log "ls $i files again without statahead: $delta sec"
8213                     lctl set_param llite.*.statahead_max=$max
8214                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8215                         if [  $SLOWOK -eq 0 ]; then
8216                                 error "ls $i files is slower with statahead!"
8217                         else
8218                                 log "ls $i files is slower with statahead!"
8219                         fi
8220                         break
8221                     fi
8222                 fi
8223
8224                 [ $delta -gt 20 ] && break
8225                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8226                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8227         done
8228         log "ls done"
8229
8230         stime=`date +%s`
8231         rm -r $DIR/$tdir
8232         sync
8233         etime=`date +%s`
8234         delta=$((etime - stime))
8235         log "rm -r $DIR/$tdir/: $delta seconds"
8236         log "rm done"
8237         lctl get_param -n llite.*.statahead_stats
8238 }
8239 run_test 123a "verify statahead work"
8240
8241 test_123b () { # statahead(bug 15027)
8242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8243         test_mkdir -p $DIR/$tdir
8244         createmany -o $DIR/$tdir/$tfile-%d 1000
8245
8246         cancel_lru_locks mdc
8247         cancel_lru_locks osc
8248
8249 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8250         lctl set_param fail_loc=0x80000803
8251         ls -lR $DIR/$tdir > /dev/null
8252         log "ls done"
8253         lctl set_param fail_loc=0x0
8254         lctl get_param -n llite.*.statahead_stats
8255         rm -r $DIR/$tdir
8256         sync
8257
8258 }
8259 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8260
8261 test_124a() {
8262         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8263         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8264                 skip "no lru resize on server" && return 0
8265         local NR=2000
8266         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8267
8268         log "create $NR files at $DIR/$tdir"
8269         createmany -o $DIR/$tdir/f $NR ||
8270                 error "failed to create $NR files in $DIR/$tdir"
8271
8272         cancel_lru_locks mdc
8273         ls -l $DIR/$tdir > /dev/null
8274
8275         local NSDIR=""
8276         local LRU_SIZE=0
8277         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8278                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8279                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8280                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8281                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8282                         log "NSDIR=$NSDIR"
8283                         log "NS=$(basename $NSDIR)"
8284                         break
8285                 fi
8286         done
8287
8288         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8289                 skip "Not enough cached locks created!"
8290                 return 0
8291         fi
8292         log "LRU=$LRU_SIZE"
8293
8294         local SLEEP=30
8295
8296         # We know that lru resize allows one client to hold $LIMIT locks
8297         # for 10h. After that locks begin to be killed by client.
8298         local MAX_HRS=10
8299         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8300         log "LIMIT=$LIMIT"
8301         if [ $LIMIT -lt $LRU_SIZE ]; then
8302             skip "Limit is too small $LIMIT"
8303             return 0
8304         fi
8305
8306         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8307         # killing locks. Some time was spent for creating locks. This means
8308         # that up to the moment of sleep finish we must have killed some of
8309         # them (10-100 locks). This depends on how fast ther were created.
8310         # Many of them were touched in almost the same moment and thus will
8311         # be killed in groups.
8312         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8313
8314         # Use $LRU_SIZE_B here to take into account real number of locks
8315         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8316         local LRU_SIZE_B=$LRU_SIZE
8317         log "LVF=$LVF"
8318         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8319         log "OLD_LVF=$OLD_LVF"
8320         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8321
8322         # Let's make sure that we really have some margin. Client checks
8323         # cached locks every 10 sec.
8324         SLEEP=$((SLEEP+20))
8325         log "Sleep ${SLEEP} sec"
8326         local SEC=0
8327         while ((SEC<$SLEEP)); do
8328                 echo -n "..."
8329                 sleep 5
8330                 SEC=$((SEC+5))
8331                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8332                 echo -n "$LRU_SIZE"
8333         done
8334         echo ""
8335         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8336         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8337
8338         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8339                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8340                 unlinkmany $DIR/$tdir/f $NR
8341                 return
8342         }
8343
8344         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8345         log "unlink $NR files at $DIR/$tdir"
8346         unlinkmany $DIR/$tdir/f $NR
8347 }
8348 run_test 124a "lru resize ======================================="
8349
8350 get_max_pool_limit()
8351 {
8352         local limit=$($LCTL get_param \
8353                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8354         local max=0
8355         for l in $limit; do
8356                 if [[ $l -gt $max ]]; then
8357                         max=$l
8358                 fi
8359         done
8360         echo $max
8361 }
8362
8363 test_124b() {
8364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8365         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8366                 skip "no lru resize on server" && return 0
8367
8368         LIMIT=$(get_max_pool_limit)
8369
8370         NR=$(($(default_lru_size)*20))
8371         if [[ $NR -gt $LIMIT ]]; then
8372                 log "Limit lock number by $LIMIT locks"
8373                 NR=$LIMIT
8374         fi
8375         lru_resize_disable mdc
8376         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8377                 error "failed to create $DIR/$tdir/disable_lru_resize"
8378
8379         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8380         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8381         cancel_lru_locks mdc
8382         stime=`date +%s`
8383         PID=""
8384         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8385         PID="$PID $!"
8386         sleep 2
8387         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8388         PID="$PID $!"
8389         sleep 2
8390         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8391         PID="$PID $!"
8392         wait $PID
8393         etime=`date +%s`
8394         nolruresize_delta=$((etime-stime))
8395         log "ls -la time: $nolruresize_delta seconds"
8396         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8397         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8398
8399         lru_resize_enable mdc
8400         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8401                 error "failed to create $DIR/$tdir/enable_lru_resize"
8402
8403         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8404         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8405         cancel_lru_locks mdc
8406         stime=`date +%s`
8407         PID=""
8408         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8409         PID="$PID $!"
8410         sleep 2
8411         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8412         PID="$PID $!"
8413         sleep 2
8414         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8415         PID="$PID $!"
8416         wait $PID
8417         etime=`date +%s`
8418         lruresize_delta=$((etime-stime))
8419         log "ls -la time: $lruresize_delta seconds"
8420         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8421
8422         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8423                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8424         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8425                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8426         else
8427                 log "lru resize performs the same with no lru resize"
8428         fi
8429         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8430 }
8431 run_test 124b "lru resize (performance test) ======================="
8432
8433 test_125() { # 13358
8434         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8435         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8436         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8437         test_mkdir -p $DIR/d125 || error "mkdir failed"
8438         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8439         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8440         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8441 }
8442 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8443
8444 test_126() { # bug 12829/13455
8445         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8446         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8447         $GSS && skip "must run as gss disabled" && return
8448
8449         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8450         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8451         rm -f $DIR/$tfile
8452         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8453 }
8454 run_test 126 "check that the fsgid provided by the client is taken into account"
8455
8456 test_127a() { # bug 15521
8457         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8458         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8459         $LCTL set_param osc.*.stats=0
8460         FSIZE=$((2048 * 1024))
8461         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8462         cancel_lru_locks osc
8463         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8464
8465         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8466         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8467                 echo "got $COUNT $NAME"
8468                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8469                 eval $NAME=$COUNT || error "Wrong proc format"
8470
8471                 case $NAME in
8472                         read_bytes|write_bytes)
8473                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8474                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8475                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8476                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8477                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8478                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8479                                 error "sumsquare is too small: $SUMSQ"
8480                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8481                                 error "sumsquare is too big: $SUMSQ"
8482                         ;;
8483                         *) ;;
8484                 esac
8485         done < $DIR/${tfile}.tmp
8486
8487         #check that we actually got some stats
8488         [ "$read_bytes" ] || error "Missing read_bytes stats"
8489         [ "$write_bytes" ] || error "Missing write_bytes stats"
8490         [ "$read_bytes" != 0 ] || error "no read done"
8491         [ "$write_bytes" != 0 ] || error "no write done"
8492 }
8493 run_test 127a "verify the client stats are sane"
8494
8495 test_127b() { # bug LU-333
8496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8497         $LCTL set_param llite.*.stats=0
8498         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8499         # perform 2 reads and writes so MAX is different from SUM.
8500         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8501         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8502         cancel_lru_locks osc
8503         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8504         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8505
8506         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8507         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8508                 echo "got $COUNT $NAME"
8509                 eval $NAME=$COUNT || error "Wrong proc format"
8510
8511         case $NAME in
8512                 read_bytes)
8513                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8514                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8515                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8516                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8517                         ;;
8518                 write_bytes)
8519                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8520                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8521                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8522                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8523                         ;;
8524                         *) ;;
8525                 esac
8526         done < $TMP/${tfile}.tmp
8527
8528         #check that we actually got some stats
8529         [ "$read_bytes" ] || error "Missing read_bytes stats"
8530         [ "$write_bytes" ] || error "Missing write_bytes stats"
8531         [ "$read_bytes" != 0 ] || error "no read done"
8532         [ "$write_bytes" != 0 ] || error "no write done"
8533 }
8534 run_test 127b "verify the llite client stats are sane"
8535
8536 test_128() { # bug 15212
8537         touch $DIR/$tfile
8538         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8539                 find $DIR/$tfile
8540                 find $DIR/$tfile
8541         EOF
8542
8543         result=$(grep error $TMP/$tfile.log)
8544         rm -f $DIR/$tfile
8545         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8546 }
8547 run_test 128 "interactive lfs for 2 consecutive find's"
8548
8549 set_dir_limits () {
8550         local mntdev
8551         local canondev
8552         local node
8553
8554         local LDPROC=/proc/fs/ldiskfs
8555         local facets=$(get_facets MDS)
8556
8557         for facet in ${facets//,/ }; do
8558                 canondev=$(ldiskfs_canon \
8559                            *.$(convert_facet2label $facet).mntdev $facet)
8560                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8561                                                 LDPROC=/sys/fs/ldiskfs
8562                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8563         done
8564 }
8565
8566 test_129() {
8567         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8568         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8569                 skip "Only applicable to ldiskfs-based MDTs"
8570                 return
8571         fi
8572         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8573         ENOSPC=28
8574         EFBIG=27
8575
8576         rm -rf $DIR/$tdir
8577         test_mkdir -p $DIR/$tdir
8578
8579         # block size of mds1
8580         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8581         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8582         local MAX=$((MDSBLOCKSIZE * 3))
8583         set_dir_limits $MAX
8584         local I=$(stat -c%s "$DIR/$tdir")
8585         local J=0
8586         local STRIPE_COUNT=1
8587         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8588         MAX=$((MAX*STRIPE_COUNT))
8589         while [[ $I -le $MAX ]]; do
8590                 $MULTIOP $DIR/$tdir/$J Oc
8591                 rc=$?
8592                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8593                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8594                 #and EFBIG for previous versions
8595                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8596                         set_dir_limits 0
8597                         echo "return code $rc received as expected"
8598                         multiop $DIR/$tdir/$J Oc ||
8599                                 error_exit "multiop failed w/o dir size limit"
8600
8601                         I=$(stat -c%s "$DIR/$tdir")
8602
8603                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8604                                         $(version_code 2.4.51) ]
8605                         then
8606                                 [[ $I -eq $MAX ]] && return 0
8607                         else
8608                                 [[ $I -gt $MAX ]] && return 0
8609                         fi
8610                         error_exit "current dir size $I, previous limit $MAX"
8611                 elif [ $rc -ne 0 ]; then
8612                         set_dir_limits 0
8613                         error_exit "return code $rc received instead of expected " \
8614                                    "$EFBIG or $ENOSPC, files in dir $I"
8615                 fi
8616                 J=$((J+1))
8617                 I=$(stat -c%s "$DIR/$tdir")
8618         done
8619
8620         set_dir_limits 0
8621         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8622 }
8623 run_test 129 "test directory size limit ========================"
8624
8625 OLDIFS="$IFS"
8626 cleanup_130() {
8627         trap 0
8628         IFS="$OLDIFS"
8629 }
8630
8631 test_130a() {
8632         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8633         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8634                 return
8635
8636         trap cleanup_130 EXIT RETURN
8637
8638         local fm_file=$DIR/$tfile
8639         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8640         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8641                 error "dd failed for $fm_file"
8642
8643         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8644         filefrag -ves $fm_file
8645         RC=$?
8646         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8647                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8648         [ $RC != 0 ] && error "filefrag $fm_file failed"
8649
8650         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8651                       grep -v "ext:" | grep -v "found")
8652         lun=$($GETSTRIPE -i $fm_file)
8653
8654         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8655         IFS=$'\n'
8656         tot_len=0
8657         for line in $filefrag_op
8658         do
8659                 frag_lun=`echo $line | cut -d: -f5`
8660                 ext_len=`echo $line | cut -d: -f4`
8661                 if (( $frag_lun != $lun )); then
8662                         cleanup_130
8663                         error "FIEMAP on 1-stripe file($fm_file) failed"
8664                         return
8665                 fi
8666                 (( tot_len += ext_len ))
8667         done
8668
8669         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8670                 cleanup_130
8671                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8672                 return
8673         fi
8674
8675         cleanup_130
8676
8677         echo "FIEMAP on single striped file succeeded"
8678 }
8679 run_test 130a "FIEMAP (1-stripe file)"
8680
8681 test_130b() {
8682         [ "$OSTCOUNT" -lt "2" ] &&
8683                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8684
8685         [ "$OSTCOUNT" -ge "10" ] &&
8686                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8687
8688         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8689         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8690                 return
8691
8692         trap cleanup_130 EXIT RETURN
8693
8694         local fm_file=$DIR/$tfile
8695         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8696         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8697                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8698
8699         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8700                 error "dd failed on $fm_file"
8701
8702         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8703         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8704                       grep -v "ext:" | grep -v "found")
8705
8706         last_lun=$(echo $filefrag_op | cut -d: -f5)
8707
8708         IFS=$'\n'
8709         tot_len=0
8710         num_luns=1
8711         for line in $filefrag_op
8712         do
8713                 frag_lun=`echo $line | cut -d: -f5`
8714                 ext_len=`echo $line | cut -d: -f4`
8715                 if (( $frag_lun != $last_lun )); then
8716                         if (( tot_len != 1024 )); then
8717                                 cleanup_130
8718                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8719                                 return
8720                         else
8721                                 (( num_luns += 1 ))
8722                                 tot_len=0
8723                         fi
8724                 fi
8725                 (( tot_len += ext_len ))
8726                 last_lun=$frag_lun
8727         done
8728         if (( num_luns != 2 || tot_len != 1024 )); then
8729                 cleanup_130
8730                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8731                 return
8732         fi
8733
8734         cleanup_130
8735
8736         echo "FIEMAP on 2-stripe file succeeded"
8737 }
8738 run_test 130b "FIEMAP (2-stripe file)"
8739
8740 test_130c() {
8741         [ "$OSTCOUNT" -lt "2" ] &&
8742                 skip_env "skipping FIEMAP on 2-stripe file" && return
8743
8744         [ "$OSTCOUNT" -ge "10" ] &&
8745                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8746
8747         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8748         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8749                 return
8750
8751         trap cleanup_130 EXIT RETURN
8752
8753         local fm_file=$DIR/$tfile
8754         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8755         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8756                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8757
8758         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8759
8760         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8761         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8762
8763         last_lun=`echo $filefrag_op | cut -d: -f5`
8764
8765         IFS=$'\n'
8766         tot_len=0
8767         num_luns=1
8768         for line in $filefrag_op
8769         do
8770                 frag_lun=`echo $line | cut -d: -f5`
8771                 ext_len=`echo $line | cut -d: -f4`
8772                 if (( $frag_lun != $last_lun )); then
8773                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8774                         if (( logical != 512 )); then
8775                                 cleanup_130
8776                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8777                                 return
8778                         fi
8779                         if (( tot_len != 512 )); then
8780                                 cleanup_130
8781                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8782                                 return
8783                         else
8784                                 (( num_luns += 1 ))
8785                                 tot_len=0
8786                         fi
8787                 fi
8788                 (( tot_len += ext_len ))
8789                 last_lun=$frag_lun
8790         done
8791         if (( num_luns != 2 || tot_len != 512 )); then
8792                 cleanup_130
8793                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8794                 return
8795         fi
8796
8797         cleanup_130
8798
8799         echo "FIEMAP on 2-stripe file with hole succeeded"
8800 }
8801 run_test 130c "FIEMAP (2-stripe file with hole)"
8802
8803 test_130d() {
8804         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8805
8806         [ "$OSTCOUNT" -ge "10" ] &&
8807                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8808
8809         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8810         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8811
8812         trap cleanup_130 EXIT RETURN
8813
8814         local fm_file=$DIR/$tfile
8815         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8816         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8817                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8818
8819         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8820         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8821                 error "dd failed on $fm_file"
8822
8823         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8824         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8825                 grep -v "ext:" | grep -v "found"`
8826
8827         last_lun=`echo $filefrag_op | cut -d: -f5`
8828
8829         IFS=$'\n'
8830         tot_len=0
8831         num_luns=1
8832         for line in $filefrag_op
8833         do
8834                 frag_lun=`echo $line | cut -d: -f5`
8835                 ext_len=`echo $line | cut -d: -f4`
8836                 if (( $frag_lun != $last_lun )); then
8837                         if (( tot_len != 1024 )); then
8838                                 cleanup_130
8839                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8840                                 return
8841                         else
8842                                 (( num_luns += 1 ))
8843                                 tot_len=0
8844                         fi
8845                 fi
8846                 (( tot_len += ext_len ))
8847                 last_lun=$frag_lun
8848         done
8849         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8850                 cleanup_130
8851                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8852                 return
8853         fi
8854
8855         cleanup_130
8856
8857         echo "FIEMAP on N-stripe file succeeded"
8858 }
8859 run_test 130d "FIEMAP (N-stripe file)"
8860
8861 test_130e() {
8862         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8863
8864         [ "$OSTCOUNT" -ge "10" ] &&
8865                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8866
8867         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8868         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8869
8870         trap cleanup_130 EXIT RETURN
8871
8872         local fm_file=$DIR/$tfile
8873         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8874         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8875                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8876
8877         NUM_BLKS=512
8878         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8879         for ((i = 0; i < $NUM_BLKS; i++))
8880         do
8881                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8882         done
8883
8884         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8885         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8886
8887         last_lun=`echo $filefrag_op | cut -d: -f5`
8888
8889         IFS=$'\n'
8890         tot_len=0
8891         num_luns=1
8892         for line in $filefrag_op
8893         do
8894                 frag_lun=`echo $line | cut -d: -f5`
8895                 ext_len=`echo $line | cut -d: -f4`
8896                 if (( $frag_lun != $last_lun )); then
8897                         if (( tot_len != $EXPECTED_LEN )); then
8898                                 cleanup_130
8899                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8900                                 return
8901                         else
8902                                 (( num_luns += 1 ))
8903                                 tot_len=0
8904                         fi
8905                 fi
8906                 (( tot_len += ext_len ))
8907                 last_lun=$frag_lun
8908         done
8909         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8910                 cleanup_130
8911                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8912                 return
8913         fi
8914
8915         cleanup_130
8916
8917         echo "FIEMAP with continuation calls succeeded"
8918 }
8919 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8920
8921 # Test for writev/readv
8922 test_131a() {
8923         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8924         error "writev test failed"
8925         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8926         error "readv failed"
8927         rm -f $DIR/$tfile
8928 }
8929 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8930
8931 test_131b() {
8932         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8933         error "append writev test failed"
8934         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8935         error "append writev test failed"
8936         rm -f $DIR/$tfile
8937 }
8938 run_test 131b "test append writev"
8939
8940 test_131c() {
8941         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8942         error "NOT PASS"
8943 }
8944 run_test 131c "test read/write on file w/o objects"
8945
8946 test_131d() {
8947         rwv -f $DIR/$tfile -w -n 1 1572864
8948         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8949         if [ "$NOB" != 1572864 ]; then
8950                 error "Short read filed: read $NOB bytes instead of 1572864"
8951         fi
8952         rm -f $DIR/$tfile
8953 }
8954 run_test 131d "test short read"
8955
8956 test_131e() {
8957         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8958         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8959         error "read hitting hole failed"
8960         rm -f $DIR/$tfile
8961 }
8962 run_test 131e "test read hitting hole"
8963
8964 check_stats() {
8965         local res
8966         local count
8967         case $1 in
8968         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8969                  ;;
8970         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8971                  ;;
8972         *) error "Wrong argument $1" ;;
8973         esac
8974         echo $res
8975         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8976         # if the argument $3 is zero, it means any stat increment is ok.
8977         if [[ $3 -gt 0 ]]; then
8978                 count=$(echo $res | awk '{ print $2 }')
8979                 [[ $count -ne $3 ]] &&
8980                         error "The $2 counter on $1 is wrong - expected $3"
8981         fi
8982 }
8983
8984 test_133a() {
8985         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8986         remote_ost_nodsh && skip "remote OST with nodsh" && return
8987         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8988
8989         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8990                 { skip "MDS doesn't support rename stats"; return; }
8991         local testdir=$DIR/${tdir}/stats_testdir
8992         mkdir -p $DIR/${tdir}
8993
8994         # clear stats.
8995         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8996         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8997
8998         # verify mdt stats first.
8999         mkdir ${testdir} || error "mkdir failed"
9000         check_stats $SINGLEMDS "mkdir" 1
9001         touch ${testdir}/${tfile} || "touch failed"
9002         check_stats $SINGLEMDS "open" 1
9003         check_stats $SINGLEMDS "close" 1
9004         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9005         check_stats $SINGLEMDS "mknod" 1
9006         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9007         check_stats $SINGLEMDS "unlink" 1
9008         rm -f ${testdir}/${tfile} || error "file remove failed"
9009         check_stats $SINGLEMDS "unlink" 2
9010
9011         # remove working dir and check mdt stats again.
9012         rmdir ${testdir} || error "rmdir failed"
9013         check_stats $SINGLEMDS "rmdir" 1
9014
9015         local testdir1=$DIR/${tdir}/stats_testdir1
9016         mkdir -p ${testdir}
9017         mkdir -p ${testdir1}
9018         touch ${testdir1}/test1
9019         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9020         check_stats $SINGLEMDS "crossdir_rename" 1
9021
9022         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9023         check_stats $SINGLEMDS "samedir_rename" 1
9024
9025         rm -rf $DIR/${tdir}
9026 }
9027 run_test 133a "Verifying MDT stats ========================================"
9028
9029 test_133b() {
9030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9031         remote_ost_nodsh && skip "remote OST with nodsh" && return
9032         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9033         local testdir=$DIR/${tdir}/stats_testdir
9034         mkdir -p ${testdir} || error "mkdir failed"
9035         touch ${testdir}/${tfile} || "touch failed"
9036         cancel_lru_locks mdc
9037
9038         # clear stats.
9039         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9040         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9041
9042         # extra mdt stats verification.
9043         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9044         check_stats $SINGLEMDS "setattr" 1
9045         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9046         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9047         then            # LU-1740
9048                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9049                 check_stats $SINGLEMDS "getattr" 1
9050         fi
9051         $LFS df || error "lfs failed"
9052         check_stats $SINGLEMDS "statfs" 1
9053
9054         rm -rf $DIR/${tdir}
9055 }
9056 run_test 133b "Verifying extra MDT stats =================================="
9057
9058 test_133c() {
9059         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9060         remote_ost_nodsh && skip "remote OST with nodsh" && return
9061         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9062         local testdir=$DIR/${tdir}/stats_testdir
9063         test_mkdir -p ${testdir} || error "mkdir failed"
9064
9065         # verify obdfilter stats.
9066         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9067         sync
9068         cancel_lru_locks osc
9069         wait_delete_completed
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         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9076         sync
9077         cancel_lru_locks osc
9078         check_stats ost "write" 1
9079
9080         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9081         check_stats ost "read" 1
9082
9083         > ${testdir}/${tfile} || error "truncate failed"
9084         check_stats ost "punch" 1
9085
9086         rm -f ${testdir}/${tfile} || error "file remove failed"
9087         wait_delete_completed
9088         check_stats ost "destroy" 1
9089
9090         rm -rf $DIR/${tdir}
9091 }
9092 run_test 133c "Verifying OST stats ========================================"
9093
9094 order_2() {
9095         local value=$1
9096         local orig=$value
9097         local order=1
9098
9099         while [ $value -ge 2 ]; do
9100                 order=$((order*2))
9101                 value=$((value/2))
9102         done
9103
9104         if [ $orig -gt $order ]; then
9105                 order=$((order*2))
9106         fi
9107         echo $order
9108 }
9109
9110 size_in_KMGT() {
9111     local value=$1
9112     local size=('K' 'M' 'G' 'T');
9113     local i=0
9114     local size_string=$value
9115
9116     while [ $value -ge 1024 ]; do
9117         if [ $i -gt 3 ]; then
9118             #T is the biggest unit we get here, if that is bigger,
9119             #just return XXXT
9120             size_string=${value}T
9121             break
9122         fi
9123         value=$((value >> 10))
9124         if [ $value -lt 1024 ]; then
9125             size_string=${value}${size[$i]}
9126             break
9127         fi
9128         i=$((i + 1))
9129     done
9130
9131     echo $size_string
9132 }
9133
9134 get_rename_size() {
9135     local size=$1
9136     local context=${2:-.}
9137     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9138                 grep -A1 $context |
9139                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9140     echo $sample
9141 }
9142
9143 test_133d() {
9144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9145         remote_ost_nodsh && skip "remote OST with nodsh" && return
9146         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9147         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9148         { skip "MDS doesn't support rename stats"; return; }
9149
9150         local testdir1=$DIR/${tdir}/stats_testdir1
9151         local testdir2=$DIR/${tdir}/stats_testdir2
9152
9153         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9154
9155         mkdir -p ${testdir1} || error "mkdir failed"
9156         mkdir -p ${testdir2} || error "mkdir failed"
9157
9158         createmany -o $testdir1/test 512 || error "createmany failed"
9159
9160         # check samedir rename size
9161         mv ${testdir1}/test0 ${testdir1}/test_0
9162
9163         local testdir1_size=$(ls -l $DIR/${tdir} |
9164                 awk '/stats_testdir1/ {print $5}')
9165         local testdir2_size=$(ls -l $DIR/${tdir} |
9166                 awk '/stats_testdir2/ {print $5}')
9167
9168         testdir1_size=$(order_2 $testdir1_size)
9169         testdir2_size=$(order_2 $testdir2_size)
9170
9171         testdir1_size=$(size_in_KMGT $testdir1_size)
9172         testdir2_size=$(size_in_KMGT $testdir2_size)
9173
9174         echo "source rename dir size: ${testdir1_size}"
9175         echo "target rename dir size: ${testdir2_size}"
9176
9177         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9178         eval $cmd || error "$cmd failed"
9179         local samedir=$($cmd | grep 'same_dir')
9180         local same_sample=$(get_rename_size $testdir1_size)
9181         [ -z "$samedir" ] && error "samedir_rename_size count error"
9182         [[ $same_sample -eq 1 ]] ||
9183                 error "samedir_rename_size error $same_sample"
9184         echo "Check same dir rename stats success"
9185
9186         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9187
9188         # check crossdir rename size
9189         mv ${testdir1}/test_0 ${testdir2}/test_0
9190
9191         testdir1_size=$(ls -l $DIR/${tdir} |
9192                 awk '/stats_testdir1/ {print $5}')
9193         testdir2_size=$(ls -l $DIR/${tdir} |
9194                 awk '/stats_testdir2/ {print $5}')
9195
9196         testdir1_size=$(order_2 $testdir1_size)
9197         testdir2_size=$(order_2 $testdir2_size)
9198
9199         testdir1_size=$(size_in_KMGT $testdir1_size)
9200         testdir2_size=$(size_in_KMGT $testdir2_size)
9201
9202         echo "source rename dir size: ${testdir1_size}"
9203         echo "target rename dir size: ${testdir2_size}"
9204
9205         eval $cmd || error "$cmd failed"
9206         local crossdir=$($cmd | grep 'crossdir')
9207         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9208         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9209         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9210         [[ $src_sample -eq 1 ]] ||
9211                 error "crossdir_rename_size error $src_sample"
9212         [[ $tgt_sample -eq 1 ]] ||
9213                 error "crossdir_rename_size error $tgt_sample"
9214         echo "Check cross dir rename stats success"
9215         rm -rf $DIR/${tdir}
9216 }
9217 run_test 133d "Verifying rename_stats ========================================"
9218
9219 test_133e() {
9220         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9221         remote_ost_nodsh && skip "remote OST with nodsh" && return
9222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9223         local testdir=$DIR/${tdir}/stats_testdir
9224         local ctr f0 f1 bs=32768 count=42 sum
9225
9226         mkdir -p ${testdir} || error "mkdir failed"
9227
9228         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9229
9230         for ctr in {write,read}_bytes; do
9231                 sync
9232                 cancel_lru_locks osc
9233
9234                 do_facet ost1 $LCTL set_param -n \
9235                         "obdfilter.*.exports.clear=clear"
9236
9237                 if [ $ctr = write_bytes ]; then
9238                         f0=/dev/zero
9239                         f1=${testdir}/${tfile}
9240                 else
9241                         f0=${testdir}/${tfile}
9242                         f1=/dev/null
9243                 fi
9244
9245                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9246                         error "dd failed"
9247                 sync
9248                 cancel_lru_locks osc
9249
9250                 sum=$(do_facet ost1 $LCTL get_param \
9251                         "obdfilter.*.exports.*.stats" |
9252                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9253                                 $1 == ctr { sum += $7 }
9254                                 END { printf("%0.0f", sum) }')
9255
9256                 if ((sum != bs * count)); then
9257                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9258                 fi
9259         done
9260
9261         rm -rf $DIR/${tdir}
9262 }
9263 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9264
9265 test_133f() {
9266         local proc_dirs
9267
9268         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9269 /sys/fs/lustre/ /sys/fs/lnet/"
9270         local dir
9271         for dir in $dirs; do
9272                 if [ -d $dir ]; then
9273                         proc_dirs="$proc_dirs $dir"
9274                 fi
9275         done
9276
9277         local facet
9278
9279         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9280         remote_ost_nodsh && skip "remote OST with nodsh" && return
9281         # First without trusting modes.
9282         find $proc_dirs -exec cat '{}' \; &> /dev/null
9283
9284         # Second verifying readability.
9285         find $proc_dirs \
9286                 -type f \
9287                 -exec cat '{}' \; &> /dev/null ||
9288                         error "proc file read failed"
9289
9290         for facet in $SINGLEMDS ost1; do
9291                 do_facet $facet find $proc_dirs \
9292                         ! -name req_history \
9293                         -exec cat '{}' \\\; &> /dev/null
9294
9295                 do_facet $facet find $proc_dirs \
9296                         ! -name req_history \
9297                         -type f \
9298                         -exec cat '{}' \\\; &> /dev/null ||
9299                                 error "proc file read failed"
9300         done
9301 }
9302 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9303
9304 test_133g() {
9305         local proc_dirs
9306
9307         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9308 /sys/fs/lustre/ /sys/fs/lnet/"
9309         local dir
9310         for dir in $dirs; do
9311                 if [ -d $dir ]; then
9312                         proc_dirs="$proc_dirs $dir"
9313                 fi
9314         done
9315
9316         local facet
9317
9318         # Second verifying readability.
9319         find $proc_dirs \
9320                 -type f \
9321                 -not -name force_lbug \
9322                 -not -name changelog_mask \
9323                 -exec badarea_io '{}' \; &> /dev/null ||
9324                 error "find $proc_dirs failed"
9325
9326         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9327                 skip "Too old lustre on MDS" && return
9328
9329         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9330                 skip "Too old lustre on ost1" && return
9331
9332         for facet in $SINGLEMDS ost1; do
9333                 do_facet $facet find $proc_dirs \
9334                         -type f \
9335                         -not -name force_lbug \
9336                         -not -name changelog_mask \
9337                         -exec badarea_io '{}' \\\; &> /dev/null ||
9338                 error "$facet find $proc_dirs failed"
9339
9340         done
9341
9342         # remount the FS in case writes/reads /proc break the FS
9343         cleanup || error "failed to unmount"
9344         setup || error "failed to setup"
9345         true
9346 }
9347 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9348
9349 test_140() { #bug-17379
9350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9351         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9352         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9353         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9354
9355         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9356         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9357         local i=0
9358         while i=`expr $i + 1`; do
9359                 test_mkdir -p $i || error "Creating dir $i"
9360                 cd $i || error "Changing to $i"
9361                 ln -s ../stat stat || error "Creating stat symlink"
9362                 # Read the symlink until ELOOP present,
9363                 # not LBUGing the system is considered success,
9364                 # we didn't overrun the stack.
9365                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9366                 [ $ret -ne 0 ] && {
9367                         if [ $ret -eq 40 ]; then
9368                                 break  # -ELOOP
9369                         else
9370                                 error "Open stat symlink"
9371                                 return
9372                         fi
9373                 }
9374         done
9375         i=`expr $i - 1`
9376         echo "The symlink depth = $i"
9377         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9378                                         error "Invalid symlink depth"
9379
9380         # Test recursive symlink
9381         ln -s symlink_self symlink_self
9382         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9383         echo "open symlink_self returns $ret"
9384         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9385 }
9386 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9387
9388 test_150() {
9389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9390         local TF="$TMP/$tfile"
9391
9392         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9393         cp $TF $DIR/$tfile
9394         cancel_lru_locks osc
9395         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9396         remount_client $MOUNT
9397         df -P $MOUNT
9398         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9399
9400         $TRUNCATE $TF 6000
9401         $TRUNCATE $DIR/$tfile 6000
9402         cancel_lru_locks osc
9403         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9404
9405         echo "12345" >>$TF
9406         echo "12345" >>$DIR/$tfile
9407         cancel_lru_locks osc
9408         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9409
9410         echo "12345" >>$TF
9411         echo "12345" >>$DIR/$tfile
9412         cancel_lru_locks osc
9413         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9414
9415         rm -f $TF
9416         true
9417 }
9418 run_test 150 "truncate/append tests"
9419
9420 #LU-2902 roc_hit was not able to read all values from lproc
9421 function roc_hit_init() {
9422         local list=$(comma_list $(osts_nodes))
9423         local dir=$DIR/$tdir-check
9424         local file=$dir/file
9425         local BEFORE
9426         local AFTER
9427         local idx
9428
9429         test_mkdir -p $dir
9430         #use setstripe to do a write to every ost
9431         for i in $(seq 0 $((OSTCOUNT-1))); do
9432                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9433                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9434                 idx=$(printf %04x $i)
9435                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9436                         awk '$1 == "cache_access" {sum += $7}
9437                                 END { printf("%0.0f", sum) }')
9438
9439                 cancel_lru_locks osc
9440                 cat $file >/dev/null
9441
9442                 AFTER=$(get_osd_param $list *OST*$idx stats |
9443                         awk '$1 == "cache_access" {sum += $7}
9444                                 END { printf("%0.0f", sum) }')
9445
9446                 echo BEFORE:$BEFORE AFTER:$AFTER
9447                 if ! let "AFTER - BEFORE == 4"; then
9448                         rm -rf $dir
9449                         error "roc_hit is not safe to use"
9450                 fi
9451                 rm $file
9452         done
9453
9454         rm -rf $dir
9455 }
9456
9457 function roc_hit() {
9458         local list=$(comma_list $(osts_nodes))
9459         echo $(get_osd_param $list '' stats |
9460                 awk '$1 == "cache_hit" {sum += $7}
9461                         END { printf("%0.0f", sum) }')
9462 }
9463
9464 function set_cache() {
9465         local on=1
9466
9467         if [ "$2" == "off" ]; then
9468                 on=0;
9469         fi
9470         local list=$(comma_list $(osts_nodes))
9471         set_osd_param $list '' $1_cache_enable $on
9472
9473         cancel_lru_locks osc
9474 }
9475
9476 test_151() {
9477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9478         remote_ost_nodsh && skip "remote OST with nodsh" && return
9479
9480         local CPAGES=3
9481         local list=$(comma_list $(osts_nodes))
9482
9483         # check whether obdfilter is cache capable at all
9484         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9485                 echo "not cache-capable obdfilter"
9486                 return 0
9487         fi
9488
9489         # check cache is enabled on all obdfilters
9490         if get_osd_param $list '' read_cache_enable | grep 0; then
9491                 echo "oss cache is disabled"
9492                 return 0
9493         fi
9494
9495         set_osd_param $list '' writethrough_cache_enable 1
9496
9497         # check write cache is enabled on all obdfilters
9498         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9499                 echo "oss write cache is NOT enabled"
9500                 return 0
9501         fi
9502
9503         roc_hit_init
9504
9505         #define OBD_FAIL_OBD_NO_LRU  0x609
9506         do_nodes $list $LCTL set_param fail_loc=0x609
9507
9508         # pages should be in the case right after write
9509         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9510                 error "dd failed"
9511
9512         local BEFORE=$(roc_hit)
9513         cancel_lru_locks osc
9514         cat $DIR/$tfile >/dev/null
9515         local AFTER=$(roc_hit)
9516
9517         do_nodes $list $LCTL set_param fail_loc=0
9518
9519         if ! let "AFTER - BEFORE == CPAGES"; then
9520                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9521         fi
9522
9523         # the following read invalidates the cache
9524         cancel_lru_locks osc
9525         set_osd_param $list '' read_cache_enable 0
9526         cat $DIR/$tfile >/dev/null
9527
9528         # now data shouldn't be found in the cache
9529         BEFORE=$(roc_hit)
9530         cancel_lru_locks osc
9531         cat $DIR/$tfile >/dev/null
9532         AFTER=$(roc_hit)
9533         if let "AFTER - BEFORE != 0"; then
9534                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9535         fi
9536
9537         set_osd_param $list '' read_cache_enable 1
9538         rm -f $DIR/$tfile
9539 }
9540 run_test 151 "test cache on oss and controls ==============================="
9541
9542 test_152() {
9543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9544         local TF="$TMP/$tfile"
9545
9546         # simulate ENOMEM during write
9547 #define OBD_FAIL_OST_NOMEM      0x226
9548         lctl set_param fail_loc=0x80000226
9549         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9550         cp $TF $DIR/$tfile
9551         sync || error "sync failed"
9552         lctl set_param fail_loc=0
9553
9554         # discard client's cache
9555         cancel_lru_locks osc
9556
9557         # simulate ENOMEM during read
9558         lctl set_param fail_loc=0x80000226
9559         cmp $TF $DIR/$tfile || error "cmp failed"
9560         lctl set_param fail_loc=0
9561
9562         rm -f $TF
9563 }
9564 run_test 152 "test read/write with enomem ============================"
9565
9566 test_153() {
9567         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9568 }
9569 run_test 153 "test if fdatasync does not crash ======================="
9570
9571 dot_lustre_fid_permission_check() {
9572         local fid=$1
9573         local ffid=$MOUNT/.lustre/fid/$fid
9574         local test_dir=$2
9575
9576         echo "stat fid $fid"
9577         stat $ffid > /dev/null || error "stat $ffid failed."
9578         echo "touch fid $fid"
9579         touch $ffid || error "touch $ffid failed."
9580         echo "write to fid $fid"
9581         cat /etc/hosts > $ffid || error "write $ffid failed."
9582         echo "read fid $fid"
9583         diff /etc/hosts $ffid || error "read $ffid failed."
9584         echo "append write to fid $fid"
9585         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9586         echo "rename fid $fid"
9587         mv $ffid $test_dir/$tfile.1 &&
9588                 error "rename $ffid to $tfile.1 should fail."
9589         touch $test_dir/$tfile.1
9590         mv $test_dir/$tfile.1 $ffid &&
9591                 error "rename $tfile.1 to $ffid should fail."
9592         rm -f $test_dir/$tfile.1
9593         echo "truncate fid $fid"
9594         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9595         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9596                 echo "link fid $fid"
9597                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9598         fi
9599         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9600                 echo "setfacl fid $fid"
9601                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9602                 echo "getfacl fid $fid"
9603                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9604         fi
9605         echo "unlink fid $fid"
9606         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9607         echo "mknod fid $fid"
9608         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9609
9610         fid=[0xf00000400:0x1:0x0]
9611         ffid=$MOUNT/.lustre/fid/$fid
9612
9613         echo "stat non-exist fid $fid"
9614         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9615         echo "write to non-exist fid $fid"
9616         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9617         echo "link new fid $fid"
9618         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9619
9620         mkdir -p $test_dir/$tdir
9621         touch $test_dir/$tdir/$tfile
9622         fid=$($LFS path2fid $test_dir/$tdir)
9623         rc=$?
9624         [ $rc -ne 0 ] &&
9625                 error "error: could not get fid for $test_dir/$dir/$tfile."
9626
9627         ffid=$MOUNT/.lustre/fid/$fid
9628
9629         echo "ls $fid"
9630         ls $ffid > /dev/null || error "ls $ffid failed."
9631         echo "touch $fid/$tfile.1"
9632         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9633
9634         echo "touch $MOUNT/.lustre/fid/$tfile"
9635         touch $MOUNT/.lustre/fid/$tfile && \
9636                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9637
9638         echo "setxattr to $MOUNT/.lustre/fid"
9639         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9640
9641         echo "listxattr for $MOUNT/.lustre/fid"
9642         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9643
9644         echo "delxattr from $MOUNT/.lustre/fid"
9645         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9646
9647         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9648         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9649                 error "touch invalid fid should fail."
9650
9651         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9652         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9653                 error "touch non-normal fid should fail."
9654
9655         echo "rename $tdir to $MOUNT/.lustre/fid"
9656         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9657                 error "rename to $MOUNT/.lustre/fid should fail."
9658
9659         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9660         then            # LU-3547
9661                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9662                 local new_obf_mode=777
9663
9664                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9665                 chmod $new_obf_mode $DIR/.lustre/fid ||
9666                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9667
9668                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9669                 [ $obf_mode -eq $new_obf_mode ] ||
9670                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9671
9672                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9673                 chmod $old_obf_mode $DIR/.lustre/fid ||
9674                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9675         fi
9676
9677         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9678         fid=$($LFS path2fid $test_dir/$tfile-2)
9679
9680         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9681         then # LU-5424
9682                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9683                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9684                         error "create lov data thru .lustre failed"
9685         fi
9686         echo "cp /etc/passwd $test_dir/$tfile-2"
9687         cp /etc/passwd $test_dir/$tfile-2 ||
9688                 error "copy to $test_dir/$tfile-2 failed."
9689         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9690         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9691                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9692
9693         rm -rf $test_dir/tfile.lnk
9694         rm -rf $test_dir/$tfile-2
9695 }
9696
9697 test_154A() {
9698         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9699                 skip "Need MDS version at least 2.4.1" && return
9700
9701         touch $DIR/$tfile
9702         local FID=$($LFS path2fid $DIR/$tfile)
9703         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9704
9705         # check that we get the same pathname back
9706         local FOUND=$($LFS fid2path $MOUNT "$FID")
9707         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9708         [ "$FOUND" != "$DIR/$tfile" ] &&
9709                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9710
9711         rm -rf $DIR/$tfile
9712 }
9713 run_test 154A "lfs path2fid and fid2path basic checks"
9714
9715 test_154a() {
9716         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9717         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9718                 { skip "Need MDS version at least 2.2.51"; return 0; }
9719         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9720
9721         cp /etc/hosts $DIR/$tfile
9722
9723         fid=$($LFS path2fid $DIR/$tfile)
9724         rc=$?
9725         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9726
9727         dot_lustre_fid_permission_check "$fid" $DIR ||
9728                 error "dot lustre permission check $fid failed"
9729
9730         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9731
9732         touch $MOUNT/.lustre/file &&
9733                 error "creation is not allowed under .lustre"
9734
9735         mkdir $MOUNT/.lustre/dir &&
9736                 error "mkdir is not allowed under .lustre"
9737
9738         rm -rf $DIR/$tfile
9739 }
9740 run_test 154a "Open-by-FID"
9741
9742 test_154b() {
9743         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9744         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9745                 { skip "Need MDS version at least 2.2.51"; return 0; }
9746
9747         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9748
9749         local remote_dir=$DIR/$tdir/remote_dir
9750         local MDTIDX=1
9751         local rc=0
9752
9753         mkdir -p $DIR/$tdir
9754         $LFS mkdir -i $MDTIDX $remote_dir ||
9755                 error "create remote directory failed"
9756
9757         cp /etc/hosts $remote_dir/$tfile
9758
9759         fid=$($LFS path2fid $remote_dir/$tfile)
9760         rc=$?
9761         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9762
9763         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9764                 error "dot lustre permission check $fid failed"
9765         rm -rf $DIR/$tdir
9766 }
9767 run_test 154b "Open-by-FID for remote directory"
9768
9769 test_154c() {
9770         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9771                 skip "Need MDS version at least 2.4.1" && return
9772
9773         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9774         local FID1=$($LFS path2fid $DIR/$tfile.1)
9775         local FID2=$($LFS path2fid $DIR/$tfile.2)
9776         local FID3=$($LFS path2fid $DIR/$tfile.3)
9777
9778         local N=1
9779         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9780                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9781                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9782                 local want=FID$N
9783                 [ "$FID" = "${!want}" ] ||
9784                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9785                 N=$((N + 1))
9786         done
9787
9788         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
9789         do
9790                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9791                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9792                 N=$((N + 1))
9793         done
9794 }
9795 run_test 154c "lfs path2fid and fid2path multiple arguments"
9796
9797 test_154d() {
9798         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9799                 skip "Need MDS version at least 2.5.53" && return
9800
9801         if remote_mds; then
9802                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9803         else
9804                 nid="0@lo"
9805         fi
9806         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9807         local fd
9808         local cmd
9809
9810         rm -f $DIR/$tfile
9811         touch $DIR/$tfile
9812
9813         local fid=$($LFS path2fid $DIR/$tfile)
9814         # Open the file
9815         fd=$(free_fd)
9816         cmd="exec $fd<$DIR/$tfile"
9817         eval $cmd
9818         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9819         echo "$fid_list" | grep "$fid"
9820         rc=$?
9821
9822         cmd="exec $fd>/dev/null"
9823         eval $cmd
9824         if [ $rc -ne 0 ]; then
9825                 error "FID $fid not found in open files list $fid_list"
9826         fi
9827 }
9828 run_test 154d "Verify open file fid"
9829
9830 test_154e()
9831 {
9832         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9833                 skip "Need MDS version at least 2.6.50" && return
9834
9835         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9836                 error ".lustre returned by readdir"
9837         fi
9838 }
9839 run_test 154e ".lustre is not returned by readdir"
9840
9841 test_154f() {
9842         # create parent directory on a single MDT to avoid cross-MDT hardlinks
9843         test_mkdir -p -c1 $DIR/$tdir/d
9844         # test dirs inherit from its stripe
9845         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
9846         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
9847         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
9848         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
9849         touch $DIR/f
9850
9851         # get fid of parents
9852         local FID0=$($LFS path2fid $DIR/$tdir/d)
9853         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
9854         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
9855         local FID3=$($LFS path2fid $DIR)
9856
9857         # check that path2fid --parents returns expected <parent_fid>/name
9858         # 1) test for a directory (single parent)
9859         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
9860         [ "$parent" == "$FID0/foo1" ] ||
9861                 error "expected parent: $FID0/foo1, got: $parent"
9862
9863         # 2) test for a file with nlink > 1 (multiple parents)
9864         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
9865         echo "$parent" | grep -F "$FID1/$tfile" ||
9866                 error "$FID1/$tfile not returned in parent list"
9867         echo "$parent" | grep -F "$FID2/link" ||
9868                 error "$FID2/link not returned in parent list"
9869
9870         # 3) get parent by fid
9871         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
9872         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9873         echo "$parent" | grep -F "$FID1/$tfile" ||
9874                 error "$FID1/$tfile not returned in parent list (by fid)"
9875         echo "$parent" | grep -F "$FID2/link" ||
9876                 error "$FID2/link not returned in parent list (by fid)"
9877
9878         # 4) test for entry in root directory
9879         parent=$($LFS path2fid --parents $DIR/f)
9880         echo "$parent" | grep -F "$FID3/f" ||
9881                 error "$FID3/f not returned in parent list"
9882
9883         # 5) test it on root directory
9884         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
9885                 error "$MOUNT should not have parents"
9886
9887         # enable xattr caching and check that linkea is correctly updated
9888         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9889         save_lustre_params client "llite.*.xattr_cache" > $save
9890         lctl set_param llite.*.xattr_cache 1
9891
9892         # 6.1) linkea update on rename
9893         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
9894
9895         # get parents by fid
9896         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9897         # foo1 should no longer be returned in parent list
9898         echo "$parent" | grep -F "$FID1" &&
9899                 error "$FID1 should no longer be in parent list"
9900         # the new path should appear
9901         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
9902                 error "$FID2/$tfile.moved is not in parent list"
9903
9904         # 6.2) linkea update on unlink
9905         rm -f $DIR/$tdir/d/foo2/link
9906         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9907         # foo2/link should no longer be returned in parent list
9908         echo "$parent" | grep -F "$FID2/link" &&
9909                 error "$FID2/link should no longer be in parent list"
9910         true
9911
9912         rm -f $DIR/f
9913         restore_lustre_params < $save
9914 }
9915 run_test 154f "get parent fids by reading link ea"
9916
9917 test_154g()
9918 {
9919         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
9920                 { skip "Need MDS version at least 2.6.92"; return 0; }
9921
9922         mkdir -p $DIR/$tdir
9923         llapi_fid_test -d $DIR/$tdir
9924 }
9925 run_test 154g "various llapi FID tests"
9926
9927 test_155_small_load() {
9928     local temp=$TMP/$tfile
9929     local file=$DIR/$tfile
9930
9931     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9932         error "dd of=$temp bs=6096 count=1 failed"
9933     cp $temp $file
9934     cancel_lru_locks osc
9935     cmp $temp $file || error "$temp $file differ"
9936
9937     $TRUNCATE $temp 6000
9938     $TRUNCATE $file 6000
9939     cmp $temp $file || error "$temp $file differ (truncate1)"
9940
9941     echo "12345" >>$temp
9942     echo "12345" >>$file
9943     cmp $temp $file || error "$temp $file differ (append1)"
9944
9945     echo "12345" >>$temp
9946     echo "12345" >>$file
9947     cmp $temp $file || error "$temp $file differ (append2)"
9948
9949     rm -f $temp $file
9950     true
9951 }
9952
9953 test_155_big_load() {
9954     remote_ost_nodsh && skip "remote OST with nodsh" && return
9955     local temp=$TMP/$tfile
9956     local file=$DIR/$tfile
9957
9958     free_min_max
9959     local cache_size=$(do_facet ost$((MAXI+1)) \
9960         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9961     local large_file_size=$((cache_size * 2))
9962
9963     echo "OSS cache size: $cache_size KB"
9964     echo "Large file size: $large_file_size KB"
9965
9966     [ $MAXV -le $large_file_size ] && \
9967         skip_env "max available OST size needs > $large_file_size KB" && \
9968         return 0
9969
9970     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9971
9972     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9973         error "dd of=$temp bs=$large_file_size count=1k failed"
9974     cp $temp $file
9975     ls -lh $temp $file
9976     cancel_lru_locks osc
9977     cmp $temp $file || error "$temp $file differ"
9978
9979     rm -f $temp $file
9980     true
9981 }
9982
9983 test_155a() {
9984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9985         set_cache read on
9986         set_cache writethrough on
9987         test_155_small_load
9988 }
9989 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9990
9991 test_155b() {
9992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9993         set_cache read on
9994         set_cache writethrough off
9995         test_155_small_load
9996 }
9997 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9998
9999 test_155c() {
10000         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10001         set_cache read off
10002         set_cache writethrough on
10003         test_155_small_load
10004 }
10005 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10006
10007 test_155d() {
10008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10009         set_cache read off
10010         set_cache writethrough off
10011         test_155_small_load
10012 }
10013 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10014
10015 test_155e() {
10016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10017         set_cache read on
10018         set_cache writethrough on
10019         test_155_big_load
10020 }
10021 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10022
10023 test_155f() {
10024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10025         set_cache read on
10026         set_cache writethrough off
10027         test_155_big_load
10028 }
10029 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10030
10031 test_155g() {
10032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10033         set_cache read off
10034         set_cache writethrough on
10035         test_155_big_load
10036 }
10037 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10038
10039 test_155h() {
10040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10041         set_cache read off
10042         set_cache writethrough off
10043         test_155_big_load
10044 }
10045 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10046
10047 test_156() {
10048         remote_ost_nodsh && skip "remote OST with nodsh" && return
10049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10050         local CPAGES=3
10051         local BEFORE
10052         local AFTER
10053         local file="$DIR/$tfile"
10054
10055         [ "$(facet_fstype ost1)" = "zfs" ] &&
10056                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
10057                 return
10058
10059         roc_hit_init
10060
10061     log "Turn on read and write cache"
10062     set_cache read on
10063     set_cache writethrough on
10064
10065     log "Write data and read it back."
10066     log "Read should be satisfied from the cache."
10067     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10068     BEFORE=`roc_hit`
10069     cancel_lru_locks osc
10070     cat $file >/dev/null
10071     AFTER=`roc_hit`
10072     if ! let "AFTER - BEFORE == CPAGES"; then
10073         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10074     else
10075         log "cache hits:: before: $BEFORE, after: $AFTER"
10076     fi
10077
10078     log "Read again; it should be satisfied from the cache."
10079     BEFORE=$AFTER
10080     cancel_lru_locks osc
10081     cat $file >/dev/null
10082     AFTER=`roc_hit`
10083     if ! let "AFTER - BEFORE == CPAGES"; then
10084         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10085     else
10086         log "cache hits:: before: $BEFORE, after: $AFTER"
10087     fi
10088
10089
10090     log "Turn off the read cache and turn on the write cache"
10091     set_cache read off
10092     set_cache writethrough on
10093
10094     log "Read again; it should be satisfied from the cache."
10095     BEFORE=`roc_hit`
10096     cancel_lru_locks osc
10097     cat $file >/dev/null
10098     AFTER=`roc_hit`
10099     if ! let "AFTER - BEFORE == CPAGES"; then
10100         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10101     else
10102         log "cache hits:: before: $BEFORE, after: $AFTER"
10103     fi
10104
10105     log "Read again; it should not be satisfied from the cache."
10106     BEFORE=$AFTER
10107     cancel_lru_locks osc
10108     cat $file >/dev/null
10109     AFTER=`roc_hit`
10110     if ! let "AFTER - BEFORE == 0"; then
10111         error "IN CACHE: before: $BEFORE, after: $AFTER"
10112     else
10113         log "cache hits:: before: $BEFORE, after: $AFTER"
10114     fi
10115
10116     log "Write data and read it back."
10117     log "Read should be satisfied from the cache."
10118     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10119     BEFORE=`roc_hit`
10120     cancel_lru_locks osc
10121     cat $file >/dev/null
10122     AFTER=`roc_hit`
10123     if ! let "AFTER - BEFORE == CPAGES"; then
10124         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10125     else
10126         log "cache hits:: before: $BEFORE, after: $AFTER"
10127     fi
10128
10129     log "Read again; it should not be satisfied from the cache."
10130     BEFORE=$AFTER
10131     cancel_lru_locks osc
10132     cat $file >/dev/null
10133     AFTER=`roc_hit`
10134     if ! let "AFTER - BEFORE == 0"; then
10135         error "IN CACHE: before: $BEFORE, after: $AFTER"
10136     else
10137         log "cache hits:: before: $BEFORE, after: $AFTER"
10138     fi
10139
10140
10141     log "Turn off read and write cache"
10142     set_cache read off
10143     set_cache writethrough off
10144
10145     log "Write data and read it back"
10146     log "It should not be satisfied from the cache."
10147     rm -f $file
10148     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10149     cancel_lru_locks osc
10150     BEFORE=`roc_hit`
10151     cat $file >/dev/null
10152     AFTER=`roc_hit`
10153         if ! let "AFTER - BEFORE == 0"; then
10154                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10155         else
10156                 log "cache hits:: before: $BEFORE, after: $AFTER"
10157         fi
10158
10159     log "Turn on the read cache and turn off the write cache"
10160     set_cache read on
10161     set_cache writethrough off
10162
10163     log "Write data and read it back"
10164     log "It should not be satisfied from the cache."
10165     rm -f $file
10166     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10167     BEFORE=`roc_hit`
10168     cancel_lru_locks osc
10169     cat $file >/dev/null
10170     AFTER=`roc_hit`
10171         if ! let "AFTER - BEFORE == 0"; then
10172                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10173         else
10174                 log "cache hits:: before: $BEFORE, after: $AFTER"
10175         fi
10176
10177     log "Read again; it should be satisfied from the cache."
10178     BEFORE=`roc_hit`
10179     cancel_lru_locks osc
10180     cat $file >/dev/null
10181     AFTER=`roc_hit`
10182     if ! let "AFTER - BEFORE == CPAGES"; then
10183         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10184     else
10185         log "cache hits:: before: $BEFORE, after: $AFTER"
10186     fi
10187
10188     rm -f $file
10189 }
10190 run_test 156 "Verification of tunables ============================"
10191
10192 #Changelogs
10193 err17935 () {
10194         if [[ $MDSCOUNT -gt 1 ]]; then
10195                 error_ignore bz17935 $*
10196         else
10197                 error $*
10198         fi
10199 }
10200
10201 changelog_chmask()
10202 {
10203         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10204
10205         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10206
10207         if [ $MASK -eq 1 ]; then
10208                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10209         else
10210                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10211         fi
10212 }
10213
10214 changelog_extract_field() {
10215         local mdt=$1
10216         local cltype=$2
10217         local file=$3
10218         local identifier=$4
10219
10220         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10221                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10222                 tail -1
10223 }
10224
10225 test_160a() {
10226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10227         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10228         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10229                 { skip "Need MDS version at least 2.2.0"; return; }
10230
10231         local CL_USERS="mdd.$MDT0.changelog_users"
10232         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10233         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10234         echo "Registered as changelog user $USER"
10235         $GET_CL_USERS | grep -q $USER ||
10236                 error "User $USER not found in changelog_users"
10237
10238         # change something
10239         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10240         touch $DIR/$tdir/pics/2008/zachy/timestamp
10241         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10242         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10243         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10244         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10245         rm $DIR/$tdir/pics/desktop.jpg
10246
10247         $LFS changelog $MDT0 | tail -5
10248
10249         echo "verifying changelog mask"
10250         changelog_chmask "MKDIR"
10251         changelog_chmask "CLOSE"
10252
10253         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10254         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10255
10256         changelog_chmask "MKDIR"
10257         changelog_chmask "CLOSE"
10258
10259         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10260         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10261
10262         $LFS changelog $MDT0
10263         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10264         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10265         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10266         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10267
10268         # verify contents
10269         echo "verifying target fid"
10270         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10271         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10272         [ "$fidc" == "$fidf" ] ||
10273                 err17935 "fid in changelog $fidc != file fid $fidf"
10274         echo "verifying parent fid"
10275         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10276         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10277         [ "$fidc" == "$fidf" ] ||
10278                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10279
10280         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10281         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10282         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10283         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10284         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10285                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10286
10287         MIN_REC=$($GET_CL_USERS |
10288                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10289         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10290         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10291         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10292                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10293
10294         # LU-3446 changelog index reset on MDT restart
10295         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10296         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10297         $LFS changelog_clear $MDT0 $USER 0
10298         stop $SINGLEMDS || error "Fail to stop MDT."
10299         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10300         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10301         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10302         [ $CUR_REC1 == $CUR_REC2 ] ||
10303                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10304
10305         echo "verifying user deregister"
10306         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10307         $GET_CL_USERS | grep -q $USER &&
10308                 error "User $USER still in changelog_users"
10309
10310         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10311         if [ $USERS -eq 0 ]; then
10312                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10313                 touch $DIR/$tdir/chloe
10314                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10315                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10316                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10317         else
10318                 echo "$USERS other changelog users; can't verify off"
10319         fi
10320 }
10321 run_test 160a "changelog sanity"
10322
10323 test_160b() { # LU-3587
10324         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10325         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10326         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10327                 { skip "Need MDS version at least 2.2.0"; return; }
10328
10329         local CL_USERS="mdd.$MDT0.changelog_users"
10330         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10331         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10332         echo "Registered as changelog user $USER"
10333         $GET_CL_USERS | grep -q $USER ||
10334                 error "User $USER not found in changelog_users"
10335
10336         local LONGNAME1=$(str_repeat a 255)
10337         local LONGNAME2=$(str_repeat b 255)
10338
10339         cd $DIR
10340         echo "creating very long named file"
10341         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10342         echo "moving very long named file"
10343         mv $LONGNAME1 $LONGNAME2
10344
10345         $LFS changelog $MDT0 | grep RENME
10346
10347         echo "deregistering $USER"
10348         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10349
10350         rm -f $LONGNAME2
10351 }
10352 run_test 160b "Verify that very long rename doesn't crash in changelog"
10353
10354 test_160c() {
10355         local rc=0
10356         local server_version=$(lustre_version_code $SINGLEMDS)
10357
10358         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10359                 [[ $server_version -gt $(version_code 2.5.1) &&
10360                    $server_version -lt $(version_code 2.5.50) ]] ||
10361                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10363
10364         # Registration step
10365         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10366                 changelog_register -n)
10367
10368         rm -rf $DIR/$tdir
10369         mkdir -p $DIR/$tdir
10370         $MCREATE $DIR/$tdir/foo_160c
10371         changelog_chmask "TRUNC"
10372         $TRUNCATE $DIR/$tdir/foo_160c 200
10373         changelog_chmask "TRUNC"
10374         $TRUNCATE $DIR/$tdir/foo_160c 199
10375         $LFS changelog $MDT0
10376         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10377         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10378         $LFS changelog_clear $MDT0 $USER 0
10379
10380         # Deregistration step
10381         echo "deregistering $USER"
10382         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10383 }
10384 run_test 160c "verify that changelog log catch the truncate event"
10385
10386 test_161a() {
10387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10388         test_mkdir -p -c1 $DIR/$tdir
10389         cp /etc/hosts $DIR/$tdir/$tfile
10390         test_mkdir -c1 $DIR/$tdir/foo1
10391         test_mkdir -c1 $DIR/$tdir/foo2
10392         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10393         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10394         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10395         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10396         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10397         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10398                 $LFS fid2path $DIR $FID
10399                 err17935 "bad link ea"
10400         fi
10401     # middle
10402     rm $DIR/$tdir/foo2/zachary
10403     # last
10404     rm $DIR/$tdir/foo2/thor
10405     # first
10406     rm $DIR/$tdir/$tfile
10407     # rename
10408     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10409     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10410         then
10411         $LFS fid2path $DIR $FID
10412         err17935 "bad link rename"
10413     fi
10414     rm $DIR/$tdir/foo2/maggie
10415
10416         # overflow the EA
10417         local longname=filename_avg_len_is_thirty_two_
10418         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10419                 error "failed to hardlink many files"
10420         links=$($LFS fid2path $DIR $FID | wc -l)
10421         echo -n "${links}/1000 links in link EA"
10422         [[ $links -gt 60 ]] ||
10423                 err17935 "expected at least 60 links in link EA"
10424         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10425                 error "failed to unlink many hardlinks"
10426 }
10427 run_test 161a "link ea sanity"
10428
10429 test_161b() {
10430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10431         [ $MDSCOUNT -lt 2 ] &&
10432                 skip "skipping remote directory test" && return
10433         local MDTIDX=1
10434         local remote_dir=$DIR/$tdir/remote_dir
10435
10436         mkdir -p $DIR/$tdir
10437         $LFS mkdir -i $MDTIDX $remote_dir ||
10438                 error "create remote directory failed"
10439
10440         cp /etc/hosts $remote_dir/$tfile
10441         mkdir -p $remote_dir/foo1
10442         mkdir -p $remote_dir/foo2
10443         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10444         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10445         ln $remote_dir/$tfile $remote_dir/foo1/luna
10446         ln $remote_dir/$tfile $remote_dir/foo2/thor
10447
10448         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10449                      tr -d ']')
10450         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10451                 $LFS fid2path $DIR $FID
10452                 err17935 "bad link ea"
10453         fi
10454         # middle
10455         rm $remote_dir/foo2/zachary
10456         # last
10457         rm $remote_dir/foo2/thor
10458         # first
10459         rm $remote_dir/$tfile
10460         # rename
10461         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10462         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10463         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10464                 $LFS fid2path $DIR $FID
10465                 err17935 "bad link rename"
10466         fi
10467         rm $remote_dir/foo2/maggie
10468
10469         # overflow the EA
10470         local longname=filename_avg_len_is_thirty_two_
10471         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10472                 error "failed to hardlink many files"
10473         links=$($LFS fid2path $DIR $FID | wc -l)
10474         echo -n "${links}/1000 links in link EA"
10475         [[ ${links} -gt 60 ]] ||
10476                 err17935 "expected at least 60 links in link EA"
10477         unlinkmany $remote_dir/foo2/$longname 1000 ||
10478         error "failed to unlink many hardlinks"
10479 }
10480 run_test 161b "link ea sanity under remote directory"
10481
10482 test_161c() {
10483         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10485         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10486                 skip "Need MDS version at least 2.1.5" && return
10487
10488         # define CLF_RENAME_LAST 0x0001
10489         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10490         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10491                 changelog_register -n)
10492         rm -rf $DIR/$tdir
10493         mkdir -p $DIR/$tdir
10494         touch $DIR/$tdir/foo_161c
10495         touch $DIR/$tdir/bar_161c
10496         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10497         $LFS changelog $MDT0 | grep RENME
10498         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10499                 cut -f5 -d' ')
10500         $LFS changelog_clear $MDT0 $USER 0
10501         if [ x$flags != "x0x1" ]; then
10502                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10503                         $USER
10504                 error "flag $flags is not 0x1"
10505         fi
10506         echo "rename overwrite a target having nlink = 1," \
10507                 "changelog record has flags of $flags"
10508
10509         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10510         touch $DIR/$tdir/foo_161c
10511         touch $DIR/$tdir/bar_161c
10512         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10513         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10514         $LFS changelog $MDT0 | grep RENME
10515         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10516         $LFS changelog_clear $MDT0 $USER 0
10517         if [ x$flags != "x0x0" ]; then
10518                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10519                         $USER
10520                 error "flag $flags is not 0x0"
10521         fi
10522         echo "rename overwrite a target having nlink > 1," \
10523                 "changelog record has flags of $flags"
10524
10525         # rename doesn't overwrite a target (changelog flag 0x0)
10526         touch $DIR/$tdir/foo_161c
10527         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10528         $LFS changelog $MDT0 | grep RENME
10529         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10530         $LFS changelog_clear $MDT0 $USER 0
10531         if [ x$flags != "x0x0" ]; then
10532                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10533                         $USER
10534                 error "flag $flags is not 0x0"
10535         fi
10536         echo "rename doesn't overwrite a target," \
10537                 "changelog record has flags of $flags"
10538
10539         # define CLF_UNLINK_LAST 0x0001
10540         # unlink a file having nlink = 1 (changelog flag 0x1)
10541         rm -f $DIR/$tdir/foo2_161c
10542         $LFS changelog $MDT0 | grep UNLNK
10543         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10544         $LFS changelog_clear $MDT0 $USER 0
10545         if [ x$flags != "x0x1" ]; then
10546                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10547                         $USER
10548                 error "flag $flags is not 0x1"
10549         fi
10550         echo "unlink a file having nlink = 1," \
10551                 "changelog record has flags of $flags"
10552
10553         # unlink a file having nlink > 1 (changelog flag 0x0)
10554         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10555         rm -f $DIR/$tdir/foobar_161c
10556         $LFS changelog $MDT0 | grep UNLNK
10557         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10558         $LFS changelog_clear $MDT0 $USER 0
10559         if [ x$flags != "x0x0" ]; then
10560                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10561                         $USER
10562                 error "flag $flags is not 0x0"
10563         fi
10564         echo "unlink a file having nlink > 1," \
10565                 "changelog record has flags of $flags"
10566         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10567 }
10568 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10569
10570 check_path() {
10571     local expected=$1
10572     shift
10573     local fid=$2
10574
10575     local path=$(${LFS} fid2path $*)
10576     # Remove the '//' indicating a remote directory
10577     path=$(echo $path | sed 's#//#/#g')
10578     RC=$?
10579
10580     if [ $RC -ne 0 ]; then
10581         err17935 "path looked up of $expected failed. Error $RC"
10582         return $RC
10583     elif [ "${path}" != "${expected}" ]; then
10584         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10585         return 2
10586     fi
10587     echo "fid $fid resolves to path $path (expected $expected)"
10588 }
10589
10590 test_162() {
10591         # Make changes to filesystem
10592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10593         test_mkdir -p -c1 $DIR/$tdir/d2
10594         touch $DIR/$tdir/d2/$tfile
10595         touch $DIR/$tdir/d2/x1
10596         touch $DIR/$tdir/d2/x2
10597         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10598         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10599         # regular file
10600         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10601         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10602                 error "check path $tdir/d2/$tfile failed"
10603
10604         # softlink
10605         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10606         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10607         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10608                 error "check path $tdir/d2/p/q/r/slink failed"
10609
10610         # softlink to wrong file
10611         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10612         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10613         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10614                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10615
10616         # hardlink
10617         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10618         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10619         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10620         # fid2path dir/fsname should both work
10621         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10622                 error "check path $tdir/d2/a/b/c/new_file failed"
10623         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10624                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10625
10626         # hardlink count: check that there are 2 links
10627         # Doesnt work with CMD yet: 17935
10628         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10629                 err17935 "expected 2 links"
10630
10631         # hardlink indexing: remove the first link
10632         rm $DIR/$tdir/d2/p/q/r/hlink
10633         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10634                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10635
10636         return 0
10637 }
10638 run_test 162 "path lookup sanity"
10639
10640 test_162b() {
10641         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10642         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10643
10644         mkdir $DIR/$tdir
10645         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10646                                 error "create striped dir failed"
10647
10648         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10649                                         tail -n 1 | awk '{print $2}')
10650         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10651
10652         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10653         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10654
10655         # regular file
10656         for ((i=0;i<5;i++)); do
10657                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10658                         error "get fid for f$i failed"
10659                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10660                         error "check path $tdir/striped_dir/f$i failed"
10661
10662                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10663                         error "get fid for d$i failed"
10664                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10665                         error "check path $tdir/striped_dir/d$i failed"
10666         done
10667
10668         return 0
10669 }
10670 run_test 162b "striped directory path lookup sanity"
10671
10672 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10673 test_162c() {
10674         test_mkdir $DIR/$tdir.local
10675         test_mkdir $DIR/$tdir.remote
10676         local lpath=$tdir.local
10677         local rpath=$tdir.remote
10678
10679         for ((i = 0; i <= 101; i++)); do
10680                 lpath="$lpath/$i"
10681                 mkdir $DIR/$lpath
10682                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10683                         error "get fid for local directory $DIR/$lpath failed"
10684                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10685                         error "check path for local directory $DIR/$lpath failed"
10686
10687                 rpath="$rpath/$i"
10688                 test_mkdir $DIR/$rpath
10689                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10690                         error "get fid for remote directory $DIR/$rpath failed"
10691                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10692                         error "check path for remote directory $DIR/$rpath failed"
10693         done
10694
10695         return 0
10696 }
10697 run_test 162c "fid2path works with paths 100 or more directories deep"
10698
10699 test_169() {
10700         # do directio so as not to populate the page cache
10701         log "creating a 10 Mb file"
10702         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10703         log "starting reads"
10704         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10705         log "truncating the file"
10706         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10707         log "killing dd"
10708         kill %+ || true # reads might have finished
10709         echo "wait until dd is finished"
10710         wait
10711         log "removing the temporary file"
10712         rm -rf $DIR/$tfile || error "tmp file removal failed"
10713 }
10714 run_test 169 "parallel read and truncate should not deadlock"
10715
10716 test_170() {
10717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10718         $LCTL clear     # bug 18514
10719         $LCTL debug_daemon start $TMP/${tfile}_log_good
10720         touch $DIR/$tfile
10721         $LCTL debug_daemon stop
10722         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10723                error "sed failed to read log_good"
10724
10725         $LCTL debug_daemon start $TMP/${tfile}_log_good
10726         rm -rf $DIR/$tfile
10727         $LCTL debug_daemon stop
10728
10729         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10730                error "lctl df log_bad failed"
10731
10732         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10733         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10734
10735         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10736         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10737
10738         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10739                 error "bad_line good_line1 good_line2 are empty"
10740
10741         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10742         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10743         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10744
10745         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10746         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10747         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10748
10749         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10750                 error "bad_line_new good_line_new are empty"
10751
10752         local expected_good=$((good_line1 + good_line2*2))
10753
10754         rm -f $TMP/${tfile}*
10755         # LU-231, short malformed line may not be counted into bad lines
10756         if [ $bad_line -ne $bad_line_new ] &&
10757                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10758                 error "expected $bad_line bad lines, but got $bad_line_new"
10759                 return 1
10760         fi
10761
10762         if [ $expected_good -ne $good_line_new ]; then
10763                 error "expected $expected_good good lines, but got $good_line_new"
10764                 return 2
10765         fi
10766         true
10767 }
10768 run_test 170 "test lctl df to handle corrupted log ====================="
10769
10770 test_171() { # bug20592
10771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10772 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10773         $LCTL set_param fail_loc=0x50e
10774         $LCTL set_param fail_val=3000
10775         multiop_bg_pause $DIR/$tfile O_s || true
10776         local MULTIPID=$!
10777         kill -USR1 $MULTIPID
10778         # cause log dump
10779         sleep 3
10780         wait $MULTIPID
10781         if dmesg | grep "recursive fault"; then
10782                 error "caught a recursive fault"
10783         fi
10784         $LCTL set_param fail_loc=0
10785         true
10786 }
10787 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10788
10789 # it would be good to share it with obdfilter-survey/iokit-libecho code
10790 setup_obdecho_osc () {
10791         local rc=0
10792         local ost_nid=$1
10793         local obdfilter_name=$2
10794         echo "Creating new osc for $obdfilter_name on $ost_nid"
10795         # make sure we can find loopback nid
10796         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10797
10798         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10799                            ${obdfilter_name}_osc_UUID || rc=2; }
10800         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10801                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10802         return $rc
10803 }
10804
10805 cleanup_obdecho_osc () {
10806         local obdfilter_name=$1
10807         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10808         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10809         return 0
10810 }
10811
10812 obdecho_test() {
10813         local OBD=$1
10814         local node=$2
10815         local pages=${3:-64}
10816         local rc=0
10817         local id
10818
10819         local count=10
10820         local obd_size=$(get_obd_size $node $OBD)
10821         local page_size=$(get_page_size $node)
10822         if [[ -n "$obd_size" ]]; then
10823                 local new_count=$((obd_size / (pages * page_size / 1024)))
10824                 [[ $new_count -ge $count ]] || count=$new_count
10825         fi
10826
10827         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10828         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10829                            rc=2; }
10830         if [ $rc -eq 0 ]; then
10831             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10832             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10833         fi
10834         echo "New object id is $id"
10835         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10836                            rc=4; }
10837         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10838                            "test_brw $count w v $pages $id" || rc=4; }
10839         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10840                            rc=4; }
10841         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10842                                         "cleanup" || rc=5; }
10843         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10844                                         "detach" || rc=6; }
10845         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10846         return $rc
10847 }
10848
10849 test_180a() {
10850         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10851         remote_ost_nodsh && skip "remote OST with nodsh" && return
10852         local rc=0
10853         local rmmod_local=0
10854
10855         if ! module_loaded obdecho; then
10856             load_module obdecho/obdecho
10857             rmmod_local=1
10858         fi
10859
10860         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10861         local host=$(lctl get_param -n osc.$osc.import |
10862                              awk '/current_connection:/ {print $2}' )
10863         local target=$(lctl get_param -n osc.$osc.import |
10864                              awk '/target:/ {print $2}' )
10865         target=${target%_UUID}
10866
10867         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10868         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10869         [[ -n $target ]] && cleanup_obdecho_osc $target
10870         [ $rmmod_local -eq 1 ] && rmmod obdecho
10871         return $rc
10872 }
10873 run_test 180a "test obdecho on osc"
10874
10875 test_180b() {
10876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10877         remote_ost_nodsh && skip "remote OST with nodsh" && return
10878         local rc=0
10879         local rmmod_remote=0
10880
10881         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10882                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10883                       "modprobe obdecho; }" && rmmod_remote=1
10884         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10885         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10886         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10887         return $rc
10888 }
10889 run_test 180b "test obdecho directly on obdfilter"
10890
10891 test_180c() { # LU-2598
10892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10893         remote_ost_nodsh && skip "remote OST with nodsh" && return
10894         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10895                 skip "Need MDS version at least 2.4.0" && return
10896
10897         local rc=0
10898         local rmmod_remote=false
10899         local pages=16384 # 64MB bulk I/O RPC size
10900         local target
10901
10902         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10903                 rmmod_remote=true || error "failed to load module obdecho"
10904
10905         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10906                 head -n1)
10907         if [ -n "$target" ]; then
10908                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10909         else
10910                 echo "there is no obdfilter target on ost1"
10911                 rc=2
10912         fi
10913         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10914         return $rc
10915 }
10916 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10917
10918 test_181() { # bug 22177
10919         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10920         # create enough files to index the directory
10921         createmany -o $DIR/$tdir/foobar 4000
10922         # print attributes for debug purpose
10923         lsattr -d .
10924         # open dir
10925         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10926         MULTIPID=$!
10927         # remove the files & current working dir
10928         unlinkmany $DIR/$tdir/foobar 4000
10929         rmdir $DIR/$tdir
10930         kill -USR1 $MULTIPID
10931         wait $MULTIPID
10932         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10933         return 0
10934 }
10935 run_test 181 "Test open-unlinked dir ========================"
10936
10937 test_182() {
10938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10939         # disable MDC RPC lock wouldn't crash client
10940         local fcount=1000
10941         local tcount=4
10942
10943         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10944 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10945         $LCTL set_param fail_loc=0x804
10946
10947         for (( i=0; i < $tcount; i++ )) ; do
10948                 mkdir $DIR/$tdir/$i
10949                 createmany -o $DIR/$tdir/$i/f- $fcount &
10950         done
10951         wait
10952
10953         for (( i=0; i < $tcount; i++ )) ; do
10954                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10955         done
10956         wait
10957
10958         rm -rf $DIR/$tdir
10959
10960         $LCTL set_param fail_loc=0
10961 }
10962 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10963
10964 test_183() { # LU-2275
10965         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10966         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10967                 skip "Need MDS version at least 2.3.56" && return
10968
10969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10970         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10971         echo aaa > $DIR/$tdir/$tfile
10972
10973 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10974         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10975
10976         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10977         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10978
10979         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10980
10981         # Flush negative dentry cache
10982         touch $DIR/$tdir/$tfile
10983
10984         # We are not checking for any leaked references here, they'll
10985         # become evident next time we do cleanup with module unload.
10986         rm -rf $DIR/$tdir
10987 }
10988 run_test 183 "No crash or request leak in case of strange dispositions ========"
10989
10990 # test suite 184 is for LU-2016, LU-2017
10991 test_184a() {
10992         check_swap_layouts_support && return 0
10993
10994         dir0=$DIR/$tdir/$testnum
10995         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10996         ref1=/etc/passwd
10997         ref2=/etc/group
10998         file1=$dir0/f1
10999         file2=$dir0/f2
11000         $SETSTRIPE -c1 $file1
11001         cp $ref1 $file1
11002         $SETSTRIPE -c2 $file2
11003         cp $ref2 $file2
11004         gen1=$($GETSTRIPE -g $file1)
11005         gen2=$($GETSTRIPE -g $file2)
11006
11007         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11008         gen=$($GETSTRIPE -g $file1)
11009         [[ $gen1 != $gen ]] ||
11010                 "Layout generation on $file1 does not change"
11011         gen=$($GETSTRIPE -g $file2)
11012         [[ $gen2 != $gen ]] ||
11013                 "Layout generation on $file2 does not change"
11014
11015         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11016         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11017 }
11018 run_test 184a "Basic layout swap"
11019
11020 test_184b() {
11021         check_swap_layouts_support && return 0
11022
11023         dir0=$DIR/$tdir/$testnum
11024         mkdir -p $dir0 || error "creating dir $dir0"
11025         file1=$dir0/f1
11026         file2=$dir0/f2
11027         file3=$dir0/f3
11028         dir1=$dir0/d1
11029         dir2=$dir0/d2
11030         mkdir $dir1 $dir2
11031         $SETSTRIPE -c1 $file1
11032         $SETSTRIPE -c2 $file2
11033         $SETSTRIPE -c1 $file3
11034         chown $RUNAS_ID $file3
11035         gen1=$($GETSTRIPE -g $file1)
11036         gen2=$($GETSTRIPE -g $file2)
11037
11038         $LFS swap_layouts $dir1 $dir2 &&
11039                 error "swap of directories layouts should fail"
11040         $LFS swap_layouts $dir1 $file1 &&
11041                 error "swap of directory and file layouts should fail"
11042         $RUNAS $LFS swap_layouts $file1 $file2 &&
11043                 error "swap of file we cannot write should fail"
11044         $LFS swap_layouts $file1 $file3 &&
11045                 error "swap of file with different owner should fail"
11046         /bin/true # to clear error code
11047 }
11048 run_test 184b "Forbidden layout swap (will generate errors)"
11049
11050 test_184c() {
11051         check_swap_layouts_support && return 0
11052
11053         local dir0=$DIR/$tdir/$testnum
11054         mkdir -p $dir0 || error "creating dir $dir0"
11055
11056         local ref1=$dir0/ref1
11057         local ref2=$dir0/ref2
11058         local file1=$dir0/file1
11059         local file2=$dir0/file2
11060         # create a file large enough for the concurent test
11061         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11062         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11063         echo "ref file size: ref1($(stat -c %s $ref1))," \
11064              "ref2($(stat -c %s $ref2))"
11065
11066         cp $ref2 $file2
11067         dd if=$ref1 of=$file1 bs=16k &
11068         local DD_PID=$!
11069
11070         # Make sure dd starts to copy file
11071         while [ ! -f $file1 ]; do sleep 0.1; done
11072
11073         $LFS swap_layouts $file1 $file2
11074         local rc=$?
11075         wait $DD_PID
11076         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11077         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11078
11079         # how many bytes copied before swapping layout
11080         local copied=$(stat -c %s $file2)
11081         local remaining=$(stat -c %s $ref1)
11082         remaining=$((remaining - copied))
11083         echo "Copied $copied bytes before swapping layout..."
11084
11085         cmp -n $copied $file1 $ref2 | grep differ &&
11086                 error "Content mismatch [0, $copied) of ref2 and file1"
11087         cmp -n $copied $file2 $ref1 ||
11088                 error "Content mismatch [0, $copied) of ref1 and file2"
11089         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11090                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11091
11092         # clean up
11093         rm -f $ref1 $ref2 $file1 $file2
11094 }
11095 run_test 184c "Concurrent write and layout swap"
11096
11097 test_184d() {
11098         check_swap_layouts_support && return 0
11099         [ -z "$(which getfattr 2>/dev/null)" ] &&
11100                 skip "no getfattr command" && return 0
11101
11102         local file1=$DIR/$tdir/$tfile-1
11103         local file2=$DIR/$tdir/$tfile-2
11104         local file3=$DIR/$tdir/$tfile-3
11105         local lovea1
11106         local lovea2
11107
11108         mkdir -p $DIR/$tdir
11109         touch $file1 || error "create $file1 failed"
11110         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11111                 error "create $file2 failed"
11112         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11113                 error "create $file3 failed"
11114         lovea1=$($LFS getstripe $file1 | sed 1d)
11115
11116         $LFS swap_layouts $file2 $file3 ||
11117                 error "swap $file2 $file3 layouts failed"
11118         $LFS swap_layouts $file1 $file2 ||
11119                 error "swap $file1 $file2 layouts failed"
11120
11121         lovea2=$($LFS getstripe $file2 | sed 1d)
11122         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11123
11124         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11125         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11126 }
11127 run_test 184d "allow stripeless layouts swap"
11128
11129 test_184e() {
11130         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11131                 { skip "Need MDS version at least 2.6.94"; return 0; }
11132         check_swap_layouts_support && return 0
11133         [ -z "$(which getfattr 2>/dev/null)" ] &&
11134                 skip "no getfattr command" && return 0
11135
11136         local file1=$DIR/$tdir/$tfile-1
11137         local file2=$DIR/$tdir/$tfile-2
11138         local file3=$DIR/$tdir/$tfile-3
11139         local lovea
11140
11141         mkdir -p $DIR/$tdir
11142         touch $file1 || error "create $file1 failed"
11143         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11144                 error "create $file2 failed"
11145         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11146                 error "create $file3 failed"
11147
11148         $LFS swap_layouts $file1 $file2 ||
11149                 error "swap $file1 $file2 layouts failed"
11150
11151         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11152         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11153
11154         echo 123 > $file1 || error "Should be able to write into $file1"
11155
11156         $LFS swap_layouts $file1 $file3 ||
11157                 error "swap $file1 $file3 layouts failed"
11158
11159         echo 123 > $file1 || error "Should be able to write into $file1"
11160
11161         rm -rf $file1 $file2 $file3
11162 }
11163 run_test 184e "Recreate layout after stripeless layout swaps"
11164
11165 test_185() { # LU-2441
11166         # LU-3553 - no volatile file support in old servers
11167         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11168                 { skip "Need MDS version at least 2.3.60"; return 0; }
11169
11170         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11171         touch $DIR/$tdir/spoo
11172         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11173         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11174                 error "cannot create/write a volatile file"
11175         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11176                 error "FID is still valid after close"
11177
11178         multiop_bg_pause $DIR/$tdir vVw4096_c
11179         local multi_pid=$!
11180
11181         local OLD_IFS=$IFS
11182         IFS=":"
11183         local fidv=($fid)
11184         IFS=$OLD_IFS
11185         # assume that the next FID for this client is sequential, since stdout
11186         # is unfortunately eaten by multiop_bg_pause
11187         local n=$((${fidv[1]} + 1))
11188         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11189         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11190                 error "FID is missing before close"
11191         kill -USR1 $multi_pid
11192         # 1 second delay, so if mtime change we will see it
11193         sleep 1
11194         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11195         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11196 }
11197 run_test 185 "Volatile file support"
11198
11199 test_187a() {
11200         local dir0=$DIR/$tdir/$testnum
11201         mkdir -p $dir0 || error "creating dir $dir0"
11202
11203         local file=$dir0/file1
11204         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11205         local dv1=$($LFS data_version $file)
11206         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11207         local dv2=$($LFS data_version $file)
11208         [[ $dv1 != $dv2 ]] ||
11209                 error "data version did not change on write $dv1 == $dv2"
11210
11211         # clean up
11212         rm -f $file1
11213 }
11214 run_test 187a "Test data version change"
11215
11216 test_187b() {
11217         local dir0=$DIR/$tdir/$testnum
11218         mkdir -p $dir0 || error "creating dir $dir0"
11219
11220         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11221         [[ ${DV[0]} != ${DV[1]} ]] ||
11222                 error "data version did not change on write"\
11223                       " ${DV[0]} == ${DV[1]}"
11224
11225         # clean up
11226         rm -f $file1
11227 }
11228 run_test 187b "Test data version change on volatile file"
11229
11230 test_200() {
11231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11232         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11233
11234         local POOL=${POOL:-cea1}
11235         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11236         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11237         # Pool OST targets
11238         local first_ost=0
11239         local last_ost=$(($OSTCOUNT - 1))
11240         local ost_step=2
11241         local ost_list=$(seq $first_ost $ost_step $last_ost)
11242         local ost_range="$first_ost $last_ost $ost_step"
11243         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11244         local file_dir=$POOL_ROOT/file_tst
11245         local subdir=$test_path/subdir
11246
11247         local rc=0
11248         while : ; do
11249                 # former test_200a test_200b
11250                 pool_add $POOL                          || { rc=$? ; break; }
11251                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11252                 # former test_200c test_200d
11253                 mkdir -p $test_path
11254                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11255                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11256                 mkdir -p $subdir
11257                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11258                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11259                                                         || { rc=$? ; break; }
11260                 # former test_200e test_200f
11261                 local files=$((OSTCOUNT*3))
11262                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11263                                                         || { rc=$? ; break; }
11264                 pool_create_files $POOL $file_dir $files "$ost_list" \
11265                                                         || { rc=$? ; break; }
11266                 # former test_200g test_200h
11267                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11268                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11269
11270                 # former test_201a test_201b test_201c
11271                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11272
11273                 local f=$test_path/$tfile
11274                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11275                 pool_remove $POOL $f                    || { rc=$? ; break; }
11276                 break
11277         done
11278
11279         cleanup_pools
11280         return $rc
11281 }
11282 run_test 200 "OST pools"
11283
11284 # usage: default_attr <count | size | offset>
11285 default_attr() {
11286         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11287 }
11288
11289 # usage: check_default_stripe_attr
11290 check_default_stripe_attr() {
11291         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11292         case $1 in
11293         --stripe-count|--count)
11294                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11295         --stripe-size|--size)
11296                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11297         --stripe-index|--index)
11298                 EXPECTED=-1;;
11299         *)
11300                 error "unknown getstripe attr '$1'"
11301         esac
11302
11303         [ $ACTUAL != $EXPECTED ] &&
11304                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11305 }
11306
11307 test_204a() {
11308         test_mkdir -p $DIR/$tdir
11309         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11310
11311         check_default_stripe_attr --stripe-count
11312         check_default_stripe_attr --stripe-size
11313         check_default_stripe_attr --stripe-index
11314
11315         return 0
11316 }
11317 run_test 204a "Print default stripe attributes ================="
11318
11319 test_204b() {
11320         test_mkdir -p $DIR/$tdir
11321         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11322
11323         check_default_stripe_attr --stripe-size
11324         check_default_stripe_attr --stripe-index
11325
11326         return 0
11327 }
11328 run_test 204b "Print default stripe size and offset  ==========="
11329
11330 test_204c() {
11331         test_mkdir -p $DIR/$tdir
11332         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11333
11334         check_default_stripe_attr --stripe-count
11335         check_default_stripe_attr --stripe-index
11336
11337         return 0
11338 }
11339 run_test 204c "Print default stripe count and offset ==========="
11340
11341 test_204d() {
11342         test_mkdir -p $DIR/$tdir
11343         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11344
11345         check_default_stripe_attr --stripe-count
11346         check_default_stripe_attr --stripe-size
11347
11348         return 0
11349 }
11350 run_test 204d "Print default stripe count and size ============="
11351
11352 test_204e() {
11353         test_mkdir -p $DIR/$tdir
11354         $SETSTRIPE -d $DIR/$tdir
11355
11356         check_default_stripe_attr --stripe-count --raw
11357         check_default_stripe_attr --stripe-size --raw
11358         check_default_stripe_attr --stripe-index --raw
11359
11360         return 0
11361 }
11362 run_test 204e "Print raw stripe attributes ================="
11363
11364 test_204f() {
11365         test_mkdir -p $DIR/$tdir
11366         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11367
11368         check_default_stripe_attr --stripe-size --raw
11369         check_default_stripe_attr --stripe-index --raw
11370
11371         return 0
11372 }
11373 run_test 204f "Print raw stripe size and offset  ==========="
11374
11375 test_204g() {
11376         test_mkdir -p $DIR/$tdir
11377         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11378
11379         check_default_stripe_attr --stripe-count --raw
11380         check_default_stripe_attr --stripe-index --raw
11381
11382         return 0
11383 }
11384 run_test 204g "Print raw stripe count and offset ==========="
11385
11386 test_204h() {
11387         test_mkdir -p $DIR/$tdir
11388         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11389
11390         check_default_stripe_attr --stripe-count --raw
11391         check_default_stripe_attr --stripe-size --raw
11392
11393         return 0
11394 }
11395 run_test 204h "Print raw stripe count and size ============="
11396
11397 # Figure out which job scheduler is being used, if any,
11398 # or use a fake one
11399 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11400         JOBENV=SLURM_JOB_ID
11401 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11402         JOBENV=LSB_JOBID
11403 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11404         JOBENV=PBS_JOBID
11405 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11406         JOBENV=LOADL_STEP_ID
11407 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11408         JOBENV=JOB_ID
11409 else
11410         JOBENV=FAKE_JOBID
11411 fi
11412
11413 verify_jobstats() {
11414         local cmd=$1
11415         local target=$2
11416
11417         # clear old jobstats
11418         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11419         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11420
11421         # use a new JobID for this test, or we might see an old one
11422         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11423
11424         JOBVAL=${!JOBENV}
11425         log "Test: $cmd"
11426         log "Using JobID environment variable $JOBENV=$JOBVAL"
11427
11428         if [ $JOBENV = "FAKE_JOBID" ]; then
11429                 FAKE_JOBID=$JOBVAL $cmd
11430         else
11431                 $cmd
11432         fi
11433
11434         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11435                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11436                 do_facet $FACET lctl get_param mdt.*.job_stats |
11437                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11438         fi
11439         if [ "$target" = "ost" -o "$target" = "both" ]; then
11440                 FACET=ost1
11441                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11442                         grep $JOBVAL || error "No job stats found on OST $FACET"
11443         fi
11444 }
11445
11446 jobstats_set() {
11447         trap 0
11448         NEW_JOBENV=${1:-$OLD_JOBENV}
11449         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11450         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11451 }
11452
11453 test_205() { # Job stats
11454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11455         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11456         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11457                 skip "Server doesn't support jobstats" && return 0
11458         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11459
11460         local cmd
11461         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11462         if [ $OLD_JOBENV != $JOBENV ]; then
11463                 jobstats_set $JOBENV
11464                 trap jobstats_set EXIT
11465         fi
11466
11467         local user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11468                      changelog_register -n)
11469         echo "Registered as changelog user $user"
11470
11471         # mkdir
11472         cmd="mkdir $DIR/$tfile"
11473         verify_jobstats "$cmd" "mdt"
11474         # rmdir
11475         cmd="rm -fr $DIR/$tfile"
11476         verify_jobstats "$cmd" "mdt"
11477         # mknod
11478         cmd="mknod $DIR/$tfile c 1 3"
11479         verify_jobstats "$cmd" "mdt"
11480         # unlink
11481         cmd="rm -f $DIR/$tfile"
11482         verify_jobstats "$cmd" "mdt"
11483         # open & close
11484         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11485         verify_jobstats "$cmd" "mdt"
11486         # setattr
11487         cmd="touch $DIR/$tfile"
11488         verify_jobstats "$cmd" "both"
11489         # write
11490         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11491         verify_jobstats "$cmd" "ost"
11492         # read
11493         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11494         verify_jobstats "$cmd" "ost"
11495         # truncate
11496         cmd="$TRUNCATE $DIR/$tfile 0"
11497         verify_jobstats "$cmd" "both"
11498         # rename
11499         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11500         verify_jobstats "$cmd" "mdt"
11501
11502         # Ensure that jobid are present in changelog (if supported by MDS)
11503         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11504         then
11505                 $LFS changelog $MDT0 | tail -9
11506                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11507                 [ $jobids -eq 9 ] ||
11508                         error "Wrong changelog jobid count $jobids != 9"
11509
11510                 # LU-5862
11511                 JOBENV="disable"
11512                 jobstats_set $JOBENV
11513                 touch $DIR/$tfile
11514                 $LFS changelog $MDT0 | tail -1
11515                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11516                 [ $jobids -eq 0 ] ||
11517                         error "Unexpected jobids when jobid_var=$JOBENV"
11518         fi
11519
11520         # cleanup
11521         rm -f $DIR/jobstats_test_rename
11522
11523         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11524 }
11525 run_test 205 "Verify job stats"
11526
11527 # LU-1480, LU-1773 and LU-1657
11528 test_206() {
11529         mkdir -p $DIR/$tdir
11530         $SETSTRIPE -c -1 $DIR/$tdir
11531 #define OBD_FAIL_LOV_INIT 0x1403
11532         $LCTL set_param fail_loc=0xa0001403
11533         $LCTL set_param fail_val=1
11534         touch $DIR/$tdir/$tfile || true
11535 }
11536 run_test 206 "fail lov_init_raid0() doesn't lbug"
11537
11538 test_207a() {
11539         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11540         local fsz=`stat -c %s $DIR/$tfile`
11541         cancel_lru_locks mdc
11542
11543         # do not return layout in getattr intent
11544 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11545         $LCTL set_param fail_loc=0x170
11546         local sz=`stat -c %s $DIR/$tfile`
11547
11548         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11549
11550         rm -rf $DIR/$tfile
11551 }
11552 run_test 207a "can refresh layout at glimpse"
11553
11554 test_207b() {
11555         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11556         local cksum=`md5sum $DIR/$tfile`
11557         local fsz=`stat -c %s $DIR/$tfile`
11558         cancel_lru_locks mdc
11559         cancel_lru_locks osc
11560
11561         # do not return layout in getattr intent
11562 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11563         $LCTL set_param fail_loc=0x171
11564
11565         # it will refresh layout after the file is opened but before read issues
11566         echo checksum is "$cksum"
11567         echo "$cksum" |md5sum -c --quiet || error "file differs"
11568
11569         rm -rf $DIR/$tfile
11570 }
11571 run_test 207b "can refresh layout at open"
11572
11573 test_208() {
11574         # FIXME: in this test suite, only RD lease is used. This is okay
11575         # for now as only exclusive open is supported. After generic lease
11576         # is done, this test suite should be revised. - Jinshan
11577
11578         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11579                 { skip "Need MDS version at least 2.4.52"; return 0; }
11580         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11581
11582         echo "==== test 1: verify get lease work"
11583         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11584
11585         echo "==== test 2: verify lease can be broken by upcoming open"
11586         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11587         local PID=$!
11588         sleep 1
11589
11590         $MULTIOP $DIR/$tfile oO_RDONLY:c
11591         kill -USR1 $PID && wait $PID || error "break lease error"
11592
11593         echo "==== test 3: verify lease can't be granted if an open already exists"
11594         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11595         local PID=$!
11596         sleep 1
11597
11598         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11599         kill -USR1 $PID && wait $PID || error "open file error"
11600
11601         echo "==== test 4: lease can sustain over recovery"
11602         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11603         PID=$!
11604         sleep 1
11605
11606         fail mds1
11607
11608         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11609
11610         echo "==== test 5: lease broken can't be regained by replay"
11611         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11612         PID=$!
11613         sleep 1
11614
11615         # open file to break lease and then recovery
11616         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11617         fail mds1
11618
11619         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11620
11621         rm -f $DIR/$tfile
11622 }
11623 run_test 208 "Exclusive open"
11624
11625 test_209() {
11626         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11627                 skip_env "must have disp_stripe" && return
11628
11629         touch $DIR/$tfile
11630         sync; sleep 5; sync;
11631
11632         echo 3 > /proc/sys/vm/drop_caches
11633         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11634
11635         # open/close 500 times
11636         for i in $(seq 500); do
11637                 cat $DIR/$tfile
11638         done
11639
11640         echo 3 > /proc/sys/vm/drop_caches
11641         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11642
11643         echo "before: $req_before, after: $req_after"
11644         [ $((req_after - req_before)) -ge 300 ] &&
11645                 error "open/close requests are not freed"
11646         return 0
11647 }
11648 run_test 209 "read-only open/close requests should be freed promptly"
11649
11650 test_212() {
11651         size=`date +%s`
11652         size=$((size % 8192 + 1))
11653         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11654         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11655         rm -f $DIR/f212 $DIR/f212.xyz
11656 }
11657 run_test 212 "Sendfile test ============================================"
11658
11659 test_213() {
11660         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11661         cancel_lru_locks osc
11662         lctl set_param fail_loc=0x8000040f
11663         # generate a read lock
11664         cat $DIR/$tfile > /dev/null
11665         # write to the file, it will try to cancel the above read lock.
11666         cat /etc/hosts >> $DIR/$tfile
11667 }
11668 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11669
11670 test_214() { # for bug 20133
11671         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11672         for (( i=0; i < 340; i++ )) ; do
11673                 touch $DIR/$tdir/d214c/a$i
11674         done
11675
11676         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11677         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11678         ls $DIR/d214c || error "ls $DIR/d214c failed"
11679         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11680         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11681 }
11682 run_test 214 "hash-indexed directory test - bug 20133"
11683
11684 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11685 create_lnet_proc_files() {
11686         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11687         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11688
11689         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11690         rm -f "$TMP/lnet_$1.sys_tmp"
11691 }
11692
11693 # counterpart of create_lnet_proc_files
11694 remove_lnet_proc_files() {
11695         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11696 }
11697
11698 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11699 # 3rd arg as regexp for body
11700 check_lnet_proc_stats() {
11701         local l=$(cat "$TMP/lnet_$1" |wc -l)
11702         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11703
11704         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11705 }
11706
11707 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11708 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11709 # optional and can be regexp for 2nd line (lnet.routes case)
11710 check_lnet_proc_entry() {
11711         local blp=2          # blp stands for 'position of 1st line of body'
11712         [ -z "$5" ] || blp=3 # lnet.routes case
11713
11714         local l=$(cat "$TMP/lnet_$1" |wc -l)
11715         # subtracting one from $blp because the body can be empty
11716         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11717
11718         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11719                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11720
11721         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11722                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11723
11724         # bail out if any unexpected line happened
11725         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11726         [ "$?" != 0 ] || error "$2 misformatted"
11727 }
11728
11729 test_215() { # for bugs 18102, 21079, 21517
11730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11731         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11732         local P='[1-9][0-9]*'           # positive numeric
11733         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11734         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11735         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11736         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11737
11738         local L1 # regexp for 1st line
11739         local L2 # regexp for 2nd line (optional)
11740         local BR # regexp for the rest (body)
11741
11742         # lnet.stats should look as 11 space-separated non-negative numerics
11743         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11744         create_lnet_proc_files "stats"
11745         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11746         remove_lnet_proc_files "stats"
11747
11748         # lnet.routes should look like this:
11749         # Routing disabled/enabled
11750         # net hops priority state router
11751         # where net is a string like tcp0, hops > 0, priority >= 0,
11752         # state is up/down,
11753         # router is a string like 192.168.1.1@tcp2
11754         L1="^Routing (disabled|enabled)$"
11755         L2="^net +hops +priority +state +router$"
11756         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11757         create_lnet_proc_files "routes"
11758         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11759         remove_lnet_proc_files "routes"
11760
11761         # lnet.routers should look like this:
11762         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11763         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11764         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11765         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11766         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11767         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11768         create_lnet_proc_files "routers"
11769         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11770         remove_lnet_proc_files "routers"
11771
11772         # lnet.peers should look like this:
11773         # nid refs state last max rtr min tx min queue
11774         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11775         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11776         # numeric (0 or >0 or <0), queue >= 0.
11777         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11778         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11779         create_lnet_proc_files "peers"
11780         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11781         remove_lnet_proc_files "peers"
11782
11783         # lnet.buffers  should look like this:
11784         # pages count credits min
11785         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11786         L1="^pages +count +credits +min$"
11787         BR="^ +$N +$N +$I +$I$"
11788         create_lnet_proc_files "buffers"
11789         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11790         remove_lnet_proc_files "buffers"
11791
11792         # lnet.nis should look like this:
11793         # nid status alive refs peer rtr max tx min
11794         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11795         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11796         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11797         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11798         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11799         create_lnet_proc_files "nis"
11800         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11801         remove_lnet_proc_files "nis"
11802
11803         # can we successfully write to lnet.stats?
11804         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
11805         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11806 }
11807 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
11808
11809 test_216() { # bug 20317
11810         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11811         remote_ost_nodsh && skip "remote OST with nodsh" && return
11812
11813         local node
11814         local facets=$(get_facets OST)
11815         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11816
11817         save_lustre_params client "osc.*.contention_seconds" > $p
11818         save_lustre_params $facets \
11819                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11820         save_lustre_params $facets \
11821                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11822         save_lustre_params $facets \
11823                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11824         clear_osc_stats
11825
11826         # agressive lockless i/o settings
11827         for node in $(osts_nodes); do
11828                 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'
11829         done
11830         lctl set_param -n osc.*.contention_seconds 60
11831
11832         $DIRECTIO write $DIR/$tfile 0 10 4096
11833         $CHECKSTAT -s 40960 $DIR/$tfile
11834
11835         # disable lockless i/o
11836         for node in $(osts_nodes); do
11837                 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'
11838         done
11839         lctl set_param -n osc.*.contention_seconds 0
11840         clear_osc_stats
11841
11842         dd if=/dev/zero of=$DIR/$tfile count=0
11843         $CHECKSTAT -s 0 $DIR/$tfile
11844
11845         restore_lustre_params <$p
11846         rm -f $p
11847         rm $DIR/$tfile
11848 }
11849 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11850
11851 test_217() { # bug 22430
11852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11853         local node
11854         local nid
11855
11856         for node in $(nodes_list); do
11857                 nid=$(host_nids_address $node $NETTYPE)
11858                 if [[ $nid = *-* ]] ; then
11859                         echo "lctl ping $nid@$NETTYPE"
11860                         lctl ping $nid@$NETTYPE
11861                 else
11862                         echo "skipping $node (no hyphen detected)"
11863                 fi
11864         done
11865 }
11866 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11867
11868 test_218() {
11869        # do directio so as not to populate the page cache
11870        log "creating a 10 Mb file"
11871        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11872        log "starting reads"
11873        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11874        log "truncating the file"
11875        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11876        log "killing dd"
11877        kill %+ || true # reads might have finished
11878        echo "wait until dd is finished"
11879        wait
11880        log "removing the temporary file"
11881        rm -rf $DIR/$tfile || error "tmp file removal failed"
11882 }
11883 run_test 218 "parallel read and truncate should not deadlock ======================="
11884
11885 test_219() {
11886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11887         # write one partial page
11888         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11889         # set no grant so vvp_io_commit_write will do sync write
11890         $LCTL set_param fail_loc=0x411
11891         # write a full page at the end of file
11892         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11893
11894         $LCTL set_param fail_loc=0
11895         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11896         $LCTL set_param fail_loc=0x411
11897         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11898
11899         # LU-4201
11900         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11901         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11902 }
11903 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11904
11905 test_220() { #LU-325
11906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11907         remote_ost_nodsh && skip "remote OST with nodsh" && return
11908         local OSTIDX=0
11909
11910         test_mkdir -p $DIR/$tdir
11911         local OST=$($LFS osts | grep ${OSTIDX}": " | \
11912                 awk '{print $2}' | sed -e 's/_UUID$//')
11913
11914         # on the mdt's osc
11915         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11916         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11917                         osc.$mdtosc_proc1.prealloc_last_id)
11918         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11919                         osc.$mdtosc_proc1.prealloc_next_id)
11920
11921         $LFS df -i
11922
11923         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11924         #define OBD_FAIL_OST_ENOINO              0x229
11925         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11926         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11927         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11928
11929         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11930
11931         MDSOBJS=$((last_id - next_id))
11932         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11933
11934         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11935         echo "OST still has $count kbytes free"
11936
11937         echo "create $MDSOBJS files @next_id..."
11938         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11939
11940         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11941                         osc.$mdtosc_proc1.prealloc_last_id)
11942         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11943                         osc.$mdtosc_proc1.prealloc_next_id)
11944
11945         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11946         $LFS df -i
11947
11948         echo "cleanup..."
11949
11950         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11951         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11952
11953         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11954         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11955         echo "unlink $MDSOBJS files @$next_id..."
11956         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11957 }
11958 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11959
11960 test_221() {
11961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11962         dd if=`which date` of=$MOUNT/date oflag=sync
11963         chmod +x $MOUNT/date
11964
11965         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11966         $LCTL set_param fail_loc=0x80001401
11967
11968         $MOUNT/date > /dev/null
11969         rm -f $MOUNT/date
11970 }
11971 run_test 221 "make sure fault and truncate race to not cause OOM"
11972
11973 test_222a () {
11974         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11975        rm -rf $DIR/$tdir
11976        test_mkdir -p $DIR/$tdir
11977        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11978        createmany -o $DIR/$tdir/$tfile 10
11979        cancel_lru_locks mdc
11980        cancel_lru_locks osc
11981        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11982        $LCTL set_param fail_loc=0x31a
11983        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11984        $LCTL set_param fail_loc=0
11985        rm -r $DIR/$tdir
11986 }
11987 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11988
11989 test_222b () {
11990         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11991        rm -rf $DIR/$tdir
11992        test_mkdir -p $DIR/$tdir
11993        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11994        createmany -o $DIR/$tdir/$tfile 10
11995        cancel_lru_locks mdc
11996        cancel_lru_locks osc
11997        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11998        $LCTL set_param fail_loc=0x31a
11999        rm -r $DIR/$tdir || "AGL for rmdir failed"
12000        $LCTL set_param fail_loc=0
12001 }
12002 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12003
12004 test_223 () {
12005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12006        rm -rf $DIR/$tdir
12007        test_mkdir -p $DIR/$tdir
12008        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12009        createmany -o $DIR/$tdir/$tfile 10
12010        cancel_lru_locks mdc
12011        cancel_lru_locks osc
12012        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12013        $LCTL set_param fail_loc=0x31b
12014        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12015        $LCTL set_param fail_loc=0
12016        rm -r $DIR/$tdir
12017 }
12018 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12019
12020 test_224a() { # LU-1039, MRP-303
12021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12022         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12023         $LCTL set_param fail_loc=0x508
12024         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12025         $LCTL set_param fail_loc=0
12026         df $DIR
12027 }
12028 run_test 224a "Don't panic on bulk IO failure"
12029
12030 test_224b() { # LU-1039, MRP-303
12031         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12032         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12033         cancel_lru_locks osc
12034         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12035         $LCTL set_param fail_loc=0x515
12036         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12037         $LCTL set_param fail_loc=0
12038         df $DIR
12039 }
12040 run_test 224b "Don't panic on bulk IO failure"
12041
12042 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12043 test_225a () {
12044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12045         if [ -z ${MDSSURVEY} ]; then
12046               skip_env "mds-survey not found" && return
12047         fi
12048
12049         [ $MDSCOUNT -ge 2 ] &&
12050                 skip "skipping now for more than one MDT" && return
12051
12052        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12053             { skip "Need MDS version at least 2.2.51"; return; }
12054
12055        local mds=$(facet_host $SINGLEMDS)
12056        local target=$(do_nodes $mds 'lctl dl' | \
12057                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12058
12059        local cmd1="file_count=1000 thrhi=4"
12060        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12061        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12062        local cmd="$cmd1 $cmd2 $cmd3"
12063
12064        rm -f ${TMP}/mds_survey*
12065        echo + $cmd
12066        eval $cmd || error "mds-survey with zero-stripe failed"
12067        cat ${TMP}/mds_survey*
12068        rm -f ${TMP}/mds_survey*
12069 }
12070 run_test 225a "Metadata survey sanity with zero-stripe"
12071
12072 test_225b () {
12073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12074
12075         if [ -z ${MDSSURVEY} ]; then
12076               skip_env "mds-survey not found" && return
12077         fi
12078         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12079             { skip "Need MDS version at least 2.2.51"; return; }
12080
12081         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12082               skip_env "Need to mount OST to test" && return
12083         fi
12084
12085         [ $MDSCOUNT -ge 2 ] &&
12086                 skip "skipping now for more than one MDT" && return
12087        local mds=$(facet_host $SINGLEMDS)
12088        local target=$(do_nodes $mds 'lctl dl' | \
12089                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12090
12091        local cmd1="file_count=1000 thrhi=4"
12092        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12093        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12094        local cmd="$cmd1 $cmd2 $cmd3"
12095
12096        rm -f ${TMP}/mds_survey*
12097        echo + $cmd
12098        eval $cmd || error "mds-survey with stripe_count failed"
12099        cat ${TMP}/mds_survey*
12100        rm -f ${TMP}/mds_survey*
12101 }
12102 run_test 225b "Metadata survey sanity with stripe_count = 1"
12103
12104 mcreate_path2fid () {
12105         local mode=$1
12106         local major=$2
12107         local minor=$3
12108         local name=$4
12109         local desc=$5
12110         local path=$DIR/$tdir/$name
12111         local fid
12112         local rc
12113         local fid_path
12114
12115         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12116                 error "cannot create $desc"
12117
12118         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12119         rc=$?
12120         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12121
12122         fid_path=$($LFS fid2path $MOUNT $fid)
12123         rc=$?
12124         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12125
12126         [ "$path" == "$fid_path" ] ||
12127                 error "fid2path returned $fid_path, expected $path"
12128
12129         echo "pass with $path and $fid"
12130 }
12131
12132 test_226a () {
12133         rm -rf $DIR/$tdir
12134         mkdir -p $DIR/$tdir
12135
12136         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12137         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12138         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12139         mcreate_path2fid 0040666 0 0 dir "directory"
12140         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12141         mcreate_path2fid 0100666 0 0 file "regular file"
12142         mcreate_path2fid 0120666 0 0 link "symbolic link"
12143         mcreate_path2fid 0140666 0 0 sock "socket"
12144 }
12145 run_test 226a "call path2fid and fid2path on files of all type"
12146
12147 test_226b () {
12148         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12149         rm -rf $DIR/$tdir
12150         local MDTIDX=1
12151
12152         mkdir -p $DIR/$tdir
12153         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12154                 error "create remote directory failed"
12155         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12156         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12157                                 "character special file (null)"
12158         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12159                                 "character special file (no device)"
12160         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12161         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12162                                 "block special file (loop)"
12163         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12164         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12165         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12166 }
12167 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12168
12169 # LU-1299 Executing or running ldd on a truncated executable does not
12170 # cause an out-of-memory condition.
12171 test_227() {
12172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12173         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12174         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12175         chmod +x $MOUNT/date
12176
12177         $MOUNT/date > /dev/null
12178         ldd $MOUNT/date > /dev/null
12179         rm -f $MOUNT/date
12180 }
12181 run_test 227 "running truncated executable does not cause OOM"
12182
12183 # LU-1512 try to reuse idle OI blocks
12184 test_228a() {
12185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12186         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12187         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12188                 skip "non-ldiskfs backend" && return
12189
12190         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12191         local myDIR=$DIR/$tdir
12192
12193         mkdir -p $myDIR
12194         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12195         $LCTL set_param fail_loc=0x80001002
12196         createmany -o $myDIR/t- 10000
12197         $LCTL set_param fail_loc=0
12198         # The guard is current the largest FID holder
12199         touch $myDIR/guard
12200         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12201                     tr -d '[')
12202         local IDX=$(($SEQ % 64))
12203
12204         do_facet $SINGLEMDS sync
12205         # Make sure journal flushed.
12206         sleep 6
12207         local blk1=$(do_facet $SINGLEMDS \
12208                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12209                      grep Blockcount | awk '{print $4}')
12210
12211         # Remove old files, some OI blocks will become idle.
12212         unlinkmany $myDIR/t- 10000
12213         # Create new files, idle OI blocks should be reused.
12214         createmany -o $myDIR/t- 2000
12215         do_facet $SINGLEMDS sync
12216         # Make sure journal flushed.
12217         sleep 6
12218         local blk2=$(do_facet $SINGLEMDS \
12219                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12220                      grep Blockcount | awk '{print $4}')
12221
12222         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12223 }
12224 run_test 228a "try to reuse idle OI blocks"
12225
12226 test_228b() {
12227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12228         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12229         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12230                 skip "non-ldiskfs backend" && return
12231
12232         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12233         local myDIR=$DIR/$tdir
12234
12235         mkdir -p $myDIR
12236         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12237         $LCTL set_param fail_loc=0x80001002
12238         createmany -o $myDIR/t- 10000
12239         $LCTL set_param fail_loc=0
12240         # The guard is current the largest FID holder
12241         touch $myDIR/guard
12242         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12243                     tr -d '[')
12244         local IDX=$(($SEQ % 64))
12245
12246         do_facet $SINGLEMDS sync
12247         # Make sure journal flushed.
12248         sleep 6
12249         local blk1=$(do_facet $SINGLEMDS \
12250                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12251                      grep Blockcount | awk '{print $4}')
12252
12253         # Remove old files, some OI blocks will become idle.
12254         unlinkmany $myDIR/t- 10000
12255
12256         # stop the MDT
12257         stop $SINGLEMDS || error "Fail to stop MDT."
12258         # remount the MDT
12259         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12260
12261         df $MOUNT || error "Fail to df."
12262         # Create new files, idle OI blocks should be reused.
12263         createmany -o $myDIR/t- 2000
12264         do_facet $SINGLEMDS sync
12265         # Make sure journal flushed.
12266         sleep 6
12267         local blk2=$(do_facet $SINGLEMDS \
12268                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12269                      grep Blockcount | awk '{print $4}')
12270
12271         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12272 }
12273 run_test 228b "idle OI blocks can be reused after MDT restart"
12274
12275 #LU-1881
12276 test_228c() {
12277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12278         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12279         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12280                 skip "non-ldiskfs backend" && return
12281
12282         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12283         local myDIR=$DIR/$tdir
12284
12285         mkdir -p $myDIR
12286         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12287         $LCTL set_param fail_loc=0x80001002
12288         # 20000 files can guarantee there are index nodes in the OI file
12289         createmany -o $myDIR/t- 20000
12290         $LCTL set_param fail_loc=0
12291         # The guard is current the largest FID holder
12292         touch $myDIR/guard
12293         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12294                     tr -d '[')
12295         local IDX=$(($SEQ % 64))
12296
12297         do_facet $SINGLEMDS sync
12298         # Make sure journal flushed.
12299         sleep 6
12300         local blk1=$(do_facet $SINGLEMDS \
12301                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12302                      grep Blockcount | awk '{print $4}')
12303
12304         # Remove old files, some OI blocks will become idle.
12305         unlinkmany $myDIR/t- 20000
12306         rm -f $myDIR/guard
12307         # The OI file should become empty now
12308
12309         # Create new files, idle OI blocks should be reused.
12310         createmany -o $myDIR/t- 2000
12311         do_facet $SINGLEMDS sync
12312         # Make sure journal flushed.
12313         sleep 6
12314         local blk2=$(do_facet $SINGLEMDS \
12315                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12316                      grep Blockcount | awk '{print $4}')
12317
12318         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12319 }
12320 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12321
12322 test_229() { # LU-2482, LU-3448
12323         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12324                 skip "No HSM support on MDS of $(get_lustre_version)," \
12325                          "need 2.4.53 at least" && return
12326         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12327         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12328
12329         rm -f $DIR/$tfile
12330
12331         # Create a file with a released layout and stripe count 2.
12332         $MULTIOP $DIR/$tfile H2c ||
12333                 error "failed to create file with released layout"
12334
12335         $GETSTRIPE -v $DIR/$tfile
12336
12337         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12338         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12339
12340         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12341         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12342         stat $DIR/$tfile || error "failed to stat released file"
12343
12344         chown $RUNAS_ID $DIR/$tfile ||
12345                 error "chown $RUNAS_ID $DIR/$tfile failed"
12346
12347         chgrp $RUNAS_ID $DIR/$tfile ||
12348                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12349
12350         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12351         rm $DIR/$tfile || error "failed to remove released file"
12352 }
12353 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12354
12355 test_230a() {
12356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12357         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12358         local MDTIDX=1
12359
12360         test_mkdir $DIR/$tdir
12361         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
12362         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12363         [ $mdt_idx -ne 0 ] &&
12364                 error "create local directory on wrong MDT $mdt_idx"
12365
12366         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12367                         error "create remote directory failed"
12368         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12369         [ $mdt_idx -ne $MDTIDX ] &&
12370                 error "create remote directory on wrong MDT $mdt_idx"
12371
12372         createmany -o $DIR/$tdir/test_230/t- 10 ||
12373                 error "create files on remote directory failed"
12374         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12375         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12376         rm -r $DIR/$tdir || error "unlink remote directory failed"
12377 }
12378 run_test 230a "Create remote directory and files under the remote directory"
12379
12380 test_230b() {
12381         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12382         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12383         local MDTIDX=1
12384         local mdt_index
12385         local i
12386         local file
12387         local pid
12388         local stripe_count
12389         local migrate_dir=$DIR/$tdir/migrate_dir
12390         local other_dir=$DIR/$tdir/other_dir
12391
12392         test_mkdir $DIR/$tdir
12393         test_mkdir -i0 -c1 $migrate_dir
12394         test_mkdir -i0 -c1 $other_dir
12395         for ((i=0; i<10; i++)); do
12396                 mkdir -p $migrate_dir/dir_${i}
12397                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12398                         error "create files under remote dir failed $i"
12399         done
12400
12401         cp /etc/passwd $migrate_dir/$tfile
12402         cp /etc/passwd $other_dir/$tfile
12403         chattr +SAD $migrate_dir
12404         chattr +SAD $migrate_dir/$tfile
12405
12406         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12407         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12408         local old_dir_mode=$(stat -c%f $migrate_dir)
12409         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12410
12411         mkdir -p $migrate_dir/dir_default_stripe2
12412         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12413         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12414
12415         mkdir -p $other_dir
12416         ln $migrate_dir/$tfile $other_dir/luna
12417         ln $migrate_dir/$tfile $migrate_dir/sofia
12418         ln $other_dir/$tfile $migrate_dir/david
12419         ln -s $migrate_dir/$tfile $other_dir/zachary
12420         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12421         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12422
12423         $LFS mv -v -M $MDTIDX $migrate_dir ||
12424                 error "migrate remote dir error"
12425
12426         echo "migratate to MDT1, then checking.."
12427         for ((i = 0; i < 10; i++)); do
12428                 for file in $(find $migrate_dir/dir_${i}); do
12429                         mdt_index=$($LFS getstripe -M $file)
12430                         [ $mdt_index == $MDTIDX ] ||
12431                                 error "$file is not on MDT${MDTIDX}"
12432                 done
12433         done
12434
12435         # the multiple link file should still in MDT0
12436         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12437         [ $mdt_index == 0 ] ||
12438                 error "$file is not on MDT${MDTIDX}"
12439
12440         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12441         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12442                 error " expect $old_dir_flag get $new_dir_flag"
12443
12444         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12445         [ "$old_file_flag" = "$new_file_flag" ] ||
12446                 error " expect $old_file_flag get $new_file_flag"
12447
12448         local new_dir_mode=$(stat -c%f $migrate_dir)
12449         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12450                 error "expect mode $old_dir_mode get $new_dir_mode"
12451
12452         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12453         [ "$old_file_mode" = "$new_file_mode" ] ||
12454                 error "expect mode $old_file_mode get $new_file_mode"
12455
12456         diff /etc/passwd $migrate_dir/$tfile ||
12457                 error "$tfile different after migration"
12458
12459         diff /etc/passwd $other_dir/luna ||
12460                 error "luna different after migration"
12461
12462         diff /etc/passwd $migrate_dir/sofia ||
12463                 error "sofia different after migration"
12464
12465         diff /etc/passwd $migrate_dir/david ||
12466                 error "david different after migration"
12467
12468         diff /etc/passwd $other_dir/zachary ||
12469                 error "zachary different after migration"
12470
12471         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12472                 error "${tfile}_ln different after migration"
12473
12474         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12475                 error "${tfile}_ln_other different after migration"
12476
12477         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12478         [ $stripe_count = 2 ] ||
12479                 error "dir strpe_count $d != 2 after migration."
12480
12481         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12482         [ $stripe_count = 2 ] ||
12483                 error "file strpe_count $d != 2 after migration."
12484
12485         #migrate back to MDT0
12486         MDTIDX=0
12487         $LFS mv -v -M $MDTIDX $migrate_dir ||
12488                 error "migrate remote dir error"
12489
12490         echo "migrate back to MDT0, checking.."
12491         for file in $(find $migrate_dir); do
12492                 mdt_index=$($LFS getstripe -M $file)
12493                 [ $mdt_index == $MDTIDX ] ||
12494                         error "$file is not on MDT${MDTIDX}"
12495         done
12496
12497         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12498         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12499                 error " expect $old_dir_flag get $new_dir_flag"
12500
12501         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12502         [ "$old_file_flag" = "$new_file_flag" ] ||
12503                 error " expect $old_file_flag get $new_file_flag"
12504
12505         local new_dir_mode=$(stat -c%f $migrate_dir)
12506         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12507                 error "expect mode $old_dir_mode get $new_dir_mode"
12508
12509         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12510         [ "$old_file_mode" = "$new_file_mode" ] ||
12511                 error "expect mode $old_file_mode get $new_file_mode"
12512
12513         diff /etc/passwd ${migrate_dir}/$tfile ||
12514                 error "$tfile different after migration"
12515
12516         diff /etc/passwd ${other_dir}/luna ||
12517                 error "luna different after migration"
12518
12519         diff /etc/passwd ${migrate_dir}/sofia ||
12520                 error "sofia different after migration"
12521
12522         diff /etc/passwd ${other_dir}/zachary ||
12523                 error "zachary different after migration"
12524
12525         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12526                 error "${tfile}_ln different after migration"
12527
12528         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12529                 error "${tfile}_ln_other different after migration"
12530
12531         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12532         [ $stripe_count = 2 ] ||
12533                 error "dir strpe_count $d != 2 after migration."
12534
12535         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12536         [ $stripe_count = 2 ] ||
12537                 error "file strpe_count $d != 2 after migration."
12538
12539         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12540 }
12541 run_test 230b "migrate directory"
12542
12543 test_230c() {
12544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12545         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12546         local MDTIDX=1
12547         local mdt_index
12548         local file
12549         local migrate_dir=$DIR/$tdir/migrate_dir
12550
12551         #If migrating directory fails in the middle, all entries of
12552         #the directory is still accessiable.
12553         test_mkdir $DIR/$tdir
12554         test_mkdir -i0 -c1 $migrate_dir
12555         stat $migrate_dir
12556         createmany -o $migrate_dir/f 10 ||
12557                 error "create files under ${migrate_dir} failed"
12558
12559         #failed after migrating 5 entries
12560         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12561         do_facet mds1 lctl set_param fail_loc=0x20001801
12562         do_facet mds1 lctl  set_param fail_val=5
12563         local t=$(ls $migrate_dir | wc -l)
12564         $LFS mv --mdt-index $MDTIDX $migrate_dir &&
12565                 error "migrate should fail after 5 entries"
12566         local u=$(ls $migrate_dir | wc -l)
12567         [ "$u" == "$t" ] || error "$u != $t during migration"
12568
12569         for file in $(find $migrate_dir); do
12570                 stat $file || error "stat $file failed"
12571         done
12572
12573         do_facet mds1 lctl set_param fail_loc=0
12574         do_facet mds1 lctl set_param fail_val=0
12575
12576         $LFS mv -M $MDTIDX $migrate_dir ||
12577                 error "migrate open files should failed with open files"
12578
12579         echo "Finish migration, then checking.."
12580         for file in $(find $migrate_dir); do
12581                 mdt_index=$($LFS getstripe -M $file)
12582                 [ $mdt_index == $MDTIDX ] ||
12583                         error "$file is not on MDT${MDTIDX}"
12584         done
12585
12586         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12587 }
12588 run_test 230c "check directory accessiblity if migration is failed"
12589
12590 test_230d() {
12591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12592         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12593         local MDTIDX=1
12594         local mdt_index
12595         local migrate_dir=$DIR/$tdir/migrate_dir
12596         local i
12597         local j
12598
12599         test_mkdir $DIR/$tdir
12600         test_mkdir -i0 -c1 $migrate_dir
12601
12602         for ((i=0; i<100; i++)); do
12603                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
12604                 createmany -o $migrate_dir/dir_${i}/f 100 ||
12605                         error "create files under remote dir failed $i"
12606         done
12607
12608         $LFS mv -M $MDTIDX -v $migrate_dir || error "migrate remote dir error"
12609
12610         echo "Finish migration, then checking.."
12611         for file in $(find $migrate_dir); do
12612                 mdt_index=$($LFS getstripe -M $file)
12613                 [ $mdt_index == $MDTIDX ] ||
12614                         error "$file is not on MDT${MDTIDX}"
12615         done
12616
12617         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12618 }
12619 run_test 230d "check migrate big directory"
12620
12621 test_231a()
12622 {
12623         # For simplicity this test assumes that max_pages_per_rpc
12624         # is the same across all OSCs
12625         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12626         local bulk_size=$((max_pages * 4096))
12627
12628         mkdir -p $DIR/$tdir
12629
12630         # clear the OSC stats
12631         $LCTL set_param osc.*.stats=0 &>/dev/null
12632
12633         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12634         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12635                 oflag=direct &>/dev/null || error "dd failed"
12636
12637         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12638         if [ x$nrpcs != "x1" ]; then
12639                 error "found $nrpc ost_write RPCs, not 1 as expected"
12640         fi
12641
12642         # Drop the OSC cache, otherwise we will read from it
12643         cancel_lru_locks osc
12644
12645         # clear the OSC stats
12646         $LCTL set_param osc.*.stats=0 &>/dev/null
12647
12648         # Client reads $bulk_size.
12649         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12650                 iflag=direct &>/dev/null || error "dd failed"
12651
12652         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12653         if [ x$nrpcs != "x1" ]; then
12654                 error "found $nrpc ost_read RPCs, not 1 as expected"
12655         fi
12656 }
12657 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12658
12659 test_231b() {
12660         mkdir -p $DIR/$tdir
12661         local i
12662         for i in {0..1023}; do
12663                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12664                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12665                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12666         done
12667         sync
12668 }
12669 run_test 231b "must not assert on fully utilized OST request buffer"
12670
12671 test_232() {
12672         mkdir -p $DIR/$tdir
12673         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12674         $LCTL set_param fail_loc=0x31c
12675
12676         # ignore dd failure
12677         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12678
12679         $LCTL set_param fail_loc=0
12680         umount_client $MOUNT || error "umount failed"
12681         mount_client $MOUNT || error "mount failed"
12682 }
12683 run_test 232 "failed lock should not block umount"
12684
12685 test_233a() {
12686         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12687         { skip "Need MDS version at least 2.3.64"; return; }
12688
12689         local fid=$($LFS path2fid $MOUNT)
12690         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12691                 error "cannot access $MOUNT using its FID '$fid'"
12692 }
12693 run_test 233a "checking that OBF of the FS root succeeds"
12694
12695 test_233b() {
12696         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12697         { skip "Need MDS version at least 2.5.90"; return; }
12698
12699         local fid=$($LFS path2fid $MOUNT/.lustre)
12700         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12701                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12702
12703         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12704         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12705                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12706 }
12707 run_test 233b "checking that OBF of the FS .lustre succeeds"
12708
12709 test_234() {
12710         local p="$TMP/sanityN-$TESTNAME.parameters"
12711         save_lustre_params client "llite.*.xattr_cache" > $p
12712         lctl set_param llite.*.xattr_cache 1 ||
12713                 { skip "xattr cache is not supported"; return 0; }
12714
12715         mkdir -p $DIR/$tdir || error "mkdir failed"
12716         touch $DIR/$tdir/$tfile || error "touch failed"
12717         # OBD_FAIL_LLITE_XATTR_ENOMEM
12718         $LCTL set_param fail_loc=0x1405
12719         # output of the form: attr 2 4 44 3 fc13 x86_64
12720         V=($(IFS=".-" rpm -q attr))
12721         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
12722               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
12723                 # attr pre-2.4.44-7 had a bug with rc
12724                 # LU-3703 - SLES 11 and FC13 clients have older attr
12725                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12726                         error "getfattr should have failed with ENOMEM"
12727         else
12728                 skip "LU-3703: attr version $(getfattr --version) too old"
12729         fi
12730         $LCTL set_param fail_loc=0x0
12731         rm -rf $DIR/$tdir
12732
12733         restore_lustre_params < $p
12734         rm -f $p
12735 }
12736 run_test 234 "xattr cache should not crash on ENOMEM"
12737
12738 test_235() {
12739         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12740                 skip "Need MDS version at least 2.4.52" && return
12741         flock_deadlock $DIR/$tfile
12742         local RC=$?
12743         case $RC in
12744                 0)
12745                 ;;
12746                 124) error "process hangs on a deadlock"
12747                 ;;
12748                 *) error "error executing flock_deadlock $DIR/$tfile"
12749                 ;;
12750         esac
12751 }
12752 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12753
12754 #LU-2935
12755 test_236() {
12756         check_swap_layouts_support && return 0
12757         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12758
12759         local ref1=/etc/passwd
12760         local ref2=/etc/group
12761         local file1=$DIR/$tdir/f1
12762         local file2=$DIR/$tdir/f2
12763
12764         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12765         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12766         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12767         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12768         local fd=$(free_fd)
12769         local cmd="exec $fd<>$file2"
12770         eval $cmd
12771         rm $file2
12772         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12773                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12774         cmd="exec $fd>&-"
12775         eval $cmd
12776         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12777
12778         #cleanup
12779         rm -rf $DIR/$tdir
12780 }
12781 run_test 236 "Layout swap on open unlinked file"
12782
12783 # test to verify file handle related system calls
12784 # (name_to_handle_at/open_by_handle_at)
12785 # The new system calls are supported in glibc >= 2.14.
12786
12787 test_237() {
12788         echo "Test file_handle syscalls" > $DIR/$tfile ||
12789                 error "write failed"
12790         check_fhandle_syscalls $DIR/$tfile ||
12791                 error "check_fhandle_syscalls failed"
12792 }
12793 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12794
12795 # LU-4659 linkea consistency
12796 test_238() {
12797         local server_version=$(lustre_version_code $SINGLEMDS)
12798
12799         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12800                 [[ $server_version -gt $(version_code 2.5.1) &&
12801                    $server_version -lt $(version_code 2.5.50) ]] ||
12802                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12803
12804         touch $DIR/$tfile
12805         ln $DIR/$tfile $DIR/$tfile.lnk
12806         touch $DIR/$tfile.new
12807         mv $DIR/$tfile.new $DIR/$tfile
12808         local fid1=$($LFS path2fid $DIR/$tfile)
12809         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
12810         local path1=$($LFS fid2path $FSNAME "$fid1")
12811         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12812         local path2=$($LFS fid2path $FSNAME "$fid2")
12813         [ $tfile.lnk == $path2 ] ||
12814                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12815         rm -f $DIR/$tfile*
12816 }
12817 run_test 238 "Verify linkea consistency"
12818
12819 test_239() {
12820         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
12821                 skip "Need MDS version at least 2.5.60" && return
12822         local list=$(comma_list $(mdts_nodes))
12823
12824         mkdir -p $DIR/$tdir
12825         createmany -o $DIR/$tdir/f- 5000
12826         unlinkmany $DIR/$tdir/f- 5000
12827         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12828         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12829                         osc.*MDT*.sync_in_flight" | calc_sum)
12830         [ "$changes" -eq 0 ] || error "$changes not synced"
12831 }
12832 run_test 239 "osp_sync test"
12833
12834 test_240() {
12835         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12836
12837         mkdir -p $DIR/$tdir
12838
12839         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12840                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12841         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12842                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12843
12844         umount_client $MOUNT || error "umount failed"
12845         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12846         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12847         mount_client $MOUNT || error "failed to mount client"
12848
12849         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12850         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12851 }
12852 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12853
12854 test_241_bio() {
12855         for LOOP in $(seq $1); do
12856                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
12857                 cancel_lru_locks osc
12858         done
12859 }
12860
12861 test_241_dio() {
12862         for LOOP in $(seq $1); do
12863                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
12864                                                 iflag=direct 2>/dev/null
12865         done
12866 }
12867
12868 test_241() {
12869         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
12870         ls -la $DIR/$tfile
12871         cancel_lru_locks osc
12872         test_241_bio 1000 &
12873         PID=$!
12874         test_241_dio 1000
12875         wait $PID
12876 }
12877 run_test 241 "bio vs dio"
12878
12879 test_242() {
12880         mkdir -p $DIR/$tdir
12881         touch $DIR/$tdir/$tfile
12882
12883         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
12884         do_facet mds1 lctl set_param fail_loc=0x105
12885         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
12886
12887         do_facet mds1 lctl set_param fail_loc=0
12888         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
12889 }
12890 run_test 242 "mdt_readpage failure should not cause directory unreadable"
12891
12892 test_243()
12893 {
12894         test_mkdir -p $DIR/$tdir
12895         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
12896 }
12897 run_test 243 "various group lock tests"
12898
12899 test_250() {
12900         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
12901          && skip "no 16TB file size limit on ZFS" && return
12902
12903         $SETSTRIPE -c 1 $DIR/$tfile
12904         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
12905         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
12906         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
12907         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
12908                 conv=notrunc,fsync && error "append succeeded"
12909         return 0
12910 }
12911 run_test 250 "Write above 16T limit"
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_300_check_default_striped_dir()
13159 {
13160         local dirname=$1
13161         local default_count=$2
13162         local default_index=$3
13163         local stripe_count
13164         local stripe_index
13165         local dir_stripe_index
13166         local dir
13167
13168         echo "checking $dirname $default_count $default_index"
13169         $LFS setdirstripe -D -c $default_count -i $default_index \
13170                                 -t all_char $DIR/$tdir/$dirname ||
13171                 error "set default stripe on striped dir error"
13172         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13173         [ $stripe_count -eq $default_count ] ||
13174                 error "expect $default_count get $stripe_count for $dirname"
13175
13176         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13177         [ $stripe_index -eq $default_index ] ||
13178                 error "expect $default_index get $stripe_index for $dirname"
13179
13180         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13181                                                 error "create dirs failed"
13182         for dir in $(find $DIR/$tdir/$dirname/*); do
13183                 stripe_count=$($LFS getdirstripe -c $dir)
13184                 [ $stripe_count -eq $default_count ] ||
13185                 error "stripe count $default_count != $stripe_count for $dir"
13186
13187                 stripe_index=$($LFS getdirstripe -i $dir)
13188                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13189                         error "$stripe_index != $default_index for $dir"
13190
13191                 #check default stripe
13192                 stripe_count=$($LFS getdirstripe -D -c $dir)
13193                 [ $stripe_count -eq $default_count ] ||
13194                 error "default count $default_count != $stripe_count for $dir"
13195
13196                 stripe_index=$($LFS getdirstripe -D -i $dir)
13197                 [ $stripe_index -eq $default_index ] ||
13198                 error "default index $default_index != $stripe_index for $dir"
13199         done
13200         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13201 }
13202
13203 test_300g() {
13204         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13205         local dir
13206         local stripe_count
13207         local stripe_index
13208
13209         mkdir $DIR/$tdir
13210         mkdir $DIR/$tdir/normal_dir
13211
13212         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13213         test_300_check_default_striped_dir normal_dir 1 0
13214         test_300_check_default_striped_dir normal_dir 2 1
13215         test_300_check_default_striped_dir normal_dir 2 -1
13216
13217         #delete default stripe information
13218         echo "delete default stripeEA"
13219         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13220                 error "set default stripe on striped dir error"
13221
13222         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13223         for dir in $(find $DIR/$tdir/normal_dir/*); do
13224                 stripe_count=$($LFS getdirstripe -c $dir)
13225                 [ $stripe_count -eq 0 ] ||
13226                         error "expect 1 get $stripe_count for $dir"
13227                 stripe_index=$($LFS getdirstripe -i $dir)
13228                 [ $stripe_index -eq 0 ] ||
13229                         error "expect 0 get $stripe_index for $dir"
13230         done
13231 }
13232 run_test 300g "check default striped directory for normal directory"
13233
13234 test_300h() {
13235         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13236         local dir
13237         local stripe_count
13238
13239         mkdir $DIR/$tdir
13240         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13241                                         $DIR/$tdir/striped_dir ||
13242                 error "set striped dir error"
13243
13244         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13245         test_300_check_default_striped_dir striped_dir 1 0
13246         test_300_check_default_striped_dir striped_dir 2 1
13247         test_300_check_default_striped_dir striped_dir 2 -1
13248
13249         #delete default stripe information
13250         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13251                 error "set default stripe on striped dir error"
13252
13253         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13254         for dir in $(find $DIR/$tdir/striped_dir/*); do
13255                 stripe_count=$($LFS getdirstripe -c $dir)
13256                 [ $stripe_count -eq 0 ] ||
13257                         error "expect 1 get $stripe_count for $dir"
13258         done
13259 }
13260 run_test 300h "check default striped directory for striped directory"
13261
13262 test_300i() {
13263         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13264         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13265         local stripe_count
13266         local file
13267
13268         mkdir $DIR/$tdir
13269
13270         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13271                 error "set striped dir error"
13272
13273         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13274                 error "create files under striped dir failed"
13275
13276         # unfortunately, we need to umount to clear dir layout cache for now
13277         # once we fully implement dir layout, we can drop this
13278         umount_client $MOUNT || error "umount failed"
13279         mount_client $MOUNT || error "mount failed"
13280
13281         #set the stripe to be unknown hash type
13282         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13283         $LCTL set_param fail_loc=0x1901
13284         for ((i = 0; i < 10; i++)); do
13285                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13286                         error "stat f-$i failed"
13287                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13288         done
13289
13290         touch $DIR/$tdir/striped_dir/f0 &&
13291                 error "create under striped dir with unknown hash should fail"
13292
13293         $LCTL set_param fail_loc=0
13294
13295         umount_client $MOUNT || error "umount failed"
13296         mount_client $MOUNT || error "mount failed"
13297
13298         return 0
13299 }
13300 run_test 300i "client handle unknown hash type striped directory"
13301
13302 test_400a() { # LU-1606, was conf-sanity test_74
13303         local extra_flags=''
13304         local out=$TMP/$tfile
13305         local prefix=/usr/include/lustre
13306         local prog
13307
13308         if ! which $CC > /dev/null 2>&1; then
13309                 skip_env "$CC is not installed"
13310                 return 0
13311         fi
13312
13313         if ! [[ -d $prefix ]]; then
13314                 # Assume we're running in tree and fixup the include path.
13315                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13316                 extra_flags+=" -I$LUSTRE/include"
13317                 extra_flags+=" -L$LUSTRE/utils"
13318         fi
13319
13320         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13321                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13322                         error "client api broken"
13323         done
13324 }
13325 run_test 400a "Lustre client api program can compile and link"
13326
13327 test_400b() { # LU-1606, LU-5011
13328         local header
13329         local out=$TMP/$tfile
13330         local prefix=/usr/include/lustre
13331
13332         # We use a hard coded prefix so that this test will not fail
13333         # when run in tree. There are headers in lustre/include/lustre/
13334         # that are not packaged (like lustre_idl.h) and have more
13335         # complicated include dependencies (like config.h and lnet/types.h).
13336         # Since this test about correct packaging we just skip them when
13337         # they don't exist (see below) rather than try to fixup cppflags.
13338
13339         if ! which $CC > /dev/null 2>&1; then
13340                 skip_env "$CC is not installed"
13341                 return 0
13342         fi
13343
13344         for header in $prefix/*.h; do
13345                 if ! [[ -f "$header" ]]; then
13346                         continue
13347                 fi
13348
13349                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13350                         continue # liblustreapi.h is deprecated.
13351                 fi
13352
13353                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13354                         error "cannot compile '$header'"
13355         done
13356 }
13357 run_test 400b "packaged headers can be compiled"
13358
13359 #
13360 # tests that do cleanup/setup should be run at the end
13361 #
13362
13363 test_900() {
13364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13365         local ls
13366         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13367         $LCTL set_param fail_loc=0x903
13368
13369         cancel_lru_locks MGC
13370
13371         FAIL_ON_ERROR=true cleanup
13372         FAIL_ON_ERROR=true setup
13373 }
13374 run_test 900 "umount should not race with any mgc requeue thread"
13375
13376 complete $SECONDS
13377 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13378 check_and_cleanup_lustre
13379 if [ "$I_MOUNTED" != "yes" ]; then
13380         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13381 fi
13382 exit_status