Whamcloud - gitweb
79535c2d8a827d07d402fc575c0b4cea2e3c3c3b
[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 # createtest also checks that device nodes are created and
2041 # then visible correctly (#2091)
2042 test_28() { # bug 2091
2043         test_mkdir $DIR/d28
2044         $CREATETEST $DIR/d28/ct || error
2045 }
2046 run_test 28 "create/mknod/mkdir with bad file types ============"
2047
2048 test_29() {
2049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2050         cancel_lru_locks mdc
2051         test_mkdir $DIR/d29
2052         touch $DIR/d29/foo
2053         log 'first d29'
2054         ls -l $DIR/d29
2055
2056         declare -i LOCKCOUNTORIG=0
2057         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2058                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2059         done
2060         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2061
2062         declare -i LOCKUNUSEDCOUNTORIG=0
2063         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2064                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2065         done
2066
2067         log 'second d29'
2068         ls -l $DIR/d29
2069         log 'done'
2070
2071         declare -i LOCKCOUNTCURRENT=0
2072         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2073                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2074         done
2075
2076         declare -i LOCKUNUSEDCOUNTCURRENT=0
2077         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2078                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2079         done
2080
2081         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2082                 $LCTL set_param -n ldlm.dump_namespaces ""
2083                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2084                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2085                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2086                 return 2
2087         fi
2088         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2089                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2090                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2091                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2092                 return 3
2093         fi
2094 }
2095 run_test 29 "IT_GETATTR regression  ============================"
2096
2097 test_30a() { # was test_30
2098         cp $(which ls) $DIR || cp /bin/ls $DIR
2099         $DIR/ls / || error
2100         rm $DIR/ls
2101 }
2102 run_test 30a "execute binary from Lustre (execve) =============="
2103
2104 test_30b() {
2105         cp `which ls` $DIR || cp /bin/ls $DIR
2106         chmod go+rx $DIR/ls
2107         $RUNAS $DIR/ls / || error
2108         rm $DIR/ls
2109 }
2110 run_test 30b "execute binary from Lustre as non-root ==========="
2111
2112 test_30c() { # b=22376
2113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2114         cp `which ls` $DIR || cp /bin/ls $DIR
2115         chmod a-rw $DIR/ls
2116         cancel_lru_locks mdc
2117         cancel_lru_locks osc
2118         $RUNAS $DIR/ls / || error
2119         rm -f $DIR/ls
2120 }
2121 run_test 30c "execute binary from Lustre without read perms ===="
2122
2123 test_31a() {
2124         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2125         $CHECKSTAT -a $DIR/f31 || error
2126 }
2127 run_test 31a "open-unlink file =================================="
2128
2129 test_31b() {
2130         touch $DIR/f31 || error
2131         ln $DIR/f31 $DIR/f31b || error
2132         $MULTIOP $DIR/f31b Ouc || error
2133         $CHECKSTAT -t file $DIR/f31 || error
2134 }
2135 run_test 31b "unlink file with multiple links while open ======="
2136
2137 test_31c() {
2138         touch $DIR/f31 || error
2139         ln $DIR/f31 $DIR/f31c || error
2140         multiop_bg_pause $DIR/f31 O_uc || return 1
2141         MULTIPID=$!
2142         $MULTIOP $DIR/f31c Ouc
2143         kill -USR1 $MULTIPID
2144         wait $MULTIPID
2145 }
2146 run_test 31c "open-unlink file with multiple links ============="
2147
2148 test_31d() {
2149         opendirunlink $DIR/d31d $DIR/d31d || error
2150         $CHECKSTAT -a $DIR/d31d || error
2151 }
2152 run_test 31d "remove of open directory ========================="
2153
2154 test_31e() { # bug 2904
2155         check_kernel_version 34 || return 0
2156         openfilleddirunlink $DIR/d31e || error
2157 }
2158 run_test 31e "remove of open non-empty directory ==============="
2159
2160 test_31f() { # bug 4554
2161         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2162         set -vx
2163         test_mkdir $DIR/d31f
2164         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2165         cp /etc/hosts $DIR/d31f
2166         ls -l $DIR/d31f
2167         $GETSTRIPE $DIR/d31f/hosts
2168         multiop_bg_pause $DIR/d31f D_c || return 1
2169         MULTIPID=$!
2170
2171         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2172         test_mkdir $DIR/d31f
2173         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2174         cp /etc/hosts $DIR/d31f
2175         ls -l $DIR/d31f
2176         $GETSTRIPE $DIR/d31f/hosts
2177         multiop_bg_pause $DIR/d31f D_c || return 1
2178         MULTIPID2=$!
2179
2180         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2181         wait $MULTIPID || error "first opendir $MULTIPID failed"
2182
2183         sleep 6
2184
2185         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2186         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2187         set +vx
2188 }
2189 run_test 31f "remove of open directory with open-unlink file ==="
2190
2191 test_31g() {
2192         echo "-- cross directory link --"
2193         test_mkdir -c1 $DIR/${tdir}ga
2194         test_mkdir -c1 $DIR/${tdir}gb
2195         touch $DIR/${tdir}ga/f
2196         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2197         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2198         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2199         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2200         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2201 }
2202 run_test 31g "cross directory link==============="
2203
2204 test_31h() {
2205         echo "-- cross directory link --"
2206         test_mkdir -c1 $DIR/${tdir}
2207         test_mkdir -c1 $DIR/${tdir}/dir
2208         touch $DIR/${tdir}/f
2209         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2210         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2211         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2212         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2213         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2214 }
2215 run_test 31h "cross directory link under child==============="
2216
2217 test_31i() {
2218         echo "-- cross directory link --"
2219         test_mkdir -c1 $DIR/$tdir
2220         test_mkdir -c1 $DIR/$tdir/dir
2221         touch $DIR/$tdir/dir/f
2222         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2223         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2224         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2225         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2226         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2227 }
2228 run_test 31i "cross directory link under parent==============="
2229
2230 test_31j() {
2231         test_mkdir -c1 -p $DIR/$tdir
2232         test_mkdir -c1 -p $DIR/$tdir/dir1
2233         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2234         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2235         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2236         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2237         return 0
2238 }
2239 run_test 31j "link for directory==============="
2240
2241 test_31k() {
2242         test_mkdir -c1 -p $DIR/$tdir
2243         touch $DIR/$tdir/s
2244         touch $DIR/$tdir/exist
2245         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2246         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2247         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2248         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2249         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2250         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2251         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2252         return 0
2253 }
2254 run_test 31k "link to file: the same, non-existing, dir==============="
2255
2256 test_31m() {
2257         mkdir $DIR/d31m
2258         touch $DIR/d31m/s
2259         mkdir $DIR/d31m2
2260         touch $DIR/d31m2/exist
2261         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2262         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2263         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2264         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2265         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2266         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2267         return 0
2268 }
2269 run_test 31m "link to file: the same, non-existing, dir==============="
2270
2271 test_31n() {
2272         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2273         nlink=$(stat --format=%h $DIR/$tfile)
2274         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2275         local fd=$(free_fd)
2276         local cmd="exec $fd<$DIR/$tfile"
2277         eval $cmd
2278         cmd="exec $fd<&-"
2279         trap "eval $cmd" EXIT
2280         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2281         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2282         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2283         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2284         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2285         eval $cmd
2286 }
2287 run_test 31n "check link count of unlinked file"
2288
2289 link_one() {
2290         local TEMPNAME=$(mktemp $1_XXXXXX)
2291         mlink $TEMPNAME $1 2> /dev/null &&
2292                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2293         munlink $TEMPNAME
2294 }
2295
2296 test_31o() { # LU-2901
2297         mkdir -p $DIR/$tdir
2298         for LOOP in $(seq 100); do
2299                 rm -f $DIR/$tdir/$tfile*
2300                 for THREAD in $(seq 8); do
2301                         link_one $DIR/$tdir/$tfile.$LOOP &
2302                 done
2303                 wait
2304                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2305                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2306                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2307                         break || true
2308         done
2309 }
2310 run_test 31o "duplicate hard links with same filename"
2311
2312 test_31p() {
2313         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2314
2315         mkdir $DIR/$tdir
2316         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2317         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2318
2319         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2320                 error "open unlink test1 failed"
2321         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2322                 error "open unlink test2 failed"
2323
2324         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2325                 error "test1 still exists"
2326         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2327                 error "test2 still exists"
2328 }
2329 run_test 31p "remove of open striped directory"
2330
2331 cleanup_test32_mount() {
2332         trap 0
2333         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2334 }
2335
2336 test_32a() {
2337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2338         echo "== more mountpoints and symlinks ================="
2339         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2340         trap cleanup_test32_mount EXIT
2341         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2342         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2343         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2344         cleanup_test32_mount
2345 }
2346 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2347
2348 test_32b() {
2349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2350         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2351         trap cleanup_test32_mount EXIT
2352         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2353         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2354         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2355         cleanup_test32_mount
2356 }
2357 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2358
2359 test_32c() {
2360         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2361         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2362         trap cleanup_test32_mount EXIT
2363         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2364         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2365         test_mkdir -p $DIR/$tdir/d2/test_dir
2366         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2367         cleanup_test32_mount
2368 }
2369 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2370
2371 test_32d() {
2372         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2373         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2374         trap cleanup_test32_mount EXIT
2375         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2376         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2377         test_mkdir -p $DIR/$tdir/d2/test_dir
2378         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2379         cleanup_test32_mount
2380 }
2381 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2382
2383 test_32e() {
2384         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2385         test_mkdir -p $DIR/d32e/tmp
2386         TMP_DIR=$DIR/d32e/tmp
2387         ln -s $DIR/d32e $TMP_DIR/symlink11
2388         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2389         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2390         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2391 }
2392 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2393
2394 test_32f() {
2395         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2396         test_mkdir -p $DIR/d32f/tmp
2397         TMP_DIR=$DIR/d32f/tmp
2398         ln -s $DIR/d32f $TMP_DIR/symlink11
2399         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2400         ls $DIR/d32f/tmp/symlink11  || error
2401         ls $DIR/d32f/symlink01 || error
2402 }
2403 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2404
2405 test_32g() {
2406         TMP_DIR=$DIR/$tdir/tmp
2407         test_mkdir -p $DIR/$tdir/tmp
2408         test_mkdir $DIR/${tdir}2
2409         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2410         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2411         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2412         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2413         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2414         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2415 }
2416 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2417
2418 test_32h() {
2419         rm -fr $DIR/$tdir $DIR/${tdir}2
2420         TMP_DIR=$DIR/$tdir/tmp
2421         test_mkdir -p $DIR/$tdir/tmp
2422         test_mkdir $DIR/${tdir}2
2423         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2424         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2425         ls $TMP_DIR/symlink12 || error
2426         ls $DIR/$tdir/symlink02  || error
2427 }
2428 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2429
2430 test_32i() {
2431         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2432         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2433         trap cleanup_test32_mount EXIT
2434         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2435         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2436         touch $DIR/$tdir/test_file
2437         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2438         cleanup_test32_mount
2439 }
2440 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2441
2442 test_32j() {
2443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2444         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2445         trap cleanup_test32_mount EXIT
2446         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2447         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2448         touch $DIR/$tdir/test_file
2449         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2450         cleanup_test32_mount
2451 }
2452 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2453
2454 test_32k() {
2455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2456         rm -fr $DIR/$tdir
2457         trap cleanup_test32_mount EXIT
2458         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2459         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2460         test_mkdir -p $DIR/$tdir/d2
2461         touch $DIR/$tdir/d2/test_file || error
2462         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2463         cleanup_test32_mount
2464 }
2465 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2466
2467 test_32l() {
2468         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2469         rm -fr $DIR/$tdir
2470         trap cleanup_test32_mount EXIT
2471         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2472         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2473         test_mkdir -p $DIR/$tdir/d2
2474         touch $DIR/$tdir/d2/test_file
2475         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2476         cleanup_test32_mount
2477 }
2478 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2479
2480 test_32m() {
2481         rm -fr $DIR/d32m
2482         test_mkdir -p $DIR/d32m/tmp
2483         TMP_DIR=$DIR/d32m/tmp
2484         ln -s $DIR $TMP_DIR/symlink11
2485         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2486         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2487         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2488 }
2489 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2490
2491 test_32n() {
2492         rm -fr $DIR/d32n
2493         test_mkdir -p $DIR/d32n/tmp
2494         TMP_DIR=$DIR/d32n/tmp
2495         ln -s $DIR $TMP_DIR/symlink11
2496         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2497         ls -l $DIR/d32n/tmp/symlink11  || error
2498         ls -l $DIR/d32n/symlink01 || error
2499 }
2500 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2501
2502 test_32o() {
2503         touch $DIR/$tfile
2504         test_mkdir -p $DIR/d32o/tmp
2505         TMP_DIR=$DIR/d32o/tmp
2506         ln -s $DIR/$tfile $TMP_DIR/symlink12
2507         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2508         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2509         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2510         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2511         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2512 }
2513 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2514
2515 test_32p() {
2516     log 32p_1
2517         rm -fr $DIR/d32p
2518     log 32p_2
2519         rm -f $DIR/$tfile
2520     log 32p_3
2521         touch $DIR/$tfile
2522     log 32p_4
2523         test_mkdir -p $DIR/d32p/tmp
2524     log 32p_5
2525         TMP_DIR=$DIR/d32p/tmp
2526     log 32p_6
2527         ln -s $DIR/$tfile $TMP_DIR/symlink12
2528     log 32p_7
2529         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2530     log 32p_8
2531         cat $DIR/d32p/tmp/symlink12 || error
2532     log 32p_9
2533         cat $DIR/d32p/symlink02 || error
2534     log 32p_10
2535 }
2536 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2537
2538 cleanup_testdir_mount() {
2539         trap 0
2540         $UMOUNT -d $DIR/$tdir
2541 }
2542
2543 test_32q() {
2544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2545         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2546         trap cleanup_testdir_mount EXIT
2547         test_mkdir -p $DIR/$tdir
2548         touch $DIR/$tdir/under_the_mount
2549         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2550         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2551         cleanup_testdir_mount
2552 }
2553 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2554
2555 test_32r() {
2556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2557         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2558         trap cleanup_testdir_mount EXIT
2559         test_mkdir -p $DIR/$tdir
2560         touch $DIR/$tdir/under_the_mount
2561         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2562         ls $DIR/$tdir | grep -q under_the_mount && error || true
2563         cleanup_testdir_mount
2564 }
2565 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2566
2567 test_33aa() {
2568         rm -f $DIR/$tfile
2569         touch $DIR/$tfile
2570         chmod 444 $DIR/$tfile
2571         chown $RUNAS_ID $DIR/$tfile
2572         log 33_1
2573         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2574         log 33_2
2575 }
2576 run_test 33aa "write file with mode 444 (should return error) ===="
2577
2578 test_33a() {
2579         rm -fr $DIR/d33
2580         test_mkdir -p $DIR/d33
2581         chown $RUNAS_ID $DIR/d33
2582         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2583         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2584                 error "open RDWR" || true
2585 }
2586 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2587
2588 test_33b() {
2589         rm -fr $DIR/d33
2590         test_mkdir -p $DIR/d33
2591         chown $RUNAS_ID $DIR/d33
2592         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2593 }
2594 run_test 33b "test open file with malformed flags (No panic)"
2595
2596 test_33c() {
2597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2598         local ostnum
2599         local ostname
2600         local write_bytes
2601         local all_zeros
2602
2603         remote_ost_nodsh && skip "remote OST with nodsh" && return
2604         all_zeros=:
2605         rm -fr $DIR/d33
2606         test_mkdir -p $DIR/d33
2607         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2608
2609         sync
2610         for ostnum in $(seq $OSTCOUNT); do
2611                 # test-framework's OST numbering is one-based, while Lustre's
2612                 # is zero-based
2613                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2614                 # Parsing llobdstat's output sucks; we could grep the /proc
2615                 # path, but that's likely to not be as portable as using the
2616                 # llobdstat utility.  So we parse lctl output instead.
2617                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2618                         obdfilter/$ostname/stats |
2619                         awk '/^write_bytes/ {print $7}' )
2620                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2621                 if (( ${write_bytes:-0} > 0 ))
2622                 then
2623                         all_zeros=false
2624                         break;
2625                 fi
2626         done
2627
2628         $all_zeros || return 0
2629
2630         # Write four bytes
2631         echo foo > $DIR/d33/bar
2632         # Really write them
2633         sync
2634
2635         # Total up write_bytes after writing.  We'd better find non-zeros.
2636         for ostnum in $(seq $OSTCOUNT); do
2637                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2638                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2639                         obdfilter/$ostname/stats |
2640                         awk '/^write_bytes/ {print $7}' )
2641                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2642                 if (( ${write_bytes:-0} > 0 ))
2643                 then
2644                         all_zeros=false
2645                         break;
2646                 fi
2647         done
2648
2649         if $all_zeros
2650         then
2651                 for ostnum in $(seq $OSTCOUNT); do
2652                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2653                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2654                         do_facet ost$ostnum lctl get_param -n \
2655                                 obdfilter/$ostname/stats
2656                 done
2657                 error "OST not keeping write_bytes stats (b22312)"
2658         fi
2659 }
2660 run_test 33c "test llobdstat and write_bytes"
2661
2662 test_33d() {
2663         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2665         local MDTIDX=1
2666         local remote_dir=$DIR/$tdir/remote_dir
2667
2668         mkdir -p $DIR/$tdir
2669         $LFS mkdir -i $MDTIDX $remote_dir ||
2670                 error "create remote directory failed"
2671
2672         touch $remote_dir/$tfile
2673         chmod 444 $remote_dir/$tfile
2674         chown $RUNAS_ID $remote_dir/$tfile
2675
2676         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2677
2678         chown $RUNAS_ID $remote_dir
2679         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2680                                         error "create" || true
2681         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2682                                     error "open RDWR" || true
2683         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2684                                     error "create" || true
2685 }
2686 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2687
2688 test_33e() {
2689         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2690
2691         mkdir $DIR/$tdir
2692
2693         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2694         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2695         mkdir $DIR/$tdir/local_dir
2696
2697         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2698         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2699         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2700
2701         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2702                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2703
2704         rmdir $DIR/$tdir/* || error "rmdir failed"
2705
2706         umask 777
2707         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2708         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2709         mkdir $DIR/$tdir/local_dir
2710
2711         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2712         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2713         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2714
2715         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2716                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2717
2718         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2719
2720         umask 000
2721         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2722         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2723         mkdir $DIR/$tdir/local_dir
2724
2725         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2726         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2727         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2728
2729         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2730                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2731 }
2732 run_test 33e "mkdir and striped directory should have same mode"
2733
2734 cleanup_33f() {
2735         trap 0
2736         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2737 }
2738
2739 test_33f() {
2740         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2741
2742         mkdir $DIR/$tdir
2743         chmod go+rwx $DIR/$tdir
2744         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2745         trap cleanup_33f EXIT
2746
2747         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2748                 error "cannot create striped directory"
2749
2750         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2751                 error "cannot create files in striped directory"
2752
2753         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2754                 error "cannot remove files in striped directory"
2755
2756         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2757                 error "cannot remove striped directory"
2758
2759         cleanup_33f
2760 }
2761 run_test 33f "nonroot user can create, access, and remove a striped directory"
2762
2763 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2764 test_34a() {
2765         rm -f $DIR/f34
2766         $MCREATE $DIR/f34 || error
2767         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2768         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2769         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2770         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2771 }
2772 run_test 34a "truncate file that has not been opened ==========="
2773
2774 test_34b() {
2775         [ ! -f $DIR/f34 ] && test_34a
2776         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2777         $OPENFILE -f O_RDONLY $DIR/f34
2778         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2779         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2780 }
2781 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2782
2783 test_34c() {
2784         [ ! -f $DIR/f34 ] && test_34a
2785         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2786         $OPENFILE -f O_RDWR $DIR/f34
2787         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2788         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2789 }
2790 run_test 34c "O_RDWR opening file-with-size works =============="
2791
2792 test_34d() {
2793         [ ! -f $DIR/f34 ] && test_34a
2794         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2795         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2796         rm $DIR/f34
2797 }
2798 run_test 34d "write to sparse file ============================="
2799
2800 test_34e() {
2801         rm -f $DIR/f34e
2802         $MCREATE $DIR/f34e || error
2803         $TRUNCATE $DIR/f34e 1000 || error
2804         $CHECKSTAT -s 1000 $DIR/f34e || error
2805         $OPENFILE -f O_RDWR $DIR/f34e
2806         $CHECKSTAT -s 1000 $DIR/f34e || error
2807 }
2808 run_test 34e "create objects, some with size and some without =="
2809
2810 test_34f() { # bug 6242, 6243
2811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2812         SIZE34F=48000
2813         rm -f $DIR/f34f
2814         $MCREATE $DIR/f34f || error
2815         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2816         dd if=$DIR/f34f of=$TMP/f34f
2817         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2818         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2819         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2820         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2821         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2822 }
2823 run_test 34f "read from a file with no objects until EOF ======="
2824
2825 test_34g() {
2826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2827         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2828         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2829         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2830         cancel_lru_locks osc
2831         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2832                 error "wrong size after lock cancel"
2833
2834         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2835         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2836                 error "expanding truncate failed"
2837         cancel_lru_locks osc
2838         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2839                 error "wrong expanded size after lock cancel"
2840 }
2841 run_test 34g "truncate long file ==============================="
2842
2843 test_34h() {
2844         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2845         local gid=10
2846         local sz=1000
2847
2848         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2849         sync # Flush the cache so that multiop below does not block on cache
2850              # flush when getting the group lock
2851         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2852         MULTIPID=$!
2853
2854         # Since just timed wait is not good enough, let's do a sync write
2855         # that way we are sure enough time for a roundtrip + processing
2856         # passed + 2 seconds of extra margin.
2857         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2858         rm $DIR/${tfile}-1
2859         sleep 2
2860
2861         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2862                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2863                 kill -9 $MULTIPID
2864         fi
2865         wait $MULTIPID
2866         local nsz=`stat -c %s $DIR/$tfile`
2867         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2868 }
2869 run_test 34h "ftruncate file under grouplock should not block"
2870
2871 test_35a() {
2872         cp /bin/sh $DIR/f35a
2873         chmod 444 $DIR/f35a
2874         chown $RUNAS_ID $DIR/f35a
2875         $RUNAS $DIR/f35a && error || true
2876         rm $DIR/f35a
2877 }
2878 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2879
2880 test_36a() {
2881         rm -f $DIR/f36
2882         utime $DIR/f36 || error
2883 }
2884 run_test 36a "MDS utime check (mknod, utime) ==================="
2885
2886 test_36b() {
2887         echo "" > $DIR/f36
2888         utime $DIR/f36 || error
2889 }
2890 run_test 36b "OST utime check (open, utime) ===================="
2891
2892 test_36c() {
2893         rm -f $DIR/d36/f36
2894         test_mkdir $DIR/d36
2895         chown $RUNAS_ID $DIR/d36
2896         $RUNAS utime $DIR/d36/f36 || error
2897 }
2898 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2899
2900 test_36d() {
2901         [ ! -d $DIR/d36 ] && test_36c
2902         echo "" > $DIR/d36/f36
2903         $RUNAS utime $DIR/d36/f36 || error
2904 }
2905 run_test 36d "non-root OST utime check (open, utime) ==========="
2906
2907 test_36e() {
2908         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2909         test_mkdir -p $DIR/$tdir
2910         touch $DIR/$tdir/$tfile
2911         $RUNAS utime $DIR/$tdir/$tfile && \
2912                 error "utime worked, expected failure" || true
2913 }
2914 run_test 36e "utime on non-owned file (should return error) ===="
2915
2916 subr_36fh() {
2917         local fl="$1"
2918         local LANG_SAVE=$LANG
2919         local LC_LANG_SAVE=$LC_LANG
2920         export LANG=C LC_LANG=C # for date language
2921
2922         DATESTR="Dec 20  2000"
2923         test_mkdir -p $DIR/$tdir
2924         lctl set_param fail_loc=$fl
2925         date; date +%s
2926         cp /etc/hosts $DIR/$tdir/$tfile
2927         sync & # write RPC generated with "current" inode timestamp, but delayed
2928         sleep 1
2929         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2930         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2931         cancel_lru_locks osc
2932         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2933         date; date +%s
2934         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2935                 echo "BEFORE: $LS_BEFORE" && \
2936                 echo "AFTER : $LS_AFTER" && \
2937                 echo "WANT  : $DATESTR" && \
2938                 error "$DIR/$tdir/$tfile timestamps changed" || true
2939
2940         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2941 }
2942
2943 test_36f() {
2944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2945         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2946         subr_36fh "0x80000214"
2947 }
2948 run_test 36f "utime on file racing with OST BRW write =========="
2949
2950 test_36g() {
2951         remote_ost_nodsh && skip "remote OST with nodsh" && return
2952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2953         local fmd_max_age
2954         local fmd_before
2955         local fmd_after
2956
2957         test_mkdir -p $DIR/$tdir
2958         fmd_max_age=$(do_facet ost1 \
2959                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2960                 head -n 1")
2961
2962         fmd_before=$(do_facet ost1 \
2963                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2964         touch $DIR/$tdir/$tfile
2965         sleep $((fmd_max_age + 12))
2966         fmd_after=$(do_facet ost1 \
2967                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2968
2969         echo "fmd_before: $fmd_before"
2970         echo "fmd_after: $fmd_after"
2971         [[ $fmd_after -gt $fmd_before ]] &&
2972                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2973                 error "fmd didn't expire after ping" || true
2974 }
2975 run_test 36g "filter mod data cache expiry ====================="
2976
2977 test_36h() {
2978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2979         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2980         subr_36fh "0x80000227"
2981 }
2982 run_test 36h "utime on file racing with OST BRW write =========="
2983
2984 test_36i() {
2985         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2986
2987         mkdir $DIR/$tdir
2988         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2989
2990         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2991         local new_mtime=$((mtime + 200))
2992
2993         #change Modify time of striped dir
2994         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2995                         error "change mtime failed"
2996
2997         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2998
2999         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3000 }
3001 run_test 36i "change mtime on striped directory"
3002
3003 # test_37 - duplicate with tests 32q 32r
3004
3005 test_38() {
3006         local file=$DIR/$tfile
3007         touch $file
3008         openfile -f O_DIRECTORY $file
3009         local RC=$?
3010         local ENOTDIR=20
3011         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3012         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3013 }
3014 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3015
3016 test_39() {
3017         touch $DIR/$tfile
3018         touch $DIR/${tfile}2
3019 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3020 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3021 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3022         sleep 2
3023         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3024         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3025                 echo "mtime"
3026                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3027                 echo "atime"
3028                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3029                 echo "ctime"
3030                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3031                 error "O_TRUNC didn't change timestamps"
3032         fi
3033 }
3034 run_test 39 "mtime changed on create ==========================="
3035
3036 test_39b() {
3037         test_mkdir -p -c1 $DIR/$tdir
3038         cp -p /etc/passwd $DIR/$tdir/fopen
3039         cp -p /etc/passwd $DIR/$tdir/flink
3040         cp -p /etc/passwd $DIR/$tdir/funlink
3041         cp -p /etc/passwd $DIR/$tdir/frename
3042         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3043
3044         sleep 1
3045         echo "aaaaaa" >> $DIR/$tdir/fopen
3046         echo "aaaaaa" >> $DIR/$tdir/flink
3047         echo "aaaaaa" >> $DIR/$tdir/funlink
3048         echo "aaaaaa" >> $DIR/$tdir/frename
3049
3050         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3051         local link_new=`stat -c %Y $DIR/$tdir/flink`
3052         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3053         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3054
3055         cat $DIR/$tdir/fopen > /dev/null
3056         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3057         rm -f $DIR/$tdir/funlink2
3058         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3059
3060         for (( i=0; i < 2; i++ )) ; do
3061                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3062                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3063                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3064                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3065
3066                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3067                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3068                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3069                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3070
3071                 cancel_lru_locks osc
3072                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3073         done
3074 }
3075 run_test 39b "mtime change on open, link, unlink, rename  ======"
3076
3077 # this should be set to past
3078 TEST_39_MTIME=`date -d "1 year ago" +%s`
3079
3080 # bug 11063
3081 test_39c() {
3082         touch $DIR1/$tfile
3083         sleep 2
3084         local mtime0=`stat -c %Y $DIR1/$tfile`
3085
3086         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3087         local mtime1=`stat -c %Y $DIR1/$tfile`
3088         [ "$mtime1" = $TEST_39_MTIME ] || \
3089                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3090
3091         local d1=`date +%s`
3092         echo hello >> $DIR1/$tfile
3093         local d2=`date +%s`
3094         local mtime2=`stat -c %Y $DIR1/$tfile`
3095         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3096                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3097
3098         mv $DIR1/$tfile $DIR1/$tfile-1
3099
3100         for (( i=0; i < 2; i++ )) ; do
3101                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3102                 [ "$mtime2" = "$mtime3" ] || \
3103                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3104
3105                 cancel_lru_locks osc
3106                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3107         done
3108 }
3109 run_test 39c "mtime change on rename ==========================="
3110
3111 # bug 21114
3112 test_39d() {
3113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3114         touch $DIR1/$tfile
3115
3116         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3117
3118         for (( i=0; i < 2; i++ )) ; do
3119                 local mtime=`stat -c %Y $DIR1/$tfile`
3120                 [ $mtime = $TEST_39_MTIME ] || \
3121                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3122
3123                 cancel_lru_locks osc
3124                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3125         done
3126 }
3127 run_test 39d "create, utime, stat =============================="
3128
3129 # bug 21114
3130 test_39e() {
3131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3132         touch $DIR1/$tfile
3133         local mtime1=`stat -c %Y $DIR1/$tfile`
3134
3135         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3136
3137         for (( i=0; i < 2; i++ )) ; do
3138                 local mtime2=`stat -c %Y $DIR1/$tfile`
3139                 [ $mtime2 = $TEST_39_MTIME ] || \
3140                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3141
3142                 cancel_lru_locks osc
3143                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3144         done
3145 }
3146 run_test 39e "create, stat, utime, stat ========================"
3147
3148 # bug 21114
3149 test_39f() {
3150         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3151         touch $DIR1/$tfile
3152         mtime1=`stat -c %Y $DIR1/$tfile`
3153
3154         sleep 2
3155         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3156
3157         for (( i=0; i < 2; i++ )) ; do
3158                 local mtime2=`stat -c %Y $DIR1/$tfile`
3159                 [ $mtime2 = $TEST_39_MTIME ] || \
3160                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3161
3162                 cancel_lru_locks osc
3163                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3164         done
3165 }
3166 run_test 39f "create, stat, sleep, utime, stat ================="
3167
3168 # bug 11063
3169 test_39g() {
3170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3171         echo hello >> $DIR1/$tfile
3172         local mtime1=`stat -c %Y $DIR1/$tfile`
3173
3174         sleep 2
3175         chmod o+r $DIR1/$tfile
3176
3177         for (( i=0; i < 2; i++ )) ; do
3178                 local mtime2=`stat -c %Y $DIR1/$tfile`
3179                 [ "$mtime1" = "$mtime2" ] || \
3180                         error "lost mtime: $mtime2, should be $mtime1"
3181
3182                 cancel_lru_locks osc
3183                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3184         done
3185 }
3186 run_test 39g "write, chmod, stat ==============================="
3187
3188 # bug 11063
3189 test_39h() {
3190         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3191         touch $DIR1/$tfile
3192         sleep 1
3193
3194         local d1=`date`
3195         echo hello >> $DIR1/$tfile
3196         local mtime1=`stat -c %Y $DIR1/$tfile`
3197
3198         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3199         local d2=`date`
3200         if [ "$d1" != "$d2" ]; then
3201                 echo "write and touch not within one second"
3202         else
3203                 for (( i=0; i < 2; i++ )) ; do
3204                         local mtime2=`stat -c %Y $DIR1/$tfile`
3205                         [ "$mtime2" = $TEST_39_MTIME ] || \
3206                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3207
3208                         cancel_lru_locks osc
3209                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3210                 done
3211         fi
3212 }
3213 run_test 39h "write, utime within one second, stat ============="
3214
3215 test_39i() {
3216         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3217         touch $DIR1/$tfile
3218         sleep 1
3219
3220         echo hello >> $DIR1/$tfile
3221         local mtime1=`stat -c %Y $DIR1/$tfile`
3222
3223         mv $DIR1/$tfile $DIR1/$tfile-1
3224
3225         for (( i=0; i < 2; i++ )) ; do
3226                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3227
3228                 [ "$mtime1" = "$mtime2" ] || \
3229                         error "lost mtime: $mtime2, should be $mtime1"
3230
3231                 cancel_lru_locks osc
3232                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3233         done
3234 }
3235 run_test 39i "write, rename, stat =============================="
3236
3237 test_39j() {
3238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3239         start_full_debug_logging
3240         touch $DIR1/$tfile
3241         sleep 1
3242
3243         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3244         lctl set_param fail_loc=0x80000412
3245         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3246                 error "multiop failed"
3247         local multipid=$!
3248         local mtime1=`stat -c %Y $DIR1/$tfile`
3249
3250         mv $DIR1/$tfile $DIR1/$tfile-1
3251
3252         kill -USR1 $multipid
3253         wait $multipid || error "multiop close failed"
3254
3255         for (( i=0; i < 2; i++ )) ; do
3256                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3257                 [ "$mtime1" = "$mtime2" ] ||
3258                         error "mtime is lost on close: $mtime2, " \
3259                               "should be $mtime1"
3260
3261                 cancel_lru_locks osc
3262                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3263         done
3264         lctl set_param fail_loc=0
3265         stop_full_debug_logging
3266 }
3267 run_test 39j "write, rename, close, stat ======================="
3268
3269 test_39k() {
3270         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3271         touch $DIR1/$tfile
3272         sleep 1
3273
3274         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3275         local multipid=$!
3276         local mtime1=`stat -c %Y $DIR1/$tfile`
3277
3278         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3279
3280         kill -USR1 $multipid
3281         wait $multipid || error "multiop close failed"
3282
3283         for (( i=0; i < 2; i++ )) ; do
3284                 local mtime2=`stat -c %Y $DIR1/$tfile`
3285
3286                 [ "$mtime2" = $TEST_39_MTIME ] || \
3287                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3288
3289                 cancel_lru_locks osc
3290                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3291         done
3292 }
3293 run_test 39k "write, utime, close, stat ========================"
3294
3295 # this should be set to future
3296 TEST_39_ATIME=`date -d "1 year" +%s`
3297
3298 test_39l() {
3299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3300         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3301         local atime_diff=$(do_facet $SINGLEMDS \
3302                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3303         rm -rf $DIR/$tdir
3304         mkdir -p $DIR/$tdir
3305
3306         # test setting directory atime to future
3307         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3308         local atime=$(stat -c %X $DIR/$tdir)
3309         [ "$atime" = $TEST_39_ATIME ] || \
3310                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3311
3312         # test setting directory atime from future to now
3313         local d1=$(date +%s)
3314         ls $DIR/$tdir
3315         local d2=$(date +%s)
3316
3317         cancel_lru_locks mdc
3318         atime=$(stat -c %X $DIR/$tdir)
3319         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3320                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3321
3322         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3323         sleep 3
3324
3325         # test setting directory atime when now > dir atime + atime_diff
3326         d1=$(date +%s)
3327         ls $DIR/$tdir
3328         d2=$(date +%s)
3329         cancel_lru_locks mdc
3330         atime=$(stat -c %X $DIR/$tdir)
3331         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3332                 error "atime is not updated  : $atime, should be $d2"
3333
3334         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3335         sleep 3
3336
3337         # test not setting directory atime when now < dir atime + atime_diff
3338         ls $DIR/$tdir
3339         cancel_lru_locks mdc
3340         atime=$(stat -c %X $DIR/$tdir)
3341         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3342                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3343
3344         do_facet $SINGLEMDS \
3345                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3346 }
3347 run_test 39l "directory atime update ==========================="
3348
3349 test_39m() {
3350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3351         touch $DIR1/$tfile
3352         sleep 2
3353         local far_past_mtime=$(date -d "May 29 1953" +%s)
3354         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3355
3356         touch -m -d @$far_past_mtime $DIR1/$tfile
3357         touch -a -d @$far_past_atime $DIR1/$tfile
3358
3359         for (( i=0; i < 2; i++ )) ; do
3360                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3361                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3362                         error "atime or mtime set incorrectly"
3363
3364                 cancel_lru_locks osc
3365                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3366         done
3367 }
3368 run_test 39m "test atime and mtime before 1970"
3369
3370 test_39n() { # LU-3832
3371         local atime_diff=$(do_facet $SINGLEMDS \
3372                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3373         local atime0
3374         local atime1
3375         local atime2
3376
3377         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3378
3379         rm -rf $DIR/$tfile
3380         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3381         atime0=$(stat -c %X $DIR/$tfile)
3382
3383         sleep 5
3384         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3385         atime1=$(stat -c %X $DIR/$tfile)
3386
3387         sleep 5
3388         cancel_lru_locks mdc
3389         cancel_lru_locks osc
3390         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3391         atime2=$(stat -c %X $DIR/$tfile)
3392
3393         do_facet $SINGLEMDS \
3394                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3395
3396         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3397         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3398 }
3399 run_test 39n "check that O_NOATIME is honored"
3400
3401 test_39o() {
3402         TESTDIR=$DIR/$tdir/$tfile
3403         [ -e $TESTDIR ] && rm -rf $TESTDIR
3404         test_mkdir -p $TESTDIR
3405         cd $TESTDIR
3406         links1=2
3407         ls
3408         mkdir a b
3409         ls
3410         links2=$(stat -c %h .)
3411         [ $(($links1 + 2)) != $links2 ] &&
3412                 error "wrong links count $(($links1 + 2)) != $links2"
3413         rmdir b
3414         links3=$(stat -c %h .)
3415         [ $(($links1 + 1)) != $links3 ] &&
3416                 error "wrong links count $links1 != $links3"
3417         return 0
3418 }
3419 run_test 39o "directory cached attributes updated after create ========"
3420
3421 test_39p() {
3422         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3423         local MDTIDX=1
3424         TESTDIR=$DIR/$tdir/$tfile
3425         [ -e $TESTDIR ] && rm -rf $TESTDIR
3426         mkdir -p $TESTDIR
3427         cd $TESTDIR
3428         links1=2
3429         ls
3430         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3431         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3432         ls
3433         links2=$(stat -c %h .)
3434         [ $(($links1 + 2)) != $links2 ] &&
3435                 error "wrong links count $(($links1 + 2)) != $links2"
3436         rmdir remote_dir2
3437         links3=$(stat -c %h .)
3438         [ $(($links1 + 1)) != $links3 ] &&
3439                 error "wrong links count $links1 != $links3"
3440         return 0
3441 }
3442 run_test 39p "remote directory cached attributes updated after create ========"
3443
3444
3445 test_40() {
3446         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3447         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3448                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3449         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3450                 error "$tfile is not 4096 bytes in size"
3451 }
3452 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3453
3454 test_41() {
3455         # bug 1553
3456         small_write $DIR/f41 18
3457 }
3458 run_test 41 "test small file write + fstat ====================="
3459
3460 count_ost_writes() {
3461         lctl get_param -n osc.*.stats |
3462                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3463                         END { printf("%0.0f", writes) }'
3464 }
3465
3466 # decent default
3467 WRITEBACK_SAVE=500
3468 DIRTY_RATIO_SAVE=40
3469 MAX_DIRTY_RATIO=50
3470 BG_DIRTY_RATIO_SAVE=10
3471 MAX_BG_DIRTY_RATIO=25
3472
3473 start_writeback() {
3474         trap 0
3475         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3476         # dirty_ratio, dirty_background_ratio
3477         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3478                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3479                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3480                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3481         else
3482                 # if file not here, we are a 2.4 kernel
3483                 kill -CONT `pidof kupdated`
3484         fi
3485 }
3486
3487 stop_writeback() {
3488         # setup the trap first, so someone cannot exit the test at the
3489         # exact wrong time and mess up a machine
3490         trap start_writeback EXIT
3491         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3492         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3493                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3494                 sysctl -w vm.dirty_writeback_centisecs=0
3495                 sysctl -w vm.dirty_writeback_centisecs=0
3496                 # save and increase /proc/sys/vm/dirty_ratio
3497                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3498                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3499                 # save and increase /proc/sys/vm/dirty_background_ratio
3500                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3501                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3502         else
3503                 # if file not here, we are a 2.4 kernel
3504                 kill -STOP `pidof kupdated`
3505         fi
3506 }
3507
3508 # ensure that all stripes have some grant before we test client-side cache
3509 setup_test42() {
3510         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3511                 dd if=/dev/zero of=$i bs=4k count=1
3512                 rm $i
3513         done
3514 }
3515
3516 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3517 # file truncation, and file removal.
3518 test_42a() {
3519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3520         setup_test42
3521         cancel_lru_locks osc
3522         stop_writeback
3523         sync; sleep 1; sync # just to be safe
3524         BEFOREWRITES=`count_ost_writes`
3525         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3526         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3527         AFTERWRITES=`count_ost_writes`
3528         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3529                 error "$BEFOREWRITES < $AFTERWRITES"
3530         start_writeback
3531 }
3532 run_test 42a "ensure that we don't flush on close =============="
3533
3534 test_42b() {
3535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3536         setup_test42
3537         cancel_lru_locks osc
3538         stop_writeback
3539         sync
3540         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3541         BEFOREWRITES=$(count_ost_writes)
3542         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3543         AFTERWRITES=$(count_ost_writes)
3544         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3545                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3546         fi
3547         BEFOREWRITES=$(count_ost_writes)
3548         sync || error "sync: $?"
3549         AFTERWRITES=$(count_ost_writes)
3550         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3551                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3552         fi
3553         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3554         start_writeback
3555         return 0
3556 }
3557 run_test 42b "test destroy of file with cached dirty data ======"
3558
3559 # if these tests just want to test the effect of truncation,
3560 # they have to be very careful.  consider:
3561 # - the first open gets a {0,EOF}PR lock
3562 # - the first write conflicts and gets a {0, count-1}PW
3563 # - the rest of the writes are under {count,EOF}PW
3564 # - the open for truncate tries to match a {0,EOF}PR
3565 #   for the filesize and cancels the PWs.
3566 # any number of fixes (don't get {0,EOF} on open, match
3567 # composite locks, do smarter file size management) fix
3568 # this, but for now we want these tests to verify that
3569 # the cancellation with truncate intent works, so we
3570 # start the file with a full-file pw lock to match against
3571 # until the truncate.
3572 trunc_test() {
3573         test=$1
3574         file=$DIR/$test
3575         offset=$2
3576         cancel_lru_locks osc
3577         stop_writeback
3578         # prime the file with 0,EOF PW to match
3579         touch $file
3580         $TRUNCATE $file 0
3581         sync; sync
3582         # now the real test..
3583         dd if=/dev/zero of=$file bs=1024 count=100
3584         BEFOREWRITES=`count_ost_writes`
3585         $TRUNCATE $file $offset
3586         cancel_lru_locks osc
3587         AFTERWRITES=`count_ost_writes`
3588         start_writeback
3589 }
3590
3591 test_42c() {
3592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3593         trunc_test 42c 1024
3594         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3595             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3596         rm $file
3597 }
3598 run_test 42c "test partial truncate of file with cached dirty data"
3599
3600 test_42d() {
3601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3602         trunc_test 42d 0
3603         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3604             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3605         rm $file
3606 }
3607 run_test 42d "test complete truncate of file with cached dirty data"
3608
3609 test_42e() { # bug22074
3610         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3611         local TDIR=$DIR/${tdir}e
3612         local pagesz=$(page_size)
3613         local pages=16 # hardcoded 16 pages, don't change it.
3614         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3615         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3616         local max_dirty_mb
3617         local warmup_files
3618
3619         test_mkdir -p $DIR/${tdir}e
3620         $SETSTRIPE -c 1 $TDIR
3621         createmany -o $TDIR/f $files
3622
3623         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3624
3625         # we assume that with $OSTCOUNT files, at least one of them will
3626         # be allocated on OST0.
3627         warmup_files=$((OSTCOUNT * max_dirty_mb))
3628         createmany -o $TDIR/w $warmup_files
3629
3630         # write a large amount of data into one file and sync, to get good
3631         # avail_grant number from OST.
3632         for ((i=0; i<$warmup_files; i++)); do
3633                 idx=$($GETSTRIPE -i $TDIR/w$i)
3634                 [ $idx -ne 0 ] && continue
3635                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3636                 break
3637         done
3638         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3639         sync
3640         $LCTL get_param $proc_osc0/cur_dirty_bytes
3641         $LCTL get_param $proc_osc0/cur_grant_bytes
3642
3643         # create as much dirty pages as we can while not to trigger the actual
3644         # RPCs directly. but depends on the env, VFS may trigger flush during this
3645         # period, hopefully we are good.
3646         for ((i=0; i<$warmup_files; i++)); do
3647                 idx=$($GETSTRIPE -i $TDIR/w$i)
3648                 [ $idx -ne 0 ] && continue
3649                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3650         done
3651         $LCTL get_param $proc_osc0/cur_dirty_bytes
3652         $LCTL get_param $proc_osc0/cur_grant_bytes
3653
3654         # perform the real test
3655         $LCTL set_param $proc_osc0/rpc_stats 0
3656         for ((;i<$files; i++)); do
3657                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3658                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3659         done
3660         sync
3661         $LCTL get_param $proc_osc0/rpc_stats
3662
3663         local percent=0
3664         local have_ppr=false
3665         $LCTL get_param $proc_osc0/rpc_stats |
3666                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3667                         # skip lines until we are at the RPC histogram data
3668                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3669                         $have_ppr || continue
3670
3671                         # we only want the percent stat for < 16 pages
3672                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3673
3674                         percent=$((percent + WPCT))
3675                         if [[ $percent -gt 15 ]]; then
3676                                 error "less than 16-pages write RPCs" \
3677                                       "$percent% > 15%"
3678                                 break
3679                         fi
3680                 done
3681         rm -rf $TDIR
3682 }
3683 run_test 42e "verify sub-RPC writes are not done synchronously"
3684
3685 test_43() {
3686         test_mkdir -p $DIR/$tdir
3687         cp -p /bin/ls $DIR/$tdir/$tfile
3688         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3689         pid=$!
3690         # give multiop a chance to open
3691         sleep 1
3692
3693         $DIR/$tdir/$tfile && error || true
3694         kill -USR1 $pid
3695 }
3696 run_test 43 "execution of file opened for write should return -ETXTBSY"
3697
3698 test_43a() {
3699         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3700         test_mkdir -p $DIR/$tdir
3701         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3702                         cp -p multiop $DIR/$tdir/multiop
3703         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3704                         return 1
3705         MULTIOP_PID=$!
3706         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3707         kill -USR1 $MULTIOP_PID || return 2
3708         wait $MULTIOP_PID || return 3
3709         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3710 }
3711 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3712
3713 test_43b() {
3714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3715         test_mkdir -p $DIR/$tdir
3716         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3717                         cp -p multiop $DIR/$tdir/multiop
3718         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3719                         return 1
3720         MULTIOP_PID=$!
3721         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3722         kill -USR1 $MULTIOP_PID || return 2
3723         wait $MULTIOP_PID || return 3
3724         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3725 }
3726 run_test 43b "truncate of file being executed should return -ETXTBSY"
3727
3728 test_43c() {
3729         local testdir="$DIR/$tdir"
3730         test_mkdir -p $DIR/$tdir
3731         cp $SHELL $testdir/
3732         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3733                 ( cd $testdir && md5sum -c)
3734 }
3735 run_test 43c "md5sum of copy into lustre========================"
3736
3737 test_44() {
3738         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3739         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3740         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3741 }
3742 run_test 44 "zero length read from a sparse stripe ============="
3743
3744 test_44a() {
3745         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3746                 awk '{ print $2 }')
3747         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3748         [[ $nstripe -gt $OSTCOUNT ]] &&
3749             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3750             return
3751         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3752                 awk '{ print $2 }')
3753         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3754                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3755                         awk '{ print $2 }')
3756         fi
3757
3758         OFFSETS="0 $((stride/2)) $((stride-1))"
3759         for offset in $OFFSETS; do
3760                 for i in $(seq 0 $((nstripe-1))); do
3761                         local GLOBALOFFSETS=""
3762                         # size in Bytes
3763                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3764                         local myfn=$DIR/d44a-$size
3765                         echo "--------writing $myfn at $size"
3766                         ll_sparseness_write $myfn $size ||
3767                                 error "ll_sparseness_write"
3768                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3769                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3770                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3771
3772                         for j in $(seq 0 $((nstripe-1))); do
3773                                 # size in Bytes
3774                                 size=$((((j + $nstripe )*$stride + $offset)))
3775                                 ll_sparseness_write $myfn $size ||
3776                                         error "ll_sparseness_write"
3777                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3778                         done
3779                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3780                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3781                         rm -f $myfn
3782                 done
3783         done
3784 }
3785 run_test 44a "test sparse pwrite ==============================="
3786
3787 dirty_osc_total() {
3788         tot=0
3789         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3790                 tot=$(($tot + $d))
3791         done
3792         echo $tot
3793 }
3794 do_dirty_record() {
3795         before=`dirty_osc_total`
3796         echo executing "\"$*\""
3797         eval $*
3798         after=`dirty_osc_total`
3799         echo before $before, after $after
3800 }
3801 test_45() {
3802         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3803         f="$DIR/f45"
3804         # Obtain grants from OST if it supports it
3805         echo blah > ${f}_grant
3806         stop_writeback
3807         sync
3808         do_dirty_record "echo blah > $f"
3809         [[ $before -eq $after ]] && error "write wasn't cached"
3810         do_dirty_record "> $f"
3811         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3812         do_dirty_record "echo blah > $f"
3813         [[ $before -eq $after ]] && error "write wasn't cached"
3814         do_dirty_record "sync"
3815         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3816         do_dirty_record "echo blah > $f"
3817         [[ $before -eq $after ]] && error "write wasn't cached"
3818         do_dirty_record "cancel_lru_locks osc"
3819         [[ $before -gt $after ]] ||
3820                 error "lock cancellation didn't lower dirty count"
3821         start_writeback
3822 }
3823 run_test 45 "osc io page accounting ============================"
3824
3825 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3826 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3827 # objects offset and an assert hit when an rpc was built with 1023's mapped
3828 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3829 test_46() {
3830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3831         f="$DIR/f46"
3832         stop_writeback
3833         sync
3834         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3835         sync
3836         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3837         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3838         sync
3839         start_writeback
3840 }
3841 run_test 46 "dirtying a previously written page ================"
3842
3843 # test_47 is removed "Device nodes check" is moved to test_28
3844
3845 test_48a() { # bug 2399
3846         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3847         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3848                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3849                 return
3850         check_kernel_version 34 || return 0
3851         test_mkdir -p $DIR/$tdir
3852         cd $DIR/$tdir
3853         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3854         test_mkdir $DIR/$tdir || error "recreate directory failed"
3855         touch foo || error "'touch foo' failed after recreating cwd"
3856         test_mkdir $DIR/$tdir/bar ||
3857                      error "'mkdir foo' failed after recreating cwd"
3858         if check_kernel_version 44; then
3859                 touch .foo || error "'touch .foo' failed after recreating cwd"
3860                 test_mkdir $DIR/$tdir/.bar ||
3861                               error "'mkdir .foo' failed after recreating cwd"
3862         fi
3863         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3864         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3865         cd . || error "'cd .' failed after recreating cwd"
3866         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3867         rmdir . && error "'rmdir .' worked after recreating cwd"
3868         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3869         cd .. || error "'cd ..' failed after recreating cwd"
3870 }
3871 run_test 48a "Access renamed working dir (should return errors)="
3872
3873 test_48b() { # bug 2399
3874         check_kernel_version 34 || return 0
3875         rm -rf $DIR/$tdir
3876         test_mkdir -p $DIR/$tdir
3877         cd $DIR/$tdir
3878         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3879         touch foo && error "'touch foo' worked after removing cwd"
3880         test_mkdir $DIR/$tdir/foo &&
3881                      error "'mkdir foo' worked after removing cwd"
3882         if check_kernel_version 44; then
3883                 touch .foo && error "'touch .foo' worked after removing cwd"
3884                 test_mkdir $DIR/$tdir/.foo &&
3885                               error "'mkdir .foo' worked after removing cwd"
3886         fi
3887         ls . > /dev/null && error "'ls .' worked after removing cwd"
3888         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3889         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3890         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3891         rmdir . && error "'rmdir .' worked after removing cwd"
3892         ln -s . foo && error "'ln -s .' worked after removing cwd"
3893         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3894 }
3895 run_test 48b "Access removed working dir (should return errors)="
3896
3897 test_48c() { # bug 2350
3898         check_kernel_version 36 || return 0
3899         #lctl set_param debug=-1
3900         #set -vx
3901         rm -rf $DIR/$tdir
3902         test_mkdir -p $DIR/$tdir/dir
3903         cd $DIR/$tdir/dir
3904         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3905         $TRACE touch foo && error "touch foo worked after removing cwd"
3906         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3907         if check_kernel_version 44; then
3908                 touch .foo && error "touch .foo worked after removing cwd"
3909                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3910         fi
3911         $TRACE ls . && error "'ls .' worked after removing cwd"
3912         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3913         is_patchless || ( $TRACE cd . &&
3914                         error "'cd .' worked after removing cwd" )
3915         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3916         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3917         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3918         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3919 }
3920 run_test 48c "Access removed working subdir (should return errors)"
3921
3922 test_48d() { # bug 2350
3923         check_kernel_version 36 || return 0
3924         #lctl set_param debug=-1
3925         #set -vx
3926         rm -rf $DIR/$tdir
3927         test_mkdir -p $DIR/$tdir/dir
3928         cd $DIR/$tdir/dir
3929         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3930         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3931         $TRACE touch foo && error "'touch foo' worked after removing parent"
3932         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3933         if check_kernel_version 44; then
3934                 touch .foo && error "'touch .foo' worked after removing parent"
3935                 test_mkdir .foo &&
3936                               error "mkdir .foo worked after removing parent"
3937         fi
3938         $TRACE ls . && error "'ls .' worked after removing parent"
3939         $TRACE ls .. && error "'ls ..' worked after removing parent"
3940         is_patchless || ( $TRACE cd . &&
3941                         error "'cd .' worked after recreate parent" )
3942         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3943         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3944         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3945         is_patchless || ( $TRACE cd .. &&
3946                         error "'cd ..' worked after removing parent" || true )
3947 }
3948 run_test 48d "Access removed parent subdir (should return errors)"
3949
3950 test_48e() { # bug 4134
3951         check_kernel_version 41 || return 0
3952         #lctl set_param debug=-1
3953         #set -vx
3954         rm -rf $DIR/$tdir
3955         test_mkdir -p $DIR/$tdir/dir
3956         cd $DIR/$tdir/dir
3957         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3958         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3959         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3960         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3961         # On a buggy kernel addition of "touch foo" after cd .. will
3962         # produce kernel oops in lookup_hash_it
3963         touch ../foo && error "'cd ..' worked after recreate parent"
3964         cd $DIR
3965         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3966 }
3967 run_test 48e "Access to recreated parent subdir (should return errors)"
3968
3969 test_49() { # LU-1030
3970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3971         # get ost1 size - lustre-OST0000
3972         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3973                 awk '{ print $4 }')
3974         # write 800M at maximum
3975         [[ $ost1_size -lt 2 ]] && ost1_size=2
3976         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3977
3978         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3979         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3980         local dd_pid=$!
3981
3982         # change max_pages_per_rpc while writing the file
3983         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3984         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3985         # loop until dd process exits
3986         while ps ax -opid | grep -wq $dd_pid; do
3987                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3988                 sleep $((RANDOM % 5 + 1))
3989         done
3990         # restore original max_pages_per_rpc
3991         $LCTL set_param $osc1_mppc=$orig_mppc
3992         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3993 }
3994 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3995
3996 test_50() {
3997         # bug 1485
3998         test_mkdir $DIR/$tdir
3999         cd $DIR/$tdir
4000         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4001 }
4002 run_test 50 "special situations: /proc symlinks  ==============="
4003
4004 test_51a() {    # was test_51
4005         # bug 1516 - create an empty entry right after ".." then split dir
4006         test_mkdir -c1 $DIR/$tdir
4007         touch $DIR/$tdir/foo
4008         $MCREATE $DIR/$tdir/bar
4009         rm $DIR/$tdir/foo
4010         createmany -m $DIR/$tdir/longfile 201
4011         FNUM=202
4012         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4013                 $MCREATE $DIR/$tdir/longfile$FNUM
4014                 FNUM=$(($FNUM + 1))
4015                 echo -n "+"
4016         done
4017         echo
4018         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4019 }
4020 run_test 51a "special situations: split htree with empty entry =="
4021
4022 export NUMTEST=70000
4023 test_51b() {
4024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4025         local BASE=$DIR/d${base}.${TESTSUITE}
4026
4027         # cleanup the directory
4028         rm -fr $BASE
4029
4030         test_mkdir -p -c1 $BASE
4031
4032         $LFS df
4033         $LFS df -i
4034         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4035         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4036         [[ $numfree -lt 21000 ]] &&
4037                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4038                 return
4039
4040         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4041                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4042
4043         # need to check free space for the directories as well
4044         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4045         numfree=$((blkfree / 4))
4046         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4047                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4048
4049         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4050         {
4051                 $LFS df
4052                 $LFS df -i
4053                 echo "failed" > $BASE/fnum
4054                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4055         }
4056 }
4057 run_test 51b "exceed 64k subdirectory nlink limit"
4058
4059 test_51ba() { # LU-993
4060         local BASE=$DIR/d${base}.${TESTSUITE}
4061         # unlink all but 100 subdirectories, then check it still works
4062         local LEFT=100
4063         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4064
4065         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4066         local DELETE=$((NUMTEST - LEFT))
4067
4068         # continue on to run this test even if 51b didn't finish,
4069         # just to delete the many subdirectories created.
4070         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4071
4072         # for ldiskfs the nlink count should be 1, but this is OSD specific
4073         # and so this is listed for informational purposes only
4074         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4075         unlinkmany -d $BASE/d $DELETE
4076         RC=$?
4077
4078         if [ $RC -ne 0 ]; then
4079                 if [ "$NUMPREV" == "failed" ]; then
4080                         skip "previous setup failed"
4081                         return 0
4082                 else
4083                         error "unlink of first $DELETE subdirs failed"
4084                         return $RC
4085                 fi
4086         fi
4087
4088         echo "nlink between: $(stat -c %h $BASE)"
4089         # trim the first line of ls output
4090         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4091         [ $FOUND -ne $LEFT ] &&
4092                 error "can't find subdirs: found only $FOUND/$LEFT"
4093
4094         unlinkmany -d $BASE/d $DELETE $LEFT ||
4095                 error "unlink of second $LEFT subdirs failed"
4096         # regardless of whether the backing filesystem tracks nlink accurately
4097         # or not, the nlink count shouldn't be more than "." and ".." here
4098         local AFTER=$(stat -c %h $BASE)
4099         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4100                 echo "nlink after: $AFTER"
4101 }
4102 run_test 51ba "verify nlink for many subdirectory cleanup"
4103
4104 test_51d() {
4105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4106         [[ $OSTCOUNT -lt 3 ]] &&
4107                 skip_env "skipping test with few OSTs" && return
4108         test_mkdir -p $DIR/$tdir
4109         createmany -o $DIR/$tdir/t- 1000
4110         $GETSTRIPE $DIR/$tdir > $TMP/files
4111         for N in $(seq 0 $((OSTCOUNT - 1))); do
4112                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4113                         END { printf("%0.0f", objs) }' $TMP/files)
4114                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4115                         '($1 == '$N') { objs += 1 } \
4116                         END { printf("%0.0f", objs) }')
4117                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4118         done
4119         unlinkmany $DIR/$tdir/t- 1000
4120
4121         NLAST=0
4122         for N in $(seq 1 $((OSTCOUNT - 1))); do
4123                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4124                         error "OST $N has less objects vs OST $NLAST" \
4125                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4126                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4127                         error "OST $N has less objects vs OST $NLAST" \
4128                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4129
4130                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4131                         error "OST $N has less #0 objects vs OST $NLAST" \
4132                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4133                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4134                         error "OST $N has less #0 objects vs OST $NLAST" \
4135                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4136                 NLAST=$N
4137         done
4138 }
4139 run_test 51d "check object distribution ===================="
4140
4141 test_51e() {
4142         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4143                 skip "Only applicable to ldiskfs-based MDTs"
4144                 return
4145         fi
4146
4147         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4148         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4149
4150         touch $DIR/$tdir/d0/foo
4151         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4152                 error "file exceed 65000 nlink limit!"
4153         unlinkmany $DIR/$tdir/d0/f- 65001
4154         return 0
4155 }
4156 run_test 51e "check file nlink limit"
4157
4158 test_52a() {
4159         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4160         test_mkdir -p $DIR/$tdir
4161         touch $DIR/$tdir/foo
4162         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4163         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4164         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4165         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4166         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4167                                         error "link worked"
4168         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4169         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4170         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4171                                                      error "lsattr"
4172         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4173         cp -r $DIR/$tdir /tmp/
4174         rm -fr $DIR/$tdir || error "cleanup rm failed"
4175 }
4176 run_test 52a "append-only flag test (should return errors) ====="
4177
4178 test_52b() {
4179         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4180         test_mkdir -p $DIR/$tdir
4181         touch $DIR/$tdir/foo
4182         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4183         cat test > $DIR/$tdir/foo && error "cat test worked"
4184         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4185         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4186         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4187                                         error "link worked"
4188         echo foo >> $DIR/$tdir/foo && error "echo worked"
4189         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4190         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4191         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4192         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4193                                                         error "lsattr"
4194         chattr -i $DIR/$tdir/foo || error "chattr failed"
4195
4196         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4197 }
4198 run_test 52b "immutable flag test (should return errors) ======="
4199
4200 test_53() {
4201         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4202         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4203         remote_ost_nodsh && skip "remote OST with nodsh" && return
4204
4205         local param
4206         local param_seq
4207         local ostname
4208         local mds_last
4209         local mds_last_seq
4210         local ost_last
4211         local ost_last_seq
4212         local ost_last_id
4213         local ostnum
4214         local node
4215         local found=false
4216         local support_last_seq=true
4217
4218         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4219                 support_last_seq=false
4220
4221         # only test MDT0000
4222         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4223         local value
4224         for value in $(do_facet $SINGLEMDS \
4225                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4226                 param=$(echo ${value[0]} | cut -d "=" -f1)
4227                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4228
4229                 if $support_last_seq; then
4230                         param_seq=$(echo $param |
4231                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4232                         mds_last_seq=$(do_facet $SINGLEMDS \
4233                                        $LCTL get_param -n $param_seq)
4234                 fi
4235                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4236
4237                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4238                 node=$(facet_active_host ost$((ostnum+1)))
4239                 param="obdfilter.$ostname.last_id"
4240                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4241                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4242                         ost_last_id=$ost_last
4243
4244                         if $support_last_seq; then
4245                                 ost_last_id=$(echo $ost_last |
4246                                               awk -F':' '{print $2}' |
4247                                               sed -e "s/^0x//g")
4248                                 ost_last_seq=$(echo $ost_last |
4249                                                awk -F':' '{print $1}')
4250                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4251                         fi
4252
4253                         if [[ $ost_last_id != $mds_last ]]; then
4254                                 error "$ost_last_id != $mds_last"
4255                         else
4256                                 found=true
4257                                 break
4258                         fi
4259                 done
4260         done
4261         $found || error "can not match last_seq/last_id for $mdtosc"
4262         return 0
4263 }
4264 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4265
4266 test_54a() {
4267         $SOCKETSERVER $DIR/socket ||
4268                 error "$SOCKETSERVER $DIR/socket failed: $?"
4269         $SOCKETCLIENT $DIR/socket ||
4270                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4271         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4272 }
4273 run_test 54a "unix domain socket test =========================="
4274
4275 test_54b() {
4276         f="$DIR/f54b"
4277         mknod $f c 1 3
4278         chmod 0666 $f
4279         dd if=/dev/zero of=$f bs=$(page_size) count=1
4280 }
4281 run_test 54b "char device works in lustre ======================"
4282
4283 find_loop_dev() {
4284         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4285         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4286         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4287
4288         for i in $(seq 3 7); do
4289                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4290                 LOOPDEV=$LOOPBASE$i
4291                 LOOPNUM=$i
4292                 break
4293         done
4294 }
4295
4296 cleanup_54c() {
4297         loopdev="$DIR/loop54c"
4298
4299         trap 0
4300         $UMOUNT -d $DIR/$tdir || rc=$?
4301         losetup -d $loopdev || true
4302         losetup -d $LOOPDEV || true
4303         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4304         return $rc
4305 }
4306
4307 test_54c() {
4308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4309         loopdev="$DIR/loop54c"
4310
4311         find_loop_dev
4312         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4313         trap cleanup_54c EXIT
4314         mknod $loopdev b 7 $LOOPNUM
4315         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4316         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4317         losetup $loopdev $DIR/$tfile ||
4318                 error "can't set up $loopdev for $DIR/$tfile"
4319         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4320         test_mkdir -p $DIR/$tdir
4321         mount -t ext2 $loopdev $DIR/$tdir ||
4322                 error "error mounting $loopdev on $DIR/$tdir"
4323         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4324                 error "dd write"
4325         df $DIR/$tdir
4326         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4327                 error "dd read"
4328         cleanup_54c
4329 }
4330 run_test 54c "block device works in lustre ====================="
4331
4332 test_54d() {
4333         f="$DIR/f54d"
4334         string="aaaaaa"
4335         mknod $f p
4336         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4337 }
4338 run_test 54d "fifo device works in lustre ======================"
4339
4340 test_54e() {
4341         check_kernel_version 46 || return 0
4342         f="$DIR/f54e"
4343         string="aaaaaa"
4344         cp -aL /dev/console $f
4345         echo $string > $f || error "echo $string to $f failed"
4346 }
4347 run_test 54e "console/tty device works in lustre ======================"
4348
4349 #The test_55 used to be iopen test and it was removed by bz#24037.
4350 #run_test 55 "check iopen_connect_dentry() ======================"
4351
4352 test_56a() {    # was test_56
4353         rm -rf $DIR/$tdir
4354         $SETSTRIPE -d $DIR
4355         test_mkdir -p $DIR/$tdir/dir
4356         NUMFILES=3
4357         NUMFILESx2=$(($NUMFILES * 2))
4358         for i in $(seq 1 $NUMFILES); do
4359                 touch $DIR/$tdir/file$i
4360                 touch $DIR/$tdir/dir/file$i
4361         done
4362
4363         # test lfs getstripe with --recursive
4364         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4365         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4366                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4367         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4368         [[ $FILENUM -eq $NUMFILES ]] ||
4369                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4370         echo "$GETSTRIPE --recursive passed."
4371
4372         # test lfs getstripe with file instead of dir
4373         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4374         [[ $FILENUM -eq 1 ]] ||
4375                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4376         echo "$GETSTRIPE file1 passed."
4377
4378         #test lfs getstripe with --verbose
4379         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4380                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4381                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4382         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4383                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4384         echo "$GETSTRIPE --verbose passed."
4385
4386         #test lfs getstripe with --obd
4387         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4388                 grep -q "unknown obduuid" ||
4389                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4390
4391         [[ $OSTCOUNT -lt 2 ]] &&
4392                 skip_env "skipping other $GETSTRIPE --obd test" && return
4393
4394         OSTIDX=1
4395         OBDUUID=$(ostuuid_from_index $OSTIDX)
4396         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4397         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4398         [[ $FOUND -eq $FILENUM ]] ||
4399                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4400         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4401                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4402                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4403                 error "$GETSTRIPE --obd: should not show file on other obd"
4404         echo "$GETSTRIPE --obd passed"
4405 }
4406 run_test 56a "check $GETSTRIPE"
4407
4408 NUMFILES=3
4409 NUMDIRS=3
4410 setup_56() {
4411         local LOCAL_NUMFILES="$1"
4412         local LOCAL_NUMDIRS="$2"
4413         local MKDIR_PARAMS="$3"
4414         local DIR_STRIPE_PARAMS="$4"
4415
4416         if [ ! -d "$TDIR" ] ; then
4417                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4418                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4419                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4420                         touch $TDIR/file$i
4421                 done
4422                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4423                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4424                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4425                                 touch $TDIR/dir$i/file$j
4426                         done
4427                 done
4428         fi
4429 }
4430
4431 setup_56_special() {
4432         LOCAL_NUMFILES=$1
4433         LOCAL_NUMDIRS=$2
4434         setup_56 $1 $2
4435         if [ ! -e "$TDIR/loop1b" ] ; then
4436                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4437                         mknod $TDIR/loop${i}b b 7 $i
4438                         mknod $TDIR/null${i}c c 1 3
4439                         ln -s $TDIR/file1 $TDIR/link${i}l
4440                 done
4441                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4442                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4443                         mknod $TDIR/dir$i/null${i}c c 1 3
4444                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4445                 done
4446         fi
4447 }
4448
4449 test_56g() {
4450         $SETSTRIPE -d $DIR
4451
4452         TDIR=$DIR/${tdir}g
4453         setup_56 $NUMFILES $NUMDIRS
4454
4455         EXPECTED=$(($NUMDIRS + 2))
4456         # test lfs find with -name
4457         for i in $(seq 1 $NUMFILES) ; do
4458                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4459                 [ $NUMS -eq $EXPECTED ] ||
4460                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4461                               "found $NUMS, expected $EXPECTED"
4462         done
4463 }
4464 run_test 56g "check lfs find -name ============================="
4465
4466 test_56h() {
4467         $SETSTRIPE -d $DIR
4468
4469         TDIR=$DIR/${tdir}g
4470         setup_56 $NUMFILES $NUMDIRS
4471
4472         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4473         # test lfs find with ! -name
4474         for i in $(seq 1 $NUMFILES) ; do
4475                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4476                 [ $NUMS -eq $EXPECTED ] ||
4477                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4478                               "found $NUMS, expected $EXPECTED"
4479         done
4480 }
4481 run_test 56h "check lfs find ! -name ============================="
4482
4483 test_56i() {
4484        tdir=${tdir}i
4485        test_mkdir -p $DIR/$tdir
4486        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4487        CMD="$LFIND -ost $UUID $DIR/$tdir"
4488        OUT=$($CMD)
4489        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4490 }
4491 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4492
4493 test_56j() {
4494         TDIR=$DIR/${tdir}g
4495         setup_56_special $NUMFILES $NUMDIRS
4496
4497         EXPECTED=$((NUMDIRS + 1))
4498         CMD="$LFIND -type d $TDIR"
4499         NUMS=$($CMD | wc -l)
4500         [ $NUMS -eq $EXPECTED ] ||
4501                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4502 }
4503 run_test 56j "check lfs find -type d ============================="
4504
4505 test_56k() {
4506         TDIR=$DIR/${tdir}g
4507         setup_56_special $NUMFILES $NUMDIRS
4508
4509         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4510         CMD="$LFIND -type f $TDIR"
4511         NUMS=$($CMD | wc -l)
4512         [ $NUMS -eq $EXPECTED ] ||
4513                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4514 }
4515 run_test 56k "check lfs find -type f ============================="
4516
4517 test_56l() {
4518         TDIR=$DIR/${tdir}g
4519         setup_56_special $NUMFILES $NUMDIRS
4520
4521         EXPECTED=$((NUMDIRS + NUMFILES))
4522         CMD="$LFIND -type b $TDIR"
4523         NUMS=$($CMD | wc -l)
4524         [ $NUMS -eq $EXPECTED ] ||
4525                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4526 }
4527 run_test 56l "check lfs find -type b ============================="
4528
4529 test_56m() {
4530         TDIR=$DIR/${tdir}g
4531         setup_56_special $NUMFILES $NUMDIRS
4532
4533         EXPECTED=$((NUMDIRS + NUMFILES))
4534         CMD="$LFIND -type c $TDIR"
4535         NUMS=$($CMD | wc -l)
4536         [ $NUMS -eq $EXPECTED ] ||
4537                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4538 }
4539 run_test 56m "check lfs find -type c ============================="
4540
4541 test_56n() {
4542         TDIR=$DIR/${tdir}g
4543         setup_56_special $NUMFILES $NUMDIRS
4544
4545         EXPECTED=$((NUMDIRS + NUMFILES))
4546         CMD="$LFIND -type l $TDIR"
4547         NUMS=$($CMD | wc -l)
4548         [ $NUMS -eq $EXPECTED ] ||
4549                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4550 }
4551 run_test 56n "check lfs find -type l ============================="
4552
4553 test_56o() {
4554         TDIR=$DIR/${tdir}o
4555         setup_56 $NUMFILES $NUMDIRS
4556         utime $TDIR/file1 > /dev/null || error "utime (1)"
4557         utime $TDIR/file2 > /dev/null || error "utime (2)"
4558         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4559         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4560         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4561         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4562
4563         EXPECTED=4
4564         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4565         [ $NUMS -eq $EXPECTED ] || \
4566                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4567
4568         EXPECTED=12
4569         CMD="$LFIND -mtime 0 $TDIR"
4570         NUMS=$($CMD | wc -l)
4571         [ $NUMS -eq $EXPECTED ] ||
4572                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4573 }
4574 run_test 56o "check lfs find -mtime for old files =========================="
4575
4576 test_56p() {
4577         [ $RUNAS_ID -eq $UID ] &&
4578                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4579
4580         TDIR=$DIR/${tdir}p
4581         setup_56 $NUMFILES $NUMDIRS
4582
4583         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4584         EXPECTED=$NUMFILES
4585         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4586         NUMS=$($CMD | wc -l)
4587         [ $NUMS -eq $EXPECTED ] || \
4588                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4589
4590         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4591         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4592         NUMS=$($CMD | wc -l)
4593         [ $NUMS -eq $EXPECTED ] || \
4594                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4595 }
4596 run_test 56p "check lfs find -uid and ! -uid ==============================="
4597
4598 test_56q() {
4599         [ $RUNAS_ID -eq $UID ] &&
4600                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4601
4602         TDIR=$DIR/${tdir}q
4603         setup_56 $NUMFILES $NUMDIRS
4604
4605         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4606
4607         EXPECTED=$NUMFILES
4608         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4609         NUMS=$($CMD | wc -l)
4610         [ $NUMS -eq $EXPECTED ] ||
4611                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4612
4613         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4614         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4615         NUMS=$($CMD | wc -l)
4616         [ $NUMS -eq $EXPECTED ] ||
4617                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4618 }
4619 run_test 56q "check lfs find -gid and ! -gid ==============================="
4620
4621 test_56r() {
4622         TDIR=$DIR/${tdir}r
4623         setup_56 $NUMFILES $NUMDIRS
4624
4625         EXPECTED=12
4626         CMD="$LFIND -size 0 -type f $TDIR"
4627         NUMS=$($CMD | wc -l)
4628         [ $NUMS -eq $EXPECTED ] ||
4629                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4630         EXPECTED=0
4631         CMD="$LFIND ! -size 0 -type f $TDIR"
4632         NUMS=$($CMD | wc -l)
4633         [ $NUMS -eq $EXPECTED ] ||
4634                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4635         echo "test" > $TDIR/$tfile
4636         echo "test2" > $TDIR/$tfile.2 && sync
4637         EXPECTED=1
4638         CMD="$LFIND -size 5 -type f $TDIR"
4639         NUMS=$($CMD | wc -l)
4640         [ $NUMS -eq $EXPECTED ] ||
4641                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4642         EXPECTED=1
4643         CMD="$LFIND -size +5 -type f $TDIR"
4644         NUMS=$($CMD | wc -l)
4645         [ $NUMS -eq $EXPECTED ] ||
4646                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4647         EXPECTED=2
4648         CMD="$LFIND -size +0 -type f $TDIR"
4649         NUMS=$($CMD | wc -l)
4650         [ $NUMS -eq $EXPECTED ] ||
4651                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4652         EXPECTED=2
4653         CMD="$LFIND ! -size -5 -type f $TDIR"
4654         NUMS=$($CMD | wc -l)
4655         [ $NUMS -eq $EXPECTED ] ||
4656                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4657         EXPECTED=12
4658         CMD="$LFIND -size -5 -type f $TDIR"
4659         NUMS=$($CMD | wc -l)
4660         [ $NUMS -eq $EXPECTED ] ||
4661                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4662 }
4663 run_test 56r "check lfs find -size works =========================="
4664
4665 test_56s() { # LU-611
4666         TDIR=$DIR/${tdir}s
4667         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4668
4669         if [[ $OSTCOUNT -gt 1 ]]; then
4670                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4671                 ONESTRIPE=4
4672                 EXTRA=4
4673         else
4674                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4675                 EXTRA=0
4676         fi
4677
4678         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4679         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4680         NUMS=$($CMD | wc -l)
4681         [ $NUMS -eq $EXPECTED ] || {
4682                 $GETSTRIPE -R $TDIR
4683                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4684         }
4685
4686         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4687         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4688         NUMS=$($CMD | wc -l)
4689         [ $NUMS -eq $EXPECTED ] || {
4690                 $GETSTRIPE -R $TDIR
4691                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4692         }
4693
4694         EXPECTED=$ONESTRIPE
4695         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4696         NUMS=$($CMD | wc -l)
4697         [ $NUMS -eq $EXPECTED ] || {
4698                 $GETSTRIPE -R $TDIR
4699                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4700         }
4701
4702         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4703         NUMS=$($CMD | wc -l)
4704         [ $NUMS -eq $EXPECTED ] || {
4705                 $GETSTRIPE -R $TDIR
4706                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4707         }
4708
4709         EXPECTED=0
4710         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4711         NUMS=$($CMD | wc -l)
4712         [ $NUMS -eq $EXPECTED ] || {
4713                 $GETSTRIPE -R $TDIR
4714                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4715         }
4716 }
4717 run_test 56s "check lfs find -stripe-count works"
4718
4719 test_56t() { # LU-611
4720         TDIR=$DIR/${tdir}t
4721         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4722
4723         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4724
4725         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4726         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4727         NUMS=$($CMD | wc -l)
4728         [ $NUMS -eq $EXPECTED ] ||
4729                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4730
4731         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4732         NUMS=$($CMD | wc -l)
4733         [ $NUMS -eq $EXPECTED ] ||
4734                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4735
4736         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4737         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4738         NUMS=$($CMD | wc -l)
4739         [ $NUMS -eq $EXPECTED ] ||
4740                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4741
4742         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4743         NUMS=$($CMD | wc -l)
4744         [ $NUMS -eq $EXPECTED ] ||
4745                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4746
4747         EXPECTED=4
4748         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4749         NUMS=$($CMD | wc -l)
4750         [ $NUMS -eq $EXPECTED ] ||
4751                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4752
4753         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4754         NUMS=$($CMD | wc -l)
4755         [ $NUMS -eq $EXPECTED ] ||
4756                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4757
4758         EXPECTED=0
4759         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4760         NUMS=$($CMD | wc -l)
4761         [ $NUMS -eq $EXPECTED ] ||
4762                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4763 }
4764 run_test 56t "check lfs find -stripe-size works"
4765
4766 test_56u() { # LU-611
4767         TDIR=$DIR/${tdir}u
4768         setup_56 $NUMFILES $NUMDIRS "-i 0"
4769
4770         if [[ $OSTCOUNT -gt 1 ]]; then
4771                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4772                 ONESTRIPE=4
4773         else
4774                 ONESTRIPE=0
4775         fi
4776
4777         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4778         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4779         NUMS=$($CMD | wc -l)
4780         [ $NUMS -eq $EXPECTED ] ||
4781                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4782
4783         EXPECTED=$ONESTRIPE
4784         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4785         NUMS=$($CMD | wc -l)
4786         [ $NUMS -eq $EXPECTED ] ||
4787                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4788
4789         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4790         NUMS=$($CMD | wc -l)
4791         [ $NUMS -eq $EXPECTED ] ||
4792                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4793
4794         EXPECTED=0
4795         # This should produce an error and not return any files
4796         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4797         NUMS=$($CMD 2>/dev/null | wc -l)
4798         [ $NUMS -eq $EXPECTED ] ||
4799                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4800
4801         if [[ $OSTCOUNT -gt 1 ]]; then
4802                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4803                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4804                 NUMS=$($CMD | wc -l)
4805                 [ $NUMS -eq $EXPECTED ] ||
4806                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4807         fi
4808 }
4809 run_test 56u "check lfs find -stripe-index works"
4810
4811 test_56v() {
4812     local MDT_IDX=0
4813
4814     TDIR=$DIR/${tdir}v
4815     rm -rf $TDIR
4816     setup_56 $NUMFILES $NUMDIRS
4817
4818     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4819     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4820
4821     for file in $($LFIND -mdt $UUID $TDIR); do
4822         file_mdt_idx=$($GETSTRIPE -M $file)
4823         [ $file_mdt_idx -eq $MDT_IDX ] ||
4824             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4825     done
4826 }
4827 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4828
4829 test_56w() {
4830         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4831                 return
4832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4833         TDIR=$DIR/${tdir}w
4834
4835     rm -rf $TDIR || error "remove $TDIR failed"
4836     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4837
4838     local stripe_size
4839     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4840         error "$GETSTRIPE -S -d $TDIR failed"
4841     stripe_size=${stripe_size%% *}
4842
4843     local file_size=$((stripe_size * OSTCOUNT))
4844     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4845     local required_space=$((file_num * file_size))
4846     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4847     [[ $free_space -le $((required_space / 1024)) ]] &&
4848         skip_env "need at least $required_space bytes free space," \
4849                  "have $free_space kbytes" && return
4850
4851     local dd_bs=65536
4852     local dd_count=$((file_size / dd_bs))
4853
4854     # write data into the files
4855     local i
4856     local j
4857     local file
4858     for i in $(seq 1 $NUMFILES); do
4859         file=$TDIR/file$i
4860         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4861             error "write data into $file failed"
4862     done
4863     for i in $(seq 1 $NUMDIRS); do
4864         for j in $(seq 1 $NUMFILES); do
4865             file=$TDIR/dir$i/file$j
4866             yes | dd bs=$dd_bs count=$dd_count of=$file \
4867                 >/dev/null 2>&1 ||
4868                 error "write data into $file failed"
4869         done
4870     done
4871
4872     local expected=-1
4873     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4874
4875     # lfs_migrate file
4876     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4877     echo "$cmd"
4878     eval $cmd || error "$cmd failed"
4879
4880     check_stripe_count $TDIR/file1 $expected
4881
4882         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4883         then
4884                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4885                 # OST 1 if it is on OST 0. This file is small enough to
4886                 # be on only one stripe.
4887                 file=$TDIR/migr_1_ost
4888                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4889                         error "write data into $file failed"
4890                 local obdidx=$($LFS getstripe -i $file)
4891                 local oldmd5=$(md5sum $file)
4892                 local newobdidx=0
4893                 [[ $obdidx -eq 0 ]] && newobdidx=1
4894                 cmd="$LFS migrate -i $newobdidx $file"
4895                 echo $cmd
4896                 eval $cmd || error "$cmd failed"
4897                 local realobdix=$($LFS getstripe -i $file)
4898                 local newmd5=$(md5sum $file)
4899                 [[ $newobdidx -ne $realobdix ]] &&
4900                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4901                 [[ "$oldmd5" != "$newmd5" ]] &&
4902                         error "md5sum differ: $oldmd5, $newmd5"
4903         fi
4904
4905     # lfs_migrate dir
4906     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4907     echo "$cmd"
4908     eval $cmd || error "$cmd failed"
4909
4910     for j in $(seq 1 $NUMFILES); do
4911         check_stripe_count $TDIR/dir1/file$j $expected
4912     done
4913
4914     # lfs_migrate works with lfs find
4915     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4916          $LFS_MIGRATE -y -c $expected"
4917     echo "$cmd"
4918     eval $cmd || error "$cmd failed"
4919
4920     for i in $(seq 2 $NUMFILES); do
4921         check_stripe_count $TDIR/file$i $expected
4922     done
4923     for i in $(seq 2 $NUMDIRS); do
4924         for j in $(seq 1 $NUMFILES); do
4925             check_stripe_count $TDIR/dir$i/file$j $expected
4926         done
4927     done
4928 }
4929 run_test 56w "check lfs_migrate -c stripe_count works"
4930
4931 test_56x() {
4932         check_swap_layouts_support && return 0
4933         [[ $OSTCOUNT -lt 2 ]] &&
4934                 skip_env "need 2 OST, skipping test" && return
4935
4936         local dir0=$DIR/$tdir/$testnum
4937         mkdir -p $dir0 || error "creating dir $dir0"
4938
4939         local ref1=/etc/passwd
4940         local file1=$dir0/file1
4941
4942         $SETSTRIPE -c 2 $file1
4943         cp $ref1 $file1
4944         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4945         stripe=$($GETSTRIPE -c $file1)
4946         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4947         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4948
4949         # clean up
4950         rm -f $file1
4951 }
4952 run_test 56x "lfs migration support"
4953
4954 test_56y() {
4955         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4956                 skip "No HSM support on MDS of $(get_lustre_version)," \
4957                          "need 2.4.53 at least" && return
4958         local res=""
4959
4960         local dir0=$DIR/$tdir/$testnum
4961         mkdir -p $dir0 || error "creating dir $dir0"
4962         local f1=$dir0/file1
4963         local f2=$dir0/file2
4964
4965         touch $f1 || error "creating std file $f1"
4966         $MULTIOP $f2 H2c || error "creating released file $f2"
4967
4968         # a directory can be raid0, so ask only for files
4969         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4970         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4971
4972         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4973         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4974
4975         # only files can be released, so no need to force file search
4976         res=$($LFIND $dir0 -L released)
4977         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4978
4979         res=$($LFIND $dir0 \! -L released)
4980         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4981
4982 }
4983 run_test 56y "lfs find -L raid0|released"
4984
4985 test_56z() { # LU-4824
4986         # This checks to make sure 'lfs find' continues after errors
4987         # There are two classes of errors that should be caught:
4988         # - If multiple paths are provided, all should be searched even if one
4989         #   errors out
4990         # - If errors are encountered during the search, it should not terminate
4991         #   early
4992         local i
4993         test_mkdir $DIR/$tdir
4994         for i in d{0..9}; do
4995                 test_mkdir $DIR/$tdir/$i
4996         done
4997         touch $DIR/$tdir/d{0..9}/$tfile
4998         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4999                 error "$LFS find did not return an error"
5000         # Make a directory unsearchable. This should NOT be the last entry in
5001         # directory order.  Arbitrarily pick the 6th entry
5002         chmod 700 $(lfs find $DIR/$tdir -type d | sed '6!d')
5003         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5004         # The user should be able to see 10 directories and 9 files
5005         [ $count == 19 ] || error "$LFS find did not continue after error"
5006 }
5007 run_test 56z "lfs find should continue after an error"
5008
5009 test_57a() {
5010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5011         # note test will not do anything if MDS is not local
5012         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5013                 skip "Only applicable to ldiskfs-based MDTs"
5014                 return
5015         fi
5016
5017         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5018         local MNTDEV="osd*.*MDT*.mntdev"
5019         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5020         [ -z "$DEV" ] && error "can't access $MNTDEV"
5021         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5022                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5023                         error "can't access $DEV"
5024                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5025                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5026                 rm $TMP/t57a.dump
5027         done
5028 }
5029 run_test 57a "verify MDS filesystem created with large inodes =="
5030
5031 test_57b() {
5032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5033         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5034                 skip "Only applicable to ldiskfs-based MDTs"
5035                 return
5036         fi
5037
5038         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5039         local dir=$DIR/$tdir
5040
5041         local FILECOUNT=100
5042         local FILE1=$dir/f1
5043         local FILEN=$dir/f$FILECOUNT
5044
5045         rm -rf $dir || error "removing $dir"
5046         test_mkdir -p -c1 $dir || error "creating $dir"
5047         local mdtidx=$($LFS getstripe -M $dir)
5048         local mdtname=MDT$(printf %04x $mdtidx)
5049         local facet=mds$((mdtidx + 1))
5050
5051         echo "mcreating $FILECOUNT files"
5052         createmany -m $dir/f 1 $FILECOUNT || \
5053                 error "creating files in $dir"
5054
5055         # verify that files do not have EAs yet
5056         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5057         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5058
5059         sync
5060         sleep 1
5061         df $dir  #make sure we get new statfs data
5062         local MDSFREE=$(do_facet $facet \
5063                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5064         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5065         echo "opening files to create objects/EAs"
5066         local FILE
5067         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5068                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5069         done
5070
5071         # verify that files have EAs now
5072         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5073         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5074
5075         sleep 1  #make sure we get new statfs data
5076         df $dir
5077         local MDSFREE2=$(do_facet $facet \
5078                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5079         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5080         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5081                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5082                         error "MDC before $MDCFREE != after $MDCFREE2"
5083                 else
5084                         echo "MDC before $MDCFREE != after $MDCFREE2"
5085                         echo "unable to confirm if MDS has large inodes"
5086                 fi
5087         fi
5088         rm -rf $dir
5089 }
5090 run_test 57b "default LOV EAs are stored inside large inodes ==="
5091
5092 test_58() {
5093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5094         [ -z "$(which wiretest 2>/dev/null)" ] &&
5095                         skip_env "could not find wiretest" && return
5096         wiretest
5097 }
5098 run_test 58 "verify cross-platform wire constants =============="
5099
5100 test_59() {
5101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5102         echo "touch 130 files"
5103         createmany -o $DIR/f59- 130
5104         echo "rm 130 files"
5105         unlinkmany $DIR/f59- 130
5106         sync
5107         # wait for commitment of removal
5108         wait_delete_completed
5109 }
5110 run_test 59 "verify cancellation of llog records async ========="
5111
5112 TEST60_HEAD="test_60 run $RANDOM"
5113 test_60a() {
5114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5115         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5116         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5117                 skip_env "missing subtest run-llog.sh" && return
5118         log "$TEST60_HEAD - from kernel mode"
5119         do_facet mgs sh run-llog.sh
5120 }
5121 run_test 60a "llog sanity tests run from kernel module =========="
5122
5123 test_60b() { # bug 6411
5124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5125         dmesg > $DIR/$tfile
5126         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5127                                  /llog.test/ {
5128                                          if (marker)
5129                                                  from_marker++
5130                                          from_begin++
5131                                  }
5132                                  END {
5133                                          if (marker)
5134                                                  print from_marker
5135                                          else
5136                                                  print from_begin
5137                                  }"`
5138         [[ $LLOG_COUNT -gt 50 ]] &&
5139                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5140 }
5141 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5142
5143 test_60c() {
5144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5145         echo "create 5000 files"
5146         createmany -o $DIR/f60c- 5000
5147 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5148         lctl set_param fail_loc=0x80000137
5149         unlinkmany $DIR/f60c- 5000
5150         lctl set_param fail_loc=0
5151 }
5152 run_test 60c "unlink file when mds full"
5153
5154 test_60d() {
5155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5156         SAVEPRINTK=$(lctl get_param -n printk)
5157
5158         # verify "lctl mark" is even working"
5159         MESSAGE="test message ID $RANDOM $$"
5160         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5161         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5162
5163         lctl set_param printk=0 || error "set lnet.printk failed"
5164         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5165         MESSAGE="new test message ID $RANDOM $$"
5166         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5167         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5168         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5169
5170         lctl set_param -n printk="$SAVEPRINTK"
5171 }
5172 run_test 60d "test printk console message masking"
5173
5174 test_61() {
5175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5176         f="$DIR/f61"
5177         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5178         cancel_lru_locks osc
5179         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5180         sync
5181 }
5182 run_test 61 "mmap() writes don't make sync hang ================"
5183
5184 # bug 2330 - insufficient obd_match error checking causes LBUG
5185 test_62() {
5186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5187         f="$DIR/f62"
5188         echo foo > $f
5189         cancel_lru_locks osc
5190         lctl set_param fail_loc=0x405
5191         cat $f && error "cat succeeded, expect -EIO"
5192         lctl set_param fail_loc=0
5193 }
5194 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5195 # match every page all of the time.
5196 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5197
5198 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5199 # Though this test is irrelevant anymore, it helped to reveal some
5200 # other grant bugs (LU-4482), let's keep it.
5201 test_63a() {   # was test_63
5202         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5203         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5204         for i in `seq 10` ; do
5205                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5206                 sleep 5
5207                 kill $!
5208                 sleep 1
5209         done
5210
5211         rm -f $DIR/f63 || true
5212 }
5213 run_test 63a "Verify oig_wait interruption does not crash ======="
5214
5215 # bug 2248 - async write errors didn't return to application on sync
5216 # bug 3677 - async write errors left page locked
5217 test_63b() {
5218         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5219         debugsave
5220         lctl set_param debug=-1
5221
5222         # ensure we have a grant to do async writes
5223         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5224         rm $DIR/$tfile
5225
5226         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5227         lctl set_param fail_loc=0x80000406
5228         $MULTIOP $DIR/$tfile Owy && \
5229                 error "sync didn't return ENOMEM"
5230         sync; sleep 2; sync     # do a real sync this time to flush page
5231         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5232                 error "locked page left in cache after async error" || true
5233         debugrestore
5234 }
5235 run_test 63b "async write errors should be returned to fsync ==="
5236
5237 test_64a () {
5238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5239         df $DIR
5240         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5241 }
5242 run_test 64a "verify filter grant calculations (in kernel) ====="
5243
5244 test_64b () {
5245         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5246         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5247 }
5248 run_test 64b "check out-of-space detection on client ==========="
5249
5250 test_64c() {
5251         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5252 }
5253 run_test 64c "verify grant shrink ========================------"
5254
5255 # bug 1414 - set/get directories' stripe info
5256 test_65a() {
5257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5258         test_mkdir -p $DIR/$tdir
5259         touch $DIR/$tdir/f1
5260         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5261 }
5262 run_test 65a "directory with no stripe info ===================="
5263
5264 test_65b() {
5265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5266         test_mkdir -p $DIR/$tdir
5267         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5268                                                 error "setstripe"
5269         touch $DIR/$tdir/f2
5270         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5271 }
5272 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5273
5274 test_65c() {
5275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5276         if [[ $OSTCOUNT -gt 1 ]]; then
5277                 test_mkdir -p $DIR/$tdir
5278                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5279                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5280                 touch $DIR/$tdir/f3
5281                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5282         fi
5283 }
5284 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5285
5286 test_65d() {
5287         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5288         test_mkdir -p $DIR/$tdir
5289         if [[ $STRIPECOUNT -le 0 ]]; then
5290                 sc=1
5291         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5292 #LOV_MAX_STRIPE_COUNT is 2000
5293                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5294         else
5295                 sc=$(($STRIPECOUNT - 1))
5296         fi
5297         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5298         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5299         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5300                 error "lverify failed"
5301 }
5302 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5303
5304 test_65e() {
5305         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5306         test_mkdir -p $DIR/$tdir
5307
5308         $SETSTRIPE $DIR/$tdir || error "setstripe"
5309         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5310                                         error "no stripe info failed"
5311         touch $DIR/$tdir/f6
5312         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5313 }
5314 run_test 65e "directory setstripe defaults ======================="
5315
5316 test_65f() {
5317         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5318         test_mkdir -p $DIR/${tdir}f
5319         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5320 }
5321 run_test 65f "dir setstripe permission (should return error) ==="
5322
5323 test_65g() {
5324         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5325         test_mkdir -p $DIR/$tdir
5326         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5327                                                         error "setstripe"
5328         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5329         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5330                 error "delete default stripe failed"
5331 }
5332 run_test 65g "directory setstripe -d ==========================="
5333
5334 test_65h() {
5335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5336         test_mkdir -p $DIR/$tdir
5337         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5338                                                         error "setstripe"
5339         test_mkdir -p $DIR/$tdir/dd1
5340         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5341                 error "stripe info inherit failed"
5342 }
5343 run_test 65h "directory stripe info inherit ===================="
5344
5345 test_65i() { # bug6367
5346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5347         $SETSTRIPE -S 65536 -c -1 $MOUNT
5348 }
5349 run_test 65i "set non-default striping on root directory (bug 6367)="
5350
5351 test_65ia() { # bug12836
5352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5353         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5354 }
5355 run_test 65ia "getstripe on -1 default directory striping"
5356
5357 test_65ib() { # bug12836
5358         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5359         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5360 }
5361 run_test 65ib "getstripe -v on -1 default directory striping"
5362
5363 test_65ic() { # bug12836
5364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5365         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5366 }
5367 run_test 65ic "new find on -1 default directory striping"
5368
5369 test_65j() { # bug6367
5370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5371         sync; sleep 1
5372         # if we aren't already remounting for each test, do so for this test
5373         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5374                 cleanup || error "failed to unmount"
5375                 setup
5376         fi
5377         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5378 }
5379 run_test 65j "set default striping on root directory (bug 6367)="
5380
5381 test_65k() { # bug11679
5382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5383         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5384         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5385
5386     echo "Check OST status: "
5387     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5388               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5389
5390     for OSC in $MDS_OSCS; do
5391         echo $OSC "is activate"
5392         do_facet $SINGLEMDS lctl --device %$OSC activate
5393     done
5394
5395     mkdir -p $DIR/$tdir
5396     for INACTIVE_OSC in $MDS_OSCS; do
5397         echo "Deactivate: " $INACTIVE_OSC
5398         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5399         for STRIPE_OSC in $MDS_OSCS; do
5400             OST=`osc_to_ost $STRIPE_OSC`
5401             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5402                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5403
5404             [ -f $DIR/$tdir/$IDX ] && continue
5405             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5406             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5407             RC=$?
5408             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5409         done
5410         rm -f $DIR/$tdir/*
5411         echo $INACTIVE_OSC "is Activate."
5412         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5413     done
5414 }
5415 run_test 65k "validate manual striping works properly with deactivated OSCs"
5416
5417 test_65l() { # bug 12836
5418         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5419         test_mkdir -p $DIR/$tdir/test_dir
5420         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5421         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5422 }
5423 run_test 65l "lfs find on -1 stripe dir ========================"
5424
5425 # bug 2543 - update blocks count on client
5426 test_66() {
5427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5428         COUNT=${COUNT:-8}
5429         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5430         sync; sync_all_data; sync; sync_all_data
5431         cancel_lru_locks osc
5432         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5433         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5434 }
5435 run_test 66 "update inode blocks count on client ==============="
5436
5437 LLOOP=
5438 LLITELOOPLOAD=
5439 cleanup_68() {
5440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5441         trap 0
5442         if [ ! -z "$LLOOP" ]; then
5443                 if swapon -s | grep -q $LLOOP; then
5444                         swapoff $LLOOP || error "swapoff failed"
5445                 fi
5446
5447                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5448                 rm -f $LLOOP
5449                 unset LLOOP
5450         fi
5451         if [ ! -z "$LLITELOOPLOAD" ]; then
5452                 rmmod llite_lloop
5453                 unset LLITELOOPLOAD
5454         fi
5455         rm -f $DIR/f68*
5456 }
5457
5458 meminfo() {
5459         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5460 }
5461
5462 swap_used() {
5463         swapon -s | awk '($1 == "'$1'") { print $4 }'
5464 }
5465
5466 # test case for lloop driver, basic function
5467 test_68a() {
5468         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5469         [ "$UID" != 0 ] && skip_env "must run as root" && return
5470         llite_lloop_enabled || \
5471                 { skip_env "llite_lloop module disabled" && return; }
5472
5473         trap cleanup_68 EXIT
5474
5475         if ! module_loaded llite_lloop; then
5476                 if load_module llite/llite_lloop; then
5477                         LLITELOOPLOAD=yes
5478                 else
5479                         skip_env "can't find module llite_lloop"
5480                         return
5481                 fi
5482         fi
5483
5484         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5485         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5486         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5487
5488         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5489         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5490
5491         cleanup_68
5492 }
5493 run_test 68a "lloop driver - basic test ========================"
5494
5495 # excercise swapping to lustre by adding a high priority swapfile entry
5496 # and then consuming memory until it is used.
5497 test_68b() {  # was test_68
5498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5499         [ "$UID" != 0 ] && skip_env "must run as root" && return
5500         lctl get_param -n devices | grep -q obdfilter && \
5501                 skip "local OST" && return
5502
5503         grep -q llite_lloop /proc/modules
5504         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5505
5506         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5507                 skip "can't reliably test swap with TCP" && return
5508
5509         MEMTOTAL=`meminfo MemTotal`
5510         NR_BLOCKS=$((MEMTOTAL>>8))
5511         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5512
5513         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5514         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5515         mkswap $DIR/f68b
5516
5517         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5518
5519         trap cleanup_68 EXIT
5520
5521         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5522
5523         echo "before: `swapon -s | grep $LLOOP`"
5524         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5525         echo "after: `swapon -s | grep $LLOOP`"
5526         SWAPUSED=`swap_used $LLOOP`
5527
5528         cleanup_68
5529
5530         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5531 }
5532 run_test 68b "support swapping to Lustre ========================"
5533
5534 # bug5265, obdfilter oa2dentry return -ENOENT
5535 # #define OBD_FAIL_OST_ENOENT 0x217
5536 test_69() {
5537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5538         remote_ost_nodsh && skip "remote OST with nodsh" && return
5539
5540         f="$DIR/$tfile"
5541         $SETSTRIPE -c 1 -i 0 $f
5542
5543         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5544
5545         do_facet ost1 lctl set_param fail_loc=0x217
5546         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5547         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5548
5549         do_facet ost1 lctl set_param fail_loc=0
5550         $DIRECTIO write $f 0 2 || error "write error"
5551
5552         cancel_lru_locks osc
5553         $DIRECTIO read $f 0 1 || error "read error"
5554
5555         do_facet ost1 lctl set_param fail_loc=0x217
5556         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5557
5558         do_facet ost1 lctl set_param fail_loc=0
5559         rm -f $f
5560 }
5561 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5562
5563 test_71() {
5564     test_mkdir -p $DIR/$tdir
5565     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5566 }
5567 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5568
5569 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5571         [ "$RUNAS_ID" = "$UID" ] &&
5572                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5573
5574         # Check that testing environment is properly set up. Skip if not
5575         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5576                 skip_env "User $RUNAS_ID does not exist - skipping"
5577                 return 0
5578         }
5579         touch $DIR/$tfile
5580         chmod 777 $DIR/$tfile
5581         chmod ug+s $DIR/$tfile
5582         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5583                 error "$RUNAS dd $DIR/$tfile failed"
5584         # See if we are still setuid/sgid
5585         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5586                 error "S/gid is not dropped on write"
5587         # Now test that MDS is updated too
5588         cancel_lru_locks mdc
5589         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5590                 error "S/gid is not dropped on MDS"
5591         rm -f $DIR/$tfile
5592 }
5593 run_test 72a "Test that remove suid works properly (bug5695) ===="
5594
5595 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5596         local perm
5597
5598         [ "$RUNAS_ID" = "$UID" ] && \
5599                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5600         [ "$RUNAS_ID" -eq 0 ] && \
5601                 skip_env "RUNAS_ID = 0 -- skipping" && return
5602
5603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5604         # Check that testing environment is properly set up. Skip if not
5605         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5606                 skip_env "User $RUNAS_ID does not exist - skipping"
5607                 return 0
5608         }
5609         touch $DIR/${tfile}-f{g,u}
5610         test_mkdir $DIR/${tfile}-dg
5611         test_mkdir $DIR/${tfile}-du
5612         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5613         chmod g+s $DIR/${tfile}-{f,d}g
5614         chmod u+s $DIR/${tfile}-{f,d}u
5615         for perm in 777 2777 4777; do
5616                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5617                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5618                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5619                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5620         done
5621         true
5622 }
5623 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5624
5625 # bug 3462 - multiple simultaneous MDC requests
5626 test_73() {
5627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5628         test_mkdir $DIR/d73-1
5629         test_mkdir $DIR/d73-2
5630         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5631         pid1=$!
5632
5633         lctl set_param fail_loc=0x80000129
5634         $MULTIOP $DIR/d73-1/f73-2 Oc &
5635         sleep 1
5636         lctl set_param fail_loc=0
5637
5638         $MULTIOP $DIR/d73-2/f73-3 Oc &
5639         pid3=$!
5640
5641         kill -USR1 $pid1
5642         wait $pid1 || return 1
5643
5644         sleep 25
5645
5646         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5647         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5648         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5649
5650         rm -rf $DIR/d73-*
5651 }
5652 run_test 73 "multiple MDC requests (should not deadlock)"
5653
5654 test_74a() { # bug 6149, 6184
5655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5656         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5657         #
5658         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5659         # will spin in a tight reconnection loop
5660         touch $DIR/f74a
5661         $LCTL set_param fail_loc=0x8000030e
5662         # get any lock that won't be difficult - lookup works.
5663         ls $DIR/f74a
5664         $LCTL set_param fail_loc=0
5665         rm -f $DIR/f74a
5666         true
5667 }
5668 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5669
5670 test_74b() { # bug 13310
5671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5672         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5673         #
5674         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5675         # will spin in a tight reconnection loop
5676         $LCTL set_param fail_loc=0x8000030e
5677         # get a "difficult" lock
5678         touch $DIR/f74b
5679         $LCTL set_param fail_loc=0
5680         rm -f $DIR/f74b
5681         true
5682 }
5683 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5684
5685 test_74c() {
5686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5687         #define OBD_FAIL_LDLM_NEW_LOCK
5688         $LCTL set_param fail_loc=0x319
5689         touch $DIR/$tfile && error "touch successful"
5690         $LCTL set_param fail_loc=0
5691         true
5692 }
5693 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5694
5695 num_inodes() {
5696         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5697 }
5698
5699 get_inode_slab_tunables() {
5700         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5701 }
5702
5703 set_inode_slab_tunables() {
5704         echo "lustre_inode_cache $1" > /proc/slabinfo
5705 }
5706
5707 test_76() { # Now for bug 20433, added originally in bug 1443
5708         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5709         local SLAB_SETTINGS=`get_inode_slab_tunables`
5710         local CPUS=`getconf _NPROCESSORS_ONLN`
5711         # we cannot set limit below 1 which means 1 inode in each
5712         # per-cpu cache is still allowed
5713         set_inode_slab_tunables "1 1 0"
5714         cancel_lru_locks osc
5715         BEFORE_INODES=$(num_inodes)
5716         echo "before inodes: $BEFORE_INODES"
5717         local COUNT=1000
5718         [ "$SLOW" = "no" ] && COUNT=100
5719         for i in $(seq $COUNT); do
5720                 touch $DIR/$tfile
5721                 rm -f $DIR/$tfile
5722         done
5723         cancel_lru_locks osc
5724         AFTER_INODES=$(num_inodes)
5725         echo "after inodes: $AFTER_INODES"
5726         local wait=0
5727         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5728                 sleep 2
5729                 AFTER_INODES=$(num_inodes)
5730                 wait=$((wait+2))
5731                 echo "wait $wait seconds inodes: $AFTER_INODES"
5732                 if [ $wait -gt 30 ]; then
5733                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5734                 fi
5735         done
5736         set_inode_slab_tunables "$SLAB_SETTINGS"
5737 }
5738 run_test 76 "confirm clients recycle inodes properly ===="
5739
5740
5741 export ORIG_CSUM=""
5742 set_checksums()
5743 {
5744         # Note: in sptlrpc modes which enable its own bulk checksum, the
5745         # original crc32_le bulk checksum will be automatically disabled,
5746         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5747         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5748         # In this case set_checksums() will not be no-op, because sptlrpc
5749         # bulk checksum will be enabled all through the test.
5750
5751         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5752         lctl set_param -n osc.*.checksums $1
5753         return 0
5754 }
5755
5756 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5757                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5758 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5759 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5760 set_checksum_type()
5761 {
5762         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5763         log "set checksum type to $1"
5764         return 0
5765 }
5766 F77_TMP=$TMP/f77-temp
5767 F77SZ=8
5768 setup_f77() {
5769         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5770                 error "error writing to $F77_TMP"
5771 }
5772
5773 test_77a() { # bug 10889
5774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5775         $GSS && skip "could not run with gss" && return
5776         [ ! -f $F77_TMP ] && setup_f77
5777         set_checksums 1
5778         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5779         set_checksums 0
5780         rm -f $DIR/$tfile
5781 }
5782 run_test 77a "normal checksum read/write operation"
5783
5784 test_77b() { # bug 10889
5785         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5786         $GSS && skip "could not run with gss" && return
5787         [ ! -f $F77_TMP ] && setup_f77
5788         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5789         $LCTL set_param fail_loc=0x80000409
5790         set_checksums 1
5791
5792         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5793                 error "dd error: $?"
5794         $LCTL set_param fail_loc=0
5795
5796         for algo in $CKSUM_TYPES; do
5797                 cancel_lru_locks osc
5798                 set_checksum_type $algo
5799                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5800                 $LCTL set_param fail_loc=0x80000408
5801                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5802                 $LCTL set_param fail_loc=0
5803         done
5804         set_checksums 0
5805         set_checksum_type $ORIG_CSUM_TYPE
5806         rm -f $DIR/$tfile
5807 }
5808 run_test 77b "checksum error on client write, read"
5809
5810 test_77d() { # bug 10889
5811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5812         $GSS && skip "could not run with gss" && return
5813         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5814         $LCTL set_param fail_loc=0x80000409
5815         set_checksums 1
5816         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5817                 error "direct write: rc=$?"
5818         $LCTL set_param fail_loc=0
5819         set_checksums 0
5820
5821         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5822         $LCTL set_param fail_loc=0x80000408
5823         set_checksums 1
5824         cancel_lru_locks osc
5825         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5826                 error "direct read: rc=$?"
5827         $LCTL set_param fail_loc=0
5828         set_checksums 0
5829 }
5830 run_test 77d "checksum error on OST direct write, read"
5831
5832 test_77f() { # bug 10889
5833         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5834         $GSS && skip "could not run with gss" && return
5835         set_checksums 1
5836         for algo in $CKSUM_TYPES; do
5837                 cancel_lru_locks osc
5838                 set_checksum_type $algo
5839                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5840                 $LCTL set_param fail_loc=0x409
5841                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5842                         error "direct write succeeded"
5843                 $LCTL set_param fail_loc=0
5844         done
5845         set_checksum_type $ORIG_CSUM_TYPE
5846         set_checksums 0
5847 }
5848 run_test 77f "repeat checksum error on write (expect error)"
5849
5850 test_77g() { # bug 10889
5851         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5852         $GSS && skip "could not run with gss" && return
5853         remote_ost_nodsh && skip "remote OST with nodsh" && return
5854
5855         [ ! -f $F77_TMP ] && setup_f77
5856
5857         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5858         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5859         do_facet ost1 lctl set_param fail_loc=0x8000021a
5860         set_checksums 1
5861         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5862                 error "write error: rc=$?"
5863         do_facet ost1 lctl set_param fail_loc=0
5864         set_checksums 0
5865
5866         cancel_lru_locks osc
5867         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5868         do_facet ost1 lctl set_param fail_loc=0x8000021b
5869         set_checksums 1
5870         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5871         do_facet ost1 lctl set_param fail_loc=0
5872         set_checksums 0
5873 }
5874 run_test 77g "checksum error on OST write, read"
5875
5876 test_77i() { # bug 13805
5877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5878         $GSS && skip "could not run with gss" && return
5879         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5880         lctl set_param fail_loc=0x40b
5881         remount_client $MOUNT
5882         lctl set_param fail_loc=0
5883         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5884                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5885                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5886                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5887         done
5888         remount_client $MOUNT
5889 }
5890 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5891
5892 test_77j() { # bug 13805
5893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5894         $GSS && skip "could not run with gss" && return
5895         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5896         lctl set_param fail_loc=0x40c
5897         remount_client $MOUNT
5898         lctl set_param fail_loc=0
5899         sleep 2 # wait async osc connect to finish
5900         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5901                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5902                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5903                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5904         done
5905         remount_client $MOUNT
5906 }
5907 run_test 77j "client only supporting ADLER32"
5908
5909 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5910 rm -f $F77_TMP
5911 unset F77_TMP
5912
5913 test_78() { # bug 10901
5914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5915         remote_ost || { skip_env "local OST" && return; }
5916
5917         NSEQ=5
5918         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5919         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5920         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5921         echo "MemTotal: $MEMTOTAL"
5922
5923         # reserve 256MB of memory for the kernel and other running processes,
5924         # and then take 1/2 of the remaining memory for the read/write buffers.
5925         if [ $MEMTOTAL -gt 512 ] ;then
5926                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5927         else
5928                 # for those poor memory-starved high-end clusters...
5929                 MEMTOTAL=$((MEMTOTAL / 2))
5930         fi
5931         echo "Mem to use for directio: $MEMTOTAL"
5932
5933         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5934         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5935         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5936         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5937                 head -n1)
5938         echo "Smallest OST: $SMALLESTOST"
5939         [[ $SMALLESTOST -lt 10240 ]] &&
5940                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5941
5942         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5943                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5944
5945         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5946         echo "File size: $F78SIZE"
5947         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5948         for i in $(seq 1 $NSEQ); do
5949                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5950                 echo directIO rdwr round $i of $NSEQ
5951                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5952         done
5953
5954         rm -f $DIR/$tfile
5955 }
5956 run_test 78 "handle large O_DIRECT writes correctly ============"
5957
5958 test_79() { # bug 12743
5959         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5960         wait_delete_completed
5961
5962         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5963         BKFREE=$(calc_osc_kbytes kbytesfree)
5964         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5965
5966         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5967         DFTOTAL=`echo $STRING | cut -d, -f1`
5968         DFUSED=`echo $STRING  | cut -d, -f2`
5969         DFAVAIL=`echo $STRING | cut -d, -f3`
5970         DFFREE=$(($DFTOTAL - $DFUSED))
5971
5972         ALLOWANCE=$((64 * $OSTCOUNT))
5973
5974         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5975            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5976                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5977         fi
5978         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5979            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5980                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5981         fi
5982         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5983            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5984                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5985         fi
5986 }
5987 run_test 79 "df report consistency check ======================="
5988
5989 test_80() { # bug 10718
5990         remote_ost_nodsh && skip "remote OST with nodsh" && return
5991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5992         # relax strong synchronous semantics for slow backends like ZFS
5993         local soc="obdfilter.*.sync_on_lock_cancel"
5994         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5995         local hosts=
5996         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5997                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5998                           facet_active_host $host; done | sort -u)
5999                 do_nodes $hosts lctl set_param $soc=never
6000         fi
6001
6002         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6003         sync; sleep 1; sync
6004         local BEFORE=`date +%s`
6005         cancel_lru_locks osc
6006         local AFTER=`date +%s`
6007         local DIFF=$((AFTER-BEFORE))
6008         if [ $DIFF -gt 1 ] ; then
6009                 error "elapsed for 1M@1T = $DIFF"
6010         fi
6011
6012         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6013
6014         rm -f $DIR/$tfile
6015 }
6016 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6017
6018 test_81a() { # LU-456
6019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6020         remote_ost_nodsh && skip "remote OST with nodsh" && return
6021         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6022         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6023         do_facet ost1 lctl set_param fail_loc=0x80000228
6024
6025         # write should trigger a retry and success
6026         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6027         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6028         RC=$?
6029         if [ $RC -ne 0 ] ; then
6030                 error "write should success, but failed for $RC"
6031         fi
6032 }
6033 run_test 81a "OST should retry write when get -ENOSPC ==============="
6034
6035 test_81b() { # LU-456
6036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6037         remote_ost_nodsh && skip "remote OST with nodsh" && return
6038         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6039         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6040         do_facet ost1 lctl set_param fail_loc=0x228
6041
6042         # write should retry several times and return -ENOSPC finally
6043         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6044         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6045         RC=$?
6046         ENOSPC=28
6047         if [ $RC -ne $ENOSPC ] ; then
6048                 error "dd should fail for -ENOSPC, but succeed."
6049         fi
6050 }
6051 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6052
6053 test_82() { # LU-1031
6054         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6055         local gid1=14091995
6056         local gid2=16022000
6057
6058         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6059         local MULTIPID1=$!
6060         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6061         local MULTIPID2=$!
6062         kill -USR1 $MULTIPID2
6063         sleep 2
6064         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6065                 error "First grouplock does not block second one"
6066         else
6067                 echo "Second grouplock blocks first one"
6068         fi
6069         kill -USR1 $MULTIPID1
6070         wait $MULTIPID1
6071         wait $MULTIPID2
6072 }
6073 run_test 82 "Basic grouplock test ==============================="
6074
6075 test_99a() {
6076         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6077                 return
6078         test_mkdir -p $DIR/d99cvsroot
6079         chown $RUNAS_ID $DIR/d99cvsroot
6080         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6081         cd $TMP
6082
6083         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6084         cd $oldPWD
6085 }
6086 run_test 99a "cvs init ========================================="
6087
6088 test_99b() {
6089         [ -z "$(which cvs 2>/dev/null)" ] &&
6090                 skip_env "could not find cvs" && return
6091         [ ! -d $DIR/d99cvsroot ] && test_99a
6092         cd /etc/init.d
6093         # some versions of cvs import exit(1) when asked to import links or
6094         # files they can't read.  ignore those files.
6095         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6096                         ! -perm +4 -printf '-I %f\n')
6097         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6098                 d99reposname vtag rtag
6099 }
6100 run_test 99b "cvs import ======================================="
6101
6102 test_99c() {
6103         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6104         [ ! -d $DIR/d99cvsroot ] && test_99b
6105         cd $DIR
6106         test_mkdir -p $DIR/d99reposname
6107         chown $RUNAS_ID $DIR/d99reposname
6108         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6109 }
6110 run_test 99c "cvs checkout ====================================="
6111
6112 test_99d() {
6113         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6114         [ ! -d $DIR/d99cvsroot ] && test_99c
6115         cd $DIR/d99reposname
6116         $RUNAS touch foo99
6117         $RUNAS cvs add -m 'addmsg' foo99
6118 }
6119 run_test 99d "cvs add =========================================="
6120
6121 test_99e() {
6122         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6123         [ ! -d $DIR/d99cvsroot ] && test_99c
6124         cd $DIR/d99reposname
6125         $RUNAS cvs update
6126 }
6127 run_test 99e "cvs update ======================================="
6128
6129 test_99f() {
6130         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6131         [ ! -d $DIR/d99cvsroot ] && test_99d
6132         cd $DIR/d99reposname
6133         $RUNAS cvs commit -m 'nomsg' foo99
6134     rm -fr $DIR/d99cvsroot
6135 }
6136 run_test 99f "cvs commit ======================================="
6137
6138 test_100() {
6139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6140         [ "$NETTYPE" = tcp ] || \
6141                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6142                         return ; }
6143
6144         remote_ost_nodsh && skip "remote OST with nodsh" && return
6145         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6146         remote_servers || \
6147                 { skip "useless for local single node setup" && return; }
6148
6149         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6150                 [ "$PROT" != "tcp" ] && continue
6151                 RPORT=$(echo $REMOTE | cut -d: -f2)
6152                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6153
6154                 rc=0
6155                 LPORT=`echo $LOCAL | cut -d: -f2`
6156                 if [ $LPORT -ge 1024 ]; then
6157                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6158                         netstat -tna
6159                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6160                 fi
6161         done
6162         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6163 }
6164 run_test 100 "check local port using privileged port ==========="
6165
6166 function get_named_value()
6167 {
6168     local tag
6169
6170     tag=$1
6171     while read ;do
6172         line=$REPLY
6173         case $line in
6174         $tag*)
6175             echo $line | sed "s/^$tag[ ]*//"
6176             break
6177             ;;
6178         esac
6179     done
6180 }
6181
6182 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6183                    awk '/^max_cached_mb/ { print $2 }')
6184
6185 cleanup_101a() {
6186         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6187         trap 0
6188 }
6189
6190 test_101a() {
6191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6192         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6193         local s
6194         local discard
6195         local nreads=10000
6196         local cache_limit=32
6197
6198         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6199         trap cleanup_101a EXIT
6200         $LCTL set_param -n llite.*.read_ahead_stats 0
6201         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6202
6203         #
6204         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6205         #
6206         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6207         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6208
6209         discard=0
6210         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6211                 get_named_value 'read but discarded' | cut -d" " -f1); do
6212                         discard=$(($discard + $s))
6213         done
6214         cleanup_101a
6215
6216         if [[ $(($discard * 10)) -gt $nreads ]]; then
6217                 $LCTL get_param osc.*-osc*.rpc_stats
6218                 $LCTL get_param llite.*.read_ahead_stats
6219                 error "too many ($discard) discarded pages"
6220         fi
6221         rm -f $DIR/$tfile || true
6222 }
6223 run_test 101a "check read-ahead for random reads ================"
6224
6225 setup_test101bc() {
6226         test_mkdir -p $DIR/$tdir
6227         local STRIPE_SIZE=$1
6228         local FILE_LENGTH=$2
6229         STRIPE_OFFSET=0
6230
6231         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6232
6233         local list=$(comma_list $(osts_nodes))
6234         set_osd_param $list '' read_cache_enable 0
6235         set_osd_param $list '' writethrough_cache_enable 0
6236
6237         trap cleanup_test101bc EXIT
6238         # prepare the read-ahead file
6239         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6240
6241         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6242                                 count=$FILE_SIZE_MB 2> /dev/null
6243
6244 }
6245
6246 cleanup_test101bc() {
6247         trap 0
6248         rm -rf $DIR/$tdir
6249         rm -f $DIR/$tfile
6250
6251         local list=$(comma_list $(osts_nodes))
6252         set_osd_param $list '' read_cache_enable 1
6253         set_osd_param $list '' writethrough_cache_enable 1
6254 }
6255
6256 calc_total() {
6257         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6258 }
6259
6260 ra_check_101() {
6261         local READ_SIZE=$1
6262         local STRIPE_SIZE=$2
6263         local FILE_LENGTH=$3
6264         local RA_INC=1048576
6265         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6266         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6267                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6268         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6269                         get_named_value 'read but discarded' |
6270                         cut -d" " -f1 | calc_total)
6271         if [[ $DISCARD -gt $discard_limit ]]; then
6272                 $LCTL get_param llite.*.read_ahead_stats
6273                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6274         else
6275                 echo "Read-ahead success for size ${READ_SIZE}"
6276         fi
6277 }
6278
6279 test_101b() {
6280         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6281         [[ $OSTCOUNT -lt 2 ]] &&
6282                 skip_env "skipping stride IO stride-ahead test" && return
6283         local STRIPE_SIZE=1048576
6284         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6285         if [ $SLOW == "yes" ]; then
6286                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6287         else
6288                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6289         fi
6290
6291         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6292
6293         # prepare the read-ahead file
6294         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6295         cancel_lru_locks osc
6296         for BIDX in 2 4 8 16 32 64 128 256
6297         do
6298                 local BSIZE=$((BIDX*4096))
6299                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6300                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6301                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6302                 $LCTL set_param -n llite.*.read_ahead_stats 0
6303                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6304                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6305                 cancel_lru_locks osc
6306                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6307         done
6308         cleanup_test101bc
6309         true
6310 }
6311 run_test 101b "check stride-io mode read-ahead ================="
6312
6313 test_101c() {
6314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6315         local STRIPE_SIZE=1048576
6316         local FILE_LENGTH=$((STRIPE_SIZE*100))
6317         local nreads=10000
6318         local osc_rpc_stats
6319
6320         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6321
6322         cancel_lru_locks osc
6323         $LCTL set_param osc.*.rpc_stats 0
6324         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6325         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6326                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6327                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6328                 local size
6329
6330                 if [ $lines -le 20 ]; then
6331                         continue
6332                 fi
6333                 for size in 1 2 4 8; do
6334                         local rpc=$(echo "$stats" |
6335                                     awk '($1 == "'$size':") {print $2; exit; }')
6336                         [ $rpc != 0 ] &&
6337                                 error "Small $((size*4))k read IO $rpc !"
6338                 done
6339                 echo "$osc_rpc_stats check passed!"
6340         done
6341         cleanup_test101bc
6342         true
6343 }
6344 run_test 101c "check stripe_size aligned read-ahead ================="
6345
6346 set_read_ahead() {
6347         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6348         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6349 }
6350
6351 test_101d() {
6352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6353         local file=$DIR/$tfile
6354         local sz_MB=${FILESIZE_101d:-500}
6355         local ra_MB=${READAHEAD_MB:-40}
6356
6357         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6358         [ $free_MB -lt $sz_MB ] &&
6359                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6360
6361         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6362         $SETSTRIPE -c -1 $file || error "setstripe failed"
6363
6364         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6365         echo Cancel LRU locks on lustre client to flush the client cache
6366         cancel_lru_locks osc
6367
6368         echo Disable read-ahead
6369         local old_READAHEAD=$(set_read_ahead 0)
6370
6371         echo Reading the test file $file with read-ahead disabled
6372         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6373
6374         echo Cancel LRU locks on lustre client to flush the client cache
6375         cancel_lru_locks osc
6376         echo Enable read-ahead with ${ra_MB}MB
6377         set_read_ahead $ra_MB
6378
6379         echo Reading the test file $file with read-ahead enabled
6380         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6381
6382         echo "read-ahead disabled time read $raOFF"
6383         echo "read-ahead enabled  time read $raON"
6384
6385         set_read_ahead $old_READAHEAD
6386         rm -f $file
6387         wait_delete_completed
6388
6389         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6390                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6391 }
6392 run_test 101d "file read with and without read-ahead enabled"
6393
6394 test_101e() {
6395         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6396         local file=$DIR/$tfile
6397         local size_KB=500  #KB
6398         local count=100
6399         local bsize=1024
6400
6401         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6402         local need_KB=$((count * size_KB))
6403         [[ $free_KB -le $need_KB ]] &&
6404                 skip_env "Need free space $need_KB, have $free_KB" && return
6405
6406         echo "Creating $count ${size_KB}K test files"
6407         for ((i = 0; i < $count; i++)); do
6408                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6409         done
6410
6411         echo "Cancel LRU locks on lustre client to flush the client cache"
6412         cancel_lru_locks osc
6413
6414         echo "Reset readahead stats"
6415         $LCTL set_param -n llite.*.read_ahead_stats 0
6416
6417         for ((i = 0; i < $count; i++)); do
6418                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6419         done
6420
6421         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6422                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6423
6424         for ((i = 0; i < $count; i++)); do
6425                 rm -rf $file.$i 2>/dev/null
6426         done
6427
6428         #10000 means 20% reads are missing in readahead
6429         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6430 }
6431 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6432
6433 cleanup_test101f() {
6434     trap 0
6435     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6436     rm -rf $DIR/$tfile 2>/dev/null
6437 }
6438
6439 test_101f() {
6440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6441     local file=$DIR/$tfile
6442     local nreads=1000
6443
6444     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6445     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6446     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6447     trap cleanup_test101f EXIT
6448
6449     echo Cancel LRU locks on lustre client to flush the client cache
6450     cancel_lru_locks osc
6451
6452     echo Reset readahead stats
6453     $LCTL set_param -n llite.*.read_ahead_stats 0
6454     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6455     # readahead should read in 2M file on second read, so only miss
6456     # 2 pages.
6457     echo Random 4K reads on 2M file for 1000 times
6458     $READS -f $file -s 2097152 -b 4096 -n $nreads
6459
6460     echo checking missing pages
6461     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6462           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6463
6464     [ $miss -lt 3 ] || error "misses too much pages!"
6465     cleanup_test101f
6466 }
6467 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6468
6469 setup_test102() {
6470         test_mkdir -p $DIR/$tdir
6471         chown $RUNAS_ID $DIR/$tdir
6472         STRIPE_SIZE=65536
6473         STRIPE_OFFSET=1
6474         STRIPE_COUNT=$OSTCOUNT
6475         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6476
6477         trap cleanup_test102 EXIT
6478         cd $DIR
6479         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6480         cd $DIR/$tdir
6481         for num in 1 2 3 4; do
6482                 for count in $(seq 1 $STRIPE_COUNT); do
6483                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6484                                 local size=`expr $STRIPE_SIZE \* $num`
6485                                 local file=file"$num-$idx-$count"
6486                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6487                         done
6488                 done
6489         done
6490
6491         cd $DIR
6492         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6493 }
6494
6495 cleanup_test102() {
6496         trap 0
6497         rm -f $TMP/f102.tar
6498         rm -rf $DIR/d0.sanity/d102
6499 }
6500
6501 test_102a() {
6502         local testfile=$DIR/$tfile
6503
6504         touch $testfile
6505
6506         [ "$UID" != 0 ] && skip_env "must run as root" && return
6507         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6508                 skip_env "must have user_xattr" && return
6509
6510         [ -z "$(which setfattr 2>/dev/null)" ] &&
6511                 skip_env "could not find setfattr" && return
6512
6513         echo "set/get xattr..."
6514         setfattr -n trusted.name1 -v value1 $testfile ||
6515                 error "setfattr -n trusted.name1=value1 $testfile failed"
6516         getfattr -n trusted.name1 $testfile 2> /dev/null |
6517           grep "trusted.name1=.value1" ||
6518                 error "$testfile missing trusted.name1=value1"
6519
6520         setfattr -n user.author1 -v author1 $testfile ||
6521                 error "setfattr -n user.author1=author1 $testfile failed"
6522         getfattr -n user.author1 $testfile 2> /dev/null |
6523           grep "user.author1=.author1" ||
6524                 error "$testfile missing trusted.author1=author1"
6525
6526         echo "listxattr..."
6527         setfattr -n trusted.name2 -v value2 $testfile ||
6528                 error "$testfile unable to set trusted.name2"
6529         setfattr -n trusted.name3 -v value3 $testfile ||
6530                 error "$testfile unable to set trusted.name3"
6531         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6532             grep "trusted.name" | wc -l) -eq 3 ] ||
6533                 error "$testfile missing 3 trusted.name xattrs"
6534
6535         setfattr -n user.author2 -v author2 $testfile ||
6536                 error "$testfile unable to set user.author2"
6537         setfattr -n user.author3 -v author3 $testfile ||
6538                 error "$testfile unable to set user.author3"
6539         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6540             grep "user.author" | wc -l) -eq 3 ] ||
6541                 error "$testfile missing 3 user.author xattrs"
6542
6543         echo "remove xattr..."
6544         setfattr -x trusted.name1 $testfile ||
6545                 error "$testfile error deleting trusted.name1"
6546         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6547                 error "$testfile did not delete trusted.name1 xattr"
6548
6549         setfattr -x user.author1 $testfile ||
6550                 error "$testfile error deleting user.author1"
6551         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6552                 error "$testfile did not delete trusted.name1 xattr"
6553
6554         # b10667: setting lustre special xattr be silently discarded
6555         echo "set lustre special xattr ..."
6556         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6557                 error "$testfile allowed setting trusted.lov"
6558 }
6559 run_test 102a "user xattr test =================================="
6560
6561 test_102b() {
6562         [ -z "$(which setfattr 2>/dev/null)" ] &&
6563                 skip_env "could not find setfattr" && return
6564
6565         # b10930: get/set/list trusted.lov xattr
6566         echo "get/set/list trusted.lov xattr ..."
6567         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6568         local testfile=$DIR/$tfile
6569         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6570                 error "setstripe failed"
6571         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6572                 error "getstripe failed"
6573         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6574                 error "can't get trusted.lov from $testfile"
6575
6576         local testfile2=${testfile}2
6577         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6578                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6579
6580         $MCREATE $testfile2
6581         setfattr -n trusted.lov -v $value $testfile2
6582         local stripe_size=$($GETSTRIPE -S $testfile2)
6583         local stripe_count=$($GETSTRIPE -c $testfile2)
6584         [[ $stripe_size -eq 65536 ]] ||
6585                 error "stripe size $stripe_size != 65536"
6586         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6587                 error "stripe count $stripe_count != $STRIPECOUNT"
6588         rm -f $DIR/$tfile
6589 }
6590 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6591
6592 test_102c() {
6593         [ -z "$(which setfattr 2>/dev/null)" ] &&
6594                 skip_env "could not find setfattr" && return
6595
6596         # b10930: get/set/list lustre.lov xattr
6597         echo "get/set/list lustre.lov xattr ..."
6598         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6599         test_mkdir -p $DIR/$tdir
6600         chown $RUNAS_ID $DIR/$tdir
6601         local testfile=$DIR/$tdir/$tfile
6602         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6603                 error "setstripe failed"
6604         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6605                 error "getstripe failed"
6606         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6607         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6608
6609         local testfile2=${testfile}2
6610         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6611                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6612
6613         $RUNAS $MCREATE $testfile2
6614         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6615         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6616         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6617         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6618         [ $stripe_count -eq $STRIPECOUNT ] ||
6619                 error "stripe count $stripe_count != $STRIPECOUNT"
6620 }
6621 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6622
6623 compare_stripe_info1() {
6624         local stripe_index_all_zero=true
6625
6626         for num in 1 2 3 4; do
6627                 for count in $(seq 1 $STRIPE_COUNT); do
6628                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6629                                 local size=$((STRIPE_SIZE * num))
6630                                 local file=file"$num-$offset-$count"
6631                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6632                                 [[ $stripe_size -ne $size ]] &&
6633                                     error "$file: size $stripe_size != $size"
6634                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6635                                 # allow fewer stripes to be created, ORI-601
6636                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6637                                     error "$file: count $stripe_count != $count"
6638                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6639                                 [[ $stripe_index -ne 0 ]] &&
6640                                         stripe_index_all_zero=false
6641                         done
6642                 done
6643         done
6644         $stripe_index_all_zero &&
6645                 error "all files are being extracted starting from OST index 0"
6646         return 0
6647 }
6648
6649 find_lustre_tar() {
6650         [ -n "$(which tar 2>/dev/null)" ] &&
6651                 strings $(which tar) | grep -q "lustre" && echo tar
6652 }
6653
6654 test_102d() {
6655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6656         # b10930: tar test for trusted.lov xattr
6657         TAR=$(find_lustre_tar)
6658         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6659         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6660         setup_test102
6661         test_mkdir -p $DIR/d102d
6662         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6663         cd $DIR/d102d/$tdir
6664         compare_stripe_info1
6665 }
6666 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6667
6668 test_102f() {
6669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6670         # b10930: tar test for trusted.lov xattr
6671         TAR=$(find_lustre_tar)
6672         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6673         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6674         setup_test102
6675         test_mkdir -p $DIR/d102f
6676         cd $DIR
6677         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6678         cd $DIR/d102f/$tdir
6679         compare_stripe_info1
6680 }
6681 run_test 102f "tar copy files, not keep osts ==========="
6682
6683 grow_xattr() {
6684         local xsize=${1:-1024}  # in bytes
6685         local file=$DIR/$tfile
6686
6687         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6688                 skip "must have user_xattr" && return 0
6689         [ -z "$(which setfattr 2>/dev/null)" ] &&
6690                 skip_env "could not find setfattr" && return 0
6691         [ -z "$(which getfattr 2>/dev/null)" ] &&
6692                 skip_env "could not find getfattr" && return 0
6693
6694         touch $file
6695
6696         local value="$(generate_string $xsize)"
6697
6698         local xbig=trusted.big
6699         log "save $xbig on $file"
6700         setfattr -n $xbig -v $value $file ||
6701                 error "saving $xbig on $file failed"
6702
6703         local orig=$(get_xattr_value $xbig $file)
6704         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6705
6706         local xsml=trusted.sml
6707         log "save $xsml on $file"
6708         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6709
6710         local new=$(get_xattr_value $xbig $file)
6711         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6712
6713         log "grow $xsml on $file"
6714         setfattr -n $xsml -v "$value" $file ||
6715                 error "growing $xsml on $file failed"
6716
6717         new=$(get_xattr_value $xbig $file)
6718         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6719         log "$xbig still valid after growing $xsml"
6720
6721         rm -f $file
6722 }
6723
6724 test_102h() { # bug 15777
6725         grow_xattr 1024
6726 }
6727 run_test 102h "grow xattr from inside inode to external block"
6728
6729 test_102ha() {
6730         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6731         grow_xattr $(max_xattr_size)
6732 }
6733 run_test 102ha "grow xattr from inside inode to external inode"
6734
6735 test_102i() { # bug 17038
6736         [ -z "$(which getfattr 2>/dev/null)" ] &&
6737                 skip "could not find getfattr" && return
6738         touch $DIR/$tfile
6739         ln -s $DIR/$tfile $DIR/${tfile}link
6740         getfattr -n trusted.lov $DIR/$tfile ||
6741                 error "lgetxattr on $DIR/$tfile failed"
6742         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6743                 grep -i "no such attr" ||
6744                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6745         rm -f $DIR/$tfile $DIR/${tfile}link
6746 }
6747 run_test 102i "lgetxattr test on symbolic link ============"
6748
6749 test_102j() {
6750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6751         TAR=$(find_lustre_tar)
6752         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6753         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6754         setup_test102 "$RUNAS"
6755         test_mkdir -p $DIR/d102j
6756         chown $RUNAS_ID $DIR/d102j
6757         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6758         cd $DIR/d102j/$tdir
6759         compare_stripe_info1 "$RUNAS"
6760 }
6761 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6762
6763 test_102k() {
6764         [ -z "$(which setfattr 2>/dev/null)" ] &&
6765                 skip "could not find setfattr" && return
6766         touch $DIR/$tfile
6767         # b22187 just check that does not crash for regular file.
6768         setfattr -n trusted.lov $DIR/$tfile
6769         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6770         local test_kdir=$DIR/d102k
6771         test_mkdir $test_kdir
6772         local default_size=`$GETSTRIPE -S $test_kdir`
6773         local default_count=`$GETSTRIPE -c $test_kdir`
6774         local default_offset=`$GETSTRIPE -i $test_kdir`
6775         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6776                 error 'dir setstripe failed'
6777         setfattr -n trusted.lov $test_kdir
6778         local stripe_size=`$GETSTRIPE -S $test_kdir`
6779         local stripe_count=`$GETSTRIPE -c $test_kdir`
6780         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6781         [ $stripe_size -eq $default_size ] ||
6782                 error "stripe size $stripe_size != $default_size"
6783         [ $stripe_count -eq $default_count ] ||
6784                 error "stripe count $stripe_count != $default_count"
6785         [ $stripe_offset -eq $default_offset ] ||
6786                 error "stripe offset $stripe_offset != $default_offset"
6787         rm -rf $DIR/$tfile $test_kdir
6788 }
6789 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6790
6791 test_102l() {
6792         [ -z "$(which getfattr 2>/dev/null)" ] &&
6793                 skip "could not find getfattr" && return
6794
6795         # LU-532 trusted. xattr is invisible to non-root
6796         local testfile=$DIR/$tfile
6797
6798         touch $testfile
6799
6800         echo "listxattr as user..."
6801         chown $RUNAS_ID $testfile
6802         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6803             grep -q "trusted" &&
6804                 error "$testfile trusted xattrs are user visible"
6805
6806         return 0;
6807 }
6808 run_test 102l "listxattr size test =================================="
6809
6810 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6811         local path=$DIR/$tfile
6812         touch $path
6813
6814         listxattr_size_check $path || error "listattr_size_check $path failed"
6815 }
6816 run_test 102m "Ensure listxattr fails on small bufffer ========"
6817
6818 cleanup_test102
6819
6820 getxattr() { # getxattr path name
6821         # Return the base64 encoding of the value of xattr name on path.
6822         local path=$1
6823         local name=$2
6824
6825         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6826         # file: $path
6827         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6828         #
6829         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6830
6831         getfattr --absolute-names --encoding=base64 --name=$name $path |
6832                 awk -F= -v name=$name '$1 == name {
6833                         print substr($0, index($0, "=") + 1);
6834         }'
6835 }
6836
6837 test_102n() { # LU-4101 mdt: protect internal xattrs
6838         local file0=$DIR/$tfile.0
6839         local file1=$DIR/$tfile.1
6840         local xattr0=$TMP/$tfile.0
6841         local xattr1=$TMP/$tfile.1
6842         local name
6843         local value
6844
6845         [ -z "$(which setfattr 2>/dev/null)" ] &&
6846                 skip "could not find setfattr" && return
6847
6848         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6849         then
6850                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6851                 return
6852         fi
6853
6854         rm -rf $file0 $file1 $xattr0 $xattr1
6855         touch $file0 $file1
6856
6857         # Get 'before' xattrs of $file1.
6858         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6859
6860         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6861                 # Try to copy xattr from $file0 to $file1.
6862                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6863
6864                 setfattr --name=trusted.$name --value="$value" $file1 ||
6865                         error "setxattr 'trusted.$name' failed"
6866
6867                 # Try to set a garbage xattr.
6868                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6869
6870                 setfattr --name=trusted.$name --value="$value" $file1 ||
6871                         error "setxattr 'trusted.$name' failed"
6872
6873                 # Try to remove the xattr from $file1. We don't care if this
6874                 # appears to succeed or fail, we just don't want there to be
6875                 # any changes or crashes.
6876                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6877         done
6878
6879         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6880         then
6881                 name="lfsck_ns"
6882                 # Try to copy xattr from $file0 to $file1.
6883                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6884
6885                 setfattr --name=trusted.$name --value="$value" $file1 ||
6886                         error "setxattr 'trusted.$name' failed"
6887
6888                 # Try to set a garbage xattr.
6889                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6890
6891                 setfattr --name=trusted.$name --value="$value" $file1 ||
6892                         error "setxattr 'trusted.$name' failed"
6893
6894                 # Try to remove the xattr from $file1. We don't care if this
6895                 # appears to succeed or fail, we just don't want there to be
6896                 # any changes or crashes.
6897                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6898         fi
6899
6900         # Get 'after' xattrs of file1.
6901         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6902
6903         if ! diff $xattr0 $xattr1; then
6904                 error "before and after xattrs of '$file1' differ"
6905         fi
6906
6907         rm -rf $file0 $file1 $xattr0 $xattr1
6908
6909         return 0
6910 }
6911 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6912
6913 test_102p() { # LU-4703 setxattr did not check ownership
6914         local testfile=$DIR/$tfile
6915
6916         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6917                 skip "MDS needs to be at least 2.5.56" && return
6918
6919         touch $testfile
6920
6921         echo "setfacl as user..."
6922         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6923         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6924
6925         echo "setfattr as user..."
6926         setfacl -m "u:$RUNAS_ID:---" $testfile
6927         $RUNAS setfattr -x system.posix_acl_access $testfile
6928         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6929 }
6930 run_test 102p "check setxattr(2) correctly fails without permission"
6931
6932 test_102q() {
6933         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
6934 }
6935 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
6936
6937 test_102r() {
6938         touch $DIR/$tfile || error "touch"
6939         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
6940         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
6941         rm $DIR/$tfile || error "rm"
6942 }
6943 run_test 102r "set EAs with empty values"
6944
6945 run_acl_subtest()
6946 {
6947     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6948     return $?
6949 }
6950
6951 test_103a() {
6952         [ "$UID" != 0 ] && skip_env "must run as root" && return
6953         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6954                 skip "must have acl enabled" && return
6955         [ -z "$(which setfacl 2>/dev/null)" ] &&
6956                 skip_env "could not find setfacl" && return
6957         $GSS && skip "could not run under gss" && return
6958
6959         gpasswd -a daemon bin                           # LU-5641
6960         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
6961
6962         declare -a identity_old
6963
6964         for num in $(seq $MDSCOUNT); do
6965                 switch_identity $num true || identity_old[$num]=$?
6966         done
6967
6968         SAVE_UMASK=$(umask)
6969         umask 0022
6970         cd $DIR
6971
6972         echo "performing cp ..."
6973         run_acl_subtest cp || error "run_acl_subtest cp failed"
6974         echo "performing getfacl-noacl..."
6975         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6976         echo "performing misc..."
6977         run_acl_subtest misc || error  "misc test failed"
6978         echo "performing permissions..."
6979         run_acl_subtest permissions || error "permissions failed"
6980         echo "performing setfacl..."
6981         run_acl_subtest setfacl || error  "setfacl test failed"
6982
6983         # inheritance test got from HP
6984         echo "performing inheritance..."
6985         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6986         chmod +x make-tree || error "chmod +x failed"
6987         run_acl_subtest inheritance || error "inheritance test failed"
6988         rm -f make-tree
6989
6990         echo "LU-974 ignore umask when acl is enabled..."
6991         run_acl_subtest 974 || error "LU-974 umask test failed"
6992         if [ $MDSCOUNT -ge 2 ]; then
6993                 run_acl_subtest 974_remote ||
6994                         error "LU-974 umask test failed under remote dir"
6995         fi
6996
6997         echo "LU-2561 newly created file is same size as directory..."
6998         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6999                 run_acl_subtest 2561 || error "LU-2561 test failed"
7000         else
7001                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7002         fi
7003
7004         run_acl_subtest 4924 || error "LU-4924 test failed"
7005
7006         cd $SAVE_PWD
7007         umask $SAVE_UMASK
7008
7009         for num in $(seq $MDSCOUNT); do
7010                 if [ "${identity_old[$num]}" = 1 ]; then
7011                         switch_identity $num false || identity_old[$num]=$?
7012                 fi
7013         done
7014 }
7015 run_test 103a "acl test ========================================="
7016
7017 test_103b() {
7018         local noacl=false
7019         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7020         local mountopts=$MDS_MOUNT_OPTS
7021
7022         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7023                 noacl=true
7024         else
7025                 # stop the MDT
7026                 stop $SINGLEMDS || error "failed to stop MDT."
7027                 # remount the MDT
7028                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7029                         MDS_MOUNT_OPTS="-o noacl"
7030                 else
7031                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7032                 fi
7033                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7034                         error "failed to start MDT."
7035                 MDS_MOUNT_OPTS=$mountopts
7036         fi
7037
7038         touch $DIR/$tfile
7039         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7040
7041         if ! $noacl; then
7042                 # stop the MDT
7043                 stop $SINGLEMDS || error "failed to stop MDT."
7044                 # remount the MDT
7045                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7046                         error "failed to start MDT."
7047         fi
7048
7049         true
7050 }
7051 run_test 103b "MDS mount option 'noacl'"
7052
7053 test_103c() {
7054         mkdir -p $DIR/$tdir
7055         cp -rp $DIR/$tdir $DIR/$tdir.bak
7056
7057         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7058                 error "$DIR/$tdir shouldn't contain default ACL"
7059         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7060                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7061         true
7062 }
7063 run_test 103c "'cp -rp' won't set empty acl"
7064
7065 test_104a() {
7066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7067         touch $DIR/$tfile
7068         lfs df || error "lfs df failed"
7069         lfs df -ih || error "lfs df -ih failed"
7070         lfs df -h $DIR || error "lfs df -h $DIR failed"
7071         lfs df -i $DIR || error "lfs df -i $DIR failed"
7072         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7073         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7074
7075         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7076         lctl --device %$OSC deactivate
7077         lfs df || error "lfs df with deactivated OSC failed"
7078         lctl --device %$OSC activate
7079         # wait the osc back to normal
7080         wait_osc_import_state client ost FULL
7081
7082         lfs df || error "lfs df with reactivated OSC failed"
7083         rm -f $DIR/$tfile
7084 }
7085 run_test 104a "lfs df [-ih] [path] test ========================="
7086
7087 test_104b() {
7088         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7089         [ $RUNAS_ID -eq $UID ] &&
7090                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7091         chmod 666 /dev/obd
7092         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7093                         grep "Permission denied" | wc -l)))
7094         if [ $denied_cnt -ne 0 ]; then
7095                 error "lfs check servers test failed"
7096         fi
7097 }
7098 run_test 104b "$RUNAS lfs check servers test ===================="
7099
7100 test_105a() {
7101         # doesn't work on 2.4 kernels
7102         touch $DIR/$tfile
7103         if $(flock_is_enabled); then
7104                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7105         else
7106                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7107         fi
7108         rm -f $DIR/$tfile
7109 }
7110 run_test 105a "flock when mounted without -o flock test ========"
7111
7112 test_105b() {
7113         touch $DIR/$tfile
7114         if $(flock_is_enabled); then
7115                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7116         else
7117                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7118         fi
7119         rm -f $DIR/$tfile
7120 }
7121 run_test 105b "fcntl when mounted without -o flock test ========"
7122
7123 test_105c() {
7124         touch $DIR/$tfile
7125         if $(flock_is_enabled); then
7126                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7127         else
7128                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7129         fi
7130         rm -f $DIR/$tfile
7131 }
7132 run_test 105c "lockf when mounted without -o flock test ========"
7133
7134 test_105d() { # bug 15924
7135         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7136         test_mkdir -p $DIR/$tdir
7137         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7138         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7139         $LCTL set_param fail_loc=0x80000315
7140         flocks_test 2 $DIR/$tdir
7141 }
7142 run_test 105d "flock race (should not freeze) ========"
7143
7144 test_105e() { # bug 22660 && 22040
7145         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7146         touch $DIR/$tfile
7147         flocks_test 3 $DIR/$tfile
7148 }
7149 run_test 105e "Two conflicting flocks from same process ======="
7150
7151 test_106() { #bug 10921
7152         test_mkdir -p $DIR/$tdir
7153         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7154         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7155 }
7156 run_test 106 "attempt exec of dir followed by chown of that dir"
7157
7158 test_107() {
7159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7160         CDIR=`pwd`
7161         cd $DIR
7162
7163         local file=core
7164         rm -f $file
7165
7166         local save_pattern=$(sysctl -n kernel.core_pattern)
7167         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7168         sysctl -w kernel.core_pattern=$file
7169         sysctl -w kernel.core_uses_pid=0
7170
7171         ulimit -c unlimited
7172         sleep 60 &
7173         SLEEPPID=$!
7174
7175         sleep 1
7176
7177         kill -s 11 $SLEEPPID
7178         wait $SLEEPPID
7179         if [ -e $file ]; then
7180                 size=`stat -c%s $file`
7181                 [ $size -eq 0 ] && error "Fail to create core file $file"
7182         else
7183                 error "Fail to create core file $file"
7184         fi
7185         rm -f $file
7186         sysctl -w kernel.core_pattern=$save_pattern
7187         sysctl -w kernel.core_uses_pid=$save_uses_pid
7188         cd $CDIR
7189 }
7190 run_test 107 "Coredump on SIG"
7191
7192 test_110() {
7193         test_mkdir -p $DIR/$tdir
7194         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7195                 error "mkdir with 255 char failed"
7196         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7197                 error "mkdir with 256 char should fail, but did not"
7198         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7199                 error "create with 255 char failed"
7200         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7201                 error "create with 256 char should fail, but did not"
7202
7203         ls -l $DIR/$tdir
7204         rm -rf $DIR/$tdir
7205 }
7206 run_test 110 "filename length checking"
7207
7208 test_115() {
7209         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7210         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7211                 tail -1 | cut -c11-20)
7212         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7213         echo "Starting with $OSTIO_pre threads"
7214
7215         NUMTEST=20000
7216         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7217         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7218         echo "$NUMTEST creates/unlinks"
7219         test_mkdir -p $DIR/$tdir
7220         createmany -o $DIR/$tdir/$tfile $NUMTEST
7221         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7222
7223         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7224                 tail -1 | cut -c11-20)
7225
7226         # don't return an error
7227         [ $OSTIO_post == $OSTIO_pre ] && echo \
7228             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7229             echo "This may be fine, depending on what ran before this test" &&
7230             echo "and how fast this system is." && return
7231
7232         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7233 }
7234 run_test 115 "verify dynamic thread creation===================="
7235
7236 free_min_max () {
7237         wait_delete_completed
7238         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7239         echo OST kbytes available: ${AVAIL[@]}
7240         MAXI=0; MAXV=${AVAIL[0]}
7241         MINI=0; MINV=${AVAIL[0]}
7242         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7243                 #echo OST $i: ${AVAIL[i]}kb
7244                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7245                         MAXV=${AVAIL[i]}; MAXI=$i
7246                 fi
7247                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7248                         MINV=${AVAIL[i]}; MINI=$i
7249                 fi
7250         done
7251         echo Min free space: OST $MINI: $MINV
7252         echo Max free space: OST $MAXI: $MAXV
7253 }
7254
7255 test_116a() { # was previously test_116()
7256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7257         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7258
7259         echo -n "Free space priority "
7260         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7261                 head -n1
7262         declare -a AVAIL
7263         free_min_max
7264
7265         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7266         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7267                 && return
7268         trap simple_cleanup_common EXIT
7269
7270
7271         # Check if we need to generate uneven OSTs
7272         test_mkdir -p $DIR/$tdir/OST${MINI}
7273         local FILL=$(($MINV / 4))
7274         local DIFF=$(($MAXV - $MINV))
7275         local DIFF2=$(($DIFF * 100 / $MINV))
7276
7277         local threshold=$(do_facet $SINGLEMDS \
7278                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7279         threshold=${threshold%%%}
7280         echo -n "Check for uneven OSTs: "
7281         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7282
7283         if [[ $DIFF2 -gt $threshold ]]; then
7284                 echo "ok"
7285                 echo "Don't need to fill OST$MINI"
7286         else
7287                 # generate uneven OSTs. Write 2% over the QOS threshold value
7288                 echo "no"
7289                 DIFF=$(($threshold - $DIFF2 + 2))
7290                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7291                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7292                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7293                         error "setstripe failed"
7294                 DIFF=$(($DIFF2 / 2048))
7295                 i=0
7296                 while [ $i -lt $DIFF ]; do
7297                         i=$(($i + 1))
7298                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7299                                 bs=2M count=1 2>/dev/null
7300                         echo -n .
7301                 done
7302                 echo .
7303                 sync
7304                 sleep_maxage
7305                 free_min_max
7306         fi
7307
7308         DIFF=$(($MAXV - $MINV))
7309         DIFF2=$(($DIFF * 100 / $MINV))
7310         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7311         if [[ $DIFF2 -gt $threshold ]]; then
7312                 echo "ok"
7313         else
7314                 echo "failed - QOS mode won't be used"
7315                 skip "QOS imbalance criteria not met"
7316                 simple_cleanup_common
7317                 return
7318         fi
7319
7320         MINI1=$MINI; MINV1=$MINV
7321         MAXI1=$MAXI; MAXV1=$MAXV
7322
7323         # now fill using QOS
7324         $SETSTRIPE -c 1 $DIR/$tdir
7325         FILL=$(($FILL / 200))
7326         if [ $FILL -gt 600 ]; then
7327                 FILL=600
7328         fi
7329         echo "writing $FILL files to QOS-assigned OSTs"
7330         i=0
7331         while [ $i -lt $FILL ]; do
7332                 i=$((i + 1))
7333                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7334                         count=1 2>/dev/null
7335                 echo -n .
7336         done
7337         echo "wrote $i 200k files"
7338         sync
7339         sleep_maxage
7340
7341         echo "Note: free space may not be updated, so measurements might be off"
7342         free_min_max
7343         DIFF2=$(($MAXV - $MINV))
7344         echo "free space delta: orig $DIFF final $DIFF2"
7345         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7346         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7347         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7348         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7349         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7350         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7351         [ $DIFF -gt 0 ] &&
7352                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7353
7354         # Figure out which files were written where
7355         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7356                   awk '/'$MINI1': / {print $2; exit}')
7357         echo $UUID
7358         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7359         echo "$MINC files created on smaller OST $MINI1"
7360         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7361                   awk '/'$MAXI1': / {print $2; exit}')
7362         echo $UUID
7363         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7364         echo "$MAXC files created on larger OST $MAXI1"
7365         FILL=$(($MAXC * 100 / $MINC - 100))
7366         [[ $MINC -gt 0 ]] &&
7367                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7368         [[ $MAXC -gt $MINC ]] ||
7369                 error_ignore LU-9 "stripe QOS didn't balance free space"
7370         simple_cleanup_common
7371 }
7372 run_test 116a "stripe QOS: free space balance ==================="
7373
7374 test_116b() { # LU-2093
7375         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7376 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7377         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7378                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7379         [ -z "$old_rr" ] && skip "no QOS" && return 0
7380         do_facet $SINGLEMDS lctl set_param \
7381                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7382         mkdir -p $DIR/$tdir
7383         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7384         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7385         do_facet $SINGLEMDS lctl set_param fail_loc=0
7386         rm -rf $DIR/$tdir
7387         do_facet $SINGLEMDS lctl set_param \
7388                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7389 }
7390 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7391
7392 test_117() # bug 10891
7393 {
7394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7395         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7396         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7397         lctl set_param fail_loc=0x21e
7398         > $DIR/$tfile || error "truncate failed"
7399         lctl set_param fail_loc=0
7400         echo "Truncate succeeded."
7401         rm -f $DIR/$tfile
7402 }
7403 run_test 117 "verify osd extend =========="
7404
7405 NO_SLOW_RESENDCOUNT=4
7406 export OLD_RESENDCOUNT=""
7407 set_resend_count () {
7408         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7409         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7410         lctl set_param -n $PROC_RESENDCOUNT $1
7411         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7412 }
7413
7414 # for reduce test_118* time (b=14842)
7415 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7416
7417 # Reset async IO behavior after error case
7418 reset_async() {
7419         FILE=$DIR/reset_async
7420
7421         # Ensure all OSCs are cleared
7422         $SETSTRIPE -c -1 $FILE
7423         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7424         sync
7425         rm $FILE
7426 }
7427
7428 test_118a() #bug 11710
7429 {
7430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7431         reset_async
7432
7433         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7434         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7435         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7436
7437         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7438                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7439                 return 1;
7440         fi
7441         rm -f $DIR/$tfile
7442 }
7443 run_test 118a "verify O_SYNC works =========="
7444
7445 test_118b()
7446 {
7447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7448         remote_ost_nodsh && skip "remote OST with nodsh" && return
7449
7450         reset_async
7451
7452         #define OBD_FAIL_OST_ENOENT 0x217
7453         set_nodes_failloc "$(osts_nodes)" 0x217
7454         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7455         RC=$?
7456         set_nodes_failloc "$(osts_nodes)" 0
7457         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7458         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7459                     grep -c writeback)
7460
7461         if [[ $RC -eq 0 ]]; then
7462                 error "Must return error due to dropped pages, rc=$RC"
7463                 return 1;
7464         fi
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
7471         echo "Dirty pages not leaked on ENOENT"
7472
7473         # Due to the above error the OSC will issue all RPCs syncronously
7474         # until a subsequent RPC completes successfully without error.
7475         $MULTIOP $DIR/$tfile Ow4096yc
7476         rm -f $DIR/$tfile
7477
7478         return 0
7479 }
7480 run_test 118b "Reclaim dirty pages on fatal error =========="
7481
7482 test_118c()
7483 {
7484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7485
7486         # for 118c, restore the original resend count, LU-1940
7487         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7488                                 set_resend_count $OLD_RESENDCOUNT
7489         remote_ost_nodsh && skip "remote OST with nodsh" && return
7490
7491         reset_async
7492
7493         #define OBD_FAIL_OST_EROFS               0x216
7494         set_nodes_failloc "$(osts_nodes)" 0x216
7495
7496         # multiop should block due to fsync until pages are written
7497         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7498         MULTIPID=$!
7499         sleep 1
7500
7501         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7502                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7503         fi
7504
7505         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7506                     grep -c writeback)
7507         if [[ $WRITEBACK -eq 0 ]]; then
7508                 error "No page in writeback, writeback=$WRITEBACK"
7509         fi
7510
7511         set_nodes_failloc "$(osts_nodes)" 0
7512         wait $MULTIPID
7513         RC=$?
7514         if [[ $RC -ne 0 ]]; then
7515                 error "Multiop fsync failed, rc=$RC"
7516         fi
7517
7518         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7519         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7520                     grep -c writeback)
7521         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7522                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7523         fi
7524
7525         rm -f $DIR/$tfile
7526         echo "Dirty pages flushed via fsync on EROFS"
7527         return 0
7528 }
7529 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7530
7531 # continue to use small resend count to reduce test_118* time (b=14842)
7532 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7533
7534 test_118d()
7535 {
7536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7537         remote_ost_nodsh && skip "remote OST with nodsh" && return
7538
7539         reset_async
7540
7541         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7542         set_nodes_failloc "$(osts_nodes)" 0x214
7543         # multiop should block due to fsync until pages are written
7544         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7545         MULTIPID=$!
7546         sleep 1
7547
7548         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7549                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7550         fi
7551
7552         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7553                     grep -c writeback)
7554         if [[ $WRITEBACK -eq 0 ]]; then
7555                 error "No page in writeback, writeback=$WRITEBACK"
7556         fi
7557
7558         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7559         set_nodes_failloc "$(osts_nodes)" 0
7560
7561         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7562         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7563                     grep -c writeback)
7564         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7565                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7566         fi
7567
7568         rm -f $DIR/$tfile
7569         echo "Dirty pages gaurenteed flushed via fsync"
7570         return 0
7571 }
7572 run_test 118d "Fsync validation inject a delay of the bulk =========="
7573
7574 test_118f() {
7575         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7576         reset_async
7577
7578         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7579         lctl set_param fail_loc=0x8000040a
7580
7581         # Should simulate EINVAL error which is fatal
7582         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7583         RC=$?
7584         if [[ $RC -eq 0 ]]; then
7585                 error "Must return error due to dropped pages, rc=$RC"
7586         fi
7587
7588         lctl set_param fail_loc=0x0
7589
7590         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7591         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7592         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7593                     grep -c writeback)
7594         if [[ $LOCKED -ne 0 ]]; then
7595                 error "Locked pages remain in cache, locked=$LOCKED"
7596         fi
7597
7598         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7599                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7600         fi
7601
7602         rm -f $DIR/$tfile
7603         echo "No pages locked after fsync"
7604
7605         reset_async
7606         return 0
7607 }
7608 run_test 118f "Simulate unrecoverable OSC side error =========="
7609
7610 test_118g() {
7611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7612         reset_async
7613
7614         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7615         lctl set_param fail_loc=0x406
7616
7617         # simulate local -ENOMEM
7618         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7619         RC=$?
7620
7621         lctl set_param fail_loc=0
7622         if [[ $RC -eq 0 ]]; then
7623                 error "Must return error due to dropped pages, rc=$RC"
7624         fi
7625
7626         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7627         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7628         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7629                         grep -c writeback)
7630         if [[ $LOCKED -ne 0 ]]; then
7631                 error "Locked pages remain in cache, locked=$LOCKED"
7632         fi
7633
7634         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7635                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7636         fi
7637
7638         rm -f $DIR/$tfile
7639         echo "No pages locked after fsync"
7640
7641         reset_async
7642         return 0
7643 }
7644 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7645
7646 test_118h() {
7647         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7648         remote_ost_nodsh && skip "remote OST with nodsh" && return
7649
7650         reset_async
7651
7652         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7653         set_nodes_failloc "$(osts_nodes)" 0x20e
7654         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7655         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7656         RC=$?
7657
7658         set_nodes_failloc "$(osts_nodes)" 0
7659         if [[ $RC -eq 0 ]]; then
7660                 error "Must return error due to dropped pages, rc=$RC"
7661         fi
7662
7663         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7664         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7665         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7666                     grep -c writeback)
7667         if [[ $LOCKED -ne 0 ]]; then
7668                 error "Locked pages remain in cache, locked=$LOCKED"
7669         fi
7670
7671         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7672                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7673         fi
7674
7675         rm -f $DIR/$tfile
7676         echo "No pages locked after fsync"
7677
7678         return 0
7679 }
7680 run_test 118h "Verify timeout in handling recoverables errors  =========="
7681
7682 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7683
7684 test_118i() {
7685         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7686         remote_ost_nodsh && skip "remote OST with nodsh" && return
7687
7688         reset_async
7689
7690         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7691         set_nodes_failloc "$(osts_nodes)" 0x20e
7692
7693         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7694         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7695         PID=$!
7696         sleep 5
7697         set_nodes_failloc "$(osts_nodes)" 0
7698
7699         wait $PID
7700         RC=$?
7701         if [[ $RC -ne 0 ]]; then
7702                 error "got error, but should be not, rc=$RC"
7703         fi
7704
7705         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7706         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7707         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7708         if [[ $LOCKED -ne 0 ]]; then
7709                 error "Locked pages remain in cache, locked=$LOCKED"
7710         fi
7711
7712         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7713                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7714         fi
7715
7716         rm -f $DIR/$tfile
7717         echo "No pages locked after fsync"
7718
7719         return 0
7720 }
7721 run_test 118i "Fix error before timeout in recoverable error  =========="
7722
7723 [ "$SLOW" = "no" ] && set_resend_count 4
7724
7725 test_118j() {
7726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7727         remote_ost_nodsh && skip "remote OST with nodsh" && return
7728
7729         reset_async
7730
7731         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7732         set_nodes_failloc "$(osts_nodes)" 0x220
7733
7734         # return -EIO from OST
7735         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7736         RC=$?
7737         set_nodes_failloc "$(osts_nodes)" 0x0
7738         if [[ $RC -eq 0 ]]; then
7739                 error "Must return error due to dropped pages, rc=$RC"
7740         fi
7741
7742         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7743         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7744         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7745         if [[ $LOCKED -ne 0 ]]; then
7746                 error "Locked pages remain in cache, locked=$LOCKED"
7747         fi
7748
7749         # in recoverable error on OST we want resend and stay until it finished
7750         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7751                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7752         fi
7753
7754         rm -f $DIR/$tfile
7755         echo "No pages locked after fsync"
7756
7757         return 0
7758 }
7759 run_test 118j "Simulate unrecoverable OST side error =========="
7760
7761 test_118k()
7762 {
7763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7764         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7765
7766         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7767         set_nodes_failloc "$(osts_nodes)" 0x20e
7768         test_mkdir -p $DIR/$tdir
7769
7770         for ((i=0;i<10;i++)); do
7771                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7772                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7773                 SLEEPPID=$!
7774                 sleep 0.500s
7775                 kill $SLEEPPID
7776                 wait $SLEEPPID
7777         done
7778
7779         set_nodes_failloc "$(osts_nodes)" 0
7780         rm -rf $DIR/$tdir
7781 }
7782 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7783
7784 test_118l()
7785 {
7786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7787         # LU-646
7788         test_mkdir -p $DIR/$tdir
7789         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7790         rm -rf $DIR/$tdir
7791 }
7792 run_test 118l "fsync dir ========="
7793
7794 test_118m() # LU-3066
7795 {
7796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7797         test_mkdir -p $DIR/$tdir
7798         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7799         rm -rf $DIR/$tdir
7800 }
7801 run_test 118m "fdatasync dir ========="
7802
7803 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7804
7805 test_119a() # bug 11737
7806 {
7807         BSIZE=$((512 * 1024))
7808         directio write $DIR/$tfile 0 1 $BSIZE
7809         # We ask to read two blocks, which is more than a file size.
7810         # directio will indicate an error when requested and actual
7811         # sizes aren't equeal (a normal situation in this case) and
7812         # print actual read amount.
7813         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7814         if [ "$NOB" != "$BSIZE" ]; then
7815                 error "read $NOB bytes instead of $BSIZE"
7816         fi
7817         rm -f $DIR/$tfile
7818 }
7819 run_test 119a "Short directIO read must return actual read amount"
7820
7821 test_119b() # bug 11737
7822 {
7823         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7824
7825         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7826         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7827         sync
7828         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7829                 error "direct read failed"
7830         rm -f $DIR/$tfile
7831 }
7832 run_test 119b "Sparse directIO read must return actual read amount"
7833
7834 test_119c() # bug 13099
7835 {
7836         BSIZE=1048576
7837         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7838         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7839         rm -f $DIR/$tfile
7840 }
7841 run_test 119c "Testing for direct read hitting hole"
7842
7843 test_119d() # bug 15950
7844 {
7845         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7846         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7847         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7848         BSIZE=1048576
7849         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7850         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7851         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7852         lctl set_param fail_loc=0x40d
7853         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7854         pid_dio=$!
7855         sleep 1
7856         cat $DIR/$tfile > /dev/null &
7857         lctl set_param fail_loc=0
7858         pid_reads=$!
7859         wait $pid_dio
7860         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7861         sleep 2
7862         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7863         error "the read rpcs have not completed in 2s"
7864         rm -f $DIR/$tfile
7865         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7866 }
7867 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7868
7869 test_120a() {
7870         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7871         test_mkdir -p $DIR/$tdir
7872         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7873                skip "no early lock cancel on server" && return 0
7874
7875         lru_resize_disable mdc
7876         lru_resize_disable osc
7877         cancel_lru_locks mdc
7878         # asynchronous object destroy at MDT could cause bl ast to client
7879         cancel_lru_locks osc
7880
7881         stat $DIR/$tdir > /dev/null
7882         can1=$(do_facet $SINGLEMDS \
7883                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7884                awk '/ldlm_cancel/ {print $2}')
7885         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7886                awk '/ldlm_bl_callback/ {print $2}')
7887         test_mkdir -c1 $DIR/$tdir/d1
7888         can2=$(do_facet $SINGLEMDS \
7889                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7890                awk '/ldlm_cancel/ {print $2}')
7891         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7892                awk '/ldlm_bl_callback/ {print $2}')
7893         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7894         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7895         lru_resize_enable mdc
7896         lru_resize_enable osc
7897 }
7898 run_test 120a "Early Lock Cancel: mkdir test"
7899
7900 test_120b() {
7901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7902         test_mkdir $DIR/$tdir
7903         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7904                skip "no early lock cancel on server" && return 0
7905         lru_resize_disable mdc
7906         lru_resize_disable osc
7907         cancel_lru_locks mdc
7908         stat $DIR/$tdir > /dev/null
7909         can1=$(do_facet $SINGLEMDS \
7910                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7911                awk '/ldlm_cancel/ {print $2}')
7912         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7913                awk '/ldlm_bl_callback/ {print $2}')
7914         touch $DIR/$tdir/f1
7915         can2=$(do_facet $SINGLEMDS \
7916                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7917                awk '/ldlm_cancel/ {print $2}')
7918         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7919                awk '/ldlm_bl_callback/ {print $2}')
7920         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7921         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7922         lru_resize_enable mdc
7923         lru_resize_enable osc
7924 }
7925 run_test 120b "Early Lock Cancel: create test"
7926
7927 test_120c() {
7928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7929         test_mkdir -c1 $DIR/$tdir
7930         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7931                skip "no early lock cancel on server" && return 0
7932         lru_resize_disable mdc
7933         lru_resize_disable osc
7934         test_mkdir -p -c1 $DIR/$tdir/d1
7935         test_mkdir -p -c1 $DIR/$tdir/d2
7936         touch $DIR/$tdir/d1/f1
7937         cancel_lru_locks mdc
7938         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7939         can1=$(do_facet $SINGLEMDS \
7940                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7941                awk '/ldlm_cancel/ {print $2}')
7942         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7943                awk '/ldlm_bl_callback/ {print $2}')
7944         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7945         can2=$(do_facet $SINGLEMDS \
7946                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7947                awk '/ldlm_cancel/ {print $2}')
7948         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7949                awk '/ldlm_bl_callback/ {print $2}')
7950         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7951         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7952         lru_resize_enable mdc
7953         lru_resize_enable osc
7954 }
7955 run_test 120c "Early Lock Cancel: link test"
7956
7957 test_120d() {
7958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7959         test_mkdir -p -c1 $DIR/$tdir
7960         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7961                skip "no early lock cancel on server" && return 0
7962         lru_resize_disable mdc
7963         lru_resize_disable osc
7964         touch $DIR/$tdir
7965         cancel_lru_locks mdc
7966         stat $DIR/$tdir > /dev/null
7967         can1=$(do_facet $SINGLEMDS \
7968                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7969                awk '/ldlm_cancel/ {print $2}')
7970         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7971                awk '/ldlm_bl_callback/ {print $2}')
7972         chmod a+x $DIR/$tdir
7973         can2=$(do_facet $SINGLEMDS \
7974                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7975                awk '/ldlm_cancel/ {print $2}')
7976         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7977                awk '/ldlm_bl_callback/ {print $2}')
7978         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7979         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7980         lru_resize_enable mdc
7981         lru_resize_enable osc
7982 }
7983 run_test 120d "Early Lock Cancel: setattr test"
7984
7985 test_120e() {
7986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7987         test_mkdir -p -c1 $DIR/$tdir
7988         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7989                skip "no early lock cancel on server" && return 0
7990         lru_resize_disable mdc
7991         lru_resize_disable osc
7992         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7993         cancel_lru_locks mdc
7994         cancel_lru_locks osc
7995         dd if=$DIR/$tdir/f1 of=/dev/null
7996         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7997         # XXX client can not do early lock cancel of OST lock
7998         # during unlink (LU-4206), so cancel osc lock now.
7999         cancel_lru_locks osc
8000         can1=$(do_facet $SINGLEMDS \
8001                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8002                awk '/ldlm_cancel/ {print $2}')
8003         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8004                awk '/ldlm_bl_callback/ {print $2}')
8005         unlink $DIR/$tdir/f1
8006         sleep 5
8007         can2=$(do_facet $SINGLEMDS \
8008                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8009                awk '/ldlm_cancel/ {print $2}')
8010         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8011                awk '/ldlm_bl_callback/ {print $2}')
8012         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8013         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8014         lru_resize_enable mdc
8015         lru_resize_enable osc
8016 }
8017 run_test 120e "Early Lock Cancel: unlink test"
8018
8019 test_120f() {
8020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8021         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8022                skip "no early lock cancel on server" && return 0
8023         test_mkdir -p -c1 $DIR/$tdir
8024         lru_resize_disable mdc
8025         lru_resize_disable osc
8026         test_mkdir -p -c1 $DIR/$tdir/d1
8027         test_mkdir -p -c1 $DIR/$tdir/d2
8028         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8029         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8030         cancel_lru_locks mdc
8031         cancel_lru_locks osc
8032         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8033         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8034         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8035         # XXX client can not do early lock cancel of OST lock
8036         # during rename (LU-4206), so cancel osc lock now.
8037         cancel_lru_locks osc
8038         can1=$(do_facet $SINGLEMDS \
8039                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8040                awk '/ldlm_cancel/ {print $2}')
8041         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8042                awk '/ldlm_bl_callback/ {print $2}')
8043         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8044         sleep 5
8045         can2=$(do_facet $SINGLEMDS \
8046                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8047                awk '/ldlm_cancel/ {print $2}')
8048         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8049                awk '/ldlm_bl_callback/ {print $2}')
8050         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8051         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8052         lru_resize_enable mdc
8053         lru_resize_enable osc
8054 }
8055 run_test 120f "Early Lock Cancel: rename test"
8056
8057 test_120g() {
8058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8059         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8060                skip "no early lock cancel on server" && return 0
8061         lru_resize_disable mdc
8062         lru_resize_disable osc
8063         count=10000
8064         echo create $count files
8065         test_mkdir -p $DIR/$tdir
8066         cancel_lru_locks mdc
8067         cancel_lru_locks osc
8068         t0=`date +%s`
8069
8070         can0=$(do_facet $SINGLEMDS \
8071                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8072                awk '/ldlm_cancel/ {print $2}')
8073         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8074                awk '/ldlm_bl_callback/ {print $2}')
8075         createmany -o $DIR/$tdir/f $count
8076         sync
8077         can1=$(do_facet $SINGLEMDS \
8078                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8079                awk '/ldlm_cancel/ {print $2}')
8080         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8081                awk '/ldlm_bl_callback/ {print $2}')
8082         t1=$(date +%s)
8083         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8084         echo rm $count files
8085         rm -r $DIR/$tdir
8086         sync
8087         can2=$(do_facet $SINGLEMDS \
8088                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8089                awk '/ldlm_cancel/ {print $2}')
8090         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8091                awk '/ldlm_bl_callback/ {print $2}')
8092         t2=$(date +%s)
8093         echo total: $count removes in $((t2-t1))
8094         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8095         sleep 2
8096         # wait for commitment of removal
8097         lru_resize_enable mdc
8098         lru_resize_enable osc
8099 }
8100 run_test 120g "Early Lock Cancel: performance test"
8101
8102 test_121() { #bug #10589
8103         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8104         rm -rf $DIR/$tfile
8105         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8106 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8107         lctl set_param fail_loc=0x310
8108         cancel_lru_locks osc > /dev/null
8109         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8110         lctl set_param fail_loc=0
8111         [[ $reads -eq $writes ]] ||
8112                 error "read $reads blocks, must be $writes blocks"
8113 }
8114 run_test 121 "read cancel race ========="
8115
8116 test_123a() { # was test 123, statahead(bug 11401)
8117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8118         SLOWOK=0
8119         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8120             log "testing on UP system. Performance may be not as good as expected."
8121                         SLOWOK=1
8122         fi
8123
8124         rm -rf $DIR/$tdir
8125         test_mkdir -p $DIR/$tdir
8126         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8127         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8128         MULT=10
8129         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8130                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8131
8132                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8133                 lctl set_param -n llite.*.statahead_max 0
8134                 lctl get_param llite.*.statahead_max
8135                 cancel_lru_locks mdc
8136                 cancel_lru_locks osc
8137                 stime=`date +%s`
8138                 time ls -l $DIR/$tdir | wc -l
8139                 etime=`date +%s`
8140                 delta=$((etime - stime))
8141                 log "ls $i files without statahead: $delta sec"
8142                 lctl set_param llite.*.statahead_max=$max
8143
8144                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8145                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8146                 cancel_lru_locks mdc
8147                 cancel_lru_locks osc
8148                 stime=`date +%s`
8149                 time ls -l $DIR/$tdir | wc -l
8150                 etime=`date +%s`
8151                 delta_sa=$((etime - stime))
8152                 log "ls $i files with statahead: $delta_sa sec"
8153                 lctl get_param -n llite.*.statahead_stats
8154                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8155
8156                 [[ $swrong -lt $ewrong ]] &&
8157                         log "statahead was stopped, maybe too many locks held!"
8158                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8159
8160                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
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 again without statahead: $delta sec"
8171                     lctl set_param llite.*.statahead_max=$max
8172                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8173                         if [  $SLOWOK -eq 0 ]; then
8174                                 error "ls $i files is slower with statahead!"
8175                         else
8176                                 log "ls $i files is slower with statahead!"
8177                         fi
8178                         break
8179                     fi
8180                 fi
8181
8182                 [ $delta -gt 20 ] && break
8183                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8184                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8185         done
8186         log "ls done"
8187
8188         stime=`date +%s`
8189         rm -r $DIR/$tdir
8190         sync
8191         etime=`date +%s`
8192         delta=$((etime - stime))
8193         log "rm -r $DIR/$tdir/: $delta seconds"
8194         log "rm done"
8195         lctl get_param -n llite.*.statahead_stats
8196 }
8197 run_test 123a "verify statahead work"
8198
8199 test_123b () { # statahead(bug 15027)
8200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8201         test_mkdir -p $DIR/$tdir
8202         createmany -o $DIR/$tdir/$tfile-%d 1000
8203
8204         cancel_lru_locks mdc
8205         cancel_lru_locks osc
8206
8207 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8208         lctl set_param fail_loc=0x80000803
8209         ls -lR $DIR/$tdir > /dev/null
8210         log "ls done"
8211         lctl set_param fail_loc=0x0
8212         lctl get_param -n llite.*.statahead_stats
8213         rm -r $DIR/$tdir
8214         sync
8215
8216 }
8217 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8218
8219 test_124a() {
8220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8221         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8222                 skip "no lru resize on server" && return 0
8223         local NR=2000
8224         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8225
8226         log "create $NR files at $DIR/$tdir"
8227         createmany -o $DIR/$tdir/f $NR ||
8228                 error "failed to create $NR files in $DIR/$tdir"
8229
8230         cancel_lru_locks mdc
8231         ls -l $DIR/$tdir > /dev/null
8232
8233         local NSDIR=""
8234         local LRU_SIZE=0
8235         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8236                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8237                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8238                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8239                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8240                         log "NSDIR=$NSDIR"
8241                         log "NS=$(basename $NSDIR)"
8242                         break
8243                 fi
8244         done
8245
8246         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8247                 skip "Not enough cached locks created!"
8248                 return 0
8249         fi
8250         log "LRU=$LRU_SIZE"
8251
8252         local SLEEP=30
8253
8254         # We know that lru resize allows one client to hold $LIMIT locks
8255         # for 10h. After that locks begin to be killed by client.
8256         local MAX_HRS=10
8257         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8258         log "LIMIT=$LIMIT"
8259         if [ $LIMIT -lt $LRU_SIZE ]; then
8260             skip "Limit is too small $LIMIT"
8261             return 0
8262         fi
8263
8264         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8265         # killing locks. Some time was spent for creating locks. This means
8266         # that up to the moment of sleep finish we must have killed some of
8267         # them (10-100 locks). This depends on how fast ther were created.
8268         # Many of them were touched in almost the same moment and thus will
8269         # be killed in groups.
8270         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8271
8272         # Use $LRU_SIZE_B here to take into account real number of locks
8273         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8274         local LRU_SIZE_B=$LRU_SIZE
8275         log "LVF=$LVF"
8276         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8277         log "OLD_LVF=$OLD_LVF"
8278         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8279
8280         # Let's make sure that we really have some margin. Client checks
8281         # cached locks every 10 sec.
8282         SLEEP=$((SLEEP+20))
8283         log "Sleep ${SLEEP} sec"
8284         local SEC=0
8285         while ((SEC<$SLEEP)); do
8286                 echo -n "..."
8287                 sleep 5
8288                 SEC=$((SEC+5))
8289                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8290                 echo -n "$LRU_SIZE"
8291         done
8292         echo ""
8293         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8294         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8295
8296         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8297                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8298                 unlinkmany $DIR/$tdir/f $NR
8299                 return
8300         }
8301
8302         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8303         log "unlink $NR files at $DIR/$tdir"
8304         unlinkmany $DIR/$tdir/f $NR
8305 }
8306 run_test 124a "lru resize ======================================="
8307
8308 get_max_pool_limit()
8309 {
8310         local limit=$($LCTL get_param \
8311                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8312         local max=0
8313         for l in $limit; do
8314                 if [[ $l -gt $max ]]; then
8315                         max=$l
8316                 fi
8317         done
8318         echo $max
8319 }
8320
8321 test_124b() {
8322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8323         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8324                 skip "no lru resize on server" && return 0
8325
8326         LIMIT=$(get_max_pool_limit)
8327
8328         NR=$(($(default_lru_size)*20))
8329         if [[ $NR -gt $LIMIT ]]; then
8330                 log "Limit lock number by $LIMIT locks"
8331                 NR=$LIMIT
8332         fi
8333         lru_resize_disable mdc
8334         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8335                 error "failed to create $DIR/$tdir/disable_lru_resize"
8336
8337         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8338         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8339         cancel_lru_locks mdc
8340         stime=`date +%s`
8341         PID=""
8342         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8343         PID="$PID $!"
8344         sleep 2
8345         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8346         PID="$PID $!"
8347         sleep 2
8348         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8349         PID="$PID $!"
8350         wait $PID
8351         etime=`date +%s`
8352         nolruresize_delta=$((etime-stime))
8353         log "ls -la time: $nolruresize_delta seconds"
8354         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8355         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8356
8357         lru_resize_enable mdc
8358         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8359                 error "failed to create $DIR/$tdir/enable_lru_resize"
8360
8361         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8362         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8363         cancel_lru_locks mdc
8364         stime=`date +%s`
8365         PID=""
8366         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8367         PID="$PID $!"
8368         sleep 2
8369         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8370         PID="$PID $!"
8371         sleep 2
8372         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8373         PID="$PID $!"
8374         wait $PID
8375         etime=`date +%s`
8376         lruresize_delta=$((etime-stime))
8377         log "ls -la time: $lruresize_delta seconds"
8378         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8379
8380         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8381                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8382         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8383                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8384         else
8385                 log "lru resize performs the same with no lru resize"
8386         fi
8387         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8388 }
8389 run_test 124b "lru resize (performance test) ======================="
8390
8391 test_125() { # 13358
8392         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8393         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8394         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8395         test_mkdir -p $DIR/d125 || error "mkdir failed"
8396         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8397         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8398         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8399 }
8400 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8401
8402 test_126() { # bug 12829/13455
8403         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8404         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8405         $GSS && skip "must run as gss disabled" && return
8406
8407         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8408         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8409         rm -f $DIR/$tfile
8410         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8411 }
8412 run_test 126 "check that the fsgid provided by the client is taken into account"
8413
8414 test_127a() { # bug 15521
8415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8416         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8417         $LCTL set_param osc.*.stats=0
8418         FSIZE=$((2048 * 1024))
8419         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8420         cancel_lru_locks osc
8421         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8422
8423         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8424         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8425                 echo "got $COUNT $NAME"
8426                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8427                 eval $NAME=$COUNT || error "Wrong proc format"
8428
8429                 case $NAME in
8430                         read_bytes|write_bytes)
8431                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8432                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8433                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8434                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8435                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8436                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8437                                 error "sumsquare is too small: $SUMSQ"
8438                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8439                                 error "sumsquare is too big: $SUMSQ"
8440                         ;;
8441                         *) ;;
8442                 esac
8443         done < $DIR/${tfile}.tmp
8444
8445         #check that we actually got some stats
8446         [ "$read_bytes" ] || error "Missing read_bytes stats"
8447         [ "$write_bytes" ] || error "Missing write_bytes stats"
8448         [ "$read_bytes" != 0 ] || error "no read done"
8449         [ "$write_bytes" != 0 ] || error "no write done"
8450 }
8451 run_test 127a "verify the client stats are sane"
8452
8453 test_127b() { # bug LU-333
8454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8455         $LCTL set_param llite.*.stats=0
8456         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8457         # perform 2 reads and writes so MAX is different from SUM.
8458         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8459         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8460         cancel_lru_locks osc
8461         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8462         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8463
8464         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8465         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8466                 echo "got $COUNT $NAME"
8467                 eval $NAME=$COUNT || error "Wrong proc format"
8468
8469         case $NAME in
8470                 read_bytes)
8471                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8472                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8473                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8474                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8475                         ;;
8476                 write_bytes)
8477                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8478                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8479                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8480                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8481                         ;;
8482                         *) ;;
8483                 esac
8484         done < $TMP/${tfile}.tmp
8485
8486         #check that we actually got some stats
8487         [ "$read_bytes" ] || error "Missing read_bytes stats"
8488         [ "$write_bytes" ] || error "Missing write_bytes stats"
8489         [ "$read_bytes" != 0 ] || error "no read done"
8490         [ "$write_bytes" != 0 ] || error "no write done"
8491 }
8492 run_test 127b "verify the llite client stats are sane"
8493
8494 test_128() { # bug 15212
8495         touch $DIR/$tfile
8496         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8497                 find $DIR/$tfile
8498                 find $DIR/$tfile
8499         EOF
8500
8501         result=$(grep error $TMP/$tfile.log)
8502         rm -f $DIR/$tfile
8503         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8504 }
8505 run_test 128 "interactive lfs for 2 consecutive find's"
8506
8507 set_dir_limits () {
8508         local mntdev
8509         local canondev
8510         local node
8511
8512         local LDPROC=/proc/fs/ldiskfs
8513         local facets=$(get_facets MDS)
8514
8515         for facet in ${facets//,/ }; do
8516                 canondev=$(ldiskfs_canon \
8517                            *.$(convert_facet2label $facet).mntdev $facet)
8518                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8519                                                 LDPROC=/sys/fs/ldiskfs
8520                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8521         done
8522 }
8523
8524 test_129() {
8525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8526         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8527                 skip "Only applicable to ldiskfs-based MDTs"
8528                 return
8529         fi
8530         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8531         ENOSPC=28
8532         EFBIG=27
8533
8534         rm -rf $DIR/$tdir
8535         test_mkdir -p $DIR/$tdir
8536
8537         # block size of mds1
8538         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8539         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8540         local MAX=$((MDSBLOCKSIZE * 3))
8541         set_dir_limits $MAX
8542         local I=$(stat -c%s "$DIR/$tdir")
8543         local J=0
8544         local STRIPE_COUNT=1
8545         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8546         MAX=$((MAX*STRIPE_COUNT))
8547         while [[ $I -le $MAX ]]; do
8548                 $MULTIOP $DIR/$tdir/$J Oc
8549                 rc=$?
8550                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8551                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8552                 #and EFBIG for previous versions
8553                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8554                         set_dir_limits 0
8555                         echo "return code $rc received as expected"
8556                         multiop $DIR/$tdir/$J Oc ||
8557                                 error_exit "multiop failed w/o dir size limit"
8558
8559                         I=$(stat -c%s "$DIR/$tdir")
8560
8561                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8562                                         $(version_code 2.4.51) ]
8563                         then
8564                                 [[ $I -eq $MAX ]] && return 0
8565                         else
8566                                 [[ $I -gt $MAX ]] && return 0
8567                         fi
8568                         error_exit "current dir size $I, previous limit $MAX"
8569                 elif [ $rc -ne 0 ]; then
8570                         set_dir_limits 0
8571                         error_exit "return code $rc received instead of expected " \
8572                                    "$EFBIG or $ENOSPC, files in dir $I"
8573                 fi
8574                 J=$((J+1))
8575                 I=$(stat -c%s "$DIR/$tdir")
8576         done
8577
8578         set_dir_limits 0
8579         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8580 }
8581 run_test 129 "test directory size limit ========================"
8582
8583 OLDIFS="$IFS"
8584 cleanup_130() {
8585         trap 0
8586         IFS="$OLDIFS"
8587 }
8588
8589 test_130a() {
8590         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8591         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8592                 return
8593
8594         trap cleanup_130 EXIT RETURN
8595
8596         local fm_file=$DIR/$tfile
8597         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8598         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8599                 error "dd failed for $fm_file"
8600
8601         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8602         filefrag -ves $fm_file
8603         RC=$?
8604         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8605                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8606         [ $RC != 0 ] && error "filefrag $fm_file failed"
8607
8608         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8609                       grep -v "ext:" | grep -v "found")
8610         lun=$($GETSTRIPE -i $fm_file)
8611
8612         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8613         IFS=$'\n'
8614         tot_len=0
8615         for line in $filefrag_op
8616         do
8617                 frag_lun=`echo $line | cut -d: -f5`
8618                 ext_len=`echo $line | cut -d: -f4`
8619                 if (( $frag_lun != $lun )); then
8620                         cleanup_130
8621                         error "FIEMAP on 1-stripe file($fm_file) failed"
8622                         return
8623                 fi
8624                 (( tot_len += ext_len ))
8625         done
8626
8627         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8628                 cleanup_130
8629                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8630                 return
8631         fi
8632
8633         cleanup_130
8634
8635         echo "FIEMAP on single striped file succeeded"
8636 }
8637 run_test 130a "FIEMAP (1-stripe file)"
8638
8639 test_130b() {
8640         [ "$OSTCOUNT" -lt "2" ] &&
8641                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8642
8643         [ "$OSTCOUNT" -ge "10" ] &&
8644                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8645
8646         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8647         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8648                 return
8649
8650         trap cleanup_130 EXIT RETURN
8651
8652         local fm_file=$DIR/$tfile
8653         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8654         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8655                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8656
8657         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8658                 error "dd failed on $fm_file"
8659
8660         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8661         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8662                       grep -v "ext:" | grep -v "found")
8663
8664         last_lun=$(echo $filefrag_op | cut -d: -f5)
8665
8666         IFS=$'\n'
8667         tot_len=0
8668         num_luns=1
8669         for line in $filefrag_op
8670         do
8671                 frag_lun=`echo $line | cut -d: -f5`
8672                 ext_len=`echo $line | cut -d: -f4`
8673                 if (( $frag_lun != $last_lun )); then
8674                         if (( tot_len != 1024 )); then
8675                                 cleanup_130
8676                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8677                                 return
8678                         else
8679                                 (( num_luns += 1 ))
8680                                 tot_len=0
8681                         fi
8682                 fi
8683                 (( tot_len += ext_len ))
8684                 last_lun=$frag_lun
8685         done
8686         if (( num_luns != 2 || tot_len != 1024 )); then
8687                 cleanup_130
8688                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8689                 return
8690         fi
8691
8692         cleanup_130
8693
8694         echo "FIEMAP on 2-stripe file succeeded"
8695 }
8696 run_test 130b "FIEMAP (2-stripe file)"
8697
8698 test_130c() {
8699         [ "$OSTCOUNT" -lt "2" ] &&
8700                 skip_env "skipping FIEMAP on 2-stripe file" && return
8701
8702         [ "$OSTCOUNT" -ge "10" ] &&
8703                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8704
8705         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8706         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8707                 return
8708
8709         trap cleanup_130 EXIT RETURN
8710
8711         local fm_file=$DIR/$tfile
8712         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8713         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8714                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8715
8716         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8717
8718         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8719         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8720
8721         last_lun=`echo $filefrag_op | cut -d: -f5`
8722
8723         IFS=$'\n'
8724         tot_len=0
8725         num_luns=1
8726         for line in $filefrag_op
8727         do
8728                 frag_lun=`echo $line | cut -d: -f5`
8729                 ext_len=`echo $line | cut -d: -f4`
8730                 if (( $frag_lun != $last_lun )); then
8731                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8732                         if (( logical != 512 )); then
8733                                 cleanup_130
8734                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8735                                 return
8736                         fi
8737                         if (( tot_len != 512 )); then
8738                                 cleanup_130
8739                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8740                                 return
8741                         else
8742                                 (( num_luns += 1 ))
8743                                 tot_len=0
8744                         fi
8745                 fi
8746                 (( tot_len += ext_len ))
8747                 last_lun=$frag_lun
8748         done
8749         if (( num_luns != 2 || tot_len != 512 )); then
8750                 cleanup_130
8751                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8752                 return
8753         fi
8754
8755         cleanup_130
8756
8757         echo "FIEMAP on 2-stripe file with hole succeeded"
8758 }
8759 run_test 130c "FIEMAP (2-stripe file with hole)"
8760
8761 test_130d() {
8762         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8763
8764         [ "$OSTCOUNT" -ge "10" ] &&
8765                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8766
8767         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8768         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8769
8770         trap cleanup_130 EXIT RETURN
8771
8772         local fm_file=$DIR/$tfile
8773         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8774         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8775                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8776
8777         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8778         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8779                 error "dd failed on $fm_file"
8780
8781         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8782         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8783                 grep -v "ext:" | grep -v "found"`
8784
8785         last_lun=`echo $filefrag_op | cut -d: -f5`
8786
8787         IFS=$'\n'
8788         tot_len=0
8789         num_luns=1
8790         for line in $filefrag_op
8791         do
8792                 frag_lun=`echo $line | cut -d: -f5`
8793                 ext_len=`echo $line | cut -d: -f4`
8794                 if (( $frag_lun != $last_lun )); then
8795                         if (( tot_len != 1024 )); then
8796                                 cleanup_130
8797                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8798                                 return
8799                         else
8800                                 (( num_luns += 1 ))
8801                                 tot_len=0
8802                         fi
8803                 fi
8804                 (( tot_len += ext_len ))
8805                 last_lun=$frag_lun
8806         done
8807         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8808                 cleanup_130
8809                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8810                 return
8811         fi
8812
8813         cleanup_130
8814
8815         echo "FIEMAP on N-stripe file succeeded"
8816 }
8817 run_test 130d "FIEMAP (N-stripe file)"
8818
8819 test_130e() {
8820         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8821
8822         [ "$OSTCOUNT" -ge "10" ] &&
8823                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8824
8825         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8826         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8827
8828         trap cleanup_130 EXIT RETURN
8829
8830         local fm_file=$DIR/$tfile
8831         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8832         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8833                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8834
8835         NUM_BLKS=512
8836         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8837         for ((i = 0; i < $NUM_BLKS; i++))
8838         do
8839                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8840         done
8841
8842         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8843         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8844
8845         last_lun=`echo $filefrag_op | cut -d: -f5`
8846
8847         IFS=$'\n'
8848         tot_len=0
8849         num_luns=1
8850         for line in $filefrag_op
8851         do
8852                 frag_lun=`echo $line | cut -d: -f5`
8853                 ext_len=`echo $line | cut -d: -f4`
8854                 if (( $frag_lun != $last_lun )); then
8855                         if (( tot_len != $EXPECTED_LEN )); then
8856                                 cleanup_130
8857                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8858                                 return
8859                         else
8860                                 (( num_luns += 1 ))
8861                                 tot_len=0
8862                         fi
8863                 fi
8864                 (( tot_len += ext_len ))
8865                 last_lun=$frag_lun
8866         done
8867         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8868                 cleanup_130
8869                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8870                 return
8871         fi
8872
8873         cleanup_130
8874
8875         echo "FIEMAP with continuation calls succeeded"
8876 }
8877 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8878
8879 # Test for writev/readv
8880 test_131a() {
8881         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8882         error "writev test failed"
8883         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8884         error "readv failed"
8885         rm -f $DIR/$tfile
8886 }
8887 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8888
8889 test_131b() {
8890         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8891         error "append writev test failed"
8892         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8893         error "append writev test failed"
8894         rm -f $DIR/$tfile
8895 }
8896 run_test 131b "test append writev"
8897
8898 test_131c() {
8899         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8900         error "NOT PASS"
8901 }
8902 run_test 131c "test read/write on file w/o objects"
8903
8904 test_131d() {
8905         rwv -f $DIR/$tfile -w -n 1 1572864
8906         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8907         if [ "$NOB" != 1572864 ]; then
8908                 error "Short read filed: read $NOB bytes instead of 1572864"
8909         fi
8910         rm -f $DIR/$tfile
8911 }
8912 run_test 131d "test short read"
8913
8914 test_131e() {
8915         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8916         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8917         error "read hitting hole failed"
8918         rm -f $DIR/$tfile
8919 }
8920 run_test 131e "test read hitting hole"
8921
8922 get_ost_param() {
8923         local token=$1
8924         local gl_sum=0
8925         for node in $(osts_nodes); do
8926                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8927                 [ x$gl = x"" ] && gl=0
8928                 gl_sum=$((gl_sum + gl))
8929         done
8930         echo $gl_sum
8931 }
8932
8933 som_mode_switch() {
8934         local som=$1
8935         local gl1=$2
8936         local gl2=$3
8937
8938         if [ x$som = x"enabled" ]; then
8939                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8940                 MOUNT_OPTS=`echo $MOUNT_OPTS |
8941                     sed 's/som_preview,\|,som_preview\|som_preview//g'`
8942                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8943         else
8944                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8945                 MOUNT_OPTS="${MOUNT_OPTS:+$MOUNT_OPTS,}som_preview"
8946                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8947         fi
8948
8949         # do remount to make new mount-conf parameters actual
8950         echo remounting...
8951         sync
8952         stopall
8953         setupall
8954 }
8955
8956 test_132() { #1028, SOM
8957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8958         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8959         local mdtidx=$($LFS getstripe -M $DIR)
8960         local facet=mds$((mdtidx + 1))
8961
8962         local MOUNTOPT_SAVE=$MOUNTOPT
8963
8964         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8965         cancel_lru_locks osc
8966         som1=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8967                awk -F= ' {print $2}' | head -n 1)
8968
8969         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8970         stat $DIR/$tfile >/dev/null
8971         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8972         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8973         rm $DIR/$tfile
8974         som_mode_switch $som1 $gl1 $gl2
8975
8976         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8977         cancel_lru_locks osc
8978
8979         som2=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8980                awk -F= ' {print $2}' | head -n 1)
8981         if [ $som1 == $som2 ]; then
8982             error "som is still "$som2
8983             if [ x$som2 = x"enabled" ]; then
8984                 som2="disabled"
8985             else
8986                 som2="enabled"
8987             fi
8988         fi
8989
8990         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8991         stat $DIR/$tfile >/dev/null
8992         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8993         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8994         som_mode_switch $som2 $gl1 $gl2
8995         MOUNT_OPTS=$MOUNT_OPTS_SAVE
8996 }
8997 run_test 132 "som avoids glimpse rpc"
8998
8999 check_stats() {
9000         local res
9001         local count
9002         case $1 in
9003         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9004                  ;;
9005         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9006                  ;;
9007         *) error "Wrong argument $1" ;;
9008         esac
9009         echo $res
9010         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9011         # if the argument $3 is zero, it means any stat increment is ok.
9012         if [[ $3 -gt 0 ]]; then
9013                 count=$(echo $res | awk '{ print $2 }')
9014                 [[ $count -ne $3 ]] &&
9015                         error "The $2 counter on $1 is wrong - expected $3"
9016         fi
9017 }
9018
9019 test_133a() {
9020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9021         remote_ost_nodsh && skip "remote OST with nodsh" && return
9022         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9023
9024         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9025                 { skip "MDS doesn't support rename stats"; return; }
9026         local testdir=$DIR/${tdir}/stats_testdir
9027         mkdir -p $DIR/${tdir}
9028
9029         # clear stats.
9030         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9031         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9032
9033         # verify mdt stats first.
9034         mkdir ${testdir} || error "mkdir failed"
9035         check_stats $SINGLEMDS "mkdir" 1
9036         touch ${testdir}/${tfile} || "touch failed"
9037         check_stats $SINGLEMDS "open" 1
9038         check_stats $SINGLEMDS "close" 1
9039         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9040         check_stats $SINGLEMDS "mknod" 1
9041         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9042         check_stats $SINGLEMDS "unlink" 1
9043         rm -f ${testdir}/${tfile} || error "file remove failed"
9044         check_stats $SINGLEMDS "unlink" 2
9045
9046         # remove working dir and check mdt stats again.
9047         rmdir ${testdir} || error "rmdir failed"
9048         check_stats $SINGLEMDS "rmdir" 1
9049
9050         local testdir1=$DIR/${tdir}/stats_testdir1
9051         mkdir -p ${testdir}
9052         mkdir -p ${testdir1}
9053         touch ${testdir1}/test1
9054         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9055         check_stats $SINGLEMDS "crossdir_rename" 1
9056
9057         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9058         check_stats $SINGLEMDS "samedir_rename" 1
9059
9060         rm -rf $DIR/${tdir}
9061 }
9062 run_test 133a "Verifying MDT stats ========================================"
9063
9064 test_133b() {
9065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9066         remote_ost_nodsh && skip "remote OST with nodsh" && return
9067         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9068         local testdir=$DIR/${tdir}/stats_testdir
9069         mkdir -p ${testdir} || error "mkdir failed"
9070         touch ${testdir}/${tfile} || "touch failed"
9071         cancel_lru_locks mdc
9072
9073         # clear stats.
9074         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9075         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9076
9077         # extra mdt stats verification.
9078         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9079         check_stats $SINGLEMDS "setattr" 1
9080         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9081         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9082         then            # LU-1740
9083                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9084                 check_stats $SINGLEMDS "getattr" 1
9085         fi
9086         $LFS df || error "lfs failed"
9087         check_stats $SINGLEMDS "statfs" 1
9088
9089         rm -rf $DIR/${tdir}
9090 }
9091 run_test 133b "Verifying extra MDT stats =================================="
9092
9093 test_133c() {
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         test_mkdir -p ${testdir} || error "mkdir failed"
9099
9100         # verify obdfilter stats.
9101         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9102         sync
9103         cancel_lru_locks osc
9104         wait_delete_completed
9105
9106         # clear stats.
9107         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9108         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9109
9110         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9111         sync
9112         cancel_lru_locks osc
9113         check_stats ost "write" 1
9114
9115         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9116         check_stats ost "read" 1
9117
9118         > ${testdir}/${tfile} || error "truncate failed"
9119         check_stats ost "punch" 1
9120
9121         rm -f ${testdir}/${tfile} || error "file remove failed"
9122         wait_delete_completed
9123         check_stats ost "destroy" 1
9124
9125         rm -rf $DIR/${tdir}
9126 }
9127 run_test 133c "Verifying OST stats ========================================"
9128
9129 order_2() {
9130         local value=$1
9131         local orig=$value
9132         local order=1
9133
9134         while [ $value -ge 2 ]; do
9135                 order=$((order*2))
9136                 value=$((value/2))
9137         done
9138
9139         if [ $orig -gt $order ]; then
9140                 order=$((order*2))
9141         fi
9142         echo $order
9143 }
9144
9145 size_in_KMGT() {
9146     local value=$1
9147     local size=('K' 'M' 'G' 'T');
9148     local i=0
9149     local size_string=$value
9150
9151     while [ $value -ge 1024 ]; do
9152         if [ $i -gt 3 ]; then
9153             #T is the biggest unit we get here, if that is bigger,
9154             #just return XXXT
9155             size_string=${value}T
9156             break
9157         fi
9158         value=$((value >> 10))
9159         if [ $value -lt 1024 ]; then
9160             size_string=${value}${size[$i]}
9161             break
9162         fi
9163         i=$((i + 1))
9164     done
9165
9166     echo $size_string
9167 }
9168
9169 get_rename_size() {
9170     local size=$1
9171     local context=${2:-.}
9172     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9173                 grep -A1 $context |
9174                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9175     echo $sample
9176 }
9177
9178 test_133d() {
9179         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9180         remote_ost_nodsh && skip "remote OST with nodsh" && return
9181         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9182         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9183         { skip "MDS doesn't support rename stats"; return; }
9184
9185         local testdir1=$DIR/${tdir}/stats_testdir1
9186         local testdir2=$DIR/${tdir}/stats_testdir2
9187
9188         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9189
9190         mkdir -p ${testdir1} || error "mkdir failed"
9191         mkdir -p ${testdir2} || error "mkdir failed"
9192
9193         createmany -o $testdir1/test 512 || error "createmany failed"
9194
9195         # check samedir rename size
9196         mv ${testdir1}/test0 ${testdir1}/test_0
9197
9198         local testdir1_size=$(ls -l $DIR/${tdir} |
9199                 awk '/stats_testdir1/ {print $5}')
9200         local testdir2_size=$(ls -l $DIR/${tdir} |
9201                 awk '/stats_testdir2/ {print $5}')
9202
9203         testdir1_size=$(order_2 $testdir1_size)
9204         testdir2_size=$(order_2 $testdir2_size)
9205
9206         testdir1_size=$(size_in_KMGT $testdir1_size)
9207         testdir2_size=$(size_in_KMGT $testdir2_size)
9208
9209         echo "source rename dir size: ${testdir1_size}"
9210         echo "target rename dir size: ${testdir2_size}"
9211
9212         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9213         eval $cmd || error "$cmd failed"
9214         local samedir=$($cmd | grep 'same_dir')
9215         local same_sample=$(get_rename_size $testdir1_size)
9216         [ -z "$samedir" ] && error "samedir_rename_size count error"
9217         [[ $same_sample -eq 1 ]] ||
9218                 error "samedir_rename_size error $same_sample"
9219         echo "Check same dir rename stats success"
9220
9221         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9222
9223         # check crossdir rename size
9224         mv ${testdir1}/test_0 ${testdir2}/test_0
9225
9226         testdir1_size=$(ls -l $DIR/${tdir} |
9227                 awk '/stats_testdir1/ {print $5}')
9228         testdir2_size=$(ls -l $DIR/${tdir} |
9229                 awk '/stats_testdir2/ {print $5}')
9230
9231         testdir1_size=$(order_2 $testdir1_size)
9232         testdir2_size=$(order_2 $testdir2_size)
9233
9234         testdir1_size=$(size_in_KMGT $testdir1_size)
9235         testdir2_size=$(size_in_KMGT $testdir2_size)
9236
9237         echo "source rename dir size: ${testdir1_size}"
9238         echo "target rename dir size: ${testdir2_size}"
9239
9240         eval $cmd || error "$cmd failed"
9241         local crossdir=$($cmd | grep 'crossdir')
9242         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9243         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9244         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9245         [[ $src_sample -eq 1 ]] ||
9246                 error "crossdir_rename_size error $src_sample"
9247         [[ $tgt_sample -eq 1 ]] ||
9248                 error "crossdir_rename_size error $tgt_sample"
9249         echo "Check cross dir rename stats success"
9250         rm -rf $DIR/${tdir}
9251 }
9252 run_test 133d "Verifying rename_stats ========================================"
9253
9254 test_133e() {
9255         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9256         remote_ost_nodsh && skip "remote OST with nodsh" && return
9257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9258         local testdir=$DIR/${tdir}/stats_testdir
9259         local ctr f0 f1 bs=32768 count=42 sum
9260
9261         mkdir -p ${testdir} || error "mkdir failed"
9262
9263         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9264
9265         for ctr in {write,read}_bytes; do
9266                 sync
9267                 cancel_lru_locks osc
9268
9269                 do_facet ost1 $LCTL set_param -n \
9270                         "obdfilter.*.exports.clear=clear"
9271
9272                 if [ $ctr = write_bytes ]; then
9273                         f0=/dev/zero
9274                         f1=${testdir}/${tfile}
9275                 else
9276                         f0=${testdir}/${tfile}
9277                         f1=/dev/null
9278                 fi
9279
9280                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9281                         error "dd failed"
9282                 sync
9283                 cancel_lru_locks osc
9284
9285                 sum=$(do_facet ost1 $LCTL get_param \
9286                         "obdfilter.*.exports.*.stats" |
9287                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9288                                 $1 == ctr { sum += $7 }
9289                                 END { printf("%0.0f", sum) }')
9290
9291                 if ((sum != bs * count)); then
9292                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9293                 fi
9294         done
9295
9296         rm -rf $DIR/${tdir}
9297 }
9298 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9299
9300 test_133f() {
9301         local proc_dirs
9302
9303         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9304 /sys/fs/lustre/ /sys/fs/lnet/"
9305         local dir
9306         for dir in $dirs; do
9307                 if [ -d $dir ]; then
9308                         proc_dirs="$proc_dirs $dir"
9309                 fi
9310         done
9311
9312         local facet
9313
9314         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9315         remote_ost_nodsh && skip "remote OST with nodsh" && return
9316         # First without trusting modes.
9317         find $proc_dirs -exec cat '{}' \; &> /dev/null
9318
9319         # Second verifying readability.
9320         find $proc_dirs \
9321                 -type f \
9322                 -exec cat '{}' \; &> /dev/null ||
9323                         error "proc file read failed"
9324
9325         for facet in $SINGLEMDS ost1; do
9326                 do_facet $facet find $proc_dirs \
9327                         ! -name req_history \
9328                         -exec cat '{}' \\\; &> /dev/null
9329
9330                 do_facet $facet find $proc_dirs \
9331                         ! -name req_history \
9332                         -type f \
9333                         -exec cat '{}' \\\; &> /dev/null ||
9334                                 error "proc file read failed"
9335         done
9336 }
9337 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9338
9339 test_133g() {
9340         local proc_dirs
9341
9342         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9343 /sys/fs/lustre/ /sys/fs/lnet/"
9344         local dir
9345         for dir in $dirs; do
9346                 if [ -d $dir ]; then
9347                         proc_dirs="$proc_dirs $dir"
9348                 fi
9349         done
9350
9351         local facet
9352
9353         # Second verifying readability.
9354         find $proc_dirs \
9355                 -type f \
9356                 -not -name force_lbug \
9357                 -not -name changelog_mask \
9358                 -exec badarea_io '{}' \; &> /dev/null ||
9359                 error "find $proc_dirs failed"
9360
9361         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9362                 skip "Too old lustre on MDS" && return
9363
9364         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9365                 skip "Too old lustre on ost1" && return
9366
9367         for facet in $SINGLEMDS ost1; do
9368                 do_facet $facet find $proc_dirs \
9369                         -type f \
9370                         -not -name force_lbug \
9371                         -not -name changelog_mask \
9372                         -exec badarea_io '{}' \\\; &> /dev/null ||
9373                 error "$facet find $proc_dirs failed"
9374
9375         done
9376
9377         # remount the FS in case writes/reads /proc break the FS
9378         cleanup || error "failed to unmount"
9379         setup || error "failed to setup"
9380         true
9381 }
9382 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9383
9384 test_140() { #bug-17379
9385         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9386         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9387         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9388         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9389
9390         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9391         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9392         local i=0
9393         while i=`expr $i + 1`; do
9394                 test_mkdir -p $i || error "Creating dir $i"
9395                 cd $i || error "Changing to $i"
9396                 ln -s ../stat stat || error "Creating stat symlink"
9397                 # Read the symlink until ELOOP present,
9398                 # not LBUGing the system is considered success,
9399                 # we didn't overrun the stack.
9400                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9401                 [ $ret -ne 0 ] && {
9402                         if [ $ret -eq 40 ]; then
9403                                 break  # -ELOOP
9404                         else
9405                                 error "Open stat symlink"
9406                                 return
9407                         fi
9408                 }
9409         done
9410         i=`expr $i - 1`
9411         echo "The symlink depth = $i"
9412         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9413                                         error "Invalid symlink depth"
9414
9415         # Test recursive symlink
9416         ln -s symlink_self symlink_self
9417         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9418         echo "open symlink_self returns $ret"
9419         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9420 }
9421 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9422
9423 test_150() {
9424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9425         local TF="$TMP/$tfile"
9426
9427         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9428         cp $TF $DIR/$tfile
9429         cancel_lru_locks osc
9430         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9431         remount_client $MOUNT
9432         df -P $MOUNT
9433         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9434
9435         $TRUNCATE $TF 6000
9436         $TRUNCATE $DIR/$tfile 6000
9437         cancel_lru_locks osc
9438         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9439
9440         echo "12345" >>$TF
9441         echo "12345" >>$DIR/$tfile
9442         cancel_lru_locks osc
9443         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9444
9445         echo "12345" >>$TF
9446         echo "12345" >>$DIR/$tfile
9447         cancel_lru_locks osc
9448         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9449
9450         rm -f $TF
9451         true
9452 }
9453 run_test 150 "truncate/append tests"
9454
9455 #LU-2902 roc_hit was not able to read all values from lproc
9456 function roc_hit_init() {
9457         local list=$(comma_list $(osts_nodes))
9458         local dir=$DIR/$tdir-check
9459         local file=$dir/file
9460         local BEFORE
9461         local AFTER
9462         local idx
9463
9464         test_mkdir -p $dir
9465         #use setstripe to do a write to every ost
9466         for i in $(seq 0 $((OSTCOUNT-1))); do
9467                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9468                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9469                 idx=$(printf %04x $i)
9470                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9471                         awk '$1 == "cache_access" {sum += $7}
9472                                 END { printf("%0.0f", sum) }')
9473
9474                 cancel_lru_locks osc
9475                 cat $file >/dev/null
9476
9477                 AFTER=$(get_osd_param $list *OST*$idx stats |
9478                         awk '$1 == "cache_access" {sum += $7}
9479                                 END { printf("%0.0f", sum) }')
9480
9481                 echo BEFORE:$BEFORE AFTER:$AFTER
9482                 if ! let "AFTER - BEFORE == 4"; then
9483                         rm -rf $dir
9484                         error "roc_hit is not safe to use"
9485                 fi
9486                 rm $file
9487         done
9488
9489         rm -rf $dir
9490 }
9491
9492 function roc_hit() {
9493         local list=$(comma_list $(osts_nodes))
9494         echo $(get_osd_param $list '' stats |
9495                 awk '$1 == "cache_hit" {sum += $7}
9496                         END { printf("%0.0f", sum) }')
9497 }
9498
9499 function set_cache() {
9500         local on=1
9501
9502         if [ "$2" == "off" ]; then
9503                 on=0;
9504         fi
9505         local list=$(comma_list $(osts_nodes))
9506         set_osd_param $list '' $1_cache_enable $on
9507
9508         cancel_lru_locks osc
9509 }
9510
9511 test_151() {
9512         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9513         remote_ost_nodsh && skip "remote OST with nodsh" && return
9514
9515         local CPAGES=3
9516         local list=$(comma_list $(osts_nodes))
9517
9518         # check whether obdfilter is cache capable at all
9519         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9520                 echo "not cache-capable obdfilter"
9521                 return 0
9522         fi
9523
9524         # check cache is enabled on all obdfilters
9525         if get_osd_param $list '' read_cache_enable | grep 0; then
9526                 echo "oss cache is disabled"
9527                 return 0
9528         fi
9529
9530         set_osd_param $list '' writethrough_cache_enable 1
9531
9532         # check write cache is enabled on all obdfilters
9533         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9534                 echo "oss write cache is NOT enabled"
9535                 return 0
9536         fi
9537
9538         roc_hit_init
9539
9540         #define OBD_FAIL_OBD_NO_LRU  0x609
9541         do_nodes $list $LCTL set_param fail_loc=0x609
9542
9543         # pages should be in the case right after write
9544         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9545                 error "dd failed"
9546
9547         local BEFORE=$(roc_hit)
9548         cancel_lru_locks osc
9549         cat $DIR/$tfile >/dev/null
9550         local AFTER=$(roc_hit)
9551
9552         do_nodes $list $LCTL set_param fail_loc=0
9553
9554         if ! let "AFTER - BEFORE == CPAGES"; then
9555                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9556         fi
9557
9558         # the following read invalidates the cache
9559         cancel_lru_locks osc
9560         set_osd_param $list '' read_cache_enable 0
9561         cat $DIR/$tfile >/dev/null
9562
9563         # now data shouldn't be found in the cache
9564         BEFORE=$(roc_hit)
9565         cancel_lru_locks osc
9566         cat $DIR/$tfile >/dev/null
9567         AFTER=$(roc_hit)
9568         if let "AFTER - BEFORE != 0"; then
9569                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9570         fi
9571
9572         set_osd_param $list '' read_cache_enable 1
9573         rm -f $DIR/$tfile
9574 }
9575 run_test 151 "test cache on oss and controls ==============================="
9576
9577 test_152() {
9578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9579         local TF="$TMP/$tfile"
9580
9581         # simulate ENOMEM during write
9582 #define OBD_FAIL_OST_NOMEM      0x226
9583         lctl set_param fail_loc=0x80000226
9584         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9585         cp $TF $DIR/$tfile
9586         sync || error "sync failed"
9587         lctl set_param fail_loc=0
9588
9589         # discard client's cache
9590         cancel_lru_locks osc
9591
9592         # simulate ENOMEM during read
9593         lctl set_param fail_loc=0x80000226
9594         cmp $TF $DIR/$tfile || error "cmp failed"
9595         lctl set_param fail_loc=0
9596
9597         rm -f $TF
9598 }
9599 run_test 152 "test read/write with enomem ============================"
9600
9601 test_153() {
9602         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9603 }
9604 run_test 153 "test if fdatasync does not crash ======================="
9605
9606 dot_lustre_fid_permission_check() {
9607         local fid=$1
9608         local ffid=$MOUNT/.lustre/fid/$fid
9609         local test_dir=$2
9610
9611         echo "stat fid $fid"
9612         stat $ffid > /dev/null || error "stat $ffid failed."
9613         echo "touch fid $fid"
9614         touch $ffid || error "touch $ffid failed."
9615         echo "write to fid $fid"
9616         cat /etc/hosts > $ffid || error "write $ffid failed."
9617         echo "read fid $fid"
9618         diff /etc/hosts $ffid || error "read $ffid failed."
9619         echo "append write to fid $fid"
9620         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9621         echo "rename fid $fid"
9622         mv $ffid $test_dir/$tfile.1 &&
9623                 error "rename $ffid to $tfile.1 should fail."
9624         touch $test_dir/$tfile.1
9625         mv $test_dir/$tfile.1 $ffid &&
9626                 error "rename $tfile.1 to $ffid should fail."
9627         rm -f $test_dir/$tfile.1
9628         echo "truncate fid $fid"
9629         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9630         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9631                 echo "link fid $fid"
9632                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9633         fi
9634         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9635                 echo "setfacl fid $fid"
9636                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9637                 echo "getfacl fid $fid"
9638                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9639         fi
9640         echo "unlink fid $fid"
9641         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9642         echo "mknod fid $fid"
9643         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9644
9645         fid=[0xf00000400:0x1:0x0]
9646         ffid=$MOUNT/.lustre/fid/$fid
9647
9648         echo "stat non-exist fid $fid"
9649         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9650         echo "write to non-exist fid $fid"
9651         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9652         echo "link new fid $fid"
9653         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9654
9655         mkdir -p $test_dir/$tdir
9656         touch $test_dir/$tdir/$tfile
9657         fid=$($LFS path2fid $test_dir/$tdir)
9658         rc=$?
9659         [ $rc -ne 0 ] &&
9660                 error "error: could not get fid for $test_dir/$dir/$tfile."
9661
9662         ffid=$MOUNT/.lustre/fid/$fid
9663
9664         echo "ls $fid"
9665         ls $ffid > /dev/null || error "ls $ffid failed."
9666         echo "touch $fid/$tfile.1"
9667         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9668
9669         echo "touch $MOUNT/.lustre/fid/$tfile"
9670         touch $MOUNT/.lustre/fid/$tfile && \
9671                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9672
9673         echo "setxattr to $MOUNT/.lustre/fid"
9674         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9675
9676         echo "listxattr for $MOUNT/.lustre/fid"
9677         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9678
9679         echo "delxattr from $MOUNT/.lustre/fid"
9680         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9681
9682         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9683         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9684                 error "touch invalid fid should fail."
9685
9686         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9687         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9688                 error "touch non-normal fid should fail."
9689
9690         echo "rename $tdir to $MOUNT/.lustre/fid"
9691         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9692                 error "rename to $MOUNT/.lustre/fid should fail."
9693
9694         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9695         then            # LU-3547
9696                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9697                 local new_obf_mode=777
9698
9699                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9700                 chmod $new_obf_mode $DIR/.lustre/fid ||
9701                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9702
9703                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9704                 [ $obf_mode -eq $new_obf_mode ] ||
9705                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9706
9707                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9708                 chmod $old_obf_mode $DIR/.lustre/fid ||
9709                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9710         fi
9711
9712         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9713         fid=$($LFS path2fid $test_dir/$tfile-2)
9714
9715         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9716         then # LU-5424
9717                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9718                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9719                         error "create lov data thru .lustre failed"
9720         fi
9721         echo "cp /etc/passwd $test_dir/$tfile-2"
9722         cp /etc/passwd $test_dir/$tfile-2 ||
9723                 error "copy to $test_dir/$tfile-2 failed."
9724         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9725         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9726                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9727
9728         rm -rf $test_dir/tfile.lnk
9729         rm -rf $test_dir/$tfile-2
9730 }
9731
9732 test_154A() {
9733         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9734                 skip "Need MDS version at least 2.4.1" && return
9735
9736         touch $DIR/$tfile
9737         local FID=$($LFS path2fid $DIR/$tfile)
9738         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9739
9740         # check that we get the same pathname back
9741         local FOUND=$($LFS fid2path $MOUNT $FID)
9742         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9743         [ "$FOUND" != "$DIR/$tfile" ] &&
9744                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9745
9746         rm -rf $DIR/$tfile
9747 }
9748 run_test 154A "lfs path2fid and fid2path basic checks"
9749
9750 test_154a() {
9751         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9752         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9753                 { skip "Need MDS version at least 2.2.51"; return 0; }
9754         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9755
9756         cp /etc/hosts $DIR/$tfile
9757
9758         fid=$($LFS path2fid $DIR/$tfile)
9759         rc=$?
9760         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9761
9762         dot_lustre_fid_permission_check "$fid" $DIR ||
9763                 error "dot lustre permission check $fid failed"
9764
9765         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9766
9767         touch $MOUNT/.lustre/file &&
9768                 error "creation is not allowed under .lustre"
9769
9770         mkdir $MOUNT/.lustre/dir &&
9771                 error "mkdir is not allowed under .lustre"
9772
9773         rm -rf $DIR/$tfile
9774 }
9775 run_test 154a "Open-by-FID"
9776
9777 test_154b() {
9778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9779         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9780                 { skip "Need MDS version at least 2.2.51"; return 0; }
9781
9782         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9783
9784         local remote_dir=$DIR/$tdir/remote_dir
9785         local MDTIDX=1
9786         local rc=0
9787
9788         mkdir -p $DIR/$tdir
9789         $LFS mkdir -i $MDTIDX $remote_dir ||
9790                 error "create remote directory failed"
9791
9792         cp /etc/hosts $remote_dir/$tfile
9793
9794         fid=$($LFS path2fid $remote_dir/$tfile)
9795         rc=$?
9796         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9797
9798         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9799                 error "dot lustre permission check $fid failed"
9800         rm -rf $DIR/$tdir
9801 }
9802 run_test 154b "Open-by-FID for remote directory"
9803
9804 test_154c() {
9805         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9806                 skip "Need MDS version at least 2.4.1" && return
9807
9808         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9809         local FID1=$($LFS path2fid $DIR/$tfile.1)
9810         local FID2=$($LFS path2fid $DIR/$tfile.2)
9811         local FID3=$($LFS path2fid $DIR/$tfile.3)
9812
9813         local N=1
9814         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9815                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9816                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9817                 local want=FID$N
9818                 [ "$FID" = "${!want}" ] ||
9819                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9820                 N=$((N + 1))
9821         done
9822
9823         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9824                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9825                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9826                 N=$((N + 1))
9827         done
9828 }
9829 run_test 154c "lfs path2fid and fid2path multiple arguments"
9830
9831 test_154d() {
9832         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9833                 skip "Need MDS version at least 2.5.53" && return
9834
9835         if remote_mds; then
9836                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9837         else
9838                 nid="0@lo"
9839         fi
9840         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9841         local fd
9842         local cmd
9843
9844         rm -f $DIR/$tfile
9845         touch $DIR/$tfile
9846
9847         fid=$($LFS path2fid $DIR/$tfile)
9848         # Open the file
9849         fd=$(free_fd)
9850         cmd="exec $fd<$DIR/$tfile"
9851         eval $cmd
9852         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9853         echo $fid_list | grep $fid
9854         rc=$?
9855
9856         cmd="exec $fd>/dev/null"
9857         eval $cmd
9858         if [ $rc -ne 0 ]; then
9859                 error "FID $fid not found in open files list $fid_list"
9860         fi
9861 }
9862 run_test 154d "Verify open file fid"
9863
9864 test_154e()
9865 {
9866         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9867                 skip "Need MDS version at least 2.6.50" && return
9868
9869         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9870                 error ".lustre returned by readdir"
9871         fi
9872 }
9873 run_test 154e ".lustre is not returned by readdir"
9874
9875 test_154f() {
9876         # create parent directory on a single MDT to avoid cross-MDT hardlinks
9877         test_mkdir -p -c1 $DIR/$tdir/d
9878         # test dirs inherit from its stripe
9879         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
9880         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
9881         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
9882         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
9883         touch $DIR/f
9884
9885         # get fid of parents
9886         local FID0=$($LFS path2fid $DIR/$tdir/d)
9887         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
9888         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
9889         local FID3=$($LFS path2fid $DIR)
9890
9891         # check that path2fid --parents returns expected <parent_fid>/name
9892         # 1) test for a directory (single parent)
9893         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
9894         [ "$parent" == "$FID0/foo1" ] ||
9895                 error "expected parent: $FID0/foo1, got: $parent"
9896
9897         # 2) test for a file with nlink > 1 (multiple parents)
9898         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
9899         echo "$parent" | grep -F "$FID1/$tfile" ||
9900                 error "$FID1/$tfile not returned in parent list"
9901         echo "$parent" | grep -F "$FID2/link" ||
9902                 error "$FID2/link not returned in parent list"
9903
9904         # 3) get parent by fid
9905         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
9906         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9907         echo "$parent" | grep -F "$FID1/$tfile" ||
9908                 error "$FID1/$tfile not returned in parent list (by fid)"
9909         echo "$parent" | grep -F "$FID2/link" ||
9910                 error "$FID2/link not returned in parent list (by fid)"
9911
9912         # 4) test for entry in root directory
9913         parent=$($LFS path2fid --parents $DIR/f)
9914         echo "$parent" | grep -F "$FID3/f" ||
9915                 error "$FID3/f not returned in parent list"
9916
9917         # 5) test it on root directory
9918         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
9919                 error "$MOUNT should not have parents"
9920
9921         # enable xattr caching and check that linkea is correctly updated
9922         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9923         save_lustre_params client "llite.*.xattr_cache" > $save
9924         lctl set_param llite.*.xattr_cache 1
9925
9926         # 6.1) linkea update on rename
9927         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
9928
9929         # get parents by fid
9930         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9931         # foo1 should no longer be returned in parent list
9932         echo "$parent" | grep -F "$FID1" &&
9933                 error "$FID1 should no longer be in parent list"
9934         # the new path should appear
9935         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
9936                 error "$FID2/$tfile.moved is not in parent list"
9937
9938         # 6.2) linkea update on unlink
9939         rm -f $DIR/$tdir/d/foo2/link
9940         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9941         # foo2/link should no longer be returned in parent list
9942         echo "$parent" | grep -F "$FID2/link" &&
9943                 error "$FID2/link should no longer be in parent list"
9944         true
9945
9946         rm -f $DIR/f
9947         restore_lustre_params < $save
9948 }
9949 run_test 154f "get parent fids by reading link ea"
9950
9951 test_155_small_load() {
9952     local temp=$TMP/$tfile
9953     local file=$DIR/$tfile
9954
9955     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9956         error "dd of=$temp bs=6096 count=1 failed"
9957     cp $temp $file
9958     cancel_lru_locks osc
9959     cmp $temp $file || error "$temp $file differ"
9960
9961     $TRUNCATE $temp 6000
9962     $TRUNCATE $file 6000
9963     cmp $temp $file || error "$temp $file differ (truncate1)"
9964
9965     echo "12345" >>$temp
9966     echo "12345" >>$file
9967     cmp $temp $file || error "$temp $file differ (append1)"
9968
9969     echo "12345" >>$temp
9970     echo "12345" >>$file
9971     cmp $temp $file || error "$temp $file differ (append2)"
9972
9973     rm -f $temp $file
9974     true
9975 }
9976
9977 test_155_big_load() {
9978     remote_ost_nodsh && skip "remote OST with nodsh" && return
9979     local temp=$TMP/$tfile
9980     local file=$DIR/$tfile
9981
9982     free_min_max
9983     local cache_size=$(do_facet ost$((MAXI+1)) \
9984         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9985     local large_file_size=$((cache_size * 2))
9986
9987     echo "OSS cache size: $cache_size KB"
9988     echo "Large file size: $large_file_size KB"
9989
9990     [ $MAXV -le $large_file_size ] && \
9991         skip_env "max available OST size needs > $large_file_size KB" && \
9992         return 0
9993
9994     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9995
9996     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9997         error "dd of=$temp bs=$large_file_size count=1k failed"
9998     cp $temp $file
9999     ls -lh $temp $file
10000     cancel_lru_locks osc
10001     cmp $temp $file || error "$temp $file differ"
10002
10003     rm -f $temp $file
10004     true
10005 }
10006
10007 test_155a() {
10008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10009         set_cache read on
10010         set_cache writethrough on
10011         test_155_small_load
10012 }
10013 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10014
10015 test_155b() {
10016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10017         set_cache read on
10018         set_cache writethrough off
10019         test_155_small_load
10020 }
10021 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10022
10023 test_155c() {
10024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10025         set_cache read off
10026         set_cache writethrough on
10027         test_155_small_load
10028 }
10029 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10030
10031 test_155d() {
10032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10033         set_cache read off
10034         set_cache writethrough off
10035         test_155_small_load
10036 }
10037 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10038
10039 test_155e() {
10040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10041         set_cache read on
10042         set_cache writethrough on
10043         test_155_big_load
10044 }
10045 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10046
10047 test_155f() {
10048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10049         set_cache read on
10050         set_cache writethrough off
10051         test_155_big_load
10052 }
10053 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10054
10055 test_155g() {
10056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10057         set_cache read off
10058         set_cache writethrough on
10059         test_155_big_load
10060 }
10061 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10062
10063 test_155h() {
10064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10065         set_cache read off
10066         set_cache writethrough off
10067         test_155_big_load
10068 }
10069 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10070
10071 test_156() {
10072         remote_ost_nodsh && skip "remote OST with nodsh" && return
10073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10074         local CPAGES=3
10075         local BEFORE
10076         local AFTER
10077         local file="$DIR/$tfile"
10078
10079         [ "$(facet_fstype ost1)" = "zfs" ] &&
10080                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
10081                 return
10082
10083         roc_hit_init
10084
10085     log "Turn on read and write cache"
10086     set_cache read on
10087     set_cache writethrough on
10088
10089     log "Write data and read it back."
10090     log "Read should be satisfied from the cache."
10091     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10092     BEFORE=`roc_hit`
10093     cancel_lru_locks osc
10094     cat $file >/dev/null
10095     AFTER=`roc_hit`
10096     if ! let "AFTER - BEFORE == CPAGES"; then
10097         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10098     else
10099         log "cache hits:: before: $BEFORE, after: $AFTER"
10100     fi
10101
10102     log "Read again; it should be satisfied from the cache."
10103     BEFORE=$AFTER
10104     cancel_lru_locks osc
10105     cat $file >/dev/null
10106     AFTER=`roc_hit`
10107     if ! let "AFTER - BEFORE == CPAGES"; then
10108         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10109     else
10110         log "cache hits:: before: $BEFORE, after: $AFTER"
10111     fi
10112
10113
10114     log "Turn off the read cache and turn on the write cache"
10115     set_cache read off
10116     set_cache writethrough on
10117
10118     log "Read again; it should be satisfied from the cache."
10119     BEFORE=`roc_hit`
10120     cancel_lru_locks osc
10121     cat $file >/dev/null
10122     AFTER=`roc_hit`
10123     if ! let "AFTER - BEFORE == CPAGES"; then
10124         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10125     else
10126         log "cache hits:: before: $BEFORE, after: $AFTER"
10127     fi
10128
10129     log "Read again; it should not be satisfied from the cache."
10130     BEFORE=$AFTER
10131     cancel_lru_locks osc
10132     cat $file >/dev/null
10133     AFTER=`roc_hit`
10134     if ! let "AFTER - BEFORE == 0"; then
10135         error "IN CACHE: before: $BEFORE, after: $AFTER"
10136     else
10137         log "cache hits:: before: $BEFORE, after: $AFTER"
10138     fi
10139
10140     log "Write data and read it back."
10141     log "Read should be satisfied from the cache."
10142     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10143     BEFORE=`roc_hit`
10144     cancel_lru_locks osc
10145     cat $file >/dev/null
10146     AFTER=`roc_hit`
10147     if ! let "AFTER - BEFORE == CPAGES"; then
10148         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10149     else
10150         log "cache hits:: before: $BEFORE, after: $AFTER"
10151     fi
10152
10153     log "Read again; it should not be satisfied from the cache."
10154     BEFORE=$AFTER
10155     cancel_lru_locks osc
10156     cat $file >/dev/null
10157     AFTER=`roc_hit`
10158     if ! let "AFTER - BEFORE == 0"; then
10159         error "IN CACHE: before: $BEFORE, after: $AFTER"
10160     else
10161         log "cache hits:: before: $BEFORE, after: $AFTER"
10162     fi
10163
10164
10165     log "Turn off read and write cache"
10166     set_cache read off
10167     set_cache writethrough off
10168
10169     log "Write data and read it back"
10170     log "It should not be satisfied from the cache."
10171     rm -f $file
10172     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10173     cancel_lru_locks osc
10174     BEFORE=`roc_hit`
10175     cat $file >/dev/null
10176     AFTER=`roc_hit`
10177         if ! let "AFTER - BEFORE == 0"; then
10178                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10179         else
10180                 log "cache hits:: before: $BEFORE, after: $AFTER"
10181         fi
10182
10183     log "Turn on the read cache and turn off the write cache"
10184     set_cache read on
10185     set_cache writethrough off
10186
10187     log "Write data and read it back"
10188     log "It should not be satisfied from the cache."
10189     rm -f $file
10190     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10191     BEFORE=`roc_hit`
10192     cancel_lru_locks osc
10193     cat $file >/dev/null
10194     AFTER=`roc_hit`
10195         if ! let "AFTER - BEFORE == 0"; then
10196                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10197         else
10198                 log "cache hits:: before: $BEFORE, after: $AFTER"
10199         fi
10200
10201     log "Read again; it should be satisfied from the cache."
10202     BEFORE=`roc_hit`
10203     cancel_lru_locks osc
10204     cat $file >/dev/null
10205     AFTER=`roc_hit`
10206     if ! let "AFTER - BEFORE == CPAGES"; then
10207         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10208     else
10209         log "cache hits:: before: $BEFORE, after: $AFTER"
10210     fi
10211
10212     rm -f $file
10213 }
10214 run_test 156 "Verification of tunables ============================"
10215
10216 #Changelogs
10217 err17935 () {
10218         if [[ $MDSCOUNT -gt 1 ]]; then
10219                 error_ignore bz17935 $*
10220         else
10221                 error $*
10222         fi
10223 }
10224
10225 changelog_chmask()
10226 {
10227         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10228
10229         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10230
10231         if [ $MASK -eq 1 ]; then
10232                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10233         else
10234                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10235         fi
10236 }
10237
10238 changelog_extract_field() {
10239         local mdt=$1
10240         local cltype=$2
10241         local file=$3
10242         local identifier=$4
10243
10244         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10245                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10246                 tail -1
10247 }
10248
10249 test_160a() {
10250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10251         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10252         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10253                 { skip "Need MDS version at least 2.2.0"; return; }
10254
10255         local CL_USERS="mdd.$MDT0.changelog_users"
10256         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10257         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10258         echo "Registered as changelog user $USER"
10259         $GET_CL_USERS | grep -q $USER ||
10260                 error "User $USER not found in changelog_users"
10261
10262         # change something
10263         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10264         touch $DIR/$tdir/pics/2008/zachy/timestamp
10265         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10266         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10267         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10268         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10269         rm $DIR/$tdir/pics/desktop.jpg
10270
10271         $LFS changelog $MDT0 | tail -5
10272
10273         echo "verifying changelog mask"
10274         changelog_chmask "MKDIR"
10275         changelog_chmask "CLOSE"
10276
10277         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10278         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10279
10280         changelog_chmask "MKDIR"
10281         changelog_chmask "CLOSE"
10282
10283         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10284         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10285
10286         $LFS changelog $MDT0
10287         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10288         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10289         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10290         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10291
10292         # verify contents
10293         echo "verifying target fid"
10294         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10295         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10296         [ "$fidc" == "$fidf" ] ||
10297                 err17935 "fid in changelog $fidc != file fid $fidf"
10298         echo "verifying parent fid"
10299         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10300         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10301         [ "$fidc" == "$fidf" ] ||
10302                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10303
10304         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10305         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10306         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10307         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10308         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10309                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10310
10311         MIN_REC=$($GET_CL_USERS |
10312                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10313         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10314         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10315         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10316                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10317
10318         # LU-3446 changelog index reset on MDT restart
10319         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10320         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10321         $LFS changelog_clear $MDT0 $USER 0
10322         stop $SINGLEMDS || error "Fail to stop MDT."
10323         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10324         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10325         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10326         [ $CUR_REC1 == $CUR_REC2 ] ||
10327                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10328
10329         echo "verifying user deregister"
10330         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10331         $GET_CL_USERS | grep -q $USER &&
10332                 error "User $USER still in changelog_users"
10333
10334         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10335         if [ $USERS -eq 0 ]; then
10336                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10337                 touch $DIR/$tdir/chloe
10338                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10339                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10340                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10341         else
10342                 echo "$USERS other changelog users; can't verify off"
10343         fi
10344 }
10345 run_test 160a "changelog sanity"
10346
10347 test_160b() { # LU-3587
10348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10349         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10350         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10351                 { skip "Need MDS version at least 2.2.0"; return; }
10352
10353         local CL_USERS="mdd.$MDT0.changelog_users"
10354         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10355         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10356         echo "Registered as changelog user $USER"
10357         $GET_CL_USERS | grep -q $USER ||
10358                 error "User $USER not found in changelog_users"
10359
10360         local LONGNAME1=$(str_repeat a 255)
10361         local LONGNAME2=$(str_repeat b 255)
10362
10363         cd $DIR
10364         echo "creating very long named file"
10365         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10366         echo "moving very long named file"
10367         mv $LONGNAME1 $LONGNAME2
10368
10369         $LFS changelog $MDT0 | grep RENME
10370
10371         echo "deregistering $USER"
10372         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10373
10374         rm -f $LONGNAME2
10375 }
10376 run_test 160b "Verify that very long rename doesn't crash in changelog"
10377
10378 test_160c() {
10379         local rc=0
10380         local server_version=$(lustre_version_code $SINGLEMDS)
10381
10382         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10383                 [[ $server_version -gt $(version_code 2.5.1) &&
10384                    $server_version -lt $(version_code 2.5.50) ]] ||
10385                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10386         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10387
10388         # Registration step
10389         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10390                 changelog_register -n)
10391
10392         rm -rf $DIR/$tdir
10393         mkdir -p $DIR/$tdir
10394         $MCREATE $DIR/$tdir/foo_160c
10395         changelog_chmask "TRUNC"
10396         $TRUNCATE $DIR/$tdir/foo_160c 200
10397         changelog_chmask "TRUNC"
10398         $TRUNCATE $DIR/$tdir/foo_160c 199
10399         $LFS changelog $MDT0
10400         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10401         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10402         $LFS changelog_clear $MDT0 $USER 0
10403
10404         # Deregistration step
10405         echo "deregistering $USER"
10406         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10407 }
10408 run_test 160c "verify that changelog log catch the truncate event"
10409
10410 test_161a() {
10411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10412         test_mkdir -p -c1 $DIR/$tdir
10413         cp /etc/hosts $DIR/$tdir/$tfile
10414         test_mkdir -c1 $DIR/$tdir/foo1
10415         test_mkdir -c1 $DIR/$tdir/foo2
10416         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10417         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10418         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10419         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10420         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10421         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10422                 $LFS fid2path $DIR $FID
10423                 err17935 "bad link ea"
10424         fi
10425     # middle
10426     rm $DIR/$tdir/foo2/zachary
10427     # last
10428     rm $DIR/$tdir/foo2/thor
10429     # first
10430     rm $DIR/$tdir/$tfile
10431     # rename
10432     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10433     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10434         then
10435         $LFS fid2path $DIR $FID
10436         err17935 "bad link rename"
10437     fi
10438     rm $DIR/$tdir/foo2/maggie
10439
10440         # overflow the EA
10441         local longname=filename_avg_len_is_thirty_two_
10442         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10443                 error "failed to hardlink many files"
10444         links=$($LFS fid2path $DIR $FID | wc -l)
10445         echo -n "${links}/1000 links in link EA"
10446         [[ $links -gt 60 ]] ||
10447                 err17935 "expected at least 60 links in link EA"
10448         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10449                 error "failed to unlink many hardlinks"
10450 }
10451 run_test 161a "link ea sanity"
10452
10453 test_161b() {
10454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10455         [ $MDSCOUNT -lt 2 ] &&
10456                 skip "skipping remote directory test" && return
10457         local MDTIDX=1
10458         local remote_dir=$DIR/$tdir/remote_dir
10459
10460         mkdir -p $DIR/$tdir
10461         $LFS mkdir -i $MDTIDX $remote_dir ||
10462                 error "create remote directory failed"
10463
10464         cp /etc/hosts $remote_dir/$tfile
10465         mkdir -p $remote_dir/foo1
10466         mkdir -p $remote_dir/foo2
10467         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10468         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10469         ln $remote_dir/$tfile $remote_dir/foo1/luna
10470         ln $remote_dir/$tfile $remote_dir/foo2/thor
10471
10472         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10473                      tr -d ']')
10474         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10475                 $LFS fid2path $DIR $FID
10476                 err17935 "bad link ea"
10477         fi
10478         # middle
10479         rm $remote_dir/foo2/zachary
10480         # last
10481         rm $remote_dir/foo2/thor
10482         # first
10483         rm $remote_dir/$tfile
10484         # rename
10485         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10486         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10487         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10488                 $LFS fid2path $DIR $FID
10489                 err17935 "bad link rename"
10490         fi
10491         rm $remote_dir/foo2/maggie
10492
10493         # overflow the EA
10494         local longname=filename_avg_len_is_thirty_two_
10495         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10496                 error "failed to hardlink many files"
10497         links=$($LFS fid2path $DIR $FID | wc -l)
10498         echo -n "${links}/1000 links in link EA"
10499         [[ ${links} -gt 60 ]] ||
10500                 err17935 "expected at least 60 links in link EA"
10501         unlinkmany $remote_dir/foo2/$longname 1000 ||
10502         error "failed to unlink many hardlinks"
10503 }
10504 run_test 161b "link ea sanity under remote directory"
10505
10506 test_161c() {
10507         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10508         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10509         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10510                 skip "Need MDS version at least 2.1.5" && return
10511
10512         # define CLF_RENAME_LAST 0x0001
10513         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10514         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10515                 changelog_register -n)
10516         rm -rf $DIR/$tdir
10517         mkdir -p $DIR/$tdir
10518         touch $DIR/$tdir/foo_161c
10519         touch $DIR/$tdir/bar_161c
10520         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10521         $LFS changelog $MDT0 | grep RENME
10522         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10523                 cut -f5 -d' ')
10524         $LFS changelog_clear $MDT0 $USER 0
10525         if [ x$flags != "x0x1" ]; then
10526                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10527                         $USER
10528                 error "flag $flags is not 0x1"
10529         fi
10530         echo "rename overwrite a target having nlink = 1," \
10531                 "changelog record has flags of $flags"
10532
10533         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10534         touch $DIR/$tdir/foo_161c
10535         touch $DIR/$tdir/bar_161c
10536         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10537         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10538         $LFS changelog $MDT0 | grep RENME
10539         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10540         $LFS changelog_clear $MDT0 $USER 0
10541         if [ x$flags != "x0x0" ]; then
10542                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10543                         $USER
10544                 error "flag $flags is not 0x0"
10545         fi
10546         echo "rename overwrite a target having nlink > 1," \
10547                 "changelog record has flags of $flags"
10548
10549         # rename doesn't overwrite a target (changelog flag 0x0)
10550         touch $DIR/$tdir/foo_161c
10551         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10552         $LFS changelog $MDT0 | grep RENME
10553         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10554         $LFS changelog_clear $MDT0 $USER 0
10555         if [ x$flags != "x0x0" ]; then
10556                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10557                         $USER
10558                 error "flag $flags is not 0x0"
10559         fi
10560         echo "rename doesn't overwrite a target," \
10561                 "changelog record has flags of $flags"
10562
10563         # define CLF_UNLINK_LAST 0x0001
10564         # unlink a file having nlink = 1 (changelog flag 0x1)
10565         rm -f $DIR/$tdir/foo2_161c
10566         $LFS changelog $MDT0 | grep UNLNK
10567         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10568         $LFS changelog_clear $MDT0 $USER 0
10569         if [ x$flags != "x0x1" ]; then
10570                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10571                         $USER
10572                 error "flag $flags is not 0x1"
10573         fi
10574         echo "unlink a file having nlink = 1," \
10575                 "changelog record has flags of $flags"
10576
10577         # unlink a file having nlink > 1 (changelog flag 0x0)
10578         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10579         rm -f $DIR/$tdir/foobar_161c
10580         $LFS changelog $MDT0 | grep UNLNK
10581         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10582         $LFS changelog_clear $MDT0 $USER 0
10583         if [ x$flags != "x0x0" ]; then
10584                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10585                         $USER
10586                 error "flag $flags is not 0x0"
10587         fi
10588         echo "unlink a file having nlink > 1," \
10589                 "changelog record has flags of $flags"
10590         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10591 }
10592 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10593
10594 check_path() {
10595     local expected=$1
10596     shift
10597     local fid=$2
10598
10599     local path=$(${LFS} fid2path $*)
10600     # Remove the '//' indicating a remote directory
10601     path=$(echo $path | sed 's#//#/#g')
10602     RC=$?
10603
10604     if [ $RC -ne 0 ]; then
10605         err17935 "path looked up of $expected failed. Error $RC"
10606         return $RC
10607     elif [ "${path}" != "${expected}" ]; then
10608         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10609         return 2
10610     fi
10611     echo "fid $fid resolves to path $path (expected $expected)"
10612 }
10613
10614 test_162() {
10615         # Make changes to filesystem
10616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10617         test_mkdir -p -c1 $DIR/$tdir/d2
10618         touch $DIR/$tdir/d2/$tfile
10619         touch $DIR/$tdir/d2/x1
10620         touch $DIR/$tdir/d2/x2
10621         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10622         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10623         # regular file
10624         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10625         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10626                 error "check path $tdir/d2/$tfile failed"
10627
10628         # softlink
10629         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10630         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10631         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10632                 error "check path $tdir/d2/p/q/r/slink failed"
10633
10634         # softlink to wrong file
10635         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10636         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10637         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10638                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10639
10640         # hardlink
10641         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10642         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10643         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10644         # fid2path dir/fsname should both work
10645         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10646                 error "check path $tdir/d2/a/b/c/new_file failed"
10647         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10648                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10649
10650         # hardlink count: check that there are 2 links
10651         # Doesnt work with CMD yet: 17935
10652         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10653                 err17935 "expected 2 links"
10654
10655         # hardlink indexing: remove the first link
10656         rm $DIR/$tdir/d2/p/q/r/hlink
10657         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10658                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10659
10660         return 0
10661 }
10662 run_test 162 "path lookup sanity"
10663
10664 test_162b() {
10665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10666         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10667
10668         mkdir $DIR/$tdir
10669         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10670                                 error "create striped dir failed"
10671
10672         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10673                                         tail -n 1 | awk '{print $2}')
10674         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10675
10676         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10677         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10678
10679         # regular file
10680         for ((i=0;i<5;i++)); do
10681                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10682                         error "get fid for f$i failed"
10683                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10684                         error "check path $tdir/striped_dir/f$i failed"
10685
10686                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10687                         error "get fid for d$i failed"
10688                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10689                         error "check path $tdir/striped_dir/d$i failed"
10690         done
10691
10692         return 0
10693 }
10694 run_test 162b "striped directory path lookup sanity"
10695
10696 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10697 test_162c() {
10698         test_mkdir $DIR/$tdir.local
10699         test_mkdir $DIR/$tdir.remote
10700         local lpath=$tdir.local
10701         local rpath=$tdir.remote
10702
10703         for ((i = 0; i <= 101; i++)); do
10704                 lpath="$lpath/$i"
10705                 mkdir $DIR/$lpath
10706                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10707                         error "get fid for local directory $DIR/$lpath failed"
10708                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10709                         error "check path for local directory $DIR/$lpath failed"
10710
10711                 rpath="$rpath/$i"
10712                 test_mkdir $DIR/$rpath
10713                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10714                         error "get fid for remote directory $DIR/$rpath failed"
10715                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10716                         error "check path for remote directory $DIR/$rpath failed"
10717         done
10718
10719         return 0
10720 }
10721 run_test 162c "fid2path works with paths 100 or more directories deep"
10722
10723 test_169() {
10724         # do directio so as not to populate the page cache
10725         log "creating a 10 Mb file"
10726         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10727         log "starting reads"
10728         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10729         log "truncating the file"
10730         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10731         log "killing dd"
10732         kill %+ || true # reads might have finished
10733         echo "wait until dd is finished"
10734         wait
10735         log "removing the temporary file"
10736         rm -rf $DIR/$tfile || error "tmp file removal failed"
10737 }
10738 run_test 169 "parallel read and truncate should not deadlock"
10739
10740 test_170() {
10741         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10742         $LCTL clear     # bug 18514
10743         $LCTL debug_daemon start $TMP/${tfile}_log_good
10744         touch $DIR/$tfile
10745         $LCTL debug_daemon stop
10746         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10747                error "sed failed to read log_good"
10748
10749         $LCTL debug_daemon start $TMP/${tfile}_log_good
10750         rm -rf $DIR/$tfile
10751         $LCTL debug_daemon stop
10752
10753         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10754                error "lctl df log_bad failed"
10755
10756         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10757         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10758
10759         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10760         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10761
10762         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10763                 error "bad_line good_line1 good_line2 are empty"
10764
10765         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10766         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10767         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10768
10769         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10770         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10771         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10772
10773         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10774                 error "bad_line_new good_line_new are empty"
10775
10776         local expected_good=$((good_line1 + good_line2*2))
10777
10778         rm -f $TMP/${tfile}*
10779         # LU-231, short malformed line may not be counted into bad lines
10780         if [ $bad_line -ne $bad_line_new ] &&
10781                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10782                 error "expected $bad_line bad lines, but got $bad_line_new"
10783                 return 1
10784         fi
10785
10786         if [ $expected_good -ne $good_line_new ]; then
10787                 error "expected $expected_good good lines, but got $good_line_new"
10788                 return 2
10789         fi
10790         true
10791 }
10792 run_test 170 "test lctl df to handle corrupted log ====================="
10793
10794 test_171() { # bug20592
10795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10796 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10797         $LCTL set_param fail_loc=0x50e
10798         $LCTL set_param fail_val=3000
10799         multiop_bg_pause $DIR/$tfile O_s || true
10800         local MULTIPID=$!
10801         kill -USR1 $MULTIPID
10802         # cause log dump
10803         sleep 3
10804         wait $MULTIPID
10805         if dmesg | grep "recursive fault"; then
10806                 error "caught a recursive fault"
10807         fi
10808         $LCTL set_param fail_loc=0
10809         true
10810 }
10811 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10812
10813 # it would be good to share it with obdfilter-survey/iokit-libecho code
10814 setup_obdecho_osc () {
10815         local rc=0
10816         local ost_nid=$1
10817         local obdfilter_name=$2
10818         echo "Creating new osc for $obdfilter_name on $ost_nid"
10819         # make sure we can find loopback nid
10820         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10821
10822         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10823                            ${obdfilter_name}_osc_UUID || rc=2; }
10824         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10825                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10826         return $rc
10827 }
10828
10829 cleanup_obdecho_osc () {
10830         local obdfilter_name=$1
10831         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10832         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10833         return 0
10834 }
10835
10836 obdecho_test() {
10837         local OBD=$1
10838         local node=$2
10839         local pages=${3:-64}
10840         local rc=0
10841         local id
10842
10843         local count=10
10844         local obd_size=$(get_obd_size $node $OBD)
10845         local page_size=$(get_page_size $node)
10846         if [[ -n "$obd_size" ]]; then
10847                 local new_count=$((obd_size / (pages * page_size / 1024)))
10848                 [[ $new_count -ge $count ]] || count=$new_count
10849         fi
10850
10851         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10852         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10853                            rc=2; }
10854         if [ $rc -eq 0 ]; then
10855             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10856             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10857         fi
10858         echo "New object id is $id"
10859         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10860                            rc=4; }
10861         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10862                            "test_brw $count w v $pages $id" || rc=4; }
10863         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10864                            rc=4; }
10865         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10866                                         "cleanup" || rc=5; }
10867         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10868                                         "detach" || rc=6; }
10869         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10870         return $rc
10871 }
10872
10873 test_180a() {
10874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10875         remote_ost_nodsh && skip "remote OST with nodsh" && return
10876         local rc=0
10877         local rmmod_local=0
10878
10879         if ! module_loaded obdecho; then
10880             load_module obdecho/obdecho
10881             rmmod_local=1
10882         fi
10883
10884         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10885         local host=$(lctl get_param -n osc.$osc.import |
10886                              awk '/current_connection:/ {print $2}' )
10887         local target=$(lctl get_param -n osc.$osc.import |
10888                              awk '/target:/ {print $2}' )
10889         target=${target%_UUID}
10890
10891         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10892         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10893         [[ -n $target ]] && cleanup_obdecho_osc $target
10894         [ $rmmod_local -eq 1 ] && rmmod obdecho
10895         return $rc
10896 }
10897 run_test 180a "test obdecho on osc"
10898
10899 test_180b() {
10900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10901         remote_ost_nodsh && skip "remote OST with nodsh" && return
10902         local rc=0
10903         local rmmod_remote=0
10904
10905         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10906                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10907                       "modprobe obdecho; }" && rmmod_remote=1
10908         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10909         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10910         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10911         return $rc
10912 }
10913 run_test 180b "test obdecho directly on obdfilter"
10914
10915 test_180c() { # LU-2598
10916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10917         remote_ost_nodsh && skip "remote OST with nodsh" && return
10918         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10919                 skip "Need MDS version at least 2.4.0" && return
10920
10921         local rc=0
10922         local rmmod_remote=false
10923         local pages=16384 # 64MB bulk I/O RPC size
10924         local target
10925
10926         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10927                 rmmod_remote=true || error "failed to load module obdecho"
10928
10929         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10930                 head -n1)
10931         if [ -n "$target" ]; then
10932                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10933         else
10934                 echo "there is no obdfilter target on ost1"
10935                 rc=2
10936         fi
10937         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10938         return $rc
10939 }
10940 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10941
10942 test_181() { # bug 22177
10943         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10944         # create enough files to index the directory
10945         createmany -o $DIR/$tdir/foobar 4000
10946         # print attributes for debug purpose
10947         lsattr -d .
10948         # open dir
10949         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10950         MULTIPID=$!
10951         # remove the files & current working dir
10952         unlinkmany $DIR/$tdir/foobar 4000
10953         rmdir $DIR/$tdir
10954         kill -USR1 $MULTIPID
10955         wait $MULTIPID
10956         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10957         return 0
10958 }
10959 run_test 181 "Test open-unlinked dir ========================"
10960
10961 test_182() {
10962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10963         # disable MDC RPC lock wouldn't crash client
10964         local fcount=1000
10965         local tcount=4
10966
10967         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10968 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10969         $LCTL set_param fail_loc=0x804
10970
10971         for (( i=0; i < $tcount; i++ )) ; do
10972                 mkdir $DIR/$tdir/$i
10973                 createmany -o $DIR/$tdir/$i/f- $fcount &
10974         done
10975         wait
10976
10977         for (( i=0; i < $tcount; i++ )) ; do
10978                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10979         done
10980         wait
10981
10982         rm -rf $DIR/$tdir
10983
10984         $LCTL set_param fail_loc=0
10985 }
10986 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10987
10988 test_183() { # LU-2275
10989         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10990         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10991                 skip "Need MDS version at least 2.3.56" && return
10992
10993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10994         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10995         echo aaa > $DIR/$tdir/$tfile
10996
10997 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10998         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10999
11000         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11001         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11002
11003         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11004
11005         # Flush negative dentry cache
11006         touch $DIR/$tdir/$tfile
11007
11008         # We are not checking for any leaked references here, they'll
11009         # become evident next time we do cleanup with module unload.
11010         rm -rf $DIR/$tdir
11011 }
11012 run_test 183 "No crash or request leak in case of strange dispositions ========"
11013
11014 # test suite 184 is for LU-2016, LU-2017
11015 test_184a() {
11016         check_swap_layouts_support && return 0
11017
11018         dir0=$DIR/$tdir/$testnum
11019         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11020         ref1=/etc/passwd
11021         ref2=/etc/group
11022         file1=$dir0/f1
11023         file2=$dir0/f2
11024         $SETSTRIPE -c1 $file1
11025         cp $ref1 $file1
11026         $SETSTRIPE -c2 $file2
11027         cp $ref2 $file2
11028         gen1=$($GETSTRIPE -g $file1)
11029         gen2=$($GETSTRIPE -g $file2)
11030
11031         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11032         gen=$($GETSTRIPE -g $file1)
11033         [[ $gen1 != $gen ]] ||
11034                 "Layout generation on $file1 does not change"
11035         gen=$($GETSTRIPE -g $file2)
11036         [[ $gen2 != $gen ]] ||
11037                 "Layout generation on $file2 does not change"
11038
11039         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11040         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11041 }
11042 run_test 184a "Basic layout swap"
11043
11044 test_184b() {
11045         check_swap_layouts_support && return 0
11046
11047         dir0=$DIR/$tdir/$testnum
11048         mkdir -p $dir0 || error "creating dir $dir0"
11049         file1=$dir0/f1
11050         file2=$dir0/f2
11051         file3=$dir0/f3
11052         dir1=$dir0/d1
11053         dir2=$dir0/d2
11054         mkdir $dir1 $dir2
11055         $SETSTRIPE -c1 $file1
11056         $SETSTRIPE -c2 $file2
11057         $SETSTRIPE -c1 $file3
11058         chown $RUNAS_ID $file3
11059         gen1=$($GETSTRIPE -g $file1)
11060         gen2=$($GETSTRIPE -g $file2)
11061
11062         $LFS swap_layouts $dir1 $dir2 &&
11063                 error "swap of directories layouts should fail"
11064         $LFS swap_layouts $dir1 $file1 &&
11065                 error "swap of directory and file layouts should fail"
11066         $RUNAS $LFS swap_layouts $file1 $file2 &&
11067                 error "swap of file we cannot write should fail"
11068         $LFS swap_layouts $file1 $file3 &&
11069                 error "swap of file with different owner should fail"
11070         /bin/true # to clear error code
11071 }
11072 run_test 184b "Forbidden layout swap (will generate errors)"
11073
11074 test_184c() {
11075         check_swap_layouts_support && return 0
11076
11077         local dir0=$DIR/$tdir/$testnum
11078         mkdir -p $dir0 || error "creating dir $dir0"
11079
11080         local ref1=$dir0/ref1
11081         local ref2=$dir0/ref2
11082         local file1=$dir0/file1
11083         local file2=$dir0/file2
11084         # create a file large enough for the concurent test
11085         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11086         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11087         echo "ref file size: ref1($(stat -c %s $ref1))," \
11088              "ref2($(stat -c %s $ref2))"
11089
11090         cp $ref2 $file2
11091         dd if=$ref1 of=$file1 bs=16k &
11092         local DD_PID=$!
11093
11094         # Make sure dd starts to copy file
11095         while [ ! -f $file1 ]; do sleep 0.1; done
11096
11097         $LFS swap_layouts $file1 $file2
11098         local rc=$?
11099         wait $DD_PID
11100         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11101         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11102
11103         # how many bytes copied before swapping layout
11104         local copied=$(stat -c %s $file2)
11105         local remaining=$(stat -c %s $ref1)
11106         remaining=$((remaining - copied))
11107         echo "Copied $copied bytes before swapping layout..."
11108
11109         cmp -n $copied $file1 $ref2 | grep differ &&
11110                 error "Content mismatch [0, $copied) of ref2 and file1"
11111         cmp -n $copied $file2 $ref1 ||
11112                 error "Content mismatch [0, $copied) of ref1 and file2"
11113         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11114                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11115
11116         # clean up
11117         rm -f $ref1 $ref2 $file1 $file2
11118 }
11119 run_test 184c "Concurrent write and layout swap"
11120
11121 test_184d() {
11122         check_swap_layouts_support && return 0
11123         [ -z "$(which getfattr 2>/dev/null)" ] &&
11124                 skip "no getfattr command" && return 0
11125
11126         local file1=$DIR/$tdir/$tfile-1
11127         local file2=$DIR/$tdir/$tfile-2
11128         local file3=$DIR/$tdir/$tfile-3
11129         local lovea1
11130         local lovea2
11131
11132         mkdir -p $DIR/$tdir
11133         touch $file1 || error "create $file1 failed"
11134         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11135                 error "create $file2 failed"
11136         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11137                 error "create $file3 failed"
11138         lovea1=$($LFS getstripe $file1 | sed 1d)
11139
11140         $LFS swap_layouts $file2 $file3 ||
11141                 error "swap $file2 $file3 layouts failed"
11142         $LFS swap_layouts $file1 $file2 ||
11143                 error "swap $file1 $file2 layouts failed"
11144
11145         lovea2=$($LFS getstripe $file2 | sed 1d)
11146         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11147
11148         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11149         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11150 }
11151 run_test 184d "allow stripeless layouts swap"
11152
11153 test_184e() {
11154         check_swap_layouts_support && return 0
11155         [ -z "$(which getfattr 2>/dev/null)" ] &&
11156                 skip "no getfattr command" && return 0
11157
11158         local file1=$DIR/$tdir/$tfile-1
11159         local file2=$DIR/$tdir/$tfile-2
11160         local file3=$DIR/$tdir/$tfile-3
11161         local lovea
11162
11163         mkdir -p $DIR/$tdir
11164         touch $file1 || error "create $file1 failed"
11165         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11166                 error "create $file2 failed"
11167         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11168                 error "create $file3 failed"
11169
11170         $LFS swap_layouts $file1 $file2 ||
11171                 error "swap $file1 $file2 layouts failed"
11172
11173         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11174         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11175
11176         echo 123 > $file1 || error "Should be able to write into $file1"
11177
11178         $LFS swap_layouts $file1 $file3 ||
11179                 error "swap $file1 $file3 layouts failed"
11180
11181         echo 123 > $file1 || error "Should be able to write into $file1"
11182
11183         rm -rf $file1 $file2 $file3
11184 }
11185 run_test 184e "Recreate layout after stripeless layout swaps"
11186
11187 test_185() { # LU-2441
11188         # LU-3553 - no volatile file support in old servers
11189         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11190                 { skip "Need MDS version at least 2.3.60"; return 0; }
11191
11192         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11193         touch $DIR/$tdir/spoo
11194         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11195         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11196                 error "cannot create/write a volatile file"
11197         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11198                 error "FID is still valid after close"
11199
11200         multiop_bg_pause $DIR/$tdir vVw4096_c
11201         local multi_pid=$!
11202
11203         local OLD_IFS=$IFS
11204         IFS=":"
11205         local fidv=($fid)
11206         IFS=$OLD_IFS
11207         # assume that the next FID for this client is sequential, since stdout
11208         # is unfortunately eaten by multiop_bg_pause
11209         local n=$((${fidv[1]} + 1))
11210         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11211         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11212                 error "FID is missing before close"
11213         kill -USR1 $multi_pid
11214         # 1 second delay, so if mtime change we will see it
11215         sleep 1
11216         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11217         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11218 }
11219 run_test 185 "Volatile file support"
11220
11221 test_187a() {
11222         local dir0=$DIR/$tdir/$testnum
11223         mkdir -p $dir0 || error "creating dir $dir0"
11224
11225         local file=$dir0/file1
11226         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11227         local dv1=$($LFS data_version $file)
11228         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11229         local dv2=$($LFS data_version $file)
11230         [[ $dv1 != $dv2 ]] ||
11231                 error "data version did not change on write $dv1 == $dv2"
11232
11233         # clean up
11234         rm -f $file1
11235 }
11236 run_test 187a "Test data version change"
11237
11238 test_187b() {
11239         local dir0=$DIR/$tdir/$testnum
11240         mkdir -p $dir0 || error "creating dir $dir0"
11241
11242         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11243         [[ ${DV[0]} != ${DV[1]} ]] ||
11244                 error "data version did not change on write"\
11245                       " ${DV[0]} == ${DV[1]}"
11246
11247         # clean up
11248         rm -f $file1
11249 }
11250 run_test 187b "Test data version change on volatile file"
11251
11252 test_200() {
11253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11254         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11255
11256         local POOL=${POOL:-cea1}
11257         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11258         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11259         # Pool OST targets
11260         local first_ost=0
11261         local last_ost=$(($OSTCOUNT - 1))
11262         local ost_step=2
11263         local ost_list=$(seq $first_ost $ost_step $last_ost)
11264         local ost_range="$first_ost $last_ost $ost_step"
11265         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11266         local file_dir=$POOL_ROOT/file_tst
11267         local subdir=$test_path/subdir
11268
11269         local rc=0
11270         while : ; do
11271                 # former test_200a test_200b
11272                 pool_add $POOL                          || { rc=$? ; break; }
11273                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11274                 # former test_200c test_200d
11275                 mkdir -p $test_path
11276                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11277                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11278                 mkdir -p $subdir
11279                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11280                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11281                                                         || { rc=$? ; break; }
11282                 # former test_200e test_200f
11283                 local files=$((OSTCOUNT*3))
11284                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11285                                                         || { rc=$? ; break; }
11286                 pool_create_files $POOL $file_dir $files "$ost_list" \
11287                                                         || { rc=$? ; break; }
11288                 # former test_200g test_200h
11289                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11290                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11291
11292                 # former test_201a test_201b test_201c
11293                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11294
11295                 local f=$test_path/$tfile
11296                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11297                 pool_remove $POOL $f                    || { rc=$? ; break; }
11298                 break
11299         done
11300
11301         cleanup_pools
11302         return $rc
11303 }
11304 run_test 200 "OST pools"
11305
11306 # usage: default_attr <count | size | offset>
11307 default_attr() {
11308         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11309 }
11310
11311 # usage: check_default_stripe_attr
11312 check_default_stripe_attr() {
11313         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11314         case $1 in
11315         --stripe-count|--count)
11316                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11317         --stripe-size|--size)
11318                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11319         --stripe-index|--index)
11320                 EXPECTED=-1;;
11321         *)
11322                 error "unknown getstripe attr '$1'"
11323         esac
11324
11325         [ $ACTUAL != $EXPECTED ] &&
11326                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11327 }
11328
11329 test_204a() {
11330         test_mkdir -p $DIR/$tdir
11331         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11332
11333         check_default_stripe_attr --stripe-count
11334         check_default_stripe_attr --stripe-size
11335         check_default_stripe_attr --stripe-index
11336
11337         return 0
11338 }
11339 run_test 204a "Print default stripe attributes ================="
11340
11341 test_204b() {
11342         test_mkdir -p $DIR/$tdir
11343         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11344
11345         check_default_stripe_attr --stripe-size
11346         check_default_stripe_attr --stripe-index
11347
11348         return 0
11349 }
11350 run_test 204b "Print default stripe size and offset  ==========="
11351
11352 test_204c() {
11353         test_mkdir -p $DIR/$tdir
11354         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11355
11356         check_default_stripe_attr --stripe-count
11357         check_default_stripe_attr --stripe-index
11358
11359         return 0
11360 }
11361 run_test 204c "Print default stripe count and offset ==========="
11362
11363 test_204d() {
11364         test_mkdir -p $DIR/$tdir
11365         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11366
11367         check_default_stripe_attr --stripe-count
11368         check_default_stripe_attr --stripe-size
11369
11370         return 0
11371 }
11372 run_test 204d "Print default stripe count and size ============="
11373
11374 test_204e() {
11375         test_mkdir -p $DIR/$tdir
11376         $SETSTRIPE -d $DIR/$tdir
11377
11378         check_default_stripe_attr --stripe-count --raw
11379         check_default_stripe_attr --stripe-size --raw
11380         check_default_stripe_attr --stripe-index --raw
11381
11382         return 0
11383 }
11384 run_test 204e "Print raw stripe attributes ================="
11385
11386 test_204f() {
11387         test_mkdir -p $DIR/$tdir
11388         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11389
11390         check_default_stripe_attr --stripe-size --raw
11391         check_default_stripe_attr --stripe-index --raw
11392
11393         return 0
11394 }
11395 run_test 204f "Print raw stripe size and offset  ==========="
11396
11397 test_204g() {
11398         test_mkdir -p $DIR/$tdir
11399         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11400
11401         check_default_stripe_attr --stripe-count --raw
11402         check_default_stripe_attr --stripe-index --raw
11403
11404         return 0
11405 }
11406 run_test 204g "Print raw stripe count and offset ==========="
11407
11408 test_204h() {
11409         test_mkdir -p $DIR/$tdir
11410         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11411
11412         check_default_stripe_attr --stripe-count --raw
11413         check_default_stripe_attr --stripe-size --raw
11414
11415         return 0
11416 }
11417 run_test 204h "Print raw stripe count and size ============="
11418
11419 # Figure out which job scheduler is being used, if any,
11420 # or use a fake one
11421 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11422         JOBENV=SLURM_JOB_ID
11423 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11424         JOBENV=LSB_JOBID
11425 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11426         JOBENV=PBS_JOBID
11427 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11428         JOBENV=LOADL_STEP_ID
11429 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11430         JOBENV=JOB_ID
11431 else
11432         JOBENV=FAKE_JOBID
11433 fi
11434
11435 verify_jobstats() {
11436         local cmd=$1
11437         local target=$2
11438
11439         # clear old jobstats
11440         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11441         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11442
11443         # use a new JobID for this test, or we might see an old one
11444         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11445
11446         JOBVAL=${!JOBENV}
11447         log "Test: $cmd"
11448         log "Using JobID environment variable $JOBENV=$JOBVAL"
11449
11450         if [ $JOBENV = "FAKE_JOBID" ]; then
11451                 FAKE_JOBID=$JOBVAL $cmd
11452         else
11453                 $cmd
11454         fi
11455
11456         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11457                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11458                 do_facet $FACET lctl get_param mdt.*.job_stats |
11459                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11460         fi
11461         if [ "$target" = "ost" -o "$target" = "both" ]; then
11462                 FACET=ost1
11463                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11464                         grep $JOBVAL || error "No job stats found on OST $FACET"
11465         fi
11466 }
11467
11468 jobstats_set() {
11469         trap 0
11470         NEW_JOBENV=${1:-$OLD_JOBENV}
11471         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11472         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11473 }
11474
11475 test_205() { # Job stats
11476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11477         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11478         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11479                 skip "Server doesn't support jobstats" && return 0
11480         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11481
11482         local cmd
11483         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11484         if [ $OLD_JOBENV != $JOBENV ]; then
11485                 jobstats_set $JOBENV
11486                 trap jobstats_set EXIT
11487         fi
11488
11489         local user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11490                      changelog_register -n)
11491         echo "Registered as changelog user $user"
11492
11493         # mkdir
11494         cmd="mkdir $DIR/$tfile"
11495         verify_jobstats "$cmd" "mdt"
11496         # rmdir
11497         cmd="rm -fr $DIR/$tfile"
11498         verify_jobstats "$cmd" "mdt"
11499         # mknod
11500         cmd="mknod $DIR/$tfile c 1 3"
11501         verify_jobstats "$cmd" "mdt"
11502         # unlink
11503         cmd="rm -f $DIR/$tfile"
11504         verify_jobstats "$cmd" "mdt"
11505         # open & close
11506         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11507         verify_jobstats "$cmd" "mdt"
11508         # setattr
11509         cmd="touch $DIR/$tfile"
11510         verify_jobstats "$cmd" "both"
11511         # write
11512         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11513         verify_jobstats "$cmd" "ost"
11514         # read
11515         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11516         verify_jobstats "$cmd" "ost"
11517         # truncate
11518         cmd="$TRUNCATE $DIR/$tfile 0"
11519         verify_jobstats "$cmd" "both"
11520         # rename
11521         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11522         verify_jobstats "$cmd" "mdt"
11523
11524         # Ensure that jobid are present in changelog (if supported by MDS)
11525         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11526         then
11527                 $LFS changelog $MDT0 | tail -9
11528                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11529                 [ $jobids -eq 9 ] ||
11530                         error "Wrong changelog jobid count $jobids != 9"
11531
11532                 # LU-5862
11533                 JOBENV="disable"
11534                 jobstats_set $JOBENV
11535                 touch $DIR/$tfile
11536                 $LFS changelog $MDT0 | tail -1
11537                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11538                 [ $jobids -eq 0 ] ||
11539                         error "Unexpected jobids when jobid_var=$JOBENV"
11540         fi
11541
11542         # cleanup
11543         rm -f $DIR/jobstats_test_rename
11544
11545         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11546 }
11547 run_test 205 "Verify job stats"
11548
11549 # LU-1480, LU-1773 and LU-1657
11550 test_206() {
11551         mkdir -p $DIR/$tdir
11552         $SETSTRIPE -c -1 $DIR/$tdir
11553 #define OBD_FAIL_LOV_INIT 0x1403
11554         $LCTL set_param fail_loc=0xa0001403
11555         $LCTL set_param fail_val=1
11556         touch $DIR/$tdir/$tfile || true
11557 }
11558 run_test 206 "fail lov_init_raid0() doesn't lbug"
11559
11560 test_207a() {
11561         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11562         local fsz=`stat -c %s $DIR/$tfile`
11563         cancel_lru_locks mdc
11564
11565         # do not return layout in getattr intent
11566 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11567         $LCTL set_param fail_loc=0x170
11568         local sz=`stat -c %s $DIR/$tfile`
11569
11570         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11571
11572         rm -rf $DIR/$tfile
11573 }
11574 run_test 207a "can refresh layout at glimpse"
11575
11576 test_207b() {
11577         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11578         local cksum=`md5sum $DIR/$tfile`
11579         local fsz=`stat -c %s $DIR/$tfile`
11580         cancel_lru_locks mdc
11581         cancel_lru_locks osc
11582
11583         # do not return layout in getattr intent
11584 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11585         $LCTL set_param fail_loc=0x171
11586
11587         # it will refresh layout after the file is opened but before read issues
11588         echo checksum is "$cksum"
11589         echo "$cksum" |md5sum -c --quiet || error "file differs"
11590
11591         rm -rf $DIR/$tfile
11592 }
11593 run_test 207b "can refresh layout at open"
11594
11595 test_208() {
11596         # FIXME: in this test suite, only RD lease is used. This is okay
11597         # for now as only exclusive open is supported. After generic lease
11598         # is done, this test suite should be revised. - Jinshan
11599
11600         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11601                 { skip "Need MDS version at least 2.4.52"; return 0; }
11602         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11603
11604         echo "==== test 1: verify get lease work"
11605         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11606
11607         echo "==== test 2: verify lease can be broken by upcoming open"
11608         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11609         local PID=$!
11610         sleep 1
11611
11612         $MULTIOP $DIR/$tfile oO_RDONLY:c
11613         kill -USR1 $PID && wait $PID || error "break lease error"
11614
11615         echo "==== test 3: verify lease can't be granted if an open already exists"
11616         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11617         local PID=$!
11618         sleep 1
11619
11620         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11621         kill -USR1 $PID && wait $PID || error "open file error"
11622
11623         echo "==== test 4: lease can sustain over recovery"
11624         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11625         PID=$!
11626         sleep 1
11627
11628         fail mds1
11629
11630         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11631
11632         echo "==== test 5: lease broken can't be regained by replay"
11633         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11634         PID=$!
11635         sleep 1
11636
11637         # open file to break lease and then recovery
11638         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11639         fail mds1
11640
11641         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11642
11643         rm -f $DIR/$tfile
11644 }
11645 run_test 208 "Exclusive open"
11646
11647 test_209() {
11648         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11649                 skip_env "must have disp_stripe" && return
11650
11651         touch $DIR/$tfile
11652         sync; sleep 5; sync;
11653
11654         echo 3 > /proc/sys/vm/drop_caches
11655         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11656
11657         # open/close 500 times
11658         for i in $(seq 500); do
11659                 cat $DIR/$tfile
11660         done
11661
11662         echo 3 > /proc/sys/vm/drop_caches
11663         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11664
11665         echo "before: $req_before, after: $req_after"
11666         [ $((req_after - req_before)) -ge 300 ] &&
11667                 error "open/close requests are not freed"
11668         return 0
11669 }
11670 run_test 209 "read-only open/close requests should be freed promptly"
11671
11672 test_212() {
11673         size=`date +%s`
11674         size=$((size % 8192 + 1))
11675         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11676         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11677         rm -f $DIR/f212 $DIR/f212.xyz
11678 }
11679 run_test 212 "Sendfile test ============================================"
11680
11681 test_213() {
11682         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11683         cancel_lru_locks osc
11684         lctl set_param fail_loc=0x8000040f
11685         # generate a read lock
11686         cat $DIR/$tfile > /dev/null
11687         # write to the file, it will try to cancel the above read lock.
11688         cat /etc/hosts >> $DIR/$tfile
11689 }
11690 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11691
11692 test_214() { # for bug 20133
11693         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11694         for (( i=0; i < 340; i++ )) ; do
11695                 touch $DIR/$tdir/d214c/a$i
11696         done
11697
11698         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11699         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11700         ls $DIR/d214c || error "ls $DIR/d214c failed"
11701         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11702         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11703 }
11704 run_test 214 "hash-indexed directory test - bug 20133"
11705
11706 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11707 create_lnet_proc_files() {
11708         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11709         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11710
11711         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11712         rm -f "$TMP/lnet_$1.sys_tmp"
11713 }
11714
11715 # counterpart of create_lnet_proc_files
11716 remove_lnet_proc_files() {
11717         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11718 }
11719
11720 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11721 # 3rd arg as regexp for body
11722 check_lnet_proc_stats() {
11723         local l=$(cat "$TMP/lnet_$1" |wc -l)
11724         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11725
11726         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11727 }
11728
11729 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11730 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11731 # optional and can be regexp for 2nd line (lnet.routes case)
11732 check_lnet_proc_entry() {
11733         local blp=2          # blp stands for 'position of 1st line of body'
11734         [ -z "$5" ] || blp=3 # lnet.routes case
11735
11736         local l=$(cat "$TMP/lnet_$1" |wc -l)
11737         # subtracting one from $blp because the body can be empty
11738         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11739
11740         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11741                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11742
11743         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11744                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11745
11746         # bail out if any unexpected line happened
11747         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11748         [ "$?" != 0 ] || error "$2 misformatted"
11749 }
11750
11751 test_215() { # for bugs 18102, 21079, 21517
11752         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11753         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11754         local P='[1-9][0-9]*'           # positive numeric
11755         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11756         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11757         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11758         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11759
11760         local L1 # regexp for 1st line
11761         local L2 # regexp for 2nd line (optional)
11762         local BR # regexp for the rest (body)
11763
11764         # lnet.stats should look as 11 space-separated non-negative numerics
11765         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11766         create_lnet_proc_files "stats"
11767         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11768         remove_lnet_proc_files "stats"
11769
11770         # lnet.routes should look like this:
11771         # Routing disabled/enabled
11772         # net hops priority state router
11773         # where net is a string like tcp0, hops > 0, priority >= 0,
11774         # state is up/down,
11775         # router is a string like 192.168.1.1@tcp2
11776         L1="^Routing (disabled|enabled)$"
11777         L2="^net +hops +priority +state +router$"
11778         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11779         create_lnet_proc_files "routes"
11780         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11781         remove_lnet_proc_files "routes"
11782
11783         # lnet.routers should look like this:
11784         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11785         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11786         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11787         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11788         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11789         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11790         create_lnet_proc_files "routers"
11791         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11792         remove_lnet_proc_files "routers"
11793
11794         # lnet.peers should look like this:
11795         # nid refs state last max rtr min tx min queue
11796         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11797         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11798         # numeric (0 or >0 or <0), queue >= 0.
11799         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11800         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11801         create_lnet_proc_files "peers"
11802         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11803         remove_lnet_proc_files "peers"
11804
11805         # lnet.buffers  should look like this:
11806         # pages count credits min
11807         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11808         L1="^pages +count +credits +min$"
11809         BR="^ +$N +$N +$I +$I$"
11810         create_lnet_proc_files "buffers"
11811         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11812         remove_lnet_proc_files "buffers"
11813
11814         # lnet.nis should look like this:
11815         # nid status alive refs peer rtr max tx min
11816         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11817         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11818         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11819         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11820         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11821         create_lnet_proc_files "nis"
11822         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11823         remove_lnet_proc_files "nis"
11824
11825         # can we successfully write to lnet.stats?
11826         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
11827         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11828 }
11829 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
11830
11831 test_216() { # bug 20317
11832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11833         remote_ost_nodsh && skip "remote OST with nodsh" && return
11834
11835         local node
11836         local facets=$(get_facets OST)
11837         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11838
11839         save_lustre_params client "osc.*.contention_seconds" > $p
11840         save_lustre_params $facets \
11841                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11842         save_lustre_params $facets \
11843                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11844         save_lustre_params $facets \
11845                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11846         clear_osc_stats
11847
11848         # agressive lockless i/o settings
11849         for node in $(osts_nodes); do
11850                 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'
11851         done
11852         lctl set_param -n osc.*.contention_seconds 60
11853
11854         $DIRECTIO write $DIR/$tfile 0 10 4096
11855         $CHECKSTAT -s 40960 $DIR/$tfile
11856
11857         # disable lockless i/o
11858         for node in $(osts_nodes); do
11859                 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'
11860         done
11861         lctl set_param -n osc.*.contention_seconds 0
11862         clear_osc_stats
11863
11864         dd if=/dev/zero of=$DIR/$tfile count=0
11865         $CHECKSTAT -s 0 $DIR/$tfile
11866
11867         restore_lustre_params <$p
11868         rm -f $p
11869         rm $DIR/$tfile
11870 }
11871 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11872
11873 test_217() { # bug 22430
11874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11875         local node
11876         local nid
11877
11878         for node in $(nodes_list); do
11879                 nid=$(host_nids_address $node $NETTYPE)
11880                 if [[ $nid = *-* ]] ; then
11881                         echo "lctl ping $nid@$NETTYPE"
11882                         lctl ping $nid@$NETTYPE
11883                 else
11884                         echo "skipping $node (no hyphen detected)"
11885                 fi
11886         done
11887 }
11888 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11889
11890 test_218() {
11891        # do directio so as not to populate the page cache
11892        log "creating a 10 Mb file"
11893        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11894        log "starting reads"
11895        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11896        log "truncating the file"
11897        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11898        log "killing dd"
11899        kill %+ || true # reads might have finished
11900        echo "wait until dd is finished"
11901        wait
11902        log "removing the temporary file"
11903        rm -rf $DIR/$tfile || error "tmp file removal failed"
11904 }
11905 run_test 218 "parallel read and truncate should not deadlock ======================="
11906
11907 test_219() {
11908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11909         # write one partial page
11910         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11911         # set no grant so vvp_io_commit_write will do sync write
11912         $LCTL set_param fail_loc=0x411
11913         # write a full page at the end of file
11914         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11915
11916         $LCTL set_param fail_loc=0
11917         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11918         $LCTL set_param fail_loc=0x411
11919         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11920
11921         # LU-4201
11922         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11923         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11924 }
11925 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11926
11927 test_220() { #LU-325
11928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11929         remote_ost_nodsh && skip "remote OST with nodsh" && return
11930         local OSTIDX=0
11931
11932         test_mkdir -p $DIR/$tdir
11933         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11934                 awk '{print $2}' | sed -e 's/_UUID$//')
11935
11936         # on the mdt's osc
11937         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11938         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11939                         osc.$mdtosc_proc1.prealloc_last_id)
11940         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11941                         osc.$mdtosc_proc1.prealloc_next_id)
11942
11943         $LFS df -i
11944
11945         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11946         #define OBD_FAIL_OST_ENOINO              0x229
11947         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11948         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11949         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11950
11951         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11952
11953         MDSOBJS=$((last_id - next_id))
11954         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11955
11956         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11957         echo "OST still has $count kbytes free"
11958
11959         echo "create $MDSOBJS files @next_id..."
11960         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11961
11962         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11963                         osc.$mdtosc_proc1.prealloc_last_id)
11964         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11965                         osc.$mdtosc_proc1.prealloc_next_id)
11966
11967         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11968         $LFS df -i
11969
11970         echo "cleanup..."
11971
11972         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11973         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11974
11975         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11976         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11977         echo "unlink $MDSOBJS files @$next_id..."
11978         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11979 }
11980 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11981
11982 test_221() {
11983         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11984         dd if=`which date` of=$MOUNT/date oflag=sync
11985         chmod +x $MOUNT/date
11986
11987         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11988         $LCTL set_param fail_loc=0x80001401
11989
11990         $MOUNT/date > /dev/null
11991         rm -f $MOUNT/date
11992 }
11993 run_test 221 "make sure fault and truncate race to not cause OOM"
11994
11995 test_222a () {
11996         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11997        rm -rf $DIR/$tdir
11998        test_mkdir -p $DIR/$tdir
11999        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12000        createmany -o $DIR/$tdir/$tfile 10
12001        cancel_lru_locks mdc
12002        cancel_lru_locks osc
12003        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12004        $LCTL set_param fail_loc=0x31a
12005        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12006        $LCTL set_param fail_loc=0
12007        rm -r $DIR/$tdir
12008 }
12009 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12010
12011 test_222b () {
12012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12013        rm -rf $DIR/$tdir
12014        test_mkdir -p $DIR/$tdir
12015        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12016        createmany -o $DIR/$tdir/$tfile 10
12017        cancel_lru_locks mdc
12018        cancel_lru_locks osc
12019        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12020        $LCTL set_param fail_loc=0x31a
12021        rm -r $DIR/$tdir || "AGL for rmdir failed"
12022        $LCTL set_param fail_loc=0
12023 }
12024 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12025
12026 test_223 () {
12027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12028        rm -rf $DIR/$tdir
12029        test_mkdir -p $DIR/$tdir
12030        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12031        createmany -o $DIR/$tdir/$tfile 10
12032        cancel_lru_locks mdc
12033        cancel_lru_locks osc
12034        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12035        $LCTL set_param fail_loc=0x31b
12036        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12037        $LCTL set_param fail_loc=0
12038        rm -r $DIR/$tdir
12039 }
12040 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12041
12042 test_224a() { # LU-1039, MRP-303
12043         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12044         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12045         $LCTL set_param fail_loc=0x508
12046         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12047         $LCTL set_param fail_loc=0
12048         df $DIR
12049 }
12050 run_test 224a "Don't panic on bulk IO failure"
12051
12052 test_224b() { # LU-1039, MRP-303
12053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12054         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12055         cancel_lru_locks osc
12056         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12057         $LCTL set_param fail_loc=0x515
12058         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12059         $LCTL set_param fail_loc=0
12060         df $DIR
12061 }
12062 run_test 224b "Don't panic on bulk IO failure"
12063
12064 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12065 test_225a () {
12066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12067         if [ -z ${MDSSURVEY} ]; then
12068               skip_env "mds-survey not found" && return
12069         fi
12070
12071         [ $MDSCOUNT -ge 2 ] &&
12072                 skip "skipping now for more than one MDT" && return
12073
12074        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12075             { skip "Need MDS version at least 2.2.51"; return; }
12076
12077        local mds=$(facet_host $SINGLEMDS)
12078        local target=$(do_nodes $mds 'lctl dl' | \
12079                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12080
12081        local cmd1="file_count=1000 thrhi=4"
12082        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12083        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12084        local cmd="$cmd1 $cmd2 $cmd3"
12085
12086        rm -f ${TMP}/mds_survey*
12087        echo + $cmd
12088        eval $cmd || error "mds-survey with zero-stripe failed"
12089        cat ${TMP}/mds_survey*
12090        rm -f ${TMP}/mds_survey*
12091 }
12092 run_test 225a "Metadata survey sanity with zero-stripe"
12093
12094 test_225b () {
12095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12096
12097         if [ -z ${MDSSURVEY} ]; then
12098               skip_env "mds-survey not found" && return
12099         fi
12100         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12101             { skip "Need MDS version at least 2.2.51"; return; }
12102
12103         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12104               skip_env "Need to mount OST to test" && return
12105         fi
12106
12107         [ $MDSCOUNT -ge 2 ] &&
12108                 skip "skipping now for more than one MDT" && return
12109        local mds=$(facet_host $SINGLEMDS)
12110        local target=$(do_nodes $mds 'lctl dl' | \
12111                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12112
12113        local cmd1="file_count=1000 thrhi=4"
12114        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12115        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12116        local cmd="$cmd1 $cmd2 $cmd3"
12117
12118        rm -f ${TMP}/mds_survey*
12119        echo + $cmd
12120        eval $cmd || error "mds-survey with stripe_count failed"
12121        cat ${TMP}/mds_survey*
12122        rm -f ${TMP}/mds_survey*
12123 }
12124 run_test 225b "Metadata survey sanity with stripe_count = 1"
12125
12126 mcreate_path2fid () {
12127         local mode=$1
12128         local major=$2
12129         local minor=$3
12130         local name=$4
12131         local desc=$5
12132         local path=$DIR/$tdir/$name
12133         local fid
12134         local rc
12135         local fid_path
12136
12137         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12138                 error "cannot create $desc"
12139
12140         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12141         rc=$?
12142         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12143
12144         fid_path=$($LFS fid2path $MOUNT $fid)
12145         rc=$?
12146         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12147
12148         [ "$path" == "$fid_path" ] ||
12149                 error "fid2path returned $fid_path, expected $path"
12150
12151         echo "pass with $path and $fid"
12152 }
12153
12154 test_226a () {
12155         rm -rf $DIR/$tdir
12156         mkdir -p $DIR/$tdir
12157
12158         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12159         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12160         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12161         mcreate_path2fid 0040666 0 0 dir "directory"
12162         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12163         mcreate_path2fid 0100666 0 0 file "regular file"
12164         mcreate_path2fid 0120666 0 0 link "symbolic link"
12165         mcreate_path2fid 0140666 0 0 sock "socket"
12166 }
12167 run_test 226a "call path2fid and fid2path on files of all type"
12168
12169 test_226b () {
12170         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12171         rm -rf $DIR/$tdir
12172         local MDTIDX=1
12173
12174         mkdir -p $DIR/$tdir
12175         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12176                 error "create remote directory failed"
12177         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12178         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12179                                 "character special file (null)"
12180         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12181                                 "character special file (no device)"
12182         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12183         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12184                                 "block special file (loop)"
12185         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12186         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12187         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12188 }
12189 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12190
12191 # LU-1299 Executing or running ldd on a truncated executable does not
12192 # cause an out-of-memory condition.
12193 test_227() {
12194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12195         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12196         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12197         chmod +x $MOUNT/date
12198
12199         $MOUNT/date > /dev/null
12200         ldd $MOUNT/date > /dev/null
12201         rm -f $MOUNT/date
12202 }
12203 run_test 227 "running truncated executable does not cause OOM"
12204
12205 # LU-1512 try to reuse idle OI blocks
12206 test_228a() {
12207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12208         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12209         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12210                 skip "non-ldiskfs backend" && return
12211
12212         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12213         local myDIR=$DIR/$tdir
12214
12215         mkdir -p $myDIR
12216         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12217         $LCTL set_param fail_loc=0x80001002
12218         createmany -o $myDIR/t- 10000
12219         $LCTL set_param fail_loc=0
12220         # The guard is current the largest FID holder
12221         touch $myDIR/guard
12222         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12223                     tr -d '[')
12224         local IDX=$(($SEQ % 64))
12225
12226         do_facet $SINGLEMDS sync
12227         # Make sure journal flushed.
12228         sleep 6
12229         local blk1=$(do_facet $SINGLEMDS \
12230                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12231                      grep Blockcount | awk '{print $4}')
12232
12233         # Remove old files, some OI blocks will become idle.
12234         unlinkmany $myDIR/t- 10000
12235         # Create new files, idle OI blocks should be reused.
12236         createmany -o $myDIR/t- 2000
12237         do_facet $SINGLEMDS sync
12238         # Make sure journal flushed.
12239         sleep 6
12240         local blk2=$(do_facet $SINGLEMDS \
12241                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12242                      grep Blockcount | awk '{print $4}')
12243
12244         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12245 }
12246 run_test 228a "try to reuse idle OI blocks"
12247
12248 test_228b() {
12249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12250         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12251         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12252                 skip "non-ldiskfs backend" && return
12253
12254         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12255         local myDIR=$DIR/$tdir
12256
12257         mkdir -p $myDIR
12258         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12259         $LCTL set_param fail_loc=0x80001002
12260         createmany -o $myDIR/t- 10000
12261         $LCTL set_param fail_loc=0
12262         # The guard is current the largest FID holder
12263         touch $myDIR/guard
12264         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12265                     tr -d '[')
12266         local IDX=$(($SEQ % 64))
12267
12268         do_facet $SINGLEMDS sync
12269         # Make sure journal flushed.
12270         sleep 6
12271         local blk1=$(do_facet $SINGLEMDS \
12272                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12273                      grep Blockcount | awk '{print $4}')
12274
12275         # Remove old files, some OI blocks will become idle.
12276         unlinkmany $myDIR/t- 10000
12277
12278         # stop the MDT
12279         stop $SINGLEMDS || error "Fail to stop MDT."
12280         # remount the MDT
12281         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12282
12283         df $MOUNT || error "Fail to df."
12284         # Create new files, idle OI blocks should be reused.
12285         createmany -o $myDIR/t- 2000
12286         do_facet $SINGLEMDS sync
12287         # Make sure journal flushed.
12288         sleep 6
12289         local blk2=$(do_facet $SINGLEMDS \
12290                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12291                      grep Blockcount | awk '{print $4}')
12292
12293         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12294 }
12295 run_test 228b "idle OI blocks can be reused after MDT restart"
12296
12297 #LU-1881
12298 test_228c() {
12299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12300         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12301         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12302                 skip "non-ldiskfs backend" && return
12303
12304         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12305         local myDIR=$DIR/$tdir
12306
12307         mkdir -p $myDIR
12308         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12309         $LCTL set_param fail_loc=0x80001002
12310         # 20000 files can guarantee there are index nodes in the OI file
12311         createmany -o $myDIR/t- 20000
12312         $LCTL set_param fail_loc=0
12313         # The guard is current the largest FID holder
12314         touch $myDIR/guard
12315         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12316                     tr -d '[')
12317         local IDX=$(($SEQ % 64))
12318
12319         do_facet $SINGLEMDS sync
12320         # Make sure journal flushed.
12321         sleep 6
12322         local blk1=$(do_facet $SINGLEMDS \
12323                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12324                      grep Blockcount | awk '{print $4}')
12325
12326         # Remove old files, some OI blocks will become idle.
12327         unlinkmany $myDIR/t- 20000
12328         rm -f $myDIR/guard
12329         # The OI file should become empty now
12330
12331         # Create new files, idle OI blocks should be reused.
12332         createmany -o $myDIR/t- 2000
12333         do_facet $SINGLEMDS sync
12334         # Make sure journal flushed.
12335         sleep 6
12336         local blk2=$(do_facet $SINGLEMDS \
12337                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12338                      grep Blockcount | awk '{print $4}')
12339
12340         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12341 }
12342 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12343
12344 test_229() { # LU-2482, LU-3448
12345         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12346                 skip "No HSM support on MDS of $(get_lustre_version)," \
12347                          "need 2.4.53 at least" && return
12348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12349         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12350
12351         rm -f $DIR/$tfile
12352
12353         # Create a file with a released layout and stripe count 2.
12354         $MULTIOP $DIR/$tfile H2c ||
12355                 error "failed to create file with released layout"
12356
12357         $GETSTRIPE -v $DIR/$tfile
12358
12359         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12360         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12361
12362         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12363         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12364         stat $DIR/$tfile || error "failed to stat released file"
12365
12366         chown $RUNAS_ID $DIR/$tfile ||
12367                 error "chown $RUNAS_ID $DIR/$tfile failed"
12368
12369         chgrp $RUNAS_ID $DIR/$tfile ||
12370                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12371
12372         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12373         rm $DIR/$tfile || error "failed to remove released file"
12374 }
12375 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12376
12377 test_230a() {
12378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12379         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12380         local MDTIDX=1
12381
12382         mkdir -p $DIR/$tdir/test_230_local
12383         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12384         [ $mdt_idx -ne 0 ] &&
12385                 error "create local directory on wrong MDT $mdt_idx"
12386
12387         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12388                         error "create remote directory failed"
12389         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12390         [ $mdt_idx -ne $MDTIDX ] &&
12391                 error "create remote directory on wrong MDT $mdt_idx"
12392
12393         createmany -o $DIR/$tdir/test_230/t- 10 ||
12394                 error "create files on remote directory failed"
12395         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12396         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12397         rm -r $DIR/$tdir || error "unlink remote directory failed"
12398 }
12399 run_test 230a "Create remote directory and files under the remote directory"
12400
12401 test_230b() {
12402         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12403         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12404         local MDTIDX=1
12405         local mdt_index
12406         local i
12407         local file
12408         local pid
12409         local stripe_count
12410         local migrate_dir=$DIR/$tdir/migrate_dir
12411         local other_dir=$DIR/$tdir/other_dir
12412
12413         mkdir -p $migrate_dir
12414         mkdir -p $other_dir
12415         for ((i=0; i<10; i++)); do
12416                 mkdir -p $migrate_dir/dir_${i}
12417                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12418                         error "create files under remote dir failed $i"
12419         done
12420
12421         cp /etc/passwd $migrate_dir/$tfile
12422         cp /etc/passwd $other_dir/$tfile
12423         chattr +SAD $migrate_dir
12424         chattr +SAD $migrate_dir/$tfile
12425
12426         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12427         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12428         local old_dir_mode=$(stat -c%f $migrate_dir)
12429         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12430
12431         mkdir -p $migrate_dir/dir_default_stripe2
12432         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12433         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12434
12435         mkdir -p $other_dir
12436         ln $migrate_dir/$tfile $other_dir/luna
12437         ln $migrate_dir/$tfile $migrate_dir/sofia
12438         ln $other_dir/$tfile $migrate_dir/david
12439         ln -s $migrate_dir/$tfile $other_dir/zachary
12440         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12441         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12442
12443         $LFS mv -v -M $MDTIDX $migrate_dir ||
12444                 error "migrate remote dir error"
12445
12446         echo "migratate to MDT1, then checking.."
12447         for ((i = 0; i < 10; i++)); do
12448                 for file in $(find $migrate_dir/dir_${i}); do
12449                         mdt_index=$($LFS getstripe -M $file)
12450                         [ $mdt_index == $MDTIDX ] ||
12451                                 error "$file is not on MDT${MDTIDX}"
12452                 done
12453         done
12454
12455         # the multiple link file should still in MDT0
12456         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12457         [ $mdt_index == 0 ] ||
12458                 error "$file is not on MDT${MDTIDX}"
12459
12460         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12461         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12462                 error " expect $old_dir_flag get $new_dir_flag"
12463
12464         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12465         [ "$old_file_flag" = "$new_file_flag" ] ||
12466                 error " expect $old_file_flag get $new_file_flag"
12467
12468         local new_dir_mode=$(stat -c%f $migrate_dir)
12469         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12470                 error "expect mode $old_dir_mode get $new_dir_mode"
12471
12472         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12473         [ "$old_file_mode" = "$new_file_mode" ] ||
12474                 error "expect mode $old_file_mode get $new_file_mode"
12475
12476         diff /etc/passwd $migrate_dir/$tfile ||
12477                 error "$tfile different after migration"
12478
12479         diff /etc/passwd $other_dir/luna ||
12480                 error "luna different after migration"
12481
12482         diff /etc/passwd $migrate_dir/sofia ||
12483                 error "sofia different after migration"
12484
12485         diff /etc/passwd $migrate_dir/david ||
12486                 error "david different after migration"
12487
12488         diff /etc/passwd $other_dir/zachary ||
12489                 error "zachary different after migration"
12490
12491         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12492                 error "${tfile}_ln different after migration"
12493
12494         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12495                 error "${tfile}_ln_other different after migration"
12496
12497         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12498         [ $stripe_count = 2 ] ||
12499                 error "dir strpe_count $d != 2 after migration."
12500
12501         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12502         [ $stripe_count = 2 ] ||
12503                 error "file strpe_count $d != 2 after migration."
12504
12505         #migrate back to MDT0
12506         MDTIDX=0
12507         $LFS mv -v -M $MDTIDX $migrate_dir ||
12508                 error "migrate remote dir error"
12509
12510         echo "migrate back to MDT0, checking.."
12511         for file in $(find $migrate_dir); do
12512                 mdt_index=$($LFS getstripe -M $file)
12513                 [ $mdt_index == $MDTIDX ] ||
12514                         error "$file is not on MDT${MDTIDX}"
12515         done
12516
12517         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12518         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12519                 error " expect $old_dir_flag get $new_dir_flag"
12520
12521         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12522         [ "$old_file_flag" = "$new_file_flag" ] ||
12523                 error " expect $old_file_flag get $new_file_flag"
12524
12525         local new_dir_mode=$(stat -c%f $migrate_dir)
12526         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12527                 error "expect mode $old_dir_mode get $new_dir_mode"
12528
12529         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12530         [ "$old_file_mode" = "$new_file_mode" ] ||
12531                 error "expect mode $old_file_mode get $new_file_mode"
12532
12533         diff /etc/passwd ${migrate_dir}/$tfile ||
12534                 error "$tfile different after migration"
12535
12536         diff /etc/passwd ${other_dir}/luna ||
12537                 error "luna different after migration"
12538
12539         diff /etc/passwd ${migrate_dir}/sofia ||
12540                 error "sofia different after migration"
12541
12542         diff /etc/passwd ${other_dir}/zachary ||
12543                 error "zachary different after migration"
12544
12545         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12546                 error "${tfile}_ln different after migration"
12547
12548         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12549                 error "${tfile}_ln_other different after migration"
12550
12551         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12552         [ $stripe_count = 2 ] ||
12553                 error "dir strpe_count $d != 2 after migration."
12554
12555         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12556         [ $stripe_count = 2 ] ||
12557                 error "file strpe_count $d != 2 after migration."
12558
12559         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12560 }
12561 run_test 230b "migrate directory"
12562
12563 test_230c() {
12564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12565         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12566         local MDTIDX=1
12567         local mdt_index
12568         local file
12569
12570         #If migrating directory fails in the middle, all entries of
12571         #the directory is still accessiable.
12572         mkdir -p $DIR/$tdir
12573         stat $DIR/$tdir
12574         createmany -o $DIR/$tdir/f 10 ||
12575                 error "create files under ${tdir} failed"
12576
12577         #failed after migrating 5 entries
12578         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12579         do_facet mds1 lctl set_param fail_loc=0x20001801
12580         do_facet mds1 lctl  set_param fail_val=5
12581         local t=$(ls $DIR/$tdir | wc -l)
12582         $LFS mv --mdt-index $MDTIDX $DIR/$tdir &&
12583                 error "migrate should fail after 5 entries"
12584         local u=$(ls $DIR/$tdir | wc -l)
12585         [ "$u" == "$t" ] || error "$u != $t during migration"
12586
12587         for file in $(find $DIR/$tdir); do
12588                 stat $file || error "stat $file failed"
12589         done
12590
12591         do_facet mds1 lctl set_param fail_loc=0
12592         do_facet mds1 lctl set_param fail_val=0
12593
12594         $LFS mv -M $MDTIDX $DIR/$tdir ||
12595                 error "migrate open files should failed with open files"
12596
12597         echo "Finish migration, then checking.."
12598         for file in $(find $DIR/$tdir); do
12599                 mdt_index=$($LFS getstripe -M $file)
12600                 [ $mdt_index == $MDTIDX ] ||
12601                         error "$file is not on MDT${MDTIDX}"
12602         done
12603
12604         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12605 }
12606 run_test 230c "check directory accessiblity if migration is failed"
12607
12608 test_230d() {
12609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12610         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12611         local MDTIDX=1
12612         local mdt_index
12613         local i
12614         local j
12615
12616         mkdir -p $DIR/$tdir
12617
12618         for ((i=0; i<100; i++)); do
12619                 mkdir -p $DIR/$tdir/dir_${i}
12620                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12621                         error "create files under remote dir failed $i"
12622         done
12623
12624         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12625
12626         echo "Finish migration, then checking.."
12627         for file in $(find $DIR/$tdir); do
12628                 mdt_index=$($LFS getstripe -M $file)
12629                 [ $mdt_index == $MDTIDX ] ||
12630                         error "$file is not on MDT${MDTIDX}"
12631         done
12632
12633         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12634 }
12635 run_test 230d "check migrate big directory"
12636
12637 test_231a()
12638 {
12639         # For simplicity this test assumes that max_pages_per_rpc
12640         # is the same across all OSCs
12641         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12642         local bulk_size=$((max_pages * 4096))
12643
12644         mkdir -p $DIR/$tdir
12645
12646         # clear the OSC stats
12647         $LCTL set_param osc.*.stats=0 &>/dev/null
12648
12649         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12650         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12651                 oflag=direct &>/dev/null || error "dd failed"
12652
12653         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12654         if [ x$nrpcs != "x1" ]; then
12655                 error "found $nrpc ost_write RPCs, not 1 as expected"
12656         fi
12657
12658         # Drop the OSC cache, otherwise we will read from it
12659         cancel_lru_locks osc
12660
12661         # clear the OSC stats
12662         $LCTL set_param osc.*.stats=0 &>/dev/null
12663
12664         # Client reads $bulk_size.
12665         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12666                 iflag=direct &>/dev/null || error "dd failed"
12667
12668         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12669         if [ x$nrpcs != "x1" ]; then
12670                 error "found $nrpc ost_read RPCs, not 1 as expected"
12671         fi
12672 }
12673 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12674
12675 test_231b() {
12676         mkdir -p $DIR/$tdir
12677         local i
12678         for i in {0..1023}; do
12679                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12680                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12681                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12682         done
12683         sync
12684 }
12685 run_test 231b "must not assert on fully utilized OST request buffer"
12686
12687 test_232() {
12688         mkdir -p $DIR/$tdir
12689         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12690         $LCTL set_param fail_loc=0x31c
12691
12692         # ignore dd failure
12693         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12694
12695         $LCTL set_param fail_loc=0
12696         umount_client $MOUNT || error "umount failed"
12697         mount_client $MOUNT || error "mount failed"
12698 }
12699 run_test 232 "failed lock should not block umount"
12700
12701 test_233a() {
12702         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12703         { skip "Need MDS version at least 2.3.64"; return; }
12704
12705         local fid=$($LFS path2fid $MOUNT)
12706         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12707                 error "cannot access $MOUNT using its FID '$fid'"
12708 }
12709 run_test 233a "checking that OBF of the FS root succeeds"
12710
12711 test_233b() {
12712         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12713         { skip "Need MDS version at least 2.5.90"; return; }
12714
12715         local fid=$($LFS path2fid $MOUNT/.lustre)
12716         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12717                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12718
12719         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12720         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12721                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12722 }
12723 run_test 233b "checking that OBF of the FS .lustre succeeds"
12724
12725 test_234() {
12726         local p="$TMP/sanityN-$TESTNAME.parameters"
12727         save_lustre_params client "llite.*.xattr_cache" > $p
12728         lctl set_param llite.*.xattr_cache 1 ||
12729                 { skip "xattr cache is not supported"; return 0; }
12730
12731         mkdir -p $DIR/$tdir || error "mkdir failed"
12732         touch $DIR/$tdir/$tfile || error "touch failed"
12733         # OBD_FAIL_LLITE_XATTR_ENOMEM
12734         $LCTL set_param fail_loc=0x1405
12735         # output of the form: attr 2 4 44 3 fc13 x86_64
12736         V=($(IFS=".-" rpm -q attr))
12737         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
12738               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
12739                 # attr pre-2.4.44-7 had a bug with rc
12740                 # LU-3703 - SLES 11 and FC13 clients have older attr
12741                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12742                         error "getfattr should have failed with ENOMEM"
12743         else
12744                 skip "LU-3703: attr version $(getfattr --version) too old"
12745         fi
12746         $LCTL set_param fail_loc=0x0
12747         rm -rf $DIR/$tdir
12748
12749         restore_lustre_params < $p
12750         rm -f $p
12751 }
12752 run_test 234 "xattr cache should not crash on ENOMEM"
12753
12754 test_235() {
12755         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12756                 skip "Need MDS version at least 2.4.52" && return
12757         flock_deadlock $DIR/$tfile
12758         local RC=$?
12759         case $RC in
12760                 0)
12761                 ;;
12762                 124) error "process hangs on a deadlock"
12763                 ;;
12764                 *) error "error executing flock_deadlock $DIR/$tfile"
12765                 ;;
12766         esac
12767 }
12768 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12769
12770 #LU-2935
12771 test_236() {
12772         check_swap_layouts_support && return 0
12773         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12774
12775         local ref1=/etc/passwd
12776         local ref2=/etc/group
12777         local file1=$DIR/$tdir/f1
12778         local file2=$DIR/$tdir/f2
12779
12780         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12781         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12782         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12783         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12784         local fd=$(free_fd)
12785         local cmd="exec $fd<>$file2"
12786         eval $cmd
12787         rm $file2
12788         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12789                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12790         cmd="exec $fd>&-"
12791         eval $cmd
12792         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12793
12794         #cleanup
12795         rm -rf $DIR/$tdir
12796 }
12797 run_test 236 "Layout swap on open unlinked file"
12798
12799 # test to verify file handle related system calls
12800 # (name_to_handle_at/open_by_handle_at)
12801 # The new system calls are supported in glibc >= 2.14.
12802
12803 test_237() {
12804         echo "Test file_handle syscalls" > $DIR/$tfile ||
12805                 error "write failed"
12806         check_fhandle_syscalls $DIR/$tfile ||
12807                 error "check_fhandle_syscalls failed"
12808 }
12809 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12810
12811 # LU-4659 linkea consistency
12812 test_238() {
12813         local server_version=$(lustre_version_code $SINGLEMDS)
12814
12815         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12816                 [[ $server_version -gt $(version_code 2.5.1) &&
12817                    $server_version -lt $(version_code 2.5.50) ]] ||
12818                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12819
12820         touch $DIR/$tfile
12821         ln $DIR/$tfile $DIR/$tfile.lnk
12822         touch $DIR/$tfile.new
12823         mv $DIR/$tfile.new $DIR/$tfile
12824         local fid1=$(lfs path2fid $DIR/$tfile)
12825         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12826         local path1=$(lfs fid2path $FSNAME $fid1)
12827         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12828         local path2=$(lfs fid2path $FSNAME $fid2)
12829         [ $tfile.lnk == $path2 ] ||
12830                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12831         rm -f $DIR/$tfile*
12832 }
12833 run_test 238 "Verify linkea consistency"
12834
12835 test_239() {
12836         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
12837                 skip "Need MDS version at least 2.5.60" && return
12838         local list=$(comma_list $(mdts_nodes))
12839
12840         mkdir -p $DIR/$tdir
12841         createmany -o $DIR/$tdir/f- 5000
12842         unlinkmany $DIR/$tdir/f- 5000
12843         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12844         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12845                         osc.*MDT*.sync_in_flight" | calc_sum)
12846         [ "$changes" -eq 0 ] || error "$changes not synced"
12847 }
12848 run_test 239 "osp_sync test"
12849
12850 test_240() {
12851         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12852
12853         mkdir -p $DIR/$tdir
12854
12855         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12856                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12857         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12858                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12859
12860         umount_client $MOUNT || error "umount failed"
12861         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12862         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12863         mount_client $MOUNT || error "failed to mount client"
12864
12865         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12866         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12867 }
12868 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12869
12870 test_241_bio() {
12871         for LOOP in $(seq $1); do
12872                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
12873                 cancel_lru_locks osc
12874         done
12875 }
12876
12877 test_241_dio() {
12878         for LOOP in $(seq $1); do
12879                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
12880                                                 iflag=direct 2>/dev/null
12881         done
12882 }
12883
12884 test_241() {
12885         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
12886         ls -la $DIR/$tfile
12887         cancel_lru_locks osc
12888         test_241_bio 1000 &
12889         PID=$!
12890         test_241_dio 1000
12891         wait $PID
12892 }
12893 run_test 241 "bio vs dio"
12894
12895 test_242() {
12896         mkdir -p $DIR/$tdir
12897         touch $DIR/$tdir/$tfile
12898
12899         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
12900         do_facet mds1 lctl set_param fail_loc=0x105
12901         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
12902
12903         do_facet mds1 lctl set_param fail_loc=0
12904         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
12905 }
12906 run_test 242 "mdt_readpage failure should not cause directory unreadable"
12907
12908 test_243()
12909 {
12910         test_mkdir -p $DIR/$tdir
12911         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
12912 }
12913 run_test 243 "various group lock tests"
12914
12915 cleanup_test_300() {
12916         trap 0
12917         umask $SAVE_UMASK
12918 }
12919 test_striped_dir() {
12920         local mdt_index=$1
12921         local stripe_count
12922         local stripe_index
12923
12924         mkdir -p $DIR/$tdir
12925
12926         SAVE_UMASK=$(umask)
12927         trap cleanup_test_300 RETURN EXIT
12928
12929         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12930                                                 $DIR/$tdir/striped_dir ||
12931                 error "set striped dir error"
12932
12933         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12934         [ "$mode" = "755" ] || error "expect 755 got $mode"
12935
12936         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12937         if [ "$stripe_count" != "2" ]; then
12938                 error "stripe_count is $stripe_count, expect 2"
12939         fi
12940
12941         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12942         if [ "$stripe_index" != "$mdt_index" ]; then
12943                 error "stripe_index is $stripe_index, expect $mdt_index"
12944         fi
12945
12946         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12947                 error "nlink error after create striped dir"
12948
12949         mkdir $DIR/$tdir/striped_dir/a
12950         mkdir $DIR/$tdir/striped_dir/b
12951
12952         stat $DIR/$tdir/striped_dir/a ||
12953                 error "create dir under striped dir failed"
12954         stat $DIR/$tdir/striped_dir/b ||
12955                 error "create dir under striped dir failed"
12956
12957         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12958                 error "nlink error after mkdir"
12959
12960         rmdir $DIR/$tdir/striped_dir/a
12961         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12962                 error "nlink error after rmdir"
12963
12964         rmdir $DIR/$tdir/striped_dir/b
12965         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12966                 error "nlink error after rmdir"
12967
12968         rmdir $DIR/$tdir/striped_dir ||
12969                 error "rmdir striped dir error"
12970
12971         cleanup_test_300
12972
12973         true
12974 }
12975
12976 test_300a() {
12977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12978         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12979
12980         test_striped_dir 0 || error "failed on striped dir on MDT0"
12981         test_striped_dir 1 || error "failed on striped dir on MDT0"
12982 }
12983 run_test 300a "basic striped dir sanity test"
12984
12985 test_300b() {
12986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12987         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12988         local i
12989         local mtime1
12990         local mtime2
12991         local mtime3
12992
12993         test_mkdir $DIR/$tdir || error "mkdir fail"
12994         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12995                 error "set striped dir error"
12996         for ((i=0; i<10; i++)); do
12997                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12998                 sleep 1
12999                 touch $DIR/$tdir/striped_dir/file_$i ||
13000                                         error "touch error $i"
13001                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13002                 [ $mtime1 -eq $mtime2 ] &&
13003                         error "mtime not change after create"
13004                 sleep 1
13005                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13006                                         error "unlink error $i"
13007                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13008                 [ $mtime2 -eq $mtime3 ] &&
13009                         error "mtime did not change after unlink"
13010         done
13011         true
13012 }
13013 run_test 300b "check ctime/mtime for striped dir"
13014
13015 test_300c() {
13016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13017         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13018         local file_count
13019
13020         mkdir -p $DIR/$tdir
13021         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13022                 error "set striped dir error"
13023
13024         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13025                 error "chown striped dir failed"
13026
13027         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13028                 error "create 5k files failed"
13029
13030         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13031
13032         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13033
13034         rm -rf $DIR/$tdir
13035 }
13036 run_test 300c "chown && check ls under striped directory"
13037
13038 test_300d() {
13039         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13040         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13041         local stripe_count
13042         local file
13043
13044         mkdir -p $DIR/$tdir
13045         $SETSTRIPE -c 2 $DIR/$tdir
13046
13047         #local striped directory
13048         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13049                 error "set striped dir error"
13050         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13051                 error "create 10 files failed"
13052
13053         #remote striped directory
13054         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13055                 error "set striped dir error"
13056         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13057                 error "create 10 files failed"
13058
13059         for file in $(find $DIR/$tdir); do
13060                 stripe_count=$($GETSTRIPE -c $file)
13061                 [ $stripe_count -eq 2 ] ||
13062                         error "wrong stripe $stripe_count for $file"
13063         done
13064
13065         rm -rf $DIR/$tdir
13066 }
13067 run_test 300d "check default stripe under striped directory"
13068
13069 test_300e() {
13070         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13071         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13072         local stripe_count
13073         local file
13074
13075         mkdir -p $DIR/$tdir
13076
13077         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13078                 error "set striped dir error"
13079
13080         touch $DIR/$tdir/striped_dir/a
13081         touch $DIR/$tdir/striped_dir/b
13082         touch $DIR/$tdir/striped_dir/c
13083
13084         mkdir $DIR/$tdir/striped_dir/dir_a
13085         mkdir $DIR/$tdir/striped_dir/dir_b
13086         mkdir $DIR/$tdir/striped_dir/dir_c
13087
13088         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13089                 error "set striped dir under striped dir error"
13090
13091         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13092                 error "set striped dir under striped dir error"
13093
13094         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13095                 error "set striped dir under striped dir error"
13096
13097         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
13098                 error "rename file under striped dir should fail"
13099
13100         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
13101                 error "rename dir under striped dir should fail"
13102
13103         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
13104                 error "rename dir under different stripes should fail"
13105
13106         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13107                 error "rename file under striped dir should succeed"
13108
13109         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
13110                 error "rename dir under striped dir should succeed"
13111
13112         rm -rf $DIR/$tdir
13113 }
13114 run_test 300e "check rename under striped directory"
13115
13116 test_300f() {
13117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13118         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13119         local stripe_count
13120         local file
13121
13122         rm -rf $DIR/$tdir
13123         mkdir -p $DIR/$tdir
13124
13125         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13126                 error "set striped dir error"
13127
13128         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13129                 error "set striped dir error"
13130
13131         touch $DIR/$tdir/striped_dir/a
13132         mkdir $DIR/$tdir/striped_dir/dir_a
13133         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13134                 error "create striped dir under striped dir fails"
13135
13136         touch $DIR/$tdir/striped_dir1/b
13137         mkdir $DIR/$tdir/striped_dir1/dir_b
13138         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13139                 error "create striped dir under striped dir fails"
13140
13141         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
13142                 error "rename file under different striped dir should fail"
13143
13144         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
13145                 error "rename dir under different striped dir should fail"
13146
13147         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
13148                 error "rename striped dir under diff striped dir should fail"
13149
13150         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13151                 error "rename file under diff striped dirs fails"
13152
13153         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
13154                 error "rename dir under diff striped dirs fails"
13155
13156         rm -rf $DIR/$tdir
13157 }
13158 run_test 300f "check rename cross striped directory"
13159
13160 test_300_check_default_striped_dir()
13161 {
13162         local dirname=$1
13163         local default_count=$2
13164         local default_index=$3
13165         local stripe_count
13166         local stripe_index
13167         local dir_stripe_index
13168         local dir
13169
13170         echo "checking $dirname $default_count $default_index"
13171         $LFS setdirstripe -D -c $default_count -i $default_index \
13172                                 -t all_char $DIR/$tdir/$dirname ||
13173                 error "set default stripe on striped dir error"
13174         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13175         [ $stripe_count -eq $default_count ] ||
13176                 error "expect $default_count get $stripe_count for $dirname"
13177
13178         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13179         [ $stripe_index -eq $default_index ] ||
13180                 error "expect $default_index get $stripe_index for $dirname"
13181
13182         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13183                                                 error "create dirs failed"
13184         for dir in $(find $DIR/$tdir/$dirname/*); do
13185                 stripe_count=$($LFS getdirstripe -c $dir)
13186                 [ $stripe_count -eq $default_count ] ||
13187                 error "stripe count $default_count != $stripe_count for $dir"
13188
13189                 stripe_index=$($LFS getdirstripe -i $dir)
13190                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13191                         error "$stripe_index != $default_index for $dir"
13192
13193                 #check default stripe
13194                 stripe_count=$($LFS getdirstripe -D -c $dir)
13195                 [ $stripe_count -eq $default_count ] ||
13196                 error "default count $default_count != $stripe_count for $dir"
13197
13198                 stripe_index=$($LFS getdirstripe -D -i $dir)
13199                 [ $stripe_index -eq $default_index ] ||
13200                 error "default index $default_index != $stripe_index for $dir"
13201         done
13202         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13203 }
13204
13205 test_300g() {
13206         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13207         local dir
13208         local stripe_count
13209         local stripe_index
13210
13211         mkdir $DIR/$tdir
13212         mkdir $DIR/$tdir/normal_dir
13213
13214         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13215         test_300_check_default_striped_dir normal_dir 1 0
13216         test_300_check_default_striped_dir normal_dir 2 1
13217         test_300_check_default_striped_dir normal_dir 2 -1
13218
13219         #delete default stripe information
13220         echo "delete default stripeEA"
13221         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13222                 error "set default stripe on striped dir error"
13223
13224         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13225         for dir in $(find $DIR/$tdir/normal_dir/*); do
13226                 stripe_count=$($LFS getdirstripe -c $dir)
13227                 [ $stripe_count -eq 0 ] ||
13228                         error "expect 1 get $stripe_count for $dir"
13229                 stripe_index=$($LFS getdirstripe -i $dir)
13230                 [ $stripe_index -eq 0 ] ||
13231                         error "expect 0 get $stripe_index for $dir"
13232         done
13233 }
13234 run_test 300g "check default striped directory for normal directory"
13235
13236 test_300h() {
13237         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13238         local dir
13239         local stripe_count
13240
13241         mkdir $DIR/$tdir
13242         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13243                                         $DIR/$tdir/striped_dir ||
13244                 error "set striped dir error"
13245
13246         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13247         test_300_check_default_striped_dir striped_dir 1 0
13248         test_300_check_default_striped_dir striped_dir 2 1
13249         test_300_check_default_striped_dir striped_dir 2 -1
13250
13251         #delete default stripe information
13252         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13253                 error "set default stripe on striped dir error"
13254
13255         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13256         for dir in $(find $DIR/$tdir/striped_dir/*); do
13257                 stripe_count=$($LFS getdirstripe -c $dir)
13258                 [ $stripe_count -eq 0 ] ||
13259                         error "expect 1 get $stripe_count for $dir"
13260         done
13261 }
13262 run_test 300h "check default striped directory for striped directory"
13263
13264 test_300i() {
13265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13266         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13267         local stripe_count
13268         local file
13269
13270         mkdir $DIR/$tdir
13271
13272         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13273                 error "set striped dir error"
13274
13275         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13276                 error "create files under striped dir failed"
13277
13278         # unfortunately, we need to umount to clear dir layout cache for now
13279         # once we fully implement dir layout, we can drop this
13280         umount_client $MOUNT || error "umount failed"
13281         mount_client $MOUNT || error "mount failed"
13282
13283         #set the stripe to be unknown hash type
13284         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13285         $LCTL set_param fail_loc=0x1901
13286         for ((i = 0; i < 10; i++)); do
13287                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13288                         error "stat f-$i failed"
13289                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13290         done
13291
13292         touch $DIR/$tdir/striped_dir/f0 &&
13293                 error "create under striped dir with unknown hash should fail"
13294
13295         $LCTL set_param fail_loc=0
13296
13297         umount_client $MOUNT || error "umount failed"
13298         mount_client $MOUNT || error "mount failed"
13299
13300         return 0
13301 }
13302 run_test 300i "client handle unknown hash type striped directory"
13303
13304 test_400a() { # LU-1606, was conf-sanity test_74
13305         local extra_flags=''
13306         local out=$TMP/$tfile
13307         local prefix=/usr/include/lustre
13308         local prog
13309
13310         if ! which $CC > /dev/null 2>&1; then
13311                 skip_env "$CC is not installed"
13312                 return 0
13313         fi
13314
13315         if ! [[ -d $prefix ]]; then
13316                 # Assume we're running in tree and fixup the include path.
13317                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13318                 extra_flags+=" -I$LUSTRE/include"
13319                 extra_flags+=" -L$LUSTRE/utils"
13320         fi
13321
13322         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13323                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13324                         error "client api broken"
13325         done
13326 }
13327 run_test 400a "Lustre client api program can compile and link"
13328
13329 test_400b() { # LU-1606, LU-5011
13330         local header
13331         local out=$TMP/$tfile
13332         local prefix=/usr/include/lustre
13333
13334         # We use a hard coded prefix so that this test will not fail
13335         # when run in tree. There are headers in lustre/include/lustre/
13336         # that are not packaged (like lustre_idl.h) and have more
13337         # complicated include dependencies (like config.h and lnet/types.h).
13338         # Since this test about correct packaging we just skip them when
13339         # they don't exist (see below) rather than try to fixup cppflags.
13340
13341         if ! which $CC > /dev/null 2>&1; then
13342                 skip_env "$CC is not installed"
13343                 return 0
13344         fi
13345
13346         for header in $prefix/*.h; do
13347                 if ! [[ -f "$header" ]]; then
13348                         continue
13349                 fi
13350
13351                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13352                         continue # liblustreapi.h is deprecated.
13353                 fi
13354
13355                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13356                         error "cannot compile '$header'"
13357         done
13358 }
13359 run_test 400b "packaged headers can be compiled"
13360
13361 #
13362 # tests that do cleanup/setup should be run at the end
13363 #
13364
13365 test_900() {
13366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13367         local ls
13368         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13369         $LCTL set_param fail_loc=0x903
13370
13371         cancel_lru_locks MGC
13372
13373         FAIL_ON_ERROR=true cleanup
13374         FAIL_ON_ERROR=true setup
13375 }
13376 run_test 900 "umount should not race with any mgc requeue thread"
13377
13378 complete $SECONDS
13379 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13380 check_and_cleanup_lustre
13381 if [ "$I_MOUNTED" != "yes" ]; then
13382         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13383 fi
13384 exit_status