Whamcloud - gitweb
26eeb26b40dc196a0d75001365845412d650c5b3
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19 SRCDIR=$(cd $(dirname $0); echo $PWD)
20 export PATH=$PATH:/sbin
21
22 TMP=${TMP:-/tmp}
23
24 CC=${CC:-cc}
25 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
26 CREATETEST=${CREATETEST:-createtest}
27 LFS=${LFS:-lfs}
28 LFIND=${LFIND:-"$LFS find"}
29 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
30 LCTL=${LCTL:-lctl}
31 OPENFILE=${OPENFILE:-openfile}
32 OPENUNLINK=${OPENUNLINK:-openunlink}
33 export MULTIOP=${MULTIOP:-multiop}
34 READS=${READS:-"reads"}
35 MUNLINK=${MUNLINK:-munlink}
36 SOCKETSERVER=${SOCKETSERVER:-socketserver}
37 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
38 MEMHOG=${MEMHOG:-memhog}
39 DIRECTIO=${DIRECTIO:-directio}
40 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
41 UMOUNT=${UMOUNT:-"umount -d"}
42 STRIPES_PER_OBJ=-1
43 CHECK_GRANT=${CHECK_GRANT:-"yes"}
44 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
45 export PARALLEL=${PARALLEL:-"no"}
46
47 export NAME=${NAME:-local}
48
49 SAVE_PWD=$PWD
50
51 CLEANUP=${CLEANUP:-:}
52 SETUP=${SETUP:-:}
53 TRACE=${TRACE:-""}
54 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
55 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
56 . $LUSTRE/tests/test-framework.sh
57 init_test_env $@
58 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
59 init_logging
60
61 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24D 27m 64b 68 71 77f 78 115 124b 230d"
62
63 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
64         # bug number for skipped test: LU-1593  LU-4536 LU-5242  LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  34h      65ic    78 79 80 180     184c"
66         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b 51ba"
67 fi
68
69 FAIL_ON_ERROR=false
70
71 cleanup() {
72         echo -n "cln.."
73         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
74         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
75 }
76 setup() {
77         echo -n "mnt.."
78         load_modules
79         setupall || exit 10
80         echo "done"
81 }
82
83 check_kernel_version() {
84         WANT_VER=$1
85         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
86         case $GOT_VER in
87         patchless|patchless_client) return 0;;
88         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
89         esac
90         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
91         return 1
92 }
93
94 check_swap_layouts_support()
95 {
96         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
97                 { skip "Does not support layout lock."; return 0; }
98         return 1
99 }
100
101 if [ "$ONLY" == "cleanup" ]; then
102        sh llmountcleanup.sh
103        exit 0
104 fi
105
106 check_and_setup_lustre
107
108 DIR=${DIR:-$MOUNT}
109 assert_DIR
110
111 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
112         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
113 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
114 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
115 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
116 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
117 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
118 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
119
120 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
121 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
122 rm -rf $DIR/[Rdfs][0-9]*
123
124 # $RUNAS_ID may get set incorrectly somewhere else
125 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
126
127 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
128
129 build_test_filter
130
131 if [ "${ONLY}" = "MOUNT" ] ; then
132         echo "Lustre is up, please go on"
133         exit
134 fi
135
136 echo "preparing for tests involving mounts"
137 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
138 touch $EXT2_DEV
139 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
140 echo # add a newline after mke2fs.
141
142 umask 077
143
144 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
145 lctl set_param debug=-1 2> /dev/null || true
146 test_0a() {
147         touch $DIR/$tfile
148         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
149         rm $DIR/$tfile
150         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
151 }
152 run_test 0a "touch; rm ====================="
153
154 test_0b() {
155         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
156         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
157 }
158 run_test 0b "chmod 0755 $DIR ============================="
159
160 test_0c() {
161         $LCTL get_param mdc.*.import | grep "state: FULL" ||
162                 error "import not FULL"
163         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
164                 error "bad target"
165 }
166 run_test 0c "check import proc ============================="
167
168 test_1() {
169         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
170         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
171         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
172         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
173         rmdir $DIR/$tdir/d2
174         rmdir $DIR/$tdir
175         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
176 }
177 run_test 1 "mkdir; remkdir; rmdir =============================="
178
179 test_2() {
180         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
181         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
182         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
183         rm -r $DIR/$tdir
184         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
185 }
186 run_test 2 "mkdir; touch; rmdir; check file ===================="
187
188 test_3() {
189         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
190         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
191         touch $DIR/$tdir/$tfile
192         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
193         rm -r $DIR/$tdir
194         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
195 }
196 run_test 3 "mkdir; touch; rmdir; check dir ====================="
197
198 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
199 test_4() {
200         local MDTIDX=1
201
202         test_mkdir $DIR/$tdir ||
203                 error "Create remote directory failed"
204
205         touch $DIR/$tdir/$tfile ||
206                 error "Create file under remote directory failed"
207
208         rmdir $DIR/$tdir &&
209                 error "Expect error removing in-use dir $DIR/$tdir"
210
211         test -d $DIR/$tdir || error "Remote directory disappeared"
212
213         rm -rf $DIR/$tdir || error "remove remote dir error"
214 }
215 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
216
217 test_5() {
218         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
219         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
220         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
221         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
222         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
223 }
224 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
225
226 test_6a() {
227         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
228         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile does not have perm 0666 or UID $UID"
231         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
232         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
233                 error "$tfile should be 0666 and owned by UID $UID"
234 }
235 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
236
237 test_6c() {
238         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
239         touch $DIR/$tfile
240         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
244         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
245                 error "$tfile should be owned by UID $RUNAS_ID"
246 }
247 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
248
249 test_6e() {
250         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
251         touch $DIR/$tfile
252         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by GID $UID"
255         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
256         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
257                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
258 }
259 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
260
261 test_6g() {
262         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
263         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
264         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
265         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
266         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
267         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
268         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
269                 error "$tdir/d/subdir should be GID $RUNAS_GID"
270 }
271 run_test 6g "Is new dir in sgid dir inheriting group?"
272
273 test_6h() { # bug 7331
274         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
275         touch $DIR/$tfile || error "touch failed"
276         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
277         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
278                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
279         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
280                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
281 }
282 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
283
284 test_7a() {
285         test_mkdir $DIR/$tdir
286         $MCREATE $DIR/$tdir/$tfile
287         chmod 0666 $DIR/$tdir/$tfile
288         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
289                 error "$tdir/$tfile should be mode 0666"
290 }
291 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
292
293 test_7b() {
294         if [ ! -d $DIR/$tdir ]; then
295                 mkdir $DIR/$tdir
296         fi
297         $MCREATE $DIR/$tdir/$tfile
298         echo -n foo > $DIR/$tdir/$tfile
299         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
300         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
301 }
302 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
303
304 test_8() {
305         test_mkdir $DIR/$tdir
306         touch $DIR/$tdir/$tfile
307         chmod 0666 $DIR/$tdir/$tfile
308         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
309                 error "$tfile mode not 0666"
310 }
311 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
312
313 test_9() {
314         test_mkdir $DIR/$tdir
315         test_mkdir $DIR/$tdir/d2
316         test_mkdir $DIR/$tdir/d2/d3
317         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
318 }
319 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
320
321 test_10() {
322         test_mkdir $DIR/$tdir
323         test_mkdir $DIR/$tdir/d2
324         touch $DIR/$tdir/d2/$tfile
325         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
326                 error "$tdir/d2/$tfile not a file"
327 }
328 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
329
330 test_11() {
331         test_mkdir $DIR/$tdir
332         test_mkdir $DIR/$tdir/d2
333         chmod 0666 $DIR/$tdir/d2
334         chmod 0705 $DIR/$tdir/d2
335         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
336                 error "$tdir/d2 mode not 0705"
337 }
338 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
339
340 test_12() {
341         test_mkdir $DIR/$tdir
342         touch $DIR/$tdir/$tfile
343         chmod 0666 $DIR/$tdir/$tfile
344         chmod 0654 $DIR/$tdir/$tfile
345         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
346                 error "$tdir/d2 mode not 0654"
347 }
348 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
349
350 test_13() {
351         test_mkdir $DIR/$tdir
352         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
353         >  $DIR/$tdir/$tfile
354         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
355                 error "$tdir/$tfile size not 0 after truncate"
356 }
357 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
358
359 test_14() {
360         test_mkdir $DIR/$tdir
361         touch $DIR/$tdir/$tfile
362         rm $DIR/$tdir/$tfile
363         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
364 }
365 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
366
367 test_15() {
368         test_mkdir $DIR/$tdir
369         touch $DIR/$tdir/$tfile
370         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
371         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
372                 error "$tdir/${tfile_2} not a file after rename"
373 }
374 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
375
376 test_16() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm -rf $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
381 }
382 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
383
384 test_17a() {
385         test_mkdir -p $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
388         ls -l $DIR/$tdir
389         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
390                 error "$tdir/l-exist not a symlink"
391         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
392                 error "$tdir/l-exist not referencing a file"
393         rm -f $DIR/$tdir/l-exist
394         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
395 }
396 run_test 17a "symlinks: create, remove (real) =================="
397
398 test_17b() {
399         test_mkdir -p $DIR/$tdir
400         ln -s no-such-file $DIR/$tdir/l-dangle
401         ls -l $DIR/$tdir
402         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
403                 error "$tdir/l-dangle not referencing no-such-file"
404         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
405                 error "$tdir/l-dangle not referencing non-existent file"
406         rm -f $DIR/$tdir/l-dangle
407         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
408 }
409 run_test 17b "symlinks: create, remove (dangling) =============="
410
411 test_17c() { # bug 3440 - don't save failed open RPC for replay
412         test_mkdir -p $DIR/$tdir
413         ln -s foo $DIR/$tdir/$tfile
414         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
415 }
416 run_test 17c "symlinks: open dangling (should return error) ===="
417
418 test_17d() {
419         test_mkdir -p $DIR/$tdir
420         ln -s foo $DIR/$tdir/$tfile
421         touch $DIR/$tdir/$tfile || error "creating to new symlink"
422 }
423 run_test 17d "symlinks: create dangling ========================"
424
425 test_17e() {
426         test_mkdir -p $DIR/$tdir
427         local foo=$DIR/$tdir/$tfile
428         ln -s $foo $foo || error "create symlink failed"
429         ls -l $foo || error "ls -l failed"
430         ls $foo && error "ls not failed" || true
431 }
432 run_test 17e "symlinks: create recursive symlink (should return error) ===="
433
434 test_17f() {
435         test_mkdir -p $DIR/$tdir
436         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
437         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
438         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
439         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
440         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
441         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
442         ls -l  $DIR/$tdir
443 }
444 run_test 17f "symlinks: long and very long symlink name ========================"
445
446 # str_repeat(S, N) generate a string that is string S repeated N times
447 str_repeat() {
448         local s=$1
449         local n=$2
450         local ret=''
451         while [ $((n -= 1)) -ge 0 ]; do
452                 ret=$ret$s
453         done
454         echo $ret
455 }
456
457 # Long symlinks and LU-2241
458 test_17g() {
459         test_mkdir -p $DIR/$tdir
460         local TESTS="59 60 61 4094 4095"
461
462         # Fix for inode size boundary in 2.1.4
463         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
464                 TESTS="4094 4095"
465
466         # Patch not applied to 2.2 or 2.3 branches
467         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
468         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
469                 TESTS="4094 4095"
470
471         # skip long symlink name for rhel6.5.
472         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
473         grep -q '6.5' /etc/redhat-release &>/dev/null &&
474                 TESTS="59 60 61 4062 4063"
475
476         for i in $TESTS; do
477                 local SYMNAME=$(str_repeat 'x' $i)
478                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
479                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
480         done
481 }
482 run_test 17g "symlinks: really long symlink name and inode boundaries"
483
484 test_17h() { #bug 17378
485         remote_mds_nodsh && skip "remote MDS with nodsh" && return
486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
487         local mdt_idx
488         test_mkdir -p $DIR/$tdir
489         if [[ $MDSCOUNT -gt 1 ]]; then
490                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
491         else
492                 mdt_idx=0
493         fi
494         $SETSTRIPE -c -1 $DIR/$tdir
495 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
496         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
497         touch $DIR/$tdir/$tfile || true
498 }
499 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
500
501 test_17i() { #bug 20018
502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
504         test_mkdir -c1 $DIR/$tdir
505         local foo=$DIR/$tdir/$tfile
506         local mdt_idx
507         if [[ $MDSCOUNT -gt 1 ]]; then
508                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
509         else
510                 mdt_idx=0
511         fi
512         ln -s $foo $foo || error "create symlink failed"
513 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
514         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
515         ls -l $foo && error "error not detected"
516         return 0
517 }
518 run_test 17i "don't panic on short symlink"
519
520 test_17k() { #bug 22301
521         [[ -z "$(which rsync 2>/dev/null)" ]] &&
522                 skip "no rsync command" && return 0
523         rsync --help | grep -q xattr ||
524                 skip_env "$(rsync --version | head -n1) does not support xattrs"
525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
526         test_mkdir -p $DIR/$tdir
527         test_mkdir -p $DIR/$tdir.new
528         touch $DIR/$tdir/$tfile
529         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
530         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
531                 error "rsync failed with xattrs enabled"
532 }
533 run_test 17k "symlinks: rsync with xattrs enabled ========================="
534
535 test_17l() { # LU-279
536         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
537                 skip "no getfattr command" && return 0
538         mkdir -p $DIR/$tdir
539         touch $DIR/$tdir/$tfile
540         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
541         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
542                 # -h to not follow symlinks. -m '' to list all the xattrs.
543                 # grep to remove first line: '# file: $path'.
544                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
545                 do
546                         lgetxattr_size_check $path $xattr ||
547                                 error "lgetxattr_size_check $path $xattr failed"
548                 done
549         done
550 }
551 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
552
553 # LU-1540
554 test_17m() {
555         local short_sym="0123456789"
556         local WDIR=$DIR/${tdir}m
557         local mds_index
558         local devname
559         local cmd
560         local i
561         local rc=0
562
563         remote_mds_nodsh && skip "remote MDS with nodsh" && return
564         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
565         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
566                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
567
568         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
569                 skip "only for ldiskfs MDT" && return 0
570
571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
572
573         mkdir -p $WDIR
574         long_sym=$short_sym
575         # create a long symlink file
576         for ((i = 0; i < 4; ++i)); do
577                 long_sym=${long_sym}${long_sym}
578         done
579
580         echo "create 512 short and long symlink files under $WDIR"
581         for ((i = 0; i < 256; ++i)); do
582                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
583                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
584         done
585
586         echo "erase them"
587         rm -f $WDIR/*
588         sync
589         wait_delete_completed
590
591         echo "recreate the 512 symlink files with a shorter string"
592         for ((i = 0; i < 512; ++i)); do
593                 # rewrite the symlink file with a shorter string
594                 ln -sf ${long_sym} $WDIR/long-$i
595                 ln -sf ${short_sym} $WDIR/short-$i
596         done
597
598         mds_index=$($LFS getstripe -M $WDIR)
599         mds_index=$((mds_index+1))
600         devname=$(mdsdevname $mds_index)
601         cmd="$E2FSCK -fnvd $devname"
602
603         echo "stop and checking mds${mds_index}: $cmd"
604         # e2fsck should not return error
605         stop mds${mds_index}
606         do_facet mds${mds_index} $cmd || rc=$?
607
608         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
609         df $MOUNT > /dev/null 2>&1
610         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
611                 "short/long symlink MDT: rc=$rc"
612         return $rc
613 }
614 run_test 17m "run e2fsck against MDT which contains short/long symlink"
615
616 check_fs_consistency_17n() {
617         local mdt_index
618         local devname
619         local cmd
620         local rc=0
621
622         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
623         # so it only check MDT1/MDT2 instead of all of MDTs.
624         for mdt_index in $(seq 1 2); do
625                 devname=$(mdsdevname $mdt_index)
626                 cmd="$E2FSCK -fnvd $devname"
627
628                 echo "stop and checking mds${mdt_index}: $cmd"
629                 # e2fsck should not return error
630                 stop mds${mdt_index}
631                 do_facet mds${mdt_index} $cmd || rc=$?
632
633                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
634                         error "mount mds${mdt_index} failed"
635                 df $MOUNT > /dev/null 2>&1
636                 [ $rc -ne 0 ] && break
637         done
638         return $rc
639 }
640
641 test_17n() {
642         local i
643
644         remote_mds_nodsh && skip "remote MDS with nodsh" && return
645         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
646         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
647                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
648
649         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
650                 skip "only for ldiskfs MDT" && return 0
651
652         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
653
654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
655
656         mkdir $DIR/$tdir
657         for ((i=0; i<10; i++)); do
658                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
659                         error "create remote dir error $i"
660                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
661                         error "create files under remote dir failed $i"
662         done
663
664         check_fs_consistency_17n ||
665                 error "e2fsck report error after create files under remote dir"
666
667         for ((i = 0; i < 10; i++)); do
668                 rm -rf $DIR/$tdir/remote_dir_${i} ||
669                         error "destroy remote dir error $i"
670         done
671
672         check_fs_consistency_17n ||
673                 error "e2fsck report error after unlink files under remote dir"
674
675         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
676                 skip "lustre < 2.4.50 does not support migrate mv " && return
677
678         for ((i = 0; i < 10; i++)); do
679                 mkdir -p $DIR/$tdir/remote_dir_${i}
680                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
681                         error "create files under remote dir failed $i"
682                 $LFS mv --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
683                         error "migrate remote dir error $i"
684         done
685         check_fs_consistency_17n || error "e2fsck report error after migration"
686
687         for ((i = 0; i < 10; i++)); do
688                 rm -rf $DIR/$tdir/remote_dir_${i} ||
689                         error "destroy remote dir error $i"
690         done
691
692         check_fs_consistency_17n || error "e2fsck report error after unlink"
693 }
694 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
695
696 test_17o() {
697         remote_mds_nodsh && skip "remote MDS with nodsh" && return
698         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
699                 skip "Need MDS version at least 2.3.64" && return
700
701         local WDIR=$DIR/${tdir}o
702         local mdt_index
703         local mdtdevname
704         local rc=0
705
706         mkdir -p $WDIR
707         mdt_index=$($LFS getstripe -M $WDIR)
708         mdt_index=$((mdt_index+1))
709         mdtdevname=$(mdsdevname $mdt_index)
710
711         touch $WDIR/$tfile
712         stop mds${mdt_index}
713         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS ||
714                 error "mount mds${mdt_index} failed"
715
716         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
717         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
718         ls -l $WDIR/$tfile && rc=1
719         do_facet mds${mdt_index} lctl set_param fail_loc=0
720         [[ $rc -ne 0 ]] && error "stat file should fail"
721         true
722 }
723 run_test 17o "stat file with incompat LMA feature"
724
725 test_18() {
726         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
727         ls $DIR || error "Failed to ls $DIR: $?"
728 }
729 run_test 18 "touch .../f ; ls ... =============================="
730
731 test_19a() {
732         touch $DIR/$tfile
733         ls -l $DIR
734         rm $DIR/$tfile
735         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
736 }
737 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
738
739 test_19b() {
740         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
741 }
742 run_test 19b "ls -l .../f19 (should return error) =============="
743
744 test_19c() {
745         [ $RUNAS_ID -eq $UID ] &&
746                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
747         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
748 }
749 run_test 19c "$RUNAS touch .../f19 (should return error) =="
750
751 test_19d() {
752         cat $DIR/f19 && error || true
753 }
754 run_test 19d "cat .../f19 (should return error) =============="
755
756 test_20() {
757         touch $DIR/$tfile
758         rm $DIR/$tfile
759         log "1 done"
760         touch $DIR/$tfile
761         rm $DIR/$tfile
762         log "2 done"
763         touch $DIR/$tfile
764         rm $DIR/$tfile
765         log "3 done"
766         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
767 }
768 run_test 20 "touch .../f ; ls -l ... ==========================="
769
770 test_21() {
771         test_mkdir -p $DIR/$tdir
772         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
773         ln -s dangle $DIR/$tdir/link
774         echo foo >> $DIR/$tdir/link
775         cat $DIR/$tdir/dangle
776         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
777         $CHECKSTAT -f -t file $DIR/$tdir/link ||
778                 error "$tdir/link not linked to a file"
779 }
780 run_test 21 "write to dangling link ============================"
781
782 test_22() {
783         WDIR=$DIR/$tdir
784         test_mkdir -p $DIR/$tdir
785         chown $RUNAS_ID:$RUNAS_GID $WDIR
786         (cd $WDIR || error "cd $WDIR failed";
787         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
788         $RUNAS tar xf -)
789         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
790         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
791         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
792 }
793 run_test 22 "unpack tar archive as non-root user ==============="
794
795 # was test_23
796 test_23a() {
797         test_mkdir -p $DIR/$tdir
798         local file=$DIR/$tdir/$tfile
799
800         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
801         openfile -f O_CREAT:O_EXCL $file &&
802                 error "$file recreate succeeded" || true
803 }
804 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
805
806 test_23b() { # bug 18988
807         test_mkdir -p $DIR/$tdir
808         local file=$DIR/$tdir/$tfile
809
810         rm -f $file
811         echo foo > $file || error "write filed"
812         echo bar >> $file || error "append filed"
813         $CHECKSTAT -s 8 $file || error "wrong size"
814         rm $file
815 }
816 run_test 23b "O_APPEND check =========================="
817
818 # rename sanity
819 test_24a() {
820         echo '-- same directory rename'
821         test_mkdir $DIR/$tdir
822         touch $DIR/$tdir/$tfile.1
823         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
824         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
825 }
826 run_test 24a "rename file to non-existent target"
827
828 test_24b() {
829         test_mkdir $DIR/$tdir
830         touch $DIR/$tdir/$tfile.{1,2}
831         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
832         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
833         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
834 }
835 run_test 24b "rename file to existing target"
836
837 test_24c() {
838         test_mkdir $DIR/$tdir
839         test_mkdir $DIR/$tdir/d$testnum.1
840         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
841         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
842         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
843 }
844 run_test 24c "rename directory to non-existent target"
845
846 test_24d() {
847         test_mkdir -c1 $DIR/$tdir
848         test_mkdir -c1 $DIR/$tdir/d$testnum.1
849         test_mkdir -c1 $DIR/$tdir/d$testnum.2
850         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
851         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
852         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
853 }
854 run_test 24d "rename directory to existing target"
855
856 test_24e() {
857         echo '-- cross directory renames --'
858         test_mkdir $DIR/R5a
859         test_mkdir $DIR/R5b
860         touch $DIR/R5a/f
861         mv $DIR/R5a/f $DIR/R5b/g
862         $CHECKSTAT -a $DIR/R5a/f || error
863         $CHECKSTAT -t file $DIR/R5b/g || error
864 }
865 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
866
867 test_24f() {
868         test_mkdir $DIR/R6a
869         test_mkdir $DIR/R6b
870         touch $DIR/R6a/f $DIR/R6b/g
871         mv $DIR/R6a/f $DIR/R6b/g
872         $CHECKSTAT -a $DIR/R6a/f || error
873         $CHECKSTAT -t file $DIR/R6b/g || error
874 }
875 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
876
877 test_24g() {
878         test_mkdir $DIR/R7a
879         test_mkdir $DIR/R7b
880         test_mkdir $DIR/R7a/d
881         mv $DIR/R7a/d $DIR/R7b/e
882         $CHECKSTAT -a $DIR/R7a/d || error
883         $CHECKSTAT -t dir $DIR/R7b/e || error
884 }
885 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
886
887 test_24h() {
888         test_mkdir -c1 $DIR/R8a
889         test_mkdir -c1 $DIR/R8b
890         test_mkdir -c1 $DIR/R8a/d
891         test_mkdir -c1 $DIR/R8b/e
892         mrename $DIR/R8a/d $DIR/R8b/e
893         $CHECKSTAT -a $DIR/R8a/d || error
894         $CHECKSTAT -t dir $DIR/R8b/e || error
895 }
896 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
897
898 test_24i() {
899         echo "-- rename error cases"
900         test_mkdir $DIR/R9
901         test_mkdir $DIR/R9/a
902         touch $DIR/R9/f
903         mrename $DIR/R9/f $DIR/R9/a
904         $CHECKSTAT -t file $DIR/R9/f || error
905         $CHECKSTAT -t dir  $DIR/R9/a || error
906         $CHECKSTAT -a $DIR/R9/a/f || error
907 }
908 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
909
910 test_24j() {
911         test_mkdir $DIR/R10
912         mrename $DIR/R10/f $DIR/R10/g
913         $CHECKSTAT -t dir $DIR/R10 || error
914         $CHECKSTAT -a $DIR/R10/f || error
915         $CHECKSTAT -a $DIR/R10/g || error
916 }
917 run_test 24j "source does not exist ============================"
918
919 test_24k() {
920         test_mkdir $DIR/R11a
921         test_mkdir $DIR/R11a/d
922         touch $DIR/R11a/f
923         mv $DIR/R11a/f $DIR/R11a/d
924         $CHECKSTAT -a $DIR/R11a/f || error
925         $CHECKSTAT -t file $DIR/R11a/d/f || error
926 }
927 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
928
929 # bug 2429 - rename foo foo foo creates invalid file
930 test_24l() {
931         f="$DIR/f24l"
932         $MULTIOP $f OcNs || error
933 }
934 run_test 24l "Renaming a file to itself ========================"
935
936 test_24m() {
937         f="$DIR/f24m"
938         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
939         # on ext3 this does not remove either the source or target files
940         # though the "expected" operation would be to remove the source
941         $CHECKSTAT -t file ${f} || error "${f} missing"
942         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
943 }
944 run_test 24m "Renaming a file to a hard link to itself ========="
945
946 test_24n() {
947     f="$DIR/f24n"
948     # this stats the old file after it was renamed, so it should fail
949     touch ${f}
950     $CHECKSTAT ${f}
951     mv ${f} ${f}.rename
952     $CHECKSTAT ${f}.rename
953     $CHECKSTAT -a ${f}
954 }
955 run_test 24n "Statting the old file after renaming (Posix rename 2)"
956
957 test_24o() {
958         check_kernel_version 37 || return 0
959         test_mkdir -p $DIR/d24o
960         rename_many -s random -v -n 10 $DIR/d24o
961 }
962 run_test 24o "rename of files during htree split ==============="
963
964 test_24p() {
965         test_mkdir $DIR/R12a
966         test_mkdir $DIR/R12b
967         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
968         mrename $DIR/R12a $DIR/R12b
969         $CHECKSTAT -a $DIR/R12a || error
970         $CHECKSTAT -t dir $DIR/R12b || error
971         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
972         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
973 }
974 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
975
976 cleanup_multiop_pause() {
977         trap 0
978         kill -USR1 $MULTIPID
979 }
980
981 test_24q() {
982         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
983         test_mkdir $DIR/R13a
984         test_mkdir $DIR/R13b
985         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
986         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
987         MULTIPID=$!
988
989         trap cleanup_multiop_pause EXIT
990         mrename $DIR/R13a $DIR/R13b
991         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
992         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
993         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
994         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
995         cleanup_multiop_pause
996         wait $MULTIPID || error "multiop close failed"
997 }
998 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
999
1000 test_24r() { #bug 3789
1001         test_mkdir $DIR/R14a
1002         test_mkdir $DIR/R14a/b
1003         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1004         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1005         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1006 }
1007 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1008
1009 test_24s() {
1010         test_mkdir $DIR/R15a
1011         test_mkdir $DIR/R15a/b
1012         test_mkdir $DIR/R15a/b/c
1013         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1014         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1015         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1016 }
1017 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1018 test_24t() {
1019         test_mkdir $DIR/R16a
1020         test_mkdir $DIR/R16a/b
1021         test_mkdir $DIR/R16a/b/c
1022         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1023         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1024         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1025 }
1026 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1027
1028 test_24u() { # bug12192
1029         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1030         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1031 }
1032 run_test 24u "create stripe file"
1033
1034 page_size() {
1035         getconf PAGE_SIZE
1036 }
1037
1038 simple_cleanup_common() {
1039         trap 0
1040         rm -rf $DIR/$tdir
1041         wait_delete_completed
1042 }
1043
1044 max_pages_per_rpc() {
1045         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1046 }
1047
1048 test_24v() {
1049         local NRFILES=100000
1050         local FREE_INODES=$(mdt_free_inodes 0)
1051         [[ $FREE_INODES -lt $NRFILES ]] &&
1052                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1053                 return
1054
1055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1056         trap simple_cleanup_common EXIT
1057
1058         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1059         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1060
1061         mkdir -p $DIR/$tdir
1062         createmany -m $DIR/$tdir/$tfile $NRFILES
1063
1064         cancel_lru_locks mdc
1065         lctl set_param mdc.*.stats clear
1066
1067         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1068
1069         # LU-5 large readdir
1070         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1071         #               8 bytes for name(filename is mostly 5 in this test) +
1072         #               8 bytes for luda_type
1073         # take into account of overhead in lu_dirpage header and end mark in
1074         # each page, plus one in RPC_NUM calculation.
1075         DIRENT_SIZE=48
1076         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1077         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1078         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1079                                 awk '/^mds_readpage/ {print $2}')
1080         [[ $mds_readpage -gt $RPC_NUM ]] &&
1081                 error "large readdir doesn't take effect"
1082
1083         simple_cleanup_common
1084 }
1085 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1086
1087 test_24w() { # bug21506
1088         SZ1=234852
1089         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1090         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1091         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1092         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1093         [[ "$SZ1" -eq "$SZ2" ]] ||
1094                 error "Error reading at the end of the file $tfile"
1095 }
1096 run_test 24w "Reading a file larger than 4Gb"
1097
1098 test_24x() {
1099         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1101         local MDTIDX=1
1102         local remote_dir=$DIR/$tdir/remote_dir
1103
1104         mkdir -p $DIR/$tdir
1105         $LFS mkdir -i $MDTIDX $remote_dir ||
1106                 error "create remote directory failed"
1107
1108         mkdir -p $DIR/$tdir/src_dir
1109         touch $DIR/$tdir/src_file
1110         mkdir -p $remote_dir/tgt_dir
1111         touch $remote_dir/tgt_file
1112
1113         mrename $remote_dir $DIR/ &&
1114                 error "rename dir cross MDT works!"
1115
1116         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1117                 error "rename dir cross MDT works!"
1118
1119         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1120                 error "rename file cross MDT works!"
1121
1122         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1123                 error "ln file cross MDT should not work!"
1124
1125         rm -rf $DIR/$tdir || error "Can not delete directories"
1126 }
1127 run_test 24x "cross rename/link should be failed"
1128
1129 test_24y() {
1130         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1132         local MDTIDX=1
1133         local remote_dir=$DIR/$tdir/remote_dir
1134
1135         mkdir -p $DIR/$tdir
1136         $LFS mkdir -i $MDTIDX $remote_dir ||
1137                    error "create remote directory failed"
1138
1139         mkdir -p $remote_dir/src_dir
1140         touch $remote_dir/src_file
1141         mkdir -p $remote_dir/tgt_dir
1142         touch $remote_dir/tgt_file
1143
1144         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1145                 error "rename subdir in the same remote dir failed!"
1146
1147         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1148                 error "rename files in the same remote dir failed!"
1149
1150         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1151                 error "link files in the same remote dir failed!"
1152
1153         rm -rf $DIR/$tdir || error "Can not delete directories"
1154 }
1155 run_test 24y "rename/link on the same dir should succeed"
1156
1157 test_24z() {
1158         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1160         local MDTIDX=1
1161         local remote_src=$DIR/$tdir/remote_dir
1162         local remote_tgt=$DIR/$tdir/remote_tgt
1163
1164         mkdir -p $DIR/$tdir
1165         $LFS mkdir -i $MDTIDX $remote_src ||
1166                    error "create remote directory failed"
1167
1168         $LFS mkdir -i $MDTIDX $remote_tgt ||
1169                    error "create remote directory failed"
1170
1171         mrename $remote_src $remote_tgt &&
1172                 error "rename remote dirs should not work!"
1173
1174         # If target dir does not exists, it should succeed
1175         rm -rf $remote_tgt
1176         mrename $remote_src $remote_tgt ||
1177                 error "rename remote dirs(tgt dir does not exists) failed!"
1178
1179         rm -rf $DIR/$tdir || error "Can not delete directories"
1180 }
1181 run_test 24z "rename one remote dir to another remote dir should fail"
1182
1183 test_24A() { # LU-3182
1184         local NFILES=5000
1185
1186         rm -rf $DIR/$tdir
1187         mkdir -p $DIR/$tdir
1188         createmany -m $DIR/$tdir/$tfile $NFILES
1189         local t=$(ls $DIR/$tdir | wc -l)
1190         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1191         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1192         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1193                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1194         fi
1195
1196         rm -rf $DIR/$tdir || error "Can not delete directories"
1197 }
1198 run_test 24A "readdir() returns correct number of entries."
1199
1200 test_24B() { # LU-4805
1201         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1202         local count
1203
1204         mkdir $DIR/$tdir
1205         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1206                 error "create striped dir failed"
1207
1208         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1209         [ $count -eq 2 ] || error "Expected 2, got $count"
1210
1211         touch $DIR/$tdir/striped_dir/a
1212
1213         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1214         [ $count -eq 3 ] || error "Expected 3, got $count"
1215
1216         touch $DIR/$tdir/striped_dir/.f
1217
1218         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1219         [ $count -eq 4 ] || error "Expected 4, got $count"
1220
1221         rm -rf $DIR/$tdir || error "Can not delete directories"
1222 }
1223 run_test 24B "readdir for striped dir return correct number of entries"
1224
1225 test_24C() {
1226         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1227
1228         mkdir $DIR/$tdir
1229         mkdir $DIR/$tdir/d0
1230         mkdir $DIR/$tdir/d1
1231
1232         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1233                 error "create striped dir failed"
1234
1235         cd $DIR/$tdir/d0/striped_dir
1236
1237         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1238         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1239         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1240
1241         [ "$d0_ino" = "$parent_ino" ] ||
1242                 error ".. wrong, expect $d0_ino, get $parent_ino"
1243
1244         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1245                 error "mv striped dir failed"
1246
1247         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1248
1249         [ "$d1_ino" = "$parent_ino" ] ||
1250                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1251 }
1252 run_test 24C "check .. in striped dir"
1253
1254 test_24D() { # LU-6101
1255         local NFILES=50000
1256
1257         rm -rf $DIR/$tdir
1258         mkdir -p $DIR/$tdir
1259         createmany -m $DIR/$tdir/$tfile $NFILES
1260         local t=$(ls $DIR/$tdir | wc -l)
1261         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1262         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1263         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1264                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1265         fi
1266
1267         rm -rf $DIR/$tdir || error "Can not delete directories"
1268 }
1269 run_test 24D "readdir() returns correct number of entries after cursor reload"
1270
1271 test_25a() {
1272         echo '== symlink sanity ============================================='
1273
1274         test_mkdir $DIR/d25
1275         ln -s d25 $DIR/s25
1276         touch $DIR/s25/foo || error
1277 }
1278 run_test 25a "create file in symlinked directory ==============="
1279
1280 test_25b() {
1281         [ ! -d $DIR/d25 ] && test_25a
1282         $CHECKSTAT -t file $DIR/s25/foo || error
1283 }
1284 run_test 25b "lookup file in symlinked directory ==============="
1285
1286 test_26a() {
1287         test_mkdir $DIR/d26
1288         test_mkdir $DIR/d26/d26-2
1289         ln -s d26/d26-2 $DIR/s26
1290         touch $DIR/s26/foo || error
1291 }
1292 run_test 26a "multiple component symlink ======================="
1293
1294 test_26b() {
1295         test_mkdir -p $DIR/d26b/d26-2
1296         ln -s d26b/d26-2/foo $DIR/s26-2
1297         touch $DIR/s26-2 || error
1298 }
1299 run_test 26b "multiple component symlink at end of lookup ======"
1300
1301 test_26c() {
1302         test_mkdir $DIR/d26.2
1303         touch $DIR/d26.2/foo
1304         ln -s d26.2 $DIR/s26.2-1
1305         ln -s s26.2-1 $DIR/s26.2-2
1306         ln -s s26.2-2 $DIR/s26.2-3
1307         chmod 0666 $DIR/s26.2-3/foo
1308 }
1309 run_test 26c "chain of symlinks ================================"
1310
1311 # recursive symlinks (bug 439)
1312 test_26d() {
1313         ln -s d26-3/foo $DIR/d26-3
1314 }
1315 run_test 26d "create multiple component recursive symlink ======"
1316
1317 test_26e() {
1318         [ ! -h $DIR/d26-3 ] && test_26d
1319         rm $DIR/d26-3
1320 }
1321 run_test 26e "unlink multiple component recursive symlink ======"
1322
1323 # recursive symlinks (bug 7022)
1324 test_26f() {
1325         test_mkdir -p $DIR/$tdir
1326         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1327         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1328         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1329         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1330         cd $tfile                || error "cd $tfile failed"
1331         ln -s .. dotdot          || error "ln dotdot failed"
1332         ln -s dotdot/lndir lndir || error "ln lndir failed"
1333         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1334         output=`ls $tfile/$tfile/lndir/bar1`
1335         [ "$output" = bar1 ] && error "unexpected output"
1336         rm -r $tfile             || error "rm $tfile failed"
1337         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1338 }
1339 run_test 26f "rm -r of a directory which has recursive symlink ="
1340
1341 test_27a() {
1342         echo '== stripe sanity =============================================='
1343         test_mkdir -p $DIR/d27 || error "mkdir failed"
1344         $GETSTRIPE $DIR/d27
1345         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1346         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1347         log "== test_27a: write to one stripe file ========================="
1348         cp /etc/hosts $DIR/d27/f0 || error
1349 }
1350 run_test 27a "one stripe file =================================="
1351
1352 test_27b() {
1353         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1354         test_mkdir -p $DIR/d27
1355         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1356         $GETSTRIPE -c $DIR/d27/f01
1357         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1358                 error "two-stripe file doesn't have two stripes"
1359
1360         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1361 }
1362 run_test 27b "create and write to two stripe file"
1363
1364 test_27d() {
1365         test_mkdir -p $DIR/d27
1366         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1367         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1368         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1369 }
1370 run_test 27d "create file with default settings ================"
1371
1372 test_27e() {
1373         test_mkdir -p $DIR/d27
1374         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1375         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1376         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1377 }
1378 run_test 27e "setstripe existing file (should return error) ======"
1379
1380 test_27f() {
1381         test_mkdir $DIR/$tdir
1382         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1383                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1384         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1385                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1386         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1387         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1388 }
1389 run_test 27f "setstripe with bad stripe size (should return error)"
1390
1391 test_27g() {
1392         test_mkdir -p $DIR/d27
1393         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1394         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1395                 error "$DIR/d27/fnone has object"
1396 }
1397 run_test 27g "$GETSTRIPE with no objects"
1398
1399 test_27i() {
1400         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_57a() {
5039         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5040         # note test will not do anything if MDS is not local
5041         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5042                 skip "Only applicable to ldiskfs-based MDTs"
5043                 return
5044         fi
5045
5046         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5047         local MNTDEV="osd*.*MDT*.mntdev"
5048         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5049         [ -z "$DEV" ] && error "can't access $MNTDEV"
5050         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5051                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5052                         error "can't access $DEV"
5053                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5054                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5055                 rm $TMP/t57a.dump
5056         done
5057 }
5058 run_test 57a "verify MDS filesystem created with large inodes =="
5059
5060 test_57b() {
5061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5062         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5063                 skip "Only applicable to ldiskfs-based MDTs"
5064                 return
5065         fi
5066
5067         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5068         local dir=$DIR/$tdir
5069
5070         local FILECOUNT=100
5071         local FILE1=$dir/f1
5072         local FILEN=$dir/f$FILECOUNT
5073
5074         rm -rf $dir || error "removing $dir"
5075         test_mkdir -p -c1 $dir || error "creating $dir"
5076         local mdtidx=$($LFS getstripe -M $dir)
5077         local mdtname=MDT$(printf %04x $mdtidx)
5078         local facet=mds$((mdtidx + 1))
5079
5080         echo "mcreating $FILECOUNT files"
5081         createmany -m $dir/f 1 $FILECOUNT || \
5082                 error "creating files in $dir"
5083
5084         # verify that files do not have EAs yet
5085         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5086         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5087
5088         sync
5089         sleep 1
5090         df $dir  #make sure we get new statfs data
5091         local MDSFREE=$(do_facet $facet \
5092                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5093         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5094         echo "opening files to create objects/EAs"
5095         local FILE
5096         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5097                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5098         done
5099
5100         # verify that files have EAs now
5101         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5102         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5103
5104         sleep 1  #make sure we get new statfs data
5105         df $dir
5106         local MDSFREE2=$(do_facet $facet \
5107                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5108         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5109         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5110                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5111                         error "MDC before $MDCFREE != after $MDCFREE2"
5112                 else
5113                         echo "MDC before $MDCFREE != after $MDCFREE2"
5114                         echo "unable to confirm if MDS has large inodes"
5115                 fi
5116         fi
5117         rm -rf $dir
5118 }
5119 run_test 57b "default LOV EAs are stored inside large inodes ==="
5120
5121 test_58() {
5122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5123         [ -z "$(which wiretest 2>/dev/null)" ] &&
5124                         skip_env "could not find wiretest" && return
5125         wiretest
5126 }
5127 run_test 58 "verify cross-platform wire constants =============="
5128
5129 test_59() {
5130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5131         echo "touch 130 files"
5132         createmany -o $DIR/f59- 130
5133         echo "rm 130 files"
5134         unlinkmany $DIR/f59- 130
5135         sync
5136         # wait for commitment of removal
5137         wait_delete_completed
5138 }
5139 run_test 59 "verify cancellation of llog records async ========="
5140
5141 TEST60_HEAD="test_60 run $RANDOM"
5142 test_60a() {
5143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5144         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5145         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5146                 skip_env "missing subtest run-llog.sh" && return
5147         log "$TEST60_HEAD - from kernel mode"
5148         do_facet mgs sh run-llog.sh
5149 }
5150 run_test 60a "llog sanity tests run from kernel module =========="
5151
5152 test_60b() { # bug 6411
5153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5154         dmesg > $DIR/$tfile
5155         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5156                                  /llog.test/ {
5157                                          if (marker)
5158                                                  from_marker++
5159                                          from_begin++
5160                                  }
5161                                  END {
5162                                          if (marker)
5163                                                  print from_marker
5164                                          else
5165                                                  print from_begin
5166                                  }"`
5167         [[ $LLOG_COUNT -gt 50 ]] &&
5168                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5169 }
5170 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5171
5172 test_60c() {
5173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5174         echo "create 5000 files"
5175         createmany -o $DIR/f60c- 5000
5176 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5177         lctl set_param fail_loc=0x80000137
5178         unlinkmany $DIR/f60c- 5000
5179         lctl set_param fail_loc=0
5180 }
5181 run_test 60c "unlink file when mds full"
5182
5183 test_60d() {
5184         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5185         SAVEPRINTK=$(lctl get_param -n printk)
5186
5187         # verify "lctl mark" is even working"
5188         MESSAGE="test message ID $RANDOM $$"
5189         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5190         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5191
5192         lctl set_param printk=0 || error "set lnet.printk failed"
5193         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5194         MESSAGE="new test message ID $RANDOM $$"
5195         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5196         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5197         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5198
5199         lctl set_param -n printk="$SAVEPRINTK"
5200 }
5201 run_test 60d "test printk console message masking"
5202
5203 test_61() {
5204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5205         f="$DIR/f61"
5206         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5207         cancel_lru_locks osc
5208         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5209         sync
5210 }
5211 run_test 61 "mmap() writes don't make sync hang ================"
5212
5213 # bug 2330 - insufficient obd_match error checking causes LBUG
5214 test_62() {
5215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5216         f="$DIR/f62"
5217         echo foo > $f
5218         cancel_lru_locks osc
5219         lctl set_param fail_loc=0x405
5220         cat $f && error "cat succeeded, expect -EIO"
5221         lctl set_param fail_loc=0
5222 }
5223 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5224 # match every page all of the time.
5225 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5226
5227 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5228 # Though this test is irrelevant anymore, it helped to reveal some
5229 # other grant bugs (LU-4482), let's keep it.
5230 test_63a() {   # was test_63
5231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5232         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5233         for i in `seq 10` ; do
5234                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5235                 sleep 5
5236                 kill $!
5237                 sleep 1
5238         done
5239
5240         rm -f $DIR/f63 || true
5241 }
5242 run_test 63a "Verify oig_wait interruption does not crash ======="
5243
5244 # bug 2248 - async write errors didn't return to application on sync
5245 # bug 3677 - async write errors left page locked
5246 test_63b() {
5247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5248         debugsave
5249         lctl set_param debug=-1
5250
5251         # ensure we have a grant to do async writes
5252         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5253         rm $DIR/$tfile
5254
5255         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5256         lctl set_param fail_loc=0x80000406
5257         $MULTIOP $DIR/$tfile Owy && \
5258                 error "sync didn't return ENOMEM"
5259         sync; sleep 2; sync     # do a real sync this time to flush page
5260         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5261                 error "locked page left in cache after async error" || true
5262         debugrestore
5263 }
5264 run_test 63b "async write errors should be returned to fsync ==="
5265
5266 test_64a () {
5267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5268         df $DIR
5269         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5270 }
5271 run_test 64a "verify filter grant calculations (in kernel) ====="
5272
5273 test_64b () {
5274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5275         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5276 }
5277 run_test 64b "check out-of-space detection on client ==========="
5278
5279 test_64c() {
5280         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5281 }
5282 run_test 64c "verify grant shrink ========================------"
5283
5284 # bug 1414 - set/get directories' stripe info
5285 test_65a() {
5286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5287         test_mkdir -p $DIR/$tdir
5288         touch $DIR/$tdir/f1
5289         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5290 }
5291 run_test 65a "directory with no stripe info ===================="
5292
5293 test_65b() {
5294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5295         test_mkdir -p $DIR/$tdir
5296         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5297                                                 error "setstripe"
5298         touch $DIR/$tdir/f2
5299         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5300 }
5301 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5302
5303 test_65c() {
5304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5305         if [[ $OSTCOUNT -gt 1 ]]; then
5306                 test_mkdir -p $DIR/$tdir
5307                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5308                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5309                 touch $DIR/$tdir/f3
5310                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5311         fi
5312 }
5313 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5314
5315 test_65d() {
5316         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5317         test_mkdir -p $DIR/$tdir
5318         if [[ $STRIPECOUNT -le 0 ]]; then
5319                 sc=1
5320         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5321 #LOV_MAX_STRIPE_COUNT is 2000
5322                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5323         else
5324                 sc=$(($STRIPECOUNT - 1))
5325         fi
5326         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5327         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5328         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5329                 error "lverify failed"
5330 }
5331 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5332
5333 test_65e() {
5334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5335         test_mkdir -p $DIR/$tdir
5336
5337         $SETSTRIPE $DIR/$tdir || error "setstripe"
5338         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5339                                         error "no stripe info failed"
5340         touch $DIR/$tdir/f6
5341         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5342 }
5343 run_test 65e "directory setstripe defaults ======================="
5344
5345 test_65f() {
5346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5347         test_mkdir -p $DIR/${tdir}f
5348         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5349 }
5350 run_test 65f "dir setstripe permission (should return error) ==="
5351
5352 test_65g() {
5353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5354         test_mkdir -p $DIR/$tdir
5355         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5356                                                         error "setstripe"
5357         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5358         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5359                 error "delete default stripe failed"
5360 }
5361 run_test 65g "directory setstripe -d ==========================="
5362
5363 test_65h() {
5364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5365         test_mkdir -p $DIR/$tdir
5366         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5367                                                         error "setstripe"
5368         test_mkdir -p $DIR/$tdir/dd1
5369         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5370                 error "stripe info inherit failed"
5371 }
5372 run_test 65h "directory stripe info inherit ===================="
5373
5374 test_65i() { # bug6367
5375         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5376         $SETSTRIPE -S 65536 -c -1 $MOUNT
5377 }
5378 run_test 65i "set non-default striping on root directory (bug 6367)="
5379
5380 test_65ia() { # bug12836
5381         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5382         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5383 }
5384 run_test 65ia "getstripe on -1 default directory striping"
5385
5386 test_65ib() { # bug12836
5387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5388         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5389 }
5390 run_test 65ib "getstripe -v on -1 default directory striping"
5391
5392 test_65ic() { # bug12836
5393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5394         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5395 }
5396 run_test 65ic "new find on -1 default directory striping"
5397
5398 test_65j() { # bug6367
5399         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5400         sync; sleep 1
5401         # if we aren't already remounting for each test, do so for this test
5402         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5403                 cleanup || error "failed to unmount"
5404                 setup
5405         fi
5406         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5407 }
5408 run_test 65j "set default striping on root directory (bug 6367)="
5409
5410 test_65k() { # bug11679
5411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5412         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5413         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5414
5415     echo "Check OST status: "
5416     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5417               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5418
5419     for OSC in $MDS_OSCS; do
5420         echo $OSC "is activate"
5421         do_facet $SINGLEMDS lctl --device %$OSC activate
5422     done
5423
5424     mkdir -p $DIR/$tdir
5425     for INACTIVE_OSC in $MDS_OSCS; do
5426         echo "Deactivate: " $INACTIVE_OSC
5427         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5428         for STRIPE_OSC in $MDS_OSCS; do
5429             OST=`osc_to_ost $STRIPE_OSC`
5430             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5431                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5432
5433             [ -f $DIR/$tdir/$IDX ] && continue
5434             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5435             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5436             RC=$?
5437             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5438         done
5439         rm -f $DIR/$tdir/*
5440         echo $INACTIVE_OSC "is Activate."
5441         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5442     done
5443 }
5444 run_test 65k "validate manual striping works properly with deactivated OSCs"
5445
5446 test_65l() { # bug 12836
5447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5448         test_mkdir -p $DIR/$tdir/test_dir
5449         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5450         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5451 }
5452 run_test 65l "lfs find on -1 stripe dir ========================"
5453
5454 # bug 2543 - update blocks count on client
5455 test_66() {
5456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5457         COUNT=${COUNT:-8}
5458         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5459         sync; sync_all_data; sync; sync_all_data
5460         cancel_lru_locks osc
5461         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5462         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5463 }
5464 run_test 66 "update inode blocks count on client ==============="
5465
5466 LLOOP=
5467 LLITELOOPLOAD=
5468 cleanup_68() {
5469         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5470         trap 0
5471         if [ ! -z "$LLOOP" ]; then
5472                 if swapon -s | grep -q $LLOOP; then
5473                         swapoff $LLOOP || error "swapoff failed"
5474                 fi
5475
5476                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5477                 rm -f $LLOOP
5478                 unset LLOOP
5479         fi
5480         if [ ! -z "$LLITELOOPLOAD" ]; then
5481                 rmmod llite_lloop
5482                 unset LLITELOOPLOAD
5483         fi
5484         rm -f $DIR/f68*
5485 }
5486
5487 meminfo() {
5488         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5489 }
5490
5491 swap_used() {
5492         swapon -s | awk '($1 == "'$1'") { print $4 }'
5493 }
5494
5495 # test case for lloop driver, basic function
5496 test_68a() {
5497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5498         [ "$UID" != 0 ] && skip_env "must run as root" && return
5499         llite_lloop_enabled || \
5500                 { skip_env "llite_lloop module disabled" && return; }
5501
5502         trap cleanup_68 EXIT
5503
5504         if ! module_loaded llite_lloop; then
5505                 if load_module llite/llite_lloop; then
5506                         LLITELOOPLOAD=yes
5507                 else
5508                         skip_env "can't find module llite_lloop"
5509                         return
5510                 fi
5511         fi
5512
5513         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5514         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5515         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5516
5517         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5518         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5519
5520         cleanup_68
5521 }
5522 run_test 68a "lloop driver - basic test ========================"
5523
5524 # excercise swapping to lustre by adding a high priority swapfile entry
5525 # and then consuming memory until it is used.
5526 test_68b() {  # was test_68
5527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5528         [ "$UID" != 0 ] && skip_env "must run as root" && return
5529         lctl get_param -n devices | grep -q obdfilter && \
5530                 skip "local OST" && return
5531
5532         grep -q llite_lloop /proc/modules
5533         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5534
5535         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5536                 skip "can't reliably test swap with TCP" && return
5537
5538         MEMTOTAL=`meminfo MemTotal`
5539         NR_BLOCKS=$((MEMTOTAL>>8))
5540         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5541
5542         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5543         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5544         mkswap $DIR/f68b
5545
5546         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5547
5548         trap cleanup_68 EXIT
5549
5550         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5551
5552         echo "before: `swapon -s | grep $LLOOP`"
5553         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5554         echo "after: `swapon -s | grep $LLOOP`"
5555         SWAPUSED=`swap_used $LLOOP`
5556
5557         cleanup_68
5558
5559         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5560 }
5561 run_test 68b "support swapping to Lustre ========================"
5562
5563 # bug5265, obdfilter oa2dentry return -ENOENT
5564 # #define OBD_FAIL_OST_ENOENT 0x217
5565 test_69() {
5566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5567         remote_ost_nodsh && skip "remote OST with nodsh" && return
5568
5569         f="$DIR/$tfile"
5570         $SETSTRIPE -c 1 -i 0 $f
5571
5572         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5573
5574         do_facet ost1 lctl set_param fail_loc=0x217
5575         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5576         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5577
5578         do_facet ost1 lctl set_param fail_loc=0
5579         $DIRECTIO write $f 0 2 || error "write error"
5580
5581         cancel_lru_locks osc
5582         $DIRECTIO read $f 0 1 || error "read error"
5583
5584         do_facet ost1 lctl set_param fail_loc=0x217
5585         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5586
5587         do_facet ost1 lctl set_param fail_loc=0
5588         rm -f $f
5589 }
5590 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5591
5592 test_71() {
5593     test_mkdir -p $DIR/$tdir
5594     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5595 }
5596 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5597
5598 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5600         [ "$RUNAS_ID" = "$UID" ] &&
5601                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5602
5603         # Check that testing environment is properly set up. Skip if not
5604         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5605                 skip_env "User $RUNAS_ID does not exist - skipping"
5606                 return 0
5607         }
5608         touch $DIR/$tfile
5609         chmod 777 $DIR/$tfile
5610         chmod ug+s $DIR/$tfile
5611         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5612                 error "$RUNAS dd $DIR/$tfile failed"
5613         # See if we are still setuid/sgid
5614         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5615                 error "S/gid is not dropped on write"
5616         # Now test that MDS is updated too
5617         cancel_lru_locks mdc
5618         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5619                 error "S/gid is not dropped on MDS"
5620         rm -f $DIR/$tfile
5621 }
5622 run_test 72a "Test that remove suid works properly (bug5695) ===="
5623
5624 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5625         local perm
5626
5627         [ "$RUNAS_ID" = "$UID" ] && \
5628                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5629         [ "$RUNAS_ID" -eq 0 ] && \
5630                 skip_env "RUNAS_ID = 0 -- skipping" && return
5631
5632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5633         # Check that testing environment is properly set up. Skip if not
5634         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5635                 skip_env "User $RUNAS_ID does not exist - skipping"
5636                 return 0
5637         }
5638         touch $DIR/${tfile}-f{g,u}
5639         test_mkdir $DIR/${tfile}-dg
5640         test_mkdir $DIR/${tfile}-du
5641         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5642         chmod g+s $DIR/${tfile}-{f,d}g
5643         chmod u+s $DIR/${tfile}-{f,d}u
5644         for perm in 777 2777 4777; do
5645                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5646                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5647                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5648                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5649         done
5650         true
5651 }
5652 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5653
5654 # bug 3462 - multiple simultaneous MDC requests
5655 test_73() {
5656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5657         test_mkdir $DIR/d73-1
5658         test_mkdir $DIR/d73-2
5659         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5660         pid1=$!
5661
5662         lctl set_param fail_loc=0x80000129
5663         $MULTIOP $DIR/d73-1/f73-2 Oc &
5664         sleep 1
5665         lctl set_param fail_loc=0
5666
5667         $MULTIOP $DIR/d73-2/f73-3 Oc &
5668         pid3=$!
5669
5670         kill -USR1 $pid1
5671         wait $pid1 || return 1
5672
5673         sleep 25
5674
5675         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5676         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5677         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5678
5679         rm -rf $DIR/d73-*
5680 }
5681 run_test 73 "multiple MDC requests (should not deadlock)"
5682
5683 test_74a() { # bug 6149, 6184
5684         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5685         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5686         #
5687         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5688         # will spin in a tight reconnection loop
5689         touch $DIR/f74a
5690         $LCTL set_param fail_loc=0x8000030e
5691         # get any lock that won't be difficult - lookup works.
5692         ls $DIR/f74a
5693         $LCTL set_param fail_loc=0
5694         rm -f $DIR/f74a
5695         true
5696 }
5697 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5698
5699 test_74b() { # bug 13310
5700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5701         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5702         #
5703         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5704         # will spin in a tight reconnection loop
5705         $LCTL set_param fail_loc=0x8000030e
5706         # get a "difficult" lock
5707         touch $DIR/f74b
5708         $LCTL set_param fail_loc=0
5709         rm -f $DIR/f74b
5710         true
5711 }
5712 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5713
5714 test_74c() {
5715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5716         #define OBD_FAIL_LDLM_NEW_LOCK
5717         $LCTL set_param fail_loc=0x319
5718         touch $DIR/$tfile && error "touch successful"
5719         $LCTL set_param fail_loc=0
5720         true
5721 }
5722 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5723
5724 num_inodes() {
5725         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5726 }
5727
5728 get_inode_slab_tunables() {
5729         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5730 }
5731
5732 set_inode_slab_tunables() {
5733         echo "lustre_inode_cache $1" > /proc/slabinfo
5734 }
5735
5736 test_76() { # Now for bug 20433, added originally in bug 1443
5737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5738         local SLAB_SETTINGS=`get_inode_slab_tunables`
5739         local CPUS=`getconf _NPROCESSORS_ONLN`
5740         # we cannot set limit below 1 which means 1 inode in each
5741         # per-cpu cache is still allowed
5742         set_inode_slab_tunables "1 1 0"
5743         cancel_lru_locks osc
5744         BEFORE_INODES=$(num_inodes)
5745         echo "before inodes: $BEFORE_INODES"
5746         local COUNT=1000
5747         [ "$SLOW" = "no" ] && COUNT=100
5748         for i in $(seq $COUNT); do
5749                 touch $DIR/$tfile
5750                 rm -f $DIR/$tfile
5751         done
5752         cancel_lru_locks osc
5753         AFTER_INODES=$(num_inodes)
5754         echo "after inodes: $AFTER_INODES"
5755         local wait=0
5756         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5757                 sleep 2
5758                 AFTER_INODES=$(num_inodes)
5759                 wait=$((wait+2))
5760                 echo "wait $wait seconds inodes: $AFTER_INODES"
5761                 if [ $wait -gt 30 ]; then
5762                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5763                 fi
5764         done
5765         set_inode_slab_tunables "$SLAB_SETTINGS"
5766 }
5767 run_test 76 "confirm clients recycle inodes properly ===="
5768
5769
5770 export ORIG_CSUM=""
5771 set_checksums()
5772 {
5773         # Note: in sptlrpc modes which enable its own bulk checksum, the
5774         # original crc32_le bulk checksum will be automatically disabled,
5775         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5776         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5777         # In this case set_checksums() will not be no-op, because sptlrpc
5778         # bulk checksum will be enabled all through the test.
5779
5780         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5781         lctl set_param -n osc.*.checksums $1
5782         return 0
5783 }
5784
5785 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5786                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5787 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5788 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5789 set_checksum_type()
5790 {
5791         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5792         log "set checksum type to $1"
5793         return 0
5794 }
5795 F77_TMP=$TMP/f77-temp
5796 F77SZ=8
5797 setup_f77() {
5798         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5799                 error "error writing to $F77_TMP"
5800 }
5801
5802 test_77a() { # bug 10889
5803         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5804         $GSS && skip "could not run with gss" && return
5805         [ ! -f $F77_TMP ] && setup_f77
5806         set_checksums 1
5807         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5808         set_checksums 0
5809         rm -f $DIR/$tfile
5810 }
5811 run_test 77a "normal checksum read/write operation"
5812
5813 test_77b() { # bug 10889
5814         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5815         $GSS && skip "could not run with gss" && return
5816         [ ! -f $F77_TMP ] && setup_f77
5817         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5818         $LCTL set_param fail_loc=0x80000409
5819         set_checksums 1
5820
5821         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5822                 error "dd error: $?"
5823         $LCTL set_param fail_loc=0
5824
5825         for algo in $CKSUM_TYPES; do
5826                 cancel_lru_locks osc
5827                 set_checksum_type $algo
5828                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5829                 $LCTL set_param fail_loc=0x80000408
5830                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5831                 $LCTL set_param fail_loc=0
5832         done
5833         set_checksums 0
5834         set_checksum_type $ORIG_CSUM_TYPE
5835         rm -f $DIR/$tfile
5836 }
5837 run_test 77b "checksum error on client write, read"
5838
5839 test_77d() { # bug 10889
5840         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5841         $GSS && skip "could not run with gss" && return
5842         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5843         $LCTL set_param fail_loc=0x80000409
5844         set_checksums 1
5845         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5846                 error "direct write: rc=$?"
5847         $LCTL set_param fail_loc=0
5848         set_checksums 0
5849
5850         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5851         $LCTL set_param fail_loc=0x80000408
5852         set_checksums 1
5853         cancel_lru_locks osc
5854         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5855                 error "direct read: rc=$?"
5856         $LCTL set_param fail_loc=0
5857         set_checksums 0
5858 }
5859 run_test 77d "checksum error on OST direct write, read"
5860
5861 test_77f() { # bug 10889
5862         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5863         $GSS && skip "could not run with gss" && return
5864         set_checksums 1
5865         for algo in $CKSUM_TYPES; do
5866                 cancel_lru_locks osc
5867                 set_checksum_type $algo
5868                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5869                 $LCTL set_param fail_loc=0x409
5870                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5871                         error "direct write succeeded"
5872                 $LCTL set_param fail_loc=0
5873         done
5874         set_checksum_type $ORIG_CSUM_TYPE
5875         set_checksums 0
5876 }
5877 run_test 77f "repeat checksum error on write (expect error)"
5878
5879 test_77g() { # bug 10889
5880         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5881         $GSS && skip "could not run with gss" && return
5882         remote_ost_nodsh && skip "remote OST with nodsh" && return
5883
5884         [ ! -f $F77_TMP ] && setup_f77
5885
5886         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5887         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5888         do_facet ost1 lctl set_param fail_loc=0x8000021a
5889         set_checksums 1
5890         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5891                 error "write error: rc=$?"
5892         do_facet ost1 lctl set_param fail_loc=0
5893         set_checksums 0
5894
5895         cancel_lru_locks osc
5896         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5897         do_facet ost1 lctl set_param fail_loc=0x8000021b
5898         set_checksums 1
5899         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5900         do_facet ost1 lctl set_param fail_loc=0
5901         set_checksums 0
5902 }
5903 run_test 77g "checksum error on OST write, read"
5904
5905 test_77i() { # bug 13805
5906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5907         $GSS && skip "could not run with gss" && return
5908         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5909         lctl set_param fail_loc=0x40b
5910         remount_client $MOUNT
5911         lctl set_param fail_loc=0
5912         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5913                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5914                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5915                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5916         done
5917         remount_client $MOUNT
5918 }
5919 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5920
5921 test_77j() { # bug 13805
5922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5923         $GSS && skip "could not run with gss" && return
5924         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5925         lctl set_param fail_loc=0x40c
5926         remount_client $MOUNT
5927         lctl set_param fail_loc=0
5928         sleep 2 # wait async osc connect to finish
5929         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5930                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5931                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5932                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5933         done
5934         remount_client $MOUNT
5935 }
5936 run_test 77j "client only supporting ADLER32"
5937
5938 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5939 rm -f $F77_TMP
5940 unset F77_TMP
5941
5942 test_78() { # bug 10901
5943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5944         remote_ost || { skip_env "local OST" && return; }
5945
5946         NSEQ=5
5947         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5948         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5949         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5950         echo "MemTotal: $MEMTOTAL"
5951
5952         # reserve 256MB of memory for the kernel and other running processes,
5953         # and then take 1/2 of the remaining memory for the read/write buffers.
5954         if [ $MEMTOTAL -gt 512 ] ;then
5955                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5956         else
5957                 # for those poor memory-starved high-end clusters...
5958                 MEMTOTAL=$((MEMTOTAL / 2))
5959         fi
5960         echo "Mem to use for directio: $MEMTOTAL"
5961
5962         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5963         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5964         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5965         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5966                 head -n1)
5967         echo "Smallest OST: $SMALLESTOST"
5968         [[ $SMALLESTOST -lt 10240 ]] &&
5969                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5970
5971         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5972                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5973
5974         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5975         echo "File size: $F78SIZE"
5976         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5977         for i in $(seq 1 $NSEQ); do
5978                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5979                 echo directIO rdwr round $i of $NSEQ
5980                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5981         done
5982
5983         rm -f $DIR/$tfile
5984 }
5985 run_test 78 "handle large O_DIRECT writes correctly ============"
5986
5987 test_79() { # bug 12743
5988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5989         wait_delete_completed
5990
5991         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5992         BKFREE=$(calc_osc_kbytes kbytesfree)
5993         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5994
5995         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5996         DFTOTAL=`echo $STRING | cut -d, -f1`
5997         DFUSED=`echo $STRING  | cut -d, -f2`
5998         DFAVAIL=`echo $STRING | cut -d, -f3`
5999         DFFREE=$(($DFTOTAL - $DFUSED))
6000
6001         ALLOWANCE=$((64 * $OSTCOUNT))
6002
6003         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6004            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6005                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6006         fi
6007         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6008            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6009                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6010         fi
6011         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6012            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6013                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6014         fi
6015 }
6016 run_test 79 "df report consistency check ======================="
6017
6018 test_80() { # bug 10718
6019         remote_ost_nodsh && skip "remote OST with nodsh" && return
6020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6021         # relax strong synchronous semantics for slow backends like ZFS
6022         local soc="obdfilter.*.sync_on_lock_cancel"
6023         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6024         local hosts=
6025         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6026                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6027                           facet_active_host $host; done | sort -u)
6028                 do_nodes $hosts lctl set_param $soc=never
6029         fi
6030
6031         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6032         sync; sleep 1; sync
6033         local BEFORE=`date +%s`
6034         cancel_lru_locks osc
6035         local AFTER=`date +%s`
6036         local DIFF=$((AFTER-BEFORE))
6037         if [ $DIFF -gt 1 ] ; then
6038                 error "elapsed for 1M@1T = $DIFF"
6039         fi
6040
6041         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6042
6043         rm -f $DIR/$tfile
6044 }
6045 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6046
6047 test_81a() { # LU-456
6048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6049         remote_ost_nodsh && skip "remote OST with nodsh" && return
6050         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6051         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6052         do_facet ost1 lctl set_param fail_loc=0x80000228
6053
6054         # write should trigger a retry and success
6055         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6056         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6057         RC=$?
6058         if [ $RC -ne 0 ] ; then
6059                 error "write should success, but failed for $RC"
6060         fi
6061 }
6062 run_test 81a "OST should retry write when get -ENOSPC ==============="
6063
6064 test_81b() { # LU-456
6065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6066         remote_ost_nodsh && skip "remote OST with nodsh" && return
6067         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6068         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6069         do_facet ost1 lctl set_param fail_loc=0x228
6070
6071         # write should retry several times and return -ENOSPC finally
6072         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6073         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6074         RC=$?
6075         ENOSPC=28
6076         if [ $RC -ne $ENOSPC ] ; then
6077                 error "dd should fail for -ENOSPC, but succeed."
6078         fi
6079 }
6080 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6081
6082 test_82() { # LU-1031
6083         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6084         local gid1=14091995
6085         local gid2=16022000
6086
6087         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6088         local MULTIPID1=$!
6089         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6090         local MULTIPID2=$!
6091         kill -USR1 $MULTIPID2
6092         sleep 2
6093         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6094                 error "First grouplock does not block second one"
6095         else
6096                 echo "Second grouplock blocks first one"
6097         fi
6098         kill -USR1 $MULTIPID1
6099         wait $MULTIPID1
6100         wait $MULTIPID2
6101 }
6102 run_test 82 "Basic grouplock test ==============================="
6103
6104 test_99a() {
6105         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6106                 return
6107         test_mkdir -p $DIR/d99cvsroot
6108         chown $RUNAS_ID $DIR/d99cvsroot
6109         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6110         cd $TMP
6111
6112         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6113         cd $oldPWD
6114 }
6115 run_test 99a "cvs init ========================================="
6116
6117 test_99b() {
6118         [ -z "$(which cvs 2>/dev/null)" ] &&
6119                 skip_env "could not find cvs" && return
6120         [ ! -d $DIR/d99cvsroot ] && test_99a
6121         cd /etc/init.d
6122         # some versions of cvs import exit(1) when asked to import links or
6123         # files they can't read.  ignore those files.
6124         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6125                         ! -perm +4 -printf '-I %f\n')
6126         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6127                 d99reposname vtag rtag
6128 }
6129 run_test 99b "cvs import ======================================="
6130
6131 test_99c() {
6132         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6133         [ ! -d $DIR/d99cvsroot ] && test_99b
6134         cd $DIR
6135         test_mkdir -p $DIR/d99reposname
6136         chown $RUNAS_ID $DIR/d99reposname
6137         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6138 }
6139 run_test 99c "cvs checkout ====================================="
6140
6141 test_99d() {
6142         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6143         [ ! -d $DIR/d99cvsroot ] && test_99c
6144         cd $DIR/d99reposname
6145         $RUNAS touch foo99
6146         $RUNAS cvs add -m 'addmsg' foo99
6147 }
6148 run_test 99d "cvs add =========================================="
6149
6150 test_99e() {
6151         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6152         [ ! -d $DIR/d99cvsroot ] && test_99c
6153         cd $DIR/d99reposname
6154         $RUNAS cvs update
6155 }
6156 run_test 99e "cvs update ======================================="
6157
6158 test_99f() {
6159         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6160         [ ! -d $DIR/d99cvsroot ] && test_99d
6161         cd $DIR/d99reposname
6162         $RUNAS cvs commit -m 'nomsg' foo99
6163     rm -fr $DIR/d99cvsroot
6164 }
6165 run_test 99f "cvs commit ======================================="
6166
6167 test_100() {
6168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6169         [ "$NETTYPE" = tcp ] || \
6170                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6171                         return ; }
6172
6173         remote_ost_nodsh && skip "remote OST with nodsh" && return
6174         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6175         remote_servers || \
6176                 { skip "useless for local single node setup" && return; }
6177
6178         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6179                 [ "$PROT" != "tcp" ] && continue
6180                 RPORT=$(echo $REMOTE | cut -d: -f2)
6181                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6182
6183                 rc=0
6184                 LPORT=`echo $LOCAL | cut -d: -f2`
6185                 if [ $LPORT -ge 1024 ]; then
6186                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6187                         netstat -tna
6188                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6189                 fi
6190         done
6191         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6192 }
6193 run_test 100 "check local port using privileged port ==========="
6194
6195 function get_named_value()
6196 {
6197     local tag
6198
6199     tag=$1
6200     while read ;do
6201         line=$REPLY
6202         case $line in
6203         $tag*)
6204             echo $line | sed "s/^$tag[ ]*//"
6205             break
6206             ;;
6207         esac
6208     done
6209 }
6210
6211 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6212                    awk '/^max_cached_mb/ { print $2 }')
6213
6214 cleanup_101a() {
6215         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6216         trap 0
6217 }
6218
6219 test_101a() {
6220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6221         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6222         local s
6223         local discard
6224         local nreads=10000
6225         local cache_limit=32
6226
6227         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6228         trap cleanup_101a EXIT
6229         $LCTL set_param -n llite.*.read_ahead_stats 0
6230         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6231
6232         #
6233         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6234         #
6235         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6236         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6237
6238         discard=0
6239         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6240                 get_named_value 'read but discarded' | cut -d" " -f1); do
6241                         discard=$(($discard + $s))
6242         done
6243         cleanup_101a
6244
6245         if [[ $(($discard * 10)) -gt $nreads ]]; then
6246                 $LCTL get_param osc.*-osc*.rpc_stats
6247                 $LCTL get_param llite.*.read_ahead_stats
6248                 error "too many ($discard) discarded pages"
6249         fi
6250         rm -f $DIR/$tfile || true
6251 }
6252 run_test 101a "check read-ahead for random reads ================"
6253
6254 setup_test101bc() {
6255         test_mkdir -p $DIR/$tdir
6256         local STRIPE_SIZE=$1
6257         local FILE_LENGTH=$2
6258         STRIPE_OFFSET=0
6259
6260         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6261
6262         local list=$(comma_list $(osts_nodes))
6263         set_osd_param $list '' read_cache_enable 0
6264         set_osd_param $list '' writethrough_cache_enable 0
6265
6266         trap cleanup_test101bc EXIT
6267         # prepare the read-ahead file
6268         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6269
6270         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6271                                 count=$FILE_SIZE_MB 2> /dev/null
6272
6273 }
6274
6275 cleanup_test101bc() {
6276         trap 0
6277         rm -rf $DIR/$tdir
6278         rm -f $DIR/$tfile
6279
6280         local list=$(comma_list $(osts_nodes))
6281         set_osd_param $list '' read_cache_enable 1
6282         set_osd_param $list '' writethrough_cache_enable 1
6283 }
6284
6285 calc_total() {
6286         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6287 }
6288
6289 ra_check_101() {
6290         local READ_SIZE=$1
6291         local STRIPE_SIZE=$2
6292         local FILE_LENGTH=$3
6293         local RA_INC=1048576
6294         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6295         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6296                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6297         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6298                         get_named_value 'read but discarded' |
6299                         cut -d" " -f1 | calc_total)
6300         if [[ $DISCARD -gt $discard_limit ]]; then
6301                 $LCTL get_param llite.*.read_ahead_stats
6302                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6303         else
6304                 echo "Read-ahead success for size ${READ_SIZE}"
6305         fi
6306 }
6307
6308 test_101b() {
6309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6310         [[ $OSTCOUNT -lt 2 ]] &&
6311                 skip_env "skipping stride IO stride-ahead test" && return
6312         local STRIPE_SIZE=1048576
6313         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6314         if [ $SLOW == "yes" ]; then
6315                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6316         else
6317                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6318         fi
6319
6320         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6321
6322         # prepare the read-ahead file
6323         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6324         cancel_lru_locks osc
6325         for BIDX in 2 4 8 16 32 64 128 256
6326         do
6327                 local BSIZE=$((BIDX*4096))
6328                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6329                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6330                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6331                 $LCTL set_param -n llite.*.read_ahead_stats 0
6332                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6333                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6334                 cancel_lru_locks osc
6335                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6336         done
6337         cleanup_test101bc
6338         true
6339 }
6340 run_test 101b "check stride-io mode read-ahead ================="
6341
6342 test_101c() {
6343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6344         local STRIPE_SIZE=1048576
6345         local FILE_LENGTH=$((STRIPE_SIZE*100))
6346         local nreads=10000
6347         local osc_rpc_stats
6348
6349         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6350
6351         cancel_lru_locks osc
6352         $LCTL set_param osc.*.rpc_stats 0
6353         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6354         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6355                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6356                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6357                 local size
6358
6359                 if [ $lines -le 20 ]; then
6360                         continue
6361                 fi
6362                 for size in 1 2 4 8; do
6363                         local rpc=$(echo "$stats" |
6364                                     awk '($1 == "'$size':") {print $2; exit; }')
6365                         [ $rpc != 0 ] &&
6366                                 error "Small $((size*4))k read IO $rpc !"
6367                 done
6368                 echo "$osc_rpc_stats check passed!"
6369         done
6370         cleanup_test101bc
6371         true
6372 }
6373 run_test 101c "check stripe_size aligned read-ahead ================="
6374
6375 set_read_ahead() {
6376         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6377         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6378 }
6379
6380 test_101d() {
6381         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6382         local file=$DIR/$tfile
6383         local sz_MB=${FILESIZE_101d:-500}
6384         local ra_MB=${READAHEAD_MB:-40}
6385
6386         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6387         [ $free_MB -lt $sz_MB ] &&
6388                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6389
6390         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6391         $SETSTRIPE -c -1 $file || error "setstripe failed"
6392
6393         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6394         echo Cancel LRU locks on lustre client to flush the client cache
6395         cancel_lru_locks osc
6396
6397         echo Disable read-ahead
6398         local old_READAHEAD=$(set_read_ahead 0)
6399
6400         echo Reading the test file $file with read-ahead disabled
6401         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6402
6403         echo Cancel LRU locks on lustre client to flush the client cache
6404         cancel_lru_locks osc
6405         echo Enable read-ahead with ${ra_MB}MB
6406         set_read_ahead $ra_MB
6407
6408         echo Reading the test file $file with read-ahead enabled
6409         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6410
6411         echo "read-ahead disabled time read $raOFF"
6412         echo "read-ahead enabled  time read $raON"
6413
6414         set_read_ahead $old_READAHEAD
6415         rm -f $file
6416         wait_delete_completed
6417
6418         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6419                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6420 }
6421 run_test 101d "file read with and without read-ahead enabled"
6422
6423 test_101e() {
6424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6425         local file=$DIR/$tfile
6426         local size_KB=500  #KB
6427         local count=100
6428         local bsize=1024
6429
6430         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6431         local need_KB=$((count * size_KB))
6432         [[ $free_KB -le $need_KB ]] &&
6433                 skip_env "Need free space $need_KB, have $free_KB" && return
6434
6435         echo "Creating $count ${size_KB}K test files"
6436         for ((i = 0; i < $count; i++)); do
6437                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6438         done
6439
6440         echo "Cancel LRU locks on lustre client to flush the client cache"
6441         cancel_lru_locks osc
6442
6443         echo "Reset readahead stats"
6444         $LCTL set_param -n llite.*.read_ahead_stats 0
6445
6446         for ((i = 0; i < $count; i++)); do
6447                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6448         done
6449
6450         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6451                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6452
6453         for ((i = 0; i < $count; i++)); do
6454                 rm -rf $file.$i 2>/dev/null
6455         done
6456
6457         #10000 means 20% reads are missing in readahead
6458         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6459 }
6460 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6461
6462 cleanup_test101f() {
6463     trap 0
6464     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6465     rm -rf $DIR/$tfile 2>/dev/null
6466 }
6467
6468 test_101f() {
6469         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6470     local file=$DIR/$tfile
6471     local nreads=1000
6472
6473     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6474     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6475     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6476     trap cleanup_test101f EXIT
6477
6478     echo Cancel LRU locks on lustre client to flush the client cache
6479     cancel_lru_locks osc
6480
6481     echo Reset readahead stats
6482     $LCTL set_param -n llite.*.read_ahead_stats 0
6483     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6484     # readahead should read in 2M file on second read, so only miss
6485     # 2 pages.
6486     echo Random 4K reads on 2M file for 1000 times
6487     $READS -f $file -s 2097152 -b 4096 -n $nreads
6488
6489     echo checking missing pages
6490     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6491           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6492
6493     [ $miss -lt 3 ] || error "misses too much pages!"
6494     cleanup_test101f
6495 }
6496 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6497
6498 setup_test102() {
6499         test_mkdir -p $DIR/$tdir
6500         chown $RUNAS_ID $DIR/$tdir
6501         STRIPE_SIZE=65536
6502         STRIPE_OFFSET=1
6503         STRIPE_COUNT=$OSTCOUNT
6504         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6505
6506         trap cleanup_test102 EXIT
6507         cd $DIR
6508         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6509         cd $DIR/$tdir
6510         for num in 1 2 3 4; do
6511                 for count in $(seq 1 $STRIPE_COUNT); do
6512                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6513                                 local size=`expr $STRIPE_SIZE \* $num`
6514                                 local file=file"$num-$idx-$count"
6515                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6516                         done
6517                 done
6518         done
6519
6520         cd $DIR
6521         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6522 }
6523
6524 cleanup_test102() {
6525         trap 0
6526         rm -f $TMP/f102.tar
6527         rm -rf $DIR/d0.sanity/d102
6528 }
6529
6530 test_102a() {
6531         local testfile=$DIR/$tfile
6532
6533         touch $testfile
6534
6535         [ "$UID" != 0 ] && skip_env "must run as root" && return
6536         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6537                 skip_env "must have user_xattr" && return
6538
6539         [ -z "$(which setfattr 2>/dev/null)" ] &&
6540                 skip_env "could not find setfattr" && return
6541
6542         echo "set/get xattr..."
6543         setfattr -n trusted.name1 -v value1 $testfile ||
6544                 error "setfattr -n trusted.name1=value1 $testfile failed"
6545         getfattr -n trusted.name1 $testfile 2> /dev/null |
6546           grep "trusted.name1=.value1" ||
6547                 error "$testfile missing trusted.name1=value1"
6548
6549         setfattr -n user.author1 -v author1 $testfile ||
6550                 error "setfattr -n user.author1=author1 $testfile failed"
6551         getfattr -n user.author1 $testfile 2> /dev/null |
6552           grep "user.author1=.author1" ||
6553                 error "$testfile missing trusted.author1=author1"
6554
6555         echo "listxattr..."
6556         setfattr -n trusted.name2 -v value2 $testfile ||
6557                 error "$testfile unable to set trusted.name2"
6558         setfattr -n trusted.name3 -v value3 $testfile ||
6559                 error "$testfile unable to set trusted.name3"
6560         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6561             grep "trusted.name" | wc -l) -eq 3 ] ||
6562                 error "$testfile missing 3 trusted.name xattrs"
6563
6564         setfattr -n user.author2 -v author2 $testfile ||
6565                 error "$testfile unable to set user.author2"
6566         setfattr -n user.author3 -v author3 $testfile ||
6567                 error "$testfile unable to set user.author3"
6568         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6569             grep "user.author" | wc -l) -eq 3 ] ||
6570                 error "$testfile missing 3 user.author xattrs"
6571
6572         echo "remove xattr..."
6573         setfattr -x trusted.name1 $testfile ||
6574                 error "$testfile error deleting trusted.name1"
6575         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6576                 error "$testfile did not delete trusted.name1 xattr"
6577
6578         setfattr -x user.author1 $testfile ||
6579                 error "$testfile error deleting user.author1"
6580         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6581                 error "$testfile did not delete trusted.name1 xattr"
6582
6583         # b10667: setting lustre special xattr be silently discarded
6584         echo "set lustre special xattr ..."
6585         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6586                 error "$testfile allowed setting trusted.lov"
6587 }
6588 run_test 102a "user xattr test =================================="
6589
6590 test_102b() {
6591         [ -z "$(which setfattr 2>/dev/null)" ] &&
6592                 skip_env "could not find setfattr" && return
6593
6594         # b10930: get/set/list trusted.lov xattr
6595         echo "get/set/list trusted.lov xattr ..."
6596         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6597         local testfile=$DIR/$tfile
6598         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6599                 error "setstripe failed"
6600         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6601                 error "getstripe failed"
6602         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6603                 error "can't get trusted.lov from $testfile"
6604
6605         local testfile2=${testfile}2
6606         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6607                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6608
6609         $MCREATE $testfile2
6610         setfattr -n trusted.lov -v $value $testfile2
6611         local stripe_size=$($GETSTRIPE -S $testfile2)
6612         local stripe_count=$($GETSTRIPE -c $testfile2)
6613         [[ $stripe_size -eq 65536 ]] ||
6614                 error "stripe size $stripe_size != 65536"
6615         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6616                 error "stripe count $stripe_count != $STRIPECOUNT"
6617         rm -f $DIR/$tfile
6618 }
6619 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6620
6621 test_102c() {
6622         [ -z "$(which setfattr 2>/dev/null)" ] &&
6623                 skip_env "could not find setfattr" && return
6624
6625         # b10930: get/set/list lustre.lov xattr
6626         echo "get/set/list lustre.lov xattr ..."
6627         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6628         test_mkdir -p $DIR/$tdir
6629         chown $RUNAS_ID $DIR/$tdir
6630         local testfile=$DIR/$tdir/$tfile
6631         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6632                 error "setstripe failed"
6633         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6634                 error "getstripe failed"
6635         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6636         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6637
6638         local testfile2=${testfile}2
6639         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6640                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6641
6642         $RUNAS $MCREATE $testfile2
6643         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6644         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6645         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6646         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6647         [ $stripe_count -eq $STRIPECOUNT ] ||
6648                 error "stripe count $stripe_count != $STRIPECOUNT"
6649 }
6650 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6651
6652 compare_stripe_info1() {
6653         local stripe_index_all_zero=true
6654
6655         for num in 1 2 3 4; do
6656                 for count in $(seq 1 $STRIPE_COUNT); do
6657                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6658                                 local size=$((STRIPE_SIZE * num))
6659                                 local file=file"$num-$offset-$count"
6660                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6661                                 [[ $stripe_size -ne $size ]] &&
6662                                     error "$file: size $stripe_size != $size"
6663                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6664                                 # allow fewer stripes to be created, ORI-601
6665                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6666                                     error "$file: count $stripe_count != $count"
6667                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6668                                 [[ $stripe_index -ne 0 ]] &&
6669                                         stripe_index_all_zero=false
6670                         done
6671                 done
6672         done
6673         $stripe_index_all_zero &&
6674                 error "all files are being extracted starting from OST index 0"
6675         return 0
6676 }
6677
6678 find_lustre_tar() {
6679         [ -n "$(which tar 2>/dev/null)" ] &&
6680                 strings $(which tar) | grep -q "lustre" && echo tar
6681 }
6682
6683 test_102d() {
6684         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6685         # b10930: tar test for trusted.lov xattr
6686         TAR=$(find_lustre_tar)
6687         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6688         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6689         setup_test102
6690         test_mkdir -p $DIR/d102d
6691         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6692         cd $DIR/d102d/$tdir
6693         compare_stripe_info1
6694 }
6695 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6696
6697 test_102f() {
6698         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6699         # b10930: tar test for trusted.lov xattr
6700         TAR=$(find_lustre_tar)
6701         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6702         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6703         setup_test102
6704         test_mkdir -p $DIR/d102f
6705         cd $DIR
6706         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6707         cd $DIR/d102f/$tdir
6708         compare_stripe_info1
6709 }
6710 run_test 102f "tar copy files, not keep osts ==========="
6711
6712 grow_xattr() {
6713         local xsize=${1:-1024}  # in bytes
6714         local file=$DIR/$tfile
6715
6716         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6717                 skip "must have user_xattr" && return 0
6718         [ -z "$(which setfattr 2>/dev/null)" ] &&
6719                 skip_env "could not find setfattr" && return 0
6720         [ -z "$(which getfattr 2>/dev/null)" ] &&
6721                 skip_env "could not find getfattr" && return 0
6722
6723         touch $file
6724
6725         local value="$(generate_string $xsize)"
6726
6727         local xbig=trusted.big
6728         log "save $xbig on $file"
6729         setfattr -n $xbig -v $value $file ||
6730                 error "saving $xbig on $file failed"
6731
6732         local orig=$(get_xattr_value $xbig $file)
6733         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6734
6735         local xsml=trusted.sml
6736         log "save $xsml on $file"
6737         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6738
6739         local new=$(get_xattr_value $xbig $file)
6740         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6741
6742         log "grow $xsml on $file"
6743         setfattr -n $xsml -v "$value" $file ||
6744                 error "growing $xsml on $file failed"
6745
6746         new=$(get_xattr_value $xbig $file)
6747         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6748         log "$xbig still valid after growing $xsml"
6749
6750         rm -f $file
6751 }
6752
6753 test_102h() { # bug 15777
6754         grow_xattr 1024
6755 }
6756 run_test 102h "grow xattr from inside inode to external block"
6757
6758 test_102ha() {
6759         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6760         grow_xattr $(max_xattr_size)
6761 }
6762 run_test 102ha "grow xattr from inside inode to external inode"
6763
6764 test_102i() { # bug 17038
6765         [ -z "$(which getfattr 2>/dev/null)" ] &&
6766                 skip "could not find getfattr" && return
6767         touch $DIR/$tfile
6768         ln -s $DIR/$tfile $DIR/${tfile}link
6769         getfattr -n trusted.lov $DIR/$tfile ||
6770                 error "lgetxattr on $DIR/$tfile failed"
6771         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6772                 grep -i "no such attr" ||
6773                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6774         rm -f $DIR/$tfile $DIR/${tfile}link
6775 }
6776 run_test 102i "lgetxattr test on symbolic link ============"
6777
6778 test_102j() {
6779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6780         TAR=$(find_lustre_tar)
6781         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6782         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6783         setup_test102 "$RUNAS"
6784         test_mkdir -p $DIR/d102j
6785         chown $RUNAS_ID $DIR/d102j
6786         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6787         cd $DIR/d102j/$tdir
6788         compare_stripe_info1 "$RUNAS"
6789 }
6790 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6791
6792 test_102k() {
6793         [ -z "$(which setfattr 2>/dev/null)" ] &&
6794                 skip "could not find setfattr" && return
6795         touch $DIR/$tfile
6796         # b22187 just check that does not crash for regular file.
6797         setfattr -n trusted.lov $DIR/$tfile
6798         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6799         local test_kdir=$DIR/d102k
6800         test_mkdir $test_kdir
6801         local default_size=`$GETSTRIPE -S $test_kdir`
6802         local default_count=`$GETSTRIPE -c $test_kdir`
6803         local default_offset=`$GETSTRIPE -i $test_kdir`
6804         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6805                 error 'dir setstripe failed'
6806         setfattr -n trusted.lov $test_kdir
6807         local stripe_size=`$GETSTRIPE -S $test_kdir`
6808         local stripe_count=`$GETSTRIPE -c $test_kdir`
6809         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6810         [ $stripe_size -eq $default_size ] ||
6811                 error "stripe size $stripe_size != $default_size"
6812         [ $stripe_count -eq $default_count ] ||
6813                 error "stripe count $stripe_count != $default_count"
6814         [ $stripe_offset -eq $default_offset ] ||
6815                 error "stripe offset $stripe_offset != $default_offset"
6816         rm -rf $DIR/$tfile $test_kdir
6817 }
6818 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6819
6820 test_102l() {
6821         [ -z "$(which getfattr 2>/dev/null)" ] &&
6822                 skip "could not find getfattr" && return
6823
6824         # LU-532 trusted. xattr is invisible to non-root
6825         local testfile=$DIR/$tfile
6826
6827         touch $testfile
6828
6829         echo "listxattr as user..."
6830         chown $RUNAS_ID $testfile
6831         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6832             grep -q "trusted" &&
6833                 error "$testfile trusted xattrs are user visible"
6834
6835         return 0;
6836 }
6837 run_test 102l "listxattr size test =================================="
6838
6839 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6840         local path=$DIR/$tfile
6841         touch $path
6842
6843         listxattr_size_check $path || error "listattr_size_check $path failed"
6844 }
6845 run_test 102m "Ensure listxattr fails on small bufffer ========"
6846
6847 cleanup_test102
6848
6849 getxattr() { # getxattr path name
6850         # Return the base64 encoding of the value of xattr name on path.
6851         local path=$1
6852         local name=$2
6853
6854         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6855         # file: $path
6856         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6857         #
6858         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6859
6860         getfattr --absolute-names --encoding=base64 --name=$name $path |
6861                 awk -F= -v name=$name '$1 == name {
6862                         print substr($0, index($0, "=") + 1);
6863         }'
6864 }
6865
6866 test_102n() { # LU-4101 mdt: protect internal xattrs
6867         local file0=$DIR/$tfile.0
6868         local file1=$DIR/$tfile.1
6869         local xattr0=$TMP/$tfile.0
6870         local xattr1=$TMP/$tfile.1
6871         local name
6872         local value
6873
6874         [ -z "$(which setfattr 2>/dev/null)" ] &&
6875                 skip "could not find setfattr" && return
6876
6877         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6878         then
6879                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6880                 return
6881         fi
6882
6883         rm -rf $file0 $file1 $xattr0 $xattr1
6884         touch $file0 $file1
6885
6886         # Get 'before' xattrs of $file1.
6887         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6888
6889         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6890                 # Try to copy xattr from $file0 to $file1.
6891                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6892
6893                 setfattr --name=trusted.$name --value="$value" $file1 ||
6894                         error "setxattr 'trusted.$name' failed"
6895
6896                 # Try to set a garbage xattr.
6897                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6898
6899                 setfattr --name=trusted.$name --value="$value" $file1 ||
6900                         error "setxattr 'trusted.$name' failed"
6901
6902                 # Try to remove the xattr from $file1. We don't care if this
6903                 # appears to succeed or fail, we just don't want there to be
6904                 # any changes or crashes.
6905                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6906         done
6907
6908         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6909         then
6910                 name="lfsck_ns"
6911                 # Try to copy xattr from $file0 to $file1.
6912                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6913
6914                 setfattr --name=trusted.$name --value="$value" $file1 ||
6915                         error "setxattr 'trusted.$name' failed"
6916
6917                 # Try to set a garbage xattr.
6918                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6919
6920                 setfattr --name=trusted.$name --value="$value" $file1 ||
6921                         error "setxattr 'trusted.$name' failed"
6922
6923                 # Try to remove the xattr from $file1. We don't care if this
6924                 # appears to succeed or fail, we just don't want there to be
6925                 # any changes or crashes.
6926                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6927         fi
6928
6929         # Get 'after' xattrs of file1.
6930         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6931
6932         if ! diff $xattr0 $xattr1; then
6933                 error "before and after xattrs of '$file1' differ"
6934         fi
6935
6936         rm -rf $file0 $file1 $xattr0 $xattr1
6937
6938         return 0
6939 }
6940 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6941
6942 test_102p() { # LU-4703 setxattr did not check ownership
6943         local testfile=$DIR/$tfile
6944
6945         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6946                 skip "MDS needs to be at least 2.5.56" && return
6947
6948         touch $testfile
6949
6950         echo "setfacl as user..."
6951         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6952         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6953
6954         echo "setfattr as user..."
6955         setfacl -m "u:$RUNAS_ID:---" $testfile
6956         $RUNAS setfattr -x system.posix_acl_access $testfile
6957         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6958 }
6959 run_test 102p "check setxattr(2) correctly fails without permission"
6960
6961 test_102q() {
6962         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
6963 }
6964 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
6965
6966 test_102r() {
6967         touch $DIR/$tfile || error "touch"
6968         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
6969         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
6970         rm $DIR/$tfile || error "rm"
6971 }
6972 run_test 102r "set EAs with empty values"
6973
6974 run_acl_subtest()
6975 {
6976     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6977     return $?
6978 }
6979
6980 test_103a() {
6981         [ "$UID" != 0 ] && skip_env "must run as root" && return
6982         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6983                 skip "must have acl enabled" && return
6984         [ -z "$(which setfacl 2>/dev/null)" ] &&
6985                 skip_env "could not find setfacl" && return
6986         $GSS && skip "could not run under gss" && return
6987
6988         gpasswd -a daemon bin                           # LU-5641
6989         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
6990
6991         declare -a identity_old
6992
6993         for num in $(seq $MDSCOUNT); do
6994                 switch_identity $num true || identity_old[$num]=$?
6995         done
6996
6997         SAVE_UMASK=$(umask)
6998         umask 0022
6999         cd $DIR
7000
7001         echo "performing cp ..."
7002         run_acl_subtest cp || error "run_acl_subtest cp failed"
7003         echo "performing getfacl-noacl..."
7004         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7005         echo "performing misc..."
7006         run_acl_subtest misc || error  "misc test failed"
7007         echo "performing permissions..."
7008         run_acl_subtest permissions || error "permissions failed"
7009         echo "performing setfacl..."
7010         run_acl_subtest setfacl || error  "setfacl test failed"
7011
7012         # inheritance test got from HP
7013         echo "performing inheritance..."
7014         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7015         chmod +x make-tree || error "chmod +x failed"
7016         run_acl_subtest inheritance || error "inheritance test failed"
7017         rm -f make-tree
7018
7019         echo "LU-974 ignore umask when acl is enabled..."
7020         run_acl_subtest 974 || error "LU-974 umask test failed"
7021         if [ $MDSCOUNT -ge 2 ]; then
7022                 run_acl_subtest 974_remote ||
7023                         error "LU-974 umask test failed under remote dir"
7024         fi
7025
7026         echo "LU-2561 newly created file is same size as directory..."
7027         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7028                 run_acl_subtest 2561 || error "LU-2561 test failed"
7029         else
7030                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7031         fi
7032
7033         run_acl_subtest 4924 || error "LU-4924 test failed"
7034
7035         cd $SAVE_PWD
7036         umask $SAVE_UMASK
7037
7038         for num in $(seq $MDSCOUNT); do
7039                 if [ "${identity_old[$num]}" = 1 ]; then
7040                         switch_identity $num false || identity_old[$num]=$?
7041                 fi
7042         done
7043 }
7044 run_test 103a "acl test ========================================="
7045
7046 test_103b() {
7047         local noacl=false
7048         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7049         local mountopts=$MDS_MOUNT_OPTS
7050
7051         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7052                 noacl=true
7053         else
7054                 # stop the MDT
7055                 stop $SINGLEMDS || error "failed to stop MDT."
7056                 # remount the MDT
7057                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7058                         MDS_MOUNT_OPTS="-o noacl"
7059                 else
7060                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7061                 fi
7062                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7063                         error "failed to start MDT."
7064                 MDS_MOUNT_OPTS=$mountopts
7065         fi
7066
7067         touch $DIR/$tfile
7068         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7069
7070         if ! $noacl; then
7071                 # stop the MDT
7072                 stop $SINGLEMDS || error "failed to stop MDT."
7073                 # remount the MDT
7074                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7075                         error "failed to start MDT."
7076         fi
7077
7078         true
7079 }
7080 run_test 103b "MDS mount option 'noacl'"
7081
7082 test_103c() {
7083         mkdir -p $DIR/$tdir
7084         cp -rp $DIR/$tdir $DIR/$tdir.bak
7085
7086         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7087                 error "$DIR/$tdir shouldn't contain default ACL"
7088         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7089                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7090         true
7091 }
7092 run_test 103c "'cp -rp' won't set empty acl"
7093
7094 test_104a() {
7095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7096         touch $DIR/$tfile
7097         lfs df || error "lfs df failed"
7098         lfs df -ih || error "lfs df -ih failed"
7099         lfs df -h $DIR || error "lfs df -h $DIR failed"
7100         lfs df -i $DIR || error "lfs df -i $DIR failed"
7101         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7102         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7103
7104         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7105         lctl --device %$OSC deactivate
7106         lfs df || error "lfs df with deactivated OSC failed"
7107         lctl --device %$OSC activate
7108         # wait the osc back to normal
7109         wait_osc_import_state client ost FULL
7110
7111         lfs df || error "lfs df with reactivated OSC failed"
7112         rm -f $DIR/$tfile
7113 }
7114 run_test 104a "lfs df [-ih] [path] test ========================="
7115
7116 test_104b() {
7117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7118         [ $RUNAS_ID -eq $UID ] &&
7119                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7120         chmod 666 /dev/obd
7121         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7122                         grep "Permission denied" | wc -l)))
7123         if [ $denied_cnt -ne 0 ]; then
7124                 error "lfs check servers test failed"
7125         fi
7126 }
7127 run_test 104b "$RUNAS lfs check servers test ===================="
7128
7129 test_105a() {
7130         # doesn't work on 2.4 kernels
7131         touch $DIR/$tfile
7132         if $(flock_is_enabled); then
7133                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7134         else
7135                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7136         fi
7137         rm -f $DIR/$tfile
7138 }
7139 run_test 105a "flock when mounted without -o flock test ========"
7140
7141 test_105b() {
7142         touch $DIR/$tfile
7143         if $(flock_is_enabled); then
7144                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7145         else
7146                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7147         fi
7148         rm -f $DIR/$tfile
7149 }
7150 run_test 105b "fcntl when mounted without -o flock test ========"
7151
7152 test_105c() {
7153         touch $DIR/$tfile
7154         if $(flock_is_enabled); then
7155                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7156         else
7157                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7158         fi
7159         rm -f $DIR/$tfile
7160 }
7161 run_test 105c "lockf when mounted without -o flock test ========"
7162
7163 test_105d() { # bug 15924
7164         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7165         test_mkdir -p $DIR/$tdir
7166         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7167         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7168         $LCTL set_param fail_loc=0x80000315
7169         flocks_test 2 $DIR/$tdir
7170 }
7171 run_test 105d "flock race (should not freeze) ========"
7172
7173 test_105e() { # bug 22660 && 22040
7174         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7175         touch $DIR/$tfile
7176         flocks_test 3 $DIR/$tfile
7177 }
7178 run_test 105e "Two conflicting flocks from same process ======="
7179
7180 test_106() { #bug 10921
7181         test_mkdir -p $DIR/$tdir
7182         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7183         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7184 }
7185 run_test 106 "attempt exec of dir followed by chown of that dir"
7186
7187 test_107() {
7188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7189         CDIR=`pwd`
7190         cd $DIR
7191
7192         local file=core
7193         rm -f $file
7194
7195         local save_pattern=$(sysctl -n kernel.core_pattern)
7196         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7197         sysctl -w kernel.core_pattern=$file
7198         sysctl -w kernel.core_uses_pid=0
7199
7200         ulimit -c unlimited
7201         sleep 60 &
7202         SLEEPPID=$!
7203
7204         sleep 1
7205
7206         kill -s 11 $SLEEPPID
7207         wait $SLEEPPID
7208         if [ -e $file ]; then
7209                 size=`stat -c%s $file`
7210                 [ $size -eq 0 ] && error "Fail to create core file $file"
7211         else
7212                 error "Fail to create core file $file"
7213         fi
7214         rm -f $file
7215         sysctl -w kernel.core_pattern=$save_pattern
7216         sysctl -w kernel.core_uses_pid=$save_uses_pid
7217         cd $CDIR
7218 }
7219 run_test 107 "Coredump on SIG"
7220
7221 test_110() {
7222         test_mkdir -p $DIR/$tdir
7223         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7224                 error "mkdir with 255 char failed"
7225         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7226                 error "mkdir with 256 char should fail, but did not"
7227         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7228                 error "create with 255 char failed"
7229         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7230                 error "create with 256 char should fail, but did not"
7231
7232         ls -l $DIR/$tdir
7233         rm -rf $DIR/$tdir
7234 }
7235 run_test 110 "filename length checking"
7236
7237 test_115() {
7238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7239         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7240                 tail -1 | cut -c11-20)
7241         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7242         echo "Starting with $OSTIO_pre threads"
7243
7244         NUMTEST=20000
7245         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7246         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7247         echo "$NUMTEST creates/unlinks"
7248         test_mkdir -p $DIR/$tdir
7249         createmany -o $DIR/$tdir/$tfile $NUMTEST
7250         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7251
7252         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7253                 tail -1 | cut -c11-20)
7254
7255         # don't return an error
7256         [ $OSTIO_post == $OSTIO_pre ] && echo \
7257             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7258             echo "This may be fine, depending on what ran before this test" &&
7259             echo "and how fast this system is." && return
7260
7261         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7262 }
7263 run_test 115 "verify dynamic thread creation===================="
7264
7265 free_min_max () {
7266         wait_delete_completed
7267         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7268         echo OST kbytes available: ${AVAIL[@]}
7269         MAXI=0; MAXV=${AVAIL[0]}
7270         MINI=0; MINV=${AVAIL[0]}
7271         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7272                 #echo OST $i: ${AVAIL[i]}kb
7273                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7274                         MAXV=${AVAIL[i]}; MAXI=$i
7275                 fi
7276                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7277                         MINV=${AVAIL[i]}; MINI=$i
7278                 fi
7279         done
7280         echo Min free space: OST $MINI: $MINV
7281         echo Max free space: OST $MAXI: $MAXV
7282 }
7283
7284 test_116a() { # was previously test_116()
7285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7286         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7287
7288         echo -n "Free space priority "
7289         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7290                 head -n1
7291         declare -a AVAIL
7292         free_min_max
7293
7294         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7295         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7296                 && return
7297         trap simple_cleanup_common EXIT
7298
7299
7300         # Check if we need to generate uneven OSTs
7301         test_mkdir -p $DIR/$tdir/OST${MINI}
7302         local FILL=$(($MINV / 4))
7303         local DIFF=$(($MAXV - $MINV))
7304         local DIFF2=$(($DIFF * 100 / $MINV))
7305
7306         local threshold=$(do_facet $SINGLEMDS \
7307                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7308         threshold=${threshold%%%}
7309         echo -n "Check for uneven OSTs: "
7310         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7311
7312         if [[ $DIFF2 -gt $threshold ]]; then
7313                 echo "ok"
7314                 echo "Don't need to fill OST$MINI"
7315         else
7316                 # generate uneven OSTs. Write 2% over the QOS threshold value
7317                 echo "no"
7318                 DIFF=$(($threshold - $DIFF2 + 2))
7319                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7320                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7321                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7322                         error "setstripe failed"
7323                 DIFF=$(($DIFF2 / 2048))
7324                 i=0
7325                 while [ $i -lt $DIFF ]; do
7326                         i=$(($i + 1))
7327                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7328                                 bs=2M count=1 2>/dev/null
7329                         echo -n .
7330                 done
7331                 echo .
7332                 sync
7333                 sleep_maxage
7334                 free_min_max
7335         fi
7336
7337         DIFF=$(($MAXV - $MINV))
7338         DIFF2=$(($DIFF * 100 / $MINV))
7339         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7340         if [[ $DIFF2 -gt $threshold ]]; then
7341                 echo "ok"
7342         else
7343                 echo "failed - QOS mode won't be used"
7344                 skip "QOS imbalance criteria not met"
7345                 simple_cleanup_common
7346                 return
7347         fi
7348
7349         MINI1=$MINI; MINV1=$MINV
7350         MAXI1=$MAXI; MAXV1=$MAXV
7351
7352         # now fill using QOS
7353         $SETSTRIPE -c 1 $DIR/$tdir
7354         FILL=$(($FILL / 200))
7355         if [ $FILL -gt 600 ]; then
7356                 FILL=600
7357         fi
7358         echo "writing $FILL files to QOS-assigned OSTs"
7359         i=0
7360         while [ $i -lt $FILL ]; do
7361                 i=$((i + 1))
7362                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7363                         count=1 2>/dev/null
7364                 echo -n .
7365         done
7366         echo "wrote $i 200k files"
7367         sync
7368         sleep_maxage
7369
7370         echo "Note: free space may not be updated, so measurements might be off"
7371         free_min_max
7372         DIFF2=$(($MAXV - $MINV))
7373         echo "free space delta: orig $DIFF final $DIFF2"
7374         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7375         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7376         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7377         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7378         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7379         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7380         [ $DIFF -gt 0 ] &&
7381                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7382
7383         # Figure out which files were written where
7384         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7385                   awk '/'$MINI1': / {print $2; exit}')
7386         echo $UUID
7387         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7388         echo "$MINC files created on smaller OST $MINI1"
7389         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7390                   awk '/'$MAXI1': / {print $2; exit}')
7391         echo $UUID
7392         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7393         echo "$MAXC files created on larger OST $MAXI1"
7394         FILL=$(($MAXC * 100 / $MINC - 100))
7395         [[ $MINC -gt 0 ]] &&
7396                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7397         [[ $MAXC -gt $MINC ]] ||
7398                 error_ignore LU-9 "stripe QOS didn't balance free space"
7399         simple_cleanup_common
7400 }
7401 run_test 116a "stripe QOS: free space balance ==================="
7402
7403 test_116b() { # LU-2093
7404         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7405 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7406         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7407                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7408         [ -z "$old_rr" ] && skip "no QOS" && return 0
7409         do_facet $SINGLEMDS lctl set_param \
7410                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7411         mkdir -p $DIR/$tdir
7412         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7413         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7414         do_facet $SINGLEMDS lctl set_param fail_loc=0
7415         rm -rf $DIR/$tdir
7416         do_facet $SINGLEMDS lctl set_param \
7417                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7418 }
7419 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7420
7421 test_117() # bug 10891
7422 {
7423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7424         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7425         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7426         lctl set_param fail_loc=0x21e
7427         > $DIR/$tfile || error "truncate failed"
7428         lctl set_param fail_loc=0
7429         echo "Truncate succeeded."
7430         rm -f $DIR/$tfile
7431 }
7432 run_test 117 "verify osd extend =========="
7433
7434 NO_SLOW_RESENDCOUNT=4
7435 export OLD_RESENDCOUNT=""
7436 set_resend_count () {
7437         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7438         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7439         lctl set_param -n $PROC_RESENDCOUNT $1
7440         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7441 }
7442
7443 # for reduce test_118* time (b=14842)
7444 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7445
7446 # Reset async IO behavior after error case
7447 reset_async() {
7448         FILE=$DIR/reset_async
7449
7450         # Ensure all OSCs are cleared
7451         $SETSTRIPE -c -1 $FILE
7452         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7453         sync
7454         rm $FILE
7455 }
7456
7457 test_118a() #bug 11710
7458 {
7459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7460         reset_async
7461
7462         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7463         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7464         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7465
7466         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7467                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7468                 return 1;
7469         fi
7470         rm -f $DIR/$tfile
7471 }
7472 run_test 118a "verify O_SYNC works =========="
7473
7474 test_118b()
7475 {
7476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7477         remote_ost_nodsh && skip "remote OST with nodsh" && return
7478
7479         reset_async
7480
7481         #define OBD_FAIL_OST_ENOENT 0x217
7482         set_nodes_failloc "$(osts_nodes)" 0x217
7483         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7484         RC=$?
7485         set_nodes_failloc "$(osts_nodes)" 0
7486         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7487         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7488                     grep -c writeback)
7489
7490         if [[ $RC -eq 0 ]]; then
7491                 error "Must return error due to dropped pages, rc=$RC"
7492                 return 1;
7493         fi
7494
7495         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7496                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7497                 return 1;
7498         fi
7499
7500         echo "Dirty pages not leaked on ENOENT"
7501
7502         # Due to the above error the OSC will issue all RPCs syncronously
7503         # until a subsequent RPC completes successfully without error.
7504         $MULTIOP $DIR/$tfile Ow4096yc
7505         rm -f $DIR/$tfile
7506
7507         return 0
7508 }
7509 run_test 118b "Reclaim dirty pages on fatal error =========="
7510
7511 test_118c()
7512 {
7513         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7514
7515         # for 118c, restore the original resend count, LU-1940
7516         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7517                                 set_resend_count $OLD_RESENDCOUNT
7518         remote_ost_nodsh && skip "remote OST with nodsh" && return
7519
7520         reset_async
7521
7522         #define OBD_FAIL_OST_EROFS               0x216
7523         set_nodes_failloc "$(osts_nodes)" 0x216
7524
7525         # multiop should block due to fsync until pages are written
7526         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7527         MULTIPID=$!
7528         sleep 1
7529
7530         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7531                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7532         fi
7533
7534         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7535                     grep -c writeback)
7536         if [[ $WRITEBACK -eq 0 ]]; then
7537                 error "No page in writeback, writeback=$WRITEBACK"
7538         fi
7539
7540         set_nodes_failloc "$(osts_nodes)" 0
7541         wait $MULTIPID
7542         RC=$?
7543         if [[ $RC -ne 0 ]]; then
7544                 error "Multiop fsync failed, rc=$RC"
7545         fi
7546
7547         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7548         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7549                     grep -c writeback)
7550         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7551                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7552         fi
7553
7554         rm -f $DIR/$tfile
7555         echo "Dirty pages flushed via fsync on EROFS"
7556         return 0
7557 }
7558 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7559
7560 # continue to use small resend count to reduce test_118* time (b=14842)
7561 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7562
7563 test_118d()
7564 {
7565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7566         remote_ost_nodsh && skip "remote OST with nodsh" && return
7567
7568         reset_async
7569
7570         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7571         set_nodes_failloc "$(osts_nodes)" 0x214
7572         # multiop should block due to fsync until pages are written
7573         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7574         MULTIPID=$!
7575         sleep 1
7576
7577         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7578                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7579         fi
7580
7581         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7582                     grep -c writeback)
7583         if [[ $WRITEBACK -eq 0 ]]; then
7584                 error "No page in writeback, writeback=$WRITEBACK"
7585         fi
7586
7587         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7588         set_nodes_failloc "$(osts_nodes)" 0
7589
7590         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7591         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7592                     grep -c writeback)
7593         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7594                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7595         fi
7596
7597         rm -f $DIR/$tfile
7598         echo "Dirty pages gaurenteed flushed via fsync"
7599         return 0
7600 }
7601 run_test 118d "Fsync validation inject a delay of the bulk =========="
7602
7603 test_118f() {
7604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7605         reset_async
7606
7607         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7608         lctl set_param fail_loc=0x8000040a
7609
7610         # Should simulate EINVAL error which is fatal
7611         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7612         RC=$?
7613         if [[ $RC -eq 0 ]]; then
7614                 error "Must return error due to dropped pages, rc=$RC"
7615         fi
7616
7617         lctl set_param fail_loc=0x0
7618
7619         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7620         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7621         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7622                     grep -c writeback)
7623         if [[ $LOCKED -ne 0 ]]; then
7624                 error "Locked pages remain in cache, locked=$LOCKED"
7625         fi
7626
7627         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7628                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7629         fi
7630
7631         rm -f $DIR/$tfile
7632         echo "No pages locked after fsync"
7633
7634         reset_async
7635         return 0
7636 }
7637 run_test 118f "Simulate unrecoverable OSC side error =========="
7638
7639 test_118g() {
7640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7641         reset_async
7642
7643         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7644         lctl set_param fail_loc=0x406
7645
7646         # simulate local -ENOMEM
7647         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7648         RC=$?
7649
7650         lctl set_param fail_loc=0
7651         if [[ $RC -eq 0 ]]; then
7652                 error "Must return error due to dropped pages, rc=$RC"
7653         fi
7654
7655         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7656         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7657         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7658                         grep -c writeback)
7659         if [[ $LOCKED -ne 0 ]]; then
7660                 error "Locked pages remain in cache, locked=$LOCKED"
7661         fi
7662
7663         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7664                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7665         fi
7666
7667         rm -f $DIR/$tfile
7668         echo "No pages locked after fsync"
7669
7670         reset_async
7671         return 0
7672 }
7673 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7674
7675 test_118h() {
7676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7677         remote_ost_nodsh && skip "remote OST with nodsh" && return
7678
7679         reset_async
7680
7681         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7682         set_nodes_failloc "$(osts_nodes)" 0x20e
7683         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7684         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7685         RC=$?
7686
7687         set_nodes_failloc "$(osts_nodes)" 0
7688         if [[ $RC -eq 0 ]]; then
7689                 error "Must return error due to dropped pages, rc=$RC"
7690         fi
7691
7692         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7693         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7694         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7695                     grep -c writeback)
7696         if [[ $LOCKED -ne 0 ]]; then
7697                 error "Locked pages remain in cache, locked=$LOCKED"
7698         fi
7699
7700         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7701                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7702         fi
7703
7704         rm -f $DIR/$tfile
7705         echo "No pages locked after fsync"
7706
7707         return 0
7708 }
7709 run_test 118h "Verify timeout in handling recoverables errors  =========="
7710
7711 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7712
7713 test_118i() {
7714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7715         remote_ost_nodsh && skip "remote OST with nodsh" && return
7716
7717         reset_async
7718
7719         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7720         set_nodes_failloc "$(osts_nodes)" 0x20e
7721
7722         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7723         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7724         PID=$!
7725         sleep 5
7726         set_nodes_failloc "$(osts_nodes)" 0
7727
7728         wait $PID
7729         RC=$?
7730         if [[ $RC -ne 0 ]]; then
7731                 error "got error, but should be not, rc=$RC"
7732         fi
7733
7734         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7735         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7736         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7737         if [[ $LOCKED -ne 0 ]]; then
7738                 error "Locked pages remain in cache, locked=$LOCKED"
7739         fi
7740
7741         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7742                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7743         fi
7744
7745         rm -f $DIR/$tfile
7746         echo "No pages locked after fsync"
7747
7748         return 0
7749 }
7750 run_test 118i "Fix error before timeout in recoverable error  =========="
7751
7752 [ "$SLOW" = "no" ] && set_resend_count 4
7753
7754 test_118j() {
7755         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7756         remote_ost_nodsh && skip "remote OST with nodsh" && return
7757
7758         reset_async
7759
7760         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7761         set_nodes_failloc "$(osts_nodes)" 0x220
7762
7763         # return -EIO from OST
7764         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7765         RC=$?
7766         set_nodes_failloc "$(osts_nodes)" 0x0
7767         if [[ $RC -eq 0 ]]; then
7768                 error "Must return error due to dropped pages, rc=$RC"
7769         fi
7770
7771         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7772         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7773         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7774         if [[ $LOCKED -ne 0 ]]; then
7775                 error "Locked pages remain in cache, locked=$LOCKED"
7776         fi
7777
7778         # in recoverable error on OST we want resend and stay until it finished
7779         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7780                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7781         fi
7782
7783         rm -f $DIR/$tfile
7784         echo "No pages locked after fsync"
7785
7786         return 0
7787 }
7788 run_test 118j "Simulate unrecoverable OST side error =========="
7789
7790 test_118k()
7791 {
7792         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7793         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7794
7795         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7796         set_nodes_failloc "$(osts_nodes)" 0x20e
7797         test_mkdir -p $DIR/$tdir
7798
7799         for ((i=0;i<10;i++)); do
7800                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7801                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7802                 SLEEPPID=$!
7803                 sleep 0.500s
7804                 kill $SLEEPPID
7805                 wait $SLEEPPID
7806         done
7807
7808         set_nodes_failloc "$(osts_nodes)" 0
7809         rm -rf $DIR/$tdir
7810 }
7811 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7812
7813 test_118l()
7814 {
7815         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7816         # LU-646
7817         test_mkdir -p $DIR/$tdir
7818         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7819         rm -rf $DIR/$tdir
7820 }
7821 run_test 118l "fsync dir ========="
7822
7823 test_118m() # LU-3066
7824 {
7825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7826         test_mkdir -p $DIR/$tdir
7827         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7828         rm -rf $DIR/$tdir
7829 }
7830 run_test 118m "fdatasync dir ========="
7831
7832 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7833
7834 test_119a() # bug 11737
7835 {
7836         BSIZE=$((512 * 1024))
7837         directio write $DIR/$tfile 0 1 $BSIZE
7838         # We ask to read two blocks, which is more than a file size.
7839         # directio will indicate an error when requested and actual
7840         # sizes aren't equeal (a normal situation in this case) and
7841         # print actual read amount.
7842         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7843         if [ "$NOB" != "$BSIZE" ]; then
7844                 error "read $NOB bytes instead of $BSIZE"
7845         fi
7846         rm -f $DIR/$tfile
7847 }
7848 run_test 119a "Short directIO read must return actual read amount"
7849
7850 test_119b() # bug 11737
7851 {
7852         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7853
7854         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7855         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7856         sync
7857         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7858                 error "direct read failed"
7859         rm -f $DIR/$tfile
7860 }
7861 run_test 119b "Sparse directIO read must return actual read amount"
7862
7863 test_119c() # bug 13099
7864 {
7865         BSIZE=1048576
7866         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7867         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7868         rm -f $DIR/$tfile
7869 }
7870 run_test 119c "Testing for direct read hitting hole"
7871
7872 test_119d() # bug 15950
7873 {
7874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7875         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7876         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7877         BSIZE=1048576
7878         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7879         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7880         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7881         lctl set_param fail_loc=0x40d
7882         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7883         pid_dio=$!
7884         sleep 1
7885         cat $DIR/$tfile > /dev/null &
7886         lctl set_param fail_loc=0
7887         pid_reads=$!
7888         wait $pid_dio
7889         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7890         sleep 2
7891         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7892         error "the read rpcs have not completed in 2s"
7893         rm -f $DIR/$tfile
7894         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7895 }
7896 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7897
7898 test_120a() {
7899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7900         test_mkdir -p $DIR/$tdir
7901         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7902                skip "no early lock cancel on server" && return 0
7903
7904         lru_resize_disable mdc
7905         lru_resize_disable osc
7906         cancel_lru_locks mdc
7907         # asynchronous object destroy at MDT could cause bl ast to client
7908         cancel_lru_locks osc
7909
7910         stat $DIR/$tdir > /dev/null
7911         can1=$(do_facet $SINGLEMDS \
7912                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7913                awk '/ldlm_cancel/ {print $2}')
7914         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7915                awk '/ldlm_bl_callback/ {print $2}')
7916         test_mkdir -c1 $DIR/$tdir/d1
7917         can2=$(do_facet $SINGLEMDS \
7918                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7919                awk '/ldlm_cancel/ {print $2}')
7920         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7921                awk '/ldlm_bl_callback/ {print $2}')
7922         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7923         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7924         lru_resize_enable mdc
7925         lru_resize_enable osc
7926 }
7927 run_test 120a "Early Lock Cancel: mkdir test"
7928
7929 test_120b() {
7930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7931         test_mkdir $DIR/$tdir
7932         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7933                skip "no early lock cancel on server" && return 0
7934         lru_resize_disable mdc
7935         lru_resize_disable osc
7936         cancel_lru_locks mdc
7937         stat $DIR/$tdir > /dev/null
7938         can1=$(do_facet $SINGLEMDS \
7939                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7940                awk '/ldlm_cancel/ {print $2}')
7941         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7942                awk '/ldlm_bl_callback/ {print $2}')
7943         touch $DIR/$tdir/f1
7944         can2=$(do_facet $SINGLEMDS \
7945                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7946                awk '/ldlm_cancel/ {print $2}')
7947         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7948                awk '/ldlm_bl_callback/ {print $2}')
7949         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7950         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7951         lru_resize_enable mdc
7952         lru_resize_enable osc
7953 }
7954 run_test 120b "Early Lock Cancel: create test"
7955
7956 test_120c() {
7957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7958         test_mkdir -c1 $DIR/$tdir
7959         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7960                skip "no early lock cancel on server" && return 0
7961         lru_resize_disable mdc
7962         lru_resize_disable osc
7963         test_mkdir -p -c1 $DIR/$tdir/d1
7964         test_mkdir -p -c1 $DIR/$tdir/d2
7965         touch $DIR/$tdir/d1/f1
7966         cancel_lru_locks mdc
7967         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7968         can1=$(do_facet $SINGLEMDS \
7969                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7970                awk '/ldlm_cancel/ {print $2}')
7971         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7972                awk '/ldlm_bl_callback/ {print $2}')
7973         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7974         can2=$(do_facet $SINGLEMDS \
7975                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7976                awk '/ldlm_cancel/ {print $2}')
7977         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7978                awk '/ldlm_bl_callback/ {print $2}')
7979         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7980         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7981         lru_resize_enable mdc
7982         lru_resize_enable osc
7983 }
7984 run_test 120c "Early Lock Cancel: link test"
7985
7986 test_120d() {
7987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7988         test_mkdir -p -c1 $DIR/$tdir
7989         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7990                skip "no early lock cancel on server" && return 0
7991         lru_resize_disable mdc
7992         lru_resize_disable osc
7993         touch $DIR/$tdir
7994         cancel_lru_locks mdc
7995         stat $DIR/$tdir > /dev/null
7996         can1=$(do_facet $SINGLEMDS \
7997                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7998                awk '/ldlm_cancel/ {print $2}')
7999         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8000                awk '/ldlm_bl_callback/ {print $2}')
8001         chmod a+x $DIR/$tdir
8002         can2=$(do_facet $SINGLEMDS \
8003                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8004                awk '/ldlm_cancel/ {print $2}')
8005         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8006                awk '/ldlm_bl_callback/ {print $2}')
8007         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8008         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8009         lru_resize_enable mdc
8010         lru_resize_enable osc
8011 }
8012 run_test 120d "Early Lock Cancel: setattr test"
8013
8014 test_120e() {
8015         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8016         test_mkdir -p -c1 $DIR/$tdir
8017         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8018                skip "no early lock cancel on server" && return 0
8019         lru_resize_disable mdc
8020         lru_resize_disable osc
8021         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8022         cancel_lru_locks mdc
8023         cancel_lru_locks osc
8024         dd if=$DIR/$tdir/f1 of=/dev/null
8025         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8026         # XXX client can not do early lock cancel of OST lock
8027         # during unlink (LU-4206), so cancel osc lock now.
8028         cancel_lru_locks osc
8029         can1=$(do_facet $SINGLEMDS \
8030                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8031                awk '/ldlm_cancel/ {print $2}')
8032         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8033                awk '/ldlm_bl_callback/ {print $2}')
8034         unlink $DIR/$tdir/f1
8035         sleep 5
8036         can2=$(do_facet $SINGLEMDS \
8037                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8038                awk '/ldlm_cancel/ {print $2}')
8039         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8040                awk '/ldlm_bl_callback/ {print $2}')
8041         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8042         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8043         lru_resize_enable mdc
8044         lru_resize_enable osc
8045 }
8046 run_test 120e "Early Lock Cancel: unlink test"
8047
8048 test_120f() {
8049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8050         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8051                skip "no early lock cancel on server" && return 0
8052         test_mkdir -p -c1 $DIR/$tdir
8053         lru_resize_disable mdc
8054         lru_resize_disable osc
8055         test_mkdir -p -c1 $DIR/$tdir/d1
8056         test_mkdir -p -c1 $DIR/$tdir/d2
8057         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8058         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8059         cancel_lru_locks mdc
8060         cancel_lru_locks osc
8061         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8062         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8063         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8064         # XXX client can not do early lock cancel of OST lock
8065         # during rename (LU-4206), so cancel osc lock now.
8066         cancel_lru_locks osc
8067         can1=$(do_facet $SINGLEMDS \
8068                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8069                awk '/ldlm_cancel/ {print $2}')
8070         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8071                awk '/ldlm_bl_callback/ {print $2}')
8072         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8073         sleep 5
8074         can2=$(do_facet $SINGLEMDS \
8075                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8076                awk '/ldlm_cancel/ {print $2}')
8077         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8078                awk '/ldlm_bl_callback/ {print $2}')
8079         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8080         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8081         lru_resize_enable mdc
8082         lru_resize_enable osc
8083 }
8084 run_test 120f "Early Lock Cancel: rename test"
8085
8086 test_120g() {
8087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8088         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8089                skip "no early lock cancel on server" && return 0
8090         lru_resize_disable mdc
8091         lru_resize_disable osc
8092         count=10000
8093         echo create $count files
8094         test_mkdir -p $DIR/$tdir
8095         cancel_lru_locks mdc
8096         cancel_lru_locks osc
8097         t0=`date +%s`
8098
8099         can0=$(do_facet $SINGLEMDS \
8100                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8101                awk '/ldlm_cancel/ {print $2}')
8102         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8103                awk '/ldlm_bl_callback/ {print $2}')
8104         createmany -o $DIR/$tdir/f $count
8105         sync
8106         can1=$(do_facet $SINGLEMDS \
8107                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8108                awk '/ldlm_cancel/ {print $2}')
8109         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8110                awk '/ldlm_bl_callback/ {print $2}')
8111         t1=$(date +%s)
8112         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8113         echo rm $count files
8114         rm -r $DIR/$tdir
8115         sync
8116         can2=$(do_facet $SINGLEMDS \
8117                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8118                awk '/ldlm_cancel/ {print $2}')
8119         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8120                awk '/ldlm_bl_callback/ {print $2}')
8121         t2=$(date +%s)
8122         echo total: $count removes in $((t2-t1))
8123         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8124         sleep 2
8125         # wait for commitment of removal
8126         lru_resize_enable mdc
8127         lru_resize_enable osc
8128 }
8129 run_test 120g "Early Lock Cancel: performance test"
8130
8131 test_121() { #bug #10589
8132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8133         rm -rf $DIR/$tfile
8134         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8135 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8136         lctl set_param fail_loc=0x310
8137         cancel_lru_locks osc > /dev/null
8138         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8139         lctl set_param fail_loc=0
8140         [[ $reads -eq $writes ]] ||
8141                 error "read $reads blocks, must be $writes blocks"
8142 }
8143 run_test 121 "read cancel race ========="
8144
8145 test_123a() { # was test 123, statahead(bug 11401)
8146         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8147         SLOWOK=0
8148         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8149             log "testing on UP system. Performance may be not as good as expected."
8150                         SLOWOK=1
8151         fi
8152
8153         rm -rf $DIR/$tdir
8154         test_mkdir -p $DIR/$tdir
8155         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8156         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8157         MULT=10
8158         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8159                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8160
8161                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8162                 lctl set_param -n llite.*.statahead_max 0
8163                 lctl get_param llite.*.statahead_max
8164                 cancel_lru_locks mdc
8165                 cancel_lru_locks osc
8166                 stime=`date +%s`
8167                 time ls -l $DIR/$tdir | wc -l
8168                 etime=`date +%s`
8169                 delta=$((etime - stime))
8170                 log "ls $i files without statahead: $delta sec"
8171                 lctl set_param llite.*.statahead_max=$max
8172
8173                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8174                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8175                 cancel_lru_locks mdc
8176                 cancel_lru_locks osc
8177                 stime=`date +%s`
8178                 time ls -l $DIR/$tdir | wc -l
8179                 etime=`date +%s`
8180                 delta_sa=$((etime - stime))
8181                 log "ls $i files with statahead: $delta_sa sec"
8182                 lctl get_param -n llite.*.statahead_stats
8183                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8184
8185                 [[ $swrong -lt $ewrong ]] &&
8186                         log "statahead was stopped, maybe too many locks held!"
8187                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8188
8189                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8190                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8191                     lctl set_param -n llite.*.statahead_max 0
8192                     lctl get_param llite.*.statahead_max
8193                     cancel_lru_locks mdc
8194                     cancel_lru_locks osc
8195                     stime=`date +%s`
8196                     time ls -l $DIR/$tdir | wc -l
8197                     etime=`date +%s`
8198                     delta=$((etime - stime))
8199                     log "ls $i files again without statahead: $delta sec"
8200                     lctl set_param llite.*.statahead_max=$max
8201                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8202                         if [  $SLOWOK -eq 0 ]; then
8203                                 error "ls $i files is slower with statahead!"
8204                         else
8205                                 log "ls $i files is slower with statahead!"
8206                         fi
8207                         break
8208                     fi
8209                 fi
8210
8211                 [ $delta -gt 20 ] && break
8212                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8213                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8214         done
8215         log "ls done"
8216
8217         stime=`date +%s`
8218         rm -r $DIR/$tdir
8219         sync
8220         etime=`date +%s`
8221         delta=$((etime - stime))
8222         log "rm -r $DIR/$tdir/: $delta seconds"
8223         log "rm done"
8224         lctl get_param -n llite.*.statahead_stats
8225 }
8226 run_test 123a "verify statahead work"
8227
8228 test_123b () { # statahead(bug 15027)
8229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8230         test_mkdir -p $DIR/$tdir
8231         createmany -o $DIR/$tdir/$tfile-%d 1000
8232
8233         cancel_lru_locks mdc
8234         cancel_lru_locks osc
8235
8236 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8237         lctl set_param fail_loc=0x80000803
8238         ls -lR $DIR/$tdir > /dev/null
8239         log "ls done"
8240         lctl set_param fail_loc=0x0
8241         lctl get_param -n llite.*.statahead_stats
8242         rm -r $DIR/$tdir
8243         sync
8244
8245 }
8246 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8247
8248 test_124a() {
8249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8250         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8251                 skip "no lru resize on server" && return 0
8252         local NR=2000
8253         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8254
8255         log "create $NR files at $DIR/$tdir"
8256         createmany -o $DIR/$tdir/f $NR ||
8257                 error "failed to create $NR files in $DIR/$tdir"
8258
8259         cancel_lru_locks mdc
8260         ls -l $DIR/$tdir > /dev/null
8261
8262         local NSDIR=""
8263         local LRU_SIZE=0
8264         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8265                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8266                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8267                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8268                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8269                         log "NSDIR=$NSDIR"
8270                         log "NS=$(basename $NSDIR)"
8271                         break
8272                 fi
8273         done
8274
8275         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8276                 skip "Not enough cached locks created!"
8277                 return 0
8278         fi
8279         log "LRU=$LRU_SIZE"
8280
8281         local SLEEP=30
8282
8283         # We know that lru resize allows one client to hold $LIMIT locks
8284         # for 10h. After that locks begin to be killed by client.
8285         local MAX_HRS=10
8286         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8287         log "LIMIT=$LIMIT"
8288         if [ $LIMIT -lt $LRU_SIZE ]; then
8289             skip "Limit is too small $LIMIT"
8290             return 0
8291         fi
8292
8293         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8294         # killing locks. Some time was spent for creating locks. This means
8295         # that up to the moment of sleep finish we must have killed some of
8296         # them (10-100 locks). This depends on how fast ther were created.
8297         # Many of them were touched in almost the same moment and thus will
8298         # be killed in groups.
8299         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8300
8301         # Use $LRU_SIZE_B here to take into account real number of locks
8302         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8303         local LRU_SIZE_B=$LRU_SIZE
8304         log "LVF=$LVF"
8305         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8306         log "OLD_LVF=$OLD_LVF"
8307         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8308
8309         # Let's make sure that we really have some margin. Client checks
8310         # cached locks every 10 sec.
8311         SLEEP=$((SLEEP+20))
8312         log "Sleep ${SLEEP} sec"
8313         local SEC=0
8314         while ((SEC<$SLEEP)); do
8315                 echo -n "..."
8316                 sleep 5
8317                 SEC=$((SEC+5))
8318                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8319                 echo -n "$LRU_SIZE"
8320         done
8321         echo ""
8322         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8323         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8324
8325         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8326                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8327                 unlinkmany $DIR/$tdir/f $NR
8328                 return
8329         }
8330
8331         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8332         log "unlink $NR files at $DIR/$tdir"
8333         unlinkmany $DIR/$tdir/f $NR
8334 }
8335 run_test 124a "lru resize ======================================="
8336
8337 get_max_pool_limit()
8338 {
8339         local limit=$($LCTL get_param \
8340                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8341         local max=0
8342         for l in $limit; do
8343                 if [[ $l -gt $max ]]; then
8344                         max=$l
8345                 fi
8346         done
8347         echo $max
8348 }
8349
8350 test_124b() {
8351         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8352         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8353                 skip "no lru resize on server" && return 0
8354
8355         LIMIT=$(get_max_pool_limit)
8356
8357         NR=$(($(default_lru_size)*20))
8358         if [[ $NR -gt $LIMIT ]]; then
8359                 log "Limit lock number by $LIMIT locks"
8360                 NR=$LIMIT
8361         fi
8362         lru_resize_disable mdc
8363         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8364                 error "failed to create $DIR/$tdir/disable_lru_resize"
8365
8366         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8367         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8368         cancel_lru_locks mdc
8369         stime=`date +%s`
8370         PID=""
8371         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8372         PID="$PID $!"
8373         sleep 2
8374         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8375         PID="$PID $!"
8376         sleep 2
8377         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8378         PID="$PID $!"
8379         wait $PID
8380         etime=`date +%s`
8381         nolruresize_delta=$((etime-stime))
8382         log "ls -la time: $nolruresize_delta seconds"
8383         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8384         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8385
8386         lru_resize_enable mdc
8387         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8388                 error "failed to create $DIR/$tdir/enable_lru_resize"
8389
8390         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8391         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8392         cancel_lru_locks mdc
8393         stime=`date +%s`
8394         PID=""
8395         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8396         PID="$PID $!"
8397         sleep 2
8398         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8399         PID="$PID $!"
8400         sleep 2
8401         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8402         PID="$PID $!"
8403         wait $PID
8404         etime=`date +%s`
8405         lruresize_delta=$((etime-stime))
8406         log "ls -la time: $lruresize_delta seconds"
8407         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8408
8409         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8410                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8411         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8412                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8413         else
8414                 log "lru resize performs the same with no lru resize"
8415         fi
8416         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8417 }
8418 run_test 124b "lru resize (performance test) ======================="
8419
8420 test_125() { # 13358
8421         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8422         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8423         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8424         test_mkdir -p $DIR/d125 || error "mkdir failed"
8425         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8426         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8427         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8428 }
8429 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8430
8431 test_126() { # bug 12829/13455
8432         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8433         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8434         $GSS && skip "must run as gss disabled" && return
8435
8436         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8437         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8438         rm -f $DIR/$tfile
8439         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8440 }
8441 run_test 126 "check that the fsgid provided by the client is taken into account"
8442
8443 test_127a() { # bug 15521
8444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8445         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8446         $LCTL set_param osc.*.stats=0
8447         FSIZE=$((2048 * 1024))
8448         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8449         cancel_lru_locks osc
8450         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8451
8452         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8453         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8454                 echo "got $COUNT $NAME"
8455                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8456                 eval $NAME=$COUNT || error "Wrong proc format"
8457
8458                 case $NAME in
8459                         read_bytes|write_bytes)
8460                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8461                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8462                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8463                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8464                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8465                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8466                                 error "sumsquare is too small: $SUMSQ"
8467                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8468                                 error "sumsquare is too big: $SUMSQ"
8469                         ;;
8470                         *) ;;
8471                 esac
8472         done < $DIR/${tfile}.tmp
8473
8474         #check that we actually got some stats
8475         [ "$read_bytes" ] || error "Missing read_bytes stats"
8476         [ "$write_bytes" ] || error "Missing write_bytes stats"
8477         [ "$read_bytes" != 0 ] || error "no read done"
8478         [ "$write_bytes" != 0 ] || error "no write done"
8479 }
8480 run_test 127a "verify the client stats are sane"
8481
8482 test_127b() { # bug LU-333
8483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8484         $LCTL set_param llite.*.stats=0
8485         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8486         # perform 2 reads and writes so MAX is different from SUM.
8487         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8488         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8489         cancel_lru_locks osc
8490         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8491         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8492
8493         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8494         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8495                 echo "got $COUNT $NAME"
8496                 eval $NAME=$COUNT || error "Wrong proc format"
8497
8498         case $NAME in
8499                 read_bytes)
8500                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8501                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8502                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8503                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8504                         ;;
8505                 write_bytes)
8506                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8507                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8508                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8509                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8510                         ;;
8511                         *) ;;
8512                 esac
8513         done < $TMP/${tfile}.tmp
8514
8515         #check that we actually got some stats
8516         [ "$read_bytes" ] || error "Missing read_bytes stats"
8517         [ "$write_bytes" ] || error "Missing write_bytes stats"
8518         [ "$read_bytes" != 0 ] || error "no read done"
8519         [ "$write_bytes" != 0 ] || error "no write done"
8520 }
8521 run_test 127b "verify the llite client stats are sane"
8522
8523 test_128() { # bug 15212
8524         touch $DIR/$tfile
8525         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8526                 find $DIR/$tfile
8527                 find $DIR/$tfile
8528         EOF
8529
8530         result=$(grep error $TMP/$tfile.log)
8531         rm -f $DIR/$tfile
8532         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8533 }
8534 run_test 128 "interactive lfs for 2 consecutive find's"
8535
8536 set_dir_limits () {
8537         local mntdev
8538         local canondev
8539         local node
8540
8541         local LDPROC=/proc/fs/ldiskfs
8542         local facets=$(get_facets MDS)
8543
8544         for facet in ${facets//,/ }; do
8545                 canondev=$(ldiskfs_canon \
8546                            *.$(convert_facet2label $facet).mntdev $facet)
8547                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8548                                                 LDPROC=/sys/fs/ldiskfs
8549                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8550         done
8551 }
8552
8553 test_129() {
8554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8555         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8556                 skip "Only applicable to ldiskfs-based MDTs"
8557                 return
8558         fi
8559         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8560         ENOSPC=28
8561         EFBIG=27
8562
8563         rm -rf $DIR/$tdir
8564         test_mkdir -p $DIR/$tdir
8565
8566         # block size of mds1
8567         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8568         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8569         local MAX=$((MDSBLOCKSIZE * 3))
8570         set_dir_limits $MAX
8571         local I=$(stat -c%s "$DIR/$tdir")
8572         local J=0
8573         local STRIPE_COUNT=1
8574         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8575         MAX=$((MAX*STRIPE_COUNT))
8576         while [[ $I -le $MAX ]]; do
8577                 $MULTIOP $DIR/$tdir/$J Oc
8578                 rc=$?
8579                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8580                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8581                 #and EFBIG for previous versions
8582                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8583                         set_dir_limits 0
8584                         echo "return code $rc received as expected"
8585                         multiop $DIR/$tdir/$J Oc ||
8586                                 error_exit "multiop failed w/o dir size limit"
8587
8588                         I=$(stat -c%s "$DIR/$tdir")
8589
8590                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8591                                         $(version_code 2.4.51) ]
8592                         then
8593                                 [[ $I -eq $MAX ]] && return 0
8594                         else
8595                                 [[ $I -gt $MAX ]] && return 0
8596                         fi
8597                         error_exit "current dir size $I, previous limit $MAX"
8598                 elif [ $rc -ne 0 ]; then
8599                         set_dir_limits 0
8600                         error_exit "return code $rc received instead of expected " \
8601                                    "$EFBIG or $ENOSPC, files in dir $I"
8602                 fi
8603                 J=$((J+1))
8604                 I=$(stat -c%s "$DIR/$tdir")
8605         done
8606
8607         set_dir_limits 0
8608         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8609 }
8610 run_test 129 "test directory size limit ========================"
8611
8612 OLDIFS="$IFS"
8613 cleanup_130() {
8614         trap 0
8615         IFS="$OLDIFS"
8616 }
8617
8618 test_130a() {
8619         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8620         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8621                 return
8622
8623         trap cleanup_130 EXIT RETURN
8624
8625         local fm_file=$DIR/$tfile
8626         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8627         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8628                 error "dd failed for $fm_file"
8629
8630         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8631         filefrag -ves $fm_file
8632         RC=$?
8633         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8634                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8635         [ $RC != 0 ] && error "filefrag $fm_file failed"
8636
8637         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8638                       grep -v "ext:" | grep -v "found")
8639         lun=$($GETSTRIPE -i $fm_file)
8640
8641         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8642         IFS=$'\n'
8643         tot_len=0
8644         for line in $filefrag_op
8645         do
8646                 frag_lun=`echo $line | cut -d: -f5`
8647                 ext_len=`echo $line | cut -d: -f4`
8648                 if (( $frag_lun != $lun )); then
8649                         cleanup_130
8650                         error "FIEMAP on 1-stripe file($fm_file) failed"
8651                         return
8652                 fi
8653                 (( tot_len += ext_len ))
8654         done
8655
8656         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8657                 cleanup_130
8658                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8659                 return
8660         fi
8661
8662         cleanup_130
8663
8664         echo "FIEMAP on single striped file succeeded"
8665 }
8666 run_test 130a "FIEMAP (1-stripe file)"
8667
8668 test_130b() {
8669         [ "$OSTCOUNT" -lt "2" ] &&
8670                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8671
8672         [ "$OSTCOUNT" -ge "10" ] &&
8673                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8674
8675         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8676         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8677                 return
8678
8679         trap cleanup_130 EXIT RETURN
8680
8681         local fm_file=$DIR/$tfile
8682         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8683         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8684                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8685
8686         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8687                 error "dd failed on $fm_file"
8688
8689         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8690         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8691                       grep -v "ext:" | grep -v "found")
8692
8693         last_lun=$(echo $filefrag_op | cut -d: -f5)
8694
8695         IFS=$'\n'
8696         tot_len=0
8697         num_luns=1
8698         for line in $filefrag_op
8699         do
8700                 frag_lun=`echo $line | cut -d: -f5`
8701                 ext_len=`echo $line | cut -d: -f4`
8702                 if (( $frag_lun != $last_lun )); then
8703                         if (( tot_len != 1024 )); then
8704                                 cleanup_130
8705                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8706                                 return
8707                         else
8708                                 (( num_luns += 1 ))
8709                                 tot_len=0
8710                         fi
8711                 fi
8712                 (( tot_len += ext_len ))
8713                 last_lun=$frag_lun
8714         done
8715         if (( num_luns != 2 || tot_len != 1024 )); then
8716                 cleanup_130
8717                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8718                 return
8719         fi
8720
8721         cleanup_130
8722
8723         echo "FIEMAP on 2-stripe file succeeded"
8724 }
8725 run_test 130b "FIEMAP (2-stripe file)"
8726
8727 test_130c() {
8728         [ "$OSTCOUNT" -lt "2" ] &&
8729                 skip_env "skipping FIEMAP on 2-stripe file" && return
8730
8731         [ "$OSTCOUNT" -ge "10" ] &&
8732                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8733
8734         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8735         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8736                 return
8737
8738         trap cleanup_130 EXIT RETURN
8739
8740         local fm_file=$DIR/$tfile
8741         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8742         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8743                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8744
8745         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8746
8747         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8748         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8749
8750         last_lun=`echo $filefrag_op | cut -d: -f5`
8751
8752         IFS=$'\n'
8753         tot_len=0
8754         num_luns=1
8755         for line in $filefrag_op
8756         do
8757                 frag_lun=`echo $line | cut -d: -f5`
8758                 ext_len=`echo $line | cut -d: -f4`
8759                 if (( $frag_lun != $last_lun )); then
8760                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8761                         if (( logical != 512 )); then
8762                                 cleanup_130
8763                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8764                                 return
8765                         fi
8766                         if (( tot_len != 512 )); then
8767                                 cleanup_130
8768                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8769                                 return
8770                         else
8771                                 (( num_luns += 1 ))
8772                                 tot_len=0
8773                         fi
8774                 fi
8775                 (( tot_len += ext_len ))
8776                 last_lun=$frag_lun
8777         done
8778         if (( num_luns != 2 || tot_len != 512 )); then
8779                 cleanup_130
8780                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8781                 return
8782         fi
8783
8784         cleanup_130
8785
8786         echo "FIEMAP on 2-stripe file with hole succeeded"
8787 }
8788 run_test 130c "FIEMAP (2-stripe file with hole)"
8789
8790 test_130d() {
8791         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8792
8793         [ "$OSTCOUNT" -ge "10" ] &&
8794                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8795
8796         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8797         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8798
8799         trap cleanup_130 EXIT RETURN
8800
8801         local fm_file=$DIR/$tfile
8802         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8803         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8804                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8805
8806         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8807         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8808                 error "dd failed on $fm_file"
8809
8810         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8811         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8812                 grep -v "ext:" | grep -v "found"`
8813
8814         last_lun=`echo $filefrag_op | cut -d: -f5`
8815
8816         IFS=$'\n'
8817         tot_len=0
8818         num_luns=1
8819         for line in $filefrag_op
8820         do
8821                 frag_lun=`echo $line | cut -d: -f5`
8822                 ext_len=`echo $line | cut -d: -f4`
8823                 if (( $frag_lun != $last_lun )); then
8824                         if (( tot_len != 1024 )); then
8825                                 cleanup_130
8826                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8827                                 return
8828                         else
8829                                 (( num_luns += 1 ))
8830                                 tot_len=0
8831                         fi
8832                 fi
8833                 (( tot_len += ext_len ))
8834                 last_lun=$frag_lun
8835         done
8836         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8837                 cleanup_130
8838                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8839                 return
8840         fi
8841
8842         cleanup_130
8843
8844         echo "FIEMAP on N-stripe file succeeded"
8845 }
8846 run_test 130d "FIEMAP (N-stripe file)"
8847
8848 test_130e() {
8849         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8850
8851         [ "$OSTCOUNT" -ge "10" ] &&
8852                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8853
8854         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8855         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8856
8857         trap cleanup_130 EXIT RETURN
8858
8859         local fm_file=$DIR/$tfile
8860         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8861         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8862                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8863
8864         NUM_BLKS=512
8865         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8866         for ((i = 0; i < $NUM_BLKS; i++))
8867         do
8868                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8869         done
8870
8871         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8872         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8873
8874         last_lun=`echo $filefrag_op | cut -d: -f5`
8875
8876         IFS=$'\n'
8877         tot_len=0
8878         num_luns=1
8879         for line in $filefrag_op
8880         do
8881                 frag_lun=`echo $line | cut -d: -f5`
8882                 ext_len=`echo $line | cut -d: -f4`
8883                 if (( $frag_lun != $last_lun )); then
8884                         if (( tot_len != $EXPECTED_LEN )); then
8885                                 cleanup_130
8886                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8887                                 return
8888                         else
8889                                 (( num_luns += 1 ))
8890                                 tot_len=0
8891                         fi
8892                 fi
8893                 (( tot_len += ext_len ))
8894                 last_lun=$frag_lun
8895         done
8896         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8897                 cleanup_130
8898                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8899                 return
8900         fi
8901
8902         cleanup_130
8903
8904         echo "FIEMAP with continuation calls succeeded"
8905 }
8906 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8907
8908 # Test for writev/readv
8909 test_131a() {
8910         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8911         error "writev test failed"
8912         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8913         error "readv failed"
8914         rm -f $DIR/$tfile
8915 }
8916 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8917
8918 test_131b() {
8919         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8920         error "append writev test failed"
8921         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8922         error "append writev test failed"
8923         rm -f $DIR/$tfile
8924 }
8925 run_test 131b "test append writev"
8926
8927 test_131c() {
8928         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8929         error "NOT PASS"
8930 }
8931 run_test 131c "test read/write on file w/o objects"
8932
8933 test_131d() {
8934         rwv -f $DIR/$tfile -w -n 1 1572864
8935         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8936         if [ "$NOB" != 1572864 ]; then
8937                 error "Short read filed: read $NOB bytes instead of 1572864"
8938         fi
8939         rm -f $DIR/$tfile
8940 }
8941 run_test 131d "test short read"
8942
8943 test_131e() {
8944         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8945         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8946         error "read hitting hole failed"
8947         rm -f $DIR/$tfile
8948 }
8949 run_test 131e "test read hitting hole"
8950
8951 get_ost_param() {
8952         local token=$1
8953         local gl_sum=0
8954         for node in $(osts_nodes); do
8955                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8956                 [ x$gl = x"" ] && gl=0
8957                 gl_sum=$((gl_sum + gl))
8958         done
8959         echo $gl_sum
8960 }
8961
8962 som_mode_switch() {
8963         local som=$1
8964         local gl1=$2
8965         local gl2=$3
8966
8967         if [ x$som = x"enabled" ]; then
8968                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8969                 MOUNT_OPTS=`echo $MOUNT_OPTS |
8970                     sed 's/som_preview,\|,som_preview\|som_preview//g'`
8971                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8972         else
8973                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8974                 MOUNT_OPTS="${MOUNT_OPTS:+$MOUNT_OPTS,}som_preview"
8975                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8976         fi
8977
8978         # do remount to make new mount-conf parameters actual
8979         echo remounting...
8980         sync
8981         stopall
8982         setupall
8983 }
8984
8985 test_132() { #1028, SOM
8986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8987         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8988         local mdtidx=$($LFS getstripe -M $DIR)
8989         local facet=mds$((mdtidx + 1))
8990
8991         local MOUNTOPT_SAVE=$MOUNTOPT
8992
8993         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8994         cancel_lru_locks osc
8995         som1=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8996                awk -F= ' {print $2}' | head -n 1)
8997
8998         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8999         stat $DIR/$tfile >/dev/null
9000         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
9001         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
9002         rm $DIR/$tfile
9003         som_mode_switch $som1 $gl1 $gl2
9004
9005         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
9006         cancel_lru_locks osc
9007
9008         som2=$(do_facet $facet "$LCTL get_param mdt.*.som" |
9009                awk -F= ' {print $2}' | head -n 1)
9010         if [ $som1 == $som2 ]; then
9011             error "som is still "$som2
9012             if [ x$som2 = x"enabled" ]; then
9013                 som2="disabled"
9014             else
9015                 som2="enabled"
9016             fi
9017         fi
9018
9019         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
9020         stat $DIR/$tfile >/dev/null
9021         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
9022         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
9023         som_mode_switch $som2 $gl1 $gl2
9024         MOUNT_OPTS=$MOUNT_OPTS_SAVE
9025 }
9026 run_test 132 "som avoids glimpse rpc"
9027
9028 check_stats() {
9029         local res
9030         local count
9031         case $1 in
9032         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9033                  ;;
9034         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9035                  ;;
9036         *) error "Wrong argument $1" ;;
9037         esac
9038         echo $res
9039         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9040         # if the argument $3 is zero, it means any stat increment is ok.
9041         if [[ $3 -gt 0 ]]; then
9042                 count=$(echo $res | awk '{ print $2 }')
9043                 [[ $count -ne $3 ]] &&
9044                         error "The $2 counter on $1 is wrong - expected $3"
9045         fi
9046 }
9047
9048 test_133a() {
9049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9050         remote_ost_nodsh && skip "remote OST with nodsh" && return
9051         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9052
9053         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9054                 { skip "MDS doesn't support rename stats"; return; }
9055         local testdir=$DIR/${tdir}/stats_testdir
9056         mkdir -p $DIR/${tdir}
9057
9058         # clear stats.
9059         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9060         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9061
9062         # verify mdt stats first.
9063         mkdir ${testdir} || error "mkdir failed"
9064         check_stats $SINGLEMDS "mkdir" 1
9065         touch ${testdir}/${tfile} || "touch failed"
9066         check_stats $SINGLEMDS "open" 1
9067         check_stats $SINGLEMDS "close" 1
9068         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9069         check_stats $SINGLEMDS "mknod" 1
9070         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9071         check_stats $SINGLEMDS "unlink" 1
9072         rm -f ${testdir}/${tfile} || error "file remove failed"
9073         check_stats $SINGLEMDS "unlink" 2
9074
9075         # remove working dir and check mdt stats again.
9076         rmdir ${testdir} || error "rmdir failed"
9077         check_stats $SINGLEMDS "rmdir" 1
9078
9079         local testdir1=$DIR/${tdir}/stats_testdir1
9080         mkdir -p ${testdir}
9081         mkdir -p ${testdir1}
9082         touch ${testdir1}/test1
9083         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9084         check_stats $SINGLEMDS "crossdir_rename" 1
9085
9086         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9087         check_stats $SINGLEMDS "samedir_rename" 1
9088
9089         rm -rf $DIR/${tdir}
9090 }
9091 run_test 133a "Verifying MDT stats ========================================"
9092
9093 test_133b() {
9094         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9095         remote_ost_nodsh && skip "remote OST with nodsh" && return
9096         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9097         local testdir=$DIR/${tdir}/stats_testdir
9098         mkdir -p ${testdir} || error "mkdir failed"
9099         touch ${testdir}/${tfile} || "touch failed"
9100         cancel_lru_locks mdc
9101
9102         # clear stats.
9103         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9104         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9105
9106         # extra mdt stats verification.
9107         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9108         check_stats $SINGLEMDS "setattr" 1
9109         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9110         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9111         then            # LU-1740
9112                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9113                 check_stats $SINGLEMDS "getattr" 1
9114         fi
9115         $LFS df || error "lfs failed"
9116         check_stats $SINGLEMDS "statfs" 1
9117
9118         rm -rf $DIR/${tdir}
9119 }
9120 run_test 133b "Verifying extra MDT stats =================================="
9121
9122 test_133c() {
9123         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9124         remote_ost_nodsh && skip "remote OST with nodsh" && return
9125         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9126         local testdir=$DIR/${tdir}/stats_testdir
9127         test_mkdir -p ${testdir} || error "mkdir failed"
9128
9129         # verify obdfilter stats.
9130         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9131         sync
9132         cancel_lru_locks osc
9133         wait_delete_completed
9134
9135         # clear stats.
9136         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9137         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9138
9139         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9140         sync
9141         cancel_lru_locks osc
9142         check_stats ost "write" 1
9143
9144         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9145         check_stats ost "read" 1
9146
9147         > ${testdir}/${tfile} || error "truncate failed"
9148         check_stats ost "punch" 1
9149
9150         rm -f ${testdir}/${tfile} || error "file remove failed"
9151         wait_delete_completed
9152         check_stats ost "destroy" 1
9153
9154         rm -rf $DIR/${tdir}
9155 }
9156 run_test 133c "Verifying OST stats ========================================"
9157
9158 order_2() {
9159         local value=$1
9160         local orig=$value
9161         local order=1
9162
9163         while [ $value -ge 2 ]; do
9164                 order=$((order*2))
9165                 value=$((value/2))
9166         done
9167
9168         if [ $orig -gt $order ]; then
9169                 order=$((order*2))
9170         fi
9171         echo $order
9172 }
9173
9174 size_in_KMGT() {
9175     local value=$1
9176     local size=('K' 'M' 'G' 'T');
9177     local i=0
9178     local size_string=$value
9179
9180     while [ $value -ge 1024 ]; do
9181         if [ $i -gt 3 ]; then
9182             #T is the biggest unit we get here, if that is bigger,
9183             #just return XXXT
9184             size_string=${value}T
9185             break
9186         fi
9187         value=$((value >> 10))
9188         if [ $value -lt 1024 ]; then
9189             size_string=${value}${size[$i]}
9190             break
9191         fi
9192         i=$((i + 1))
9193     done
9194
9195     echo $size_string
9196 }
9197
9198 get_rename_size() {
9199     local size=$1
9200     local context=${2:-.}
9201     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9202                 grep -A1 $context |
9203                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9204     echo $sample
9205 }
9206
9207 test_133d() {
9208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9209         remote_ost_nodsh && skip "remote OST with nodsh" && return
9210         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9211         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9212         { skip "MDS doesn't support rename stats"; return; }
9213
9214         local testdir1=$DIR/${tdir}/stats_testdir1
9215         local testdir2=$DIR/${tdir}/stats_testdir2
9216
9217         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9218
9219         mkdir -p ${testdir1} || error "mkdir failed"
9220         mkdir -p ${testdir2} || error "mkdir failed"
9221
9222         createmany -o $testdir1/test 512 || error "createmany failed"
9223
9224         # check samedir rename size
9225         mv ${testdir1}/test0 ${testdir1}/test_0
9226
9227         local testdir1_size=$(ls -l $DIR/${tdir} |
9228                 awk '/stats_testdir1/ {print $5}')
9229         local testdir2_size=$(ls -l $DIR/${tdir} |
9230                 awk '/stats_testdir2/ {print $5}')
9231
9232         testdir1_size=$(order_2 $testdir1_size)
9233         testdir2_size=$(order_2 $testdir2_size)
9234
9235         testdir1_size=$(size_in_KMGT $testdir1_size)
9236         testdir2_size=$(size_in_KMGT $testdir2_size)
9237
9238         echo "source rename dir size: ${testdir1_size}"
9239         echo "target rename dir size: ${testdir2_size}"
9240
9241         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9242         eval $cmd || error "$cmd failed"
9243         local samedir=$($cmd | grep 'same_dir')
9244         local same_sample=$(get_rename_size $testdir1_size)
9245         [ -z "$samedir" ] && error "samedir_rename_size count error"
9246         [[ $same_sample -eq 1 ]] ||
9247                 error "samedir_rename_size error $same_sample"
9248         echo "Check same dir rename stats success"
9249
9250         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9251
9252         # check crossdir rename size
9253         mv ${testdir1}/test_0 ${testdir2}/test_0
9254
9255         testdir1_size=$(ls -l $DIR/${tdir} |
9256                 awk '/stats_testdir1/ {print $5}')
9257         testdir2_size=$(ls -l $DIR/${tdir} |
9258                 awk '/stats_testdir2/ {print $5}')
9259
9260         testdir1_size=$(order_2 $testdir1_size)
9261         testdir2_size=$(order_2 $testdir2_size)
9262
9263         testdir1_size=$(size_in_KMGT $testdir1_size)
9264         testdir2_size=$(size_in_KMGT $testdir2_size)
9265
9266         echo "source rename dir size: ${testdir1_size}"
9267         echo "target rename dir size: ${testdir2_size}"
9268
9269         eval $cmd || error "$cmd failed"
9270         local crossdir=$($cmd | grep 'crossdir')
9271         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9272         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9273         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9274         [[ $src_sample -eq 1 ]] ||
9275                 error "crossdir_rename_size error $src_sample"
9276         [[ $tgt_sample -eq 1 ]] ||
9277                 error "crossdir_rename_size error $tgt_sample"
9278         echo "Check cross dir rename stats success"
9279         rm -rf $DIR/${tdir}
9280 }
9281 run_test 133d "Verifying rename_stats ========================================"
9282
9283 test_133e() {
9284         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9285         remote_ost_nodsh && skip "remote OST with nodsh" && return
9286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9287         local testdir=$DIR/${tdir}/stats_testdir
9288         local ctr f0 f1 bs=32768 count=42 sum
9289
9290         mkdir -p ${testdir} || error "mkdir failed"
9291
9292         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9293
9294         for ctr in {write,read}_bytes; do
9295                 sync
9296                 cancel_lru_locks osc
9297
9298                 do_facet ost1 $LCTL set_param -n \
9299                         "obdfilter.*.exports.clear=clear"
9300
9301                 if [ $ctr = write_bytes ]; then
9302                         f0=/dev/zero
9303                         f1=${testdir}/${tfile}
9304                 else
9305                         f0=${testdir}/${tfile}
9306                         f1=/dev/null
9307                 fi
9308
9309                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9310                         error "dd failed"
9311                 sync
9312                 cancel_lru_locks osc
9313
9314                 sum=$(do_facet ost1 $LCTL get_param \
9315                         "obdfilter.*.exports.*.stats" |
9316                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9317                                 $1 == ctr { sum += $7 }
9318                                 END { printf("%0.0f", sum) }')
9319
9320                 if ((sum != bs * count)); then
9321                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9322                 fi
9323         done
9324
9325         rm -rf $DIR/${tdir}
9326 }
9327 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9328
9329 test_133f() {
9330         local proc_dirs
9331
9332         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9333 /sys/fs/lustre/ /sys/fs/lnet/"
9334         local dir
9335         for dir in $dirs; do
9336                 if [ -d $dir ]; then
9337                         proc_dirs="$proc_dirs $dir"
9338                 fi
9339         done
9340
9341         local facet
9342
9343         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9344         remote_ost_nodsh && skip "remote OST with nodsh" && return
9345         # First without trusting modes.
9346         find $proc_dirs -exec cat '{}' \; &> /dev/null
9347
9348         # Second verifying readability.
9349         find $proc_dirs \
9350                 -type f \
9351                 -exec cat '{}' \; &> /dev/null ||
9352                         error "proc file read failed"
9353
9354         for facet in $SINGLEMDS ost1; do
9355                 do_facet $facet find $proc_dirs \
9356                         ! -name req_history \
9357                         -exec cat '{}' \\\; &> /dev/null
9358
9359                 do_facet $facet find $proc_dirs \
9360                         ! -name req_history \
9361                         -type f \
9362                         -exec cat '{}' \\\; &> /dev/null ||
9363                                 error "proc file read failed"
9364         done
9365 }
9366 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9367
9368 test_133g() {
9369         local proc_dirs
9370
9371         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9372 /sys/fs/lustre/ /sys/fs/lnet/"
9373         local dir
9374         for dir in $dirs; do
9375                 if [ -d $dir ]; then
9376                         proc_dirs="$proc_dirs $dir"
9377                 fi
9378         done
9379
9380         local facet
9381
9382         # Second verifying readability.
9383         find $proc_dirs \
9384                 -type f \
9385                 -not -name force_lbug \
9386                 -not -name changelog_mask \
9387                 -exec badarea_io '{}' \; &> /dev/null ||
9388                 error "find $proc_dirs failed"
9389
9390         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9391                 skip "Too old lustre on MDS" && return
9392
9393         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9394                 skip "Too old lustre on ost1" && return
9395
9396         for facet in $SINGLEMDS ost1; do
9397                 do_facet $facet find $proc_dirs \
9398                         -type f \
9399                         -not -name force_lbug \
9400                         -not -name changelog_mask \
9401                         -exec badarea_io '{}' \\\; &> /dev/null ||
9402                 error "$facet find $proc_dirs failed"
9403
9404         done
9405
9406         # remount the FS in case writes/reads /proc break the FS
9407         cleanup || error "failed to unmount"
9408         setup || error "failed to setup"
9409         true
9410 }
9411 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9412
9413 test_140() { #bug-17379
9414         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9415         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9416         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9417         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9418
9419         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9420         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9421         local i=0
9422         while i=`expr $i + 1`; do
9423                 test_mkdir -p $i || error "Creating dir $i"
9424                 cd $i || error "Changing to $i"
9425                 ln -s ../stat stat || error "Creating stat symlink"
9426                 # Read the symlink until ELOOP present,
9427                 # not LBUGing the system is considered success,
9428                 # we didn't overrun the stack.
9429                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9430                 [ $ret -ne 0 ] && {
9431                         if [ $ret -eq 40 ]; then
9432                                 break  # -ELOOP
9433                         else
9434                                 error "Open stat symlink"
9435                                 return
9436                         fi
9437                 }
9438         done
9439         i=`expr $i - 1`
9440         echo "The symlink depth = $i"
9441         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9442                                         error "Invalid symlink depth"
9443
9444         # Test recursive symlink
9445         ln -s symlink_self symlink_self
9446         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9447         echo "open symlink_self returns $ret"
9448         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9449 }
9450 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9451
9452 test_150() {
9453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9454         local TF="$TMP/$tfile"
9455
9456         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9457         cp $TF $DIR/$tfile
9458         cancel_lru_locks osc
9459         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9460         remount_client $MOUNT
9461         df -P $MOUNT
9462         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9463
9464         $TRUNCATE $TF 6000
9465         $TRUNCATE $DIR/$tfile 6000
9466         cancel_lru_locks osc
9467         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9468
9469         echo "12345" >>$TF
9470         echo "12345" >>$DIR/$tfile
9471         cancel_lru_locks osc
9472         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9473
9474         echo "12345" >>$TF
9475         echo "12345" >>$DIR/$tfile
9476         cancel_lru_locks osc
9477         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9478
9479         rm -f $TF
9480         true
9481 }
9482 run_test 150 "truncate/append tests"
9483
9484 #LU-2902 roc_hit was not able to read all values from lproc
9485 function roc_hit_init() {
9486         local list=$(comma_list $(osts_nodes))
9487         local dir=$DIR/$tdir-check
9488         local file=$dir/file
9489         local BEFORE
9490         local AFTER
9491         local idx
9492
9493         test_mkdir -p $dir
9494         #use setstripe to do a write to every ost
9495         for i in $(seq 0 $((OSTCOUNT-1))); do
9496                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9497                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9498                 idx=$(printf %04x $i)
9499                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9500                         awk '$1 == "cache_access" {sum += $7}
9501                                 END { printf("%0.0f", sum) }')
9502
9503                 cancel_lru_locks osc
9504                 cat $file >/dev/null
9505
9506                 AFTER=$(get_osd_param $list *OST*$idx stats |
9507                         awk '$1 == "cache_access" {sum += $7}
9508                                 END { printf("%0.0f", sum) }')
9509
9510                 echo BEFORE:$BEFORE AFTER:$AFTER
9511                 if ! let "AFTER - BEFORE == 4"; then
9512                         rm -rf $dir
9513                         error "roc_hit is not safe to use"
9514                 fi
9515                 rm $file
9516         done
9517
9518         rm -rf $dir
9519 }
9520
9521 function roc_hit() {
9522         local list=$(comma_list $(osts_nodes))
9523         echo $(get_osd_param $list '' stats |
9524                 awk '$1 == "cache_hit" {sum += $7}
9525                         END { printf("%0.0f", sum) }')
9526 }
9527
9528 function set_cache() {
9529         local on=1
9530
9531         if [ "$2" == "off" ]; then
9532                 on=0;
9533         fi
9534         local list=$(comma_list $(osts_nodes))
9535         set_osd_param $list '' $1_cache_enable $on
9536
9537         cancel_lru_locks osc
9538 }
9539
9540 test_151() {
9541         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9542         remote_ost_nodsh && skip "remote OST with nodsh" && return
9543
9544         local CPAGES=3
9545         local list=$(comma_list $(osts_nodes))
9546
9547         # check whether obdfilter is cache capable at all
9548         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9549                 echo "not cache-capable obdfilter"
9550                 return 0
9551         fi
9552
9553         # check cache is enabled on all obdfilters
9554         if get_osd_param $list '' read_cache_enable | grep 0; then
9555                 echo "oss cache is disabled"
9556                 return 0
9557         fi
9558
9559         set_osd_param $list '' writethrough_cache_enable 1
9560
9561         # check write cache is enabled on all obdfilters
9562         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9563                 echo "oss write cache is NOT enabled"
9564                 return 0
9565         fi
9566
9567         roc_hit_init
9568
9569         #define OBD_FAIL_OBD_NO_LRU  0x609
9570         do_nodes $list $LCTL set_param fail_loc=0x609
9571
9572         # pages should be in the case right after write
9573         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9574                 error "dd failed"
9575
9576         local BEFORE=$(roc_hit)
9577         cancel_lru_locks osc
9578         cat $DIR/$tfile >/dev/null
9579         local AFTER=$(roc_hit)
9580
9581         do_nodes $list $LCTL set_param fail_loc=0
9582
9583         if ! let "AFTER - BEFORE == CPAGES"; then
9584                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9585         fi
9586
9587         # the following read invalidates the cache
9588         cancel_lru_locks osc
9589         set_osd_param $list '' read_cache_enable 0
9590         cat $DIR/$tfile >/dev/null
9591
9592         # now data shouldn't be found in the cache
9593         BEFORE=$(roc_hit)
9594         cancel_lru_locks osc
9595         cat $DIR/$tfile >/dev/null
9596         AFTER=$(roc_hit)
9597         if let "AFTER - BEFORE != 0"; then
9598                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9599         fi
9600
9601         set_osd_param $list '' read_cache_enable 1
9602         rm -f $DIR/$tfile
9603 }
9604 run_test 151 "test cache on oss and controls ==============================="
9605
9606 test_152() {
9607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9608         local TF="$TMP/$tfile"
9609
9610         # simulate ENOMEM during write
9611 #define OBD_FAIL_OST_NOMEM      0x226
9612         lctl set_param fail_loc=0x80000226
9613         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9614         cp $TF $DIR/$tfile
9615         sync || error "sync failed"
9616         lctl set_param fail_loc=0
9617
9618         # discard client's cache
9619         cancel_lru_locks osc
9620
9621         # simulate ENOMEM during read
9622         lctl set_param fail_loc=0x80000226
9623         cmp $TF $DIR/$tfile || error "cmp failed"
9624         lctl set_param fail_loc=0
9625
9626         rm -f $TF
9627 }
9628 run_test 152 "test read/write with enomem ============================"
9629
9630 test_153() {
9631         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9632 }
9633 run_test 153 "test if fdatasync does not crash ======================="
9634
9635 dot_lustre_fid_permission_check() {
9636         local fid=$1
9637         local ffid=$MOUNT/.lustre/fid/$fid
9638         local test_dir=$2
9639
9640         echo "stat fid $fid"
9641         stat $ffid > /dev/null || error "stat $ffid failed."
9642         echo "touch fid $fid"
9643         touch $ffid || error "touch $ffid failed."
9644         echo "write to fid $fid"
9645         cat /etc/hosts > $ffid || error "write $ffid failed."
9646         echo "read fid $fid"
9647         diff /etc/hosts $ffid || error "read $ffid failed."
9648         echo "append write to fid $fid"
9649         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9650         echo "rename fid $fid"
9651         mv $ffid $test_dir/$tfile.1 &&
9652                 error "rename $ffid to $tfile.1 should fail."
9653         touch $test_dir/$tfile.1
9654         mv $test_dir/$tfile.1 $ffid &&
9655                 error "rename $tfile.1 to $ffid should fail."
9656         rm -f $test_dir/$tfile.1
9657         echo "truncate fid $fid"
9658         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9659         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9660                 echo "link fid $fid"
9661                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9662         fi
9663         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9664                 echo "setfacl fid $fid"
9665                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9666                 echo "getfacl fid $fid"
9667                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9668         fi
9669         echo "unlink fid $fid"
9670         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9671         echo "mknod fid $fid"
9672         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9673
9674         fid=[0xf00000400:0x1:0x0]
9675         ffid=$MOUNT/.lustre/fid/$fid
9676
9677         echo "stat non-exist fid $fid"
9678         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9679         echo "write to non-exist fid $fid"
9680         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9681         echo "link new fid $fid"
9682         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9683
9684         mkdir -p $test_dir/$tdir
9685         touch $test_dir/$tdir/$tfile
9686         fid=$($LFS path2fid $test_dir/$tdir)
9687         rc=$?
9688         [ $rc -ne 0 ] &&
9689                 error "error: could not get fid for $test_dir/$dir/$tfile."
9690
9691         ffid=$MOUNT/.lustre/fid/$fid
9692
9693         echo "ls $fid"
9694         ls $ffid > /dev/null || error "ls $ffid failed."
9695         echo "touch $fid/$tfile.1"
9696         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9697
9698         echo "touch $MOUNT/.lustre/fid/$tfile"
9699         touch $MOUNT/.lustre/fid/$tfile && \
9700                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9701
9702         echo "setxattr to $MOUNT/.lustre/fid"
9703         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9704
9705         echo "listxattr for $MOUNT/.lustre/fid"
9706         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9707
9708         echo "delxattr from $MOUNT/.lustre/fid"
9709         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9710
9711         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9712         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9713                 error "touch invalid fid should fail."
9714
9715         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9716         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9717                 error "touch non-normal fid should fail."
9718
9719         echo "rename $tdir to $MOUNT/.lustre/fid"
9720         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9721                 error "rename to $MOUNT/.lustre/fid should fail."
9722
9723         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9724         then            # LU-3547
9725                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9726                 local new_obf_mode=777
9727
9728                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9729                 chmod $new_obf_mode $DIR/.lustre/fid ||
9730                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9731
9732                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9733                 [ $obf_mode -eq $new_obf_mode ] ||
9734                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9735
9736                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9737                 chmod $old_obf_mode $DIR/.lustre/fid ||
9738                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9739         fi
9740
9741         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9742         fid=$($LFS path2fid $test_dir/$tfile-2)
9743
9744         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9745         then # LU-5424
9746                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9747                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9748                         error "create lov data thru .lustre failed"
9749         fi
9750         echo "cp /etc/passwd $test_dir/$tfile-2"
9751         cp /etc/passwd $test_dir/$tfile-2 ||
9752                 error "copy to $test_dir/$tfile-2 failed."
9753         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9754         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9755                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9756
9757         rm -rf $test_dir/tfile.lnk
9758         rm -rf $test_dir/$tfile-2
9759 }
9760
9761 test_154A() {
9762         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9763                 skip "Need MDS version at least 2.4.1" && return
9764
9765         touch $DIR/$tfile
9766         local FID=$($LFS path2fid $DIR/$tfile)
9767         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9768
9769         # check that we get the same pathname back
9770         local FOUND=$($LFS fid2path $MOUNT $FID)
9771         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9772         [ "$FOUND" != "$DIR/$tfile" ] &&
9773                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9774
9775         rm -rf $DIR/$tfile
9776 }
9777 run_test 154A "lfs path2fid and fid2path basic checks"
9778
9779 test_154a() {
9780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9781         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9782                 { skip "Need MDS version at least 2.2.51"; return 0; }
9783         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9784
9785         cp /etc/hosts $DIR/$tfile
9786
9787         fid=$($LFS path2fid $DIR/$tfile)
9788         rc=$?
9789         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9790
9791         dot_lustre_fid_permission_check "$fid" $DIR ||
9792                 error "dot lustre permission check $fid failed"
9793
9794         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9795
9796         touch $MOUNT/.lustre/file &&
9797                 error "creation is not allowed under .lustre"
9798
9799         mkdir $MOUNT/.lustre/dir &&
9800                 error "mkdir is not allowed under .lustre"
9801
9802         rm -rf $DIR/$tfile
9803 }
9804 run_test 154a "Open-by-FID"
9805
9806 test_154b() {
9807         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9808         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9809                 { skip "Need MDS version at least 2.2.51"; return 0; }
9810
9811         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9812
9813         local remote_dir=$DIR/$tdir/remote_dir
9814         local MDTIDX=1
9815         local rc=0
9816
9817         mkdir -p $DIR/$tdir
9818         $LFS mkdir -i $MDTIDX $remote_dir ||
9819                 error "create remote directory failed"
9820
9821         cp /etc/hosts $remote_dir/$tfile
9822
9823         fid=$($LFS path2fid $remote_dir/$tfile)
9824         rc=$?
9825         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9826
9827         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9828                 error "dot lustre permission check $fid failed"
9829         rm -rf $DIR/$tdir
9830 }
9831 run_test 154b "Open-by-FID for remote directory"
9832
9833 test_154c() {
9834         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9835                 skip "Need MDS version at least 2.4.1" && return
9836
9837         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9838         local FID1=$($LFS path2fid $DIR/$tfile.1)
9839         local FID2=$($LFS path2fid $DIR/$tfile.2)
9840         local FID3=$($LFS path2fid $DIR/$tfile.3)
9841
9842         local N=1
9843         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9844                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9845                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9846                 local want=FID$N
9847                 [ "$FID" = "${!want}" ] ||
9848                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9849                 N=$((N + 1))
9850         done
9851
9852         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9853                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9854                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9855                 N=$((N + 1))
9856         done
9857 }
9858 run_test 154c "lfs path2fid and fid2path multiple arguments"
9859
9860 test_154d() {
9861         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9862                 skip "Need MDS version at least 2.5.53" && return
9863
9864         if remote_mds; then
9865                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9866         else
9867                 nid="0@lo"
9868         fi
9869         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9870         local fd
9871         local cmd
9872
9873         rm -f $DIR/$tfile
9874         touch $DIR/$tfile
9875
9876         fid=$($LFS path2fid $DIR/$tfile)
9877         # Open the file
9878         fd=$(free_fd)
9879         cmd="exec $fd<$DIR/$tfile"
9880         eval $cmd
9881         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9882         echo $fid_list | grep $fid
9883         rc=$?
9884
9885         cmd="exec $fd>/dev/null"
9886         eval $cmd
9887         if [ $rc -ne 0 ]; then
9888                 error "FID $fid not found in open files list $fid_list"
9889         fi
9890 }
9891 run_test 154d "Verify open file fid"
9892
9893 test_154e()
9894 {
9895         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9896                 skip "Need MDS version at least 2.6.50" && return
9897
9898         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9899                 error ".lustre returned by readdir"
9900         fi
9901 }
9902 run_test 154e ".lustre is not returned by readdir"
9903
9904 test_154f() {
9905         # create parent directory on a single MDT to avoid cross-MDT hardlinks
9906         test_mkdir -p -c1 $DIR/$tdir/d
9907         # test dirs inherit from its stripe
9908         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
9909         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
9910         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
9911         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
9912         touch $DIR/f
9913
9914         # get fid of parents
9915         local FID0=$($LFS path2fid $DIR/$tdir/d)
9916         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
9917         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
9918         local FID3=$($LFS path2fid $DIR)
9919
9920         # check that path2fid --parents returns expected <parent_fid>/name
9921         # 1) test for a directory (single parent)
9922         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
9923         [ "$parent" == "$FID0/foo1" ] ||
9924                 error "expected parent: $FID0/foo1, got: $parent"
9925
9926         # 2) test for a file with nlink > 1 (multiple parents)
9927         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
9928         echo "$parent" | grep -F "$FID1/$tfile" ||
9929                 error "$FID1/$tfile not returned in parent list"
9930         echo "$parent" | grep -F "$FID2/link" ||
9931                 error "$FID2/link not returned in parent list"
9932
9933         # 3) get parent by fid
9934         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
9935         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9936         echo "$parent" | grep -F "$FID1/$tfile" ||
9937                 error "$FID1/$tfile not returned in parent list (by fid)"
9938         echo "$parent" | grep -F "$FID2/link" ||
9939                 error "$FID2/link not returned in parent list (by fid)"
9940
9941         # 4) test for entry in root directory
9942         parent=$($LFS path2fid --parents $DIR/f)
9943         echo "$parent" | grep -F "$FID3/f" ||
9944                 error "$FID3/f not returned in parent list"
9945
9946         # 5) test it on root directory
9947         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
9948                 error "$MOUNT should not have parents"
9949
9950         # enable xattr caching and check that linkea is correctly updated
9951         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9952         save_lustre_params client "llite.*.xattr_cache" > $save
9953         lctl set_param llite.*.xattr_cache 1
9954
9955         # 6.1) linkea update on rename
9956         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
9957
9958         # get parents by fid
9959         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9960         # foo1 should no longer be returned in parent list
9961         echo "$parent" | grep -F "$FID1" &&
9962                 error "$FID1 should no longer be in parent list"
9963         # the new path should appear
9964         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
9965                 error "$FID2/$tfile.moved is not in parent list"
9966
9967         # 6.2) linkea update on unlink
9968         rm -f $DIR/$tdir/d/foo2/link
9969         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9970         # foo2/link should no longer be returned in parent list
9971         echo "$parent" | grep -F "$FID2/link" &&
9972                 error "$FID2/link should no longer be in parent list"
9973         true
9974
9975         rm -f $DIR/f
9976         restore_lustre_params < $save
9977 }
9978 run_test 154f "get parent fids by reading link ea"
9979
9980 test_155_small_load() {
9981     local temp=$TMP/$tfile
9982     local file=$DIR/$tfile
9983
9984     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9985         error "dd of=$temp bs=6096 count=1 failed"
9986     cp $temp $file
9987     cancel_lru_locks osc
9988     cmp $temp $file || error "$temp $file differ"
9989
9990     $TRUNCATE $temp 6000
9991     $TRUNCATE $file 6000
9992     cmp $temp $file || error "$temp $file differ (truncate1)"
9993
9994     echo "12345" >>$temp
9995     echo "12345" >>$file
9996     cmp $temp $file || error "$temp $file differ (append1)"
9997
9998     echo "12345" >>$temp
9999     echo "12345" >>$file
10000     cmp $temp $file || error "$temp $file differ (append2)"
10001
10002     rm -f $temp $file
10003     true
10004 }
10005
10006 test_155_big_load() {
10007     remote_ost_nodsh && skip "remote OST with nodsh" && return
10008     local temp=$TMP/$tfile
10009     local file=$DIR/$tfile
10010
10011     free_min_max
10012     local cache_size=$(do_facet ost$((MAXI+1)) \
10013         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10014     local large_file_size=$((cache_size * 2))
10015
10016     echo "OSS cache size: $cache_size KB"
10017     echo "Large file size: $large_file_size KB"
10018
10019     [ $MAXV -le $large_file_size ] && \
10020         skip_env "max available OST size needs > $large_file_size KB" && \
10021         return 0
10022
10023     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10024
10025     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10026         error "dd of=$temp bs=$large_file_size count=1k failed"
10027     cp $temp $file
10028     ls -lh $temp $file
10029     cancel_lru_locks osc
10030     cmp $temp $file || error "$temp $file differ"
10031
10032     rm -f $temp $file
10033     true
10034 }
10035
10036 test_155a() {
10037         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10038         set_cache read on
10039         set_cache writethrough on
10040         test_155_small_load
10041 }
10042 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10043
10044 test_155b() {
10045         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10046         set_cache read on
10047         set_cache writethrough off
10048         test_155_small_load
10049 }
10050 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10051
10052 test_155c() {
10053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10054         set_cache read off
10055         set_cache writethrough on
10056         test_155_small_load
10057 }
10058 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10059
10060 test_155d() {
10061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10062         set_cache read off
10063         set_cache writethrough off
10064         test_155_small_load
10065 }
10066 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10067
10068 test_155e() {
10069         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10070         set_cache read on
10071         set_cache writethrough on
10072         test_155_big_load
10073 }
10074 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10075
10076 test_155f() {
10077         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10078         set_cache read on
10079         set_cache writethrough off
10080         test_155_big_load
10081 }
10082 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10083
10084 test_155g() {
10085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10086         set_cache read off
10087         set_cache writethrough on
10088         test_155_big_load
10089 }
10090 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10091
10092 test_155h() {
10093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10094         set_cache read off
10095         set_cache writethrough off
10096         test_155_big_load
10097 }
10098 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10099
10100 test_156() {
10101         remote_ost_nodsh && skip "remote OST with nodsh" && return
10102         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10103         local CPAGES=3
10104         local BEFORE
10105         local AFTER
10106         local file="$DIR/$tfile"
10107
10108         [ "$(facet_fstype ost1)" = "zfs" ] &&
10109                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
10110                 return
10111
10112         roc_hit_init
10113
10114     log "Turn on read and write cache"
10115     set_cache read on
10116     set_cache writethrough on
10117
10118     log "Write data and read it back."
10119     log "Read should be satisfied from the cache."
10120     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10121     BEFORE=`roc_hit`
10122     cancel_lru_locks osc
10123     cat $file >/dev/null
10124     AFTER=`roc_hit`
10125     if ! let "AFTER - BEFORE == CPAGES"; then
10126         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10127     else
10128         log "cache hits:: before: $BEFORE, after: $AFTER"
10129     fi
10130
10131     log "Read again; it should be satisfied from the cache."
10132     BEFORE=$AFTER
10133     cancel_lru_locks osc
10134     cat $file >/dev/null
10135     AFTER=`roc_hit`
10136     if ! let "AFTER - BEFORE == CPAGES"; then
10137         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10138     else
10139         log "cache hits:: before: $BEFORE, after: $AFTER"
10140     fi
10141
10142
10143     log "Turn off the read cache and turn on the write cache"
10144     set_cache read off
10145     set_cache writethrough on
10146
10147     log "Read again; it should be satisfied from the cache."
10148     BEFORE=`roc_hit`
10149     cancel_lru_locks osc
10150     cat $file >/dev/null
10151     AFTER=`roc_hit`
10152     if ! let "AFTER - BEFORE == CPAGES"; then
10153         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10154     else
10155         log "cache hits:: before: $BEFORE, after: $AFTER"
10156     fi
10157
10158     log "Read again; it should not be satisfied from the cache."
10159     BEFORE=$AFTER
10160     cancel_lru_locks osc
10161     cat $file >/dev/null
10162     AFTER=`roc_hit`
10163     if ! let "AFTER - BEFORE == 0"; then
10164         error "IN CACHE: before: $BEFORE, after: $AFTER"
10165     else
10166         log "cache hits:: before: $BEFORE, after: $AFTER"
10167     fi
10168
10169     log "Write data and read it back."
10170     log "Read should be satisfied from the cache."
10171     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10172     BEFORE=`roc_hit`
10173     cancel_lru_locks osc
10174     cat $file >/dev/null
10175     AFTER=`roc_hit`
10176     if ! let "AFTER - BEFORE == CPAGES"; then
10177         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10178     else
10179         log "cache hits:: before: $BEFORE, after: $AFTER"
10180     fi
10181
10182     log "Read again; it should not be satisfied from the cache."
10183     BEFORE=$AFTER
10184     cancel_lru_locks osc
10185     cat $file >/dev/null
10186     AFTER=`roc_hit`
10187     if ! let "AFTER - BEFORE == 0"; then
10188         error "IN CACHE: before: $BEFORE, after: $AFTER"
10189     else
10190         log "cache hits:: before: $BEFORE, after: $AFTER"
10191     fi
10192
10193
10194     log "Turn off read and write cache"
10195     set_cache read off
10196     set_cache writethrough off
10197
10198     log "Write data and read it back"
10199     log "It should not be satisfied from the cache."
10200     rm -f $file
10201     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10202     cancel_lru_locks osc
10203     BEFORE=`roc_hit`
10204     cat $file >/dev/null
10205     AFTER=`roc_hit`
10206         if ! let "AFTER - BEFORE == 0"; then
10207                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10208         else
10209                 log "cache hits:: before: $BEFORE, after: $AFTER"
10210         fi
10211
10212     log "Turn on the read cache and turn off the write cache"
10213     set_cache read on
10214     set_cache writethrough off
10215
10216     log "Write data and read it back"
10217     log "It should not be satisfied from the cache."
10218     rm -f $file
10219     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10220     BEFORE=`roc_hit`
10221     cancel_lru_locks osc
10222     cat $file >/dev/null
10223     AFTER=`roc_hit`
10224         if ! let "AFTER - BEFORE == 0"; then
10225                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10226         else
10227                 log "cache hits:: before: $BEFORE, after: $AFTER"
10228         fi
10229
10230     log "Read again; it should be satisfied from the cache."
10231     BEFORE=`roc_hit`
10232     cancel_lru_locks osc
10233     cat $file >/dev/null
10234     AFTER=`roc_hit`
10235     if ! let "AFTER - BEFORE == CPAGES"; then
10236         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10237     else
10238         log "cache hits:: before: $BEFORE, after: $AFTER"
10239     fi
10240
10241     rm -f $file
10242 }
10243 run_test 156 "Verification of tunables ============================"
10244
10245 #Changelogs
10246 err17935 () {
10247         if [[ $MDSCOUNT -gt 1 ]]; then
10248                 error_ignore bz17935 $*
10249         else
10250                 error $*
10251         fi
10252 }
10253
10254 changelog_chmask()
10255 {
10256         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10257
10258         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10259
10260         if [ $MASK -eq 1 ]; then
10261                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10262         else
10263                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10264         fi
10265 }
10266
10267 changelog_extract_field() {
10268         local mdt=$1
10269         local cltype=$2
10270         local file=$3
10271         local identifier=$4
10272
10273         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10274                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10275                 tail -1
10276 }
10277
10278 test_160a() {
10279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10280         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10281         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10282                 { skip "Need MDS version at least 2.2.0"; return; }
10283
10284         local CL_USERS="mdd.$MDT0.changelog_users"
10285         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10286         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10287         echo "Registered as changelog user $USER"
10288         $GET_CL_USERS | grep -q $USER ||
10289                 error "User $USER not found in changelog_users"
10290
10291         # change something
10292         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10293         touch $DIR/$tdir/pics/2008/zachy/timestamp
10294         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10295         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10296         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10297         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10298         rm $DIR/$tdir/pics/desktop.jpg
10299
10300         $LFS changelog $MDT0 | tail -5
10301
10302         echo "verifying changelog mask"
10303         changelog_chmask "MKDIR"
10304         changelog_chmask "CLOSE"
10305
10306         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10307         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10308
10309         changelog_chmask "MKDIR"
10310         changelog_chmask "CLOSE"
10311
10312         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10313         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10314
10315         $LFS changelog $MDT0
10316         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10317         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10318         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10319         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10320
10321         # verify contents
10322         echo "verifying target fid"
10323         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10324         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10325         [ "$fidc" == "$fidf" ] ||
10326                 err17935 "fid in changelog $fidc != file fid $fidf"
10327         echo "verifying parent fid"
10328         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10329         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10330         [ "$fidc" == "$fidf" ] ||
10331                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10332
10333         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10334         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10335         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10336         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10337         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10338                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10339
10340         MIN_REC=$($GET_CL_USERS |
10341                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10342         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10343         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10344         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10345                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10346
10347         # LU-3446 changelog index reset on MDT restart
10348         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10349         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10350         $LFS changelog_clear $MDT0 $USER 0
10351         stop $SINGLEMDS || error "Fail to stop MDT."
10352         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10353         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10354         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10355         [ $CUR_REC1 == $CUR_REC2 ] ||
10356                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10357
10358         echo "verifying user deregister"
10359         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10360         $GET_CL_USERS | grep -q $USER &&
10361                 error "User $USER still in changelog_users"
10362
10363         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10364         if [ $USERS -eq 0 ]; then
10365                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10366                 touch $DIR/$tdir/chloe
10367                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10368                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10369                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10370         else
10371                 echo "$USERS other changelog users; can't verify off"
10372         fi
10373 }
10374 run_test 160a "changelog sanity"
10375
10376 test_160b() { # LU-3587
10377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10378         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10379         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10380                 { skip "Need MDS version at least 2.2.0"; return; }
10381
10382         local CL_USERS="mdd.$MDT0.changelog_users"
10383         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10384         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10385         echo "Registered as changelog user $USER"
10386         $GET_CL_USERS | grep -q $USER ||
10387                 error "User $USER not found in changelog_users"
10388
10389         local LONGNAME1=$(str_repeat a 255)
10390         local LONGNAME2=$(str_repeat b 255)
10391
10392         cd $DIR
10393         echo "creating very long named file"
10394         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10395         echo "moving very long named file"
10396         mv $LONGNAME1 $LONGNAME2
10397
10398         $LFS changelog $MDT0 | grep RENME
10399
10400         echo "deregistering $USER"
10401         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10402
10403         rm -f $LONGNAME2
10404 }
10405 run_test 160b "Verify that very long rename doesn't crash in changelog"
10406
10407 test_160c() {
10408         local rc=0
10409         local server_version=$(lustre_version_code $SINGLEMDS)
10410
10411         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10412                 [[ $server_version -gt $(version_code 2.5.1) &&
10413                    $server_version -lt $(version_code 2.5.50) ]] ||
10414                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10416
10417         # Registration step
10418         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10419                 changelog_register -n)
10420
10421         rm -rf $DIR/$tdir
10422         mkdir -p $DIR/$tdir
10423         $MCREATE $DIR/$tdir/foo_160c
10424         changelog_chmask "TRUNC"
10425         $TRUNCATE $DIR/$tdir/foo_160c 200
10426         changelog_chmask "TRUNC"
10427         $TRUNCATE $DIR/$tdir/foo_160c 199
10428         $LFS changelog $MDT0
10429         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10430         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10431         $LFS changelog_clear $MDT0 $USER 0
10432
10433         # Deregistration step
10434         echo "deregistering $USER"
10435         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10436 }
10437 run_test 160c "verify that changelog log catch the truncate event"
10438
10439 test_161a() {
10440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10441         test_mkdir -p -c1 $DIR/$tdir
10442         cp /etc/hosts $DIR/$tdir/$tfile
10443         test_mkdir -c1 $DIR/$tdir/foo1
10444         test_mkdir -c1 $DIR/$tdir/foo2
10445         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10446         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10447         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10448         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10449         local FID=$($LFS path2fid $DIR/$tdir/$tfile | 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 $DIR/$tdir/foo2/zachary
10456     # last
10457     rm $DIR/$tdir/foo2/thor
10458     # first
10459     rm $DIR/$tdir/$tfile
10460     # rename
10461     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10462     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10463         then
10464         $LFS fid2path $DIR $FID
10465         err17935 "bad link rename"
10466     fi
10467     rm $DIR/$tdir/foo2/maggie
10468
10469         # overflow the EA
10470         local longname=filename_avg_len_is_thirty_two_
10471         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/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 $DIR/$tdir/foo2/$longname 1000 ||
10478                 error "failed to unlink many hardlinks"
10479 }
10480 run_test 161a "link ea sanity"
10481
10482 test_161b() {
10483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10484         [ $MDSCOUNT -lt 2 ] &&
10485                 skip "skipping remote directory test" && return
10486         local MDTIDX=1
10487         local remote_dir=$DIR/$tdir/remote_dir
10488
10489         mkdir -p $DIR/$tdir
10490         $LFS mkdir -i $MDTIDX $remote_dir ||
10491                 error "create remote directory failed"
10492
10493         cp /etc/hosts $remote_dir/$tfile
10494         mkdir -p $remote_dir/foo1
10495         mkdir -p $remote_dir/foo2
10496         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10497         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10498         ln $remote_dir/$tfile $remote_dir/foo1/luna
10499         ln $remote_dir/$tfile $remote_dir/foo2/thor
10500
10501         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10502                      tr -d ']')
10503         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10504                 $LFS fid2path $DIR $FID
10505                 err17935 "bad link ea"
10506         fi
10507         # middle
10508         rm $remote_dir/foo2/zachary
10509         # last
10510         rm $remote_dir/foo2/thor
10511         # first
10512         rm $remote_dir/$tfile
10513         # rename
10514         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10515         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10516         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10517                 $LFS fid2path $DIR $FID
10518                 err17935 "bad link rename"
10519         fi
10520         rm $remote_dir/foo2/maggie
10521
10522         # overflow the EA
10523         local longname=filename_avg_len_is_thirty_two_
10524         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10525                 error "failed to hardlink many files"
10526         links=$($LFS fid2path $DIR $FID | wc -l)
10527         echo -n "${links}/1000 links in link EA"
10528         [[ ${links} -gt 60 ]] ||
10529                 err17935 "expected at least 60 links in link EA"
10530         unlinkmany $remote_dir/foo2/$longname 1000 ||
10531         error "failed to unlink many hardlinks"
10532 }
10533 run_test 161b "link ea sanity under remote directory"
10534
10535 test_161c() {
10536         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10538         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10539                 skip "Need MDS version at least 2.1.5" && return
10540
10541         # define CLF_RENAME_LAST 0x0001
10542         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10543         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10544                 changelog_register -n)
10545         rm -rf $DIR/$tdir
10546         mkdir -p $DIR/$tdir
10547         touch $DIR/$tdir/foo_161c
10548         touch $DIR/$tdir/bar_161c
10549         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10550         $LFS changelog $MDT0 | grep RENME
10551         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10552                 cut -f5 -d' ')
10553         $LFS changelog_clear $MDT0 $USER 0
10554         if [ x$flags != "x0x1" ]; then
10555                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10556                         $USER
10557                 error "flag $flags is not 0x1"
10558         fi
10559         echo "rename overwrite a target having nlink = 1," \
10560                 "changelog record has flags of $flags"
10561
10562         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10563         touch $DIR/$tdir/foo_161c
10564         touch $DIR/$tdir/bar_161c
10565         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10566         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10567         $LFS changelog $MDT0 | grep RENME
10568         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10569         $LFS changelog_clear $MDT0 $USER 0
10570         if [ x$flags != "x0x0" ]; then
10571                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10572                         $USER
10573                 error "flag $flags is not 0x0"
10574         fi
10575         echo "rename overwrite a target having nlink > 1," \
10576                 "changelog record has flags of $flags"
10577
10578         # rename doesn't overwrite a target (changelog flag 0x0)
10579         touch $DIR/$tdir/foo_161c
10580         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10581         $LFS changelog $MDT0 | grep RENME
10582         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10583         $LFS changelog_clear $MDT0 $USER 0
10584         if [ x$flags != "x0x0" ]; then
10585                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10586                         $USER
10587                 error "flag $flags is not 0x0"
10588         fi
10589         echo "rename doesn't overwrite a target," \
10590                 "changelog record has flags of $flags"
10591
10592         # define CLF_UNLINK_LAST 0x0001
10593         # unlink a file having nlink = 1 (changelog flag 0x1)
10594         rm -f $DIR/$tdir/foo2_161c
10595         $LFS changelog $MDT0 | grep UNLNK
10596         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10597         $LFS changelog_clear $MDT0 $USER 0
10598         if [ x$flags != "x0x1" ]; then
10599                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10600                         $USER
10601                 error "flag $flags is not 0x1"
10602         fi
10603         echo "unlink a file having nlink = 1," \
10604                 "changelog record has flags of $flags"
10605
10606         # unlink a file having nlink > 1 (changelog flag 0x0)
10607         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10608         rm -f $DIR/$tdir/foobar_161c
10609         $LFS changelog $MDT0 | grep UNLNK
10610         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10611         $LFS changelog_clear $MDT0 $USER 0
10612         if [ x$flags != "x0x0" ]; then
10613                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10614                         $USER
10615                 error "flag $flags is not 0x0"
10616         fi
10617         echo "unlink a file having nlink > 1," \
10618                 "changelog record has flags of $flags"
10619         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10620 }
10621 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10622
10623 check_path() {
10624     local expected=$1
10625     shift
10626     local fid=$2
10627
10628     local path=$(${LFS} fid2path $*)
10629     # Remove the '//' indicating a remote directory
10630     path=$(echo $path | sed 's#//#/#g')
10631     RC=$?
10632
10633     if [ $RC -ne 0 ]; then
10634         err17935 "path looked up of $expected failed. Error $RC"
10635         return $RC
10636     elif [ "${path}" != "${expected}" ]; then
10637         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10638         return 2
10639     fi
10640     echo "fid $fid resolves to path $path (expected $expected)"
10641 }
10642
10643 test_162() {
10644         # Make changes to filesystem
10645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10646         test_mkdir -p -c1 $DIR/$tdir/d2
10647         touch $DIR/$tdir/d2/$tfile
10648         touch $DIR/$tdir/d2/x1
10649         touch $DIR/$tdir/d2/x2
10650         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10651         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10652         # regular file
10653         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10654         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10655                 error "check path $tdir/d2/$tfile failed"
10656
10657         # softlink
10658         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10659         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10660         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10661                 error "check path $tdir/d2/p/q/r/slink failed"
10662
10663         # softlink to wrong file
10664         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10665         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10666         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10667                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10668
10669         # hardlink
10670         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10671         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10672         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10673         # fid2path dir/fsname should both work
10674         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10675                 error "check path $tdir/d2/a/b/c/new_file failed"
10676         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10677                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10678
10679         # hardlink count: check that there are 2 links
10680         # Doesnt work with CMD yet: 17935
10681         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10682                 err17935 "expected 2 links"
10683
10684         # hardlink indexing: remove the first link
10685         rm $DIR/$tdir/d2/p/q/r/hlink
10686         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10687                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10688
10689         return 0
10690 }
10691 run_test 162 "path lookup sanity"
10692
10693 test_162b() {
10694         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10695         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10696
10697         mkdir $DIR/$tdir
10698         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10699                                 error "create striped dir failed"
10700
10701         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10702                                         tail -n 1 | awk '{print $2}')
10703         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10704
10705         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10706         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10707
10708         # regular file
10709         for ((i=0;i<5;i++)); do
10710                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10711                         error "get fid for f$i failed"
10712                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10713                         error "check path $tdir/striped_dir/f$i failed"
10714
10715                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10716                         error "get fid for d$i failed"
10717                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10718                         error "check path $tdir/striped_dir/d$i failed"
10719         done
10720
10721         return 0
10722 }
10723 run_test 162b "striped directory path lookup sanity"
10724
10725 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10726 test_162c() {
10727         test_mkdir $DIR/$tdir.local
10728         test_mkdir $DIR/$tdir.remote
10729         local lpath=$tdir.local
10730         local rpath=$tdir.remote
10731
10732         for ((i = 0; i <= 101; i++)); do
10733                 lpath="$lpath/$i"
10734                 mkdir $DIR/$lpath
10735                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10736                         error "get fid for local directory $DIR/$lpath failed"
10737                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10738                         error "check path for local directory $DIR/$lpath failed"
10739
10740                 rpath="$rpath/$i"
10741                 test_mkdir $DIR/$rpath
10742                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10743                         error "get fid for remote directory $DIR/$rpath failed"
10744                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10745                         error "check path for remote directory $DIR/$rpath failed"
10746         done
10747
10748         return 0
10749 }
10750 run_test 162c "fid2path works with paths 100 or more directories deep"
10751
10752 test_169() {
10753         # do directio so as not to populate the page cache
10754         log "creating a 10 Mb file"
10755         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10756         log "starting reads"
10757         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10758         log "truncating the file"
10759         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10760         log "killing dd"
10761         kill %+ || true # reads might have finished
10762         echo "wait until dd is finished"
10763         wait
10764         log "removing the temporary file"
10765         rm -rf $DIR/$tfile || error "tmp file removal failed"
10766 }
10767 run_test 169 "parallel read and truncate should not deadlock"
10768
10769 test_170() {
10770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10771         $LCTL clear     # bug 18514
10772         $LCTL debug_daemon start $TMP/${tfile}_log_good
10773         touch $DIR/$tfile
10774         $LCTL debug_daemon stop
10775         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10776                error "sed failed to read log_good"
10777
10778         $LCTL debug_daemon start $TMP/${tfile}_log_good
10779         rm -rf $DIR/$tfile
10780         $LCTL debug_daemon stop
10781
10782         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10783                error "lctl df log_bad failed"
10784
10785         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10786         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10787
10788         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10789         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10790
10791         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10792                 error "bad_line good_line1 good_line2 are empty"
10793
10794         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10795         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10796         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10797
10798         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10799         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10800         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10801
10802         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10803                 error "bad_line_new good_line_new are empty"
10804
10805         local expected_good=$((good_line1 + good_line2*2))
10806
10807         rm -f $TMP/${tfile}*
10808         # LU-231, short malformed line may not be counted into bad lines
10809         if [ $bad_line -ne $bad_line_new ] &&
10810                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10811                 error "expected $bad_line bad lines, but got $bad_line_new"
10812                 return 1
10813         fi
10814
10815         if [ $expected_good -ne $good_line_new ]; then
10816                 error "expected $expected_good good lines, but got $good_line_new"
10817                 return 2
10818         fi
10819         true
10820 }
10821 run_test 170 "test lctl df to handle corrupted log ====================="
10822
10823 test_171() { # bug20592
10824         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10825 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10826         $LCTL set_param fail_loc=0x50e
10827         $LCTL set_param fail_val=3000
10828         multiop_bg_pause $DIR/$tfile O_s || true
10829         local MULTIPID=$!
10830         kill -USR1 $MULTIPID
10831         # cause log dump
10832         sleep 3
10833         wait $MULTIPID
10834         if dmesg | grep "recursive fault"; then
10835                 error "caught a recursive fault"
10836         fi
10837         $LCTL set_param fail_loc=0
10838         true
10839 }
10840 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10841
10842 # it would be good to share it with obdfilter-survey/iokit-libecho code
10843 setup_obdecho_osc () {
10844         local rc=0
10845         local ost_nid=$1
10846         local obdfilter_name=$2
10847         echo "Creating new osc for $obdfilter_name on $ost_nid"
10848         # make sure we can find loopback nid
10849         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10850
10851         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10852                            ${obdfilter_name}_osc_UUID || rc=2; }
10853         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10854                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10855         return $rc
10856 }
10857
10858 cleanup_obdecho_osc () {
10859         local obdfilter_name=$1
10860         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10861         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10862         return 0
10863 }
10864
10865 obdecho_test() {
10866         local OBD=$1
10867         local node=$2
10868         local pages=${3:-64}
10869         local rc=0
10870         local id
10871
10872         local count=10
10873         local obd_size=$(get_obd_size $node $OBD)
10874         local page_size=$(get_page_size $node)
10875         if [[ -n "$obd_size" ]]; then
10876                 local new_count=$((obd_size / (pages * page_size / 1024)))
10877                 [[ $new_count -ge $count ]] || count=$new_count
10878         fi
10879
10880         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10881         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10882                            rc=2; }
10883         if [ $rc -eq 0 ]; then
10884             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10885             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10886         fi
10887         echo "New object id is $id"
10888         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10889                            rc=4; }
10890         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10891                            "test_brw $count w v $pages $id" || rc=4; }
10892         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10893                            rc=4; }
10894         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10895                                         "cleanup" || rc=5; }
10896         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10897                                         "detach" || rc=6; }
10898         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10899         return $rc
10900 }
10901
10902 test_180a() {
10903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10904         remote_ost_nodsh && skip "remote OST with nodsh" && return
10905         local rc=0
10906         local rmmod_local=0
10907
10908         if ! module_loaded obdecho; then
10909             load_module obdecho/obdecho
10910             rmmod_local=1
10911         fi
10912
10913         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10914         local host=$(lctl get_param -n osc.$osc.import |
10915                              awk '/current_connection:/ {print $2}' )
10916         local target=$(lctl get_param -n osc.$osc.import |
10917                              awk '/target:/ {print $2}' )
10918         target=${target%_UUID}
10919
10920         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10921         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10922         [[ -n $target ]] && cleanup_obdecho_osc $target
10923         [ $rmmod_local -eq 1 ] && rmmod obdecho
10924         return $rc
10925 }
10926 run_test 180a "test obdecho on osc"
10927
10928 test_180b() {
10929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10930         remote_ost_nodsh && skip "remote OST with nodsh" && return
10931         local rc=0
10932         local rmmod_remote=0
10933
10934         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10935                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10936                       "modprobe obdecho; }" && rmmod_remote=1
10937         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10938         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10939         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10940         return $rc
10941 }
10942 run_test 180b "test obdecho directly on obdfilter"
10943
10944 test_180c() { # LU-2598
10945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10946         remote_ost_nodsh && skip "remote OST with nodsh" && return
10947         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10948                 skip "Need MDS version at least 2.4.0" && return
10949
10950         local rc=0
10951         local rmmod_remote=false
10952         local pages=16384 # 64MB bulk I/O RPC size
10953         local target
10954
10955         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10956                 rmmod_remote=true || error "failed to load module obdecho"
10957
10958         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10959                 head -n1)
10960         if [ -n "$target" ]; then
10961                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10962         else
10963                 echo "there is no obdfilter target on ost1"
10964                 rc=2
10965         fi
10966         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10967         return $rc
10968 }
10969 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10970
10971 test_181() { # bug 22177
10972         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10973         # create enough files to index the directory
10974         createmany -o $DIR/$tdir/foobar 4000
10975         # print attributes for debug purpose
10976         lsattr -d .
10977         # open dir
10978         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10979         MULTIPID=$!
10980         # remove the files & current working dir
10981         unlinkmany $DIR/$tdir/foobar 4000
10982         rmdir $DIR/$tdir
10983         kill -USR1 $MULTIPID
10984         wait $MULTIPID
10985         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10986         return 0
10987 }
10988 run_test 181 "Test open-unlinked dir ========================"
10989
10990 test_182() {
10991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10992         # disable MDC RPC lock wouldn't crash client
10993         local fcount=1000
10994         local tcount=4
10995
10996         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10997 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10998         $LCTL set_param fail_loc=0x804
10999
11000         for (( i=0; i < $tcount; i++ )) ; do
11001                 mkdir $DIR/$tdir/$i
11002                 createmany -o $DIR/$tdir/$i/f- $fcount &
11003         done
11004         wait
11005
11006         for (( i=0; i < $tcount; i++ )) ; do
11007                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11008         done
11009         wait
11010
11011         rm -rf $DIR/$tdir
11012
11013         $LCTL set_param fail_loc=0
11014 }
11015 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
11016
11017 test_183() { # LU-2275
11018         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11019         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11020                 skip "Need MDS version at least 2.3.56" && return
11021
11022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11023         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11024         echo aaa > $DIR/$tdir/$tfile
11025
11026 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11027         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11028
11029         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11030         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11031
11032         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11033
11034         # Flush negative dentry cache
11035         touch $DIR/$tdir/$tfile
11036
11037         # We are not checking for any leaked references here, they'll
11038         # become evident next time we do cleanup with module unload.
11039         rm -rf $DIR/$tdir
11040 }
11041 run_test 183 "No crash or request leak in case of strange dispositions ========"
11042
11043 # test suite 184 is for LU-2016, LU-2017
11044 test_184a() {
11045         check_swap_layouts_support && return 0
11046
11047         dir0=$DIR/$tdir/$testnum
11048         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11049         ref1=/etc/passwd
11050         ref2=/etc/group
11051         file1=$dir0/f1
11052         file2=$dir0/f2
11053         $SETSTRIPE -c1 $file1
11054         cp $ref1 $file1
11055         $SETSTRIPE -c2 $file2
11056         cp $ref2 $file2
11057         gen1=$($GETSTRIPE -g $file1)
11058         gen2=$($GETSTRIPE -g $file2)
11059
11060         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11061         gen=$($GETSTRIPE -g $file1)
11062         [[ $gen1 != $gen ]] ||
11063                 "Layout generation on $file1 does not change"
11064         gen=$($GETSTRIPE -g $file2)
11065         [[ $gen2 != $gen ]] ||
11066                 "Layout generation on $file2 does not change"
11067
11068         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11069         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11070 }
11071 run_test 184a "Basic layout swap"
11072
11073 test_184b() {
11074         check_swap_layouts_support && return 0
11075
11076         dir0=$DIR/$tdir/$testnum
11077         mkdir -p $dir0 || error "creating dir $dir0"
11078         file1=$dir0/f1
11079         file2=$dir0/f2
11080         file3=$dir0/f3
11081         dir1=$dir0/d1
11082         dir2=$dir0/d2
11083         mkdir $dir1 $dir2
11084         $SETSTRIPE -c1 $file1
11085         $SETSTRIPE -c2 $file2
11086         $SETSTRIPE -c1 $file3
11087         chown $RUNAS_ID $file3
11088         gen1=$($GETSTRIPE -g $file1)
11089         gen2=$($GETSTRIPE -g $file2)
11090
11091         $LFS swap_layouts $dir1 $dir2 &&
11092                 error "swap of directories layouts should fail"
11093         $LFS swap_layouts $dir1 $file1 &&
11094                 error "swap of directory and file layouts should fail"
11095         $RUNAS $LFS swap_layouts $file1 $file2 &&
11096                 error "swap of file we cannot write should fail"
11097         $LFS swap_layouts $file1 $file3 &&
11098                 error "swap of file with different owner should fail"
11099         /bin/true # to clear error code
11100 }
11101 run_test 184b "Forbidden layout swap (will generate errors)"
11102
11103 test_184c() {
11104         check_swap_layouts_support && return 0
11105
11106         local dir0=$DIR/$tdir/$testnum
11107         mkdir -p $dir0 || error "creating dir $dir0"
11108
11109         local ref1=$dir0/ref1
11110         local ref2=$dir0/ref2
11111         local file1=$dir0/file1
11112         local file2=$dir0/file2
11113         # create a file large enough for the concurent test
11114         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11115         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11116         echo "ref file size: ref1($(stat -c %s $ref1))," \
11117              "ref2($(stat -c %s $ref2))"
11118
11119         cp $ref2 $file2
11120         dd if=$ref1 of=$file1 bs=16k &
11121         local DD_PID=$!
11122
11123         # Make sure dd starts to copy file
11124         while [ ! -f $file1 ]; do sleep 0.1; done
11125
11126         $LFS swap_layouts $file1 $file2
11127         local rc=$?
11128         wait $DD_PID
11129         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11130         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11131
11132         # how many bytes copied before swapping layout
11133         local copied=$(stat -c %s $file2)
11134         local remaining=$(stat -c %s $ref1)
11135         remaining=$((remaining - copied))
11136         echo "Copied $copied bytes before swapping layout..."
11137
11138         cmp -n $copied $file1 $ref2 | grep differ &&
11139                 error "Content mismatch [0, $copied) of ref2 and file1"
11140         cmp -n $copied $file2 $ref1 ||
11141                 error "Content mismatch [0, $copied) of ref1 and file2"
11142         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11143                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11144
11145         # clean up
11146         rm -f $ref1 $ref2 $file1 $file2
11147 }
11148 run_test 184c "Concurrent write and layout swap"
11149
11150 test_184d() {
11151         check_swap_layouts_support && return 0
11152         [ -z "$(which getfattr 2>/dev/null)" ] &&
11153                 skip "no getfattr command" && return 0
11154
11155         local file1=$DIR/$tdir/$tfile-1
11156         local file2=$DIR/$tdir/$tfile-2
11157         local file3=$DIR/$tdir/$tfile-3
11158         local lovea1
11159         local lovea2
11160
11161         mkdir -p $DIR/$tdir
11162         touch $file1 || error "create $file1 failed"
11163         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11164                 error "create $file2 failed"
11165         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11166                 error "create $file3 failed"
11167         lovea1=$($LFS getstripe $file1 | sed 1d)
11168
11169         $LFS swap_layouts $file2 $file3 ||
11170                 error "swap $file2 $file3 layouts failed"
11171         $LFS swap_layouts $file1 $file2 ||
11172                 error "swap $file1 $file2 layouts failed"
11173
11174         lovea2=$($LFS getstripe $file2 | sed 1d)
11175         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11176
11177         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11178         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11179 }
11180 run_test 184d "allow stripeless layouts swap"
11181
11182 test_184e() {
11183         check_swap_layouts_support && return 0
11184         [ -z "$(which getfattr 2>/dev/null)" ] &&
11185                 skip "no getfattr command" && return 0
11186
11187         local file1=$DIR/$tdir/$tfile-1
11188         local file2=$DIR/$tdir/$tfile-2
11189         local file3=$DIR/$tdir/$tfile-3
11190         local lovea
11191
11192         mkdir -p $DIR/$tdir
11193         touch $file1 || error "create $file1 failed"
11194         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11195                 error "create $file2 failed"
11196         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11197                 error "create $file3 failed"
11198
11199         $LFS swap_layouts $file1 $file2 ||
11200                 error "swap $file1 $file2 layouts failed"
11201
11202         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11203         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11204
11205         echo 123 > $file1 || error "Should be able to write into $file1"
11206
11207         $LFS swap_layouts $file1 $file3 ||
11208                 error "swap $file1 $file3 layouts failed"
11209
11210         echo 123 > $file1 || error "Should be able to write into $file1"
11211
11212         rm -rf $file1 $file2 $file3
11213 }
11214 run_test 184e "Recreate layout after stripeless layout swaps"
11215
11216 test_185() { # LU-2441
11217         # LU-3553 - no volatile file support in old servers
11218         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11219                 { skip "Need MDS version at least 2.3.60"; return 0; }
11220
11221         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11222         touch $DIR/$tdir/spoo
11223         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11224         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11225                 error "cannot create/write a volatile file"
11226         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11227                 error "FID is still valid after close"
11228
11229         multiop_bg_pause $DIR/$tdir vVw4096_c
11230         local multi_pid=$!
11231
11232         local OLD_IFS=$IFS
11233         IFS=":"
11234         local fidv=($fid)
11235         IFS=$OLD_IFS
11236         # assume that the next FID for this client is sequential, since stdout
11237         # is unfortunately eaten by multiop_bg_pause
11238         local n=$((${fidv[1]} + 1))
11239         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11240         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11241                 error "FID is missing before close"
11242         kill -USR1 $multi_pid
11243         # 1 second delay, so if mtime change we will see it
11244         sleep 1
11245         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11246         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11247 }
11248 run_test 185 "Volatile file support"
11249
11250 test_187a() {
11251         local dir0=$DIR/$tdir/$testnum
11252         mkdir -p $dir0 || error "creating dir $dir0"
11253
11254         local file=$dir0/file1
11255         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11256         local dv1=$($LFS data_version $file)
11257         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11258         local dv2=$($LFS data_version $file)
11259         [[ $dv1 != $dv2 ]] ||
11260                 error "data version did not change on write $dv1 == $dv2"
11261
11262         # clean up
11263         rm -f $file1
11264 }
11265 run_test 187a "Test data version change"
11266
11267 test_187b() {
11268         local dir0=$DIR/$tdir/$testnum
11269         mkdir -p $dir0 || error "creating dir $dir0"
11270
11271         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11272         [[ ${DV[0]} != ${DV[1]} ]] ||
11273                 error "data version did not change on write"\
11274                       " ${DV[0]} == ${DV[1]}"
11275
11276         # clean up
11277         rm -f $file1
11278 }
11279 run_test 187b "Test data version change on volatile file"
11280
11281 test_200() {
11282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11283         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11284
11285         local POOL=${POOL:-cea1}
11286         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11287         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11288         # Pool OST targets
11289         local first_ost=0
11290         local last_ost=$(($OSTCOUNT - 1))
11291         local ost_step=2
11292         local ost_list=$(seq $first_ost $ost_step $last_ost)
11293         local ost_range="$first_ost $last_ost $ost_step"
11294         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11295         local file_dir=$POOL_ROOT/file_tst
11296         local subdir=$test_path/subdir
11297
11298         local rc=0
11299         while : ; do
11300                 # former test_200a test_200b
11301                 pool_add $POOL                          || { rc=$? ; break; }
11302                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11303                 # former test_200c test_200d
11304                 mkdir -p $test_path
11305                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11306                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11307                 mkdir -p $subdir
11308                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11309                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11310                                                         || { rc=$? ; break; }
11311                 # former test_200e test_200f
11312                 local files=$((OSTCOUNT*3))
11313                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11314                                                         || { rc=$? ; break; }
11315                 pool_create_files $POOL $file_dir $files "$ost_list" \
11316                                                         || { rc=$? ; break; }
11317                 # former test_200g test_200h
11318                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11319                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11320
11321                 # former test_201a test_201b test_201c
11322                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11323
11324                 local f=$test_path/$tfile
11325                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11326                 pool_remove $POOL $f                    || { rc=$? ; break; }
11327                 break
11328         done
11329
11330         cleanup_pools
11331         return $rc
11332 }
11333 run_test 200 "OST pools"
11334
11335 # usage: default_attr <count | size | offset>
11336 default_attr() {
11337         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11338 }
11339
11340 # usage: check_default_stripe_attr
11341 check_default_stripe_attr() {
11342         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11343         case $1 in
11344         --stripe-count|--count)
11345                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11346         --stripe-size|--size)
11347                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11348         --stripe-index|--index)
11349                 EXPECTED=-1;;
11350         *)
11351                 error "unknown getstripe attr '$1'"
11352         esac
11353
11354         [ $ACTUAL != $EXPECTED ] &&
11355                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11356 }
11357
11358 test_204a() {
11359         test_mkdir -p $DIR/$tdir
11360         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11361
11362         check_default_stripe_attr --stripe-count
11363         check_default_stripe_attr --stripe-size
11364         check_default_stripe_attr --stripe-index
11365
11366         return 0
11367 }
11368 run_test 204a "Print default stripe attributes ================="
11369
11370 test_204b() {
11371         test_mkdir -p $DIR/$tdir
11372         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11373
11374         check_default_stripe_attr --stripe-size
11375         check_default_stripe_attr --stripe-index
11376
11377         return 0
11378 }
11379 run_test 204b "Print default stripe size and offset  ==========="
11380
11381 test_204c() {
11382         test_mkdir -p $DIR/$tdir
11383         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11384
11385         check_default_stripe_attr --stripe-count
11386         check_default_stripe_attr --stripe-index
11387
11388         return 0
11389 }
11390 run_test 204c "Print default stripe count and offset ==========="
11391
11392 test_204d() {
11393         test_mkdir -p $DIR/$tdir
11394         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11395
11396         check_default_stripe_attr --stripe-count
11397         check_default_stripe_attr --stripe-size
11398
11399         return 0
11400 }
11401 run_test 204d "Print default stripe count and size ============="
11402
11403 test_204e() {
11404         test_mkdir -p $DIR/$tdir
11405         $SETSTRIPE -d $DIR/$tdir
11406
11407         check_default_stripe_attr --stripe-count --raw
11408         check_default_stripe_attr --stripe-size --raw
11409         check_default_stripe_attr --stripe-index --raw
11410
11411         return 0
11412 }
11413 run_test 204e "Print raw stripe attributes ================="
11414
11415 test_204f() {
11416         test_mkdir -p $DIR/$tdir
11417         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11418
11419         check_default_stripe_attr --stripe-size --raw
11420         check_default_stripe_attr --stripe-index --raw
11421
11422         return 0
11423 }
11424 run_test 204f "Print raw stripe size and offset  ==========="
11425
11426 test_204g() {
11427         test_mkdir -p $DIR/$tdir
11428         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11429
11430         check_default_stripe_attr --stripe-count --raw
11431         check_default_stripe_attr --stripe-index --raw
11432
11433         return 0
11434 }
11435 run_test 204g "Print raw stripe count and offset ==========="
11436
11437 test_204h() {
11438         test_mkdir -p $DIR/$tdir
11439         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11440
11441         check_default_stripe_attr --stripe-count --raw
11442         check_default_stripe_attr --stripe-size --raw
11443
11444         return 0
11445 }
11446 run_test 204h "Print raw stripe count and size ============="
11447
11448 # Figure out which job scheduler is being used, if any,
11449 # or use a fake one
11450 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11451         JOBENV=SLURM_JOB_ID
11452 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11453         JOBENV=LSB_JOBID
11454 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11455         JOBENV=PBS_JOBID
11456 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11457         JOBENV=LOADL_STEP_ID
11458 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11459         JOBENV=JOB_ID
11460 else
11461         JOBENV=FAKE_JOBID
11462 fi
11463
11464 verify_jobstats() {
11465         local cmd=$1
11466         local target=$2
11467
11468         # clear old jobstats
11469         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11470         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11471
11472         # use a new JobID for this test, or we might see an old one
11473         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11474
11475         JOBVAL=${!JOBENV}
11476         log "Test: $cmd"
11477         log "Using JobID environment variable $JOBENV=$JOBVAL"
11478
11479         if [ $JOBENV = "FAKE_JOBID" ]; then
11480                 FAKE_JOBID=$JOBVAL $cmd
11481         else
11482                 $cmd
11483         fi
11484
11485         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11486                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11487                 do_facet $FACET lctl get_param mdt.*.job_stats |
11488                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11489         fi
11490         if [ "$target" = "ost" -o "$target" = "both" ]; then
11491                 FACET=ost1
11492                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11493                         grep $JOBVAL || error "No job stats found on OST $FACET"
11494         fi
11495 }
11496
11497 jobstats_set() {
11498         trap 0
11499         NEW_JOBENV=${1:-$OLD_JOBENV}
11500         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11501         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11502 }
11503
11504 test_205() { # Job stats
11505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11506         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11507         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11508                 skip "Server doesn't support jobstats" && return 0
11509         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11510
11511         local cmd
11512         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11513         if [ $OLD_JOBENV != $JOBENV ]; then
11514                 jobstats_set $JOBENV
11515                 trap jobstats_set EXIT
11516         fi
11517
11518         local user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11519                      changelog_register -n)
11520         echo "Registered as changelog user $user"
11521
11522         # mkdir
11523         cmd="mkdir $DIR/$tfile"
11524         verify_jobstats "$cmd" "mdt"
11525         # rmdir
11526         cmd="rm -fr $DIR/$tfile"
11527         verify_jobstats "$cmd" "mdt"
11528         # mknod
11529         cmd="mknod $DIR/$tfile c 1 3"
11530         verify_jobstats "$cmd" "mdt"
11531         # unlink
11532         cmd="rm -f $DIR/$tfile"
11533         verify_jobstats "$cmd" "mdt"
11534         # open & close
11535         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11536         verify_jobstats "$cmd" "mdt"
11537         # setattr
11538         cmd="touch $DIR/$tfile"
11539         verify_jobstats "$cmd" "both"
11540         # write
11541         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11542         verify_jobstats "$cmd" "ost"
11543         # read
11544         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11545         verify_jobstats "$cmd" "ost"
11546         # truncate
11547         cmd="$TRUNCATE $DIR/$tfile 0"
11548         verify_jobstats "$cmd" "both"
11549         # rename
11550         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11551         verify_jobstats "$cmd" "mdt"
11552
11553         # Ensure that jobid are present in changelog (if supported by MDS)
11554         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11555         then
11556                 $LFS changelog $MDT0 | tail -9
11557                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11558                 [ $jobids -eq 9 ] ||
11559                         error "Wrong changelog jobid count $jobids != 9"
11560
11561                 # LU-5862
11562                 JOBENV="disable"
11563                 jobstats_set $JOBENV
11564                 touch $DIR/$tfile
11565                 $LFS changelog $MDT0 | tail -1
11566                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11567                 [ $jobids -eq 0 ] ||
11568                         error "Unexpected jobids when jobid_var=$JOBENV"
11569         fi
11570
11571         # cleanup
11572         rm -f $DIR/jobstats_test_rename
11573
11574         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11575 }
11576 run_test 205 "Verify job stats"
11577
11578 # LU-1480, LU-1773 and LU-1657
11579 test_206() {
11580         mkdir -p $DIR/$tdir
11581         $SETSTRIPE -c -1 $DIR/$tdir
11582 #define OBD_FAIL_LOV_INIT 0x1403
11583         $LCTL set_param fail_loc=0xa0001403
11584         $LCTL set_param fail_val=1
11585         touch $DIR/$tdir/$tfile || true
11586 }
11587 run_test 206 "fail lov_init_raid0() doesn't lbug"
11588
11589 test_207a() {
11590         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11591         local fsz=`stat -c %s $DIR/$tfile`
11592         cancel_lru_locks mdc
11593
11594         # do not return layout in getattr intent
11595 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11596         $LCTL set_param fail_loc=0x170
11597         local sz=`stat -c %s $DIR/$tfile`
11598
11599         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11600
11601         rm -rf $DIR/$tfile
11602 }
11603 run_test 207a "can refresh layout at glimpse"
11604
11605 test_207b() {
11606         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11607         local cksum=`md5sum $DIR/$tfile`
11608         local fsz=`stat -c %s $DIR/$tfile`
11609         cancel_lru_locks mdc
11610         cancel_lru_locks osc
11611
11612         # do not return layout in getattr intent
11613 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11614         $LCTL set_param fail_loc=0x171
11615
11616         # it will refresh layout after the file is opened but before read issues
11617         echo checksum is "$cksum"
11618         echo "$cksum" |md5sum -c --quiet || error "file differs"
11619
11620         rm -rf $DIR/$tfile
11621 }
11622 run_test 207b "can refresh layout at open"
11623
11624 test_208() {
11625         # FIXME: in this test suite, only RD lease is used. This is okay
11626         # for now as only exclusive open is supported. After generic lease
11627         # is done, this test suite should be revised. - Jinshan
11628
11629         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11630                 { skip "Need MDS version at least 2.4.52"; return 0; }
11631         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11632
11633         echo "==== test 1: verify get lease work"
11634         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11635
11636         echo "==== test 2: verify lease can be broken by upcoming open"
11637         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11638         local PID=$!
11639         sleep 1
11640
11641         $MULTIOP $DIR/$tfile oO_RDONLY:c
11642         kill -USR1 $PID && wait $PID || error "break lease error"
11643
11644         echo "==== test 3: verify lease can't be granted if an open already exists"
11645         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11646         local PID=$!
11647         sleep 1
11648
11649         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11650         kill -USR1 $PID && wait $PID || error "open file error"
11651
11652         echo "==== test 4: lease can sustain over recovery"
11653         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11654         PID=$!
11655         sleep 1
11656
11657         fail mds1
11658
11659         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11660
11661         echo "==== test 5: lease broken can't be regained by replay"
11662         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11663         PID=$!
11664         sleep 1
11665
11666         # open file to break lease and then recovery
11667         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11668         fail mds1
11669
11670         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11671
11672         rm -f $DIR/$tfile
11673 }
11674 run_test 208 "Exclusive open"
11675
11676 test_209() {
11677         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11678                 skip_env "must have disp_stripe" && return
11679
11680         touch $DIR/$tfile
11681         sync; sleep 5; sync;
11682
11683         echo 3 > /proc/sys/vm/drop_caches
11684         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11685
11686         # open/close 500 times
11687         for i in $(seq 500); do
11688                 cat $DIR/$tfile
11689         done
11690
11691         echo 3 > /proc/sys/vm/drop_caches
11692         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11693
11694         echo "before: $req_before, after: $req_after"
11695         [ $((req_after - req_before)) -ge 300 ] &&
11696                 error "open/close requests are not freed"
11697         return 0
11698 }
11699 run_test 209 "read-only open/close requests should be freed promptly"
11700
11701 test_212() {
11702         size=`date +%s`
11703         size=$((size % 8192 + 1))
11704         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11705         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11706         rm -f $DIR/f212 $DIR/f212.xyz
11707 }
11708 run_test 212 "Sendfile test ============================================"
11709
11710 test_213() {
11711         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11712         cancel_lru_locks osc
11713         lctl set_param fail_loc=0x8000040f
11714         # generate a read lock
11715         cat $DIR/$tfile > /dev/null
11716         # write to the file, it will try to cancel the above read lock.
11717         cat /etc/hosts >> $DIR/$tfile
11718 }
11719 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11720
11721 test_214() { # for bug 20133
11722         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11723         for (( i=0; i < 340; i++ )) ; do
11724                 touch $DIR/$tdir/d214c/a$i
11725         done
11726
11727         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11728         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11729         ls $DIR/d214c || error "ls $DIR/d214c failed"
11730         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11731         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11732 }
11733 run_test 214 "hash-indexed directory test - bug 20133"
11734
11735 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11736 create_lnet_proc_files() {
11737         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11738         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11739
11740         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11741         rm -f "$TMP/lnet_$1.sys_tmp"
11742 }
11743
11744 # counterpart of create_lnet_proc_files
11745 remove_lnet_proc_files() {
11746         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11747 }
11748
11749 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11750 # 3rd arg as regexp for body
11751 check_lnet_proc_stats() {
11752         local l=$(cat "$TMP/lnet_$1" |wc -l)
11753         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11754
11755         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11756 }
11757
11758 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11759 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11760 # optional and can be regexp for 2nd line (lnet.routes case)
11761 check_lnet_proc_entry() {
11762         local blp=2          # blp stands for 'position of 1st line of body'
11763         [ -z "$5" ] || blp=3 # lnet.routes case
11764
11765         local l=$(cat "$TMP/lnet_$1" |wc -l)
11766         # subtracting one from $blp because the body can be empty
11767         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11768
11769         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11770                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11771
11772         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11773                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11774
11775         # bail out if any unexpected line happened
11776         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11777         [ "$?" != 0 ] || error "$2 misformatted"
11778 }
11779
11780 test_215() { # for bugs 18102, 21079, 21517
11781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11782         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11783         local P='[1-9][0-9]*'           # positive numeric
11784         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11785         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11786         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11787         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11788
11789         local L1 # regexp for 1st line
11790         local L2 # regexp for 2nd line (optional)
11791         local BR # regexp for the rest (body)
11792
11793         # lnet.stats should look as 11 space-separated non-negative numerics
11794         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11795         create_lnet_proc_files "stats"
11796         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11797         remove_lnet_proc_files "stats"
11798
11799         # lnet.routes should look like this:
11800         # Routing disabled/enabled
11801         # net hops priority state router
11802         # where net is a string like tcp0, hops > 0, priority >= 0,
11803         # state is up/down,
11804         # router is a string like 192.168.1.1@tcp2
11805         L1="^Routing (disabled|enabled)$"
11806         L2="^net +hops +priority +state +router$"
11807         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11808         create_lnet_proc_files "routes"
11809         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11810         remove_lnet_proc_files "routes"
11811
11812         # lnet.routers should look like this:
11813         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11814         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11815         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11816         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11817         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11818         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11819         create_lnet_proc_files "routers"
11820         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11821         remove_lnet_proc_files "routers"
11822
11823         # lnet.peers should look like this:
11824         # nid refs state last max rtr min tx min queue
11825         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11826         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11827         # numeric (0 or >0 or <0), queue >= 0.
11828         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11829         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11830         create_lnet_proc_files "peers"
11831         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11832         remove_lnet_proc_files "peers"
11833
11834         # lnet.buffers  should look like this:
11835         # pages count credits min
11836         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11837         L1="^pages +count +credits +min$"
11838         BR="^ +$N +$N +$I +$I$"
11839         create_lnet_proc_files "buffers"
11840         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11841         remove_lnet_proc_files "buffers"
11842
11843         # lnet.nis should look like this:
11844         # nid status alive refs peer rtr max tx min
11845         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11846         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11847         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11848         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11849         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11850         create_lnet_proc_files "nis"
11851         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11852         remove_lnet_proc_files "nis"
11853
11854         # can we successfully write to lnet.stats?
11855         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
11856         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11857 }
11858 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
11859
11860 test_216() { # bug 20317
11861         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11862         remote_ost_nodsh && skip "remote OST with nodsh" && return
11863
11864         local node
11865         local facets=$(get_facets OST)
11866         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11867
11868         save_lustre_params client "osc.*.contention_seconds" > $p
11869         save_lustre_params $facets \
11870                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11871         save_lustre_params $facets \
11872                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11873         save_lustre_params $facets \
11874                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11875         clear_osc_stats
11876
11877         # agressive lockless i/o settings
11878         for node in $(osts_nodes); do
11879                 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'
11880         done
11881         lctl set_param -n osc.*.contention_seconds 60
11882
11883         $DIRECTIO write $DIR/$tfile 0 10 4096
11884         $CHECKSTAT -s 40960 $DIR/$tfile
11885
11886         # disable lockless i/o
11887         for node in $(osts_nodes); do
11888                 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'
11889         done
11890         lctl set_param -n osc.*.contention_seconds 0
11891         clear_osc_stats
11892
11893         dd if=/dev/zero of=$DIR/$tfile count=0
11894         $CHECKSTAT -s 0 $DIR/$tfile
11895
11896         restore_lustre_params <$p
11897         rm -f $p
11898         rm $DIR/$tfile
11899 }
11900 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11901
11902 test_217() { # bug 22430
11903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11904         local node
11905         local nid
11906
11907         for node in $(nodes_list); do
11908                 nid=$(host_nids_address $node $NETTYPE)
11909                 if [[ $nid = *-* ]] ; then
11910                         echo "lctl ping $nid@$NETTYPE"
11911                         lctl ping $nid@$NETTYPE
11912                 else
11913                         echo "skipping $node (no hyphen detected)"
11914                 fi
11915         done
11916 }
11917 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11918
11919 test_218() {
11920        # do directio so as not to populate the page cache
11921        log "creating a 10 Mb file"
11922        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11923        log "starting reads"
11924        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11925        log "truncating the file"
11926        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11927        log "killing dd"
11928        kill %+ || true # reads might have finished
11929        echo "wait until dd is finished"
11930        wait
11931        log "removing the temporary file"
11932        rm -rf $DIR/$tfile || error "tmp file removal failed"
11933 }
11934 run_test 218 "parallel read and truncate should not deadlock ======================="
11935
11936 test_219() {
11937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11938         # write one partial page
11939         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11940         # set no grant so vvp_io_commit_write will do sync write
11941         $LCTL set_param fail_loc=0x411
11942         # write a full page at the end of file
11943         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11944
11945         $LCTL set_param fail_loc=0
11946         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11947         $LCTL set_param fail_loc=0x411
11948         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11949
11950         # LU-4201
11951         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11952         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11953 }
11954 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11955
11956 test_220() { #LU-325
11957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11958         remote_ost_nodsh && skip "remote OST with nodsh" && return
11959         local OSTIDX=0
11960
11961         test_mkdir -p $DIR/$tdir
11962         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11963                 awk '{print $2}' | sed -e 's/_UUID$//')
11964
11965         # on the mdt's osc
11966         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11967         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11968                         osc.$mdtosc_proc1.prealloc_last_id)
11969         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11970                         osc.$mdtosc_proc1.prealloc_next_id)
11971
11972         $LFS df -i
11973
11974         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11975         #define OBD_FAIL_OST_ENOINO              0x229
11976         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11977         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11978         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11979
11980         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11981
11982         MDSOBJS=$((last_id - next_id))
11983         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11984
11985         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11986         echo "OST still has $count kbytes free"
11987
11988         echo "create $MDSOBJS files @next_id..."
11989         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11990
11991         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11992                         osc.$mdtosc_proc1.prealloc_last_id)
11993         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11994                         osc.$mdtosc_proc1.prealloc_next_id)
11995
11996         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11997         $LFS df -i
11998
11999         echo "cleanup..."
12000
12001         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12002         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12003
12004         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12005         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12006         echo "unlink $MDSOBJS files @$next_id..."
12007         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12008 }
12009 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12010
12011 test_221() {
12012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12013         dd if=`which date` of=$MOUNT/date oflag=sync
12014         chmod +x $MOUNT/date
12015
12016         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12017         $LCTL set_param fail_loc=0x80001401
12018
12019         $MOUNT/date > /dev/null
12020         rm -f $MOUNT/date
12021 }
12022 run_test 221 "make sure fault and truncate race to not cause OOM"
12023
12024 test_222a () {
12025         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12026        rm -rf $DIR/$tdir
12027        test_mkdir -p $DIR/$tdir
12028        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12029        createmany -o $DIR/$tdir/$tfile 10
12030        cancel_lru_locks mdc
12031        cancel_lru_locks osc
12032        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12033        $LCTL set_param fail_loc=0x31a
12034        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12035        $LCTL set_param fail_loc=0
12036        rm -r $DIR/$tdir
12037 }
12038 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12039
12040 test_222b () {
12041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12042        rm -rf $DIR/$tdir
12043        test_mkdir -p $DIR/$tdir
12044        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12045        createmany -o $DIR/$tdir/$tfile 10
12046        cancel_lru_locks mdc
12047        cancel_lru_locks osc
12048        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12049        $LCTL set_param fail_loc=0x31a
12050        rm -r $DIR/$tdir || "AGL for rmdir failed"
12051        $LCTL set_param fail_loc=0
12052 }
12053 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12054
12055 test_223 () {
12056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12057        rm -rf $DIR/$tdir
12058        test_mkdir -p $DIR/$tdir
12059        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12060        createmany -o $DIR/$tdir/$tfile 10
12061        cancel_lru_locks mdc
12062        cancel_lru_locks osc
12063        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12064        $LCTL set_param fail_loc=0x31b
12065        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12066        $LCTL set_param fail_loc=0
12067        rm -r $DIR/$tdir
12068 }
12069 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12070
12071 test_224a() { # LU-1039, MRP-303
12072         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12073         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12074         $LCTL set_param fail_loc=0x508
12075         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12076         $LCTL set_param fail_loc=0
12077         df $DIR
12078 }
12079 run_test 224a "Don't panic on bulk IO failure"
12080
12081 test_224b() { # LU-1039, MRP-303
12082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12083         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12084         cancel_lru_locks osc
12085         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12086         $LCTL set_param fail_loc=0x515
12087         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12088         $LCTL set_param fail_loc=0
12089         df $DIR
12090 }
12091 run_test 224b "Don't panic on bulk IO failure"
12092
12093 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12094 test_225a () {
12095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12096         if [ -z ${MDSSURVEY} ]; then
12097               skip_env "mds-survey not found" && return
12098         fi
12099
12100         [ $MDSCOUNT -ge 2 ] &&
12101                 skip "skipping now for more than one MDT" && return
12102
12103        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12104             { skip "Need MDS version at least 2.2.51"; return; }
12105
12106        local mds=$(facet_host $SINGLEMDS)
12107        local target=$(do_nodes $mds 'lctl dl' | \
12108                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12109
12110        local cmd1="file_count=1000 thrhi=4"
12111        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12112        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12113        local cmd="$cmd1 $cmd2 $cmd3"
12114
12115        rm -f ${TMP}/mds_survey*
12116        echo + $cmd
12117        eval $cmd || error "mds-survey with zero-stripe failed"
12118        cat ${TMP}/mds_survey*
12119        rm -f ${TMP}/mds_survey*
12120 }
12121 run_test 225a "Metadata survey sanity with zero-stripe"
12122
12123 test_225b () {
12124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12125
12126         if [ -z ${MDSSURVEY} ]; then
12127               skip_env "mds-survey not found" && return
12128         fi
12129         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12130             { skip "Need MDS version at least 2.2.51"; return; }
12131
12132         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12133               skip_env "Need to mount OST to test" && return
12134         fi
12135
12136         [ $MDSCOUNT -ge 2 ] &&
12137                 skip "skipping now for more than one MDT" && return
12138        local mds=$(facet_host $SINGLEMDS)
12139        local target=$(do_nodes $mds 'lctl dl' | \
12140                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12141
12142        local cmd1="file_count=1000 thrhi=4"
12143        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12144        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12145        local cmd="$cmd1 $cmd2 $cmd3"
12146
12147        rm -f ${TMP}/mds_survey*
12148        echo + $cmd
12149        eval $cmd || error "mds-survey with stripe_count failed"
12150        cat ${TMP}/mds_survey*
12151        rm -f ${TMP}/mds_survey*
12152 }
12153 run_test 225b "Metadata survey sanity with stripe_count = 1"
12154
12155 mcreate_path2fid () {
12156         local mode=$1
12157         local major=$2
12158         local minor=$3
12159         local name=$4
12160         local desc=$5
12161         local path=$DIR/$tdir/$name
12162         local fid
12163         local rc
12164         local fid_path
12165
12166         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12167                 error "cannot create $desc"
12168
12169         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12170         rc=$?
12171         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12172
12173         fid_path=$($LFS fid2path $MOUNT $fid)
12174         rc=$?
12175         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12176
12177         [ "$path" == "$fid_path" ] ||
12178                 error "fid2path returned $fid_path, expected $path"
12179
12180         echo "pass with $path and $fid"
12181 }
12182
12183 test_226a () {
12184         rm -rf $DIR/$tdir
12185         mkdir -p $DIR/$tdir
12186
12187         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12188         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12189         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12190         mcreate_path2fid 0040666 0 0 dir "directory"
12191         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12192         mcreate_path2fid 0100666 0 0 file "regular file"
12193         mcreate_path2fid 0120666 0 0 link "symbolic link"
12194         mcreate_path2fid 0140666 0 0 sock "socket"
12195 }
12196 run_test 226a "call path2fid and fid2path on files of all type"
12197
12198 test_226b () {
12199         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12200         rm -rf $DIR/$tdir
12201         local MDTIDX=1
12202
12203         mkdir -p $DIR/$tdir
12204         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12205                 error "create remote directory failed"
12206         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12207         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12208                                 "character special file (null)"
12209         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12210                                 "character special file (no device)"
12211         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12212         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12213                                 "block special file (loop)"
12214         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12215         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12216         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12217 }
12218 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12219
12220 # LU-1299 Executing or running ldd on a truncated executable does not
12221 # cause an out-of-memory condition.
12222 test_227() {
12223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12224         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12225         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12226         chmod +x $MOUNT/date
12227
12228         $MOUNT/date > /dev/null
12229         ldd $MOUNT/date > /dev/null
12230         rm -f $MOUNT/date
12231 }
12232 run_test 227 "running truncated executable does not cause OOM"
12233
12234 # LU-1512 try to reuse idle OI blocks
12235 test_228a() {
12236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12237         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12238         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12239                 skip "non-ldiskfs backend" && return
12240
12241         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12242         local myDIR=$DIR/$tdir
12243
12244         mkdir -p $myDIR
12245         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12246         $LCTL set_param fail_loc=0x80001002
12247         createmany -o $myDIR/t- 10000
12248         $LCTL set_param fail_loc=0
12249         # The guard is current the largest FID holder
12250         touch $myDIR/guard
12251         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12252                     tr -d '[')
12253         local IDX=$(($SEQ % 64))
12254
12255         do_facet $SINGLEMDS sync
12256         # Make sure journal flushed.
12257         sleep 6
12258         local blk1=$(do_facet $SINGLEMDS \
12259                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12260                      grep Blockcount | awk '{print $4}')
12261
12262         # Remove old files, some OI blocks will become idle.
12263         unlinkmany $myDIR/t- 10000
12264         # Create new files, idle OI blocks should be reused.
12265         createmany -o $myDIR/t- 2000
12266         do_facet $SINGLEMDS sync
12267         # Make sure journal flushed.
12268         sleep 6
12269         local blk2=$(do_facet $SINGLEMDS \
12270                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12271                      grep Blockcount | awk '{print $4}')
12272
12273         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12274 }
12275 run_test 228a "try to reuse idle OI blocks"
12276
12277 test_228b() {
12278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12279         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12280         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12281                 skip "non-ldiskfs backend" && return
12282
12283         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12284         local myDIR=$DIR/$tdir
12285
12286         mkdir -p $myDIR
12287         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12288         $LCTL set_param fail_loc=0x80001002
12289         createmany -o $myDIR/t- 10000
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- 10000
12306
12307         # stop the MDT
12308         stop $SINGLEMDS || error "Fail to stop MDT."
12309         # remount the MDT
12310         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12311
12312         df $MOUNT || error "Fail to df."
12313         # Create new files, idle OI blocks should be reused.
12314         createmany -o $myDIR/t- 2000
12315         do_facet $SINGLEMDS sync
12316         # Make sure journal flushed.
12317         sleep 6
12318         local blk2=$(do_facet $SINGLEMDS \
12319                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12320                      grep Blockcount | awk '{print $4}')
12321
12322         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12323 }
12324 run_test 228b "idle OI blocks can be reused after MDT restart"
12325
12326 #LU-1881
12327 test_228c() {
12328         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12329         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12330         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12331                 skip "non-ldiskfs backend" && return
12332
12333         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12334         local myDIR=$DIR/$tdir
12335
12336         mkdir -p $myDIR
12337         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12338         $LCTL set_param fail_loc=0x80001002
12339         # 20000 files can guarantee there are index nodes in the OI file
12340         createmany -o $myDIR/t- 20000
12341         $LCTL set_param fail_loc=0
12342         # The guard is current the largest FID holder
12343         touch $myDIR/guard
12344         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12345                     tr -d '[')
12346         local IDX=$(($SEQ % 64))
12347
12348         do_facet $SINGLEMDS sync
12349         # Make sure journal flushed.
12350         sleep 6
12351         local blk1=$(do_facet $SINGLEMDS \
12352                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12353                      grep Blockcount | awk '{print $4}')
12354
12355         # Remove old files, some OI blocks will become idle.
12356         unlinkmany $myDIR/t- 20000
12357         rm -f $myDIR/guard
12358         # The OI file should become empty now
12359
12360         # Create new files, idle OI blocks should be reused.
12361         createmany -o $myDIR/t- 2000
12362         do_facet $SINGLEMDS sync
12363         # Make sure journal flushed.
12364         sleep 6
12365         local blk2=$(do_facet $SINGLEMDS \
12366                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12367                      grep Blockcount | awk '{print $4}')
12368
12369         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12370 }
12371 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12372
12373 test_229() { # LU-2482, LU-3448
12374         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12375                 skip "No HSM support on MDS of $(get_lustre_version)," \
12376                          "need 2.4.53 at least" && return
12377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12378         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12379
12380         rm -f $DIR/$tfile
12381
12382         # Create a file with a released layout and stripe count 2.
12383         $MULTIOP $DIR/$tfile H2c ||
12384                 error "failed to create file with released layout"
12385
12386         $GETSTRIPE -v $DIR/$tfile
12387
12388         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12389         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12390
12391         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12392         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12393         stat $DIR/$tfile || error "failed to stat released file"
12394
12395         chown $RUNAS_ID $DIR/$tfile ||
12396                 error "chown $RUNAS_ID $DIR/$tfile failed"
12397
12398         chgrp $RUNAS_ID $DIR/$tfile ||
12399                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12400
12401         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12402         rm $DIR/$tfile || error "failed to remove released file"
12403 }
12404 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12405
12406 test_230a() {
12407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12408         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12409         local MDTIDX=1
12410
12411         test_mkdir $DIR/$tdir
12412         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
12413         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12414         [ $mdt_idx -ne 0 ] &&
12415                 error "create local directory on wrong MDT $mdt_idx"
12416
12417         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12418                         error "create remote directory failed"
12419         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12420         [ $mdt_idx -ne $MDTIDX ] &&
12421                 error "create remote directory on wrong MDT $mdt_idx"
12422
12423         createmany -o $DIR/$tdir/test_230/t- 10 ||
12424                 error "create files on remote directory failed"
12425         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12426         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12427         rm -r $DIR/$tdir || error "unlink remote directory failed"
12428 }
12429 run_test 230a "Create remote directory and files under the remote directory"
12430
12431 test_230b() {
12432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12433         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12434         local MDTIDX=1
12435         local mdt_index
12436         local i
12437         local file
12438         local pid
12439         local stripe_count
12440         local migrate_dir=$DIR/$tdir/migrate_dir
12441         local other_dir=$DIR/$tdir/other_dir
12442
12443         test_mkdir $DIR/$tdir
12444         test_mkdir -i0 -c1 $migrate_dir
12445         test_mkdir -i0 -c1 $other_dir
12446         for ((i=0; i<10; i++)); do
12447                 mkdir -p $migrate_dir/dir_${i}
12448                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12449                         error "create files under remote dir failed $i"
12450         done
12451
12452         cp /etc/passwd $migrate_dir/$tfile
12453         cp /etc/passwd $other_dir/$tfile
12454         chattr +SAD $migrate_dir
12455         chattr +SAD $migrate_dir/$tfile
12456
12457         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12458         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12459         local old_dir_mode=$(stat -c%f $migrate_dir)
12460         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12461
12462         mkdir -p $migrate_dir/dir_default_stripe2
12463         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12464         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12465
12466         mkdir -p $other_dir
12467         ln $migrate_dir/$tfile $other_dir/luna
12468         ln $migrate_dir/$tfile $migrate_dir/sofia
12469         ln $other_dir/$tfile $migrate_dir/david
12470         ln -s $migrate_dir/$tfile $other_dir/zachary
12471         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12472         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12473
12474         $LFS mv -v -M $MDTIDX $migrate_dir ||
12475                 error "migrate remote dir error"
12476
12477         echo "migratate to MDT1, then checking.."
12478         for ((i = 0; i < 10; i++)); do
12479                 for file in $(find $migrate_dir/dir_${i}); do
12480                         mdt_index=$($LFS getstripe -M $file)
12481                         [ $mdt_index == $MDTIDX ] ||
12482                                 error "$file is not on MDT${MDTIDX}"
12483                 done
12484         done
12485
12486         # the multiple link file should still in MDT0
12487         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12488         [ $mdt_index == 0 ] ||
12489                 error "$file is not on MDT${MDTIDX}"
12490
12491         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12492         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12493                 error " expect $old_dir_flag get $new_dir_flag"
12494
12495         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12496         [ "$old_file_flag" = "$new_file_flag" ] ||
12497                 error " expect $old_file_flag get $new_file_flag"
12498
12499         local new_dir_mode=$(stat -c%f $migrate_dir)
12500         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12501                 error "expect mode $old_dir_mode get $new_dir_mode"
12502
12503         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12504         [ "$old_file_mode" = "$new_file_mode" ] ||
12505                 error "expect mode $old_file_mode get $new_file_mode"
12506
12507         diff /etc/passwd $migrate_dir/$tfile ||
12508                 error "$tfile different after migration"
12509
12510         diff /etc/passwd $other_dir/luna ||
12511                 error "luna different after migration"
12512
12513         diff /etc/passwd $migrate_dir/sofia ||
12514                 error "sofia different after migration"
12515
12516         diff /etc/passwd $migrate_dir/david ||
12517                 error "david different after migration"
12518
12519         diff /etc/passwd $other_dir/zachary ||
12520                 error "zachary different after migration"
12521
12522         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12523                 error "${tfile}_ln different after migration"
12524
12525         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12526                 error "${tfile}_ln_other different after migration"
12527
12528         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12529         [ $stripe_count = 2 ] ||
12530                 error "dir strpe_count $d != 2 after migration."
12531
12532         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12533         [ $stripe_count = 2 ] ||
12534                 error "file strpe_count $d != 2 after migration."
12535
12536         #migrate back to MDT0
12537         MDTIDX=0
12538         $LFS mv -v -M $MDTIDX $migrate_dir ||
12539                 error "migrate remote dir error"
12540
12541         echo "migrate back to MDT0, checking.."
12542         for file in $(find $migrate_dir); do
12543                 mdt_index=$($LFS getstripe -M $file)
12544                 [ $mdt_index == $MDTIDX ] ||
12545                         error "$file is not on MDT${MDTIDX}"
12546         done
12547
12548         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12549         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12550                 error " expect $old_dir_flag get $new_dir_flag"
12551
12552         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12553         [ "$old_file_flag" = "$new_file_flag" ] ||
12554                 error " expect $old_file_flag get $new_file_flag"
12555
12556         local new_dir_mode=$(stat -c%f $migrate_dir)
12557         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12558                 error "expect mode $old_dir_mode get $new_dir_mode"
12559
12560         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12561         [ "$old_file_mode" = "$new_file_mode" ] ||
12562                 error "expect mode $old_file_mode get $new_file_mode"
12563
12564         diff /etc/passwd ${migrate_dir}/$tfile ||
12565                 error "$tfile different after migration"
12566
12567         diff /etc/passwd ${other_dir}/luna ||
12568                 error "luna different after migration"
12569
12570         diff /etc/passwd ${migrate_dir}/sofia ||
12571                 error "sofia different after migration"
12572
12573         diff /etc/passwd ${other_dir}/zachary ||
12574                 error "zachary different after migration"
12575
12576         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12577                 error "${tfile}_ln different after migration"
12578
12579         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12580                 error "${tfile}_ln_other different after migration"
12581
12582         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12583         [ $stripe_count = 2 ] ||
12584                 error "dir strpe_count $d != 2 after migration."
12585
12586         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12587         [ $stripe_count = 2 ] ||
12588                 error "file strpe_count $d != 2 after migration."
12589
12590         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12591 }
12592 run_test 230b "migrate directory"
12593
12594 test_230c() {
12595         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12596         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12597         local MDTIDX=1
12598         local mdt_index
12599         local file
12600         local migrate_dir=$DIR/$tdir/migrate_dir
12601
12602         #If migrating directory fails in the middle, all entries of
12603         #the directory is still accessiable.
12604         test_mkdir $DIR/$tdir
12605         test_mkdir -i0 -c1 $migrate_dir
12606         stat $migrate_dir
12607         createmany -o $migrate_dir/f 10 ||
12608                 error "create files under ${migrate_dir} failed"
12609
12610         #failed after migrating 5 entries
12611         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12612         do_facet mds1 lctl set_param fail_loc=0x20001801
12613         do_facet mds1 lctl  set_param fail_val=5
12614         local t=$(ls $migrate_dir | wc -l)
12615         $LFS mv --mdt-index $MDTIDX $migrate_dir &&
12616                 error "migrate should fail after 5 entries"
12617         local u=$(ls $migrate_dir | wc -l)
12618         [ "$u" == "$t" ] || error "$u != $t during migration"
12619
12620         for file in $(find $migrate_dir); do
12621                 stat $file || error "stat $file failed"
12622         done
12623
12624         do_facet mds1 lctl set_param fail_loc=0
12625         do_facet mds1 lctl set_param fail_val=0
12626
12627         $LFS mv -M $MDTIDX $migrate_dir ||
12628                 error "migrate open files should failed with open files"
12629
12630         echo "Finish migration, then checking.."
12631         for file in $(find $migrate_dir); do
12632                 mdt_index=$($LFS getstripe -M $file)
12633                 [ $mdt_index == $MDTIDX ] ||
12634                         error "$file is not on MDT${MDTIDX}"
12635         done
12636
12637         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12638 }
12639 run_test 230c "check directory accessiblity if migration is failed"
12640
12641 test_230d() {
12642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12643         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12644         local MDTIDX=1
12645         local mdt_index
12646         local migrate_dir=$DIR/$tdir/migrate_dir
12647         local i
12648         local j
12649
12650         test_mkdir $DIR/$tdir
12651         test_mkdir -i0 -c1 $migrate_dir
12652
12653         for ((i=0; i<100; i++)); do
12654                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
12655                 createmany -o $migrate_dir/dir_${i}/f 100 ||
12656                         error "create files under remote dir failed $i"
12657         done
12658
12659         $LFS mv -M $MDTIDX -v $migrate_dir || error "migrate remote dir error"
12660
12661         echo "Finish migration, then checking.."
12662         for file in $(find $migrate_dir); do
12663                 mdt_index=$($LFS getstripe -M $file)
12664                 [ $mdt_index == $MDTIDX ] ||
12665                         error "$file is not on MDT${MDTIDX}"
12666         done
12667
12668         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12669 }
12670 run_test 230d "check migrate big directory"
12671
12672 test_231a()
12673 {
12674         # For simplicity this test assumes that max_pages_per_rpc
12675         # is the same across all OSCs
12676         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12677         local bulk_size=$((max_pages * 4096))
12678
12679         mkdir -p $DIR/$tdir
12680
12681         # clear the OSC stats
12682         $LCTL set_param osc.*.stats=0 &>/dev/null
12683
12684         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12685         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12686                 oflag=direct &>/dev/null || error "dd failed"
12687
12688         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12689         if [ x$nrpcs != "x1" ]; then
12690                 error "found $nrpc ost_write RPCs, not 1 as expected"
12691         fi
12692
12693         # Drop the OSC cache, otherwise we will read from it
12694         cancel_lru_locks osc
12695
12696         # clear the OSC stats
12697         $LCTL set_param osc.*.stats=0 &>/dev/null
12698
12699         # Client reads $bulk_size.
12700         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12701                 iflag=direct &>/dev/null || error "dd failed"
12702
12703         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12704         if [ x$nrpcs != "x1" ]; then
12705                 error "found $nrpc ost_read RPCs, not 1 as expected"
12706         fi
12707 }
12708 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12709
12710 test_231b() {
12711         mkdir -p $DIR/$tdir
12712         local i
12713         for i in {0..1023}; do
12714                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12715                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12716                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12717         done
12718         sync
12719 }
12720 run_test 231b "must not assert on fully utilized OST request buffer"
12721
12722 test_232() {
12723         mkdir -p $DIR/$tdir
12724         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12725         $LCTL set_param fail_loc=0x31c
12726
12727         # ignore dd failure
12728         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12729
12730         $LCTL set_param fail_loc=0
12731         umount_client $MOUNT || error "umount failed"
12732         mount_client $MOUNT || error "mount failed"
12733 }
12734 run_test 232 "failed lock should not block umount"
12735
12736 test_233a() {
12737         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12738         { skip "Need MDS version at least 2.3.64"; return; }
12739
12740         local fid=$($LFS path2fid $MOUNT)
12741         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12742                 error "cannot access $MOUNT using its FID '$fid'"
12743 }
12744 run_test 233a "checking that OBF of the FS root succeeds"
12745
12746 test_233b() {
12747         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12748         { skip "Need MDS version at least 2.5.90"; return; }
12749
12750         local fid=$($LFS path2fid $MOUNT/.lustre)
12751         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12752                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12753
12754         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12755         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12756                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12757 }
12758 run_test 233b "checking that OBF of the FS .lustre succeeds"
12759
12760 test_234() {
12761         local p="$TMP/sanityN-$TESTNAME.parameters"
12762         save_lustre_params client "llite.*.xattr_cache" > $p
12763         lctl set_param llite.*.xattr_cache 1 ||
12764                 { skip "xattr cache is not supported"; return 0; }
12765
12766         mkdir -p $DIR/$tdir || error "mkdir failed"
12767         touch $DIR/$tdir/$tfile || error "touch failed"
12768         # OBD_FAIL_LLITE_XATTR_ENOMEM
12769         $LCTL set_param fail_loc=0x1405
12770         # output of the form: attr 2 4 44 3 fc13 x86_64
12771         V=($(IFS=".-" rpm -q attr))
12772         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
12773               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
12774                 # attr pre-2.4.44-7 had a bug with rc
12775                 # LU-3703 - SLES 11 and FC13 clients have older attr
12776                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12777                         error "getfattr should have failed with ENOMEM"
12778         else
12779                 skip "LU-3703: attr version $(getfattr --version) too old"
12780         fi
12781         $LCTL set_param fail_loc=0x0
12782         rm -rf $DIR/$tdir
12783
12784         restore_lustre_params < $p
12785         rm -f $p
12786 }
12787 run_test 234 "xattr cache should not crash on ENOMEM"
12788
12789 test_235() {
12790         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12791                 skip "Need MDS version at least 2.4.52" && return
12792         flock_deadlock $DIR/$tfile
12793         local RC=$?
12794         case $RC in
12795                 0)
12796                 ;;
12797                 124) error "process hangs on a deadlock"
12798                 ;;
12799                 *) error "error executing flock_deadlock $DIR/$tfile"
12800                 ;;
12801         esac
12802 }
12803 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12804
12805 #LU-2935
12806 test_236() {
12807         check_swap_layouts_support && return 0
12808         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12809
12810         local ref1=/etc/passwd
12811         local ref2=/etc/group
12812         local file1=$DIR/$tdir/f1
12813         local file2=$DIR/$tdir/f2
12814
12815         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12816         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12817         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12818         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12819         local fd=$(free_fd)
12820         local cmd="exec $fd<>$file2"
12821         eval $cmd
12822         rm $file2
12823         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12824                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12825         cmd="exec $fd>&-"
12826         eval $cmd
12827         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12828
12829         #cleanup
12830         rm -rf $DIR/$tdir
12831 }
12832 run_test 236 "Layout swap on open unlinked file"
12833
12834 # test to verify file handle related system calls
12835 # (name_to_handle_at/open_by_handle_at)
12836 # The new system calls are supported in glibc >= 2.14.
12837
12838 test_237() {
12839         echo "Test file_handle syscalls" > $DIR/$tfile ||
12840                 error "write failed"
12841         check_fhandle_syscalls $DIR/$tfile ||
12842                 error "check_fhandle_syscalls failed"
12843 }
12844 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12845
12846 # LU-4659 linkea consistency
12847 test_238() {
12848         local server_version=$(lustre_version_code $SINGLEMDS)
12849
12850         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12851                 [[ $server_version -gt $(version_code 2.5.1) &&
12852                    $server_version -lt $(version_code 2.5.50) ]] ||
12853                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12854
12855         touch $DIR/$tfile
12856         ln $DIR/$tfile $DIR/$tfile.lnk
12857         touch $DIR/$tfile.new
12858         mv $DIR/$tfile.new $DIR/$tfile
12859         local fid1=$(lfs path2fid $DIR/$tfile)
12860         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12861         local path1=$(lfs fid2path $FSNAME $fid1)
12862         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12863         local path2=$(lfs fid2path $FSNAME $fid2)
12864         [ $tfile.lnk == $path2 ] ||
12865                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12866         rm -f $DIR/$tfile*
12867 }
12868 run_test 238 "Verify linkea consistency"
12869
12870 test_239() {
12871         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
12872                 skip "Need MDS version at least 2.5.60" && return
12873         local list=$(comma_list $(mdts_nodes))
12874
12875         mkdir -p $DIR/$tdir
12876         createmany -o $DIR/$tdir/f- 5000
12877         unlinkmany $DIR/$tdir/f- 5000
12878         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12879         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12880                         osc.*MDT*.sync_in_flight" | calc_sum)
12881         [ "$changes" -eq 0 ] || error "$changes not synced"
12882 }
12883 run_test 239 "osp_sync test"
12884
12885 test_240() {
12886         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12887
12888         mkdir -p $DIR/$tdir
12889
12890         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12891                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12892         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12893                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12894
12895         umount_client $MOUNT || error "umount failed"
12896         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12897         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12898         mount_client $MOUNT || error "failed to mount client"
12899
12900         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12901         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12902 }
12903 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12904
12905 test_241_bio() {
12906         for LOOP in $(seq $1); do
12907                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
12908                 cancel_lru_locks osc
12909         done
12910 }
12911
12912 test_241_dio() {
12913         for LOOP in $(seq $1); do
12914                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
12915                                                 iflag=direct 2>/dev/null
12916         done
12917 }
12918
12919 test_241() {
12920         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
12921         ls -la $DIR/$tfile
12922         cancel_lru_locks osc
12923         test_241_bio 1000 &
12924         PID=$!
12925         test_241_dio 1000
12926         wait $PID
12927 }
12928 run_test 241 "bio vs dio"
12929
12930 test_242() {
12931         mkdir -p $DIR/$tdir
12932         touch $DIR/$tdir/$tfile
12933
12934         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
12935         do_facet mds1 lctl set_param fail_loc=0x105
12936         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
12937
12938         do_facet mds1 lctl set_param fail_loc=0
12939         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
12940 }
12941 run_test 242 "mdt_readpage failure should not cause directory unreadable"
12942
12943 test_243()
12944 {
12945         test_mkdir -p $DIR/$tdir
12946         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
12947 }
12948 run_test 243 "various group lock tests"
12949
12950 test_250() {
12951         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
12952          && skip "no 16TB file size limit on ZFS" && return
12953
12954         $SETSTRIPE -c 1 $DIR/$tfile
12955         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
12956         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
12957         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
12958         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
12959                 conv=notrunc,fsync && error "append succeeded"
12960         return 0
12961 }
12962 run_test 250 "Write above 16T limit"
12963
12964 cleanup_test_300() {
12965         trap 0
12966         umask $SAVE_UMASK
12967 }
12968 test_striped_dir() {
12969         local mdt_index=$1
12970         local stripe_count
12971         local stripe_index
12972
12973         mkdir -p $DIR/$tdir
12974
12975         SAVE_UMASK=$(umask)
12976         trap cleanup_test_300 RETURN EXIT
12977
12978         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12979                                                 $DIR/$tdir/striped_dir ||
12980                 error "set striped dir error"
12981
12982         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12983         [ "$mode" = "755" ] || error "expect 755 got $mode"
12984
12985         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12986         if [ "$stripe_count" != "2" ]; then
12987                 error "stripe_count is $stripe_count, expect 2"
12988         fi
12989
12990         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12991         if [ "$stripe_index" != "$mdt_index" ]; then
12992                 error "stripe_index is $stripe_index, expect $mdt_index"
12993         fi
12994
12995         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12996                 error "nlink error after create striped dir"
12997
12998         mkdir $DIR/$tdir/striped_dir/a
12999         mkdir $DIR/$tdir/striped_dir/b
13000
13001         stat $DIR/$tdir/striped_dir/a ||
13002                 error "create dir under striped dir failed"
13003         stat $DIR/$tdir/striped_dir/b ||
13004                 error "create dir under striped dir failed"
13005
13006         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
13007                 error "nlink error after mkdir"
13008
13009         rmdir $DIR/$tdir/striped_dir/a
13010         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
13011                 error "nlink error after rmdir"
13012
13013         rmdir $DIR/$tdir/striped_dir/b
13014         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13015                 error "nlink error after rmdir"
13016
13017         rmdir $DIR/$tdir/striped_dir ||
13018                 error "rmdir striped dir error"
13019
13020         cleanup_test_300
13021
13022         true
13023 }
13024
13025 test_300a() {
13026         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13027         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13028
13029         test_striped_dir 0 || error "failed on striped dir on MDT0"
13030         test_striped_dir 1 || error "failed on striped dir on MDT0"
13031 }
13032 run_test 300a "basic striped dir sanity test"
13033
13034 test_300b() {
13035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13036         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13037         local i
13038         local mtime1
13039         local mtime2
13040         local mtime3
13041
13042         test_mkdir $DIR/$tdir || error "mkdir fail"
13043         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13044                 error "set striped dir error"
13045         for ((i=0; i<10; i++)); do
13046                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
13047                 sleep 1
13048                 touch $DIR/$tdir/striped_dir/file_$i ||
13049                                         error "touch error $i"
13050                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13051                 [ $mtime1 -eq $mtime2 ] &&
13052                         error "mtime not change after create"
13053                 sleep 1
13054                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13055                                         error "unlink error $i"
13056                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13057                 [ $mtime2 -eq $mtime3 ] &&
13058                         error "mtime did not change after unlink"
13059         done
13060         true
13061 }
13062 run_test 300b "check ctime/mtime for striped dir"
13063
13064 test_300c() {
13065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13066         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13067         local file_count
13068
13069         mkdir -p $DIR/$tdir
13070         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13071                 error "set striped dir error"
13072
13073         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13074                 error "chown striped dir failed"
13075
13076         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13077                 error "create 5k files failed"
13078
13079         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13080
13081         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13082
13083         rm -rf $DIR/$tdir
13084 }
13085 run_test 300c "chown && check ls under striped directory"
13086
13087 test_300d() {
13088         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13089         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13090         local stripe_count
13091         local file
13092
13093         mkdir -p $DIR/$tdir
13094         $SETSTRIPE -c 2 $DIR/$tdir
13095
13096         #local striped directory
13097         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13098                 error "set striped dir error"
13099         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13100                 error "create 10 files failed"
13101
13102         #remote striped directory
13103         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13104                 error "set striped dir error"
13105         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13106                 error "create 10 files failed"
13107
13108         for file in $(find $DIR/$tdir); do
13109                 stripe_count=$($GETSTRIPE -c $file)
13110                 [ $stripe_count -eq 2 ] ||
13111                         error "wrong stripe $stripe_count for $file"
13112         done
13113
13114         rm -rf $DIR/$tdir
13115 }
13116 run_test 300d "check default stripe under striped directory"
13117
13118 test_300e() {
13119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13120         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13121         local stripe_count
13122         local file
13123
13124         mkdir -p $DIR/$tdir
13125
13126         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13127                 error "set striped dir error"
13128
13129         touch $DIR/$tdir/striped_dir/a
13130         touch $DIR/$tdir/striped_dir/b
13131         touch $DIR/$tdir/striped_dir/c
13132
13133         mkdir $DIR/$tdir/striped_dir/dir_a
13134         mkdir $DIR/$tdir/striped_dir/dir_b
13135         mkdir $DIR/$tdir/striped_dir/dir_c
13136
13137         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13138                 error "set striped dir under striped dir error"
13139
13140         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13141                 error "set striped dir under striped dir error"
13142
13143         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13144                 error "set striped dir under striped dir error"
13145
13146         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
13147                 error "rename file under striped dir should fail"
13148
13149         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
13150                 error "rename dir under striped dir should fail"
13151
13152         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
13153                 error "rename dir under different stripes should fail"
13154
13155         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13156                 error "rename file under striped dir should succeed"
13157
13158         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
13159                 error "rename dir under striped dir should succeed"
13160
13161         rm -rf $DIR/$tdir
13162 }
13163 run_test 300e "check rename under striped directory"
13164
13165 test_300f() {
13166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13167         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13168         local stripe_count
13169         local file
13170
13171         rm -rf $DIR/$tdir
13172         mkdir -p $DIR/$tdir
13173
13174         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13175                 error "set striped dir error"
13176
13177         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13178                 error "set striped dir error"
13179
13180         touch $DIR/$tdir/striped_dir/a
13181         mkdir $DIR/$tdir/striped_dir/dir_a
13182         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13183                 error "create striped dir under striped dir fails"
13184
13185         touch $DIR/$tdir/striped_dir1/b
13186         mkdir $DIR/$tdir/striped_dir1/dir_b
13187         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13188                 error "create striped dir under striped dir fails"
13189
13190         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
13191                 error "rename file under different striped dir should fail"
13192
13193         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
13194                 error "rename dir under different striped dir should fail"
13195
13196         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
13197                 error "rename striped dir under diff striped dir should fail"
13198
13199         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13200                 error "rename file under diff striped dirs fails"
13201
13202         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
13203                 error "rename dir under diff striped dirs fails"
13204
13205         rm -rf $DIR/$tdir
13206 }
13207 run_test 300f "check rename cross striped directory"
13208
13209 test_300_check_default_striped_dir()
13210 {
13211         local dirname=$1
13212         local default_count=$2
13213         local default_index=$3
13214         local stripe_count
13215         local stripe_index
13216         local dir_stripe_index
13217         local dir
13218
13219         echo "checking $dirname $default_count $default_index"
13220         $LFS setdirstripe -D -c $default_count -i $default_index \
13221                                 -t all_char $DIR/$tdir/$dirname ||
13222                 error "set default stripe on striped dir error"
13223         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13224         [ $stripe_count -eq $default_count ] ||
13225                 error "expect $default_count get $stripe_count for $dirname"
13226
13227         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13228         [ $stripe_index -eq $default_index ] ||
13229                 error "expect $default_index get $stripe_index for $dirname"
13230
13231         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13232                                                 error "create dirs failed"
13233         for dir in $(find $DIR/$tdir/$dirname/*); do
13234                 stripe_count=$($LFS getdirstripe -c $dir)
13235                 [ $stripe_count -eq $default_count ] ||
13236                 error "stripe count $default_count != $stripe_count for $dir"
13237
13238                 stripe_index=$($LFS getdirstripe -i $dir)
13239                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13240                         error "$stripe_index != $default_index for $dir"
13241
13242                 #check default stripe
13243                 stripe_count=$($LFS getdirstripe -D -c $dir)
13244                 [ $stripe_count -eq $default_count ] ||
13245                 error "default count $default_count != $stripe_count for $dir"
13246
13247                 stripe_index=$($LFS getdirstripe -D -i $dir)
13248                 [ $stripe_index -eq $default_index ] ||
13249                 error "default index $default_index != $stripe_index for $dir"
13250         done
13251         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13252 }
13253
13254 test_300g() {
13255         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13256         local dir
13257         local stripe_count
13258         local stripe_index
13259
13260         mkdir $DIR/$tdir
13261         mkdir $DIR/$tdir/normal_dir
13262
13263         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13264         test_300_check_default_striped_dir normal_dir 1 0
13265         test_300_check_default_striped_dir normal_dir 2 1
13266         test_300_check_default_striped_dir normal_dir 2 -1
13267
13268         #delete default stripe information
13269         echo "delete default stripeEA"
13270         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13271                 error "set default stripe on striped dir error"
13272
13273         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13274         for dir in $(find $DIR/$tdir/normal_dir/*); do
13275                 stripe_count=$($LFS getdirstripe -c $dir)
13276                 [ $stripe_count -eq 0 ] ||
13277                         error "expect 1 get $stripe_count for $dir"
13278                 stripe_index=$($LFS getdirstripe -i $dir)
13279                 [ $stripe_index -eq 0 ] ||
13280                         error "expect 0 get $stripe_index for $dir"
13281         done
13282 }
13283 run_test 300g "check default striped directory for normal directory"
13284
13285 test_300h() {
13286         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13287         local dir
13288         local stripe_count
13289
13290         mkdir $DIR/$tdir
13291         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13292                                         $DIR/$tdir/striped_dir ||
13293                 error "set striped dir error"
13294
13295         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13296         test_300_check_default_striped_dir striped_dir 1 0
13297         test_300_check_default_striped_dir striped_dir 2 1
13298         test_300_check_default_striped_dir striped_dir 2 -1
13299
13300         #delete default stripe information
13301         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13302                 error "set default stripe on striped dir error"
13303
13304         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13305         for dir in $(find $DIR/$tdir/striped_dir/*); do
13306                 stripe_count=$($LFS getdirstripe -c $dir)
13307                 [ $stripe_count -eq 0 ] ||
13308                         error "expect 1 get $stripe_count for $dir"
13309         done
13310 }
13311 run_test 300h "check default striped directory for striped directory"
13312
13313 test_300i() {
13314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13315         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13316         local stripe_count
13317         local file
13318
13319         mkdir $DIR/$tdir
13320
13321         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13322                 error "set striped dir error"
13323
13324         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13325                 error "create files under striped dir failed"
13326
13327         # unfortunately, we need to umount to clear dir layout cache for now
13328         # once we fully implement dir layout, we can drop this
13329         umount_client $MOUNT || error "umount failed"
13330         mount_client $MOUNT || error "mount failed"
13331
13332         #set the stripe to be unknown hash type
13333         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13334         $LCTL set_param fail_loc=0x1901
13335         for ((i = 0; i < 10; i++)); do
13336                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13337                         error "stat f-$i failed"
13338                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13339         done
13340
13341         touch $DIR/$tdir/striped_dir/f0 &&
13342                 error "create under striped dir with unknown hash should fail"
13343
13344         $LCTL set_param fail_loc=0
13345
13346         umount_client $MOUNT || error "umount failed"
13347         mount_client $MOUNT || error "mount failed"
13348
13349         return 0
13350 }
13351 run_test 300i "client handle unknown hash type striped directory"
13352
13353 test_400a() { # LU-1606, was conf-sanity test_74
13354         local extra_flags=''
13355         local out=$TMP/$tfile
13356         local prefix=/usr/include/lustre
13357         local prog
13358
13359         if ! which $CC > /dev/null 2>&1; then
13360                 skip_env "$CC is not installed"
13361                 return 0
13362         fi
13363
13364         if ! [[ -d $prefix ]]; then
13365                 # Assume we're running in tree and fixup the include path.
13366                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13367                 extra_flags+=" -I$LUSTRE/include"
13368                 extra_flags+=" -L$LUSTRE/utils"
13369         fi
13370
13371         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13372                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13373                         error "client api broken"
13374         done
13375 }
13376 run_test 400a "Lustre client api program can compile and link"
13377
13378 test_400b() { # LU-1606, LU-5011
13379         local header
13380         local out=$TMP/$tfile
13381         local prefix=/usr/include/lustre
13382
13383         # We use a hard coded prefix so that this test will not fail
13384         # when run in tree. There are headers in lustre/include/lustre/
13385         # that are not packaged (like lustre_idl.h) and have more
13386         # complicated include dependencies (like config.h and lnet/types.h).
13387         # Since this test about correct packaging we just skip them when
13388         # they don't exist (see below) rather than try to fixup cppflags.
13389
13390         if ! which $CC > /dev/null 2>&1; then
13391                 skip_env "$CC is not installed"
13392                 return 0
13393         fi
13394
13395         for header in $prefix/*.h; do
13396                 if ! [[ -f "$header" ]]; then
13397                         continue
13398                 fi
13399
13400                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13401                         continue # liblustreapi.h is deprecated.
13402                 fi
13403
13404                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13405                         error "cannot compile '$header'"
13406         done
13407 }
13408 run_test 400b "packaged headers can be compiled"
13409
13410 #
13411 # tests that do cleanup/setup should be run at the end
13412 #
13413
13414 test_900() {
13415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13416         local ls
13417         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13418         $LCTL set_param fail_loc=0x903
13419
13420         cancel_lru_locks MGC
13421
13422         FAIL_ON_ERROR=true cleanup
13423         FAIL_ON_ERROR=true setup
13424 }
13425 run_test 900 "umount should not race with any mgc requeue thread"
13426
13427 complete $SECONDS
13428 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13429 check_and_cleanup_lustre
13430 if [ "$I_MOUNTED" != "yes" ]; then
13431         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13432 fi
13433 exit_status