Whamcloud - gitweb
LU-2524 test: Modify tdir to be single directory
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 4; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19 is_sles11()                                             # LU-4351
20 {
21         if [ -r /etc/SuSE-release ]
22         then
23                 local vers=`grep VERSION /etc/SuSE-release | awk '{print $3}'`
24                 if [ $vers -eq 11 ]
25                 then
26                         return 0
27                 fi
28         fi
29         return 1
30 }
31
32 if is_sles11; then                                      # LU-4351
33         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 54c"
34 fi
35
36 SRCDIR=$(cd $(dirname $0); echo $PWD)
37 export PATH=$PATH:/sbin
38
39 TMP=${TMP:-/tmp}
40
41 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
42 CREATETEST=${CREATETEST:-createtest}
43 LFS=${LFS:-lfs}
44 LFIND=${LFIND:-"$LFS find"}
45 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
46 LCTL=${LCTL:-lctl}
47 MCREATE=${MCREATE:-mcreate}
48 OPENFILE=${OPENFILE:-openfile}
49 OPENUNLINK=${OPENUNLINK:-openunlink}
50 export MULTIOP=${MULTIOP:-multiop}
51 READS=${READS:-"reads"}
52 MUNLINK=${MUNLINK:-munlink}
53 SOCKETSERVER=${SOCKETSERVER:-socketserver}
54 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
55 MEMHOG=${MEMHOG:-memhog}
56 DIRECTIO=${DIRECTIO:-directio}
57 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
58 UMOUNT=${UMOUNT:-"umount -d"}
59 STRIPES_PER_OBJ=-1
60 CHECK_GRANT=${CHECK_GRANT:-"yes"}
61 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
62 export PARALLEL=${PARALLEL:-"no"}
63
64 export NAME=${NAME:-local}
65
66 SAVE_PWD=$PWD
67
68 CLEANUP=${CLEANUP:-:}
69 SETUP=${SETUP:-:}
70 TRACE=${TRACE:-""}
71 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
72 . $LUSTRE/tests/test-framework.sh
73 init_test_env $@
74 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
75 init_logging
76
77 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b"
78
79 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
80 # bug number for skipped test:        LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
81         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     40      48a     180     184c"
82
83 FAIL_ON_ERROR=false
84
85 cleanup() {
86         echo -n "cln.."
87         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
88         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
89 }
90 setup() {
91         echo -n "mnt.."
92         load_modules
93         setupall || exit 10
94         echo "done"
95 }
96
97 check_kernel_version() {
98         WANT_VER=$1
99         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
100         case $GOT_VER in
101         patchless|patchless_client) return 0;;
102         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
103         esac
104         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
105         return 1
106 }
107
108 check_swap_layouts_support()
109 {
110         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
111                 { skip "Does not support layout lock."; return 0; }
112         return 1
113 }
114
115 if [ "$ONLY" == "cleanup" ]; then
116        sh llmountcleanup.sh
117        exit 0
118 fi
119
120 check_and_setup_lustre
121
122 DIR=${DIR:-$MOUNT}
123 assert_DIR
124
125 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
126         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
127 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
128 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
129 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
130 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
131 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
132 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
133
134 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
135 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
136 rm -rf $DIR/[Rdfs][0-9]*
137
138 # $RUNAS_ID may get set incorrectly somewhere else
139 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
140
141 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
142
143 build_test_filter
144
145 if [ "${ONLY}" = "MOUNT" ] ; then
146         echo "Lustre is up, please go on"
147         exit
148 fi
149
150 echo "preparing for tests involving mounts"
151 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
152 touch $EXT2_DEV
153 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
154 echo # add a newline after mke2fs.
155
156 umask 077
157
158 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
159 lctl set_param debug=-1 2> /dev/null || true
160 test_0a() {
161         touch $DIR/$tfile
162         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
163         rm $DIR/$tfile
164         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
165 }
166 run_test 0a "touch; rm ====================="
167
168 test_0b() {
169         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
170         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
171 }
172 run_test 0b "chmod 0755 $DIR ============================="
173
174 test_0c() {
175         $LCTL get_param mdc.*.import | grep "state: FULL" ||
176                 error "import not FULL"
177         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
178                 error "bad target"
179 }
180 run_test 0c "check import proc ============================="
181
182 test_1() {
183         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
184         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
185         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
186         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
187         rmdir $DIR/$tdir/d2
188         rmdir $DIR/$tdir
189         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
190 }
191 run_test 1 "mkdir; remkdir; rmdir =============================="
192
193 test_2() {
194         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
195         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
196         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
197         rm -r $DIR/$tdir
198         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
199 }
200 run_test 2 "mkdir; touch; rmdir; check file ===================="
201
202 test_3() {
203         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
204         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
205         touch $DIR/$tdir/$tfile
206         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
207         rm -r $DIR/$tdir
208         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
209 }
210 run_test 3 "mkdir; touch; rmdir; check dir ====================="
211
212 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
213 test_4() {
214         local MDTIDX=1
215         local remote_dir=remote_dir
216
217         [ $MDSCOUNT -ge 2 ] && skip "skip now for LU-4690" && return #LU-4690
218         test_mkdir $DIR/$remote_dir ||
219                 error "Create remote directory failed"
220
221         touch $DIR/$remote_dir/$tfile ||
222                 error "Create file under remote directory failed"
223
224         rmdir $DIR/$remote_dir &&
225                 error "Expect error removing in-use dir $DIR/$remote_dir"
226
227         test -d $DIR/$remote_dir || error "Remote directory disappeared"
228 }
229 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
230
231 test_5() {
232         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
233         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
234         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
235         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
236         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
237 }
238 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
239
240 test_6a() {
241         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
242         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
243         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
244                 error "$tfile does not have perm 0666 or UID $UID"
245         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
246         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
247                 error "$tfile should be 0666 and owned by UID $UID"
248 }
249 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
250
251 test_6c() {
252         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
253         touch $DIR/$tfile
254         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
255         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
256                 error "$tfile should be owned by UID $RUNAS_ID"
257         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
258         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
259                 error "$tfile should be owned by UID $RUNAS_ID"
260 }
261 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
262
263 test_6e() {
264         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
265         touch $DIR/$tfile
266         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
267         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
268                 error "$tfile should be owned by GID $UID"
269         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
270         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
271                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
272 }
273 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
274
275 test_6g() {
276         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
277         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
278         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
279         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
280         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
281         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
282         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
283                 error "$tdir/d/subdir should be GID $RUNAS_GID"
284 }
285 run_test 6g "Is new dir in sgid dir inheriting group?"
286
287 test_6h() { # bug 7331
288         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
289         touch $DIR/$tfile || error "touch failed"
290         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
291         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
292                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
293         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
294                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
295 }
296 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
297
298 test_7a() {
299         test_mkdir $DIR/$tdir
300         $MCREATE $DIR/$tdir/$tfile
301         chmod 0666 $DIR/$tdir/$tfile
302         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
303                 error "$tdir/$tfile should be mode 0666"
304 }
305 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
306
307 test_7b() {
308         if [ ! -d $DIR/$tdir ]; then
309                 mkdir $DIR/$tdir
310         fi
311         $MCREATE $DIR/$tdir/$tfile
312         echo -n foo > $DIR/$tdir/$tfile
313         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
314         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
315 }
316 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
317
318 test_8() {
319         test_mkdir $DIR/$tdir
320         touch $DIR/$tdir/$tfile
321         chmod 0666 $DIR/$tdir/$tfile
322         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
323                 error "$tfile mode not 0666"
324 }
325 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
326
327 test_9() {
328         test_mkdir $DIR/$tdir
329         test_mkdir $DIR/$tdir/d2
330         test_mkdir $DIR/$tdir/d2/d3
331         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
332 }
333 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
334
335 test_10() {
336         test_mkdir $DIR/$tdir
337         test_mkdir $DIR/$tdir/d2
338         touch $DIR/$tdir/d2/$tfile
339         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
340                 error "$tdir/d2/$tfile not a file"
341 }
342 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
343
344 test_11() {
345         test_mkdir $DIR/$tdir
346         test_mkdir $DIR/$tdir/d2
347         chmod 0666 $DIR/$tdir/d2
348         chmod 0705 $DIR/$tdir/d2
349         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
350                 error "$tdir/d2 mode not 0705"
351 }
352 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
353
354 test_12() {
355         test_mkdir $DIR/$tdir
356         touch $DIR/$tdir/$tfile
357         chmod 0666 $DIR/$tdir/$tfile
358         chmod 0654 $DIR/$tdir/$tfile
359         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
360                 error "$tdir/d2 mode not 0654"
361 }
362 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
363
364 test_13() {
365         test_mkdir $DIR/$tdir
366         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
367         >  $DIR/$tdir/$tfile
368         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
369                 error "$tdir/$tfile size not 0 after truncate"
370 }
371 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
372
373 test_14() {
374         test_mkdir $DIR/$tdir
375         touch $DIR/$tdir/$tfile
376         rm $DIR/$tdir/$tfile
377         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
378 }
379 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
380
381 test_15() {
382         test_mkdir $DIR/$tdir
383         touch $DIR/$tdir/$tfile
384         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
385         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
386                 error "$tdir/${tfile_2} not a file after rename"
387 }
388 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
389
390 test_16() {
391         test_mkdir $DIR/$tdir
392         touch $DIR/$tdir/$tfile
393         rm -rf $DIR/$tdir/$tfile
394         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
395 }
396 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
397
398 test_17a() {
399         test_mkdir -p $DIR/$tdir
400         touch $DIR/$tdir/$tfile
401         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
402         ls -l $DIR/$tdir
403         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
404                 error "$tdir/l-exist not a symlink"
405         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
406                 error "$tdir/l-exist not referencing a file"
407         rm -f $DIR/$tdir/l-exist
408         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
409 }
410 run_test 17a "symlinks: create, remove (real) =================="
411
412 test_17b() {
413         test_mkdir -p $DIR/$tdir
414         ln -s no-such-file $DIR/$tdir/l-dangle
415         ls -l $DIR/$tdir
416         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
417                 error "$tdir/l-dangle not referencing no-such-file"
418         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
419                 error "$tdir/l-dangle not referencing non-existent file"
420         rm -f $DIR/$tdir/l-dangle
421         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
422 }
423 run_test 17b "symlinks: create, remove (dangling) =============="
424
425 test_17c() { # bug 3440 - don't save failed open RPC for replay
426         test_mkdir -p $DIR/$tdir
427         ln -s foo $DIR/$tdir/$tfile
428         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
429 }
430 run_test 17c "symlinks: open dangling (should return error) ===="
431
432 test_17d() {
433         test_mkdir -p $DIR/$tdir
434         ln -s foo $DIR/$tdir/$tfile
435         touch $DIR/$tdir/$tfile || error "creating to new symlink"
436 }
437 run_test 17d "symlinks: create dangling ========================"
438
439 test_17e() {
440         test_mkdir -p $DIR/$tdir
441         local foo=$DIR/$tdir/$tfile
442         ln -s $foo $foo || error "create symlink failed"
443         ls -l $foo || error "ls -l failed"
444         ls $foo && error "ls not failed" || true
445 }
446 run_test 17e "symlinks: create recursive symlink (should return error) ===="
447
448 test_17f() {
449         test_mkdir -p $DIR/d17f
450         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
455         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/d17f/666
456         ls -l  $DIR/d17f
457 }
458 run_test 17f "symlinks: long and very long symlink name ========================"
459
460 # str_repeat(S, N) generate a string that is string S repeated N times
461 str_repeat() {
462         local s=$1
463         local n=$2
464         local ret=''
465         while [ $((n -= 1)) -ge 0 ]; do
466                 ret=$ret$s
467         done
468         echo $ret
469 }
470
471 # Long symlinks and LU-2241
472 test_17g() {
473         test_mkdir -p $DIR/$tdir
474         local TESTS="59 60 61 4094 4095"
475
476         # Fix for inode size boundary in 2.1.4
477         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
478                 TESTS="4094 4095"
479
480         # Patch not applied to 2.2 or 2.3 branches
481         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
482         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
483                 TESTS="4094 4095"
484
485         # skip long symlink name for rhel6.5.
486         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
487         grep -q '6.5' /etc/redhat-release &>/dev/null &&
488                 TESTS="59 60 61 4062 4063"
489
490         for i in $TESTS; do
491                 local SYMNAME=$(str_repeat 'x' $i)
492                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
493                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
494         done
495 }
496 run_test 17g "symlinks: really long symlink name and inode boundaries"
497
498 test_17h() { #bug 17378
499         remote_mds_nodsh && skip "remote MDS with nodsh" && return
500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
501         local mdt_idx
502         test_mkdir -p $DIR/$tdir
503         if [[ $MDSCOUNT -gt 1 ]]; then
504                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
505         else
506                 mdt_idx=0
507         fi
508         $SETSTRIPE -c -1 $DIR/$tdir
509 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
510         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
511         touch $DIR/$tdir/$tfile || true
512 }
513 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
514
515 test_17i() { #bug 20018
516         remote_mds_nodsh && skip "remote MDS with nodsh" && return
517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
518         test_mkdir -p $DIR/$tdir
519         local foo=$DIR/$tdir/$tfile
520         local mdt_idx
521         if [[ $MDSCOUNT -gt 1 ]]; then
522                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
523         else
524                 mdt_idx=0
525         fi
526         ln -s $foo $foo || error "create symlink failed"
527 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
528         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
529         ls -l $foo && error "error not detected"
530         return 0
531 }
532 run_test 17i "don't panic on short symlink"
533
534 test_17k() { #bug 22301
535         [[ -z "$(which rsync 2>/dev/null)" ]] &&
536                 skip "no rsync command" && return 0
537         rsync --help | grep -q xattr ||
538                 skip_env "$(rsync --version | head -n1) does not support xattrs"
539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
540         test_mkdir -p $DIR/$tdir
541         test_mkdir -p $DIR/$tdir.new
542         touch $DIR/$tdir/$tfile
543         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
544         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
545                 error "rsync failed with xattrs enabled"
546 }
547 run_test 17k "symlinks: rsync with xattrs enabled ========================="
548
549 test_17l() { # LU-279
550         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
551                 skip "no getfattr command" && return 0
552         mkdir -p $DIR/$tdir
553         touch $DIR/$tdir/$tfile
554         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
555         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
556                 # -h to not follow symlinks. -m '' to list all the xattrs.
557                 # grep to remove first line: '# file: $path'.
558                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
559                 do
560                         lgetxattr_size_check $path $xattr ||
561                                 error "lgetxattr_size_check $path $xattr failed"
562                 done
563         done
564 }
565 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
566
567 # LU-1540
568 test_17m() {
569         local short_sym="0123456789"
570         local WDIR=$DIR/${tdir}m
571         local mds_index
572         local devname
573         local cmd
574         local i
575         local rc=0
576
577         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
578         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
579                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
580
581         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
582                 skip "only for ldiskfs MDT" && return 0
583
584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
585
586         mkdir -p $WDIR
587         long_sym=$short_sym
588         # create a long symlink file
589         for ((i = 0; i < 4; ++i)); do
590                 long_sym=${long_sym}${long_sym}
591         done
592
593         echo "create 512 short and long symlink files under $WDIR"
594         for ((i = 0; i < 256; ++i)); do
595                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
596                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
597         done
598
599         echo "erase them"
600         rm -f $WDIR/*
601         sync
602         wait_delete_completed
603
604         echo "recreate the 512 symlink files with a shorter string"
605         for ((i = 0; i < 512; ++i)); do
606                 # rewrite the symlink file with a shorter string
607                 ln -sf ${long_sym} $WDIR/long-$i
608                 ln -sf ${short_sym} $WDIR/short-$i
609         done
610
611         mds_index=$($LFS getstripe -M $WDIR)
612         mds_index=$((mds_index+1))
613         devname=$(mdsdevname $mds_index)
614         cmd="$E2FSCK -fnvd $devname"
615
616         echo "stop and checking mds${mds_index}: $cmd"
617         # e2fsck should not return error
618         stop mds${mds_index} -f
619         do_facet mds${mds_index} $cmd || rc=$?
620
621         start mds${mds_index} $devname $MDS_MOUNT_OPTS
622         df $MOUNT > /dev/null 2>&1
623         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
624                 "short/long symlink MDT: rc=$rc"
625         return $rc
626 }
627 run_test 17m "run e2fsck against MDT which contains short/long symlink"
628
629 check_fs_consistency_17n() {
630         local mdt_index
631         local devname
632         local cmd
633         local rc=0
634
635         for mdt_index in $(seq 1 $MDSCOUNT); do
636                 devname=$(mdsdevname $mdt_index)
637                 cmd="$E2FSCK -fnvd $devname"
638
639                 echo "stop and checking mds${mdt_index}: $cmd"
640                 # e2fsck should not return error
641                 stop mds${mdt_index}
642                 do_facet mds${mdt_index} $cmd || rc=$?
643
644                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
645                 df $MOUNT > /dev/null 2>&1
646                 [ $rc -ne 0 ] && break
647         done
648         return $rc
649 }
650
651 test_17n() {
652         local i
653
654         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
655         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
656                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
657
658         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
659                 skip "only for ldiskfs MDT" && return 0
660
661         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
662
663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
664
665         mkdir -p $DIR/$tdir
666         for ((i=0; i<10; i++)); do
667                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
668                         error "create remote dir error $i"
669                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
670                         error "create files under remote dir failed $i"
671         done
672
673         check_fs_consistency_17n || error "e2fsck report error"
674
675         for ((i=0;i<10;i++)); do
676                 rm -rf $DIR/$tdir/remote_dir_${i} ||
677                         error "destroy remote dir error $i"
678         done
679
680         check_fs_consistency_17n || error "e2fsck report error"
681 }
682 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
683
684 test_17o() {
685         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
686                 skip "Need MDS version at least 2.3.64" && return
687
688         local WDIR=$DIR/${tdir}o
689         local mdt_index
690         local mdtdevname
691         local rc=0
692
693         mkdir -p $WDIR
694         mdt_index=$($LFS getstripe -M $WDIR)
695         mdt_index=$((mdt_index+1))
696         mdtdevname=$(mdsdevname $mdt_index)
697
698         touch $WDIR/$tfile
699         stop mds${mdt_index}
700         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
701
702         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
703         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
704         ls -l $WDIR/$tfile && rc=1
705         do_facet mds${mdt_index} lctl set_param fail_loc=0
706         [[ $rc -ne 0 ]] && error "stat file should fail"
707         true
708 }
709 run_test 17o "stat file with incompat LMA feature"
710
711 test_18() {
712         touch $DIR/f || error "Failed to touch $DIR/f: $?"
713         ls $DIR || error "Failed to ls $DIR: $?"
714 }
715 run_test 18 "touch .../f ; ls ... =============================="
716
717 test_19a() {
718         touch $DIR/$tfile
719         ls -l $DIR
720         rm $DIR/$tfile
721         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
722 }
723 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
724
725 test_19b() {
726         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
727 }
728 run_test 19b "ls -l .../f19 (should return error) =============="
729
730 test_19c() {
731         [ $RUNAS_ID -eq $UID ] &&
732                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
733         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
734 }
735 run_test 19c "$RUNAS touch .../f19 (should return error) =="
736
737 test_19d() {
738         cat $DIR/f19 && error || true
739 }
740 run_test 19d "cat .../f19 (should return error) =============="
741
742 test_20() {
743         touch $DIR/$tfile
744         rm $DIR/$tfile
745         log "1 done"
746         touch $DIR/$tfile
747         rm $DIR/$tfile
748         log "2 done"
749         touch $DIR/$tfile
750         rm $DIR/$tfile
751         log "3 done"
752         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
753 }
754 run_test 20 "touch .../f ; ls -l ... ==========================="
755
756 test_21() {
757         test_mkdir -p $DIR/$tdir
758         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
759         ln -s dangle $DIR/$tdir/link
760         echo foo >> $DIR/$tdir/link
761         cat $DIR/$tdir/dangle
762         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
763         $CHECKSTAT -f -t file $DIR/$tdir/link ||
764                 error "$tdir/link not linked to a file"
765 }
766 run_test 21 "write to dangling link ============================"
767
768 test_22() {
769         WDIR=$DIR/$tdir
770         test_mkdir -p $DIR/$tdir
771         chown $RUNAS_ID:$RUNAS_GID $WDIR
772         (cd $WDIR || error "cd $WDIR failed";
773         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
774         $RUNAS tar xf -)
775         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
776         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
777         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
778 }
779 run_test 22 "unpack tar archive as non-root user ==============="
780
781 # was test_23
782 test_23a() {
783         test_mkdir -p $DIR/$tdir
784         local file=$DIR/$tdir/$tfile
785
786         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
787         openfile -f O_CREAT:O_EXCL $file &&
788                 error "$file recreate succeeded" || true
789 }
790 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
791
792 test_23b() { # bug 18988
793         test_mkdir -p $DIR/$tdir
794         local file=$DIR/$tdir/$tfile
795
796         rm -f $file
797         echo foo > $file || error "write filed"
798         echo bar >> $file || error "append filed"
799         $CHECKSTAT -s 8 $file || error "wrong size"
800         rm $file
801 }
802 run_test 23b "O_APPEND check =========================="
803
804 # rename sanity
805 test_24a() {
806         echo '-- same directory rename'
807         test_mkdir $DIR/$tdir
808         touch $DIR/$tdir/$tfile.1
809         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
810         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
811 }
812 run_test 24a "rename file to non-existent target"
813
814 test_24b() {
815         test_mkdir $DIR/$tdir
816         touch $DIR/$tdir/$tfile.{1,2}
817         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
818         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
819         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
820 }
821 run_test 24b "rename file to existing target"
822
823 test_24c() {
824         test_mkdir $DIR/$tdir
825         test_mkdir $DIR/$tdir/d$testnum.1
826         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
827         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
828         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
829 }
830 run_test 24c "rename directory to non-existent target"
831
832 test_24d() {
833         test_mkdir $DIR/$tdir
834         test_mkdir $DIR/$tdir/d$testnum.1
835         test_mkdir $DIR/$tdir/d$ttestnum.2
836         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
837         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
838         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
839 }
840 run_test 24d "rename directory to existing target"
841
842 test_24e() {
843         echo '-- cross directory renames --'
844         test_mkdir $DIR/R5a
845         test_mkdir $DIR/R5b
846         touch $DIR/R5a/f
847         mv $DIR/R5a/f $DIR/R5b/g
848         $CHECKSTAT -a $DIR/R5a/f || error
849         $CHECKSTAT -t file $DIR/R5b/g || error
850 }
851 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
852
853 test_24f() {
854         test_mkdir $DIR/R6a
855         test_mkdir $DIR/R6b
856         touch $DIR/R6a/f $DIR/R6b/g
857         mv $DIR/R6a/f $DIR/R6b/g
858         $CHECKSTAT -a $DIR/R6a/f || error
859         $CHECKSTAT -t file $DIR/R6b/g || error
860 }
861 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
862
863 test_24g() {
864         test_mkdir $DIR/R7a
865         test_mkdir $DIR/R7b
866         test_mkdir $DIR/R7a/d
867         mv $DIR/R7a/d $DIR/R7b/e
868         $CHECKSTAT -a $DIR/R7a/d || error
869         $CHECKSTAT -t dir $DIR/R7b/e || error
870 }
871 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
872
873 test_24h() {
874         test_mkdir $DIR/R8a
875         test_mkdir $DIR/R8b
876         test_mkdir $DIR/R8a/d
877         test_mkdir $DIR/R8b/e
878         mrename $DIR/R8a/d $DIR/R8b/e
879         $CHECKSTAT -a $DIR/R8a/d || error
880         $CHECKSTAT -t dir $DIR/R8b/e || error
881 }
882 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
883
884 test_24i() {
885         echo "-- rename error cases"
886         test_mkdir $DIR/R9
887         test_mkdir $DIR/R9/a
888         touch $DIR/R9/f
889         mrename $DIR/R9/f $DIR/R9/a
890         $CHECKSTAT -t file $DIR/R9/f || error
891         $CHECKSTAT -t dir  $DIR/R9/a || error
892         $CHECKSTAT -a $DIR/R9/a/f || error
893 }
894 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
895
896 test_24j() {
897         test_mkdir $DIR/R10
898         mrename $DIR/R10/f $DIR/R10/g
899         $CHECKSTAT -t dir $DIR/R10 || error
900         $CHECKSTAT -a $DIR/R10/f || error
901         $CHECKSTAT -a $DIR/R10/g || error
902 }
903 run_test 24j "source does not exist ============================"
904
905 test_24k() {
906         test_mkdir $DIR/R11a
907         test_mkdir $DIR/R11a/d
908         touch $DIR/R11a/f
909         mv $DIR/R11a/f $DIR/R11a/d
910         $CHECKSTAT -a $DIR/R11a/f || error
911         $CHECKSTAT -t file $DIR/R11a/d/f || error
912 }
913 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
914
915 # bug 2429 - rename foo foo foo creates invalid file
916 test_24l() {
917         f="$DIR/f24l"
918         $MULTIOP $f OcNs || error
919 }
920 run_test 24l "Renaming a file to itself ========================"
921
922 test_24m() {
923         f="$DIR/f24m"
924         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
925         # on ext3 this does not remove either the source or target files
926         # though the "expected" operation would be to remove the source
927         $CHECKSTAT -t file ${f} || error "${f} missing"
928         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
929 }
930 run_test 24m "Renaming a file to a hard link to itself ========="
931
932 test_24n() {
933     f="$DIR/f24n"
934     # this stats the old file after it was renamed, so it should fail
935     touch ${f}
936     $CHECKSTAT ${f}
937     mv ${f} ${f}.rename
938     $CHECKSTAT ${f}.rename
939     $CHECKSTAT -a ${f}
940 }
941 run_test 24n "Statting the old file after renaming (Posix rename 2)"
942
943 test_24o() {
944         check_kernel_version 37 || return 0
945         test_mkdir -p $DIR/d24o
946         rename_many -s random -v -n 10 $DIR/d24o
947 }
948 run_test 24o "rename of files during htree split ==============="
949
950 test_24p() {
951         test_mkdir $DIR/R12a
952         test_mkdir $DIR/R12b
953         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
954         mrename $DIR/R12a $DIR/R12b
955         $CHECKSTAT -a $DIR/R12a || error
956         $CHECKSTAT -t dir $DIR/R12b || error
957         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
958         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
959 }
960 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
961
962 cleanup_multiop_pause() {
963         trap 0
964         kill -USR1 $MULTIPID
965 }
966
967 test_24q() {
968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
969         test_mkdir $DIR/R13a
970         test_mkdir $DIR/R13b
971         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
972         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
973         MULTIPID=$!
974
975         trap cleanup_multiop_pause EXIT
976         mrename $DIR/R13a $DIR/R13b
977         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
978         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
979         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
980         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
981         cleanup_multiop_pause
982         wait $MULTIPID || error "multiop close failed"
983 }
984 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
985
986 test_24r() { #bug 3789
987         test_mkdir $DIR/R14a
988         test_mkdir $DIR/R14a/b
989         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
990         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
991         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
992 }
993 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
994
995 test_24s() {
996         test_mkdir $DIR/R15a
997         test_mkdir $DIR/R15a/b
998         test_mkdir $DIR/R15a/b/c
999         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1000         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1001         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1002 }
1003 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1004 test_24t() {
1005         test_mkdir $DIR/R16a
1006         test_mkdir $DIR/R16a/b
1007         test_mkdir $DIR/R16a/b/c
1008         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1009         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1010         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1011 }
1012 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1013
1014 test_24u() { # bug12192
1015         rm -rf $DIR/$tfile
1016         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1017         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1018 }
1019 run_test 24u "create stripe file"
1020
1021 page_size() {
1022         getconf PAGE_SIZE
1023 }
1024
1025 simple_cleanup_common() {
1026         trap 0
1027         rm -rf $DIR/$tdir
1028         wait_delete_completed
1029 }
1030
1031 max_pages_per_rpc() {
1032         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1033 }
1034
1035 test_24v() {
1036         local NRFILES=100000
1037         local FREE_INODES=$(mdt_free_inodes 0)
1038         [[ $FREE_INODES -lt $NRFILES ]] &&
1039                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1040                 return
1041
1042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1043         trap simple_cleanup_common EXIT
1044
1045         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1046         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1047
1048         mkdir -p $DIR/$tdir
1049         createmany -m $DIR/$tdir/$tfile $NRFILES
1050
1051         cancel_lru_locks mdc
1052         lctl set_param mdc.*.stats clear
1053
1054         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1055
1056         # LU-5 large readdir
1057         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1058         #               8 bytes for name(filename is mostly 5 in this test) +
1059         #               8 bytes for luda_type
1060         # take into account of overhead in lu_dirpage header and end mark in
1061         # each page, plus one in RPC_NUM calculation.
1062         DIRENT_SIZE=48
1063         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1064         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1065         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1066                                 awk '/^mds_readpage/ {print $2}')
1067         [[ $mds_readpage -gt $RPC_NUM ]] &&
1068                 error "large readdir doesn't take effect"
1069
1070         simple_cleanup_common
1071 }
1072 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1073
1074 test_24w() { # bug21506
1075         SZ1=234852
1076         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1077         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1078         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1079         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1080         [ "$SZ1" = "$SZ2" ] || \
1081                 error "Error reading at the end of the file $tfile"
1082 }
1083 run_test 24w "Reading a file larger than 4Gb"
1084
1085 test_24x() {
1086         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1088         local MDTIDX=1
1089         local remote_dir=$DIR/$tdir/remote_dir
1090
1091         mkdir -p $DIR/$tdir
1092         $LFS mkdir -i $MDTIDX $remote_dir ||
1093                 error "create remote directory failed"
1094
1095         mkdir -p $DIR/$tdir/src_dir
1096         touch $DIR/$tdir/src_file
1097         mkdir -p $remote_dir/tgt_dir
1098         touch $remote_dir/tgt_file
1099
1100         mrename $remote_dir $DIR/ &&
1101                 error "rename dir cross MDT works!"
1102
1103         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1104                 error "rename dir cross MDT works!"
1105
1106         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1107                 error "rename file cross MDT works!"
1108
1109         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1110                 error "ln file cross MDT should not work!"
1111
1112         rm -rf $DIR/$tdir || error "Can not delete directories"
1113 }
1114 run_test 24x "cross rename/link should be failed"
1115
1116 test_24y() {
1117         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1119         local MDTIDX=1
1120         local remote_dir=$DIR/$tdir/remote_dir
1121
1122         mkdir -p $DIR/$tdir
1123         $LFS mkdir -i $MDTIDX $remote_dir ||
1124                    error "create remote directory failed"
1125
1126         mkdir -p $remote_dir/src_dir
1127         touch $remote_dir/src_file
1128         mkdir -p $remote_dir/tgt_dir
1129         touch $remote_dir/tgt_file
1130
1131         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1132                 error "rename subdir in the same remote dir failed!"
1133
1134         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1135                 error "rename files in the same remote dir failed!"
1136
1137         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1138                 error "link files in the same remote dir failed!"
1139
1140         rm -rf $DIR/$tdir || error "Can not delete directories"
1141 }
1142 run_test 24y "rename/link on the same dir should succeed"
1143
1144 test_24z() {
1145         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1146         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1147         local MDTIDX=1
1148         local remote_src=$DIR/$tdir/remote_dir
1149         local remote_tgt=$DIR/$tdir/remote_tgt
1150
1151         mkdir -p $DIR/$tdir
1152         $LFS mkdir -i $MDTIDX $remote_src ||
1153                    error "create remote directory failed"
1154
1155         $LFS mkdir -i $MDTIDX $remote_tgt ||
1156                    error "create remote directory failed"
1157
1158         mrename $remote_src $remote_tgt &&
1159                 error "rename remote dirs should not work!"
1160
1161         # If target dir does not exists, it should succeed
1162         rm -rf $remote_tgt
1163         mrename $remote_src $remote_tgt ||
1164                 error "rename remote dirs(tgt dir does not exists) failed!"
1165
1166         rm -rf $DIR/$tdir || error "Can not delete directories"
1167 }
1168 run_test 24z "rename one remote dir to another remote dir should fail"
1169
1170 test_24A() { # LU-3182
1171         local NFILES=5000
1172
1173         mkdir -p $DIR/$tdir
1174         createmany -m $DIR/$tdir/$tfile $NFILES
1175         local t=`ls $DIR/$tdir | wc -l`
1176         local u=`ls $DIR/$tdir | sort -u | wc -l`
1177         if [ $t -ne $NFILES -o $u -ne $NFILES ] ; then
1178                 error "Expected $NFILES files, got $t ($u unique)"
1179         fi
1180
1181         rm -rf $DIR/$tdir || error "Can not delete directories"
1182 }
1183 run_test 24A "readdir() returns correct number of entries."
1184
1185 test_25a() {
1186         echo '== symlink sanity ============================================='
1187
1188         test_mkdir $DIR/d25
1189         ln -s d25 $DIR/s25
1190         touch $DIR/s25/foo || error
1191 }
1192 run_test 25a "create file in symlinked directory ==============="
1193
1194 test_25b() {
1195         [ ! -d $DIR/d25 ] && test_25a
1196         $CHECKSTAT -t file $DIR/s25/foo || error
1197 }
1198 run_test 25b "lookup file in symlinked directory ==============="
1199
1200 test_26a() {
1201         test_mkdir $DIR/d26
1202         test_mkdir $DIR/d26/d26-2
1203         ln -s d26/d26-2 $DIR/s26
1204         touch $DIR/s26/foo || error
1205 }
1206 run_test 26a "multiple component symlink ======================="
1207
1208 test_26b() {
1209         test_mkdir -p $DIR/d26b/d26-2
1210         ln -s d26b/d26-2/foo $DIR/s26-2
1211         touch $DIR/s26-2 || error
1212 }
1213 run_test 26b "multiple component symlink at end of lookup ======"
1214
1215 test_26c() {
1216         test_mkdir $DIR/d26.2
1217         touch $DIR/d26.2/foo
1218         ln -s d26.2 $DIR/s26.2-1
1219         ln -s s26.2-1 $DIR/s26.2-2
1220         ln -s s26.2-2 $DIR/s26.2-3
1221         chmod 0666 $DIR/s26.2-3/foo
1222 }
1223 run_test 26c "chain of symlinks ================================"
1224
1225 # recursive symlinks (bug 439)
1226 test_26d() {
1227         ln -s d26-3/foo $DIR/d26-3
1228 }
1229 run_test 26d "create multiple component recursive symlink ======"
1230
1231 test_26e() {
1232         [ ! -h $DIR/d26-3 ] && test_26d
1233         rm $DIR/d26-3
1234 }
1235 run_test 26e "unlink multiple component recursive symlink ======"
1236
1237 # recursive symlinks (bug 7022)
1238 test_26f() {
1239         test_mkdir -p $DIR/$tdir
1240         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1241         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1242         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1243         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1244         cd $tfile                || error "cd $tfile failed"
1245         ln -s .. dotdot          || error "ln dotdot failed"
1246         ln -s dotdot/lndir lndir || error "ln lndir failed"
1247         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1248         output=`ls $tfile/$tfile/lndir/bar1`
1249         [ "$output" = bar1 ] && error "unexpected output"
1250         rm -r $tfile             || error "rm $tfile failed"
1251         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1252 }
1253 run_test 26f "rm -r of a directory which has recursive symlink ="
1254
1255 test_27a() {
1256         echo '== stripe sanity =============================================='
1257         test_mkdir -p $DIR/d27 || error "mkdir failed"
1258         $GETSTRIPE $DIR/d27
1259         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1260         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1261         log "== test_27a: write to one stripe file ========================="
1262         cp /etc/hosts $DIR/d27/f0 || error
1263 }
1264 run_test 27a "one stripe file =================================="
1265
1266 test_27b() {
1267         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1268         test_mkdir -p $DIR/d27
1269         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1270         $GETSTRIPE -c $DIR/d27/f01
1271         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1272                 error "two-stripe file doesn't have two stripes"
1273
1274         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1275 }
1276 run_test 27b "create and write to two stripe file"
1277
1278 test_27d() {
1279         test_mkdir -p $DIR/d27
1280         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1281         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1282         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1283 }
1284 run_test 27d "create file with default settings ================"
1285
1286 test_27e() {
1287         test_mkdir -p $DIR/d27
1288         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1289         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1290         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1291 }
1292 run_test 27e "setstripe existing file (should return error) ======"
1293
1294 test_27f() {
1295         test_mkdir -p $DIR/d27
1296         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1297         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1298         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1299 }
1300 run_test 27f "setstripe with bad stripe size (should return error)"
1301
1302 test_27g() {
1303         test_mkdir -p $DIR/d27
1304         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1305         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1306                 error "$DIR/d27/fnone has object"
1307 }
1308 run_test 27g "$GETSTRIPE with no objects"
1309
1310 test_27i() {
1311         touch $DIR/d27/fsome || error "touch failed"
1312         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1313 }
1314 run_test 27i "$GETSTRIPE with some objects"
1315
1316 test_27j() {
1317         test_mkdir -p $DIR/d27
1318         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1319 }
1320 run_test 27j "setstripe with bad stripe offset (should return error)"
1321
1322 test_27k() { # bug 2844
1323         test_mkdir -p $DIR/d27
1324         FILE=$DIR/d27/f27k
1325         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1326         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1327         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1328         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1329         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1330         dd if=/dev/zero of=$FILE bs=4k count=1
1331         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1332         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1333 }
1334 run_test 27k "limit i_blksize for broken user apps ============="
1335
1336 test_27l() {
1337         test_mkdir -p $DIR/d27
1338         mcreate $DIR/f27l || error "creating file"
1339         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1340                 error "setstripe should have failed" || true
1341 }
1342 run_test 27l "check setstripe permissions (should return error)"
1343
1344 test_27m() {
1345         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1346                 return
1347         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1348                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1349                 return
1350         fi
1351         trap simple_cleanup_common EXIT
1352         test_mkdir -p $DIR/$tdir
1353         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1354         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1355                 error "dd should fill OST0"
1356         i=2
1357         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1358                 i=$((i + 1))
1359                 [ $i -gt 256 ] && break
1360         done
1361         i=$((i + 1))
1362         touch $DIR/$tdir/f27m_$i
1363         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1364                 error "OST0 was full but new created file still use it"
1365         i=$((i + 1))
1366         touch $DIR/$tdir/f27m_$i
1367         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1368                 error "OST0 was full but new created file still use it"
1369         simple_cleanup_common
1370 }
1371 run_test 27m "create file while OST0 was full =================="
1372
1373 sleep_maxage() {
1374         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1375         sleep $DELAY
1376 }
1377
1378 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1379 # if the OST isn't full anymore.
1380 reset_enospc() {
1381         local OSTIDX=${1:-""}
1382
1383         local list=$(comma_list $(osts_nodes))
1384         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1385
1386         do_nodes $list lctl set_param fail_loc=0
1387         sync    # initiate all OST_DESTROYs from MDS to OST
1388         sleep_maxage
1389 }
1390
1391 exhaust_precreations() {
1392         local OSTIDX=$1
1393         local FAILLOC=$2
1394         local FAILIDX=${3:-$OSTIDX}
1395
1396         test_mkdir -p $DIR/$tdir
1397         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1398         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1399
1400         local OST=$(ostname_from_index $OSTIDX)
1401         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1402                           sed -e 's/_UUID$//;s/^.*-//')
1403
1404         # on the mdt's osc
1405         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1406         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1407                         osc.$mdtosc_proc1.prealloc_last_id)
1408         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1409                         osc.$mdtosc_proc1.prealloc_next_id)
1410
1411         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1412         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1413
1414         test_mkdir -p $DIR/$tdir/${OST}
1415         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1416 #define OBD_FAIL_OST_ENOSPC              0x215
1417         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1418         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1419         echo "Creating to objid $last_id on ost $OST..."
1420         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1421         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1422         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1423         sleep_maxage
1424 }
1425
1426 exhaust_all_precreations() {
1427         local i
1428         for (( i=0; i < OSTCOUNT; i++ )) ; do
1429                 exhaust_precreations $i $1 -1
1430         done
1431 }
1432
1433 test_27n() {
1434         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1436         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1437         remote_ost_nodsh && skip "remote OST with nodsh" && return
1438
1439         reset_enospc
1440         rm -f $DIR/$tdir/$tfile
1441         exhaust_precreations 0 0x80000215
1442         $SETSTRIPE -c -1 $DIR/$tdir
1443         touch $DIR/$tdir/$tfile || error
1444         $GETSTRIPE $DIR/$tdir/$tfile
1445         reset_enospc
1446 }
1447 run_test 27n "create file with some full OSTs =================="
1448
1449 test_27o() {
1450         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1451         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1452         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1453         remote_ost_nodsh && skip "remote OST with nodsh" && return
1454
1455         reset_enospc
1456         rm -f $DIR/$tdir/$tfile
1457         exhaust_all_precreations 0x215
1458
1459         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1460
1461         reset_enospc
1462         rm -rf $DIR/$tdir/*
1463 }
1464 run_test 27o "create file with all full OSTs (should error) ===="
1465
1466 test_27p() {
1467         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1468         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1469         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1470         remote_ost_nodsh && skip "remote OST with nodsh" && return
1471
1472         reset_enospc
1473         rm -f $DIR/$tdir/$tfile
1474         test_mkdir -p $DIR/$tdir
1475
1476         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1477         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1478         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1479
1480         exhaust_precreations 0 0x80000215
1481         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1482         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1483         $GETSTRIPE $DIR/$tdir/$tfile
1484
1485         reset_enospc
1486 }
1487 run_test 27p "append to a truncated file with some full OSTs ==="
1488
1489 test_27q() {
1490         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1492         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1493         remote_ost_nodsh && skip "remote OST with nodsh" && return
1494
1495         reset_enospc
1496         rm -f $DIR/$tdir/$tfile
1497
1498         test_mkdir -p $DIR/$tdir
1499         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1500         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1501         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1502
1503         exhaust_all_precreations 0x215
1504
1505         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1506         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1507
1508         reset_enospc
1509 }
1510 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1511
1512 test_27r() {
1513         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1515         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1516         remote_ost_nodsh && skip "remote OST with nodsh" && return
1517
1518         reset_enospc
1519         rm -f $DIR/$tdir/$tfile
1520         exhaust_precreations 0 0x80000215
1521
1522         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1523
1524         reset_enospc
1525 }
1526 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1527
1528 test_27s() { # bug 10725
1529         test_mkdir -p $DIR/$tdir
1530         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1531         local stripe_count=0
1532         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1533         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1534                 error "stripe width >= 2^32 succeeded" || true
1535
1536 }
1537 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1538
1539 test_27t() { # bug 10864
1540         WDIR=$(pwd)
1541         WLFS=$(which lfs)
1542         cd $DIR
1543         touch $tfile
1544         $WLFS getstripe $tfile
1545         cd $WDIR
1546 }
1547 run_test 27t "check that utils parse path correctly"
1548
1549 test_27u() { # bug 4900
1550         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1551         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1552         local index
1553         local list=$(comma_list $(mdts_nodes))
1554
1555 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1556         do_nodes $list $LCTL set_param fail_loc=0x139
1557         test_mkdir -p $DIR/$tdir
1558         rm -rf $DIR/$tdir/*
1559         createmany -o $DIR/$tdir/t- 1000
1560         do_nodes $list $LCTL set_param fail_loc=0
1561
1562         TLOG=$DIR/$tfile.getstripe
1563         $GETSTRIPE $DIR/$tdir > $TLOG
1564         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1565         unlinkmany $DIR/$tdir/t- 1000
1566         [[ $OBJS -gt 0 ]] &&
1567                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1568 }
1569 run_test 27u "skip object creation on OSC w/o objects =========="
1570
1571 test_27v() { # bug 4900
1572         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1574         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1575         remote_ost_nodsh && skip "remote OST with nodsh" && return
1576
1577         exhaust_all_precreations 0x215
1578         reset_enospc
1579
1580         test_mkdir -p $DIR/$tdir
1581         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1582
1583         touch $DIR/$tdir/$tfile
1584         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1585         # all except ost1
1586         for (( i=1; i < OSTCOUNT; i++ )); do
1587                 do_facet ost$i lctl set_param fail_loc=0x705
1588         done
1589         local START=`date +%s`
1590         createmany -o $DIR/$tdir/$tfile 32
1591
1592         local FINISH=`date +%s`
1593         local TIMEOUT=`lctl get_param -n timeout`
1594         local PROCESS=$((FINISH - START))
1595         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1596                error "$FINISH - $START >= $TIMEOUT / 2"
1597         sleep $((TIMEOUT / 2 - PROCESS))
1598         reset_enospc
1599 }
1600 run_test 27v "skip object creation on slow OST ================="
1601
1602 test_27w() { # bug 10997
1603         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1604         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1605         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1606                 error "stripe size $size != 65536" || true
1607         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1608                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1609 }
1610 run_test 27w "check $SETSTRIPE -S option"
1611
1612 test_27wa() {
1613         [[ $OSTCOUNT -lt 2 ]] &&
1614                 skip_env "skipping multiple stripe count/offset test" && return
1615
1616         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1617         for i in $(seq 1 $OSTCOUNT); do
1618                 offset=$((i - 1))
1619                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1620                         error "setstripe -c $i -i $offset failed"
1621                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1622                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1623                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1624                 [ $index -ne $offset ] &&
1625                         error "stripe offset $index != $offset" || true
1626         done
1627 }
1628 run_test 27wa "check $SETSTRIPE -c -i options"
1629
1630 test_27x() {
1631         remote_ost_nodsh && skip "remote OST with nodsh" && return
1632         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1633         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1634         OFFSET=$(($OSTCOUNT - 1))
1635         OSTIDX=0
1636         local OST=$(ostname_from_index $OSTIDX)
1637
1638         test_mkdir -p $DIR/$tdir
1639         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1640         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1641         sleep_maxage
1642         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1643         for i in `seq 0 $OFFSET`; do
1644                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1645                 error "OST0 was degraded but new created file still use it"
1646         done
1647         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1648 }
1649 run_test 27x "create files while OST0 is degraded"
1650
1651 test_27y() {
1652         [[ $OSTCOUNT -lt 2 ]] &&
1653                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1654         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1655         remote_ost_nodsh && skip "remote OST with nodsh" && return
1656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1657
1658         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1659         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1660             osc.$mdtosc.prealloc_last_id)
1661         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1662             osc.$mdtosc.prealloc_next_id)
1663         local fcount=$((last_id - next_id))
1664         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1665         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1666
1667         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1668                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1669         local OST_DEACTIVE_IDX=-1
1670         local OSC
1671         local OSTIDX
1672         local OST
1673
1674         for OSC in $MDS_OSCS; do
1675                 OST=$(osc_to_ost $OSC)
1676                 OSTIDX=$(index_from_ostuuid $OST)
1677                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1678                         OST_DEACTIVE_IDX=$OSTIDX
1679                 fi
1680                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1681                         echo $OSC "is Deactivated:"
1682                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1683                 fi
1684         done
1685
1686         OSTIDX=$(index_from_ostuuid $OST)
1687         mkdir -p $DIR/$tdir
1688         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1689
1690         for OSC in $MDS_OSCS; do
1691                 OST=$(osc_to_ost $OSC)
1692                 OSTIDX=$(index_from_ostuuid $OST)
1693                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1694                         echo $OST "is degraded:"
1695                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1696                                                 obdfilter.$OST.degraded=1
1697                 fi
1698         done
1699
1700         sleep_maxage
1701         createmany -o $DIR/$tdir/$tfile $fcount
1702
1703         for OSC in $MDS_OSCS; do
1704                 OST=$(osc_to_ost $OSC)
1705                 OSTIDX=$(index_from_ostuuid $OST)
1706                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1707                         echo $OST "is recovered from degraded:"
1708                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1709                                                 obdfilter.$OST.degraded=0
1710                 else
1711                         do_facet $SINGLEMDS lctl --device %$OSC activate
1712                 fi
1713         done
1714
1715         # all osp devices get activated, hence -1 stripe count restored
1716         local stripecnt=0
1717
1718         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1719         # devices get activated.
1720         sleep_maxage
1721         $SETSTRIPE -c -1 $DIR/$tfile
1722         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1723         rm -f $DIR/$tfile
1724         [ $stripecnt -ne $OSTCOUNT ] &&
1725                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1726         return 0
1727 }
1728 run_test 27y "create files while OST0 is degraded and the rest inactive"
1729
1730 check_seq_oid()
1731 {
1732         log "check file $1"
1733
1734         lmm_count=$($GETSTRIPE -c $1)
1735         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1736         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1737
1738         local old_ifs="$IFS"
1739         IFS=$'[:]'
1740         fid=($($LFS path2fid $1))
1741         IFS="$old_ifs"
1742
1743         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1744         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1745
1746         # compare lmm_seq and lu_fid->f_seq
1747         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1748         # compare lmm_object_id and lu_fid->oid
1749         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1750
1751         # check the trusted.fid attribute of the OST objects of the file
1752         local have_obdidx=false
1753         local stripe_nr=0
1754         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1755                 # skip lines up to and including "obdidx"
1756                 [ -z "$obdidx" ] && break
1757                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1758                 $have_obdidx || continue
1759
1760                 local ost=$((obdidx + 1))
1761                 local dev=$(ostdevname $ost)
1762                 local oid_hex
1763
1764                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1765                         echo "Currently only works with ldiskfs-based OSTs"
1766                         continue
1767                 fi
1768
1769                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1770
1771                 #don't unmount/remount the OSTs if we don't need to do that
1772                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1773                 # update too, until that use mount/ll_decode_filter_fid/mount
1774                 local dir=$(facet_mntpt ost$ost)
1775                 local opts=${OST_MOUNT_OPTS}
1776
1777                 if !  do_facet ost$ost test -b ${dev}; then
1778                         opts=$(csa_add "$opts" -o loop)
1779                 fi
1780
1781                 stop ost$ost
1782                 do_facet ost$ost mount -t $(facet_fstype ost$ost) $opts $dev $dir ||
1783                         { error "mounting $dev as $FSTYPE failed"; return 3; }
1784
1785                 seq=$(echo $seq | sed -e "s/^0x//g")
1786                 if [ $seq == 0 ]; then
1787                         oid_hex=$(echo $oid)
1788                 else
1789                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1790                 fi
1791                 local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid_hex)
1792                 local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1793                 do_facet ost$ost umount -d $dir
1794                 start ost$ost $dev $OST_MOUNT_OPTS
1795
1796                 # re-enable when debugfs will understand new filter_fid
1797                 #local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1798                 #           $dev 2>/dev/null" | grep "parent=")
1799
1800                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1801
1802                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1803
1804                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1805                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1806                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1807                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1808                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1809                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1810
1811                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1812                 [ $ff_pseq = $lmm_seq ] ||
1813                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1814                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1815                 [ $ff_poid = $lmm_oid ] ||
1816                         error "FF parent OID $ff_poid != $lmm_oid"
1817                 [ $ff_pstripe = $stripe_nr ] ||
1818                         error "FF stripe $ff_pstripe != $stripe_nr"
1819
1820                 stripe_nr=$((stripe_nr + 1))
1821         done
1822 }
1823
1824 test_27z() {
1825         remote_ost_nodsh && skip "remote OST with nodsh" && return
1826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1827         test_mkdir -p $DIR/$tdir
1828
1829         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1830                 { error "setstripe -c -1 failed"; return 1; }
1831         # We need to send a write to every object to get parent FID info set.
1832         # This _should_ also work for setattr, but does not currently.
1833         # touch $DIR/$tdir/$tfile-1 ||
1834         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1835                 { error "dd $tfile-1 failed"; return 2; }
1836         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1837                 { error "setstripe -c -1 failed"; return 3; }
1838         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1839                 { error "dd $tfile-2 failed"; return 4; }
1840
1841         # make sure write RPCs have been sent to OSTs
1842         sync; sleep 5; sync
1843
1844         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1845         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1846 }
1847 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1848
1849 test_27A() { # b=19102
1850         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1851         local restore_size=$($GETSTRIPE -S $MOUNT)
1852         local restore_count=$($GETSTRIPE -c $MOUNT)
1853         local restore_offset=$($GETSTRIPE -i $MOUNT)
1854         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1855         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1856                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1857         local default_size=$($GETSTRIPE -S $MOUNT)
1858         local default_offset=$($GETSTRIPE -i $MOUNT)
1859         local dsize=$((1024 * 1024))
1860         [ $default_size -eq $dsize ] ||
1861                 error "stripe size $default_size != $dsize"
1862         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1863         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1864 }
1865 run_test 27A "check filesystem-wide default LOV EA values"
1866
1867 test_27B() { # LU-2523
1868         test_mkdir -p $DIR/$tdir
1869         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1870         touch $DIR/$tdir/f0
1871         # open f1 with O_LOV_DELAY_CREATE
1872         # rename f0 onto f1
1873         # call setstripe ioctl on open file descriptor for f1
1874         # close
1875         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1876                 $DIR/$tdir/f0
1877
1878         rm -f $DIR/$tdir/f1
1879         # open f1 with O_LOV_DELAY_CREATE
1880         # unlink f1
1881         # call setstripe ioctl on open file descriptor for f1
1882         # close
1883         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1884
1885         # Allow multiop to fail in imitation of NFS's busted semantics.
1886         true
1887 }
1888 run_test 27B "call setstripe on open unlinked file/rename victim"
1889
1890 test_27C() { #LU-2871
1891         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1892
1893         declare -a ost_idx
1894         local index
1895         local found
1896         local i
1897         local j
1898
1899         test_mkdir -p $DIR/$tdir
1900         cd $DIR/$tdir
1901         for i in $(seq 0 $((OSTCOUNT - 1))); do
1902                 # set stripe across all OSTs starting from OST$i
1903                 $SETSTRIPE -i $i -c -1 $tfile$i
1904                 # get striping information
1905                 ost_idx=($($GETSTRIPE $tfile$i |
1906                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1907                 echo ${ost_idx[@]}
1908
1909                 # check the layout
1910                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1911                         error "${#ost_idx[@]} != $OSTCOUNT"
1912
1913                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1914                         found=0
1915                         for j in $(echo ${ost_idx[@]}); do
1916                                 if [ $index -eq $j ]; then
1917                                         found=1
1918                                         break
1919                                 fi
1920                         done
1921                         [ $found = 1 ] ||
1922                                 error "Can not find $index in ${ost_idx[@]}"
1923                 done
1924         done
1925 }
1926 run_test 27C "check full striping across all OSTs"
1927
1928 # createtest also checks that device nodes are created and
1929 # then visible correctly (#2091)
1930 test_28() { # bug 2091
1931         test_mkdir $DIR/d28
1932         $CREATETEST $DIR/d28/ct || error
1933 }
1934 run_test 28 "create/mknod/mkdir with bad file types ============"
1935
1936 test_29() {
1937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1938         cancel_lru_locks mdc
1939         test_mkdir $DIR/d29
1940         touch $DIR/d29/foo
1941         log 'first d29'
1942         ls -l $DIR/d29
1943
1944         declare -i LOCKCOUNTORIG=0
1945         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1946                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1947         done
1948         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1949
1950         declare -i LOCKUNUSEDCOUNTORIG=0
1951         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1952                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1953         done
1954
1955         log 'second d29'
1956         ls -l $DIR/d29
1957         log 'done'
1958
1959         declare -i LOCKCOUNTCURRENT=0
1960         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1961                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1962         done
1963
1964         declare -i LOCKUNUSEDCOUNTCURRENT=0
1965         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1966                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1967         done
1968
1969         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
1970                 $LCTL set_param -n ldlm.dump_namespaces ""
1971                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1972                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1973                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1974                 return 2
1975         fi
1976         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
1977                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1978                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1979                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1980                 return 3
1981         fi
1982 }
1983 run_test 29 "IT_GETATTR regression  ============================"
1984
1985 test_30a() { # was test_30
1986         cp $(which ls) $DIR || cp /bin/ls $DIR
1987         $DIR/ls / || error
1988         rm $DIR/ls
1989 }
1990 run_test 30a "execute binary from Lustre (execve) =============="
1991
1992 test_30b() {
1993         cp `which ls` $DIR || cp /bin/ls $DIR
1994         chmod go+rx $DIR/ls
1995         $RUNAS $DIR/ls / || error
1996         rm $DIR/ls
1997 }
1998 run_test 30b "execute binary from Lustre as non-root ==========="
1999
2000 test_30c() { # b=22376
2001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2002         cp `which ls` $DIR || cp /bin/ls $DIR
2003         chmod a-rw $DIR/ls
2004         cancel_lru_locks mdc
2005         cancel_lru_locks osc
2006         $RUNAS $DIR/ls / || error
2007         rm -f $DIR/ls
2008 }
2009 run_test 30c "execute binary from Lustre without read perms ===="
2010
2011 test_31a() {
2012         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2013         $CHECKSTAT -a $DIR/f31 || error
2014 }
2015 run_test 31a "open-unlink file =================================="
2016
2017 test_31b() {
2018         touch $DIR/f31 || error
2019         ln $DIR/f31 $DIR/f31b || error
2020         $MULTIOP $DIR/f31b Ouc || error
2021         $CHECKSTAT -t file $DIR/f31 || error
2022 }
2023 run_test 31b "unlink file with multiple links while open ======="
2024
2025 test_31c() {
2026         touch $DIR/f31 || error
2027         ln $DIR/f31 $DIR/f31c || error
2028         multiop_bg_pause $DIR/f31 O_uc || return 1
2029         MULTIPID=$!
2030         $MULTIOP $DIR/f31c Ouc
2031         kill -USR1 $MULTIPID
2032         wait $MULTIPID
2033 }
2034 run_test 31c "open-unlink file with multiple links ============="
2035
2036 test_31d() {
2037         opendirunlink $DIR/d31d $DIR/d31d || error
2038         $CHECKSTAT -a $DIR/d31d || error
2039 }
2040 run_test 31d "remove of open directory ========================="
2041
2042 test_31e() { # bug 2904
2043         check_kernel_version 34 || return 0
2044         openfilleddirunlink $DIR/d31e || error
2045 }
2046 run_test 31e "remove of open non-empty directory ==============="
2047
2048 test_31f() { # bug 4554
2049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2050         set -vx
2051         test_mkdir $DIR/d31f
2052         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2053         cp /etc/hosts $DIR/d31f
2054         ls -l $DIR/d31f
2055         $GETSTRIPE $DIR/d31f/hosts
2056         multiop_bg_pause $DIR/d31f D_c || return 1
2057         MULTIPID=$!
2058
2059         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2060         test_mkdir $DIR/d31f
2061         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2062         cp /etc/hosts $DIR/d31f
2063         ls -l $DIR/d31f
2064         $GETSTRIPE $DIR/d31f/hosts
2065         multiop_bg_pause $DIR/d31f D_c || return 1
2066         MULTIPID2=$!
2067
2068         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2069         wait $MULTIPID || error "first opendir $MULTIPID failed"
2070
2071         sleep 6
2072
2073         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2074         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2075         set +vx
2076 }
2077 run_test 31f "remove of open directory with open-unlink file ==="
2078
2079 test_31g() {
2080         echo "-- cross directory link --"
2081         test_mkdir $DIR/d31ga
2082         test_mkdir $DIR/d31gb
2083         touch $DIR/d31ga/f
2084         ln $DIR/d31ga/f $DIR/d31gb/g
2085         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
2086         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
2087         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
2088         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
2089 }
2090 run_test 31g "cross directory link==============="
2091
2092 test_31h() {
2093         echo "-- cross directory link --"
2094         test_mkdir $DIR/d31h
2095         test_mkdir $DIR/d31h/dir
2096         touch $DIR/d31h/f
2097         ln $DIR/d31h/f $DIR/d31h/dir/g
2098         $CHECKSTAT -t file $DIR/d31h/f || error "source"
2099         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
2100         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
2101         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
2102 }
2103 run_test 31h "cross directory link under child==============="
2104
2105 test_31i() {
2106         echo "-- cross directory link --"
2107         test_mkdir $DIR/d31i
2108         test_mkdir $DIR/d31i/dir
2109         touch $DIR/d31i/dir/f
2110         ln $DIR/d31i/dir/f $DIR/d31i/g
2111         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
2112         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
2113         $CHECKSTAT -t file $DIR/d31i/g || error "target"
2114         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
2115 }
2116 run_test 31i "cross directory link under parent==============="
2117
2118
2119 test_31j() {
2120         test_mkdir $DIR/d31j
2121         test_mkdir $DIR/d31j/dir1
2122         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
2123         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
2124         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
2125         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
2126         return 0
2127 }
2128 run_test 31j "link for directory==============="
2129
2130
2131 test_31k() {
2132         test_mkdir $DIR/d31k
2133         touch $DIR/d31k/s
2134         touch $DIR/d31k/exist
2135         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
2136         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
2137         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
2138         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
2139         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
2140         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
2141         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
2142         return 0
2143 }
2144 run_test 31k "link to file: the same, non-existing, dir==============="
2145
2146 test_31m() {
2147         test_mkdir $DIR/d31m
2148         touch $DIR/d31m/s
2149         test_mkdir $DIR/d31m2
2150         touch $DIR/d31m2/exist
2151         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2152         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2153         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2154         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2155         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2156         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2157         return 0
2158 }
2159 run_test 31m "link to file: the same, non-existing, dir==============="
2160
2161 test_31n() {
2162         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2163         nlink=$(stat --format=%h $DIR/$tfile)
2164         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2165         local fd=$(free_fd)
2166         local cmd="exec $fd<$DIR/$tfile"
2167         eval $cmd
2168         cmd="exec $fd<&-"
2169         trap "eval $cmd" EXIT
2170         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2171         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2172         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2173         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2174         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2175         eval $cmd
2176 }
2177 run_test 31n "check link count of unlinked file"
2178
2179 link_one() {
2180         local TEMPNAME=$(mktemp $1_XXXXXX)
2181         mlink $TEMPNAME $1 2> /dev/null &&
2182                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2183         munlink $TEMPNAME
2184 }
2185
2186 test_31o() { # LU-2901
2187         mkdir -p $DIR/$tdir
2188         for LOOP in $(seq 100); do
2189                 rm -f $DIR/$tdir/$tfile*
2190                 for THREAD in $(seq 8); do
2191                         link_one $DIR/$tdir/$tfile.$LOOP &
2192                 done
2193                 wait
2194                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2195                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2196                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2197                         break || true
2198         done
2199 }
2200 run_test 31o "duplicate hard links with same filename"
2201
2202 cleanup_test32_mount() {
2203         trap 0
2204         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2205 }
2206
2207 test_32a() {
2208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2209         echo "== more mountpoints and symlinks ================="
2210         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2211         trap cleanup_test32_mount EXIT
2212         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2213         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2214         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2215         cleanup_test32_mount
2216 }
2217 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2218
2219 test_32b() {
2220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2221         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2222         trap cleanup_test32_mount EXIT
2223         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2224         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2225         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2226         cleanup_test32_mount
2227 }
2228 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2229
2230 test_32c() {
2231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2232         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2233         trap cleanup_test32_mount EXIT
2234         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2235         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2236         test_mkdir -p $DIR/$tdir/d2/test_dir
2237         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2238         cleanup_test32_mount
2239 }
2240 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2241
2242 test_32d() {
2243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2244         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2245         trap cleanup_test32_mount EXIT
2246         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2247         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2248         test_mkdir -p $DIR/$tdir/d2/test_dir
2249         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2250         cleanup_test32_mount
2251 }
2252 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2253
2254 test_32e() {
2255         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2256         test_mkdir -p $DIR/d32e/tmp
2257         TMP_DIR=$DIR/d32e/tmp
2258         ln -s $DIR/d32e $TMP_DIR/symlink11
2259         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2260         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2261         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2262 }
2263 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2264
2265 test_32f() {
2266         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2267         test_mkdir -p $DIR/d32f/tmp
2268         TMP_DIR=$DIR/d32f/tmp
2269         ln -s $DIR/d32f $TMP_DIR/symlink11
2270         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2271         ls $DIR/d32f/tmp/symlink11  || error
2272         ls $DIR/d32f/symlink01 || error
2273 }
2274 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2275
2276 test_32g() {
2277         TMP_DIR=$DIR/$tdir/tmp
2278         test_mkdir -p $DIR/$tdir/tmp
2279         test_mkdir $DIR/${tdir}2
2280         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2281         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2282         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2283         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2284         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2285         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2286 }
2287 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2288
2289 test_32h() {
2290         rm -fr $DIR/$tdir $DIR/${tdir}2
2291         TMP_DIR=$DIR/$tdir/tmp
2292         test_mkdir -p $DIR/$tdir/tmp
2293         test_mkdir $DIR/${tdir}2
2294         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2295         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2296         ls $TMP_DIR/symlink12 || error
2297         ls $DIR/$tdir/symlink02  || error
2298 }
2299 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2300
2301 test_32i() {
2302         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2303         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2304         trap cleanup_test32_mount EXIT
2305         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2306         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2307         touch $DIR/$tdir/test_file
2308         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2309         cleanup_test32_mount
2310 }
2311 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2312
2313 test_32j() {
2314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2315         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2316         trap cleanup_test32_mount EXIT
2317         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2318         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2319         touch $DIR/$tdir/test_file
2320         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2321         cleanup_test32_mount
2322 }
2323 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2324
2325 test_32k() {
2326         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2327         rm -fr $DIR/$tdir
2328         trap cleanup_test32_mount EXIT
2329         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2330         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2331         test_mkdir -p $DIR/$tdir/d2
2332         touch $DIR/$tdir/d2/test_file || error
2333         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2334         cleanup_test32_mount
2335 }
2336 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2337
2338 test_32l() {
2339         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2340         rm -fr $DIR/$tdir
2341         trap cleanup_test32_mount EXIT
2342         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2343         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2344         test_mkdir -p $DIR/$tdir/d2
2345         touch $DIR/$tdir/d2/test_file
2346         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2347         cleanup_test32_mount
2348 }
2349 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2350
2351 test_32m() {
2352         rm -fr $DIR/d32m
2353         test_mkdir -p $DIR/d32m/tmp
2354         TMP_DIR=$DIR/d32m/tmp
2355         ln -s $DIR $TMP_DIR/symlink11
2356         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2357         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2358         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2359 }
2360 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2361
2362 test_32n() {
2363         rm -fr $DIR/d32n
2364         test_mkdir -p $DIR/d32n/tmp
2365         TMP_DIR=$DIR/d32n/tmp
2366         ln -s $DIR $TMP_DIR/symlink11
2367         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2368         ls -l $DIR/d32n/tmp/symlink11  || error
2369         ls -l $DIR/d32n/symlink01 || error
2370 }
2371 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2372
2373 test_32o() {
2374         rm -fr $DIR/d32o $DIR/$tfile
2375         touch $DIR/$tfile
2376         test_mkdir -p $DIR/d32o/tmp
2377         TMP_DIR=$DIR/d32o/tmp
2378         ln -s $DIR/$tfile $TMP_DIR/symlink12
2379         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2380         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2381         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2382         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2383         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2384 }
2385 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2386
2387 test_32p() {
2388     log 32p_1
2389         rm -fr $DIR/d32p
2390     log 32p_2
2391         rm -f $DIR/$tfile
2392     log 32p_3
2393         touch $DIR/$tfile
2394     log 32p_4
2395         test_mkdir -p $DIR/d32p/tmp
2396     log 32p_5
2397         TMP_DIR=$DIR/d32p/tmp
2398     log 32p_6
2399         ln -s $DIR/$tfile $TMP_DIR/symlink12
2400     log 32p_7
2401         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2402     log 32p_8
2403         cat $DIR/d32p/tmp/symlink12 || error
2404     log 32p_9
2405         cat $DIR/d32p/symlink02 || error
2406     log 32p_10
2407 }
2408 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2409
2410 cleanup_testdir_mount() {
2411         trap 0
2412         $UMOUNT -d $DIR/$tdir
2413 }
2414
2415 test_32q() {
2416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2417         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2418         trap cleanup_testdir_mount EXIT
2419         test_mkdir -p $DIR/$tdir
2420         touch $DIR/$tdir/under_the_mount
2421         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2422         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2423         cleanup_testdir_mount
2424 }
2425 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2426
2427 test_32r() {
2428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2429         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2430         trap cleanup_testdir_mount EXIT
2431         test_mkdir -p $DIR/$tdir
2432         touch $DIR/$tdir/under_the_mount
2433         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2434         ls $DIR/$tdir | grep -q under_the_mount && error || true
2435         cleanup_testdir_mount
2436 }
2437 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2438
2439 test_33aa() {
2440         rm -f $DIR/$tfile
2441         touch $DIR/$tfile
2442         chmod 444 $DIR/$tfile
2443         chown $RUNAS_ID $DIR/$tfile
2444         log 33_1
2445         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2446         log 33_2
2447 }
2448 run_test 33aa "write file with mode 444 (should return error) ===="
2449
2450 test_33a() {
2451         rm -fr $DIR/d33
2452         test_mkdir -p $DIR/d33
2453         chown $RUNAS_ID $DIR/d33
2454         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2455         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2456                 error "open RDWR" || true
2457 }
2458 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2459
2460 test_33b() {
2461         rm -fr $DIR/d33
2462         test_mkdir -p $DIR/d33
2463         chown $RUNAS_ID $DIR/d33
2464         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2465 }
2466 run_test 33b "test open file with malformed flags (No panic and return error)"
2467
2468 test_33c() {
2469         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2470         local ostnum
2471         local ostname
2472         local write_bytes
2473         local all_zeros
2474
2475         remote_ost_nodsh && skip "remote OST with nodsh" && return
2476         all_zeros=:
2477         rm -fr $DIR/d33
2478         test_mkdir -p $DIR/d33
2479         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2480
2481         sync
2482         for ostnum in $(seq $OSTCOUNT); do
2483                 # test-framework's OST numbering is one-based, while Lustre's
2484                 # is zero-based
2485                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2486                 # Parsing llobdstat's output sucks; we could grep the /proc
2487                 # path, but that's likely to not be as portable as using the
2488                 # llobdstat utility.  So we parse lctl output instead.
2489                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2490                         obdfilter/$ostname/stats |
2491                         awk '/^write_bytes/ {print $7}' )
2492                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2493                 if (( ${write_bytes:-0} > 0 ))
2494                 then
2495                         all_zeros=false
2496                         break;
2497                 fi
2498         done
2499
2500         $all_zeros || return 0
2501
2502         # Write four bytes
2503         echo foo > $DIR/d33/bar
2504         # Really write them
2505         sync
2506
2507         # Total up write_bytes after writing.  We'd better find non-zeros.
2508         for ostnum in $(seq $OSTCOUNT); do
2509                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2510                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2511                         obdfilter/$ostname/stats |
2512                         awk '/^write_bytes/ {print $7}' )
2513                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2514                 if (( ${write_bytes:-0} > 0 ))
2515                 then
2516                         all_zeros=false
2517                         break;
2518                 fi
2519         done
2520
2521         if $all_zeros
2522         then
2523                 for ostnum in $(seq $OSTCOUNT); do
2524                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2525                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2526                         do_facet ost$ostnum lctl get_param -n \
2527                                 obdfilter/$ostname/stats
2528                 done
2529                 error "OST not keeping write_bytes stats (b22312)"
2530         fi
2531 }
2532 run_test 33c "test llobdstat and write_bytes"
2533
2534 test_33d() {
2535         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2537         local MDTIDX=1
2538         local remote_dir=$DIR/$tdir/remote_dir
2539
2540         mkdir -p $DIR/$tdir
2541         $LFS mkdir -i $MDTIDX $remote_dir ||
2542                 error "create remote directory failed"
2543
2544         touch $remote_dir/$tfile
2545         chmod 444 $remote_dir/$tfile
2546         chown $RUNAS_ID $remote_dir/$tfile
2547
2548         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2549
2550         chown $RUNAS_ID $remote_dir
2551         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2552                                         error "create" || true
2553         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2554                                     error "open RDWR" || true
2555         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2556                                     error "create" || true
2557 }
2558 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2559
2560 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2561 test_34a() {
2562         rm -f $DIR/f34
2563         $MCREATE $DIR/f34 || error
2564         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2565         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2566         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2567         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2568 }
2569 run_test 34a "truncate file that has not been opened ==========="
2570
2571 test_34b() {
2572         [ ! -f $DIR/f34 ] && test_34a
2573         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2574         $OPENFILE -f O_RDONLY $DIR/f34
2575         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2576         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2577 }
2578 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2579
2580 test_34c() {
2581         [ ! -f $DIR/f34 ] && test_34a
2582         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2583         $OPENFILE -f O_RDWR $DIR/f34
2584         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2585         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2586 }
2587 run_test 34c "O_RDWR opening file-with-size works =============="
2588
2589 test_34d() {
2590         [ ! -f $DIR/f34 ] && test_34a
2591         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2592         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2593         rm $DIR/f34
2594 }
2595 run_test 34d "write to sparse file ============================="
2596
2597 test_34e() {
2598         rm -f $DIR/f34e
2599         $MCREATE $DIR/f34e || error
2600         $TRUNCATE $DIR/f34e 1000 || error
2601         $CHECKSTAT -s 1000 $DIR/f34e || error
2602         $OPENFILE -f O_RDWR $DIR/f34e
2603         $CHECKSTAT -s 1000 $DIR/f34e || error
2604 }
2605 run_test 34e "create objects, some with size and some without =="
2606
2607 test_34f() { # bug 6242, 6243
2608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2609         SIZE34F=48000
2610         rm -f $DIR/f34f
2611         $MCREATE $DIR/f34f || error
2612         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2613         dd if=$DIR/f34f of=$TMP/f34f
2614         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2615         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2616         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2617         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2618         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2619 }
2620 run_test 34f "read from a file with no objects until EOF ======="
2621
2622 test_34g() {
2623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2624         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2625         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2626         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2627         cancel_lru_locks osc
2628         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2629                 error "wrong size after lock cancel"
2630
2631         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2632         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2633                 error "expanding truncate failed"
2634         cancel_lru_locks osc
2635         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2636                 error "wrong expanded size after lock cancel"
2637 }
2638 run_test 34g "truncate long file ==============================="
2639
2640 test_34h() {
2641         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2642         local gid=10
2643         local sz=1000
2644
2645         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2646         sync # Flush the cache so that multiop below does not block on cache
2647              # flush when getting the group lock
2648         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2649         MULTIPID=$!
2650
2651         # Since just timed wait is not good enough, let's do a sync write
2652         # that way we are sure enough time for a roundtrip + processing
2653         # passed + 2 seconds of extra margin.
2654         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2655         rm $DIR/${tfile}-1
2656         sleep 2
2657
2658         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2659                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2660                 kill -9 $MULTIPID
2661         fi
2662         wait $MULTIPID
2663         local nsz=`stat -c %s $DIR/$tfile`
2664         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2665 }
2666 run_test 34h "ftruncate file under grouplock should not block"
2667
2668 test_35a() {
2669         cp /bin/sh $DIR/f35a
2670         chmod 444 $DIR/f35a
2671         chown $RUNAS_ID $DIR/f35a
2672         $RUNAS $DIR/f35a && error || true
2673         rm $DIR/f35a
2674 }
2675 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2676
2677 test_36a() {
2678         rm -f $DIR/f36
2679         utime $DIR/f36 || error
2680 }
2681 run_test 36a "MDS utime check (mknod, utime) ==================="
2682
2683 test_36b() {
2684         echo "" > $DIR/f36
2685         utime $DIR/f36 || error
2686 }
2687 run_test 36b "OST utime check (open, utime) ===================="
2688
2689 test_36c() {
2690         rm -f $DIR/d36/f36
2691         test_mkdir $DIR/d36
2692         chown $RUNAS_ID $DIR/d36
2693         $RUNAS utime $DIR/d36/f36 || error
2694 }
2695 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2696
2697 test_36d() {
2698         [ ! -d $DIR/d36 ] && test_36c
2699         echo "" > $DIR/d36/f36
2700         $RUNAS utime $DIR/d36/f36 || error
2701 }
2702 run_test 36d "non-root OST utime check (open, utime) ==========="
2703
2704 test_36e() {
2705         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2706         test_mkdir -p $DIR/$tdir
2707         touch $DIR/$tdir/$tfile
2708         $RUNAS utime $DIR/$tdir/$tfile && \
2709                 error "utime worked, expected failure" || true
2710 }
2711 run_test 36e "utime on non-owned file (should return error) ===="
2712
2713 subr_36fh() {
2714         local fl="$1"
2715         local LANG_SAVE=$LANG
2716         local LC_LANG_SAVE=$LC_LANG
2717         export LANG=C LC_LANG=C # for date language
2718
2719         DATESTR="Dec 20  2000"
2720         test_mkdir -p $DIR/$tdir
2721         lctl set_param fail_loc=$fl
2722         date; date +%s
2723         cp /etc/hosts $DIR/$tdir/$tfile
2724         sync & # write RPC generated with "current" inode timestamp, but delayed
2725         sleep 1
2726         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2727         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2728         cancel_lru_locks osc
2729         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2730         date; date +%s
2731         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2732                 echo "BEFORE: $LS_BEFORE" && \
2733                 echo "AFTER : $LS_AFTER" && \
2734                 echo "WANT  : $DATESTR" && \
2735                 error "$DIR/$tdir/$tfile timestamps changed" || true
2736
2737         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2738 }
2739
2740 test_36f() {
2741         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2742         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2743         subr_36fh "0x80000214"
2744 }
2745 run_test 36f "utime on file racing with OST BRW write =========="
2746
2747 test_36g() {
2748         remote_ost_nodsh && skip "remote OST with nodsh" && return
2749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2750         local fmd_max_age
2751         local fmd_before
2752         local fmd_after
2753
2754         test_mkdir -p $DIR/$tdir
2755         fmd_max_age=$(do_facet ost1 \
2756                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2757                 head -n 1")
2758
2759         fmd_before=$(do_facet ost1 \
2760                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2761         touch $DIR/$tdir/$tfile
2762         sleep $((fmd_max_age + 12))
2763         fmd_after=$(do_facet ost1 \
2764                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2765
2766         echo "fmd_before: $fmd_before"
2767         echo "fmd_after: $fmd_after"
2768         [[ $fmd_after -gt $fmd_before ]] &&
2769                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2770                 error "fmd didn't expire after ping" || true
2771 }
2772 run_test 36g "filter mod data cache expiry ====================="
2773
2774 test_36h() {
2775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2776         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2777         subr_36fh "0x80000227"
2778 }
2779 run_test 36h "utime on file racing with OST BRW write =========="
2780
2781 # test_37 - duplicate with tests 32q 32r
2782
2783 test_38() {
2784         local file=$DIR/$tfile
2785         touch $file
2786         openfile -f O_DIRECTORY $file
2787         local RC=$?
2788         local ENOTDIR=20
2789         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2790         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2791 }
2792 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2793
2794 test_39() {
2795         touch $DIR/$tfile
2796         touch $DIR/${tfile}2
2797 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2798 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2799 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2800         sleep 2
2801         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2802         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2803                 echo "mtime"
2804                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2805                 echo "atime"
2806                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2807                 echo "ctime"
2808                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2809                 error "O_TRUNC didn't change timestamps"
2810         fi
2811 }
2812 run_test 39 "mtime changed on create ==========================="
2813
2814 test_39b() {
2815         test_mkdir -p $DIR/$tdir
2816         cp -p /etc/passwd $DIR/$tdir/fopen
2817         cp -p /etc/passwd $DIR/$tdir/flink
2818         cp -p /etc/passwd $DIR/$tdir/funlink
2819         cp -p /etc/passwd $DIR/$tdir/frename
2820         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2821
2822         sleep 1
2823         echo "aaaaaa" >> $DIR/$tdir/fopen
2824         echo "aaaaaa" >> $DIR/$tdir/flink
2825         echo "aaaaaa" >> $DIR/$tdir/funlink
2826         echo "aaaaaa" >> $DIR/$tdir/frename
2827
2828         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2829         local link_new=`stat -c %Y $DIR/$tdir/flink`
2830         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2831         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2832
2833         cat $DIR/$tdir/fopen > /dev/null
2834         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2835         rm -f $DIR/$tdir/funlink2
2836         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2837
2838         for (( i=0; i < 2; i++ )) ; do
2839                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2840                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2841                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2842                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2843
2844                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2845                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2846                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2847                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2848
2849                 cancel_lru_locks osc
2850                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2851         done
2852 }
2853 run_test 39b "mtime change on open, link, unlink, rename  ======"
2854
2855 # this should be set to past
2856 TEST_39_MTIME=`date -d "1 year ago" +%s`
2857
2858 # bug 11063
2859 test_39c() {
2860         touch $DIR1/$tfile
2861         sleep 2
2862         local mtime0=`stat -c %Y $DIR1/$tfile`
2863
2864         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2865         local mtime1=`stat -c %Y $DIR1/$tfile`
2866         [ "$mtime1" = $TEST_39_MTIME ] || \
2867                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2868
2869         local d1=`date +%s`
2870         echo hello >> $DIR1/$tfile
2871         local d2=`date +%s`
2872         local mtime2=`stat -c %Y $DIR1/$tfile`
2873         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2874                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2875
2876         mv $DIR1/$tfile $DIR1/$tfile-1
2877
2878         for (( i=0; i < 2; i++ )) ; do
2879                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2880                 [ "$mtime2" = "$mtime3" ] || \
2881                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2882
2883                 cancel_lru_locks osc
2884                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2885         done
2886 }
2887 run_test 39c "mtime change on rename ==========================="
2888
2889 # bug 21114
2890 test_39d() {
2891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2892         touch $DIR1/$tfile
2893
2894         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2895
2896         for (( i=0; i < 2; i++ )) ; do
2897                 local mtime=`stat -c %Y $DIR1/$tfile`
2898                 [ $mtime = $TEST_39_MTIME ] || \
2899                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2900
2901                 cancel_lru_locks osc
2902                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2903         done
2904 }
2905 run_test 39d "create, utime, stat =============================="
2906
2907 # bug 21114
2908 test_39e() {
2909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2910         touch $DIR1/$tfile
2911         local mtime1=`stat -c %Y $DIR1/$tfile`
2912
2913         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2914
2915         for (( i=0; i < 2; i++ )) ; do
2916                 local mtime2=`stat -c %Y $DIR1/$tfile`
2917                 [ $mtime2 = $TEST_39_MTIME ] || \
2918                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2919
2920                 cancel_lru_locks osc
2921                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2922         done
2923 }
2924 run_test 39e "create, stat, utime, stat ========================"
2925
2926 # bug 21114
2927 test_39f() {
2928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2929         touch $DIR1/$tfile
2930         mtime1=`stat -c %Y $DIR1/$tfile`
2931
2932         sleep 2
2933         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2934
2935         for (( i=0; i < 2; i++ )) ; do
2936                 local mtime2=`stat -c %Y $DIR1/$tfile`
2937                 [ $mtime2 = $TEST_39_MTIME ] || \
2938                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2939
2940                 cancel_lru_locks osc
2941                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2942         done
2943 }
2944 run_test 39f "create, stat, sleep, utime, stat ================="
2945
2946 # bug 11063
2947 test_39g() {
2948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2949         echo hello >> $DIR1/$tfile
2950         local mtime1=`stat -c %Y $DIR1/$tfile`
2951
2952         sleep 2
2953         chmod o+r $DIR1/$tfile
2954
2955         for (( i=0; i < 2; i++ )) ; do
2956                 local mtime2=`stat -c %Y $DIR1/$tfile`
2957                 [ "$mtime1" = "$mtime2" ] || \
2958                         error "lost mtime: $mtime2, should be $mtime1"
2959
2960                 cancel_lru_locks osc
2961                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2962         done
2963 }
2964 run_test 39g "write, chmod, stat ==============================="
2965
2966 # bug 11063
2967 test_39h() {
2968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2969         touch $DIR1/$tfile
2970         sleep 1
2971
2972         local d1=`date`
2973         echo hello >> $DIR1/$tfile
2974         local mtime1=`stat -c %Y $DIR1/$tfile`
2975
2976         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2977         local d2=`date`
2978         if [ "$d1" != "$d2" ]; then
2979                 echo "write and touch not within one second"
2980         else
2981                 for (( i=0; i < 2; i++ )) ; do
2982                         local mtime2=`stat -c %Y $DIR1/$tfile`
2983                         [ "$mtime2" = $TEST_39_MTIME ] || \
2984                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2985
2986                         cancel_lru_locks osc
2987                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2988                 done
2989         fi
2990 }
2991 run_test 39h "write, utime within one second, stat ============="
2992
2993 test_39i() {
2994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2995         touch $DIR1/$tfile
2996         sleep 1
2997
2998         echo hello >> $DIR1/$tfile
2999         local mtime1=`stat -c %Y $DIR1/$tfile`
3000
3001         mv $DIR1/$tfile $DIR1/$tfile-1
3002
3003         for (( i=0; i < 2; i++ )) ; do
3004                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3005
3006                 [ "$mtime1" = "$mtime2" ] || \
3007                         error "lost mtime: $mtime2, should be $mtime1"
3008
3009                 cancel_lru_locks osc
3010                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3011         done
3012 }
3013 run_test 39i "write, rename, stat =============================="
3014
3015 test_39j() {
3016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3017         start_full_debug_logging
3018         touch $DIR1/$tfile
3019         sleep 1
3020
3021         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3022         lctl set_param fail_loc=0x80000412
3023         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3024                 error "multiop failed"
3025         local multipid=$!
3026         local mtime1=`stat -c %Y $DIR1/$tfile`
3027
3028         mv $DIR1/$tfile $DIR1/$tfile-1
3029
3030         kill -USR1 $multipid
3031         wait $multipid || error "multiop close failed"
3032
3033         for (( i=0; i < 2; i++ )) ; do
3034                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3035                 [ "$mtime1" = "$mtime2" ] ||
3036                         error "mtime is lost on close: $mtime2, " \
3037                               "should be $mtime1"
3038
3039                 cancel_lru_locks osc
3040                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3041         done
3042         lctl set_param fail_loc=0
3043         stop_full_debug_logging
3044 }
3045 run_test 39j "write, rename, close, stat ======================="
3046
3047 test_39k() {
3048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3049         touch $DIR1/$tfile
3050         sleep 1
3051
3052         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3053         local multipid=$!
3054         local mtime1=`stat -c %Y $DIR1/$tfile`
3055
3056         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3057
3058         kill -USR1 $multipid
3059         wait $multipid || error "multiop close failed"
3060
3061         for (( i=0; i < 2; i++ )) ; do
3062                 local mtime2=`stat -c %Y $DIR1/$tfile`
3063
3064                 [ "$mtime2" = $TEST_39_MTIME ] || \
3065                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3066
3067                 cancel_lru_locks osc
3068                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3069         done
3070 }
3071 run_test 39k "write, utime, close, stat ========================"
3072
3073 # this should be set to future
3074 TEST_39_ATIME=`date -d "1 year" +%s`
3075
3076 test_39l() {
3077         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3078         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3079         local atime_diff=$(do_facet $SINGLEMDS \
3080                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3081         rm -rf $DIR/$tdir
3082         mkdir -p $DIR/$tdir
3083
3084         # test setting directory atime to future
3085         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3086         local atime=$(stat -c %X $DIR/$tdir)
3087         [ "$atime" = $TEST_39_ATIME ] || \
3088                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3089
3090         # test setting directory atime from future to now
3091         local d1=$(date +%s)
3092         ls $DIR/$tdir
3093         local d2=$(date +%s)
3094
3095         cancel_lru_locks mdc
3096         atime=$(stat -c %X $DIR/$tdir)
3097         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3098                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3099
3100         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3101         sleep 3
3102
3103         # test setting directory atime when now > dir atime + atime_diff
3104         d1=$(date +%s)
3105         ls $DIR/$tdir
3106         d2=$(date +%s)
3107         cancel_lru_locks mdc
3108         atime=$(stat -c %X $DIR/$tdir)
3109         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3110                 error "atime is not updated  : $atime, should be $d2"
3111
3112         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3113         sleep 3
3114
3115         # test not setting directory atime when now < dir atime + atime_diff
3116         ls $DIR/$tdir
3117         cancel_lru_locks mdc
3118         atime=$(stat -c %X $DIR/$tdir)
3119         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3120                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3121
3122         do_facet $SINGLEMDS \
3123                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3124 }
3125 run_test 39l "directory atime update ==========================="
3126
3127 test_39m() {
3128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3129         touch $DIR1/$tfile
3130         sleep 2
3131         local far_past_mtime=$(date -d "May 29 1953" +%s)
3132         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3133
3134         touch -m -d @$far_past_mtime $DIR1/$tfile
3135         touch -a -d @$far_past_atime $DIR1/$tfile
3136
3137         for (( i=0; i < 2; i++ )) ; do
3138                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3139                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3140                         error "atime or mtime set incorrectly"
3141
3142                 cancel_lru_locks osc
3143                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3144         done
3145 }
3146 run_test 39m "test atime and mtime before 1970"
3147
3148 test_39n() { # LU-3832
3149         local atime_diff=$(do_facet $SINGLEMDS \
3150                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3151         local atime0
3152         local atime1
3153         local atime2
3154
3155         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3156
3157         rm -rf $DIR/$tfile
3158         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3159         atime0=$(stat -c %X $DIR/$tfile)
3160
3161         sleep 5
3162         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3163         atime1=$(stat -c %X $DIR/$tfile)
3164
3165         sleep 5
3166         cancel_lru_locks mdc
3167         cancel_lru_locks osc
3168         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3169         atime2=$(stat -c %X $DIR/$tfile)
3170
3171         do_facet $SINGLEMDS \
3172                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3173
3174         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3175         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3176 }
3177 run_test 39n "check that O_NOATIME is honored"
3178
3179 test_39o() {
3180         TESTDIR=$DIR/$tdir/$tfile
3181         [ -e $TESTDIR ] && rm -rf $TESTDIR
3182         mkdir -p $TESTDIR
3183         cd $TESTDIR
3184         links1=2
3185         ls
3186         mkdir a b
3187         ls
3188         links2=$(stat -c %h .)
3189         [ $(($links1 + 2)) != $links2 ] &&
3190                 error "wrong links count $(($links1 + 2)) != $links2"
3191         rmdir b
3192         links3=$(stat -c %h .)
3193         [ $(($links1 + 1)) != $links3 ] &&
3194                 error "wrong links count $links1 != $links3"
3195         return 0
3196 }
3197 run_test 39o "directory cached attributes updated after create ========"
3198
3199 test_40() {
3200         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3201         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3202                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3203         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3204                 error "$tfile is not 4096 bytes in size"
3205 }
3206 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3207
3208 test_41() {
3209         # bug 1553
3210         small_write $DIR/f41 18
3211 }
3212 run_test 41 "test small file write + fstat ====================="
3213
3214 count_ost_writes() {
3215         lctl get_param -n osc.*.stats |
3216                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3217                         END { printf("%0.0f", writes) }'
3218 }
3219
3220 # decent default
3221 WRITEBACK_SAVE=500
3222 DIRTY_RATIO_SAVE=40
3223 MAX_DIRTY_RATIO=50
3224 BG_DIRTY_RATIO_SAVE=10
3225 MAX_BG_DIRTY_RATIO=25
3226
3227 start_writeback() {
3228         trap 0
3229         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3230         # dirty_ratio, dirty_background_ratio
3231         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3232                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3233                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3234                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3235         else
3236                 # if file not here, we are a 2.4 kernel
3237                 kill -CONT `pidof kupdated`
3238         fi
3239 }
3240
3241 stop_writeback() {
3242         # setup the trap first, so someone cannot exit the test at the
3243         # exact wrong time and mess up a machine
3244         trap start_writeback EXIT
3245         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3246         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3247                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3248                 sysctl -w vm.dirty_writeback_centisecs=0
3249                 sysctl -w vm.dirty_writeback_centisecs=0
3250                 # save and increase /proc/sys/vm/dirty_ratio
3251                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3252                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3253                 # save and increase /proc/sys/vm/dirty_background_ratio
3254                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3255                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3256         else
3257                 # if file not here, we are a 2.4 kernel
3258                 kill -STOP `pidof kupdated`
3259         fi
3260 }
3261
3262 # ensure that all stripes have some grant before we test client-side cache
3263 setup_test42() {
3264         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3265                 dd if=/dev/zero of=$i bs=4k count=1
3266                 rm $i
3267         done
3268 }
3269
3270 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3271 # file truncation, and file removal.
3272 test_42a() {
3273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3274         setup_test42
3275         cancel_lru_locks osc
3276         stop_writeback
3277         sync; sleep 1; sync # just to be safe
3278         BEFOREWRITES=`count_ost_writes`
3279         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3280         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3281         AFTERWRITES=`count_ost_writes`
3282         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3283                 error "$BEFOREWRITES < $AFTERWRITES"
3284         start_writeback
3285 }
3286 run_test 42a "ensure that we don't flush on close =============="
3287
3288 test_42b() {
3289         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3290         setup_test42
3291         cancel_lru_locks osc
3292         stop_writeback
3293         sync
3294         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3295         BEFOREWRITES=$(count_ost_writes)
3296         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3297         AFTERWRITES=$(count_ost_writes)
3298         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3299                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3300         fi
3301         BEFOREWRITES=$(count_ost_writes)
3302         sync || error "sync: $?"
3303         AFTERWRITES=$(count_ost_writes)
3304         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3305                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3306         fi
3307         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3308         start_writeback
3309         return 0
3310 }
3311 run_test 42b "test destroy of file with cached dirty data ======"
3312
3313 # if these tests just want to test the effect of truncation,
3314 # they have to be very careful.  consider:
3315 # - the first open gets a {0,EOF}PR lock
3316 # - the first write conflicts and gets a {0, count-1}PW
3317 # - the rest of the writes are under {count,EOF}PW
3318 # - the open for truncate tries to match a {0,EOF}PR
3319 #   for the filesize and cancels the PWs.
3320 # any number of fixes (don't get {0,EOF} on open, match
3321 # composite locks, do smarter file size management) fix
3322 # this, but for now we want these tests to verify that
3323 # the cancellation with truncate intent works, so we
3324 # start the file with a full-file pw lock to match against
3325 # until the truncate.
3326 trunc_test() {
3327         test=$1
3328         file=$DIR/$test
3329         offset=$2
3330         cancel_lru_locks osc
3331         stop_writeback
3332         # prime the file with 0,EOF PW to match
3333         touch $file
3334         $TRUNCATE $file 0
3335         sync; sync
3336         # now the real test..
3337         dd if=/dev/zero of=$file bs=1024 count=100
3338         BEFOREWRITES=`count_ost_writes`
3339         $TRUNCATE $file $offset
3340         cancel_lru_locks osc
3341         AFTERWRITES=`count_ost_writes`
3342         start_writeback
3343 }
3344
3345 test_42c() {
3346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3347         trunc_test 42c 1024
3348         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3349             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3350         rm $file
3351 }
3352 run_test 42c "test partial truncate of file with cached dirty data"
3353
3354 test_42d() {
3355         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3356         trunc_test 42d 0
3357         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3358             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3359         rm $file
3360 }
3361 run_test 42d "test complete truncate of file with cached dirty data"
3362
3363 test_42e() { # bug22074
3364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3365         local TDIR=$DIR/${tdir}e
3366         local pagesz=$(page_size)
3367         local pages=16 # hardcoded 16 pages, don't change it.
3368         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3369         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3370         local max_dirty_mb
3371         local warmup_files
3372
3373         test_mkdir -p $DIR/${tdir}e
3374         $SETSTRIPE -c 1 $TDIR
3375         createmany -o $TDIR/f $files
3376
3377         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3378
3379         # we assume that with $OSTCOUNT files, at least one of them will
3380         # be allocated on OST0.
3381         warmup_files=$((OSTCOUNT * max_dirty_mb))
3382         createmany -o $TDIR/w $warmup_files
3383
3384         # write a large amount of data into one file and sync, to get good
3385         # avail_grant number from OST.
3386         for ((i=0; i<$warmup_files; i++)); do
3387                 idx=$($GETSTRIPE -i $TDIR/w$i)
3388                 [ $idx -ne 0 ] && continue
3389                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3390                 break
3391         done
3392         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3393         sync
3394         $LCTL get_param $proc_osc0/cur_dirty_bytes
3395         $LCTL get_param $proc_osc0/cur_grant_bytes
3396
3397         # create as much dirty pages as we can while not to trigger the actual
3398         # RPCs directly. but depends on the env, VFS may trigger flush during this
3399         # period, hopefully we are good.
3400         for ((i=0; i<$warmup_files; i++)); do
3401                 idx=$($GETSTRIPE -i $TDIR/w$i)
3402                 [ $idx -ne 0 ] && continue
3403                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3404         done
3405         $LCTL get_param $proc_osc0/cur_dirty_bytes
3406         $LCTL get_param $proc_osc0/cur_grant_bytes
3407
3408         # perform the real test
3409         $LCTL set_param $proc_osc0/rpc_stats 0
3410         for ((;i<$files; i++)); do
3411                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3412                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3413         done
3414         sync
3415         $LCTL get_param $proc_osc0/rpc_stats
3416
3417         local percent=0
3418         local have_ppr=false
3419         $LCTL get_param $proc_osc0/rpc_stats |
3420                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3421                         # skip lines until we are at the RPC histogram data
3422                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3423                         $have_ppr || continue
3424
3425                         # we only want the percent stat for < 16 pages
3426                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3427
3428                         percent=$((percent + WPCT))
3429                         if [[ $percent -gt 15 ]]; then
3430                                 error "less than 16-pages write RPCs" \
3431                                       "$percent% > 15%"
3432                                 break
3433                         fi
3434                 done
3435         rm -rf $TDIR
3436 }
3437 run_test 42e "verify sub-RPC writes are not done synchronously"
3438
3439 test_43() {
3440         test_mkdir -p $DIR/$tdir
3441         cp -p /bin/ls $DIR/$tdir/$tfile
3442         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3443         pid=$!
3444         # give multiop a chance to open
3445         sleep 1
3446
3447         $DIR/$tdir/$tfile && error || true
3448         kill -USR1 $pid
3449 }
3450 run_test 43 "execution of file opened for write should return -ETXTBSY"
3451
3452 test_43a() {
3453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3454         test_mkdir -p $DIR/$tdir
3455         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3456                         cp -p multiop $DIR/$tdir/multiop
3457         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3458                         return 1
3459         MULTIOP_PID=$!
3460         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3461         kill -USR1 $MULTIOP_PID || return 2
3462         wait $MULTIOP_PID || return 3
3463         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3464 }
3465 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3466
3467 test_43b() {
3468         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3469         test_mkdir -p $DIR/$tdir
3470         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3471                         cp -p multiop $DIR/$tdir/multiop
3472         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3473                         return 1
3474         MULTIOP_PID=$!
3475         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3476         kill -USR1 $MULTIOP_PID || return 2
3477         wait $MULTIOP_PID || return 3
3478         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3479 }
3480 run_test 43b "truncate of file being executed should return -ETXTBSY"
3481
3482 test_43c() {
3483         local testdir="$DIR/$tdir"
3484         test_mkdir -p $DIR/$tdir
3485         cp $SHELL $testdir/
3486         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3487                 ( cd $testdir && md5sum -c)
3488 }
3489 run_test 43c "md5sum of copy into lustre========================"
3490
3491 test_44() {
3492         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3493         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3494         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3495 }
3496 run_test 44 "zero length read from a sparse stripe ============="
3497
3498 test_44a() {
3499         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3500                 awk '{ print $2 }')
3501         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3502         [[ $nstripe -gt $OSTCOUNT ]] &&
3503             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3504             return
3505         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3506                 awk '{ print $2 }')
3507         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3508                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3509                         awk '{ print $2 }')
3510         fi
3511
3512         OFFSETS="0 $((stride/2)) $((stride-1))"
3513         for offset in $OFFSETS; do
3514                 for i in $(seq 0 $((nstripe-1))); do
3515                         local GLOBALOFFSETS=""
3516                         # size in Bytes
3517                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3518                         local myfn=$DIR/d44a-$size
3519                         echo "--------writing $myfn at $size"
3520                         ll_sparseness_write $myfn $size ||
3521                                 error "ll_sparseness_write"
3522                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3523                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3524                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3525
3526                         for j in $(seq 0 $((nstripe-1))); do
3527                                 # size in Bytes
3528                                 size=$((((j + $nstripe )*$stride + $offset)))
3529                                 ll_sparseness_write $myfn $size ||
3530                                         error "ll_sparseness_write"
3531                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3532                         done
3533                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3534                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3535                         rm -f $myfn
3536                 done
3537         done
3538 }
3539 run_test 44a "test sparse pwrite ==============================="
3540
3541 dirty_osc_total() {
3542         tot=0
3543         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3544                 tot=$(($tot + $d))
3545         done
3546         echo $tot
3547 }
3548 do_dirty_record() {
3549         before=`dirty_osc_total`
3550         echo executing "\"$*\""
3551         eval $*
3552         after=`dirty_osc_total`
3553         echo before $before, after $after
3554 }
3555 test_45() {
3556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3557         f="$DIR/f45"
3558         # Obtain grants from OST if it supports it
3559         echo blah > ${f}_grant
3560         stop_writeback
3561         sync
3562         do_dirty_record "echo blah > $f"
3563         [[ $before -eq $after ]] && error "write wasn't cached"
3564         do_dirty_record "> $f"
3565         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3566         do_dirty_record "echo blah > $f"
3567         [[ $before -eq $after ]] && error "write wasn't cached"
3568         do_dirty_record "sync"
3569         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3570         do_dirty_record "echo blah > $f"
3571         [[ $before -eq $after ]] && error "write wasn't cached"
3572         do_dirty_record "cancel_lru_locks osc"
3573         [[ $before -gt $after ]] ||
3574                 error "lock cancellation didn't lower dirty count"
3575         start_writeback
3576 }
3577 run_test 45 "osc io page accounting ============================"
3578
3579 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3580 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3581 # objects offset and an assert hit when an rpc was built with 1023's mapped
3582 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3583 test_46() {
3584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3585         f="$DIR/f46"
3586         stop_writeback
3587         sync
3588         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3589         sync
3590         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3591         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3592         sync
3593         start_writeback
3594 }
3595 run_test 46 "dirtying a previously written page ================"
3596
3597 # test_47 is removed "Device nodes check" is moved to test_28
3598
3599 test_48a() { # bug 2399
3600         check_kernel_version 34 || return 0
3601         test_mkdir -p $DIR/$tdir
3602         cd $DIR/$tdir
3603         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3604         test_mkdir $DIR/$tdir || error "recreate directory failed"
3605         touch foo || error "'touch foo' failed after recreating cwd"
3606         test_mkdir $DIR/$tdir/bar ||
3607                      error "'mkdir foo' failed after recreating cwd"
3608         if check_kernel_version 44; then
3609                 touch .foo || error "'touch .foo' failed after recreating cwd"
3610                 test_mkdir $DIR/$tdir/.bar ||
3611                               error "'mkdir .foo' failed after recreating cwd"
3612         fi
3613         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3614         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3615         cd . || error "'cd .' failed after recreating cwd"
3616         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3617         rmdir . && error "'rmdir .' worked after recreating cwd"
3618         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3619         cd .. || error "'cd ..' failed after recreating cwd"
3620 }
3621 run_test 48a "Access renamed working dir (should return errors)="
3622
3623 test_48b() { # bug 2399
3624         check_kernel_version 34 || return 0
3625         rm -rf $DIR/$tdir
3626         test_mkdir -p $DIR/$tdir
3627         cd $DIR/$tdir
3628         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3629         touch foo && error "'touch foo' worked after removing cwd"
3630         test_mkdir $DIR/$tdir/foo &&
3631                      error "'mkdir foo' worked after removing cwd"
3632         if check_kernel_version 44; then
3633                 touch .foo && error "'touch .foo' worked after removing cwd"
3634                 test_mkdir $DIR/$tdir/.foo &&
3635                               error "'mkdir .foo' worked after removing cwd"
3636         fi
3637         ls . > /dev/null && error "'ls .' worked after removing cwd"
3638         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3639         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3640         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3641         rmdir . && error "'rmdir .' worked after removing cwd"
3642         ln -s . foo && error "'ln -s .' worked after removing cwd"
3643         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3644 }
3645 run_test 48b "Access removed working dir (should return errors)="
3646
3647 test_48c() { # bug 2350
3648         check_kernel_version 36 || return 0
3649         #lctl set_param debug=-1
3650         #set -vx
3651         rm -rf $DIR/$tdir
3652         test_mkdir -p $DIR/$tdir/dir
3653         cd $DIR/$tdir/dir
3654         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3655         $TRACE touch foo && error "touch foo worked after removing cwd"
3656         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3657         if check_kernel_version 44; then
3658                 touch .foo && error "touch .foo worked after removing cwd"
3659                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3660         fi
3661         $TRACE ls . && error "'ls .' worked after removing cwd"
3662         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3663         is_patchless || ( $TRACE cd . &&
3664                         error "'cd .' worked after removing cwd" )
3665         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3666         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3667         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3668         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3669 }
3670 run_test 48c "Access removed working subdir (should return errors)"
3671
3672 test_48d() { # bug 2350
3673         check_kernel_version 36 || return 0
3674         #lctl set_param debug=-1
3675         #set -vx
3676         rm -rf $DIR/$tdir
3677         test_mkdir -p $DIR/$tdir/dir
3678         cd $DIR/$tdir/dir
3679         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3680         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3681         $TRACE touch foo && error "'touch foo' worked after removing parent"
3682         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3683         if check_kernel_version 44; then
3684                 touch .foo && error "'touch .foo' worked after removing parent"
3685                 test_mkdir .foo &&
3686                               error "mkdir .foo worked after removing parent"
3687         fi
3688         $TRACE ls . && error "'ls .' worked after removing parent"
3689         $TRACE ls .. && error "'ls ..' worked after removing parent"
3690         is_patchless || ( $TRACE cd . &&
3691                         error "'cd .' worked after recreate parent" )
3692         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3693         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3694         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3695         is_patchless || ( $TRACE cd .. &&
3696                         error "'cd ..' worked after removing parent" || true )
3697 }
3698 run_test 48d "Access removed parent subdir (should return errors)"
3699
3700 test_48e() { # bug 4134
3701         check_kernel_version 41 || return 0
3702         #lctl set_param debug=-1
3703         #set -vx
3704         rm -rf $DIR/$tdir
3705         test_mkdir -p $DIR/$tdir/dir
3706         cd $DIR/$tdir/dir
3707         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3708         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3709         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3710         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3711         # On a buggy kernel addition of "touch foo" after cd .. will
3712         # produce kernel oops in lookup_hash_it
3713         touch ../foo && error "'cd ..' worked after recreate parent"
3714         cd $DIR
3715         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3716 }
3717 run_test 48e "Access to recreated parent subdir (should return errors)"
3718
3719 test_49() { # LU-1030
3720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3721         # get ost1 size - lustre-OST0000
3722         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3723                 awk '{ print $4 }')
3724         # write 800M at maximum
3725         [[ $ost1_size -lt 2 ]] && ost1_size=2
3726         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3727
3728         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3729         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3730         local dd_pid=$!
3731
3732         # change max_pages_per_rpc while writing the file
3733         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3734         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3735         # loop until dd process exits
3736         while ps ax -opid | grep -wq $dd_pid; do
3737                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3738                 sleep $((RANDOM % 5 + 1))
3739         done
3740         # restore original max_pages_per_rpc
3741         $LCTL set_param $osc1_mppc=$orig_mppc
3742         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3743 }
3744 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3745
3746 test_50() {
3747         # bug 1485
3748         test_mkdir $DIR/$tdir
3749         cd $DIR/$tdir
3750         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3751 }
3752 run_test 50 "special situations: /proc symlinks  ==============="
3753
3754 test_51a() {    # was test_51
3755         # bug 1516 - create an empty entry right after ".." then split dir
3756         test_mkdir -p $DIR/$tdir
3757         touch $DIR/$tdir/foo
3758         $MCREATE $DIR/$tdir/bar
3759         rm $DIR/$tdir/foo
3760         createmany -m $DIR/$tdir/longfile 201
3761         FNUM=202
3762         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3763                 $MCREATE $DIR/$tdir/longfile$FNUM
3764                 FNUM=$(($FNUM + 1))
3765                 echo -n "+"
3766         done
3767         echo
3768         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3769 }
3770 run_test 51a "special situations: split htree with empty entry =="
3771
3772 export NUMTEST=70000
3773 test_51b() {
3774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3775         local BASE=$DIR/d${base}.${TESTSUITE}
3776
3777         # cleanup the directory
3778         rm -fr $BASE
3779
3780         test_mkdir -p $BASE
3781
3782         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3783         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3784         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3785                 return
3786
3787         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3788                 echo "reduced count to $NUMTEST due to inodes"
3789
3790         # need to check free space for the directories as well
3791         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3792         numfree=$((blkfree / 4))
3793         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3794                 echo "reduced count to $NUMTEST due to blocks"
3795
3796         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3797                 echo "failed" > $BASE/fnum
3798 }
3799 run_test 51b "exceed 64k subdirectory nlink limit"
3800
3801 test_51ba() { # LU-993
3802         local BASE=$DIR/d${base}.${TESTSUITE}
3803         # unlink all but 100 subdirectories, then check it still works
3804         local LEFT=100
3805         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3806
3807         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3808         local DELETE=$((NUMTEST - LEFT))
3809
3810         # continue on to run this test even if 51b didn't finish,
3811         # just to delete the many subdirectories created.
3812         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3813
3814         # for ldiskfs the nlink count should be 1, but this is OSD specific
3815         # and so this is listed for informational purposes only
3816         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3817         unlinkmany -d $BASE/d $DELETE
3818         RC=$?
3819
3820         if [ $RC -ne 0 ]; then
3821                 if [ "$NUMPREV" == "failed" ]; then
3822                         skip "previous setup failed"
3823                         return 0
3824                 else
3825                         error "unlink of first $DELETE subdirs failed"
3826                         return $RC
3827                 fi
3828         fi
3829
3830         echo "nlink between: $(stat -c %h $BASE)"
3831         # trim the first line of ls output
3832         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3833         [ $FOUND -ne $LEFT ] &&
3834                 error "can't find subdirs: found only $FOUND/$LEFT"
3835
3836         unlinkmany -d $BASE/d $DELETE $LEFT ||
3837                 error "unlink of second $LEFT subdirs failed"
3838         # regardless of whether the backing filesystem tracks nlink accurately
3839         # or not, the nlink count shouldn't be more than "." and ".." here
3840         local AFTER=$(stat -c %h $BASE)
3841         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
3842                 echo "nlink after: $AFTER"
3843 }
3844 run_test 51ba "verify nlink for many subdirectory cleanup"
3845
3846 test_51d() {
3847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3848         [[ $OSTCOUNT -lt 3 ]] &&
3849                 skip_env "skipping test with few OSTs" && return
3850         test_mkdir -p $DIR/$tdir
3851         createmany -o $DIR/$tdir/t- 1000
3852         $GETSTRIPE $DIR/$tdir > $TMP/files
3853         for N in $(seq 0 $((OSTCOUNT - 1))); do
3854                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
3855                         END { printf("%0.0f", objs) }' $TMP/files)
3856                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
3857                         '($1 == '$N') { objs += 1 } \
3858                         END { printf("%0.0f", objs) }')
3859                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3860         done
3861         unlinkmany $DIR/$tdir/t- 1000
3862
3863         NLAST=0
3864         for N in $(seq 1 $((OSTCOUNT - 1))); do
3865                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
3866                         error "OST $N has less objects vs OST $NLAST" \
3867                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
3868                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
3869                         error "OST $N has less objects vs OST $NLAST" \
3870                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
3871
3872                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
3873                         error "OST $N has less #0 objects vs OST $NLAST" \
3874                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3875                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
3876                         error "OST $N has less #0 objects vs OST $NLAST" \
3877                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3878                 NLAST=$N
3879         done
3880 }
3881 run_test 51d "check object distribution ===================="
3882
3883 test_52a() {
3884         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3885         test_mkdir -p $DIR/$tdir
3886         touch $DIR/$tdir/foo
3887         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3888         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3889         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3890         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3891         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3892                                         error "link worked"
3893         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3894         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3895         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3896                                                      error "lsattr"
3897         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3898         cp -r $DIR/$tdir /tmp/
3899         rm -fr $DIR/$tdir || error "cleanup rm failed"
3900 }
3901 run_test 52a "append-only flag test (should return errors) ====="
3902
3903 test_52b() {
3904         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3905         test_mkdir -p $DIR/$tdir
3906         touch $DIR/$tdir/foo
3907         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3908         cat test > $DIR/$tdir/foo && error "cat test worked"
3909         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3910         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3911         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3912                                         error "link worked"
3913         echo foo >> $DIR/$tdir/foo && error "echo worked"
3914         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3915         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
3916         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
3917         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3918                                                         error "lsattr"
3919         chattr -i $DIR/$tdir/foo || error "chattr failed"
3920
3921         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
3922 }
3923 run_test 52b "immutable flag test (should return errors) ======="
3924
3925 test_53() {
3926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3927         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3928         remote_ost_nodsh && skip "remote OST with nodsh" && return
3929
3930         local param
3931         local param_seq
3932         local ostname
3933         local mds_last
3934         local mds_last_seq
3935         local ost_last
3936         local ost_last_seq
3937         local ost_last_id
3938         local ostnum
3939         local node
3940         local found=false
3941         local support_last_seq=true
3942
3943         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
3944                 support_last_seq=false
3945
3946         # only test MDT0000
3947         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3948         local value
3949         for value in $(do_facet $SINGLEMDS \
3950                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
3951                 param=$(echo ${value[0]} | cut -d "=" -f1)
3952                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
3953
3954                 if $support_last_seq; then
3955                         param_seq=$(echo $param |
3956                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
3957                         mds_last_seq=$(do_facet $SINGLEMDS \
3958                                        $LCTL get_param -n $param_seq)
3959                 fi
3960                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
3961
3962                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3963                 node=$(facet_active_host ost$((ostnum+1)))
3964                 param="obdfilter.$ostname.last_id"
3965                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
3966                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
3967                         ost_last_id=$ost_last
3968
3969                         if $support_last_seq; then
3970                                 ost_last_id=$(echo $ost_last |
3971                                               awk -F':' '{print $2}' |
3972                                               sed -e "s/^0x//g")
3973                                 ost_last_seq=$(echo $ost_last |
3974                                                awk -F':' '{print $1}')
3975                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
3976                         fi
3977
3978                         if [[ $ost_last_id != $mds_last ]]; then
3979                                 error "$ost_last_id != $mds_last"
3980                         else
3981                                 found=true
3982                                 break
3983                         fi
3984                 done
3985         done
3986         $found || error "can not match last_seq/last_id for $mdtosc"
3987         return 0
3988 }
3989 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3990
3991 test_54a() {
3992         $SOCKETSERVER $DIR/socket ||
3993                 error "$SOCKETSERVER $DIR/socket failed: $?"
3994         $SOCKETCLIENT $DIR/socket ||
3995                 error "$SOCKETCLIENT $DIR/socket failed: $?"
3996         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
3997 }
3998 run_test 54a "unix domain socket test =========================="
3999
4000 test_54b() {
4001         f="$DIR/f54b"
4002         mknod $f c 1 3
4003         chmod 0666 $f
4004         dd if=/dev/zero of=$f bs=$(page_size) count=1
4005 }
4006 run_test 54b "char device works in lustre ======================"
4007
4008 find_loop_dev() {
4009         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4010         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4011         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4012
4013         for i in $(seq 3 7); do
4014                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4015                 LOOPDEV=$LOOPBASE$i
4016                 LOOPNUM=$i
4017                 break
4018         done
4019 }
4020
4021 test_54c() {
4022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4023         tfile="$DIR/f54c"
4024         tdir="$DIR/d54c"
4025         loopdev="$DIR/loop54c"
4026
4027         find_loop_dev
4028         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4029         mknod $loopdev b 7 $LOOPNUM
4030         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
4031         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
4032         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
4033         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4034         test_mkdir -p $tdir
4035         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
4036         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
4037         df $tdir
4038         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
4039         $UMOUNT -d $tdir
4040         losetup -d $loopdev
4041         rm $loopdev
4042 }
4043 run_test 54c "block device works in lustre ====================="
4044
4045 test_54d() {
4046         f="$DIR/f54d"
4047         string="aaaaaa"
4048         mknod $f p
4049         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4050 }
4051 run_test 54d "fifo device works in lustre ======================"
4052
4053 test_54e() {
4054         check_kernel_version 46 || return 0
4055         f="$DIR/f54e"
4056         string="aaaaaa"
4057         cp -aL /dev/console $f
4058         echo $string > $f || error "echo $string to $f failed"
4059 }
4060 run_test 54e "console/tty device works in lustre ======================"
4061
4062 #The test_55 used to be iopen test and it was removed by bz#24037.
4063 #run_test 55 "check iopen_connect_dentry() ======================"
4064
4065 test_56a() {    # was test_56
4066         rm -rf $DIR/$tdir
4067         $SETSTRIPE -d $DIR
4068         test_mkdir -p $DIR/$tdir/dir
4069         NUMFILES=3
4070         NUMFILESx2=$(($NUMFILES * 2))
4071         for i in $(seq 1 $NUMFILES); do
4072                 touch $DIR/$tdir/file$i
4073                 touch $DIR/$tdir/dir/file$i
4074         done
4075
4076         # test lfs getstripe with --recursive
4077         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4078         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4079                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4080         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4081         [[ $FILENUM -eq $NUMFILES ]] ||
4082                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4083         echo "$GETSTRIPE --recursive passed."
4084
4085         # test lfs getstripe with file instead of dir
4086         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4087         [[ $FILENUM -eq 1 ]] ||
4088                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4089         echo "$GETSTRIPE file1 passed."
4090
4091         #test lfs getstripe with --verbose
4092         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4093                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4094                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4095         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4096                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4097         echo "$GETSTRIPE --verbose passed."
4098
4099         #test lfs getstripe with --obd
4100         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4101                 grep -q "unknown obduuid" ||
4102                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4103
4104         [[ $OSTCOUNT -lt 2 ]] &&
4105                 skip_env "skipping other $GETSTRIPE --obd test" && return
4106
4107         OSTIDX=1
4108         OBDUUID=$(ostuuid_from_index $OSTIDX)
4109         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4110         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4111         [[ $FOUND -eq $FILENUM ]] ||
4112                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4113         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4114                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4115                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4116                 error "$GETSTRIPE --obd: should not show file on other obd"
4117         echo "$GETSTRIPE --obd passed"
4118 }
4119 run_test 56a "check $GETSTRIPE"
4120
4121 NUMFILES=3
4122 NUMDIRS=3
4123 setup_56() {
4124         local LOCAL_NUMFILES="$1"
4125         local LOCAL_NUMDIRS="$2"
4126         local MKDIR_PARAMS="$3"
4127
4128         if [ ! -d "$TDIR" ] ; then
4129                 test_mkdir -p $TDIR
4130                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4131                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4132                         touch $TDIR/file$i
4133                 done
4134                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4135                         test_mkdir $TDIR/dir$i
4136                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4137                                 touch $TDIR/dir$i/file$j
4138                         done
4139                 done
4140         fi
4141 }
4142
4143 setup_56_special() {
4144         LOCAL_NUMFILES=$1
4145         LOCAL_NUMDIRS=$2
4146         setup_56 $1 $2
4147         if [ ! -e "$TDIR/loop1b" ] ; then
4148                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4149                         mknod $TDIR/loop${i}b b 7 $i
4150                         mknod $TDIR/null${i}c c 1 3
4151                         ln -s $TDIR/file1 $TDIR/link${i}l
4152                 done
4153                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4154                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4155                         mknod $TDIR/dir$i/null${i}c c 1 3
4156                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4157                 done
4158         fi
4159 }
4160
4161 test_56g() {
4162         $SETSTRIPE -d $DIR
4163
4164         TDIR=$DIR/${tdir}g
4165         setup_56 $NUMFILES $NUMDIRS
4166
4167         EXPECTED=$(($NUMDIRS + 2))
4168         # test lfs find with -name
4169         for i in $(seq 1 $NUMFILES) ; do
4170                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4171                 [ $NUMS -eq $EXPECTED ] ||
4172                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4173                               "found $NUMS, expected $EXPECTED"
4174         done
4175 }
4176 run_test 56g "check lfs find -name ============================="
4177
4178 test_56h() {
4179         $SETSTRIPE -d $DIR
4180
4181         TDIR=$DIR/${tdir}g
4182         setup_56 $NUMFILES $NUMDIRS
4183
4184         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4185         # test lfs find with ! -name
4186         for i in $(seq 1 $NUMFILES) ; do
4187                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4188                 [ $NUMS -eq $EXPECTED ] ||
4189                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4190                               "found $NUMS, expected $EXPECTED"
4191         done
4192 }
4193 run_test 56h "check lfs find ! -name ============================="
4194
4195 test_56i() {
4196        tdir=${tdir}i
4197        test_mkdir -p $DIR/$tdir
4198        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4199        CMD="$LFIND -ost $UUID $DIR/$tdir"
4200        OUT=$($CMD)
4201        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4202 }
4203 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4204
4205 test_56j() {
4206         TDIR=$DIR/${tdir}g
4207         setup_56_special $NUMFILES $NUMDIRS
4208
4209         EXPECTED=$((NUMDIRS + 1))
4210         CMD="$LFIND -type d $TDIR"
4211         NUMS=$($CMD | wc -l)
4212         [ $NUMS -eq $EXPECTED ] ||
4213                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4214 }
4215 run_test 56j "check lfs find -type d ============================="
4216
4217 test_56k() {
4218         TDIR=$DIR/${tdir}g
4219         setup_56_special $NUMFILES $NUMDIRS
4220
4221         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4222         CMD="$LFIND -type f $TDIR"
4223         NUMS=$($CMD | wc -l)
4224         [ $NUMS -eq $EXPECTED ] ||
4225                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4226 }
4227 run_test 56k "check lfs find -type f ============================="
4228
4229 test_56l() {
4230         TDIR=$DIR/${tdir}g
4231         setup_56_special $NUMFILES $NUMDIRS
4232
4233         EXPECTED=$((NUMDIRS + NUMFILES))
4234         CMD="$LFIND -type b $TDIR"
4235         NUMS=$($CMD | wc -l)
4236         [ $NUMS -eq $EXPECTED ] ||
4237                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4238 }
4239 run_test 56l "check lfs find -type b ============================="
4240
4241 test_56m() {
4242         TDIR=$DIR/${tdir}g
4243         setup_56_special $NUMFILES $NUMDIRS
4244
4245         EXPECTED=$((NUMDIRS + NUMFILES))
4246         CMD="$LFIND -type c $TDIR"
4247         NUMS=$($CMD | wc -l)
4248         [ $NUMS -eq $EXPECTED ] ||
4249                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4250 }
4251 run_test 56m "check lfs find -type c ============================="
4252
4253 test_56n() {
4254         TDIR=$DIR/${tdir}g
4255         setup_56_special $NUMFILES $NUMDIRS
4256
4257         EXPECTED=$((NUMDIRS + NUMFILES))
4258         CMD="$LFIND -type l $TDIR"
4259         NUMS=$($CMD | wc -l)
4260         [ $NUMS -eq $EXPECTED ] ||
4261                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4262 }
4263 run_test 56n "check lfs find -type l ============================="
4264
4265 test_56o() {
4266         TDIR=$DIR/${tdir}o
4267         setup_56 $NUMFILES $NUMDIRS
4268
4269         utime $TDIR/file1 > /dev/null || error "utime (1)"
4270         utime $TDIR/file2 > /dev/null || error "utime (2)"
4271         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4272         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4273         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4274         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4275
4276         EXPECTED=4
4277         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4278         [ $NUMS -eq $EXPECTED ] || \
4279                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4280
4281         EXPECTED=12
4282         CMD="$LFIND -mtime 0 $TDIR"
4283         NUMS=$($CMD | wc -l)
4284         [ $NUMS -eq $EXPECTED ] ||
4285                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4286 }
4287 run_test 56o "check lfs find -mtime for old files =========================="
4288
4289 test_56p() {
4290         [ $RUNAS_ID -eq $UID ] &&
4291                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4292
4293         TDIR=$DIR/${tdir}p
4294         setup_56 $NUMFILES $NUMDIRS
4295
4296         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4297         EXPECTED=$NUMFILES
4298         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4299         NUMS=$($CMD | wc -l)
4300         [ $NUMS -eq $EXPECTED ] || \
4301                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4302
4303         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4304         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4305         NUMS=$($CMD | wc -l)
4306         [ $NUMS -eq $EXPECTED ] || \
4307                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4308 }
4309 run_test 56p "check lfs find -uid and ! -uid ==============================="
4310
4311 test_56q() {
4312         [ $RUNAS_ID -eq $UID ] &&
4313                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4314
4315         TDIR=$DIR/${tdir}q
4316         setup_56 $NUMFILES $NUMDIRS
4317
4318         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4319
4320         EXPECTED=$NUMFILES
4321         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4322         NUMS=$($CMD | wc -l)
4323         [ $NUMS -eq $EXPECTED ] ||
4324                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4325
4326         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4327         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4328         NUMS=$($CMD | wc -l)
4329         [ $NUMS -eq $EXPECTED ] ||
4330                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4331 }
4332 run_test 56q "check lfs find -gid and ! -gid ==============================="
4333
4334 test_56r() {
4335         TDIR=$DIR/${tdir}r
4336         setup_56 $NUMFILES $NUMDIRS
4337
4338         EXPECTED=12
4339         CMD="$LFIND -size 0 -type f $TDIR"
4340         NUMS=$($CMD | wc -l)
4341         [ $NUMS -eq $EXPECTED ] ||
4342                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4343         EXPECTED=0
4344         CMD="$LFIND ! -size 0 -type f $TDIR"
4345         NUMS=$($CMD | wc -l)
4346         [ $NUMS -eq $EXPECTED ] ||
4347                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4348         echo "test" > $TDIR/$tfile
4349         echo "test2" > $TDIR/$tfile.2 && sync
4350         EXPECTED=1
4351         CMD="$LFIND -size 5 -type f $TDIR"
4352         NUMS=$($CMD | wc -l)
4353         [ $NUMS -eq $EXPECTED ] ||
4354                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4355         EXPECTED=1
4356         CMD="$LFIND -size +5 -type f $TDIR"
4357         NUMS=$($CMD | wc -l)
4358         [ $NUMS -eq $EXPECTED ] ||
4359                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4360         EXPECTED=2
4361         CMD="$LFIND -size +0 -type f $TDIR"
4362         NUMS=$($CMD | wc -l)
4363         [ $NUMS -eq $EXPECTED ] ||
4364                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4365         EXPECTED=2
4366         CMD="$LFIND ! -size -5 -type f $TDIR"
4367         NUMS=$($CMD | wc -l)
4368         [ $NUMS -eq $EXPECTED ] ||
4369                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4370         EXPECTED=12
4371         CMD="$LFIND -size -5 -type f $TDIR"
4372         NUMS=$($CMD | wc -l)
4373         [ $NUMS -eq $EXPECTED ] ||
4374                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4375 }
4376 run_test 56r "check lfs find -size works =========================="
4377
4378 test_56s() { # LU-611
4379         TDIR=$DIR/${tdir}s
4380         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4381
4382         if [[ $OSTCOUNT -gt 1 ]]; then
4383                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4384                 ONESTRIPE=4
4385                 EXTRA=4
4386         else
4387                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4388                 EXTRA=0
4389         fi
4390
4391         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4392         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4393         NUMS=$($CMD | wc -l)
4394         [ $NUMS -eq $EXPECTED ] ||
4395                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4396
4397         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4398         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4399         NUMS=$($CMD | wc -l)
4400         [ $NUMS -eq $EXPECTED ] ||
4401                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4402
4403         EXPECTED=$ONESTRIPE
4404         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4405         NUMS=$($CMD | wc -l)
4406         [ $NUMS -eq $EXPECTED ] ||
4407                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4408
4409         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4410         NUMS=$($CMD | wc -l)
4411         [ $NUMS -eq $EXPECTED ] ||
4412                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4413
4414         EXPECTED=0
4415         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4416         NUMS=$($CMD | wc -l)
4417         [ $NUMS -eq $EXPECTED ] ||
4418                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4419 }
4420 run_test 56s "check lfs find -stripe-count works"
4421
4422 test_56t() { # LU-611
4423         TDIR=$DIR/${tdir}t
4424         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4425
4426         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4427
4428         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4429         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4430         NUMS=$($CMD | wc -l)
4431         [ $NUMS -eq $EXPECTED ] ||
4432                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4433
4434         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4435         NUMS=$($CMD | wc -l)
4436         [ $NUMS -eq $EXPECTED ] ||
4437                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4438
4439         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4440         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4441         NUMS=$($CMD | wc -l)
4442         [ $NUMS -eq $EXPECTED ] ||
4443                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4444
4445         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4446         NUMS=$($CMD | wc -l)
4447         [ $NUMS -eq $EXPECTED ] ||
4448                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4449
4450         EXPECTED=4
4451         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4452         NUMS=$($CMD | wc -l)
4453         [ $NUMS -eq $EXPECTED ] ||
4454                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4455
4456         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4457         NUMS=$($CMD | wc -l)
4458         [ $NUMS -eq $EXPECTED ] ||
4459                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4460
4461         EXPECTED=0
4462         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4463         NUMS=$($CMD | wc -l)
4464         [ $NUMS -eq $EXPECTED ] ||
4465                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4466 }
4467 run_test 56t "check lfs find -stripe-size works"
4468
4469 test_56u() { # LU-611
4470         TDIR=$DIR/${tdir}u
4471         setup_56 $NUMFILES $NUMDIRS "-i 0"
4472
4473         if [[ $OSTCOUNT -gt 1 ]]; then
4474                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4475                 ONESTRIPE=4
4476         else
4477                 ONESTRIPE=0
4478         fi
4479
4480         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4481         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4482         NUMS=$($CMD | wc -l)
4483         [ $NUMS -eq $EXPECTED ] ||
4484                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4485
4486         EXPECTED=$ONESTRIPE
4487         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4488         NUMS=$($CMD | wc -l)
4489         [ $NUMS -eq $EXPECTED ] ||
4490                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4491
4492         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4493         NUMS=$($CMD | wc -l)
4494         [ $NUMS -eq $EXPECTED ] ||
4495                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4496
4497         EXPECTED=0
4498         # This should produce an error and not return any files
4499         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4500         NUMS=$($CMD 2>/dev/null | wc -l)
4501         [ $NUMS -eq $EXPECTED ] ||
4502                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4503
4504         if [[ $OSTCOUNT -gt 1 ]]; then
4505                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4506                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4507                 NUMS=$($CMD | wc -l)
4508                 [ $NUMS -eq $EXPECTED ] ||
4509                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4510         fi
4511 }
4512 run_test 56u "check lfs find -stripe-index works"
4513
4514 test_56v() {
4515     local MDT_IDX=0
4516
4517     TDIR=$DIR/${tdir}v
4518     rm -rf $TDIR
4519     setup_56 $NUMFILES $NUMDIRS
4520
4521     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4522     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4523
4524     for file in $($LFIND -mdt $UUID $TDIR); do
4525         file_mdt_idx=$($GETSTRIPE -M $file)
4526         [ $file_mdt_idx -eq $MDT_IDX ] ||
4527             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4528     done
4529 }
4530 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4531
4532 # Get and check the actual stripe count of one file.
4533 # Usage: check_stripe_count <file> <expected_stripe_count>
4534 check_stripe_count() {
4535     local file=$1
4536     local expected=$2
4537     local actual
4538
4539     [[ -z "$file" || -z "$expected" ]] &&
4540         error "check_stripe_count: invalid argument!"
4541
4542     local cmd="$GETSTRIPE -c $file"
4543     actual=$($cmd) || error "$cmd failed"
4544     actual=${actual%% *}
4545
4546     if [[ $actual -ne $expected ]]; then
4547         [[ $expected -eq -1 ]] ||
4548             error "$cmd wrong: found $actual, expected $expected"
4549         [[ $actual -eq $OSTCOUNT ]] ||
4550             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4551     fi
4552 }
4553
4554 test_56w() {
4555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4556         TDIR=$DIR/${tdir}w
4557
4558     rm -rf $TDIR || error "remove $TDIR failed"
4559     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4560
4561     local stripe_size
4562     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4563         error "$GETSTRIPE -S -d $TDIR failed"
4564     stripe_size=${stripe_size%% *}
4565
4566     local file_size=$((stripe_size * OSTCOUNT))
4567     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4568     local required_space=$((file_num * file_size))
4569     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4570     [[ $free_space -le $((required_space / 1024)) ]] &&
4571         skip_env "need at least $required_space bytes free space," \
4572                  "have $free_space kbytes" && return
4573
4574     local dd_bs=65536
4575     local dd_count=$((file_size / dd_bs))
4576
4577     # write data into the files
4578     local i
4579     local j
4580     local file
4581     for i in $(seq 1 $NUMFILES); do
4582         file=$TDIR/file$i
4583         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4584             error "write data into $file failed"
4585     done
4586     for i in $(seq 1 $NUMDIRS); do
4587         for j in $(seq 1 $NUMFILES); do
4588             file=$TDIR/dir$i/file$j
4589             yes | dd bs=$dd_bs count=$dd_count of=$file \
4590                 >/dev/null 2>&1 ||
4591                 error "write data into $file failed"
4592         done
4593     done
4594
4595     local expected=-1
4596     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4597
4598     # lfs_migrate file
4599     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4600     echo "$cmd"
4601     eval $cmd || error "$cmd failed"
4602
4603     check_stripe_count $TDIR/file1 $expected
4604
4605     # lfs_migrate dir
4606     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4607     echo "$cmd"
4608     eval $cmd || error "$cmd failed"
4609
4610     for j in $(seq 1 $NUMFILES); do
4611         check_stripe_count $TDIR/dir1/file$j $expected
4612     done
4613
4614     # lfs_migrate works with lfs find
4615     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4616          $LFS_MIGRATE -y -c $expected"
4617     echo "$cmd"
4618     eval $cmd || error "$cmd failed"
4619
4620     for i in $(seq 2 $NUMFILES); do
4621         check_stripe_count $TDIR/file$i $expected
4622     done
4623     for i in $(seq 2 $NUMDIRS); do
4624         for j in $(seq 1 $NUMFILES); do
4625             check_stripe_count $TDIR/dir$i/file$j $expected
4626         done
4627     done
4628 }
4629 run_test 56w "check lfs_migrate -c stripe_count works"
4630
4631 test_56x() {
4632         check_swap_layouts_support && return 0
4633         [[ $OSTCOUNT -lt 2 ]] &&
4634                 skip_env "need 2 OST, skipping test" && return
4635
4636         local dir0=$DIR/$tdir/$testnum
4637         mkdir -p $dir0 || error "creating dir $dir0"
4638
4639         local ref1=/etc/passwd
4640         local file1=$dir0/file1
4641
4642         $SETSTRIPE -c 2 $file1
4643         cp $ref1 $file1
4644         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4645         stripe=$($GETSTRIPE -c $file1)
4646         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4647         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4648
4649         # clean up
4650         rm -f $file1
4651 }
4652 run_test 56x "lfs migration support"
4653
4654 test_56y() {
4655         local server_version=$(get_lustre_version $SINGLEMDS)
4656         [[ $(version_code $server_version) -lt $(version_code 2.4.53) ]] &&
4657                 skip "No HSM support on $server_version MDS," \
4658                         "need 2.4.53 at least" && return
4659
4660         local res=""
4661
4662         local dir0=$DIR/$tdir/$testnum
4663         mkdir -p $dir0 || error "creating dir $dir0"
4664         local f1=$dir0/file1
4665         local f2=$dir0/file2
4666
4667         touch $f1 || error "creating std file $f1"
4668         $MULTIOP $f2 H2c || error "creating released file $f2"
4669
4670         # a directory can be raid0, so ask only for files
4671         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4672         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4673
4674         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4675         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4676
4677         # only files can be released, so no need to force file search
4678         res=$($LFIND $dir0 -L released)
4679         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4680
4681         res=$($LFIND $dir0 \! -L released)
4682         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4683
4684 }
4685 run_test 56y "lfs find -L raid0|released"
4686
4687 test_57a() {
4688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4689         # note test will not do anything if MDS is not local
4690         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4691                 skip "Only applicable to ldiskfs-based MDTs"
4692                 return
4693         fi
4694
4695         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4696         local MNTDEV="osd*.*MDT*.mntdev"
4697         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4698         [ -z "$DEV" ] && error "can't access $MNTDEV"
4699         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4700                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4701                         error "can't access $DEV"
4702                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4703                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4704                 rm $TMP/t57a.dump
4705         done
4706 }
4707 run_test 57a "verify MDS filesystem created with large inodes =="
4708
4709 test_57b() {
4710         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4711         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4712                 skip "Only applicable to ldiskfs-based MDTs"
4713                 return
4714         fi
4715
4716         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4717         local dir=$DIR/d57b
4718
4719         local FILECOUNT=100
4720         local FILE1=$dir/f1
4721         local FILEN=$dir/f$FILECOUNT
4722
4723         rm -rf $dir || error "removing $dir"
4724         test_mkdir -p $dir || error "creating $dir"
4725         local num=$(get_mds_dir $dir)
4726         local mymds=mds$num
4727
4728         echo "mcreating $FILECOUNT files"
4729         createmany -m $dir/f 1 $FILECOUNT || \
4730                 error "creating files in $dir"
4731
4732         # verify that files do not have EAs yet
4733         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4734         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4735
4736         sync
4737         sleep 1
4738         df $dir  #make sure we get new statfs data
4739         local MDSFREE=$(do_facet $mymds \
4740                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4741         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4742         echo "opening files to create objects/EAs"
4743         local FILE
4744         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4745                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4746         done
4747
4748         # verify that files have EAs now
4749         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4750         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4751
4752         sleep 1  #make sure we get new statfs data
4753         df $dir
4754         local MDSFREE2=$(do_facet $mymds \
4755                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4756         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4757         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
4758                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4759                         error "MDC before $MDCFREE != after $MDCFREE2"
4760                 else
4761                         echo "MDC before $MDCFREE != after $MDCFREE2"
4762                         echo "unable to confirm if MDS has large inodes"
4763                 fi
4764         fi
4765         rm -rf $dir
4766 }
4767 run_test 57b "default LOV EAs are stored inside large inodes ==="
4768
4769 test_58() {
4770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4771         [ -z "$(which wiretest 2>/dev/null)" ] &&
4772                         skip_env "could not find wiretest" && return
4773         wiretest
4774 }
4775 run_test 58 "verify cross-platform wire constants =============="
4776
4777 test_59() {
4778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4779         echo "touch 130 files"
4780         createmany -o $DIR/f59- 130
4781         echo "rm 130 files"
4782         unlinkmany $DIR/f59- 130
4783         sync
4784         # wait for commitment of removal
4785         wait_delete_completed
4786 }
4787 run_test 59 "verify cancellation of llog records async ========="
4788
4789 TEST60_HEAD="test_60 run $RANDOM"
4790 test_60a() {
4791         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4792         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4793         do_facet mgs "! which run-llog.sh &> /dev/null" &&
4794                 skip_env "missing subtest run-llog.sh" && return
4795         log "$TEST60_HEAD - from kernel mode"
4796         do_facet mgs sh run-llog.sh
4797 }
4798 run_test 60a "llog sanity tests run from kernel module =========="
4799
4800 test_60b() { # bug 6411
4801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4802         dmesg > $DIR/$tfile
4803         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4804                                  /llog.test/ {
4805                                          if (marker)
4806                                                  from_marker++
4807                                          from_begin++
4808                                  }
4809                                  END {
4810                                          if (marker)
4811                                                  print from_marker
4812                                          else
4813                                                  print from_begin
4814                                  }"`
4815         [[ $LLOG_COUNT -gt 50 ]] &&
4816                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
4817 }
4818 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4819
4820 test_60c() {
4821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4822         echo "create 5000 files"
4823         createmany -o $DIR/f60c- 5000
4824 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4825         lctl set_param fail_loc=0x80000137
4826         unlinkmany $DIR/f60c- 5000
4827         lctl set_param fail_loc=0
4828 }
4829 run_test 60c "unlink file when mds full"
4830
4831 test_60d() {
4832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4833         SAVEPRINTK=$(lctl get_param -n printk)
4834
4835         # verify "lctl mark" is even working"
4836         MESSAGE="test message ID $RANDOM $$"
4837         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4838         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4839
4840         lctl set_param printk=0 || error "set lnet.printk failed"
4841         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4842         MESSAGE="new test message ID $RANDOM $$"
4843         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4844         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4845         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4846
4847         lctl set_param -n printk="$SAVEPRINTK"
4848 }
4849 run_test 60d "test printk console message masking"
4850
4851 test_61() {
4852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4853         f="$DIR/f61"
4854         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
4855         cancel_lru_locks osc
4856         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
4857         sync
4858 }
4859 run_test 61 "mmap() writes don't make sync hang ================"
4860
4861 # bug 2330 - insufficient obd_match error checking causes LBUG
4862 test_62() {
4863         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4864         f="$DIR/f62"
4865         echo foo > $f
4866         cancel_lru_locks osc
4867         lctl set_param fail_loc=0x405
4868         cat $f && error "cat succeeded, expect -EIO"
4869         lctl set_param fail_loc=0
4870 }
4871 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4872 # match every page all of the time.
4873 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4874
4875 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4876 test_63a() {    # was test_63
4877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4878         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4879         lctl set_param -n osc.*.max_dirty_mb 0
4880         for i in `seq 10` ; do
4881                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4882                 sleep 5
4883                 kill $!
4884                 sleep 1
4885         done
4886
4887         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4888         rm -f $DIR/f63 || true
4889 }
4890 run_test 63a "Verify oig_wait interruption does not crash ======="
4891
4892 # bug 2248 - async write errors didn't return to application on sync
4893 # bug 3677 - async write errors left page locked
4894 test_63b() {
4895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4896         debugsave
4897         lctl set_param debug=-1
4898
4899         # ensure we have a grant to do async writes
4900         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4901         rm $DIR/$tfile
4902
4903         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4904         lctl set_param fail_loc=0x80000406
4905         $MULTIOP $DIR/$tfile Owy && \
4906                 error "sync didn't return ENOMEM"
4907         sync; sleep 2; sync     # do a real sync this time to flush page
4908         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4909                 error "locked page left in cache after async error" || true
4910         debugrestore
4911 }
4912 run_test 63b "async write errors should be returned to fsync ==="
4913
4914 test_64a () {
4915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4916         df $DIR
4917         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4918 }
4919 run_test 64a "verify filter grant calculations (in kernel) ====="
4920
4921 test_64b () {
4922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4923         sh oos.sh $MOUNT || error "oos.sh failed: $?"
4924 }
4925 run_test 64b "check out-of-space detection on client ==========="
4926
4927 test_64c() {
4928         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4929 }
4930 run_test 64c "verify grant shrink ========================------"
4931
4932 # bug 1414 - set/get directories' stripe info
4933 test_65a() {
4934         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4935         test_mkdir -p $DIR/$tdir
4936         touch $DIR/$tdir/f1
4937         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4938 }
4939 run_test 65a "directory with no stripe info ===================="
4940
4941 test_65b() {
4942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4943         test_mkdir -p $DIR/$tdir
4944         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4945                                                 error "setstripe"
4946         touch $DIR/$tdir/f2
4947         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4948 }
4949 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4950
4951 test_65c() {
4952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4953         if [[ $OSTCOUNT -gt 1 ]]; then
4954                 test_mkdir -p $DIR/$tdir
4955                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4956                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4957                 touch $DIR/$tdir/f3
4958                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4959         fi
4960 }
4961 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4962
4963 test_65d() {
4964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4965         test_mkdir -p $DIR/$tdir
4966         if [[ $STRIPECOUNT -le 0 ]]; then
4967                 sc=1
4968         elif [[ $STRIPECOUNT -gt 2000 ]]; then
4969 #LOV_MAX_STRIPE_COUNT is 2000
4970                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
4971         else
4972                 sc=$(($STRIPECOUNT - 1))
4973         fi
4974         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4975         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4976         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4977                 error "lverify failed"
4978 }
4979 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4980
4981 test_65e() {
4982         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4983         test_mkdir -p $DIR/$tdir
4984
4985         $SETSTRIPE $DIR/$tdir || error "setstripe"
4986         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4987                                         error "no stripe info failed"
4988         touch $DIR/$tdir/f6
4989         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4990 }
4991 run_test 65e "directory setstripe defaults ======================="
4992
4993 test_65f() {
4994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4995         test_mkdir -p $DIR/${tdir}f
4996         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4997 }
4998 run_test 65f "dir setstripe permission (should return error) ==="
4999
5000 test_65g() {
5001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5002         test_mkdir -p $DIR/$tdir
5003         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5004                                                         error "setstripe"
5005         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5006         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5007                 error "delete default stripe failed"
5008 }
5009 run_test 65g "directory setstripe -d ==========================="
5010
5011 test_65h() {
5012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5013         test_mkdir -p $DIR/$tdir
5014         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5015                                                         error "setstripe"
5016         test_mkdir -p $DIR/$tdir/dd1
5017         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5018                 error "stripe info inherit failed"
5019 }
5020 run_test 65h "directory stripe info inherit ===================="
5021
5022 test_65i() { # bug6367
5023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5024         $SETSTRIPE -S 65536 -c -1 $MOUNT
5025 }
5026 run_test 65i "set non-default striping on root directory (bug 6367)="
5027
5028 test_65ia() { # bug12836
5029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5030         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5031 }
5032 run_test 65ia "getstripe on -1 default directory striping"
5033
5034 test_65ib() { # bug12836
5035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5036         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5037 }
5038 run_test 65ib "getstripe -v on -1 default directory striping"
5039
5040 test_65ic() { # bug12836
5041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5042         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5043 }
5044 run_test 65ic "new find on -1 default directory striping"
5045
5046 test_65j() { # bug6367
5047         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5048         sync; sleep 1
5049         # if we aren't already remounting for each test, do so for this test
5050         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5051                 cleanup || error "failed to unmount"
5052                 setup
5053         fi
5054         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5055 }
5056 run_test 65j "set default striping on root directory (bug 6367)="
5057
5058 test_65k() { # bug11679
5059         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5060         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5061         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5062
5063     echo "Check OST status: "
5064     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5065               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5066
5067     for OSC in $MDS_OSCS; do
5068         echo $OSC "is activate"
5069         do_facet $SINGLEMDS lctl --device %$OSC activate
5070     done
5071
5072     mkdir -p $DIR/$tdir
5073     for INACTIVE_OSC in $MDS_OSCS; do
5074         echo "Deactivate: " $INACTIVE_OSC
5075         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5076         for STRIPE_OSC in $MDS_OSCS; do
5077             OST=`osc_to_ost $STRIPE_OSC`
5078             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5079                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5080
5081             [ -f $DIR/$tdir/$IDX ] && continue
5082             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5083             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5084             RC=$?
5085             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5086         done
5087         rm -f $DIR/$tdir/*
5088         echo $INACTIVE_OSC "is Activate."
5089         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5090     done
5091 }
5092 run_test 65k "validate manual striping works properly with deactivated OSCs"
5093
5094 test_65l() { # bug 12836
5095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5096         test_mkdir -p $DIR/$tdir/test_dir
5097         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5098         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5099 }
5100 run_test 65l "lfs find on -1 stripe dir ========================"
5101
5102 # bug 2543 - update blocks count on client
5103 test_66() {
5104         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5105         COUNT=${COUNT:-8}
5106         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5107         sync; sync_all_data; sync; sync_all_data
5108         cancel_lru_locks osc
5109         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5110         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5111 }
5112 run_test 66 "update inode blocks count on client ==============="
5113
5114 LLOOP=
5115 LLITELOOPLOAD=
5116 cleanup_68() {
5117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5118         trap 0
5119         if [ ! -z "$LLOOP" ]; then
5120                 if swapon -s | grep -q $LLOOP; then
5121                         swapoff $LLOOP || error "swapoff failed"
5122                 fi
5123
5124                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5125                 rm -f $LLOOP
5126                 unset LLOOP
5127         fi
5128         if [ ! -z "$LLITELOOPLOAD" ]; then
5129                 rmmod llite_lloop
5130                 unset LLITELOOPLOAD
5131         fi
5132         rm -f $DIR/f68*
5133 }
5134
5135 meminfo() {
5136         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5137 }
5138
5139 swap_used() {
5140         swapon -s | awk '($1 == "'$1'") { print $4 }'
5141 }
5142
5143 # test case for lloop driver, basic function
5144 test_68a() {
5145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5146         [ "$UID" != 0 ] && skip_env "must run as root" && return
5147         llite_lloop_enabled || \
5148                 { skip_env "llite_lloop module disabled" && return; }
5149
5150         trap cleanup_68 EXIT
5151
5152         if ! module_loaded llite_lloop; then
5153                 if load_module llite/llite_lloop; then
5154                         LLITELOOPLOAD=yes
5155                 else
5156                         skip_env "can't find module llite_lloop"
5157                         return
5158                 fi
5159         fi
5160
5161         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5162         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5163         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5164
5165         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5166         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5167
5168         cleanup_68
5169 }
5170 run_test 68a "lloop driver - basic test ========================"
5171
5172 # excercise swapping to lustre by adding a high priority swapfile entry
5173 # and then consuming memory until it is used.
5174 test_68b() {  # was test_68
5175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5176         [ "$UID" != 0 ] && skip_env "must run as root" && return
5177         lctl get_param -n devices | grep -q obdfilter && \
5178                 skip "local OST" && return
5179
5180         grep -q llite_lloop /proc/modules
5181         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5182
5183         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5184                 skip "can't reliably test swap with TCP" && return
5185
5186         MEMTOTAL=`meminfo MemTotal`
5187         NR_BLOCKS=$((MEMTOTAL>>8))
5188         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5189
5190         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5191         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5192         mkswap $DIR/f68b
5193
5194         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5195
5196         trap cleanup_68 EXIT
5197
5198         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5199
5200         echo "before: `swapon -s | grep $LLOOP`"
5201         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5202         echo "after: `swapon -s | grep $LLOOP`"
5203         SWAPUSED=`swap_used $LLOOP`
5204
5205         cleanup_68
5206
5207         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5208 }
5209 run_test 68b "support swapping to Lustre ========================"
5210
5211 # bug5265, obdfilter oa2dentry return -ENOENT
5212 # #define OBD_FAIL_OST_ENOENT 0x217
5213 test_69() {
5214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5215         remote_ost_nodsh && skip "remote OST with nodsh" && return
5216
5217         f="$DIR/$tfile"
5218         $SETSTRIPE -c 1 -i 0 $f
5219
5220         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5221
5222         do_facet ost1 lctl set_param fail_loc=0x217
5223         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5224         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5225
5226         do_facet ost1 lctl set_param fail_loc=0
5227         $DIRECTIO write $f 0 2 || error "write error"
5228
5229         cancel_lru_locks osc
5230         $DIRECTIO read $f 0 1 || error "read error"
5231
5232         do_facet ost1 lctl set_param fail_loc=0x217
5233         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5234
5235         do_facet ost1 lctl set_param fail_loc=0
5236         rm -f $f
5237 }
5238 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5239
5240 test_71() {
5241     test_mkdir -p $DIR/$tdir
5242     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5243 }
5244 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5245
5246 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5248         [ "$RUNAS_ID" = "$UID" ] &&
5249                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5250
5251         # Check that testing environment is properly set up. Skip if not
5252         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5253                 skip_env "User $RUNAS_ID does not exist - skipping"
5254                 return 0
5255         }
5256         # We had better clear the $DIR to get enough space for dd
5257         rm -rf $DIR/*
5258         touch $DIR/$tfile
5259         chmod 777 $DIR/$tfile
5260         chmod ug+s $DIR/$tfile
5261         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5262                 error "$RUNAS dd $DIR/$tfile failed"
5263         # See if we are still setuid/sgid
5264         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5265                 error "S/gid is not dropped on write"
5266         # Now test that MDS is updated too
5267         cancel_lru_locks mdc
5268         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5269                 error "S/gid is not dropped on MDS"
5270         rm -f $DIR/$tfile
5271 }
5272 run_test 72a "Test that remove suid works properly (bug5695) ===="
5273
5274 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5275         local perm
5276
5277         [ "$RUNAS_ID" = "$UID" ] && \
5278                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5279         [ "$RUNAS_ID" -eq 0 ] && \
5280                 skip_env "RUNAS_ID = 0 -- skipping" && return
5281
5282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5283         # Check that testing environment is properly set up. Skip if not
5284         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5285                 skip_env "User $RUNAS_ID does not exist - skipping"
5286                 return 0
5287         }
5288         touch $DIR/${tfile}-f{g,u}
5289         test_mkdir $DIR/${tfile}-dg
5290         test_mkdir $DIR/${tfile}-du
5291         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5292         chmod g+s $DIR/${tfile}-{f,d}g
5293         chmod u+s $DIR/${tfile}-{f,d}u
5294         for perm in 777 2777 4777; do
5295                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5296                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5297                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5298                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5299         done
5300         true
5301 }
5302 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5303
5304 # bug 3462 - multiple simultaneous MDC requests
5305 test_73() {
5306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5307         test_mkdir $DIR/d73-1
5308         test_mkdir $DIR/d73-2
5309         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5310         pid1=$!
5311
5312         lctl set_param fail_loc=0x80000129
5313         $MULTIOP $DIR/d73-1/f73-2 Oc &
5314         sleep 1
5315         lctl set_param fail_loc=0
5316
5317         $MULTIOP $DIR/d73-2/f73-3 Oc &
5318         pid3=$!
5319
5320         kill -USR1 $pid1
5321         wait $pid1 || return 1
5322
5323         sleep 25
5324
5325         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5326         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5327         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5328
5329         rm -rf $DIR/d73-*
5330 }
5331 run_test 73 "multiple MDC requests (should not deadlock)"
5332
5333 test_74a() { # bug 6149, 6184
5334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5335         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5336         #
5337         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5338         # will spin in a tight reconnection loop
5339         touch $DIR/f74a
5340         $LCTL set_param fail_loc=0x8000030e
5341         # get any lock that won't be difficult - lookup works.
5342         ls $DIR/f74a
5343         $LCTL set_param fail_loc=0
5344         rm -f $DIR/f74a
5345         true
5346 }
5347 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5348
5349 test_74b() { # bug 13310
5350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5351         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5352         #
5353         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5354         # will spin in a tight reconnection loop
5355         $LCTL set_param fail_loc=0x8000030e
5356         # get a "difficult" lock
5357         touch $DIR/f74b
5358         $LCTL set_param fail_loc=0
5359         rm -f $DIR/f74b
5360         true
5361 }
5362 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5363
5364 test_74c() {
5365         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5366         #define OBD_FAIL_LDLM_NEW_LOCK
5367         $LCTL set_param fail_loc=0x319
5368         touch $DIR/$tfile && error "touch successful"
5369         $LCTL set_param fail_loc=0
5370         true
5371 }
5372 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5373
5374 num_inodes() {
5375         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5376 }
5377
5378 get_inode_slab_tunables() {
5379         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5380 }
5381
5382 set_inode_slab_tunables() {
5383         echo "lustre_inode_cache $1" > /proc/slabinfo
5384 }
5385
5386 test_76() { # Now for bug 20433, added originally in bug 1443
5387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5388         local SLAB_SETTINGS=`get_inode_slab_tunables`
5389         local CPUS=`getconf _NPROCESSORS_ONLN`
5390         # we cannot set limit below 1 which means 1 inode in each
5391         # per-cpu cache is still allowed
5392         set_inode_slab_tunables "1 1 0"
5393         cancel_lru_locks osc
5394         BEFORE_INODES=$(num_inodes)
5395         echo "before inodes: $BEFORE_INODES"
5396         local COUNT=1000
5397         [ "$SLOW" = "no" ] && COUNT=100
5398         for i in $(seq $COUNT); do
5399                 touch $DIR/$tfile
5400                 rm -f $DIR/$tfile
5401         done
5402         cancel_lru_locks osc
5403         AFTER_INODES=$(num_inodes)
5404         echo "after inodes: $AFTER_INODES"
5405         local wait=0
5406         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5407                 sleep 2
5408                 AFTER_INODES=$(num_inodes)
5409                 wait=$((wait+2))
5410                 echo "wait $wait seconds inodes: $AFTER_INODES"
5411                 if [ $wait -gt 30 ]; then
5412                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5413                 fi
5414         done
5415         set_inode_slab_tunables "$SLAB_SETTINGS"
5416 }
5417 run_test 76 "confirm clients recycle inodes properly ===="
5418
5419
5420 export ORIG_CSUM=""
5421 set_checksums()
5422 {
5423         # Note: in sptlrpc modes which enable its own bulk checksum, the
5424         # original crc32_le bulk checksum will be automatically disabled,
5425         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5426         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5427         # In this case set_checksums() will not be no-op, because sptlrpc
5428         # bulk checksum will be enabled all through the test.
5429
5430         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5431         lctl set_param -n osc.*.checksums $1
5432         return 0
5433 }
5434
5435 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5436                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5437 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5438 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5439 set_checksum_type()
5440 {
5441         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5442         log "set checksum type to $1"
5443         return 0
5444 }
5445 F77_TMP=$TMP/f77-temp
5446 F77SZ=8
5447 setup_f77() {
5448         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5449                 error "error writing to $F77_TMP"
5450 }
5451
5452 test_77a() { # bug 10889
5453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5454         $GSS && skip "could not run with gss" && return
5455         [ ! -f $F77_TMP ] && setup_f77
5456         set_checksums 1
5457         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5458         set_checksums 0
5459         rm -f $DIR/$tfile
5460 }
5461 run_test 77a "normal checksum read/write operation"
5462
5463 test_77b() { # bug 10889
5464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5465         $GSS && skip "could not run with gss" && return
5466         [ ! -f $F77_TMP ] && setup_f77
5467         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5468         $LCTL set_param fail_loc=0x80000409
5469         set_checksums 1
5470
5471         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5472                 error "dd error: $?"
5473         $LCTL set_param fail_loc=0
5474
5475         for algo in $CKSUM_TYPES; do
5476                 cancel_lru_locks osc
5477                 set_checksum_type $algo
5478                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5479                 $LCTL set_param fail_loc=0x80000408
5480                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5481                 $LCTL set_param fail_loc=0
5482         done
5483         set_checksums 0
5484         set_checksum_type $ORIG_CSUM_TYPE
5485         rm -f $DIR/$tfile
5486 }
5487 run_test 77b "checksum error on client write, read"
5488
5489 test_77d() { # bug 10889
5490         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5491         $GSS && skip "could not run with gss" && return
5492         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5493         $LCTL set_param fail_loc=0x80000409
5494         set_checksums 1
5495         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5496                 error "direct write: rc=$?"
5497         $LCTL set_param fail_loc=0
5498         set_checksums 0
5499
5500         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5501         $LCTL set_param fail_loc=0x80000408
5502         set_checksums 1
5503         cancel_lru_locks osc
5504         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5505                 error "direct read: rc=$?"
5506         $LCTL set_param fail_loc=0
5507         set_checksums 0
5508 }
5509 run_test 77d "checksum error on OST direct write, read"
5510
5511 test_77f() { # bug 10889
5512         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5513         $GSS && skip "could not run with gss" && return
5514         set_checksums 1
5515         for algo in $CKSUM_TYPES; do
5516                 cancel_lru_locks osc
5517                 set_checksum_type $algo
5518                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5519                 $LCTL set_param fail_loc=0x409
5520                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5521                         error "direct write succeeded"
5522                 $LCTL set_param fail_loc=0
5523         done
5524         set_checksum_type $ORIG_CSUM_TYPE
5525         set_checksums 0
5526 }
5527 run_test 77f "repeat checksum error on write (expect error)"
5528
5529 test_77g() { # bug 10889
5530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5531         $GSS && skip "could not run with gss" && return
5532         remote_ost_nodsh && skip "remote OST with nodsh" && return
5533
5534         [ ! -f $F77_TMP ] && setup_f77
5535
5536         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5537         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5538         do_facet ost1 lctl set_param fail_loc=0x8000021a
5539         set_checksums 1
5540         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5541                 error "write error: rc=$?"
5542         do_facet ost1 lctl set_param fail_loc=0
5543         set_checksums 0
5544
5545         cancel_lru_locks osc
5546         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5547         do_facet ost1 lctl set_param fail_loc=0x8000021b
5548         set_checksums 1
5549         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5550         do_facet ost1 lctl set_param fail_loc=0
5551         set_checksums 0
5552 }
5553 run_test 77g "checksum error on OST write, read"
5554
5555 test_77i() { # bug 13805
5556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5557         $GSS && skip "could not run with gss" && return
5558         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5559         lctl set_param fail_loc=0x40b
5560         remount_client $MOUNT
5561         lctl set_param fail_loc=0
5562         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5563                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5564                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5565                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5566         done
5567         remount_client $MOUNT
5568 }
5569 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5570
5571 test_77j() { # bug 13805
5572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5573         $GSS && skip "could not run with gss" && return
5574         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5575         lctl set_param fail_loc=0x40c
5576         remount_client $MOUNT
5577         lctl set_param fail_loc=0
5578         sleep 2 # wait async osc connect to finish
5579         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5580                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5581                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5582                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5583         done
5584         remount_client $MOUNT
5585 }
5586 run_test 77j "client only supporting ADLER32"
5587
5588 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5589 rm -f $F77_TMP
5590 unset F77_TMP
5591
5592 test_78() { # bug 10901
5593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5594         remote_ost || { skip_env "local OST" && return; }
5595
5596         NSEQ=5
5597         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5598         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5599         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5600         echo "MemTotal: $MEMTOTAL"
5601
5602         # reserve 256MB of memory for the kernel and other running processes,
5603         # and then take 1/2 of the remaining memory for the read/write buffers.
5604         if [ $MEMTOTAL -gt 512 ] ;then
5605                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5606         else
5607                 # for those poor memory-starved high-end clusters...
5608                 MEMTOTAL=$((MEMTOTAL / 2))
5609         fi
5610         echo "Mem to use for directio: $MEMTOTAL"
5611
5612         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5613         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5614         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5615         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5616                 head -n1)
5617         echo "Smallest OST: $SMALLESTOST"
5618         [[ $SMALLESTOST -lt 10240 ]] &&
5619                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5620
5621         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5622                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5623
5624         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5625         echo "File size: $F78SIZE"
5626         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5627         for i in $(seq 1 $NSEQ); do
5628                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5629                 echo directIO rdwr round $i of $NSEQ
5630                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5631         done
5632
5633         rm -f $DIR/$tfile
5634 }
5635 run_test 78 "handle large O_DIRECT writes correctly ============"
5636
5637 test_79() { # bug 12743
5638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5639         wait_delete_completed
5640
5641         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5642         BKFREE=$(calc_osc_kbytes kbytesfree)
5643         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5644
5645         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5646         DFTOTAL=`echo $STRING | cut -d, -f1`
5647         DFUSED=`echo $STRING  | cut -d, -f2`
5648         DFAVAIL=`echo $STRING | cut -d, -f3`
5649         DFFREE=$(($DFTOTAL - $DFUSED))
5650
5651         ALLOWANCE=$((64 * $OSTCOUNT))
5652
5653         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5654            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5655                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5656         fi
5657         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5658            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5659                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5660         fi
5661         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5662            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5663                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5664         fi
5665 }
5666 run_test 79 "df report consistency check ======================="
5667
5668 test_80() { # bug 10718
5669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5670         # relax strong synchronous semantics for slow backends like ZFS
5671         local soc="obdfilter.*.sync_on_lock_cancel"
5672         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5673         local hosts=
5674         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5675                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5676                           facet_active_host $host; done | sort -u)
5677                 do_nodes $hosts lctl set_param $soc=never
5678         fi
5679
5680         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5681         sync; sleep 1; sync
5682         local BEFORE=`date +%s`
5683         cancel_lru_locks osc
5684         local AFTER=`date +%s`
5685         local DIFF=$((AFTER-BEFORE))
5686         if [ $DIFF -gt 1 ] ; then
5687                 error "elapsed for 1M@1T = $DIFF"
5688         fi
5689
5690         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5691
5692         rm -f $DIR/$tfile
5693 }
5694 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5695
5696 test_81a() { # LU-456
5697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5698         remote_ost_nodsh && skip "remote OST with nodsh" && return
5699         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5700         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5701         do_facet ost1 lctl set_param fail_loc=0x80000228
5702
5703         # write should trigger a retry and success
5704         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5705         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5706         RC=$?
5707         if [ $RC -ne 0 ] ; then
5708                 error "write should success, but failed for $RC"
5709         fi
5710 }
5711 run_test 81a "OST should retry write when get -ENOSPC ==============="
5712
5713 test_81b() { # LU-456
5714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5715         remote_ost_nodsh && skip "remote OST with nodsh" && return
5716         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5717         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5718         do_facet ost1 lctl set_param fail_loc=0x228
5719
5720         # write should retry several times and return -ENOSPC finally
5721         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5722         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5723         RC=$?
5724         ENOSPC=28
5725         if [ $RC -ne $ENOSPC ] ; then
5726                 error "dd should fail for -ENOSPC, but succeed."
5727         fi
5728 }
5729 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5730
5731 test_82() { # LU-1031
5732         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5733         local gid1=14091995
5734         local gid2=16022000
5735
5736         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5737         local MULTIPID1=$!
5738         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5739         local MULTIPID2=$!
5740         kill -USR1 $MULTIPID2
5741         sleep 2
5742         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5743                 error "First grouplock does not block second one"
5744         else
5745                 echo "Second grouplock blocks first one"
5746         fi
5747         kill -USR1 $MULTIPID1
5748         wait $MULTIPID1
5749         wait $MULTIPID2
5750 }
5751 run_test 82 "Basic grouplock test ==============================="
5752
5753 test_99a() {
5754         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5755                 return
5756         test_mkdir -p $DIR/d99cvsroot
5757         chown $RUNAS_ID $DIR/d99cvsroot
5758         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5759         cd $TMP
5760
5761         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5762         cd $oldPWD
5763 }
5764 run_test 99a "cvs init ========================================="
5765
5766 test_99b() {
5767         [ -z "$(which cvs 2>/dev/null)" ] &&
5768                 skip_env "could not find cvs" && return
5769         [ ! -d $DIR/d99cvsroot ] && test_99a
5770         cd /etc/init.d
5771         # some versions of cvs import exit(1) when asked to import links or
5772         # files they can't read.  ignore those files.
5773         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5774                         ! -perm +4 -printf '-I %f\n')
5775         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5776                 d99reposname vtag rtag
5777 }
5778 run_test 99b "cvs import ======================================="
5779
5780 test_99c() {
5781         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5782         [ ! -d $DIR/d99cvsroot ] && test_99b
5783         cd $DIR
5784         test_mkdir -p $DIR/d99reposname
5785         chown $RUNAS_ID $DIR/d99reposname
5786         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5787 }
5788 run_test 99c "cvs checkout ====================================="
5789
5790 test_99d() {
5791         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5792         [ ! -d $DIR/d99cvsroot ] && test_99c
5793         cd $DIR/d99reposname
5794         $RUNAS touch foo99
5795         $RUNAS cvs add -m 'addmsg' foo99
5796 }
5797 run_test 99d "cvs add =========================================="
5798
5799 test_99e() {
5800         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5801         [ ! -d $DIR/d99cvsroot ] && test_99c
5802         cd $DIR/d99reposname
5803         $RUNAS cvs update
5804 }
5805 run_test 99e "cvs update ======================================="
5806
5807 test_99f() {
5808         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5809         [ ! -d $DIR/d99cvsroot ] && test_99d
5810         cd $DIR/d99reposname
5811         $RUNAS cvs commit -m 'nomsg' foo99
5812     rm -fr $DIR/d99cvsroot
5813 }
5814 run_test 99f "cvs commit ======================================="
5815
5816 test_100() {
5817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5818         [ "$NETTYPE" = tcp ] || \
5819                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5820                         return ; }
5821
5822         remote_ost_nodsh && skip "remote OST with nodsh" && return
5823         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5824         remote_servers || \
5825                 { skip "useless for local single node setup" && return; }
5826
5827         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5828                 [ "$PROT" != "tcp" ] && continue
5829                 RPORT=$(echo $REMOTE | cut -d: -f2)
5830                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5831
5832                 rc=0
5833                 LPORT=`echo $LOCAL | cut -d: -f2`
5834                 if [ $LPORT -ge 1024 ]; then
5835                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5836                         netstat -tna
5837                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5838                 fi
5839         done
5840         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5841 }
5842 run_test 100 "check local port using privileged port ==========="
5843
5844 function get_named_value()
5845 {
5846     local tag
5847
5848     tag=$1
5849     while read ;do
5850         line=$REPLY
5851         case $line in
5852         $tag*)
5853             echo $line | sed "s/^$tag[ ]*//"
5854             break
5855             ;;
5856         esac
5857     done
5858 }
5859
5860 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5861                    awk '/^max_cached_mb/ { print $2 }')
5862
5863 cleanup_101a() {
5864         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5865         trap 0
5866 }
5867
5868 test_101a() {
5869         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5870         local s
5871         local discard
5872         local nreads=10000
5873         local cache_limit=32
5874
5875         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5876         trap cleanup_101a EXIT
5877         $LCTL set_param -n llite.*.read_ahead_stats 0
5878         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5879
5880         #
5881         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5882         #
5883         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5884         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5885
5886         discard=0
5887         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
5888                 get_named_value 'read but discarded' | cut -d" " -f1); do
5889                         discard=$(($discard + $s))
5890         done
5891         cleanup_101a
5892
5893         if [[ $(($discard * 10)) -gt $nreads ]]; then
5894                 $LCTL get_param osc.*-osc*.rpc_stats
5895                 $LCTL get_param llite.*.read_ahead_stats
5896                 error "too many ($discard) discarded pages"
5897         fi
5898         rm -f $DIR/$tfile || true
5899 }
5900 run_test 101a "check read-ahead for random reads ================"
5901
5902 setup_test101bc() {
5903         test_mkdir -p $DIR/$tdir
5904         STRIPE_SIZE=1048576
5905         STRIPE_COUNT=$OSTCOUNT
5906         STRIPE_OFFSET=0
5907
5908         local list=$(comma_list $(osts_nodes))
5909         set_osd_param $list '' read_cache_enable 0
5910         set_osd_param $list '' writethrough_cache_enable 0
5911
5912         trap cleanup_test101bc EXIT
5913         # prepare the read-ahead file
5914         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5915
5916         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5917 }
5918
5919 cleanup_test101bc() {
5920         trap 0
5921         rm -rf $DIR/$tdir
5922         rm -f $DIR/$tfile
5923
5924         local list=$(comma_list $(osts_nodes))
5925         set_osd_param $list '' read_cache_enable 1
5926         set_osd_param $list '' writethrough_cache_enable 1
5927 }
5928
5929 calc_total() {
5930         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5931 }
5932
5933 ra_check_101() {
5934         local READ_SIZE=$1
5935         local STRIPE_SIZE=1048576
5936         local RA_INC=1048576
5937         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5938         local FILE_LENGTH=$((64*100))
5939         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5940                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5941         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
5942                         get_named_value 'read but discarded' |
5943                         cut -d" " -f1 | calc_total)
5944         if [[ $DISCARD -gt $discard_limit ]]; then
5945                 $LCTL get_param llite.*.read_ahead_stats
5946                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5947         else
5948                 echo "Read-ahead success for size ${READ_SIZE}"
5949         fi
5950 }
5951
5952 test_101b() {
5953         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5954         [[ $OSTCOUNT -lt 2 ]] &&
5955                 skip_env "skipping stride IO stride-ahead test" && return
5956         local STRIPE_SIZE=1048576
5957         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5958         local FILE_LENGTH=$((STRIPE_SIZE*100))
5959         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5960         # prepare the read-ahead file
5961         setup_test101bc
5962         cancel_lru_locks osc
5963         for BIDX in 2 4 8 16 32 64 128 256
5964         do
5965                 local BSIZE=$((BIDX*4096))
5966                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5967                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5968                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5969                 $LCTL set_param -n llite.*.read_ahead_stats 0
5970                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5971                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5972                 cancel_lru_locks osc
5973                 ra_check_101 $BSIZE
5974         done
5975         cleanup_test101bc
5976         true
5977 }
5978 run_test 101b "check stride-io mode read-ahead ================="
5979
5980 test_101c() {
5981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5982         local STRIPE_SIZE=1048576
5983         local FILE_LENGTH=$((STRIPE_SIZE*100))
5984         local nreads=10000
5985         local osc_rpc_stats
5986
5987         setup_test101bc
5988
5989         cancel_lru_locks osc
5990         $LCTL set_param osc.*.rpc_stats 0
5991         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5992         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
5993                 local stats=$($LCTL get_param -n $osc_rpc_stats)
5994                 local lines=$(echo "$stats" | awk 'END {print NR;}')
5995                 local size
5996
5997                 if [ $lines -le 20 ]; then
5998                         continue
5999                 fi
6000                 for size in 1 2 4 8; do
6001                         local rpc=$(echo "$stats" |
6002                                     awk '($1 == "'$size':") {print $2; exit; }')
6003                         [ $rpc != 0 ] &&
6004                                 error "Small $((size*4))k read IO $rpc !"
6005                 done
6006                 echo "$osc_rpc_stats check passed!"
6007         done
6008         cleanup_test101bc
6009         true
6010 }
6011 run_test 101c "check stripe_size aligned read-ahead ================="
6012
6013 set_read_ahead() {
6014    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6015    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6016 }
6017
6018 test_101d() {
6019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6020         local file=$DIR/$tfile
6021         local size=${FILESIZE_101c:-500}
6022         local ra_MB=${READAHEAD_MB:-40}
6023
6024         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6025         [ $space -gt $((size * 1024)) ] ||
6026                 { skip "Need free space ${size}M, have ${space}K" && return; }
6027
6028         echo "Creating test file $file of size ${size}M with ${space}K free space"
6029         $SETSTRIPE -c -1 $file || error "setstripe failed"
6030         dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
6031         echo Cancel LRU locks on lustre client to flush the client cache
6032         cancel_lru_locks osc
6033
6034     echo Disable read-ahead
6035     local old_READAHEAD=$(set_read_ahead 0)
6036
6037     echo Reading the test file $file with read-ahead disabled
6038     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
6039
6040     echo Cancel LRU locks on lustre client to flush the client cache
6041     cancel_lru_locks osc
6042     echo Enable read-ahead with ${ra_MB}MB
6043     set_read_ahead $ra_MB
6044
6045     echo Reading the test file $file with read-ahead enabled
6046     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
6047
6048     echo read-ahead disabled time read $time_ra_OFF
6049     echo read-ahead enabled  time read $time_ra_ON
6050
6051         set_read_ahead $old_READAHEAD
6052         rm -f $file
6053         wait_delete_completed
6054
6055     [ $time_ra_ON -lt $time_ra_OFF ] ||
6056         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
6057                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
6058 }
6059 run_test 101d "file read with and without read-ahead enabled  ================="
6060
6061 test_101e() {
6062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6063         local file=$DIR/$tfile
6064         local size_KB=500  #KB
6065         local count=100
6066         local bsize=1024
6067
6068         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6069         local need_KB=$((count * size_KB))
6070         [[ $free_KB -le $need_KB ]] &&
6071                 skip_env "Need free space $need_KB, have $free_KB" && return
6072
6073         echo "Creating $count ${size_KB}K test files"
6074         for ((i = 0; i < $count; i++)); do
6075                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6076         done
6077
6078         echo Cancel LRU locks on lustre client to flush the client cache
6079         cancel_lru_locks osc
6080
6081         echo Reset readahead stats
6082         $LCTL set_param -n llite.*.read_ahead_stats 0
6083
6084         for ((i = 0; i < $count; i++)); do
6085                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6086         done
6087
6088         local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
6089                   get_named_value 'misses' | cut -d" " -f1 | calc_total)
6090
6091         for ((i = 0; i < $count; i++)); do
6092                 rm -rf $file.$i 2>/dev/null
6093         done
6094
6095         #10000 means 20% reads are missing in readahead
6096         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6097 }
6098 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6099
6100 cleanup_test101f() {
6101     trap 0
6102     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6103     rm -rf $DIR/$tfile 2>/dev/null
6104 }
6105
6106 test_101f() {
6107         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6108     local file=$DIR/$tfile
6109     local nreads=1000
6110
6111     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6112     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6113     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6114     trap cleanup_test101f EXIT
6115
6116     echo Cancel LRU locks on lustre client to flush the client cache
6117     cancel_lru_locks osc
6118
6119     echo Reset readahead stats
6120     $LCTL set_param -n llite.*.read_ahead_stats 0
6121     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6122     # readahead should read in 2M file on second read, so only miss
6123     # 2 pages.
6124     echo Random 4K reads on 2M file for 1000 times
6125     $READS -f $file -s 2097152 -b 4096 -n $nreads
6126
6127     echo checking missing pages
6128     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6129           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6130
6131     [ $miss -lt 3 ] || error "misses too much pages!"
6132     cleanup_test101f
6133 }
6134 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6135
6136 setup_test102() {
6137         test_mkdir -p $DIR/$tdir
6138         chown $RUNAS_ID $DIR/$tdir
6139         STRIPE_SIZE=65536
6140         STRIPE_OFFSET=1
6141         STRIPE_COUNT=$OSTCOUNT
6142         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6143
6144         trap cleanup_test102 EXIT
6145         cd $DIR
6146         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6147         cd $DIR/$tdir
6148         for num in 1 2 3 4; do
6149                 for count in $(seq 1 $STRIPE_COUNT); do
6150                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6151                                 local size=`expr $STRIPE_SIZE \* $num`
6152                                 local file=file"$num-$idx-$count"
6153                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6154                         done
6155                 done
6156         done
6157
6158         cd $DIR
6159         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6160 }
6161
6162 cleanup_test102() {
6163         trap 0
6164         rm -f $TMP/f102.tar
6165         rm -rf $DIR/d0.sanity/d102
6166 }
6167
6168 test_102a() {
6169         local testfile=$DIR/xattr_testfile
6170
6171         touch $testfile
6172
6173         [ "$UID" != 0 ] && skip_env "must run as root" && return
6174         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6175                 skip_env "must have user_xattr" && return
6176
6177         [ -z "$(which setfattr 2>/dev/null)" ] &&
6178                 skip_env "could not find setfattr" && return
6179
6180         echo "set/get xattr..."
6181         setfattr -n trusted.name1 -v value1 $testfile ||
6182                 error "setfattr -n trusted.name1=value1 $testfile failed"
6183         getfattr -n trusted.name1 $testfile 2> /dev/null |
6184           grep "trusted.name1=.value1" ||
6185                 error "$testfile missing trusted.name1=value1"
6186
6187         setfattr -n user.author1 -v author1 $testfile ||
6188                 error "setfattr -n user.author1=author1 $testfile failed"
6189         getfattr -n user.author1 $testfile 2> /dev/null |
6190           grep "user.author1=.author1" ||
6191                 error "$testfile missing trusted.author1=author1"
6192
6193         echo "listxattr..."
6194         setfattr -n trusted.name2 -v value2 $testfile ||
6195                 error "$testfile unable to set trusted.name2"
6196         setfattr -n trusted.name3 -v value3 $testfile ||
6197                 error "$testfile unable to set trusted.name3"
6198         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6199             grep "trusted.name" | wc -l) -eq 3 ] ||
6200                 error "$testfile missing 3 trusted.name xattrs"
6201
6202         setfattr -n user.author2 -v author2 $testfile ||
6203                 error "$testfile unable to set user.author2"
6204         setfattr -n user.author3 -v author3 $testfile ||
6205                 error "$testfile unable to set user.author3"
6206         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6207             grep "user.author" | wc -l) -eq 3 ] ||
6208                 error "$testfile missing 3 user.author xattrs"
6209
6210         echo "remove xattr..."
6211         setfattr -x trusted.name1 $testfile ||
6212                 error "$testfile error deleting trusted.name1"
6213         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6214                 error "$testfile did not delete trusted.name1 xattr"
6215
6216         setfattr -x user.author1 $testfile ||
6217                 error "$testfile error deleting user.author1"
6218         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6219                 error "$testfile did not delete trusted.name1 xattr"
6220
6221         # b10667: setting lustre special xattr be silently discarded
6222         echo "set lustre special xattr ..."
6223         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6224                 error "$testfile allowed setting trusted.lov"
6225 }
6226 run_test 102a "user xattr test =================================="
6227
6228 test_102b() {
6229         [ -z "$(which setfattr 2>/dev/null)" ] &&
6230                 skip_env "could not find setfattr" && return
6231
6232         # b10930: get/set/list trusted.lov xattr
6233         echo "get/set/list trusted.lov xattr ..."
6234         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6235         local testfile=$DIR/$tfile
6236         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6237                 error "setstripe failed"
6238         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6239                 error "getstripe failed"
6240         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6241                 error "can't get trusted.lov from $testfile"
6242
6243         local testfile2=${testfile}2
6244         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6245                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6246
6247         $MCREATE $testfile2
6248         setfattr -n trusted.lov -v $value $testfile2
6249         local stripe_size=$($GETSTRIPE -S $testfile2)
6250         local stripe_count=$($GETSTRIPE -c $testfile2)
6251         [[ $stripe_size -eq 65536 ]] ||
6252                 error "stripe size $stripe_size != 65536"
6253         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6254                 error "stripe count $stripe_count != $STRIPECOUNT"
6255         rm -f $DIR/$tfile
6256 }
6257 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6258
6259 test_102c() {
6260         [ -z "$(which setfattr 2>/dev/null)" ] &&
6261                 skip_env "could not find setfattr" && return
6262
6263         # b10930: get/set/list lustre.lov xattr
6264         echo "get/set/list lustre.lov xattr ..."
6265         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6266         test_mkdir -p $DIR/$tdir
6267         chown $RUNAS_ID $DIR/$tdir
6268         local testfile=$DIR/$tdir/$tfile
6269         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6270                 error "setstripe failed"
6271         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6272                 error "getstripe failed"
6273         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6274         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6275
6276         local testfile2=${testfile}2
6277         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6278                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6279
6280         $RUNAS $MCREATE $testfile2
6281         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6282         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6283         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6284         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6285         [ $stripe_count -eq $STRIPECOUNT ] ||
6286                 error "stripe count $stripe_count != $STRIPECOUNT"
6287 }
6288 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6289
6290 compare_stripe_info1() {
6291         local stripe_index_all_zero=true
6292
6293         for num in 1 2 3 4; do
6294                 for count in $(seq 1 $STRIPE_COUNT); do
6295                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6296                                 local size=$((STRIPE_SIZE * num))
6297                                 local file=file"$num-$offset-$count"
6298                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6299                                 [[ $stripe_size -ne $size ]] &&
6300                                     error "$file: size $stripe_size != $size"
6301                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6302                                 # allow fewer stripes to be created, ORI-601
6303                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6304                                     error "$file: count $stripe_count != $count"
6305                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6306                                 [[ $stripe_index -ne 0 ]] &&
6307                                         stripe_index_all_zero=false
6308                         done
6309                 done
6310         done
6311         $stripe_index_all_zero &&
6312                 error "all files are being extracted starting from OST index 0"
6313         return 0
6314 }
6315
6316 find_lustre_tar() {
6317         [ -n "$(which tar 2>/dev/null)" ] &&
6318                 strings $(which tar) | grep -q "lustre" && echo tar
6319 }
6320
6321 test_102d() {
6322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6323         # b10930: tar test for trusted.lov xattr
6324         TAR=$(find_lustre_tar)
6325         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6326         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6327         setup_test102
6328         test_mkdir -p $DIR/d102d
6329         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6330         cd $DIR/d102d/$tdir
6331         compare_stripe_info1
6332 }
6333 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6334
6335 test_102f() {
6336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6337         # b10930: tar test for trusted.lov xattr
6338         TAR=$(find_lustre_tar)
6339         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6340         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6341         setup_test102
6342         test_mkdir -p $DIR/d102f
6343         cd $DIR
6344         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6345         cd $DIR/d102f/$tdir
6346         compare_stripe_info1
6347 }
6348 run_test 102f "tar copy files, not keep osts ==========="
6349
6350 grow_xattr() {
6351         local xsize=${1:-1024}  # in bytes
6352         local file=$DIR/$tfile
6353
6354         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6355                 skip "must have user_xattr" && return 0
6356         [ -z "$(which setfattr 2>/dev/null)" ] &&
6357                 skip_env "could not find setfattr" && return 0
6358         [ -z "$(which getfattr 2>/dev/null)" ] &&
6359                 skip_env "could not find getfattr" && return 0
6360
6361         touch $file
6362
6363         local value="$(generate_string $xsize)"
6364
6365         local xbig=trusted.big
6366         log "save $xbig on $file"
6367         setfattr -n $xbig -v $value $file ||
6368                 error "saving $xbig on $file failed"
6369
6370         local orig=$(get_xattr_value $xbig $file)
6371         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6372
6373         local xsml=trusted.sml
6374         log "save $xsml on $file"
6375         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6376
6377         local new=$(get_xattr_value $xbig $file)
6378         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6379
6380         log "grow $xsml on $file"
6381         setfattr -n $xsml -v "$value" $file ||
6382                 error "growing $xsml on $file failed"
6383
6384         new=$(get_xattr_value $xbig $file)
6385         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6386         log "$xbig still valid after growing $xsml"
6387
6388         rm -f $file
6389 }
6390
6391 test_102h() { # bug 15777
6392         grow_xattr 1024
6393 }
6394 run_test 102h "grow xattr from inside inode to external block"
6395
6396 test_102ha() {
6397         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6398         grow_xattr $(max_xattr_size)
6399 }
6400 run_test 102ha "grow xattr from inside inode to external inode"
6401
6402 test_102i() { # bug 17038
6403         [ -z "$(which getfattr 2>/dev/null)" ] &&
6404                 skip "could not find getfattr" && return
6405         touch $DIR/$tfile
6406         ln -s $DIR/$tfile $DIR/${tfile}link
6407         getfattr -n trusted.lov $DIR/$tfile ||
6408                 error "lgetxattr on $DIR/$tfile failed"
6409         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6410                 grep -i "no such attr" ||
6411                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6412         rm -f $DIR/$tfile $DIR/${tfile}link
6413 }
6414 run_test 102i "lgetxattr test on symbolic link ============"
6415
6416 test_102j() {
6417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6418         TAR=$(find_lustre_tar)
6419         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6420         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6421         setup_test102 "$RUNAS"
6422         test_mkdir -p $DIR/d102j
6423         chown $RUNAS_ID $DIR/d102j
6424         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6425         cd $DIR/d102j/$tdir
6426         compare_stripe_info1 "$RUNAS"
6427 }
6428 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6429
6430 test_102k() {
6431         [ -z "$(which setfattr 2>/dev/null)" ] &&
6432                 skip "could not find setfattr" && return
6433         touch $DIR/$tfile
6434         # b22187 just check that does not crash for regular file.
6435         setfattr -n trusted.lov $DIR/$tfile
6436         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6437         local test_kdir=$DIR/d102k
6438         test_mkdir $test_kdir
6439         local default_size=`$GETSTRIPE -S $test_kdir`
6440         local default_count=`$GETSTRIPE -c $test_kdir`
6441         local default_offset=`$GETSTRIPE -i $test_kdir`
6442         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6443                 error 'dir setstripe failed'
6444         setfattr -n trusted.lov $test_kdir
6445         local stripe_size=`$GETSTRIPE -S $test_kdir`
6446         local stripe_count=`$GETSTRIPE -c $test_kdir`
6447         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6448         [ $stripe_size -eq $default_size ] ||
6449                 error "stripe size $stripe_size != $default_size"
6450         [ $stripe_count -eq $default_count ] ||
6451                 error "stripe count $stripe_count != $default_count"
6452         [ $stripe_offset -eq $default_offset ] ||
6453                 error "stripe offset $stripe_offset != $default_offset"
6454         rm -rf $DIR/$tfile $test_kdir
6455 }
6456 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6457
6458 test_102l() {
6459         [ -z "$(which getfattr 2>/dev/null)" ] &&
6460                 skip "could not find getfattr" && return
6461
6462         # LU-532 trusted. xattr is invisible to non-root
6463         local testfile=$DIR/$tfile
6464
6465         touch $testfile
6466
6467         echo "listxattr as user..."
6468         chown $RUNAS_ID $testfile
6469         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6470             grep -q "trusted" &&
6471                 error "$testfile trusted xattrs are user visible"
6472
6473         return 0;
6474 }
6475 run_test 102l "listxattr size test =================================="
6476
6477 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6478         local path=$DIR/$tfile
6479         touch $path
6480
6481         listxattr_size_check $path || error "listattr_size_check $path failed"
6482 }
6483 run_test 102m "Ensure listxattr fails on small bufffer ========"
6484
6485 cleanup_test102
6486
6487 getxattr() { # getxattr path name
6488         # Return the base64 encoding of the value of xattr name on path.
6489         local path=$1
6490         local name=$2
6491
6492         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6493         # file: $path
6494         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6495         #
6496         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6497
6498         getfattr --absolute-names --encoding=base64 --name=$name $path |
6499                 awk -F= -v name=$name '$1 == name {
6500                         print substr($0, index($0, "=") + 1);
6501         }'
6502 }
6503
6504 test_102n() { # LU-4101 mdt: protect internal xattrs
6505         local file0=$DIR/$tfile.0
6506         local file1=$DIR/$tfile.1
6507         local xattr0=$TMP/$tfile.0
6508         local xattr1=$TMP/$tfile.1
6509         local name
6510         local value
6511
6512         [ -z "$(which setfattr 2>/dev/null)" ] &&
6513                 skip "could not find setfattr" && return
6514
6515         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6516         then
6517                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6518                 return
6519         fi
6520
6521         rm -rf $file0 $file1 $xattr0 $xattr1
6522         touch $file0 $file1
6523
6524         # Get 'before' xattrs of $file1.
6525         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6526
6527         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6528                 # Try to copy xattr from $file0 to $file1.
6529                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6530
6531                 setfattr --name=trusted.$name --value="$value" $file1 ||
6532                         error "setxattr 'trusted.$name' failed"
6533
6534                 # Try to set a garbage xattr.
6535                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6536
6537                 setfattr --name=trusted.$name --value="$value" $file1 ||
6538                         error "setxattr 'trusted.$name' failed"
6539
6540                 # Try to remove the xattr from $file1. We don't care if this
6541                 # appears to succeed or fail, we just don't want there to be
6542                 # any changes or crashes.
6543                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6544         done
6545
6546         # Get 'after' xattrs of file1.
6547         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6548
6549         if ! diff $xattr0 $xattr1; then
6550                 error "before and after xattrs of '$file1' differ"
6551         fi
6552
6553         rm -rf $file0 $file1 $xattr0 $xattr1
6554
6555         return 0
6556 }
6557 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6558
6559 run_acl_subtest()
6560 {
6561     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6562     return $?
6563 }
6564
6565 test_103 () {
6566         [ "$UID" != 0 ] && skip_env "must run as root" && return
6567         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6568                 skip "must have acl enabled" && return
6569         [ -z "$(which setfacl 2>/dev/null)" ] &&
6570                 skip_env "could not find setfacl" && return
6571         $GSS && skip "could not run under gss" && return
6572
6573         declare -a identity_old
6574
6575         for num in $(seq $MDSCOUNT); do
6576                 switch_identity $num true || identity_old[$num]=$?
6577         done
6578
6579         SAVE_UMASK=$(umask)
6580         umask 0022
6581         cd $DIR
6582
6583         echo "performing cp ..."
6584         run_acl_subtest cp || error "run_acl_subtest cp failed"
6585         echo "performing getfacl-noacl..."
6586         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6587         echo "performing misc..."
6588         run_acl_subtest misc || error  "misc test failed"
6589         echo "performing permissions..."
6590         run_acl_subtest permissions || error "permissions failed"
6591         echo "performing setfacl..."
6592         run_acl_subtest setfacl || error  "setfacl test failed"
6593
6594         # inheritance test got from HP
6595         echo "performing inheritance..."
6596         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6597         chmod +x make-tree || error "chmod +x failed"
6598         run_acl_subtest inheritance || error "inheritance test failed"
6599         rm -f make-tree
6600
6601         echo "LU-974 ignore umask when acl is enabled..."
6602         run_acl_subtest 974 || error "LU-974 umask test failed"
6603         if [ $MDSCOUNT -ge 2 ]; then
6604                 run_acl_subtest 974_remote ||
6605                         error "LU-974 umask test failed under remote dir"
6606         fi
6607
6608         echo "LU-2561 newly created file is same size as directory..."
6609         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6610                 run_acl_subtest 2561 || error "LU-2561 test failed"
6611         else
6612                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6613         fi
6614
6615         run_acl_subtest 4924 || error "LU-4924 test failed"
6616
6617         cd $SAVE_PWD
6618         umask $SAVE_UMASK
6619
6620         for num in $(seq $MDSCOUNT); do
6621                 if [ "${identity_old[$num]}" = 1 ]; then
6622                         switch_identity $num false || identity_old[$num]=$?
6623                 fi
6624         done
6625 }
6626 run_test 103 "acl test ========================================="
6627
6628 test_104a() {
6629         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6630         touch $DIR/$tfile
6631         lfs df || error "lfs df failed"
6632         lfs df -ih || error "lfs df -ih failed"
6633         lfs df -h $DIR || error "lfs df -h $DIR failed"
6634         lfs df -i $DIR || error "lfs df -i $DIR failed"
6635         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6636         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6637
6638         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6639         lctl --device %$OSC deactivate
6640         lfs df || error "lfs df with deactivated OSC failed"
6641         lctl --device %$OSC activate
6642         # wait the osc back to normal
6643         wait_osc_import_state client ost FULL
6644
6645         lfs df || error "lfs df with reactivated OSC failed"
6646         rm -f $DIR/$tfile
6647 }
6648 run_test 104a "lfs df [-ih] [path] test ========================="
6649
6650 test_104b() {
6651         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6652         [ $RUNAS_ID -eq $UID ] &&
6653                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6654         chmod 666 /dev/obd
6655         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6656                         grep "Permission denied" | wc -l)))
6657         if [ $denied_cnt -ne 0 ]; then
6658                 error "lfs check servers test failed"
6659         fi
6660 }
6661 run_test 104b "$RUNAS lfs check servers test ===================="
6662
6663 test_105a() {
6664         # doesn't work on 2.4 kernels
6665         touch $DIR/$tfile
6666         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6667                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6668         else
6669                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6670         fi
6671         rm -f $DIR/$tfile
6672 }
6673 run_test 105a "flock when mounted without -o flock test ========"
6674
6675 test_105b() {
6676         touch $DIR/$tfile
6677         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6678                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6679         else
6680                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6681         fi
6682         rm -f $DIR/$tfile
6683 }
6684 run_test 105b "fcntl when mounted without -o flock test ========"
6685
6686 test_105c() {
6687         touch $DIR/$tfile
6688         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6689                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6690         else
6691                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6692         fi
6693         rm -f $DIR/$tfile
6694 }
6695 run_test 105c "lockf when mounted without -o flock test ========"
6696
6697 test_105d() { # bug 15924
6698         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6699         test_mkdir -p $DIR/$tdir
6700         [ -z "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ] &&
6701                 skip "mount w/o flock enabled" && return
6702         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6703         $LCTL set_param fail_loc=0x80000315
6704         flocks_test 2 $DIR/$tdir
6705 }
6706 run_test 105d "flock race (should not freeze) ========"
6707
6708 test_105e() { # bug 22660 && 22040
6709         [ -z "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ] &&
6710                 skip "mount w/o flock enabled" && return
6711         touch $DIR/$tfile
6712         flocks_test 3 $DIR/$tfile
6713 }
6714 run_test 105e "Two conflicting flocks from same process ======="
6715
6716 test_106() { #bug 10921
6717         test_mkdir -p $DIR/$tdir
6718         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6719         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6720 }
6721 run_test 106 "attempt exec of dir followed by chown of that dir"
6722
6723 test_107() {
6724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6725         CDIR=`pwd`
6726         cd $DIR
6727
6728         local file=core
6729         rm -f $file
6730
6731         local save_pattern=$(sysctl -n kernel.core_pattern)
6732         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6733         sysctl -w kernel.core_pattern=$file
6734         sysctl -w kernel.core_uses_pid=0
6735
6736         ulimit -c unlimited
6737         sleep 60 &
6738         SLEEPPID=$!
6739
6740         sleep 1
6741
6742         kill -s 11 $SLEEPPID
6743         wait $SLEEPPID
6744         if [ -e $file ]; then
6745                 size=`stat -c%s $file`
6746                 [ $size -eq 0 ] && error "Fail to create core file $file"
6747         else
6748                 error "Fail to create core file $file"
6749         fi
6750         rm -f $file
6751         sysctl -w kernel.core_pattern=$save_pattern
6752         sysctl -w kernel.core_uses_pid=$save_uses_pid
6753         cd $CDIR
6754 }
6755 run_test 107 "Coredump on SIG"
6756
6757 test_110() {
6758         test_mkdir -p $DIR/$tdir
6759         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6760                 error "mkdir with 255 char failed"
6761         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6762                 error "mkdir with 256 char should fail, but did not"
6763         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6764                 error "create with 255 char failed"
6765         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6766                 error "create with 256 char should fail, but did not"
6767
6768         ls -l $DIR/$tdir
6769         rm -rf $DIR/$tdir
6770 }
6771 run_test 110 "filename length checking"
6772
6773 test_115() {
6774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6775         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
6776                 tail -1 | cut -c11-20)
6777         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
6778         echo "Starting with $OSTIO_pre threads"
6779
6780         NUMTEST=20000
6781         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
6782         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
6783         echo "$NUMTEST creates/unlinks"
6784         test_mkdir -p $DIR/$tdir
6785         createmany -o $DIR/$tdir/$tfile $NUMTEST
6786         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6787
6788         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
6789                 tail -1 | cut -c11-20)
6790
6791         # don't return an error
6792         [ $OSTIO_post == $OSTIO_pre ] && echo \
6793             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6794             echo "This may be fine, depending on what ran before this test" &&
6795             echo "and how fast this system is." && return
6796
6797         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6798 }
6799 run_test 115 "verify dynamic thread creation===================="
6800
6801 free_min_max () {
6802         wait_delete_completed
6803         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6804         echo OST kbytes available: ${AVAIL[@]}
6805         MAXI=0; MAXV=${AVAIL[0]}
6806         MINI=0; MINV=${AVAIL[0]}
6807         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6808                 #echo OST $i: ${AVAIL[i]}kb
6809                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
6810                         MAXV=${AVAIL[i]}; MAXI=$i
6811                 fi
6812                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
6813                         MINV=${AVAIL[i]}; MINI=$i
6814                 fi
6815         done
6816         echo Min free space: OST $MINI: $MINV
6817         echo Max free space: OST $MAXI: $MAXV
6818 }
6819
6820 test_116a() { # was previously test_116()
6821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6822         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
6823
6824         echo -n "Free space priority "
6825         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
6826                 head -n1
6827         declare -a AVAIL
6828         free_min_max
6829
6830         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
6831         trap simple_cleanup_common EXIT
6832
6833         # Check if we need to generate uneven OSTs
6834         test_mkdir -p $DIR/$tdir/OST${MINI}
6835         local FILL=$(($MINV / 4))
6836         local DIFF=$(($MAXV - $MINV))
6837         local DIFF2=$(($DIFF * 100 / $MINV))
6838
6839         local threshold=$(do_facet $SINGLEMDS \
6840                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
6841         threshold=${threshold%%%}
6842         echo -n "Check for uneven OSTs: "
6843         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
6844
6845         if [[ $DIFF2 -gt $threshold ]]; then
6846                 echo "ok"
6847                 echo "Don't need to fill OST$MINI"
6848         else
6849                 # generate uneven OSTs. Write 2% over the QOS threshold value
6850                 echo "no"
6851                 DIFF=$(($threshold - $DIFF2 + 2))
6852                 DIFF2=$(( ($MINV * $DIFF)/100 ))
6853                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
6854                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
6855                         error "setstripe failed"
6856                 DIFF=$(($DIFF2 / 2048))
6857                 i=0
6858                 while [ $i -lt $DIFF ]; do
6859                         i=$(($i + 1))
6860                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
6861                                 bs=2M count=1 2>/dev/null
6862                         echo -n .
6863                 done
6864                 echo .
6865                 sync
6866                 sleep_maxage
6867                 free_min_max
6868         fi
6869
6870         DIFF=$(($MAXV - $MINV))
6871         DIFF2=$(($DIFF * 100 / $MINV))
6872         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
6873         if [[ $DIFF2 -gt $threshold ]]; then
6874                 echo "ok"
6875         else
6876                 echo "failed - QOS mode won't be used"
6877                 error_ignore 0000 "QOS imbalance criteria not met"
6878                 simple_cleanup_common
6879                 return
6880         fi
6881
6882         MINI1=$MINI; MINV1=$MINV
6883         MAXI1=$MAXI; MAXV1=$MAXV
6884
6885         # now fill using QOS
6886         $SETSTRIPE -c 1 $DIR/$tdir
6887         FILL=$(($FILL / 200))
6888         if [ $FILL -gt 600 ]; then
6889                 FILL=600
6890         fi
6891         echo "writing $FILL files to QOS-assigned OSTs"
6892         i=0
6893         while [ $i -lt $FILL ]; do
6894                 i=$((i + 1))
6895                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
6896                         count=1 2>/dev/null
6897                 echo -n .
6898         done
6899         echo "wrote $i 200k files"
6900         sync
6901         sleep_maxage
6902
6903         echo "Note: free space may not be updated, so measurements might be off"
6904         free_min_max
6905         DIFF2=$(($MAXV - $MINV))
6906         echo "free space delta: orig $DIFF final $DIFF2"
6907         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6908         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6909         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
6910         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6911         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
6912         FILL=$(($DIFF2 * 100 / $DIFF - 100))
6913         [ $DIFF -gt 0 ] &&
6914                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
6915
6916         # Figure out which files were written where
6917         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6918                   awk '/'$MINI1': / {print $2; exit}')
6919         echo $UUID
6920         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
6921         echo "$MINC files created on smaller OST $MINI1"
6922         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6923                   awk '/'$MAXI1': / {print $2; exit}')
6924         echo $UUID
6925         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
6926         echo "$MAXC files created on larger OST $MAXI1"
6927         FILL=$(($MAXC * 100 / $MINC - 100))
6928         [[ $MINC -gt 0 ]] &&
6929                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
6930         [[ $MAXC -gt $MINC ]] ||
6931                 error_ignore LU-9 "stripe QOS didn't balance free space"
6932         simple_cleanup_common
6933 }
6934 run_test 116a "stripe QOS: free space balance ==================="
6935
6936 test_116b() { # LU-2093
6937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6938         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
6939                 head -1 2>/dev/null)" ] && skip "no QOS" && return
6940 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6941         local old_rr
6942         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
6943                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
6944         do_facet $SINGLEMDS lctl set_param \
6945                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
6946         mkdir -p $DIR/$tdir
6947         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6948         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6949         do_facet $SINGLEMDS lctl set_param fail_loc=0
6950         rm -rf $DIR/$tdir
6951         do_facet $SINGLEMDS lctl set_param \
6952                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
6953 }
6954 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6955
6956 test_117() # bug 10891
6957 {
6958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6959         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6960         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6961         lctl set_param fail_loc=0x21e
6962         > $DIR/$tfile || error "truncate failed"
6963         lctl set_param fail_loc=0
6964         echo "Truncate succeeded."
6965         rm -f $DIR/$tfile
6966 }
6967 run_test 117 "verify fsfilt_extend =========="
6968
6969 NO_SLOW_RESENDCOUNT=4
6970 export OLD_RESENDCOUNT=""
6971 set_resend_count () {
6972         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6973         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
6974         lctl set_param -n $PROC_RESENDCOUNT $1
6975         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6976 }
6977
6978 # for reduce test_118* time (b=14842)
6979 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6980
6981 # Reset async IO behavior after error case
6982 reset_async() {
6983         FILE=$DIR/reset_async
6984
6985         # Ensure all OSCs are cleared
6986         $SETSTRIPE -c -1 $FILE
6987         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6988         sync
6989         rm $FILE
6990 }
6991
6992 test_118a() #bug 11710
6993 {
6994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6995         reset_async
6996
6997         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6998         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6999         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7000
7001         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7002                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7003                 return 1;
7004         fi
7005         rm -f $DIR/$tfile
7006 }
7007 run_test 118a "verify O_SYNC works =========="
7008
7009 test_118b()
7010 {
7011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7012         remote_ost_nodsh && skip "remote OST with nodsh" && return
7013
7014         reset_async
7015
7016         #define OBD_FAIL_OST_ENOENT 0x217
7017         set_nodes_failloc "$(osts_nodes)" 0x217
7018         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7019         RC=$?
7020         set_nodes_failloc "$(osts_nodes)" 0
7021         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7022         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7023                     grep -c writeback)
7024
7025         if [[ $RC -eq 0 ]]; then
7026                 error "Must return error due to dropped pages, rc=$RC"
7027                 return 1;
7028         fi
7029
7030         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7031                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7032                 return 1;
7033         fi
7034
7035         echo "Dirty pages not leaked on ENOENT"
7036
7037         # Due to the above error the OSC will issue all RPCs syncronously
7038         # until a subsequent RPC completes successfully without error.
7039         $MULTIOP $DIR/$tfile Ow4096yc
7040         rm -f $DIR/$tfile
7041
7042         return 0
7043 }
7044 run_test 118b "Reclaim dirty pages on fatal error =========="
7045
7046 test_118c()
7047 {
7048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7049
7050         # for 118c, restore the original resend count, LU-1940
7051         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7052                                 set_resend_count $OLD_RESENDCOUNT
7053         remote_ost_nodsh && skip "remote OST with nodsh" && return
7054
7055         reset_async
7056
7057         #define OBD_FAIL_OST_EROFS               0x216
7058         set_nodes_failloc "$(osts_nodes)" 0x216
7059
7060         # multiop should block due to fsync until pages are written
7061         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7062         MULTIPID=$!
7063         sleep 1
7064
7065         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7066                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7067         fi
7068
7069         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7070                     grep -c writeback)
7071         if [[ $WRITEBACK -eq 0 ]]; then
7072                 error "No page in writeback, writeback=$WRITEBACK"
7073         fi
7074
7075         set_nodes_failloc "$(osts_nodes)" 0
7076         wait $MULTIPID
7077         RC=$?
7078         if [[ $RC -ne 0 ]]; then
7079                 error "Multiop fsync failed, rc=$RC"
7080         fi
7081
7082         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7083         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7084                     grep -c writeback)
7085         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7086                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7087         fi
7088
7089         rm -f $DIR/$tfile
7090         echo "Dirty pages flushed via fsync on EROFS"
7091         return 0
7092 }
7093 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7094
7095 # continue to use small resend count to reduce test_118* time (b=14842)
7096 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7097
7098 test_118d()
7099 {
7100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7101         remote_ost_nodsh && skip "remote OST with nodsh" && return
7102
7103         reset_async
7104
7105         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7106         set_nodes_failloc "$(osts_nodes)" 0x214
7107         # multiop should block due to fsync until pages are written
7108         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7109         MULTIPID=$!
7110         sleep 1
7111
7112         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7113                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7114         fi
7115
7116         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7117                     grep -c writeback)
7118         if [[ $WRITEBACK -eq 0 ]]; then
7119                 error "No page in writeback, writeback=$WRITEBACK"
7120         fi
7121
7122         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7123         set_nodes_failloc "$(osts_nodes)" 0
7124
7125         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7126         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7127                     grep -c writeback)
7128         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7129                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7130         fi
7131
7132         rm -f $DIR/$tfile
7133         echo "Dirty pages gaurenteed flushed via fsync"
7134         return 0
7135 }
7136 run_test 118d "Fsync validation inject a delay of the bulk =========="
7137
7138 test_118f() {
7139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7140         reset_async
7141
7142         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7143         lctl set_param fail_loc=0x8000040a
7144
7145         # Should simulate EINVAL error which is fatal
7146         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7147         RC=$?
7148         if [[ $RC -eq 0 ]]; then
7149                 error "Must return error due to dropped pages, rc=$RC"
7150         fi
7151
7152         lctl set_param fail_loc=0x0
7153
7154         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7155         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7156         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7157                     grep -c writeback)
7158         if [[ $LOCKED -ne 0 ]]; then
7159                 error "Locked pages remain in cache, locked=$LOCKED"
7160         fi
7161
7162         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7163                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7164         fi
7165
7166         rm -f $DIR/$tfile
7167         echo "No pages locked after fsync"
7168
7169         reset_async
7170         return 0
7171 }
7172 run_test 118f "Simulate unrecoverable OSC side error =========="
7173
7174 test_118g() {
7175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7176         reset_async
7177
7178         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7179         lctl set_param fail_loc=0x406
7180
7181         # simulate local -ENOMEM
7182         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7183         RC=$?
7184
7185         lctl set_param fail_loc=0
7186         if [[ $RC -eq 0 ]]; then
7187                 error "Must return error due to dropped pages, rc=$RC"
7188         fi
7189
7190         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7191         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7192         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7193                         grep -c writeback)
7194         if [[ $LOCKED -ne 0 ]]; then
7195                 error "Locked pages remain in cache, locked=$LOCKED"
7196         fi
7197
7198         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7199                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7200         fi
7201
7202         rm -f $DIR/$tfile
7203         echo "No pages locked after fsync"
7204
7205         reset_async
7206         return 0
7207 }
7208 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7209
7210 test_118h() {
7211         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7212         remote_ost_nodsh && skip "remote OST with nodsh" && return
7213
7214         reset_async
7215
7216         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7217         set_nodes_failloc "$(osts_nodes)" 0x20e
7218         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7219         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7220         RC=$?
7221
7222         set_nodes_failloc "$(osts_nodes)" 0
7223         if [[ $RC -eq 0 ]]; then
7224                 error "Must return error due to dropped pages, rc=$RC"
7225         fi
7226
7227         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7228         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7229         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7230                     grep -c writeback)
7231         if [[ $LOCKED -ne 0 ]]; then
7232                 error "Locked pages remain in cache, locked=$LOCKED"
7233         fi
7234
7235         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7236                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7237         fi
7238
7239         rm -f $DIR/$tfile
7240         echo "No pages locked after fsync"
7241
7242         return 0
7243 }
7244 run_test 118h "Verify timeout in handling recoverables errors  =========="
7245
7246 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7247
7248 test_118i() {
7249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7250         remote_ost_nodsh && skip "remote OST with nodsh" && return
7251
7252         reset_async
7253
7254         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7255         set_nodes_failloc "$(osts_nodes)" 0x20e
7256
7257         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7258         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7259         PID=$!
7260         sleep 5
7261         set_nodes_failloc "$(osts_nodes)" 0
7262
7263         wait $PID
7264         RC=$?
7265         if [[ $RC -ne 0 ]]; then
7266                 error "got error, but should be not, rc=$RC"
7267         fi
7268
7269         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7270         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7271         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7272         if [[ $LOCKED -ne 0 ]]; then
7273                 error "Locked pages remain in cache, locked=$LOCKED"
7274         fi
7275
7276         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7277                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7278         fi
7279
7280         rm -f $DIR/$tfile
7281         echo "No pages locked after fsync"
7282
7283         return 0
7284 }
7285 run_test 118i "Fix error before timeout in recoverable error  =========="
7286
7287 [ "$SLOW" = "no" ] && set_resend_count 4
7288
7289 test_118j() {
7290         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7291         remote_ost_nodsh && skip "remote OST with nodsh" && return
7292
7293         reset_async
7294
7295         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7296         set_nodes_failloc "$(osts_nodes)" 0x220
7297
7298         # return -EIO from OST
7299         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7300         RC=$?
7301         set_nodes_failloc "$(osts_nodes)" 0x0
7302         if [[ $RC -eq 0 ]]; then
7303                 error "Must return error due to dropped pages, rc=$RC"
7304         fi
7305
7306         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7307         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7308         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7309         if [[ $LOCKED -ne 0 ]]; then
7310                 error "Locked pages remain in cache, locked=$LOCKED"
7311         fi
7312
7313         # in recoverable error on OST we want resend and stay until it finished
7314         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7315                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7316         fi
7317
7318         rm -f $DIR/$tfile
7319         echo "No pages locked after fsync"
7320
7321         return 0
7322 }
7323 run_test 118j "Simulate unrecoverable OST side error =========="
7324
7325 test_118k()
7326 {
7327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7328         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7329
7330         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7331         set_nodes_failloc "$(osts_nodes)" 0x20e
7332         test_mkdir -p $DIR/$tdir
7333
7334         for ((i=0;i<10;i++)); do
7335                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7336                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7337                 SLEEPPID=$!
7338                 sleep 0.500s
7339                 kill $SLEEPPID
7340                 wait $SLEEPPID
7341         done
7342
7343         set_nodes_failloc "$(osts_nodes)" 0
7344         rm -rf $DIR/$tdir
7345 }
7346 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7347
7348 test_118l()
7349 {
7350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7351         # LU-646
7352         test_mkdir -p $DIR/$tdir
7353         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7354         rm -rf $DIR/$tdir
7355 }
7356 run_test 118l "fsync dir ========="
7357
7358 test_118m() # LU-3066
7359 {
7360         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7361         test_mkdir -p $DIR/$tdir
7362         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7363         rm -rf $DIR/$tdir
7364 }
7365 run_test 118m "fdatasync dir ========="
7366
7367 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7368
7369 test_119a() # bug 11737
7370 {
7371         BSIZE=$((512 * 1024))
7372         directio write $DIR/$tfile 0 1 $BSIZE
7373         # We ask to read two blocks, which is more than a file size.
7374         # directio will indicate an error when requested and actual
7375         # sizes aren't equeal (a normal situation in this case) and
7376         # print actual read amount.
7377         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7378         if [ "$NOB" != "$BSIZE" ]; then
7379                 error "read $NOB bytes instead of $BSIZE"
7380         fi
7381         rm -f $DIR/$tfile
7382 }
7383 run_test 119a "Short directIO read must return actual read amount"
7384
7385 test_119b() # bug 11737
7386 {
7387         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7388
7389         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7390         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7391         sync
7392         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7393                 error "direct read failed"
7394         rm -f $DIR/$tfile
7395 }
7396 run_test 119b "Sparse directIO read must return actual read amount"
7397
7398 test_119c() # bug 13099
7399 {
7400         BSIZE=1048576
7401         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7402         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7403         rm -f $DIR/$tfile
7404 }
7405 run_test 119c "Testing for direct read hitting hole"
7406
7407 test_119d() # bug 15950
7408 {
7409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7410         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7411         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7412         BSIZE=1048576
7413         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7414         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7415         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7416         lctl set_param fail_loc=0x40d
7417         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7418         pid_dio=$!
7419         sleep 1
7420         cat $DIR/$tfile > /dev/null &
7421         lctl set_param fail_loc=0
7422         pid_reads=$!
7423         wait $pid_dio
7424         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7425         sleep 2
7426         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7427         error "the read rpcs have not completed in 2s"
7428         rm -f $DIR/$tfile
7429         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7430 }
7431 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7432
7433 test_120a() {
7434         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7435         test_mkdir -p $DIR/$tdir
7436         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7437                skip "no early lock cancel on server" && return 0
7438
7439         lru_resize_disable mdc
7440         lru_resize_disable osc
7441         cancel_lru_locks mdc
7442         # asynchronous object destroy at MDT could cause bl ast to client
7443         cancel_lru_locks osc
7444
7445         stat $DIR/$tdir > /dev/null
7446         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7447         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7448         test_mkdir $DIR/$tdir/d1
7449         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7450         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7451         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7452         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7453         lru_resize_enable mdc
7454         lru_resize_enable osc
7455 }
7456 run_test 120a "Early Lock Cancel: mkdir test"
7457
7458 test_120b() {
7459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7460         test_mkdir -p $DIR/$tdir
7461         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7462                skip "no early lock cancel on server" && return 0
7463         lru_resize_disable mdc
7464         lru_resize_disable osc
7465         cancel_lru_locks mdc
7466         stat $DIR/$tdir > /dev/null
7467         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7468         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7469         touch $DIR/$tdir/f1
7470         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7471         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7472         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7473         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7474         lru_resize_enable mdc
7475         lru_resize_enable osc
7476 }
7477 run_test 120b "Early Lock Cancel: create test"
7478
7479 test_120c() {
7480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7481         test_mkdir -p $DIR/$tdir
7482         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7483                skip "no early lock cancel on server" && return 0
7484         lru_resize_disable mdc
7485         lru_resize_disable osc
7486         test_mkdir -p $DIR/$tdir/d1
7487         test_mkdir -p $DIR/$tdir/d2
7488         touch $DIR/$tdir/d1/f1
7489         cancel_lru_locks mdc
7490         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7491         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7492         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7493         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7494         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7495         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7496         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7497         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7498         lru_resize_enable mdc
7499         lru_resize_enable osc
7500 }
7501 run_test 120c "Early Lock Cancel: link test"
7502
7503 test_120d() {
7504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7505         test_mkdir -p $DIR/$tdir
7506         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7507                skip "no early lock cancel on server" && return 0
7508         lru_resize_disable mdc
7509         lru_resize_disable osc
7510         touch $DIR/$tdir
7511         cancel_lru_locks mdc
7512         stat $DIR/$tdir > /dev/null
7513         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7514         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7515         chmod a+x $DIR/$tdir
7516         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7517         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7518         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7519         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7520         lru_resize_enable mdc
7521         lru_resize_enable osc
7522 }
7523 run_test 120d "Early Lock Cancel: setattr test"
7524
7525 test_120e() {
7526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7527         test_mkdir -p $DIR/$tdir
7528         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7529                skip "no early lock cancel on server" && return 0
7530         lru_resize_disable mdc
7531         lru_resize_disable osc
7532         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7533         cancel_lru_locks mdc
7534         cancel_lru_locks osc
7535         dd if=$DIR/$tdir/f1 of=/dev/null
7536         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7537         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7538               awk '/ldlm_cancel/ {print $2}'`
7539         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7540               awk '/ldlm_bl_callback/ {print $2}'`
7541         unlink $DIR/$tdir/f1
7542         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7543               awk '/ldlm_cancel/ {print $2}'`
7544         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7545               awk '/ldlm_bl_callback/ {print $2}'`
7546         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7547         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7548         lru_resize_enable mdc
7549         lru_resize_enable osc
7550 }
7551 run_test 120e "Early Lock Cancel: unlink test"
7552
7553 test_120f() {
7554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7555         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7556                skip "no early lock cancel on server" && return 0
7557         test_mkdir -p $DIR/$tdir
7558         lru_resize_disable mdc
7559         lru_resize_disable osc
7560         test_mkdir -p $DIR/$tdir/d1
7561         test_mkdir -p $DIR/$tdir/d2
7562         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7563         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7564         cancel_lru_locks mdc
7565         cancel_lru_locks osc
7566         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7567         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7568         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7569         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7570               awk '/ldlm_cancel/ {print $2}'`
7571         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7572               awk '/ldlm_bl_callback/ {print $2}'`
7573         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7574         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7575               awk '/ldlm_cancel/ {print $2}'`
7576         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7577               awk '/ldlm_bl_callback/ {print $2}'`
7578         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7579         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7580         lru_resize_enable mdc
7581         lru_resize_enable osc
7582 }
7583 run_test 120f "Early Lock Cancel: rename test"
7584
7585 test_120g() {
7586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7587         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7588                skip "no early lock cancel on server" && return 0
7589         lru_resize_disable mdc
7590         lru_resize_disable osc
7591         count=10000
7592         echo create $count files
7593         test_mkdir -p $DIR/$tdir
7594         cancel_lru_locks mdc
7595         cancel_lru_locks osc
7596         t0=`date +%s`
7597
7598         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7599               awk '/ldlm_cancel/ {print $2}'`
7600         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7601               awk '/ldlm_bl_callback/ {print $2}'`
7602         createmany -o $DIR/$tdir/f $count
7603         sync
7604         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7605               awk '/ldlm_cancel/ {print $2}'`
7606         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7607               awk '/ldlm_bl_callback/ {print $2}'`
7608         t1=`date +%s`
7609         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7610         echo rm $count files
7611         rm -r $DIR/$tdir
7612         sync
7613         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7614               awk '/ldlm_cancel/ {print $2}'`
7615         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7616               awk '/ldlm_bl_callback/ {print $2}'`
7617         t2=`date +%s`
7618         echo total: $count removes in $((t2-t1))
7619         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7620         sleep 2
7621         # wait for commitment of removal
7622         lru_resize_enable mdc
7623         lru_resize_enable osc
7624 }
7625 run_test 120g "Early Lock Cancel: performance test"
7626
7627 test_121() { #bug #10589
7628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7629         rm -rf $DIR/$tfile
7630         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7631 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7632         lctl set_param fail_loc=0x310
7633         cancel_lru_locks osc > /dev/null
7634         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7635         lctl set_param fail_loc=0
7636         [[ $reads -eq $writes ]] ||
7637                 error "read $reads blocks, must be $writes blocks"
7638 }
7639 run_test 121 "read cancel race ========="
7640
7641 test_123a() { # was test 123, statahead(bug 11401)
7642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7643         SLOWOK=0
7644         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7645             log "testing on UP system. Performance may be not as good as expected."
7646                         SLOWOK=1
7647         fi
7648
7649         rm -rf $DIR/$tdir
7650         test_mkdir -p $DIR/$tdir
7651         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7652         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7653         MULT=10
7654         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7655                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7656
7657                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7658                 lctl set_param -n llite.*.statahead_max 0
7659                 lctl get_param llite.*.statahead_max
7660                 cancel_lru_locks mdc
7661                 cancel_lru_locks osc
7662                 stime=`date +%s`
7663                 time ls -l $DIR/$tdir | wc -l
7664                 etime=`date +%s`
7665                 delta=$((etime - stime))
7666                 log "ls $i files without statahead: $delta sec"
7667                 lctl set_param llite.*.statahead_max=$max
7668
7669                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7670                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7671                 cancel_lru_locks mdc
7672                 cancel_lru_locks osc
7673                 stime=`date +%s`
7674                 time ls -l $DIR/$tdir | wc -l
7675                 etime=`date +%s`
7676                 delta_sa=$((etime - stime))
7677                 log "ls $i files with statahead: $delta_sa sec"
7678                 lctl get_param -n llite.*.statahead_stats
7679                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7680
7681                 [[ $swrong -lt $ewrong ]] &&
7682                         log "statahead was stopped, maybe too many locks held!"
7683                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
7684
7685                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7686                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7687                     lctl set_param -n llite.*.statahead_max 0
7688                     lctl get_param llite.*.statahead_max
7689                     cancel_lru_locks mdc
7690                     cancel_lru_locks osc
7691                     stime=`date +%s`
7692                     time ls -l $DIR/$tdir | wc -l
7693                     etime=`date +%s`
7694                     delta=$((etime - stime))
7695                     log "ls $i files again without statahead: $delta sec"
7696                     lctl set_param llite.*.statahead_max=$max
7697                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7698                         if [  $SLOWOK -eq 0 ]; then
7699                                 error "ls $i files is slower with statahead!"
7700                         else
7701                                 log "ls $i files is slower with statahead!"
7702                         fi
7703                         break
7704                     fi
7705                 fi
7706
7707                 [ $delta -gt 20 ] && break
7708                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7709                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7710         done
7711         log "ls done"
7712
7713         stime=`date +%s`
7714         rm -r $DIR/$tdir
7715         sync
7716         etime=`date +%s`
7717         delta=$((etime - stime))
7718         log "rm -r $DIR/$tdir/: $delta seconds"
7719         log "rm done"
7720         lctl get_param -n llite.*.statahead_stats
7721 }
7722 run_test 123a "verify statahead work"
7723
7724 test_123b () { # statahead(bug 15027)
7725         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7726         test_mkdir -p $DIR/$tdir
7727         createmany -o $DIR/$tdir/$tfile-%d 1000
7728
7729         cancel_lru_locks mdc
7730         cancel_lru_locks osc
7731
7732 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7733         lctl set_param fail_loc=0x80000803
7734         ls -lR $DIR/$tdir > /dev/null
7735         log "ls done"
7736         lctl set_param fail_loc=0x0
7737         lctl get_param -n llite.*.statahead_stats
7738         rm -r $DIR/$tdir
7739         sync
7740
7741 }
7742 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7743
7744 test_124a() {
7745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7746         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
7747                 skip "no lru resize on server" && return 0
7748         local NR=2000
7749         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7750
7751         log "create $NR files at $DIR/$tdir"
7752         createmany -o $DIR/$tdir/f $NR ||
7753                 error "failed to create $NR files in $DIR/$tdir"
7754
7755         cancel_lru_locks mdc
7756         ls -l $DIR/$tdir > /dev/null
7757
7758         local NSDIR=""
7759         local LRU_SIZE=0
7760         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
7761                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
7762                 LRU_SIZE=$($LCTL get_param -n $PARAM)
7763                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
7764                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7765                         log "NSDIR=$NSDIR"
7766                         log "NS=$(basename $NSDIR)"
7767                         break
7768                 fi
7769         done
7770
7771         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
7772                 skip "Not enough cached locks created!"
7773                 return 0
7774         fi
7775         log "LRU=$LRU_SIZE"
7776
7777         local SLEEP=30
7778
7779         # We know that lru resize allows one client to hold $LIMIT locks
7780         # for 10h. After that locks begin to be killed by client.
7781         local MAX_HRS=10
7782         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
7783         log "LIMIT=$LIMIT"
7784
7785         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7786         # killing locks. Some time was spent for creating locks. This means
7787         # that up to the moment of sleep finish we must have killed some of
7788         # them (10-100 locks). This depends on how fast ther were created.
7789         # Many of them were touched in almost the same moment and thus will
7790         # be killed in groups.
7791         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7792
7793         # Use $LRU_SIZE_B here to take into account real number of locks
7794         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7795         local LRU_SIZE_B=$LRU_SIZE
7796         log "LVF=$LVF"
7797         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
7798         log "OLD_LVF=$OLD_LVF"
7799         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
7800
7801         # Let's make sure that we really have some margin. Client checks
7802         # cached locks every 10 sec.
7803         SLEEP=$((SLEEP+20))
7804         log "Sleep ${SLEEP} sec"
7805         local SEC=0
7806         while ((SEC<$SLEEP)); do
7807                 echo -n "..."
7808                 sleep 5
7809                 SEC=$((SEC+5))
7810                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
7811                 echo -n "$LRU_SIZE"
7812         done
7813         echo ""
7814         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7815         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
7816
7817         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
7818                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7819                 unlinkmany $DIR/$tdir/f $NR
7820                 return
7821         }
7822
7823         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7824         log "unlink $NR files at $DIR/$tdir"
7825         unlinkmany $DIR/$tdir/f $NR
7826 }
7827 run_test 124a "lru resize ======================================="
7828
7829 get_max_pool_limit()
7830 {
7831         local limit=$($LCTL get_param \
7832                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
7833         local max=0
7834         for l in $limit; do
7835                 if [[ $l -gt $max ]]; then
7836                         max=$l
7837                 fi
7838         done
7839         echo $max
7840 }
7841
7842 test_124b() {
7843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7844         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
7845                 skip "no lru resize on server" && return 0
7846
7847         LIMIT=$(get_max_pool_limit)
7848
7849         NR=$(($(default_lru_size)*20))
7850         if [[ $NR -gt $LIMIT ]]; then
7851                 log "Limit lock number by $LIMIT locks"
7852                 NR=$LIMIT
7853         fi
7854         lru_resize_disable mdc
7855         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7856                 error "failed to create $DIR/$tdir/disable_lru_resize"
7857
7858         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7859         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7860         cancel_lru_locks mdc
7861         stime=`date +%s`
7862         PID=""
7863         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7864         PID="$PID $!"
7865         sleep 2
7866         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7867         PID="$PID $!"
7868         sleep 2
7869         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7870         PID="$PID $!"
7871         wait $PID
7872         etime=`date +%s`
7873         nolruresize_delta=$((etime-stime))
7874         log "ls -la time: $nolruresize_delta seconds"
7875         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7876         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7877
7878         lru_resize_enable mdc
7879         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7880                 error "failed to create $DIR/$tdir/enable_lru_resize"
7881
7882         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7883         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7884         cancel_lru_locks mdc
7885         stime=`date +%s`
7886         PID=""
7887         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7888         PID="$PID $!"
7889         sleep 2
7890         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7891         PID="$PID $!"
7892         sleep 2
7893         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7894         PID="$PID $!"
7895         wait $PID
7896         etime=`date +%s`
7897         lruresize_delta=$((etime-stime))
7898         log "ls -la time: $lruresize_delta seconds"
7899         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7900
7901         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7902                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7903         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7904                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7905         else
7906                 log "lru resize performs the same with no lru resize"
7907         fi
7908         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7909 }
7910 run_test 124b "lru resize (performance test) ======================="
7911
7912 test_125() { # 13358
7913         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7914         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7915         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
7916         test_mkdir -p $DIR/d125 || error "mkdir failed"
7917         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7918         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7919         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7920 }
7921 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7922
7923 test_126() { # bug 12829/13455
7924         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7925         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7926         $GSS && skip "must run as gss disabled" && return
7927
7928         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7929         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7930         rm -f $DIR/$tfile
7931         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7932 }
7933 run_test 126 "check that the fsgid provided by the client is taken into account"
7934
7935 test_127a() { # bug 15521
7936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7937         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7938         $LCTL set_param osc.*.stats=0
7939         FSIZE=$((2048 * 1024))
7940         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7941         cancel_lru_locks osc
7942         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7943
7944         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7945         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7946                 echo "got $COUNT $NAME"
7947                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7948                 eval $NAME=$COUNT || error "Wrong proc format"
7949
7950                 case $NAME in
7951                         read_bytes|write_bytes)
7952                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7953                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7954                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7955                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7956                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7957                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7958                                 error "sumsquare is too small: $SUMSQ"
7959                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7960                                 error "sumsquare is too big: $SUMSQ"
7961                         ;;
7962                         *) ;;
7963                 esac
7964         done < $DIR/${tfile}.tmp
7965
7966         #check that we actually got some stats
7967         [ "$read_bytes" ] || error "Missing read_bytes stats"
7968         [ "$write_bytes" ] || error "Missing write_bytes stats"
7969         [ "$read_bytes" != 0 ] || error "no read done"
7970         [ "$write_bytes" != 0 ] || error "no write done"
7971 }
7972 run_test 127a "verify the client stats are sane"
7973
7974 test_127b() { # bug LU-333
7975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7976         $LCTL set_param llite.*.stats=0
7977         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7978         # perform 2 reads and writes so MAX is different from SUM.
7979         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7980         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7981         cancel_lru_locks osc
7982         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7983         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7984
7985         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7986         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7987                 echo "got $COUNT $NAME"
7988                 eval $NAME=$COUNT || error "Wrong proc format"
7989
7990         case $NAME in
7991                 read_bytes)
7992                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7993                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7994                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7995                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7996                         ;;
7997                 write_bytes)
7998                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7999                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8000                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8001                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8002                         ;;
8003                         *) ;;
8004                 esac
8005         done < $TMP/${tfile}.tmp
8006
8007         #check that we actually got some stats
8008         [ "$read_bytes" ] || error "Missing read_bytes stats"
8009         [ "$write_bytes" ] || error "Missing write_bytes stats"
8010         [ "$read_bytes" != 0 ] || error "no read done"
8011         [ "$write_bytes" != 0 ] || error "no write done"
8012 }
8013 run_test 127b "verify the llite client stats are sane"
8014
8015 test_128() { # bug 15212
8016         touch $DIR/$tfile
8017         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8018                 find $DIR/$tfile
8019                 find $DIR/$tfile
8020         EOF
8021
8022         result=$(grep error $TMP/$tfile.log)
8023         rm -f $DIR/$tfile
8024         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8025 }
8026 run_test 128 "interactive lfs for 2 consecutive find's"
8027
8028 set_dir_limits () {
8029         local mntdev
8030         local canondev
8031         local node
8032
8033         local LDPROC=/proc/fs/ldiskfs
8034         local facets=$(get_facets MDS)
8035
8036         for facet in ${facets//,/ }; do
8037                 canondev=$(ldiskfs_canon \
8038                            *.$(convert_facet2label $facet).mntdev $facet)
8039                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8040                                                 LDPROC=/sys/fs/ldiskfs
8041                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8042         done
8043 }
8044
8045 test_129() {
8046         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8047         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8048                 skip "Only applicable to ldiskfs-based MDTs"
8049                 return
8050         fi
8051         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8052
8053         ENOSPC=28
8054         EFBIG=27
8055
8056         rm -rf $DIR/$tdir
8057         test_mkdir -p $DIR/$tdir
8058
8059         # block size of mds1
8060         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8061         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8062         local MAX=$((MDSBLOCKSIZE * 3))
8063         set_dir_limits $MAX
8064         local I=$(stat -c%s "$DIR/$tdir")
8065         local J=0
8066         local STRIPE_COUNT=1
8067         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8068         MAX=$((MAX*STRIPE_COUNT))
8069         while [[ $I -le $MAX ]]; do
8070                 $MULTIOP $DIR/$tdir/$J Oc
8071                 rc=$?
8072                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8073                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8074                 #and EFBIG for previous versions
8075                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8076                         set_dir_limits 0
8077                         echo "return code $rc received as expected"
8078                         multiop $DIR/$tdir/$J Oc ||
8079                                 error_exit "multiop failed w/o dir size limit"
8080
8081                         I=$(stat -c%s "$DIR/$tdir")
8082
8083                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8084                                         $(version_code 2.4.51) ]
8085                         then
8086                                 [[ $I -eq $MAX ]] && return 0
8087                         else
8088                                 [[ $I -gt $MAX ]] && return 0
8089                         fi
8090                         error_exit "current dir size $I, previous limit $MAX"
8091                 elif [ $rc -ne 0 ]; then
8092                         set_dir_limits 0
8093                         error_exit "return code $rc received instead of expected " \
8094                                    "$EFBIG or $ENOSPC, files in dir $I"
8095                 fi
8096                 J=$((J+1))
8097                 I=$(stat -c%s "$DIR/$tdir")
8098         done
8099
8100         set_dir_limits 0
8101         error "exceeded dir size limit $MAX x $MDSCOUNT $MAX : $I bytes"
8102 }
8103 run_test 129 "test directory size limit ========================"
8104
8105 OLDIFS="$IFS"
8106 cleanup_130() {
8107         trap 0
8108         IFS="$OLDIFS"
8109 }
8110
8111 test_130a() {
8112         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8113         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8114                 return
8115
8116         trap cleanup_130 EXIT RETURN
8117
8118         local fm_file=$DIR/$tfile
8119         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8120         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8121                 error "dd failed for $fm_file"
8122
8123         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8124         filefrag -ves $fm_file
8125         RC=$?
8126         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8127                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8128         [ $RC != 0 ] && error "filefrag $fm_file failed"
8129
8130         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8131                       grep -v "ext:" | grep -v "found")
8132         lun=$($GETSTRIPE -i $fm_file)
8133
8134         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8135         IFS=$'\n'
8136         tot_len=0
8137         for line in $filefrag_op
8138         do
8139                 frag_lun=`echo $line | cut -d: -f5`
8140                 ext_len=`echo $line | cut -d: -f4`
8141                 if (( $frag_lun != $lun )); then
8142                         cleanup_130
8143                         error "FIEMAP on 1-stripe file($fm_file) failed"
8144                         return
8145                 fi
8146                 (( tot_len += ext_len ))
8147         done
8148
8149         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8150                 cleanup_130
8151                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8152                 return
8153         fi
8154
8155         cleanup_130
8156
8157         echo "FIEMAP on single striped file succeeded"
8158 }
8159 run_test 130a "FIEMAP (1-stripe file)"
8160
8161 test_130b() {
8162         [ "$OSTCOUNT" -lt "2" ] &&
8163                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8164
8165         [ "$OSTCOUNT" -ge "10" ] &&
8166                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8167
8168         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8169         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8170                 return
8171
8172         trap cleanup_130 EXIT RETURN
8173
8174         local fm_file=$DIR/$tfile
8175         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8176         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8177                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8178
8179         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8180                 error "dd failed on $fm_file"
8181
8182         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8183         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8184                       grep -v "ext:" | grep -v "found")
8185
8186         last_lun=$(echo $filefrag_op | cut -d: -f5)
8187
8188         IFS=$'\n'
8189         tot_len=0
8190         num_luns=1
8191         for line in $filefrag_op
8192         do
8193                 frag_lun=`echo $line | cut -d: -f5`
8194                 ext_len=`echo $line | cut -d: -f4`
8195                 if (( $frag_lun != $last_lun )); then
8196                         if (( tot_len != 1024 )); then
8197                                 cleanup_130
8198                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8199                                 return
8200                         else
8201                                 (( num_luns += 1 ))
8202                                 tot_len=0
8203                         fi
8204                 fi
8205                 (( tot_len += ext_len ))
8206                 last_lun=$frag_lun
8207         done
8208         if (( num_luns != 2 || tot_len != 1024 )); then
8209                 cleanup_130
8210                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8211                 return
8212         fi
8213
8214         cleanup_130
8215
8216         echo "FIEMAP on 2-stripe file succeeded"
8217 }
8218 run_test 130b "FIEMAP (2-stripe file)"
8219
8220 test_130c() {
8221         [ "$OSTCOUNT" -lt "2" ] &&
8222                 skip_env "skipping FIEMAP on 2-stripe file" && return
8223
8224         [ "$OSTCOUNT" -ge "10" ] &&
8225                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8226
8227         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8228         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8229                 return
8230
8231         trap cleanup_130 EXIT RETURN
8232
8233         local fm_file=$DIR/$tfile
8234         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8235         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8236                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8237
8238         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8239
8240         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8241         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8242
8243         last_lun=`echo $filefrag_op | cut -d: -f5`
8244
8245         IFS=$'\n'
8246         tot_len=0
8247         num_luns=1
8248         for line in $filefrag_op
8249         do
8250                 frag_lun=`echo $line | cut -d: -f5`
8251                 ext_len=`echo $line | cut -d: -f4`
8252                 if (( $frag_lun != $last_lun )); then
8253                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8254                         if (( logical != 512 )); then
8255                                 cleanup_130
8256                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8257                                 return
8258                         fi
8259                         if (( tot_len != 512 )); then
8260                                 cleanup_130
8261                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8262                                 return
8263                         else
8264                                 (( num_luns += 1 ))
8265                                 tot_len=0
8266                         fi
8267                 fi
8268                 (( tot_len += ext_len ))
8269                 last_lun=$frag_lun
8270         done
8271         if (( num_luns != 2 || tot_len != 512 )); then
8272                 cleanup_130
8273                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8274                 return
8275         fi
8276
8277         cleanup_130
8278
8279         echo "FIEMAP on 2-stripe file with hole succeeded"
8280 }
8281 run_test 130c "FIEMAP (2-stripe file with hole)"
8282
8283 test_130d() {
8284         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8285
8286         [ "$OSTCOUNT" -ge "10" ] &&
8287                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8288
8289         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8290         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8291
8292         trap cleanup_130 EXIT RETURN
8293
8294         local fm_file=$DIR/$tfile
8295         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8296         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8297                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8298
8299         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8300         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8301                 error "dd failed on $fm_file"
8302
8303         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8304         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8305                 grep -v "ext:" | grep -v "found"`
8306
8307         last_lun=`echo $filefrag_op | cut -d: -f5`
8308
8309         IFS=$'\n'
8310         tot_len=0
8311         num_luns=1
8312         for line in $filefrag_op
8313         do
8314                 frag_lun=`echo $line | cut -d: -f5`
8315                 ext_len=`echo $line | cut -d: -f4`
8316                 if (( $frag_lun != $last_lun )); then
8317                         if (( tot_len != 1024 )); then
8318                                 cleanup_130
8319                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8320                                 return
8321                         else
8322                                 (( num_luns += 1 ))
8323                                 tot_len=0
8324                         fi
8325                 fi
8326                 (( tot_len += ext_len ))
8327                 last_lun=$frag_lun
8328         done
8329         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8330                 cleanup_130
8331                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8332                 return
8333         fi
8334
8335         cleanup_130
8336
8337         echo "FIEMAP on N-stripe file succeeded"
8338 }
8339 run_test 130d "FIEMAP (N-stripe file)"
8340
8341 test_130e() {
8342         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8343
8344         [ "$OSTCOUNT" -ge "10" ] &&
8345                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8346
8347         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8348         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8349
8350         trap cleanup_130 EXIT RETURN
8351
8352         local fm_file=$DIR/$tfile
8353         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8354         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8355                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8356
8357         NUM_BLKS=512
8358         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8359         for ((i = 0; i < $NUM_BLKS; i++))
8360         do
8361                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8362         done
8363
8364         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8365         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8366
8367         last_lun=`echo $filefrag_op | cut -d: -f5`
8368
8369         IFS=$'\n'
8370         tot_len=0
8371         num_luns=1
8372         for line in $filefrag_op
8373         do
8374                 frag_lun=`echo $line | cut -d: -f5`
8375                 ext_len=`echo $line | cut -d: -f4`
8376                 if (( $frag_lun != $last_lun )); then
8377                         if (( tot_len != $EXPECTED_LEN )); then
8378                                 cleanup_130
8379                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8380                                 return
8381                         else
8382                                 (( num_luns += 1 ))
8383                                 tot_len=0
8384                         fi
8385                 fi
8386                 (( tot_len += ext_len ))
8387                 last_lun=$frag_lun
8388         done
8389         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8390                 cleanup_130
8391                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8392                 return
8393         fi
8394
8395         cleanup_130
8396
8397         echo "FIEMAP with continuation calls succeeded"
8398 }
8399 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8400
8401 # Test for writev/readv
8402 test_131a() {
8403         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8404         error "writev test failed"
8405         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8406         error "readv failed"
8407         rm -f $DIR/$tfile
8408 }
8409 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8410
8411 test_131b() {
8412         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8413         error "append writev test failed"
8414         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8415         error "append writev test failed"
8416         rm -f $DIR/$tfile
8417 }
8418 run_test 131b "test append writev"
8419
8420 test_131c() {
8421         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8422         error "NOT PASS"
8423 }
8424 run_test 131c "test read/write on file w/o objects"
8425
8426 test_131d() {
8427         rwv -f $DIR/$tfile -w -n 1 1572864
8428         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8429         if [ "$NOB" != 1572864 ]; then
8430                 error "Short read filed: read $NOB bytes instead of 1572864"
8431         fi
8432         rm -f $DIR/$tfile
8433 }
8434 run_test 131d "test short read"
8435
8436 test_131e() {
8437         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8438         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8439         error "read hitting hole failed"
8440         rm -f $DIR/$tfile
8441 }
8442 run_test 131e "test read hitting hole"
8443
8444 get_ost_param() {
8445         local token=$1
8446         local gl_sum=0
8447         for node in $(osts_nodes); do
8448                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8449                 [ x$gl = x"" ] && gl=0
8450                 gl_sum=$((gl_sum + gl))
8451         done
8452         echo $gl_sum
8453 }
8454
8455 som_mode_switch() {
8456         local som=$1
8457         local gl1=$2
8458         local gl2=$3
8459
8460         if [ x$som = x"enabled" ]; then
8461                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8462                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8463                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8464         else
8465                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8466                 MOUNTOPT="$MOUNTOPT,som_preview"
8467                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8468         fi
8469
8470         # do remount to make new mount-conf parameters actual
8471         echo remounting...
8472         sync
8473         stopall
8474         setupall
8475 }
8476
8477 test_132() { #1028, SOM
8478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8479         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8480         local num=$(get_mds_dir $DIR)
8481         local mymds=mds${num}
8482         local MOUNTOPT_SAVE=$MOUNTOPT
8483
8484         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8485         cancel_lru_locks osc
8486
8487         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8488
8489         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8490         stat $DIR/$tfile >/dev/null
8491         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8492         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8493         rm $DIR/$tfile
8494         som_mode_switch $som1 $gl1 $gl2
8495
8496         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8497         cancel_lru_locks osc
8498
8499         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8500         if [ $som1 == $som2 ]; then
8501             error "som is still "$som2
8502             if [ x$som2 = x"enabled" ]; then
8503                 som2="disabled"
8504             else
8505                 som2="enabled"
8506             fi
8507         fi
8508
8509         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8510         stat $DIR/$tfile >/dev/null
8511         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8512         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8513         som_mode_switch $som2 $gl1 $gl2
8514         MOUNTOPT=$MOUNTOPT_SAVE
8515 }
8516 run_test 132 "som avoids glimpse rpc"
8517
8518 check_stats() {
8519         local res
8520         local count
8521         case $1 in
8522         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8523                  ;;
8524         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8525                  ;;
8526         *) error "Wrong argument $1" ;;
8527         esac
8528         echo $res
8529         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8530         # if the argument $3 is zero, it means any stat increment is ok.
8531         if [[ $3 -gt 0 ]]; then
8532                 count=$(echo $res | awk '{ print $2 }')
8533                 [[ $count -ne $3 ]] &&
8534                         error "The $2 counter on $1 is wrong - expected $3"
8535         fi
8536 }
8537
8538 test_133a() {
8539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8540         remote_ost_nodsh && skip "remote OST with nodsh" && return
8541         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8542
8543         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8544                 { skip "MDS doesn't support rename stats"; return; }
8545         local testdir=$DIR/${tdir}/stats_testdir
8546         mkdir -p $DIR/${tdir}
8547
8548         # clear stats.
8549         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8550         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8551
8552         # verify mdt stats first.
8553         mkdir ${testdir} || error "mkdir failed"
8554         check_stats $SINGLEMDS "mkdir" 1
8555         touch ${testdir}/${tfile} || "touch failed"
8556         check_stats $SINGLEMDS "open" 1
8557         check_stats $SINGLEMDS "close" 1
8558         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8559         check_stats $SINGLEMDS "mknod" 1
8560         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8561         check_stats $SINGLEMDS "unlink" 1
8562         rm -f ${testdir}/${tfile} || error "file remove failed"
8563         check_stats $SINGLEMDS "unlink" 2
8564
8565         # remove working dir and check mdt stats again.
8566         rmdir ${testdir} || error "rmdir failed"
8567         check_stats $SINGLEMDS "rmdir" 1
8568
8569         local testdir1=$DIR/${tdir}/stats_testdir1
8570         mkdir -p ${testdir}
8571         mkdir -p ${testdir1}
8572         touch ${testdir1}/test1
8573         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8574         check_stats $SINGLEMDS "crossdir_rename" 1
8575
8576         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8577         check_stats $SINGLEMDS "samedir_rename" 1
8578
8579         rm -rf $DIR/${tdir}
8580 }
8581 run_test 133a "Verifying MDT stats ========================================"
8582
8583 test_133b() {
8584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8585         remote_ost_nodsh && skip "remote OST with nodsh" && return
8586         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8587         local testdir=$DIR/${tdir}/stats_testdir
8588         mkdir -p ${testdir} || error "mkdir failed"
8589         touch ${testdir}/${tfile} || "touch failed"
8590         cancel_lru_locks mdc
8591
8592         # clear stats.
8593         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8594         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8595
8596         # extra mdt stats verification.
8597         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8598         check_stats $SINGLEMDS "setattr" 1
8599         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8600         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8601         then            # LU-1740
8602                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8603                 check_stats $SINGLEMDS "getattr" 1
8604         fi
8605         $LFS df || error "lfs failed"
8606         check_stats $SINGLEMDS "statfs" 1
8607
8608         rm -rf $DIR/${tdir}
8609 }
8610 run_test 133b "Verifying extra MDT stats =================================="
8611
8612 test_133c() {
8613         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8614         remote_ost_nodsh && skip "remote OST with nodsh" && return
8615         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8616         local testdir=$DIR/${tdir}/stats_testdir
8617         test_mkdir -p ${testdir} || error "mkdir failed"
8618
8619         # verify obdfilter stats.
8620         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8621         sync
8622         cancel_lru_locks osc
8623         wait_delete_completed
8624
8625         # clear stats.
8626         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8627         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8628
8629         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8630         sync
8631         cancel_lru_locks osc
8632         check_stats ost "write" 1
8633
8634         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8635         check_stats ost "read" 1
8636
8637         > ${testdir}/${tfile} || error "truncate failed"
8638         check_stats ost "punch" 1
8639
8640         rm -f ${testdir}/${tfile} || error "file remove failed"
8641         wait_delete_completed
8642         check_stats ost "destroy" 1
8643
8644         rm -rf $DIR/${tdir}
8645 }
8646 run_test 133c "Verifying OST stats ========================================"
8647
8648 order_2() {
8649         local value=$1
8650         local orig=$value
8651         local order=1
8652
8653         while [ $value -ge 2 ]; do
8654                 order=$((order*2))
8655                 value=$((value/2))
8656         done
8657
8658         if [ $orig -gt $order ]; then
8659                 order=$((order*2))
8660         fi
8661         echo $order
8662 }
8663
8664 size_in_KMGT() {
8665     local value=$1
8666     local size=('K' 'M' 'G' 'T');
8667     local i=0
8668     local size_string=$value
8669
8670     while [ $value -ge 1024 ]; do
8671         if [ $i -gt 3 ]; then
8672             #T is the biggest unit we get here, if that is bigger,
8673             #just return XXXT
8674             size_string=${value}T
8675             break
8676         fi
8677         value=$((value >> 10))
8678         if [ $value -lt 1024 ]; then
8679             size_string=${value}${size[$i]}
8680             break
8681         fi
8682         i=$((i + 1))
8683     done
8684
8685     echo $size_string
8686 }
8687
8688 get_rename_size() {
8689     local size=$1
8690     local context=${2:-.}
8691     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8692                 grep -A1 $context |
8693                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8694     echo $sample
8695 }
8696
8697 test_133d() {
8698         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8699         remote_ost_nodsh && skip "remote OST with nodsh" && return
8700         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8701         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8702         { skip "MDS doesn't support rename stats"; return; }
8703
8704         local testdir1=$DIR/${tdir}/stats_testdir1
8705         local testdir2=$DIR/${tdir}/stats_testdir2
8706
8707         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8708
8709         mkdir -p ${testdir1} || error "mkdir failed"
8710         mkdir -p ${testdir2} || error "mkdir failed"
8711
8712         createmany -o $testdir1/test 512 || error "createmany failed"
8713
8714         # check samedir rename size
8715         mv ${testdir1}/test0 ${testdir1}/test_0
8716
8717         local testdir1_size=$(ls -l $DIR/${tdir} |
8718                 awk '/stats_testdir1/ {print $5}')
8719         local testdir2_size=$(ls -l $DIR/${tdir} |
8720                 awk '/stats_testdir2/ {print $5}')
8721
8722         testdir1_size=$(order_2 $testdir1_size)
8723         testdir2_size=$(order_2 $testdir2_size)
8724
8725         testdir1_size=$(size_in_KMGT $testdir1_size)
8726         testdir2_size=$(size_in_KMGT $testdir2_size)
8727
8728         echo "source rename dir size: ${testdir1_size}"
8729         echo "target rename dir size: ${testdir2_size}"
8730
8731         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8732         eval $cmd || error "$cmd failed"
8733         local samedir=$($cmd | grep 'same_dir')
8734         local same_sample=$(get_rename_size $testdir1_size)
8735         [ -z "$samedir" ] && error "samedir_rename_size count error"
8736         [[ $same_sample -eq 1 ]] ||
8737                 error "samedir_rename_size error $same_sample"
8738         echo "Check same dir rename stats success"
8739
8740         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8741
8742         # check crossdir rename size
8743         mv ${testdir1}/test_0 ${testdir2}/test_0
8744
8745         testdir1_size=$(ls -l $DIR/${tdir} |
8746                 awk '/stats_testdir1/ {print $5}')
8747         testdir2_size=$(ls -l $DIR/${tdir} |
8748                 awk '/stats_testdir2/ {print $5}')
8749
8750         testdir1_size=$(order_2 $testdir1_size)
8751         testdir2_size=$(order_2 $testdir2_size)
8752
8753         testdir1_size=$(size_in_KMGT $testdir1_size)
8754         testdir2_size=$(size_in_KMGT $testdir2_size)
8755
8756         echo "source rename dir size: ${testdir1_size}"
8757         echo "target rename dir size: ${testdir2_size}"
8758
8759         eval $cmd || error "$cmd failed"
8760         local crossdir=$($cmd | grep 'crossdir')
8761         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8762         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8763         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8764         [[ $src_sample -eq 1 ]] ||
8765                 error "crossdir_rename_size error $src_sample"
8766         [[ $tgt_sample -eq 1 ]] ||
8767                 error "crossdir_rename_size error $tgt_sample"
8768         echo "Check cross dir rename stats success"
8769         rm -rf $DIR/${tdir}
8770 }
8771 run_test 133d "Verifying rename_stats ========================================"
8772
8773 test_133e() {
8774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8775         local testdir=$DIR/${tdir}/stats_testdir
8776         local ctr f0 f1 bs=32768 count=42 sum
8777
8778         remote_ost_nodsh && skip "remote OST with nodsh" && return
8779         mkdir -p ${testdir} || error "mkdir failed"
8780
8781         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8782
8783         for ctr in {write,read}_bytes; do
8784                 sync
8785                 cancel_lru_locks osc
8786
8787                 do_facet ost1 $LCTL set_param -n \
8788                         "obdfilter.*.exports.clear=clear"
8789
8790                 if [ $ctr = write_bytes ]; then
8791                         f0=/dev/zero
8792                         f1=${testdir}/${tfile}
8793                 else
8794                         f0=${testdir}/${tfile}
8795                         f1=/dev/null
8796                 fi
8797
8798                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8799                         error "dd failed"
8800                 sync
8801                 cancel_lru_locks osc
8802
8803                 sum=$(do_facet ost1 $LCTL get_param \
8804                         "obdfilter.*.exports.*.stats" |
8805                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
8806                                 $1 == ctr { sum += $7 }
8807                                 END { printf("%0.0f", sum) }')
8808
8809                 if ((sum != bs * count)); then
8810                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8811                 fi
8812         done
8813
8814         rm -rf $DIR/${tdir}
8815 }
8816 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8817
8818 test_133f() {
8819         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8820         local facet
8821
8822         # First without trusting modes.
8823         find $proc_dirs -exec cat '{}' \; &> /dev/null
8824
8825         # Second verifying readability.
8826         find $proc_dirs \
8827                 -type f \
8828                 -exec cat '{}' \; &> /dev/null ||
8829                         error "proc file read failed"
8830
8831         for facet in $SINGLEMDS ost1; do
8832                 do_facet $facet find $proc_dirs \
8833                         ! -name req_history \
8834                         -exec cat '{}' \\\; &> /dev/null
8835
8836                 do_facet $facet find $proc_dirs \
8837                         ! -name req_history \
8838                         -type f \
8839                         -exec cat '{}' \\\; &> /dev/null ||
8840                                 error "proc file read failed"
8841         done
8842 }
8843 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
8844
8845 test_140() { #bug-17379
8846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8847         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8848         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8849         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8850
8851         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8852         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8853         local i=0
8854         while i=`expr $i + 1`; do
8855                 test_mkdir -p $i || error "Creating dir $i"
8856                 cd $i || error "Changing to $i"
8857                 ln -s ../stat stat || error "Creating stat symlink"
8858                 # Read the symlink until ELOOP present,
8859                 # not LBUGing the system is considered success,
8860                 # we didn't overrun the stack.
8861                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8862                 [ $ret -ne 0 ] && {
8863                         if [ $ret -eq 40 ]; then
8864                                 break  # -ELOOP
8865                         else
8866                                 error "Open stat symlink"
8867                                 return
8868                         fi
8869                 }
8870         done
8871         i=`expr $i - 1`
8872         echo "The symlink depth = $i"
8873         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8874                                         error "Invalid symlink depth"
8875
8876         # Test recursive symlink
8877         ln -s symlink_self symlink_self
8878         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8879         echo "open symlink_self returns $ret"
8880         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8881 }
8882 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8883
8884 test_150() {
8885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8886         local TF="$TMP/$tfile"
8887
8888         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8889         cp $TF $DIR/$tfile
8890         cancel_lru_locks osc
8891         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8892         remount_client $MOUNT
8893         df -P $MOUNT
8894         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8895
8896         $TRUNCATE $TF 6000
8897         $TRUNCATE $DIR/$tfile 6000
8898         cancel_lru_locks osc
8899         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8900
8901         echo "12345" >>$TF
8902         echo "12345" >>$DIR/$tfile
8903         cancel_lru_locks osc
8904         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8905
8906         echo "12345" >>$TF
8907         echo "12345" >>$DIR/$tfile
8908         cancel_lru_locks osc
8909         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8910
8911         rm -f $TF
8912         true
8913 }
8914 run_test 150 "truncate/append tests"
8915
8916 #LU-2902 roc_hit was not able to read all values from lproc
8917 function roc_hit_init() {
8918         local list=$(comma_list $(osts_nodes))
8919         local dir=$DIR/$tdir-check
8920         local file=$dir/file
8921         local BEFORE
8922         local AFTER
8923         local idx
8924
8925         test_mkdir -p $dir
8926         #use setstripe to do a write to every ost
8927         for i in $(seq 0 $((OSTCOUNT-1))); do
8928                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
8929                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
8930                 idx=$(printf %04x $i)
8931                 BEFORE=$(get_osd_param $list *OST*$idx stats |
8932                         awk '$1 == "cache_access" {sum += $2}
8933                                 END { printf("%0.0f", sum) }')
8934
8935                 cancel_lru_locks osc
8936                 cat $file >/dev/null
8937
8938                 AFTER=$(get_osd_param $list *OST*$idx stats |
8939                         awk '$1 == "cache_access" {sum += $2}
8940                                 END { printf("%0.0f", sum) }')
8941
8942                 echo BEFORE:$BEFORE AFTER:$AFTER
8943                 if ! let "AFTER - BEFORE == 4"; then
8944                         rm -rf $dir
8945                         error "roc_hit is not safe to use"
8946                 fi
8947                 rm $file
8948         done
8949
8950         rm -rf $dir
8951 }
8952
8953 function roc_hit() {
8954         local list=$(comma_list $(osts_nodes))
8955         echo $(get_osd_param $list '' stats |
8956                 awk '$1 == "cache_hit" {sum += $2}
8957                         END { printf("%0.0f", sum) }')
8958 }
8959
8960 function set_cache() {
8961         local on=1
8962
8963         if [ "$2" == "off" ]; then
8964                 on=0;
8965         fi
8966         local list=$(comma_list $(osts_nodes))
8967         set_osd_param $list '' $1_cache_enable $on
8968
8969         cancel_lru_locks osc
8970 }
8971
8972 test_151() {
8973         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8974         remote_ost_nodsh && skip "remote OST with nodsh" && return
8975
8976         local CPAGES=3
8977         local list=$(comma_list $(osts_nodes))
8978
8979         # check whether obdfilter is cache capable at all
8980         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8981                 echo "not cache-capable obdfilter"
8982                 return 0
8983         fi
8984
8985         # check cache is enabled on all obdfilters
8986         if get_osd_param $list '' read_cache_enable | grep 0; then
8987                 echo "oss cache is disabled"
8988                 return 0
8989         fi
8990
8991         set_osd_param $list '' writethrough_cache_enable 1
8992
8993         # check write cache is enabled on all obdfilters
8994         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
8995                 echo "oss write cache is NOT enabled"
8996                 return 0
8997         fi
8998
8999         roc_hit_init
9000
9001         #define OBD_FAIL_OBD_NO_LRU  0x609
9002         do_nodes $list $LCTL set_param fail_loc=0x609
9003
9004         # pages should be in the case right after write
9005         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9006                 error "dd failed"
9007
9008         local BEFORE=$(roc_hit)
9009         cancel_lru_locks osc
9010         cat $DIR/$tfile >/dev/null
9011         local AFTER=$(roc_hit)
9012
9013         do_nodes $list $LCTL set_param fail_loc=0
9014
9015         if ! let "AFTER - BEFORE == CPAGES"; then
9016                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9017         fi
9018
9019         # the following read invalidates the cache
9020         cancel_lru_locks osc
9021         set_osd_param $list '' read_cache_enable 0
9022         cat $DIR/$tfile >/dev/null
9023
9024         # now data shouldn't be found in the cache
9025         BEFORE=$(roc_hit)
9026         cancel_lru_locks osc
9027         cat $DIR/$tfile >/dev/null
9028         AFTER=$(roc_hit)
9029         if let "AFTER - BEFORE != 0"; then
9030                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9031         fi
9032
9033         set_osd_param $list '' read_cache_enable 1
9034         rm -f $DIR/$tfile
9035 }
9036 run_test 151 "test cache on oss and controls ==============================="
9037
9038 test_152() {
9039         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9040         local TF="$TMP/$tfile"
9041
9042         # simulate ENOMEM during write
9043 #define OBD_FAIL_OST_NOMEM      0x226
9044         lctl set_param fail_loc=0x80000226
9045         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9046         cp $TF $DIR/$tfile
9047         sync || error "sync failed"
9048         lctl set_param fail_loc=0
9049
9050         # discard client's cache
9051         cancel_lru_locks osc
9052
9053         # simulate ENOMEM during read
9054         lctl set_param fail_loc=0x80000226
9055         cmp $TF $DIR/$tfile || error "cmp failed"
9056         lctl set_param fail_loc=0
9057
9058         rm -f $TF
9059 }
9060 run_test 152 "test read/write with enomem ============================"
9061
9062 test_153() {
9063         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9064 }
9065 run_test 153 "test if fdatasync does not crash ======================="
9066
9067 dot_lustre_fid_permission_check() {
9068         local fid=$1
9069         local ffid=$MOUNT/.lustre/fid/$fid
9070         local test_dir=$2
9071
9072         echo "stat fid $fid"
9073         stat $ffid > /dev/null || error "stat $ffid failed."
9074         echo "touch fid $fid"
9075         touch $ffid || error "touch $ffid failed."
9076         echo "write to fid $fid"
9077         cat /etc/hosts > $ffid || error "write $ffid failed."
9078         echo "read fid $fid"
9079         diff /etc/hosts $ffid || error "read $ffid failed."
9080         echo "append write to fid $fid"
9081         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9082         echo "rename fid $fid"
9083         mv $ffid $test_dir/$tfile.1 &&
9084                 error "rename $ffid to $tfile.1 should fail."
9085         touch $test_dir/$tfile.1
9086         mv $test_dir/$tfile.1 $ffid &&
9087                 error "rename $tfile.1 to $ffid should fail."
9088         rm -f $test_dir/$tfile.1
9089         echo "truncate fid $fid"
9090         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9091         echo "link fid $fid"
9092         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9093         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9094                 echo "setfacl fid $fid"
9095                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9096                 echo "getfacl fid $fid"
9097                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9098         fi
9099         echo "unlink fid $fid"
9100         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9101         echo "mknod fid $fid"
9102         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9103
9104         fid=[0xf00000400:0x1:0x0]
9105         ffid=$MOUNT/.lustre/fid/$fid
9106
9107         echo "stat non-exist fid $fid"
9108         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9109         echo "write to non-exist fid $fid"
9110         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9111         echo "link new fid $fid"
9112         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9113
9114         mkdir -p $test_dir/$tdir
9115         touch $test_dir/$tdir/$tfile
9116         fid=$($LFS path2fid $test_dir/$tdir)
9117         rc=$?
9118         [ $rc -ne 0 ] &&
9119                 error "error: could not get fid for $test_dir/$dir/$tfile."
9120
9121         ffid=$MOUNT/.lustre/fid/$fid
9122
9123         echo "ls $fid"
9124         ls $ffid > /dev/null || error "ls $ffid failed."
9125         echo "touch $fid/$tfile.1"
9126         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9127
9128         echo "touch $MOUNT/.lustre/fid/$tfile"
9129         touch $MOUNT/.lustre/fid/$tfile && \
9130                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9131
9132         echo "setxattr to $MOUNT/.lustre/fid"
9133         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9134
9135         echo "listxattr for $MOUNT/.lustre/fid"
9136         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9137
9138         echo "delxattr from $MOUNT/.lustre/fid"
9139         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9140
9141         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9142         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9143                 error "touch invalid fid should fail."
9144
9145         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9146         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9147                 error "touch non-normal fid should fail."
9148
9149         echo "rename $tdir to $MOUNT/.lustre/fid"
9150         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9151                 error "rename to $MOUNT/.lustre/fid should fail."
9152
9153         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9154         then            # LU-3547
9155                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9156                 local new_obf_mode=777
9157
9158                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9159                 chmod $new_obf_mode $DIR/.lustre/fid ||
9160                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9161
9162                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9163                 [ $obf_mode -eq $new_obf_mode ] ||
9164                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9165
9166                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9167                 chmod $old_obf_mode $DIR/.lustre/fid ||
9168                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9169         fi
9170
9171         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9172         fid=$($LFS path2fid $test_dir/$tfile-2)
9173         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9174         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9175                 error "create lov data thru .lustre should fail."
9176         echo "cp /etc/passwd $test_dir/$tfile-2"
9177         cp /etc/passwd $test_dir/$tfile-2 ||
9178                 error "copy to $test_dir/$tfile-2 failed."
9179         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9180         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9181                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9182
9183         rm -rf $test_dir/tfile.lnk
9184         rm -rf $test_dir/$tfile-2
9185 }
9186
9187 test_154A() {
9188         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9189                 skip "Need MDS version at least 2.4.1" && return
9190
9191         touch $DIR/$tfile
9192         local FID=$($LFS path2fid $DIR/$tfile)
9193         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9194
9195         # check that we get the same pathname back
9196         local FOUND=$($LFS fid2path $MOUNT $FID)
9197         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9198         [ "$FOUND" != "$DIR/$tfile" ] &&
9199                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9200
9201         rm -rf $DIR/$tfile
9202 }
9203 run_test 154A "lfs path2fid and fid2path basic checks"
9204
9205 test_154a() {
9206         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9207         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9208                 { skip "Need MDS version at least 2.2.51"; return 0; }
9209         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9210
9211         cp /etc/hosts $DIR/$tfile
9212
9213         fid=$($LFS path2fid $DIR/$tfile)
9214         rc=$?
9215         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9216
9217         dot_lustre_fid_permission_check "$fid" $DIR ||
9218                 error "dot lustre permission check $fid failed"
9219
9220         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9221
9222         touch $MOUNT/.lustre/file &&
9223                 error "creation is not allowed under .lustre"
9224
9225         mkdir $MOUNT/.lustre/dir &&
9226                 error "mkdir is not allowed under .lustre"
9227
9228         rm -rf $DIR/$tfile
9229 }
9230 run_test 154a "Open-by-FID"
9231
9232 test_154b() {
9233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9234         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9235                 { skip "Need MDS version at least 2.2.51"; return 0; }
9236
9237         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9238
9239         local remote_dir=$DIR/$tdir/remote_dir
9240         local MDTIDX=1
9241         local rc=0
9242
9243         mkdir -p $DIR/$tdir
9244         $LFS mkdir -i $MDTIDX $remote_dir ||
9245                 error "create remote directory failed"
9246
9247         cp /etc/hosts $remote_dir/$tfile
9248
9249         fid=$($LFS path2fid $remote_dir/$tfile)
9250         rc=$?
9251         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9252
9253         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9254                 error "dot lustre permission check $fid failed"
9255         rm -rf $DIR/$tdir
9256 }
9257 run_test 154b "Open-by-FID for remote directory"
9258
9259 test_154c() {
9260         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9261                 skip "Need MDS version at least 2.4.1" && return
9262
9263         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9264         local FID1=$($LFS path2fid $DIR/$tfile.1)
9265         local FID2=$($LFS path2fid $DIR/$tfile.2)
9266         local FID3=$($LFS path2fid $DIR/$tfile.3)
9267
9268         local N=1
9269         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9270                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9271                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9272                 local want=FID$N
9273                 [ "$FID" = "${!want}" ] ||
9274                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9275                 N=$((N + 1))
9276         done
9277
9278         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9279                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9280                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9281                 N=$((N + 1))
9282         done
9283 }
9284 run_test 154c "lfs path2fid and fid2path multiple arguments"
9285
9286 test_154d() {
9287         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9288                 skip "Need MDS version at least 2.5.53" && return
9289
9290         if remote_mds; then
9291                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9292         else
9293                 nid="0@lo"
9294         fi
9295         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9296         local fd
9297         local cmd
9298
9299         rm -f $DIR/$tfile
9300         touch $DIR/$tfile
9301
9302         fid=$($LFS path2fid $DIR/$tfile)
9303         # Open the file
9304         fd=$(free_fd)
9305         cmd="exec $fd<$DIR/$tfile"
9306         eval $cmd
9307         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9308         echo $fid_list | grep $fid
9309         rc=$?
9310
9311         cmd="exec $fd>/dev/null"
9312         eval $cmd
9313         if [ $rc -ne 0 ]; then
9314                 error "FID $fid not found in open files list $fid_list"
9315         fi
9316 }
9317 run_test 154d "Verify open file fid"
9318
9319 test_155_small_load() {
9320     local temp=$TMP/$tfile
9321     local file=$DIR/$tfile
9322
9323     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9324         error "dd of=$temp bs=6096 count=1 failed"
9325     cp $temp $file
9326     cancel_lru_locks osc
9327     cmp $temp $file || error "$temp $file differ"
9328
9329     $TRUNCATE $temp 6000
9330     $TRUNCATE $file 6000
9331     cmp $temp $file || error "$temp $file differ (truncate1)"
9332
9333     echo "12345" >>$temp
9334     echo "12345" >>$file
9335     cmp $temp $file || error "$temp $file differ (append1)"
9336
9337     echo "12345" >>$temp
9338     echo "12345" >>$file
9339     cmp $temp $file || error "$temp $file differ (append2)"
9340
9341     rm -f $temp $file
9342     true
9343 }
9344
9345 test_155_big_load() {
9346     remote_ost_nodsh && skip "remote OST with nodsh" && return
9347     local temp=$TMP/$tfile
9348     local file=$DIR/$tfile
9349
9350     free_min_max
9351     local cache_size=$(do_facet ost$((MAXI+1)) \
9352         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9353     local large_file_size=$((cache_size * 2))
9354
9355     echo "OSS cache size: $cache_size KB"
9356     echo "Large file size: $large_file_size KB"
9357
9358     [ $MAXV -le $large_file_size ] && \
9359         skip_env "max available OST size needs > $large_file_size KB" && \
9360         return 0
9361
9362     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9363
9364     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9365         error "dd of=$temp bs=$large_file_size count=1k failed"
9366     cp $temp $file
9367     ls -lh $temp $file
9368     cancel_lru_locks osc
9369     cmp $temp $file || error "$temp $file differ"
9370
9371     rm -f $temp $file
9372     true
9373 }
9374
9375 test_155a() {
9376         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9377         set_cache read on
9378         set_cache writethrough on
9379         test_155_small_load
9380 }
9381 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9382
9383 test_155b() {
9384         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9385         set_cache read on
9386         set_cache writethrough off
9387         test_155_small_load
9388 }
9389 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9390
9391 test_155c() {
9392         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9393         set_cache read off
9394         set_cache writethrough on
9395         test_155_small_load
9396 }
9397 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9398
9399 test_155d() {
9400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9401         set_cache read off
9402         set_cache writethrough off
9403         test_155_small_load
9404 }
9405 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9406
9407 test_155e() {
9408         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9409         set_cache read on
9410         set_cache writethrough on
9411         test_155_big_load
9412 }
9413 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9414
9415 test_155f() {
9416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9417         set_cache read on
9418         set_cache writethrough off
9419         test_155_big_load
9420 }
9421 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9422
9423 test_155g() {
9424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9425         set_cache read off
9426         set_cache writethrough on
9427         test_155_big_load
9428 }
9429 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9430
9431 test_155h() {
9432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9433         set_cache read off
9434         set_cache writethrough off
9435         test_155_big_load
9436 }
9437 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9438
9439 test_156() {
9440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9441         local CPAGES=3
9442         local BEFORE
9443         local AFTER
9444         local file="$DIR/$tfile"
9445
9446         [ "$(facet_fstype ost1)" = "zfs" ] &&
9447                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9448                 return
9449
9450         roc_hit_init
9451
9452     log "Turn on read and write cache"
9453     set_cache read on
9454     set_cache writethrough on
9455
9456     log "Write data and read it back."
9457     log "Read should be satisfied from the cache."
9458     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9459     BEFORE=`roc_hit`
9460     cancel_lru_locks osc
9461     cat $file >/dev/null
9462     AFTER=`roc_hit`
9463     if ! let "AFTER - BEFORE == CPAGES"; then
9464         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9465     else
9466         log "cache hits:: before: $BEFORE, after: $AFTER"
9467     fi
9468
9469     log "Read again; it should be satisfied from the cache."
9470     BEFORE=$AFTER
9471     cancel_lru_locks osc
9472     cat $file >/dev/null
9473     AFTER=`roc_hit`
9474     if ! let "AFTER - BEFORE == CPAGES"; then
9475         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9476     else
9477         log "cache hits:: before: $BEFORE, after: $AFTER"
9478     fi
9479
9480
9481     log "Turn off the read cache and turn on the write cache"
9482     set_cache read off
9483     set_cache writethrough on
9484
9485     log "Read again; it should be satisfied from the cache."
9486     BEFORE=`roc_hit`
9487     cancel_lru_locks osc
9488     cat $file >/dev/null
9489     AFTER=`roc_hit`
9490     if ! let "AFTER - BEFORE == CPAGES"; then
9491         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9492     else
9493         log "cache hits:: before: $BEFORE, after: $AFTER"
9494     fi
9495
9496     log "Read again; it should not be satisfied from the cache."
9497     BEFORE=$AFTER
9498     cancel_lru_locks osc
9499     cat $file >/dev/null
9500     AFTER=`roc_hit`
9501     if ! let "AFTER - BEFORE == 0"; then
9502         error "IN CACHE: before: $BEFORE, after: $AFTER"
9503     else
9504         log "cache hits:: before: $BEFORE, after: $AFTER"
9505     fi
9506
9507     log "Write data and read it back."
9508     log "Read should be satisfied from the cache."
9509     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9510     BEFORE=`roc_hit`
9511     cancel_lru_locks osc
9512     cat $file >/dev/null
9513     AFTER=`roc_hit`
9514     if ! let "AFTER - BEFORE == CPAGES"; then
9515         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9516     else
9517         log "cache hits:: before: $BEFORE, after: $AFTER"
9518     fi
9519
9520     log "Read again; it should not be satisfied from the cache."
9521     BEFORE=$AFTER
9522     cancel_lru_locks osc
9523     cat $file >/dev/null
9524     AFTER=`roc_hit`
9525     if ! let "AFTER - BEFORE == 0"; then
9526         error "IN CACHE: before: $BEFORE, after: $AFTER"
9527     else
9528         log "cache hits:: before: $BEFORE, after: $AFTER"
9529     fi
9530
9531
9532     log "Turn off read and write cache"
9533     set_cache read off
9534     set_cache writethrough off
9535
9536     log "Write data and read it back"
9537     log "It should not be satisfied from the cache."
9538     rm -f $file
9539     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9540     cancel_lru_locks osc
9541     BEFORE=`roc_hit`
9542     cat $file >/dev/null
9543     AFTER=`roc_hit`
9544     if ! let "AFTER - BEFORE == 0"; then
9545         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9546     else
9547         log "cache hits:: before: $BEFORE, after: $AFTER"
9548     fi
9549
9550
9551     log "Turn on the read cache and turn off the write cache"
9552     set_cache read on
9553     set_cache writethrough off
9554
9555     log "Write data and read it back"
9556     log "It should not be satisfied from the cache."
9557     rm -f $file
9558     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9559     BEFORE=`roc_hit`
9560     cancel_lru_locks osc
9561     cat $file >/dev/null
9562     AFTER=`roc_hit`
9563     if ! let "AFTER - BEFORE == 0"; then
9564         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9565     else
9566         log "cache hits:: before: $BEFORE, after: $AFTER"
9567     fi
9568
9569     log "Read again; it should be satisfied from the cache."
9570     BEFORE=`roc_hit`
9571     cancel_lru_locks osc
9572     cat $file >/dev/null
9573     AFTER=`roc_hit`
9574     if ! let "AFTER - BEFORE == CPAGES"; then
9575         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9576     else
9577         log "cache hits:: before: $BEFORE, after: $AFTER"
9578     fi
9579
9580     rm -f $file
9581 }
9582 run_test 156 "Verification of tunables ============================"
9583
9584 #Changelogs
9585 err17935 () {
9586         if [[ $MDSCOUNT -gt 1 ]]; then
9587                 error_ignore bz17935 $*
9588         else
9589                 error $*
9590         fi
9591 }
9592
9593 changelog_chmask()
9594 {
9595         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9596
9597         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9598
9599         if [ $MASK -eq 1 ]; then
9600                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9601         else
9602                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9603         fi
9604 }
9605
9606 changelog_extract_field() {
9607         local mdt=$1
9608         local cltype=$2
9609         local file=$3
9610         local identifier=$4
9611
9612         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9613                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9614                 tail -1
9615 }
9616
9617 test_160a() {
9618         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9619         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9620         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9621                 { skip "Need MDS version at least 2.2.0"; return; }
9622
9623         local CL_USERS="mdd.$MDT0.changelog_users"
9624         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9625         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9626         echo "Registered as changelog user $USER"
9627         $GET_CL_USERS | grep -q $USER ||
9628                 error "User $USER not found in changelog_users"
9629
9630         # change something
9631         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9632         touch $DIR/$tdir/pics/2008/zachy/timestamp
9633         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9634         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9635         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9636         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9637         rm $DIR/$tdir/pics/desktop.jpg
9638
9639         $LFS changelog $MDT0 | tail -5
9640
9641         echo "verifying changelog mask"
9642         changelog_chmask "MKDIR"
9643         changelog_chmask "CLOSE"
9644
9645         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9646         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9647
9648         changelog_chmask "MKDIR"
9649         changelog_chmask "CLOSE"
9650
9651         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9652         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9653
9654         $LFS changelog $MDT0
9655         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9656         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9657         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9658         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9659
9660         # verify contents
9661         echo "verifying target fid"
9662         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9663         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9664         [ "$fidc" == "$fidf" ] ||
9665                 err17935 "fid in changelog $fidc != file fid $fidf"
9666         echo "verifying parent fid"
9667         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9668         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9669         [ "$fidc" == "$fidf" ] ||
9670                 err17935 "pfid in changelog $fidc != dir fid $fidf"
9671
9672         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9673         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9674         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9675         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9676         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
9677                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
9678
9679         MIN_REC=$($GET_CL_USERS |
9680                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
9681         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
9682         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9683         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
9684                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
9685
9686         # LU-3446 changelog index reset on MDT restart
9687         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9688         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9689         $LFS changelog_clear $MDT0 $USER 0
9690         stop $SINGLEMDS || error "Fail to stop MDT."
9691         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9692         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9693         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
9694         [ $CUR_REC1 == $CUR_REC2 ] ||
9695                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
9696
9697         echo "verifying user deregister"
9698         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9699         $GET_CL_USERS | grep -q $USER &&
9700                 error "User $USER still in changelog_users"
9701
9702         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
9703         if [ $USERS -eq 0 ]; then
9704                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9705                 touch $DIR/$tdir/chloe
9706                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9707                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
9708                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9709         else
9710                 echo "$USERS other changelog users; can't verify off"
9711         fi
9712 }
9713 run_test 160a "changelog sanity"
9714
9715 test_160b() { # LU-3587
9716         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9717         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9718         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9719                 { skip "Need MDS version at least 2.2.0"; return; }
9720
9721         local CL_USERS="mdd.$MDT0.changelog_users"
9722         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9723         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9724         echo "Registered as changelog user $USER"
9725         $GET_CL_USERS | grep -q $USER ||
9726                 error "User $USER not found in changelog_users"
9727
9728         local LONGNAME1=$(str_repeat a 255)
9729         local LONGNAME2=$(str_repeat b 255)
9730
9731         cd $DIR
9732         echo "creating very long named file"
9733         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
9734         echo "moving very long named file"
9735         mv $LONGNAME1 $LONGNAME2
9736
9737         $LFS changelog $MDT0 | grep RENME
9738
9739         echo "deregistering $USER"
9740         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9741
9742         rm -f $LONGNAME2
9743 }
9744 run_test 160b "Verify that very long rename doesn't crash in changelog"
9745
9746 test_160c() {
9747         local rc=0
9748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9749
9750         # Registration step
9751         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
9752                 changelog_register -n)
9753
9754         rm -rf $DIR/$tdir
9755         mkdir -p $DIR/$tdir
9756         $MCREATE $DIR/$tdir/foo_160c
9757         changelog_chmask "TRUNC"
9758         $TRUNCATE $DIR/$tdir/foo_160c 200
9759         changelog_chmask "TRUNC"
9760         $TRUNCATE $DIR/$tdir/foo_160c 199
9761         $LFS changelog $MDT0
9762         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
9763         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
9764         $LFS changelog_clear $MDT0 $USER 0
9765
9766         # Deregistration step
9767         echo "deregistering $USER"
9768         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9769 }
9770 run_test 160c "verify that changelog log catch the truncate event"
9771
9772 test_161a() {
9773         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9774     test_mkdir -p $DIR/$tdir
9775     cp /etc/hosts $DIR/$tdir/$tfile
9776     test_mkdir $DIR/$tdir/foo1
9777     test_mkdir $DIR/$tdir/foo2
9778     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9779     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9780     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9781     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9782         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9783         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9784                 $LFS fid2path $DIR $FID
9785                 err17935 "bad link ea"
9786         fi
9787     # middle
9788     rm $DIR/$tdir/foo2/zachary
9789     # last
9790     rm $DIR/$tdir/foo2/thor
9791     # first
9792     rm $DIR/$tdir/$tfile
9793     # rename
9794     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9795     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9796         then
9797         $LFS fid2path $DIR $FID
9798         err17935 "bad link rename"
9799     fi
9800     rm $DIR/$tdir/foo2/maggie
9801
9802         # overflow the EA
9803         local longname=filename_avg_len_is_thirty_two_
9804         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
9805                 error "failed to hardlink many files"
9806         links=$($LFS fid2path $DIR $FID | wc -l)
9807         echo -n "${links}/1000 links in link EA"
9808         [[ $links -gt 60 ]] ||
9809                 err17935 "expected at least 60 links in link EA"
9810         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
9811                 error "failed to unlink many hardlinks"
9812 }
9813 run_test 161a "link ea sanity"
9814
9815 test_161b() {
9816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9817         [ $MDSCOUNT -lt 2 ] &&
9818                 skip "skipping remote directory test" && return
9819         local MDTIDX=1
9820         local remote_dir=$DIR/$tdir/remote_dir
9821
9822         mkdir -p $DIR/$tdir
9823         $LFS mkdir -i $MDTIDX $remote_dir ||
9824                 error "create remote directory failed"
9825
9826         cp /etc/hosts $remote_dir/$tfile
9827         mkdir -p $remote_dir/foo1
9828         mkdir -p $remote_dir/foo2
9829         ln $remote_dir/$tfile $remote_dir/foo1/sofia
9830         ln $remote_dir/$tfile $remote_dir/foo2/zachary
9831         ln $remote_dir/$tfile $remote_dir/foo1/luna
9832         ln $remote_dir/$tfile $remote_dir/foo2/thor
9833
9834         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
9835                      tr -d ']')
9836         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9837                 $LFS fid2path $DIR $FID
9838                 err17935 "bad link ea"
9839         fi
9840         # middle
9841         rm $remote_dir/foo2/zachary
9842         # last
9843         rm $remote_dir/foo2/thor
9844         # first
9845         rm $remote_dir/$tfile
9846         # rename
9847         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
9848         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
9849         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
9850                 $LFS fid2path $DIR $FID
9851                 err17935 "bad link rename"
9852         fi
9853         rm $remote_dir/foo2/maggie
9854
9855         # overflow the EA
9856         local longname=filename_avg_len_is_thirty_two_
9857         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
9858                 error "failed to hardlink many files"
9859         links=$($LFS fid2path $DIR $FID | wc -l)
9860         echo -n "${links}/1000 links in link EA"
9861         [[ ${links} -gt 60 ]] ||
9862                 err17935 "expected at least 60 links in link EA"
9863         unlinkmany $remote_dir/foo2/$longname 1000 ||
9864         error "failed to unlink many hardlinks"
9865 }
9866 run_test 161b "link ea sanity under remote directory"
9867
9868 test_161c() {
9869         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9870         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
9871                 skip "Need MDS version at least 2.1.5" && return
9872
9873         # define CLF_RENAME_LAST 0x0001
9874         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
9875         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
9876                 changelog_register -n)
9877         rm -rf $DIR/$tdir
9878         mkdir -p $DIR/$tdir
9879         touch $DIR/$tdir/foo_161c
9880         touch $DIR/$tdir/bar_161c
9881         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
9882         $LFS changelog $MDT0 | grep RENME
9883         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
9884                 cut -f5 -d' ')
9885         $LFS changelog_clear $MDT0 $USER 0
9886         if [ x$flags != "x0x1" ]; then
9887                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9888                         $USER
9889                 error "flag $flags is not 0x1"
9890         fi
9891         echo "rename overwrite a target having nlink = 1," \
9892                 "changelog record has flags of $flags"
9893
9894         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
9895         touch $DIR/$tdir/foo_161c
9896         touch $DIR/$tdir/bar_161c
9897         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
9898         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
9899         $LFS changelog $MDT0 | grep RENME
9900         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
9901         $LFS changelog_clear $MDT0 $USER 0
9902         if [ x$flags != "x0x0" ]; then
9903                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9904                         $USER
9905                 error "flag $flags is not 0x0"
9906         fi
9907         echo "rename overwrite a target having nlink > 1," \
9908                 "changelog record has flags of $flags"
9909
9910         # rename doesn't overwrite a target (changelog flag 0x0)
9911         touch $DIR/$tdir/foo_161c
9912         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
9913         $LFS changelog $MDT0 | grep RENME
9914         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
9915         $LFS changelog_clear $MDT0 $USER 0
9916         if [ x$flags != "x0x0" ]; then
9917                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9918                         $USER
9919                 error "flag $flags is not 0x0"
9920         fi
9921         echo "rename doesn't overwrite a target," \
9922                 "changelog record has flags of $flags"
9923
9924         # define CLF_UNLINK_LAST 0x0001
9925         # unlink a file having nlink = 1 (changelog flag 0x1)
9926         rm -f $DIR/$tdir/foo2_161c
9927         $LFS changelog $MDT0 | grep UNLNK
9928         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
9929         $LFS changelog_clear $MDT0 $USER 0
9930         if [ x$flags != "x0x1" ]; then
9931                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9932                         $USER
9933                 error "flag $flags is not 0x1"
9934         fi
9935         echo "unlink a file having nlink = 1," \
9936                 "changelog record has flags of $flags"
9937
9938         # unlink a file having nlink > 1 (changelog flag 0x0)
9939         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
9940         rm -f $DIR/$tdir/foobar_161c
9941         $LFS changelog $MDT0 | grep UNLNK
9942         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
9943         $LFS changelog_clear $MDT0 $USER 0
9944         if [ x$flags != "x0x0" ]; then
9945                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9946                         $USER
9947                 error "flag $flags is not 0x0"
9948         fi
9949         echo "unlink a file having nlink > 1," \
9950                 "changelog record has flags of $flags"
9951         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9952 }
9953 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
9954
9955 check_path() {
9956     local expected=$1
9957     shift
9958     local fid=$2
9959
9960     local path=$(${LFS} fid2path $*)
9961     RC=$?
9962
9963     if [ $RC -ne 0 ]; then
9964         err17935 "path looked up of $expected failed. Error $RC"
9965         return $RC
9966     elif [ "${path}" != "${expected}" ]; then
9967         err17935 "path looked up \"${path}\" instead of \"${expected}\""
9968         return 2
9969     fi
9970     echo "fid $fid resolves to path $path (expected $expected)"
9971 }
9972
9973 test_162() {
9974         # Make changes to filesystem
9975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9976         test_mkdir -p $DIR/$tdir/d2
9977         touch $DIR/$tdir/d2/$tfile
9978         touch $DIR/$tdir/d2/x1
9979         touch $DIR/$tdir/d2/x2
9980         test_mkdir -p $DIR/$tdir/d2/a/b/c
9981         test_mkdir -p $DIR/$tdir/d2/p/q/r
9982         # regular file
9983         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
9984         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
9985
9986         # softlink
9987         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
9988         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
9989         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
9990
9991         # softlink to wrong file
9992         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
9993         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
9994         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
9995
9996         # hardlink
9997         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
9998         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
9999         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10000         # fid2path dir/fsname should both work
10001         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
10002         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
10003
10004         # hardlink count: check that there are 2 links
10005         # Doesnt work with CMD yet: 17935
10006         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10007                 err17935 "expected 2 links"
10008
10009         # hardlink indexing: remove the first link
10010         rm $DIR/$tdir/d2/p/q/r/hlink
10011         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
10012
10013         return 0
10014 }
10015 run_test 162 "path lookup sanity"
10016
10017 test_169() {
10018         # do directio so as not to populate the page cache
10019         log "creating a 10 Mb file"
10020         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10021         log "starting reads"
10022         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10023         log "truncating the file"
10024         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10025         log "killing dd"
10026         kill %+ || true # reads might have finished
10027         echo "wait until dd is finished"
10028         wait
10029         log "removing the temporary file"
10030         rm -rf $DIR/$tfile || error "tmp file removal failed"
10031 }
10032 run_test 169 "parallel read and truncate should not deadlock"
10033
10034 test_170() {
10035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10036         $LCTL clear     # bug 18514
10037         $LCTL debug_daemon start $TMP/${tfile}_log_good
10038         touch $DIR/$tfile
10039         $LCTL debug_daemon stop
10040         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10041                error "sed failed to read log_good"
10042
10043         $LCTL debug_daemon start $TMP/${tfile}_log_good
10044         rm -rf $DIR/$tfile
10045         $LCTL debug_daemon stop
10046
10047         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10048                error "lctl df log_bad failed"
10049
10050         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10051         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10052
10053         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10054         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10055
10056         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10057                 error "bad_line good_line1 good_line2 are empty"
10058
10059         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10060         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10061         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10062
10063         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10064         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10065         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10066
10067         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10068                 error "bad_line_new good_line_new are empty"
10069
10070         local expected_good=$((good_line1 + good_line2*2))
10071
10072         rm -f $TMP/${tfile}*
10073         # LU-231, short malformed line may not be counted into bad lines
10074         if [ $bad_line -ne $bad_line_new ] &&
10075                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10076                 error "expected $bad_line bad lines, but got $bad_line_new"
10077                 return 1
10078         fi
10079
10080         if [ $expected_good -ne $good_line_new ]; then
10081                 error "expected $expected_good good lines, but got $good_line_new"
10082                 return 2
10083         fi
10084         true
10085 }
10086 run_test 170 "test lctl df to handle corrupted log ====================="
10087
10088 test_171() { # bug20592
10089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10090 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10091         $LCTL set_param fail_loc=0x50e
10092         $LCTL set_param fail_val=3000
10093         multiop_bg_pause $DIR/$tfile O_s || true
10094         local MULTIPID=$!
10095         kill -USR1 $MULTIPID
10096         # cause log dump
10097         sleep 3
10098         wait $MULTIPID
10099         if dmesg | grep "recursive fault"; then
10100                 error "caught a recursive fault"
10101         fi
10102         $LCTL set_param fail_loc=0
10103         true
10104 }
10105 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10106
10107 # it would be good to share it with obdfilter-survey/libecho code
10108 setup_obdecho_osc () {
10109         local rc=0
10110         local ost_nid=$1
10111         local obdfilter_name=$2
10112         echo "Creating new osc for $obdfilter_name on $ost_nid"
10113         # make sure we can find loopback nid
10114         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10115
10116         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10117                            ${obdfilter_name}_osc_UUID || rc=2; }
10118         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10119                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10120         return $rc
10121 }
10122
10123 cleanup_obdecho_osc () {
10124         local obdfilter_name=$1
10125         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10126         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10127         return 0
10128 }
10129
10130 obdecho_test() {
10131         local OBD=$1
10132         local node=$2
10133         local pages=${3:-64}
10134         local rc=0
10135         local id
10136
10137         local count=10
10138         local obd_size=$(get_obd_size $node $OBD)
10139         local page_size=$(get_page_size $node)
10140         if [[ -n "$obd_size" ]]; then
10141                 local new_count=$((obd_size / (pages * page_size / 1024)))
10142                 [[ $new_count -ge $count ]] || count=$new_count
10143         fi
10144
10145         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10146         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10147                            rc=2; }
10148         if [ $rc -eq 0 ]; then
10149             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10150             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10151         fi
10152         echo "New object id is $id"
10153         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10154                            rc=4; }
10155         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10156                            "test_brw $count w v $pages $id" || rc=4; }
10157         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10158                            rc=4; }
10159         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10160                                         "cleanup" || rc=5; }
10161         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10162                                         "detach" || rc=6; }
10163         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10164         return $rc
10165 }
10166
10167 test_180a() {
10168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10169         remote_ost_nodsh && skip "remote OST with nodsh" && return
10170         local rc=0
10171         local rmmod_local=0
10172
10173         if ! module_loaded obdecho; then
10174             load_module obdecho/obdecho
10175             rmmod_local=1
10176         fi
10177
10178         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10179         local host=$(lctl get_param -n osc.$osc.import |
10180                              awk '/current_connection:/ {print $2}' )
10181         local target=$(lctl get_param -n osc.$osc.import |
10182                              awk '/target:/ {print $2}' )
10183         target=${target%_UUID}
10184
10185         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10186         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10187         [[ -n $target ]] && cleanup_obdecho_osc $target
10188         [ $rmmod_local -eq 1 ] && rmmod obdecho
10189         return $rc
10190 }
10191 run_test 180a "test obdecho on osc"
10192
10193 test_180b() {
10194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10195         remote_ost_nodsh && skip "remote OST with nodsh" && return
10196         local rc=0
10197         local rmmod_remote=0
10198
10199         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10200                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10201                       "modprobe obdecho; }" && rmmod_remote=1
10202         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10203         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10204         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10205         return $rc
10206 }
10207 run_test 180b "test obdecho directly on obdfilter"
10208
10209 test_180c() { # LU-2598
10210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10211         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10212                 skip "Need MDS version at least 2.4.0" && return
10213
10214         local rc=0
10215         local rmmod_remote=false
10216         local pages=16384 # 64MB bulk I/O RPC size
10217         local target
10218
10219         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10220                 rmmod_remote=true || error "failed to load module obdecho"
10221
10222         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10223                 head -n1)
10224         if [ -n "$target" ]; then
10225                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10226         else
10227                 echo "there is no obdfilter target on ost1"
10228                 rc=2
10229         fi
10230         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10231         return $rc
10232 }
10233 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10234
10235 test_181() { # bug 22177
10236         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10237         # create enough files to index the directory
10238         createmany -o $DIR/$tdir/foobar 4000
10239         # print attributes for debug purpose
10240         lsattr -d .
10241         # open dir
10242         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10243         MULTIPID=$!
10244         # remove the files & current working dir
10245         unlinkmany $DIR/$tdir/foobar 4000
10246         rmdir $DIR/$tdir
10247         kill -USR1 $MULTIPID
10248         wait $MULTIPID
10249         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10250         return 0
10251 }
10252 run_test 181 "Test open-unlinked dir ========================"
10253
10254 test_182() {
10255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10256         # disable MDC RPC lock wouldn't crash client
10257         local fcount=1000
10258         local tcount=4
10259
10260         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10261 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10262         $LCTL set_param fail_loc=0x804
10263
10264         for (( i=0; i < $tcount; i++ )) ; do
10265                 mkdir $DIR/$tdir/$i
10266                 createmany -o $DIR/$tdir/$i/f- $fcount &
10267         done
10268         wait
10269
10270         for (( i=0; i < $tcount; i++ )) ; do
10271                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10272         done
10273         wait
10274
10275         rm -rf $DIR/$tdir
10276
10277         $LCTL set_param fail_loc=0
10278 }
10279 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10280
10281 test_183() { # LU-2275
10282         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10283                 skip "Need MDS version at least 2.3.56" && return
10284
10285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10286         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10287         echo aaa > $DIR/$tdir/$tfile
10288
10289 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10290         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10291
10292         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10293         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10294
10295         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10296
10297         # Flush negative dentry cache
10298         touch $DIR/$tdir/$tfile
10299
10300         # We are not checking for any leaked references here, they'll
10301         # become evident next time we do cleanup with module unload.
10302         rm -rf $DIR/$tdir
10303 }
10304 run_test 183 "No crash or request leak in case of strange dispositions ========"
10305
10306 # test suite 184 is for LU-2016, LU-2017
10307 test_184a() {
10308         check_swap_layouts_support && return 0
10309
10310         dir0=$DIR/$tdir/$testnum
10311         test_mkdir -p $dir0 || error "creating dir $dir0"
10312         ref1=/etc/passwd
10313         ref2=/etc/group
10314         file1=$dir0/f1
10315         file2=$dir0/f2
10316         $SETSTRIPE -c1 $file1
10317         cp $ref1 $file1
10318         $SETSTRIPE -c2 $file2
10319         cp $ref2 $file2
10320         gen1=$($GETSTRIPE -g $file1)
10321         gen2=$($GETSTRIPE -g $file2)
10322
10323         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10324         gen=$($GETSTRIPE -g $file1)
10325         [[ $gen1 != $gen ]] ||
10326                 "Layout generation on $file1 does not change"
10327         gen=$($GETSTRIPE -g $file2)
10328         [[ $gen2 != $gen ]] ||
10329                 "Layout generation on $file2 does not change"
10330
10331         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10332         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10333 }
10334 run_test 184a "Basic layout swap"
10335
10336 test_184b() {
10337         check_swap_layouts_support && return 0
10338
10339         dir0=$DIR/$tdir/$testnum
10340         mkdir -p $dir0 || error "creating dir $dir0"
10341         file1=$dir0/f1
10342         file2=$dir0/f2
10343         file3=$dir0/f3
10344         dir1=$dir0/d1
10345         dir2=$dir0/d2
10346         mkdir $dir1 $dir2
10347         $SETSTRIPE -c1 $file1
10348         $SETSTRIPE -c2 $file2
10349         $SETSTRIPE -c1 $file3
10350         chown $RUNAS_ID $file3
10351         gen1=$($GETSTRIPE -g $file1)
10352         gen2=$($GETSTRIPE -g $file2)
10353
10354         $LFS swap_layouts $dir1 $dir2 &&
10355                 error "swap of directories layouts should fail"
10356         $LFS swap_layouts $dir1 $file1 &&
10357                 error "swap of directory and file layouts should fail"
10358         $RUNAS $LFS swap_layouts $file1 $file2 &&
10359                 error "swap of file we cannot write should fail"
10360         $LFS swap_layouts $file1 $file3 &&
10361                 error "swap of file with different owner should fail"
10362         /bin/true # to clear error code
10363 }
10364 run_test 184b "Forbidden layout swap (will generate errors)"
10365
10366 test_184c() {
10367         check_swap_layouts_support && return 0
10368
10369         local dir0=$DIR/$tdir/$testnum
10370         mkdir -p $dir0 || error "creating dir $dir0"
10371
10372         local ref1=$dir0/ref1
10373         local ref2=$dir0/ref2
10374         local file1=$dir0/file1
10375         local file2=$dir0/file2
10376         # create a file large enough for the concurent test
10377         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10378         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10379         echo "ref file size: ref1($(stat -c %s $ref1))," \
10380              "ref2($(stat -c %s $ref2))"
10381
10382         cp $ref2 $file2
10383         dd if=$ref1 of=$file1 bs=16k &
10384         local DD_PID=$!
10385
10386         # Make sure dd starts to copy file
10387         while [ ! -f $file1 ]; do sleep 0.1; done
10388
10389         $LFS swap_layouts $file1 $file2
10390         local rc=$?
10391         wait $DD_PID
10392         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10393         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10394
10395         # how many bytes copied before swapping layout
10396         local copied=$(stat -c %s $file2)
10397         local remaining=$(stat -c %s $ref1)
10398         remaining=$((remaining - copied))
10399         echo "Copied $copied bytes before swapping layout..."
10400
10401         cmp -n $copied $file1 $ref2 | grep differ &&
10402                 error "Content mismatch [0, $copied) of ref2 and file1"
10403         cmp -n $copied $file2 $ref1 ||
10404                 error "Content mismatch [0, $copied) of ref1 and file2"
10405         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10406                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10407
10408         # clean up
10409         rm -f $ref1 $ref2 $file1 $file2
10410 }
10411 run_test 184c "Concurrent write and layout swap"
10412
10413 test_184d() {
10414         check_swap_layouts_support && return 0
10415         [ -z "$(which getfattr 2>/dev/null)" ] &&
10416                 skip "no getfattr command" && return 0
10417
10418         local file1=$DIR/$tdir/$tfile-1
10419         local file2=$DIR/$tdir/$tfile-2
10420         local file3=$DIR/$tdir/$tfile-3
10421         local lovea1
10422         local lovea2
10423
10424         mkdir -p $DIR/$tdir
10425         touch $file1 || error "create $file1 failed"
10426         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10427                 error "create $file2 failed"
10428         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10429                 error "create $file3 failed"
10430         lovea1=$($LFS getstripe $file1 | sed 1d)
10431
10432         $LFS swap_layouts $file2 $file3 ||
10433                 error "swap $file2 $file3 layouts failed"
10434         $LFS swap_layouts $file1 $file2 ||
10435                 error "swap $file1 $file2 layouts failed"
10436
10437         lovea2=$($LFS getstripe $file2 | sed 1d)
10438         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10439
10440         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10441         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10442 }
10443 run_test 184d "allow stripeless layouts swap"
10444
10445
10446 test_185() { # LU-2441
10447         # LU-3553 - no volatile file support in old servers
10448         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10449                 { skip "Need MDS version at least 2.3.60"; return 0; }
10450
10451         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10452         touch $DIR/$tdir/spoo
10453         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10454         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10455                 error "cannot create/write a volatile file"
10456         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10457                 error "FID is still valid after close"
10458
10459         multiop_bg_pause $DIR/$tdir vVw4096_c
10460         local multi_pid=$!
10461
10462         local OLD_IFS=$IFS
10463         IFS=":"
10464         local fidv=($fid)
10465         IFS=$OLD_IFS
10466         # assume that the next FID for this client is sequential, since stdout
10467         # is unfortunately eaten by multiop_bg_pause
10468         local n=$((${fidv[1]} + 1))
10469         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10470         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10471                 error "FID is missing before close"
10472         kill -USR1 $multi_pid
10473         # 1 second delay, so if mtime change we will see it
10474         sleep 1
10475         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10476         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10477 }
10478 run_test 185 "Volatile file support"
10479
10480 test_187a() {
10481         local dir0=$DIR/$tdir/$testnum
10482         mkdir -p $dir0 || error "creating dir $dir0"
10483
10484         local file=$dir0/file1
10485         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10486         local dv1=$($LFS data_version $file)
10487         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10488         local dv2=$($LFS data_version $file)
10489         [[ $dv1 != $dv2 ]] ||
10490                 error "data version did not change on write $dv1 == $dv2"
10491
10492         # clean up
10493         rm -f $file1
10494 }
10495 run_test 187a "Test data version change"
10496
10497 test_187b() {
10498         local dir0=$DIR/$tdir/$testnum
10499         mkdir -p $dir0 || error "creating dir $dir0"
10500
10501         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10502         [[ ${DV[0]} != ${DV[1]} ]] ||
10503                 error "data version did not change on write"\
10504                       " ${DV[0]} == ${DV[1]}"
10505
10506         # clean up
10507         rm -f $file1
10508 }
10509 run_test 187b "Test data version change on volatile file"
10510
10511 # OST pools tests
10512 check_file_in_pool()
10513 {
10514         local file=$1
10515         local pool=$2
10516         local tlist="$3"
10517         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10518         for i in $res
10519         do
10520                 for t in $tlist ; do
10521                         [ "$i" -eq "$t" ] && continue 2
10522                 done
10523
10524                 echo "pool list: $tlist"
10525                 echo "striping: $res"
10526                 error_noexit "$file not allocated in $pool"
10527                 return 1
10528         done
10529         return 0
10530 }
10531
10532 pool_add() {
10533         echo "Creating new pool"
10534         local pool=$1
10535
10536         create_pool $FSNAME.$pool ||
10537                 { error_noexit "No pool created, result code $?"; return 1; }
10538         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10539                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10540 }
10541
10542 pool_add_targets() {
10543         echo "Adding targets to pool"
10544         local pool=$1
10545         local first=$2
10546         local last=$3
10547         local step=${4:-1}
10548
10549         local list=$(seq $first $step $last)
10550
10551         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10552         do_facet mgs $LCTL pool_add \
10553                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10554         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10555                         | sort -u | tr '\n' ' ' " "$t" || { 
10556                 error_noexit "Add to pool failed"
10557                 return 1
10558         }
10559         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10560         local addcount=$(((last - first) / step + 1))
10561         [ $lfscount -eq $addcount ] || {
10562                 error_noexit "lfs pool_list bad ost count" \
10563                                                 "$lfscount != $addcount"
10564                 return 2
10565         }
10566 }
10567
10568 pool_set_dir() {
10569         local pool=$1
10570         local tdir=$2
10571         echo "Setting pool on directory $tdir"
10572
10573         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10574
10575         error_noexit "Cannot set pool $pool to $tdir"
10576         return 1
10577 }
10578
10579 pool_check_dir() {
10580         local pool=$1
10581         local tdir=$2
10582         echo "Checking pool on directory $tdir"
10583
10584         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10585         [ "$res" = "$pool" ] && return 0
10586
10587         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10588         return 1
10589 }
10590
10591 pool_dir_rel_path() {
10592         echo "Testing relative path works well"
10593         local pool=$1
10594         local tdir=$2
10595         local root=$3
10596
10597         mkdir -p $root/$tdir/$tdir
10598         cd $root/$tdir
10599         pool_set_dir $pool $tdir          || return 1
10600         pool_set_dir $pool ./$tdir        || return 2
10601         pool_set_dir $pool ../$tdir       || return 3
10602         pool_set_dir $pool ../$tdir/$tdir || return 4
10603         rm -rf $tdir; cd - > /dev/null
10604 }
10605
10606 pool_alloc_files() {
10607         echo "Checking files allocation from directory pool"
10608         local pool=$1
10609         local tdir=$2
10610         local count=$3
10611         local tlist="$4"
10612
10613         local failed=0
10614         for i in $(seq -w 1 $count)
10615         do
10616                 local file=$tdir/file-$i
10617                 touch $file
10618                 check_file_in_pool $file $pool "$tlist" || \
10619                         failed=$((failed + 1))
10620         done
10621         [ "$failed" = 0 ] && return 0
10622
10623         error_noexit "$failed files not allocated in $pool"
10624         return 1
10625 }
10626
10627 pool_create_files() {
10628         echo "Creating files in pool"
10629         local pool=$1
10630         local tdir=$2
10631         local count=$3
10632         local tlist="$4"
10633
10634         mkdir -p $tdir
10635         local failed=0
10636         for i in $(seq -w 1 $count)
10637         do
10638                 local file=$tdir/spoo-$i
10639                 $SETSTRIPE -p $pool $file
10640                 check_file_in_pool $file $pool "$tlist" || \
10641                         failed=$((failed + 1))
10642         done
10643         [ "$failed" = 0 ] && return 0
10644
10645         error_noexit "$failed files not allocated in $pool"
10646         return 1
10647 }
10648
10649 pool_lfs_df() {
10650         echo "Checking 'lfs df' output"
10651         local pool=$1
10652
10653         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
10654                         tr '\n' ' ')
10655         local res=$($LFS df --pool $FSNAME.$pool |
10656                         awk '{print $1}' |
10657                         grep "$FSNAME-OST" |
10658                         tr '\n' ' ')
10659         [ "$res" = "$t" ] && return 0
10660
10661         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
10662         return 1
10663 }
10664
10665 pool_file_rel_path() {
10666         echo "Creating files in a pool with relative pathname"
10667         local pool=$1
10668         local tdir=$2
10669
10670         mkdir -p $tdir ||
10671                 { error_noexit "unable to create $tdir"; return 1 ; }
10672         local file="/..$tdir/$tfile-1"
10673         $SETSTRIPE -p $pool $file ||
10674                 { error_noexit "unable to create $file" ; return 2 ; }
10675
10676         cd $tdir
10677         $SETSTRIPE -p $pool $tfile-2 || {
10678                 error_noexit "unable to create $tfile-2 in $tdir"
10679                 return 3
10680         }
10681 }
10682
10683 pool_remove_first_target() {
10684         echo "Removing first target from a pool"
10685         local pool=$1
10686
10687         local pname="lov.$FSNAME-*.pools.$pool"
10688         local t=$($LCTL get_param -n $pname | head -n1)
10689         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10690         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
10691                 error_noexit "$t not removed from $FSNAME.$pool"
10692                 return 1
10693         }
10694 }
10695
10696 pool_remove_all_targets() {
10697         echo "Removing all targets from pool"
10698         local pool=$1
10699         local file=$2
10700         local pname="lov.$FSNAME-*.pools.$pool"
10701         for t in $($LCTL get_param -n $pname | sort -u)
10702         do
10703                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10704         done
10705         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
10706                 error_noexit "Pool $FSNAME.$pool cannot be drained"
10707                 return 1
10708         }
10709         # striping on an empty/nonexistant pool should fall back
10710         # to "pool of everything"
10711         touch $file || {
10712                 error_noexit "failed to use fallback striping for empty pool"
10713                 return 2
10714         }
10715         # setstripe on an empty pool should fail
10716         $SETSTRIPE -p $pool $file 2>/dev/null && {
10717                 error_noexit "expected failure when creating file" \
10718                                                         "with empty pool"
10719                 return 3
10720         }
10721         return 0
10722 }
10723
10724 pool_remove() {
10725         echo "Destroying pool"
10726         local pool=$1
10727         local file=$2
10728
10729         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
10730
10731         sleep 2
10732         # striping on an empty/nonexistant pool should fall back 
10733         # to "pool of everything"
10734         touch $file || {
10735                 error_noexit "failed to use fallback striping for missing pool"
10736                 return 1
10737         }
10738         # setstripe on an empty pool should fail
10739         $SETSTRIPE -p $pool $file 2>/dev/null && {
10740                 error_noexit "expected failure when creating file" \
10741                                                         "with missing pool"
10742                 return 2
10743         }
10744
10745         # get param should return err once pool is gone
10746         if wait_update $HOSTNAME "lctl get_param -n \
10747                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
10748         then
10749                 remove_pool_from_list $FSNAME.$pool
10750                 return 0
10751         fi
10752         error_noexit "Pool $FSNAME.$pool is not destroyed"
10753         return 3
10754 }
10755
10756 test_200() {
10757         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10758         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
10759
10760         local POOL=${POOL:-cea1}
10761         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
10762         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
10763         # Pool OST targets
10764         local first_ost=0
10765         local last_ost=$(($OSTCOUNT - 1))
10766         local ost_step=2
10767         local ost_list=$(seq $first_ost $ost_step $last_ost)
10768         local ost_range="$first_ost $last_ost $ost_step"
10769         local test_path=$POOL_ROOT/$POOL_DIR_NAME
10770         local file_dir=$POOL_ROOT/file_tst
10771
10772         local rc=0
10773         while : ; do
10774                 # former test_200a test_200b
10775                 pool_add $POOL                          || { rc=$? ; break; }
10776                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
10777                 # former test_200c test_200d
10778                 mkdir -p $test_path
10779                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
10780                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
10781                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
10782                                                         || { rc=$? ; break; }
10783                 # former test_200e test_200f
10784                 local files=$((OSTCOUNT*3))
10785                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
10786                                                         || { rc=$? ; break; }
10787                 pool_create_files $POOL $file_dir $files "$ost_list" \
10788                                                         || { rc=$? ; break; }
10789                 # former test_200g test_200h
10790                 pool_lfs_df $POOL                       || { rc=$? ; break; }
10791                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
10792
10793                 # former test_201a test_201b test_201c
10794                 pool_remove_first_target $POOL          || { rc=$? ; break; }
10795
10796                 local f=$test_path/$tfile
10797                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
10798                 pool_remove $POOL $f                    || { rc=$? ; break; }
10799                 break
10800         done
10801
10802         cleanup_pools
10803         return $rc
10804 }
10805 run_test 200 "OST pools"
10806
10807 # usage: default_attr <count | size | offset>
10808 default_attr() {
10809         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
10810 }
10811
10812 # usage: check_default_stripe_attr
10813 check_default_stripe_attr() {
10814         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
10815         case $1 in
10816         --stripe-count|--count)
10817                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
10818         --stripe-size|--size)
10819                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
10820         --stripe-index|--index)
10821                 EXPECTED=-1;;
10822         *)
10823                 error "unknown getstripe attr '$1'"
10824         esac
10825
10826         [ $ACTUAL != $EXPECTED ] &&
10827                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
10828 }
10829
10830 test_204a() {
10831         test_mkdir -p $DIR/$tdir
10832         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
10833
10834         check_default_stripe_attr --stripe-count
10835         check_default_stripe_attr --stripe-size
10836         check_default_stripe_attr --stripe-index
10837
10838         return 0
10839 }
10840 run_test 204a "Print default stripe attributes ================="
10841
10842 test_204b() {
10843         test_mkdir -p $DIR/$tdir
10844         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10845
10846         check_default_stripe_attr --stripe-size
10847         check_default_stripe_attr --stripe-index
10848
10849         return 0
10850 }
10851 run_test 204b "Print default stripe size and offset  ==========="
10852
10853 test_204c() {
10854         test_mkdir -p $DIR/$tdir
10855         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10856
10857         check_default_stripe_attr --stripe-count
10858         check_default_stripe_attr --stripe-index
10859
10860         return 0
10861 }
10862 run_test 204c "Print default stripe count and offset ==========="
10863
10864 test_204d() {
10865         test_mkdir -p $DIR/$tdir
10866         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10867
10868         check_default_stripe_attr --stripe-count
10869         check_default_stripe_attr --stripe-size
10870
10871         return 0
10872 }
10873 run_test 204d "Print default stripe count and size ============="
10874
10875 test_204e() {
10876         test_mkdir -p $DIR/$tdir
10877         $SETSTRIPE -d $DIR/$tdir
10878
10879         check_default_stripe_attr --stripe-count --raw
10880         check_default_stripe_attr --stripe-size --raw
10881         check_default_stripe_attr --stripe-index --raw
10882
10883         return 0
10884 }
10885 run_test 204e "Print raw stripe attributes ================="
10886
10887 test_204f() {
10888         test_mkdir -p $DIR/$tdir
10889         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10890
10891         check_default_stripe_attr --stripe-size --raw
10892         check_default_stripe_attr --stripe-index --raw
10893
10894         return 0
10895 }
10896 run_test 204f "Print raw stripe size and offset  ==========="
10897
10898 test_204g() {
10899         test_mkdir -p $DIR/$tdir
10900         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10901
10902         check_default_stripe_attr --stripe-count --raw
10903         check_default_stripe_attr --stripe-index --raw
10904
10905         return 0
10906 }
10907 run_test 204g "Print raw stripe count and offset ==========="
10908
10909 test_204h() {
10910         test_mkdir -p $DIR/$tdir
10911         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10912
10913         check_default_stripe_attr --stripe-count --raw
10914         check_default_stripe_attr --stripe-size --raw
10915
10916         return 0
10917 }
10918 run_test 204h "Print raw stripe count and size ============="
10919
10920 # Figure out which job scheduler is being used, if any,
10921 # or use a fake one
10922 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
10923         JOBENV=SLURM_JOB_ID
10924 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
10925         JOBENV=LSB_JOBID
10926 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
10927         JOBENV=PBS_JOBID
10928 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
10929         JOBENV=LOADL_STEP_ID
10930 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
10931         JOBENV=JOB_ID
10932 else
10933         JOBENV=FAKE_JOBID
10934 fi
10935
10936 verify_jobstats() {
10937         local cmd=$1
10938         local target=$2
10939
10940         # clear old jobstats
10941         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
10942         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
10943
10944         # use a new JobID for this test, or we might see an old one
10945         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
10946
10947         JOBVAL=${!JOBENV}
10948         log "Test: $cmd"
10949         log "Using JobID environment variable $JOBENV=$JOBVAL"
10950
10951         if [ $JOBENV = "FAKE_JOBID" ]; then
10952                 FAKE_JOBID=$JOBVAL $cmd
10953         else
10954                 $cmd
10955         fi
10956
10957         if [ "$target" = "mdt" -o "$target" = "both" ]; then
10958                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
10959                 do_facet $FACET lctl get_param mdt.*.job_stats |
10960                         grep $JOBVAL || error "No job stats found on MDT $FACET"
10961         fi
10962         if [ "$target" = "ost" -o "$target" = "both" ]; then
10963                 FACET=ost1
10964                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
10965                         grep $JOBVAL || error "No job stats found on OST $FACET"
10966         fi
10967 }
10968
10969 jobstats_set() {
10970         trap 0
10971         NEW_JOBENV=${1:-$OLD_JOBENV}
10972         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
10973         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
10974 }
10975
10976 test_205() { # Job stats
10977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10978         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
10979                 skip "Server doesn't support jobstats" && return 0
10980         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
10981
10982         local cmd
10983         OLD_JOBENV=$($LCTL get_param -n jobid_var)
10984         if [ $OLD_JOBENV != $JOBENV ]; then
10985                 jobstats_set $JOBENV
10986                 trap jobstats_set EXIT
10987         fi
10988
10989         # mkdir
10990         cmd="mkdir $DIR/$tfile"
10991         verify_jobstats "$cmd" "mdt"
10992         # rmdir
10993         cmd="rm -fr $DIR/$tfile"
10994         verify_jobstats "$cmd" "mdt"
10995         # mknod
10996         cmd="mknod $DIR/$tfile c 1 3"
10997         verify_jobstats "$cmd" "mdt"
10998         # unlink
10999         cmd="rm -f $DIR/$tfile"
11000         verify_jobstats "$cmd" "mdt"
11001         # open & close
11002         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11003         verify_jobstats "$cmd" "mdt"
11004         # setattr
11005         cmd="touch $DIR/$tfile"
11006         verify_jobstats "$cmd" "both"
11007         # write
11008         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11009         verify_jobstats "$cmd" "ost"
11010         # read
11011         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11012         verify_jobstats "$cmd" "ost"
11013         # truncate
11014         cmd="$TRUNCATE $DIR/$tfile 0"
11015         verify_jobstats "$cmd" "both"
11016         # rename
11017         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11018         verify_jobstats "$cmd" "mdt"
11019
11020         # cleanup
11021         rm -f $DIR/jobstats_test_rename
11022
11023         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11024 }
11025 run_test 205 "Verify job stats"
11026
11027 # LU-1480, LU-1773 and LU-1657
11028 test_206() {
11029         mkdir -p $DIR/$tdir
11030         $SETSTRIPE -c -1 $DIR/$tdir
11031 #define OBD_FAIL_LOV_INIT 0x1403
11032         $LCTL set_param fail_loc=0xa0001403
11033         $LCTL set_param fail_val=1
11034         touch $DIR/$tdir/$tfile || true
11035 }
11036 run_test 206 "fail lov_init_raid0() doesn't lbug"
11037
11038 test_207a() {
11039         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11040         local fsz=`stat -c %s $DIR/$tfile`
11041         cancel_lru_locks mdc
11042
11043         # do not return layout in getattr intent
11044 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11045         $LCTL set_param fail_loc=0x170
11046         local sz=`stat -c %s $DIR/$tfile`
11047
11048         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11049
11050         rm -rf $DIR/$tfile
11051 }
11052 run_test 207a "can refresh layout at glimpse"
11053
11054 test_207b() {
11055         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11056         local cksum=`md5sum $DIR/$tfile`
11057         local fsz=`stat -c %s $DIR/$tfile`
11058         cancel_lru_locks mdc
11059         cancel_lru_locks osc
11060
11061         # do not return layout in getattr intent
11062 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11063         $LCTL set_param fail_loc=0x171
11064
11065         # it will refresh layout after the file is opened but before read issues
11066         echo checksum is "$cksum"
11067         echo "$cksum" |md5sum -c --quiet || error "file differs"
11068
11069         rm -rf $DIR/$tfile
11070 }
11071 run_test 207b "can refresh layout at open"
11072
11073 test_208() {
11074         # FIXME: in this test suite, only RD lease is used. This is okay
11075         # for now as only exclusive open is supported. After generic lease
11076         # is done, this test suite should be revised. - Jinshan
11077
11078         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11079                 { skip "Need MDS version at least 2.4.52"; return 0; }
11080
11081         echo "==== test 1: verify get lease work"
11082         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11083
11084         echo "==== test 2: verify lease can be broken by upcoming open"
11085         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11086         local PID=$!
11087         sleep 1
11088
11089         $MULTIOP $DIR/$tfile oO_RDONLY:c
11090         kill -USR1 $PID && wait $PID || error "break lease error"
11091
11092         echo "==== test 3: verify lease can't be granted if an open already exists"
11093         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11094         local PID=$!
11095         sleep 1
11096
11097         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11098         kill -USR1 $PID && wait $PID || error "open file error"
11099
11100         echo "==== test 4: lease can sustain over recovery"
11101         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11102         PID=$!
11103         sleep 1
11104
11105         fail mds1
11106
11107         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11108
11109         echo "==== test 5: lease broken can't be regained by replay"
11110         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11111         PID=$!
11112         sleep 1
11113
11114         # open file to break lease and then recovery
11115         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11116         fail mds1
11117
11118         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11119
11120         rm -f $DIR/$tfile
11121 }
11122 run_test 208 "Exclusive open"
11123
11124 test_209() {
11125         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11126                 skip_env "must have disp_stripe" && return
11127
11128         touch $DIR/$tfile
11129         sync; sleep 5; sync;
11130
11131         echo 3 > /proc/sys/vm/drop_caches
11132         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11133
11134         # open/close 500 times
11135         for i in $(seq 500); do
11136                 cat $DIR/$tfile
11137         done
11138
11139         echo 3 > /proc/sys/vm/drop_caches
11140         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11141
11142         echo "before: $req_before, after: $req_after"
11143         [ $((req_after - req_before)) -ge 300 ] &&
11144                 error "open/close requests are not freed"
11145         return 0
11146 }
11147 run_test 209 "read-only open/close requests should be freed promptly"
11148
11149 test_212() {
11150         size=`date +%s`
11151         size=$((size % 8192 + 1))
11152         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11153         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11154         rm -f $DIR/f212 $DIR/f212.xyz
11155 }
11156 run_test 212 "Sendfile test ============================================"
11157
11158 test_213() {
11159         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11160         cancel_lru_locks osc
11161         lctl set_param fail_loc=0x8000040f
11162         # generate a read lock
11163         cat $DIR/$tfile > /dev/null
11164         # write to the file, it will try to cancel the above read lock.
11165         cat /etc/hosts >> $DIR/$tfile
11166 }
11167 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11168
11169 test_214() { # for bug 20133
11170         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11171         for (( i=0; i < 340; i++ )) ; do
11172                 touch $DIR/$tdir/d214c/a$i
11173         done
11174
11175         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11176         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11177         ls $DIR/d214c || error "ls $DIR/d214c failed"
11178         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11179         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11180 }
11181 run_test 214 "hash-indexed directory test - bug 20133"
11182
11183 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11184 create_lnet_proc_files() {
11185         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11186         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11187
11188         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11189         rm -f "$TMP/lnet_$1.sys_tmp"
11190 }
11191
11192 # counterpart of create_lnet_proc_files
11193 remove_lnet_proc_files() {
11194         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11195 }
11196
11197 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11198 # 3rd arg as regexp for body
11199 check_lnet_proc_stats() {
11200         local l=$(cat "$TMP/lnet_$1" |wc -l)
11201         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11202
11203         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11204 }
11205
11206 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11207 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11208 # optional and can be regexp for 2nd line (lnet.routes case)
11209 check_lnet_proc_entry() {
11210         local blp=2          # blp stands for 'position of 1st line of body'
11211         [ -z "$5" ] || blp=3 # lnet.routes case
11212
11213         local l=$(cat "$TMP/lnet_$1" |wc -l)
11214         # subtracting one from $blp because the body can be empty
11215         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11216
11217         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11218                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11219
11220         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11221                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11222
11223         # bail out if any unexpected line happened
11224         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11225         [ "$?" != 0 ] || error "$2 misformatted"
11226 }
11227
11228 test_215() { # for bugs 18102, 21079, 21517
11229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11230         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11231         local P='[1-9][0-9]*'           # positive numeric
11232         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11233         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11234         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11235         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11236
11237         local L1 # regexp for 1st line
11238         local L2 # regexp for 2nd line (optional)
11239         local BR # regexp for the rest (body)
11240
11241         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11242         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11243         create_lnet_proc_files "stats"
11244         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11245         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11246         remove_lnet_proc_files "stats"
11247
11248         # /proc/sys/lnet/routes should look like this:
11249         # Routing disabled/enabled
11250         # net hops priority state router
11251         # where net is a string like tcp0, hops > 0, priority >= 0,
11252         # state is up/down,
11253         # router is a string like 192.168.1.1@tcp2
11254         L1="^Routing (disabled|enabled)$"
11255         L2="^net +hops +priority +state +router$"
11256         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11257         create_lnet_proc_files "routes"
11258         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11259         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11260         remove_lnet_proc_files "routes"
11261
11262         # /proc/sys/lnet/routers should look like this:
11263         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11264         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11265         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11266         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11267         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11268         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11269         create_lnet_proc_files "routers"
11270         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11271         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11272         remove_lnet_proc_files "routers"
11273
11274         # /proc/sys/lnet/peers should look like this:
11275         # nid refs state last max rtr min tx min queue
11276         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11277         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11278         # numeric (0 or >0 or <0), queue >= 0.
11279         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11280         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11281         create_lnet_proc_files "peers"
11282         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11283         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11284         remove_lnet_proc_files "peers"
11285
11286         # /proc/sys/lnet/buffers  should look like this:
11287         # pages count credits min
11288         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11289         L1="^pages +count +credits +min$"
11290         BR="^ +$N +$N +$I +$I$"
11291         create_lnet_proc_files "buffers"
11292         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11293         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11294         remove_lnet_proc_files "buffers"
11295
11296         # /proc/sys/lnet/nis should look like this:
11297         # nid status alive refs peer rtr max tx min
11298         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11299         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11300         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11301         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11302         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11303         create_lnet_proc_files "nis"
11304         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11305         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11306         remove_lnet_proc_files "nis"
11307
11308         # can we successfully write to /proc/sys/lnet/stats?
11309         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11310         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11311 }
11312 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11313
11314 test_216() { # bug 20317
11315         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11316         remote_ost_nodsh && skip "remote OST with nodsh" && return
11317
11318         local node
11319         local facets=$(get_facets OST)
11320         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11321
11322         save_lustre_params client "osc.*.contention_seconds" > $p
11323         save_lustre_params $facets \
11324                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11325         save_lustre_params $facets \
11326                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11327         save_lustre_params $facets \
11328                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11329         clear_osc_stats
11330
11331         # agressive lockless i/o settings
11332         for node in $(osts_nodes); do
11333                 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'
11334         done
11335         lctl set_param -n osc.*.contention_seconds 60
11336
11337         $DIRECTIO write $DIR/$tfile 0 10 4096
11338         $CHECKSTAT -s 40960 $DIR/$tfile
11339
11340         # disable lockless i/o
11341         for node in $(osts_nodes); do
11342                 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'
11343         done
11344         lctl set_param -n osc.*.contention_seconds 0
11345         clear_osc_stats
11346
11347         dd if=/dev/zero of=$DIR/$tfile count=0
11348         $CHECKSTAT -s 0 $DIR/$tfile
11349
11350         restore_lustre_params <$p
11351         rm -f $p
11352         rm $DIR/$tfile
11353 }
11354 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11355
11356 test_217() { # bug 22430
11357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11358         local node
11359         local nid
11360
11361         for node in $(nodes_list); do
11362                 nid=$(host_nids_address $node $NETTYPE)
11363                 if [[ $nid = *-* ]] ; then
11364                         echo "lctl ping $nid@$NETTYPE"
11365                         lctl ping $nid@$NETTYPE
11366                 else
11367                         echo "skipping $node (no hyphen detected)"
11368                 fi
11369         done
11370 }
11371 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11372
11373 test_218() {
11374        # do directio so as not to populate the page cache
11375        log "creating a 10 Mb file"
11376        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11377        log "starting reads"
11378        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11379        log "truncating the file"
11380        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11381        log "killing dd"
11382        kill %+ || true # reads might have finished
11383        echo "wait until dd is finished"
11384        wait
11385        log "removing the temporary file"
11386        rm -rf $DIR/$tfile || error "tmp file removal failed"
11387 }
11388 run_test 218 "parallel read and truncate should not deadlock ======================="
11389
11390 test_219() {
11391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11392         # write one partial page
11393         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11394         # set no grant so vvp_io_commit_write will do sync write
11395         $LCTL set_param fail_loc=0x411
11396         # write a full page at the end of file
11397         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11398
11399         $LCTL set_param fail_loc=0
11400         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11401         $LCTL set_param fail_loc=0x411
11402         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11403 }
11404 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11405
11406 test_220() { #LU-325
11407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11408         remote_ost_nodsh && skip "remote OST with nodsh" && return
11409         local OSTIDX=0
11410
11411         test_mkdir -p $DIR/$tdir
11412         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11413                 awk '{print $2}' | sed -e 's/_UUID$//')
11414
11415         # on the mdt's osc
11416         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11417         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11418                         osc.$mdtosc_proc1.prealloc_last_id)
11419         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11420                         osc.$mdtosc_proc1.prealloc_next_id)
11421
11422         $LFS df -i
11423
11424         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11425         #define OBD_FAIL_OST_ENOINO              0x229
11426         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11427         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11428         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11429
11430         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11431
11432         MDSOBJS=$((last_id - next_id))
11433         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11434
11435         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11436         echo "OST still has $count kbytes free"
11437
11438         echo "create $MDSOBJS files @next_id..."
11439         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11440
11441         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11442                         osc.$mdtosc_proc1.prealloc_last_id)
11443         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11444                         osc.$mdtosc_proc1.prealloc_next_id)
11445
11446         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11447         $LFS df -i
11448
11449         echo "cleanup..."
11450
11451         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11452         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11453
11454         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11455         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11456         echo "unlink $MDSOBJS files @$next_id..."
11457         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11458 }
11459 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11460
11461 test_221() {
11462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11463         dd if=`which date` of=$MOUNT/date oflag=sync
11464         chmod +x $MOUNT/date
11465
11466         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11467         $LCTL set_param fail_loc=0x80001401
11468
11469         $MOUNT/date > /dev/null
11470         rm -f $MOUNT/date
11471 }
11472 run_test 221 "make sure fault and truncate race to not cause OOM"
11473
11474 test_222a () {
11475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11476        rm -rf $DIR/$tdir
11477        test_mkdir -p $DIR/$tdir
11478        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11479        createmany -o $DIR/$tdir/$tfile 10
11480        cancel_lru_locks mdc
11481        cancel_lru_locks osc
11482        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11483        $LCTL set_param fail_loc=0x31a
11484        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11485        $LCTL set_param fail_loc=0
11486        rm -r $DIR/$tdir
11487 }
11488 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11489
11490 test_222b () {
11491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11492        rm -rf $DIR/$tdir
11493        test_mkdir -p $DIR/$tdir
11494        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11495        createmany -o $DIR/$tdir/$tfile 10
11496        cancel_lru_locks mdc
11497        cancel_lru_locks osc
11498        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11499        $LCTL set_param fail_loc=0x31a
11500        rm -r $DIR/$tdir || "AGL for rmdir failed"
11501        $LCTL set_param fail_loc=0
11502 }
11503 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11504
11505 test_223 () {
11506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11507        rm -rf $DIR/$tdir
11508        test_mkdir -p $DIR/$tdir
11509        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11510        createmany -o $DIR/$tdir/$tfile 10
11511        cancel_lru_locks mdc
11512        cancel_lru_locks osc
11513        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11514        $LCTL set_param fail_loc=0x31b
11515        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11516        $LCTL set_param fail_loc=0
11517        rm -r $DIR/$tdir
11518 }
11519 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11520
11521 test_224a() { # LU-1039, MRP-303
11522         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11523         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11524         $LCTL set_param fail_loc=0x508
11525         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11526         $LCTL set_param fail_loc=0
11527         df $DIR
11528 }
11529 run_test 224a "Don't panic on bulk IO failure"
11530
11531 test_224b() { # LU-1039, MRP-303
11532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11533         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11534         cancel_lru_locks osc
11535         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11536         $LCTL set_param fail_loc=0x515
11537         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11538         $LCTL set_param fail_loc=0
11539         df $DIR
11540 }
11541 run_test 224b "Don't panic on bulk IO failure"
11542
11543 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11544 test_225a () {
11545         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11546         if [ -z ${MDSSURVEY} ]; then
11547               skip_env "mds-survey not found" && return
11548         fi
11549
11550         [ $MDSCOUNT -ge 2 ] &&
11551                 skip "skipping now for more than one MDT" && return
11552
11553        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11554             { skip "Need MDS version at least 2.2.51"; return; }
11555
11556        local mds=$(facet_host $SINGLEMDS)
11557        local target=$(do_nodes $mds 'lctl dl' | \
11558                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11559
11560        local cmd1="file_count=1000 thrhi=4"
11561        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11562        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11563        local cmd="$cmd1 $cmd2 $cmd3"
11564
11565        rm -f ${TMP}/mds_survey*
11566        echo + $cmd
11567        eval $cmd || error "mds-survey with zero-stripe failed"
11568        cat ${TMP}/mds_survey*
11569        rm -f ${TMP}/mds_survey*
11570 }
11571 run_test 225a "Metadata survey sanity with zero-stripe"
11572
11573 test_225b () {
11574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11575
11576         if [ -z ${MDSSURVEY} ]; then
11577               skip_env "mds-survey not found" && return
11578         fi
11579         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11580             { skip "Need MDS version at least 2.2.51"; return; }
11581
11582         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11583               skip_env "Need to mount OST to test" && return
11584         fi
11585
11586         [ $MDSCOUNT -ge 2 ] &&
11587                 skip "skipping now for more than one MDT" && return
11588        local mds=$(facet_host $SINGLEMDS)
11589        local target=$(do_nodes $mds 'lctl dl' | \
11590                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11591
11592        local cmd1="file_count=1000 thrhi=4"
11593        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11594        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11595        local cmd="$cmd1 $cmd2 $cmd3"
11596
11597        rm -f ${TMP}/mds_survey*
11598        echo + $cmd
11599        eval $cmd || error "mds-survey with stripe_count failed"
11600        cat ${TMP}/mds_survey*
11601        rm -f ${TMP}/mds_survey*
11602 }
11603 run_test 225b "Metadata survey sanity with stripe_count = 1"
11604
11605 mcreate_path2fid () {
11606         local mode=$1
11607         local major=$2
11608         local minor=$3
11609         local name=$4
11610         local desc=$5
11611         local path=$DIR/$tdir/$name
11612         local fid
11613         local rc
11614         local fid_path
11615
11616         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11617                 error "cannot create $desc"
11618
11619         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11620         rc=$?
11621         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11622
11623         fid_path=$($LFS fid2path $MOUNT $fid)
11624         rc=$?
11625         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11626
11627         [ "$path" == "$fid_path" ] ||
11628                 error "fid2path returned $fid_path, expected $path"
11629
11630         echo "pass with $path and $fid"
11631 }
11632
11633 test_226a () {
11634         rm -rf $DIR/$tdir
11635         mkdir -p $DIR/$tdir
11636
11637         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11638         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11639         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11640         mcreate_path2fid 0040666 0 0 dir "directory"
11641         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11642         mcreate_path2fid 0100666 0 0 file "regular file"
11643         mcreate_path2fid 0120666 0 0 link "symbolic link"
11644         mcreate_path2fid 0140666 0 0 sock "socket"
11645 }
11646 run_test 226a "call path2fid and fid2path on files of all type"
11647
11648 test_226b () {
11649         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11650         rm -rf $DIR/$tdir
11651         local MDTIDX=1
11652
11653         mkdir -p $DIR/$tdir
11654         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11655                 error "create remote directory failed"
11656         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11657         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11658                                 "character special file (null)"
11659         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11660                                 "character special file (no device)"
11661         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11662         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11663                                 "block special file (loop)"
11664         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11665         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11666         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11667 }
11668 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11669
11670 # LU-1299 Executing or running ldd on a truncated executable does not
11671 # cause an out-of-memory condition.
11672 test_227() {
11673         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11674         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
11675         dd if=$(which date) of=$MOUNT/date bs=1k count=1
11676         chmod +x $MOUNT/date
11677
11678         $MOUNT/date > /dev/null
11679         ldd $MOUNT/date > /dev/null
11680         rm -f $MOUNT/date
11681 }
11682 run_test 227 "running truncated executable does not cause OOM"
11683
11684 # LU-1512 try to reuse idle OI blocks
11685 test_228a() {
11686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11687         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11688                 skip "non-ldiskfs backend" && return
11689
11690         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11691         local myDIR=$DIR/$tdir
11692
11693         mkdir -p $myDIR
11694         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11695         $LCTL set_param fail_loc=0x80001002
11696         createmany -o $myDIR/t- 10000
11697         $LCTL set_param fail_loc=0
11698         # The guard is current the largest FID holder
11699         touch $myDIR/guard
11700         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11701                     tr -d '[')
11702         local IDX=$(($SEQ % 64))
11703
11704         do_facet $SINGLEMDS sync
11705         # Make sure journal flushed.
11706         sleep 6
11707         local blk1=$(do_facet $SINGLEMDS \
11708                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11709                      grep Blockcount | awk '{print $4}')
11710
11711         # Remove old files, some OI blocks will become idle.
11712         unlinkmany $myDIR/t- 10000
11713         # Create new files, idle OI blocks should be reused.
11714         createmany -o $myDIR/t- 2000
11715         do_facet $SINGLEMDS sync
11716         # Make sure journal flushed.
11717         sleep 6
11718         local blk2=$(do_facet $SINGLEMDS \
11719                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11720                      grep Blockcount | awk '{print $4}')
11721
11722         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11723 }
11724 run_test 228a "try to reuse idle OI blocks"
11725
11726 test_228b() {
11727         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11728         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11729                 skip "non-ldiskfs backend" && return
11730
11731         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11732         local myDIR=$DIR/$tdir
11733
11734         mkdir -p $myDIR
11735         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11736         $LCTL set_param fail_loc=0x80001002
11737         createmany -o $myDIR/t- 10000
11738         $LCTL set_param fail_loc=0
11739         # The guard is current the largest FID holder
11740         touch $myDIR/guard
11741         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11742                     tr -d '[')
11743         local IDX=$(($SEQ % 64))
11744
11745         do_facet $SINGLEMDS sync
11746         # Make sure journal flushed.
11747         sleep 6
11748         local blk1=$(do_facet $SINGLEMDS \
11749                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11750                      grep Blockcount | awk '{print $4}')
11751
11752         # Remove old files, some OI blocks will become idle.
11753         unlinkmany $myDIR/t- 10000
11754
11755         # stop the MDT
11756         stop $SINGLEMDS || error "Fail to stop MDT."
11757         # remount the MDT
11758         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11759
11760         df $MOUNT || error "Fail to df."
11761         # Create new files, idle OI blocks should be reused.
11762         createmany -o $myDIR/t- 2000
11763         do_facet $SINGLEMDS sync
11764         # Make sure journal flushed.
11765         sleep 6
11766         local blk2=$(do_facet $SINGLEMDS \
11767                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11768                      grep Blockcount | awk '{print $4}')
11769
11770         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11771 }
11772 run_test 228b "idle OI blocks can be reused after MDT restart"
11773
11774 #LU-1881
11775 test_228c() {
11776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11777         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11778                 skip "non-ldiskfs backend" && return
11779
11780         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11781         local myDIR=$DIR/$tdir
11782
11783         mkdir -p $myDIR
11784         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11785         $LCTL set_param fail_loc=0x80001002
11786         # 20000 files can guarantee there are index nodes in the OI file
11787         createmany -o $myDIR/t- 20000
11788         $LCTL set_param fail_loc=0
11789         # The guard is current the largest FID holder
11790         touch $myDIR/guard
11791         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11792                     tr -d '[')
11793         local IDX=$(($SEQ % 64))
11794
11795         do_facet $SINGLEMDS sync
11796         # Make sure journal flushed.
11797         sleep 6
11798         local blk1=$(do_facet $SINGLEMDS \
11799                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11800                      grep Blockcount | awk '{print $4}')
11801
11802         # Remove old files, some OI blocks will become idle.
11803         unlinkmany $myDIR/t- 20000
11804         rm -f $myDIR/guard
11805         # The OI file should become empty now
11806
11807         # Create new files, idle OI blocks should be reused.
11808         createmany -o $myDIR/t- 2000
11809         do_facet $SINGLEMDS sync
11810         # Make sure journal flushed.
11811         sleep 6
11812         local blk2=$(do_facet $SINGLEMDS \
11813                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11814                      grep Blockcount | awk '{print $4}')
11815
11816         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11817 }
11818 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
11819
11820 test_229() { # LU-2482, LU-3448
11821         local server_version=$(get_lustre_version $SINGLEMDS)
11822         [[ $(version_code $server_version) -lt $(version_code 2.4.53) ]] &&
11823                 skip "No HSM support on $server_version MDS," \
11824                         "need 2.4.53 at least" && return
11825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11826         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
11827
11828         rm -f $DIR/$tfile
11829
11830         # Create a file with a released layout and stripe count 2.
11831         $MULTIOP $DIR/$tfile H2c ||
11832                 error "failed to create file with released layout"
11833
11834         $GETSTRIPE -v $DIR/$tfile
11835
11836         local pattern=$($GETSTRIPE -L $DIR/$tfile)
11837         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
11838
11839         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
11840         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
11841         stat $DIR/$tfile || error "failed to stat released file"
11842
11843         chown $RUNAS_ID $DIR/$tfile ||
11844                 error "chown $RUNAS_ID $DIR/$tfile failed"
11845
11846         chgrp $RUNAS_ID $DIR/$tfile ||
11847                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
11848
11849         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
11850         rm $DIR/$tfile || error "failed to remove released file"
11851 }
11852 run_test 229 "getstripe/stat/rm/attr changes work on released files"
11853
11854 test_230a() {
11855         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11856         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11857         local MDTIDX=1
11858
11859         mkdir -p $DIR/$tdir/test_230_local
11860         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
11861         [ $mdt_idx -ne 0 ] &&
11862                 error "create local directory on wrong MDT $mdt_idx"
11863
11864         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
11865                         error "create remote directory failed"
11866         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
11867         [ $mdt_idx -ne $MDTIDX ] &&
11868                 error "create remote directory on wrong MDT $mdt_idx"
11869
11870         createmany -o $DIR/$tdir/test_230/t- 10 ||
11871                 error "create files on remote directory failed"
11872         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
11873         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
11874         rm -r $DIR/$tdir || error "unlink remote directory failed"
11875 }
11876 run_test 230a "Create remote directory and files under the remote directory"
11877
11878 test_230b() {
11879         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11880         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11881         local MDTIDX=1
11882         local remote_dir=$DIR/$tdir/remote_dir
11883         local rc=0
11884
11885         mkdir -p $DIR/$tdir
11886         $LFS mkdir -i $MDTIDX $remote_dir ||
11887                 error "create remote directory failed"
11888
11889         $LFS mkdir -i 0 $remote_dir/new_dir &&
11890                 error "nested remote directory create succeed!"
11891
11892         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=1
11893         $LFS mkdir -i 0 $remote_dir/new_dir || rc=$?
11894         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=0
11895
11896         [ $rc -ne 0 ] &&
11897            error "create remote directory failed after set enable_remote_dir"
11898
11899         rm -rf $remote_dir || error "first unlink remote directory failed"
11900
11901         $RUNAS -G$RUNAS_GID $LFS mkdir -i $MDTIDX $DIR/$tfile &&
11902                                                         error "chown worked"
11903
11904         do_facet mds$MDTIDX lctl set_param \
11905                                 mdt.*.enable_remote_dir_gid=$RUNAS_GID
11906         $LFS mkdir -i $MDTIDX $remote_dir || rc=$?
11907         do_facet mds$MDTIDX lctl set_param mdt.*.enable_remote_dir_gid=0
11908
11909         [ $rc -ne 0 ] &&
11910            error "create remote dir failed after set enable_remote_dir_gid"
11911
11912         rm -r $DIR/$tdir || error "second unlink remote directory failed"
11913 }
11914 run_test 230b "nested remote directory should be failed"
11915
11916 test_231a()
11917 {
11918         # For simplicity this test assumes that max_pages_per_rpc
11919         # is the same across all OSCs
11920         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
11921         local bulk_size=$((max_pages * 4096))
11922
11923         mkdir -p $DIR/$tdir
11924
11925         # clear the OSC stats
11926         $LCTL set_param osc.*.stats=0 &>/dev/null
11927
11928         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
11929         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
11930                 oflag=direct &>/dev/null || error "dd failed"
11931
11932         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
11933         if [ x$nrpcs != "x1" ]; then
11934                 error "found $nrpc ost_write RPCs, not 1 as expected"
11935         fi
11936
11937         # Drop the OSC cache, otherwise we will read from it
11938         cancel_lru_locks osc
11939
11940         # clear the OSC stats
11941         $LCTL set_param osc.*.stats=0 &>/dev/null
11942
11943         # Client reads $bulk_size.
11944         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
11945                 iflag=direct &>/dev/null || error "dd failed"
11946
11947         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
11948         if [ x$nrpcs != "x1" ]; then
11949                 error "found $nrpc ost_read RPCs, not 1 as expected"
11950         fi
11951 }
11952 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
11953
11954 test_231b() {
11955         mkdir -p $DIR/$tdir
11956         local i
11957         for i in {0..1023}; do
11958                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
11959                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
11960                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
11961         done
11962         sync
11963 }
11964 run_test 231b "must not assert on fully utilized OST request buffer"
11965
11966 test_232() {
11967         mkdir -p $DIR/$tdir
11968         #define OBD_FAIL_LDLM_OST_LVB            0x31c
11969         $LCTL set_param fail_loc=0x31c
11970
11971         # ignore dd failure
11972         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
11973
11974         $LCTL set_param fail_loc=0
11975         umount_client $MOUNT || error "umount failed"
11976         mount_client $MOUNT || error "mount failed"
11977 }
11978 run_test 232 "failed lock should not block umount"
11979
11980 test_233a() {
11981         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
11982         { skip "Need MDS version at least 2.3.64"; return; }
11983
11984         local fid=$($LFS path2fid $MOUNT)
11985         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
11986                 error "cannot access $MOUNT using its FID '$fid'"
11987 }
11988 run_test 233a "checking that OBF of the FS root succeeds"
11989
11990 test_233b() {
11991         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
11992         { skip "Need MDS version at least 2.5.90"; return; }
11993
11994         local fid=$($LFS path2fid $MOUNT/.lustre)
11995         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
11996                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
11997
11998         fid=$($LFS path2fid $MOUNT/.lustre/fid)
11999         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12000                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12001 }
12002 run_test 233b "checking that OBF of the FS .lustre succeeds"
12003
12004 test_234() {
12005         local p="$TMP/sanityN-$TESTNAME.parameters"
12006         save_lustre_params client "llite.*.xattr_cache" > $p
12007         lctl set_param llite.*.xattr_cache 1 ||
12008                 { skip "xattr cache is not supported"; return 0; }
12009
12010         mkdir -p $DIR/$tdir || error "mkdir failed"
12011         touch $DIR/$tdir/$tfile || error "touch failed"
12012         # OBD_FAIL_LLITE_XATTR_ENOMEM
12013         $LCTL set_param fail_loc=0x1405
12014         if [ ! -f /etc/SuSE-release ]; then
12015                 # attr pre-2.4.44-7 had a bug with rc
12016                 # LU-3703 - SLES clients have older attr
12017                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12018                         error "getfattr should have failed with ENOMEM"
12019         fi
12020         $LCTL set_param fail_loc=0x0
12021         rm -rf $DIR/$tdir
12022
12023         restore_lustre_params < $p
12024         rm -f $p
12025 }
12026 run_test 234 "xattr cache should not crash on ENOMEM"
12027
12028 test_235() {
12029         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12030                 skip "Need MDS version at least 2.4.52" && return
12031         flock_deadlock $DIR/$tfile
12032         local RC=$?
12033         case $RC in
12034                 0)
12035                 ;;
12036                 124) error "process hangs on a deadlock"
12037                 ;;
12038                 *) error "error executing flock_deadlock $DIR/$tfile"
12039                 ;;
12040         esac
12041 }
12042 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12043
12044 #LU-2935
12045 test_236() {
12046         check_swap_layouts_support && return 0
12047         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12048
12049         local ref1=/etc/passwd
12050         local ref2=/etc/group
12051         local file1=$DIR/$tdir/f1
12052         local file2=$DIR/$tdir/f2
12053
12054         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12055         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12056         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12057         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12058         local fd=$(free_fd)
12059         local cmd="exec $fd<>$file2"
12060         eval $cmd
12061         rm $file2
12062         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12063                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12064         cmd="exec $fd>&-"
12065         eval $cmd
12066         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12067
12068         #cleanup
12069         rm -rf $DIR/$tdir
12070 }
12071 run_test 236 "Layout swap on open unlinked file"
12072
12073 # LU-4659 linkea consistency
12074 test_238() {
12075         touch $DIR/$tfile
12076         ln $DIR/$tfile $DIR/$tfile.lnk
12077         touch $DIR/$tfile.new
12078         mv $DIR/$tfile.new $DIR/$tfile
12079         local fid1=$(lfs path2fid $DIR/$tfile)
12080         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12081         local path1=$(lfs fid2path $FSNAME $fid1)
12082         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12083         local path2=$(lfs fid2path $FSNAME $fid2)
12084         [ $tfile.lnk == $path2 ] ||
12085                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12086         rm -f $DIR/$tfile*
12087 }
12088 run_test 238 "Verify linkea consistency"
12089 #
12090 # tests that do cleanup/setup should be run at the end
12091 #
12092
12093 test_900() {
12094         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12095         local ls
12096         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
12097         $LCTL set_param fail_loc=0x903
12098         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
12099         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
12100                 echo "clear" > $ls
12101         done
12102         FAIL_ON_ERROR=true cleanup
12103         FAIL_ON_ERROR=true setup
12104 }
12105 run_test 900 "umount should not race with any mgc requeue thread"
12106
12107 complete $SECONDS
12108 check_and_cleanup_lustre
12109 if [ "$I_MOUNTED" != "yes" ]; then
12110         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
12111 fi
12112 exit_status