Whamcloud - gitweb
LU-5068 mdt: check default LMV EA when set reply EA size
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19 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 CC=${CC:-cc}
42 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
43 CREATETEST=${CREATETEST:-createtest}
44 LFS=${LFS:-lfs}
45 LFIND=${LFIND:-"$LFS find"}
46 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
47 LCTL=${LCTL:-lctl}
48 MCREATE=${MCREATE:-mcreate}
49 OPENFILE=${OPENFILE:-openfile}
50 OPENUNLINK=${OPENUNLINK:-openunlink}
51 export MULTIOP=${MULTIOP:-multiop}
52 READS=${READS:-"reads"}
53 MUNLINK=${MUNLINK:-munlink}
54 SOCKETSERVER=${SOCKETSERVER:-socketserver}
55 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
56 MEMHOG=${MEMHOG:-memhog}
57 DIRECTIO=${DIRECTIO:-directio}
58 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
59 UMOUNT=${UMOUNT:-"umount -d"}
60 STRIPES_PER_OBJ=-1
61 CHECK_GRANT=${CHECK_GRANT:-"yes"}
62 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
63 export PARALLEL=${PARALLEL:-"no"}
64
65 export NAME=${NAME:-local}
66
67 SAVE_PWD=$PWD
68
69 CLEANUP=${CLEANUP:-:}
70 SETUP=${SETUP:-:}
71 TRACE=${TRACE:-""}
72 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
73 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
74 . $LUSTRE/tests/test-framework.sh
75 init_test_env $@
76 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
77 init_logging
78
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b 230d"
80
81 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
82 # bug number for skipped test:        LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     40      48a     180     184c"
84
85 FAIL_ON_ERROR=false
86
87 cleanup() {
88         echo -n "cln.."
89         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
90         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
91 }
92 setup() {
93         echo -n "mnt.."
94         load_modules
95         setupall || exit 10
96         echo "done"
97 }
98
99 check_kernel_version() {
100         WANT_VER=$1
101         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
102         case $GOT_VER in
103         patchless|patchless_client) return 0;;
104         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
105         esac
106         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
107         return 1
108 }
109
110 check_swap_layouts_support()
111 {
112         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
113                 { skip "Does not support layout lock."; return 0; }
114         return 1
115 }
116
117 if [ "$ONLY" == "cleanup" ]; then
118        sh llmountcleanup.sh
119        exit 0
120 fi
121
122 check_and_setup_lustre
123
124 DIR=${DIR:-$MOUNT}
125 assert_DIR
126
127 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
128         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
129 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
130 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
131 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
132 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
133 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
134 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
135
136 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
137 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
138 rm -rf $DIR/[Rdfs][0-9]*
139
140 # $RUNAS_ID may get set incorrectly somewhere else
141 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
142
143 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
144
145 build_test_filter
146
147 if [ "${ONLY}" = "MOUNT" ] ; then
148         echo "Lustre is up, please go on"
149         exit
150 fi
151
152 echo "preparing for tests involving mounts"
153 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
154 touch $EXT2_DEV
155 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
156 echo # add a newline after mke2fs.
157
158 umask 077
159
160 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
161 lctl set_param debug=-1 2> /dev/null || true
162 test_0a() {
163         touch $DIR/$tfile
164         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
165         rm $DIR/$tfile
166         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
167 }
168 run_test 0a "touch; rm ====================="
169
170 test_0b() {
171         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
172         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
173 }
174 run_test 0b "chmod 0755 $DIR ============================="
175
176 test_0c() {
177         $LCTL get_param mdc.*.import | grep "state: FULL" ||
178                 error "import not FULL"
179         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
180                 error "bad target"
181 }
182 run_test 0c "check import proc ============================="
183
184 test_1() {
185         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
186         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
187         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
188         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
189         rmdir $DIR/$tdir/d2
190         rmdir $DIR/$tdir
191         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
192 }
193 run_test 1 "mkdir; remkdir; rmdir =============================="
194
195 test_2() {
196         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
197         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
198         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
199         rm -r $DIR/$tdir
200         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
201 }
202 run_test 2 "mkdir; touch; rmdir; check file ===================="
203
204 test_3() {
205         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
206         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
207         touch $DIR/$tdir/$tfile
208         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
209         rm -r $DIR/$tdir
210         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
211 }
212 run_test 3 "mkdir; touch; rmdir; check dir ====================="
213
214 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
215 test_4() {
216         local MDTIDX=1
217         local remote_dir=remote_dir
218
219         test_mkdir $DIR/$remote_dir ||
220                 error "Create remote directory failed"
221
222         touch $DIR/$remote_dir/$tfile ||
223                 error "Create file under remote directory failed"
224
225         rmdir $DIR/$remote_dir &&
226                 error "Expect error removing in-use dir $DIR/$remote_dir"
227
228         test -d $DIR/$remote_dir || error "Remote directory disappeared"
229
230         rm -rf $DIR/$remote_dir || error "remove remote dir error"
231 }
232 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
233
234 test_5() {
235         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
236         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
237         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
238         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
239         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
240 }
241 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
242
243 test_6a() {
244         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
245         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
246         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
247                 error "$tfile does not have perm 0666 or UID $UID"
248         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
249         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
250                 error "$tfile should be 0666 and owned by UID $UID"
251 }
252 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
253
254 test_6c() {
255         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
256         touch $DIR/$tfile
257         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
258         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
259                 error "$tfile should be owned by UID $RUNAS_ID"
260         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
261         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
262                 error "$tfile should be owned by UID $RUNAS_ID"
263 }
264 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
265
266 test_6e() {
267         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
268         touch $DIR/$tfile
269         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
270         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
271                 error "$tfile should be owned by GID $UID"
272         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
273         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
274                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
275 }
276 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
277
278 test_6g() {
279         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
280         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
281         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
282         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
283         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
284         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
285         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
286                 error "$tdir/d/subdir should be GID $RUNAS_GID"
287 }
288 run_test 6g "Is new dir in sgid dir inheriting group?"
289
290 test_6h() { # bug 7331
291         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
292         touch $DIR/$tfile || error "touch failed"
293         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
294         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
295                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
296         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
297                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
298 }
299 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
300
301 test_7a() {
302         test_mkdir $DIR/$tdir
303         $MCREATE $DIR/$tdir/$tfile
304         chmod 0666 $DIR/$tdir/$tfile
305         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
306                 error "$tdir/$tfile should be mode 0666"
307 }
308 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
309
310 test_7b() {
311         if [ ! -d $DIR/$tdir ]; then
312                 mkdir $DIR/$tdir
313         fi
314         $MCREATE $DIR/$tdir/$tfile
315         echo -n foo > $DIR/$tdir/$tfile
316         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
317         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
318 }
319 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
320
321 test_8() {
322         test_mkdir $DIR/$tdir
323         touch $DIR/$tdir/$tfile
324         chmod 0666 $DIR/$tdir/$tfile
325         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
326                 error "$tfile mode not 0666"
327 }
328 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
329
330 test_9() {
331         test_mkdir $DIR/$tdir
332         test_mkdir $DIR/$tdir/d2
333         test_mkdir $DIR/$tdir/d2/d3
334         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
335 }
336 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
337
338 test_10() {
339         test_mkdir $DIR/$tdir
340         test_mkdir $DIR/$tdir/d2
341         touch $DIR/$tdir/d2/$tfile
342         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
343                 error "$tdir/d2/$tfile not a file"
344 }
345 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
346
347 test_11() {
348         test_mkdir $DIR/$tdir
349         test_mkdir $DIR/$tdir/d2
350         chmod 0666 $DIR/$tdir/d2
351         chmod 0705 $DIR/$tdir/d2
352         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
353                 error "$tdir/d2 mode not 0705"
354 }
355 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
356
357 test_12() {
358         test_mkdir $DIR/$tdir
359         touch $DIR/$tdir/$tfile
360         chmod 0666 $DIR/$tdir/$tfile
361         chmod 0654 $DIR/$tdir/$tfile
362         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
363                 error "$tdir/d2 mode not 0654"
364 }
365 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
366
367 test_13() {
368         test_mkdir $DIR/$tdir
369         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
370         >  $DIR/$tdir/$tfile
371         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
372                 error "$tdir/$tfile size not 0 after truncate"
373 }
374 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
375
376 test_14() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
381 }
382 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
383
384 test_15() {
385         test_mkdir $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
388         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
389                 error "$tdir/${tfile_2} not a file after rename"
390 }
391 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
392
393 test_16() {
394         test_mkdir $DIR/$tdir
395         touch $DIR/$tdir/$tfile
396         rm -rf $DIR/$tdir/$tfile
397         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
398 }
399 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
400
401 test_17a() {
402         test_mkdir -p $DIR/$tdir
403         touch $DIR/$tdir/$tfile
404         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
405         ls -l $DIR/$tdir
406         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
407                 error "$tdir/l-exist not a symlink"
408         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
409                 error "$tdir/l-exist not referencing a file"
410         rm -f $DIR/$tdir/l-exist
411         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
412 }
413 run_test 17a "symlinks: create, remove (real) =================="
414
415 test_17b() {
416         test_mkdir -p $DIR/$tdir
417         ln -s no-such-file $DIR/$tdir/l-dangle
418         ls -l $DIR/$tdir
419         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
420                 error "$tdir/l-dangle not referencing no-such-file"
421         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
422                 error "$tdir/l-dangle not referencing non-existent file"
423         rm -f $DIR/$tdir/l-dangle
424         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
425 }
426 run_test 17b "symlinks: create, remove (dangling) =============="
427
428 test_17c() { # bug 3440 - don't save failed open RPC for replay
429         test_mkdir -p $DIR/$tdir
430         ln -s foo $DIR/$tdir/$tfile
431         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
432 }
433 run_test 17c "symlinks: open dangling (should return error) ===="
434
435 test_17d() {
436         test_mkdir -p $DIR/$tdir
437         ln -s foo $DIR/$tdir/$tfile
438         touch $DIR/$tdir/$tfile || error "creating to new symlink"
439 }
440 run_test 17d "symlinks: create dangling ========================"
441
442 test_17e() {
443         test_mkdir -p $DIR/$tdir
444         local foo=$DIR/$tdir/$tfile
445         ln -s $foo $foo || error "create symlink failed"
446         ls -l $foo || error "ls -l failed"
447         ls $foo && error "ls not failed" || true
448 }
449 run_test 17e "symlinks: create recursive symlink (should return error) ===="
450
451 test_17f() {
452         test_mkdir -p $DIR/d17f
453         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
455         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
456         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
457         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
458         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
459         ls -l  $DIR/d17f
460 }
461 run_test 17f "symlinks: long and very long symlink name ========================"
462
463 # str_repeat(S, N) generate a string that is string S repeated N times
464 str_repeat() {
465         local s=$1
466         local n=$2
467         local ret=''
468         while [ $((n -= 1)) -ge 0 ]; do
469                 ret=$ret$s
470         done
471         echo $ret
472 }
473
474 # Long symlinks and LU-2241
475 test_17g() {
476         test_mkdir -p $DIR/$tdir
477         local TESTS="59 60 61 4094 4095"
478
479         # Fix for inode size boundary in 2.1.4
480         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
481                 TESTS="4094 4095"
482
483         # Patch not applied to 2.2 or 2.3 branches
484         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
485         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
486                 TESTS="4094 4095"
487
488         # skip long symlink name for rhel6.5.
489         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
490         grep -q '6.5' /etc/redhat-release &>/dev/null &&
491                 TESTS="59 60 61 4062 4063"
492
493         for i in $TESTS; do
494                 local SYMNAME=$(str_repeat 'x' $i)
495                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
496                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
497         done
498 }
499 run_test 17g "symlinks: really long symlink name and inode boundaries"
500
501 test_17h() { #bug 17378
502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
504         local mdt_idx
505         test_mkdir -p $DIR/$tdir
506         if [[ $MDSCOUNT -gt 1 ]]; then
507                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
508         else
509                 mdt_idx=0
510         fi
511         $SETSTRIPE -c -1 $DIR/$tdir
512 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
513         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
514         touch $DIR/$tdir/$tfile || true
515 }
516 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
517
518 test_17i() { #bug 20018
519         remote_mds_nodsh && skip "remote MDS with nodsh" && return
520         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
521         test_mkdir -c1 $DIR/$tdir
522         local foo=$DIR/$tdir/$tfile
523         local mdt_idx
524         if [[ $MDSCOUNT -gt 1 ]]; then
525                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
526         else
527                 mdt_idx=0
528         fi
529         ln -s $foo $foo || error "create symlink failed"
530 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
531         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
532         ls -l $foo && error "error not detected"
533         return 0
534 }
535 run_test 17i "don't panic on short symlink"
536
537 test_17k() { #bug 22301
538         [[ -z "$(which rsync 2>/dev/null)" ]] &&
539                 skip "no rsync command" && return 0
540         rsync --help | grep -q xattr ||
541                 skip_env "$(rsync --version | head -n1) does not support xattrs"
542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
543         test_mkdir -p $DIR/$tdir
544         test_mkdir -p $DIR/$tdir.new
545         touch $DIR/$tdir/$tfile
546         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
547         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
548                 error "rsync failed with xattrs enabled"
549 }
550 run_test 17k "symlinks: rsync with xattrs enabled ========================="
551
552 test_17l() { # LU-279
553         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
554                 skip "no getfattr command" && return 0
555         mkdir -p $DIR/$tdir
556         touch $DIR/$tdir/$tfile
557         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
558         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
559                 # -h to not follow symlinks. -m '' to list all the xattrs.
560                 # grep to remove first line: '# file: $path'.
561                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
562                 do
563                         lgetxattr_size_check $path $xattr ||
564                                 error "lgetxattr_size_check $path $xattr failed"
565                 done
566         done
567 }
568 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
569
570 # LU-1540
571 test_17m() {
572         local short_sym="0123456789"
573         local WDIR=$DIR/${tdir}m
574         local mds_index
575         local devname
576         local cmd
577         local i
578         local rc=0
579
580         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
581         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
582                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
583
584         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
585                 skip "only for ldiskfs MDT" && return 0
586
587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
588
589         mkdir -p $WDIR
590         long_sym=$short_sym
591         # create a long symlink file
592         for ((i = 0; i < 4; ++i)); do
593                 long_sym=${long_sym}${long_sym}
594         done
595
596         echo "create 512 short and long symlink files under $WDIR"
597         for ((i = 0; i < 256; ++i)); do
598                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
599                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
600         done
601
602         echo "erase them"
603         rm -f $WDIR/*
604         sync
605         wait_delete_completed
606
607         echo "recreate the 512 symlink files with a shorter string"
608         for ((i = 0; i < 512; ++i)); do
609                 # rewrite the symlink file with a shorter string
610                 ln -sf ${long_sym} $WDIR/long-$i
611                 ln -sf ${short_sym} $WDIR/short-$i
612         done
613
614         mds_index=$($LFS getstripe -M $WDIR)
615         mds_index=$((mds_index+1))
616         devname=$(mdsdevname $mds_index)
617         cmd="$E2FSCK -fnvd $devname"
618
619         echo "stop and checking mds${mds_index}: $cmd"
620         # e2fsck should not return error
621         stop mds${mds_index}
622         do_facet mds${mds_index} $cmd || rc=$?
623
624         start mds${mds_index} $devname $MDS_MOUNT_OPTS
625         df $MOUNT > /dev/null 2>&1
626         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
627                 "short/long symlink MDT: rc=$rc"
628         return $rc
629 }
630 run_test 17m "run e2fsck against MDT which contains short/long symlink"
631
632 check_fs_consistency_17n() {
633         local mdt_index
634         local devname
635         local cmd
636         local rc=0
637
638         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
639         # so it only check MDT1/MDT2 instead of all of MDTs.
640         for mdt_index in $(seq 1 2); do
641                 devname=$(mdsdevname $mdt_index)
642                 cmd="$E2FSCK -fnvd $devname"
643
644                 echo "stop and checking mds${mdt_index}: $cmd"
645                 # e2fsck should not return error
646                 stop mds${mdt_index}
647                 do_facet mds${mdt_index} $cmd || rc=$?
648
649                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
650                 df $MOUNT > /dev/null 2>&1
651                 [ $rc -ne 0 ] && break
652         done
653         return $rc
654 }
655
656 test_17n() {
657         local i
658
659         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
660         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
661                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
662
663         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
664                 skip "only for ldiskfs MDT" && return 0
665
666         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
667
668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
669
670         mkdir $DIR/$tdir
671         for ((i=0; i<10; i++)); do
672                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
673                         error "create remote dir error $i"
674                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
675                         error "create files under remote dir failed $i"
676         done
677
678         check_fs_consistency_17n ||
679                 error "e2fsck report error after create files under remote dir"
680
681         for ((i=0;i<10;i++)); do
682                 rm -rf $DIR/$tdir/remote_dir_${i} ||
683                         error "destroy remote dir error $i"
684         done
685
686         check_fs_consistency_17n ||
687                 error "e2fsck report error after unlink files under remote dir"
688
689         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
690                 skip "lustre < 2.4.50 does not support migrate mv " && return
691
692         for ((i=0; i<10; i++)); do
693                 mkdir -p $DIR/$tdir/remote_dir_${i}
694                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
695                         error "create files under remote dir failed $i"
696                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
697                         error "migrate remote dir error $i"
698         done
699         check_fs_consistency_17n || error "e2fsck report error after migration"
700
701         for ((i=0;i<10;i++)); do
702                 rm -rf $DIR/$tdir/remote_dir_${i} ||
703                         error "destroy remote dir error $i"
704         done
705
706         check_fs_consistency_17n || error "e2fsck report error after unlink"
707 }
708 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
709
710 test_17o() {
711         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
712                 skip "Need MDS version at least 2.3.64" && return
713
714         local WDIR=$DIR/${tdir}o
715         local mdt_index
716         local mdtdevname
717         local rc=0
718
719         mkdir -p $WDIR
720         mdt_index=$($LFS getstripe -M $WDIR)
721         mdt_index=$((mdt_index+1))
722         mdtdevname=$(mdsdevname $mdt_index)
723
724         touch $WDIR/$tfile
725         stop mds${mdt_index}
726         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
727
728         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
729         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
730         ls -l $WDIR/$tfile && rc=1
731         do_facet mds${mdt_index} lctl set_param fail_loc=0
732         [[ $rc -ne 0 ]] && error "stat file should fail"
733         true
734 }
735 run_test 17o "stat file with incompat LMA feature"
736
737 test_18() {
738         touch $DIR/f || error "Failed to touch $DIR/f: $?"
739         ls $DIR || error "Failed to ls $DIR: $?"
740 }
741 run_test 18 "touch .../f ; ls ... =============================="
742
743 test_19a() {
744         touch $DIR/$tfile
745         ls -l $DIR
746         rm $DIR/$tfile
747         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
748 }
749 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
750
751 test_19b() {
752         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
753 }
754 run_test 19b "ls -l .../f19 (should return error) =============="
755
756 test_19c() {
757         [ $RUNAS_ID -eq $UID ] &&
758                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
759         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
760 }
761 run_test 19c "$RUNAS touch .../f19 (should return error) =="
762
763 test_19d() {
764         cat $DIR/f19 && error || true
765 }
766 run_test 19d "cat .../f19 (should return error) =============="
767
768 test_20() {
769         touch $DIR/$tfile
770         rm $DIR/$tfile
771         log "1 done"
772         touch $DIR/$tfile
773         rm $DIR/$tfile
774         log "2 done"
775         touch $DIR/$tfile
776         rm $DIR/$tfile
777         log "3 done"
778         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
779 }
780 run_test 20 "touch .../f ; ls -l ... ==========================="
781
782 test_21() {
783         test_mkdir -p $DIR/$tdir
784         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
785         ln -s dangle $DIR/$tdir/link
786         echo foo >> $DIR/$tdir/link
787         cat $DIR/$tdir/dangle
788         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
789         $CHECKSTAT -f -t file $DIR/$tdir/link ||
790                 error "$tdir/link not linked to a file"
791 }
792 run_test 21 "write to dangling link ============================"
793
794 test_22() {
795         WDIR=$DIR/$tdir
796         test_mkdir -p $DIR/$tdir
797         chown $RUNAS_ID:$RUNAS_GID $WDIR
798         (cd $WDIR || error "cd $WDIR failed";
799         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
800         $RUNAS tar xf -)
801         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
802         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
803         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
804 }
805 run_test 22 "unpack tar archive as non-root user ==============="
806
807 # was test_23
808 test_23a() {
809         test_mkdir -p $DIR/$tdir
810         local file=$DIR/$tdir/$tfile
811
812         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
813         openfile -f O_CREAT:O_EXCL $file &&
814                 error "$file recreate succeeded" || true
815 }
816 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
817
818 test_23b() { # bug 18988
819         test_mkdir -p $DIR/$tdir
820         local file=$DIR/$tdir/$tfile
821
822         rm -f $file
823         echo foo > $file || error "write filed"
824         echo bar >> $file || error "append filed"
825         $CHECKSTAT -s 8 $file || error "wrong size"
826         rm $file
827 }
828 run_test 23b "O_APPEND check =========================="
829
830 # rename sanity
831 test_24a() {
832         echo '-- same directory rename'
833         test_mkdir $DIR/$tdir
834         touch $DIR/$tdir/$tfile.1
835         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
836         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
837 }
838 run_test 24a "rename file to non-existent target"
839
840 test_24b() {
841         test_mkdir $DIR/$tdir
842         touch $DIR/$tdir/$tfile.{1,2}
843         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
844         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
845         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
846 }
847 run_test 24b "rename file to existing target"
848
849 test_24c() {
850         test_mkdir $DIR/$tdir
851         test_mkdir $DIR/$tdir/d$testnum.1
852         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
853         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
854         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
855 }
856 run_test 24c "rename directory to non-existent target"
857
858 test_24d() {
859         test_mkdir -c1 $DIR/$tdir
860         test_mkdir -c1 $DIR/$tdir/d$testnum.1
861         test_mkdir -c1 $DIR/$tdir/d$testnum.2
862         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
863         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
864         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
865 }
866 run_test 24d "rename directory to existing target"
867
868 test_24e() {
869         echo '-- cross directory renames --'
870         test_mkdir $DIR/R5a
871         test_mkdir $DIR/R5b
872         touch $DIR/R5a/f
873         mv $DIR/R5a/f $DIR/R5b/g
874         $CHECKSTAT -a $DIR/R5a/f || error
875         $CHECKSTAT -t file $DIR/R5b/g || error
876 }
877 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
878
879 test_24f() {
880         test_mkdir $DIR/R6a
881         test_mkdir $DIR/R6b
882         touch $DIR/R6a/f $DIR/R6b/g
883         mv $DIR/R6a/f $DIR/R6b/g
884         $CHECKSTAT -a $DIR/R6a/f || error
885         $CHECKSTAT -t file $DIR/R6b/g || error
886 }
887 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
888
889 test_24g() {
890         test_mkdir $DIR/R7a
891         test_mkdir $DIR/R7b
892         test_mkdir $DIR/R7a/d
893         mv $DIR/R7a/d $DIR/R7b/e
894         $CHECKSTAT -a $DIR/R7a/d || error
895         $CHECKSTAT -t dir $DIR/R7b/e || error
896 }
897 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
898
899 test_24h() {
900         test_mkdir -c1 $DIR/R8a
901         test_mkdir -c1 $DIR/R8b
902         test_mkdir -c1 $DIR/R8a/d
903         test_mkdir -c1 $DIR/R8b/e
904         mrename $DIR/R8a/d $DIR/R8b/e
905         $CHECKSTAT -a $DIR/R8a/d || error
906         $CHECKSTAT -t dir $DIR/R8b/e || error
907 }
908 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
909
910 test_24i() {
911         echo "-- rename error cases"
912         test_mkdir $DIR/R9
913         test_mkdir $DIR/R9/a
914         touch $DIR/R9/f
915         mrename $DIR/R9/f $DIR/R9/a
916         $CHECKSTAT -t file $DIR/R9/f || error
917         $CHECKSTAT -t dir  $DIR/R9/a || error
918         $CHECKSTAT -a $DIR/R9/a/f || error
919 }
920 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
921
922 test_24j() {
923         test_mkdir $DIR/R10
924         mrename $DIR/R10/f $DIR/R10/g
925         $CHECKSTAT -t dir $DIR/R10 || error
926         $CHECKSTAT -a $DIR/R10/f || error
927         $CHECKSTAT -a $DIR/R10/g || error
928 }
929 run_test 24j "source does not exist ============================"
930
931 test_24k() {
932         test_mkdir $DIR/R11a
933         test_mkdir $DIR/R11a/d
934         touch $DIR/R11a/f
935         mv $DIR/R11a/f $DIR/R11a/d
936         $CHECKSTAT -a $DIR/R11a/f || error
937         $CHECKSTAT -t file $DIR/R11a/d/f || error
938 }
939 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
940
941 # bug 2429 - rename foo foo foo creates invalid file
942 test_24l() {
943         f="$DIR/f24l"
944         $MULTIOP $f OcNs || error
945 }
946 run_test 24l "Renaming a file to itself ========================"
947
948 test_24m() {
949         f="$DIR/f24m"
950         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
951         # on ext3 this does not remove either the source or target files
952         # though the "expected" operation would be to remove the source
953         $CHECKSTAT -t file ${f} || error "${f} missing"
954         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
955 }
956 run_test 24m "Renaming a file to a hard link to itself ========="
957
958 test_24n() {
959     f="$DIR/f24n"
960     # this stats the old file after it was renamed, so it should fail
961     touch ${f}
962     $CHECKSTAT ${f}
963     mv ${f} ${f}.rename
964     $CHECKSTAT ${f}.rename
965     $CHECKSTAT -a ${f}
966 }
967 run_test 24n "Statting the old file after renaming (Posix rename 2)"
968
969 test_24o() {
970         check_kernel_version 37 || return 0
971         test_mkdir -p $DIR/d24o
972         rename_many -s random -v -n 10 $DIR/d24o
973 }
974 run_test 24o "rename of files during htree split ==============="
975
976 test_24p() {
977         test_mkdir $DIR/R12a
978         test_mkdir $DIR/R12b
979         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
980         mrename $DIR/R12a $DIR/R12b
981         $CHECKSTAT -a $DIR/R12a || error
982         $CHECKSTAT -t dir $DIR/R12b || error
983         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
984         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
985 }
986 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
987
988 cleanup_multiop_pause() {
989         trap 0
990         kill -USR1 $MULTIPID
991 }
992
993 test_24q() {
994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
995         test_mkdir $DIR/R13a
996         test_mkdir $DIR/R13b
997         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
998         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
999         MULTIPID=$!
1000
1001         trap cleanup_multiop_pause EXIT
1002         mrename $DIR/R13a $DIR/R13b
1003         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1004         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1005         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1006         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1007         cleanup_multiop_pause
1008         wait $MULTIPID || error "multiop close failed"
1009 }
1010 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1011
1012 test_24r() { #bug 3789
1013         test_mkdir $DIR/R14a
1014         test_mkdir $DIR/R14a/b
1015         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1016         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1017         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1018 }
1019 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1020
1021 test_24s() {
1022         test_mkdir $DIR/R15a
1023         test_mkdir $DIR/R15a/b
1024         test_mkdir $DIR/R15a/b/c
1025         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1026         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1027         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1028 }
1029 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1030 test_24t() {
1031         test_mkdir $DIR/R16a
1032         test_mkdir $DIR/R16a/b
1033         test_mkdir $DIR/R16a/b/c
1034         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1035         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1036         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1037 }
1038 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1039
1040 test_24u() { # bug12192
1041         rm -rf $DIR/$tfile
1042         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1043         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1044 }
1045 run_test 24u "create stripe file"
1046
1047 page_size() {
1048         getconf PAGE_SIZE
1049 }
1050
1051 simple_cleanup_common() {
1052         trap 0
1053         rm -rf $DIR/$tdir
1054         wait_delete_completed
1055 }
1056
1057 max_pages_per_rpc() {
1058         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1059 }
1060
1061 test_24v() {
1062         local NRFILES=100000
1063         local FREE_INODES=$(mdt_free_inodes 0)
1064         [[ $FREE_INODES -lt $NRFILES ]] &&
1065                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1066                 return
1067
1068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1069         trap simple_cleanup_common EXIT
1070
1071         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1072         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1073
1074         mkdir -p $DIR/$tdir
1075         createmany -m $DIR/$tdir/$tfile $NRFILES
1076
1077         cancel_lru_locks mdc
1078         lctl set_param mdc.*.stats clear
1079
1080         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1081
1082         # LU-5 large readdir
1083         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1084         #               8 bytes for name(filename is mostly 5 in this test) +
1085         #               8 bytes for luda_type
1086         # take into account of overhead in lu_dirpage header and end mark in
1087         # each page, plus one in RPC_NUM calculation.
1088         DIRENT_SIZE=48
1089         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1090         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1091         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1092                                 awk '/^mds_readpage/ {print $2}')
1093         [[ $mds_readpage -gt $RPC_NUM ]] &&
1094                 error "large readdir doesn't take effect"
1095
1096         simple_cleanup_common
1097 }
1098 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1099
1100 test_24w() { # bug21506
1101         SZ1=234852
1102         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1103         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1104         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1105         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1106         [ "$SZ1" = "$SZ2" ] || \
1107                 error "Error reading at the end of the file $tfile"
1108 }
1109 run_test 24w "Reading a file larger than 4Gb"
1110
1111 test_24x() {
1112         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1114         local MDTIDX=1
1115         local remote_dir=$DIR/$tdir/remote_dir
1116
1117         mkdir -p $DIR/$tdir
1118         $LFS mkdir -i $MDTIDX $remote_dir ||
1119                 error "create remote directory failed"
1120
1121         mkdir -p $DIR/$tdir/src_dir
1122         touch $DIR/$tdir/src_file
1123         mkdir -p $remote_dir/tgt_dir
1124         touch $remote_dir/tgt_file
1125
1126         mrename $remote_dir $DIR/ &&
1127                 error "rename dir cross MDT works!"
1128
1129         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1130                 error "rename dir cross MDT works!"
1131
1132         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1133                 error "rename file cross MDT works!"
1134
1135         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1136                 error "ln file cross MDT should not work!"
1137
1138         rm -rf $DIR/$tdir || error "Can not delete directories"
1139 }
1140 run_test 24x "cross rename/link should be failed"
1141
1142 test_24y() {
1143         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1145         local MDTIDX=1
1146         local remote_dir=$DIR/$tdir/remote_dir
1147
1148         mkdir -p $DIR/$tdir
1149         $LFS mkdir -i $MDTIDX $remote_dir ||
1150                    error "create remote directory failed"
1151
1152         mkdir -p $remote_dir/src_dir
1153         touch $remote_dir/src_file
1154         mkdir -p $remote_dir/tgt_dir
1155         touch $remote_dir/tgt_file
1156
1157         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1158                 error "rename subdir in the same remote dir failed!"
1159
1160         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1161                 error "rename files in the same remote dir failed!"
1162
1163         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1164                 error "link files in the same remote dir failed!"
1165
1166         rm -rf $DIR/$tdir || error "Can not delete directories"
1167 }
1168 run_test 24y "rename/link on the same dir should succeed"
1169
1170 test_24z() {
1171         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1173         local MDTIDX=1
1174         local remote_src=$DIR/$tdir/remote_dir
1175         local remote_tgt=$DIR/$tdir/remote_tgt
1176
1177         mkdir -p $DIR/$tdir
1178         $LFS mkdir -i $MDTIDX $remote_src ||
1179                    error "create remote directory failed"
1180
1181         $LFS mkdir -i $MDTIDX $remote_tgt ||
1182                    error "create remote directory failed"
1183
1184         mrename $remote_src $remote_tgt &&
1185                 error "rename remote dirs should not work!"
1186
1187         # If target dir does not exists, it should succeed
1188         rm -rf $remote_tgt
1189         mrename $remote_src $remote_tgt ||
1190                 error "rename remote dirs(tgt dir does not exists) failed!"
1191
1192         rm -rf $DIR/$tdir || error "Can not delete directories"
1193 }
1194 run_test 24z "rename one remote dir to another remote dir should fail"
1195
1196 test_24A() { # LU-3182
1197         local NFILES=5000
1198
1199         rm -rf $DIR/$tdir
1200         mkdir -p $DIR/$tdir
1201         createmany -m $DIR/$tdir/$tfile $NFILES
1202         local t=$(ls $DIR/$tdir | wc -l)
1203         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1204         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1205         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1206                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1207         fi
1208
1209         rm -rf $DIR/$tdir || error "Can not delete directories"
1210 }
1211 run_test 24A "readdir() returns correct number of entries."
1212
1213 test_24B() { # LU-4805
1214         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1215         local count
1216
1217         mkdir $DIR/$tdir
1218         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1219                 error "create striped dir failed"
1220
1221         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1222         [ $count -eq 2 ] || error "Expected 2, got $count"
1223
1224         touch $DIR/$tdir/striped_dir/a
1225
1226         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1227         [ $count -eq 3 ] || error "Expected 3, got $count"
1228
1229         touch $DIR/$tdir/striped_dir/.f
1230
1231         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1232         [ $count -eq 4 ] || error "Expected 4, got $count"
1233
1234         rm -rf $DIR/$tdir || error "Can not delete directories"
1235 }
1236 run_test 24B "readdir for striped dir return correct number of entries"
1237
1238 test_24C() {
1239         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1240
1241         mkdir $DIR/$tdir
1242         mkdir $DIR/$tdir/d0
1243         mkdir $DIR/$tdir/d1
1244
1245         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1246                 error "create striped dir failed"
1247
1248         cd $DIR/$tdir/d0/striped_dir
1249
1250         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1251         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1252         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1253
1254         [ "$d0_ino" = "$parent_ino" ] ||
1255                 error ".. wrong, expect $d0_ino, get $parent_ino"
1256
1257         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1258                 error "mv striped dir failed"
1259
1260         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1261
1262         [ "$d1_ino" = "$parent_ino" ] ||
1263                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1264 }
1265 run_test 24C "check .. in striped dir"
1266
1267 test_25a() {
1268         echo '== symlink sanity ============================================='
1269
1270         test_mkdir $DIR/d25
1271         ln -s d25 $DIR/s25
1272         touch $DIR/s25/foo || error
1273 }
1274 run_test 25a "create file in symlinked directory ==============="
1275
1276 test_25b() {
1277         [ ! -d $DIR/d25 ] && test_25a
1278         $CHECKSTAT -t file $DIR/s25/foo || error
1279 }
1280 run_test 25b "lookup file in symlinked directory ==============="
1281
1282 test_26a() {
1283         test_mkdir $DIR/d26
1284         test_mkdir $DIR/d26/d26-2
1285         ln -s d26/d26-2 $DIR/s26
1286         touch $DIR/s26/foo || error
1287 }
1288 run_test 26a "multiple component symlink ======================="
1289
1290 test_26b() {
1291         test_mkdir -p $DIR/d26b/d26-2
1292         ln -s d26b/d26-2/foo $DIR/s26-2
1293         touch $DIR/s26-2 || error
1294 }
1295 run_test 26b "multiple component symlink at end of lookup ======"
1296
1297 test_26c() {
1298         test_mkdir $DIR/d26.2
1299         touch $DIR/d26.2/foo
1300         ln -s d26.2 $DIR/s26.2-1
1301         ln -s s26.2-1 $DIR/s26.2-2
1302         ln -s s26.2-2 $DIR/s26.2-3
1303         chmod 0666 $DIR/s26.2-3/foo
1304 }
1305 run_test 26c "chain of symlinks ================================"
1306
1307 # recursive symlinks (bug 439)
1308 test_26d() {
1309         ln -s d26-3/foo $DIR/d26-3
1310 }
1311 run_test 26d "create multiple component recursive symlink ======"
1312
1313 test_26e() {
1314         [ ! -h $DIR/d26-3 ] && test_26d
1315         rm $DIR/d26-3
1316 }
1317 run_test 26e "unlink multiple component recursive symlink ======"
1318
1319 # recursive symlinks (bug 7022)
1320 test_26f() {
1321         test_mkdir -p $DIR/$tdir
1322         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1323         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1324         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1325         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1326         cd $tfile                || error "cd $tfile failed"
1327         ln -s .. dotdot          || error "ln dotdot failed"
1328         ln -s dotdot/lndir lndir || error "ln lndir failed"
1329         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1330         output=`ls $tfile/$tfile/lndir/bar1`
1331         [ "$output" = bar1 ] && error "unexpected output"
1332         rm -r $tfile             || error "rm $tfile failed"
1333         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1334 }
1335 run_test 26f "rm -r of a directory which has recursive symlink ="
1336
1337 test_27a() {
1338         echo '== stripe sanity =============================================='
1339         test_mkdir -p $DIR/d27 || error "mkdir failed"
1340         $GETSTRIPE $DIR/d27
1341         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1342         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1343         log "== test_27a: write to one stripe file ========================="
1344         cp /etc/hosts $DIR/d27/f0 || error
1345 }
1346 run_test 27a "one stripe file =================================="
1347
1348 test_27b() {
1349         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1350         test_mkdir -p $DIR/d27
1351         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1352         $GETSTRIPE -c $DIR/d27/f01
1353         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1354                 error "two-stripe file doesn't have two stripes"
1355
1356         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1357 }
1358 run_test 27b "create and write to two stripe file"
1359
1360 test_27d() {
1361         test_mkdir -p $DIR/d27
1362         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1363         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1364         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1365 }
1366 run_test 27d "create file with default settings ================"
1367
1368 test_27e() {
1369         test_mkdir -p $DIR/d27
1370         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1371         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1372         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1373 }
1374 run_test 27e "setstripe existing file (should return error) ======"
1375
1376 test_27f() {
1377         test_mkdir -p $DIR/d27
1378         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1379         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1380         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1381 }
1382 run_test 27f "setstripe with bad stripe size (should return error)"
1383
1384 test_27g() {
1385         test_mkdir -p $DIR/d27
1386         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1387         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1388                 error "$DIR/d27/fnone has object"
1389 }
1390 run_test 27g "$GETSTRIPE with no objects"
1391
1392 test_27i() {
1393         touch $DIR/d27/fsome || error "touch failed"
1394         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1395 }
1396 run_test 27i "$GETSTRIPE with some objects"
1397
1398 test_27j() {
1399         test_mkdir -p $DIR/d27
1400         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1401 }
1402 run_test 27j "setstripe with bad stripe offset (should return error)"
1403
1404 test_27k() { # bug 2844
1405         test_mkdir -p $DIR/d27
1406         FILE=$DIR/d27/f27k
1407         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1408         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1409         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1410         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1411         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1412         dd if=/dev/zero of=$FILE bs=4k count=1
1413         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1414         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1415 }
1416 run_test 27k "limit i_blksize for broken user apps ============="
1417
1418 test_27l() {
1419         test_mkdir -p $DIR/d27
1420         mcreate $DIR/f27l || error "creating file"
1421         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1422                 error "setstripe should have failed" || true
1423 }
1424 run_test 27l "check setstripe permissions (should return error)"
1425
1426 test_27m() {
1427         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1428                 return
1429         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1430                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1431                 return
1432         fi
1433         trap simple_cleanup_common EXIT
1434         test_mkdir -p $DIR/$tdir
1435         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1436         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1437                 error "dd should fill OST0"
1438         i=2
1439         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1440                 i=$((i + 1))
1441                 [ $i -gt 256 ] && break
1442         done
1443         i=$((i + 1))
1444         touch $DIR/$tdir/f27m_$i
1445         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1446                 error "OST0 was full but new created file still use it"
1447         i=$((i + 1))
1448         touch $DIR/$tdir/f27m_$i
1449         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1450                 error "OST0 was full but new created file still use it"
1451         simple_cleanup_common
1452 }
1453 run_test 27m "create file while OST0 was full =================="
1454
1455 sleep_maxage() {
1456         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1457         sleep $DELAY
1458 }
1459
1460 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1461 # if the OST isn't full anymore.
1462 reset_enospc() {
1463         local OSTIDX=${1:-""}
1464
1465         local list=$(comma_list $(osts_nodes))
1466         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1467
1468         do_nodes $list lctl set_param fail_loc=0
1469         sync    # initiate all OST_DESTROYs from MDS to OST
1470         sleep_maxage
1471 }
1472
1473 exhaust_precreations() {
1474         local OSTIDX=$1
1475         local FAILLOC=$2
1476         local FAILIDX=${3:-$OSTIDX}
1477
1478         test_mkdir -p $DIR/$tdir
1479         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1480         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1481
1482         local OST=$(ostname_from_index $OSTIDX)
1483         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1484                           sed -e 's/_UUID$//;s/^.*-//')
1485
1486         # on the mdt's osc
1487         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1488         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1489                         osc.$mdtosc_proc1.prealloc_last_id)
1490         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1491                         osc.$mdtosc_proc1.prealloc_next_id)
1492
1493         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1494         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1495
1496         test_mkdir -p $DIR/$tdir/${OST}
1497         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1498 #define OBD_FAIL_OST_ENOSPC              0x215
1499         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1500         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1501         echo "Creating to objid $last_id on ost $OST..."
1502         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1503         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1504         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1505         sleep_maxage
1506 }
1507
1508 exhaust_all_precreations() {
1509         local i
1510         for (( i=0; i < OSTCOUNT; i++ )) ; do
1511                 exhaust_precreations $i $1 -1
1512         done
1513 }
1514
1515 test_27n() {
1516         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1518         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1519         remote_ost_nodsh && skip "remote OST with nodsh" && return
1520
1521         reset_enospc
1522         rm -f $DIR/$tdir/$tfile
1523         exhaust_precreations 0 0x80000215
1524         $SETSTRIPE -c -1 $DIR/$tdir
1525         touch $DIR/$tdir/$tfile || error
1526         $GETSTRIPE $DIR/$tdir/$tfile
1527         reset_enospc
1528 }
1529 run_test 27n "create file with some full OSTs =================="
1530
1531 test_27o() {
1532         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1534         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1535         remote_ost_nodsh && skip "remote OST with nodsh" && return
1536
1537         reset_enospc
1538         rm -f $DIR/$tdir/$tfile
1539         exhaust_all_precreations 0x215
1540
1541         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1542
1543         reset_enospc
1544         rm -rf $DIR/$tdir/*
1545 }
1546 run_test 27o "create file with all full OSTs (should error) ===="
1547
1548 test_27p() {
1549         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1550         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1551         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1552         remote_ost_nodsh && skip "remote OST with nodsh" && return
1553
1554         reset_enospc
1555         rm -f $DIR/$tdir/$tfile
1556         test_mkdir -p $DIR/$tdir
1557
1558         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1559         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1560         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1561
1562         exhaust_precreations 0 0x80000215
1563         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1564         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1565         $GETSTRIPE $DIR/$tdir/$tfile
1566
1567         reset_enospc
1568 }
1569 run_test 27p "append to a truncated file with some full OSTs ==="
1570
1571 test_27q() {
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         reset_enospc
1578         rm -f $DIR/$tdir/$tfile
1579
1580         test_mkdir -p $DIR/$tdir
1581         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1582         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1583         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1584
1585         exhaust_all_precreations 0x215
1586
1587         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1588         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1589
1590         reset_enospc
1591 }
1592 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1593
1594 test_27r() {
1595         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1597         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1598         remote_ost_nodsh && skip "remote OST with nodsh" && return
1599
1600         reset_enospc
1601         rm -f $DIR/$tdir/$tfile
1602         exhaust_precreations 0 0x80000215
1603
1604         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1605
1606         reset_enospc
1607 }
1608 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1609
1610 test_27s() { # bug 10725
1611         test_mkdir -p $DIR/$tdir
1612         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1613         local stripe_count=0
1614         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1615         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1616                 error "stripe width >= 2^32 succeeded" || true
1617
1618 }
1619 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1620
1621 test_27t() { # bug 10864
1622         WDIR=$(pwd)
1623         WLFS=$(which lfs)
1624         cd $DIR
1625         touch $tfile
1626         $WLFS getstripe $tfile
1627         cd $WDIR
1628 }
1629 run_test 27t "check that utils parse path correctly"
1630
1631 test_27u() { # bug 4900
1632         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1633         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1634         local index
1635         local list=$(comma_list $(mdts_nodes))
1636
1637 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1638         do_nodes $list $LCTL set_param fail_loc=0x139
1639         test_mkdir -p $DIR/$tdir
1640         rm -rf $DIR/$tdir/*
1641         createmany -o $DIR/$tdir/t- 1000
1642         do_nodes $list $LCTL set_param fail_loc=0
1643
1644         TLOG=$DIR/$tfile.getstripe
1645         $GETSTRIPE $DIR/$tdir > $TLOG
1646         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1647         unlinkmany $DIR/$tdir/t- 1000
1648         [[ $OBJS -gt 0 ]] &&
1649                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1650 }
1651 run_test 27u "skip object creation on OSC w/o objects =========="
1652
1653 test_27v() { # bug 4900
1654         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1656         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1657         remote_ost_nodsh && skip "remote OST with nodsh" && return
1658
1659         exhaust_all_precreations 0x215
1660         reset_enospc
1661
1662         test_mkdir -p $DIR/$tdir
1663         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1664
1665         touch $DIR/$tdir/$tfile
1666         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1667         # all except ost1
1668         for (( i=1; i < OSTCOUNT; i++ )); do
1669                 do_facet ost$i lctl set_param fail_loc=0x705
1670         done
1671         local START=`date +%s`
1672         createmany -o $DIR/$tdir/$tfile 32
1673
1674         local FINISH=`date +%s`
1675         local TIMEOUT=`lctl get_param -n timeout`
1676         local PROCESS=$((FINISH - START))
1677         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1678                error "$FINISH - $START >= $TIMEOUT / 2"
1679         sleep $((TIMEOUT / 2 - PROCESS))
1680         reset_enospc
1681 }
1682 run_test 27v "skip object creation on slow OST ================="
1683
1684 test_27w() { # bug 10997
1685         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1686         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1687         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1688                 error "stripe size $size != 65536" || true
1689         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1690                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1691 }
1692 run_test 27w "check $SETSTRIPE -S option"
1693
1694 test_27wa() {
1695         [[ $OSTCOUNT -lt 2 ]] &&
1696                 skip_env "skipping multiple stripe count/offset test" && return
1697
1698         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1699         for i in $(seq 1 $OSTCOUNT); do
1700                 offset=$((i - 1))
1701                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1702                         error "setstripe -c $i -i $offset failed"
1703                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1704                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1705                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1706                 [ $index -ne $offset ] &&
1707                         error "stripe offset $index != $offset" || true
1708         done
1709 }
1710 run_test 27wa "check $SETSTRIPE -c -i options"
1711
1712 test_27x() {
1713         remote_ost_nodsh && skip "remote OST with nodsh" && return
1714         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1716         OFFSET=$(($OSTCOUNT - 1))
1717         OSTIDX=0
1718         local OST=$(ostname_from_index $OSTIDX)
1719
1720         test_mkdir -p $DIR/$tdir
1721         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1722         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1723         sleep_maxage
1724         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1725         for i in `seq 0 $OFFSET`; do
1726                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1727                 error "OST0 was degraded but new created file still use it"
1728         done
1729         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1730 }
1731 run_test 27x "create files while OST0 is degraded"
1732
1733 test_27y() {
1734         [[ $OSTCOUNT -lt 2 ]] &&
1735                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1736         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1737         remote_ost_nodsh && skip "remote OST with nodsh" && return
1738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1739
1740         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1741         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1742             osc.$mdtosc.prealloc_last_id)
1743         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1744             osc.$mdtosc.prealloc_next_id)
1745         local fcount=$((last_id - next_id))
1746         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1747         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1748
1749         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1750                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1751         local OST_DEACTIVE_IDX=-1
1752         local OSC
1753         local OSTIDX
1754         local OST
1755
1756         for OSC in $MDS_OSCS; do
1757                 OST=$(osc_to_ost $OSC)
1758                 OSTIDX=$(index_from_ostuuid $OST)
1759                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1760                         OST_DEACTIVE_IDX=$OSTIDX
1761                 fi
1762                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1763                         echo $OSC "is Deactivated:"
1764                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1765                 fi
1766         done
1767
1768         OSTIDX=$(index_from_ostuuid $OST)
1769         mkdir -p $DIR/$tdir
1770         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1771
1772         for OSC in $MDS_OSCS; do
1773                 OST=$(osc_to_ost $OSC)
1774                 OSTIDX=$(index_from_ostuuid $OST)
1775                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1776                         echo $OST "is degraded:"
1777                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1778                                                 obdfilter.$OST.degraded=1
1779                 fi
1780         done
1781
1782         sleep_maxage
1783         createmany -o $DIR/$tdir/$tfile $fcount
1784
1785         for OSC in $MDS_OSCS; do
1786                 OST=$(osc_to_ost $OSC)
1787                 OSTIDX=$(index_from_ostuuid $OST)
1788                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1789                         echo $OST "is recovered from degraded:"
1790                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1791                                                 obdfilter.$OST.degraded=0
1792                 else
1793                         do_facet $SINGLEMDS lctl --device %$OSC activate
1794                 fi
1795         done
1796
1797         # all osp devices get activated, hence -1 stripe count restored
1798         local stripecnt=0
1799
1800         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1801         # devices get activated.
1802         sleep_maxage
1803         $SETSTRIPE -c -1 $DIR/$tfile
1804         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1805         rm -f $DIR/$tfile
1806         [ $stripecnt -ne $OSTCOUNT ] &&
1807                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1808         return 0
1809 }
1810 run_test 27y "create files while OST0 is degraded and the rest inactive"
1811
1812 check_seq_oid()
1813 {
1814         log "check file $1"
1815
1816         lmm_count=$($GETSTRIPE -c $1)
1817         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1818         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1819
1820         local old_ifs="$IFS"
1821         IFS=$'[:]'
1822         fid=($($LFS path2fid $1))
1823         IFS="$old_ifs"
1824
1825         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1826         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1827
1828         # compare lmm_seq and lu_fid->f_seq
1829         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1830         # compare lmm_object_id and lu_fid->oid
1831         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1832
1833         # check the trusted.fid attribute of the OST objects of the file
1834         local have_obdidx=false
1835         local stripe_nr=0
1836         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1837                 # skip lines up to and including "obdidx"
1838                 [ -z "$obdidx" ] && break
1839                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1840                 $have_obdidx || continue
1841
1842                 local ost=$((obdidx + 1))
1843                 local dev=$(ostdevname $ost)
1844                 local oid_hex
1845
1846                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1847
1848                 seq=$(echo $seq | sed -e "s/^0x//g")
1849                 if [ $seq == 0 ]; then
1850                         oid_hex=$(echo $oid)
1851                 else
1852                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1853                 fi
1854                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1855
1856                 local ff
1857                 #
1858                 # Don't unmount/remount the OSTs if we don't need to do that.
1859                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1860                 # update too, until that use mount/ll_decode_filter_fid/mount.
1861                 # Re-enable when debugfs will understand new filter_fid.
1862                 #
1863                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1864                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1865                                 $dev 2>/dev/null" | grep "parent=")
1866                 else
1867                         stop ost$ost
1868                         mount_fstype ost$ost
1869                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1870                                 $(facet_mntpt ost$ost)/$obj_file)
1871                         unmount_fstype ost$ost
1872                         start ost$ost $dev $OST_MOUNT_OPTS
1873                 fi
1874
1875                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1876
1877                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1878
1879                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1880                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1881                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1882                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1883                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1884                 local ff_pstripe
1885                 if echo $ff_parent | grep -q 'stripe='; then
1886                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1887                 else
1888                         #
1889                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1890                         # into f_ver in this case.  See the comment on
1891                         # ff_parent.
1892                         #
1893                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1894                                 sed -e 's/\]//')
1895                 fi
1896
1897                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1898                 [ $ff_pseq = $lmm_seq ] ||
1899                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1900                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1901                 [ $ff_poid = $lmm_oid ] ||
1902                         error "FF parent OID $ff_poid != $lmm_oid"
1903                 (($ff_pstripe == $stripe_nr)) ||
1904                         error "FF stripe $ff_pstripe != $stripe_nr"
1905
1906                 stripe_nr=$((stripe_nr + 1))
1907         done
1908 }
1909
1910 test_27z() {
1911         remote_ost_nodsh && skip "remote OST with nodsh" && return
1912         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1913         test_mkdir -p $DIR/$tdir
1914
1915         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1916                 { error "setstripe -c -1 failed"; return 1; }
1917         # We need to send a write to every object to get parent FID info set.
1918         # This _should_ also work for setattr, but does not currently.
1919         # touch $DIR/$tdir/$tfile-1 ||
1920         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1921                 { error "dd $tfile-1 failed"; return 2; }
1922         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1923                 { error "setstripe -c -1 failed"; return 3; }
1924         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1925                 { error "dd $tfile-2 failed"; return 4; }
1926
1927         # make sure write RPCs have been sent to OSTs
1928         sync; sleep 5; sync
1929
1930         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1931         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1932 }
1933 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1934
1935 test_27A() { # b=19102
1936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1937         local restore_size=$($GETSTRIPE -S $MOUNT)
1938         local restore_count=$($GETSTRIPE -c $MOUNT)
1939         local restore_offset=$($GETSTRIPE -i $MOUNT)
1940         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1941         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1942                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1943         local default_size=$($GETSTRIPE -S $MOUNT)
1944         local default_offset=$($GETSTRIPE -i $MOUNT)
1945         local dsize=$((1024 * 1024))
1946         [ $default_size -eq $dsize ] ||
1947                 error "stripe size $default_size != $dsize"
1948         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1949         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1950 }
1951 run_test 27A "check filesystem-wide default LOV EA values"
1952
1953 test_27B() { # LU-2523
1954         test_mkdir -p $DIR/$tdir
1955         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1956         touch $DIR/$tdir/f0
1957         # open f1 with O_LOV_DELAY_CREATE
1958         # rename f0 onto f1
1959         # call setstripe ioctl on open file descriptor for f1
1960         # close
1961         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1962                 $DIR/$tdir/f0
1963
1964         rm -f $DIR/$tdir/f1
1965         # open f1 with O_LOV_DELAY_CREATE
1966         # unlink f1
1967         # call setstripe ioctl on open file descriptor for f1
1968         # close
1969         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1970
1971         # Allow multiop to fail in imitation of NFS's busted semantics.
1972         true
1973 }
1974 run_test 27B "call setstripe on open unlinked file/rename victim"
1975
1976 test_27C() { #LU-2871
1977         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1978
1979         declare -a ost_idx
1980         local index
1981         local found
1982         local i
1983         local j
1984
1985         test_mkdir -p $DIR/$tdir
1986         cd $DIR/$tdir
1987         for i in $(seq 0 $((OSTCOUNT - 1))); do
1988                 # set stripe across all OSTs starting from OST$i
1989                 $SETSTRIPE -i $i -c -1 $tfile$i
1990                 # get striping information
1991                 ost_idx=($($GETSTRIPE $tfile$i |
1992                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1993                 echo ${ost_idx[@]}
1994
1995                 # check the layout
1996                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1997                         error "${#ost_idx[@]} != $OSTCOUNT"
1998
1999                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2000                         found=0
2001                         for j in $(echo ${ost_idx[@]}); do
2002                                 if [ $index -eq $j ]; then
2003                                         found=1
2004                                         break
2005                                 fi
2006                         done
2007                         [ $found = 1 ] ||
2008                                 error "Can not find $index in ${ost_idx[@]}"
2009                 done
2010         done
2011 }
2012 run_test 27C "check full striping across all OSTs"
2013
2014 # createtest also checks that device nodes are created and
2015 # then visible correctly (#2091)
2016 test_28() { # bug 2091
2017         test_mkdir $DIR/d28
2018         $CREATETEST $DIR/d28/ct || error
2019 }
2020 run_test 28 "create/mknod/mkdir with bad file types ============"
2021
2022 test_29() {
2023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2024         cancel_lru_locks mdc
2025         test_mkdir $DIR/d29
2026         touch $DIR/d29/foo
2027         log 'first d29'
2028         ls -l $DIR/d29
2029
2030         declare -i LOCKCOUNTORIG=0
2031         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2032                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2033         done
2034         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2035
2036         declare -i LOCKUNUSEDCOUNTORIG=0
2037         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2038                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2039         done
2040
2041         log 'second d29'
2042         ls -l $DIR/d29
2043         log 'done'
2044
2045         declare -i LOCKCOUNTCURRENT=0
2046         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2047                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2048         done
2049
2050         declare -i LOCKUNUSEDCOUNTCURRENT=0
2051         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2052                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2053         done
2054
2055         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2056                 $LCTL set_param -n ldlm.dump_namespaces ""
2057                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2058                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2059                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2060                 return 2
2061         fi
2062         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2063                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2064                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2065                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2066                 return 3
2067         fi
2068 }
2069 run_test 29 "IT_GETATTR regression  ============================"
2070
2071 test_30a() { # was test_30
2072         cp $(which ls) $DIR || cp /bin/ls $DIR
2073         $DIR/ls / || error
2074         rm $DIR/ls
2075 }
2076 run_test 30a "execute binary from Lustre (execve) =============="
2077
2078 test_30b() {
2079         cp `which ls` $DIR || cp /bin/ls $DIR
2080         chmod go+rx $DIR/ls
2081         $RUNAS $DIR/ls / || error
2082         rm $DIR/ls
2083 }
2084 run_test 30b "execute binary from Lustre as non-root ==========="
2085
2086 test_30c() { # b=22376
2087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2088         cp `which ls` $DIR || cp /bin/ls $DIR
2089         chmod a-rw $DIR/ls
2090         cancel_lru_locks mdc
2091         cancel_lru_locks osc
2092         $RUNAS $DIR/ls / || error
2093         rm -f $DIR/ls
2094 }
2095 run_test 30c "execute binary from Lustre without read perms ===="
2096
2097 test_31a() {
2098         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2099         $CHECKSTAT -a $DIR/f31 || error
2100 }
2101 run_test 31a "open-unlink file =================================="
2102
2103 test_31b() {
2104         touch $DIR/f31 || error
2105         ln $DIR/f31 $DIR/f31b || error
2106         $MULTIOP $DIR/f31b Ouc || error
2107         $CHECKSTAT -t file $DIR/f31 || error
2108 }
2109 run_test 31b "unlink file with multiple links while open ======="
2110
2111 test_31c() {
2112         touch $DIR/f31 || error
2113         ln $DIR/f31 $DIR/f31c || error
2114         multiop_bg_pause $DIR/f31 O_uc || return 1
2115         MULTIPID=$!
2116         $MULTIOP $DIR/f31c Ouc
2117         kill -USR1 $MULTIPID
2118         wait $MULTIPID
2119 }
2120 run_test 31c "open-unlink file with multiple links ============="
2121
2122 test_31d() {
2123         opendirunlink $DIR/d31d $DIR/d31d || error
2124         $CHECKSTAT -a $DIR/d31d || error
2125 }
2126 run_test 31d "remove of open directory ========================="
2127
2128 test_31e() { # bug 2904
2129         check_kernel_version 34 || return 0
2130         openfilleddirunlink $DIR/d31e || error
2131 }
2132 run_test 31e "remove of open non-empty directory ==============="
2133
2134 test_31f() { # bug 4554
2135         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2136         set -vx
2137         test_mkdir $DIR/d31f
2138         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2139         cp /etc/hosts $DIR/d31f
2140         ls -l $DIR/d31f
2141         $GETSTRIPE $DIR/d31f/hosts
2142         multiop_bg_pause $DIR/d31f D_c || return 1
2143         MULTIPID=$!
2144
2145         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2146         test_mkdir $DIR/d31f
2147         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2148         cp /etc/hosts $DIR/d31f
2149         ls -l $DIR/d31f
2150         $GETSTRIPE $DIR/d31f/hosts
2151         multiop_bg_pause $DIR/d31f D_c || return 1
2152         MULTIPID2=$!
2153
2154         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2155         wait $MULTIPID || error "first opendir $MULTIPID failed"
2156
2157         sleep 6
2158
2159         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2160         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2161         set +vx
2162 }
2163 run_test 31f "remove of open directory with open-unlink file ==="
2164
2165 test_31g() {
2166         echo "-- cross directory link --"
2167         test_mkdir -c1 $DIR/${tdir}ga
2168         test_mkdir -c1 $DIR/${tdir}gb
2169         touch $DIR/${tdir}ga/f
2170         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2171         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2172         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2173         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2174         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2175 }
2176 run_test 31g "cross directory link==============="
2177
2178 test_31h() {
2179         echo "-- cross directory link --"
2180         test_mkdir -c1 $DIR/${tdir}
2181         test_mkdir -c1 $DIR/${tdir}/dir
2182         touch $DIR/${tdir}/f
2183         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2184         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2185         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2186         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2187         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2188 }
2189 run_test 31h "cross directory link under child==============="
2190
2191 test_31i() {
2192         echo "-- cross directory link --"
2193         test_mkdir -c1 $DIR/$tdir
2194         test_mkdir -c1 $DIR/$tdir/dir
2195         touch $DIR/$tdir/dir/f
2196         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2197         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2198         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2199         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2200         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2201 }
2202 run_test 31i "cross directory link under parent==============="
2203
2204 test_31j() {
2205         test_mkdir -c1 -p $DIR/$tdir
2206         test_mkdir -c1 -p $DIR/$tdir/dir1
2207         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2208         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2209         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2210         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2211         return 0
2212 }
2213 run_test 31j "link for directory==============="
2214
2215 test_31k() {
2216         test_mkdir -c1 -p $DIR/$tdir
2217         touch $DIR/$tdir/s
2218         touch $DIR/$tdir/exist
2219         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2220         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2221         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2222         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2223         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2224         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2225         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2226         return 0
2227 }
2228 run_test 31k "link to file: the same, non-existing, dir==============="
2229
2230 test_31m() {
2231         mkdir $DIR/d31m
2232         touch $DIR/d31m/s
2233         mkdir $DIR/d31m2
2234         touch $DIR/d31m2/exist
2235         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2236         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2237         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2238         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2239         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2240         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2241         return 0
2242 }
2243 run_test 31m "link to file: the same, non-existing, dir==============="
2244
2245 test_31n() {
2246         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2247         nlink=$(stat --format=%h $DIR/$tfile)
2248         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2249         local fd=$(free_fd)
2250         local cmd="exec $fd<$DIR/$tfile"
2251         eval $cmd
2252         cmd="exec $fd<&-"
2253         trap "eval $cmd" EXIT
2254         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2255         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2256         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2257         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2258         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2259         eval $cmd
2260 }
2261 run_test 31n "check link count of unlinked file"
2262
2263 link_one() {
2264         local TEMPNAME=$(mktemp $1_XXXXXX)
2265         mlink $TEMPNAME $1 2> /dev/null &&
2266                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2267         munlink $TEMPNAME
2268 }
2269
2270 test_31o() { # LU-2901
2271         mkdir -p $DIR/$tdir
2272         for LOOP in $(seq 100); do
2273                 rm -f $DIR/$tdir/$tfile*
2274                 for THREAD in $(seq 8); do
2275                         link_one $DIR/$tdir/$tfile.$LOOP &
2276                 done
2277                 wait
2278                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2279                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2280                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2281                         break || true
2282         done
2283 }
2284 run_test 31o "duplicate hard links with same filename"
2285
2286 test_31p() {
2287         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2288
2289         mkdir $DIR/$tdir
2290         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2291         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2292
2293         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2294                 error "open unlink test1 failed"
2295         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2296                 error "open unlink test2 failed"
2297
2298         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2299                 error "test1 still exists"
2300         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2301                 error "test2 still exists"
2302 }
2303 run_test 31p "remove of open striped directory"
2304
2305 cleanup_test32_mount() {
2306         trap 0
2307         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2308 }
2309
2310 test_32a() {
2311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2312         echo "== more mountpoints and symlinks ================="
2313         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2314         trap cleanup_test32_mount EXIT
2315         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2316         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2317         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2318         cleanup_test32_mount
2319 }
2320 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2321
2322 test_32b() {
2323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2324         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2325         trap cleanup_test32_mount EXIT
2326         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2327         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2328         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2329         cleanup_test32_mount
2330 }
2331 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2332
2333 test_32c() {
2334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2335         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2336         trap cleanup_test32_mount EXIT
2337         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2338         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2339         test_mkdir -p $DIR/$tdir/d2/test_dir
2340         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2341         cleanup_test32_mount
2342 }
2343 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2344
2345 test_32d() {
2346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2347         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2348         trap cleanup_test32_mount EXIT
2349         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2350         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2351         test_mkdir -p $DIR/$tdir/d2/test_dir
2352         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2353         cleanup_test32_mount
2354 }
2355 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2356
2357 test_32e() {
2358         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2359         test_mkdir -p $DIR/d32e/tmp
2360         TMP_DIR=$DIR/d32e/tmp
2361         ln -s $DIR/d32e $TMP_DIR/symlink11
2362         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2363         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2364         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2365 }
2366 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2367
2368 test_32f() {
2369         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2370         test_mkdir -p $DIR/d32f/tmp
2371         TMP_DIR=$DIR/d32f/tmp
2372         ln -s $DIR/d32f $TMP_DIR/symlink11
2373         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2374         ls $DIR/d32f/tmp/symlink11  || error
2375         ls $DIR/d32f/symlink01 || error
2376 }
2377 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2378
2379 test_32g() {
2380         TMP_DIR=$DIR/$tdir/tmp
2381         test_mkdir -p $DIR/$tdir/tmp
2382         test_mkdir $DIR/${tdir}2
2383         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2384         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2385         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2386         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2387         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2388         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2389 }
2390 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2391
2392 test_32h() {
2393         rm -fr $DIR/$tdir $DIR/${tdir}2
2394         TMP_DIR=$DIR/$tdir/tmp
2395         test_mkdir -p $DIR/$tdir/tmp
2396         test_mkdir $DIR/${tdir}2
2397         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2398         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2399         ls $TMP_DIR/symlink12 || error
2400         ls $DIR/$tdir/symlink02  || error
2401 }
2402 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2403
2404 test_32i() {
2405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2406         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2407         trap cleanup_test32_mount EXIT
2408         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2409         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2410         touch $DIR/$tdir/test_file
2411         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2412         cleanup_test32_mount
2413 }
2414 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2415
2416 test_32j() {
2417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2418         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2419         trap cleanup_test32_mount EXIT
2420         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2421         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2422         touch $DIR/$tdir/test_file
2423         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2424         cleanup_test32_mount
2425 }
2426 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2427
2428 test_32k() {
2429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2430         rm -fr $DIR/$tdir
2431         trap cleanup_test32_mount EXIT
2432         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2433         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2434         test_mkdir -p $DIR/$tdir/d2
2435         touch $DIR/$tdir/d2/test_file || error
2436         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2437         cleanup_test32_mount
2438 }
2439 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2440
2441 test_32l() {
2442         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2443         rm -fr $DIR/$tdir
2444         trap cleanup_test32_mount EXIT
2445         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2446         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2447         test_mkdir -p $DIR/$tdir/d2
2448         touch $DIR/$tdir/d2/test_file
2449         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2450         cleanup_test32_mount
2451 }
2452 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2453
2454 test_32m() {
2455         rm -fr $DIR/d32m
2456         test_mkdir -p $DIR/d32m/tmp
2457         TMP_DIR=$DIR/d32m/tmp
2458         ln -s $DIR $TMP_DIR/symlink11
2459         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2460         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2461         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2462 }
2463 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2464
2465 test_32n() {
2466         rm -fr $DIR/d32n
2467         test_mkdir -p $DIR/d32n/tmp
2468         TMP_DIR=$DIR/d32n/tmp
2469         ln -s $DIR $TMP_DIR/symlink11
2470         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2471         ls -l $DIR/d32n/tmp/symlink11  || error
2472         ls -l $DIR/d32n/symlink01 || error
2473 }
2474 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2475
2476 test_32o() {
2477         rm -fr $DIR/d32o $DIR/$tfile
2478         touch $DIR/$tfile
2479         test_mkdir -p $DIR/d32o/tmp
2480         TMP_DIR=$DIR/d32o/tmp
2481         ln -s $DIR/$tfile $TMP_DIR/symlink12
2482         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2483         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2484         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2485         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2486         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2487 }
2488 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2489
2490 test_32p() {
2491     log 32p_1
2492         rm -fr $DIR/d32p
2493     log 32p_2
2494         rm -f $DIR/$tfile
2495     log 32p_3
2496         touch $DIR/$tfile
2497     log 32p_4
2498         test_mkdir -p $DIR/d32p/tmp
2499     log 32p_5
2500         TMP_DIR=$DIR/d32p/tmp
2501     log 32p_6
2502         ln -s $DIR/$tfile $TMP_DIR/symlink12
2503     log 32p_7
2504         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2505     log 32p_8
2506         cat $DIR/d32p/tmp/symlink12 || error
2507     log 32p_9
2508         cat $DIR/d32p/symlink02 || error
2509     log 32p_10
2510 }
2511 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2512
2513 cleanup_testdir_mount() {
2514         trap 0
2515         $UMOUNT -d $DIR/$tdir
2516 }
2517
2518 test_32q() {
2519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2520         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2521         trap cleanup_testdir_mount EXIT
2522         test_mkdir -p $DIR/$tdir
2523         touch $DIR/$tdir/under_the_mount
2524         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2525         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2526         cleanup_testdir_mount
2527 }
2528 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2529
2530 test_32r() {
2531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2532         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2533         trap cleanup_testdir_mount EXIT
2534         test_mkdir -p $DIR/$tdir
2535         touch $DIR/$tdir/under_the_mount
2536         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2537         ls $DIR/$tdir | grep -q under_the_mount && error || true
2538         cleanup_testdir_mount
2539 }
2540 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2541
2542 test_33aa() {
2543         rm -f $DIR/$tfile
2544         touch $DIR/$tfile
2545         chmod 444 $DIR/$tfile
2546         chown $RUNAS_ID $DIR/$tfile
2547         log 33_1
2548         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2549         log 33_2
2550 }
2551 run_test 33aa "write file with mode 444 (should return error) ===="
2552
2553 test_33a() {
2554         rm -fr $DIR/d33
2555         test_mkdir -p $DIR/d33
2556         chown $RUNAS_ID $DIR/d33
2557         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2558         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2559                 error "open RDWR" || true
2560 }
2561 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2562
2563 test_33b() {
2564         rm -fr $DIR/d33
2565         test_mkdir -p $DIR/d33
2566         chown $RUNAS_ID $DIR/d33
2567         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2568 }
2569 run_test 33b "test open file with malformed flags (No panic and return error)"
2570
2571 test_33c() {
2572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2573         local ostnum
2574         local ostname
2575         local write_bytes
2576         local all_zeros
2577
2578         remote_ost_nodsh && skip "remote OST with nodsh" && return
2579         all_zeros=:
2580         rm -fr $DIR/d33
2581         test_mkdir -p $DIR/d33
2582         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2583
2584         sync
2585         for ostnum in $(seq $OSTCOUNT); do
2586                 # test-framework's OST numbering is one-based, while Lustre's
2587                 # is zero-based
2588                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2589                 # Parsing llobdstat's output sucks; we could grep the /proc
2590                 # path, but that's likely to not be as portable as using the
2591                 # llobdstat utility.  So we parse lctl output instead.
2592                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2593                         obdfilter/$ostname/stats |
2594                         awk '/^write_bytes/ {print $7}' )
2595                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2596                 if (( ${write_bytes:-0} > 0 ))
2597                 then
2598                         all_zeros=false
2599                         break;
2600                 fi
2601         done
2602
2603         $all_zeros || return 0
2604
2605         # Write four bytes
2606         echo foo > $DIR/d33/bar
2607         # Really write them
2608         sync
2609
2610         # Total up write_bytes after writing.  We'd better find non-zeros.
2611         for ostnum in $(seq $OSTCOUNT); do
2612                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2613                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2614                         obdfilter/$ostname/stats |
2615                         awk '/^write_bytes/ {print $7}' )
2616                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2617                 if (( ${write_bytes:-0} > 0 ))
2618                 then
2619                         all_zeros=false
2620                         break;
2621                 fi
2622         done
2623
2624         if $all_zeros
2625         then
2626                 for ostnum in $(seq $OSTCOUNT); do
2627                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2628                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2629                         do_facet ost$ostnum lctl get_param -n \
2630                                 obdfilter/$ostname/stats
2631                 done
2632                 error "OST not keeping write_bytes stats (b22312)"
2633         fi
2634 }
2635 run_test 33c "test llobdstat and write_bytes"
2636
2637 test_33d() {
2638         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2640         local MDTIDX=1
2641         local remote_dir=$DIR/$tdir/remote_dir
2642
2643         mkdir -p $DIR/$tdir
2644         $LFS mkdir -i $MDTIDX $remote_dir ||
2645                 error "create remote directory failed"
2646
2647         touch $remote_dir/$tfile
2648         chmod 444 $remote_dir/$tfile
2649         chown $RUNAS_ID $remote_dir/$tfile
2650
2651         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2652
2653         chown $RUNAS_ID $remote_dir
2654         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2655                                         error "create" || true
2656         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2657                                     error "open RDWR" || true
2658         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2659                                     error "create" || true
2660 }
2661 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2662
2663 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2664 test_34a() {
2665         rm -f $DIR/f34
2666         $MCREATE $DIR/f34 || error
2667         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2668         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2669         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2670         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2671 }
2672 run_test 34a "truncate file that has not been opened ==========="
2673
2674 test_34b() {
2675         [ ! -f $DIR/f34 ] && test_34a
2676         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2677         $OPENFILE -f O_RDONLY $DIR/f34
2678         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2679         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2680 }
2681 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2682
2683 test_34c() {
2684         [ ! -f $DIR/f34 ] && test_34a
2685         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2686         $OPENFILE -f O_RDWR $DIR/f34
2687         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2688         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2689 }
2690 run_test 34c "O_RDWR opening file-with-size works =============="
2691
2692 test_34d() {
2693         [ ! -f $DIR/f34 ] && test_34a
2694         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2695         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2696         rm $DIR/f34
2697 }
2698 run_test 34d "write to sparse file ============================="
2699
2700 test_34e() {
2701         rm -f $DIR/f34e
2702         $MCREATE $DIR/f34e || error
2703         $TRUNCATE $DIR/f34e 1000 || error
2704         $CHECKSTAT -s 1000 $DIR/f34e || error
2705         $OPENFILE -f O_RDWR $DIR/f34e
2706         $CHECKSTAT -s 1000 $DIR/f34e || error
2707 }
2708 run_test 34e "create objects, some with size and some without =="
2709
2710 test_34f() { # bug 6242, 6243
2711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2712         SIZE34F=48000
2713         rm -f $DIR/f34f
2714         $MCREATE $DIR/f34f || error
2715         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2716         dd if=$DIR/f34f of=$TMP/f34f
2717         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2718         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2719         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2720         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2721         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2722 }
2723 run_test 34f "read from a file with no objects until EOF ======="
2724
2725 test_34g() {
2726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2727         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2728         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2729         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2730         cancel_lru_locks osc
2731         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2732                 error "wrong size after lock cancel"
2733
2734         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2735         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2736                 error "expanding truncate failed"
2737         cancel_lru_locks osc
2738         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2739                 error "wrong expanded size after lock cancel"
2740 }
2741 run_test 34g "truncate long file ==============================="
2742
2743 test_34h() {
2744         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2745         local gid=10
2746         local sz=1000
2747
2748         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2749         sync # Flush the cache so that multiop below does not block on cache
2750              # flush when getting the group lock
2751         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2752         MULTIPID=$!
2753
2754         # Since just timed wait is not good enough, let's do a sync write
2755         # that way we are sure enough time for a roundtrip + processing
2756         # passed + 2 seconds of extra margin.
2757         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2758         rm $DIR/${tfile}-1
2759         sleep 2
2760
2761         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2762                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2763                 kill -9 $MULTIPID
2764         fi
2765         wait $MULTIPID
2766         local nsz=`stat -c %s $DIR/$tfile`
2767         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2768 }
2769 run_test 34h "ftruncate file under grouplock should not block"
2770
2771 test_35a() {
2772         cp /bin/sh $DIR/f35a
2773         chmod 444 $DIR/f35a
2774         chown $RUNAS_ID $DIR/f35a
2775         $RUNAS $DIR/f35a && error || true
2776         rm $DIR/f35a
2777 }
2778 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2779
2780 test_36a() {
2781         rm -f $DIR/f36
2782         utime $DIR/f36 || error
2783 }
2784 run_test 36a "MDS utime check (mknod, utime) ==================="
2785
2786 test_36b() {
2787         echo "" > $DIR/f36
2788         utime $DIR/f36 || error
2789 }
2790 run_test 36b "OST utime check (open, utime) ===================="
2791
2792 test_36c() {
2793         rm -f $DIR/d36/f36
2794         test_mkdir $DIR/d36
2795         chown $RUNAS_ID $DIR/d36
2796         $RUNAS utime $DIR/d36/f36 || error
2797 }
2798 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2799
2800 test_36d() {
2801         [ ! -d $DIR/d36 ] && test_36c
2802         echo "" > $DIR/d36/f36
2803         $RUNAS utime $DIR/d36/f36 || error
2804 }
2805 run_test 36d "non-root OST utime check (open, utime) ==========="
2806
2807 test_36e() {
2808         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2809         test_mkdir -p $DIR/$tdir
2810         touch $DIR/$tdir/$tfile
2811         $RUNAS utime $DIR/$tdir/$tfile && \
2812                 error "utime worked, expected failure" || true
2813 }
2814 run_test 36e "utime on non-owned file (should return error) ===="
2815
2816 subr_36fh() {
2817         local fl="$1"
2818         local LANG_SAVE=$LANG
2819         local LC_LANG_SAVE=$LC_LANG
2820         export LANG=C LC_LANG=C # for date language
2821
2822         DATESTR="Dec 20  2000"
2823         test_mkdir -p $DIR/$tdir
2824         lctl set_param fail_loc=$fl
2825         date; date +%s
2826         cp /etc/hosts $DIR/$tdir/$tfile
2827         sync & # write RPC generated with "current" inode timestamp, but delayed
2828         sleep 1
2829         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2830         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2831         cancel_lru_locks osc
2832         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2833         date; date +%s
2834         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2835                 echo "BEFORE: $LS_BEFORE" && \
2836                 echo "AFTER : $LS_AFTER" && \
2837                 echo "WANT  : $DATESTR" && \
2838                 error "$DIR/$tdir/$tfile timestamps changed" || true
2839
2840         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2841 }
2842
2843 test_36f() {
2844         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2845         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2846         subr_36fh "0x80000214"
2847 }
2848 run_test 36f "utime on file racing with OST BRW write =========="
2849
2850 test_36g() {
2851         remote_ost_nodsh && skip "remote OST with nodsh" && return
2852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2853         local fmd_max_age
2854         local fmd_before
2855         local fmd_after
2856
2857         test_mkdir -p $DIR/$tdir
2858         fmd_max_age=$(do_facet ost1 \
2859                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2860                 head -n 1")
2861
2862         fmd_before=$(do_facet ost1 \
2863                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2864         touch $DIR/$tdir/$tfile
2865         sleep $((fmd_max_age + 12))
2866         fmd_after=$(do_facet ost1 \
2867                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2868
2869         echo "fmd_before: $fmd_before"
2870         echo "fmd_after: $fmd_after"
2871         [[ $fmd_after -gt $fmd_before ]] &&
2872                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2873                 error "fmd didn't expire after ping" || true
2874 }
2875 run_test 36g "filter mod data cache expiry ====================="
2876
2877 test_36h() {
2878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2879         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2880         subr_36fh "0x80000227"
2881 }
2882 run_test 36h "utime on file racing with OST BRW write =========="
2883
2884 test_36i() {
2885         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2886
2887         mkdir $DIR/$tdir
2888         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2889
2890         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2891         local new_mtime=$((mtime + 200))
2892
2893         #change Modify time of striped dir
2894         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2895                         error "change mtime failed"
2896
2897         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2898
2899         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2900 }
2901 run_test 36i "change mtime on striped directory"
2902
2903 # test_37 - duplicate with tests 32q 32r
2904
2905 test_38() {
2906         local file=$DIR/$tfile
2907         touch $file
2908         openfile -f O_DIRECTORY $file
2909         local RC=$?
2910         local ENOTDIR=20
2911         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2912         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2913 }
2914 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2915
2916 test_39() {
2917         touch $DIR/$tfile
2918         touch $DIR/${tfile}2
2919 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2920 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2921 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2922         sleep 2
2923         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2924         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2925                 echo "mtime"
2926                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2927                 echo "atime"
2928                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2929                 echo "ctime"
2930                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2931                 error "O_TRUNC didn't change timestamps"
2932         fi
2933 }
2934 run_test 39 "mtime changed on create ==========================="
2935
2936 test_39b() {
2937         test_mkdir -p -c1 $DIR/$tdir
2938         cp -p /etc/passwd $DIR/$tdir/fopen
2939         cp -p /etc/passwd $DIR/$tdir/flink
2940         cp -p /etc/passwd $DIR/$tdir/funlink
2941         cp -p /etc/passwd $DIR/$tdir/frename
2942         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2943
2944         sleep 1
2945         echo "aaaaaa" >> $DIR/$tdir/fopen
2946         echo "aaaaaa" >> $DIR/$tdir/flink
2947         echo "aaaaaa" >> $DIR/$tdir/funlink
2948         echo "aaaaaa" >> $DIR/$tdir/frename
2949
2950         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2951         local link_new=`stat -c %Y $DIR/$tdir/flink`
2952         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2953         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2954
2955         cat $DIR/$tdir/fopen > /dev/null
2956         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2957         rm -f $DIR/$tdir/funlink2
2958         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2959
2960         for (( i=0; i < 2; i++ )) ; do
2961                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2962                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2963                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2964                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2965
2966                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2967                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2968                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2969                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2970
2971                 cancel_lru_locks osc
2972                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2973         done
2974 }
2975 run_test 39b "mtime change on open, link, unlink, rename  ======"
2976
2977 # this should be set to past
2978 TEST_39_MTIME=`date -d "1 year ago" +%s`
2979
2980 # bug 11063
2981 test_39c() {
2982         touch $DIR1/$tfile
2983         sleep 2
2984         local mtime0=`stat -c %Y $DIR1/$tfile`
2985
2986         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2987         local mtime1=`stat -c %Y $DIR1/$tfile`
2988         [ "$mtime1" = $TEST_39_MTIME ] || \
2989                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2990
2991         local d1=`date +%s`
2992         echo hello >> $DIR1/$tfile
2993         local d2=`date +%s`
2994         local mtime2=`stat -c %Y $DIR1/$tfile`
2995         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2996                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2997
2998         mv $DIR1/$tfile $DIR1/$tfile-1
2999
3000         for (( i=0; i < 2; i++ )) ; do
3001                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3002                 [ "$mtime2" = "$mtime3" ] || \
3003                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3004
3005                 cancel_lru_locks osc
3006                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3007         done
3008 }
3009 run_test 39c "mtime change on rename ==========================="
3010
3011 # bug 21114
3012 test_39d() {
3013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3014         touch $DIR1/$tfile
3015
3016         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3017
3018         for (( i=0; i < 2; i++ )) ; do
3019                 local mtime=`stat -c %Y $DIR1/$tfile`
3020                 [ $mtime = $TEST_39_MTIME ] || \
3021                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3022
3023                 cancel_lru_locks osc
3024                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3025         done
3026 }
3027 run_test 39d "create, utime, stat =============================="
3028
3029 # bug 21114
3030 test_39e() {
3031         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3032         touch $DIR1/$tfile
3033         local mtime1=`stat -c %Y $DIR1/$tfile`
3034
3035         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3036
3037         for (( i=0; i < 2; i++ )) ; do
3038                 local mtime2=`stat -c %Y $DIR1/$tfile`
3039                 [ $mtime2 = $TEST_39_MTIME ] || \
3040                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3041
3042                 cancel_lru_locks osc
3043                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3044         done
3045 }
3046 run_test 39e "create, stat, utime, stat ========================"
3047
3048 # bug 21114
3049 test_39f() {
3050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3051         touch $DIR1/$tfile
3052         mtime1=`stat -c %Y $DIR1/$tfile`
3053
3054         sleep 2
3055         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3056
3057         for (( i=0; i < 2; i++ )) ; do
3058                 local mtime2=`stat -c %Y $DIR1/$tfile`
3059                 [ $mtime2 = $TEST_39_MTIME ] || \
3060                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3061
3062                 cancel_lru_locks osc
3063                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3064         done
3065 }
3066 run_test 39f "create, stat, sleep, utime, stat ================="
3067
3068 # bug 11063
3069 test_39g() {
3070         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3071         echo hello >> $DIR1/$tfile
3072         local mtime1=`stat -c %Y $DIR1/$tfile`
3073
3074         sleep 2
3075         chmod o+r $DIR1/$tfile
3076
3077         for (( i=0; i < 2; i++ )) ; do
3078                 local mtime2=`stat -c %Y $DIR1/$tfile`
3079                 [ "$mtime1" = "$mtime2" ] || \
3080                         error "lost mtime: $mtime2, should be $mtime1"
3081
3082                 cancel_lru_locks osc
3083                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3084         done
3085 }
3086 run_test 39g "write, chmod, stat ==============================="
3087
3088 # bug 11063
3089 test_39h() {
3090         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3091         touch $DIR1/$tfile
3092         sleep 1
3093
3094         local d1=`date`
3095         echo hello >> $DIR1/$tfile
3096         local mtime1=`stat -c %Y $DIR1/$tfile`
3097
3098         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3099         local d2=`date`
3100         if [ "$d1" != "$d2" ]; then
3101                 echo "write and touch not within one second"
3102         else
3103                 for (( i=0; i < 2; i++ )) ; do
3104                         local mtime2=`stat -c %Y $DIR1/$tfile`
3105                         [ "$mtime2" = $TEST_39_MTIME ] || \
3106                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3107
3108                         cancel_lru_locks osc
3109                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3110                 done
3111         fi
3112 }
3113 run_test 39h "write, utime within one second, stat ============="
3114
3115 test_39i() {
3116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3117         touch $DIR1/$tfile
3118         sleep 1
3119
3120         echo hello >> $DIR1/$tfile
3121         local mtime1=`stat -c %Y $DIR1/$tfile`
3122
3123         mv $DIR1/$tfile $DIR1/$tfile-1
3124
3125         for (( i=0; i < 2; i++ )) ; do
3126                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3127
3128                 [ "$mtime1" = "$mtime2" ] || \
3129                         error "lost mtime: $mtime2, should be $mtime1"
3130
3131                 cancel_lru_locks osc
3132                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3133         done
3134 }
3135 run_test 39i "write, rename, stat =============================="
3136
3137 test_39j() {
3138         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3139         start_full_debug_logging
3140         touch $DIR1/$tfile
3141         sleep 1
3142
3143         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3144         lctl set_param fail_loc=0x80000412
3145         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3146                 error "multiop failed"
3147         local multipid=$!
3148         local mtime1=`stat -c %Y $DIR1/$tfile`
3149
3150         mv $DIR1/$tfile $DIR1/$tfile-1
3151
3152         kill -USR1 $multipid
3153         wait $multipid || error "multiop close failed"
3154
3155         for (( i=0; i < 2; i++ )) ; do
3156                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3157                 [ "$mtime1" = "$mtime2" ] ||
3158                         error "mtime is lost on close: $mtime2, " \
3159                               "should be $mtime1"
3160
3161                 cancel_lru_locks osc
3162                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3163         done
3164         lctl set_param fail_loc=0
3165         stop_full_debug_logging
3166 }
3167 run_test 39j "write, rename, close, stat ======================="
3168
3169 test_39k() {
3170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3171         touch $DIR1/$tfile
3172         sleep 1
3173
3174         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3175         local multipid=$!
3176         local mtime1=`stat -c %Y $DIR1/$tfile`
3177
3178         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3179
3180         kill -USR1 $multipid
3181         wait $multipid || error "multiop close failed"
3182
3183         for (( i=0; i < 2; i++ )) ; do
3184                 local mtime2=`stat -c %Y $DIR1/$tfile`
3185
3186                 [ "$mtime2" = $TEST_39_MTIME ] || \
3187                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3188
3189                 cancel_lru_locks osc
3190                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3191         done
3192 }
3193 run_test 39k "write, utime, close, stat ========================"
3194
3195 # this should be set to future
3196 TEST_39_ATIME=`date -d "1 year" +%s`
3197
3198 test_39l() {
3199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3200         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3201         local atime_diff=$(do_facet $SINGLEMDS \
3202                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3203         rm -rf $DIR/$tdir
3204         mkdir -p $DIR/$tdir
3205
3206         # test setting directory atime to future
3207         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3208         local atime=$(stat -c %X $DIR/$tdir)
3209         [ "$atime" = $TEST_39_ATIME ] || \
3210                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3211
3212         # test setting directory atime from future to now
3213         local d1=$(date +%s)
3214         ls $DIR/$tdir
3215         local d2=$(date +%s)
3216
3217         cancel_lru_locks mdc
3218         atime=$(stat -c %X $DIR/$tdir)
3219         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3220                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3221
3222         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3223         sleep 3
3224
3225         # test setting directory atime when now > dir atime + atime_diff
3226         d1=$(date +%s)
3227         ls $DIR/$tdir
3228         d2=$(date +%s)
3229         cancel_lru_locks mdc
3230         atime=$(stat -c %X $DIR/$tdir)
3231         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3232                 error "atime is not updated  : $atime, should be $d2"
3233
3234         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3235         sleep 3
3236
3237         # test not setting directory atime when now < dir atime + atime_diff
3238         ls $DIR/$tdir
3239         cancel_lru_locks mdc
3240         atime=$(stat -c %X $DIR/$tdir)
3241         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3242                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3243
3244         do_facet $SINGLEMDS \
3245                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3246 }
3247 run_test 39l "directory atime update ==========================="
3248
3249 test_39m() {
3250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3251         touch $DIR1/$tfile
3252         sleep 2
3253         local far_past_mtime=$(date -d "May 29 1953" +%s)
3254         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3255
3256         touch -m -d @$far_past_mtime $DIR1/$tfile
3257         touch -a -d @$far_past_atime $DIR1/$tfile
3258
3259         for (( i=0; i < 2; i++ )) ; do
3260                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3261                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3262                         error "atime or mtime set incorrectly"
3263
3264                 cancel_lru_locks osc
3265                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3266         done
3267 }
3268 run_test 39m "test atime and mtime before 1970"
3269
3270 test_39n() { # LU-3832
3271         local atime_diff=$(do_facet $SINGLEMDS \
3272                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3273         local atime0
3274         local atime1
3275         local atime2
3276
3277         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3278
3279         rm -rf $DIR/$tfile
3280         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3281         atime0=$(stat -c %X $DIR/$tfile)
3282
3283         sleep 5
3284         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3285         atime1=$(stat -c %X $DIR/$tfile)
3286
3287         sleep 5
3288         cancel_lru_locks mdc
3289         cancel_lru_locks osc
3290         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3291         atime2=$(stat -c %X $DIR/$tfile)
3292
3293         do_facet $SINGLEMDS \
3294                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3295
3296         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3297         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3298 }
3299 run_test 39n "check that O_NOATIME is honored"
3300
3301 test_39o() {
3302         TESTDIR=$DIR/$tdir/$tfile
3303         [ -e $TESTDIR ] && rm -rf $TESTDIR
3304         test_mkdir -p $TESTDIR
3305         cd $TESTDIR
3306         links1=2
3307         ls
3308         mkdir a b
3309         ls
3310         links2=$(stat -c %h .)
3311         [ $(($links1 + 2)) != $links2 ] &&
3312                 error "wrong links count $(($links1 + 2)) != $links2"
3313         rmdir b
3314         links3=$(stat -c %h .)
3315         [ $(($links1 + 1)) != $links3 ] &&
3316                 error "wrong links count $links1 != $links3"
3317         return 0
3318 }
3319 run_test 39o "directory cached attributes updated after create ========"
3320
3321 test_39p() {
3322         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3323         local MDTIDX=1
3324         TESTDIR=$DIR/$tdir/$tfile
3325         [ -e $TESTDIR ] && rm -rf $TESTDIR
3326         mkdir -p $TESTDIR
3327         cd $TESTDIR
3328         links1=2
3329         ls
3330         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3331         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3332         ls
3333         links2=$(stat -c %h .)
3334         [ $(($links1 + 2)) != $links2 ] &&
3335                 error "wrong links count $(($links1 + 2)) != $links2"
3336         rmdir remote_dir2
3337         links3=$(stat -c %h .)
3338         [ $(($links1 + 1)) != $links3 ] &&
3339                 error "wrong links count $links1 != $links3"
3340         return 0
3341 }
3342 run_test 39p "remote directory cached attributes updated after create ========"
3343
3344
3345 test_40() {
3346         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3347         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3348                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3349         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3350                 error "$tfile is not 4096 bytes in size"
3351 }
3352 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3353
3354 test_41() {
3355         # bug 1553
3356         small_write $DIR/f41 18
3357 }
3358 run_test 41 "test small file write + fstat ====================="
3359
3360 count_ost_writes() {
3361         lctl get_param -n osc.*.stats |
3362                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3363                         END { printf("%0.0f", writes) }'
3364 }
3365
3366 # decent default
3367 WRITEBACK_SAVE=500
3368 DIRTY_RATIO_SAVE=40
3369 MAX_DIRTY_RATIO=50
3370 BG_DIRTY_RATIO_SAVE=10
3371 MAX_BG_DIRTY_RATIO=25
3372
3373 start_writeback() {
3374         trap 0
3375         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3376         # dirty_ratio, dirty_background_ratio
3377         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3378                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3379                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3380                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3381         else
3382                 # if file not here, we are a 2.4 kernel
3383                 kill -CONT `pidof kupdated`
3384         fi
3385 }
3386
3387 stop_writeback() {
3388         # setup the trap first, so someone cannot exit the test at the
3389         # exact wrong time and mess up a machine
3390         trap start_writeback EXIT
3391         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3392         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3393                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3394                 sysctl -w vm.dirty_writeback_centisecs=0
3395                 sysctl -w vm.dirty_writeback_centisecs=0
3396                 # save and increase /proc/sys/vm/dirty_ratio
3397                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3398                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3399                 # save and increase /proc/sys/vm/dirty_background_ratio
3400                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3401                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3402         else
3403                 # if file not here, we are a 2.4 kernel
3404                 kill -STOP `pidof kupdated`
3405         fi
3406 }
3407
3408 # ensure that all stripes have some grant before we test client-side cache
3409 setup_test42() {
3410         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3411                 dd if=/dev/zero of=$i bs=4k count=1
3412                 rm $i
3413         done
3414 }
3415
3416 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3417 # file truncation, and file removal.
3418 test_42a() {
3419         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3420         setup_test42
3421         cancel_lru_locks osc
3422         stop_writeback
3423         sync; sleep 1; sync # just to be safe
3424         BEFOREWRITES=`count_ost_writes`
3425         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3426         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3427         AFTERWRITES=`count_ost_writes`
3428         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3429                 error "$BEFOREWRITES < $AFTERWRITES"
3430         start_writeback
3431 }
3432 run_test 42a "ensure that we don't flush on close =============="
3433
3434 test_42b() {
3435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3436         setup_test42
3437         cancel_lru_locks osc
3438         stop_writeback
3439         sync
3440         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3441         BEFOREWRITES=$(count_ost_writes)
3442         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3443         AFTERWRITES=$(count_ost_writes)
3444         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3445                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3446         fi
3447         BEFOREWRITES=$(count_ost_writes)
3448         sync || error "sync: $?"
3449         AFTERWRITES=$(count_ost_writes)
3450         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3451                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3452         fi
3453         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3454         start_writeback
3455         return 0
3456 }
3457 run_test 42b "test destroy of file with cached dirty data ======"
3458
3459 # if these tests just want to test the effect of truncation,
3460 # they have to be very careful.  consider:
3461 # - the first open gets a {0,EOF}PR lock
3462 # - the first write conflicts and gets a {0, count-1}PW
3463 # - the rest of the writes are under {count,EOF}PW
3464 # - the open for truncate tries to match a {0,EOF}PR
3465 #   for the filesize and cancels the PWs.
3466 # any number of fixes (don't get {0,EOF} on open, match
3467 # composite locks, do smarter file size management) fix
3468 # this, but for now we want these tests to verify that
3469 # the cancellation with truncate intent works, so we
3470 # start the file with a full-file pw lock to match against
3471 # until the truncate.
3472 trunc_test() {
3473         test=$1
3474         file=$DIR/$test
3475         offset=$2
3476         cancel_lru_locks osc
3477         stop_writeback
3478         # prime the file with 0,EOF PW to match
3479         touch $file
3480         $TRUNCATE $file 0
3481         sync; sync
3482         # now the real test..
3483         dd if=/dev/zero of=$file bs=1024 count=100
3484         BEFOREWRITES=`count_ost_writes`
3485         $TRUNCATE $file $offset
3486         cancel_lru_locks osc
3487         AFTERWRITES=`count_ost_writes`
3488         start_writeback
3489 }
3490
3491 test_42c() {
3492         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3493         trunc_test 42c 1024
3494         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3495             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3496         rm $file
3497 }
3498 run_test 42c "test partial truncate of file with cached dirty data"
3499
3500 test_42d() {
3501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3502         trunc_test 42d 0
3503         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3504             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3505         rm $file
3506 }
3507 run_test 42d "test complete truncate of file with cached dirty data"
3508
3509 test_42e() { # bug22074
3510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3511         local TDIR=$DIR/${tdir}e
3512         local pagesz=$(page_size)
3513         local pages=16 # hardcoded 16 pages, don't change it.
3514         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3515         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3516         local max_dirty_mb
3517         local warmup_files
3518
3519         test_mkdir -p $DIR/${tdir}e
3520         $SETSTRIPE -c 1 $TDIR
3521         createmany -o $TDIR/f $files
3522
3523         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3524
3525         # we assume that with $OSTCOUNT files, at least one of them will
3526         # be allocated on OST0.
3527         warmup_files=$((OSTCOUNT * max_dirty_mb))
3528         createmany -o $TDIR/w $warmup_files
3529
3530         # write a large amount of data into one file and sync, to get good
3531         # avail_grant number from OST.
3532         for ((i=0; i<$warmup_files; i++)); do
3533                 idx=$($GETSTRIPE -i $TDIR/w$i)
3534                 [ $idx -ne 0 ] && continue
3535                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3536                 break
3537         done
3538         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3539         sync
3540         $LCTL get_param $proc_osc0/cur_dirty_bytes
3541         $LCTL get_param $proc_osc0/cur_grant_bytes
3542
3543         # create as much dirty pages as we can while not to trigger the actual
3544         # RPCs directly. but depends on the env, VFS may trigger flush during this
3545         # period, hopefully we are good.
3546         for ((i=0; i<$warmup_files; i++)); do
3547                 idx=$($GETSTRIPE -i $TDIR/w$i)
3548                 [ $idx -ne 0 ] && continue
3549                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3550         done
3551         $LCTL get_param $proc_osc0/cur_dirty_bytes
3552         $LCTL get_param $proc_osc0/cur_grant_bytes
3553
3554         # perform the real test
3555         $LCTL set_param $proc_osc0/rpc_stats 0
3556         for ((;i<$files; i++)); do
3557                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3558                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3559         done
3560         sync
3561         $LCTL get_param $proc_osc0/rpc_stats
3562
3563         local percent=0
3564         local have_ppr=false
3565         $LCTL get_param $proc_osc0/rpc_stats |
3566                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3567                         # skip lines until we are at the RPC histogram data
3568                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3569                         $have_ppr || continue
3570
3571                         # we only want the percent stat for < 16 pages
3572                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3573
3574                         percent=$((percent + WPCT))
3575                         if [[ $percent -gt 15 ]]; then
3576                                 error "less than 16-pages write RPCs" \
3577                                       "$percent% > 15%"
3578                                 break
3579                         fi
3580                 done
3581         rm -rf $TDIR
3582 }
3583 run_test 42e "verify sub-RPC writes are not done synchronously"
3584
3585 test_43() {
3586         test_mkdir -p $DIR/$tdir
3587         cp -p /bin/ls $DIR/$tdir/$tfile
3588         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3589         pid=$!
3590         # give multiop a chance to open
3591         sleep 1
3592
3593         $DIR/$tdir/$tfile && error || true
3594         kill -USR1 $pid
3595 }
3596 run_test 43 "execution of file opened for write should return -ETXTBSY"
3597
3598 test_43a() {
3599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3600         test_mkdir -p $DIR/$tdir
3601         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3602                         cp -p multiop $DIR/$tdir/multiop
3603         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3604                         return 1
3605         MULTIOP_PID=$!
3606         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3607         kill -USR1 $MULTIOP_PID || return 2
3608         wait $MULTIOP_PID || return 3
3609         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3610 }
3611 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3612
3613 test_43b() {
3614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3615         test_mkdir -p $DIR/$tdir
3616         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3617                         cp -p multiop $DIR/$tdir/multiop
3618         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3619                         return 1
3620         MULTIOP_PID=$!
3621         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3622         kill -USR1 $MULTIOP_PID || return 2
3623         wait $MULTIOP_PID || return 3
3624         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3625 }
3626 run_test 43b "truncate of file being executed should return -ETXTBSY"
3627
3628 test_43c() {
3629         local testdir="$DIR/$tdir"
3630         test_mkdir -p $DIR/$tdir
3631         cp $SHELL $testdir/
3632         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3633                 ( cd $testdir && md5sum -c)
3634 }
3635 run_test 43c "md5sum of copy into lustre========================"
3636
3637 test_44() {
3638         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3639         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3640         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3641 }
3642 run_test 44 "zero length read from a sparse stripe ============="
3643
3644 test_44a() {
3645         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3646                 awk '{ print $2 }')
3647         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3648         [[ $nstripe -gt $OSTCOUNT ]] &&
3649             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3650             return
3651         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3652                 awk '{ print $2 }')
3653         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3654                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3655                         awk '{ print $2 }')
3656         fi
3657
3658         OFFSETS="0 $((stride/2)) $((stride-1))"
3659         for offset in $OFFSETS; do
3660                 for i in $(seq 0 $((nstripe-1))); do
3661                         local GLOBALOFFSETS=""
3662                         # size in Bytes
3663                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3664                         local myfn=$DIR/d44a-$size
3665                         echo "--------writing $myfn at $size"
3666                         ll_sparseness_write $myfn $size ||
3667                                 error "ll_sparseness_write"
3668                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3669                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3670                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3671
3672                         for j in $(seq 0 $((nstripe-1))); do
3673                                 # size in Bytes
3674                                 size=$((((j + $nstripe )*$stride + $offset)))
3675                                 ll_sparseness_write $myfn $size ||
3676                                         error "ll_sparseness_write"
3677                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3678                         done
3679                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3680                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3681                         rm -f $myfn
3682                 done
3683         done
3684 }
3685 run_test 44a "test sparse pwrite ==============================="
3686
3687 dirty_osc_total() {
3688         tot=0
3689         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3690                 tot=$(($tot + $d))
3691         done
3692         echo $tot
3693 }
3694 do_dirty_record() {
3695         before=`dirty_osc_total`
3696         echo executing "\"$*\""
3697         eval $*
3698         after=`dirty_osc_total`
3699         echo before $before, after $after
3700 }
3701 test_45() {
3702         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3703         f="$DIR/f45"
3704         # Obtain grants from OST if it supports it
3705         echo blah > ${f}_grant
3706         stop_writeback
3707         sync
3708         do_dirty_record "echo blah > $f"
3709         [[ $before -eq $after ]] && error "write wasn't cached"
3710         do_dirty_record "> $f"
3711         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3712         do_dirty_record "echo blah > $f"
3713         [[ $before -eq $after ]] && error "write wasn't cached"
3714         do_dirty_record "sync"
3715         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3716         do_dirty_record "echo blah > $f"
3717         [[ $before -eq $after ]] && error "write wasn't cached"
3718         do_dirty_record "cancel_lru_locks osc"
3719         [[ $before -gt $after ]] ||
3720                 error "lock cancellation didn't lower dirty count"
3721         start_writeback
3722 }
3723 run_test 45 "osc io page accounting ============================"
3724
3725 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3726 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3727 # objects offset and an assert hit when an rpc was built with 1023's mapped
3728 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3729 test_46() {
3730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3731         f="$DIR/f46"
3732         stop_writeback
3733         sync
3734         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3735         sync
3736         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3737         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3738         sync
3739         start_writeback
3740 }
3741 run_test 46 "dirtying a previously written page ================"
3742
3743 # test_47 is removed "Device nodes check" is moved to test_28
3744
3745 test_48a() { # bug 2399
3746         check_kernel_version 34 || return 0
3747         test_mkdir -p $DIR/$tdir
3748         cd $DIR/$tdir
3749         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3750         test_mkdir $DIR/$tdir || error "recreate directory failed"
3751         touch foo || error "'touch foo' failed after recreating cwd"
3752         test_mkdir $DIR/$tdir/bar ||
3753                      error "'mkdir foo' failed after recreating cwd"
3754         if check_kernel_version 44; then
3755                 touch .foo || error "'touch .foo' failed after recreating cwd"
3756                 test_mkdir $DIR/$tdir/.bar ||
3757                               error "'mkdir .foo' failed after recreating cwd"
3758         fi
3759         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3760         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3761         cd . || error "'cd .' failed after recreating cwd"
3762         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3763         rmdir . && error "'rmdir .' worked after recreating cwd"
3764         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3765         cd .. || error "'cd ..' failed after recreating cwd"
3766 }
3767 run_test 48a "Access renamed working dir (should return errors)="
3768
3769 test_48b() { # bug 2399
3770         check_kernel_version 34 || return 0
3771         rm -rf $DIR/$tdir
3772         test_mkdir -p $DIR/$tdir
3773         cd $DIR/$tdir
3774         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3775         touch foo && error "'touch foo' worked after removing cwd"
3776         test_mkdir $DIR/$tdir/foo &&
3777                      error "'mkdir foo' worked after removing cwd"
3778         if check_kernel_version 44; then
3779                 touch .foo && error "'touch .foo' worked after removing cwd"
3780                 test_mkdir $DIR/$tdir/.foo &&
3781                               error "'mkdir .foo' worked after removing cwd"
3782         fi
3783         ls . > /dev/null && error "'ls .' worked after removing cwd"
3784         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3785         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3786         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3787         rmdir . && error "'rmdir .' worked after removing cwd"
3788         ln -s . foo && error "'ln -s .' worked after removing cwd"
3789         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3790 }
3791 run_test 48b "Access removed working dir (should return errors)="
3792
3793 test_48c() { # bug 2350
3794         check_kernel_version 36 || return 0
3795         #lctl set_param debug=-1
3796         #set -vx
3797         rm -rf $DIR/$tdir
3798         test_mkdir -p $DIR/$tdir/dir
3799         cd $DIR/$tdir/dir
3800         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3801         $TRACE touch foo && error "touch foo worked after removing cwd"
3802         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3803         if check_kernel_version 44; then
3804                 touch .foo && error "touch .foo worked after removing cwd"
3805                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3806         fi
3807         $TRACE ls . && error "'ls .' worked after removing cwd"
3808         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3809         is_patchless || ( $TRACE cd . &&
3810                         error "'cd .' worked after removing cwd" )
3811         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3812         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3813         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3814         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3815 }
3816 run_test 48c "Access removed working subdir (should return errors)"
3817
3818 test_48d() { # bug 2350
3819         check_kernel_version 36 || return 0
3820         #lctl set_param debug=-1
3821         #set -vx
3822         rm -rf $DIR/$tdir
3823         test_mkdir -p $DIR/$tdir/dir
3824         cd $DIR/$tdir/dir
3825         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3826         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3827         $TRACE touch foo && error "'touch foo' worked after removing parent"
3828         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3829         if check_kernel_version 44; then
3830                 touch .foo && error "'touch .foo' worked after removing parent"
3831                 test_mkdir .foo &&
3832                               error "mkdir .foo worked after removing parent"
3833         fi
3834         $TRACE ls . && error "'ls .' worked after removing parent"
3835         $TRACE ls .. && error "'ls ..' worked after removing parent"
3836         is_patchless || ( $TRACE cd . &&
3837                         error "'cd .' worked after recreate parent" )
3838         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3839         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3840         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3841         is_patchless || ( $TRACE cd .. &&
3842                         error "'cd ..' worked after removing parent" || true )
3843 }
3844 run_test 48d "Access removed parent subdir (should return errors)"
3845
3846 test_48e() { # bug 4134
3847         check_kernel_version 41 || return 0
3848         #lctl set_param debug=-1
3849         #set -vx
3850         rm -rf $DIR/$tdir
3851         test_mkdir -p $DIR/$tdir/dir
3852         cd $DIR/$tdir/dir
3853         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3854         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3855         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3856         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3857         # On a buggy kernel addition of "touch foo" after cd .. will
3858         # produce kernel oops in lookup_hash_it
3859         touch ../foo && error "'cd ..' worked after recreate parent"
3860         cd $DIR
3861         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3862 }
3863 run_test 48e "Access to recreated parent subdir (should return errors)"
3864
3865 test_49() { # LU-1030
3866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3867         # get ost1 size - lustre-OST0000
3868         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3869                 awk '{ print $4 }')
3870         # write 800M at maximum
3871         [[ $ost1_size -lt 2 ]] && ost1_size=2
3872         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3873
3874         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3875         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3876         local dd_pid=$!
3877
3878         # change max_pages_per_rpc while writing the file
3879         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3880         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3881         # loop until dd process exits
3882         while ps ax -opid | grep -wq $dd_pid; do
3883                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3884                 sleep $((RANDOM % 5 + 1))
3885         done
3886         # restore original max_pages_per_rpc
3887         $LCTL set_param $osc1_mppc=$orig_mppc
3888         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3889 }
3890 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3891
3892 test_50() {
3893         # bug 1485
3894         test_mkdir $DIR/$tdir
3895         cd $DIR/$tdir
3896         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3897 }
3898 run_test 50 "special situations: /proc symlinks  ==============="
3899
3900 test_51a() {    # was test_51
3901         # bug 1516 - create an empty entry right after ".." then split dir
3902         test_mkdir -c1 $DIR/$tdir
3903         touch $DIR/$tdir/foo
3904         $MCREATE $DIR/$tdir/bar
3905         rm $DIR/$tdir/foo
3906         createmany -m $DIR/$tdir/longfile 201
3907         FNUM=202
3908         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3909                 $MCREATE $DIR/$tdir/longfile$FNUM
3910                 FNUM=$(($FNUM + 1))
3911                 echo -n "+"
3912         done
3913         echo
3914         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3915 }
3916 run_test 51a "special situations: split htree with empty entry =="
3917
3918 export NUMTEST=70000
3919 test_51b() {
3920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3921         local BASE=$DIR/d${base}.${TESTSUITE}
3922
3923         # cleanup the directory
3924         rm -fr $BASE
3925
3926         test_mkdir -p -c1 $BASE
3927
3928         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3929         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3930         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3931                 return
3932
3933         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3934                 echo "reduced count to $NUMTEST due to inodes"
3935
3936         # need to check free space for the directories as well
3937         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3938         numfree=$((blkfree / 4))
3939         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3940                 echo "reduced count to $NUMTEST due to blocks"
3941
3942         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3943                 echo "failed" > $BASE/fnum
3944 }
3945 run_test 51b "exceed 64k subdirectory nlink limit"
3946
3947 test_51ba() { # LU-993
3948         local BASE=$DIR/d${base}.${TESTSUITE}
3949         # unlink all but 100 subdirectories, then check it still works
3950         local LEFT=100
3951         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3952
3953         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3954         local DELETE=$((NUMTEST - LEFT))
3955
3956         # continue on to run this test even if 51b didn't finish,
3957         # just to delete the many subdirectories created.
3958         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3959
3960         # for ldiskfs the nlink count should be 1, but this is OSD specific
3961         # and so this is listed for informational purposes only
3962         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3963         unlinkmany -d $BASE/d $DELETE
3964         RC=$?
3965
3966         if [ $RC -ne 0 ]; then
3967                 if [ "$NUMPREV" == "failed" ]; then
3968                         skip "previous setup failed"
3969                         return 0
3970                 else
3971                         error "unlink of first $DELETE subdirs failed"
3972                         return $RC
3973                 fi
3974         fi
3975
3976         echo "nlink between: $(stat -c %h $BASE)"
3977         # trim the first line of ls output
3978         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3979         [ $FOUND -ne $LEFT ] &&
3980                 error "can't find subdirs: found only $FOUND/$LEFT"
3981
3982         unlinkmany -d $BASE/d $DELETE $LEFT ||
3983                 error "unlink of second $LEFT subdirs failed"
3984         # regardless of whether the backing filesystem tracks nlink accurately
3985         # or not, the nlink count shouldn't be more than "." and ".." here
3986         local AFTER=$(stat -c %h $BASE)
3987         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
3988                 echo "nlink after: $AFTER"
3989 }
3990 run_test 51ba "verify nlink for many subdirectory cleanup"
3991
3992 test_51d() {
3993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3994         [[ $OSTCOUNT -lt 3 ]] &&
3995                 skip_env "skipping test with few OSTs" && return
3996         test_mkdir -p $DIR/$tdir
3997         createmany -o $DIR/$tdir/t- 1000
3998         $GETSTRIPE $DIR/$tdir > $TMP/files
3999         for N in $(seq 0 $((OSTCOUNT - 1))); do
4000                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4001                         END { printf("%0.0f", objs) }' $TMP/files)
4002                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4003                         '($1 == '$N') { objs += 1 } \
4004                         END { printf("%0.0f", objs) }')
4005                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4006         done
4007         unlinkmany $DIR/$tdir/t- 1000
4008
4009         NLAST=0
4010         for N in $(seq 1 $((OSTCOUNT - 1))); do
4011                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4012                         error "OST $N has less objects vs OST $NLAST" \
4013                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4014                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4015                         error "OST $N has less objects vs OST $NLAST" \
4016                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4017
4018                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4019                         error "OST $N has less #0 objects vs OST $NLAST" \
4020                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4021                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4022                         error "OST $N has less #0 objects vs OST $NLAST" \
4023                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4024                 NLAST=$N
4025         done
4026 }
4027 run_test 51d "check object distribution ===================="
4028
4029 test_52a() {
4030         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4031         test_mkdir -p $DIR/$tdir
4032         touch $DIR/$tdir/foo
4033         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4034         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4035         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4036         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4037         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4038                                         error "link worked"
4039         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4040         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4041         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4042                                                      error "lsattr"
4043         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4044         cp -r $DIR/$tdir /tmp/
4045         rm -fr $DIR/$tdir || error "cleanup rm failed"
4046 }
4047 run_test 52a "append-only flag test (should return errors) ====="
4048
4049 test_52b() {
4050         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4051         test_mkdir -p $DIR/$tdir
4052         touch $DIR/$tdir/foo
4053         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4054         cat test > $DIR/$tdir/foo && error "cat test worked"
4055         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4056         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4057         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4058                                         error "link worked"
4059         echo foo >> $DIR/$tdir/foo && error "echo worked"
4060         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4061         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4062         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4063         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4064                                                         error "lsattr"
4065         chattr -i $DIR/$tdir/foo || error "chattr failed"
4066
4067         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4068 }
4069 run_test 52b "immutable flag test (should return errors) ======="
4070
4071 test_53() {
4072         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4073         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4074         remote_ost_nodsh && skip "remote OST with nodsh" && return
4075
4076         local param
4077         local param_seq
4078         local ostname
4079         local mds_last
4080         local mds_last_seq
4081         local ost_last
4082         local ost_last_seq
4083         local ost_last_id
4084         local ostnum
4085         local node
4086         local found=false
4087         local support_last_seq=true
4088
4089         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4090                 support_last_seq=false
4091
4092         # only test MDT0000
4093         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4094         local value
4095         for value in $(do_facet $SINGLEMDS \
4096                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4097                 param=$(echo ${value[0]} | cut -d "=" -f1)
4098                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4099
4100                 if $support_last_seq; then
4101                         param_seq=$(echo $param |
4102                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4103                         mds_last_seq=$(do_facet $SINGLEMDS \
4104                                        $LCTL get_param -n $param_seq)
4105                 fi
4106                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4107
4108                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4109                 node=$(facet_active_host ost$((ostnum+1)))
4110                 param="obdfilter.$ostname.last_id"
4111                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4112                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4113                         ost_last_id=$ost_last
4114
4115                         if $support_last_seq; then
4116                                 ost_last_id=$(echo $ost_last |
4117                                               awk -F':' '{print $2}' |
4118                                               sed -e "s/^0x//g")
4119                                 ost_last_seq=$(echo $ost_last |
4120                                                awk -F':' '{print $1}')
4121                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4122                         fi
4123
4124                         if [[ $ost_last_id != $mds_last ]]; then
4125                                 error "$ost_last_id != $mds_last"
4126                         else
4127                                 found=true
4128                                 break
4129                         fi
4130                 done
4131         done
4132         $found || error "can not match last_seq/last_id for $mdtosc"
4133         return 0
4134 }
4135 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4136
4137 test_54a() {
4138         $SOCKETSERVER $DIR/socket ||
4139                 error "$SOCKETSERVER $DIR/socket failed: $?"
4140         $SOCKETCLIENT $DIR/socket ||
4141                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4142         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4143 }
4144 run_test 54a "unix domain socket test =========================="
4145
4146 test_54b() {
4147         f="$DIR/f54b"
4148         mknod $f c 1 3
4149         chmod 0666 $f
4150         dd if=/dev/zero of=$f bs=$(page_size) count=1
4151 }
4152 run_test 54b "char device works in lustre ======================"
4153
4154 find_loop_dev() {
4155         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4156         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4157         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4158
4159         for i in $(seq 3 7); do
4160                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4161                 LOOPDEV=$LOOPBASE$i
4162                 LOOPNUM=$i
4163                 break
4164         done
4165 }
4166
4167 cleanup_54c() {
4168         loopdev="$DIR/loop54c"
4169
4170         trap 0
4171         $UMOUNT -d $tdir || rc=$?
4172         losetup -d $loopdev || true
4173         rm $loopdev
4174         return $rc
4175 }
4176
4177 test_54c() {
4178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4179         tfile="$DIR/f54c"
4180         tdir="$DIR/d54c"
4181         loopdev="$DIR/loop54c"
4182
4183         find_loop_dev
4184         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4185         mknod $loopdev b 7 $LOOPNUM
4186         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
4187         dd if=/dev/zero of=$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4188         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
4189         trap cleanup_54c EXIT
4190         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4191         test_mkdir -p $tdir
4192         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
4193         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
4194         df $tdir
4195         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
4196         cleanup_54c
4197 }
4198 run_test 54c "block device works in lustre ====================="
4199
4200 test_54d() {
4201         f="$DIR/f54d"
4202         string="aaaaaa"
4203         mknod $f p
4204         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4205 }
4206 run_test 54d "fifo device works in lustre ======================"
4207
4208 test_54e() {
4209         check_kernel_version 46 || return 0
4210         f="$DIR/f54e"
4211         string="aaaaaa"
4212         cp -aL /dev/console $f
4213         echo $string > $f || error "echo $string to $f failed"
4214 }
4215 run_test 54e "console/tty device works in lustre ======================"
4216
4217 #The test_55 used to be iopen test and it was removed by bz#24037.
4218 #run_test 55 "check iopen_connect_dentry() ======================"
4219
4220 test_56a() {    # was test_56
4221         rm -rf $DIR/$tdir
4222         $SETSTRIPE -d $DIR
4223         test_mkdir -p $DIR/$tdir/dir
4224         NUMFILES=3
4225         NUMFILESx2=$(($NUMFILES * 2))
4226         for i in $(seq 1 $NUMFILES); do
4227                 touch $DIR/$tdir/file$i
4228                 touch $DIR/$tdir/dir/file$i
4229         done
4230
4231         # test lfs getstripe with --recursive
4232         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4233         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4234                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4235         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4236         [[ $FILENUM -eq $NUMFILES ]] ||
4237                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4238         echo "$GETSTRIPE --recursive passed."
4239
4240         # test lfs getstripe with file instead of dir
4241         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4242         [[ $FILENUM -eq 1 ]] ||
4243                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4244         echo "$GETSTRIPE file1 passed."
4245
4246         #test lfs getstripe with --verbose
4247         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4248                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4249                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4250         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4251                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4252         echo "$GETSTRIPE --verbose passed."
4253
4254         #test lfs getstripe with --obd
4255         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4256                 grep -q "unknown obduuid" ||
4257                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4258
4259         [[ $OSTCOUNT -lt 2 ]] &&
4260                 skip_env "skipping other $GETSTRIPE --obd test" && return
4261
4262         OSTIDX=1
4263         OBDUUID=$(ostuuid_from_index $OSTIDX)
4264         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4265         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4266         [[ $FOUND -eq $FILENUM ]] ||
4267                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4268         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4269                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4270                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4271                 error "$GETSTRIPE --obd: should not show file on other obd"
4272         echo "$GETSTRIPE --obd passed"
4273 }
4274 run_test 56a "check $GETSTRIPE"
4275
4276 NUMFILES=3
4277 NUMDIRS=3
4278 setup_56() {
4279         local LOCAL_NUMFILES="$1"
4280         local LOCAL_NUMDIRS="$2"
4281         local MKDIR_PARAMS="$3"
4282         local DIR_STRIPE_PARAMS="$4"
4283
4284         if [ ! -d "$TDIR" ] ; then
4285                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4286                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4287                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4288                         touch $TDIR/file$i
4289                 done
4290                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4291                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4292                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4293                                 touch $TDIR/dir$i/file$j
4294                         done
4295                 done
4296         fi
4297 }
4298
4299 setup_56_special() {
4300         LOCAL_NUMFILES=$1
4301         LOCAL_NUMDIRS=$2
4302         setup_56 $1 $2
4303         if [ ! -e "$TDIR/loop1b" ] ; then
4304                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4305                         mknod $TDIR/loop${i}b b 7 $i
4306                         mknod $TDIR/null${i}c c 1 3
4307                         ln -s $TDIR/file1 $TDIR/link${i}l
4308                 done
4309                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4310                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4311                         mknod $TDIR/dir$i/null${i}c c 1 3
4312                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4313                 done
4314         fi
4315 }
4316
4317 test_56g() {
4318         $SETSTRIPE -d $DIR
4319
4320         TDIR=$DIR/${tdir}g
4321         setup_56 $NUMFILES $NUMDIRS
4322
4323         EXPECTED=$(($NUMDIRS + 2))
4324         # test lfs find with -name
4325         for i in $(seq 1 $NUMFILES) ; do
4326                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4327                 [ $NUMS -eq $EXPECTED ] ||
4328                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4329                               "found $NUMS, expected $EXPECTED"
4330         done
4331 }
4332 run_test 56g "check lfs find -name ============================="
4333
4334 test_56h() {
4335         $SETSTRIPE -d $DIR
4336
4337         TDIR=$DIR/${tdir}g
4338         setup_56 $NUMFILES $NUMDIRS
4339
4340         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4341         # test lfs find with ! -name
4342         for i in $(seq 1 $NUMFILES) ; do
4343                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4344                 [ $NUMS -eq $EXPECTED ] ||
4345                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4346                               "found $NUMS, expected $EXPECTED"
4347         done
4348 }
4349 run_test 56h "check lfs find ! -name ============================="
4350
4351 test_56i() {
4352        tdir=${tdir}i
4353        test_mkdir -p $DIR/$tdir
4354        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4355        CMD="$LFIND -ost $UUID $DIR/$tdir"
4356        OUT=$($CMD)
4357        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4358 }
4359 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4360
4361 test_56j() {
4362         TDIR=$DIR/${tdir}g
4363         setup_56_special $NUMFILES $NUMDIRS
4364
4365         EXPECTED=$((NUMDIRS + 1))
4366         CMD="$LFIND -type d $TDIR"
4367         NUMS=$($CMD | wc -l)
4368         [ $NUMS -eq $EXPECTED ] ||
4369                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4370 }
4371 run_test 56j "check lfs find -type d ============================="
4372
4373 test_56k() {
4374         TDIR=$DIR/${tdir}g
4375         setup_56_special $NUMFILES $NUMDIRS
4376
4377         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4378         CMD="$LFIND -type f $TDIR"
4379         NUMS=$($CMD | wc -l)
4380         [ $NUMS -eq $EXPECTED ] ||
4381                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4382 }
4383 run_test 56k "check lfs find -type f ============================="
4384
4385 test_56l() {
4386         TDIR=$DIR/${tdir}g
4387         setup_56_special $NUMFILES $NUMDIRS
4388
4389         EXPECTED=$((NUMDIRS + NUMFILES))
4390         CMD="$LFIND -type b $TDIR"
4391         NUMS=$($CMD | wc -l)
4392         [ $NUMS -eq $EXPECTED ] ||
4393                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4394 }
4395 run_test 56l "check lfs find -type b ============================="
4396
4397 test_56m() {
4398         TDIR=$DIR/${tdir}g
4399         setup_56_special $NUMFILES $NUMDIRS
4400
4401         EXPECTED=$((NUMDIRS + NUMFILES))
4402         CMD="$LFIND -type c $TDIR"
4403         NUMS=$($CMD | wc -l)
4404         [ $NUMS -eq $EXPECTED ] ||
4405                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4406 }
4407 run_test 56m "check lfs find -type c ============================="
4408
4409 test_56n() {
4410         TDIR=$DIR/${tdir}g
4411         setup_56_special $NUMFILES $NUMDIRS
4412
4413         EXPECTED=$((NUMDIRS + NUMFILES))
4414         CMD="$LFIND -type l $TDIR"
4415         NUMS=$($CMD | wc -l)
4416         [ $NUMS -eq $EXPECTED ] ||
4417                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4418 }
4419 run_test 56n "check lfs find -type l ============================="
4420
4421 test_56o() {
4422         TDIR=$DIR/${tdir}o
4423         setup_56 $NUMFILES $NUMDIRS
4424         utime $TDIR/file1 > /dev/null || error "utime (1)"
4425         utime $TDIR/file2 > /dev/null || error "utime (2)"
4426         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4427         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4428         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4429         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4430
4431         EXPECTED=4
4432         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4433         [ $NUMS -eq $EXPECTED ] || \
4434                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4435
4436         EXPECTED=12
4437         CMD="$LFIND -mtime 0 $TDIR"
4438         NUMS=$($CMD | wc -l)
4439         [ $NUMS -eq $EXPECTED ] ||
4440                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4441 }
4442 run_test 56o "check lfs find -mtime for old files =========================="
4443
4444 test_56p() {
4445         [ $RUNAS_ID -eq $UID ] &&
4446                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4447
4448         TDIR=$DIR/${tdir}p
4449         setup_56 $NUMFILES $NUMDIRS
4450
4451         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4452         EXPECTED=$NUMFILES
4453         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4454         NUMS=$($CMD | wc -l)
4455         [ $NUMS -eq $EXPECTED ] || \
4456                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4457
4458         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4459         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4460         NUMS=$($CMD | wc -l)
4461         [ $NUMS -eq $EXPECTED ] || \
4462                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4463 }
4464 run_test 56p "check lfs find -uid and ! -uid ==============================="
4465
4466 test_56q() {
4467         [ $RUNAS_ID -eq $UID ] &&
4468                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4469
4470         TDIR=$DIR/${tdir}q
4471         setup_56 $NUMFILES $NUMDIRS
4472
4473         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4474
4475         EXPECTED=$NUMFILES
4476         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4477         NUMS=$($CMD | wc -l)
4478         [ $NUMS -eq $EXPECTED ] ||
4479                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4480
4481         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4482         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4483         NUMS=$($CMD | wc -l)
4484         [ $NUMS -eq $EXPECTED ] ||
4485                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4486 }
4487 run_test 56q "check lfs find -gid and ! -gid ==============================="
4488
4489 test_56r() {
4490         TDIR=$DIR/${tdir}r
4491         setup_56 $NUMFILES $NUMDIRS
4492
4493         EXPECTED=12
4494         CMD="$LFIND -size 0 -type f $TDIR"
4495         NUMS=$($CMD | wc -l)
4496         [ $NUMS -eq $EXPECTED ] ||
4497                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4498         EXPECTED=0
4499         CMD="$LFIND ! -size 0 -type f $TDIR"
4500         NUMS=$($CMD | wc -l)
4501         [ $NUMS -eq $EXPECTED ] ||
4502                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4503         echo "test" > $TDIR/$tfile
4504         echo "test2" > $TDIR/$tfile.2 && sync
4505         EXPECTED=1
4506         CMD="$LFIND -size 5 -type f $TDIR"
4507         NUMS=$($CMD | wc -l)
4508         [ $NUMS -eq $EXPECTED ] ||
4509                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4510         EXPECTED=1
4511         CMD="$LFIND -size +5 -type f $TDIR"
4512         NUMS=$($CMD | wc -l)
4513         [ $NUMS -eq $EXPECTED ] ||
4514                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4515         EXPECTED=2
4516         CMD="$LFIND -size +0 -type f $TDIR"
4517         NUMS=$($CMD | wc -l)
4518         [ $NUMS -eq $EXPECTED ] ||
4519                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4520         EXPECTED=2
4521         CMD="$LFIND ! -size -5 -type f $TDIR"
4522         NUMS=$($CMD | wc -l)
4523         [ $NUMS -eq $EXPECTED ] ||
4524                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4525         EXPECTED=12
4526         CMD="$LFIND -size -5 -type f $TDIR"
4527         NUMS=$($CMD | wc -l)
4528         [ $NUMS -eq $EXPECTED ] ||
4529                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4530 }
4531 run_test 56r "check lfs find -size works =========================="
4532
4533 test_56s() { # LU-611
4534         TDIR=$DIR/${tdir}s
4535         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4536
4537         if [[ $OSTCOUNT -gt 1 ]]; then
4538                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4539                 ONESTRIPE=4
4540                 EXTRA=4
4541         else
4542                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4543                 EXTRA=0
4544         fi
4545
4546         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4547         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4548         NUMS=$($CMD | wc -l)
4549         [ $NUMS -eq $EXPECTED ] || {
4550                 $GETSTRIPE -R $TDIR
4551                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4552         }
4553
4554         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4555         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4556         NUMS=$($CMD | wc -l)
4557         [ $NUMS -eq $EXPECTED ] || {
4558                 $GETSTRIPE -R $TDIR
4559                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4560         }
4561
4562         EXPECTED=$ONESTRIPE
4563         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4564         NUMS=$($CMD | wc -l)
4565         [ $NUMS -eq $EXPECTED ] || {
4566                 $GETSTRIPE -R $TDIR
4567                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4568         }
4569
4570         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4571         NUMS=$($CMD | wc -l)
4572         [ $NUMS -eq $EXPECTED ] || {
4573                 $GETSTRIPE -R $TDIR
4574                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4575         }
4576
4577         EXPECTED=0
4578         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4579         NUMS=$($CMD | wc -l)
4580         [ $NUMS -eq $EXPECTED ] || {
4581                 $GETSTRIPE -R $TDIR
4582                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4583         }
4584 }
4585 run_test 56s "check lfs find -stripe-count works"
4586
4587 test_56t() { # LU-611
4588         TDIR=$DIR/${tdir}t
4589         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4590
4591         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4592
4593         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4594         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4595         NUMS=$($CMD | wc -l)
4596         [ $NUMS -eq $EXPECTED ] ||
4597                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4598
4599         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4600         NUMS=$($CMD | wc -l)
4601         [ $NUMS -eq $EXPECTED ] ||
4602                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4603
4604         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4605         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4606         NUMS=$($CMD | wc -l)
4607         [ $NUMS -eq $EXPECTED ] ||
4608                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4609
4610         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4611         NUMS=$($CMD | wc -l)
4612         [ $NUMS -eq $EXPECTED ] ||
4613                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4614
4615         EXPECTED=4
4616         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4617         NUMS=$($CMD | wc -l)
4618         [ $NUMS -eq $EXPECTED ] ||
4619                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4620
4621         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4622         NUMS=$($CMD | wc -l)
4623         [ $NUMS -eq $EXPECTED ] ||
4624                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4625
4626         EXPECTED=0
4627         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4628         NUMS=$($CMD | wc -l)
4629         [ $NUMS -eq $EXPECTED ] ||
4630                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4631 }
4632 run_test 56t "check lfs find -stripe-size works"
4633
4634 test_56u() { # LU-611
4635         TDIR=$DIR/${tdir}u
4636         setup_56 $NUMFILES $NUMDIRS "-i 0"
4637
4638         if [[ $OSTCOUNT -gt 1 ]]; then
4639                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4640                 ONESTRIPE=4
4641         else
4642                 ONESTRIPE=0
4643         fi
4644
4645         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4646         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4647         NUMS=$($CMD | wc -l)
4648         [ $NUMS -eq $EXPECTED ] ||
4649                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4650
4651         EXPECTED=$ONESTRIPE
4652         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4653         NUMS=$($CMD | wc -l)
4654         [ $NUMS -eq $EXPECTED ] ||
4655                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4656
4657         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4658         NUMS=$($CMD | wc -l)
4659         [ $NUMS -eq $EXPECTED ] ||
4660                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4661
4662         EXPECTED=0
4663         # This should produce an error and not return any files
4664         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4665         NUMS=$($CMD 2>/dev/null | wc -l)
4666         [ $NUMS -eq $EXPECTED ] ||
4667                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4668
4669         if [[ $OSTCOUNT -gt 1 ]]; then
4670                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4671                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4672                 NUMS=$($CMD | wc -l)
4673                 [ $NUMS -eq $EXPECTED ] ||
4674                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4675         fi
4676 }
4677 run_test 56u "check lfs find -stripe-index works"
4678
4679 test_56v() {
4680     local MDT_IDX=0
4681
4682     TDIR=$DIR/${tdir}v
4683     rm -rf $TDIR
4684     setup_56 $NUMFILES $NUMDIRS
4685
4686     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4687     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4688
4689     for file in $($LFIND -mdt $UUID $TDIR); do
4690         file_mdt_idx=$($GETSTRIPE -M $file)
4691         [ $file_mdt_idx -eq $MDT_IDX ] ||
4692             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4693     done
4694 }
4695 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4696
4697 # Get and check the actual stripe count of one file.
4698 # Usage: check_stripe_count <file> <expected_stripe_count>
4699 check_stripe_count() {
4700     local file=$1
4701     local expected=$2
4702     local actual
4703
4704     [[ -z "$file" || -z "$expected" ]] &&
4705         error "check_stripe_count: invalid argument!"
4706
4707     local cmd="$GETSTRIPE -c $file"
4708     actual=$($cmd) || error "$cmd failed"
4709     actual=${actual%% *}
4710
4711     if [[ $actual -ne $expected ]]; then
4712         [[ $expected -eq -1 ]] ||
4713             error "$cmd wrong: found $actual, expected $expected"
4714         [[ $actual -eq $OSTCOUNT ]] ||
4715             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4716     fi
4717 }
4718
4719 test_56w() {
4720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4721         TDIR=$DIR/${tdir}w
4722
4723     rm -rf $TDIR || error "remove $TDIR failed"
4724     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4725
4726     local stripe_size
4727     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4728         error "$GETSTRIPE -S -d $TDIR failed"
4729     stripe_size=${stripe_size%% *}
4730
4731     local file_size=$((stripe_size * OSTCOUNT))
4732     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4733     local required_space=$((file_num * file_size))
4734     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4735     [[ $free_space -le $((required_space / 1024)) ]] &&
4736         skip_env "need at least $required_space bytes free space," \
4737                  "have $free_space kbytes" && return
4738
4739     local dd_bs=65536
4740     local dd_count=$((file_size / dd_bs))
4741
4742     # write data into the files
4743     local i
4744     local j
4745     local file
4746     for i in $(seq 1 $NUMFILES); do
4747         file=$TDIR/file$i
4748         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4749             error "write data into $file failed"
4750     done
4751     for i in $(seq 1 $NUMDIRS); do
4752         for j in $(seq 1 $NUMFILES); do
4753             file=$TDIR/dir$i/file$j
4754             yes | dd bs=$dd_bs count=$dd_count of=$file \
4755                 >/dev/null 2>&1 ||
4756                 error "write data into $file failed"
4757         done
4758     done
4759
4760     local expected=-1
4761     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4762
4763     # lfs_migrate file
4764     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4765     echo "$cmd"
4766     eval $cmd || error "$cmd failed"
4767
4768     check_stripe_count $TDIR/file1 $expected
4769
4770     # lfs_migrate dir
4771     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4772     echo "$cmd"
4773     eval $cmd || error "$cmd failed"
4774
4775     for j in $(seq 1 $NUMFILES); do
4776         check_stripe_count $TDIR/dir1/file$j $expected
4777     done
4778
4779     # lfs_migrate works with lfs find
4780     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4781          $LFS_MIGRATE -y -c $expected"
4782     echo "$cmd"
4783     eval $cmd || error "$cmd failed"
4784
4785     for i in $(seq 2 $NUMFILES); do
4786         check_stripe_count $TDIR/file$i $expected
4787     done
4788     for i in $(seq 2 $NUMDIRS); do
4789         for j in $(seq 1 $NUMFILES); do
4790             check_stripe_count $TDIR/dir$i/file$j $expected
4791         done
4792     done
4793 }
4794 run_test 56w "check lfs_migrate -c stripe_count works"
4795
4796 test_56x() {
4797         check_swap_layouts_support && return 0
4798         [[ $OSTCOUNT -lt 2 ]] &&
4799                 skip_env "need 2 OST, skipping test" && return
4800
4801         local dir0=$DIR/$tdir/$testnum
4802         mkdir -p $dir0 || error "creating dir $dir0"
4803
4804         local ref1=/etc/passwd
4805         local file1=$dir0/file1
4806
4807         $SETSTRIPE -c 2 $file1
4808         cp $ref1 $file1
4809         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4810         stripe=$($GETSTRIPE -c $file1)
4811         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4812         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4813
4814         # clean up
4815         rm -f $file1
4816 }
4817 run_test 56x "lfs migration support"
4818
4819 test_56y() {
4820         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4821                 skip "No HSM support on MDS of $(get_lustre_version)," \
4822                          "need 2.4.53 at least" && return
4823         local res=""
4824
4825         local dir0=$DIR/$tdir/$testnum
4826         mkdir -p $dir0 || error "creating dir $dir0"
4827         local f1=$dir0/file1
4828         local f2=$dir0/file2
4829
4830         touch $f1 || error "creating std file $f1"
4831         $MULTIOP $f2 H2c || error "creating released file $f2"
4832
4833         # a directory can be raid0, so ask only for files
4834         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4835         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4836
4837         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4838         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4839
4840         # only files can be released, so no need to force file search
4841         res=$($LFIND $dir0 -L released)
4842         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4843
4844         res=$($LFIND $dir0 \! -L released)
4845         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4846
4847 }
4848 run_test 56y "lfs find -L raid0|released"
4849
4850 test_57a() {
4851         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4852         # note test will not do anything if MDS is not local
4853         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4854                 skip "Only applicable to ldiskfs-based MDTs"
4855                 return
4856         fi
4857
4858         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4859         local MNTDEV="osd*.*MDT*.mntdev"
4860         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4861         [ -z "$DEV" ] && error "can't access $MNTDEV"
4862         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4863                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4864                         error "can't access $DEV"
4865                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4866                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4867                 rm $TMP/t57a.dump
4868         done
4869 }
4870 run_test 57a "verify MDS filesystem created with large inodes =="
4871
4872 test_57b() {
4873         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4874         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4875                 skip "Only applicable to ldiskfs-based MDTs"
4876                 return
4877         fi
4878
4879         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4880         local dir=$DIR/d57b
4881
4882         local FILECOUNT=100
4883         local FILE1=$dir/f1
4884         local FILEN=$dir/f$FILECOUNT
4885
4886         rm -rf $dir || error "removing $dir"
4887         test_mkdir -p $dir || error "creating $dir"
4888         local num=$(get_mds_dir $dir)
4889         local mymds=mds$num
4890
4891         echo "mcreating $FILECOUNT files"
4892         createmany -m $dir/f 1 $FILECOUNT || \
4893                 error "creating files in $dir"
4894
4895         # verify that files do not have EAs yet
4896         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4897         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4898
4899         sync
4900         sleep 1
4901         df $dir  #make sure we get new statfs data
4902         local MDSFREE=$(do_facet $mymds \
4903                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4904         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4905         echo "opening files to create objects/EAs"
4906         local FILE
4907         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4908                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4909         done
4910
4911         # verify that files have EAs now
4912         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4913         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4914
4915         sleep 1  #make sure we get new statfs data
4916         df $dir
4917         local MDSFREE2=$(do_facet $mymds \
4918                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4919         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4920         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
4921                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4922                         error "MDC before $MDCFREE != after $MDCFREE2"
4923                 else
4924                         echo "MDC before $MDCFREE != after $MDCFREE2"
4925                         echo "unable to confirm if MDS has large inodes"
4926                 fi
4927         fi
4928         rm -rf $dir
4929 }
4930 run_test 57b "default LOV EAs are stored inside large inodes ==="
4931
4932 test_58() {
4933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4934         [ -z "$(which wiretest 2>/dev/null)" ] &&
4935                         skip_env "could not find wiretest" && return
4936         wiretest
4937 }
4938 run_test 58 "verify cross-platform wire constants =============="
4939
4940 test_59() {
4941         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4942         echo "touch 130 files"
4943         createmany -o $DIR/f59- 130
4944         echo "rm 130 files"
4945         unlinkmany $DIR/f59- 130
4946         sync
4947         # wait for commitment of removal
4948         wait_delete_completed
4949 }
4950 run_test 59 "verify cancellation of llog records async ========="
4951
4952 TEST60_HEAD="test_60 run $RANDOM"
4953 test_60a() {
4954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4955         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4956         do_facet mgs "! which run-llog.sh &> /dev/null" &&
4957                 skip_env "missing subtest run-llog.sh" && return
4958         log "$TEST60_HEAD - from kernel mode"
4959         do_facet mgs sh run-llog.sh
4960 }
4961 run_test 60a "llog sanity tests run from kernel module =========="
4962
4963 test_60b() { # bug 6411
4964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4965         dmesg > $DIR/$tfile
4966         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4967                                  /llog.test/ {
4968                                          if (marker)
4969                                                  from_marker++
4970                                          from_begin++
4971                                  }
4972                                  END {
4973                                          if (marker)
4974                                                  print from_marker
4975                                          else
4976                                                  print from_begin
4977                                  }"`
4978         [[ $LLOG_COUNT -gt 50 ]] &&
4979                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
4980 }
4981 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4982
4983 test_60c() {
4984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4985         echo "create 5000 files"
4986         createmany -o $DIR/f60c- 5000
4987 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4988         lctl set_param fail_loc=0x80000137
4989         unlinkmany $DIR/f60c- 5000
4990         lctl set_param fail_loc=0
4991 }
4992 run_test 60c "unlink file when mds full"
4993
4994 test_60d() {
4995         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4996         SAVEPRINTK=$(lctl get_param -n printk)
4997
4998         # verify "lctl mark" is even working"
4999         MESSAGE="test message ID $RANDOM $$"
5000         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5001         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5002
5003         lctl set_param printk=0 || error "set lnet.printk failed"
5004         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5005         MESSAGE="new test message ID $RANDOM $$"
5006         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5007         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5008         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5009
5010         lctl set_param -n printk="$SAVEPRINTK"
5011 }
5012 run_test 60d "test printk console message masking"
5013
5014 test_61() {
5015         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5016         f="$DIR/f61"
5017         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5018         cancel_lru_locks osc
5019         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5020         sync
5021 }
5022 run_test 61 "mmap() writes don't make sync hang ================"
5023
5024 # bug 2330 - insufficient obd_match error checking causes LBUG
5025 test_62() {
5026         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5027         f="$DIR/f62"
5028         echo foo > $f
5029         cancel_lru_locks osc
5030         lctl set_param fail_loc=0x405
5031         cat $f && error "cat succeeded, expect -EIO"
5032         lctl set_param fail_loc=0
5033 }
5034 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5035 # match every page all of the time.
5036 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5037
5038 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5039 # Though this test is irrelevant anymore, it helped to reveal some
5040 # other grant bugs (LU-4482), let's keep it.
5041 test_63a() {   # was test_63
5042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5043         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5044         for i in `seq 10` ; do
5045                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5046                 sleep 5
5047                 kill $!
5048                 sleep 1
5049         done
5050
5051         rm -f $DIR/f63 || true
5052 }
5053 run_test 63a "Verify oig_wait interruption does not crash ======="
5054
5055 # bug 2248 - async write errors didn't return to application on sync
5056 # bug 3677 - async write errors left page locked
5057 test_63b() {
5058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5059         debugsave
5060         lctl set_param debug=-1
5061
5062         # ensure we have a grant to do async writes
5063         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5064         rm $DIR/$tfile
5065
5066         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5067         lctl set_param fail_loc=0x80000406
5068         $MULTIOP $DIR/$tfile Owy && \
5069                 error "sync didn't return ENOMEM"
5070         sync; sleep 2; sync     # do a real sync this time to flush page
5071         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5072                 error "locked page left in cache after async error" || true
5073         debugrestore
5074 }
5075 run_test 63b "async write errors should be returned to fsync ==="
5076
5077 test_64a () {
5078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5079         df $DIR
5080         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5081 }
5082 run_test 64a "verify filter grant calculations (in kernel) ====="
5083
5084 test_64b () {
5085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5086         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5087 }
5088 run_test 64b "check out-of-space detection on client ==========="
5089
5090 test_64c() {
5091         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5092 }
5093 run_test 64c "verify grant shrink ========================------"
5094
5095 # bug 1414 - set/get directories' stripe info
5096 test_65a() {
5097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5098         test_mkdir -p $DIR/$tdir
5099         touch $DIR/$tdir/f1
5100         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5101 }
5102 run_test 65a "directory with no stripe info ===================="
5103
5104 test_65b() {
5105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5106         test_mkdir -p $DIR/$tdir
5107         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5108                                                 error "setstripe"
5109         touch $DIR/$tdir/f2
5110         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5111 }
5112 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5113
5114 test_65c() {
5115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5116         if [[ $OSTCOUNT -gt 1 ]]; then
5117                 test_mkdir -p $DIR/$tdir
5118                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5119                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5120                 touch $DIR/$tdir/f3
5121                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5122         fi
5123 }
5124 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5125
5126 test_65d() {
5127         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5128         test_mkdir -p $DIR/$tdir
5129         if [[ $STRIPECOUNT -le 0 ]]; then
5130                 sc=1
5131         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5132 #LOV_MAX_STRIPE_COUNT is 2000
5133                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5134         else
5135                 sc=$(($STRIPECOUNT - 1))
5136         fi
5137         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5138         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5139         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5140                 error "lverify failed"
5141 }
5142 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5143
5144 test_65e() {
5145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5146         test_mkdir -p $DIR/$tdir
5147
5148         $SETSTRIPE $DIR/$tdir || error "setstripe"
5149         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5150                                         error "no stripe info failed"
5151         touch $DIR/$tdir/f6
5152         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5153 }
5154 run_test 65e "directory setstripe defaults ======================="
5155
5156 test_65f() {
5157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5158         test_mkdir -p $DIR/${tdir}f
5159         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5160 }
5161 run_test 65f "dir setstripe permission (should return error) ==="
5162
5163 test_65g() {
5164         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5165         test_mkdir -p $DIR/$tdir
5166         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5167                                                         error "setstripe"
5168         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5169         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5170                 error "delete default stripe failed"
5171 }
5172 run_test 65g "directory setstripe -d ==========================="
5173
5174 test_65h() {
5175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5176         test_mkdir -p $DIR/$tdir
5177         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5178                                                         error "setstripe"
5179         test_mkdir -p $DIR/$tdir/dd1
5180         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5181                 error "stripe info inherit failed"
5182 }
5183 run_test 65h "directory stripe info inherit ===================="
5184
5185 test_65i() { # bug6367
5186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5187         $SETSTRIPE -S 65536 -c -1 $MOUNT
5188 }
5189 run_test 65i "set non-default striping on root directory (bug 6367)="
5190
5191 test_65ia() { # bug12836
5192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5193         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5194 }
5195 run_test 65ia "getstripe on -1 default directory striping"
5196
5197 test_65ib() { # bug12836
5198         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5199         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5200 }
5201 run_test 65ib "getstripe -v on -1 default directory striping"
5202
5203 test_65ic() { # bug12836
5204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5205         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5206 }
5207 run_test 65ic "new find on -1 default directory striping"
5208
5209 test_65j() { # bug6367
5210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5211         sync; sleep 1
5212         # if we aren't already remounting for each test, do so for this test
5213         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5214                 cleanup || error "failed to unmount"
5215                 setup
5216         fi
5217         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5218 }
5219 run_test 65j "set default striping on root directory (bug 6367)="
5220
5221 test_65k() { # bug11679
5222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5223         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5224         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5225
5226     echo "Check OST status: "
5227     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5228               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5229
5230     for OSC in $MDS_OSCS; do
5231         echo $OSC "is activate"
5232         do_facet $SINGLEMDS lctl --device %$OSC activate
5233     done
5234
5235     mkdir -p $DIR/$tdir
5236     for INACTIVE_OSC in $MDS_OSCS; do
5237         echo "Deactivate: " $INACTIVE_OSC
5238         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5239         for STRIPE_OSC in $MDS_OSCS; do
5240             OST=`osc_to_ost $STRIPE_OSC`
5241             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5242                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5243
5244             [ -f $DIR/$tdir/$IDX ] && continue
5245             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5246             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5247             RC=$?
5248             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5249         done
5250         rm -f $DIR/$tdir/*
5251         echo $INACTIVE_OSC "is Activate."
5252         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5253     done
5254 }
5255 run_test 65k "validate manual striping works properly with deactivated OSCs"
5256
5257 test_65l() { # bug 12836
5258         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5259         test_mkdir -p $DIR/$tdir/test_dir
5260         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5261         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5262 }
5263 run_test 65l "lfs find on -1 stripe dir ========================"
5264
5265 # bug 2543 - update blocks count on client
5266 test_66() {
5267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5268         COUNT=${COUNT:-8}
5269         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5270         sync; sync_all_data; sync; sync_all_data
5271         cancel_lru_locks osc
5272         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5273         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5274 }
5275 run_test 66 "update inode blocks count on client ==============="
5276
5277 LLOOP=
5278 LLITELOOPLOAD=
5279 cleanup_68() {
5280         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5281         trap 0
5282         if [ ! -z "$LLOOP" ]; then
5283                 if swapon -s | grep -q $LLOOP; then
5284                         swapoff $LLOOP || error "swapoff failed"
5285                 fi
5286
5287                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5288                 rm -f $LLOOP
5289                 unset LLOOP
5290         fi
5291         if [ ! -z "$LLITELOOPLOAD" ]; then
5292                 rmmod llite_lloop
5293                 unset LLITELOOPLOAD
5294         fi
5295         rm -f $DIR/f68*
5296 }
5297
5298 meminfo() {
5299         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5300 }
5301
5302 swap_used() {
5303         swapon -s | awk '($1 == "'$1'") { print $4 }'
5304 }
5305
5306 # test case for lloop driver, basic function
5307 test_68a() {
5308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5309         [ "$UID" != 0 ] && skip_env "must run as root" && return
5310         llite_lloop_enabled || \
5311                 { skip_env "llite_lloop module disabled" && return; }
5312
5313         trap cleanup_68 EXIT
5314
5315         if ! module_loaded llite_lloop; then
5316                 if load_module llite/llite_lloop; then
5317                         LLITELOOPLOAD=yes
5318                 else
5319                         skip_env "can't find module llite_lloop"
5320                         return
5321                 fi
5322         fi
5323
5324         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5325         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5326         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5327
5328         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5329         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5330
5331         cleanup_68
5332 }
5333 run_test 68a "lloop driver - basic test ========================"
5334
5335 # excercise swapping to lustre by adding a high priority swapfile entry
5336 # and then consuming memory until it is used.
5337 test_68b() {  # was test_68
5338         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5339         [ "$UID" != 0 ] && skip_env "must run as root" && return
5340         lctl get_param -n devices | grep -q obdfilter && \
5341                 skip "local OST" && return
5342
5343         grep -q llite_lloop /proc/modules
5344         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5345
5346         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5347                 skip "can't reliably test swap with TCP" && return
5348
5349         MEMTOTAL=`meminfo MemTotal`
5350         NR_BLOCKS=$((MEMTOTAL>>8))
5351         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5352
5353         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5354         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5355         mkswap $DIR/f68b
5356
5357         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5358
5359         trap cleanup_68 EXIT
5360
5361         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5362
5363         echo "before: `swapon -s | grep $LLOOP`"
5364         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5365         echo "after: `swapon -s | grep $LLOOP`"
5366         SWAPUSED=`swap_used $LLOOP`
5367
5368         cleanup_68
5369
5370         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5371 }
5372 run_test 68b "support swapping to Lustre ========================"
5373
5374 # bug5265, obdfilter oa2dentry return -ENOENT
5375 # #define OBD_FAIL_OST_ENOENT 0x217
5376 test_69() {
5377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5378         remote_ost_nodsh && skip "remote OST with nodsh" && return
5379
5380         f="$DIR/$tfile"
5381         $SETSTRIPE -c 1 -i 0 $f
5382
5383         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5384
5385         do_facet ost1 lctl set_param fail_loc=0x217
5386         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5387         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5388
5389         do_facet ost1 lctl set_param fail_loc=0
5390         $DIRECTIO write $f 0 2 || error "write error"
5391
5392         cancel_lru_locks osc
5393         $DIRECTIO read $f 0 1 || error "read error"
5394
5395         do_facet ost1 lctl set_param fail_loc=0x217
5396         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5397
5398         do_facet ost1 lctl set_param fail_loc=0
5399         rm -f $f
5400 }
5401 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5402
5403 test_71() {
5404     test_mkdir -p $DIR/$tdir
5405     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5406 }
5407 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5408
5409 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5410         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5411         [ "$RUNAS_ID" = "$UID" ] &&
5412                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5413
5414         # Check that testing environment is properly set up. Skip if not
5415         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5416                 skip_env "User $RUNAS_ID does not exist - skipping"
5417                 return 0
5418         }
5419         # We had better clear the $DIR to get enough space for dd
5420         rm -rf $DIR/*
5421         touch $DIR/$tfile
5422         chmod 777 $DIR/$tfile
5423         chmod ug+s $DIR/$tfile
5424         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5425                 error "$RUNAS dd $DIR/$tfile failed"
5426         # See if we are still setuid/sgid
5427         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5428                 error "S/gid is not dropped on write"
5429         # Now test that MDS is updated too
5430         cancel_lru_locks mdc
5431         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5432                 error "S/gid is not dropped on MDS"
5433         rm -f $DIR/$tfile
5434 }
5435 run_test 72a "Test that remove suid works properly (bug5695) ===="
5436
5437 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5438         local perm
5439
5440         [ "$RUNAS_ID" = "$UID" ] && \
5441                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5442         [ "$RUNAS_ID" -eq 0 ] && \
5443                 skip_env "RUNAS_ID = 0 -- skipping" && return
5444
5445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5446         # Check that testing environment is properly set up. Skip if not
5447         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5448                 skip_env "User $RUNAS_ID does not exist - skipping"
5449                 return 0
5450         }
5451         touch $DIR/${tfile}-f{g,u}
5452         test_mkdir $DIR/${tfile}-dg
5453         test_mkdir $DIR/${tfile}-du
5454         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5455         chmod g+s $DIR/${tfile}-{f,d}g
5456         chmod u+s $DIR/${tfile}-{f,d}u
5457         for perm in 777 2777 4777; do
5458                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5459                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5460                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5461                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5462         done
5463         true
5464 }
5465 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5466
5467 # bug 3462 - multiple simultaneous MDC requests
5468 test_73() {
5469         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5470         test_mkdir $DIR/d73-1
5471         test_mkdir $DIR/d73-2
5472         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5473         pid1=$!
5474
5475         lctl set_param fail_loc=0x80000129
5476         $MULTIOP $DIR/d73-1/f73-2 Oc &
5477         sleep 1
5478         lctl set_param fail_loc=0
5479
5480         $MULTIOP $DIR/d73-2/f73-3 Oc &
5481         pid3=$!
5482
5483         kill -USR1 $pid1
5484         wait $pid1 || return 1
5485
5486         sleep 25
5487
5488         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5489         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5490         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5491
5492         rm -rf $DIR/d73-*
5493 }
5494 run_test 73 "multiple MDC requests (should not deadlock)"
5495
5496 test_74a() { # bug 6149, 6184
5497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5498         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5499         #
5500         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5501         # will spin in a tight reconnection loop
5502         touch $DIR/f74a
5503         $LCTL set_param fail_loc=0x8000030e
5504         # get any lock that won't be difficult - lookup works.
5505         ls $DIR/f74a
5506         $LCTL set_param fail_loc=0
5507         rm -f $DIR/f74a
5508         true
5509 }
5510 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5511
5512 test_74b() { # bug 13310
5513         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5514         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5515         #
5516         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5517         # will spin in a tight reconnection loop
5518         $LCTL set_param fail_loc=0x8000030e
5519         # get a "difficult" lock
5520         touch $DIR/f74b
5521         $LCTL set_param fail_loc=0
5522         rm -f $DIR/f74b
5523         true
5524 }
5525 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5526
5527 test_74c() {
5528         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5529         #define OBD_FAIL_LDLM_NEW_LOCK
5530         $LCTL set_param fail_loc=0x319
5531         touch $DIR/$tfile && error "touch successful"
5532         $LCTL set_param fail_loc=0
5533         true
5534 }
5535 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5536
5537 num_inodes() {
5538         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5539 }
5540
5541 get_inode_slab_tunables() {
5542         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5543 }
5544
5545 set_inode_slab_tunables() {
5546         echo "lustre_inode_cache $1" > /proc/slabinfo
5547 }
5548
5549 test_76() { # Now for bug 20433, added originally in bug 1443
5550         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5551         local SLAB_SETTINGS=`get_inode_slab_tunables`
5552         local CPUS=`getconf _NPROCESSORS_ONLN`
5553         # we cannot set limit below 1 which means 1 inode in each
5554         # per-cpu cache is still allowed
5555         set_inode_slab_tunables "1 1 0"
5556         cancel_lru_locks osc
5557         BEFORE_INODES=$(num_inodes)
5558         echo "before inodes: $BEFORE_INODES"
5559         local COUNT=1000
5560         [ "$SLOW" = "no" ] && COUNT=100
5561         for i in $(seq $COUNT); do
5562                 touch $DIR/$tfile
5563                 rm -f $DIR/$tfile
5564         done
5565         cancel_lru_locks osc
5566         AFTER_INODES=$(num_inodes)
5567         echo "after inodes: $AFTER_INODES"
5568         local wait=0
5569         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5570                 sleep 2
5571                 AFTER_INODES=$(num_inodes)
5572                 wait=$((wait+2))
5573                 echo "wait $wait seconds inodes: $AFTER_INODES"
5574                 if [ $wait -gt 30 ]; then
5575                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5576                 fi
5577         done
5578         set_inode_slab_tunables "$SLAB_SETTINGS"
5579 }
5580 run_test 76 "confirm clients recycle inodes properly ===="
5581
5582
5583 export ORIG_CSUM=""
5584 set_checksums()
5585 {
5586         # Note: in sptlrpc modes which enable its own bulk checksum, the
5587         # original crc32_le bulk checksum will be automatically disabled,
5588         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5589         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5590         # In this case set_checksums() will not be no-op, because sptlrpc
5591         # bulk checksum will be enabled all through the test.
5592
5593         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5594         lctl set_param -n osc.*.checksums $1
5595         return 0
5596 }
5597
5598 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5599                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5600 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5601 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5602 set_checksum_type()
5603 {
5604         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5605         log "set checksum type to $1"
5606         return 0
5607 }
5608 F77_TMP=$TMP/f77-temp
5609 F77SZ=8
5610 setup_f77() {
5611         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5612                 error "error writing to $F77_TMP"
5613 }
5614
5615 test_77a() { # bug 10889
5616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5617         $GSS && skip "could not run with gss" && return
5618         [ ! -f $F77_TMP ] && setup_f77
5619         set_checksums 1
5620         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5621         set_checksums 0
5622         rm -f $DIR/$tfile
5623 }
5624 run_test 77a "normal checksum read/write operation"
5625
5626 test_77b() { # bug 10889
5627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5628         $GSS && skip "could not run with gss" && return
5629         [ ! -f $F77_TMP ] && setup_f77
5630         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5631         $LCTL set_param fail_loc=0x80000409
5632         set_checksums 1
5633
5634         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5635                 error "dd error: $?"
5636         $LCTL set_param fail_loc=0
5637
5638         for algo in $CKSUM_TYPES; do
5639                 cancel_lru_locks osc
5640                 set_checksum_type $algo
5641                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5642                 $LCTL set_param fail_loc=0x80000408
5643                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5644                 $LCTL set_param fail_loc=0
5645         done
5646         set_checksums 0
5647         set_checksum_type $ORIG_CSUM_TYPE
5648         rm -f $DIR/$tfile
5649 }
5650 run_test 77b "checksum error on client write, read"
5651
5652 test_77d() { # bug 10889
5653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5654         $GSS && skip "could not run with gss" && return
5655         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5656         $LCTL set_param fail_loc=0x80000409
5657         set_checksums 1
5658         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5659                 error "direct write: rc=$?"
5660         $LCTL set_param fail_loc=0
5661         set_checksums 0
5662
5663         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5664         $LCTL set_param fail_loc=0x80000408
5665         set_checksums 1
5666         cancel_lru_locks osc
5667         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5668                 error "direct read: rc=$?"
5669         $LCTL set_param fail_loc=0
5670         set_checksums 0
5671 }
5672 run_test 77d "checksum error on OST direct write, read"
5673
5674 test_77f() { # bug 10889
5675         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5676         $GSS && skip "could not run with gss" && return
5677         set_checksums 1
5678         for algo in $CKSUM_TYPES; do
5679                 cancel_lru_locks osc
5680                 set_checksum_type $algo
5681                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5682                 $LCTL set_param fail_loc=0x409
5683                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5684                         error "direct write succeeded"
5685                 $LCTL set_param fail_loc=0
5686         done
5687         set_checksum_type $ORIG_CSUM_TYPE
5688         set_checksums 0
5689 }
5690 run_test 77f "repeat checksum error on write (expect error)"
5691
5692 test_77g() { # bug 10889
5693         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5694         $GSS && skip "could not run with gss" && return
5695         remote_ost_nodsh && skip "remote OST with nodsh" && return
5696
5697         [ ! -f $F77_TMP ] && setup_f77
5698
5699         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5700         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5701         do_facet ost1 lctl set_param fail_loc=0x8000021a
5702         set_checksums 1
5703         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5704                 error "write error: rc=$?"
5705         do_facet ost1 lctl set_param fail_loc=0
5706         set_checksums 0
5707
5708         cancel_lru_locks osc
5709         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5710         do_facet ost1 lctl set_param fail_loc=0x8000021b
5711         set_checksums 1
5712         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5713         do_facet ost1 lctl set_param fail_loc=0
5714         set_checksums 0
5715 }
5716 run_test 77g "checksum error on OST write, read"
5717
5718 test_77i() { # bug 13805
5719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5720         $GSS && skip "could not run with gss" && return
5721         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5722         lctl set_param fail_loc=0x40b
5723         remount_client $MOUNT
5724         lctl set_param fail_loc=0
5725         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5726                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5727                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5728                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5729         done
5730         remount_client $MOUNT
5731 }
5732 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5733
5734 test_77j() { # bug 13805
5735         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5736         $GSS && skip "could not run with gss" && return
5737         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5738         lctl set_param fail_loc=0x40c
5739         remount_client $MOUNT
5740         lctl set_param fail_loc=0
5741         sleep 2 # wait async osc connect to finish
5742         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5743                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5744                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5745                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5746         done
5747         remount_client $MOUNT
5748 }
5749 run_test 77j "client only supporting ADLER32"
5750
5751 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5752 rm -f $F77_TMP
5753 unset F77_TMP
5754
5755 test_78() { # bug 10901
5756         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5757         remote_ost || { skip_env "local OST" && return; }
5758
5759         NSEQ=5
5760         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5761         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5762         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5763         echo "MemTotal: $MEMTOTAL"
5764
5765         # reserve 256MB of memory for the kernel and other running processes,
5766         # and then take 1/2 of the remaining memory for the read/write buffers.
5767         if [ $MEMTOTAL -gt 512 ] ;then
5768                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5769         else
5770                 # for those poor memory-starved high-end clusters...
5771                 MEMTOTAL=$((MEMTOTAL / 2))
5772         fi
5773         echo "Mem to use for directio: $MEMTOTAL"
5774
5775         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5776         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5777         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5778         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5779                 head -n1)
5780         echo "Smallest OST: $SMALLESTOST"
5781         [[ $SMALLESTOST -lt 10240 ]] &&
5782                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5783
5784         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5785                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5786
5787         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5788         echo "File size: $F78SIZE"
5789         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5790         for i in $(seq 1 $NSEQ); do
5791                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5792                 echo directIO rdwr round $i of $NSEQ
5793                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5794         done
5795
5796         rm -f $DIR/$tfile
5797 }
5798 run_test 78 "handle large O_DIRECT writes correctly ============"
5799
5800 test_79() { # bug 12743
5801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5802         wait_delete_completed
5803
5804         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5805         BKFREE=$(calc_osc_kbytes kbytesfree)
5806         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5807
5808         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5809         DFTOTAL=`echo $STRING | cut -d, -f1`
5810         DFUSED=`echo $STRING  | cut -d, -f2`
5811         DFAVAIL=`echo $STRING | cut -d, -f3`
5812         DFFREE=$(($DFTOTAL - $DFUSED))
5813
5814         ALLOWANCE=$((64 * $OSTCOUNT))
5815
5816         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5817            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5818                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5819         fi
5820         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5821            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5822                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5823         fi
5824         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5825            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5826                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5827         fi
5828 }
5829 run_test 79 "df report consistency check ======================="
5830
5831 test_80() { # bug 10718
5832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5833         # relax strong synchronous semantics for slow backends like ZFS
5834         local soc="obdfilter.*.sync_on_lock_cancel"
5835         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5836         local hosts=
5837         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5838                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5839                           facet_active_host $host; done | sort -u)
5840                 do_nodes $hosts lctl set_param $soc=never
5841         fi
5842
5843         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5844         sync; sleep 1; sync
5845         local BEFORE=`date +%s`
5846         cancel_lru_locks osc
5847         local AFTER=`date +%s`
5848         local DIFF=$((AFTER-BEFORE))
5849         if [ $DIFF -gt 1 ] ; then
5850                 error "elapsed for 1M@1T = $DIFF"
5851         fi
5852
5853         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5854
5855         rm -f $DIR/$tfile
5856 }
5857 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5858
5859 test_81a() { # LU-456
5860         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5861         remote_ost_nodsh && skip "remote OST with nodsh" && return
5862         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5863         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5864         do_facet ost1 lctl set_param fail_loc=0x80000228
5865
5866         # write should trigger a retry and success
5867         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5868         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5869         RC=$?
5870         if [ $RC -ne 0 ] ; then
5871                 error "write should success, but failed for $RC"
5872         fi
5873 }
5874 run_test 81a "OST should retry write when get -ENOSPC ==============="
5875
5876 test_81b() { # LU-456
5877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5878         remote_ost_nodsh && skip "remote OST with nodsh" && return
5879         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5880         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5881         do_facet ost1 lctl set_param fail_loc=0x228
5882
5883         # write should retry several times and return -ENOSPC finally
5884         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5885         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5886         RC=$?
5887         ENOSPC=28
5888         if [ $RC -ne $ENOSPC ] ; then
5889                 error "dd should fail for -ENOSPC, but succeed."
5890         fi
5891 }
5892 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5893
5894 test_82() { # LU-1031
5895         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5896         local gid1=14091995
5897         local gid2=16022000
5898
5899         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5900         local MULTIPID1=$!
5901         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5902         local MULTIPID2=$!
5903         kill -USR1 $MULTIPID2
5904         sleep 2
5905         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5906                 error "First grouplock does not block second one"
5907         else
5908                 echo "Second grouplock blocks first one"
5909         fi
5910         kill -USR1 $MULTIPID1
5911         wait $MULTIPID1
5912         wait $MULTIPID2
5913 }
5914 run_test 82 "Basic grouplock test ==============================="
5915
5916 test_99a() {
5917         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5918                 return
5919         test_mkdir -p $DIR/d99cvsroot
5920         chown $RUNAS_ID $DIR/d99cvsroot
5921         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5922         cd $TMP
5923
5924         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5925         cd $oldPWD
5926 }
5927 run_test 99a "cvs init ========================================="
5928
5929 test_99b() {
5930         [ -z "$(which cvs 2>/dev/null)" ] &&
5931                 skip_env "could not find cvs" && return
5932         [ ! -d $DIR/d99cvsroot ] && test_99a
5933         cd /etc/init.d
5934         # some versions of cvs import exit(1) when asked to import links or
5935         # files they can't read.  ignore those files.
5936         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5937                         ! -perm +4 -printf '-I %f\n')
5938         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5939                 d99reposname vtag rtag
5940 }
5941 run_test 99b "cvs import ======================================="
5942
5943 test_99c() {
5944         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5945         [ ! -d $DIR/d99cvsroot ] && test_99b
5946         cd $DIR
5947         test_mkdir -p $DIR/d99reposname
5948         chown $RUNAS_ID $DIR/d99reposname
5949         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5950 }
5951 run_test 99c "cvs checkout ====================================="
5952
5953 test_99d() {
5954         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5955         [ ! -d $DIR/d99cvsroot ] && test_99c
5956         cd $DIR/d99reposname
5957         $RUNAS touch foo99
5958         $RUNAS cvs add -m 'addmsg' foo99
5959 }
5960 run_test 99d "cvs add =========================================="
5961
5962 test_99e() {
5963         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5964         [ ! -d $DIR/d99cvsroot ] && test_99c
5965         cd $DIR/d99reposname
5966         $RUNAS cvs update
5967 }
5968 run_test 99e "cvs update ======================================="
5969
5970 test_99f() {
5971         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5972         [ ! -d $DIR/d99cvsroot ] && test_99d
5973         cd $DIR/d99reposname
5974         $RUNAS cvs commit -m 'nomsg' foo99
5975     rm -fr $DIR/d99cvsroot
5976 }
5977 run_test 99f "cvs commit ======================================="
5978
5979 test_100() {
5980         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5981         [ "$NETTYPE" = tcp ] || \
5982                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5983                         return ; }
5984
5985         remote_ost_nodsh && skip "remote OST with nodsh" && return
5986         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5987         remote_servers || \
5988                 { skip "useless for local single node setup" && return; }
5989
5990         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5991                 [ "$PROT" != "tcp" ] && continue
5992                 RPORT=$(echo $REMOTE | cut -d: -f2)
5993                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5994
5995                 rc=0
5996                 LPORT=`echo $LOCAL | cut -d: -f2`
5997                 if [ $LPORT -ge 1024 ]; then
5998                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5999                         netstat -tna
6000                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6001                 fi
6002         done
6003         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6004 }
6005 run_test 100 "check local port using privileged port ==========="
6006
6007 function get_named_value()
6008 {
6009     local tag
6010
6011     tag=$1
6012     while read ;do
6013         line=$REPLY
6014         case $line in
6015         $tag*)
6016             echo $line | sed "s/^$tag[ ]*//"
6017             break
6018             ;;
6019         esac
6020     done
6021 }
6022
6023 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6024                    awk '/^max_cached_mb/ { print $2 }')
6025
6026 cleanup_101a() {
6027         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6028         trap 0
6029 }
6030
6031 test_101a() {
6032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6033         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6034         local s
6035         local discard
6036         local nreads=10000
6037         local cache_limit=32
6038
6039         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6040         trap cleanup_101a EXIT
6041         $LCTL set_param -n llite.*.read_ahead_stats 0
6042         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6043
6044         #
6045         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6046         #
6047         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6048         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6049
6050         discard=0
6051         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6052                 get_named_value 'read but discarded' | cut -d" " -f1); do
6053                         discard=$(($discard + $s))
6054         done
6055         cleanup_101a
6056
6057         if [[ $(($discard * 10)) -gt $nreads ]]; then
6058                 $LCTL get_param osc.*-osc*.rpc_stats
6059                 $LCTL get_param llite.*.read_ahead_stats
6060                 error "too many ($discard) discarded pages"
6061         fi
6062         rm -f $DIR/$tfile || true
6063 }
6064 run_test 101a "check read-ahead for random reads ================"
6065
6066 setup_test101bc() {
6067         test_mkdir -p $DIR/$tdir
6068         local STRIPE_SIZE=$1
6069         local FILE_LENGTH=$2
6070         STRIPE_OFFSET=0
6071
6072         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6073
6074         local list=$(comma_list $(osts_nodes))
6075         set_osd_param $list '' read_cache_enable 0
6076         set_osd_param $list '' writethrough_cache_enable 0
6077
6078         trap cleanup_test101bc EXIT
6079         # prepare the read-ahead file
6080         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6081
6082         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6083                                 count=$FILE_SIZE_MB 2> /dev/null
6084
6085 }
6086
6087 cleanup_test101bc() {
6088         trap 0
6089         rm -rf $DIR/$tdir
6090         rm -f $DIR/$tfile
6091
6092         local list=$(comma_list $(osts_nodes))
6093         set_osd_param $list '' read_cache_enable 1
6094         set_osd_param $list '' writethrough_cache_enable 1
6095 }
6096
6097 calc_total() {
6098         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6099 }
6100
6101 ra_check_101() {
6102         local READ_SIZE=$1
6103         local STRIPE_SIZE=$2
6104         local FILE_LENGTH=$3
6105         local RA_INC=1048576
6106         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6107         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6108                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6109         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6110                         get_named_value 'read but discarded' |
6111                         cut -d" " -f1 | calc_total)
6112         if [[ $DISCARD -gt $discard_limit ]]; then
6113                 $LCTL get_param llite.*.read_ahead_stats
6114                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6115         else
6116                 echo "Read-ahead success for size ${READ_SIZE}"
6117         fi
6118 }
6119
6120 test_101b() {
6121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6122         [[ $OSTCOUNT -lt 2 ]] &&
6123                 skip_env "skipping stride IO stride-ahead test" && return
6124         local STRIPE_SIZE=1048576
6125         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6126         if [ $SLOW == "yes" ]; then
6127                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6128         else
6129                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6130         fi
6131
6132         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6133
6134         # prepare the read-ahead file
6135         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6136         cancel_lru_locks osc
6137         for BIDX in 2 4 8 16 32 64 128 256
6138         do
6139                 local BSIZE=$((BIDX*4096))
6140                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6141                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6142                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6143                 $LCTL set_param -n llite.*.read_ahead_stats 0
6144                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6145                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6146                 cancel_lru_locks osc
6147                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6148         done
6149         cleanup_test101bc
6150         true
6151 }
6152 run_test 101b "check stride-io mode read-ahead ================="
6153
6154 test_101c() {
6155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6156         local STRIPE_SIZE=1048576
6157         local FILE_LENGTH=$((STRIPE_SIZE*100))
6158         local nreads=10000
6159         local osc_rpc_stats
6160
6161         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6162
6163         cancel_lru_locks osc
6164         $LCTL set_param osc.*.rpc_stats 0
6165         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6166         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6167                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6168                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6169                 local size
6170
6171                 if [ $lines -le 20 ]; then
6172                         continue
6173                 fi
6174                 for size in 1 2 4 8; do
6175                         local rpc=$(echo "$stats" |
6176                                     awk '($1 == "'$size':") {print $2; exit; }')
6177                         [ $rpc != 0 ] &&
6178                                 error "Small $((size*4))k read IO $rpc !"
6179                 done
6180                 echo "$osc_rpc_stats check passed!"
6181         done
6182         cleanup_test101bc
6183         true
6184 }
6185 run_test 101c "check stripe_size aligned read-ahead ================="
6186
6187 set_read_ahead() {
6188         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6189         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6190 }
6191
6192 test_101d() {
6193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6194         local file=$DIR/$tfile
6195         local sz_MB=${FILESIZE_101d:-500}
6196         local ra_MB=${READAHEAD_MB:-40}
6197
6198         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6199         [ $free_MB -lt $sz_MB ] &&
6200                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6201
6202         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6203         $SETSTRIPE -c -1 $file || error "setstripe failed"
6204
6205         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6206         echo Cancel LRU locks on lustre client to flush the client cache
6207         cancel_lru_locks osc
6208
6209         echo Disable read-ahead
6210         local old_READAHEAD=$(set_read_ahead 0)
6211
6212         echo Reading the test file $file with read-ahead disabled
6213         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6214
6215         echo Cancel LRU locks on lustre client to flush the client cache
6216         cancel_lru_locks osc
6217         echo Enable read-ahead with ${ra_MB}MB
6218         set_read_ahead $ra_MB
6219
6220         echo Reading the test file $file with read-ahead enabled
6221         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6222
6223         echo "read-ahead disabled time read $raOFF"
6224         echo "read-ahead enabled  time read $raON"
6225
6226         set_read_ahead $old_READAHEAD
6227         rm -f $file
6228         wait_delete_completed
6229
6230         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6231                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6232 }
6233 run_test 101d "file read with and without read-ahead enabled"
6234
6235 test_101e() {
6236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6237         local file=$DIR/$tfile
6238         local size_KB=500  #KB
6239         local count=100
6240         local bsize=1024
6241
6242         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6243         local need_KB=$((count * size_KB))
6244         [[ $free_KB -le $need_KB ]] &&
6245                 skip_env "Need free space $need_KB, have $free_KB" && return
6246
6247         echo "Creating $count ${size_KB}K test files"
6248         for ((i = 0; i < $count; i++)); do
6249                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6250         done
6251
6252         echo "Cancel LRU locks on lustre client to flush the client cache"
6253         cancel_lru_locks osc
6254
6255         echo "Reset readahead stats"
6256         $LCTL set_param -n llite.*.read_ahead_stats 0
6257
6258         for ((i = 0; i < $count; i++)); do
6259                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6260         done
6261
6262         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6263                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6264
6265         for ((i = 0; i < $count; i++)); do
6266                 rm -rf $file.$i 2>/dev/null
6267         done
6268
6269         #10000 means 20% reads are missing in readahead
6270         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6271 }
6272 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6273
6274 cleanup_test101f() {
6275     trap 0
6276     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6277     rm -rf $DIR/$tfile 2>/dev/null
6278 }
6279
6280 test_101f() {
6281         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6282     local file=$DIR/$tfile
6283     local nreads=1000
6284
6285     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6286     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6287     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6288     trap cleanup_test101f EXIT
6289
6290     echo Cancel LRU locks on lustre client to flush the client cache
6291     cancel_lru_locks osc
6292
6293     echo Reset readahead stats
6294     $LCTL set_param -n llite.*.read_ahead_stats 0
6295     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6296     # readahead should read in 2M file on second read, so only miss
6297     # 2 pages.
6298     echo Random 4K reads on 2M file for 1000 times
6299     $READS -f $file -s 2097152 -b 4096 -n $nreads
6300
6301     echo checking missing pages
6302     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6303           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6304
6305     [ $miss -lt 3 ] || error "misses too much pages!"
6306     cleanup_test101f
6307 }
6308 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6309
6310 setup_test102() {
6311         test_mkdir -p $DIR/$tdir
6312         chown $RUNAS_ID $DIR/$tdir
6313         STRIPE_SIZE=65536
6314         STRIPE_OFFSET=1
6315         STRIPE_COUNT=$OSTCOUNT
6316         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6317
6318         trap cleanup_test102 EXIT
6319         cd $DIR
6320         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6321         cd $DIR/$tdir
6322         for num in 1 2 3 4; do
6323                 for count in $(seq 1 $STRIPE_COUNT); do
6324                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6325                                 local size=`expr $STRIPE_SIZE \* $num`
6326                                 local file=file"$num-$idx-$count"
6327                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6328                         done
6329                 done
6330         done
6331
6332         cd $DIR
6333         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6334 }
6335
6336 cleanup_test102() {
6337         trap 0
6338         rm -f $TMP/f102.tar
6339         rm -rf $DIR/d0.sanity/d102
6340 }
6341
6342 test_102a() {
6343         local testfile=$DIR/xattr_testfile
6344
6345         touch $testfile
6346
6347         [ "$UID" != 0 ] && skip_env "must run as root" && return
6348         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6349                 skip_env "must have user_xattr" && return
6350
6351         [ -z "$(which setfattr 2>/dev/null)" ] &&
6352                 skip_env "could not find setfattr" && return
6353
6354         echo "set/get xattr..."
6355         setfattr -n trusted.name1 -v value1 $testfile ||
6356                 error "setfattr -n trusted.name1=value1 $testfile failed"
6357         getfattr -n trusted.name1 $testfile 2> /dev/null |
6358           grep "trusted.name1=.value1" ||
6359                 error "$testfile missing trusted.name1=value1"
6360
6361         setfattr -n user.author1 -v author1 $testfile ||
6362                 error "setfattr -n user.author1=author1 $testfile failed"
6363         getfattr -n user.author1 $testfile 2> /dev/null |
6364           grep "user.author1=.author1" ||
6365                 error "$testfile missing trusted.author1=author1"
6366
6367         echo "listxattr..."
6368         setfattr -n trusted.name2 -v value2 $testfile ||
6369                 error "$testfile unable to set trusted.name2"
6370         setfattr -n trusted.name3 -v value3 $testfile ||
6371                 error "$testfile unable to set trusted.name3"
6372         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6373             grep "trusted.name" | wc -l) -eq 3 ] ||
6374                 error "$testfile missing 3 trusted.name xattrs"
6375
6376         setfattr -n user.author2 -v author2 $testfile ||
6377                 error "$testfile unable to set user.author2"
6378         setfattr -n user.author3 -v author3 $testfile ||
6379                 error "$testfile unable to set user.author3"
6380         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6381             grep "user.author" | wc -l) -eq 3 ] ||
6382                 error "$testfile missing 3 user.author xattrs"
6383
6384         echo "remove xattr..."
6385         setfattr -x trusted.name1 $testfile ||
6386                 error "$testfile error deleting trusted.name1"
6387         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6388                 error "$testfile did not delete trusted.name1 xattr"
6389
6390         setfattr -x user.author1 $testfile ||
6391                 error "$testfile error deleting user.author1"
6392         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6393                 error "$testfile did not delete trusted.name1 xattr"
6394
6395         # b10667: setting lustre special xattr be silently discarded
6396         echo "set lustre special xattr ..."
6397         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6398                 error "$testfile allowed setting trusted.lov"
6399 }
6400 run_test 102a "user xattr test =================================="
6401
6402 test_102b() {
6403         [ -z "$(which setfattr 2>/dev/null)" ] &&
6404                 skip_env "could not find setfattr" && return
6405
6406         # b10930: get/set/list trusted.lov xattr
6407         echo "get/set/list trusted.lov xattr ..."
6408         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6409         local testfile=$DIR/$tfile
6410         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6411                 error "setstripe failed"
6412         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6413                 error "getstripe failed"
6414         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6415                 error "can't get trusted.lov from $testfile"
6416
6417         local testfile2=${testfile}2
6418         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6419                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6420
6421         $MCREATE $testfile2
6422         setfattr -n trusted.lov -v $value $testfile2
6423         local stripe_size=$($GETSTRIPE -S $testfile2)
6424         local stripe_count=$($GETSTRIPE -c $testfile2)
6425         [[ $stripe_size -eq 65536 ]] ||
6426                 error "stripe size $stripe_size != 65536"
6427         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6428                 error "stripe count $stripe_count != $STRIPECOUNT"
6429         rm -f $DIR/$tfile
6430 }
6431 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6432
6433 test_102c() {
6434         [ -z "$(which setfattr 2>/dev/null)" ] &&
6435                 skip_env "could not find setfattr" && return
6436
6437         # b10930: get/set/list lustre.lov xattr
6438         echo "get/set/list lustre.lov xattr ..."
6439         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6440         test_mkdir -p $DIR/$tdir
6441         chown $RUNAS_ID $DIR/$tdir
6442         local testfile=$DIR/$tdir/$tfile
6443         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6444                 error "setstripe failed"
6445         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6446                 error "getstripe failed"
6447         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6448         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6449
6450         local testfile2=${testfile}2
6451         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6452                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6453
6454         $RUNAS $MCREATE $testfile2
6455         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6456         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6457         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6458         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6459         [ $stripe_count -eq $STRIPECOUNT ] ||
6460                 error "stripe count $stripe_count != $STRIPECOUNT"
6461 }
6462 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6463
6464 compare_stripe_info1() {
6465         local stripe_index_all_zero=true
6466
6467         for num in 1 2 3 4; do
6468                 for count in $(seq 1 $STRIPE_COUNT); do
6469                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6470                                 local size=$((STRIPE_SIZE * num))
6471                                 local file=file"$num-$offset-$count"
6472                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6473                                 [[ $stripe_size -ne $size ]] &&
6474                                     error "$file: size $stripe_size != $size"
6475                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6476                                 # allow fewer stripes to be created, ORI-601
6477                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6478                                     error "$file: count $stripe_count != $count"
6479                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6480                                 [[ $stripe_index -ne 0 ]] &&
6481                                         stripe_index_all_zero=false
6482                         done
6483                 done
6484         done
6485         $stripe_index_all_zero &&
6486                 error "all files are being extracted starting from OST index 0"
6487         return 0
6488 }
6489
6490 find_lustre_tar() {
6491         [ -n "$(which tar 2>/dev/null)" ] &&
6492                 strings $(which tar) | grep -q "lustre" && echo tar
6493 }
6494
6495 test_102d() {
6496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6497         # b10930: tar test for trusted.lov xattr
6498         TAR=$(find_lustre_tar)
6499         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6500         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6501         setup_test102
6502         test_mkdir -p $DIR/d102d
6503         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6504         cd $DIR/d102d/$tdir
6505         compare_stripe_info1
6506 }
6507 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6508
6509 test_102f() {
6510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6511         # b10930: tar test for trusted.lov xattr
6512         TAR=$(find_lustre_tar)
6513         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6514         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6515         setup_test102
6516         test_mkdir -p $DIR/d102f
6517         cd $DIR
6518         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6519         cd $DIR/d102f/$tdir
6520         compare_stripe_info1
6521 }
6522 run_test 102f "tar copy files, not keep osts ==========="
6523
6524 grow_xattr() {
6525         local xsize=${1:-1024}  # in bytes
6526         local file=$DIR/$tfile
6527
6528         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6529                 skip "must have user_xattr" && return 0
6530         [ -z "$(which setfattr 2>/dev/null)" ] &&
6531                 skip_env "could not find setfattr" && return 0
6532         [ -z "$(which getfattr 2>/dev/null)" ] &&
6533                 skip_env "could not find getfattr" && return 0
6534
6535         touch $file
6536
6537         local value="$(generate_string $xsize)"
6538
6539         local xbig=trusted.big
6540         log "save $xbig on $file"
6541         setfattr -n $xbig -v $value $file ||
6542                 error "saving $xbig on $file failed"
6543
6544         local orig=$(get_xattr_value $xbig $file)
6545         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6546
6547         local xsml=trusted.sml
6548         log "save $xsml on $file"
6549         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6550
6551         local new=$(get_xattr_value $xbig $file)
6552         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6553
6554         log "grow $xsml on $file"
6555         setfattr -n $xsml -v "$value" $file ||
6556                 error "growing $xsml on $file failed"
6557
6558         new=$(get_xattr_value $xbig $file)
6559         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6560         log "$xbig still valid after growing $xsml"
6561
6562         rm -f $file
6563 }
6564
6565 test_102h() { # bug 15777
6566         grow_xattr 1024
6567 }
6568 run_test 102h "grow xattr from inside inode to external block"
6569
6570 test_102ha() {
6571         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6572         grow_xattr $(max_xattr_size)
6573 }
6574 run_test 102ha "grow xattr from inside inode to external inode"
6575
6576 test_102i() { # bug 17038
6577         [ -z "$(which getfattr 2>/dev/null)" ] &&
6578                 skip "could not find getfattr" && return
6579         touch $DIR/$tfile
6580         ln -s $DIR/$tfile $DIR/${tfile}link
6581         getfattr -n trusted.lov $DIR/$tfile ||
6582                 error "lgetxattr on $DIR/$tfile failed"
6583         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6584                 grep -i "no such attr" ||
6585                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6586         rm -f $DIR/$tfile $DIR/${tfile}link
6587 }
6588 run_test 102i "lgetxattr test on symbolic link ============"
6589
6590 test_102j() {
6591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6592         TAR=$(find_lustre_tar)
6593         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6594         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6595         setup_test102 "$RUNAS"
6596         test_mkdir -p $DIR/d102j
6597         chown $RUNAS_ID $DIR/d102j
6598         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6599         cd $DIR/d102j/$tdir
6600         compare_stripe_info1 "$RUNAS"
6601 }
6602 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6603
6604 test_102k() {
6605         [ -z "$(which setfattr 2>/dev/null)" ] &&
6606                 skip "could not find setfattr" && return
6607         touch $DIR/$tfile
6608         # b22187 just check that does not crash for regular file.
6609         setfattr -n trusted.lov $DIR/$tfile
6610         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6611         local test_kdir=$DIR/d102k
6612         test_mkdir $test_kdir
6613         local default_size=`$GETSTRIPE -S $test_kdir`
6614         local default_count=`$GETSTRIPE -c $test_kdir`
6615         local default_offset=`$GETSTRIPE -i $test_kdir`
6616         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6617                 error 'dir setstripe failed'
6618         setfattr -n trusted.lov $test_kdir
6619         local stripe_size=`$GETSTRIPE -S $test_kdir`
6620         local stripe_count=`$GETSTRIPE -c $test_kdir`
6621         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6622         [ $stripe_size -eq $default_size ] ||
6623                 error "stripe size $stripe_size != $default_size"
6624         [ $stripe_count -eq $default_count ] ||
6625                 error "stripe count $stripe_count != $default_count"
6626         [ $stripe_offset -eq $default_offset ] ||
6627                 error "stripe offset $stripe_offset != $default_offset"
6628         rm -rf $DIR/$tfile $test_kdir
6629 }
6630 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6631
6632 test_102l() {
6633         [ -z "$(which getfattr 2>/dev/null)" ] &&
6634                 skip "could not find getfattr" && return
6635
6636         # LU-532 trusted. xattr is invisible to non-root
6637         local testfile=$DIR/$tfile
6638
6639         touch $testfile
6640
6641         echo "listxattr as user..."
6642         chown $RUNAS_ID $testfile
6643         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6644             grep -q "trusted" &&
6645                 error "$testfile trusted xattrs are user visible"
6646
6647         return 0;
6648 }
6649 run_test 102l "listxattr size test =================================="
6650
6651 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6652         local path=$DIR/$tfile
6653         touch $path
6654
6655         listxattr_size_check $path || error "listattr_size_check $path failed"
6656 }
6657 run_test 102m "Ensure listxattr fails on small bufffer ========"
6658
6659 cleanup_test102
6660
6661 getxattr() { # getxattr path name
6662         # Return the base64 encoding of the value of xattr name on path.
6663         local path=$1
6664         local name=$2
6665
6666         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6667         # file: $path
6668         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6669         #
6670         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6671
6672         getfattr --absolute-names --encoding=base64 --name=$name $path |
6673                 awk -F= -v name=$name '$1 == name {
6674                         print substr($0, index($0, "=") + 1);
6675         }'
6676 }
6677
6678 test_102n() { # LU-4101 mdt: protect internal xattrs
6679         local file0=$DIR/$tfile.0
6680         local file1=$DIR/$tfile.1
6681         local xattr0=$TMP/$tfile.0
6682         local xattr1=$TMP/$tfile.1
6683         local name
6684         local value
6685
6686         [ -z "$(which setfattr 2>/dev/null)" ] &&
6687                 skip "could not find setfattr" && return
6688
6689         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6690         then
6691                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6692                 return
6693         fi
6694
6695         rm -rf $file0 $file1 $xattr0 $xattr1
6696         touch $file0 $file1
6697
6698         # Get 'before' xattrs of $file1.
6699         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6700
6701         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6702                 # Try to copy xattr from $file0 to $file1.
6703                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6704
6705                 setfattr --name=trusted.$name --value="$value" $file1 ||
6706                         error "setxattr 'trusted.$name' failed"
6707
6708                 # Try to set a garbage xattr.
6709                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6710
6711                 setfattr --name=trusted.$name --value="$value" $file1 ||
6712                         error "setxattr 'trusted.$name' failed"
6713
6714                 # Try to remove the xattr from $file1. We don't care if this
6715                 # appears to succeed or fail, we just don't want there to be
6716                 # any changes or crashes.
6717                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6718         done
6719
6720         # Get 'after' xattrs of file1.
6721         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6722
6723         if ! diff $xattr0 $xattr1; then
6724                 error "before and after xattrs of '$file1' differ"
6725         fi
6726
6727         rm -rf $file0 $file1 $xattr0 $xattr1
6728
6729         return 0
6730 }
6731 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6732
6733 test_102p() { # LU-4703 setxattr did not check ownership
6734         local testfile=$DIR/$tfile
6735
6736         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6737                 skip "MDS needs to be at least 2.5.56" && return
6738
6739         touch $testfile
6740
6741         echo "setfacl as user..."
6742         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6743         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6744
6745         echo "setfattr as user..."
6746         setfacl -m "u:$RUNAS_ID:---" $testfile
6747         $RUNAS setfattr -x system.posix_acl_access $testfile
6748         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6749 }
6750 run_test 102p "check setxattr(2) correctly fails without permission"
6751
6752 run_acl_subtest()
6753 {
6754     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6755     return $?
6756 }
6757
6758 test_103 () {
6759         [ "$UID" != 0 ] && skip_env "must run as root" && return
6760         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6761                 skip "must have acl enabled" && return
6762         [ -z "$(which setfacl 2>/dev/null)" ] &&
6763                 skip_env "could not find setfacl" && return
6764         $GSS && skip "could not run under gss" && return
6765
6766         declare -a identity_old
6767
6768         for num in $(seq $MDSCOUNT); do
6769                 switch_identity $num true || identity_old[$num]=$?
6770         done
6771
6772         SAVE_UMASK=$(umask)
6773         umask 0022
6774         cd $DIR
6775
6776         echo "performing cp ..."
6777         run_acl_subtest cp || error "run_acl_subtest cp failed"
6778         echo "performing getfacl-noacl..."
6779         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6780         echo "performing misc..."
6781         run_acl_subtest misc || error  "misc test failed"
6782         echo "performing permissions..."
6783         run_acl_subtest permissions || error "permissions failed"
6784         echo "performing setfacl..."
6785         run_acl_subtest setfacl || error  "setfacl test failed"
6786
6787         # inheritance test got from HP
6788         echo "performing inheritance..."
6789         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6790         chmod +x make-tree || error "chmod +x failed"
6791         run_acl_subtest inheritance || error "inheritance test failed"
6792         rm -f make-tree
6793
6794         echo "LU-974 ignore umask when acl is enabled..."
6795         run_acl_subtest 974 || error "LU-974 umask test failed"
6796         if [ $MDSCOUNT -ge 2 ]; then
6797                 run_acl_subtest 974_remote ||
6798                         error "LU-974 umask test failed under remote dir"
6799         fi
6800
6801         echo "LU-2561 newly created file is same size as directory..."
6802         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6803                 run_acl_subtest 2561 || error "LU-2561 test failed"
6804         else
6805                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6806         fi
6807
6808         run_acl_subtest 4924 || error "LU-4924 test failed"
6809
6810         cd $SAVE_PWD
6811         umask $SAVE_UMASK
6812
6813         for num in $(seq $MDSCOUNT); do
6814                 if [ "${identity_old[$num]}" = 1 ]; then
6815                         switch_identity $num false || identity_old[$num]=$?
6816                 fi
6817         done
6818 }
6819 run_test 103 "acl test ========================================="
6820
6821 test_104a() {
6822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6823         touch $DIR/$tfile
6824         lfs df || error "lfs df failed"
6825         lfs df -ih || error "lfs df -ih failed"
6826         lfs df -h $DIR || error "lfs df -h $DIR failed"
6827         lfs df -i $DIR || error "lfs df -i $DIR failed"
6828         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6829         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6830
6831         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6832         lctl --device %$OSC deactivate
6833         lfs df || error "lfs df with deactivated OSC failed"
6834         lctl --device %$OSC activate
6835         # wait the osc back to normal
6836         wait_osc_import_state client ost FULL
6837
6838         lfs df || error "lfs df with reactivated OSC failed"
6839         rm -f $DIR/$tfile
6840 }
6841 run_test 104a "lfs df [-ih] [path] test ========================="
6842
6843 test_104b() {
6844         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6845         [ $RUNAS_ID -eq $UID ] &&
6846                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6847         chmod 666 /dev/obd
6848         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6849                         grep "Permission denied" | wc -l)))
6850         if [ $denied_cnt -ne 0 ]; then
6851                 error "lfs check servers test failed"
6852         fi
6853 }
6854 run_test 104b "$RUNAS lfs check servers test ===================="
6855
6856 test_105a() {
6857         # doesn't work on 2.4 kernels
6858         touch $DIR/$tfile
6859         if $(flock_is_enabled); then
6860                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6861         else
6862                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6863         fi
6864         rm -f $DIR/$tfile
6865 }
6866 run_test 105a "flock when mounted without -o flock test ========"
6867
6868 test_105b() {
6869         touch $DIR/$tfile
6870         if $(flock_is_enabled); then
6871                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6872         else
6873                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6874         fi
6875         rm -f $DIR/$tfile
6876 }
6877 run_test 105b "fcntl when mounted without -o flock test ========"
6878
6879 test_105c() {
6880         touch $DIR/$tfile
6881         if $(flock_is_enabled); then
6882                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6883         else
6884                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6885         fi
6886         rm -f $DIR/$tfile
6887 }
6888 run_test 105c "lockf when mounted without -o flock test ========"
6889
6890 test_105d() { # bug 15924
6891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6892         test_mkdir -p $DIR/$tdir
6893         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6894         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6895         $LCTL set_param fail_loc=0x80000315
6896         flocks_test 2 $DIR/$tdir
6897 }
6898 run_test 105d "flock race (should not freeze) ========"
6899
6900 test_105e() { # bug 22660 && 22040
6901         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6902         touch $DIR/$tfile
6903         flocks_test 3 $DIR/$tfile
6904 }
6905 run_test 105e "Two conflicting flocks from same process ======="
6906
6907 test_106() { #bug 10921
6908         test_mkdir -p $DIR/$tdir
6909         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6910         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6911 }
6912 run_test 106 "attempt exec of dir followed by chown of that dir"
6913
6914 test_107() {
6915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6916         CDIR=`pwd`
6917         cd $DIR
6918
6919         local file=core
6920         rm -f $file
6921
6922         local save_pattern=$(sysctl -n kernel.core_pattern)
6923         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6924         sysctl -w kernel.core_pattern=$file
6925         sysctl -w kernel.core_uses_pid=0
6926
6927         ulimit -c unlimited
6928         sleep 60 &
6929         SLEEPPID=$!
6930
6931         sleep 1
6932
6933         kill -s 11 $SLEEPPID
6934         wait $SLEEPPID
6935         if [ -e $file ]; then
6936                 size=`stat -c%s $file`
6937                 [ $size -eq 0 ] && error "Fail to create core file $file"
6938         else
6939                 error "Fail to create core file $file"
6940         fi
6941         rm -f $file
6942         sysctl -w kernel.core_pattern=$save_pattern
6943         sysctl -w kernel.core_uses_pid=$save_uses_pid
6944         cd $CDIR
6945 }
6946 run_test 107 "Coredump on SIG"
6947
6948 test_110() {
6949         test_mkdir -p $DIR/$tdir
6950         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6951                 error "mkdir with 255 char failed"
6952         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6953                 error "mkdir with 256 char should fail, but did not"
6954         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6955                 error "create with 255 char failed"
6956         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6957                 error "create with 256 char should fail, but did not"
6958
6959         ls -l $DIR/$tdir
6960         rm -rf $DIR/$tdir
6961 }
6962 run_test 110 "filename length checking"
6963
6964 test_115() {
6965         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6966         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
6967                 tail -1 | cut -c11-20)
6968         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
6969         echo "Starting with $OSTIO_pre threads"
6970
6971         NUMTEST=20000
6972         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
6973         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
6974         echo "$NUMTEST creates/unlinks"
6975         test_mkdir -p $DIR/$tdir
6976         createmany -o $DIR/$tdir/$tfile $NUMTEST
6977         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6978
6979         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
6980                 tail -1 | cut -c11-20)
6981
6982         # don't return an error
6983         [ $OSTIO_post == $OSTIO_pre ] && echo \
6984             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6985             echo "This may be fine, depending on what ran before this test" &&
6986             echo "and how fast this system is." && return
6987
6988         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6989 }
6990 run_test 115 "verify dynamic thread creation===================="
6991
6992 free_min_max () {
6993         wait_delete_completed
6994         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6995         echo OST kbytes available: ${AVAIL[@]}
6996         MAXI=0; MAXV=${AVAIL[0]}
6997         MINI=0; MINV=${AVAIL[0]}
6998         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6999                 #echo OST $i: ${AVAIL[i]}kb
7000                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7001                         MAXV=${AVAIL[i]}; MAXI=$i
7002                 fi
7003                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7004                         MINV=${AVAIL[i]}; MINI=$i
7005                 fi
7006         done
7007         echo Min free space: OST $MINI: $MINV
7008         echo Max free space: OST $MAXI: $MAXV
7009 }
7010
7011 test_116a() { # was previously test_116()
7012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7013         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7014
7015         echo -n "Free space priority "
7016         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7017                 head -n1
7018         declare -a AVAIL
7019         free_min_max
7020
7021         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7022         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7023                 && return
7024         trap simple_cleanup_common EXIT
7025
7026
7027         # Check if we need to generate uneven OSTs
7028         test_mkdir -p $DIR/$tdir/OST${MINI}
7029         local FILL=$(($MINV / 4))
7030         local DIFF=$(($MAXV - $MINV))
7031         local DIFF2=$(($DIFF * 100 / $MINV))
7032
7033         local threshold=$(do_facet $SINGLEMDS \
7034                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7035         threshold=${threshold%%%}
7036         echo -n "Check for uneven OSTs: "
7037         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7038
7039         if [[ $DIFF2 -gt $threshold ]]; then
7040                 echo "ok"
7041                 echo "Don't need to fill OST$MINI"
7042         else
7043                 # generate uneven OSTs. Write 2% over the QOS threshold value
7044                 echo "no"
7045                 DIFF=$(($threshold - $DIFF2 + 2))
7046                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7047                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7048                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7049                         error "setstripe failed"
7050                 DIFF=$(($DIFF2 / 2048))
7051                 i=0
7052                 while [ $i -lt $DIFF ]; do
7053                         i=$(($i + 1))
7054                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7055                                 bs=2M count=1 2>/dev/null
7056                         echo -n .
7057                 done
7058                 echo .
7059                 sync
7060                 sleep_maxage
7061                 free_min_max
7062         fi
7063
7064         DIFF=$(($MAXV - $MINV))
7065         DIFF2=$(($DIFF * 100 / $MINV))
7066         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7067         if [[ $DIFF2 -gt $threshold ]]; then
7068                 echo "ok"
7069         else
7070                 echo "failed - QOS mode won't be used"
7071                 skip "QOS imbalance criteria not met"
7072                 simple_cleanup_common
7073                 return
7074         fi
7075
7076         MINI1=$MINI; MINV1=$MINV
7077         MAXI1=$MAXI; MAXV1=$MAXV
7078
7079         # now fill using QOS
7080         $SETSTRIPE -c 1 $DIR/$tdir
7081         FILL=$(($FILL / 200))
7082         if [ $FILL -gt 600 ]; then
7083                 FILL=600
7084         fi
7085         echo "writing $FILL files to QOS-assigned OSTs"
7086         i=0
7087         while [ $i -lt $FILL ]; do
7088                 i=$((i + 1))
7089                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7090                         count=1 2>/dev/null
7091                 echo -n .
7092         done
7093         echo "wrote $i 200k files"
7094         sync
7095         sleep_maxage
7096
7097         echo "Note: free space may not be updated, so measurements might be off"
7098         free_min_max
7099         DIFF2=$(($MAXV - $MINV))
7100         echo "free space delta: orig $DIFF final $DIFF2"
7101         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7102         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7103         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7104         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7105         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7106         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7107         [ $DIFF -gt 0 ] &&
7108                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7109
7110         # Figure out which files were written where
7111         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7112                   awk '/'$MINI1': / {print $2; exit}')
7113         echo $UUID
7114         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7115         echo "$MINC files created on smaller OST $MINI1"
7116         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7117                   awk '/'$MAXI1': / {print $2; exit}')
7118         echo $UUID
7119         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7120         echo "$MAXC files created on larger OST $MAXI1"
7121         FILL=$(($MAXC * 100 / $MINC - 100))
7122         [[ $MINC -gt 0 ]] &&
7123                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7124         [[ $MAXC -gt $MINC ]] ||
7125                 error_ignore LU-9 "stripe QOS didn't balance free space"
7126         simple_cleanup_common
7127 }
7128 run_test 116a "stripe QOS: free space balance ==================="
7129
7130 test_116b() { # LU-2093
7131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7132         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7133                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7134 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7135         local old_rr
7136         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7137                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7138         do_facet $SINGLEMDS lctl set_param \
7139                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7140         mkdir -p $DIR/$tdir
7141         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7142         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7143         do_facet $SINGLEMDS lctl set_param fail_loc=0
7144         rm -rf $DIR/$tdir
7145         do_facet $SINGLEMDS lctl set_param \
7146                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7147 }
7148 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7149
7150 test_117() # bug 10891
7151 {
7152         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7153         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7154         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7155         lctl set_param fail_loc=0x21e
7156         > $DIR/$tfile || error "truncate failed"
7157         lctl set_param fail_loc=0
7158         echo "Truncate succeeded."
7159         rm -f $DIR/$tfile
7160 }
7161 run_test 117 "verify osd extend =========="
7162
7163 NO_SLOW_RESENDCOUNT=4
7164 export OLD_RESENDCOUNT=""
7165 set_resend_count () {
7166         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7167         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7168         lctl set_param -n $PROC_RESENDCOUNT $1
7169         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7170 }
7171
7172 # for reduce test_118* time (b=14842)
7173 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7174
7175 # Reset async IO behavior after error case
7176 reset_async() {
7177         FILE=$DIR/reset_async
7178
7179         # Ensure all OSCs are cleared
7180         $SETSTRIPE -c -1 $FILE
7181         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7182         sync
7183         rm $FILE
7184 }
7185
7186 test_118a() #bug 11710
7187 {
7188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7189         reset_async
7190
7191         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7192         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7193         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7194
7195         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7196                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7197                 return 1;
7198         fi
7199         rm -f $DIR/$tfile
7200 }
7201 run_test 118a "verify O_SYNC works =========="
7202
7203 test_118b()
7204 {
7205         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7206         remote_ost_nodsh && skip "remote OST with nodsh" && return
7207
7208         reset_async
7209
7210         #define OBD_FAIL_OST_ENOENT 0x217
7211         set_nodes_failloc "$(osts_nodes)" 0x217
7212         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7213         RC=$?
7214         set_nodes_failloc "$(osts_nodes)" 0
7215         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7216         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7217                     grep -c writeback)
7218
7219         if [[ $RC -eq 0 ]]; then
7220                 error "Must return error due to dropped pages, rc=$RC"
7221                 return 1;
7222         fi
7223
7224         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7225                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7226                 return 1;
7227         fi
7228
7229         echo "Dirty pages not leaked on ENOENT"
7230
7231         # Due to the above error the OSC will issue all RPCs syncronously
7232         # until a subsequent RPC completes successfully without error.
7233         $MULTIOP $DIR/$tfile Ow4096yc
7234         rm -f $DIR/$tfile
7235
7236         return 0
7237 }
7238 run_test 118b "Reclaim dirty pages on fatal error =========="
7239
7240 test_118c()
7241 {
7242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7243
7244         # for 118c, restore the original resend count, LU-1940
7245         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7246                                 set_resend_count $OLD_RESENDCOUNT
7247         remote_ost_nodsh && skip "remote OST with nodsh" && return
7248
7249         reset_async
7250
7251         #define OBD_FAIL_OST_EROFS               0x216
7252         set_nodes_failloc "$(osts_nodes)" 0x216
7253
7254         # multiop should block due to fsync until pages are written
7255         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7256         MULTIPID=$!
7257         sleep 1
7258
7259         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7260                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7261         fi
7262
7263         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7264                     grep -c writeback)
7265         if [[ $WRITEBACK -eq 0 ]]; then
7266                 error "No page in writeback, writeback=$WRITEBACK"
7267         fi
7268
7269         set_nodes_failloc "$(osts_nodes)" 0
7270         wait $MULTIPID
7271         RC=$?
7272         if [[ $RC -ne 0 ]]; then
7273                 error "Multiop fsync failed, rc=$RC"
7274         fi
7275
7276         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7277         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7278                     grep -c writeback)
7279         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7280                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7281         fi
7282
7283         rm -f $DIR/$tfile
7284         echo "Dirty pages flushed via fsync on EROFS"
7285         return 0
7286 }
7287 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7288
7289 # continue to use small resend count to reduce test_118* time (b=14842)
7290 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7291
7292 test_118d()
7293 {
7294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7295         remote_ost_nodsh && skip "remote OST with nodsh" && return
7296
7297         reset_async
7298
7299         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7300         set_nodes_failloc "$(osts_nodes)" 0x214
7301         # multiop should block due to fsync until pages are written
7302         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7303         MULTIPID=$!
7304         sleep 1
7305
7306         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7307                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7308         fi
7309
7310         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7311                     grep -c writeback)
7312         if [[ $WRITEBACK -eq 0 ]]; then
7313                 error "No page in writeback, writeback=$WRITEBACK"
7314         fi
7315
7316         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7317         set_nodes_failloc "$(osts_nodes)" 0
7318
7319         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7320         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7321                     grep -c writeback)
7322         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7323                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7324         fi
7325
7326         rm -f $DIR/$tfile
7327         echo "Dirty pages gaurenteed flushed via fsync"
7328         return 0
7329 }
7330 run_test 118d "Fsync validation inject a delay of the bulk =========="
7331
7332 test_118f() {
7333         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7334         reset_async
7335
7336         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7337         lctl set_param fail_loc=0x8000040a
7338
7339         # Should simulate EINVAL error which is fatal
7340         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7341         RC=$?
7342         if [[ $RC -eq 0 ]]; then
7343                 error "Must return error due to dropped pages, rc=$RC"
7344         fi
7345
7346         lctl set_param fail_loc=0x0
7347
7348         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7349         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7350         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7351                     grep -c writeback)
7352         if [[ $LOCKED -ne 0 ]]; then
7353                 error "Locked pages remain in cache, locked=$LOCKED"
7354         fi
7355
7356         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7357                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7358         fi
7359
7360         rm -f $DIR/$tfile
7361         echo "No pages locked after fsync"
7362
7363         reset_async
7364         return 0
7365 }
7366 run_test 118f "Simulate unrecoverable OSC side error =========="
7367
7368 test_118g() {
7369         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7370         reset_async
7371
7372         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7373         lctl set_param fail_loc=0x406
7374
7375         # simulate local -ENOMEM
7376         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7377         RC=$?
7378
7379         lctl set_param fail_loc=0
7380         if [[ $RC -eq 0 ]]; then
7381                 error "Must return error due to dropped pages, rc=$RC"
7382         fi
7383
7384         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7385         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7386         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7387                         grep -c writeback)
7388         if [[ $LOCKED -ne 0 ]]; then
7389                 error "Locked pages remain in cache, locked=$LOCKED"
7390         fi
7391
7392         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7393                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7394         fi
7395
7396         rm -f $DIR/$tfile
7397         echo "No pages locked after fsync"
7398
7399         reset_async
7400         return 0
7401 }
7402 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7403
7404 test_118h() {
7405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7406         remote_ost_nodsh && skip "remote OST with nodsh" && return
7407
7408         reset_async
7409
7410         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7411         set_nodes_failloc "$(osts_nodes)" 0x20e
7412         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7413         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7414         RC=$?
7415
7416         set_nodes_failloc "$(osts_nodes)" 0
7417         if [[ $RC -eq 0 ]]; then
7418                 error "Must return error due to dropped pages, rc=$RC"
7419         fi
7420
7421         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7422         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7423         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7424                     grep -c writeback)
7425         if [[ $LOCKED -ne 0 ]]; then
7426                 error "Locked pages remain in cache, locked=$LOCKED"
7427         fi
7428
7429         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7430                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7431         fi
7432
7433         rm -f $DIR/$tfile
7434         echo "No pages locked after fsync"
7435
7436         return 0
7437 }
7438 run_test 118h "Verify timeout in handling recoverables errors  =========="
7439
7440 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7441
7442 test_118i() {
7443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7444         remote_ost_nodsh && skip "remote OST with nodsh" && return
7445
7446         reset_async
7447
7448         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7449         set_nodes_failloc "$(osts_nodes)" 0x20e
7450
7451         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7452         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7453         PID=$!
7454         sleep 5
7455         set_nodes_failloc "$(osts_nodes)" 0
7456
7457         wait $PID
7458         RC=$?
7459         if [[ $RC -ne 0 ]]; then
7460                 error "got error, but should be not, rc=$RC"
7461         fi
7462
7463         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7464         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7465         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7466         if [[ $LOCKED -ne 0 ]]; then
7467                 error "Locked pages remain in cache, locked=$LOCKED"
7468         fi
7469
7470         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7471                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7472         fi
7473
7474         rm -f $DIR/$tfile
7475         echo "No pages locked after fsync"
7476
7477         return 0
7478 }
7479 run_test 118i "Fix error before timeout in recoverable error  =========="
7480
7481 [ "$SLOW" = "no" ] && set_resend_count 4
7482
7483 test_118j() {
7484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7485         remote_ost_nodsh && skip "remote OST with nodsh" && return
7486
7487         reset_async
7488
7489         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7490         set_nodes_failloc "$(osts_nodes)" 0x220
7491
7492         # return -EIO from OST
7493         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7494         RC=$?
7495         set_nodes_failloc "$(osts_nodes)" 0x0
7496         if [[ $RC -eq 0 ]]; then
7497                 error "Must return error due to dropped pages, rc=$RC"
7498         fi
7499
7500         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7501         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7502         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7503         if [[ $LOCKED -ne 0 ]]; then
7504                 error "Locked pages remain in cache, locked=$LOCKED"
7505         fi
7506
7507         # in recoverable error on OST we want resend and stay until it finished
7508         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7509                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7510         fi
7511
7512         rm -f $DIR/$tfile
7513         echo "No pages locked after fsync"
7514
7515         return 0
7516 }
7517 run_test 118j "Simulate unrecoverable OST side error =========="
7518
7519 test_118k()
7520 {
7521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7522         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7523
7524         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7525         set_nodes_failloc "$(osts_nodes)" 0x20e
7526         test_mkdir -p $DIR/$tdir
7527
7528         for ((i=0;i<10;i++)); do
7529                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7530                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7531                 SLEEPPID=$!
7532                 sleep 0.500s
7533                 kill $SLEEPPID
7534                 wait $SLEEPPID
7535         done
7536
7537         set_nodes_failloc "$(osts_nodes)" 0
7538         rm -rf $DIR/$tdir
7539 }
7540 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7541
7542 test_118l()
7543 {
7544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7545         # LU-646
7546         test_mkdir -p $DIR/$tdir
7547         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7548         rm -rf $DIR/$tdir
7549 }
7550 run_test 118l "fsync dir ========="
7551
7552 test_118m() # LU-3066
7553 {
7554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7555         test_mkdir -p $DIR/$tdir
7556         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7557         rm -rf $DIR/$tdir
7558 }
7559 run_test 118m "fdatasync dir ========="
7560
7561 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7562
7563 test_119a() # bug 11737
7564 {
7565         BSIZE=$((512 * 1024))
7566         directio write $DIR/$tfile 0 1 $BSIZE
7567         # We ask to read two blocks, which is more than a file size.
7568         # directio will indicate an error when requested and actual
7569         # sizes aren't equeal (a normal situation in this case) and
7570         # print actual read amount.
7571         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7572         if [ "$NOB" != "$BSIZE" ]; then
7573                 error "read $NOB bytes instead of $BSIZE"
7574         fi
7575         rm -f $DIR/$tfile
7576 }
7577 run_test 119a "Short directIO read must return actual read amount"
7578
7579 test_119b() # bug 11737
7580 {
7581         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7582
7583         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7584         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7585         sync
7586         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7587                 error "direct read failed"
7588         rm -f $DIR/$tfile
7589 }
7590 run_test 119b "Sparse directIO read must return actual read amount"
7591
7592 test_119c() # bug 13099
7593 {
7594         BSIZE=1048576
7595         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7596         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7597         rm -f $DIR/$tfile
7598 }
7599 run_test 119c "Testing for direct read hitting hole"
7600
7601 test_119d() # bug 15950
7602 {
7603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7604         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7605         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7606         BSIZE=1048576
7607         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7608         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7609         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7610         lctl set_param fail_loc=0x40d
7611         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7612         pid_dio=$!
7613         sleep 1
7614         cat $DIR/$tfile > /dev/null &
7615         lctl set_param fail_loc=0
7616         pid_reads=$!
7617         wait $pid_dio
7618         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7619         sleep 2
7620         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7621         error "the read rpcs have not completed in 2s"
7622         rm -f $DIR/$tfile
7623         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7624 }
7625 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7626
7627 test_120a() {
7628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7629         test_mkdir -p $DIR/$tdir
7630         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7631                skip "no early lock cancel on server" && return 0
7632
7633         lru_resize_disable mdc
7634         lru_resize_disable osc
7635         cancel_lru_locks mdc
7636         # asynchronous object destroy at MDT could cause bl ast to client
7637         cancel_lru_locks osc
7638
7639         stat $DIR/$tdir > /dev/null
7640         can1=$(do_facet $SINGLEMDS \
7641                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7642                awk '/ldlm_cancel/ {print $2}')
7643         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7644                awk '/ldlm_bl_callback/ {print $2}')
7645         test_mkdir -c1 $DIR/$tdir/d1
7646         can2=$(do_facet $SINGLEMDS \
7647                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7648                awk '/ldlm_cancel/ {print $2}')
7649         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7650                awk '/ldlm_bl_callback/ {print $2}')
7651         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7652         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7653         lru_resize_enable mdc
7654         lru_resize_enable osc
7655 }
7656 run_test 120a "Early Lock Cancel: mkdir test"
7657
7658 test_120b() {
7659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7660         test_mkdir $DIR/$tdir
7661         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7662                skip "no early lock cancel on server" && return 0
7663         lru_resize_disable mdc
7664         lru_resize_disable osc
7665         cancel_lru_locks mdc
7666         stat $DIR/$tdir > /dev/null
7667         can1=$(do_facet $SINGLEMDS \
7668                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7669                awk '/ldlm_cancel/ {print $2}')
7670         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7671                awk '/ldlm_bl_callback/ {print $2}')
7672         touch $DIR/$tdir/f1
7673         can2=$(do_facet $SINGLEMDS \
7674                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7675                awk '/ldlm_cancel/ {print $2}')
7676         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7677                awk '/ldlm_bl_callback/ {print $2}')
7678         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7679         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7680         lru_resize_enable mdc
7681         lru_resize_enable osc
7682 }
7683 run_test 120b "Early Lock Cancel: create test"
7684
7685 test_120c() {
7686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7687         test_mkdir -c1 $DIR/$tdir
7688         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7689                skip "no early lock cancel on server" && return 0
7690         lru_resize_disable mdc
7691         lru_resize_disable osc
7692         test_mkdir -p -c1 $DIR/$tdir/d1
7693         test_mkdir -p -c1 $DIR/$tdir/d2
7694         touch $DIR/$tdir/d1/f1
7695         cancel_lru_locks mdc
7696         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7697         can1=$(do_facet $SINGLEMDS \
7698                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7699                awk '/ldlm_cancel/ {print $2}')
7700         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7701                awk '/ldlm_bl_callback/ {print $2}')
7702         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7703         can2=$(do_facet $SINGLEMDS \
7704                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7705                awk '/ldlm_cancel/ {print $2}')
7706         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7707                awk '/ldlm_bl_callback/ {print $2}')
7708         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7709         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7710         lru_resize_enable mdc
7711         lru_resize_enable osc
7712 }
7713 run_test 120c "Early Lock Cancel: link test"
7714
7715 test_120d() {
7716         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7717         test_mkdir -p -c1 $DIR/$tdir
7718         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7719                skip "no early lock cancel on server" && return 0
7720         lru_resize_disable mdc
7721         lru_resize_disable osc
7722         touch $DIR/$tdir
7723         cancel_lru_locks mdc
7724         stat $DIR/$tdir > /dev/null
7725         can1=$(do_facet $SINGLEMDS \
7726                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7727                awk '/ldlm_cancel/ {print $2}')
7728         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7729                awk '/ldlm_bl_callback/ {print $2}')
7730         chmod a+x $DIR/$tdir
7731         can2=$(do_facet $SINGLEMDS \
7732                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7733                awk '/ldlm_cancel/ {print $2}')
7734         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7735                awk '/ldlm_bl_callback/ {print $2}')
7736         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7737         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7738         lru_resize_enable mdc
7739         lru_resize_enable osc
7740 }
7741 run_test 120d "Early Lock Cancel: setattr test"
7742
7743 test_120e() {
7744         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7745         test_mkdir -p -c1 $DIR/$tdir
7746         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7747                skip "no early lock cancel on server" && return 0
7748         lru_resize_disable mdc
7749         lru_resize_disable osc
7750         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7751         cancel_lru_locks mdc
7752         cancel_lru_locks osc
7753         dd if=$DIR/$tdir/f1 of=/dev/null
7754         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7755         # XXX client can not do early lock cancel of OST lock
7756         # during unlink (LU-4206), so cancel osc lock now.
7757         cancel_lru_locks osc
7758         can1=$(do_facet $SINGLEMDS \
7759                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7760                awk '/ldlm_cancel/ {print $2}')
7761         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7762                awk '/ldlm_bl_callback/ {print $2}')
7763         unlink $DIR/$tdir/f1
7764         sleep 5
7765         can2=$(do_facet $SINGLEMDS \
7766                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7767                awk '/ldlm_cancel/ {print $2}')
7768         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7769                awk '/ldlm_bl_callback/ {print $2}')
7770         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7771         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7772         lru_resize_enable mdc
7773         lru_resize_enable osc
7774 }
7775 run_test 120e "Early Lock Cancel: unlink test"
7776
7777 test_120f() {
7778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7779         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7780                skip "no early lock cancel on server" && return 0
7781         test_mkdir -p -c1 $DIR/$tdir
7782         lru_resize_disable mdc
7783         lru_resize_disable osc
7784         test_mkdir -p -c1 $DIR/$tdir/d1
7785         test_mkdir -p -c1 $DIR/$tdir/d2
7786         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7787         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7788         cancel_lru_locks mdc
7789         cancel_lru_locks osc
7790         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7791         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7792         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7793         # XXX client can not do early lock cancel of OST lock
7794         # during rename (LU-4206), so cancel osc lock now.
7795         cancel_lru_locks osc
7796         can1=$(do_facet $SINGLEMDS \
7797                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7798                awk '/ldlm_cancel/ {print $2}')
7799         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7800                awk '/ldlm_bl_callback/ {print $2}')
7801         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7802         sleep 5
7803         can2=$(do_facet $SINGLEMDS \
7804                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7805                awk '/ldlm_cancel/ {print $2}')
7806         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7807                awk '/ldlm_bl_callback/ {print $2}')
7808         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7809         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7810         lru_resize_enable mdc
7811         lru_resize_enable osc
7812 }
7813 run_test 120f "Early Lock Cancel: rename test"
7814
7815 test_120g() {
7816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7817         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7818                skip "no early lock cancel on server" && return 0
7819         lru_resize_disable mdc
7820         lru_resize_disable osc
7821         count=10000
7822         echo create $count files
7823         test_mkdir -p $DIR/$tdir
7824         cancel_lru_locks mdc
7825         cancel_lru_locks osc
7826         t0=`date +%s`
7827
7828         can0=$(do_facet $SINGLEMDS \
7829                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7830                awk '/ldlm_cancel/ {print $2}')
7831         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7832                awk '/ldlm_bl_callback/ {print $2}')
7833         createmany -o $DIR/$tdir/f $count
7834         sync
7835         can1=$(do_facet $SINGLEMDS \
7836                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7837                awk '/ldlm_cancel/ {print $2}')
7838         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7839                awk '/ldlm_bl_callback/ {print $2}')
7840         t1=$(date +%s)
7841         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7842         echo rm $count files
7843         rm -r $DIR/$tdir
7844         sync
7845         can2=$(do_facet $SINGLEMDS \
7846                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7847                awk '/ldlm_cancel/ {print $2}')
7848         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7849                awk '/ldlm_bl_callback/ {print $2}')
7850         t2=$(date +%s)
7851         echo total: $count removes in $((t2-t1))
7852         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7853         sleep 2
7854         # wait for commitment of removal
7855         lru_resize_enable mdc
7856         lru_resize_enable osc
7857 }
7858 run_test 120g "Early Lock Cancel: performance test"
7859
7860 test_121() { #bug #10589
7861         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7862         rm -rf $DIR/$tfile
7863         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7864 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7865         lctl set_param fail_loc=0x310
7866         cancel_lru_locks osc > /dev/null
7867         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7868         lctl set_param fail_loc=0
7869         [[ $reads -eq $writes ]] ||
7870                 error "read $reads blocks, must be $writes blocks"
7871 }
7872 run_test 121 "read cancel race ========="
7873
7874 test_123a() { # was test 123, statahead(bug 11401)
7875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7876         SLOWOK=0
7877         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7878             log "testing on UP system. Performance may be not as good as expected."
7879                         SLOWOK=1
7880         fi
7881
7882         rm -rf $DIR/$tdir
7883         test_mkdir -p $DIR/$tdir
7884         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7885         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7886         MULT=10
7887         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7888                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7889
7890                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7891                 lctl set_param -n llite.*.statahead_max 0
7892                 lctl get_param llite.*.statahead_max
7893                 cancel_lru_locks mdc
7894                 cancel_lru_locks osc
7895                 stime=`date +%s`
7896                 time ls -l $DIR/$tdir | wc -l
7897                 etime=`date +%s`
7898                 delta=$((etime - stime))
7899                 log "ls $i files without statahead: $delta sec"
7900                 lctl set_param llite.*.statahead_max=$max
7901
7902                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7903                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7904                 cancel_lru_locks mdc
7905                 cancel_lru_locks osc
7906                 stime=`date +%s`
7907                 time ls -l $DIR/$tdir | wc -l
7908                 etime=`date +%s`
7909                 delta_sa=$((etime - stime))
7910                 log "ls $i files with statahead: $delta_sa sec"
7911                 lctl get_param -n llite.*.statahead_stats
7912                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7913
7914                 [[ $swrong -lt $ewrong ]] &&
7915                         log "statahead was stopped, maybe too many locks held!"
7916                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
7917
7918                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7919                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7920                     lctl set_param -n llite.*.statahead_max 0
7921                     lctl get_param llite.*.statahead_max
7922                     cancel_lru_locks mdc
7923                     cancel_lru_locks osc
7924                     stime=`date +%s`
7925                     time ls -l $DIR/$tdir | wc -l
7926                     etime=`date +%s`
7927                     delta=$((etime - stime))
7928                     log "ls $i files again without statahead: $delta sec"
7929                     lctl set_param llite.*.statahead_max=$max
7930                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7931                         if [  $SLOWOK -eq 0 ]; then
7932                                 error "ls $i files is slower with statahead!"
7933                         else
7934                                 log "ls $i files is slower with statahead!"
7935                         fi
7936                         break
7937                     fi
7938                 fi
7939
7940                 [ $delta -gt 20 ] && break
7941                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7942                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7943         done
7944         log "ls done"
7945
7946         stime=`date +%s`
7947         rm -r $DIR/$tdir
7948         sync
7949         etime=`date +%s`
7950         delta=$((etime - stime))
7951         log "rm -r $DIR/$tdir/: $delta seconds"
7952         log "rm done"
7953         lctl get_param -n llite.*.statahead_stats
7954 }
7955 run_test 123a "verify statahead work"
7956
7957 test_123b () { # statahead(bug 15027)
7958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7959         test_mkdir -p $DIR/$tdir
7960         createmany -o $DIR/$tdir/$tfile-%d 1000
7961
7962         cancel_lru_locks mdc
7963         cancel_lru_locks osc
7964
7965 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7966         lctl set_param fail_loc=0x80000803
7967         ls -lR $DIR/$tdir > /dev/null
7968         log "ls done"
7969         lctl set_param fail_loc=0x0
7970         lctl get_param -n llite.*.statahead_stats
7971         rm -r $DIR/$tdir
7972         sync
7973
7974 }
7975 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7976
7977 test_124a() {
7978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7979         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
7980                 skip "no lru resize on server" && return 0
7981         local NR=2000
7982         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7983
7984         log "create $NR files at $DIR/$tdir"
7985         createmany -o $DIR/$tdir/f $NR ||
7986                 error "failed to create $NR files in $DIR/$tdir"
7987
7988         cancel_lru_locks mdc
7989         ls -l $DIR/$tdir > /dev/null
7990
7991         local NSDIR=""
7992         local LRU_SIZE=0
7993         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
7994                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
7995                 LRU_SIZE=$($LCTL get_param -n $PARAM)
7996                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
7997                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7998                         log "NSDIR=$NSDIR"
7999                         log "NS=$(basename $NSDIR)"
8000                         break
8001                 fi
8002         done
8003
8004         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8005                 skip "Not enough cached locks created!"
8006                 return 0
8007         fi
8008         log "LRU=$LRU_SIZE"
8009
8010         local SLEEP=30
8011
8012         # We know that lru resize allows one client to hold $LIMIT locks
8013         # for 10h. After that locks begin to be killed by client.
8014         local MAX_HRS=10
8015         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8016         log "LIMIT=$LIMIT"
8017
8018         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8019         # killing locks. Some time was spent for creating locks. This means
8020         # that up to the moment of sleep finish we must have killed some of
8021         # them (10-100 locks). This depends on how fast ther were created.
8022         # Many of them were touched in almost the same moment and thus will
8023         # be killed in groups.
8024         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8025
8026         # Use $LRU_SIZE_B here to take into account real number of locks
8027         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8028         local LRU_SIZE_B=$LRU_SIZE
8029         log "LVF=$LVF"
8030         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8031         log "OLD_LVF=$OLD_LVF"
8032         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8033
8034         # Let's make sure that we really have some margin. Client checks
8035         # cached locks every 10 sec.
8036         SLEEP=$((SLEEP+20))
8037         log "Sleep ${SLEEP} sec"
8038         local SEC=0
8039         while ((SEC<$SLEEP)); do
8040                 echo -n "..."
8041                 sleep 5
8042                 SEC=$((SEC+5))
8043                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8044                 echo -n "$LRU_SIZE"
8045         done
8046         echo ""
8047         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8048         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8049
8050         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8051                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8052                 unlinkmany $DIR/$tdir/f $NR
8053                 return
8054         }
8055
8056         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8057         log "unlink $NR files at $DIR/$tdir"
8058         unlinkmany $DIR/$tdir/f $NR
8059 }
8060 run_test 124a "lru resize ======================================="
8061
8062 get_max_pool_limit()
8063 {
8064         local limit=$($LCTL get_param \
8065                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8066         local max=0
8067         for l in $limit; do
8068                 if [[ $l -gt $max ]]; then
8069                         max=$l
8070                 fi
8071         done
8072         echo $max
8073 }
8074
8075 test_124b() {
8076         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8077         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8078                 skip "no lru resize on server" && return 0
8079
8080         LIMIT=$(get_max_pool_limit)
8081
8082         NR=$(($(default_lru_size)*20))
8083         if [[ $NR -gt $LIMIT ]]; then
8084                 log "Limit lock number by $LIMIT locks"
8085                 NR=$LIMIT
8086         fi
8087         lru_resize_disable mdc
8088         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8089                 error "failed to create $DIR/$tdir/disable_lru_resize"
8090
8091         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8092         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8093         cancel_lru_locks mdc
8094         stime=`date +%s`
8095         PID=""
8096         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8097         PID="$PID $!"
8098         sleep 2
8099         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8100         PID="$PID $!"
8101         sleep 2
8102         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8103         PID="$PID $!"
8104         wait $PID
8105         etime=`date +%s`
8106         nolruresize_delta=$((etime-stime))
8107         log "ls -la time: $nolruresize_delta seconds"
8108         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8109         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8110
8111         lru_resize_enable mdc
8112         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8113                 error "failed to create $DIR/$tdir/enable_lru_resize"
8114
8115         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8116         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8117         cancel_lru_locks mdc
8118         stime=`date +%s`
8119         PID=""
8120         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8121         PID="$PID $!"
8122         sleep 2
8123         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8124         PID="$PID $!"
8125         sleep 2
8126         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8127         PID="$PID $!"
8128         wait $PID
8129         etime=`date +%s`
8130         lruresize_delta=$((etime-stime))
8131         log "ls -la time: $lruresize_delta seconds"
8132         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8133
8134         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8135                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8136         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8137                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8138         else
8139                 log "lru resize performs the same with no lru resize"
8140         fi
8141         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8142 }
8143 run_test 124b "lru resize (performance test) ======================="
8144
8145 test_125() { # 13358
8146         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8147         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8148         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8149         test_mkdir -p $DIR/d125 || error "mkdir failed"
8150         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8151         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8152         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8153 }
8154 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8155
8156 test_126() { # bug 12829/13455
8157         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8158         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8159         $GSS && skip "must run as gss disabled" && return
8160
8161         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8162         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8163         rm -f $DIR/$tfile
8164         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8165 }
8166 run_test 126 "check that the fsgid provided by the client is taken into account"
8167
8168 test_127a() { # bug 15521
8169         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8170         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8171         $LCTL set_param osc.*.stats=0
8172         FSIZE=$((2048 * 1024))
8173         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8174         cancel_lru_locks osc
8175         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8176
8177         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8178         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8179                 echo "got $COUNT $NAME"
8180                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8181                 eval $NAME=$COUNT || error "Wrong proc format"
8182
8183                 case $NAME in
8184                         read_bytes|write_bytes)
8185                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8186                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8187                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8188                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8189                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8190                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8191                                 error "sumsquare is too small: $SUMSQ"
8192                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8193                                 error "sumsquare is too big: $SUMSQ"
8194                         ;;
8195                         *) ;;
8196                 esac
8197         done < $DIR/${tfile}.tmp
8198
8199         #check that we actually got some stats
8200         [ "$read_bytes" ] || error "Missing read_bytes stats"
8201         [ "$write_bytes" ] || error "Missing write_bytes stats"
8202         [ "$read_bytes" != 0 ] || error "no read done"
8203         [ "$write_bytes" != 0 ] || error "no write done"
8204 }
8205 run_test 127a "verify the client stats are sane"
8206
8207 test_127b() { # bug LU-333
8208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8209         $LCTL set_param llite.*.stats=0
8210         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8211         # perform 2 reads and writes so MAX is different from SUM.
8212         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8213         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8214         cancel_lru_locks osc
8215         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8216         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8217
8218         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8219         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8220                 echo "got $COUNT $NAME"
8221                 eval $NAME=$COUNT || error "Wrong proc format"
8222
8223         case $NAME in
8224                 read_bytes)
8225                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8226                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8227                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8228                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8229                         ;;
8230                 write_bytes)
8231                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8232                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8233                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8234                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8235                         ;;
8236                         *) ;;
8237                 esac
8238         done < $TMP/${tfile}.tmp
8239
8240         #check that we actually got some stats
8241         [ "$read_bytes" ] || error "Missing read_bytes stats"
8242         [ "$write_bytes" ] || error "Missing write_bytes stats"
8243         [ "$read_bytes" != 0 ] || error "no read done"
8244         [ "$write_bytes" != 0 ] || error "no write done"
8245 }
8246 run_test 127b "verify the llite client stats are sane"
8247
8248 test_128() { # bug 15212
8249         touch $DIR/$tfile
8250         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8251                 find $DIR/$tfile
8252                 find $DIR/$tfile
8253         EOF
8254
8255         result=$(grep error $TMP/$tfile.log)
8256         rm -f $DIR/$tfile
8257         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8258 }
8259 run_test 128 "interactive lfs for 2 consecutive find's"
8260
8261 set_dir_limits () {
8262         local mntdev
8263         local canondev
8264         local node
8265
8266         local LDPROC=/proc/fs/ldiskfs
8267         local facets=$(get_facets MDS)
8268
8269         for facet in ${facets//,/ }; do
8270                 canondev=$(ldiskfs_canon \
8271                            *.$(convert_facet2label $facet).mntdev $facet)
8272                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8273                                                 LDPROC=/sys/fs/ldiskfs
8274                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8275         done
8276 }
8277
8278 test_129() {
8279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8280         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8281                 skip "Only applicable to ldiskfs-based MDTs"
8282                 return
8283         fi
8284         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8285         ENOSPC=28
8286         EFBIG=27
8287
8288         rm -rf $DIR/$tdir
8289         test_mkdir -p $DIR/$tdir
8290
8291         # block size of mds1
8292         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8293         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8294         local MAX=$((MDSBLOCKSIZE * 3))
8295         set_dir_limits $MAX
8296         local I=$(stat -c%s "$DIR/$tdir")
8297         local J=0
8298         local STRIPE_COUNT=1
8299         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8300         MAX=$((MAX*STRIPE_COUNT))
8301         while [[ $I -le $MAX ]]; do
8302                 $MULTIOP $DIR/$tdir/$J Oc
8303                 rc=$?
8304                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8305                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8306                 #and EFBIG for previous versions
8307                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8308                         set_dir_limits 0
8309                         echo "return code $rc received as expected"
8310                         multiop $DIR/$tdir/$J Oc ||
8311                                 error_exit "multiop failed w/o dir size limit"
8312
8313                         I=$(stat -c%s "$DIR/$tdir")
8314
8315                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8316                                         $(version_code 2.4.51) ]
8317                         then
8318                                 [[ $I -eq $MAX ]] && return 0
8319                         else
8320                                 [[ $I -gt $MAX ]] && return 0
8321                         fi
8322                         error_exit "current dir size $I, previous limit $MAX"
8323                 elif [ $rc -ne 0 ]; then
8324                         set_dir_limits 0
8325                         error_exit "return code $rc received instead of expected " \
8326                                    "$EFBIG or $ENOSPC, files in dir $I"
8327                 fi
8328                 J=$((J+1))
8329                 I=$(stat -c%s "$DIR/$tdir")
8330         done
8331
8332         set_dir_limits 0
8333         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8334 }
8335 run_test 129 "test directory size limit ========================"
8336
8337 OLDIFS="$IFS"
8338 cleanup_130() {
8339         trap 0
8340         IFS="$OLDIFS"
8341 }
8342
8343 test_130a() {
8344         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8345         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8346                 return
8347
8348         trap cleanup_130 EXIT RETURN
8349
8350         local fm_file=$DIR/$tfile
8351         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8352         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8353                 error "dd failed for $fm_file"
8354
8355         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8356         filefrag -ves $fm_file
8357         RC=$?
8358         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8359                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8360         [ $RC != 0 ] && error "filefrag $fm_file failed"
8361
8362         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8363                       grep -v "ext:" | grep -v "found")
8364         lun=$($GETSTRIPE -i $fm_file)
8365
8366         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8367         IFS=$'\n'
8368         tot_len=0
8369         for line in $filefrag_op
8370         do
8371                 frag_lun=`echo $line | cut -d: -f5`
8372                 ext_len=`echo $line | cut -d: -f4`
8373                 if (( $frag_lun != $lun )); then
8374                         cleanup_130
8375                         error "FIEMAP on 1-stripe file($fm_file) failed"
8376                         return
8377                 fi
8378                 (( tot_len += ext_len ))
8379         done
8380
8381         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8382                 cleanup_130
8383                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8384                 return
8385         fi
8386
8387         cleanup_130
8388
8389         echo "FIEMAP on single striped file succeeded"
8390 }
8391 run_test 130a "FIEMAP (1-stripe file)"
8392
8393 test_130b() {
8394         [ "$OSTCOUNT" -lt "2" ] &&
8395                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8396
8397         [ "$OSTCOUNT" -ge "10" ] &&
8398                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8399
8400         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8401         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8402                 return
8403
8404         trap cleanup_130 EXIT RETURN
8405
8406         local fm_file=$DIR/$tfile
8407         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8408         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8409                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8410
8411         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8412                 error "dd failed on $fm_file"
8413
8414         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8415         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8416                       grep -v "ext:" | grep -v "found")
8417
8418         last_lun=$(echo $filefrag_op | cut -d: -f5)
8419
8420         IFS=$'\n'
8421         tot_len=0
8422         num_luns=1
8423         for line in $filefrag_op
8424         do
8425                 frag_lun=`echo $line | cut -d: -f5`
8426                 ext_len=`echo $line | cut -d: -f4`
8427                 if (( $frag_lun != $last_lun )); then
8428                         if (( tot_len != 1024 )); then
8429                                 cleanup_130
8430                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8431                                 return
8432                         else
8433                                 (( num_luns += 1 ))
8434                                 tot_len=0
8435                         fi
8436                 fi
8437                 (( tot_len += ext_len ))
8438                 last_lun=$frag_lun
8439         done
8440         if (( num_luns != 2 || tot_len != 1024 )); then
8441                 cleanup_130
8442                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8443                 return
8444         fi
8445
8446         cleanup_130
8447
8448         echo "FIEMAP on 2-stripe file succeeded"
8449 }
8450 run_test 130b "FIEMAP (2-stripe file)"
8451
8452 test_130c() {
8453         [ "$OSTCOUNT" -lt "2" ] &&
8454                 skip_env "skipping FIEMAP on 2-stripe file" && return
8455
8456         [ "$OSTCOUNT" -ge "10" ] &&
8457                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8458
8459         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8460         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8461                 return
8462
8463         trap cleanup_130 EXIT RETURN
8464
8465         local fm_file=$DIR/$tfile
8466         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8467         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8468                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8469
8470         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8471
8472         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8473         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8474
8475         last_lun=`echo $filefrag_op | cut -d: -f5`
8476
8477         IFS=$'\n'
8478         tot_len=0
8479         num_luns=1
8480         for line in $filefrag_op
8481         do
8482                 frag_lun=`echo $line | cut -d: -f5`
8483                 ext_len=`echo $line | cut -d: -f4`
8484                 if (( $frag_lun != $last_lun )); then
8485                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8486                         if (( logical != 512 )); then
8487                                 cleanup_130
8488                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8489                                 return
8490                         fi
8491                         if (( tot_len != 512 )); then
8492                                 cleanup_130
8493                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8494                                 return
8495                         else
8496                                 (( num_luns += 1 ))
8497                                 tot_len=0
8498                         fi
8499                 fi
8500                 (( tot_len += ext_len ))
8501                 last_lun=$frag_lun
8502         done
8503         if (( num_luns != 2 || tot_len != 512 )); then
8504                 cleanup_130
8505                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8506                 return
8507         fi
8508
8509         cleanup_130
8510
8511         echo "FIEMAP on 2-stripe file with hole succeeded"
8512 }
8513 run_test 130c "FIEMAP (2-stripe file with hole)"
8514
8515 test_130d() {
8516         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8517
8518         [ "$OSTCOUNT" -ge "10" ] &&
8519                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8520
8521         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8522         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8523
8524         trap cleanup_130 EXIT RETURN
8525
8526         local fm_file=$DIR/$tfile
8527         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8528         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8529                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8530
8531         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8532         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8533                 error "dd failed on $fm_file"
8534
8535         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8536         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8537                 grep -v "ext:" | grep -v "found"`
8538
8539         last_lun=`echo $filefrag_op | cut -d: -f5`
8540
8541         IFS=$'\n'
8542         tot_len=0
8543         num_luns=1
8544         for line in $filefrag_op
8545         do
8546                 frag_lun=`echo $line | cut -d: -f5`
8547                 ext_len=`echo $line | cut -d: -f4`
8548                 if (( $frag_lun != $last_lun )); then
8549                         if (( tot_len != 1024 )); then
8550                                 cleanup_130
8551                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8552                                 return
8553                         else
8554                                 (( num_luns += 1 ))
8555                                 tot_len=0
8556                         fi
8557                 fi
8558                 (( tot_len += ext_len ))
8559                 last_lun=$frag_lun
8560         done
8561         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8562                 cleanup_130
8563                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8564                 return
8565         fi
8566
8567         cleanup_130
8568
8569         echo "FIEMAP on N-stripe file succeeded"
8570 }
8571 run_test 130d "FIEMAP (N-stripe file)"
8572
8573 test_130e() {
8574         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8575
8576         [ "$OSTCOUNT" -ge "10" ] &&
8577                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8578
8579         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8580         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8581
8582         trap cleanup_130 EXIT RETURN
8583
8584         local fm_file=$DIR/$tfile
8585         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8586         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8587                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8588
8589         NUM_BLKS=512
8590         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8591         for ((i = 0; i < $NUM_BLKS; i++))
8592         do
8593                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8594         done
8595
8596         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8597         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8598
8599         last_lun=`echo $filefrag_op | cut -d: -f5`
8600
8601         IFS=$'\n'
8602         tot_len=0
8603         num_luns=1
8604         for line in $filefrag_op
8605         do
8606                 frag_lun=`echo $line | cut -d: -f5`
8607                 ext_len=`echo $line | cut -d: -f4`
8608                 if (( $frag_lun != $last_lun )); then
8609                         if (( tot_len != $EXPECTED_LEN )); then
8610                                 cleanup_130
8611                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8612                                 return
8613                         else
8614                                 (( num_luns += 1 ))
8615                                 tot_len=0
8616                         fi
8617                 fi
8618                 (( tot_len += ext_len ))
8619                 last_lun=$frag_lun
8620         done
8621         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8622                 cleanup_130
8623                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8624                 return
8625         fi
8626
8627         cleanup_130
8628
8629         echo "FIEMAP with continuation calls succeeded"
8630 }
8631 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8632
8633 # Test for writev/readv
8634 test_131a() {
8635         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8636         error "writev test failed"
8637         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8638         error "readv failed"
8639         rm -f $DIR/$tfile
8640 }
8641 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8642
8643 test_131b() {
8644         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8645         error "append writev test failed"
8646         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8647         error "append writev test failed"
8648         rm -f $DIR/$tfile
8649 }
8650 run_test 131b "test append writev"
8651
8652 test_131c() {
8653         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8654         error "NOT PASS"
8655 }
8656 run_test 131c "test read/write on file w/o objects"
8657
8658 test_131d() {
8659         rwv -f $DIR/$tfile -w -n 1 1572864
8660         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8661         if [ "$NOB" != 1572864 ]; then
8662                 error "Short read filed: read $NOB bytes instead of 1572864"
8663         fi
8664         rm -f $DIR/$tfile
8665 }
8666 run_test 131d "test short read"
8667
8668 test_131e() {
8669         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8670         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8671         error "read hitting hole failed"
8672         rm -f $DIR/$tfile
8673 }
8674 run_test 131e "test read hitting hole"
8675
8676 get_ost_param() {
8677         local token=$1
8678         local gl_sum=0
8679         for node in $(osts_nodes); do
8680                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8681                 [ x$gl = x"" ] && gl=0
8682                 gl_sum=$((gl_sum + gl))
8683         done
8684         echo $gl_sum
8685 }
8686
8687 som_mode_switch() {
8688         local som=$1
8689         local gl1=$2
8690         local gl2=$3
8691
8692         if [ x$som = x"enabled" ]; then
8693                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8694                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8695                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8696         else
8697                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8698                 MOUNTOPT="$MOUNTOPT,som_preview"
8699                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8700         fi
8701
8702         # do remount to make new mount-conf parameters actual
8703         echo remounting...
8704         sync
8705         stopall
8706         setupall
8707 }
8708
8709 test_132() { #1028, SOM
8710         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8711         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8712         local num=$(get_mds_dir $DIR)
8713         local mymds=mds${num}
8714         local MOUNTOPT_SAVE=$MOUNTOPT
8715
8716         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8717         cancel_lru_locks osc
8718
8719         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8720
8721         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8722         stat $DIR/$tfile >/dev/null
8723         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8724         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8725         rm $DIR/$tfile
8726         som_mode_switch $som1 $gl1 $gl2
8727
8728         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8729         cancel_lru_locks osc
8730
8731         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8732         if [ $som1 == $som2 ]; then
8733             error "som is still "$som2
8734             if [ x$som2 = x"enabled" ]; then
8735                 som2="disabled"
8736             else
8737                 som2="enabled"
8738             fi
8739         fi
8740
8741         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8742         stat $DIR/$tfile >/dev/null
8743         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8744         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8745         som_mode_switch $som2 $gl1 $gl2
8746         MOUNTOPT=$MOUNTOPT_SAVE
8747 }
8748 run_test 132 "som avoids glimpse rpc"
8749
8750 check_stats() {
8751         local res
8752         local count
8753         case $1 in
8754         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8755                  ;;
8756         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8757                  ;;
8758         *) error "Wrong argument $1" ;;
8759         esac
8760         echo $res
8761         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8762         # if the argument $3 is zero, it means any stat increment is ok.
8763         if [[ $3 -gt 0 ]]; then
8764                 count=$(echo $res | awk '{ print $2 }')
8765                 [[ $count -ne $3 ]] &&
8766                         error "The $2 counter on $1 is wrong - expected $3"
8767         fi
8768 }
8769
8770 test_133a() {
8771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8772         remote_ost_nodsh && skip "remote OST with nodsh" && return
8773         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8774
8775         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8776                 { skip "MDS doesn't support rename stats"; return; }
8777         local testdir=$DIR/${tdir}/stats_testdir
8778         mkdir -p $DIR/${tdir}
8779
8780         # clear stats.
8781         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8782         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8783
8784         # verify mdt stats first.
8785         mkdir ${testdir} || error "mkdir failed"
8786         check_stats $SINGLEMDS "mkdir" 1
8787         touch ${testdir}/${tfile} || "touch failed"
8788         check_stats $SINGLEMDS "open" 1
8789         check_stats $SINGLEMDS "close" 1
8790         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8791         check_stats $SINGLEMDS "mknod" 1
8792         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8793         check_stats $SINGLEMDS "unlink" 1
8794         rm -f ${testdir}/${tfile} || error "file remove failed"
8795         check_stats $SINGLEMDS "unlink" 2
8796
8797         # remove working dir and check mdt stats again.
8798         rmdir ${testdir} || error "rmdir failed"
8799         check_stats $SINGLEMDS "rmdir" 1
8800
8801         local testdir1=$DIR/${tdir}/stats_testdir1
8802         mkdir -p ${testdir}
8803         mkdir -p ${testdir1}
8804         touch ${testdir1}/test1
8805         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8806         check_stats $SINGLEMDS "crossdir_rename" 1
8807
8808         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8809         check_stats $SINGLEMDS "samedir_rename" 1
8810
8811         rm -rf $DIR/${tdir}
8812 }
8813 run_test 133a "Verifying MDT stats ========================================"
8814
8815 test_133b() {
8816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8817         remote_ost_nodsh && skip "remote OST with nodsh" && return
8818         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8819         local testdir=$DIR/${tdir}/stats_testdir
8820         mkdir -p ${testdir} || error "mkdir failed"
8821         touch ${testdir}/${tfile} || "touch failed"
8822         cancel_lru_locks mdc
8823
8824         # clear stats.
8825         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8826         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8827
8828         # extra mdt stats verification.
8829         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8830         check_stats $SINGLEMDS "setattr" 1
8831         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8832         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8833         then            # LU-1740
8834                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8835                 check_stats $SINGLEMDS "getattr" 1
8836         fi
8837         $LFS df || error "lfs failed"
8838         check_stats $SINGLEMDS "statfs" 1
8839
8840         rm -rf $DIR/${tdir}
8841 }
8842 run_test 133b "Verifying extra MDT stats =================================="
8843
8844 test_133c() {
8845         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8846         remote_ost_nodsh && skip "remote OST with nodsh" && return
8847         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8848         local testdir=$DIR/${tdir}/stats_testdir
8849         test_mkdir -p ${testdir} || error "mkdir failed"
8850
8851         # verify obdfilter stats.
8852         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8853         sync
8854         cancel_lru_locks osc
8855         wait_delete_completed
8856
8857         # clear stats.
8858         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8859         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8860
8861         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8862         sync
8863         cancel_lru_locks osc
8864         check_stats ost "write" 1
8865
8866         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8867         check_stats ost "read" 1
8868
8869         > ${testdir}/${tfile} || error "truncate failed"
8870         check_stats ost "punch" 1
8871
8872         rm -f ${testdir}/${tfile} || error "file remove failed"
8873         wait_delete_completed
8874         check_stats ost "destroy" 1
8875
8876         rm -rf $DIR/${tdir}
8877 }
8878 run_test 133c "Verifying OST stats ========================================"
8879
8880 order_2() {
8881         local value=$1
8882         local orig=$value
8883         local order=1
8884
8885         while [ $value -ge 2 ]; do
8886                 order=$((order*2))
8887                 value=$((value/2))
8888         done
8889
8890         if [ $orig -gt $order ]; then
8891                 order=$((order*2))
8892         fi
8893         echo $order
8894 }
8895
8896 size_in_KMGT() {
8897     local value=$1
8898     local size=('K' 'M' 'G' 'T');
8899     local i=0
8900     local size_string=$value
8901
8902     while [ $value -ge 1024 ]; do
8903         if [ $i -gt 3 ]; then
8904             #T is the biggest unit we get here, if that is bigger,
8905             #just return XXXT
8906             size_string=${value}T
8907             break
8908         fi
8909         value=$((value >> 10))
8910         if [ $value -lt 1024 ]; then
8911             size_string=${value}${size[$i]}
8912             break
8913         fi
8914         i=$((i + 1))
8915     done
8916
8917     echo $size_string
8918 }
8919
8920 get_rename_size() {
8921     local size=$1
8922     local context=${2:-.}
8923     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8924                 grep -A1 $context |
8925                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8926     echo $sample
8927 }
8928
8929 test_133d() {
8930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8931         remote_ost_nodsh && skip "remote OST with nodsh" && return
8932         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8933         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8934         { skip "MDS doesn't support rename stats"; return; }
8935
8936         local testdir1=$DIR/${tdir}/stats_testdir1
8937         local testdir2=$DIR/${tdir}/stats_testdir2
8938
8939         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8940
8941         mkdir -p ${testdir1} || error "mkdir failed"
8942         mkdir -p ${testdir2} || error "mkdir failed"
8943
8944         createmany -o $testdir1/test 512 || error "createmany failed"
8945
8946         # check samedir rename size
8947         mv ${testdir1}/test0 ${testdir1}/test_0
8948
8949         local testdir1_size=$(ls -l $DIR/${tdir} |
8950                 awk '/stats_testdir1/ {print $5}')
8951         local testdir2_size=$(ls -l $DIR/${tdir} |
8952                 awk '/stats_testdir2/ {print $5}')
8953
8954         testdir1_size=$(order_2 $testdir1_size)
8955         testdir2_size=$(order_2 $testdir2_size)
8956
8957         testdir1_size=$(size_in_KMGT $testdir1_size)
8958         testdir2_size=$(size_in_KMGT $testdir2_size)
8959
8960         echo "source rename dir size: ${testdir1_size}"
8961         echo "target rename dir size: ${testdir2_size}"
8962
8963         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8964         eval $cmd || error "$cmd failed"
8965         local samedir=$($cmd | grep 'same_dir')
8966         local same_sample=$(get_rename_size $testdir1_size)
8967         [ -z "$samedir" ] && error "samedir_rename_size count error"
8968         [[ $same_sample -eq 1 ]] ||
8969                 error "samedir_rename_size error $same_sample"
8970         echo "Check same dir rename stats success"
8971
8972         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8973
8974         # check crossdir rename size
8975         mv ${testdir1}/test_0 ${testdir2}/test_0
8976
8977         testdir1_size=$(ls -l $DIR/${tdir} |
8978                 awk '/stats_testdir1/ {print $5}')
8979         testdir2_size=$(ls -l $DIR/${tdir} |
8980                 awk '/stats_testdir2/ {print $5}')
8981
8982         testdir1_size=$(order_2 $testdir1_size)
8983         testdir2_size=$(order_2 $testdir2_size)
8984
8985         testdir1_size=$(size_in_KMGT $testdir1_size)
8986         testdir2_size=$(size_in_KMGT $testdir2_size)
8987
8988         echo "source rename dir size: ${testdir1_size}"
8989         echo "target rename dir size: ${testdir2_size}"
8990
8991         eval $cmd || error "$cmd failed"
8992         local crossdir=$($cmd | grep 'crossdir')
8993         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8994         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8995         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8996         [[ $src_sample -eq 1 ]] ||
8997                 error "crossdir_rename_size error $src_sample"
8998         [[ $tgt_sample -eq 1 ]] ||
8999                 error "crossdir_rename_size error $tgt_sample"
9000         echo "Check cross dir rename stats success"
9001         rm -rf $DIR/${tdir}
9002 }
9003 run_test 133d "Verifying rename_stats ========================================"
9004
9005 test_133e() {
9006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9007         local testdir=$DIR/${tdir}/stats_testdir
9008         local ctr f0 f1 bs=32768 count=42 sum
9009
9010         remote_ost_nodsh && skip "remote OST with nodsh" && return
9011         mkdir -p ${testdir} || error "mkdir failed"
9012
9013         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9014
9015         for ctr in {write,read}_bytes; do
9016                 sync
9017                 cancel_lru_locks osc
9018
9019                 do_facet ost1 $LCTL set_param -n \
9020                         "obdfilter.*.exports.clear=clear"
9021
9022                 if [ $ctr = write_bytes ]; then
9023                         f0=/dev/zero
9024                         f1=${testdir}/${tfile}
9025                 else
9026                         f0=${testdir}/${tfile}
9027                         f1=/dev/null
9028                 fi
9029
9030                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9031                         error "dd failed"
9032                 sync
9033                 cancel_lru_locks osc
9034
9035                 sum=$(do_facet ost1 $LCTL get_param \
9036                         "obdfilter.*.exports.*.stats" |
9037                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9038                                 $1 == ctr { sum += $7 }
9039                                 END { printf("%0.0f", sum) }')
9040
9041                 if ((sum != bs * count)); then
9042                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9043                 fi
9044         done
9045
9046         rm -rf $DIR/${tdir}
9047 }
9048 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9049
9050 test_133f() {
9051         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9052         local facet
9053
9054         # First without trusting modes.
9055         find $proc_dirs -exec cat '{}' \; &> /dev/null
9056
9057         # Second verifying readability.
9058         find $proc_dirs \
9059                 -type f \
9060                 -exec cat '{}' \; &> /dev/null ||
9061                         error "proc file read failed"
9062
9063         for facet in $SINGLEMDS ost1; do
9064                 do_facet $facet find $proc_dirs \
9065                         ! -name req_history \
9066                         -exec cat '{}' \\\; &> /dev/null
9067
9068                 do_facet $facet find $proc_dirs \
9069                         ! -name req_history \
9070                         -type f \
9071                         -exec cat '{}' \\\; &> /dev/null ||
9072                                 error "proc file read failed"
9073         done
9074 }
9075 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9076
9077 test_133g() {
9078         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9079         local facet
9080
9081         # Second verifying readability.
9082         find $proc_dirs \
9083                 -type f \
9084                 -not -name force_lbug \
9085                 -not -name changelog_mask \
9086                 -exec badarea_io '{}' \; &> /dev/null ||
9087                 error "find $proc_dirs failed"
9088
9089         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9090                 skip "Too old lustre on MDS"
9091
9092         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9093                 skip "Too old lustre on ost1"
9094
9095         for facet in $SINGLEMDS ost1; do
9096                 do_facet $facet find $proc_dirs \
9097                         -type f \
9098                         -not -name force_lbug \
9099                         -not -name changelog_mask \
9100                         -exec badarea_io '{}' \\\; &> /dev/null ||
9101                 error "$facet find $proc_dirs failed"
9102
9103         done
9104
9105         # remount the FS in case writes/reads /proc break the FS
9106         cleanup || error "failed to unmount"
9107         setup || error "failed to setup"
9108         true
9109 }
9110 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9111
9112 test_140() { #bug-17379
9113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9114         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9115         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9116         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9117
9118         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9119         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9120         local i=0
9121         while i=`expr $i + 1`; do
9122                 test_mkdir -p $i || error "Creating dir $i"
9123                 cd $i || error "Changing to $i"
9124                 ln -s ../stat stat || error "Creating stat symlink"
9125                 # Read the symlink until ELOOP present,
9126                 # not LBUGing the system is considered success,
9127                 # we didn't overrun the stack.
9128                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9129                 [ $ret -ne 0 ] && {
9130                         if [ $ret -eq 40 ]; then
9131                                 break  # -ELOOP
9132                         else
9133                                 error "Open stat symlink"
9134                                 return
9135                         fi
9136                 }
9137         done
9138         i=`expr $i - 1`
9139         echo "The symlink depth = $i"
9140         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9141                                         error "Invalid symlink depth"
9142
9143         # Test recursive symlink
9144         ln -s symlink_self symlink_self
9145         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9146         echo "open symlink_self returns $ret"
9147         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9148 }
9149 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9150
9151 test_150() {
9152         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9153         local TF="$TMP/$tfile"
9154
9155         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9156         cp $TF $DIR/$tfile
9157         cancel_lru_locks osc
9158         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9159         remount_client $MOUNT
9160         df -P $MOUNT
9161         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9162
9163         $TRUNCATE $TF 6000
9164         $TRUNCATE $DIR/$tfile 6000
9165         cancel_lru_locks osc
9166         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9167
9168         echo "12345" >>$TF
9169         echo "12345" >>$DIR/$tfile
9170         cancel_lru_locks osc
9171         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9172
9173         echo "12345" >>$TF
9174         echo "12345" >>$DIR/$tfile
9175         cancel_lru_locks osc
9176         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9177
9178         rm -f $TF
9179         true
9180 }
9181 run_test 150 "truncate/append tests"
9182
9183 #LU-2902 roc_hit was not able to read all values from lproc
9184 function roc_hit_init() {
9185         local list=$(comma_list $(osts_nodes))
9186         local dir=$DIR/$tdir-check
9187         local file=$dir/file
9188         local BEFORE
9189         local AFTER
9190         local idx
9191
9192         test_mkdir -p $dir
9193         #use setstripe to do a write to every ost
9194         for i in $(seq 0 $((OSTCOUNT-1))); do
9195                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9196                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9197                 idx=$(printf %04x $i)
9198                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9199                         awk '$1 == "cache_access" {sum += $2}
9200                                 END { printf("%0.0f", sum) }')
9201
9202                 cancel_lru_locks osc
9203                 cat $file >/dev/null
9204
9205                 AFTER=$(get_osd_param $list *OST*$idx stats |
9206                         awk '$1 == "cache_access" {sum += $2}
9207                                 END { printf("%0.0f", sum) }')
9208
9209                 echo BEFORE:$BEFORE AFTER:$AFTER
9210                 if ! let "AFTER - BEFORE == 4"; then
9211                         rm -rf $dir
9212                         error "roc_hit is not safe to use"
9213                 fi
9214                 rm $file
9215         done
9216
9217         rm -rf $dir
9218 }
9219
9220 function roc_hit() {
9221         local list=$(comma_list $(osts_nodes))
9222         echo $(get_osd_param $list '' stats |
9223                 awk '$1 == "cache_hit" {sum += $2}
9224                         END { printf("%0.0f", sum) }')
9225 }
9226
9227 function set_cache() {
9228         local on=1
9229
9230         if [ "$2" == "off" ]; then
9231                 on=0;
9232         fi
9233         local list=$(comma_list $(osts_nodes))
9234         set_osd_param $list '' $1_cache_enable $on
9235
9236         cancel_lru_locks osc
9237 }
9238
9239 test_151() {
9240         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9241         remote_ost_nodsh && skip "remote OST with nodsh" && return
9242
9243         local CPAGES=3
9244         local list=$(comma_list $(osts_nodes))
9245
9246         # check whether obdfilter is cache capable at all
9247         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9248                 echo "not cache-capable obdfilter"
9249                 return 0
9250         fi
9251
9252         # check cache is enabled on all obdfilters
9253         if get_osd_param $list '' read_cache_enable | grep 0; then
9254                 echo "oss cache is disabled"
9255                 return 0
9256         fi
9257
9258         set_osd_param $list '' writethrough_cache_enable 1
9259
9260         # check write cache is enabled on all obdfilters
9261         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9262                 echo "oss write cache is NOT enabled"
9263                 return 0
9264         fi
9265
9266         roc_hit_init
9267
9268         #define OBD_FAIL_OBD_NO_LRU  0x609
9269         do_nodes $list $LCTL set_param fail_loc=0x609
9270
9271         # pages should be in the case right after write
9272         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9273                 error "dd failed"
9274
9275         local BEFORE=$(roc_hit)
9276         cancel_lru_locks osc
9277         cat $DIR/$tfile >/dev/null
9278         local AFTER=$(roc_hit)
9279
9280         do_nodes $list $LCTL set_param fail_loc=0
9281
9282         if ! let "AFTER - BEFORE == CPAGES"; then
9283                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9284         fi
9285
9286         # the following read invalidates the cache
9287         cancel_lru_locks osc
9288         set_osd_param $list '' read_cache_enable 0
9289         cat $DIR/$tfile >/dev/null
9290
9291         # now data shouldn't be found in the cache
9292         BEFORE=$(roc_hit)
9293         cancel_lru_locks osc
9294         cat $DIR/$tfile >/dev/null
9295         AFTER=$(roc_hit)
9296         if let "AFTER - BEFORE != 0"; then
9297                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9298         fi
9299
9300         set_osd_param $list '' read_cache_enable 1
9301         rm -f $DIR/$tfile
9302 }
9303 run_test 151 "test cache on oss and controls ==============================="
9304
9305 test_152() {
9306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9307         local TF="$TMP/$tfile"
9308
9309         # simulate ENOMEM during write
9310 #define OBD_FAIL_OST_NOMEM      0x226
9311         lctl set_param fail_loc=0x80000226
9312         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9313         cp $TF $DIR/$tfile
9314         sync || error "sync failed"
9315         lctl set_param fail_loc=0
9316
9317         # discard client's cache
9318         cancel_lru_locks osc
9319
9320         # simulate ENOMEM during read
9321         lctl set_param fail_loc=0x80000226
9322         cmp $TF $DIR/$tfile || error "cmp failed"
9323         lctl set_param fail_loc=0
9324
9325         rm -f $TF
9326 }
9327 run_test 152 "test read/write with enomem ============================"
9328
9329 test_153() {
9330         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9331 }
9332 run_test 153 "test if fdatasync does not crash ======================="
9333
9334 dot_lustre_fid_permission_check() {
9335         local fid=$1
9336         local ffid=$MOUNT/.lustre/fid/$fid
9337         local test_dir=$2
9338
9339         echo "stat fid $fid"
9340         stat $ffid > /dev/null || error "stat $ffid failed."
9341         echo "touch fid $fid"
9342         touch $ffid || error "touch $ffid failed."
9343         echo "write to fid $fid"
9344         cat /etc/hosts > $ffid || error "write $ffid failed."
9345         echo "read fid $fid"
9346         diff /etc/hosts $ffid || error "read $ffid failed."
9347         echo "append write to fid $fid"
9348         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9349         echo "rename fid $fid"
9350         mv $ffid $test_dir/$tfile.1 &&
9351                 error "rename $ffid to $tfile.1 should fail."
9352         touch $test_dir/$tfile.1
9353         mv $test_dir/$tfile.1 $ffid &&
9354                 error "rename $tfile.1 to $ffid should fail."
9355         rm -f $test_dir/$tfile.1
9356         echo "truncate fid $fid"
9357         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9358         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9359                 echo "link fid $fid"
9360                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9361         fi
9362         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9363                 echo "setfacl fid $fid"
9364                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9365                 echo "getfacl fid $fid"
9366                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9367         fi
9368         echo "unlink fid $fid"
9369         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9370         echo "mknod fid $fid"
9371         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9372
9373         fid=[0xf00000400:0x1:0x0]
9374         ffid=$MOUNT/.lustre/fid/$fid
9375
9376         echo "stat non-exist fid $fid"
9377         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9378         echo "write to non-exist fid $fid"
9379         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9380         echo "link new fid $fid"
9381         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9382
9383         mkdir -p $test_dir/$tdir
9384         touch $test_dir/$tdir/$tfile
9385         fid=$($LFS path2fid $test_dir/$tdir)
9386         rc=$?
9387         [ $rc -ne 0 ] &&
9388                 error "error: could not get fid for $test_dir/$dir/$tfile."
9389
9390         ffid=$MOUNT/.lustre/fid/$fid
9391
9392         echo "ls $fid"
9393         ls $ffid > /dev/null || error "ls $ffid failed."
9394         echo "touch $fid/$tfile.1"
9395         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9396
9397         echo "touch $MOUNT/.lustre/fid/$tfile"
9398         touch $MOUNT/.lustre/fid/$tfile && \
9399                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9400
9401         echo "setxattr to $MOUNT/.lustre/fid"
9402         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9403
9404         echo "listxattr for $MOUNT/.lustre/fid"
9405         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9406
9407         echo "delxattr from $MOUNT/.lustre/fid"
9408         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9409
9410         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9411         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9412                 error "touch invalid fid should fail."
9413
9414         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9415         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9416                 error "touch non-normal fid should fail."
9417
9418         echo "rename $tdir to $MOUNT/.lustre/fid"
9419         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9420                 error "rename to $MOUNT/.lustre/fid should fail."
9421
9422         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9423         then            # LU-3547
9424                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9425                 local new_obf_mode=777
9426
9427                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9428                 chmod $new_obf_mode $DIR/.lustre/fid ||
9429                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9430
9431                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9432                 [ $obf_mode -eq $new_obf_mode ] ||
9433                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9434
9435                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9436                 chmod $old_obf_mode $DIR/.lustre/fid ||
9437                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9438         fi
9439
9440         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9441         fid=$($LFS path2fid $test_dir/$tfile-2)
9442         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9443         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9444                 error "create lov data thru .lustre should fail."
9445         echo "cp /etc/passwd $test_dir/$tfile-2"
9446         cp /etc/passwd $test_dir/$tfile-2 ||
9447                 error "copy to $test_dir/$tfile-2 failed."
9448         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9449         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9450                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9451
9452         rm -rf $test_dir/tfile.lnk
9453         rm -rf $test_dir/$tfile-2
9454 }
9455
9456 test_154A() {
9457         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9458                 skip "Need MDS version at least 2.4.1" && return
9459
9460         touch $DIR/$tfile
9461         local FID=$($LFS path2fid $DIR/$tfile)
9462         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9463
9464         # check that we get the same pathname back
9465         local FOUND=$($LFS fid2path $MOUNT $FID)
9466         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9467         [ "$FOUND" != "$DIR/$tfile" ] &&
9468                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9469
9470         rm -rf $DIR/$tfile
9471 }
9472 run_test 154A "lfs path2fid and fid2path basic checks"
9473
9474 test_154a() {
9475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9476         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9477                 { skip "Need MDS version at least 2.2.51"; return 0; }
9478         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9479
9480         cp /etc/hosts $DIR/$tfile
9481
9482         fid=$($LFS path2fid $DIR/$tfile)
9483         rc=$?
9484         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9485
9486         dot_lustre_fid_permission_check "$fid" $DIR ||
9487                 error "dot lustre permission check $fid failed"
9488
9489         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9490
9491         touch $MOUNT/.lustre/file &&
9492                 error "creation is not allowed under .lustre"
9493
9494         mkdir $MOUNT/.lustre/dir &&
9495                 error "mkdir is not allowed under .lustre"
9496
9497         rm -rf $DIR/$tfile
9498 }
9499 run_test 154a "Open-by-FID"
9500
9501 test_154b() {
9502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9503         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9504                 { skip "Need MDS version at least 2.2.51"; return 0; }
9505
9506         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9507
9508         local remote_dir=$DIR/$tdir/remote_dir
9509         local MDTIDX=1
9510         local rc=0
9511
9512         mkdir -p $DIR/$tdir
9513         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9514                 error "create remote directory failed"
9515
9516         cp /etc/hosts $remote_dir/$tfile
9517
9518         fid=$($LFS path2fid $remote_dir/$tfile)
9519         rc=$?
9520         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9521
9522         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9523                 error "dot lustre permission check $fid failed"
9524         rm -rf $DIR/$tdir
9525 }
9526 run_test 154b "Open-by-FID for remote directory"
9527
9528 test_154c() {
9529         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9530                 skip "Need MDS version at least 2.4.1" && return
9531
9532         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9533         local FID1=$($LFS path2fid $DIR/$tfile.1)
9534         local FID2=$($LFS path2fid $DIR/$tfile.2)
9535         local FID3=$($LFS path2fid $DIR/$tfile.3)
9536
9537         local N=1
9538         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9539                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9540                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9541                 local want=FID$N
9542                 [ "$FID" = "${!want}" ] ||
9543                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9544                 N=$((N + 1))
9545         done
9546
9547         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9548                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9549                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9550                 N=$((N + 1))
9551         done
9552 }
9553 run_test 154c "lfs path2fid and fid2path multiple arguments"
9554
9555 test_154d() {
9556         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9557                 skip "Need MDS version at least 2.5.53" && return
9558
9559         if remote_mds; then
9560                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9561         else
9562                 nid="0@lo"
9563         fi
9564         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9565         local fd
9566         local cmd
9567
9568         rm -f $DIR/$tfile
9569         touch $DIR/$tfile
9570
9571         fid=$($LFS path2fid $DIR/$tfile)
9572         # Open the file
9573         fd=$(free_fd)
9574         cmd="exec $fd<$DIR/$tfile"
9575         eval $cmd
9576         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9577         echo $fid_list | grep $fid
9578         rc=$?
9579
9580         cmd="exec $fd>/dev/null"
9581         eval $cmd
9582         if [ $rc -ne 0 ]; then
9583                 error "FID $fid not found in open files list $fid_list"
9584         fi
9585 }
9586 run_test 154d "Verify open file fid"
9587
9588 test_155_small_load() {
9589     local temp=$TMP/$tfile
9590     local file=$DIR/$tfile
9591
9592     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9593         error "dd of=$temp bs=6096 count=1 failed"
9594     cp $temp $file
9595     cancel_lru_locks osc
9596     cmp $temp $file || error "$temp $file differ"
9597
9598     $TRUNCATE $temp 6000
9599     $TRUNCATE $file 6000
9600     cmp $temp $file || error "$temp $file differ (truncate1)"
9601
9602     echo "12345" >>$temp
9603     echo "12345" >>$file
9604     cmp $temp $file || error "$temp $file differ (append1)"
9605
9606     echo "12345" >>$temp
9607     echo "12345" >>$file
9608     cmp $temp $file || error "$temp $file differ (append2)"
9609
9610     rm -f $temp $file
9611     true
9612 }
9613
9614 test_155_big_load() {
9615     remote_ost_nodsh && skip "remote OST with nodsh" && return
9616     local temp=$TMP/$tfile
9617     local file=$DIR/$tfile
9618
9619     free_min_max
9620     local cache_size=$(do_facet ost$((MAXI+1)) \
9621         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9622     local large_file_size=$((cache_size * 2))
9623
9624     echo "OSS cache size: $cache_size KB"
9625     echo "Large file size: $large_file_size KB"
9626
9627     [ $MAXV -le $large_file_size ] && \
9628         skip_env "max available OST size needs > $large_file_size KB" && \
9629         return 0
9630
9631     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9632
9633     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9634         error "dd of=$temp bs=$large_file_size count=1k failed"
9635     cp $temp $file
9636     ls -lh $temp $file
9637     cancel_lru_locks osc
9638     cmp $temp $file || error "$temp $file differ"
9639
9640     rm -f $temp $file
9641     true
9642 }
9643
9644 test_155a() {
9645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9646         set_cache read on
9647         set_cache writethrough on
9648         test_155_small_load
9649 }
9650 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9651
9652 test_155b() {
9653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9654         set_cache read on
9655         set_cache writethrough off
9656         test_155_small_load
9657 }
9658 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9659
9660 test_155c() {
9661         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9662         set_cache read off
9663         set_cache writethrough on
9664         test_155_small_load
9665 }
9666 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9667
9668 test_155d() {
9669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9670         set_cache read off
9671         set_cache writethrough off
9672         test_155_small_load
9673 }
9674 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9675
9676 test_155e() {
9677         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9678         set_cache read on
9679         set_cache writethrough on
9680         test_155_big_load
9681 }
9682 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9683
9684 test_155f() {
9685         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9686         set_cache read on
9687         set_cache writethrough off
9688         test_155_big_load
9689 }
9690 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9691
9692 test_155g() {
9693         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9694         set_cache read off
9695         set_cache writethrough on
9696         test_155_big_load
9697 }
9698 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9699
9700 test_155h() {
9701         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9702         set_cache read off
9703         set_cache writethrough off
9704         test_155_big_load
9705 }
9706 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9707
9708 test_156() {
9709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9710         local CPAGES=3
9711         local BEFORE
9712         local AFTER
9713         local file="$DIR/$tfile"
9714
9715         [ "$(facet_fstype ost1)" = "zfs" ] &&
9716                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9717                 return
9718
9719         roc_hit_init
9720
9721     log "Turn on read and write cache"
9722     set_cache read on
9723     set_cache writethrough on
9724
9725     log "Write data and read it back."
9726     log "Read should be satisfied from the cache."
9727     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9728     BEFORE=`roc_hit`
9729     cancel_lru_locks osc
9730     cat $file >/dev/null
9731     AFTER=`roc_hit`
9732     if ! let "AFTER - BEFORE == CPAGES"; then
9733         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9734     else
9735         log "cache hits:: before: $BEFORE, after: $AFTER"
9736     fi
9737
9738     log "Read again; it should be satisfied from the cache."
9739     BEFORE=$AFTER
9740     cancel_lru_locks osc
9741     cat $file >/dev/null
9742     AFTER=`roc_hit`
9743     if ! let "AFTER - BEFORE == CPAGES"; then
9744         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9745     else
9746         log "cache hits:: before: $BEFORE, after: $AFTER"
9747     fi
9748
9749
9750     log "Turn off the read cache and turn on the write cache"
9751     set_cache read off
9752     set_cache writethrough on
9753
9754     log "Read again; it should be satisfied from the cache."
9755     BEFORE=`roc_hit`
9756     cancel_lru_locks osc
9757     cat $file >/dev/null
9758     AFTER=`roc_hit`
9759     if ! let "AFTER - BEFORE == CPAGES"; then
9760         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9761     else
9762         log "cache hits:: before: $BEFORE, after: $AFTER"
9763     fi
9764
9765     log "Read again; it should not be satisfied from the cache."
9766     BEFORE=$AFTER
9767     cancel_lru_locks osc
9768     cat $file >/dev/null
9769     AFTER=`roc_hit`
9770     if ! let "AFTER - BEFORE == 0"; then
9771         error "IN CACHE: before: $BEFORE, after: $AFTER"
9772     else
9773         log "cache hits:: before: $BEFORE, after: $AFTER"
9774     fi
9775
9776     log "Write data and read it back."
9777     log "Read should be satisfied from the cache."
9778     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9779     BEFORE=`roc_hit`
9780     cancel_lru_locks osc
9781     cat $file >/dev/null
9782     AFTER=`roc_hit`
9783     if ! let "AFTER - BEFORE == CPAGES"; then
9784         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9785     else
9786         log "cache hits:: before: $BEFORE, after: $AFTER"
9787     fi
9788
9789     log "Read again; it should not be satisfied from the cache."
9790     BEFORE=$AFTER
9791     cancel_lru_locks osc
9792     cat $file >/dev/null
9793     AFTER=`roc_hit`
9794     if ! let "AFTER - BEFORE == 0"; then
9795         error "IN CACHE: before: $BEFORE, after: $AFTER"
9796     else
9797         log "cache hits:: before: $BEFORE, after: $AFTER"
9798     fi
9799
9800
9801     log "Turn off read and write cache"
9802     set_cache read off
9803     set_cache writethrough off
9804
9805     log "Write data and read it back"
9806     log "It should not be satisfied from the cache."
9807     rm -f $file
9808     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9809     cancel_lru_locks osc
9810     BEFORE=`roc_hit`
9811     cat $file >/dev/null
9812     AFTER=`roc_hit`
9813         if ! let "AFTER - BEFORE == 0"; then
9814                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9815         else
9816                 log "cache hits:: before: $BEFORE, after: $AFTER"
9817         fi
9818
9819     log "Turn on the read cache and turn off the write cache"
9820     set_cache read on
9821     set_cache writethrough off
9822
9823     log "Write data and read it back"
9824     log "It should not be satisfied from the cache."
9825     rm -f $file
9826     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9827     BEFORE=`roc_hit`
9828     cancel_lru_locks osc
9829     cat $file >/dev/null
9830     AFTER=`roc_hit`
9831         if ! let "AFTER - BEFORE == 0"; then
9832                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9833         else
9834                 log "cache hits:: before: $BEFORE, after: $AFTER"
9835         fi
9836
9837     log "Read again; it should be satisfied from the cache."
9838     BEFORE=`roc_hit`
9839     cancel_lru_locks osc
9840     cat $file >/dev/null
9841     AFTER=`roc_hit`
9842     if ! let "AFTER - BEFORE == CPAGES"; then
9843         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9844     else
9845         log "cache hits:: before: $BEFORE, after: $AFTER"
9846     fi
9847
9848     rm -f $file
9849 }
9850 run_test 156 "Verification of tunables ============================"
9851
9852 #Changelogs
9853 err17935 () {
9854         if [[ $MDSCOUNT -gt 1 ]]; then
9855                 error_ignore bz17935 $*
9856         else
9857                 error $*
9858         fi
9859 }
9860
9861 changelog_chmask()
9862 {
9863         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9864
9865         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9866
9867         if [ $MASK -eq 1 ]; then
9868                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9869         else
9870                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9871         fi
9872 }
9873
9874 changelog_extract_field() {
9875         local mdt=$1
9876         local cltype=$2
9877         local file=$3
9878         local identifier=$4
9879
9880         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9881                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9882                 tail -1
9883 }
9884
9885 test_160a() {
9886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9887         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9888         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9889                 { skip "Need MDS version at least 2.2.0"; return; }
9890
9891         local CL_USERS="mdd.$MDT0.changelog_users"
9892         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9893         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9894         echo "Registered as changelog user $USER"
9895         $GET_CL_USERS | grep -q $USER ||
9896                 error "User $USER not found in changelog_users"
9897
9898         # change something
9899         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9900         touch $DIR/$tdir/pics/2008/zachy/timestamp
9901         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9902         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9903         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9904         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9905         rm $DIR/$tdir/pics/desktop.jpg
9906
9907         $LFS changelog $MDT0 | tail -5
9908
9909         echo "verifying changelog mask"
9910         changelog_chmask "MKDIR"
9911         changelog_chmask "CLOSE"
9912
9913         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9914         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9915
9916         changelog_chmask "MKDIR"
9917         changelog_chmask "CLOSE"
9918
9919         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9920         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9921
9922         $LFS changelog $MDT0
9923         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9924         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9925         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9926         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9927
9928         # verify contents
9929         echo "verifying target fid"
9930         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9931         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9932         [ "$fidc" == "$fidf" ] ||
9933                 err17935 "fid in changelog $fidc != file fid $fidf"
9934         echo "verifying parent fid"
9935         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9936         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9937         [ "$fidc" == "$fidf" ] ||
9938                 err17935 "pfid in changelog $fidc != dir fid $fidf"
9939
9940         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9941         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9942         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9943         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9944         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
9945                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
9946
9947         MIN_REC=$($GET_CL_USERS |
9948                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
9949         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
9950         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9951         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
9952                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
9953
9954         # LU-3446 changelog index reset on MDT restart
9955         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9956         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9957         $LFS changelog_clear $MDT0 $USER 0
9958         stop $SINGLEMDS || error "Fail to stop MDT."
9959         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9960         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9961         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
9962         [ $CUR_REC1 == $CUR_REC2 ] ||
9963                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
9964
9965         echo "verifying user deregister"
9966         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9967         $GET_CL_USERS | grep -q $USER &&
9968                 error "User $USER still in changelog_users"
9969
9970         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
9971         if [ $USERS -eq 0 ]; then
9972                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9973                 touch $DIR/$tdir/chloe
9974                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9975                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
9976                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9977         else
9978                 echo "$USERS other changelog users; can't verify off"
9979         fi
9980 }
9981 run_test 160a "changelog sanity"
9982
9983 test_160b() { # LU-3587
9984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9985         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9986         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9987                 { skip "Need MDS version at least 2.2.0"; return; }
9988
9989         local CL_USERS="mdd.$MDT0.changelog_users"
9990         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9991         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9992         echo "Registered as changelog user $USER"
9993         $GET_CL_USERS | grep -q $USER ||
9994                 error "User $USER not found in changelog_users"
9995
9996         local LONGNAME1=$(str_repeat a 255)
9997         local LONGNAME2=$(str_repeat b 255)
9998
9999         cd $DIR
10000         echo "creating very long named file"
10001         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10002         echo "moving very long named file"
10003         mv $LONGNAME1 $LONGNAME2
10004
10005         $LFS changelog $MDT0 | grep RENME
10006
10007         echo "deregistering $USER"
10008         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10009
10010         rm -f $LONGNAME2
10011 }
10012 run_test 160b "Verify that very long rename doesn't crash in changelog"
10013
10014 test_160c() {
10015         local rc=0
10016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10017
10018         # Registration step
10019         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10020                 changelog_register -n)
10021
10022         rm -rf $DIR/$tdir
10023         mkdir -p $DIR/$tdir
10024         $MCREATE $DIR/$tdir/foo_160c
10025         changelog_chmask "TRUNC"
10026         $TRUNCATE $DIR/$tdir/foo_160c 200
10027         changelog_chmask "TRUNC"
10028         $TRUNCATE $DIR/$tdir/foo_160c 199
10029         $LFS changelog $MDT0
10030         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10031         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10032         $LFS changelog_clear $MDT0 $USER 0
10033
10034         # Deregistration step
10035         echo "deregistering $USER"
10036         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10037 }
10038 run_test 160c "verify that changelog log catch the truncate event"
10039
10040 test_161a() {
10041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10042         test_mkdir -p -c1 $DIR/$tdir
10043         cp /etc/hosts $DIR/$tdir/$tfile
10044         test_mkdir -c1 $DIR/$tdir/foo1
10045         test_mkdir -c1 $DIR/$tdir/foo2
10046         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10047         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10048         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10049         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10050         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10051         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10052                 $LFS fid2path $DIR $FID
10053                 err17935 "bad link ea"
10054         fi
10055     # middle
10056     rm $DIR/$tdir/foo2/zachary
10057     # last
10058     rm $DIR/$tdir/foo2/thor
10059     # first
10060     rm $DIR/$tdir/$tfile
10061     # rename
10062     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10063     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10064         then
10065         $LFS fid2path $DIR $FID
10066         err17935 "bad link rename"
10067     fi
10068     rm $DIR/$tdir/foo2/maggie
10069
10070         # overflow the EA
10071         local longname=filename_avg_len_is_thirty_two_
10072         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10073                 error "failed to hardlink many files"
10074         links=$($LFS fid2path $DIR $FID | wc -l)
10075         echo -n "${links}/1000 links in link EA"
10076         [[ $links -gt 60 ]] ||
10077                 err17935 "expected at least 60 links in link EA"
10078         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10079                 error "failed to unlink many hardlinks"
10080 }
10081 run_test 161a "link ea sanity"
10082
10083 test_161b() {
10084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10085         [ $MDSCOUNT -lt 2 ] &&
10086                 skip "skipping remote directory test" && return
10087         local MDTIDX=1
10088         local remote_dir=$DIR/$tdir/remote_dir
10089
10090         mkdir -p $DIR/$tdir
10091         $LFS mkdir -i $MDTIDX $remote_dir ||
10092                 error "create remote directory failed"
10093
10094         cp /etc/hosts $remote_dir/$tfile
10095         mkdir -p $remote_dir/foo1
10096         mkdir -p $remote_dir/foo2
10097         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10098         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10099         ln $remote_dir/$tfile $remote_dir/foo1/luna
10100         ln $remote_dir/$tfile $remote_dir/foo2/thor
10101
10102         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10103                      tr -d ']')
10104         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10105                 $LFS fid2path $DIR $FID
10106                 err17935 "bad link ea"
10107         fi
10108         # middle
10109         rm $remote_dir/foo2/zachary
10110         # last
10111         rm $remote_dir/foo2/thor
10112         # first
10113         rm $remote_dir/$tfile
10114         # rename
10115         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10116         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10117         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10118                 $LFS fid2path $DIR $FID
10119                 err17935 "bad link rename"
10120         fi
10121         rm $remote_dir/foo2/maggie
10122
10123         # overflow the EA
10124         local longname=filename_avg_len_is_thirty_two_
10125         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10126                 error "failed to hardlink many files"
10127         links=$($LFS fid2path $DIR $FID | wc -l)
10128         echo -n "${links}/1000 links in link EA"
10129         [[ ${links} -gt 60 ]] ||
10130                 err17935 "expected at least 60 links in link EA"
10131         unlinkmany $remote_dir/foo2/$longname 1000 ||
10132         error "failed to unlink many hardlinks"
10133 }
10134 run_test 161b "link ea sanity under remote directory"
10135
10136 test_161c() {
10137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10138         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10139                 skip "Need MDS version at least 2.1.5" && return
10140
10141         # define CLF_RENAME_LAST 0x0001
10142         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10143         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10144                 changelog_register -n)
10145         rm -rf $DIR/$tdir
10146         mkdir -p $DIR/$tdir
10147         touch $DIR/$tdir/foo_161c
10148         touch $DIR/$tdir/bar_161c
10149         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10150         $LFS changelog $MDT0 | grep RENME
10151         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10152                 cut -f5 -d' ')
10153         $LFS changelog_clear $MDT0 $USER 0
10154         if [ x$flags != "x0x1" ]; then
10155                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10156                         $USER
10157                 error "flag $flags is not 0x1"
10158         fi
10159         echo "rename overwrite a target having nlink = 1," \
10160                 "changelog record has flags of $flags"
10161
10162         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10163         touch $DIR/$tdir/foo_161c
10164         touch $DIR/$tdir/bar_161c
10165         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10166         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10167         $LFS changelog $MDT0 | grep RENME
10168         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10169         $LFS changelog_clear $MDT0 $USER 0
10170         if [ x$flags != "x0x0" ]; then
10171                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10172                         $USER
10173                 error "flag $flags is not 0x0"
10174         fi
10175         echo "rename overwrite a target having nlink > 1," \
10176                 "changelog record has flags of $flags"
10177
10178         # rename doesn't overwrite a target (changelog flag 0x0)
10179         touch $DIR/$tdir/foo_161c
10180         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10181         $LFS changelog $MDT0 | grep RENME
10182         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10183         $LFS changelog_clear $MDT0 $USER 0
10184         if [ x$flags != "x0x0" ]; then
10185                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10186                         $USER
10187                 error "flag $flags is not 0x0"
10188         fi
10189         echo "rename doesn't overwrite a target," \
10190                 "changelog record has flags of $flags"
10191
10192         # define CLF_UNLINK_LAST 0x0001
10193         # unlink a file having nlink = 1 (changelog flag 0x1)
10194         rm -f $DIR/$tdir/foo2_161c
10195         $LFS changelog $MDT0 | grep UNLNK
10196         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10197         $LFS changelog_clear $MDT0 $USER 0
10198         if [ x$flags != "x0x1" ]; then
10199                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10200                         $USER
10201                 error "flag $flags is not 0x1"
10202         fi
10203         echo "unlink a file having nlink = 1," \
10204                 "changelog record has flags of $flags"
10205
10206         # unlink a file having nlink > 1 (changelog flag 0x0)
10207         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10208         rm -f $DIR/$tdir/foobar_161c
10209         $LFS changelog $MDT0 | grep UNLNK
10210         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10211         $LFS changelog_clear $MDT0 $USER 0
10212         if [ x$flags != "x0x0" ]; then
10213                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10214                         $USER
10215                 error "flag $flags is not 0x0"
10216         fi
10217         echo "unlink a file having nlink > 1," \
10218                 "changelog record has flags of $flags"
10219         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10220 }
10221 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10222
10223 check_path() {
10224     local expected=$1
10225     shift
10226     local fid=$2
10227
10228     local path=$(${LFS} fid2path $*)
10229     RC=$?
10230
10231     if [ $RC -ne 0 ]; then
10232         err17935 "path looked up of $expected failed. Error $RC"
10233         return $RC
10234     elif [ "${path}" != "${expected}" ]; then
10235         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10236         return 2
10237     fi
10238     echo "fid $fid resolves to path $path (expected $expected)"
10239 }
10240
10241 test_162() {
10242         # Make changes to filesystem
10243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10244         test_mkdir -p -c1 $DIR/$tdir/d2
10245         touch $DIR/$tdir/d2/$tfile
10246         touch $DIR/$tdir/d2/x1
10247         touch $DIR/$tdir/d2/x2
10248         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10249         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10250         # regular file
10251         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10252         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
10253
10254         # softlink
10255         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10256         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10257         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
10258
10259         # softlink to wrong file
10260         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10261         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10262         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
10263
10264         # hardlink
10265         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10266         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10267         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10268         # fid2path dir/fsname should both work
10269         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
10270         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
10271
10272         # hardlink count: check that there are 2 links
10273         # Doesnt work with CMD yet: 17935
10274         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10275                 err17935 "expected 2 links"
10276
10277         # hardlink indexing: remove the first link
10278         rm $DIR/$tdir/d2/p/q/r/hlink
10279         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
10280
10281         return 0
10282 }
10283 run_test 162 "path lookup sanity"
10284
10285 test_162b() {
10286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10287         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10288
10289         mkdir $DIR/$tdir
10290         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10291                                 error "create striped dir failed"
10292
10293         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10294                                         tail -n 1 | awk '{print $2}')
10295         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10296
10297         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10298         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10299
10300         # regular file
10301         for ((i=0;i<5;i++)); do
10302                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10303                         error "get fid for f$i failed"
10304                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
10305
10306                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10307                         error "get fid for d$i failed"
10308                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
10309         done
10310
10311         return 0
10312 }
10313 run_test 162b "striped directory path lookup sanity"
10314
10315 test_169() {
10316         # do directio so as not to populate the page cache
10317         log "creating a 10 Mb file"
10318         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10319         log "starting reads"
10320         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10321         log "truncating the file"
10322         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10323         log "killing dd"
10324         kill %+ || true # reads might have finished
10325         echo "wait until dd is finished"
10326         wait
10327         log "removing the temporary file"
10328         rm -rf $DIR/$tfile || error "tmp file removal failed"
10329 }
10330 run_test 169 "parallel read and truncate should not deadlock"
10331
10332 test_170() {
10333         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10334         $LCTL clear     # bug 18514
10335         $LCTL debug_daemon start $TMP/${tfile}_log_good
10336         touch $DIR/$tfile
10337         $LCTL debug_daemon stop
10338         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10339                error "sed failed to read log_good"
10340
10341         $LCTL debug_daemon start $TMP/${tfile}_log_good
10342         rm -rf $DIR/$tfile
10343         $LCTL debug_daemon stop
10344
10345         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10346                error "lctl df log_bad failed"
10347
10348         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10349         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10350
10351         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10352         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10353
10354         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10355                 error "bad_line good_line1 good_line2 are empty"
10356
10357         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10358         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10359         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10360
10361         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10362         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10363         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10364
10365         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10366                 error "bad_line_new good_line_new are empty"
10367
10368         local expected_good=$((good_line1 + good_line2*2))
10369
10370         rm -f $TMP/${tfile}*
10371         # LU-231, short malformed line may not be counted into bad lines
10372         if [ $bad_line -ne $bad_line_new ] &&
10373                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10374                 error "expected $bad_line bad lines, but got $bad_line_new"
10375                 return 1
10376         fi
10377
10378         if [ $expected_good -ne $good_line_new ]; then
10379                 error "expected $expected_good good lines, but got $good_line_new"
10380                 return 2
10381         fi
10382         true
10383 }
10384 run_test 170 "test lctl df to handle corrupted log ====================="
10385
10386 test_171() { # bug20592
10387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10388 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10389         $LCTL set_param fail_loc=0x50e
10390         $LCTL set_param fail_val=3000
10391         multiop_bg_pause $DIR/$tfile O_s || true
10392         local MULTIPID=$!
10393         kill -USR1 $MULTIPID
10394         # cause log dump
10395         sleep 3
10396         wait $MULTIPID
10397         if dmesg | grep "recursive fault"; then
10398                 error "caught a recursive fault"
10399         fi
10400         $LCTL set_param fail_loc=0
10401         true
10402 }
10403 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10404
10405 # it would be good to share it with obdfilter-survey/libecho code
10406 setup_obdecho_osc () {
10407         local rc=0
10408         local ost_nid=$1
10409         local obdfilter_name=$2
10410         echo "Creating new osc for $obdfilter_name on $ost_nid"
10411         # make sure we can find loopback nid
10412         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10413
10414         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10415                            ${obdfilter_name}_osc_UUID || rc=2; }
10416         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10417                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10418         return $rc
10419 }
10420
10421 cleanup_obdecho_osc () {
10422         local obdfilter_name=$1
10423         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10424         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10425         return 0
10426 }
10427
10428 obdecho_test() {
10429         local OBD=$1
10430         local node=$2
10431         local pages=${3:-64}
10432         local rc=0
10433         local id
10434
10435         local count=10
10436         local obd_size=$(get_obd_size $node $OBD)
10437         local page_size=$(get_page_size $node)
10438         if [[ -n "$obd_size" ]]; then
10439                 local new_count=$((obd_size / (pages * page_size / 1024)))
10440                 [[ $new_count -ge $count ]] || count=$new_count
10441         fi
10442
10443         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10444         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10445                            rc=2; }
10446         if [ $rc -eq 0 ]; then
10447             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10448             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10449         fi
10450         echo "New object id is $id"
10451         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10452                            rc=4; }
10453         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10454                            "test_brw $count w v $pages $id" || rc=4; }
10455         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10456                            rc=4; }
10457         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10458                                         "cleanup" || rc=5; }
10459         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10460                                         "detach" || rc=6; }
10461         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10462         return $rc
10463 }
10464
10465 test_180a() {
10466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10467         remote_ost_nodsh && skip "remote OST with nodsh" && return
10468         local rc=0
10469         local rmmod_local=0
10470
10471         if ! module_loaded obdecho; then
10472             load_module obdecho/obdecho
10473             rmmod_local=1
10474         fi
10475
10476         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10477         local host=$(lctl get_param -n osc.$osc.import |
10478                              awk '/current_connection:/ {print $2}' )
10479         local target=$(lctl get_param -n osc.$osc.import |
10480                              awk '/target:/ {print $2}' )
10481         target=${target%_UUID}
10482
10483         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10484         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10485         [[ -n $target ]] && cleanup_obdecho_osc $target
10486         [ $rmmod_local -eq 1 ] && rmmod obdecho
10487         return $rc
10488 }
10489 run_test 180a "test obdecho on osc"
10490
10491 test_180b() {
10492         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10493         remote_ost_nodsh && skip "remote OST with nodsh" && return
10494         local rc=0
10495         local rmmod_remote=0
10496
10497         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10498                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10499                       "modprobe obdecho; }" && rmmod_remote=1
10500         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10501         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10502         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10503         return $rc
10504 }
10505 run_test 180b "test obdecho directly on obdfilter"
10506
10507 test_180c() { # LU-2598
10508         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10509         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10510                 skip "Need MDS version at least 2.4.0" && return
10511
10512         local rc=0
10513         local rmmod_remote=false
10514         local pages=16384 # 64MB bulk I/O RPC size
10515         local target
10516
10517         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10518                 rmmod_remote=true || error "failed to load module obdecho"
10519
10520         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10521                 head -n1)
10522         if [ -n "$target" ]; then
10523                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10524         else
10525                 echo "there is no obdfilter target on ost1"
10526                 rc=2
10527         fi
10528         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10529         return $rc
10530 }
10531 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10532
10533 test_181() { # bug 22177
10534         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10535         # create enough files to index the directory
10536         createmany -o $DIR/$tdir/foobar 4000
10537         # print attributes for debug purpose
10538         lsattr -d .
10539         # open dir
10540         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10541         MULTIPID=$!
10542         # remove the files & current working dir
10543         unlinkmany $DIR/$tdir/foobar 4000
10544         rmdir $DIR/$tdir
10545         kill -USR1 $MULTIPID
10546         wait $MULTIPID
10547         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10548         return 0
10549 }
10550 run_test 181 "Test open-unlinked dir ========================"
10551
10552 test_182() {
10553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10554         # disable MDC RPC lock wouldn't crash client
10555         local fcount=1000
10556         local tcount=4
10557
10558         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10559 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10560         $LCTL set_param fail_loc=0x804
10561
10562         for (( i=0; i < $tcount; i++ )) ; do
10563                 mkdir $DIR/$tdir/$i
10564                 createmany -o $DIR/$tdir/$i/f- $fcount &
10565         done
10566         wait
10567
10568         for (( i=0; i < $tcount; i++ )) ; do
10569                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10570         done
10571         wait
10572
10573         rm -rf $DIR/$tdir
10574
10575         $LCTL set_param fail_loc=0
10576 }
10577 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10578
10579 test_183() { # LU-2275
10580         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10581                 skip "Need MDS version at least 2.3.56" && return
10582
10583         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10584         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10585         echo aaa > $DIR/$tdir/$tfile
10586
10587 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10588         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10589
10590         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10591         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10592
10593         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10594
10595         # Flush negative dentry cache
10596         touch $DIR/$tdir/$tfile
10597
10598         # We are not checking for any leaked references here, they'll
10599         # become evident next time we do cleanup with module unload.
10600         rm -rf $DIR/$tdir
10601 }
10602 run_test 183 "No crash or request leak in case of strange dispositions ========"
10603
10604 # test suite 184 is for LU-2016, LU-2017
10605 test_184a() {
10606         check_swap_layouts_support && return 0
10607
10608         dir0=$DIR/$tdir/$testnum
10609         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10610         ref1=/etc/passwd
10611         ref2=/etc/group
10612         file1=$dir0/f1
10613         file2=$dir0/f2
10614         $SETSTRIPE -c1 $file1
10615         cp $ref1 $file1
10616         $SETSTRIPE -c2 $file2
10617         cp $ref2 $file2
10618         gen1=$($GETSTRIPE -g $file1)
10619         gen2=$($GETSTRIPE -g $file2)
10620
10621         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10622         gen=$($GETSTRIPE -g $file1)
10623         [[ $gen1 != $gen ]] ||
10624                 "Layout generation on $file1 does not change"
10625         gen=$($GETSTRIPE -g $file2)
10626         [[ $gen2 != $gen ]] ||
10627                 "Layout generation on $file2 does not change"
10628
10629         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10630         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10631 }
10632 run_test 184a "Basic layout swap"
10633
10634 test_184b() {
10635         check_swap_layouts_support && return 0
10636
10637         dir0=$DIR/$tdir/$testnum
10638         mkdir -p $dir0 || error "creating dir $dir0"
10639         file1=$dir0/f1
10640         file2=$dir0/f2
10641         file3=$dir0/f3
10642         dir1=$dir0/d1
10643         dir2=$dir0/d2
10644         mkdir $dir1 $dir2
10645         $SETSTRIPE -c1 $file1
10646         $SETSTRIPE -c2 $file2
10647         $SETSTRIPE -c1 $file3
10648         chown $RUNAS_ID $file3
10649         gen1=$($GETSTRIPE -g $file1)
10650         gen2=$($GETSTRIPE -g $file2)
10651
10652         $LFS swap_layouts $dir1 $dir2 &&
10653                 error "swap of directories layouts should fail"
10654         $LFS swap_layouts $dir1 $file1 &&
10655                 error "swap of directory and file layouts should fail"
10656         $RUNAS $LFS swap_layouts $file1 $file2 &&
10657                 error "swap of file we cannot write should fail"
10658         $LFS swap_layouts $file1 $file3 &&
10659                 error "swap of file with different owner should fail"
10660         /bin/true # to clear error code
10661 }
10662 run_test 184b "Forbidden layout swap (will generate errors)"
10663
10664 test_184c() {
10665         check_swap_layouts_support && return 0
10666
10667         local dir0=$DIR/$tdir/$testnum
10668         mkdir -p $dir0 || error "creating dir $dir0"
10669
10670         local ref1=$dir0/ref1
10671         local ref2=$dir0/ref2
10672         local file1=$dir0/file1
10673         local file2=$dir0/file2
10674         # create a file large enough for the concurent test
10675         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10676         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10677         echo "ref file size: ref1($(stat -c %s $ref1))," \
10678              "ref2($(stat -c %s $ref2))"
10679
10680         cp $ref2 $file2
10681         dd if=$ref1 of=$file1 bs=16k &
10682         local DD_PID=$!
10683
10684         # Make sure dd starts to copy file
10685         while [ ! -f $file1 ]; do sleep 0.1; done
10686
10687         $LFS swap_layouts $file1 $file2
10688         local rc=$?
10689         wait $DD_PID
10690         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10691         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10692
10693         # how many bytes copied before swapping layout
10694         local copied=$(stat -c %s $file2)
10695         local remaining=$(stat -c %s $ref1)
10696         remaining=$((remaining - copied))
10697         echo "Copied $copied bytes before swapping layout..."
10698
10699         cmp -n $copied $file1 $ref2 | grep differ &&
10700                 error "Content mismatch [0, $copied) of ref2 and file1"
10701         cmp -n $copied $file2 $ref1 ||
10702                 error "Content mismatch [0, $copied) of ref1 and file2"
10703         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10704                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10705
10706         # clean up
10707         rm -f $ref1 $ref2 $file1 $file2
10708 }
10709 run_test 184c "Concurrent write and layout swap"
10710
10711 test_184d() {
10712         check_swap_layouts_support && return 0
10713         [ -z "$(which getfattr 2>/dev/null)" ] &&
10714                 skip "no getfattr command" && return 0
10715
10716         local file1=$DIR/$tdir/$tfile-1
10717         local file2=$DIR/$tdir/$tfile-2
10718         local file3=$DIR/$tdir/$tfile-3
10719         local lovea1
10720         local lovea2
10721
10722         mkdir -p $DIR/$tdir
10723         touch $file1 || error "create $file1 failed"
10724         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10725                 error "create $file2 failed"
10726         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10727                 error "create $file3 failed"
10728         lovea1=$($LFS getstripe $file1 | sed 1d)
10729
10730         $LFS swap_layouts $file2 $file3 ||
10731                 error "swap $file2 $file3 layouts failed"
10732         $LFS swap_layouts $file1 $file2 ||
10733                 error "swap $file1 $file2 layouts failed"
10734
10735         lovea2=$($LFS getstripe $file2 | sed 1d)
10736         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10737
10738         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10739         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10740 }
10741 run_test 184d "allow stripeless layouts swap"
10742
10743
10744 test_185() { # LU-2441
10745         # LU-3553 - no volatile file support in old servers
10746         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10747                 { skip "Need MDS version at least 2.3.60"; return 0; }
10748
10749         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10750         touch $DIR/$tdir/spoo
10751         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10752         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10753                 error "cannot create/write a volatile file"
10754         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10755                 error "FID is still valid after close"
10756
10757         multiop_bg_pause $DIR/$tdir vVw4096_c
10758         local multi_pid=$!
10759
10760         local OLD_IFS=$IFS
10761         IFS=":"
10762         local fidv=($fid)
10763         IFS=$OLD_IFS
10764         # assume that the next FID for this client is sequential, since stdout
10765         # is unfortunately eaten by multiop_bg_pause
10766         local n=$((${fidv[1]} + 1))
10767         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10768         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10769                 error "FID is missing before close"
10770         kill -USR1 $multi_pid
10771         # 1 second delay, so if mtime change we will see it
10772         sleep 1
10773         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10774         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10775 }
10776 run_test 185 "Volatile file support"
10777
10778 test_187a() {
10779         local dir0=$DIR/$tdir/$testnum
10780         mkdir -p $dir0 || error "creating dir $dir0"
10781
10782         local file=$dir0/file1
10783         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10784         local dv1=$($LFS data_version $file)
10785         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10786         local dv2=$($LFS data_version $file)
10787         [[ $dv1 != $dv2 ]] ||
10788                 error "data version did not change on write $dv1 == $dv2"
10789
10790         # clean up
10791         rm -f $file1
10792 }
10793 run_test 187a "Test data version change"
10794
10795 test_187b() {
10796         local dir0=$DIR/$tdir/$testnum
10797         mkdir -p $dir0 || error "creating dir $dir0"
10798
10799         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10800         [[ ${DV[0]} != ${DV[1]} ]] ||
10801                 error "data version did not change on write"\
10802                       " ${DV[0]} == ${DV[1]}"
10803
10804         # clean up
10805         rm -f $file1
10806 }
10807 run_test 187b "Test data version change on volatile file"
10808
10809 # OST pools tests
10810 check_file_in_pool()
10811 {
10812         local file=$1
10813         local pool=$2
10814         local tlist="$3"
10815         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10816         for i in $res
10817         do
10818                 for t in $tlist ; do
10819                         [ "$i" -eq "$t" ] && continue 2
10820                 done
10821
10822                 echo "pool list: $tlist"
10823                 echo "striping: $res"
10824                 error_noexit "$file not allocated in $pool"
10825                 return 1
10826         done
10827         return 0
10828 }
10829
10830 pool_add() {
10831         echo "Creating new pool"
10832         local pool=$1
10833
10834         create_pool $FSNAME.$pool ||
10835                 { error_noexit "No pool created, result code $?"; return 1; }
10836         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10837                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10838 }
10839
10840 pool_add_targets() {
10841         echo "Adding targets to pool"
10842         local pool=$1
10843         local first=$2
10844         local last=$3
10845         local step=${4:-1}
10846
10847         local list=$(seq $first $step $last)
10848
10849         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10850         do_facet mgs $LCTL pool_add \
10851                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10852         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10853                         | sort -u | tr '\n' ' ' " "$t" || { 
10854                 error_noexit "Add to pool failed"
10855                 return 1
10856         }
10857         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10858         local addcount=$(((last - first) / step + 1))
10859         [ $lfscount -eq $addcount ] || {
10860                 error_noexit "lfs pool_list bad ost count" \
10861                                                 "$lfscount != $addcount"
10862                 return 2
10863         }
10864 }
10865
10866 pool_set_dir() {
10867         local pool=$1
10868         local tdir=$2
10869         echo "Setting pool on directory $tdir"
10870
10871         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10872
10873         error_noexit "Cannot set pool $pool to $tdir"
10874         return 1
10875 }
10876
10877 pool_check_dir() {
10878         local pool=$1
10879         local tdir=$2
10880         echo "Checking pool on directory $tdir"
10881
10882         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10883         [ "$res" = "$pool" ] && return 0
10884
10885         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10886         return 1
10887 }
10888
10889 pool_dir_rel_path() {
10890         echo "Testing relative path works well"
10891         local pool=$1
10892         local tdir=$2
10893         local root=$3
10894
10895         mkdir -p $root/$tdir/$tdir
10896         cd $root/$tdir
10897         pool_set_dir $pool $tdir          || return 1
10898         pool_set_dir $pool ./$tdir        || return 2
10899         pool_set_dir $pool ../$tdir       || return 3
10900         pool_set_dir $pool ../$tdir/$tdir || return 4
10901         rm -rf $tdir; cd - > /dev/null
10902 }
10903
10904 pool_alloc_files() {
10905         echo "Checking files allocation from directory pool"
10906         local pool=$1
10907         local tdir=$2
10908         local count=$3
10909         local tlist="$4"
10910
10911         local failed=0
10912         for i in $(seq -w 1 $count)
10913         do
10914                 local file=$tdir/file-$i
10915                 touch $file
10916                 check_file_in_pool $file $pool "$tlist" || \
10917                         failed=$((failed + 1))
10918         done
10919         [ "$failed" = 0 ] && return 0
10920
10921         error_noexit "$failed files not allocated in $pool"
10922         return 1
10923 }
10924
10925 pool_create_files() {
10926         echo "Creating files in pool"
10927         local pool=$1
10928         local tdir=$2
10929         local count=$3
10930         local tlist="$4"
10931
10932         mkdir -p $tdir
10933         local failed=0
10934         for i in $(seq -w 1 $count)
10935         do
10936                 local file=$tdir/spoo-$i
10937                 $SETSTRIPE -p $pool $file
10938                 check_file_in_pool $file $pool "$tlist" || \
10939                         failed=$((failed + 1))
10940         done
10941         [ "$failed" = 0 ] && return 0
10942
10943         error_noexit "$failed files not allocated in $pool"
10944         return 1
10945 }
10946
10947 pool_lfs_df() {
10948         echo "Checking 'lfs df' output"
10949         local pool=$1
10950
10951         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
10952                         tr '\n' ' ')
10953         local res=$($LFS df --pool $FSNAME.$pool |
10954                         awk '{print $1}' |
10955                         grep "$FSNAME-OST" |
10956                         tr '\n' ' ')
10957         [ "$res" = "$t" ] && return 0
10958
10959         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
10960         return 1
10961 }
10962
10963 pool_file_rel_path() {
10964         echo "Creating files in a pool with relative pathname"
10965         local pool=$1
10966         local tdir=$2
10967
10968         mkdir -p $tdir ||
10969                 { error_noexit "unable to create $tdir"; return 1 ; }
10970         local file="/..$tdir/$tfile-1"
10971         $SETSTRIPE -p $pool $file ||
10972                 { error_noexit "unable to create $file" ; return 2 ; }
10973
10974         cd $tdir
10975         $SETSTRIPE -p $pool $tfile-2 || {
10976                 error_noexit "unable to create $tfile-2 in $tdir"
10977                 return 3
10978         }
10979 }
10980
10981 pool_remove_first_target() {
10982         echo "Removing first target from a pool"
10983         local pool=$1
10984
10985         local pname="lov.$FSNAME-*.pools.$pool"
10986         local t=$($LCTL get_param -n $pname | head -n1)
10987         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10988         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
10989                 error_noexit "$t not removed from $FSNAME.$pool"
10990                 return 1
10991         }
10992 }
10993
10994 pool_remove_all_targets() {
10995         echo "Removing all targets from pool"
10996         local pool=$1
10997         local file=$2
10998         local pname="lov.$FSNAME-*.pools.$pool"
10999         for t in $($LCTL get_param -n $pname | sort -u)
11000         do
11001                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11002         done
11003         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
11004                 error_noexit "Pool $FSNAME.$pool cannot be drained"
11005                 return 1
11006         }
11007         # striping on an empty/nonexistant pool should fall back
11008         # to "pool of everything"
11009         touch $file || {
11010                 error_noexit "failed to use fallback striping for empty pool"
11011                 return 2
11012         }
11013         # setstripe on an empty pool should fail
11014         $SETSTRIPE -p $pool $file 2>/dev/null && {
11015                 error_noexit "expected failure when creating file" \
11016                                                         "with empty pool"
11017                 return 3
11018         }
11019         return 0
11020 }
11021
11022 pool_remove() {
11023         echo "Destroying pool"
11024         local pool=$1
11025         local file=$2
11026
11027         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
11028
11029         sleep 2
11030         # striping on an empty/nonexistant pool should fall back 
11031         # to "pool of everything"
11032         touch $file || {
11033                 error_noexit "failed to use fallback striping for missing pool"
11034                 return 1
11035         }
11036         # setstripe on an empty pool should fail
11037         $SETSTRIPE -p $pool $file 2>/dev/null && {
11038                 error_noexit "expected failure when creating file" \
11039                                                         "with missing pool"
11040                 return 2
11041         }
11042
11043         # get param should return err once pool is gone
11044         if wait_update $HOSTNAME "lctl get_param -n \
11045                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
11046         then
11047                 remove_pool_from_list $FSNAME.$pool
11048                 return 0
11049         fi
11050         error_noexit "Pool $FSNAME.$pool is not destroyed"
11051         return 3
11052 }
11053
11054 test_200() {
11055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11056         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11057
11058         local POOL=${POOL:-cea1}
11059         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11060         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11061         # Pool OST targets
11062         local first_ost=0
11063         local last_ost=$(($OSTCOUNT - 1))
11064         local ost_step=2
11065         local ost_list=$(seq $first_ost $ost_step $last_ost)
11066         local ost_range="$first_ost $last_ost $ost_step"
11067         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11068         local file_dir=$POOL_ROOT/file_tst
11069
11070         local rc=0
11071         while : ; do
11072                 # former test_200a test_200b
11073                 pool_add $POOL                          || { rc=$? ; break; }
11074                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11075                 # former test_200c test_200d
11076                 mkdir -p $test_path
11077                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11078                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11079                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11080                                                         || { rc=$? ; break; }
11081                 # former test_200e test_200f
11082                 local files=$((OSTCOUNT*3))
11083                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11084                                                         || { rc=$? ; break; }
11085                 pool_create_files $POOL $file_dir $files "$ost_list" \
11086                                                         || { rc=$? ; break; }
11087                 # former test_200g test_200h
11088                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11089                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11090
11091                 # former test_201a test_201b test_201c
11092                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11093
11094                 local f=$test_path/$tfile
11095                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11096                 pool_remove $POOL $f                    || { rc=$? ; break; }
11097                 break
11098         done
11099
11100         cleanup_pools
11101         return $rc
11102 }
11103 run_test 200 "OST pools"
11104
11105 # usage: default_attr <count | size | offset>
11106 default_attr() {
11107         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11108 }
11109
11110 # usage: check_default_stripe_attr
11111 check_default_stripe_attr() {
11112         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11113         case $1 in
11114         --stripe-count|--count)
11115                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11116         --stripe-size|--size)
11117                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11118         --stripe-index|--index)
11119                 EXPECTED=-1;;
11120         *)
11121                 error "unknown getstripe attr '$1'"
11122         esac
11123
11124         [ $ACTUAL != $EXPECTED ] &&
11125                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11126 }
11127
11128 test_204a() {
11129         test_mkdir -p $DIR/$tdir
11130         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11131
11132         check_default_stripe_attr --stripe-count
11133         check_default_stripe_attr --stripe-size
11134         check_default_stripe_attr --stripe-index
11135
11136         return 0
11137 }
11138 run_test 204a "Print default stripe attributes ================="
11139
11140 test_204b() {
11141         test_mkdir -p $DIR/$tdir
11142         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11143
11144         check_default_stripe_attr --stripe-size
11145         check_default_stripe_attr --stripe-index
11146
11147         return 0
11148 }
11149 run_test 204b "Print default stripe size and offset  ==========="
11150
11151 test_204c() {
11152         test_mkdir -p $DIR/$tdir
11153         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11154
11155         check_default_stripe_attr --stripe-count
11156         check_default_stripe_attr --stripe-index
11157
11158         return 0
11159 }
11160 run_test 204c "Print default stripe count and offset ==========="
11161
11162 test_204d() {
11163         test_mkdir -p $DIR/$tdir
11164         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11165
11166         check_default_stripe_attr --stripe-count
11167         check_default_stripe_attr --stripe-size
11168
11169         return 0
11170 }
11171 run_test 204d "Print default stripe count and size ============="
11172
11173 test_204e() {
11174         test_mkdir -p $DIR/$tdir
11175         $SETSTRIPE -d $DIR/$tdir
11176
11177         check_default_stripe_attr --stripe-count --raw
11178         check_default_stripe_attr --stripe-size --raw
11179         check_default_stripe_attr --stripe-index --raw
11180
11181         return 0
11182 }
11183 run_test 204e "Print raw stripe attributes ================="
11184
11185 test_204f() {
11186         test_mkdir -p $DIR/$tdir
11187         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11188
11189         check_default_stripe_attr --stripe-size --raw
11190         check_default_stripe_attr --stripe-index --raw
11191
11192         return 0
11193 }
11194 run_test 204f "Print raw stripe size and offset  ==========="
11195
11196 test_204g() {
11197         test_mkdir -p $DIR/$tdir
11198         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11199
11200         check_default_stripe_attr --stripe-count --raw
11201         check_default_stripe_attr --stripe-index --raw
11202
11203         return 0
11204 }
11205 run_test 204g "Print raw stripe count and offset ==========="
11206
11207 test_204h() {
11208         test_mkdir -p $DIR/$tdir
11209         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11210
11211         check_default_stripe_attr --stripe-count --raw
11212         check_default_stripe_attr --stripe-size --raw
11213
11214         return 0
11215 }
11216 run_test 204h "Print raw stripe count and size ============="
11217
11218 # Figure out which job scheduler is being used, if any,
11219 # or use a fake one
11220 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11221         JOBENV=SLURM_JOB_ID
11222 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11223         JOBENV=LSB_JOBID
11224 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11225         JOBENV=PBS_JOBID
11226 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11227         JOBENV=LOADL_STEP_ID
11228 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11229         JOBENV=JOB_ID
11230 else
11231         JOBENV=FAKE_JOBID
11232 fi
11233
11234 verify_jobstats() {
11235         local cmd=$1
11236         local target=$2
11237
11238         # clear old jobstats
11239         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11240         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11241
11242         # use a new JobID for this test, or we might see an old one
11243         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11244
11245         JOBVAL=${!JOBENV}
11246         log "Test: $cmd"
11247         log "Using JobID environment variable $JOBENV=$JOBVAL"
11248
11249         if [ $JOBENV = "FAKE_JOBID" ]; then
11250                 FAKE_JOBID=$JOBVAL $cmd
11251         else
11252                 $cmd
11253         fi
11254
11255         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11256                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11257                 do_facet $FACET lctl get_param mdt.*.job_stats |
11258                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11259         fi
11260         if [ "$target" = "ost" -o "$target" = "both" ]; then
11261                 FACET=ost1
11262                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11263                         grep $JOBVAL || error "No job stats found on OST $FACET"
11264         fi
11265 }
11266
11267 jobstats_set() {
11268         trap 0
11269         NEW_JOBENV=${1:-$OLD_JOBENV}
11270         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11271         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11272 }
11273
11274 test_205() { # Job stats
11275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11276         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11277                 skip "Server doesn't support jobstats" && return 0
11278         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11279
11280         local cmd
11281         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11282         if [ $OLD_JOBENV != $JOBENV ]; then
11283                 jobstats_set $JOBENV
11284                 trap jobstats_set EXIT
11285         fi
11286
11287         # mkdir
11288         cmd="mkdir $DIR/$tfile"
11289         verify_jobstats "$cmd" "mdt"
11290         # rmdir
11291         cmd="rm -fr $DIR/$tfile"
11292         verify_jobstats "$cmd" "mdt"
11293         # mknod
11294         cmd="mknod $DIR/$tfile c 1 3"
11295         verify_jobstats "$cmd" "mdt"
11296         # unlink
11297         cmd="rm -f $DIR/$tfile"
11298         verify_jobstats "$cmd" "mdt"
11299         # open & close
11300         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11301         verify_jobstats "$cmd" "mdt"
11302         # setattr
11303         cmd="touch $DIR/$tfile"
11304         verify_jobstats "$cmd" "both"
11305         # write
11306         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11307         verify_jobstats "$cmd" "ost"
11308         # read
11309         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11310         verify_jobstats "$cmd" "ost"
11311         # truncate
11312         cmd="$TRUNCATE $DIR/$tfile 0"
11313         verify_jobstats "$cmd" "both"
11314         # rename
11315         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11316         verify_jobstats "$cmd" "mdt"
11317
11318         # cleanup
11319         rm -f $DIR/jobstats_test_rename
11320
11321         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11322 }
11323 run_test 205 "Verify job stats"
11324
11325 # LU-1480, LU-1773 and LU-1657
11326 test_206() {
11327         mkdir -p $DIR/$tdir
11328         $SETSTRIPE -c -1 $DIR/$tdir
11329 #define OBD_FAIL_LOV_INIT 0x1403
11330         $LCTL set_param fail_loc=0xa0001403
11331         $LCTL set_param fail_val=1
11332         touch $DIR/$tdir/$tfile || true
11333 }
11334 run_test 206 "fail lov_init_raid0() doesn't lbug"
11335
11336 test_207a() {
11337         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11338         local fsz=`stat -c %s $DIR/$tfile`
11339         cancel_lru_locks mdc
11340
11341         # do not return layout in getattr intent
11342 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11343         $LCTL set_param fail_loc=0x170
11344         local sz=`stat -c %s $DIR/$tfile`
11345
11346         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11347
11348         rm -rf $DIR/$tfile
11349 }
11350 run_test 207a "can refresh layout at glimpse"
11351
11352 test_207b() {
11353         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11354         local cksum=`md5sum $DIR/$tfile`
11355         local fsz=`stat -c %s $DIR/$tfile`
11356         cancel_lru_locks mdc
11357         cancel_lru_locks osc
11358
11359         # do not return layout in getattr intent
11360 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11361         $LCTL set_param fail_loc=0x171
11362
11363         # it will refresh layout after the file is opened but before read issues
11364         echo checksum is "$cksum"
11365         echo "$cksum" |md5sum -c --quiet || error "file differs"
11366
11367         rm -rf $DIR/$tfile
11368 }
11369 run_test 207b "can refresh layout at open"
11370
11371 test_208() {
11372         # FIXME: in this test suite, only RD lease is used. This is okay
11373         # for now as only exclusive open is supported. After generic lease
11374         # is done, this test suite should be revised. - Jinshan
11375
11376         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11377                 { skip "Need MDS version at least 2.4.52"; return 0; }
11378
11379         echo "==== test 1: verify get lease work"
11380         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11381
11382         echo "==== test 2: verify lease can be broken by upcoming open"
11383         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11384         local PID=$!
11385         sleep 1
11386
11387         $MULTIOP $DIR/$tfile oO_RDONLY:c
11388         kill -USR1 $PID && wait $PID || error "break lease error"
11389
11390         echo "==== test 3: verify lease can't be granted if an open already exists"
11391         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11392         local PID=$!
11393         sleep 1
11394
11395         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11396         kill -USR1 $PID && wait $PID || error "open file error"
11397
11398         echo "==== test 4: lease can sustain over recovery"
11399         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11400         PID=$!
11401         sleep 1
11402
11403         fail mds1
11404
11405         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11406
11407         echo "==== test 5: lease broken can't be regained by replay"
11408         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11409         PID=$!
11410         sleep 1
11411
11412         # open file to break lease and then recovery
11413         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11414         fail mds1
11415
11416         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11417
11418         rm -f $DIR/$tfile
11419 }
11420 run_test 208 "Exclusive open"
11421
11422 test_209() {
11423         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11424                 skip_env "must have disp_stripe" && return
11425
11426         touch $DIR/$tfile
11427         sync; sleep 5; sync;
11428
11429         echo 3 > /proc/sys/vm/drop_caches
11430         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11431
11432         # open/close 500 times
11433         for i in $(seq 500); do
11434                 cat $DIR/$tfile
11435         done
11436
11437         echo 3 > /proc/sys/vm/drop_caches
11438         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11439
11440         echo "before: $req_before, after: $req_after"
11441         [ $((req_after - req_before)) -ge 300 ] &&
11442                 error "open/close requests are not freed"
11443         return 0
11444 }
11445 run_test 209 "read-only open/close requests should be freed promptly"
11446
11447 test_212() {
11448         size=`date +%s`
11449         size=$((size % 8192 + 1))
11450         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11451         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11452         rm -f $DIR/f212 $DIR/f212.xyz
11453 }
11454 run_test 212 "Sendfile test ============================================"
11455
11456 test_213() {
11457         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11458         cancel_lru_locks osc
11459         lctl set_param fail_loc=0x8000040f
11460         # generate a read lock
11461         cat $DIR/$tfile > /dev/null
11462         # write to the file, it will try to cancel the above read lock.
11463         cat /etc/hosts >> $DIR/$tfile
11464 }
11465 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11466
11467 test_214() { # for bug 20133
11468         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11469         for (( i=0; i < 340; i++ )) ; do
11470                 touch $DIR/$tdir/d214c/a$i
11471         done
11472
11473         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11474         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11475         ls $DIR/d214c || error "ls $DIR/d214c failed"
11476         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11477         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11478 }
11479 run_test 214 "hash-indexed directory test - bug 20133"
11480
11481 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11482 create_lnet_proc_files() {
11483         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11484         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11485
11486         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11487         rm -f "$TMP/lnet_$1.sys_tmp"
11488 }
11489
11490 # counterpart of create_lnet_proc_files
11491 remove_lnet_proc_files() {
11492         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11493 }
11494
11495 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11496 # 3rd arg as regexp for body
11497 check_lnet_proc_stats() {
11498         local l=$(cat "$TMP/lnet_$1" |wc -l)
11499         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11500
11501         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11502 }
11503
11504 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11505 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11506 # optional and can be regexp for 2nd line (lnet.routes case)
11507 check_lnet_proc_entry() {
11508         local blp=2          # blp stands for 'position of 1st line of body'
11509         [ -z "$5" ] || blp=3 # lnet.routes case
11510
11511         local l=$(cat "$TMP/lnet_$1" |wc -l)
11512         # subtracting one from $blp because the body can be empty
11513         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11514
11515         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11516                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11517
11518         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11519                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11520
11521         # bail out if any unexpected line happened
11522         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11523         [ "$?" != 0 ] || error "$2 misformatted"
11524 }
11525
11526 test_215() { # for bugs 18102, 21079, 21517
11527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11528         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11529         local P='[1-9][0-9]*'           # positive numeric
11530         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11531         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11532         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11533         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11534
11535         local L1 # regexp for 1st line
11536         local L2 # regexp for 2nd line (optional)
11537         local BR # regexp for the rest (body)
11538
11539         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11540         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11541         create_lnet_proc_files "stats"
11542         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11543         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11544         remove_lnet_proc_files "stats"
11545
11546         # /proc/sys/lnet/routes should look like this:
11547         # Routing disabled/enabled
11548         # net hops priority state router
11549         # where net is a string like tcp0, hops > 0, priority >= 0,
11550         # state is up/down,
11551         # router is a string like 192.168.1.1@tcp2
11552         L1="^Routing (disabled|enabled)$"
11553         L2="^net +hops +priority +state +router$"
11554         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11555         create_lnet_proc_files "routes"
11556         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11557         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11558         remove_lnet_proc_files "routes"
11559
11560         # /proc/sys/lnet/routers should look like this:
11561         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11562         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11563         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11564         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11565         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11566         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11567         create_lnet_proc_files "routers"
11568         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11569         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11570         remove_lnet_proc_files "routers"
11571
11572         # /proc/sys/lnet/peers should look like this:
11573         # nid refs state last max rtr min tx min queue
11574         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11575         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11576         # numeric (0 or >0 or <0), queue >= 0.
11577         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11578         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11579         create_lnet_proc_files "peers"
11580         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11581         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11582         remove_lnet_proc_files "peers"
11583
11584         # /proc/sys/lnet/buffers  should look like this:
11585         # pages count credits min
11586         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11587         L1="^pages +count +credits +min$"
11588         BR="^ +$N +$N +$I +$I$"
11589         create_lnet_proc_files "buffers"
11590         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11591         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11592         remove_lnet_proc_files "buffers"
11593
11594         # /proc/sys/lnet/nis should look like this:
11595         # nid status alive refs peer rtr max tx min
11596         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11597         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11598         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11599         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11600         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11601         create_lnet_proc_files "nis"
11602         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11603         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11604         remove_lnet_proc_files "nis"
11605
11606         # can we successfully write to /proc/sys/lnet/stats?
11607         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11608         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11609 }
11610 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11611
11612 test_216() { # bug 20317
11613         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11614         remote_ost_nodsh && skip "remote OST with nodsh" && return
11615
11616         local node
11617         local facets=$(get_facets OST)
11618         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11619
11620         save_lustre_params client "osc.*.contention_seconds" > $p
11621         save_lustre_params $facets \
11622                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11623         save_lustre_params $facets \
11624                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11625         save_lustre_params $facets \
11626                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11627         clear_osc_stats
11628
11629         # agressive lockless i/o settings
11630         for node in $(osts_nodes); do
11631                 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'
11632         done
11633         lctl set_param -n osc.*.contention_seconds 60
11634
11635         $DIRECTIO write $DIR/$tfile 0 10 4096
11636         $CHECKSTAT -s 40960 $DIR/$tfile
11637
11638         # disable lockless i/o
11639         for node in $(osts_nodes); do
11640                 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'
11641         done
11642         lctl set_param -n osc.*.contention_seconds 0
11643         clear_osc_stats
11644
11645         dd if=/dev/zero of=$DIR/$tfile count=0
11646         $CHECKSTAT -s 0 $DIR/$tfile
11647
11648         restore_lustre_params <$p
11649         rm -f $p
11650         rm $DIR/$tfile
11651 }
11652 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11653
11654 test_217() { # bug 22430
11655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11656         local node
11657         local nid
11658
11659         for node in $(nodes_list); do
11660                 nid=$(host_nids_address $node $NETTYPE)
11661                 if [[ $nid = *-* ]] ; then
11662                         echo "lctl ping $nid@$NETTYPE"
11663                         lctl ping $nid@$NETTYPE
11664                 else
11665                         echo "skipping $node (no hyphen detected)"
11666                 fi
11667         done
11668 }
11669 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11670
11671 test_218() {
11672        # do directio so as not to populate the page cache
11673        log "creating a 10 Mb file"
11674        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11675        log "starting reads"
11676        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11677        log "truncating the file"
11678        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11679        log "killing dd"
11680        kill %+ || true # reads might have finished
11681        echo "wait until dd is finished"
11682        wait
11683        log "removing the temporary file"
11684        rm -rf $DIR/$tfile || error "tmp file removal failed"
11685 }
11686 run_test 218 "parallel read and truncate should not deadlock ======================="
11687
11688 test_219() {
11689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11690         # write one partial page
11691         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11692         # set no grant so vvp_io_commit_write will do sync write
11693         $LCTL set_param fail_loc=0x411
11694         # write a full page at the end of file
11695         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11696
11697         $LCTL set_param fail_loc=0
11698         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11699         $LCTL set_param fail_loc=0x411
11700         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11701
11702         # LU-4201
11703         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11704         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11705 }
11706 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11707
11708 test_220() { #LU-325
11709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11710         remote_ost_nodsh && skip "remote OST with nodsh" && return
11711         local OSTIDX=0
11712
11713         test_mkdir -p $DIR/$tdir
11714         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11715                 awk '{print $2}' | sed -e 's/_UUID$//')
11716
11717         # on the mdt's osc
11718         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11719         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11720                         osc.$mdtosc_proc1.prealloc_last_id)
11721         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11722                         osc.$mdtosc_proc1.prealloc_next_id)
11723
11724         $LFS df -i
11725
11726         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11727         #define OBD_FAIL_OST_ENOINO              0x229
11728         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11729         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11730         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11731
11732         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11733
11734         MDSOBJS=$((last_id - next_id))
11735         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11736
11737         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11738         echo "OST still has $count kbytes free"
11739
11740         echo "create $MDSOBJS files @next_id..."
11741         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11742
11743         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11744                         osc.$mdtosc_proc1.prealloc_last_id)
11745         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11746                         osc.$mdtosc_proc1.prealloc_next_id)
11747
11748         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11749         $LFS df -i
11750
11751         echo "cleanup..."
11752
11753         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11754         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11755
11756         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11757         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11758         echo "unlink $MDSOBJS files @$next_id..."
11759         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11760 }
11761 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11762
11763 test_221() {
11764         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11765         dd if=`which date` of=$MOUNT/date oflag=sync
11766         chmod +x $MOUNT/date
11767
11768         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11769         $LCTL set_param fail_loc=0x80001401
11770
11771         $MOUNT/date > /dev/null
11772         rm -f $MOUNT/date
11773 }
11774 run_test 221 "make sure fault and truncate race to not cause OOM"
11775
11776 test_222a () {
11777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11778        rm -rf $DIR/$tdir
11779        test_mkdir -p $DIR/$tdir
11780        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11781        createmany -o $DIR/$tdir/$tfile 10
11782        cancel_lru_locks mdc
11783        cancel_lru_locks osc
11784        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11785        $LCTL set_param fail_loc=0x31a
11786        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11787        $LCTL set_param fail_loc=0
11788        rm -r $DIR/$tdir
11789 }
11790 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11791
11792 test_222b () {
11793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11794        rm -rf $DIR/$tdir
11795        test_mkdir -p $DIR/$tdir
11796        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11797        createmany -o $DIR/$tdir/$tfile 10
11798        cancel_lru_locks mdc
11799        cancel_lru_locks osc
11800        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11801        $LCTL set_param fail_loc=0x31a
11802        rm -r $DIR/$tdir || "AGL for rmdir failed"
11803        $LCTL set_param fail_loc=0
11804 }
11805 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11806
11807 test_223 () {
11808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11809        rm -rf $DIR/$tdir
11810        test_mkdir -p $DIR/$tdir
11811        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11812        createmany -o $DIR/$tdir/$tfile 10
11813        cancel_lru_locks mdc
11814        cancel_lru_locks osc
11815        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11816        $LCTL set_param fail_loc=0x31b
11817        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11818        $LCTL set_param fail_loc=0
11819        rm -r $DIR/$tdir
11820 }
11821 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11822
11823 test_224a() { # LU-1039, MRP-303
11824         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11825         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11826         $LCTL set_param fail_loc=0x508
11827         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11828         $LCTL set_param fail_loc=0
11829         df $DIR
11830 }
11831 run_test 224a "Don't panic on bulk IO failure"
11832
11833 test_224b() { # LU-1039, MRP-303
11834         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11835         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11836         cancel_lru_locks osc
11837         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11838         $LCTL set_param fail_loc=0x515
11839         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11840         $LCTL set_param fail_loc=0
11841         df $DIR
11842 }
11843 run_test 224b "Don't panic on bulk IO failure"
11844
11845 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11846 test_225a () {
11847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11848         if [ -z ${MDSSURVEY} ]; then
11849               skip_env "mds-survey not found" && return
11850         fi
11851
11852         [ $MDSCOUNT -ge 2 ] &&
11853                 skip "skipping now for more than one MDT" && return
11854
11855        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11856             { skip "Need MDS version at least 2.2.51"; return; }
11857
11858        local mds=$(facet_host $SINGLEMDS)
11859        local target=$(do_nodes $mds 'lctl dl' | \
11860                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11861
11862        local cmd1="file_count=1000 thrhi=4"
11863        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11864        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11865        local cmd="$cmd1 $cmd2 $cmd3"
11866
11867        rm -f ${TMP}/mds_survey*
11868        echo + $cmd
11869        eval $cmd || error "mds-survey with zero-stripe failed"
11870        cat ${TMP}/mds_survey*
11871        rm -f ${TMP}/mds_survey*
11872 }
11873 run_test 225a "Metadata survey sanity with zero-stripe"
11874
11875 test_225b () {
11876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11877
11878         if [ -z ${MDSSURVEY} ]; then
11879               skip_env "mds-survey not found" && return
11880         fi
11881         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11882             { skip "Need MDS version at least 2.2.51"; return; }
11883
11884         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11885               skip_env "Need to mount OST to test" && return
11886         fi
11887
11888         [ $MDSCOUNT -ge 2 ] &&
11889                 skip "skipping now for more than one MDT" && return
11890        local mds=$(facet_host $SINGLEMDS)
11891        local target=$(do_nodes $mds 'lctl dl' | \
11892                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11893
11894        local cmd1="file_count=1000 thrhi=4"
11895        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11896        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11897        local cmd="$cmd1 $cmd2 $cmd3"
11898
11899        rm -f ${TMP}/mds_survey*
11900        echo + $cmd
11901        eval $cmd || error "mds-survey with stripe_count failed"
11902        cat ${TMP}/mds_survey*
11903        rm -f ${TMP}/mds_survey*
11904 }
11905 run_test 225b "Metadata survey sanity with stripe_count = 1"
11906
11907 mcreate_path2fid () {
11908         local mode=$1
11909         local major=$2
11910         local minor=$3
11911         local name=$4
11912         local desc=$5
11913         local path=$DIR/$tdir/$name
11914         local fid
11915         local rc
11916         local fid_path
11917
11918         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11919                 error "cannot create $desc"
11920
11921         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11922         rc=$?
11923         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11924
11925         fid_path=$($LFS fid2path $MOUNT $fid)
11926         rc=$?
11927         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11928
11929         [ "$path" == "$fid_path" ] ||
11930                 error "fid2path returned $fid_path, expected $path"
11931
11932         echo "pass with $path and $fid"
11933 }
11934
11935 test_226a () {
11936         rm -rf $DIR/$tdir
11937         mkdir -p $DIR/$tdir
11938
11939         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11940         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11941         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11942         mcreate_path2fid 0040666 0 0 dir "directory"
11943         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11944         mcreate_path2fid 0100666 0 0 file "regular file"
11945         mcreate_path2fid 0120666 0 0 link "symbolic link"
11946         mcreate_path2fid 0140666 0 0 sock "socket"
11947 }
11948 run_test 226a "call path2fid and fid2path on files of all type"
11949
11950 test_226b () {
11951         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11952         rm -rf $DIR/$tdir
11953         local MDTIDX=1
11954
11955         mkdir -p $DIR/$tdir
11956         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11957                 error "create remote directory failed"
11958         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11959         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11960                                 "character special file (null)"
11961         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11962                                 "character special file (no device)"
11963         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11964         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11965                                 "block special file (loop)"
11966         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11967         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11968         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11969 }
11970 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11971
11972 # LU-1299 Executing or running ldd on a truncated executable does not
11973 # cause an out-of-memory condition.
11974 test_227() {
11975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11976         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
11977         dd if=$(which date) of=$MOUNT/date bs=1k count=1
11978         chmod +x $MOUNT/date
11979
11980         $MOUNT/date > /dev/null
11981         ldd $MOUNT/date > /dev/null
11982         rm -f $MOUNT/date
11983 }
11984 run_test 227 "running truncated executable does not cause OOM"
11985
11986 # LU-1512 try to reuse idle OI blocks
11987 test_228a() {
11988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11989         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11990                 skip "non-ldiskfs backend" && return
11991
11992         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11993         local myDIR=$DIR/$tdir
11994
11995         mkdir -p $myDIR
11996         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11997         $LCTL set_param fail_loc=0x80001002
11998         createmany -o $myDIR/t- 10000
11999         $LCTL set_param fail_loc=0
12000         # The guard is current the largest FID holder
12001         touch $myDIR/guard
12002         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12003                     tr -d '[')
12004         local IDX=$(($SEQ % 64))
12005
12006         do_facet $SINGLEMDS sync
12007         # Make sure journal flushed.
12008         sleep 6
12009         local blk1=$(do_facet $SINGLEMDS \
12010                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12011                      grep Blockcount | awk '{print $4}')
12012
12013         # Remove old files, some OI blocks will become idle.
12014         unlinkmany $myDIR/t- 10000
12015         # Create new files, idle OI blocks should be reused.
12016         createmany -o $myDIR/t- 2000
12017         do_facet $SINGLEMDS sync
12018         # Make sure journal flushed.
12019         sleep 6
12020         local blk2=$(do_facet $SINGLEMDS \
12021                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12022                      grep Blockcount | awk '{print $4}')
12023
12024         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12025 }
12026 run_test 228a "try to reuse idle OI blocks"
12027
12028 test_228b() {
12029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12030         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12031                 skip "non-ldiskfs backend" && return
12032
12033         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12034         local myDIR=$DIR/$tdir
12035
12036         mkdir -p $myDIR
12037         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12038         $LCTL set_param fail_loc=0x80001002
12039         createmany -o $myDIR/t- 10000
12040         $LCTL set_param fail_loc=0
12041         # The guard is current the largest FID holder
12042         touch $myDIR/guard
12043         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12044                     tr -d '[')
12045         local IDX=$(($SEQ % 64))
12046
12047         do_facet $SINGLEMDS sync
12048         # Make sure journal flushed.
12049         sleep 6
12050         local blk1=$(do_facet $SINGLEMDS \
12051                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12052                      grep Blockcount | awk '{print $4}')
12053
12054         # Remove old files, some OI blocks will become idle.
12055         unlinkmany $myDIR/t- 10000
12056
12057         # stop the MDT
12058         stop $SINGLEMDS || error "Fail to stop MDT."
12059         # remount the MDT
12060         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12061
12062         df $MOUNT || error "Fail to df."
12063         # Create new files, idle OI blocks should be reused.
12064         createmany -o $myDIR/t- 2000
12065         do_facet $SINGLEMDS sync
12066         # Make sure journal flushed.
12067         sleep 6
12068         local blk2=$(do_facet $SINGLEMDS \
12069                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12070                      grep Blockcount | awk '{print $4}')
12071
12072         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12073 }
12074 run_test 228b "idle OI blocks can be reused after MDT restart"
12075
12076 #LU-1881
12077 test_228c() {
12078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12079         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12080                 skip "non-ldiskfs backend" && return
12081
12082         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12083         local myDIR=$DIR/$tdir
12084
12085         mkdir -p $myDIR
12086         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12087         $LCTL set_param fail_loc=0x80001002
12088         # 20000 files can guarantee there are index nodes in the OI file
12089         createmany -o $myDIR/t- 20000
12090         $LCTL set_param fail_loc=0
12091         # The guard is current the largest FID holder
12092         touch $myDIR/guard
12093         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12094                     tr -d '[')
12095         local IDX=$(($SEQ % 64))
12096
12097         do_facet $SINGLEMDS sync
12098         # Make sure journal flushed.
12099         sleep 6
12100         local blk1=$(do_facet $SINGLEMDS \
12101                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12102                      grep Blockcount | awk '{print $4}')
12103
12104         # Remove old files, some OI blocks will become idle.
12105         unlinkmany $myDIR/t- 20000
12106         rm -f $myDIR/guard
12107         # The OI file should become empty now
12108
12109         # Create new files, idle OI blocks should be reused.
12110         createmany -o $myDIR/t- 2000
12111         do_facet $SINGLEMDS sync
12112         # Make sure journal flushed.
12113         sleep 6
12114         local blk2=$(do_facet $SINGLEMDS \
12115                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12116                      grep Blockcount | awk '{print $4}')
12117
12118         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12119 }
12120 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12121
12122 test_229() { # LU-2482, LU-3448
12123         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12124                 skip "No HSM support on MDS of $(get_lustre_version)," \
12125                          "need 2.4.53 at least" && return
12126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12127         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12128
12129         rm -f $DIR/$tfile
12130
12131         # Create a file with a released layout and stripe count 2.
12132         $MULTIOP $DIR/$tfile H2c ||
12133                 error "failed to create file with released layout"
12134
12135         $GETSTRIPE -v $DIR/$tfile
12136
12137         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12138         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12139
12140         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12141         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12142         stat $DIR/$tfile || error "failed to stat released file"
12143
12144         chown $RUNAS_ID $DIR/$tfile ||
12145                 error "chown $RUNAS_ID $DIR/$tfile failed"
12146
12147         chgrp $RUNAS_ID $DIR/$tfile ||
12148                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12149
12150         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12151         rm $DIR/$tfile || error "failed to remove released file"
12152 }
12153 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12154
12155 test_230a() {
12156         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12157         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12158         local MDTIDX=1
12159
12160         mkdir -p $DIR/$tdir/test_230_local
12161         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12162         [ $mdt_idx -ne 0 ] &&
12163                 error "create local directory on wrong MDT $mdt_idx"
12164
12165         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12166                         error "create remote directory failed"
12167         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12168         [ $mdt_idx -ne $MDTIDX ] &&
12169                 error "create remote directory on wrong MDT $mdt_idx"
12170
12171         createmany -o $DIR/$tdir/test_230/t- 10 ||
12172                 error "create files on remote directory failed"
12173         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12174         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12175         rm -r $DIR/$tdir || error "unlink remote directory failed"
12176 }
12177 run_test 230a "Create remote directory and files under the remote directory"
12178
12179 test_230b() {
12180         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12181         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12182         local MDTIDX=1
12183         local mdt_index
12184         local i
12185         local file
12186         local pid
12187         local stripe_count
12188         local migrate_dir=$DIR/$tdir/migrate_dir
12189         local other_dir=$DIR/$tdir/other_dir
12190
12191         mkdir -p $migrate_dir
12192         mkdir -p $other_dir
12193         for ((i=0; i<10; i++)); do
12194                 mkdir -p $migrate_dir/dir_${i}
12195                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12196                         error "create files under remote dir failed $i"
12197         done
12198
12199         cp /etc/passwd $migrate_dir/$tfile
12200         cp /etc/passwd $other_dir/$tfile
12201         chattr +SAD $migrate_dir
12202         chattr +SAD $migrate_dir/$tfile
12203
12204         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12205         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12206         local old_dir_mode=$(stat -c%f $migrate_dir)
12207         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12208
12209         mkdir -p $migrate_dir/dir_default_stripe2
12210         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12211         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12212
12213         mkdir -p $other_dir
12214         ln $migrate_dir/$tfile $other_dir/luna
12215         ln $migrate_dir/$tfile $migrate_dir/sofia
12216         ln $other_dir/$tfile $migrate_dir/david
12217         ln -s $migrate_dir/$tfile $other_dir/zachary
12218         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12219         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12220
12221         $LFS mv -v -M $MDTIDX $migrate_dir ||
12222                         error "migrate remote dir error"
12223
12224         echo "migratate to MDT1, then checking.."
12225         for ((i=0; i<10; i++)); do
12226                 for file in $(find $migrate_dir/dir_${i}); do
12227                         mdt_index=$($LFS getstripe -M $file)
12228                         [ $mdt_index == $MDTIDX ] ||
12229                                 error "$file is not on MDT${MDTIDX}"
12230                 done
12231         done
12232
12233         # the multiple link file should still in MDT0
12234         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12235         [ $mdt_index == 0 ] ||
12236                 error "$file is not on MDT${MDTIDX}"
12237
12238         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12239         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12240                 error " expect $old_dir_flag get $new_dir_flag"
12241
12242         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12243         [ "$old_file_flag" = "$new_file_flag" ] ||
12244                 error " expect $old_file_flag get $new_file_flag"
12245
12246         local new_dir_mode=$(stat -c%f $migrate_dir)
12247         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12248                 error "expect mode $old_dir_mode get $new_dir_mode"
12249
12250         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12251         [ "$old_file_mode" = "$new_file_mode" ] ||
12252                 error "expect mode $old_file_mode get $new_file_mode"
12253
12254         diff /etc/passwd $migrate_dir/$tfile ||
12255                 error "$tfile different after migration"
12256
12257         diff /etc/passwd $other_dir/luna ||
12258                 error "luna different after migration"
12259
12260         diff /etc/passwd $migrate_dir/sofia ||
12261                 error "sofia different after migration"
12262
12263         diff /etc/passwd $migrate_dir/david ||
12264                 error "david different after migration"
12265
12266         diff /etc/passwd $other_dir/zachary ||
12267                 error "zachary different after migration"
12268
12269         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12270                 error "${tfile}_ln different after migration"
12271
12272         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12273                 error "${tfile}_ln_other different after migration"
12274
12275         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12276         [ $stripe_count = 2 ] ||
12277                         error "dir strpe_count $d != 2 after migration."
12278
12279         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12280         [ $stripe_count = 2 ] ||
12281                         error "file strpe_count $d != 2 after migration."
12282
12283         #migrate back to MDT0
12284         MDTIDX=0
12285         $LFS mv -v -M $MDTIDX $migrate_dir ||
12286                         error "migrate remote dir error"
12287
12288         echo "migrate back to MDT0, checking.."
12289         for file in $(find $migrate_dir); do
12290                 mdt_index=$($LFS getstripe -M $file)
12291                 [ $mdt_index == $MDTIDX ] ||
12292                         error "$file is not on MDT${MDTIDX}"
12293         done
12294
12295         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12296         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12297                 error " expect $old_dir_flag get $new_dir_flag"
12298
12299         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12300         [ "$old_file_flag" = "$new_file_flag" ] ||
12301                 error " expect $old_file_flag get $new_file_flag"
12302
12303         local new_dir_mode=$(stat -c%f $migrate_dir)
12304         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12305                 error "expect mode $old_dir_mode get $new_dir_mode"
12306
12307         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12308         [ "$old_file_mode" = "$new_file_mode" ] ||
12309                 error "expect mode $old_file_mode get $new_file_mode"
12310
12311         diff /etc/passwd ${migrate_dir}/$tfile ||
12312                 error "$tfile different after migration"
12313
12314         diff /etc/passwd ${other_dir}/luna ||
12315                 error "luna different after migration"
12316
12317         diff /etc/passwd ${migrate_dir}/sofia ||
12318                 error "sofia different after migration"
12319
12320         diff /etc/passwd ${other_dir}/zachary ||
12321                 error "zachary different after migration"
12322
12323         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12324                 error "${tfile}_ln different after migration"
12325
12326         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12327                 error "${tfile}_ln_other different after migration"
12328
12329         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12330         [ $stripe_count = 2 ] ||
12331                 error "dir strpe_count $d != 2 after migration."
12332
12333         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12334         [ $stripe_count = 2 ] ||
12335                 error "file strpe_count $d != 2 after migration."
12336
12337         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12338 }
12339 run_test 230b "migrate directory"
12340
12341 test_230c() {
12342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12343         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12344         local MDTIDX=1
12345         local mdt_index
12346         local file
12347
12348         #If migrating directory fails in the middle, all entries of
12349         #the directory is still accessiable.
12350         mkdir -p $DIR/$tdir
12351         stat $DIR/$tdir
12352         createmany -o $DIR/$tdir/f 10 ||
12353                 error "create files under ${tdir} failed"
12354
12355         #failed after migrating 5 entries
12356         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12357         do_facet mds1 lctl set_param fail_loc=0x20001801
12358         do_facet mds1 lctl  set_param fail_val=5
12359         local t=`ls $DIR/$tdir | wc -l`
12360         $LFS mv -M $MDTIDX $DIR/$tdir &&
12361                 error "migrate should failed after 5 entries"
12362         local u=`ls $DIR/$tdir | wc -l`
12363         [ "$u" == "$t" ] || error "$u != $t during migration"
12364
12365         for file in $(find $DIR/$tdir); do
12366                 stat $file || error "stat $file failed"
12367         done
12368
12369         do_facet mds1 lctl set_param fail_loc=0
12370         do_facet mds1 lctl set_param fail_val=0
12371
12372         $LFS mv -M $MDTIDX $DIR/$tdir ||
12373                 error "migrate open files should failed with open files"
12374
12375         echo "Finish migration, then checking.."
12376         for file in $(find $DIR/$tdir); do
12377                 mdt_index=$($LFS getstripe -M $file)
12378                 [ $mdt_index == $MDTIDX ] ||
12379                         error "$file is not on MDT${MDTIDX}"
12380         done
12381
12382         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12383 }
12384 run_test 230c "check directory accessiblity if migration is failed"
12385
12386 test_230d() {
12387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12388         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12389         local MDTIDX=1
12390         local mdt_index
12391         local i
12392         local j
12393
12394         mkdir -p $DIR/$tdir
12395
12396         for ((i=0; i<100; i++)); do
12397                 mkdir -p $DIR/$tdir/dir_${i}
12398                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12399                         error "create files under remote dir failed $i"
12400         done
12401
12402         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12403
12404         echo "Finish migration, then checking.."
12405         for file in $(find $DIR/$tdir); do
12406                 mdt_index=$($LFS getstripe -M $file)
12407                 [ $mdt_index == $MDTIDX ] ||
12408                         error "$file is not on MDT${MDTIDX}"
12409         done
12410
12411         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12412 }
12413 run_test 230d "check migrate big directory"
12414
12415 test_231a()
12416 {
12417         # For simplicity this test assumes that max_pages_per_rpc
12418         # is the same across all OSCs
12419         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12420         local bulk_size=$((max_pages * 4096))
12421
12422         mkdir -p $DIR/$tdir
12423
12424         # clear the OSC stats
12425         $LCTL set_param osc.*.stats=0 &>/dev/null
12426
12427         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12428         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12429                 oflag=direct &>/dev/null || error "dd failed"
12430
12431         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12432         if [ x$nrpcs != "x1" ]; then
12433                 error "found $nrpc ost_write RPCs, not 1 as expected"
12434         fi
12435
12436         # Drop the OSC cache, otherwise we will read from it
12437         cancel_lru_locks osc
12438
12439         # clear the OSC stats
12440         $LCTL set_param osc.*.stats=0 &>/dev/null
12441
12442         # Client reads $bulk_size.
12443         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12444                 iflag=direct &>/dev/null || error "dd failed"
12445
12446         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12447         if [ x$nrpcs != "x1" ]; then
12448                 error "found $nrpc ost_read RPCs, not 1 as expected"
12449         fi
12450 }
12451 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12452
12453 test_231b() {
12454         mkdir -p $DIR/$tdir
12455         local i
12456         for i in {0..1023}; do
12457                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12458                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12459                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12460         done
12461         sync
12462 }
12463 run_test 231b "must not assert on fully utilized OST request buffer"
12464
12465 test_232() {
12466         mkdir -p $DIR/$tdir
12467         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12468         $LCTL set_param fail_loc=0x31c
12469
12470         # ignore dd failure
12471         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12472
12473         $LCTL set_param fail_loc=0
12474         umount_client $MOUNT || error "umount failed"
12475         mount_client $MOUNT || error "mount failed"
12476 }
12477 run_test 232 "failed lock should not block umount"
12478
12479 test_233a() {
12480         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12481         { skip "Need MDS version at least 2.3.64"; return; }
12482
12483         local fid=$($LFS path2fid $MOUNT)
12484         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12485                 error "cannot access $MOUNT using its FID '$fid'"
12486 }
12487 run_test 233a "checking that OBF of the FS root succeeds"
12488
12489 test_233b() {
12490         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12491         { skip "Need MDS version at least 2.5.90"; return; }
12492
12493         local fid=$($LFS path2fid $MOUNT/.lustre)
12494         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12495                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12496
12497         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12498         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12499                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12500 }
12501 run_test 233b "checking that OBF of the FS .lustre succeeds"
12502
12503 test_234() {
12504         local p="$TMP/sanityN-$TESTNAME.parameters"
12505         save_lustre_params client "llite.*.xattr_cache" > $p
12506         lctl set_param llite.*.xattr_cache 1 ||
12507                 { skip "xattr cache is not supported"; return 0; }
12508
12509         mkdir -p $DIR/$tdir || error "mkdir failed"
12510         touch $DIR/$tdir/$tfile || error "touch failed"
12511         # OBD_FAIL_LLITE_XATTR_ENOMEM
12512         $LCTL set_param fail_loc=0x1405
12513         if [ ! -f /etc/SuSE-release ]; then
12514                 # attr pre-2.4.44-7 had a bug with rc
12515                 # LU-3703 - SLES clients have older attr
12516                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12517                         error "getfattr should have failed with ENOMEM"
12518         fi
12519         $LCTL set_param fail_loc=0x0
12520         rm -rf $DIR/$tdir
12521
12522         restore_lustre_params < $p
12523         rm -f $p
12524 }
12525 run_test 234 "xattr cache should not crash on ENOMEM"
12526
12527 test_235() {
12528         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12529                 skip "Need MDS version at least 2.4.52" && return
12530         flock_deadlock $DIR/$tfile
12531         local RC=$?
12532         case $RC in
12533                 0)
12534                 ;;
12535                 124) error "process hangs on a deadlock"
12536                 ;;
12537                 *) error "error executing flock_deadlock $DIR/$tfile"
12538                 ;;
12539         esac
12540 }
12541 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12542
12543 #LU-2935
12544 test_236() {
12545         check_swap_layouts_support && return 0
12546         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12547
12548         local ref1=/etc/passwd
12549         local ref2=/etc/group
12550         local file1=$DIR/$tdir/f1
12551         local file2=$DIR/$tdir/f2
12552
12553         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12554         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12555         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12556         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12557         local fd=$(free_fd)
12558         local cmd="exec $fd<>$file2"
12559         eval $cmd
12560         rm $file2
12561         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12562                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12563         cmd="exec $fd>&-"
12564         eval $cmd
12565         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12566
12567         #cleanup
12568         rm -rf $DIR/$tdir
12569 }
12570 run_test 236 "Layout swap on open unlinked file"
12571
12572 # test to verify file handle related system calls
12573 # (name_to_handle_at/open_by_handle_at)
12574 # The new system calls are supported in glibc >= 2.14.
12575
12576 test_237() {
12577         echo "Test file_handle syscalls" > $DIR/$tfile
12578         check_fhandle_syscalls $DIR/$tfile ||
12579                 error "check_fhandle_syscalls failed"
12580 }
12581 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12582
12583 # LU-4659 linkea consistency
12584 test_238() {
12585         touch $DIR/$tfile
12586         ln $DIR/$tfile $DIR/$tfile.lnk
12587         touch $DIR/$tfile.new
12588         mv $DIR/$tfile.new $DIR/$tfile
12589         local fid1=$(lfs path2fid $DIR/$tfile)
12590         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12591         local path1=$(lfs fid2path $FSNAME $fid1)
12592         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12593         local path2=$(lfs fid2path $FSNAME $fid2)
12594         [ $tfile.lnk == $path2 ] ||
12595                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12596         rm -f $DIR/$tfile*
12597 }
12598 run_test 238 "Verify linkea consistency"
12599
12600 test_striped_dir() {
12601         local mdt_index=$1
12602         local stripe_count
12603         local stripe_index
12604
12605         mkdir -p $DIR/$tdir
12606         $LFS setdirstripe -i $mdt_index -c 2 -t all_char $DIR/$tdir/striped_dir ||
12607                 error "set striped dir error"
12608
12609         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12610         if [ "$stripe_count" != "2" ]; then
12611                 error "stripe_count is $stripe_count, expect 2"
12612         fi
12613
12614         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12615         if [ "$stripe_index" != "$mdt_index" ]; then
12616                 error "stripe_index is $stripe_index, expect $mdt_index"
12617         fi
12618
12619         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12620                 error "nlink error after create striped dir"
12621
12622         mkdir $DIR/$tdir/striped_dir/a
12623         mkdir $DIR/$tdir/striped_dir/b
12624
12625         stat $DIR/$tdir/striped_dir/a ||
12626                 error "create dir under striped dir failed"
12627         stat $DIR/$tdir/striped_dir/b ||
12628                 error "create dir under striped dir failed"
12629
12630         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12631                 error "nlink error after mkdir"
12632
12633         rmdir $DIR/$tdir/striped_dir/a
12634         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12635                 error "nlink error after rmdir"
12636
12637         rmdir $DIR/$tdir/striped_dir/b
12638         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12639                 error "nlink error after rmdir"
12640
12641         rmdir $DIR/$tdir/striped_dir ||
12642                 error "rmdir striped dir error"
12643         true
12644 }
12645
12646 test_300a() {
12647         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12648         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12649
12650         test_striped_dir 0 || error "failed on striped dir on MDT0"
12651         test_striped_dir 1 || error "failed on striped dir on MDT0"
12652 }
12653 run_test 300a "basic striped dir sanity test"
12654
12655 test_300b() {
12656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12657         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12658         local i
12659         local mtime1
12660         local mtime2
12661         local mtime3
12662
12663         test_mkdir $DIR/$tdir || error "mkdir fail"
12664         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12665                 error "set striped dir error"
12666         for ((i=0; i<10; i++)); do
12667                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12668                 sleep 1
12669                 touch $DIR/$tdir/striped_dir/file_$i ||
12670                                         error "touch error $i"
12671                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12672                 [ $mtime1 -eq $mtime2 ] &&
12673                         error "mtime not change after create"
12674                 sleep 1
12675                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12676                                         error "unlink error $i"
12677                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12678                 [ $mtime2 -eq $mtime3 ] &&
12679                         error "mtime did not change after unlink"
12680         done
12681         true
12682 }
12683 run_test 300b "check ctime/mtime for striped dir"
12684
12685 test_300c() {
12686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12687         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12688         local file_count
12689
12690         mkdir -p $DIR/$tdir
12691         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12692                 error "set striped dir error"
12693
12694         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12695                 error "chown striped dir failed"
12696
12697         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12698                 error "create 5k files failed"
12699
12700         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12701
12702         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12703
12704         rm -rf $DIR/$tdir
12705 }
12706 run_test 300c "chown && check ls under striped directory"
12707
12708 test_300d() {
12709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12710         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12711         local stripe_count
12712         local file
12713
12714         mkdir -p $DIR/$tdir
12715         $SETSTRIPE -c 2 $DIR/$tdir
12716
12717         #local striped directory
12718         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12719                 error "set striped dir error"
12720         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12721                 error "create 10 files failed"
12722
12723         #remote striped directory
12724         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12725                 error "set striped dir error"
12726         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12727                 error "create 10 files failed"
12728
12729         for file in $(find $DIR/$tdir); do
12730                 stripe_count=$($GETSTRIPE -c $file)
12731                 [ $stripe_count -eq 2 ] ||
12732                         error "wrong stripe $stripe_count for $file"
12733         done
12734
12735         rm -rf $DIR/$tdir
12736 }
12737 run_test 300d "check default stripe under striped directory"
12738
12739 test_300e() {
12740         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12741         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12742         local stripe_count
12743         local file
12744
12745         mkdir -p $DIR/$tdir
12746
12747         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12748                 error "set striped dir error"
12749
12750         touch $DIR/$tdir/striped_dir/a
12751         touch $DIR/$tdir/striped_dir/b
12752         touch $DIR/$tdir/striped_dir/c
12753
12754         mkdir $DIR/$tdir/striped_dir/dir_a
12755         mkdir $DIR/$tdir/striped_dir/dir_b
12756         mkdir $DIR/$tdir/striped_dir/dir_c
12757
12758         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12759                 error "set striped dir under striped dir error"
12760
12761         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12762                 error "set striped dir under striped dir error"
12763
12764         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12765                 error "set striped dir under striped dir error"
12766
12767         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12768                 error "rename file under striped dir should fail"
12769
12770         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12771                 error "rename dir under striped dir should fail"
12772
12773         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12774                 error "rename dir under different stripes should fail"
12775
12776         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12777                 error "rename file under striped dir should succeed"
12778
12779         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12780                 error "rename dir under striped dir should succeed"
12781
12782         rm -rf $DIR/$tdir
12783 }
12784 run_test 300e "check rename under striped directory"
12785
12786 test_300f() {
12787         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12788         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12789         local stripe_count
12790         local file
12791
12792         rm -rf $DIR/$tdir
12793         mkdir -p $DIR/$tdir
12794
12795         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12796                 error "set striped dir error"
12797
12798         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12799                 error "set striped dir error"
12800
12801         touch $DIR/$tdir/striped_dir/a
12802         mkdir $DIR/$tdir/striped_dir/dir_a
12803         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12804                 error "create striped dir under striped dir fails"
12805
12806         touch $DIR/$tdir/striped_dir1/b
12807         mkdir $DIR/$tdir/striped_dir1/dir_b
12808         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12809                 error "create striped dir under striped dir fails"
12810
12811         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12812                 error "rename file under different striped dir should fail"
12813
12814         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12815                 error "rename dir under different striped dir should fail"
12816
12817         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12818                 error "rename striped dir under diff striped dir should fail"
12819
12820         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12821                 error "rename file under diff striped dirs fails"
12822
12823         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12824                 error "rename dir under diff striped dirs fails"
12825
12826         rm -rf $DIR/$tdir
12827 }
12828 run_test 300f "check rename cross striped directory"
12829
12830 test_300g() {
12831         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12832         local stripe_count
12833         local dir
12834
12835         mkdir $DIR/$tdir
12836         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
12837                                         $DIR/$tdir/striped_dir ||
12838                 error "set striped dir error"
12839
12840         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12841                 error "set default stripe on striped dir error"
12842
12843         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
12844         [ $stripe_count -eq $MDSCOUNT ] ||
12845                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
12846
12847         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12848
12849         for dir in $(find $DIR/$tdir/striped_dir/*); do
12850                 stripe_count=$($LFS getdirstripe -c $dir)
12851                 [ $stripe_count -eq $MDSCOUNT ] ||
12852                         error "expect $MDSCOUNT get $stripe_count for $dir"
12853         done
12854
12855         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
12856         #change default stripe count to 2
12857         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
12858                 error "set default stripe on striped dir error"
12859
12860         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12861
12862         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
12863
12864         #change default stripe count to 1
12865         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
12866                 error "set default stripe on striped dir error"
12867
12868         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12869         for dir in $(find $DIR/$tdir/striped_dir/*); do
12870                 stripe_count=$($LFS getdirstripe -c $dir)
12871                 [ $stripe_count -eq 1 ] ||
12872                         error "expect 1 get $stripe_count for $dir"
12873         done
12874         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
12875 }
12876 run_test 300g "check default striped directory for striped directory"
12877
12878 test_400a() { # LU-1606, was conf-sanity test_74
12879         local extra_flags=''
12880         local out=$TMP/$tfile
12881         local prefix=/usr/include/lustre
12882         local prog
12883
12884         if ! which $CC > /dev/null 2>&1; then
12885                 skip_env "$CC is not installed"
12886                 return 0
12887         fi
12888
12889         if ! [[ -d $prefix ]]; then
12890                 # Assume we're running in tree and fixup the include path.
12891                 extra_flags+=" -I$LUSTRE/../libcfs/include"
12892                 extra_flags+=" -I$LUSTRE/include"
12893                 extra_flags+=" -L$LUSTRE/utils"
12894         fi
12895
12896         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
12897                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
12898                         error "client api broken"
12899         done
12900 }
12901 run_test 400a "Lustre client api program can compile and link"
12902
12903 test_400b() { # LU-1606, LU-5011
12904         local header
12905         local out=$TMP/$tfile
12906         local prefix=/usr/include/lustre
12907
12908         # We use a hard coded prefix so that this test will not fail
12909         # when run in tree. There are headers in lustre/include/lustre/
12910         # that are not packaged (like lustre_idl.h) and have more
12911         # complicated include dependencies (like config.h and lnet/types.h).
12912         # Since this test about correct packaging we just skip them when
12913         # they don't exist (see below) rather than try to fixup cppflags.
12914
12915         if ! which $CC > /dev/null 2>&1; then
12916                 skip_env "$CC is not installed"
12917                 return 0
12918         fi
12919
12920         for header in $prefix/*.h; do
12921                 if ! [[ -f "$header" ]]; then
12922                         continue
12923                 fi
12924
12925                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
12926                         continue # liblustreapi.h is deprecated.
12927                 fi
12928
12929                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
12930                         error "cannot compile '$header'"
12931         done
12932 }
12933 run_test 400b "packaged headers can be compiled"
12934
12935 #
12936 # tests that do cleanup/setup should be run at the end
12937 #
12938
12939 test_900() {
12940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12941         local ls
12942         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
12943         $LCTL set_param fail_loc=0x903
12944         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
12945         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
12946                 echo "clear" > $ls
12947         done
12948         FAIL_ON_ERROR=true cleanup
12949         FAIL_ON_ERROR=true setup
12950 }
12951 run_test 900 "umount should not race with any mgc requeue thread"
12952
12953 complete $SECONDS
12954 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
12955 check_and_cleanup_lustre
12956 if [ "$I_MOUNTED" != "yes" ]; then
12957         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
12958 fi
12959 exit_status