Whamcloud - gitweb
LU-5472 tests: export UMOUNT to avoid SLES12 issue
[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-4341
20 {
21         if [ -r /etc/SuSE-release ]
22         then
23                 local vers=$(grep VERSION /etc/SuSE-release | awk '{print $3}')
24                 local patchlev=$(grep PATCHLEVEL /etc/SuSE-release |
25                         awk '{ print $3 }')
26                 if [ $vers -eq 11 ] && [ $patchlev -ge 3 ]; then
27                         return 0
28                 fi
29         fi
30         return 1
31 }
32
33 if is_sles11; then                                      # LU-4341
34         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 170"
35 fi
36
37 SRCDIR=$(cd $(dirname $0); echo $PWD)
38 export PATH=$PATH:/sbin
39
40 TMP=${TMP:-/tmp}
41
42 CC=${CC:-cc}
43 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
44 CREATETEST=${CREATETEST:-createtest}
45 LFS=${LFS:-lfs}
46 LFIND=${LFIND:-"$LFS find"}
47 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
48 LCTL=${LCTL:-lctl}
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 STRIPES_PER_OBJ=-1
60 CHECK_GRANT=${CHECK_GRANT:-"yes"}
61 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
62 export PARALLEL=${PARALLEL:-"no"}
63
64 export NAME=${NAME:-local}
65
66 SAVE_PWD=$PWD
67
68 CLEANUP=${CLEANUP:-:}
69 SETUP=${SETUP:-:}
70 TRACE=${TRACE:-""}
71 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
72 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
73 . $LUSTRE/tests/test-framework.sh
74 init_test_env $@
75 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
76 init_logging
77
78 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24D 27m 64b 68 71 77f 78 115 124b 300o"
79
80 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
81         # bug number for skipped test: LU-4536 LU-1957 LU-2805
82         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  65ic    180     184c"
83         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b 51ba"
84 fi
85
86 FAIL_ON_ERROR=false
87
88 cleanup() {
89         echo -n "cln.."
90         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
91         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
92 }
93 setup() {
94         echo -n "mnt.."
95         load_modules
96         setupall || exit 10
97         echo "done"
98 }
99
100 check_swap_layouts_support()
101 {
102         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
103                 { skip "Does not support layout lock."; return 0; }
104         return 1
105 }
106
107 check_and_setup_lustre
108
109 DIR=${DIR:-$MOUNT}
110 assert_DIR
111
112 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
113         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
114 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
115 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
116 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
117 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
118 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
119 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
120
121 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
122 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
123 rm -rf $DIR/[Rdfs][0-9]*
124
125 # $RUNAS_ID may get set incorrectly somewhere else
126 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
127
128 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
129
130 build_test_filter
131
132 if [ "${ONLY}" = "MOUNT" ] ; then
133         echo "Lustre is up, please go on"
134         exit
135 fi
136
137 echo "preparing for tests involving mounts"
138 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
139 touch $EXT2_DEV
140 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
141 echo # add a newline after mke2fs.
142
143 umask 077
144
145 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
146 lctl set_param debug=-1 2> /dev/null || true
147 test_0a() {
148         touch $DIR/$tfile
149         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
150         rm $DIR/$tfile
151         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
152 }
153 run_test 0a "touch; rm ====================="
154
155 test_0b() {
156         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
157         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
158 }
159 run_test 0b "chmod 0755 $DIR ============================="
160
161 test_0c() {
162         $LCTL get_param mdc.*.import | grep "state: FULL" ||
163                 error "import not FULL"
164         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
165                 error "bad target"
166 }
167 run_test 0c "check import proc ============================="
168
169 test_1() {
170         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
171         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
172         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
173         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
174         rmdir $DIR/$tdir/d2
175         rmdir $DIR/$tdir
176         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
177 }
178 run_test 1 "mkdir; remkdir; rmdir =============================="
179
180 test_2() {
181         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
182         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
183         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
184         rm -r $DIR/$tdir
185         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
186 }
187 run_test 2 "mkdir; touch; rmdir; check file ===================="
188
189 test_3() {
190         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
191         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
192         touch $DIR/$tdir/$tfile
193         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
194         rm -r $DIR/$tdir
195         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
196 }
197 run_test 3 "mkdir; touch; rmdir; check dir ====================="
198
199 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
200 test_4() {
201         local MDTIDX=1
202
203         test_mkdir $DIR/$tdir ||
204                 error "Create remote directory failed"
205
206         touch $DIR/$tdir/$tfile ||
207                 error "Create file under remote directory failed"
208
209         rmdir $DIR/$tdir &&
210                 error "Expect error removing in-use dir $DIR/$tdir"
211
212         test -d $DIR/$tdir || error "Remote directory disappeared"
213
214         rm -rf $DIR/$tdir || error "remove remote dir error"
215 }
216 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
217
218 test_5() {
219         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
220         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
221         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
222         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
223         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
224 }
225 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
226
227 test_6a() {
228         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
229         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
230         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
231                 error "$tfile does not have perm 0666 or UID $UID"
232         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
233         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
234                 error "$tfile should be 0666 and owned by UID $UID"
235 }
236 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
237
238 test_6c() {
239         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
240         touch $DIR/$tfile
241         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
242         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
243                 error "$tfile should be owned by UID $RUNAS_ID"
244         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
245         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
246                 error "$tfile should be owned by UID $RUNAS_ID"
247 }
248 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
249
250 test_6e() {
251         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
252         touch $DIR/$tfile
253         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
254         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
255                 error "$tfile should be owned by GID $UID"
256         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
257         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
258                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
259 }
260 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
261
262 test_6g() {
263         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
264         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
265         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
266         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
267         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
268         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
269         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
270                 error "$tdir/d/subdir should be GID $RUNAS_GID"
271 }
272 run_test 6g "Is new dir in sgid dir inheriting group?"
273
274 test_6h() { # bug 7331
275         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
276         touch $DIR/$tfile || error "touch failed"
277         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
278         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
279                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
280         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
281                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
282 }
283 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
284
285 test_7a() {
286         test_mkdir $DIR/$tdir
287         $MCREATE $DIR/$tdir/$tfile
288         chmod 0666 $DIR/$tdir/$tfile
289         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
290                 error "$tdir/$tfile should be mode 0666"
291 }
292 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
293
294 test_7b() {
295         if [ ! -d $DIR/$tdir ]; then
296                 test_mkdir $DIR/$tdir
297         fi
298         $MCREATE $DIR/$tdir/$tfile
299         echo -n foo > $DIR/$tdir/$tfile
300         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
301         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
302 }
303 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
304
305 test_8() {
306         test_mkdir $DIR/$tdir
307         touch $DIR/$tdir/$tfile
308         chmod 0666 $DIR/$tdir/$tfile
309         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
310                 error "$tfile mode not 0666"
311 }
312 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
313
314 test_9() {
315         test_mkdir $DIR/$tdir
316         test_mkdir $DIR/$tdir/d2
317         test_mkdir $DIR/$tdir/d2/d3
318         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
319 }
320 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
321
322 test_10() {
323         test_mkdir $DIR/$tdir
324         test_mkdir $DIR/$tdir/d2
325         touch $DIR/$tdir/d2/$tfile
326         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
327                 error "$tdir/d2/$tfile not a file"
328 }
329 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
330
331 test_11() {
332         test_mkdir $DIR/$tdir
333         test_mkdir $DIR/$tdir/d2
334         chmod 0666 $DIR/$tdir/d2
335         chmod 0705 $DIR/$tdir/d2
336         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
337                 error "$tdir/d2 mode not 0705"
338 }
339 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
340
341 test_12() {
342         test_mkdir $DIR/$tdir
343         touch $DIR/$tdir/$tfile
344         chmod 0666 $DIR/$tdir/$tfile
345         chmod 0654 $DIR/$tdir/$tfile
346         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
347                 error "$tdir/d2 mode not 0654"
348 }
349 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
350
351 test_13() {
352         test_mkdir $DIR/$tdir
353         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
354         >  $DIR/$tdir/$tfile
355         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
356                 error "$tdir/$tfile size not 0 after truncate"
357 }
358 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
359
360 test_14() {
361         test_mkdir $DIR/$tdir
362         touch $DIR/$tdir/$tfile
363         rm $DIR/$tdir/$tfile
364         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
365 }
366 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
367
368 test_15() {
369         test_mkdir $DIR/$tdir
370         touch $DIR/$tdir/$tfile
371         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
372         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
373                 error "$tdir/${tfile_2} not a file after rename"
374         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
375 }
376 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
377
378 test_16() {
379         test_mkdir $DIR/$tdir
380         touch $DIR/$tdir/$tfile
381         rm -rf $DIR/$tdir/$tfile
382         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
383 }
384 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
385
386 test_17a() {
387         test_mkdir -p $DIR/$tdir
388         touch $DIR/$tdir/$tfile
389         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
390         ls -l $DIR/$tdir
391         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
392                 error "$tdir/l-exist not a symlink"
393         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
394                 error "$tdir/l-exist not referencing a file"
395         rm -f $DIR/$tdir/l-exist
396         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
397 }
398 run_test 17a "symlinks: create, remove (real) =================="
399
400 test_17b() {
401         test_mkdir -p $DIR/$tdir
402         ln -s no-such-file $DIR/$tdir/l-dangle
403         ls -l $DIR/$tdir
404         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
405                 error "$tdir/l-dangle not referencing no-such-file"
406         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
407                 error "$tdir/l-dangle not referencing non-existent file"
408         rm -f $DIR/$tdir/l-dangle
409         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
410 }
411 run_test 17b "symlinks: create, remove (dangling) =============="
412
413 test_17c() { # bug 3440 - don't save failed open RPC for replay
414         test_mkdir -p $DIR/$tdir
415         ln -s foo $DIR/$tdir/$tfile
416         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
417 }
418 run_test 17c "symlinks: open dangling (should return error) ===="
419
420 test_17d() {
421         test_mkdir -p $DIR/$tdir
422         ln -s foo $DIR/$tdir/$tfile
423         touch $DIR/$tdir/$tfile || error "creating to new symlink"
424 }
425 run_test 17d "symlinks: create dangling ========================"
426
427 test_17e() {
428         test_mkdir -p $DIR/$tdir
429         local foo=$DIR/$tdir/$tfile
430         ln -s $foo $foo || error "create symlink failed"
431         ls -l $foo || error "ls -l failed"
432         ls $foo && error "ls not failed" || true
433 }
434 run_test 17e "symlinks: create recursive symlink (should return error) ===="
435
436 test_17f() {
437         test_mkdir -p $DIR/$tdir
438         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
439         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
440         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
441         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
442         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
443         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
444         ls -l  $DIR/$tdir
445 }
446 run_test 17f "symlinks: long and very long symlink name ========================"
447
448 # str_repeat(S, N) generate a string that is string S repeated N times
449 str_repeat() {
450         local s=$1
451         local n=$2
452         local ret=''
453         while [ $((n -= 1)) -ge 0 ]; do
454                 ret=$ret$s
455         done
456         echo $ret
457 }
458
459 # Long symlinks and LU-2241
460 test_17g() {
461         test_mkdir -p $DIR/$tdir
462         local TESTS="59 60 61 4094 4095"
463
464         # Fix for inode size boundary in 2.1.4
465         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
466                 TESTS="4094 4095"
467
468         # Patch not applied to 2.2 or 2.3 branches
469         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
470         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
471                 TESTS="4094 4095"
472
473         # skip long symlink name for rhel6.5.
474         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
475         grep -q '6.5' /etc/redhat-release &>/dev/null &&
476                 TESTS="59 60 61 4062 4063"
477
478         for i in $TESTS; do
479                 local SYMNAME=$(str_repeat 'x' $i)
480                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
481                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
482         done
483 }
484 run_test 17g "symlinks: really long symlink name and inode boundaries"
485
486 test_17h() { #bug 17378
487         remote_mds_nodsh && skip "remote MDS with nodsh" && return
488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
489         local mdt_idx
490         test_mkdir -p $DIR/$tdir
491         if [[ $MDSCOUNT -gt 1 ]]; then
492                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
493         else
494                 mdt_idx=0
495         fi
496         $SETSTRIPE -c -1 $DIR/$tdir
497 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
498         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
499         touch $DIR/$tdir/$tfile || true
500 }
501 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
502
503 test_17i() { #bug 20018
504         remote_mds_nodsh && skip "remote MDS with nodsh" && return
505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
506         test_mkdir -c1 $DIR/$tdir
507         local foo=$DIR/$tdir/$tfile
508         local mdt_idx
509         if [[ $MDSCOUNT -gt 1 ]]; then
510                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
511         else
512                 mdt_idx=0
513         fi
514         ln -s $foo $foo || error "create symlink failed"
515 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
516         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
517         ls -l $foo && error "error not detected"
518         return 0
519 }
520 run_test 17i "don't panic on short symlink"
521
522 test_17k() { #bug 22301
523         [[ -z "$(which rsync 2>/dev/null)" ]] &&
524                 skip "no rsync command" && return 0
525         rsync --help | grep -q xattr ||
526                 skip_env "$(rsync --version | head -n1) does not support xattrs"
527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
528         test_mkdir -p $DIR/$tdir
529         test_mkdir -p $DIR/$tdir.new
530         touch $DIR/$tdir/$tfile
531         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
532         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
533                 error "rsync failed with xattrs enabled"
534 }
535 run_test 17k "symlinks: rsync with xattrs enabled ========================="
536
537 test_17l() { # LU-279
538         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
539                 skip "no getfattr command" && return 0
540         test_mkdir -p $DIR/$tdir
541         touch $DIR/$tdir/$tfile
542         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
543         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
544                 # -h to not follow symlinks. -m '' to list all the xattrs.
545                 # grep to remove first line: '# file: $path'.
546                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
547                 do
548                         lgetxattr_size_check $path $xattr ||
549                                 error "lgetxattr_size_check $path $xattr failed"
550                 done
551         done
552 }
553 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
554
555 # LU-1540
556 test_17m() {
557         local short_sym="0123456789"
558         local WDIR=$DIR/${tdir}m
559         local mds_index
560         local devname
561         local cmd
562         local i
563         local rc=0
564
565         remote_mds_nodsh && skip "remote MDS with nodsh" && return
566         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
567         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
568                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
569
570         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
571                 skip "only for ldiskfs MDT" && return 0
572
573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
574
575         test_mkdir -p $WDIR
576         long_sym=$short_sym
577         # create a long symlink file
578         for ((i = 0; i < 4; ++i)); do
579                 long_sym=${long_sym}${long_sym}
580         done
581
582         echo "create 512 short and long symlink files under $WDIR"
583         for ((i = 0; i < 256; ++i)); do
584                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
585                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
586         done
587
588         echo "erase them"
589         rm -f $WDIR/*
590         sync
591         wait_delete_completed
592
593         echo "recreate the 512 symlink files with a shorter string"
594         for ((i = 0; i < 512; ++i)); do
595                 # rewrite the symlink file with a shorter string
596                 ln -sf ${long_sym} $WDIR/long-$i
597                 ln -sf ${short_sym} $WDIR/short-$i
598         done
599
600         mds_index=$($LFS getstripe -M $WDIR)
601         mds_index=$((mds_index+1))
602         devname=$(mdsdevname $mds_index)
603         cmd="$E2FSCK -fnvd $devname"
604
605         echo "stop and checking mds${mds_index}: $cmd"
606         # e2fsck should not return error
607         stop mds${mds_index}
608         do_facet mds${mds_index} $cmd || rc=$?
609
610         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
611         df $MOUNT > /dev/null 2>&1
612         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
613                 "short/long symlink MDT: rc=$rc"
614         return $rc
615 }
616 run_test 17m "run e2fsck against MDT which contains short/long symlink"
617
618 check_fs_consistency_17n() {
619         local mdt_index
620         local devname
621         local cmd
622         local rc=0
623
624         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
625         # so it only check MDT1/MDT2 instead of all of MDTs.
626         for mdt_index in $(seq 1 2); do
627                 devname=$(mdsdevname $mdt_index)
628                 cmd="$E2FSCK -fnvd $devname"
629
630                 echo "stop and checking mds${mdt_index}: $cmd"
631                 # e2fsck should not return error
632                 stop mds${mdt_index}
633                 do_facet mds${mdt_index} $cmd || rc=$?
634
635                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
636                         error "mount mds${mdt_index} failed"
637                 df $MOUNT > /dev/null 2>&1
638                 [ $rc -ne 0 ] && break
639         done
640         return $rc
641 }
642
643 test_17n() {
644         local i
645
646         remote_mds_nodsh && skip "remote MDS with nodsh" && return
647         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
648         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
649                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
650
651         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
652                 skip "only for ldiskfs MDT" && return 0
653
654         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
655
656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
657
658         test_mkdir $DIR/$tdir
659         for ((i=0; i<10; i++)); do
660                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
661                         error "create remote dir error $i"
662                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
663                         error "create files under remote dir failed $i"
664         done
665
666         check_fs_consistency_17n ||
667                 error "e2fsck report error after create files under remote dir"
668
669         for ((i = 0; i < 10; i++)); do
670                 rm -rf $DIR/$tdir/remote_dir_${i} ||
671                         error "destroy remote dir error $i"
672         done
673
674         check_fs_consistency_17n ||
675                 error "e2fsck report error after unlink files under remote dir"
676
677         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
678                 skip "lustre < 2.4.50 does not support migrate mv " && return
679
680         for ((i = 0; i < 10; i++)); do
681                 mkdir -p $DIR/$tdir/remote_dir_${i}
682                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
683                         error "create files under remote dir failed $i"
684                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
685                         error "migrate remote dir error $i"
686         done
687         check_fs_consistency_17n || error "e2fsck report error after migration"
688
689         for ((i = 0; i < 10; i++)); do
690                 rm -rf $DIR/$tdir/remote_dir_${i} ||
691                         error "destroy remote dir error $i"
692         done
693
694         check_fs_consistency_17n || error "e2fsck report error after unlink"
695 }
696 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
697
698 test_17o() {
699         remote_mds_nodsh && skip "remote MDS with nodsh" && return
700         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
701                 skip "Need MDS version at least 2.3.64" && return
702
703         local WDIR=$DIR/${tdir}o
704         local mdt_index
705         local rc=0
706
707         test_mkdir -p $WDIR
708         mdt_index=$($LFS getstripe -M $WDIR)
709         mdt_index=$((mdt_index+1))
710
711         touch $WDIR/$tfile
712
713         #fail mds will wait the failover finish then set
714         #following fail_loc to avoid interfer the recovery process.
715         fail mds${mdt_index}
716
717         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
718         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
719         ls -l $WDIR/$tfile && rc=1
720         do_facet mds${mdt_index} lctl set_param fail_loc=0
721         [[ $rc -ne 0 ]] && error "stat file should fail"
722         true
723 }
724 run_test 17o "stat file with incompat LMA feature"
725
726 test_18() {
727         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
728         ls $DIR || error "Failed to ls $DIR: $?"
729 }
730 run_test 18 "touch .../f ; ls ... =============================="
731
732 test_19a() {
733         touch $DIR/$tfile
734         ls -l $DIR
735         rm $DIR/$tfile
736         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
737 }
738 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
739
740 test_19b() {
741         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
742 }
743 run_test 19b "ls -l .../f19 (should return error) =============="
744
745 test_19c() {
746         [ $RUNAS_ID -eq $UID ] &&
747                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
748         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
749 }
750 run_test 19c "$RUNAS touch .../f19 (should return error) =="
751
752 test_19d() {
753         cat $DIR/f19 && error || true
754 }
755 run_test 19d "cat .../f19 (should return error) =============="
756
757 test_20() {
758         touch $DIR/$tfile
759         rm $DIR/$tfile
760         log "1 done"
761         touch $DIR/$tfile
762         rm $DIR/$tfile
763         log "2 done"
764         touch $DIR/$tfile
765         rm $DIR/$tfile
766         log "3 done"
767         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
768 }
769 run_test 20 "touch .../f ; ls -l ... ==========================="
770
771 test_21() {
772         test_mkdir -p $DIR/$tdir
773         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
774         ln -s dangle $DIR/$tdir/link
775         echo foo >> $DIR/$tdir/link
776         cat $DIR/$tdir/dangle
777         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
778         $CHECKSTAT -f -t file $DIR/$tdir/link ||
779                 error "$tdir/link not linked to a file"
780 }
781 run_test 21 "write to dangling link ============================"
782
783 test_22() {
784         WDIR=$DIR/$tdir
785         test_mkdir -p $DIR/$tdir
786         chown $RUNAS_ID:$RUNAS_GID $WDIR
787         (cd $WDIR || error "cd $WDIR failed";
788         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
789         $RUNAS tar xf -)
790         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
791         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
792         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
793 }
794 run_test 22 "unpack tar archive as non-root user ==============="
795
796 # was test_23
797 test_23a() {
798         test_mkdir -p $DIR/$tdir
799         local file=$DIR/$tdir/$tfile
800
801         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
802         openfile -f O_CREAT:O_EXCL $file &&
803                 error "$file recreate succeeded" || true
804 }
805 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
806
807 test_23b() { # bug 18988
808         test_mkdir -p $DIR/$tdir
809         local file=$DIR/$tdir/$tfile
810
811         rm -f $file
812         echo foo > $file || error "write filed"
813         echo bar >> $file || error "append filed"
814         $CHECKSTAT -s 8 $file || error "wrong size"
815         rm $file
816 }
817 run_test 23b "O_APPEND check =========================="
818
819 # rename sanity
820 test_24a() {
821         echo '-- same directory rename'
822         test_mkdir $DIR/$tdir
823         touch $DIR/$tdir/$tfile.1
824         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
825         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
826 }
827 run_test 24a "rename file to non-existent target"
828
829 test_24b() {
830         test_mkdir $DIR/$tdir
831         touch $DIR/$tdir/$tfile.{1,2}
832         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
833         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
834         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
835 }
836 run_test 24b "rename file to existing target"
837
838 test_24c() {
839         test_mkdir $DIR/$tdir
840         test_mkdir $DIR/$tdir/d$testnum.1
841         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
842         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
843         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
844 }
845 run_test 24c "rename directory to non-existent target"
846
847 test_24d() {
848         test_mkdir -c1 $DIR/$tdir
849         test_mkdir -c1 $DIR/$tdir/d$testnum.1
850         test_mkdir -c1 $DIR/$tdir/d$testnum.2
851         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
852         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
853         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
854 }
855 run_test 24d "rename directory to existing target"
856
857 test_24e() {
858         echo '-- cross directory renames --'
859         test_mkdir $DIR/R5a
860         test_mkdir $DIR/R5b
861         touch $DIR/R5a/f
862         mv $DIR/R5a/f $DIR/R5b/g
863         $CHECKSTAT -a $DIR/R5a/f || error
864         $CHECKSTAT -t file $DIR/R5b/g || error
865 }
866 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
867
868 test_24f() {
869         test_mkdir $DIR/R6a
870         test_mkdir $DIR/R6b
871         touch $DIR/R6a/f $DIR/R6b/g
872         mv $DIR/R6a/f $DIR/R6b/g
873         $CHECKSTAT -a $DIR/R6a/f || error
874         $CHECKSTAT -t file $DIR/R6b/g || error
875 }
876 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
877
878 test_24g() {
879         test_mkdir $DIR/R7a
880         test_mkdir $DIR/R7b
881         test_mkdir $DIR/R7a/d
882         mv $DIR/R7a/d $DIR/R7b/e
883         $CHECKSTAT -a $DIR/R7a/d || error
884         $CHECKSTAT -t dir $DIR/R7b/e || error
885 }
886 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
887
888 test_24h() {
889         test_mkdir -c1 $DIR/R8a
890         test_mkdir -c1 $DIR/R8b
891         test_mkdir -c1 $DIR/R8a/d
892         test_mkdir -c1 $DIR/R8b/e
893         mrename $DIR/R8a/d $DIR/R8b/e
894         $CHECKSTAT -a $DIR/R8a/d || error
895         $CHECKSTAT -t dir $DIR/R8b/e || error
896 }
897 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
898
899 test_24i() {
900         echo "-- rename error cases"
901         test_mkdir $DIR/R9
902         test_mkdir $DIR/R9/a
903         touch $DIR/R9/f
904         mrename $DIR/R9/f $DIR/R9/a
905         $CHECKSTAT -t file $DIR/R9/f || error
906         $CHECKSTAT -t dir  $DIR/R9/a || error
907         $CHECKSTAT -a $DIR/R9/a/f || error
908 }
909 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
910
911 test_24j() {
912         test_mkdir $DIR/R10
913         mrename $DIR/R10/f $DIR/R10/g
914         $CHECKSTAT -t dir $DIR/R10 || error
915         $CHECKSTAT -a $DIR/R10/f || error
916         $CHECKSTAT -a $DIR/R10/g || error
917 }
918 run_test 24j "source does not exist ============================"
919
920 test_24k() {
921         test_mkdir $DIR/R11a
922         test_mkdir $DIR/R11a/d
923         touch $DIR/R11a/f
924         mv $DIR/R11a/f $DIR/R11a/d
925         $CHECKSTAT -a $DIR/R11a/f || error
926         $CHECKSTAT -t file $DIR/R11a/d/f || error
927 }
928 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
929
930 # bug 2429 - rename foo foo foo creates invalid file
931 test_24l() {
932         f="$DIR/f24l"
933         $MULTIOP $f OcNs || error
934 }
935 run_test 24l "Renaming a file to itself ========================"
936
937 test_24m() {
938         f="$DIR/f24m"
939         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
940         # on ext3 this does not remove either the source or target files
941         # though the "expected" operation would be to remove the source
942         $CHECKSTAT -t file ${f} || error "${f} missing"
943         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
944 }
945 run_test 24m "Renaming a file to a hard link to itself ========="
946
947 test_24n() {
948     f="$DIR/f24n"
949     # this stats the old file after it was renamed, so it should fail
950     touch ${f}
951     $CHECKSTAT ${f}
952     mv ${f} ${f}.rename
953     $CHECKSTAT ${f}.rename
954     $CHECKSTAT -a ${f}
955 }
956 run_test 24n "Statting the old file after renaming (Posix rename 2)"
957
958 test_24o() {
959         test_mkdir -p $DIR/d24o
960         rename_many -s random -v -n 10 $DIR/d24o
961 }
962 run_test 24o "rename of files during htree split ==============="
963
964 test_24p() {
965         test_mkdir $DIR/R12a
966         test_mkdir $DIR/R12b
967         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
968         mrename $DIR/R12a $DIR/R12b
969         $CHECKSTAT -a $DIR/R12a || error
970         $CHECKSTAT -t dir $DIR/R12b || error
971         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
972         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
973 }
974 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
975
976 cleanup_multiop_pause() {
977         trap 0
978         kill -USR1 $MULTIPID
979 }
980
981 test_24q() {
982         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
983         test_mkdir $DIR/R13a
984         test_mkdir $DIR/R13b
985         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
986         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
987         MULTIPID=$!
988
989         trap cleanup_multiop_pause EXIT
990         mrename $DIR/R13a $DIR/R13b
991         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
992         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
993         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
994         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
995         cleanup_multiop_pause
996         wait $MULTIPID || error "multiop close failed"
997 }
998 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
999
1000 test_24r() { #bug 3789
1001         test_mkdir $DIR/R14a
1002         test_mkdir $DIR/R14a/b
1003         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1004         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1005         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1006 }
1007 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1008
1009 test_24s() {
1010         test_mkdir $DIR/R15a
1011         test_mkdir $DIR/R15a/b
1012         test_mkdir $DIR/R15a/b/c
1013         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1014         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1015         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1016 }
1017 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1018 test_24t() {
1019         test_mkdir $DIR/R16a
1020         test_mkdir $DIR/R16a/b
1021         test_mkdir $DIR/R16a/b/c
1022         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1023         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1024         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1025 }
1026 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1027
1028 test_24u() { # bug12192
1029         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1030         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1031 }
1032 run_test 24u "create stripe file"
1033
1034 page_size() {
1035         getconf PAGE_SIZE
1036 }
1037
1038 simple_cleanup_common() {
1039         trap 0
1040         rm -rf $DIR/$tdir
1041         wait_delete_completed
1042 }
1043
1044 max_pages_per_rpc() {
1045         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1046 }
1047
1048 test_24v() {
1049         local NRFILES=100000
1050         local FREE_INODES=$(mdt_free_inodes 0)
1051         [[ $FREE_INODES -lt $NRFILES ]] &&
1052                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1053                 return
1054
1055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1056         trap simple_cleanup_common EXIT
1057
1058         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1059         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1060
1061         test_mkdir -p $DIR/$tdir
1062         createmany -m $DIR/$tdir/$tfile $NRFILES
1063
1064         cancel_lru_locks mdc
1065         lctl set_param mdc.*.stats clear
1066
1067         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1068
1069         # LU-5 large readdir
1070         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1071         #               8 bytes for name(filename is mostly 5 in this test) +
1072         #               8 bytes for luda_type
1073         # take into account of overhead in lu_dirpage header and end mark in
1074         # each page, plus one in RPC_NUM calculation.
1075         DIRENT_SIZE=48
1076         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1077         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1078         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1079                                 awk '/^mds_readpage/ {print $2}')
1080         [[ $mds_readpage -gt $RPC_NUM ]] &&
1081                 error "large readdir doesn't take effect"
1082
1083         simple_cleanup_common
1084 }
1085 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1086
1087 test_24w() { # bug21506
1088         SZ1=234852
1089         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1090         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1091         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1092         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1093         [[ "$SZ1" -eq "$SZ2" ]] ||
1094                 error "Error reading at the end of the file $tfile"
1095 }
1096 run_test 24w "Reading a file larger than 4Gb"
1097
1098 test_24x() {
1099         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1101         local MDTIDX=1
1102         local remote_dir=$DIR/$tdir/remote_dir
1103
1104         test_mkdir -p $DIR/$tdir
1105         $LFS mkdir -i $MDTIDX $remote_dir ||
1106                 error "create remote directory failed"
1107
1108         test_mkdir -p $DIR/$tdir/src_dir
1109         touch $DIR/$tdir/src_file
1110         test_mkdir -p $remote_dir/tgt_dir
1111         touch $remote_dir/tgt_file
1112
1113         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1114                 error "rename dir cross MDT failed!"
1115
1116         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1117                 error "rename file cross MDT failed!"
1118
1119         touch $DIR/$tdir/ln_file
1120         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1121                 error "ln file cross MDT failed"
1122
1123         rm -rf $DIR/$tdir || error "Can not delete directories"
1124 }
1125 run_test 24x "cross MDT rename/link"
1126
1127 test_24y() {
1128         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1130         local MDTIDX=1
1131         local remote_dir=$DIR/$tdir/remote_dir
1132
1133         test_mkdir -p $DIR/$tdir
1134         $LFS mkdir -i $MDTIDX $remote_dir ||
1135                    error "create remote directory failed"
1136
1137         test_mkdir -p $remote_dir/src_dir
1138         touch $remote_dir/src_file
1139         test_mkdir -p $remote_dir/tgt_dir
1140         touch $remote_dir/tgt_file
1141
1142         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1143                 error "rename subdir in the same remote dir failed!"
1144
1145         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1146                 error "rename files in the same remote dir failed!"
1147
1148         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1149                 error "link files in the same remote dir failed!"
1150
1151         rm -rf $DIR/$tdir || error "Can not delete directories"
1152 }
1153 run_test 24y "rename/link on the same dir should succeed"
1154
1155 test_24A() { # LU-3182
1156         local NFILES=5000
1157
1158         rm -rf $DIR/$tdir
1159         test_mkdir -p $DIR/$tdir
1160         createmany -m $DIR/$tdir/$tfile $NFILES
1161         local t=$(ls $DIR/$tdir | wc -l)
1162         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1163         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1164         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1165                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1166         fi
1167
1168         rm -rf $DIR/$tdir || error "Can not delete directories"
1169 }
1170 run_test 24A "readdir() returns correct number of entries."
1171
1172 test_24B() { # LU-4805
1173         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1174         local count
1175
1176         test_mkdir $DIR/$tdir
1177         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1178                 error "create striped dir failed"
1179
1180         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1181         [ $count -eq 2 ] || error "Expected 2, got $count"
1182
1183         touch $DIR/$tdir/striped_dir/a
1184
1185         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1186         [ $count -eq 3 ] || error "Expected 3, got $count"
1187
1188         touch $DIR/$tdir/striped_dir/.f
1189
1190         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1191         [ $count -eq 4 ] || error "Expected 4, got $count"
1192
1193         rm -rf $DIR/$tdir || error "Can not delete directories"
1194 }
1195 run_test 24B "readdir for striped dir return correct number of entries"
1196
1197 test_24C() {
1198         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1199
1200         mkdir $DIR/$tdir
1201         mkdir $DIR/$tdir/d0
1202         mkdir $DIR/$tdir/d1
1203
1204         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1205                 error "create striped dir failed"
1206
1207         cd $DIR/$tdir/d0/striped_dir
1208
1209         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1210         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1211         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1212
1213         [ "$d0_ino" = "$parent_ino" ] ||
1214                 error ".. wrong, expect $d0_ino, get $parent_ino"
1215
1216         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1217                 error "mv striped dir failed"
1218
1219         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1220
1221         [ "$d1_ino" = "$parent_ino" ] ||
1222                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1223 }
1224 run_test 24C "check .. in striped dir"
1225
1226 test_24D() { # LU-6101
1227         local NFILES=50000
1228
1229         rm -rf $DIR/$tdir
1230         mkdir -p $DIR/$tdir
1231         createmany -m $DIR/$tdir/$tfile $NFILES
1232         local t=$(ls $DIR/$tdir | wc -l)
1233         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1234         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1235         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1236                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1237         fi
1238
1239         rm -rf $DIR/$tdir || error "Can not delete directories"
1240 }
1241 run_test 24D "readdir() returns correct number of entries after cursor reload"
1242
1243 test_24E() {
1244         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1245         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1246
1247         mkdir -p $DIR/$tdir
1248         mkdir $DIR/$tdir/src_dir
1249         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1250                 error "create remote source failed"
1251
1252         touch $DIR/$tdir/src_dir/src_child/a
1253
1254         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1255                 error "create remote target dir failed"
1256
1257         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1258                 error "create remote target child failed"
1259
1260         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1261                 error "rename dir cross MDT failed!"
1262
1263         find $DIR/$tdir
1264
1265         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1266                 error "src_child still exists after rename"
1267
1268         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1269                 error "missing file(a) after rename"
1270
1271         rm -rf $DIR/$tdir || error "Can not delete directories"
1272 }
1273 run_test 24E "cross MDT rename/link"
1274
1275 test_25a() {
1276         echo '== symlink sanity ============================================='
1277
1278         test_mkdir $DIR/d25
1279         ln -s d25 $DIR/s25
1280         touch $DIR/s25/foo || error
1281 }
1282 run_test 25a "create file in symlinked directory ==============="
1283
1284 test_25b() {
1285         [ ! -d $DIR/d25 ] && test_25a
1286         $CHECKSTAT -t file $DIR/s25/foo || error
1287 }
1288 run_test 25b "lookup file in symlinked directory ==============="
1289
1290 test_26a() {
1291         test_mkdir $DIR/d26
1292         test_mkdir $DIR/d26/d26-2
1293         ln -s d26/d26-2 $DIR/s26
1294         touch $DIR/s26/foo || error
1295 }
1296 run_test 26a "multiple component symlink ======================="
1297
1298 test_26b() {
1299         test_mkdir -p $DIR/d26b/d26-2
1300         ln -s d26b/d26-2/foo $DIR/s26-2
1301         touch $DIR/s26-2 || error
1302 }
1303 run_test 26b "multiple component symlink at end of lookup ======"
1304
1305 test_26c() {
1306         test_mkdir $DIR/d26.2
1307         touch $DIR/d26.2/foo
1308         ln -s d26.2 $DIR/s26.2-1
1309         ln -s s26.2-1 $DIR/s26.2-2
1310         ln -s s26.2-2 $DIR/s26.2-3
1311         chmod 0666 $DIR/s26.2-3/foo
1312 }
1313 run_test 26c "chain of symlinks ================================"
1314
1315 # recursive symlinks (bug 439)
1316 test_26d() {
1317         ln -s d26-3/foo $DIR/d26-3
1318 }
1319 run_test 26d "create multiple component recursive symlink ======"
1320
1321 test_26e() {
1322         [ ! -h $DIR/d26-3 ] && test_26d
1323         rm $DIR/d26-3
1324 }
1325 run_test 26e "unlink multiple component recursive symlink ======"
1326
1327 # recursive symlinks (bug 7022)
1328 test_26f() {
1329         test_mkdir -p $DIR/$tdir
1330         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1331         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1332         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1333         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1334         cd $tfile                || error "cd $tfile failed"
1335         ln -s .. dotdot          || error "ln dotdot failed"
1336         ln -s dotdot/lndir lndir || error "ln lndir failed"
1337         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1338         output=`ls $tfile/$tfile/lndir/bar1`
1339         [ "$output" = bar1 ] && error "unexpected output"
1340         rm -r $tfile             || error "rm $tfile failed"
1341         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1342 }
1343 run_test 26f "rm -r of a directory which has recursive symlink ="
1344
1345 test_27a() {
1346         echo '== stripe sanity =============================================='
1347         test_mkdir -p $DIR/d27 || error "mkdir failed"
1348         $GETSTRIPE $DIR/d27
1349         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1350         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1351         log "== test_27a: write to one stripe file ========================="
1352         cp /etc/hosts $DIR/d27/f0 || error
1353 }
1354 run_test 27a "one stripe file =================================="
1355
1356 test_27b() {
1357         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1358         test_mkdir -p $DIR/d27
1359         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1360         $GETSTRIPE -c $DIR/d27/f01
1361         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1362                 error "two-stripe file doesn't have two stripes"
1363
1364         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1365 }
1366 run_test 27b "create and write to two stripe file"
1367
1368 test_27d() {
1369         test_mkdir -p $DIR/d27
1370         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1371         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1372         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1373 }
1374 run_test 27d "create file with default settings ================"
1375
1376 test_27e() {
1377         # LU-5839 adds check for existed layout before setting it
1378         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1379                 skip "Need MDS version at least 2.7.56" && return
1380         test_mkdir -p $DIR/d27
1381         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1382         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1383         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1384 }
1385 run_test 27e "setstripe existing file (should return error) ======"
1386
1387 test_27f() {
1388         test_mkdir $DIR/$tdir
1389         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1390                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1391         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1392                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1393         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1394         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1395 }
1396 run_test 27f "setstripe with bad stripe size (should return error)"
1397
1398 test_27g() {
1399         test_mkdir -p $DIR/d27
1400         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1401         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1402                 error "$DIR/d27/fnone has object"
1403 }
1404 run_test 27g "$GETSTRIPE with no objects"
1405
1406 test_27i() {
1407         test_mkdir $DIR/$tdir
1408         touch $DIR/$tdir/$tfile || error "touch failed"
1409         [[ $($GETSTRIPE -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1410                 error "missing objects"
1411 }
1412 run_test 27i "$GETSTRIPE with some objects"
1413
1414 test_27j() {
1415         test_mkdir -p $DIR/d27
1416         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1417 }
1418 run_test 27j "setstripe with bad stripe offset (should return error)"
1419
1420 test_27k() { # bug 2844
1421         test_mkdir -p $DIR/d27
1422         FILE=$DIR/d27/f27k
1423         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1424         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1425         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1426         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1427         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1428         dd if=/dev/zero of=$FILE bs=4k count=1
1429         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1430         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1431 }
1432 run_test 27k "limit i_blksize for broken user apps ============="
1433
1434 test_27l() {
1435         test_mkdir -p $DIR/d27
1436         mcreate $DIR/f27l || error "creating file"
1437         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1438                 error "setstripe should have failed" || true
1439 }
1440 run_test 27l "check setstripe permissions (should return error)"
1441
1442 test_27m() {
1443         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1444                 return
1445         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1446                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1447                 return
1448         fi
1449         trap simple_cleanup_common EXIT
1450         test_mkdir -p $DIR/$tdir
1451         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1452         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1453                 error "dd should fill OST0"
1454         i=2
1455         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1456                 i=$((i + 1))
1457                 [ $i -gt 256 ] && break
1458         done
1459         i=$((i + 1))
1460         touch $DIR/$tdir/f27m_$i
1461         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1462                 error "OST0 was full but new created file still use it"
1463         i=$((i + 1))
1464         touch $DIR/$tdir/f27m_$i
1465         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1466                 error "OST0 was full but new created file still use it"
1467         simple_cleanup_common
1468 }
1469 run_test 27m "create file while OST0 was full =================="
1470
1471 sleep_maxage() {
1472         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1473         sleep $DELAY
1474 }
1475
1476 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1477 # if the OST isn't full anymore.
1478 reset_enospc() {
1479         local OSTIDX=${1:-""}
1480
1481         local list=$(comma_list $(osts_nodes))
1482         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1483
1484         do_nodes $list lctl set_param fail_loc=0
1485         sync    # initiate all OST_DESTROYs from MDS to OST
1486         sleep_maxage
1487 }
1488
1489 exhaust_precreations() {
1490         local OSTIDX=$1
1491         local FAILLOC=$2
1492         local FAILIDX=${3:-$OSTIDX}
1493         local ofacet=ost$((OSTIDX + 1))
1494
1495         test_mkdir -p -c1 $DIR/$tdir
1496         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1497         local mfacet=mds$((mdtidx + 1))
1498         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1499
1500         local OST=$(ostname_from_index $OSTIDX)
1501
1502         # on the mdt's osc
1503         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1504         local last_id=$(do_facet $mfacet lctl get_param -n \
1505                         osc.$mdtosc_proc1.prealloc_last_id)
1506         local next_id=$(do_facet $mfacet lctl get_param -n \
1507                         osc.$mdtosc_proc1.prealloc_next_id)
1508
1509         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1510         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1511
1512         test_mkdir -p $DIR/$tdir/${OST}
1513         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1514 #define OBD_FAIL_OST_ENOSPC              0x215
1515         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1516         echo "Creating to objid $last_id on ost $OST..."
1517         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1518         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1519         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1520         sleep_maxage
1521 }
1522
1523 exhaust_all_precreations() {
1524         local i
1525         for (( i=0; i < OSTCOUNT; i++ )) ; do
1526                 exhaust_precreations $i $1 -1
1527         done
1528 }
1529
1530 test_27n() {
1531         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1533         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1534         remote_ost_nodsh && skip "remote OST with nodsh" && return
1535
1536         reset_enospc
1537         rm -f $DIR/$tdir/$tfile
1538         exhaust_precreations 0 0x80000215
1539         $SETSTRIPE -c -1 $DIR/$tdir
1540         touch $DIR/$tdir/$tfile || error
1541         $GETSTRIPE $DIR/$tdir/$tfile
1542         reset_enospc
1543 }
1544 run_test 27n "create file with some full OSTs =================="
1545
1546 test_27o() {
1547         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1549         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1550         remote_ost_nodsh && skip "remote OST with nodsh" && return
1551
1552         reset_enospc
1553         rm -f $DIR/$tdir/$tfile
1554         exhaust_all_precreations 0x215
1555
1556         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1557
1558         reset_enospc
1559         rm -rf $DIR/$tdir/*
1560 }
1561 run_test 27o "create file with all full OSTs (should error) ===="
1562
1563 test_27p() {
1564         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1566         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1567         remote_ost_nodsh && skip "remote OST with nodsh" && return
1568
1569         reset_enospc
1570         rm -f $DIR/$tdir/$tfile
1571         test_mkdir -p $DIR/$tdir
1572
1573         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1574         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1575         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1576
1577         exhaust_precreations 0 0x80000215
1578         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1579         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1580         $GETSTRIPE $DIR/$tdir/$tfile
1581
1582         reset_enospc
1583 }
1584 run_test 27p "append to a truncated file with some full OSTs ==="
1585
1586 test_27q() {
1587         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1588         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1589         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1590         remote_ost_nodsh && skip "remote OST with nodsh" && return
1591
1592         reset_enospc
1593         rm -f $DIR/$tdir/$tfile
1594
1595         test_mkdir -p $DIR/$tdir
1596         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1597         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1598         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1599
1600         exhaust_all_precreations 0x215
1601
1602         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1603         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1604
1605         reset_enospc
1606 }
1607 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1608
1609 test_27r() {
1610         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1612         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1613         remote_ost_nodsh && skip "remote OST with nodsh" && return
1614
1615         reset_enospc
1616         rm -f $DIR/$tdir/$tfile
1617         exhaust_precreations 0 0x80000215
1618
1619         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1620
1621         reset_enospc
1622 }
1623 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1624
1625 test_27s() { # bug 10725
1626         test_mkdir -p $DIR/$tdir
1627         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1628         local stripe_count=0
1629         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1630         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1631                 error "stripe width >= 2^32 succeeded" || true
1632
1633 }
1634 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1635
1636 test_27t() { # bug 10864
1637         WDIR=$(pwd)
1638         WLFS=$(which lfs)
1639         cd $DIR
1640         touch $tfile
1641         $WLFS getstripe $tfile
1642         cd $WDIR
1643 }
1644 run_test 27t "check that utils parse path correctly"
1645
1646 test_27u() { # bug 4900
1647         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1648         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1649         local index
1650         local list=$(comma_list $(mdts_nodes))
1651
1652 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1653         do_nodes $list $LCTL set_param fail_loc=0x139
1654         test_mkdir -p $DIR/$tdir
1655         rm -rf $DIR/$tdir/*
1656         createmany -o $DIR/$tdir/t- 1000
1657         do_nodes $list $LCTL set_param fail_loc=0
1658
1659         TLOG=$DIR/$tfile.getstripe
1660         $GETSTRIPE $DIR/$tdir > $TLOG
1661         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1662         unlinkmany $DIR/$tdir/t- 1000
1663         [[ $OBJS -gt 0 ]] &&
1664                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1665 }
1666 run_test 27u "skip object creation on OSC w/o objects =========="
1667
1668 test_27v() { # bug 4900
1669         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1671         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1672         remote_ost_nodsh && skip "remote OST with nodsh" && return
1673
1674         exhaust_all_precreations 0x215
1675         reset_enospc
1676
1677         test_mkdir -p $DIR/$tdir
1678         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1679
1680         touch $DIR/$tdir/$tfile
1681         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1682         # all except ost1
1683         for (( i=1; i < OSTCOUNT; i++ )); do
1684                 do_facet ost$i lctl set_param fail_loc=0x705
1685         done
1686         local START=`date +%s`
1687         createmany -o $DIR/$tdir/$tfile 32
1688
1689         local FINISH=`date +%s`
1690         local TIMEOUT=`lctl get_param -n timeout`
1691         local PROCESS=$((FINISH - START))
1692         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1693                error "$FINISH - $START >= $TIMEOUT / 2"
1694         sleep $((TIMEOUT / 2 - PROCESS))
1695         reset_enospc
1696 }
1697 run_test 27v "skip object creation on slow OST ================="
1698
1699 test_27w() { # bug 10997
1700         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1701         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1702         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1703                 error "stripe size $size != 65536" || true
1704         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1705                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1706 }
1707 run_test 27w "check $SETSTRIPE -S option"
1708
1709 test_27wa() {
1710         [[ $OSTCOUNT -lt 2 ]] &&
1711                 skip_env "skipping multiple stripe count/offset test" && return
1712
1713         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1714         for i in $(seq 1 $OSTCOUNT); do
1715                 offset=$((i - 1))
1716                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1717                         error "setstripe -c $i -i $offset failed"
1718                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1719                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1720                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1721                 [ $index -ne $offset ] &&
1722                         error "stripe offset $index != $offset" || true
1723         done
1724 }
1725 run_test 27wa "check $SETSTRIPE -c -i options"
1726
1727 test_27x() {
1728         remote_ost_nodsh && skip "remote OST with nodsh" && return
1729         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1731         OFFSET=$(($OSTCOUNT - 1))
1732         OSTIDX=0
1733         local OST=$(ostname_from_index $OSTIDX)
1734
1735         test_mkdir -p $DIR/$tdir
1736         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1737         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1738         sleep_maxage
1739         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1740         for i in `seq 0 $OFFSET`; do
1741                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1742                 error "OST0 was degraded but new created file still use it"
1743         done
1744         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1745 }
1746 run_test 27x "create files while OST0 is degraded"
1747
1748 test_27y() {
1749         [[ $OSTCOUNT -lt 2 ]] &&
1750                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1751         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1752         remote_ost_nodsh && skip "remote OST with nodsh" && return
1753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1754
1755         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1756         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1757             osc.$mdtosc.prealloc_last_id)
1758         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1759             osc.$mdtosc.prealloc_next_id)
1760         local fcount=$((last_id - next_id))
1761         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1762         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1763
1764         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1765                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1766         local OST_DEACTIVE_IDX=-1
1767         local OSC
1768         local OSTIDX
1769         local OST
1770
1771         for OSC in $MDS_OSCS; do
1772                 OST=$(osc_to_ost $OSC)
1773                 OSTIDX=$(index_from_ostuuid $OST)
1774                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1775                         OST_DEACTIVE_IDX=$OSTIDX
1776                 fi
1777                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1778                         echo $OSC "is Deactivated:"
1779                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1780                 fi
1781         done
1782
1783         OSTIDX=$(index_from_ostuuid $OST)
1784         test_mkdir -p $DIR/$tdir
1785         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1786
1787         for OSC in $MDS_OSCS; do
1788                 OST=$(osc_to_ost $OSC)
1789                 OSTIDX=$(index_from_ostuuid $OST)
1790                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1791                         echo $OST "is degraded:"
1792                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1793                                                 obdfilter.$OST.degraded=1
1794                 fi
1795         done
1796
1797         sleep_maxage
1798         createmany -o $DIR/$tdir/$tfile $fcount
1799
1800         for OSC in $MDS_OSCS; do
1801                 OST=$(osc_to_ost $OSC)
1802                 OSTIDX=$(index_from_ostuuid $OST)
1803                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1804                         echo $OST "is recovered from degraded:"
1805                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1806                                                 obdfilter.$OST.degraded=0
1807                 else
1808                         do_facet $SINGLEMDS lctl --device %$OSC activate
1809                 fi
1810         done
1811
1812         # all osp devices get activated, hence -1 stripe count restored
1813         local stripecnt=0
1814
1815         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1816         # devices get activated.
1817         sleep_maxage
1818         $SETSTRIPE -c -1 $DIR/$tfile
1819         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1820         rm -f $DIR/$tfile
1821         [ $stripecnt -ne $OSTCOUNT ] &&
1822                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1823         return 0
1824 }
1825 run_test 27y "create files while OST0 is degraded and the rest inactive"
1826
1827 check_seq_oid()
1828 {
1829         log "check file $1"
1830
1831         lmm_count=$($GETSTRIPE -c $1)
1832         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1833         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1834
1835         local old_ifs="$IFS"
1836         IFS=$'[:]'
1837         fid=($($LFS path2fid $1))
1838         IFS="$old_ifs"
1839
1840         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1841         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1842
1843         # compare lmm_seq and lu_fid->f_seq
1844         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1845         # compare lmm_object_id and lu_fid->oid
1846         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1847
1848         # check the trusted.fid attribute of the OST objects of the file
1849         local have_obdidx=false
1850         local stripe_nr=0
1851         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1852                 # skip lines up to and including "obdidx"
1853                 [ -z "$obdidx" ] && break
1854                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1855                 $have_obdidx || continue
1856
1857                 local ost=$((obdidx + 1))
1858                 local dev=$(ostdevname $ost)
1859                 local oid_hex
1860
1861                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1862
1863                 seq=$(echo $seq | sed -e "s/^0x//g")
1864                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1865                         oid_hex=$(echo $oid)
1866                 else
1867                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1868                 fi
1869                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1870
1871                 local ff
1872                 #
1873                 # Don't unmount/remount the OSTs if we don't need to do that.
1874                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1875                 # update too, until that use mount/ll_decode_filter_fid/mount.
1876                 # Re-enable when debugfs will understand new filter_fid.
1877                 #
1878                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1879                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1880                                 $dev 2>/dev/null" | grep "parent=")
1881                 else
1882                         stop ost$ost
1883                         mount_fstype ost$ost
1884                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1885                                 $(facet_mntpt ost$ost)/$obj_file)
1886                         unmount_fstype ost$ost
1887                         start ost$ost $dev $OST_MOUNT_OPTS
1888                 fi
1889
1890                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1891
1892                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1893
1894                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1895                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1896                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1897                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1898                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1899                 local ff_pstripe
1900                 if echo $ff_parent | grep -q 'stripe='; then
1901                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1902                 else
1903                         #
1904                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1905                         # into f_ver in this case.  See the comment on
1906                         # ff_parent.
1907                         #
1908                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1909                                 sed -e 's/\]//')
1910                 fi
1911
1912                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1913                 [ $ff_pseq = $lmm_seq ] ||
1914                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1915                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1916                 [ $ff_poid = $lmm_oid ] ||
1917                         error "FF parent OID $ff_poid != $lmm_oid"
1918                 (($ff_pstripe == $stripe_nr)) ||
1919                         error "FF stripe $ff_pstripe != $stripe_nr"
1920
1921                 stripe_nr=$((stripe_nr + 1))
1922         done
1923 }
1924
1925 test_27z() {
1926         remote_ost_nodsh && skip "remote OST with nodsh" && return
1927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1928         test_mkdir -p $DIR/$tdir
1929
1930         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1931                 { error "setstripe -c -1 failed"; return 1; }
1932         # We need to send a write to every object to get parent FID info set.
1933         # This _should_ also work for setattr, but does not currently.
1934         # touch $DIR/$tdir/$tfile-1 ||
1935         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1936                 { error "dd $tfile-1 failed"; return 2; }
1937         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1938                 { error "setstripe -c -1 failed"; return 3; }
1939         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1940                 { error "dd $tfile-2 failed"; return 4; }
1941
1942         # make sure write RPCs have been sent to OSTs
1943         sync; sleep 5; sync
1944
1945         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1946         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1947 }
1948 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1949
1950 test_27A() { # b=19102
1951         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1952         local restore_size=$($GETSTRIPE -S $MOUNT)
1953         local restore_count=$($GETSTRIPE -c $MOUNT)
1954         local restore_offset=$($GETSTRIPE -i $MOUNT)
1955         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1956         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1957                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1958         local default_size=$($GETSTRIPE -S $MOUNT)
1959         local default_offset=$($GETSTRIPE -i $MOUNT)
1960         local dsize=$((1024 * 1024))
1961         [ $default_size -eq $dsize ] ||
1962                 error "stripe size $default_size != $dsize"
1963         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1964         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1965 }
1966 run_test 27A "check filesystem-wide default LOV EA values"
1967
1968 test_27B() { # LU-2523
1969         test_mkdir -p $DIR/$tdir
1970         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1971         touch $DIR/$tdir/f0
1972         # open f1 with O_LOV_DELAY_CREATE
1973         # rename f0 onto f1
1974         # call setstripe ioctl on open file descriptor for f1
1975         # close
1976         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1977                 $DIR/$tdir/f0
1978
1979         rm -f $DIR/$tdir/f1
1980         # open f1 with O_LOV_DELAY_CREATE
1981         # unlink f1
1982         # call setstripe ioctl on open file descriptor for f1
1983         # close
1984         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1985
1986         # Allow multiop to fail in imitation of NFS's busted semantics.
1987         true
1988 }
1989 run_test 27B "call setstripe on open unlinked file/rename victim"
1990
1991 test_27C() { #LU-2871
1992         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1993
1994         declare -a ost_idx
1995         local index
1996         local found
1997         local i
1998         local j
1999
2000         test_mkdir -p $DIR/$tdir
2001         cd $DIR/$tdir
2002         for i in $(seq 0 $((OSTCOUNT - 1))); do
2003                 # set stripe across all OSTs starting from OST$i
2004                 $SETSTRIPE -i $i -c -1 $tfile$i
2005                 # get striping information
2006                 ost_idx=($($GETSTRIPE $tfile$i |
2007                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2008                 echo ${ost_idx[@]}
2009
2010                 # check the layout
2011                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2012                         error "${#ost_idx[@]} != $OSTCOUNT"
2013
2014                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2015                         found=0
2016                         for j in $(echo ${ost_idx[@]}); do
2017                                 if [ $index -eq $j ]; then
2018                                         found=1
2019                                         break
2020                                 fi
2021                         done
2022                         [ $found = 1 ] ||
2023                                 error "Can not find $index in ${ost_idx[@]}"
2024                 done
2025         done
2026 }
2027 run_test 27C "check full striping across all OSTs"
2028
2029 test_27D() {
2030         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2031         local POOL=${POOL:-testpool}
2032         local first_ost=0
2033         local last_ost=$(($OSTCOUNT - 1))
2034         local ost_step=1
2035         local ost_list=$(seq $first_ost $ost_step $last_ost)
2036         local ost_range="$first_ost $last_ost $ost_step"
2037
2038         test_mkdir -p $DIR/$tdir
2039         pool_add $POOL || error "pool_add failed"
2040         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2041         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2042                 error "llapi_layout_test failed"
2043         cleanup_pools || error "cleanup_pools failed"
2044 }
2045 run_test 27D "validate llapi_layout API"
2046
2047 # Verify that default_easize is increased from its initial value after
2048 # accessing a widely striped file.
2049 test_27E() {
2050         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2051
2052         # 72 bytes is the minimum space required to store striping
2053         # information for a file striped across one OST:
2054         # (sizeof(struct lov_user_md_v3) +
2055         #  sizeof(struct lov_user_ost_data_v1))
2056         local min_easize=72
2057         $LCTL set_param -n llite.*.default_easize $min_easize ||
2058                 error "lctl set_param failed"
2059         local easize=$($LCTL get_param -n llite.*.default_easize)
2060
2061         [ $easize -eq $min_easize ] ||
2062                 error "failed to set default_easize"
2063
2064         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2065                 error "setstripe failed"
2066         cat $DIR/$tfile
2067         rm $DIR/$tfile
2068
2069         easize=$($LCTL get_param -n llite.*.default_easize)
2070
2071         [ $easize -gt $min_easize ] ||
2072                 error "default_easize not updated"
2073 }
2074 run_test 27E "check that default extended attribute size properly increases"
2075
2076 # createtest also checks that device nodes are created and
2077 # then visible correctly (#2091)
2078 test_28() { # bug 2091
2079         test_mkdir $DIR/d28
2080         $CREATETEST $DIR/d28/ct || error
2081 }
2082 run_test 28 "create/mknod/mkdir with bad file types ============"
2083
2084 test_29() {
2085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2086         cancel_lru_locks mdc
2087         test_mkdir $DIR/d29
2088         touch $DIR/d29/foo
2089         log 'first d29'
2090         ls -l $DIR/d29
2091
2092         declare -i LOCKCOUNTORIG=0
2093         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2094                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2095         done
2096         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2097
2098         declare -i LOCKUNUSEDCOUNTORIG=0
2099         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2100                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2101         done
2102
2103         log 'second d29'
2104         ls -l $DIR/d29
2105         log 'done'
2106
2107         declare -i LOCKCOUNTCURRENT=0
2108         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2109                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2110         done
2111
2112         declare -i LOCKUNUSEDCOUNTCURRENT=0
2113         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2114                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2115         done
2116
2117         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2118                 $LCTL set_param -n ldlm.dump_namespaces ""
2119                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2120                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2121                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2122                 return 2
2123         fi
2124         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2125                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2126                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2127                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2128                 return 3
2129         fi
2130 }
2131 run_test 29 "IT_GETATTR regression  ============================"
2132
2133 test_30a() { # was test_30
2134         cp $(which ls) $DIR || cp /bin/ls $DIR
2135         $DIR/ls / || error
2136         rm $DIR/ls
2137 }
2138 run_test 30a "execute binary from Lustre (execve) =============="
2139
2140 test_30b() {
2141         cp `which ls` $DIR || cp /bin/ls $DIR
2142         chmod go+rx $DIR/ls
2143         $RUNAS $DIR/ls / || error
2144         rm $DIR/ls
2145 }
2146 run_test 30b "execute binary from Lustre as non-root ==========="
2147
2148 test_30c() { # b=22376
2149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2150         cp `which ls` $DIR || cp /bin/ls $DIR
2151         chmod a-rw $DIR/ls
2152         cancel_lru_locks mdc
2153         cancel_lru_locks osc
2154         $RUNAS $DIR/ls / || error
2155         rm -f $DIR/ls
2156 }
2157 run_test 30c "execute binary from Lustre without read perms ===="
2158
2159 test_31a() {
2160         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2161         $CHECKSTAT -a $DIR/f31 || error
2162 }
2163 run_test 31a "open-unlink file =================================="
2164
2165 test_31b() {
2166         touch $DIR/f31 || error
2167         ln $DIR/f31 $DIR/f31b || error
2168         $MULTIOP $DIR/f31b Ouc || error
2169         $CHECKSTAT -t file $DIR/f31 || error
2170 }
2171 run_test 31b "unlink file with multiple links while open ======="
2172
2173 test_31c() {
2174         touch $DIR/f31 || error
2175         ln $DIR/f31 $DIR/f31c || error
2176         multiop_bg_pause $DIR/f31 O_uc || return 1
2177         MULTIPID=$!
2178         $MULTIOP $DIR/f31c Ouc
2179         kill -USR1 $MULTIPID
2180         wait $MULTIPID
2181 }
2182 run_test 31c "open-unlink file with multiple links ============="
2183
2184 test_31d() {
2185         opendirunlink $DIR/d31d $DIR/d31d || error
2186         $CHECKSTAT -a $DIR/d31d || error
2187 }
2188 run_test 31d "remove of open directory ========================="
2189
2190 test_31e() { # bug 2904
2191         openfilleddirunlink $DIR/d31e || error
2192 }
2193 run_test 31e "remove of open non-empty directory ==============="
2194
2195 test_31f() { # bug 4554
2196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2197         set -vx
2198         test_mkdir $DIR/d31f
2199         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2200         cp /etc/hosts $DIR/d31f
2201         ls -l $DIR/d31f
2202         $GETSTRIPE $DIR/d31f/hosts
2203         multiop_bg_pause $DIR/d31f D_c || return 1
2204         MULTIPID=$!
2205
2206         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2207         test_mkdir $DIR/d31f
2208         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2209         cp /etc/hosts $DIR/d31f
2210         ls -l $DIR/d31f
2211         $GETSTRIPE $DIR/d31f/hosts
2212         multiop_bg_pause $DIR/d31f D_c || return 1
2213         MULTIPID2=$!
2214
2215         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2216         wait $MULTIPID || error "first opendir $MULTIPID failed"
2217
2218         sleep 6
2219
2220         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2221         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2222         set +vx
2223 }
2224 run_test 31f "remove of open directory with open-unlink file ==="
2225
2226 test_31g() {
2227         echo "-- cross directory link --"
2228         test_mkdir -c1 $DIR/${tdir}ga
2229         test_mkdir -c1 $DIR/${tdir}gb
2230         touch $DIR/${tdir}ga/f
2231         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2232         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2233         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2234         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2235         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2236 }
2237 run_test 31g "cross directory link==============="
2238
2239 test_31h() {
2240         echo "-- cross directory link --"
2241         test_mkdir -c1 $DIR/${tdir}
2242         test_mkdir -c1 $DIR/${tdir}/dir
2243         touch $DIR/${tdir}/f
2244         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2245         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2246         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2247         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2248         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2249 }
2250 run_test 31h "cross directory link under child==============="
2251
2252 test_31i() {
2253         echo "-- cross directory link --"
2254         test_mkdir -c1 $DIR/$tdir
2255         test_mkdir -c1 $DIR/$tdir/dir
2256         touch $DIR/$tdir/dir/f
2257         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2258         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2259         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2260         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2261         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2262 }
2263 run_test 31i "cross directory link under parent==============="
2264
2265 test_31j() {
2266         test_mkdir -c1 -p $DIR/$tdir
2267         test_mkdir -c1 -p $DIR/$tdir/dir1
2268         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2269         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2270         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2271         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2272         return 0
2273 }
2274 run_test 31j "link for directory==============="
2275
2276 test_31k() {
2277         test_mkdir -c1 -p $DIR/$tdir
2278         touch $DIR/$tdir/s
2279         touch $DIR/$tdir/exist
2280         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2281         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2282         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2283         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2284         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2285         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2286         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2287         return 0
2288 }
2289 run_test 31k "link to file: the same, non-existing, dir==============="
2290
2291 test_31m() {
2292         mkdir $DIR/d31m
2293         touch $DIR/d31m/s
2294         mkdir $DIR/d31m2
2295         touch $DIR/d31m2/exist
2296         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2297         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2298         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2299         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2300         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2301         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2302         return 0
2303 }
2304 run_test 31m "link to file: the same, non-existing, dir==============="
2305
2306 test_31n() {
2307         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2308         nlink=$(stat --format=%h $DIR/$tfile)
2309         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2310         local fd=$(free_fd)
2311         local cmd="exec $fd<$DIR/$tfile"
2312         eval $cmd
2313         cmd="exec $fd<&-"
2314         trap "eval $cmd" EXIT
2315         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2316         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2317         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2318         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2319         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2320         eval $cmd
2321 }
2322 run_test 31n "check link count of unlinked file"
2323
2324 link_one() {
2325         local TEMPNAME=$(mktemp $1_XXXXXX)
2326         mlink $TEMPNAME $1 2> /dev/null &&
2327                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2328         munlink $TEMPNAME
2329 }
2330
2331 test_31o() { # LU-2901
2332         test_mkdir -p $DIR/$tdir
2333         for LOOP in $(seq 100); do
2334                 rm -f $DIR/$tdir/$tfile*
2335                 for THREAD in $(seq 8); do
2336                         link_one $DIR/$tdir/$tfile.$LOOP &
2337                 done
2338                 wait
2339                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2340                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2341                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2342                         break || true
2343         done
2344 }
2345 run_test 31o "duplicate hard links with same filename"
2346
2347 test_31p() {
2348         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2349
2350         test_mkdir $DIR/$tdir
2351         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2352         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2353
2354         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2355                 error "open unlink test1 failed"
2356         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2357                 error "open unlink test2 failed"
2358
2359         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2360                 error "test1 still exists"
2361         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2362                 error "test2 still exists"
2363 }
2364 run_test 31p "remove of open striped directory"
2365
2366 cleanup_test32_mount() {
2367         trap 0
2368         $UMOUNT $DIR/$tdir/ext2-mountpoint
2369 }
2370
2371 test_32a() {
2372         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2373         echo "== more mountpoints and symlinks ================="
2374         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2375         trap cleanup_test32_mount EXIT
2376         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2377         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2378         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2379         cleanup_test32_mount
2380 }
2381 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2382
2383 test_32b() {
2384         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2385         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2386         trap cleanup_test32_mount EXIT
2387         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2388         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2389         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2390         cleanup_test32_mount
2391 }
2392 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2393
2394 test_32c() {
2395         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2396         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2397         trap cleanup_test32_mount EXIT
2398         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2399         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2400         test_mkdir -p $DIR/$tdir/d2/test_dir
2401         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2402         cleanup_test32_mount
2403 }
2404 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2405
2406 test_32d() {
2407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2408         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2409         trap cleanup_test32_mount EXIT
2410         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2411         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2412         test_mkdir -p $DIR/$tdir/d2/test_dir
2413         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2414         cleanup_test32_mount
2415 }
2416 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2417
2418 test_32e() {
2419         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2420         test_mkdir -p $DIR/d32e/tmp
2421         TMP_DIR=$DIR/d32e/tmp
2422         ln -s $DIR/d32e $TMP_DIR/symlink11
2423         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2424         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2425         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2426 }
2427 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2428
2429 test_32f() {
2430         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2431         test_mkdir -p $DIR/d32f/tmp
2432         TMP_DIR=$DIR/d32f/tmp
2433         ln -s $DIR/d32f $TMP_DIR/symlink11
2434         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2435         ls $DIR/d32f/tmp/symlink11  || error
2436         ls $DIR/d32f/symlink01 || error
2437 }
2438 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2439
2440 test_32g() {
2441         TMP_DIR=$DIR/$tdir/tmp
2442         test_mkdir -p $DIR/$tdir/tmp
2443         test_mkdir $DIR/${tdir}2
2444         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2445         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2446         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2447         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2448         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2449         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2450 }
2451 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2452
2453 test_32h() {
2454         rm -fr $DIR/$tdir $DIR/${tdir}2
2455         TMP_DIR=$DIR/$tdir/tmp
2456         test_mkdir -p $DIR/$tdir/tmp
2457         test_mkdir $DIR/${tdir}2
2458         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2459         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2460         ls $TMP_DIR/symlink12 || error
2461         ls $DIR/$tdir/symlink02  || error
2462 }
2463 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2464
2465 test_32i() {
2466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2467         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2468         trap cleanup_test32_mount EXIT
2469         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2470         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2471         touch $DIR/$tdir/test_file
2472         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2473         cleanup_test32_mount
2474 }
2475 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2476
2477 test_32j() {
2478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2479         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2480         trap cleanup_test32_mount EXIT
2481         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2482         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2483         touch $DIR/$tdir/test_file
2484         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2485         cleanup_test32_mount
2486 }
2487 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2488
2489 test_32k() {
2490         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2491         rm -fr $DIR/$tdir
2492         trap cleanup_test32_mount EXIT
2493         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2494         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2495         test_mkdir -p $DIR/$tdir/d2
2496         touch $DIR/$tdir/d2/test_file || error
2497         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2498         cleanup_test32_mount
2499 }
2500 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2501
2502 test_32l() {
2503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2504         rm -fr $DIR/$tdir
2505         trap cleanup_test32_mount EXIT
2506         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2507         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2508         test_mkdir -p $DIR/$tdir/d2
2509         touch $DIR/$tdir/d2/test_file
2510         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2511         cleanup_test32_mount
2512 }
2513 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2514
2515 test_32m() {
2516         rm -fr $DIR/d32m
2517         test_mkdir -p $DIR/d32m/tmp
2518         TMP_DIR=$DIR/d32m/tmp
2519         ln -s $DIR $TMP_DIR/symlink11
2520         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2521         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2522         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2523 }
2524 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2525
2526 test_32n() {
2527         rm -fr $DIR/d32n
2528         test_mkdir -p $DIR/d32n/tmp
2529         TMP_DIR=$DIR/d32n/tmp
2530         ln -s $DIR $TMP_DIR/symlink11
2531         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2532         ls -l $DIR/d32n/tmp/symlink11  || error
2533         ls -l $DIR/d32n/symlink01 || error
2534 }
2535 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2536
2537 test_32o() {
2538         touch $DIR/$tfile
2539         test_mkdir -p $DIR/d32o/tmp
2540         TMP_DIR=$DIR/d32o/tmp
2541         ln -s $DIR/$tfile $TMP_DIR/symlink12
2542         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2543         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2544         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2545         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2546         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2547 }
2548 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2549
2550 test_32p() {
2551     log 32p_1
2552         rm -fr $DIR/d32p
2553     log 32p_2
2554         rm -f $DIR/$tfile
2555     log 32p_3
2556         touch $DIR/$tfile
2557     log 32p_4
2558         test_mkdir -p $DIR/d32p/tmp
2559     log 32p_5
2560         TMP_DIR=$DIR/d32p/tmp
2561     log 32p_6
2562         ln -s $DIR/$tfile $TMP_DIR/symlink12
2563     log 32p_7
2564         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2565     log 32p_8
2566         cat $DIR/d32p/tmp/symlink12 || error
2567     log 32p_9
2568         cat $DIR/d32p/symlink02 || error
2569     log 32p_10
2570 }
2571 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2572
2573 cleanup_testdir_mount() {
2574         trap 0
2575         $UMOUNT $DIR/$tdir
2576 }
2577
2578 test_32q() {
2579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2580         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2581         trap cleanup_testdir_mount EXIT
2582         test_mkdir -p $DIR/$tdir
2583         touch $DIR/$tdir/under_the_mount
2584         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2585         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2586         cleanup_testdir_mount
2587 }
2588 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2589
2590 test_32r() {
2591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2592         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2593         trap cleanup_testdir_mount EXIT
2594         test_mkdir -p $DIR/$tdir
2595         touch $DIR/$tdir/under_the_mount
2596         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2597         ls $DIR/$tdir | grep -q under_the_mount && error || true
2598         cleanup_testdir_mount
2599 }
2600 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2601
2602 test_33aa() {
2603         rm -f $DIR/$tfile
2604         touch $DIR/$tfile
2605         chmod 444 $DIR/$tfile
2606         chown $RUNAS_ID $DIR/$tfile
2607         log 33_1
2608         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2609         log 33_2
2610 }
2611 run_test 33aa "write file with mode 444 (should return error) ===="
2612
2613 test_33a() {
2614         rm -fr $DIR/d33
2615         test_mkdir -p $DIR/d33
2616         chown $RUNAS_ID $DIR/d33
2617         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2618         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2619                 error "open RDWR" || true
2620 }
2621 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2622
2623 test_33b() {
2624         rm -fr $DIR/d33
2625         test_mkdir -p $DIR/d33
2626         chown $RUNAS_ID $DIR/d33
2627         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2628 }
2629 run_test 33b "test open file with malformed flags (No panic)"
2630
2631 test_33c() {
2632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2633         local ostnum
2634         local ostname
2635         local write_bytes
2636         local all_zeros
2637
2638         remote_ost_nodsh && skip "remote OST with nodsh" && return
2639         all_zeros=:
2640         rm -fr $DIR/d33
2641         test_mkdir -p $DIR/d33
2642         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2643
2644         sync
2645         for ostnum in $(seq $OSTCOUNT); do
2646                 # test-framework's OST numbering is one-based, while Lustre's
2647                 # is zero-based
2648                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2649                 # Parsing llobdstat's output sucks; we could grep the /proc
2650                 # path, but that's likely to not be as portable as using the
2651                 # llobdstat utility.  So we parse lctl output instead.
2652                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2653                         obdfilter/$ostname/stats |
2654                         awk '/^write_bytes/ {print $7}' )
2655                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2656                 if (( ${write_bytes:-0} > 0 ))
2657                 then
2658                         all_zeros=false
2659                         break;
2660                 fi
2661         done
2662
2663         $all_zeros || return 0
2664
2665         # Write four bytes
2666         echo foo > $DIR/d33/bar
2667         # Really write them
2668         sync
2669
2670         # Total up write_bytes after writing.  We'd better find non-zeros.
2671         for ostnum in $(seq $OSTCOUNT); do
2672                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2673                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2674                         obdfilter/$ostname/stats |
2675                         awk '/^write_bytes/ {print $7}' )
2676                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2677                 if (( ${write_bytes:-0} > 0 ))
2678                 then
2679                         all_zeros=false
2680                         break;
2681                 fi
2682         done
2683
2684         if $all_zeros
2685         then
2686                 for ostnum in $(seq $OSTCOUNT); do
2687                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2688                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2689                         do_facet ost$ostnum lctl get_param -n \
2690                                 obdfilter/$ostname/stats
2691                 done
2692                 error "OST not keeping write_bytes stats (b22312)"
2693         fi
2694 }
2695 run_test 33c "test llobdstat and write_bytes"
2696
2697 test_33d() {
2698         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2699         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2700         local MDTIDX=1
2701         local remote_dir=$DIR/$tdir/remote_dir
2702
2703         test_mkdir -p $DIR/$tdir
2704         $LFS mkdir -i $MDTIDX $remote_dir ||
2705                 error "create remote directory failed"
2706
2707         touch $remote_dir/$tfile
2708         chmod 444 $remote_dir/$tfile
2709         chown $RUNAS_ID $remote_dir/$tfile
2710
2711         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2712
2713         chown $RUNAS_ID $remote_dir
2714         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2715                                         error "create" || true
2716         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2717                                     error "open RDWR" || true
2718         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2719 }
2720 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2721
2722 test_33e() {
2723         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2724
2725         mkdir $DIR/$tdir
2726
2727         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2728         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2729         mkdir $DIR/$tdir/local_dir
2730
2731         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2732         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2733         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2734
2735         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2736                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2737
2738         rmdir $DIR/$tdir/* || error "rmdir failed"
2739
2740         umask 777
2741         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2742         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2743         mkdir $DIR/$tdir/local_dir
2744
2745         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2746         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2747         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2748
2749         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2750                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2751
2752         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2753
2754         umask 000
2755         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2756         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2757         mkdir $DIR/$tdir/local_dir
2758
2759         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2760         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2761         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2762
2763         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2764                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2765 }
2766 run_test 33e "mkdir and striped directory should have same mode"
2767
2768 cleanup_33f() {
2769         trap 0
2770         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2771 }
2772
2773 test_33f() {
2774         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2775
2776         mkdir $DIR/$tdir
2777         chmod go+rwx $DIR/$tdir
2778         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2779         trap cleanup_33f EXIT
2780
2781         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2782                 error "cannot create striped directory"
2783
2784         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2785                 error "cannot create files in striped directory"
2786
2787         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2788                 error "cannot remove files in striped directory"
2789
2790         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2791                 error "cannot remove striped directory"
2792
2793         cleanup_33f
2794 }
2795 run_test 33f "nonroot user can create, access, and remove a striped directory"
2796
2797 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2798 test_34a() {
2799         rm -f $DIR/f34
2800         $MCREATE $DIR/f34 || error
2801         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2802         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2803         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2804         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2805 }
2806 run_test 34a "truncate file that has not been opened ==========="
2807
2808 test_34b() {
2809         [ ! -f $DIR/f34 ] && test_34a
2810         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2811         $OPENFILE -f O_RDONLY $DIR/f34
2812         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2813         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2814 }
2815 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2816
2817 test_34c() {
2818         [ ! -f $DIR/f34 ] && test_34a
2819         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2820         $OPENFILE -f O_RDWR $DIR/f34
2821         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2822         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2823 }
2824 run_test 34c "O_RDWR opening file-with-size works =============="
2825
2826 test_34d() {
2827         [ ! -f $DIR/f34 ] && test_34a
2828         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2829         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2830         rm $DIR/f34
2831 }
2832 run_test 34d "write to sparse file ============================="
2833
2834 test_34e() {
2835         rm -f $DIR/f34e
2836         $MCREATE $DIR/f34e || error
2837         $TRUNCATE $DIR/f34e 1000 || error
2838         $CHECKSTAT -s 1000 $DIR/f34e || error
2839         $OPENFILE -f O_RDWR $DIR/f34e
2840         $CHECKSTAT -s 1000 $DIR/f34e || error
2841 }
2842 run_test 34e "create objects, some with size and some without =="
2843
2844 test_34f() { # bug 6242, 6243
2845         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2846         SIZE34F=48000
2847         rm -f $DIR/f34f
2848         $MCREATE $DIR/f34f || error
2849         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2850         dd if=$DIR/f34f of=$TMP/f34f
2851         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2852         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2853         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2854         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2855         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2856 }
2857 run_test 34f "read from a file with no objects until EOF ======="
2858
2859 test_34g() {
2860         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2861         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2862         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2863         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2864         cancel_lru_locks osc
2865         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2866                 error "wrong size after lock cancel"
2867
2868         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2869         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2870                 error "expanding truncate failed"
2871         cancel_lru_locks osc
2872         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2873                 error "wrong expanded size after lock cancel"
2874 }
2875 run_test 34g "truncate long file ==============================="
2876
2877 test_34h() {
2878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2879         local gid=10
2880         local sz=1000
2881
2882         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2883         sync # Flush the cache so that multiop below does not block on cache
2884              # flush when getting the group lock
2885         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2886         MULTIPID=$!
2887
2888         # Since just timed wait is not good enough, let's do a sync write
2889         # that way we are sure enough time for a roundtrip + processing
2890         # passed + 2 seconds of extra margin.
2891         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2892         rm $DIR/${tfile}-1
2893         sleep 2
2894
2895         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2896                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2897                 kill -9 $MULTIPID
2898         fi
2899         wait $MULTIPID
2900         local nsz=`stat -c %s $DIR/$tfile`
2901         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2902 }
2903 run_test 34h "ftruncate file under grouplock should not block"
2904
2905 test_35a() {
2906         cp /bin/sh $DIR/f35a
2907         chmod 444 $DIR/f35a
2908         chown $RUNAS_ID $DIR/f35a
2909         $RUNAS $DIR/f35a && error || true
2910         rm $DIR/f35a
2911 }
2912 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2913
2914 test_36a() {
2915         rm -f $DIR/f36
2916         utime $DIR/f36 || error
2917 }
2918 run_test 36a "MDS utime check (mknod, utime) ==================="
2919
2920 test_36b() {
2921         echo "" > $DIR/f36
2922         utime $DIR/f36 || error
2923 }
2924 run_test 36b "OST utime check (open, utime) ===================="
2925
2926 test_36c() {
2927         rm -f $DIR/d36/f36
2928         test_mkdir $DIR/d36
2929         chown $RUNAS_ID $DIR/d36
2930         $RUNAS utime $DIR/d36/f36 || error
2931 }
2932 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2933
2934 test_36d() {
2935         [ ! -d $DIR/d36 ] && test_36c
2936         echo "" > $DIR/d36/f36
2937         $RUNAS utime $DIR/d36/f36 || error
2938 }
2939 run_test 36d "non-root OST utime check (open, utime) ==========="
2940
2941 test_36e() {
2942         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2943         test_mkdir -p $DIR/$tdir
2944         touch $DIR/$tdir/$tfile
2945         $RUNAS utime $DIR/$tdir/$tfile && \
2946                 error "utime worked, expected failure" || true
2947 }
2948 run_test 36e "utime on non-owned file (should return error) ===="
2949
2950 subr_36fh() {
2951         local fl="$1"
2952         local LANG_SAVE=$LANG
2953         local LC_LANG_SAVE=$LC_LANG
2954         export LANG=C LC_LANG=C # for date language
2955
2956         DATESTR="Dec 20  2000"
2957         test_mkdir -p $DIR/$tdir
2958         lctl set_param fail_loc=$fl
2959         date; date +%s
2960         cp /etc/hosts $DIR/$tdir/$tfile
2961         sync & # write RPC generated with "current" inode timestamp, but delayed
2962         sleep 1
2963         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2964         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2965         cancel_lru_locks osc
2966         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2967         date; date +%s
2968         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2969                 echo "BEFORE: $LS_BEFORE" && \
2970                 echo "AFTER : $LS_AFTER" && \
2971                 echo "WANT  : $DATESTR" && \
2972                 error "$DIR/$tdir/$tfile timestamps changed" || true
2973
2974         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2975 }
2976
2977 test_36f() {
2978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2979         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2980         subr_36fh "0x80000214"
2981 }
2982 run_test 36f "utime on file racing with OST BRW write =========="
2983
2984 test_36g() {
2985         remote_ost_nodsh && skip "remote OST with nodsh" && return
2986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2987         local fmd_max_age
2988         local fmd_before
2989         local fmd_after
2990
2991         test_mkdir -p $DIR/$tdir
2992         fmd_max_age=$(do_facet ost1 \
2993                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2994                 head -n 1")
2995
2996         fmd_before=$(do_facet ost1 \
2997                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2998         touch $DIR/$tdir/$tfile
2999         sleep $((fmd_max_age + 12))
3000         fmd_after=$(do_facet ost1 \
3001                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3002
3003         echo "fmd_before: $fmd_before"
3004         echo "fmd_after: $fmd_after"
3005         [[ $fmd_after -gt $fmd_before ]] &&
3006                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3007                 error "fmd didn't expire after ping" || true
3008 }
3009 run_test 36g "filter mod data cache expiry ====================="
3010
3011 test_36h() {
3012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3013         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3014         subr_36fh "0x80000227"
3015 }
3016 run_test 36h "utime on file racing with OST BRW write =========="
3017
3018 test_36i() {
3019         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3020
3021         test_mkdir $DIR/$tdir
3022         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3023
3024         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3025         local new_mtime=$((mtime + 200))
3026
3027         #change Modify time of striped dir
3028         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3029                         error "change mtime failed"
3030
3031         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3032
3033         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3034 }
3035 run_test 36i "change mtime on striped directory"
3036
3037 # test_37 - duplicate with tests 32q 32r
3038
3039 test_38() {
3040         local file=$DIR/$tfile
3041         touch $file
3042         openfile -f O_DIRECTORY $file
3043         local RC=$?
3044         local ENOTDIR=20
3045         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3046         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3047 }
3048 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3049
3050 test_39() {
3051         touch $DIR/$tfile
3052         touch $DIR/${tfile}2
3053 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3054 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3055 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3056         sleep 2
3057         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3058         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3059                 echo "mtime"
3060                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3061                 echo "atime"
3062                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3063                 echo "ctime"
3064                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3065                 error "O_TRUNC didn't change timestamps"
3066         fi
3067 }
3068 run_test 39 "mtime changed on create ==========================="
3069
3070 test_39b() {
3071         test_mkdir -p -c1 $DIR/$tdir
3072         cp -p /etc/passwd $DIR/$tdir/fopen
3073         cp -p /etc/passwd $DIR/$tdir/flink
3074         cp -p /etc/passwd $DIR/$tdir/funlink
3075         cp -p /etc/passwd $DIR/$tdir/frename
3076         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3077
3078         sleep 1
3079         echo "aaaaaa" >> $DIR/$tdir/fopen
3080         echo "aaaaaa" >> $DIR/$tdir/flink
3081         echo "aaaaaa" >> $DIR/$tdir/funlink
3082         echo "aaaaaa" >> $DIR/$tdir/frename
3083
3084         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3085         local link_new=`stat -c %Y $DIR/$tdir/flink`
3086         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3087         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3088
3089         cat $DIR/$tdir/fopen > /dev/null
3090         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3091         rm -f $DIR/$tdir/funlink2
3092         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3093
3094         for (( i=0; i < 2; i++ )) ; do
3095                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3096                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3097                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3098                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3099
3100                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3101                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3102                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3103                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3104
3105                 cancel_lru_locks osc
3106                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3107         done
3108 }
3109 run_test 39b "mtime change on open, link, unlink, rename  ======"
3110
3111 # this should be set to past
3112 TEST_39_MTIME=`date -d "1 year ago" +%s`
3113
3114 # bug 11063
3115 test_39c() {
3116         touch $DIR1/$tfile
3117         sleep 2
3118         local mtime0=`stat -c %Y $DIR1/$tfile`
3119
3120         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3121         local mtime1=`stat -c %Y $DIR1/$tfile`
3122         [ "$mtime1" = $TEST_39_MTIME ] || \
3123                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3124
3125         local d1=`date +%s`
3126         echo hello >> $DIR1/$tfile
3127         local d2=`date +%s`
3128         local mtime2=`stat -c %Y $DIR1/$tfile`
3129         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3130                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3131
3132         mv $DIR1/$tfile $DIR1/$tfile-1
3133
3134         for (( i=0; i < 2; i++ )) ; do
3135                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3136                 [ "$mtime2" = "$mtime3" ] || \
3137                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3138
3139                 cancel_lru_locks osc
3140                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3141         done
3142 }
3143 run_test 39c "mtime change on rename ==========================="
3144
3145 # bug 21114
3146 test_39d() {
3147         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3148         touch $DIR1/$tfile
3149
3150         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3151
3152         for (( i=0; i < 2; i++ )) ; do
3153                 local mtime=`stat -c %Y $DIR1/$tfile`
3154                 [ $mtime = $TEST_39_MTIME ] || \
3155                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3156
3157                 cancel_lru_locks osc
3158                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3159         done
3160 }
3161 run_test 39d "create, utime, stat =============================="
3162
3163 # bug 21114
3164 test_39e() {
3165         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3166         touch $DIR1/$tfile
3167         local mtime1=`stat -c %Y $DIR1/$tfile`
3168
3169         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3170
3171         for (( i=0; i < 2; i++ )) ; do
3172                 local mtime2=`stat -c %Y $DIR1/$tfile`
3173                 [ $mtime2 = $TEST_39_MTIME ] || \
3174                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3175
3176                 cancel_lru_locks osc
3177                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3178         done
3179 }
3180 run_test 39e "create, stat, utime, stat ========================"
3181
3182 # bug 21114
3183 test_39f() {
3184         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3185         touch $DIR1/$tfile
3186         mtime1=`stat -c %Y $DIR1/$tfile`
3187
3188         sleep 2
3189         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3190
3191         for (( i=0; i < 2; i++ )) ; do
3192                 local mtime2=`stat -c %Y $DIR1/$tfile`
3193                 [ $mtime2 = $TEST_39_MTIME ] || \
3194                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3195
3196                 cancel_lru_locks osc
3197                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3198         done
3199 }
3200 run_test 39f "create, stat, sleep, utime, stat ================="
3201
3202 # bug 11063
3203 test_39g() {
3204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3205         echo hello >> $DIR1/$tfile
3206         local mtime1=`stat -c %Y $DIR1/$tfile`
3207
3208         sleep 2
3209         chmod o+r $DIR1/$tfile
3210
3211         for (( i=0; i < 2; i++ )) ; do
3212                 local mtime2=`stat -c %Y $DIR1/$tfile`
3213                 [ "$mtime1" = "$mtime2" ] || \
3214                         error "lost mtime: $mtime2, should be $mtime1"
3215
3216                 cancel_lru_locks osc
3217                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3218         done
3219 }
3220 run_test 39g "write, chmod, stat ==============================="
3221
3222 # bug 11063
3223 test_39h() {
3224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3225         touch $DIR1/$tfile
3226         sleep 1
3227
3228         local d1=`date`
3229         echo hello >> $DIR1/$tfile
3230         local mtime1=`stat -c %Y $DIR1/$tfile`
3231
3232         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3233         local d2=`date`
3234         if [ "$d1" != "$d2" ]; then
3235                 echo "write and touch not within one second"
3236         else
3237                 for (( i=0; i < 2; i++ )) ; do
3238                         local mtime2=`stat -c %Y $DIR1/$tfile`
3239                         [ "$mtime2" = $TEST_39_MTIME ] || \
3240                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3241
3242                         cancel_lru_locks osc
3243                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3244                 done
3245         fi
3246 }
3247 run_test 39h "write, utime within one second, stat ============="
3248
3249 test_39i() {
3250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3251         touch $DIR1/$tfile
3252         sleep 1
3253
3254         echo hello >> $DIR1/$tfile
3255         local mtime1=`stat -c %Y $DIR1/$tfile`
3256
3257         mv $DIR1/$tfile $DIR1/$tfile-1
3258
3259         for (( i=0; i < 2; i++ )) ; do
3260                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3261
3262                 [ "$mtime1" = "$mtime2" ] || \
3263                         error "lost mtime: $mtime2, should be $mtime1"
3264
3265                 cancel_lru_locks osc
3266                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3267         done
3268 }
3269 run_test 39i "write, rename, stat =============================="
3270
3271 test_39j() {
3272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3273         start_full_debug_logging
3274         touch $DIR1/$tfile
3275         sleep 1
3276
3277         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3278         lctl set_param fail_loc=0x80000412
3279         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3280                 error "multiop failed"
3281         local multipid=$!
3282         local mtime1=`stat -c %Y $DIR1/$tfile`
3283
3284         mv $DIR1/$tfile $DIR1/$tfile-1
3285
3286         kill -USR1 $multipid
3287         wait $multipid || error "multiop close failed"
3288
3289         for (( i=0; i < 2; i++ )) ; do
3290                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3291                 [ "$mtime1" = "$mtime2" ] ||
3292                         error "mtime is lost on close: $mtime2, " \
3293                               "should be $mtime1"
3294
3295                 cancel_lru_locks osc
3296                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3297         done
3298         lctl set_param fail_loc=0
3299         stop_full_debug_logging
3300 }
3301 run_test 39j "write, rename, close, stat ======================="
3302
3303 test_39k() {
3304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3305         touch $DIR1/$tfile
3306         sleep 1
3307
3308         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3309         local multipid=$!
3310         local mtime1=`stat -c %Y $DIR1/$tfile`
3311
3312         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3313
3314         kill -USR1 $multipid
3315         wait $multipid || error "multiop close failed"
3316
3317         for (( i=0; i < 2; i++ )) ; do
3318                 local mtime2=`stat -c %Y $DIR1/$tfile`
3319
3320                 [ "$mtime2" = $TEST_39_MTIME ] || \
3321                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3322
3323                 cancel_lru_locks osc
3324                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3325         done
3326 }
3327 run_test 39k "write, utime, close, stat ========================"
3328
3329 # this should be set to future
3330 TEST_39_ATIME=`date -d "1 year" +%s`
3331
3332 test_39l() {
3333         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3334         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3335         local atime_diff=$(do_facet $SINGLEMDS \
3336                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3337         rm -rf $DIR/$tdir
3338         mkdir -p $DIR/$tdir
3339
3340         # test setting directory atime to future
3341         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3342         local atime=$(stat -c %X $DIR/$tdir)
3343         [ "$atime" = $TEST_39_ATIME ] || \
3344                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3345
3346         # test setting directory atime from future to now
3347         local d1=$(date +%s)
3348         ls $DIR/$tdir
3349         local d2=$(date +%s)
3350
3351         cancel_lru_locks mdc
3352         atime=$(stat -c %X $DIR/$tdir)
3353         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3354                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3355
3356         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3357         sleep 3
3358
3359         # test setting directory atime when now > dir atime + atime_diff
3360         d1=$(date +%s)
3361         ls $DIR/$tdir
3362         d2=$(date +%s)
3363         cancel_lru_locks mdc
3364         atime=$(stat -c %X $DIR/$tdir)
3365         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3366                 error "atime is not updated  : $atime, should be $d2"
3367
3368         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3369         sleep 3
3370
3371         # test not setting directory atime when now < dir atime + atime_diff
3372         ls $DIR/$tdir
3373         cancel_lru_locks mdc
3374         atime=$(stat -c %X $DIR/$tdir)
3375         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3376                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3377
3378         do_facet $SINGLEMDS \
3379                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3380 }
3381 run_test 39l "directory atime update ==========================="
3382
3383 test_39m() {
3384         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3385         touch $DIR1/$tfile
3386         sleep 2
3387         local far_past_mtime=$(date -d "May 29 1953" +%s)
3388         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3389
3390         touch -m -d @$far_past_mtime $DIR1/$tfile
3391         touch -a -d @$far_past_atime $DIR1/$tfile
3392
3393         for (( i=0; i < 2; i++ )) ; do
3394                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3395                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3396                         error "atime or mtime set incorrectly"
3397
3398                 cancel_lru_locks osc
3399                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3400         done
3401 }
3402 run_test 39m "test atime and mtime before 1970"
3403
3404 test_39n() { # LU-3832
3405         local atime_diff=$(do_facet $SINGLEMDS \
3406                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3407         local atime0
3408         local atime1
3409         local atime2
3410
3411         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3412
3413         rm -rf $DIR/$tfile
3414         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3415         atime0=$(stat -c %X $DIR/$tfile)
3416
3417         sleep 5
3418         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3419         atime1=$(stat -c %X $DIR/$tfile)
3420
3421         sleep 5
3422         cancel_lru_locks mdc
3423         cancel_lru_locks osc
3424         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3425         atime2=$(stat -c %X $DIR/$tfile)
3426
3427         do_facet $SINGLEMDS \
3428                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3429
3430         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3431         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3432 }
3433 run_test 39n "check that O_NOATIME is honored"
3434
3435 test_39o() {
3436         TESTDIR=$DIR/$tdir/$tfile
3437         [ -e $TESTDIR ] && rm -rf $TESTDIR
3438         mkdir -p $TESTDIR
3439         cd $TESTDIR
3440         links1=2
3441         ls
3442         mkdir a b
3443         ls
3444         links2=$(stat -c %h .)
3445         [ $(($links1 + 2)) != $links2 ] &&
3446                 error "wrong links count $(($links1 + 2)) != $links2"
3447         rmdir b
3448         links3=$(stat -c %h .)
3449         [ $(($links1 + 1)) != $links3 ] &&
3450                 error "wrong links count $links1 != $links3"
3451         return 0
3452 }
3453 run_test 39o "directory cached attributes updated after create ========"
3454
3455 test_39p() {
3456         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3457         local MDTIDX=1
3458         TESTDIR=$DIR/$tdir/$tfile
3459         [ -e $TESTDIR ] && rm -rf $TESTDIR
3460         test_mkdir -p $TESTDIR
3461         cd $TESTDIR
3462         links1=2
3463         ls
3464         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3465         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3466         ls
3467         links2=$(stat -c %h .)
3468         [ $(($links1 + 2)) != $links2 ] &&
3469                 error "wrong links count $(($links1 + 2)) != $links2"
3470         rmdir remote_dir2
3471         links3=$(stat -c %h .)
3472         [ $(($links1 + 1)) != $links3 ] &&
3473                 error "wrong links count $links1 != $links3"
3474         return 0
3475 }
3476 run_test 39p "remote directory cached attributes updated after create ========"
3477
3478
3479 test_40() {
3480         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3481         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3482                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3483         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3484                 error "$tfile is not 4096 bytes in size"
3485 }
3486 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3487
3488 test_41() {
3489         # bug 1553
3490         small_write $DIR/f41 18
3491 }
3492 run_test 41 "test small file write + fstat ====================="
3493
3494 count_ost_writes() {
3495         lctl get_param -n osc.*.stats |
3496                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3497                         END { printf("%0.0f", writes) }'
3498 }
3499
3500 # decent default
3501 WRITEBACK_SAVE=500
3502 DIRTY_RATIO_SAVE=40
3503 MAX_DIRTY_RATIO=50
3504 BG_DIRTY_RATIO_SAVE=10
3505 MAX_BG_DIRTY_RATIO=25
3506
3507 start_writeback() {
3508         trap 0
3509         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3510         # dirty_ratio, dirty_background_ratio
3511         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3512                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3513                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3514                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3515         else
3516                 # if file not here, we are a 2.4 kernel
3517                 kill -CONT `pidof kupdated`
3518         fi
3519 }
3520
3521 stop_writeback() {
3522         # setup the trap first, so someone cannot exit the test at the
3523         # exact wrong time and mess up a machine
3524         trap start_writeback EXIT
3525         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3526         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3527                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3528                 sysctl -w vm.dirty_writeback_centisecs=0
3529                 sysctl -w vm.dirty_writeback_centisecs=0
3530                 # save and increase /proc/sys/vm/dirty_ratio
3531                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3532                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3533                 # save and increase /proc/sys/vm/dirty_background_ratio
3534                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3535                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3536         else
3537                 # if file not here, we are a 2.4 kernel
3538                 kill -STOP `pidof kupdated`
3539         fi
3540 }
3541
3542 # ensure that all stripes have some grant before we test client-side cache
3543 setup_test42() {
3544         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3545                 dd if=/dev/zero of=$i bs=4k count=1
3546                 rm $i
3547         done
3548 }
3549
3550 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3551 # file truncation, and file removal.
3552 test_42a() {
3553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3554         setup_test42
3555         cancel_lru_locks osc
3556         stop_writeback
3557         sync; sleep 1; sync # just to be safe
3558         BEFOREWRITES=`count_ost_writes`
3559         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3560         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3561         AFTERWRITES=`count_ost_writes`
3562         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3563                 error "$BEFOREWRITES < $AFTERWRITES"
3564         start_writeback
3565 }
3566 run_test 42a "ensure that we don't flush on close =============="
3567
3568 test_42b() {
3569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3570         setup_test42
3571         cancel_lru_locks osc
3572         stop_writeback
3573         sync
3574         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3575         BEFOREWRITES=$(count_ost_writes)
3576         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3577         AFTERWRITES=$(count_ost_writes)
3578         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3579                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3580         fi
3581         BEFOREWRITES=$(count_ost_writes)
3582         sync || error "sync: $?"
3583         AFTERWRITES=$(count_ost_writes)
3584         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3585                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3586         fi
3587         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3588         start_writeback
3589         return 0
3590 }
3591 run_test 42b "test destroy of file with cached dirty data ======"
3592
3593 # if these tests just want to test the effect of truncation,
3594 # they have to be very careful.  consider:
3595 # - the first open gets a {0,EOF}PR lock
3596 # - the first write conflicts and gets a {0, count-1}PW
3597 # - the rest of the writes are under {count,EOF}PW
3598 # - the open for truncate tries to match a {0,EOF}PR
3599 #   for the filesize and cancels the PWs.
3600 # any number of fixes (don't get {0,EOF} on open, match
3601 # composite locks, do smarter file size management) fix
3602 # this, but for now we want these tests to verify that
3603 # the cancellation with truncate intent works, so we
3604 # start the file with a full-file pw lock to match against
3605 # until the truncate.
3606 trunc_test() {
3607         test=$1
3608         file=$DIR/$test
3609         offset=$2
3610         cancel_lru_locks osc
3611         stop_writeback
3612         # prime the file with 0,EOF PW to match
3613         touch $file
3614         $TRUNCATE $file 0
3615         sync; sync
3616         # now the real test..
3617         dd if=/dev/zero of=$file bs=1024 count=100
3618         BEFOREWRITES=`count_ost_writes`
3619         $TRUNCATE $file $offset
3620         cancel_lru_locks osc
3621         AFTERWRITES=`count_ost_writes`
3622         start_writeback
3623 }
3624
3625 test_42c() {
3626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3627         trunc_test 42c 1024
3628         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3629             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3630         rm $file
3631 }
3632 run_test 42c "test partial truncate of file with cached dirty data"
3633
3634 test_42d() {
3635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3636         trunc_test 42d 0
3637         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3638             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3639         rm $file
3640 }
3641 run_test 42d "test complete truncate of file with cached dirty data"
3642
3643 test_42e() { # bug22074
3644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3645         local TDIR=$DIR/${tdir}e
3646         local pagesz=$(page_size)
3647         local pages=16 # hardcoded 16 pages, don't change it.
3648         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3649         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3650         local max_dirty_mb
3651         local warmup_files
3652
3653         test_mkdir -p $DIR/${tdir}e
3654         $SETSTRIPE -c 1 $TDIR
3655         createmany -o $TDIR/f $files
3656
3657         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3658
3659         # we assume that with $OSTCOUNT files, at least one of them will
3660         # be allocated on OST0.
3661         warmup_files=$((OSTCOUNT * max_dirty_mb))
3662         createmany -o $TDIR/w $warmup_files
3663
3664         # write a large amount of data into one file and sync, to get good
3665         # avail_grant number from OST.
3666         for ((i=0; i<$warmup_files; i++)); do
3667                 idx=$($GETSTRIPE -i $TDIR/w$i)
3668                 [ $idx -ne 0 ] && continue
3669                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3670                 break
3671         done
3672         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3673         sync
3674         $LCTL get_param $proc_osc0/cur_dirty_bytes
3675         $LCTL get_param $proc_osc0/cur_grant_bytes
3676
3677         # create as much dirty pages as we can while not to trigger the actual
3678         # RPCs directly. but depends on the env, VFS may trigger flush during this
3679         # period, hopefully we are good.
3680         for ((i=0; i<$warmup_files; i++)); do
3681                 idx=$($GETSTRIPE -i $TDIR/w$i)
3682                 [ $idx -ne 0 ] && continue
3683                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3684         done
3685         $LCTL get_param $proc_osc0/cur_dirty_bytes
3686         $LCTL get_param $proc_osc0/cur_grant_bytes
3687
3688         # perform the real test
3689         $LCTL set_param $proc_osc0/rpc_stats 0
3690         for ((;i<$files; i++)); do
3691                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3692                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3693         done
3694         sync
3695         $LCTL get_param $proc_osc0/rpc_stats
3696
3697         local percent=0
3698         local have_ppr=false
3699         $LCTL get_param $proc_osc0/rpc_stats |
3700                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3701                         # skip lines until we are at the RPC histogram data
3702                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3703                         $have_ppr || continue
3704
3705                         # we only want the percent stat for < 16 pages
3706                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3707
3708                         percent=$((percent + WPCT))
3709                         if [[ $percent -gt 15 ]]; then
3710                                 error "less than 16-pages write RPCs" \
3711                                       "$percent% > 15%"
3712                                 break
3713                         fi
3714                 done
3715         rm -rf $TDIR
3716 }
3717 run_test 42e "verify sub-RPC writes are not done synchronously"
3718
3719 test_43() {
3720         test_mkdir -p $DIR/$tdir
3721         cp -p /bin/ls $DIR/$tdir/$tfile
3722         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3723         pid=$!
3724         # give multiop a chance to open
3725         sleep 1
3726
3727         $DIR/$tdir/$tfile && error || true
3728         kill -USR1 $pid
3729 }
3730 run_test 43 "execution of file opened for write should return -ETXTBSY"
3731
3732 test_43a() {
3733         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3734         test_mkdir -p $DIR/$tdir
3735         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3736                         cp -p multiop $DIR/$tdir/multiop
3737         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3738                         return 1
3739         MULTIOP_PID=$!
3740         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3741         kill -USR1 $MULTIOP_PID || return 2
3742         wait $MULTIOP_PID || return 3
3743         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3744 }
3745 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3746
3747 test_43b() {
3748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3749         test_mkdir -p $DIR/$tdir
3750         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3751                         cp -p multiop $DIR/$tdir/multiop
3752         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3753                         return 1
3754         MULTIOP_PID=$!
3755         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3756         kill -USR1 $MULTIOP_PID || return 2
3757         wait $MULTIOP_PID || return 3
3758         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3759 }
3760 run_test 43b "truncate of file being executed should return -ETXTBSY"
3761
3762 test_43c() {
3763         local testdir="$DIR/$tdir"
3764         test_mkdir -p $DIR/$tdir
3765         cp $SHELL $testdir/
3766         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3767                 ( cd $testdir && md5sum -c)
3768 }
3769 run_test 43c "md5sum of copy into lustre========================"
3770
3771 test_44() {
3772         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3773         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3774         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3775 }
3776 run_test 44 "zero length read from a sparse stripe ============="
3777
3778 test_44a() {
3779         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3780                 awk '{ print $2 }')
3781         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3782         [[ $nstripe -gt $OSTCOUNT ]] &&
3783             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3784             return
3785         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3786                 awk '{ print $2 }')
3787         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3788                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3789                         awk '{ print $2 }')
3790         fi
3791
3792         OFFSETS="0 $((stride/2)) $((stride-1))"
3793         for offset in $OFFSETS; do
3794                 for i in $(seq 0 $((nstripe-1))); do
3795                         local GLOBALOFFSETS=""
3796                         # size in Bytes
3797                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3798                         local myfn=$DIR/d44a-$size
3799                         echo "--------writing $myfn at $size"
3800                         ll_sparseness_write $myfn $size ||
3801                                 error "ll_sparseness_write"
3802                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3803                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3804                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3805
3806                         for j in $(seq 0 $((nstripe-1))); do
3807                                 # size in Bytes
3808                                 size=$((((j + $nstripe )*$stride + $offset)))
3809                                 ll_sparseness_write $myfn $size ||
3810                                         error "ll_sparseness_write"
3811                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3812                         done
3813                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3814                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3815                         rm -f $myfn
3816                 done
3817         done
3818 }
3819 run_test 44a "test sparse pwrite ==============================="
3820
3821 dirty_osc_total() {
3822         tot=0
3823         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3824                 tot=$(($tot + $d))
3825         done
3826         echo $tot
3827 }
3828 do_dirty_record() {
3829         before=`dirty_osc_total`
3830         echo executing "\"$*\""
3831         eval $*
3832         after=`dirty_osc_total`
3833         echo before $before, after $after
3834 }
3835 test_45() {
3836         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3837         f="$DIR/f45"
3838         # Obtain grants from OST if it supports it
3839         echo blah > ${f}_grant
3840         stop_writeback
3841         sync
3842         do_dirty_record "echo blah > $f"
3843         [[ $before -eq $after ]] && error "write wasn't cached"
3844         do_dirty_record "> $f"
3845         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3846         do_dirty_record "echo blah > $f"
3847         [[ $before -eq $after ]] && error "write wasn't cached"
3848         do_dirty_record "sync"
3849         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3850         do_dirty_record "echo blah > $f"
3851         [[ $before -eq $after ]] && error "write wasn't cached"
3852         do_dirty_record "cancel_lru_locks osc"
3853         [[ $before -gt $after ]] ||
3854                 error "lock cancellation didn't lower dirty count"
3855         start_writeback
3856 }
3857 run_test 45 "osc io page accounting ============================"
3858
3859 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3860 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3861 # objects offset and an assert hit when an rpc was built with 1023's mapped
3862 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3863 test_46() {
3864         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3865         f="$DIR/f46"
3866         stop_writeback
3867         sync
3868         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3869         sync
3870         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3871         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3872         sync
3873         start_writeback
3874 }
3875 run_test 46 "dirtying a previously written page ================"
3876
3877 # test_47 is removed "Device nodes check" is moved to test_28
3878
3879 test_48a() { # bug 2399
3880         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3881         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3882                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3883                 return
3884         test_mkdir $DIR/$tdir
3885         cd $DIR/$tdir
3886         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3887         test_mkdir $DIR/$tdir || error "recreate directory failed"
3888         touch foo || error "'touch foo' failed after recreating cwd"
3889         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
3890         touch .foo || error "'touch .foo' failed after recreating cwd"
3891         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3892         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3893         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3894         cd . || error "'cd .' failed after recreating cwd"
3895         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3896         rmdir . && error "'rmdir .' worked after recreating cwd"
3897         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3898         cd .. || error "'cd ..' failed after recreating cwd"
3899 }
3900 run_test 48a "Access renamed working dir (should return errors)="
3901
3902 test_48b() { # bug 2399
3903         rm -rf $DIR/$tdir
3904         test_mkdir $DIR/$tdir
3905         cd $DIR/$tdir
3906         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3907         touch foo && error "'touch foo' worked after removing cwd"
3908         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3909         touch .foo && error "'touch .foo' worked after removing cwd"
3910         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3911         ls . > /dev/null && error "'ls .' worked after removing cwd"
3912         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3913         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3914         test_mkdir . && error "'mkdir .' worked after removing cwd"
3915         rmdir . && error "'rmdir .' worked after removing cwd"
3916         ln -s . foo && error "'ln -s .' worked after removing cwd"
3917         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3918 }
3919 run_test 48b "Access removed working dir (should return errors)="
3920
3921 test_48c() { # bug 2350
3922         #lctl set_param debug=-1
3923         #set -vx
3924         rm -rf $DIR/$tdir
3925         test_mkdir -p $DIR/$tdir/dir
3926         cd $DIR/$tdir/dir
3927         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3928         $TRACE touch foo && error "touch foo worked after removing cwd"
3929         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3930         touch .foo && error "touch .foo worked after removing cwd"
3931         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3932         $TRACE ls . && error "'ls .' worked after removing cwd"
3933         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3934         is_patchless || ( $TRACE cd . &&
3935                         error "'cd .' worked after removing cwd" )
3936         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3937         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3938         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3939         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3940 }
3941 run_test 48c "Access removed working subdir (should return errors)"
3942
3943 test_48d() { # bug 2350
3944         #lctl set_param debug=-1
3945         #set -vx
3946         rm -rf $DIR/$tdir
3947         test_mkdir -p $DIR/$tdir/dir
3948         cd $DIR/$tdir/dir
3949         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3950         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3951         $TRACE touch foo && error "'touch foo' worked after removing parent"
3952         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3953         touch .foo && error "'touch .foo' worked after removing parent"
3954         test_mkdir .foo && error "mkdir .foo worked after removing parent"
3955         $TRACE ls . && error "'ls .' worked after removing parent"
3956         $TRACE ls .. && error "'ls ..' worked after removing parent"
3957         is_patchless || ( $TRACE cd . &&
3958                         error "'cd .' worked after recreate parent" )
3959         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3960         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3961         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3962         is_patchless || ( $TRACE cd .. &&
3963                         error "'cd ..' worked after removing parent" || true )
3964 }
3965 run_test 48d "Access removed parent subdir (should return errors)"
3966
3967 test_48e() { # bug 4134
3968         #lctl set_param debug=-1
3969         #set -vx
3970         rm -rf $DIR/$tdir
3971         test_mkdir -p $DIR/$tdir/dir
3972         cd $DIR/$tdir/dir
3973         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3974         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3975         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3976         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3977         # On a buggy kernel addition of "touch foo" after cd .. will
3978         # produce kernel oops in lookup_hash_it
3979         touch ../foo && error "'cd ..' worked after recreate parent"
3980         cd $DIR
3981         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3982 }
3983 run_test 48e "Access to recreated parent subdir (should return errors)"
3984
3985 test_49() { # LU-1030
3986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3987         # get ost1 size - lustre-OST0000
3988         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3989                 awk '{ print $4 }')
3990         # write 800M at maximum
3991         [[ $ost1_size -lt 2 ]] && ost1_size=2
3992         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3993
3994         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3995         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3996         local dd_pid=$!
3997
3998         # change max_pages_per_rpc while writing the file
3999         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4000         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4001         # loop until dd process exits
4002         while ps ax -opid | grep -wq $dd_pid; do
4003                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4004                 sleep $((RANDOM % 5 + 1))
4005         done
4006         # restore original max_pages_per_rpc
4007         $LCTL set_param $osc1_mppc=$orig_mppc
4008         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4009 }
4010 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4011
4012 test_50() {
4013         # bug 1485
4014         test_mkdir $DIR/$tdir
4015         cd $DIR/$tdir
4016         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4017 }
4018 run_test 50 "special situations: /proc symlinks  ==============="
4019
4020 test_51a() {    # was test_51
4021         # bug 1516 - create an empty entry right after ".." then split dir
4022         test_mkdir -c1 $DIR/$tdir
4023         touch $DIR/$tdir/foo
4024         $MCREATE $DIR/$tdir/bar
4025         rm $DIR/$tdir/foo
4026         createmany -m $DIR/$tdir/longfile 201
4027         FNUM=202
4028         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4029                 $MCREATE $DIR/$tdir/longfile$FNUM
4030                 FNUM=$(($FNUM + 1))
4031                 echo -n "+"
4032         done
4033         echo
4034         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4035 }
4036 run_test 51a "special situations: split htree with empty entry =="
4037
4038 export NUMTEST=70000
4039 test_51b() {
4040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4041         local BASE=$DIR/d${base}.${TESTSUITE}
4042
4043         # cleanup the directory
4044         rm -fr $BASE
4045
4046         test_mkdir -p -c1 $BASE
4047
4048         $LFS df
4049         $LFS df -i
4050         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4051         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4052         [[ $numfree -lt 21000 ]] &&
4053                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4054                 return
4055
4056         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4057                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4058
4059         # need to check free space for the directories as well
4060         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4061         numfree=$((blkfree / 4))
4062         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4063                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4064
4065         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4066         {
4067                 $LFS df
4068                 $LFS df -i
4069                 echo "failed" > $BASE/fnum
4070                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4071         }
4072 }
4073 run_test 51b "exceed 64k subdirectory nlink limit"
4074
4075 test_51ba() { # LU-993
4076         local BASE=$DIR/d${base}.${TESTSUITE}
4077         # unlink all but 100 subdirectories, then check it still works
4078         local LEFT=100
4079         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4080
4081         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4082         local DELETE=$((NUMTEST - LEFT))
4083
4084         # continue on to run this test even if 51b didn't finish,
4085         # just to delete the many subdirectories created.
4086         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4087
4088         # for ldiskfs the nlink count should be 1, but this is OSD specific
4089         # and so this is listed for informational purposes only
4090         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4091         unlinkmany -d $BASE/d $DELETE
4092         RC=$?
4093
4094         if [ $RC -ne 0 ]; then
4095                 if [ "$NUMPREV" == "failed" ]; then
4096                         skip "previous setup failed"
4097                         return 0
4098                 else
4099                         error "unlink of first $DELETE subdirs failed"
4100                         return $RC
4101                 fi
4102         fi
4103
4104         echo "nlink between: $(stat -c %h $BASE)"
4105         # trim the first line of ls output
4106         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4107         [ $FOUND -ne $LEFT ] &&
4108                 error "can't find subdirs: found only $FOUND/$LEFT"
4109
4110         unlinkmany -d $BASE/d $DELETE $LEFT ||
4111                 error "unlink of second $LEFT subdirs failed"
4112         # regardless of whether the backing filesystem tracks nlink accurately
4113         # or not, the nlink count shouldn't be more than "." and ".." here
4114         local AFTER=$(stat -c %h $BASE)
4115         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4116                 echo "nlink after: $AFTER"
4117 }
4118 run_test 51ba "verify nlink for many subdirectory cleanup"
4119
4120 test_51d() {
4121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4122         [[ $OSTCOUNT -lt 3 ]] &&
4123                 skip_env "skipping test with few OSTs" && return
4124         test_mkdir -p $DIR/$tdir
4125         createmany -o $DIR/$tdir/t- 1000
4126         $GETSTRIPE $DIR/$tdir > $TMP/files
4127         for N in $(seq 0 $((OSTCOUNT - 1))); do
4128                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4129                         END { printf("%0.0f", objs) }' $TMP/files)
4130                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4131                         '($1 == '$N') { objs += 1 } \
4132                         END { printf("%0.0f", objs) }')
4133                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4134         done
4135         unlinkmany $DIR/$tdir/t- 1000
4136
4137         NLAST=0
4138         for N in $(seq 1 $((OSTCOUNT - 1))); do
4139                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4140                         error "OST $N has less objects vs OST $NLAST" \
4141                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4142                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4143                         error "OST $N has less objects vs OST $NLAST" \
4144                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4145
4146                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4147                         error "OST $N has less #0 objects vs OST $NLAST" \
4148                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4149                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4150                         error "OST $N has less #0 objects vs OST $NLAST" \
4151                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4152                 NLAST=$N
4153         done
4154 }
4155 run_test 51d "check object distribution ===================="
4156
4157 test_51e() {
4158         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4159                 skip "Only applicable to ldiskfs-based MDTs"
4160                 return
4161         fi
4162
4163         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4164         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4165
4166         touch $DIR/$tdir/d0/foo
4167         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4168                 error "file exceed 65000 nlink limit!"
4169         unlinkmany $DIR/$tdir/d0/f- 65001
4170         return 0
4171 }
4172 run_test 51e "check file nlink limit"
4173
4174 test_52a() {
4175         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4176         test_mkdir -p $DIR/$tdir
4177         touch $DIR/$tdir/foo
4178         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4179         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4180         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4181         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4182         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4183                                         error "link worked"
4184         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4185         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4186         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4187                                                      error "lsattr"
4188         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4189         cp -r $DIR/$tdir /tmp/
4190         rm -fr $DIR/$tdir || error "cleanup rm failed"
4191 }
4192 run_test 52a "append-only flag test (should return errors) ====="
4193
4194 test_52b() {
4195         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4196         test_mkdir -p $DIR/$tdir
4197         touch $DIR/$tdir/foo
4198         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4199         cat test > $DIR/$tdir/foo && error "cat test worked"
4200         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4201         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4202         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4203                                         error "link worked"
4204         echo foo >> $DIR/$tdir/foo && error "echo worked"
4205         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4206         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4207         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4208         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4209                                                         error "lsattr"
4210         chattr -i $DIR/$tdir/foo || error "chattr failed"
4211
4212         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4213 }
4214 run_test 52b "immutable flag test (should return errors) ======="
4215
4216 test_53() {
4217         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4218         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4219         remote_ost_nodsh && skip "remote OST with nodsh" && return
4220
4221         local param
4222         local param_seq
4223         local ostname
4224         local mds_last
4225         local mds_last_seq
4226         local ost_last
4227         local ost_last_seq
4228         local ost_last_id
4229         local ostnum
4230         local node
4231         local found=false
4232         local support_last_seq=true
4233
4234         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4235                 support_last_seq=false
4236
4237         # only test MDT0000
4238         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4239         local value
4240         for value in $(do_facet $SINGLEMDS \
4241                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4242                 param=$(echo ${value[0]} | cut -d "=" -f1)
4243                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4244
4245                 if $support_last_seq; then
4246                         param_seq=$(echo $param |
4247                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4248                         mds_last_seq=$(do_facet $SINGLEMDS \
4249                                        $LCTL get_param -n $param_seq)
4250                 fi
4251                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4252
4253                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4254                 node=$(facet_active_host ost$((ostnum+1)))
4255                 param="obdfilter.$ostname.last_id"
4256                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4257                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4258                         ost_last_id=$ost_last
4259
4260                         if $support_last_seq; then
4261                                 ost_last_id=$(echo $ost_last |
4262                                               awk -F':' '{print $2}' |
4263                                               sed -e "s/^0x//g")
4264                                 ost_last_seq=$(echo $ost_last |
4265                                                awk -F':' '{print $1}')
4266                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4267                         fi
4268
4269                         if [[ $ost_last_id != $mds_last ]]; then
4270                                 error "$ost_last_id != $mds_last"
4271                         else
4272                                 found=true
4273                                 break
4274                         fi
4275                 done
4276         done
4277         $found || error "can not match last_seq/last_id for $mdtosc"
4278         return 0
4279 }
4280 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4281
4282 test_54a() {
4283         $SOCKETSERVER $DIR/socket ||
4284                 error "$SOCKETSERVER $DIR/socket failed: $?"
4285         $SOCKETCLIENT $DIR/socket ||
4286                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4287         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4288 }
4289 run_test 54a "unix domain socket test =========================="
4290
4291 test_54b() {
4292         f="$DIR/f54b"
4293         mknod $f c 1 3
4294         chmod 0666 $f
4295         dd if=/dev/zero of=$f bs=$(page_size) count=1
4296 }
4297 run_test 54b "char device works in lustre ======================"
4298
4299 find_loop_dev() {
4300         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4301         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4302         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4303
4304         for i in $(seq 3 7); do
4305                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4306                 LOOPDEV=$LOOPBASE$i
4307                 LOOPNUM=$i
4308                 break
4309         done
4310 }
4311
4312 cleanup_54c() {
4313         loopdev="$DIR/loop54c"
4314
4315         trap 0
4316         $UMOUNT $DIR/$tdir || rc=$?
4317         losetup -d $loopdev || true
4318         losetup -d $LOOPDEV || true
4319         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4320         return $rc
4321 }
4322
4323 test_54c() {
4324         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4325         loopdev="$DIR/loop54c"
4326
4327         find_loop_dev
4328         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4329         trap cleanup_54c EXIT
4330         mknod $loopdev b 7 $LOOPNUM
4331         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4332         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4333         losetup $loopdev $DIR/$tfile ||
4334                 error "can't set up $loopdev for $DIR/$tfile"
4335         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4336         test_mkdir -p $DIR/$tdir
4337         mount -t ext2 $loopdev $DIR/$tdir ||
4338                 error "error mounting $loopdev on $DIR/$tdir"
4339         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4340                 error "dd write"
4341         df $DIR/$tdir
4342         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4343                 error "dd read"
4344         cleanup_54c
4345 }
4346 run_test 54c "block device works in lustre ====================="
4347
4348 test_54d() {
4349         f="$DIR/f54d"
4350         string="aaaaaa"
4351         mknod $f p
4352         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4353 }
4354 run_test 54d "fifo device works in lustre ======================"
4355
4356 test_54e() {
4357         f="$DIR/f54e"
4358         string="aaaaaa"
4359         cp -aL /dev/console $f
4360         echo $string > $f || error "echo $string to $f failed"
4361 }
4362 run_test 54e "console/tty device works in lustre ======================"
4363
4364 #The test_55 used to be iopen test and it was removed by bz#24037.
4365 #run_test 55 "check iopen_connect_dentry() ======================"
4366
4367 test_56a() {    # was test_56
4368         rm -rf $DIR/$tdir
4369         $SETSTRIPE -d $DIR
4370         test_mkdir -p $DIR/$tdir/dir
4371         NUMFILES=3
4372         NUMFILESx2=$(($NUMFILES * 2))
4373         for i in $(seq 1 $NUMFILES); do
4374                 touch $DIR/$tdir/file$i
4375                 touch $DIR/$tdir/dir/file$i
4376         done
4377
4378         # test lfs getstripe with --recursive
4379         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4380         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4381                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4382         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4383         [[ $FILENUM -eq $NUMFILES ]] ||
4384                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4385         echo "$GETSTRIPE --recursive passed."
4386
4387         # test lfs getstripe with file instead of dir
4388         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4389         [[ $FILENUM -eq 1 ]] ||
4390                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4391         echo "$GETSTRIPE file1 passed."
4392
4393         #test lfs getstripe with --verbose
4394         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4395                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4396                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4397         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4398                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4399         echo "$GETSTRIPE --verbose passed."
4400
4401         #test lfs getstripe with --obd
4402         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4403                 grep -q "unknown obduuid" ||
4404                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4405
4406         [[ $OSTCOUNT -lt 2 ]] &&
4407                 skip_env "skipping other $GETSTRIPE --obd test" && return
4408
4409         OSTIDX=1
4410         OBDUUID=$(ostuuid_from_index $OSTIDX)
4411         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4412         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4413         [[ $FOUND -eq $FILENUM ]] ||
4414                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4415         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4416                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4417                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4418                 error "$GETSTRIPE --obd: should not show file on other obd"
4419         echo "$GETSTRIPE --obd passed"
4420 }
4421 run_test 56a "check $GETSTRIPE"
4422
4423 NUMFILES=3
4424 NUMDIRS=3
4425 setup_56() {
4426         local LOCAL_NUMFILES="$1"
4427         local LOCAL_NUMDIRS="$2"
4428         local MKDIR_PARAMS="$3"
4429         local DIR_STRIPE_PARAMS="$4"
4430
4431         if [ ! -d "$TDIR" ] ; then
4432                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4433                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4434                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4435                         touch $TDIR/file$i
4436                 done
4437                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4438                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4439                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4440                                 touch $TDIR/dir$i/file$j
4441                         done
4442                 done
4443         fi
4444 }
4445
4446 setup_56_special() {
4447         LOCAL_NUMFILES=$1
4448         LOCAL_NUMDIRS=$2
4449         setup_56 $1 $2
4450         if [ ! -e "$TDIR/loop1b" ] ; then
4451                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4452                         mknod $TDIR/loop${i}b b 7 $i
4453                         mknod $TDIR/null${i}c c 1 3
4454                         ln -s $TDIR/file1 $TDIR/link${i}l
4455                 done
4456                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4457                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4458                         mknod $TDIR/dir$i/null${i}c c 1 3
4459                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4460                 done
4461         fi
4462 }
4463
4464 test_56g() {
4465         $SETSTRIPE -d $DIR
4466
4467         TDIR=$DIR/${tdir}g
4468         setup_56 $NUMFILES $NUMDIRS
4469
4470         EXPECTED=$(($NUMDIRS + 2))
4471         # test lfs find with -name
4472         for i in $(seq 1 $NUMFILES) ; do
4473                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4474                 [ $NUMS -eq $EXPECTED ] ||
4475                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4476                               "found $NUMS, expected $EXPECTED"
4477         done
4478 }
4479 run_test 56g "check lfs find -name ============================="
4480
4481 test_56h() {
4482         $SETSTRIPE -d $DIR
4483
4484         TDIR=$DIR/${tdir}g
4485         setup_56 $NUMFILES $NUMDIRS
4486
4487         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4488         # test lfs find with ! -name
4489         for i in $(seq 1 $NUMFILES) ; do
4490                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4491                 [ $NUMS -eq $EXPECTED ] ||
4492                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4493                               "found $NUMS, expected $EXPECTED"
4494         done
4495 }
4496 run_test 56h "check lfs find ! -name ============================="
4497
4498 test_56i() {
4499        tdir=${tdir}i
4500        test_mkdir -p $DIR/$tdir
4501        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4502        CMD="$LFIND -ost $UUID $DIR/$tdir"
4503        OUT=$($CMD)
4504        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4505 }
4506 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4507
4508 test_56j() {
4509         TDIR=$DIR/${tdir}g
4510         setup_56_special $NUMFILES $NUMDIRS
4511
4512         EXPECTED=$((NUMDIRS + 1))
4513         CMD="$LFIND -type d $TDIR"
4514         NUMS=$($CMD | wc -l)
4515         [ $NUMS -eq $EXPECTED ] ||
4516                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4517 }
4518 run_test 56j "check lfs find -type d ============================="
4519
4520 test_56k() {
4521         TDIR=$DIR/${tdir}g
4522         setup_56_special $NUMFILES $NUMDIRS
4523
4524         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4525         CMD="$LFIND -type f $TDIR"
4526         NUMS=$($CMD | wc -l)
4527         [ $NUMS -eq $EXPECTED ] ||
4528                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4529 }
4530 run_test 56k "check lfs find -type f ============================="
4531
4532 test_56l() {
4533         TDIR=$DIR/${tdir}g
4534         setup_56_special $NUMFILES $NUMDIRS
4535
4536         EXPECTED=$((NUMDIRS + NUMFILES))
4537         CMD="$LFIND -type b $TDIR"
4538         NUMS=$($CMD | wc -l)
4539         [ $NUMS -eq $EXPECTED ] ||
4540                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4541 }
4542 run_test 56l "check lfs find -type b ============================="
4543
4544 test_56m() {
4545         TDIR=$DIR/${tdir}g
4546         setup_56_special $NUMFILES $NUMDIRS
4547
4548         EXPECTED=$((NUMDIRS + NUMFILES))
4549         CMD="$LFIND -type c $TDIR"
4550         NUMS=$($CMD | wc -l)
4551         [ $NUMS -eq $EXPECTED ] ||
4552                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4553 }
4554 run_test 56m "check lfs find -type c ============================="
4555
4556 test_56n() {
4557         TDIR=$DIR/${tdir}g
4558         setup_56_special $NUMFILES $NUMDIRS
4559
4560         EXPECTED=$((NUMDIRS + NUMFILES))
4561         CMD="$LFIND -type l $TDIR"
4562         NUMS=$($CMD | wc -l)
4563         [ $NUMS -eq $EXPECTED ] ||
4564                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4565 }
4566 run_test 56n "check lfs find -type l ============================="
4567
4568 test_56o() {
4569         TDIR=$DIR/${tdir}o
4570         setup_56 $NUMFILES $NUMDIRS
4571         utime $TDIR/file1 > /dev/null || error "utime (1)"
4572         utime $TDIR/file2 > /dev/null || error "utime (2)"
4573         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4574         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4575         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4576         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4577
4578         EXPECTED=4
4579         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4580         [ $NUMS -eq $EXPECTED ] || \
4581                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4582
4583         EXPECTED=12
4584         CMD="$LFIND -mtime 0 $TDIR"
4585         NUMS=$($CMD | wc -l)
4586         [ $NUMS -eq $EXPECTED ] ||
4587                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4588 }
4589 run_test 56o "check lfs find -mtime for old files =========================="
4590
4591 test_56p() {
4592         [ $RUNAS_ID -eq $UID ] &&
4593                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4594
4595         TDIR=$DIR/${tdir}p
4596         setup_56 $NUMFILES $NUMDIRS
4597
4598         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4599         EXPECTED=$NUMFILES
4600         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4601         NUMS=$($CMD | wc -l)
4602         [ $NUMS -eq $EXPECTED ] || \
4603                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4604
4605         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4606         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4607         NUMS=$($CMD | wc -l)
4608         [ $NUMS -eq $EXPECTED ] || \
4609                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4610 }
4611 run_test 56p "check lfs find -uid and ! -uid ==============================="
4612
4613 test_56q() {
4614         [ $RUNAS_ID -eq $UID ] &&
4615                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4616
4617         TDIR=$DIR/${tdir}q
4618         setup_56 $NUMFILES $NUMDIRS
4619
4620         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4621
4622         EXPECTED=$NUMFILES
4623         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4624         NUMS=$($CMD | wc -l)
4625         [ $NUMS -eq $EXPECTED ] ||
4626                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4627
4628         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4629         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4630         NUMS=$($CMD | wc -l)
4631         [ $NUMS -eq $EXPECTED ] ||
4632                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4633 }
4634 run_test 56q "check lfs find -gid and ! -gid ==============================="
4635
4636 test_56r() {
4637         TDIR=$DIR/${tdir}r
4638         setup_56 $NUMFILES $NUMDIRS
4639
4640         EXPECTED=12
4641         CMD="$LFIND -size 0 -type f $TDIR"
4642         NUMS=$($CMD | wc -l)
4643         [ $NUMS -eq $EXPECTED ] ||
4644                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4645         EXPECTED=0
4646         CMD="$LFIND ! -size 0 -type f $TDIR"
4647         NUMS=$($CMD | wc -l)
4648         [ $NUMS -eq $EXPECTED ] ||
4649                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4650         echo "test" > $TDIR/$tfile
4651         echo "test2" > $TDIR/$tfile.2 && sync
4652         EXPECTED=1
4653         CMD="$LFIND -size 5 -type f $TDIR"
4654         NUMS=$($CMD | wc -l)
4655         [ $NUMS -eq $EXPECTED ] ||
4656                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4657         EXPECTED=1
4658         CMD="$LFIND -size +5 -type f $TDIR"
4659         NUMS=$($CMD | wc -l)
4660         [ $NUMS -eq $EXPECTED ] ||
4661                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4662         EXPECTED=2
4663         CMD="$LFIND -size +0 -type f $TDIR"
4664         NUMS=$($CMD | wc -l)
4665         [ $NUMS -eq $EXPECTED ] ||
4666                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4667         EXPECTED=2
4668         CMD="$LFIND ! -size -5 -type f $TDIR"
4669         NUMS=$($CMD | wc -l)
4670         [ $NUMS -eq $EXPECTED ] ||
4671                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4672         EXPECTED=12
4673         CMD="$LFIND -size -5 -type f $TDIR"
4674         NUMS=$($CMD | wc -l)
4675         [ $NUMS -eq $EXPECTED ] ||
4676                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4677 }
4678 run_test 56r "check lfs find -size works =========================="
4679
4680 test_56s() { # LU-611
4681         TDIR=$DIR/${tdir}s
4682         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4683
4684         if [[ $OSTCOUNT -gt 1 ]]; then
4685                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4686                 ONESTRIPE=4
4687                 EXTRA=4
4688         else
4689                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4690                 EXTRA=0
4691         fi
4692
4693         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4694         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4695         NUMS=$($CMD | wc -l)
4696         [ $NUMS -eq $EXPECTED ] || {
4697                 $GETSTRIPE -R $TDIR
4698                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4699         }
4700
4701         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4702         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4703         NUMS=$($CMD | wc -l)
4704         [ $NUMS -eq $EXPECTED ] || {
4705                 $GETSTRIPE -R $TDIR
4706                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4707         }
4708
4709         EXPECTED=$ONESTRIPE
4710         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4711         NUMS=$($CMD | wc -l)
4712         [ $NUMS -eq $EXPECTED ] || {
4713                 $GETSTRIPE -R $TDIR
4714                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4715         }
4716
4717         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4718         NUMS=$($CMD | wc -l)
4719         [ $NUMS -eq $EXPECTED ] || {
4720                 $GETSTRIPE -R $TDIR
4721                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4722         }
4723
4724         EXPECTED=0
4725         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4726         NUMS=$($CMD | wc -l)
4727         [ $NUMS -eq $EXPECTED ] || {
4728                 $GETSTRIPE -R $TDIR
4729                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4730         }
4731 }
4732 run_test 56s "check lfs find -stripe-count works"
4733
4734 test_56t() { # LU-611
4735         TDIR=$DIR/${tdir}t
4736         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4737
4738         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4739
4740         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4741         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4742         NUMS=$($CMD | wc -l)
4743         [ $NUMS -eq $EXPECTED ] ||
4744                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4745
4746         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4747         NUMS=$($CMD | wc -l)
4748         [ $NUMS -eq $EXPECTED ] ||
4749                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4750
4751         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4752         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4753         NUMS=$($CMD | wc -l)
4754         [ $NUMS -eq $EXPECTED ] ||
4755                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4756
4757         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4758         NUMS=$($CMD | wc -l)
4759         [ $NUMS -eq $EXPECTED ] ||
4760                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4761
4762         EXPECTED=4
4763         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4764         NUMS=$($CMD | wc -l)
4765         [ $NUMS -eq $EXPECTED ] ||
4766                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4767
4768         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4769         NUMS=$($CMD | wc -l)
4770         [ $NUMS -eq $EXPECTED ] ||
4771                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4772
4773         EXPECTED=0
4774         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4775         NUMS=$($CMD | wc -l)
4776         [ $NUMS -eq $EXPECTED ] ||
4777                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4778 }
4779 run_test 56t "check lfs find -stripe-size works"
4780
4781 test_56u() { # LU-611
4782         TDIR=$DIR/${tdir}u
4783         setup_56 $NUMFILES $NUMDIRS "-i 0"
4784
4785         if [[ $OSTCOUNT -gt 1 ]]; then
4786                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4787                 ONESTRIPE=4
4788         else
4789                 ONESTRIPE=0
4790         fi
4791
4792         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4793         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4794         NUMS=$($CMD | wc -l)
4795         [ $NUMS -eq $EXPECTED ] ||
4796                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4797
4798         EXPECTED=$ONESTRIPE
4799         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4800         NUMS=$($CMD | wc -l)
4801         [ $NUMS -eq $EXPECTED ] ||
4802                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4803
4804         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4805         NUMS=$($CMD | wc -l)
4806         [ $NUMS -eq $EXPECTED ] ||
4807                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4808
4809         EXPECTED=0
4810         # This should produce an error and not return any files
4811         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4812         NUMS=$($CMD 2>/dev/null | wc -l)
4813         [ $NUMS -eq $EXPECTED ] ||
4814                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4815
4816         if [[ $OSTCOUNT -gt 1 ]]; then
4817                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4818                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4819                 NUMS=$($CMD | wc -l)
4820                 [ $NUMS -eq $EXPECTED ] ||
4821                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4822         fi
4823 }
4824 run_test 56u "check lfs find -stripe-index works"
4825
4826 test_56v() {
4827     local MDT_IDX=0
4828
4829     TDIR=$DIR/${tdir}v
4830     rm -rf $TDIR
4831     setup_56 $NUMFILES $NUMDIRS
4832
4833     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4834     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4835
4836     for file in $($LFIND -mdt $UUID $TDIR); do
4837         file_mdt_idx=$($GETSTRIPE -M $file)
4838         [ $file_mdt_idx -eq $MDT_IDX ] ||
4839             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4840     done
4841 }
4842 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4843
4844 test_56w() {
4845         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4846                 return
4847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4848         TDIR=$DIR/${tdir}w
4849
4850     rm -rf $TDIR || error "remove $TDIR failed"
4851     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4852
4853     local stripe_size
4854     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4855         error "$GETSTRIPE -S -d $TDIR failed"
4856     stripe_size=${stripe_size%% *}
4857
4858     local file_size=$((stripe_size * OSTCOUNT))
4859     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4860     local required_space=$((file_num * file_size))
4861     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4862     [[ $free_space -le $((required_space / 1024)) ]] &&
4863         skip_env "need at least $required_space bytes free space," \
4864                  "have $free_space kbytes" && return
4865
4866     local dd_bs=65536
4867     local dd_count=$((file_size / dd_bs))
4868
4869     # write data into the files
4870     local i
4871     local j
4872     local file
4873     for i in $(seq 1 $NUMFILES); do
4874         file=$TDIR/file$i
4875         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4876             error "write data into $file failed"
4877     done
4878     for i in $(seq 1 $NUMDIRS); do
4879         for j in $(seq 1 $NUMFILES); do
4880             file=$TDIR/dir$i/file$j
4881             yes | dd bs=$dd_bs count=$dd_count of=$file \
4882                 >/dev/null 2>&1 ||
4883                 error "write data into $file failed"
4884         done
4885     done
4886
4887     local expected=-1
4888     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4889
4890     # lfs_migrate file
4891     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4892     echo "$cmd"
4893     eval $cmd || error "$cmd failed"
4894
4895     check_stripe_count $TDIR/file1 $expected
4896
4897         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4898         then
4899                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4900                 # OST 1 if it is on OST 0. This file is small enough to
4901                 # be on only one stripe.
4902                 file=$TDIR/migr_1_ost
4903                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4904                         error "write data into $file failed"
4905                 local obdidx=$($LFS getstripe -i $file)
4906                 local oldmd5=$(md5sum $file)
4907                 local newobdidx=0
4908                 [[ $obdidx -eq 0 ]] && newobdidx=1
4909                 cmd="$LFS migrate -i $newobdidx $file"
4910                 echo $cmd
4911                 eval $cmd || error "$cmd failed"
4912                 local realobdix=$($LFS getstripe -i $file)
4913                 local newmd5=$(md5sum $file)
4914                 [[ $newobdidx -ne $realobdix ]] &&
4915                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4916                 [[ "$oldmd5" != "$newmd5" ]] &&
4917                         error "md5sum differ: $oldmd5, $newmd5"
4918         fi
4919
4920     # lfs_migrate dir
4921     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4922     echo "$cmd"
4923     eval $cmd || error "$cmd failed"
4924
4925     for j in $(seq 1 $NUMFILES); do
4926         check_stripe_count $TDIR/dir1/file$j $expected
4927     done
4928
4929     # lfs_migrate works with lfs find
4930     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4931          $LFS_MIGRATE -y -c $expected"
4932     echo "$cmd"
4933     eval $cmd || error "$cmd failed"
4934
4935     for i in $(seq 2 $NUMFILES); do
4936         check_stripe_count $TDIR/file$i $expected
4937     done
4938     for i in $(seq 2 $NUMDIRS); do
4939         for j in $(seq 1 $NUMFILES); do
4940             check_stripe_count $TDIR/dir$i/file$j $expected
4941         done
4942     done
4943 }
4944 run_test 56w "check lfs_migrate -c stripe_count works"
4945
4946 test_56x() {
4947         check_swap_layouts_support && return 0
4948         [[ $OSTCOUNT -lt 2 ]] &&
4949                 skip_env "need 2 OST, skipping test" && return
4950
4951         local dir0=$DIR/$tdir/$testnum
4952         test_mkdir -p $dir0 || error "creating dir $dir0"
4953
4954         local ref1=/etc/passwd
4955         local file1=$dir0/file1
4956
4957         $SETSTRIPE -c 2 $file1
4958         cp $ref1 $file1
4959         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4960         stripe=$($GETSTRIPE -c $file1)
4961         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4962         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4963
4964         # clean up
4965         rm -f $file1
4966 }
4967 run_test 56x "lfs migration support"
4968
4969 test_56y() {
4970         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4971                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
4972                 return
4973
4974         local res=""
4975         local dir0=$DIR/$tdir/$testnum
4976         test_mkdir -p $dir0 || error "creating dir $dir0"
4977         local f1=$dir0/file1
4978         local f2=$dir0/file2
4979
4980         touch $f1 || error "creating std file $f1"
4981         $MULTIOP $f2 H2c || error "creating released file $f2"
4982
4983         # a directory can be raid0, so ask only for files
4984         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4985         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4986
4987         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4988         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4989
4990         # only files can be released, so no need to force file search
4991         res=$($LFIND $dir0 -L released)
4992         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4993
4994         res=$($LFIND $dir0 \! -L released)
4995         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4996
4997 }
4998 run_test 56y "lfs find -L raid0|released"
4999
5000 test_56z() { # LU-4824
5001         # This checks to make sure 'lfs find' continues after errors
5002         # There are two classes of errors that should be caught:
5003         # - If multiple paths are provided, all should be searched even if one
5004         #   errors out
5005         # - If errors are encountered during the search, it should not terminate
5006         #   early
5007         local i
5008         test_mkdir $DIR/$tdir
5009         for i in d{0..9}; do
5010                 test_mkdir $DIR/$tdir/$i
5011         done
5012         touch $DIR/$tdir/d{0..9}/$tfile
5013         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5014                 error "$LFS find did not return an error"
5015         # Make a directory unsearchable. This should NOT be the last entry in
5016         # directory order.  Arbitrarily pick the 6th entry
5017         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5018         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5019         # The user should be able to see 10 directories and 9 files
5020         [ $count == 19 ] || error "$LFS find did not continue after error"
5021 }
5022 run_test 56z "lfs find should continue after an error"
5023
5024 test_56aa() { # LU-5937
5025         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5026
5027         mkdir $DIR/$tdir
5028         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5029
5030         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5031         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5032
5033         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5034 }
5035 run_test 56aa "lfs find --size under striped dir"
5036
5037 test_57a() {
5038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5039         # note test will not do anything if MDS is not local
5040         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5041                 skip "Only applicable to ldiskfs-based MDTs"
5042                 return
5043         fi
5044
5045         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5046         local MNTDEV="osd*.*MDT*.mntdev"
5047         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5048         [ -z "$DEV" ] && error "can't access $MNTDEV"
5049         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5050                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5051                         error "can't access $DEV"
5052                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5053                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5054                 rm $TMP/t57a.dump
5055         done
5056 }
5057 run_test 57a "verify MDS filesystem created with large inodes =="
5058
5059 test_57b() {
5060         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5061         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5062                 skip "Only applicable to ldiskfs-based MDTs"
5063                 return
5064         fi
5065
5066         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5067         local dir=$DIR/$tdir
5068
5069         local FILECOUNT=100
5070         local FILE1=$dir/f1
5071         local FILEN=$dir/f$FILECOUNT
5072
5073         rm -rf $dir || error "removing $dir"
5074         test_mkdir -p -c1 $dir || error "creating $dir"
5075         local mdtidx=$($LFS getstripe -M $dir)
5076         local mdtname=MDT$(printf %04x $mdtidx)
5077         local facet=mds$((mdtidx + 1))
5078
5079         echo "mcreating $FILECOUNT files"
5080         createmany -m $dir/f 1 $FILECOUNT || \
5081                 error "creating files in $dir"
5082
5083         # verify that files do not have EAs yet
5084         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5085         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5086
5087         sync
5088         sleep 1
5089         df $dir  #make sure we get new statfs data
5090         local MDSFREE=$(do_facet $facet \
5091                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5092         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5093         echo "opening files to create objects/EAs"
5094         local FILE
5095         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5096                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5097         done
5098
5099         # verify that files have EAs now
5100         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5101         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5102
5103         sleep 1  #make sure we get new statfs data
5104         df $dir
5105         local MDSFREE2=$(do_facet $facet \
5106                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5107         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5108         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5109                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5110                         error "MDC before $MDCFREE != after $MDCFREE2"
5111                 else
5112                         echo "MDC before $MDCFREE != after $MDCFREE2"
5113                         echo "unable to confirm if MDS has large inodes"
5114                 fi
5115         fi
5116         rm -rf $dir
5117 }
5118 run_test 57b "default LOV EAs are stored inside large inodes ==="
5119
5120 test_58() {
5121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5122         [ -z "$(which wiretest 2>/dev/null)" ] &&
5123                         skip_env "could not find wiretest" && return
5124         wiretest
5125 }
5126 run_test 58 "verify cross-platform wire constants =============="
5127
5128 test_59() {
5129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5130         echo "touch 130 files"
5131         createmany -o $DIR/f59- 130
5132         echo "rm 130 files"
5133         unlinkmany $DIR/f59- 130
5134         sync
5135         # wait for commitment of removal
5136         wait_delete_completed
5137 }
5138 run_test 59 "verify cancellation of llog records async ========="
5139
5140 TEST60_HEAD="test_60 run $RANDOM"
5141 test_60a() {
5142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5143         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5144         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5145                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5146                         skip_env "missing subtest run-llog.sh" && return
5147
5148         log "$TEST60_HEAD - from kernel mode"
5149         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5150         do_facet mgs sh run-llog.sh
5151         do_facet mgs $LCTL dk > $TMP/$tfile
5152
5153         # LU-6388: test llog_reader
5154         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5155         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5156         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5157                         skip_env "missing llog_reader" && return
5158         local fstype=$(facet_fstype mgs)
5159         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5160                 skip_env "Only for ldiskfs or zfs type mgs" && return
5161
5162         local mntpt=$(facet_mntpt mgs)
5163         local mgsdev=$(mgsdevname 1)
5164         local fid_list
5165         local fid
5166         local rec_list
5167         local rec
5168         local rec_type
5169         local obj_file
5170         local path
5171         local seq
5172         local oid
5173         local pass=true
5174
5175         #get fid and record list
5176         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5177                 tail -n 4))
5178         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5179                 tail -n 4))
5180         #remount mgs as ldiskfs or zfs type
5181         stop mgs || error "stop mgs failed"
5182         mount_fstype mgs || error "remount mgs failed"
5183         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5184                 fid=${fid_list[i]}
5185                 rec=${rec_list[i]}
5186                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5187                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5188                 oid=$((16#$oid))
5189
5190                 case $fstype in
5191                         ldiskfs )
5192                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5193                         zfs )
5194                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5195                 esac
5196                 echo "obj_file is $obj_file"
5197                 do_facet mgs $llog_reader $obj_file
5198
5199                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5200                         awk '{ print $3 }' | sed -e "s/^type=//g")
5201                 if [ $rec_type != $rec ]; then
5202                         echo "FAILED test_60a wrong record type $rec_type," \
5203                               "should be $rec"
5204                         pass=false
5205                         break
5206                 fi
5207
5208                 #check obj path if record type is LLOG_LOGID_MAGIC
5209                 if [ "$rec" == "1064553b" ]; then
5210                         path=$(do_facet mgs $llog_reader $obj_file |
5211                                 grep "path=" | awk '{ print $NF }' |
5212                                 sed -e "s/^path=//g")
5213                         if [ $obj_file != $mntpt/$path ]; then
5214                                 echo "FAILED test_60a wrong obj path" \
5215                                       "$montpt/$path, should be $obj_file"
5216                                 pass=false
5217                                 break
5218                         fi
5219                 fi
5220         done
5221         rm -f $TMP/$tfile
5222         #restart mgs before "error", otherwise it will block the next test
5223         stop mgs || error "stop mgs failed"
5224         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5225         $pass || error "test failed, see FAILED test_60a messages for specifics"
5226 }
5227 run_test 60a "llog_test run from kernel module and test llog_reader =========="
5228
5229 test_60b() { # bug 6411
5230         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5231         dmesg > $DIR/$tfile
5232         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5233                                  /llog.test/ {
5234                                          if (marker)
5235                                                  from_marker++
5236                                          from_begin++
5237                                  }
5238                                  END {
5239                                          if (marker)
5240                                                  print from_marker
5241                                          else
5242                                                  print from_begin
5243                                  }"`
5244         [[ $LLOG_COUNT -gt 50 ]] &&
5245                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5246 }
5247 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5248
5249 test_60c() {
5250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5251         echo "create 5000 files"
5252         createmany -o $DIR/f60c- 5000
5253 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5254         lctl set_param fail_loc=0x80000137
5255         unlinkmany $DIR/f60c- 5000
5256         lctl set_param fail_loc=0
5257 }
5258 run_test 60c "unlink file when mds full"
5259
5260 test_60d() {
5261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5262         SAVEPRINTK=$(lctl get_param -n printk)
5263
5264         # verify "lctl mark" is even working"
5265         MESSAGE="test message ID $RANDOM $$"
5266         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5267         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5268
5269         lctl set_param printk=0 || error "set lnet.printk failed"
5270         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5271         MESSAGE="new test message ID $RANDOM $$"
5272         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5273         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5274         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5275
5276         lctl set_param -n printk="$SAVEPRINTK"
5277 }
5278 run_test 60d "test printk console message masking"
5279
5280 test_61() {
5281         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5282         f="$DIR/f61"
5283         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5284         cancel_lru_locks osc
5285         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5286         sync
5287 }
5288 run_test 61 "mmap() writes don't make sync hang ================"
5289
5290 # bug 2330 - insufficient obd_match error checking causes LBUG
5291 test_62() {
5292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5293         f="$DIR/f62"
5294         echo foo > $f
5295         cancel_lru_locks osc
5296         lctl set_param fail_loc=0x405
5297         cat $f && error "cat succeeded, expect -EIO"
5298         lctl set_param fail_loc=0
5299 }
5300 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5301 # match every page all of the time.
5302 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5303
5304 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5305 # Though this test is irrelevant anymore, it helped to reveal some
5306 # other grant bugs (LU-4482), let's keep it.
5307 test_63a() {   # was test_63
5308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5309         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5310         for i in `seq 10` ; do
5311                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5312                 sleep 5
5313                 kill $!
5314                 sleep 1
5315         done
5316
5317         rm -f $DIR/f63 || true
5318 }
5319 run_test 63a "Verify oig_wait interruption does not crash ======="
5320
5321 # bug 2248 - async write errors didn't return to application on sync
5322 # bug 3677 - async write errors left page locked
5323 test_63b() {
5324         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5325         debugsave
5326         lctl set_param debug=-1
5327
5328         # ensure we have a grant to do async writes
5329         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5330         rm $DIR/$tfile
5331
5332         sync    # sync lest earlier test intercept the fail_loc
5333
5334         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5335         lctl set_param fail_loc=0x80000406
5336         $MULTIOP $DIR/$tfile Owy && \
5337                 error "sync didn't return ENOMEM"
5338         sync; sleep 2; sync     # do a real sync this time to flush page
5339         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5340                 error "locked page left in cache after async error" || true
5341         debugrestore
5342 }
5343 run_test 63b "async write errors should be returned to fsync ==="
5344
5345 test_64a () {
5346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5347         df $DIR
5348         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5349 }
5350 run_test 64a "verify filter grant calculations (in kernel) ====="
5351
5352 test_64b () {
5353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5354         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5355 }
5356 run_test 64b "check out-of-space detection on client ==========="
5357
5358 test_64c() {
5359         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5360 }
5361 run_test 64c "verify grant shrink ========================------"
5362
5363 # bug 1414 - set/get directories' stripe info
5364 test_65a() {
5365         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5366         test_mkdir -p $DIR/$tdir
5367         touch $DIR/$tdir/f1
5368         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5369 }
5370 run_test 65a "directory with no stripe info ===================="
5371
5372 test_65b() {
5373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5374         test_mkdir -p $DIR/$tdir
5375         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5376                                                 error "setstripe"
5377         touch $DIR/$tdir/f2
5378         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5379 }
5380 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5381
5382 test_65c() {
5383         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5384         if [[ $OSTCOUNT -gt 1 ]]; then
5385                 test_mkdir -p $DIR/$tdir
5386                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5387                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5388                 touch $DIR/$tdir/f3
5389                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5390         fi
5391 }
5392 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5393
5394 test_65d() {
5395         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5396         test_mkdir -p $DIR/$tdir
5397         if [[ $STRIPECOUNT -le 0 ]]; then
5398                 sc=1
5399         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5400 #LOV_MAX_STRIPE_COUNT is 2000
5401                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5402         else
5403                 sc=$(($STRIPECOUNT - 1))
5404         fi
5405         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5406         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5407         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5408                 error "lverify failed"
5409 }
5410 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5411
5412 test_65e() {
5413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5414         test_mkdir -p $DIR/$tdir
5415
5416         $SETSTRIPE $DIR/$tdir || error "setstripe"
5417         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5418                                         error "no stripe info failed"
5419         touch $DIR/$tdir/f6
5420         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5421 }
5422 run_test 65e "directory setstripe defaults ======================="
5423
5424 test_65f() {
5425         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5426         test_mkdir -p $DIR/${tdir}f
5427         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5428 }
5429 run_test 65f "dir setstripe permission (should return error) ==="
5430
5431 test_65g() {
5432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5433         test_mkdir -p $DIR/$tdir
5434         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5435                                                         error "setstripe"
5436         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5437         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5438                 error "delete default stripe failed"
5439 }
5440 run_test 65g "directory setstripe -d ==========================="
5441
5442 test_65h() {
5443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5444         test_mkdir -p $DIR/$tdir
5445         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5446                                                         error "setstripe"
5447         test_mkdir -p $DIR/$tdir/dd1
5448         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5449                 error "stripe info inherit failed"
5450 }
5451 run_test 65h "directory stripe info inherit ===================="
5452
5453 test_65i() { # bug6367
5454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5455         $SETSTRIPE -S 65536 -c -1 $MOUNT
5456 }
5457 run_test 65i "set non-default striping on root directory (bug 6367)="
5458
5459 test_65ia() { # bug12836
5460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5461         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5462 }
5463 run_test 65ia "getstripe on -1 default directory striping"
5464
5465 test_65ib() { # bug12836
5466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5467         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5468 }
5469 run_test 65ib "getstripe -v on -1 default directory striping"
5470
5471 test_65ic() { # bug12836
5472         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5473         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5474 }
5475 run_test 65ic "new find on -1 default directory striping"
5476
5477 test_65j() { # bug6367
5478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5479         sync; sleep 1
5480         # if we aren't already remounting for each test, do so for this test
5481         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5482                 cleanup || error "failed to unmount"
5483                 setup
5484         fi
5485         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5486 }
5487 run_test 65j "set default striping on root directory (bug 6367)="
5488
5489 test_65k() { # bug11679
5490         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5491         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5492         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5493
5494     echo "Check OST status: "
5495     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5496               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5497
5498     for OSC in $MDS_OSCS; do
5499         echo $OSC "is activate"
5500         do_facet $SINGLEMDS lctl --device %$OSC activate
5501     done
5502
5503     mkdir -p $DIR/$tdir
5504     for INACTIVE_OSC in $MDS_OSCS; do
5505         echo "Deactivate: " $INACTIVE_OSC
5506         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5507         for STRIPE_OSC in $MDS_OSCS; do
5508             OST=`osc_to_ost $STRIPE_OSC`
5509             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5510                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5511
5512             [ -f $DIR/$tdir/$IDX ] && continue
5513             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5514             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5515             RC=$?
5516             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5517         done
5518         rm -f $DIR/$tdir/*
5519         echo $INACTIVE_OSC "is Activate."
5520         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5521     done
5522 }
5523 run_test 65k "validate manual striping works properly with deactivated OSCs"
5524
5525 test_65l() { # bug 12836
5526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5527         test_mkdir -p $DIR/$tdir/test_dir
5528         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5529         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5530 }
5531 run_test 65l "lfs find on -1 stripe dir ========================"
5532
5533 # bug 2543 - update blocks count on client
5534 test_66() {
5535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5536         COUNT=${COUNT:-8}
5537         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5538         sync; sync_all_data; sync; sync_all_data
5539         cancel_lru_locks osc
5540         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5541         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5542 }
5543 run_test 66 "update inode blocks count on client ==============="
5544
5545 LLOOP=
5546 LLITELOOPLOAD=
5547 cleanup_68() {
5548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5549         trap 0
5550         if [ ! -z "$LLOOP" ]; then
5551                 if swapon -s | grep -q $LLOOP; then
5552                         swapoff $LLOOP || error "swapoff failed"
5553                 fi
5554
5555                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5556                 rm -f $LLOOP
5557                 unset LLOOP
5558         fi
5559         if [ ! -z "$LLITELOOPLOAD" ]; then
5560                 rmmod llite_lloop
5561                 unset LLITELOOPLOAD
5562         fi
5563         rm -f $DIR/f68*
5564 }
5565
5566 meminfo() {
5567         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5568 }
5569
5570 swap_used() {
5571         swapon -s | awk '($1 == "'$1'") { print $4 }'
5572 }
5573
5574 # test case for lloop driver, basic function
5575 test_68a() {
5576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5577         [ "$UID" != 0 ] && skip_env "must run as root" && return
5578         llite_lloop_enabled || \
5579                 { skip_env "llite_lloop module disabled" && return; }
5580
5581         trap cleanup_68 EXIT
5582
5583         if ! module_loaded llite_lloop; then
5584                 if load_module llite/llite_lloop; then
5585                         LLITELOOPLOAD=yes
5586                 else
5587                         skip_env "can't find module llite_lloop"
5588                         return
5589                 fi
5590         fi
5591
5592         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5593         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5594         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5595
5596         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5597         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5598
5599         cleanup_68
5600 }
5601 run_test 68a "lloop driver - basic test ========================"
5602
5603 # excercise swapping to lustre by adding a high priority swapfile entry
5604 # and then consuming memory until it is used.
5605 test_68b() {  # was test_68
5606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5607         [ "$UID" != 0 ] && skip_env "must run as root" && return
5608         lctl get_param -n devices | grep -q obdfilter && \
5609                 skip "local OST" && return
5610
5611         grep -q llite_lloop /proc/modules
5612         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5613
5614         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5615                 skip "can't reliably test swap with TCP" && return
5616
5617         MEMTOTAL=`meminfo MemTotal`
5618         NR_BLOCKS=$((MEMTOTAL>>8))
5619         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5620
5621         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5622         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5623         mkswap $DIR/f68b
5624
5625         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5626
5627         trap cleanup_68 EXIT
5628
5629         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5630
5631         echo "before: `swapon -s | grep $LLOOP`"
5632         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5633         echo "after: `swapon -s | grep $LLOOP`"
5634         SWAPUSED=`swap_used $LLOOP`
5635
5636         cleanup_68
5637
5638         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5639 }
5640 run_test 68b "support swapping to Lustre ========================"
5641
5642 # bug5265, obdfilter oa2dentry return -ENOENT
5643 # #define OBD_FAIL_OST_ENOENT 0x217
5644 test_69() {
5645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5646         remote_ost_nodsh && skip "remote OST with nodsh" && return
5647
5648         f="$DIR/$tfile"
5649         $SETSTRIPE -c 1 -i 0 $f
5650
5651         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5652
5653         do_facet ost1 lctl set_param fail_loc=0x217
5654         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5655         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5656
5657         do_facet ost1 lctl set_param fail_loc=0
5658         $DIRECTIO write $f 0 2 || error "write error"
5659
5660         cancel_lru_locks osc
5661         $DIRECTIO read $f 0 1 || error "read error"
5662
5663         do_facet ost1 lctl set_param fail_loc=0x217
5664         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5665
5666         do_facet ost1 lctl set_param fail_loc=0
5667         rm -f $f
5668 }
5669 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5670
5671 test_71() {
5672         test_mkdir -p $DIR/$tdir
5673         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5674         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5675 }
5676 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5677
5678 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5680         [ "$RUNAS_ID" = "$UID" ] &&
5681                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5682
5683         # Check that testing environment is properly set up. Skip if not
5684         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5685                 skip_env "User $RUNAS_ID does not exist - skipping"
5686                 return 0
5687         }
5688         touch $DIR/$tfile
5689         chmod 777 $DIR/$tfile
5690         chmod ug+s $DIR/$tfile
5691         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5692                 error "$RUNAS dd $DIR/$tfile failed"
5693         # See if we are still setuid/sgid
5694         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5695                 error "S/gid is not dropped on write"
5696         # Now test that MDS is updated too
5697         cancel_lru_locks mdc
5698         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5699                 error "S/gid is not dropped on MDS"
5700         rm -f $DIR/$tfile
5701 }
5702 run_test 72a "Test that remove suid works properly (bug5695) ===="
5703
5704 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5705         local perm
5706
5707         [ "$RUNAS_ID" = "$UID" ] && \
5708                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5709         [ "$RUNAS_ID" -eq 0 ] && \
5710                 skip_env "RUNAS_ID = 0 -- skipping" && return
5711
5712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5713         # Check that testing environment is properly set up. Skip if not
5714         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5715                 skip_env "User $RUNAS_ID does not exist - skipping"
5716                 return 0
5717         }
5718         touch $DIR/${tfile}-f{g,u}
5719         test_mkdir $DIR/${tfile}-dg
5720         test_mkdir $DIR/${tfile}-du
5721         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5722         chmod g+s $DIR/${tfile}-{f,d}g
5723         chmod u+s $DIR/${tfile}-{f,d}u
5724         for perm in 777 2777 4777; do
5725                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5726                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5727                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5728                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5729         done
5730         true
5731 }
5732 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5733
5734 # bug 3462 - multiple simultaneous MDC requests
5735 test_73() {
5736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5737         test_mkdir $DIR/d73-1
5738         test_mkdir $DIR/d73-2
5739         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5740         pid1=$!
5741
5742         lctl set_param fail_loc=0x80000129
5743         $MULTIOP $DIR/d73-1/f73-2 Oc &
5744         sleep 1
5745         lctl set_param fail_loc=0
5746
5747         $MULTIOP $DIR/d73-2/f73-3 Oc &
5748         pid3=$!
5749
5750         kill -USR1 $pid1
5751         wait $pid1 || return 1
5752
5753         sleep 25
5754
5755         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5756         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5757         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5758
5759         rm -rf $DIR/d73-*
5760 }
5761 run_test 73 "multiple MDC requests (should not deadlock)"
5762
5763 test_74a() { # bug 6149, 6184
5764         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5765         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5766         #
5767         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5768         # will spin in a tight reconnection loop
5769         touch $DIR/f74a
5770         $LCTL set_param fail_loc=0x8000030e
5771         # get any lock that won't be difficult - lookup works.
5772         ls $DIR/f74a
5773         $LCTL set_param fail_loc=0
5774         rm -f $DIR/f74a
5775         true
5776 }
5777 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5778
5779 test_74b() { # bug 13310
5780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5781         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5782         #
5783         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5784         # will spin in a tight reconnection loop
5785         $LCTL set_param fail_loc=0x8000030e
5786         # get a "difficult" lock
5787         touch $DIR/f74b
5788         $LCTL set_param fail_loc=0
5789         rm -f $DIR/f74b
5790         true
5791 }
5792 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5793
5794 test_74c() {
5795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5796         #define OBD_FAIL_LDLM_NEW_LOCK
5797         $LCTL set_param fail_loc=0x319
5798         touch $DIR/$tfile && error "touch successful"
5799         $LCTL set_param fail_loc=0
5800         true
5801 }
5802 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5803
5804 num_inodes() {
5805         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5806 }
5807
5808 get_inode_slab_tunables() {
5809         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5810 }
5811
5812 set_inode_slab_tunables() {
5813         echo "lustre_inode_cache $1" > /proc/slabinfo
5814 }
5815
5816 test_76() { # Now for bug 20433, added originally in bug 1443
5817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5818         local SLAB_SETTINGS=`get_inode_slab_tunables`
5819         local CPUS=`getconf _NPROCESSORS_ONLN`
5820         # we cannot set limit below 1 which means 1 inode in each
5821         # per-cpu cache is still allowed
5822         set_inode_slab_tunables "1 1 0"
5823         cancel_lru_locks osc
5824         BEFORE_INODES=$(num_inodes)
5825         echo "before inodes: $BEFORE_INODES"
5826         local COUNT=1000
5827         [ "$SLOW" = "no" ] && COUNT=100
5828         for i in $(seq $COUNT); do
5829                 touch $DIR/$tfile
5830                 rm -f $DIR/$tfile
5831         done
5832         cancel_lru_locks osc
5833         AFTER_INODES=$(num_inodes)
5834         echo "after inodes: $AFTER_INODES"
5835         local wait=0
5836         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5837                 sleep 2
5838                 AFTER_INODES=$(num_inodes)
5839                 wait=$((wait+2))
5840                 echo "wait $wait seconds inodes: $AFTER_INODES"
5841                 if [ $wait -gt 30 ]; then
5842                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5843                 fi
5844         done
5845         set_inode_slab_tunables "$SLAB_SETTINGS"
5846 }
5847 run_test 76 "confirm clients recycle inodes properly ===="
5848
5849
5850 export ORIG_CSUM=""
5851 set_checksums()
5852 {
5853         # Note: in sptlrpc modes which enable its own bulk checksum, the
5854         # original crc32_le bulk checksum will be automatically disabled,
5855         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5856         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5857         # In this case set_checksums() will not be no-op, because sptlrpc
5858         # bulk checksum will be enabled all through the test.
5859
5860         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5861         lctl set_param -n osc.*.checksums $1
5862         return 0
5863 }
5864
5865 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5866                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5867 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5868 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5869 set_checksum_type()
5870 {
5871         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5872         log "set checksum type to $1"
5873         return 0
5874 }
5875 F77_TMP=$TMP/f77-temp
5876 F77SZ=8
5877 setup_f77() {
5878         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5879                 error "error writing to $F77_TMP"
5880 }
5881
5882 test_77a() { # bug 10889
5883         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5884         $GSS && skip "could not run with gss" && return
5885         [ ! -f $F77_TMP ] && setup_f77
5886         set_checksums 1
5887         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5888         set_checksums 0
5889         rm -f $DIR/$tfile
5890 }
5891 run_test 77a "normal checksum read/write operation"
5892
5893 test_77b() { # bug 10889
5894         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5895         $GSS && skip "could not run with gss" && return
5896         [ ! -f $F77_TMP ] && setup_f77
5897         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5898         $LCTL set_param fail_loc=0x80000409
5899         set_checksums 1
5900
5901         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5902                 error "dd error: $?"
5903         $LCTL set_param fail_loc=0
5904
5905         for algo in $CKSUM_TYPES; do
5906                 cancel_lru_locks osc
5907                 set_checksum_type $algo
5908                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5909                 $LCTL set_param fail_loc=0x80000408
5910                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5911                 $LCTL set_param fail_loc=0
5912         done
5913         set_checksums 0
5914         set_checksum_type $ORIG_CSUM_TYPE
5915         rm -f $DIR/$tfile
5916 }
5917 run_test 77b "checksum error on client write, read"
5918
5919 test_77d() { # bug 10889
5920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5921         $GSS && skip "could not run with gss" && return
5922         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5923         $LCTL set_param fail_loc=0x80000409
5924         set_checksums 1
5925         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5926                 error "direct write: rc=$?"
5927         $LCTL set_param fail_loc=0
5928         set_checksums 0
5929
5930         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5931         $LCTL set_param fail_loc=0x80000408
5932         set_checksums 1
5933         cancel_lru_locks osc
5934         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5935                 error "direct read: rc=$?"
5936         $LCTL set_param fail_loc=0
5937         set_checksums 0
5938 }
5939 run_test 77d "checksum error on OST direct write, read"
5940
5941 test_77f() { # bug 10889
5942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5943         $GSS && skip "could not run with gss" && return
5944         set_checksums 1
5945         for algo in $CKSUM_TYPES; do
5946                 cancel_lru_locks osc
5947                 set_checksum_type $algo
5948                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5949                 $LCTL set_param fail_loc=0x409
5950                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5951                         error "direct write succeeded"
5952                 $LCTL set_param fail_loc=0
5953         done
5954         set_checksum_type $ORIG_CSUM_TYPE
5955         set_checksums 0
5956 }
5957 run_test 77f "repeat checksum error on write (expect error)"
5958
5959 test_77g() { # bug 10889
5960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5961         $GSS && skip "could not run with gss" && return
5962         remote_ost_nodsh && skip "remote OST with nodsh" && return
5963
5964         [ ! -f $F77_TMP ] && setup_f77
5965
5966         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5967         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5968         do_facet ost1 lctl set_param fail_loc=0x8000021a
5969         set_checksums 1
5970         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5971                 error "write error: rc=$?"
5972         do_facet ost1 lctl set_param fail_loc=0
5973         set_checksums 0
5974
5975         cancel_lru_locks osc
5976         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5977         do_facet ost1 lctl set_param fail_loc=0x8000021b
5978         set_checksums 1
5979         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5980         do_facet ost1 lctl set_param fail_loc=0
5981         set_checksums 0
5982 }
5983 run_test 77g "checksum error on OST write, read"
5984
5985 test_77i() { # bug 13805
5986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5987         $GSS && skip "could not run with gss" && return
5988         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5989         lctl set_param fail_loc=0x40b
5990         remount_client $MOUNT
5991         lctl set_param fail_loc=0
5992         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5993                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5994                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5995                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5996         done
5997         remount_client $MOUNT
5998 }
5999 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
6000
6001 test_77j() { # bug 13805
6002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6003         $GSS && skip "could not run with gss" && return
6004         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6005         lctl set_param fail_loc=0x40c
6006         remount_client $MOUNT
6007         lctl set_param fail_loc=0
6008         sleep 2 # wait async osc connect to finish
6009         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
6010                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6011                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
6012                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6013         done
6014         remount_client $MOUNT
6015 }
6016 run_test 77j "client only supporting ADLER32"
6017
6018 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6019 rm -f $F77_TMP
6020 unset F77_TMP
6021
6022 test_78() { # bug 10901
6023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6024         remote_ost || { skip_env "local OST" && return; }
6025
6026         NSEQ=5
6027         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6028         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6029         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6030         echo "MemTotal: $MEMTOTAL"
6031
6032         # reserve 256MB of memory for the kernel and other running processes,
6033         # and then take 1/2 of the remaining memory for the read/write buffers.
6034         if [ $MEMTOTAL -gt 512 ] ;then
6035                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6036         else
6037                 # for those poor memory-starved high-end clusters...
6038                 MEMTOTAL=$((MEMTOTAL / 2))
6039         fi
6040         echo "Mem to use for directio: $MEMTOTAL"
6041
6042         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6043         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6044         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6045         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6046                 head -n1)
6047         echo "Smallest OST: $SMALLESTOST"
6048         [[ $SMALLESTOST -lt 10240 ]] &&
6049                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6050
6051         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6052                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6053
6054         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6055         echo "File size: $F78SIZE"
6056         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6057         for i in $(seq 1 $NSEQ); do
6058                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6059                 echo directIO rdwr round $i of $NSEQ
6060                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6061         done
6062
6063         rm -f $DIR/$tfile
6064 }
6065 run_test 78 "handle large O_DIRECT writes correctly ============"
6066
6067 test_79() { # bug 12743
6068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6069         wait_delete_completed
6070
6071         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6072         BKFREE=$(calc_osc_kbytes kbytesfree)
6073         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6074
6075         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6076         DFTOTAL=`echo $STRING | cut -d, -f1`
6077         DFUSED=`echo $STRING  | cut -d, -f2`
6078         DFAVAIL=`echo $STRING | cut -d, -f3`
6079         DFFREE=$(($DFTOTAL - $DFUSED))
6080
6081         ALLOWANCE=$((64 * $OSTCOUNT))
6082
6083         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6084            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6085                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6086         fi
6087         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6088            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6089                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6090         fi
6091         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6092            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6093                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6094         fi
6095 }
6096 run_test 79 "df report consistency check ======================="
6097
6098 test_80() { # bug 10718
6099         remote_ost_nodsh && skip "remote OST with nodsh" && return
6100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6101         # relax strong synchronous semantics for slow backends like ZFS
6102         local soc="obdfilter.*.sync_on_lock_cancel"
6103         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6104         local hosts=
6105         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6106                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6107                           facet_active_host $host; done | sort -u)
6108                 do_nodes $hosts lctl set_param $soc=never
6109         fi
6110
6111         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6112         sync; sleep 1; sync
6113         local BEFORE=`date +%s`
6114         cancel_lru_locks osc
6115         local AFTER=`date +%s`
6116         local DIFF=$((AFTER-BEFORE))
6117         if [ $DIFF -gt 1 ] ; then
6118                 error "elapsed for 1M@1T = $DIFF"
6119         fi
6120
6121         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6122
6123         rm -f $DIR/$tfile
6124 }
6125 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6126
6127 test_81a() { # LU-456
6128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6129         remote_ost_nodsh && skip "remote OST with nodsh" && return
6130         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6131         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6132         do_facet ost1 lctl set_param fail_loc=0x80000228
6133
6134         # write should trigger a retry and success
6135         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6136         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6137         RC=$?
6138         if [ $RC -ne 0 ] ; then
6139                 error "write should success, but failed for $RC"
6140         fi
6141 }
6142 run_test 81a "OST should retry write when get -ENOSPC ==============="
6143
6144 test_81b() { # LU-456
6145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6146         remote_ost_nodsh && skip "remote OST with nodsh" && return
6147         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6148         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6149         do_facet ost1 lctl set_param fail_loc=0x228
6150
6151         # write should retry several times and return -ENOSPC finally
6152         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6153         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6154         RC=$?
6155         ENOSPC=28
6156         if [ $RC -ne $ENOSPC ] ; then
6157                 error "dd should fail for -ENOSPC, but succeed."
6158         fi
6159 }
6160 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6161
6162 test_82() { # LU-1031
6163         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6164         local gid1=14091995
6165         local gid2=16022000
6166
6167         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6168         local MULTIPID1=$!
6169         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6170         local MULTIPID2=$!
6171         kill -USR1 $MULTIPID2
6172         sleep 2
6173         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6174                 error "First grouplock does not block second one"
6175         else
6176                 echo "Second grouplock blocks first one"
6177         fi
6178         kill -USR1 $MULTIPID1
6179         wait $MULTIPID1
6180         wait $MULTIPID2
6181 }
6182 run_test 82 "Basic grouplock test ==============================="
6183
6184 test_99a() {
6185         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6186                 return
6187         test_mkdir -p $DIR/d99cvsroot
6188         chown $RUNAS_ID $DIR/d99cvsroot
6189         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6190         cd $TMP
6191
6192         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6193         cd $oldPWD
6194 }
6195 run_test 99a "cvs init ========================================="
6196
6197 test_99b() {
6198         [ -z "$(which cvs 2>/dev/null)" ] &&
6199                 skip_env "could not find cvs" && return
6200         [ ! -d $DIR/d99cvsroot ] && test_99a
6201         cd /etc/init.d
6202         # some versions of cvs import exit(1) when asked to import links or
6203         # files they can't read.  ignore those files.
6204         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6205                         ! -perm +4 -printf '-I %f\n')
6206         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6207                 d99reposname vtag rtag
6208 }
6209 run_test 99b "cvs import ======================================="
6210
6211 test_99c() {
6212         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6213         [ ! -d $DIR/d99cvsroot ] && test_99b
6214         cd $DIR
6215         test_mkdir -p $DIR/d99reposname
6216         chown $RUNAS_ID $DIR/d99reposname
6217         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6218 }
6219 run_test 99c "cvs checkout ====================================="
6220
6221 test_99d() {
6222         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6223         [ ! -d $DIR/d99cvsroot ] && test_99c
6224         cd $DIR/d99reposname
6225         $RUNAS touch foo99
6226         $RUNAS cvs add -m 'addmsg' foo99
6227 }
6228 run_test 99d "cvs add =========================================="
6229
6230 test_99e() {
6231         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6232         [ ! -d $DIR/d99cvsroot ] && test_99c
6233         cd $DIR/d99reposname
6234         $RUNAS cvs update
6235 }
6236 run_test 99e "cvs update ======================================="
6237
6238 test_99f() {
6239         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6240         [ ! -d $DIR/d99cvsroot ] && test_99d
6241         cd $DIR/d99reposname
6242         $RUNAS cvs commit -m 'nomsg' foo99
6243     rm -fr $DIR/d99cvsroot
6244 }
6245 run_test 99f "cvs commit ======================================="
6246
6247 test_100() {
6248         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6249         [ "$NETTYPE" = tcp ] || \
6250                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6251                         return ; }
6252
6253         remote_ost_nodsh && skip "remote OST with nodsh" && return
6254         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6255         remote_servers || \
6256                 { skip "useless for local single node setup" && return; }
6257
6258         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6259                 [ "$PROT" != "tcp" ] && continue
6260                 RPORT=$(echo $REMOTE | cut -d: -f2)
6261                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6262
6263                 rc=0
6264                 LPORT=`echo $LOCAL | cut -d: -f2`
6265                 if [ $LPORT -ge 1024 ]; then
6266                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6267                         netstat -tna
6268                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6269                 fi
6270         done
6271         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6272 }
6273 run_test 100 "check local port using privileged port ==========="
6274
6275 function get_named_value()
6276 {
6277     local tag
6278
6279     tag=$1
6280     while read ;do
6281         line=$REPLY
6282         case $line in
6283         $tag*)
6284             echo $line | sed "s/^$tag[ ]*//"
6285             break
6286             ;;
6287         esac
6288     done
6289 }
6290
6291 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6292                    awk '/^max_cached_mb/ { print $2 }')
6293
6294 cleanup_101a() {
6295         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6296         trap 0
6297 }
6298
6299 test_101a() {
6300         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6301         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6302         local s
6303         local discard
6304         local nreads=10000
6305         local cache_limit=32
6306
6307         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6308         trap cleanup_101a EXIT
6309         $LCTL set_param -n llite.*.read_ahead_stats 0
6310         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6311
6312         #
6313         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6314         #
6315         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6316         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6317
6318         discard=0
6319         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6320                 get_named_value 'read but discarded' | cut -d" " -f1); do
6321                         discard=$(($discard + $s))
6322         done
6323         cleanup_101a
6324
6325         if [[ $(($discard * 10)) -gt $nreads ]]; then
6326                 $LCTL get_param osc.*-osc*.rpc_stats
6327                 $LCTL get_param llite.*.read_ahead_stats
6328                 error "too many ($discard) discarded pages"
6329         fi
6330         rm -f $DIR/$tfile || true
6331 }
6332 run_test 101a "check read-ahead for random reads ================"
6333
6334 setup_test101bc() {
6335         test_mkdir -p $DIR/$tdir
6336         local STRIPE_SIZE=$1
6337         local FILE_LENGTH=$2
6338         STRIPE_OFFSET=0
6339
6340         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6341
6342         local list=$(comma_list $(osts_nodes))
6343         set_osd_param $list '' read_cache_enable 0
6344         set_osd_param $list '' writethrough_cache_enable 0
6345
6346         trap cleanup_test101bc EXIT
6347         # prepare the read-ahead file
6348         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6349
6350         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6351                                 count=$FILE_SIZE_MB 2> /dev/null
6352
6353 }
6354
6355 cleanup_test101bc() {
6356         trap 0
6357         rm -rf $DIR/$tdir
6358         rm -f $DIR/$tfile
6359
6360         local list=$(comma_list $(osts_nodes))
6361         set_osd_param $list '' read_cache_enable 1
6362         set_osd_param $list '' writethrough_cache_enable 1
6363 }
6364
6365 calc_total() {
6366         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6367 }
6368
6369 ra_check_101() {
6370         local READ_SIZE=$1
6371         local STRIPE_SIZE=$2
6372         local FILE_LENGTH=$3
6373         local RA_INC=1048576
6374         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6375         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6376                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6377         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6378                         get_named_value 'read but discarded' |
6379                         cut -d" " -f1 | calc_total)
6380         if [[ $DISCARD -gt $discard_limit ]]; then
6381                 $LCTL get_param llite.*.read_ahead_stats
6382                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6383         else
6384                 echo "Read-ahead success for size ${READ_SIZE}"
6385         fi
6386 }
6387
6388 test_101b() {
6389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6390         [[ $OSTCOUNT -lt 2 ]] &&
6391                 skip_env "skipping stride IO stride-ahead test" && return
6392         local STRIPE_SIZE=1048576
6393         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6394         if [ $SLOW == "yes" ]; then
6395                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6396         else
6397                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6398         fi
6399
6400         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6401
6402         # prepare the read-ahead file
6403         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6404         cancel_lru_locks osc
6405         for BIDX in 2 4 8 16 32 64 128 256
6406         do
6407                 local BSIZE=$((BIDX*4096))
6408                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6409                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6410                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6411                 $LCTL set_param -n llite.*.read_ahead_stats 0
6412                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6413                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6414                 cancel_lru_locks osc
6415                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6416         done
6417         cleanup_test101bc
6418         true
6419 }
6420 run_test 101b "check stride-io mode read-ahead ================="
6421
6422 test_101c() {
6423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6424         local STRIPE_SIZE=1048576
6425         local FILE_LENGTH=$((STRIPE_SIZE*100))
6426         local nreads=10000
6427         local osc_rpc_stats
6428
6429         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6430
6431         cancel_lru_locks osc
6432         $LCTL set_param osc.*.rpc_stats 0
6433         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6434         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6435                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6436                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6437                 local size
6438
6439                 if [ $lines -le 20 ]; then
6440                         continue
6441                 fi
6442                 for size in 1 2 4 8; do
6443                         local rpc=$(echo "$stats" |
6444                                     awk '($1 == "'$size':") {print $2; exit; }')
6445                         [ $rpc != 0 ] &&
6446                                 error "Small $((size*4))k read IO $rpc !"
6447                 done
6448                 echo "$osc_rpc_stats check passed!"
6449         done
6450         cleanup_test101bc
6451         true
6452 }
6453 run_test 101c "check stripe_size aligned read-ahead ================="
6454
6455 set_read_ahead() {
6456         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6457         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6458 }
6459
6460 test_101d() {
6461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6462         local file=$DIR/$tfile
6463         local sz_MB=${FILESIZE_101d:-500}
6464         local ra_MB=${READAHEAD_MB:-40}
6465
6466         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6467         [ $free_MB -lt $sz_MB ] &&
6468                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6469
6470         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6471         $SETSTRIPE -c -1 $file || error "setstripe failed"
6472
6473         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6474         echo Cancel LRU locks on lustre client to flush the client cache
6475         cancel_lru_locks osc
6476
6477         echo Disable read-ahead
6478         local old_READAHEAD=$(set_read_ahead 0)
6479
6480         echo Reading the test file $file with read-ahead disabled
6481         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6482
6483         echo Cancel LRU locks on lustre client to flush the client cache
6484         cancel_lru_locks osc
6485         echo Enable read-ahead with ${ra_MB}MB
6486         set_read_ahead $ra_MB
6487
6488         echo Reading the test file $file with read-ahead enabled
6489         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6490
6491         echo "read-ahead disabled time read $raOFF"
6492         echo "read-ahead enabled  time read $raON"
6493
6494         set_read_ahead $old_READAHEAD
6495         rm -f $file
6496         wait_delete_completed
6497
6498         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6499                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6500 }
6501 run_test 101d "file read with and without read-ahead enabled"
6502
6503 test_101e() {
6504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6505         local file=$DIR/$tfile
6506         local size_KB=500  #KB
6507         local count=100
6508         local bsize=1024
6509
6510         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6511         local need_KB=$((count * size_KB))
6512         [[ $free_KB -le $need_KB ]] &&
6513                 skip_env "Need free space $need_KB, have $free_KB" && return
6514
6515         echo "Creating $count ${size_KB}K test files"
6516         for ((i = 0; i < $count; i++)); do
6517                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6518         done
6519
6520         echo "Cancel LRU locks on lustre client to flush the client cache"
6521         cancel_lru_locks osc
6522
6523         echo "Reset readahead stats"
6524         $LCTL set_param -n llite.*.read_ahead_stats 0
6525
6526         for ((i = 0; i < $count; i++)); do
6527                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6528         done
6529
6530         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6531                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6532
6533         for ((i = 0; i < $count; i++)); do
6534                 rm -rf $file.$i 2>/dev/null
6535         done
6536
6537         #10000 means 20% reads are missing in readahead
6538         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6539 }
6540 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6541
6542 cleanup_test101f() {
6543     trap 0
6544     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6545     rm -rf $DIR/$tfile 2>/dev/null
6546 }
6547
6548 test_101f() {
6549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6550     local file=$DIR/$tfile
6551     local nreads=1000
6552
6553     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6554     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6555     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6556     trap cleanup_test101f EXIT
6557
6558     echo Cancel LRU locks on lustre client to flush the client cache
6559     cancel_lru_locks osc
6560
6561     echo Reset readahead stats
6562     $LCTL set_param -n llite.*.read_ahead_stats 0
6563     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6564     # readahead should read in 2M file on second read, so only miss
6565     # 2 pages.
6566     echo Random 4K reads on 2M file for 1000 times
6567     $READS -f $file -s 2097152 -b 4096 -n $nreads
6568
6569     echo checking missing pages
6570     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6571           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6572
6573     [ $miss -lt 3 ] || error "misses too much pages!"
6574     cleanup_test101f
6575 }
6576 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6577
6578 setup_test102() {
6579         test_mkdir -p $DIR/$tdir
6580         chown $RUNAS_ID $DIR/$tdir
6581         STRIPE_SIZE=65536
6582         STRIPE_OFFSET=1
6583         STRIPE_COUNT=$OSTCOUNT
6584         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6585
6586         trap cleanup_test102 EXIT
6587         cd $DIR
6588         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6589         cd $DIR/$tdir
6590         for num in 1 2 3 4; do
6591                 for count in $(seq 1 $STRIPE_COUNT); do
6592                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6593                                 local size=`expr $STRIPE_SIZE \* $num`
6594                                 local file=file"$num-$idx-$count"
6595                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6596                         done
6597                 done
6598         done
6599
6600         cd $DIR
6601         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6602 }
6603
6604 cleanup_test102() {
6605         trap 0
6606         rm -f $TMP/f102.tar
6607         rm -rf $DIR/d0.sanity/d102
6608 }
6609
6610 test_102a() {
6611         local testfile=$DIR/$tfile
6612
6613         touch $testfile
6614
6615         [ "$UID" != 0 ] && skip_env "must run as root" && return
6616         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6617                 skip_env "must have user_xattr" && return
6618
6619         [ -z "$(which setfattr 2>/dev/null)" ] &&
6620                 skip_env "could not find setfattr" && return
6621
6622         echo "set/get xattr..."
6623         setfattr -n trusted.name1 -v value1 $testfile ||
6624                 error "setfattr -n trusted.name1=value1 $testfile failed"
6625         getfattr -n trusted.name1 $testfile 2> /dev/null |
6626           grep "trusted.name1=.value1" ||
6627                 error "$testfile missing trusted.name1=value1"
6628
6629         setfattr -n user.author1 -v author1 $testfile ||
6630                 error "setfattr -n user.author1=author1 $testfile failed"
6631         getfattr -n user.author1 $testfile 2> /dev/null |
6632           grep "user.author1=.author1" ||
6633                 error "$testfile missing trusted.author1=author1"
6634
6635         echo "listxattr..."
6636         setfattr -n trusted.name2 -v value2 $testfile ||
6637                 error "$testfile unable to set trusted.name2"
6638         setfattr -n trusted.name3 -v value3 $testfile ||
6639                 error "$testfile unable to set trusted.name3"
6640         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6641             grep "trusted.name" | wc -l) -eq 3 ] ||
6642                 error "$testfile missing 3 trusted.name xattrs"
6643
6644         setfattr -n user.author2 -v author2 $testfile ||
6645                 error "$testfile unable to set user.author2"
6646         setfattr -n user.author3 -v author3 $testfile ||
6647                 error "$testfile unable to set user.author3"
6648         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6649             grep "user.author" | wc -l) -eq 3 ] ||
6650                 error "$testfile missing 3 user.author xattrs"
6651
6652         echo "remove xattr..."
6653         setfattr -x trusted.name1 $testfile ||
6654                 error "$testfile error deleting trusted.name1"
6655         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6656                 error "$testfile did not delete trusted.name1 xattr"
6657
6658         setfattr -x user.author1 $testfile ||
6659                 error "$testfile error deleting user.author1"
6660         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6661                 error "$testfile did not delete trusted.name1 xattr"
6662
6663         # b10667: setting lustre special xattr be silently discarded
6664         echo "set lustre special xattr ..."
6665         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6666                 error "$testfile allowed setting trusted.lov"
6667 }
6668 run_test 102a "user xattr test =================================="
6669
6670 test_102b() {
6671         [ -z "$(which setfattr 2>/dev/null)" ] &&
6672                 skip_env "could not find setfattr" && return
6673
6674         # b10930: get/set/list trusted.lov xattr
6675         echo "get/set/list trusted.lov xattr ..."
6676         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6677         local testfile=$DIR/$tfile
6678         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6679                 error "setstripe failed"
6680         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6681                 error "getstripe failed"
6682         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6683                 error "can't get trusted.lov from $testfile"
6684
6685         local testfile2=${testfile}2
6686         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6687                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6688
6689         $MCREATE $testfile2
6690         setfattr -n trusted.lov -v $value $testfile2
6691         local stripe_size=$($GETSTRIPE -S $testfile2)
6692         local stripe_count=$($GETSTRIPE -c $testfile2)
6693         [[ $stripe_size -eq 65536 ]] ||
6694                 error "stripe size $stripe_size != 65536"
6695         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6696                 error "stripe count $stripe_count != $STRIPECOUNT"
6697         rm -f $DIR/$tfile
6698 }
6699 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6700
6701 test_102c() {
6702         [ -z "$(which setfattr 2>/dev/null)" ] &&
6703                 skip_env "could not find setfattr" && return
6704
6705         # b10930: get/set/list lustre.lov xattr
6706         echo "get/set/list lustre.lov xattr ..."
6707         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6708         test_mkdir -p $DIR/$tdir
6709         chown $RUNAS_ID $DIR/$tdir
6710         local testfile=$DIR/$tdir/$tfile
6711         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6712                 error "setstripe failed"
6713         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6714                 error "getstripe failed"
6715         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6716         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6717
6718         local testfile2=${testfile}2
6719         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6720                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6721
6722         $RUNAS $MCREATE $testfile2
6723         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6724         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6725         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6726         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6727         [ $stripe_count -eq $STRIPECOUNT ] ||
6728                 error "stripe count $stripe_count != $STRIPECOUNT"
6729 }
6730 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6731
6732 compare_stripe_info1() {
6733         local stripe_index_all_zero=true
6734
6735         for num in 1 2 3 4; do
6736                 for count in $(seq 1 $STRIPE_COUNT); do
6737                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6738                                 local size=$((STRIPE_SIZE * num))
6739                                 local file=file"$num-$offset-$count"
6740                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6741                                 [[ $stripe_size -ne $size ]] &&
6742                                     error "$file: size $stripe_size != $size"
6743                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6744                                 # allow fewer stripes to be created, ORI-601
6745                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6746                                     error "$file: count $stripe_count != $count"
6747                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6748                                 [[ $stripe_index -ne 0 ]] &&
6749                                         stripe_index_all_zero=false
6750                         done
6751                 done
6752         done
6753         $stripe_index_all_zero &&
6754                 error "all files are being extracted starting from OST index 0"
6755         return 0
6756 }
6757
6758 find_lustre_tar() {
6759         [ -n "$(which tar 2>/dev/null)" ] &&
6760                 strings $(which tar) | grep -q "lustre" && echo tar
6761 }
6762
6763 test_102d() {
6764         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6765         # b10930: tar test for trusted.lov xattr
6766         TAR=$(find_lustre_tar)
6767         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6768         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6769         setup_test102
6770         test_mkdir -p $DIR/d102d
6771         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6772         cd $DIR/d102d/$tdir
6773         compare_stripe_info1
6774 }
6775 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6776
6777 test_102f() {
6778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6779         # b10930: tar test for trusted.lov xattr
6780         TAR=$(find_lustre_tar)
6781         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6782         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6783         setup_test102
6784         test_mkdir -p $DIR/d102f
6785         cd $DIR
6786         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6787         cd $DIR/d102f/$tdir
6788         compare_stripe_info1
6789 }
6790 run_test 102f "tar copy files, not keep osts ==========="
6791
6792 grow_xattr() {
6793         local xsize=${1:-1024}  # in bytes
6794         local file=$DIR/$tfile
6795
6796         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
6797                 skip "must have user_xattr" && return 0
6798         [ -z "$(which setfattr 2>/dev/null)" ] &&
6799                 skip_env "could not find setfattr" && return 0
6800         [ -z "$(which getfattr 2>/dev/null)" ] &&
6801                 skip_env "could not find getfattr" && return 0
6802
6803         touch $file
6804
6805         local value="$(generate_string $xsize)"
6806
6807         local xbig=trusted.big
6808         log "save $xbig on $file"
6809         setfattr -n $xbig -v $value $file ||
6810                 error "saving $xbig on $file failed"
6811
6812         local orig=$(get_xattr_value $xbig $file)
6813         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6814
6815         local xsml=trusted.sml
6816         log "save $xsml on $file"
6817         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6818
6819         local new=$(get_xattr_value $xbig $file)
6820         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6821
6822         log "grow $xsml on $file"
6823         setfattr -n $xsml -v "$value" $file ||
6824                 error "growing $xsml on $file failed"
6825
6826         new=$(get_xattr_value $xbig $file)
6827         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6828         log "$xbig still valid after growing $xsml"
6829
6830         rm -f $file
6831 }
6832
6833 test_102h() { # bug 15777
6834         grow_xattr 1024
6835 }
6836 run_test 102h "grow xattr from inside inode to external block"
6837
6838 test_102ha() {
6839         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6840         grow_xattr $(max_xattr_size)
6841 }
6842 run_test 102ha "grow xattr from inside inode to external inode"
6843
6844 test_102i() { # bug 17038
6845         [ -z "$(which getfattr 2>/dev/null)" ] &&
6846                 skip "could not find getfattr" && return
6847         touch $DIR/$tfile
6848         ln -s $DIR/$tfile $DIR/${tfile}link
6849         getfattr -n trusted.lov $DIR/$tfile ||
6850                 error "lgetxattr on $DIR/$tfile failed"
6851         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6852                 grep -i "no such attr" ||
6853                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6854         rm -f $DIR/$tfile $DIR/${tfile}link
6855 }
6856 run_test 102i "lgetxattr test on symbolic link ============"
6857
6858 test_102j() {
6859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6860         TAR=$(find_lustre_tar)
6861         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6862         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6863         setup_test102 "$RUNAS"
6864         test_mkdir -p $DIR/d102j
6865         chown $RUNAS_ID $DIR/d102j
6866         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6867         cd $DIR/d102j/$tdir
6868         compare_stripe_info1 "$RUNAS"
6869 }
6870 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6871
6872 test_102k() {
6873         [ -z "$(which setfattr 2>/dev/null)" ] &&
6874                 skip "could not find setfattr" && return
6875         touch $DIR/$tfile
6876         # b22187 just check that does not crash for regular file.
6877         setfattr -n trusted.lov $DIR/$tfile
6878         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6879         local test_kdir=$DIR/d102k
6880         test_mkdir $test_kdir
6881         local default_size=`$GETSTRIPE -S $test_kdir`
6882         local default_count=`$GETSTRIPE -c $test_kdir`
6883         local default_offset=`$GETSTRIPE -i $test_kdir`
6884         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6885                 error 'dir setstripe failed'
6886         setfattr -n trusted.lov $test_kdir
6887         local stripe_size=`$GETSTRIPE -S $test_kdir`
6888         local stripe_count=`$GETSTRIPE -c $test_kdir`
6889         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6890         [ $stripe_size -eq $default_size ] ||
6891                 error "stripe size $stripe_size != $default_size"
6892         [ $stripe_count -eq $default_count ] ||
6893                 error "stripe count $stripe_count != $default_count"
6894         [ $stripe_offset -eq $default_offset ] ||
6895                 error "stripe offset $stripe_offset != $default_offset"
6896         rm -rf $DIR/$tfile $test_kdir
6897 }
6898 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6899
6900 test_102l() {
6901         [ -z "$(which getfattr 2>/dev/null)" ] &&
6902                 skip "could not find getfattr" && return
6903
6904         # LU-532 trusted. xattr is invisible to non-root
6905         local testfile=$DIR/$tfile
6906
6907         touch $testfile
6908
6909         echo "listxattr as user..."
6910         chown $RUNAS_ID $testfile
6911         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6912             grep -q "trusted" &&
6913                 error "$testfile trusted xattrs are user visible"
6914
6915         return 0;
6916 }
6917 run_test 102l "listxattr size test =================================="
6918
6919 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6920         local path=$DIR/$tfile
6921         touch $path
6922
6923         listxattr_size_check $path || error "listattr_size_check $path failed"
6924 }
6925 run_test 102m "Ensure listxattr fails on small bufffer ========"
6926
6927 cleanup_test102
6928
6929 getxattr() { # getxattr path name
6930         # Return the base64 encoding of the value of xattr name on path.
6931         local path=$1
6932         local name=$2
6933
6934         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6935         # file: $path
6936         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6937         #
6938         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6939
6940         getfattr --absolute-names --encoding=base64 --name=$name $path |
6941                 awk -F= -v name=$name '$1 == name {
6942                         print substr($0, index($0, "=") + 1);
6943         }'
6944 }
6945
6946 test_102n() { # LU-4101 mdt: protect internal xattrs
6947         local file0=$DIR/$tfile.0
6948         local file1=$DIR/$tfile.1
6949         local xattr0=$TMP/$tfile.0
6950         local xattr1=$TMP/$tfile.1
6951         local name
6952         local value
6953
6954         [ -z "$(which setfattr 2>/dev/null)" ] &&
6955                 skip "could not find setfattr" && return
6956
6957         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6958         then
6959                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6960                 return
6961         fi
6962
6963         rm -rf $file0 $file1 $xattr0 $xattr1
6964         touch $file0 $file1
6965
6966         # Get 'before' xattrs of $file1.
6967         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6968
6969         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6970                 # Try to copy xattr from $file0 to $file1.
6971                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6972
6973                 setfattr --name=trusted.$name --value="$value" $file1 ||
6974                         error "setxattr 'trusted.$name' failed"
6975
6976                 # Try to set a garbage xattr.
6977                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6978
6979                 setfattr --name=trusted.$name --value="$value" $file1 ||
6980                         error "setxattr 'trusted.$name' failed"
6981
6982                 # Try to remove the xattr from $file1. We don't care if this
6983                 # appears to succeed or fail, we just don't want there to be
6984                 # any changes or crashes.
6985                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6986         done
6987
6988         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6989         then
6990                 name="lfsck_ns"
6991                 # Try to copy xattr from $file0 to $file1.
6992                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6993
6994                 setfattr --name=trusted.$name --value="$value" $file1 ||
6995                         error "setxattr 'trusted.$name' failed"
6996
6997                 # Try to set a garbage xattr.
6998                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6999
7000                 setfattr --name=trusted.$name --value="$value" $file1 ||
7001                         error "setxattr 'trusted.$name' failed"
7002
7003                 # Try to remove the xattr from $file1. We don't care if this
7004                 # appears to succeed or fail, we just don't want there to be
7005                 # any changes or crashes.
7006                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7007         fi
7008
7009         # Get 'after' xattrs of file1.
7010         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7011
7012         if ! diff $xattr0 $xattr1; then
7013                 error "before and after xattrs of '$file1' differ"
7014         fi
7015
7016         rm -rf $file0 $file1 $xattr0 $xattr1
7017
7018         return 0
7019 }
7020 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7021
7022 test_102p() { # LU-4703 setxattr did not check ownership
7023         local testfile=$DIR/$tfile
7024
7025         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7026                 skip "MDS needs to be at least 2.5.56" && return
7027
7028         touch $testfile
7029
7030         echo "setfacl as user..."
7031         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7032         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7033
7034         echo "setfattr as user..."
7035         setfacl -m "u:$RUNAS_ID:---" $testfile
7036         $RUNAS setfattr -x system.posix_acl_access $testfile
7037         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7038 }
7039 run_test 102p "check setxattr(2) correctly fails without permission"
7040
7041 test_102q() {
7042         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7043                 skip "MDS needs to be at least 2.6.92" && return
7044         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7045 }
7046 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7047
7048 test_102r() {
7049         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7050                 skip "MDS needs to be at least 2.6.93" && return
7051         touch $DIR/$tfile || error "touch"
7052         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7053         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7054         rm $DIR/$tfile || error "rm"
7055
7056         #normal directory
7057         mkdir -p $DIR/$tdir || error "mkdir"
7058         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7059         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7060         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7061                 error "$testfile error deleting user.author1"
7062         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7063                 grep "user.$(basename $tdir)" &&
7064                 error "$tdir did not delete user.$(basename $tdir)"
7065         rmdir $DIR/$tdir || error "rmdir"
7066
7067         #striped directory
7068         test_mkdir -p $DIR/$tdir || error "make striped dir"
7069         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7070         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7071         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7072                 error "$testfile error deleting user.author1"
7073         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7074                 grep "user.$(basename $tdir)" &&
7075                 error "$tdir did not delete user.$(basename $tdir)"
7076         rmdir $DIR/$tdir || error "rm striped dir"
7077 }
7078 run_test 102r "set EAs with empty values"
7079
7080 run_acl_subtest()
7081 {
7082     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7083     return $?
7084 }
7085
7086 test_103a() {
7087         [ "$UID" != 0 ] && skip_env "must run as root" && return
7088         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7089                 skip "must have acl enabled" && return
7090         [ -z "$(which setfacl 2>/dev/null)" ] &&
7091                 skip_env "could not find setfacl" && return
7092         $GSS && skip "could not run under gss" && return
7093
7094         gpasswd -a daemon bin                           # LU-5641
7095         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7096
7097         declare -a identity_old
7098
7099         for num in $(seq $MDSCOUNT); do
7100                 switch_identity $num true || identity_old[$num]=$?
7101         done
7102
7103         SAVE_UMASK=$(umask)
7104         umask 0022
7105         cd $DIR
7106
7107         echo "performing cp ..."
7108         run_acl_subtest cp || error "run_acl_subtest cp failed"
7109         echo "performing getfacl-noacl..."
7110         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7111         echo "performing misc..."
7112         run_acl_subtest misc || error  "misc test failed"
7113         echo "performing permissions..."
7114         run_acl_subtest permissions || error "permissions failed"
7115         echo "performing setfacl..."
7116         run_acl_subtest setfacl || error  "setfacl test failed"
7117
7118         # inheritance test got from HP
7119         echo "performing inheritance..."
7120         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7121         chmod +x make-tree || error "chmod +x failed"
7122         run_acl_subtest inheritance || error "inheritance test failed"
7123         rm -f make-tree
7124
7125         echo "LU-974 ignore umask when acl is enabled..."
7126         run_acl_subtest 974 || error "LU-974 umask test failed"
7127         if [ $MDSCOUNT -ge 2 ]; then
7128                 run_acl_subtest 974_remote ||
7129                         error "LU-974 umask test failed under remote dir"
7130         fi
7131
7132         echo "LU-2561 newly created file is same size as directory..."
7133         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7134                 run_acl_subtest 2561 || error "LU-2561 test failed"
7135         else
7136                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7137         fi
7138
7139         run_acl_subtest 4924 || error "LU-4924 test failed"
7140
7141         cd $SAVE_PWD
7142         umask $SAVE_UMASK
7143
7144         for num in $(seq $MDSCOUNT); do
7145                 if [ "${identity_old[$num]}" = 1 ]; then
7146                         switch_identity $num false || identity_old[$num]=$?
7147                 fi
7148         done
7149 }
7150 run_test 103a "acl test ========================================="
7151
7152 test_103b() {
7153         local noacl=false
7154         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7155         local mountopts=$MDS_MOUNT_OPTS
7156
7157         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7158                 noacl=true
7159         else
7160                 # stop the MDT
7161                 stop $SINGLEMDS || error "failed to stop MDT."
7162                 # remount the MDT
7163                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7164                         MDS_MOUNT_OPTS="-o noacl"
7165                 else
7166                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7167                 fi
7168                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7169                         error "failed to start MDT."
7170                 MDS_MOUNT_OPTS=$mountopts
7171         fi
7172
7173         touch $DIR/$tfile
7174         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7175
7176         if ! $noacl; then
7177                 # stop the MDT
7178                 stop $SINGLEMDS || error "failed to stop MDT."
7179                 # remount the MDT
7180                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7181                         error "failed to start MDT."
7182         fi
7183
7184         true
7185 }
7186 run_test 103b "MDS mount option 'noacl'"
7187
7188 test_103c() {
7189         mkdir -p $DIR/$tdir
7190         cp -rp $DIR/$tdir $DIR/$tdir.bak
7191
7192         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7193                 error "$DIR/$tdir shouldn't contain default ACL"
7194         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7195                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7196         true
7197 }
7198 run_test 103c "'cp -rp' won't set empty acl"
7199
7200 test_104a() {
7201         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7202         touch $DIR/$tfile
7203         lfs df || error "lfs df failed"
7204         lfs df -ih || error "lfs df -ih failed"
7205         lfs df -h $DIR || error "lfs df -h $DIR failed"
7206         lfs df -i $DIR || error "lfs df -i $DIR failed"
7207         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7208         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7209
7210         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7211         lctl --device %$OSC deactivate
7212         lfs df || error "lfs df with deactivated OSC failed"
7213         lctl --device %$OSC activate
7214         # wait the osc back to normal
7215         wait_osc_import_state client ost FULL
7216
7217         lfs df || error "lfs df with reactivated OSC failed"
7218         rm -f $DIR/$tfile
7219 }
7220 run_test 104a "lfs df [-ih] [path] test ========================="
7221
7222 test_104b() {
7223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7224         [ $RUNAS_ID -eq $UID ] &&
7225                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7226         chmod 666 /dev/obd
7227         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7228                         grep "Permission denied" | wc -l)))
7229         if [ $denied_cnt -ne 0 ]; then
7230                 error "lfs check servers test failed"
7231         fi
7232 }
7233 run_test 104b "$RUNAS lfs check servers test ===================="
7234
7235 test_105a() {
7236         # doesn't work on 2.4 kernels
7237         touch $DIR/$tfile
7238         if $(flock_is_enabled); then
7239                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7240         else
7241                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7242         fi
7243         rm -f $DIR/$tfile
7244 }
7245 run_test 105a "flock when mounted without -o flock test ========"
7246
7247 test_105b() {
7248         touch $DIR/$tfile
7249         if $(flock_is_enabled); then
7250                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7251         else
7252                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7253         fi
7254         rm -f $DIR/$tfile
7255 }
7256 run_test 105b "fcntl when mounted without -o flock test ========"
7257
7258 test_105c() {
7259         touch $DIR/$tfile
7260         if $(flock_is_enabled); then
7261                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7262         else
7263                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7264         fi
7265         rm -f $DIR/$tfile
7266 }
7267 run_test 105c "lockf when mounted without -o flock test ========"
7268
7269 test_105d() { # bug 15924
7270         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7271         test_mkdir -p $DIR/$tdir
7272         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7273         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7274         $LCTL set_param fail_loc=0x80000315
7275         flocks_test 2 $DIR/$tdir
7276 }
7277 run_test 105d "flock race (should not freeze) ========"
7278
7279 test_105e() { # bug 22660 && 22040
7280         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7281         touch $DIR/$tfile
7282         flocks_test 3 $DIR/$tfile
7283 }
7284 run_test 105e "Two conflicting flocks from same process ======="
7285
7286 test_106() { #bug 10921
7287         test_mkdir -p $DIR/$tdir
7288         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7289         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7290 }
7291 run_test 106 "attempt exec of dir followed by chown of that dir"
7292
7293 test_107() {
7294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7295         CDIR=`pwd`
7296         cd $DIR
7297
7298         local file=core
7299         rm -f $file
7300
7301         local save_pattern=$(sysctl -n kernel.core_pattern)
7302         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7303         sysctl -w kernel.core_pattern=$file
7304         sysctl -w kernel.core_uses_pid=0
7305
7306         ulimit -c unlimited
7307         sleep 60 &
7308         SLEEPPID=$!
7309
7310         sleep 1
7311
7312         kill -s 11 $SLEEPPID
7313         wait $SLEEPPID
7314         if [ -e $file ]; then
7315                 size=`stat -c%s $file`
7316                 [ $size -eq 0 ] && error "Fail to create core file $file"
7317         else
7318                 error "Fail to create core file $file"
7319         fi
7320         rm -f $file
7321         sysctl -w kernel.core_pattern=$save_pattern
7322         sysctl -w kernel.core_uses_pid=$save_uses_pid
7323         cd $CDIR
7324 }
7325 run_test 107 "Coredump on SIG"
7326
7327 test_110() {
7328         test_mkdir -p $DIR/$tdir
7329         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7330                 error "mkdir with 255 char failed"
7331         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7332                 error "mkdir with 256 char should fail, but did not"
7333         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7334                 error "create with 255 char failed"
7335         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7336                 error "create with 256 char should fail, but did not"
7337
7338         ls -l $DIR/$tdir
7339         rm -rf $DIR/$tdir
7340 }
7341 run_test 110 "filename length checking"
7342
7343 test_115() {
7344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7345         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7346                 tail -1 | cut -c11-20)
7347         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7348         echo "Starting with $OSTIO_pre threads"
7349
7350         NUMTEST=20000
7351         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7352         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7353         echo "$NUMTEST creates/unlinks"
7354         test_mkdir -p $DIR/$tdir
7355         createmany -o $DIR/$tdir/$tfile $NUMTEST
7356         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7357
7358         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7359                 tail -1 | cut -c11-20)
7360
7361         # don't return an error
7362         [ $OSTIO_post == $OSTIO_pre ] && echo \
7363             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7364             echo "This may be fine, depending on what ran before this test" &&
7365             echo "and how fast this system is." && return
7366
7367         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7368 }
7369 run_test 115 "verify dynamic thread creation===================="
7370
7371 free_min_max () {
7372         wait_delete_completed
7373         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7374         echo OST kbytes available: ${AVAIL[@]}
7375         MAXI=0; MAXV=${AVAIL[0]}
7376         MINI=0; MINV=${AVAIL[0]}
7377         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7378                 #echo OST $i: ${AVAIL[i]}kb
7379                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7380                         MAXV=${AVAIL[i]}; MAXI=$i
7381                 fi
7382                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7383                         MINV=${AVAIL[i]}; MINI=$i
7384                 fi
7385         done
7386         echo Min free space: OST $MINI: $MINV
7387         echo Max free space: OST $MAXI: $MAXV
7388 }
7389
7390 test_116a() { # was previously test_116()
7391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7392         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7393
7394         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7395
7396         echo -n "Free space priority "
7397         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7398                 head -n1
7399         declare -a AVAIL
7400         free_min_max
7401
7402         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7403         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7404                 && return
7405         trap simple_cleanup_common EXIT
7406
7407
7408         # Check if we need to generate uneven OSTs
7409         test_mkdir -p $DIR/$tdir/OST${MINI}
7410         local FILL=$(($MINV / 4))
7411         local DIFF=$(($MAXV - $MINV))
7412         local DIFF2=$(($DIFF * 100 / $MINV))
7413
7414         local threshold=$(do_facet $SINGLEMDS \
7415                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7416         threshold=${threshold%%%}
7417         echo -n "Check for uneven OSTs: "
7418         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7419
7420         if [[ $DIFF2 -gt $threshold ]]; then
7421                 echo "ok"
7422                 echo "Don't need to fill OST$MINI"
7423         else
7424                 # generate uneven OSTs. Write 2% over the QOS threshold value
7425                 echo "no"
7426                 DIFF=$(($threshold - $DIFF2 + 2))
7427                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7428                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7429                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7430                         error "setstripe failed"
7431                 DIFF=$(($DIFF2 / 2048))
7432                 i=0
7433                 while [ $i -lt $DIFF ]; do
7434                         i=$(($i + 1))
7435                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7436                                 bs=2M count=1 2>/dev/null
7437                         echo -n .
7438                 done
7439                 echo .
7440                 sync
7441                 sleep_maxage
7442                 free_min_max
7443         fi
7444
7445         DIFF=$(($MAXV - $MINV))
7446         DIFF2=$(($DIFF * 100 / $MINV))
7447         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7448         if [[ $DIFF2 -gt $threshold ]]; then
7449                 echo "ok"
7450         else
7451                 echo "failed - QOS mode won't be used"
7452                 skip "QOS imbalance criteria not met"
7453                 simple_cleanup_common
7454                 return
7455         fi
7456
7457         MINI1=$MINI; MINV1=$MINV
7458         MAXI1=$MAXI; MAXV1=$MAXV
7459
7460         # now fill using QOS
7461         $SETSTRIPE -c 1 $DIR/$tdir
7462         FILL=$(($FILL / 200))
7463         if [ $FILL -gt 600 ]; then
7464                 FILL=600
7465         fi
7466         echo "writing $FILL files to QOS-assigned OSTs"
7467         i=0
7468         while [ $i -lt $FILL ]; do
7469                 i=$((i + 1))
7470                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7471                         count=1 2>/dev/null
7472                 echo -n .
7473         done
7474         echo "wrote $i 200k files"
7475         sync
7476         sleep_maxage
7477
7478         echo "Note: free space may not be updated, so measurements might be off"
7479         free_min_max
7480         DIFF2=$(($MAXV - $MINV))
7481         echo "free space delta: orig $DIFF final $DIFF2"
7482         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7483         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7484         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7485         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7486         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7487         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7488         [ $DIFF -gt 0 ] &&
7489                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7490
7491         # Figure out which files were written where
7492         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7493                   awk '/'$MINI1': / {print $2; exit}')
7494         echo $UUID
7495         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7496         echo "$MINC files created on smaller OST $MINI1"
7497         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7498                   awk '/'$MAXI1': / {print $2; exit}')
7499         echo $UUID
7500         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7501         echo "$MAXC files created on larger OST $MAXI1"
7502         FILL=$(($MAXC * 100 / $MINC - 100))
7503         [[ $MINC -gt 0 ]] &&
7504                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7505         [[ $MAXC -gt $MINC ]] ||
7506                 error_ignore LU-9 "stripe QOS didn't balance free space"
7507         simple_cleanup_common
7508 }
7509 run_test 116a "stripe QOS: free space balance ==================="
7510
7511 test_116b() { # LU-2093
7512         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7513         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7514
7515 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7516         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7517                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7518         [ -z "$old_rr" ] && skip "no QOS" && return 0
7519         do_facet $SINGLEMDS lctl set_param \
7520                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7521         mkdir -p $DIR/$tdir
7522         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7523         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7524         do_facet $SINGLEMDS lctl set_param fail_loc=0
7525         rm -rf $DIR/$tdir
7526         do_facet $SINGLEMDS lctl set_param \
7527                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7528 }
7529 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7530
7531 test_117() # bug 10891
7532 {
7533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7534         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7535         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7536         lctl set_param fail_loc=0x21e
7537         > $DIR/$tfile || error "truncate failed"
7538         lctl set_param fail_loc=0
7539         echo "Truncate succeeded."
7540         rm -f $DIR/$tfile
7541 }
7542 run_test 117 "verify osd extend =========="
7543
7544 NO_SLOW_RESENDCOUNT=4
7545 export OLD_RESENDCOUNT=""
7546 set_resend_count () {
7547         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7548         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7549         lctl set_param -n $PROC_RESENDCOUNT $1
7550         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7551 }
7552
7553 # for reduce test_118* time (b=14842)
7554 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7555
7556 # Reset async IO behavior after error case
7557 reset_async() {
7558         FILE=$DIR/reset_async
7559
7560         # Ensure all OSCs are cleared
7561         $SETSTRIPE -c -1 $FILE
7562         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7563         sync
7564         rm $FILE
7565 }
7566
7567 test_118a() #bug 11710
7568 {
7569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7570         reset_async
7571
7572         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7573         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7574         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7575
7576         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7577                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7578                 return 1;
7579         fi
7580         rm -f $DIR/$tfile
7581 }
7582 run_test 118a "verify O_SYNC works =========="
7583
7584 test_118b()
7585 {
7586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7587         remote_ost_nodsh && skip "remote OST with nodsh" && return
7588
7589         reset_async
7590
7591         #define OBD_FAIL_OST_ENOENT 0x217
7592         set_nodes_failloc "$(osts_nodes)" 0x217
7593         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7594         RC=$?
7595         set_nodes_failloc "$(osts_nodes)" 0
7596         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7597         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7598                     grep -c writeback)
7599
7600         if [[ $RC -eq 0 ]]; then
7601                 error "Must return error due to dropped pages, rc=$RC"
7602                 return 1;
7603         fi
7604
7605         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7606                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7607                 return 1;
7608         fi
7609
7610         echo "Dirty pages not leaked on ENOENT"
7611
7612         # Due to the above error the OSC will issue all RPCs syncronously
7613         # until a subsequent RPC completes successfully without error.
7614         $MULTIOP $DIR/$tfile Ow4096yc
7615         rm -f $DIR/$tfile
7616
7617         return 0
7618 }
7619 run_test 118b "Reclaim dirty pages on fatal error =========="
7620
7621 test_118c()
7622 {
7623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7624
7625         # for 118c, restore the original resend count, LU-1940
7626         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7627                                 set_resend_count $OLD_RESENDCOUNT
7628         remote_ost_nodsh && skip "remote OST with nodsh" && return
7629
7630         reset_async
7631
7632         #define OBD_FAIL_OST_EROFS               0x216
7633         set_nodes_failloc "$(osts_nodes)" 0x216
7634
7635         # multiop should block due to fsync until pages are written
7636         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7637         MULTIPID=$!
7638         sleep 1
7639
7640         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7641                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7642         fi
7643
7644         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7645                     grep -c writeback)
7646         if [[ $WRITEBACK -eq 0 ]]; then
7647                 error "No page in writeback, writeback=$WRITEBACK"
7648         fi
7649
7650         set_nodes_failloc "$(osts_nodes)" 0
7651         wait $MULTIPID
7652         RC=$?
7653         if [[ $RC -ne 0 ]]; then
7654                 error "Multiop fsync failed, rc=$RC"
7655         fi
7656
7657         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7658         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7659                     grep -c writeback)
7660         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7661                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7662         fi
7663
7664         rm -f $DIR/$tfile
7665         echo "Dirty pages flushed via fsync on EROFS"
7666         return 0
7667 }
7668 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7669
7670 # continue to use small resend count to reduce test_118* time (b=14842)
7671 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7672
7673 test_118d()
7674 {
7675         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7676         remote_ost_nodsh && skip "remote OST with nodsh" && return
7677
7678         reset_async
7679
7680         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7681         set_nodes_failloc "$(osts_nodes)" 0x214
7682         # multiop should block due to fsync until pages are written
7683         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7684         MULTIPID=$!
7685         sleep 1
7686
7687         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7688                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7689         fi
7690
7691         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7692                     grep -c writeback)
7693         if [[ $WRITEBACK -eq 0 ]]; then
7694                 error "No page in writeback, writeback=$WRITEBACK"
7695         fi
7696
7697         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7698         set_nodes_failloc "$(osts_nodes)" 0
7699
7700         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7701         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7702                     grep -c writeback)
7703         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7704                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7705         fi
7706
7707         rm -f $DIR/$tfile
7708         echo "Dirty pages gaurenteed flushed via fsync"
7709         return 0
7710 }
7711 run_test 118d "Fsync validation inject a delay of the bulk =========="
7712
7713 test_118f() {
7714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7715         reset_async
7716
7717         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7718         lctl set_param fail_loc=0x8000040a
7719
7720         # Should simulate EINVAL error which is fatal
7721         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7722         RC=$?
7723         if [[ $RC -eq 0 ]]; then
7724                 error "Must return error due to dropped pages, rc=$RC"
7725         fi
7726
7727         lctl set_param fail_loc=0x0
7728
7729         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7730         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7731         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7732                     grep -c writeback)
7733         if [[ $LOCKED -ne 0 ]]; then
7734                 error "Locked pages remain in cache, locked=$LOCKED"
7735         fi
7736
7737         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7738                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7739         fi
7740
7741         rm -f $DIR/$tfile
7742         echo "No pages locked after fsync"
7743
7744         reset_async
7745         return 0
7746 }
7747 run_test 118f "Simulate unrecoverable OSC side error =========="
7748
7749 test_118g() {
7750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7751         reset_async
7752
7753         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7754         lctl set_param fail_loc=0x406
7755
7756         # simulate local -ENOMEM
7757         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7758         RC=$?
7759
7760         lctl set_param fail_loc=0
7761         if [[ $RC -eq 0 ]]; then
7762                 error "Must return error due to dropped pages, rc=$RC"
7763         fi
7764
7765         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7766         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7767         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7768                         grep -c writeback)
7769         if [[ $LOCKED -ne 0 ]]; then
7770                 error "Locked pages remain in cache, locked=$LOCKED"
7771         fi
7772
7773         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7774                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7775         fi
7776
7777         rm -f $DIR/$tfile
7778         echo "No pages locked after fsync"
7779
7780         reset_async
7781         return 0
7782 }
7783 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7784
7785 test_118h() {
7786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7787         remote_ost_nodsh && skip "remote OST with nodsh" && return
7788
7789         reset_async
7790
7791         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7792         set_nodes_failloc "$(osts_nodes)" 0x20e
7793         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7794         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7795         RC=$?
7796
7797         set_nodes_failloc "$(osts_nodes)" 0
7798         if [[ $RC -eq 0 ]]; then
7799                 error "Must return error due to dropped pages, rc=$RC"
7800         fi
7801
7802         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7803         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7804         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7805                     grep -c writeback)
7806         if [[ $LOCKED -ne 0 ]]; then
7807                 error "Locked pages remain in cache, locked=$LOCKED"
7808         fi
7809
7810         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7811                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7812         fi
7813
7814         rm -f $DIR/$tfile
7815         echo "No pages locked after fsync"
7816
7817         return 0
7818 }
7819 run_test 118h "Verify timeout in handling recoverables errors  =========="
7820
7821 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7822
7823 test_118i() {
7824         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7825         remote_ost_nodsh && skip "remote OST with nodsh" && return
7826
7827         reset_async
7828
7829         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7830         set_nodes_failloc "$(osts_nodes)" 0x20e
7831
7832         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7833         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7834         PID=$!
7835         sleep 5
7836         set_nodes_failloc "$(osts_nodes)" 0
7837
7838         wait $PID
7839         RC=$?
7840         if [[ $RC -ne 0 ]]; then
7841                 error "got error, but should be not, rc=$RC"
7842         fi
7843
7844         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7845         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7846         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7847         if [[ $LOCKED -ne 0 ]]; then
7848                 error "Locked pages remain in cache, locked=$LOCKED"
7849         fi
7850
7851         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7852                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7853         fi
7854
7855         rm -f $DIR/$tfile
7856         echo "No pages locked after fsync"
7857
7858         return 0
7859 }
7860 run_test 118i "Fix error before timeout in recoverable error  =========="
7861
7862 [ "$SLOW" = "no" ] && set_resend_count 4
7863
7864 test_118j() {
7865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7866         remote_ost_nodsh && skip "remote OST with nodsh" && return
7867
7868         reset_async
7869
7870         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7871         set_nodes_failloc "$(osts_nodes)" 0x220
7872
7873         # return -EIO from OST
7874         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7875         RC=$?
7876         set_nodes_failloc "$(osts_nodes)" 0x0
7877         if [[ $RC -eq 0 ]]; then
7878                 error "Must return error due to dropped pages, rc=$RC"
7879         fi
7880
7881         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7882         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7883         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7884         if [[ $LOCKED -ne 0 ]]; then
7885                 error "Locked pages remain in cache, locked=$LOCKED"
7886         fi
7887
7888         # in recoverable error on OST we want resend and stay until it finished
7889         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7890                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7891         fi
7892
7893         rm -f $DIR/$tfile
7894         echo "No pages locked after fsync"
7895
7896         return 0
7897 }
7898 run_test 118j "Simulate unrecoverable OST side error =========="
7899
7900 test_118k()
7901 {
7902         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7903         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7904
7905         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7906         set_nodes_failloc "$(osts_nodes)" 0x20e
7907         test_mkdir -p $DIR/$tdir
7908
7909         for ((i=0;i<10;i++)); do
7910                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7911                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7912                 SLEEPPID=$!
7913                 sleep 0.500s
7914                 kill $SLEEPPID
7915                 wait $SLEEPPID
7916         done
7917
7918         set_nodes_failloc "$(osts_nodes)" 0
7919         rm -rf $DIR/$tdir
7920 }
7921 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7922
7923 test_118l()
7924 {
7925         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7926         # LU-646
7927         test_mkdir -p $DIR/$tdir
7928         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7929         rm -rf $DIR/$tdir
7930 }
7931 run_test 118l "fsync dir ========="
7932
7933 test_118m() # LU-3066
7934 {
7935         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7936         test_mkdir -p $DIR/$tdir
7937         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7938         rm -rf $DIR/$tdir
7939 }
7940 run_test 118m "fdatasync dir ========="
7941
7942 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7943
7944 test_119a() # bug 11737
7945 {
7946         BSIZE=$((512 * 1024))
7947         directio write $DIR/$tfile 0 1 $BSIZE
7948         # We ask to read two blocks, which is more than a file size.
7949         # directio will indicate an error when requested and actual
7950         # sizes aren't equeal (a normal situation in this case) and
7951         # print actual read amount.
7952         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7953         if [ "$NOB" != "$BSIZE" ]; then
7954                 error "read $NOB bytes instead of $BSIZE"
7955         fi
7956         rm -f $DIR/$tfile
7957 }
7958 run_test 119a "Short directIO read must return actual read amount"
7959
7960 test_119b() # bug 11737
7961 {
7962         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7963
7964         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7965         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7966         sync
7967         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7968                 error "direct read failed"
7969         rm -f $DIR/$tfile
7970 }
7971 run_test 119b "Sparse directIO read must return actual read amount"
7972
7973 test_119c() # bug 13099
7974 {
7975         BSIZE=1048576
7976         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7977         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7978         rm -f $DIR/$tfile
7979 }
7980 run_test 119c "Testing for direct read hitting hole"
7981
7982 test_119d() # bug 15950
7983 {
7984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7985         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7986         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7987         BSIZE=1048576
7988         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7989         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7990         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7991         lctl set_param fail_loc=0x40d
7992         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7993         pid_dio=$!
7994         sleep 1
7995         cat $DIR/$tfile > /dev/null &
7996         lctl set_param fail_loc=0
7997         pid_reads=$!
7998         wait $pid_dio
7999         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8000         sleep 2
8001         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8002         error "the read rpcs have not completed in 2s"
8003         rm -f $DIR/$tfile
8004         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8005 }
8006 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8007
8008 test_120a() {
8009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8010         test_mkdir -p $DIR/$tdir
8011         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8012                skip "no early lock cancel on server" && return 0
8013
8014         lru_resize_disable mdc
8015         lru_resize_disable osc
8016         cancel_lru_locks mdc
8017         # asynchronous object destroy at MDT could cause bl ast to client
8018         cancel_lru_locks osc
8019
8020         stat $DIR/$tdir > /dev/null
8021         can1=$(do_facet $SINGLEMDS \
8022                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8023                awk '/ldlm_cancel/ {print $2}')
8024         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8025                awk '/ldlm_bl_callback/ {print $2}')
8026         test_mkdir -c1 $DIR/$tdir/d1
8027         can2=$(do_facet $SINGLEMDS \
8028                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8029                awk '/ldlm_cancel/ {print $2}')
8030         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8031                awk '/ldlm_bl_callback/ {print $2}')
8032         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8033         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8034         lru_resize_enable mdc
8035         lru_resize_enable osc
8036 }
8037 run_test 120a "Early Lock Cancel: mkdir test"
8038
8039 test_120b() {
8040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8041         test_mkdir $DIR/$tdir
8042         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8043                skip "no early lock cancel on server" && return 0
8044         lru_resize_disable mdc
8045         lru_resize_disable osc
8046         cancel_lru_locks mdc
8047         stat $DIR/$tdir > /dev/null
8048         can1=$(do_facet $SINGLEMDS \
8049                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8050                awk '/ldlm_cancel/ {print $2}')
8051         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8052                awk '/ldlm_bl_callback/ {print $2}')
8053         touch $DIR/$tdir/f1
8054         can2=$(do_facet $SINGLEMDS \
8055                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8056                awk '/ldlm_cancel/ {print $2}')
8057         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8058                awk '/ldlm_bl_callback/ {print $2}')
8059         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8060         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8061         lru_resize_enable mdc
8062         lru_resize_enable osc
8063 }
8064 run_test 120b "Early Lock Cancel: create test"
8065
8066 test_120c() {
8067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8068         test_mkdir -c1 $DIR/$tdir
8069         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8070                skip "no early lock cancel on server" && return 0
8071         lru_resize_disable mdc
8072         lru_resize_disable osc
8073         test_mkdir -p -c1 $DIR/$tdir/d1
8074         test_mkdir -p -c1 $DIR/$tdir/d2
8075         touch $DIR/$tdir/d1/f1
8076         cancel_lru_locks mdc
8077         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8078         can1=$(do_facet $SINGLEMDS \
8079                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8080                awk '/ldlm_cancel/ {print $2}')
8081         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8082                awk '/ldlm_bl_callback/ {print $2}')
8083         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8084         can2=$(do_facet $SINGLEMDS \
8085                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8086                awk '/ldlm_cancel/ {print $2}')
8087         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8088                awk '/ldlm_bl_callback/ {print $2}')
8089         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8090         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8091         lru_resize_enable mdc
8092         lru_resize_enable osc
8093 }
8094 run_test 120c "Early Lock Cancel: link test"
8095
8096 test_120d() {
8097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8098         test_mkdir -p -c1 $DIR/$tdir
8099         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8100                skip "no early lock cancel on server" && return 0
8101         lru_resize_disable mdc
8102         lru_resize_disable osc
8103         touch $DIR/$tdir
8104         cancel_lru_locks mdc
8105         stat $DIR/$tdir > /dev/null
8106         can1=$(do_facet $SINGLEMDS \
8107                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8108                awk '/ldlm_cancel/ {print $2}')
8109         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8110                awk '/ldlm_bl_callback/ {print $2}')
8111         chmod a+x $DIR/$tdir
8112         can2=$(do_facet $SINGLEMDS \
8113                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8114                awk '/ldlm_cancel/ {print $2}')
8115         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8116                awk '/ldlm_bl_callback/ {print $2}')
8117         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8118         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8119         lru_resize_enable mdc
8120         lru_resize_enable osc
8121 }
8122 run_test 120d "Early Lock Cancel: setattr test"
8123
8124 test_120e() {
8125         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8126         test_mkdir -p -c1 $DIR/$tdir
8127         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8128                skip "no early lock cancel on server" && return 0
8129         lru_resize_disable mdc
8130         lru_resize_disable osc
8131         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8132         cancel_lru_locks mdc
8133         cancel_lru_locks osc
8134         dd if=$DIR/$tdir/f1 of=/dev/null
8135         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8136         # XXX client can not do early lock cancel of OST lock
8137         # during unlink (LU-4206), so cancel osc lock now.
8138         cancel_lru_locks osc
8139         can1=$(do_facet $SINGLEMDS \
8140                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8141                awk '/ldlm_cancel/ {print $2}')
8142         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8143                awk '/ldlm_bl_callback/ {print $2}')
8144         unlink $DIR/$tdir/f1
8145         sleep 5
8146         can2=$(do_facet $SINGLEMDS \
8147                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8148                awk '/ldlm_cancel/ {print $2}')
8149         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8150                awk '/ldlm_bl_callback/ {print $2}')
8151         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8152         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8153         lru_resize_enable mdc
8154         lru_resize_enable osc
8155 }
8156 run_test 120e "Early Lock Cancel: unlink test"
8157
8158 test_120f() {
8159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8160         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8161                skip "no early lock cancel on server" && return 0
8162         test_mkdir -p -c1 $DIR/$tdir
8163         lru_resize_disable mdc
8164         lru_resize_disable osc
8165         test_mkdir -p -c1 $DIR/$tdir/d1
8166         test_mkdir -p -c1 $DIR/$tdir/d2
8167         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8168         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8169         cancel_lru_locks mdc
8170         cancel_lru_locks osc
8171         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8172         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8173         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8174         # XXX client can not do early lock cancel of OST lock
8175         # during rename (LU-4206), so cancel osc lock now.
8176         cancel_lru_locks osc
8177         can1=$(do_facet $SINGLEMDS \
8178                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8179                awk '/ldlm_cancel/ {print $2}')
8180         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8181                awk '/ldlm_bl_callback/ {print $2}')
8182         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8183         sleep 5
8184         can2=$(do_facet $SINGLEMDS \
8185                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8186                awk '/ldlm_cancel/ {print $2}')
8187         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8188                awk '/ldlm_bl_callback/ {print $2}')
8189         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8190         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8191         lru_resize_enable mdc
8192         lru_resize_enable osc
8193 }
8194 run_test 120f "Early Lock Cancel: rename test"
8195
8196 test_120g() {
8197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8198         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8199                skip "no early lock cancel on server" && return 0
8200         lru_resize_disable mdc
8201         lru_resize_disable osc
8202         count=10000
8203         echo create $count files
8204         test_mkdir -p $DIR/$tdir
8205         cancel_lru_locks mdc
8206         cancel_lru_locks osc
8207         t0=`date +%s`
8208
8209         can0=$(do_facet $SINGLEMDS \
8210                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8211                awk '/ldlm_cancel/ {print $2}')
8212         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8213                awk '/ldlm_bl_callback/ {print $2}')
8214         createmany -o $DIR/$tdir/f $count
8215         sync
8216         can1=$(do_facet $SINGLEMDS \
8217                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8218                awk '/ldlm_cancel/ {print $2}')
8219         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8220                awk '/ldlm_bl_callback/ {print $2}')
8221         t1=$(date +%s)
8222         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8223         echo rm $count files
8224         rm -r $DIR/$tdir
8225         sync
8226         can2=$(do_facet $SINGLEMDS \
8227                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8228                awk '/ldlm_cancel/ {print $2}')
8229         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8230                awk '/ldlm_bl_callback/ {print $2}')
8231         t2=$(date +%s)
8232         echo total: $count removes in $((t2-t1))
8233         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8234         sleep 2
8235         # wait for commitment of removal
8236         lru_resize_enable mdc
8237         lru_resize_enable osc
8238 }
8239 run_test 120g "Early Lock Cancel: performance test"
8240
8241 test_121() { #bug #10589
8242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8243         rm -rf $DIR/$tfile
8244         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8245 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8246         lctl set_param fail_loc=0x310
8247         cancel_lru_locks osc > /dev/null
8248         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8249         lctl set_param fail_loc=0
8250         [[ $reads -eq $writes ]] ||
8251                 error "read $reads blocks, must be $writes blocks"
8252 }
8253 run_test 121 "read cancel race ========="
8254
8255 test_123a() { # was test 123, statahead(bug 11401)
8256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8257         SLOWOK=0
8258         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8259             log "testing on UP system. Performance may be not as good as expected."
8260                         SLOWOK=1
8261         fi
8262
8263         rm -rf $DIR/$tdir
8264         test_mkdir -p $DIR/$tdir
8265         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8266         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8267         MULT=10
8268         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8269                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8270
8271                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8272                 lctl set_param -n llite.*.statahead_max 0
8273                 lctl get_param llite.*.statahead_max
8274                 cancel_lru_locks mdc
8275                 cancel_lru_locks osc
8276                 stime=`date +%s`
8277                 time ls -l $DIR/$tdir | wc -l
8278                 etime=`date +%s`
8279                 delta=$((etime - stime))
8280                 log "ls $i files without statahead: $delta sec"
8281                 lctl set_param llite.*.statahead_max=$max
8282
8283                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8284                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8285                 cancel_lru_locks mdc
8286                 cancel_lru_locks osc
8287                 stime=`date +%s`
8288                 time ls -l $DIR/$tdir | wc -l
8289                 etime=`date +%s`
8290                 delta_sa=$((etime - stime))
8291                 log "ls $i files with statahead: $delta_sa sec"
8292                 lctl get_param -n llite.*.statahead_stats
8293                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8294
8295                 [[ $swrong -lt $ewrong ]] &&
8296                         log "statahead was stopped, maybe too many locks held!"
8297                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8298
8299                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8300                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8301                     lctl set_param -n llite.*.statahead_max 0
8302                     lctl get_param llite.*.statahead_max
8303                     cancel_lru_locks mdc
8304                     cancel_lru_locks osc
8305                     stime=`date +%s`
8306                     time ls -l $DIR/$tdir | wc -l
8307                     etime=`date +%s`
8308                     delta=$((etime - stime))
8309                     log "ls $i files again without statahead: $delta sec"
8310                     lctl set_param llite.*.statahead_max=$max
8311                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8312                         if [  $SLOWOK -eq 0 ]; then
8313                                 error "ls $i files is slower with statahead!"
8314                         else
8315                                 log "ls $i files is slower with statahead!"
8316                         fi
8317                         break
8318                     fi
8319                 fi
8320
8321                 [ $delta -gt 20 ] && break
8322                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8323                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8324         done
8325         log "ls done"
8326
8327         stime=`date +%s`
8328         rm -r $DIR/$tdir
8329         sync
8330         etime=`date +%s`
8331         delta=$((etime - stime))
8332         log "rm -r $DIR/$tdir/: $delta seconds"
8333         log "rm done"
8334         lctl get_param -n llite.*.statahead_stats
8335 }
8336 run_test 123a "verify statahead work"
8337
8338 test_123b () { # statahead(bug 15027)
8339         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8340         test_mkdir -p $DIR/$tdir
8341         createmany -o $DIR/$tdir/$tfile-%d 1000
8342
8343         cancel_lru_locks mdc
8344         cancel_lru_locks osc
8345
8346 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8347         lctl set_param fail_loc=0x80000803
8348         ls -lR $DIR/$tdir > /dev/null
8349         log "ls done"
8350         lctl set_param fail_loc=0x0
8351         lctl get_param -n llite.*.statahead_stats
8352         rm -r $DIR/$tdir
8353         sync
8354
8355 }
8356 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8357
8358 test_124a() {
8359         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8360         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8361                 skip "no lru resize on server" && return 0
8362         local NR=2000
8363         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8364
8365         log "create $NR files at $DIR/$tdir"
8366         createmany -o $DIR/$tdir/f $NR ||
8367                 error "failed to create $NR files in $DIR/$tdir"
8368
8369         cancel_lru_locks mdc
8370         ls -l $DIR/$tdir > /dev/null
8371
8372         local NSDIR=""
8373         local LRU_SIZE=0
8374         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8375                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8376                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8377                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8378                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8379                         log "NSDIR=$NSDIR"
8380                         log "NS=$(basename $NSDIR)"
8381                         break
8382                 fi
8383         done
8384
8385         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8386                 skip "Not enough cached locks created!"
8387                 return 0
8388         fi
8389         log "LRU=$LRU_SIZE"
8390
8391         local SLEEP=30
8392
8393         # We know that lru resize allows one client to hold $LIMIT locks
8394         # for 10h. After that locks begin to be killed by client.
8395         local MAX_HRS=10
8396         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8397         log "LIMIT=$LIMIT"
8398         if [ $LIMIT -lt $LRU_SIZE ]; then
8399             skip "Limit is too small $LIMIT"
8400             return 0
8401         fi
8402
8403         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8404         # killing locks. Some time was spent for creating locks. This means
8405         # that up to the moment of sleep finish we must have killed some of
8406         # them (10-100 locks). This depends on how fast ther were created.
8407         # Many of them were touched in almost the same moment and thus will
8408         # be killed in groups.
8409         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8410
8411         # Use $LRU_SIZE_B here to take into account real number of locks
8412         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8413         local LRU_SIZE_B=$LRU_SIZE
8414         log "LVF=$LVF"
8415         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8416         log "OLD_LVF=$OLD_LVF"
8417         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8418
8419         # Let's make sure that we really have some margin. Client checks
8420         # cached locks every 10 sec.
8421         SLEEP=$((SLEEP+20))
8422         log "Sleep ${SLEEP} sec"
8423         local SEC=0
8424         while ((SEC<$SLEEP)); do
8425                 echo -n "..."
8426                 sleep 5
8427                 SEC=$((SEC+5))
8428                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8429                 echo -n "$LRU_SIZE"
8430         done
8431         echo ""
8432         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8433         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8434
8435         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8436                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8437                 unlinkmany $DIR/$tdir/f $NR
8438                 return
8439         }
8440
8441         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8442         log "unlink $NR files at $DIR/$tdir"
8443         unlinkmany $DIR/$tdir/f $NR
8444 }
8445 run_test 124a "lru resize ======================================="
8446
8447 get_max_pool_limit()
8448 {
8449         local limit=$($LCTL get_param \
8450                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8451         local max=0
8452         for l in $limit; do
8453                 if [[ $l -gt $max ]]; then
8454                         max=$l
8455                 fi
8456         done
8457         echo $max
8458 }
8459
8460 test_124b() {
8461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8462         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8463                 skip "no lru resize on server" && return 0
8464
8465         LIMIT=$(get_max_pool_limit)
8466
8467         NR=$(($(default_lru_size)*20))
8468         if [[ $NR -gt $LIMIT ]]; then
8469                 log "Limit lock number by $LIMIT locks"
8470                 NR=$LIMIT
8471         fi
8472         lru_resize_disable mdc
8473         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8474                 error "failed to create $DIR/$tdir/disable_lru_resize"
8475
8476         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8477         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8478         cancel_lru_locks mdc
8479         stime=`date +%s`
8480         PID=""
8481         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8482         PID="$PID $!"
8483         sleep 2
8484         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8485         PID="$PID $!"
8486         sleep 2
8487         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8488         PID="$PID $!"
8489         wait $PID
8490         etime=`date +%s`
8491         nolruresize_delta=$((etime-stime))
8492         log "ls -la time: $nolruresize_delta seconds"
8493         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8494         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8495
8496         lru_resize_enable mdc
8497         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8498                 error "failed to create $DIR/$tdir/enable_lru_resize"
8499
8500         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8501         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8502         cancel_lru_locks mdc
8503         stime=`date +%s`
8504         PID=""
8505         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8506         PID="$PID $!"
8507         sleep 2
8508         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8509         PID="$PID $!"
8510         sleep 2
8511         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8512         PID="$PID $!"
8513         wait $PID
8514         etime=`date +%s`
8515         lruresize_delta=$((etime-stime))
8516         log "ls -la time: $lruresize_delta seconds"
8517         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8518
8519         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8520                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8521         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8522                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8523         else
8524                 log "lru resize performs the same with no lru resize"
8525         fi
8526         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8527 }
8528 run_test 124b "lru resize (performance test) ======================="
8529
8530 test_124c() {
8531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8532         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8533                 skip "no lru resize on server" && return 0
8534
8535         # cache ununsed locks on client
8536         local nr=100
8537         cancel_lru_locks mdc
8538         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8539         createmany -o $DIR/$tdir/f $nr ||
8540                 error "failed to create $nr files in $DIR/$tdir"
8541         ls -l $DIR/$tdir > /dev/null
8542
8543         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8544         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8545         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8546         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8547         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8548
8549         # set lru_max_age to 1 sec
8550         $LCTL set_param $nsdir.lru_max_age=1000 # jiffies
8551         echo "sleep $((recalc_p * 2)) seconds..."
8552         sleep $((recalc_p * 2))
8553
8554         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8555         # restore lru_max_age
8556         $LCTL set_param -n $nsdir.lru_max_age $max_age
8557         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8558         unlinkmany $DIR/$tdir/f $nr
8559 }
8560 run_test 124c "LRUR cancel very aged locks"
8561
8562 test_125() { # 13358
8563         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8564         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8565         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8566         test_mkdir -p $DIR/d125 || error "mkdir failed"
8567         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8568         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8569         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8570 }
8571 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8572
8573 test_126() { # bug 12829/13455
8574         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8575         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8576         $GSS && skip "must run as gss disabled" && return
8577
8578         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8579         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8580         rm -f $DIR/$tfile
8581         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8582 }
8583 run_test 126 "check that the fsgid provided by the client is taken into account"
8584
8585 test_127a() { # bug 15521
8586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8587         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8588         $LCTL set_param osc.*.stats=0
8589         FSIZE=$((2048 * 1024))
8590         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8591         cancel_lru_locks osc
8592         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8593
8594         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8595         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8596                 echo "got $COUNT $NAME"
8597                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8598                 eval $NAME=$COUNT || error "Wrong proc format"
8599
8600                 case $NAME in
8601                         read_bytes|write_bytes)
8602                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8603                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8604                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8605                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8606                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8607                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8608                                 error "sumsquare is too small: $SUMSQ"
8609                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8610                                 error "sumsquare is too big: $SUMSQ"
8611                         ;;
8612                         *) ;;
8613                 esac
8614         done < $DIR/${tfile}.tmp
8615
8616         #check that we actually got some stats
8617         [ "$read_bytes" ] || error "Missing read_bytes stats"
8618         [ "$write_bytes" ] || error "Missing write_bytes stats"
8619         [ "$read_bytes" != 0 ] || error "no read done"
8620         [ "$write_bytes" != 0 ] || error "no write done"
8621 }
8622 run_test 127a "verify the client stats are sane"
8623
8624 test_127b() { # bug LU-333
8625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8626         $LCTL set_param llite.*.stats=0
8627         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8628         # perform 2 reads and writes so MAX is different from SUM.
8629         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8630         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8631         cancel_lru_locks osc
8632         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8633         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8634
8635         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8636         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8637                 echo "got $COUNT $NAME"
8638                 eval $NAME=$COUNT || error "Wrong proc format"
8639
8640         case $NAME in
8641                 read_bytes)
8642                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8643                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8644                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8645                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8646                         ;;
8647                 write_bytes)
8648                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8649                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8650                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8651                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8652                         ;;
8653                         *) ;;
8654                 esac
8655         done < $TMP/${tfile}.tmp
8656
8657         #check that we actually got some stats
8658         [ "$read_bytes" ] || error "Missing read_bytes stats"
8659         [ "$write_bytes" ] || error "Missing write_bytes stats"
8660         [ "$read_bytes" != 0 ] || error "no read done"
8661         [ "$write_bytes" != 0 ] || error "no write done"
8662 }
8663 run_test 127b "verify the llite client stats are sane"
8664
8665 test_128() { # bug 15212
8666         touch $DIR/$tfile
8667         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8668                 find $DIR/$tfile
8669                 find $DIR/$tfile
8670         EOF
8671
8672         result=$(grep error $TMP/$tfile.log)
8673         rm -f $DIR/$tfile
8674         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8675 }
8676 run_test 128 "interactive lfs for 2 consecutive find's"
8677
8678 set_dir_limits () {
8679         local mntdev
8680         local canondev
8681         local node
8682
8683         local LDPROC=/proc/fs/ldiskfs
8684         local facets=$(get_facets MDS)
8685
8686         for facet in ${facets//,/ }; do
8687                 canondev=$(ldiskfs_canon \
8688                            *.$(convert_facet2label $facet).mntdev $facet)
8689                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8690                                                 LDPROC=/sys/fs/ldiskfs
8691                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8692         done
8693 }
8694
8695 test_129() {
8696         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8697         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8698                 skip "Only applicable to ldiskfs-based MDTs"
8699                 return
8700         fi
8701         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8702         ENOSPC=28
8703         EFBIG=27
8704
8705         rm -rf $DIR/$tdir
8706         test_mkdir -p $DIR/$tdir
8707
8708         # block size of mds1
8709         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8710         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8711         local MAX=$((MDSBLOCKSIZE * 3))
8712         set_dir_limits $MAX
8713         local I=$(stat -c%s "$DIR/$tdir")
8714         local J=0
8715         local STRIPE_COUNT=1
8716         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8717         MAX=$((MAX*STRIPE_COUNT))
8718         while [[ $I -le $MAX ]]; do
8719                 $MULTIOP $DIR/$tdir/$J Oc
8720                 rc=$?
8721                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8722                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8723                 #and EFBIG for previous versions
8724                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8725                         set_dir_limits 0
8726                         echo "return code $rc received as expected"
8727                         multiop $DIR/$tdir/$J Oc ||
8728                                 error_exit "multiop failed w/o dir size limit"
8729
8730                         I=$(stat -c%s "$DIR/$tdir")
8731
8732                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8733                                         $(version_code 2.4.51) ]
8734                         then
8735                                 [[ $I -eq $MAX ]] && return 0
8736                         else
8737                                 [[ $I -gt $MAX ]] && return 0
8738                         fi
8739                         error_exit "current dir size $I, previous limit $MAX"
8740                 elif [ $rc -ne 0 ]; then
8741                         set_dir_limits 0
8742                         error_exit "return code $rc received instead of expected " \
8743                                    "$EFBIG or $ENOSPC, files in dir $I"
8744                 fi
8745                 J=$((J+1))
8746                 I=$(stat -c%s "$DIR/$tdir")
8747         done
8748
8749         set_dir_limits 0
8750         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8751 }
8752 run_test 129 "test directory size limit ========================"
8753
8754 OLDIFS="$IFS"
8755 cleanup_130() {
8756         trap 0
8757         IFS="$OLDIFS"
8758 }
8759
8760 test_130a() {
8761         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8762         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8763                 return
8764
8765         trap cleanup_130 EXIT RETURN
8766
8767         local fm_file=$DIR/$tfile
8768         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8769         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8770                 error "dd failed for $fm_file"
8771
8772         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8773         filefrag -ves $fm_file
8774         RC=$?
8775         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8776                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8777         [ $RC != 0 ] && error "filefrag $fm_file failed"
8778
8779         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8780                       grep -v "ext:" | grep -v "found")
8781         lun=$($GETSTRIPE -i $fm_file)
8782
8783         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8784         IFS=$'\n'
8785         tot_len=0
8786         for line in $filefrag_op
8787         do
8788                 frag_lun=`echo $line | cut -d: -f5`
8789                 ext_len=`echo $line | cut -d: -f4`
8790                 if (( $frag_lun != $lun )); then
8791                         cleanup_130
8792                         error "FIEMAP on 1-stripe file($fm_file) failed"
8793                         return
8794                 fi
8795                 (( tot_len += ext_len ))
8796         done
8797
8798         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8799                 cleanup_130
8800                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8801                 return
8802         fi
8803
8804         cleanup_130
8805
8806         echo "FIEMAP on single striped file succeeded"
8807 }
8808 run_test 130a "FIEMAP (1-stripe file)"
8809
8810 test_130b() {
8811         [ "$OSTCOUNT" -lt "2" ] &&
8812                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8813
8814         [ "$OSTCOUNT" -ge "10" ] &&
8815                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8816
8817         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8818         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8819                 return
8820
8821         trap cleanup_130 EXIT RETURN
8822
8823         local fm_file=$DIR/$tfile
8824         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8825         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8826                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8827
8828         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8829                 error "dd failed on $fm_file"
8830
8831         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8832         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8833                       grep -v "ext:" | grep -v "found")
8834
8835         last_lun=$(echo $filefrag_op | cut -d: -f5)
8836
8837         IFS=$'\n'
8838         tot_len=0
8839         num_luns=1
8840         for line in $filefrag_op
8841         do
8842                 frag_lun=`echo $line | cut -d: -f5`
8843                 ext_len=`echo $line | cut -d: -f4`
8844                 if (( $frag_lun != $last_lun )); then
8845                         if (( tot_len != 1024 )); then
8846                                 cleanup_130
8847                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8848                                 return
8849                         else
8850                                 (( num_luns += 1 ))
8851                                 tot_len=0
8852                         fi
8853                 fi
8854                 (( tot_len += ext_len ))
8855                 last_lun=$frag_lun
8856         done
8857         if (( num_luns != 2 || tot_len != 1024 )); then
8858                 cleanup_130
8859                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8860                 return
8861         fi
8862
8863         cleanup_130
8864
8865         echo "FIEMAP on 2-stripe file succeeded"
8866 }
8867 run_test 130b "FIEMAP (2-stripe file)"
8868
8869 test_130c() {
8870         [ "$OSTCOUNT" -lt "2" ] &&
8871                 skip_env "skipping FIEMAP on 2-stripe file" && return
8872
8873         [ "$OSTCOUNT" -ge "10" ] &&
8874                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8875
8876         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8877         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8878                 return
8879
8880         trap cleanup_130 EXIT RETURN
8881
8882         local fm_file=$DIR/$tfile
8883         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8884         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8885                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8886
8887         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8888
8889         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8890         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8891
8892         last_lun=`echo $filefrag_op | cut -d: -f5`
8893
8894         IFS=$'\n'
8895         tot_len=0
8896         num_luns=1
8897         for line in $filefrag_op
8898         do
8899                 frag_lun=`echo $line | cut -d: -f5`
8900                 ext_len=`echo $line | cut -d: -f4`
8901                 if (( $frag_lun != $last_lun )); then
8902                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8903                         if (( logical != 512 )); then
8904                                 cleanup_130
8905                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8906                                 return
8907                         fi
8908                         if (( tot_len != 512 )); then
8909                                 cleanup_130
8910                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8911                                 return
8912                         else
8913                                 (( num_luns += 1 ))
8914                                 tot_len=0
8915                         fi
8916                 fi
8917                 (( tot_len += ext_len ))
8918                 last_lun=$frag_lun
8919         done
8920         if (( num_luns != 2 || tot_len != 512 )); then
8921                 cleanup_130
8922                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8923                 return
8924         fi
8925
8926         cleanup_130
8927
8928         echo "FIEMAP on 2-stripe file with hole succeeded"
8929 }
8930 run_test 130c "FIEMAP (2-stripe file with hole)"
8931
8932 test_130d() {
8933         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8934
8935         [ "$OSTCOUNT" -ge "10" ] &&
8936                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8937
8938         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8939         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8940
8941         trap cleanup_130 EXIT RETURN
8942
8943         local fm_file=$DIR/$tfile
8944         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8945         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8946                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8947
8948         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8949         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8950                 error "dd failed on $fm_file"
8951
8952         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8953         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8954                 grep -v "ext:" | grep -v "found"`
8955
8956         last_lun=`echo $filefrag_op | cut -d: -f5`
8957
8958         IFS=$'\n'
8959         tot_len=0
8960         num_luns=1
8961         for line in $filefrag_op
8962         do
8963                 frag_lun=`echo $line | cut -d: -f5`
8964                 ext_len=`echo $line | cut -d: -f4`
8965                 if (( $frag_lun != $last_lun )); then
8966                         if (( tot_len != 1024 )); then
8967                                 cleanup_130
8968                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8969                                 return
8970                         else
8971                                 (( num_luns += 1 ))
8972                                 tot_len=0
8973                         fi
8974                 fi
8975                 (( tot_len += ext_len ))
8976                 last_lun=$frag_lun
8977         done
8978         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8979                 cleanup_130
8980                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8981                 return
8982         fi
8983
8984         cleanup_130
8985
8986         echo "FIEMAP on N-stripe file succeeded"
8987 }
8988 run_test 130d "FIEMAP (N-stripe file)"
8989
8990 test_130e() {
8991         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8992
8993         [ "$OSTCOUNT" -ge "10" ] &&
8994                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8995
8996         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8997         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8998
8999         trap cleanup_130 EXIT RETURN
9000
9001         local fm_file=$DIR/$tfile
9002         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9003         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9004                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9005
9006         NUM_BLKS=512
9007         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9008         for ((i = 0; i < $NUM_BLKS; i++))
9009         do
9010                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9011         done
9012
9013         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9014         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
9015
9016         last_lun=`echo $filefrag_op | cut -d: -f5`
9017
9018         IFS=$'\n'
9019         tot_len=0
9020         num_luns=1
9021         for line in $filefrag_op
9022         do
9023                 frag_lun=`echo $line | cut -d: -f5`
9024                 ext_len=`echo $line | cut -d: -f4`
9025                 if (( $frag_lun != $last_lun )); then
9026                         if (( tot_len != $EXPECTED_LEN )); then
9027                                 cleanup_130
9028                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
9029                                 return
9030                         else
9031                                 (( num_luns += 1 ))
9032                                 tot_len=0
9033                         fi
9034                 fi
9035                 (( tot_len += ext_len ))
9036                 last_lun=$frag_lun
9037         done
9038         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9039                 cleanup_130
9040                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
9041                 return
9042         fi
9043
9044         cleanup_130
9045
9046         echo "FIEMAP with continuation calls succeeded"
9047 }
9048 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9049
9050 # Test for writev/readv
9051 test_131a() {
9052         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
9053         error "writev test failed"
9054         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
9055         error "readv failed"
9056         rm -f $DIR/$tfile
9057 }
9058 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9059
9060 test_131b() {
9061         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
9062         error "append writev test failed"
9063         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
9064         error "append writev test failed"
9065         rm -f $DIR/$tfile
9066 }
9067 run_test 131b "test append writev"
9068
9069 test_131c() {
9070         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9071         error "NOT PASS"
9072 }
9073 run_test 131c "test read/write on file w/o objects"
9074
9075 test_131d() {
9076         rwv -f $DIR/$tfile -w -n 1 1572864
9077         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9078         if [ "$NOB" != 1572864 ]; then
9079                 error "Short read filed: read $NOB bytes instead of 1572864"
9080         fi
9081         rm -f $DIR/$tfile
9082 }
9083 run_test 131d "test short read"
9084
9085 test_131e() {
9086         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9087         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9088         error "read hitting hole failed"
9089         rm -f $DIR/$tfile
9090 }
9091 run_test 131e "test read hitting hole"
9092
9093 check_stats() {
9094         local res
9095         local count
9096         case $1 in
9097         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9098                  ;;
9099         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9100                  ;;
9101         *) error "Wrong argument $1" ;;
9102         esac
9103         echo $res
9104         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9105         # if the argument $3 is zero, it means any stat increment is ok.
9106         if [[ $3 -gt 0 ]]; then
9107                 count=$(echo $res | awk '{ print $2 }')
9108                 [[ $count -ne $3 ]] &&
9109                         error "The $2 counter on $1 is wrong - expected $3"
9110         fi
9111 }
9112
9113 test_133a() {
9114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9115         remote_ost_nodsh && skip "remote OST with nodsh" && return
9116         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9117
9118         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9119                 { skip "MDS doesn't support rename stats"; return; }
9120         local testdir=$DIR/${tdir}/stats_testdir
9121         mkdir -p $DIR/${tdir}
9122
9123         # clear stats.
9124         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9125         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9126
9127         # verify mdt stats first.
9128         mkdir ${testdir} || error "mkdir failed"
9129         check_stats $SINGLEMDS "mkdir" 1
9130         touch ${testdir}/${tfile} || "touch failed"
9131         check_stats $SINGLEMDS "open" 1
9132         check_stats $SINGLEMDS "close" 1
9133         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9134         check_stats $SINGLEMDS "mknod" 1
9135         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9136         check_stats $SINGLEMDS "unlink" 1
9137         rm -f ${testdir}/${tfile} || error "file remove failed"
9138         check_stats $SINGLEMDS "unlink" 2
9139
9140         # remove working dir and check mdt stats again.
9141         rmdir ${testdir} || error "rmdir failed"
9142         check_stats $SINGLEMDS "rmdir" 1
9143
9144         local testdir1=$DIR/${tdir}/stats_testdir1
9145         mkdir -p ${testdir}
9146         mkdir -p ${testdir1}
9147         touch ${testdir1}/test1
9148         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9149         check_stats $SINGLEMDS "crossdir_rename" 1
9150
9151         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9152         check_stats $SINGLEMDS "samedir_rename" 1
9153
9154         rm -rf $DIR/${tdir}
9155 }
9156 run_test 133a "Verifying MDT stats ========================================"
9157
9158 test_133b() {
9159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9160         remote_ost_nodsh && skip "remote OST with nodsh" && return
9161         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9162         local testdir=$DIR/${tdir}/stats_testdir
9163         mkdir -p ${testdir} || error "mkdir failed"
9164         touch ${testdir}/${tfile} || "touch failed"
9165         cancel_lru_locks mdc
9166
9167         # clear stats.
9168         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9169         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9170
9171         # extra mdt stats verification.
9172         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9173         check_stats $SINGLEMDS "setattr" 1
9174         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9175         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9176         then            # LU-1740
9177                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9178                 check_stats $SINGLEMDS "getattr" 1
9179         fi
9180         $LFS df || error "lfs failed"
9181         check_stats $SINGLEMDS "statfs" 1
9182
9183         rm -rf $DIR/${tdir}
9184 }
9185 run_test 133b "Verifying extra MDT stats =================================="
9186
9187 test_133c() {
9188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9189         remote_ost_nodsh && skip "remote OST with nodsh" && return
9190         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9191         local testdir=$DIR/${tdir}/stats_testdir
9192         test_mkdir -p ${testdir} || error "mkdir failed"
9193
9194         # verify obdfilter stats.
9195         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9196         sync
9197         cancel_lru_locks osc
9198         wait_delete_completed
9199
9200         # clear stats.
9201         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9202         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9203
9204         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9205         sync
9206         cancel_lru_locks osc
9207         check_stats ost "write" 1
9208
9209         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9210         check_stats ost "read" 1
9211
9212         > ${testdir}/${tfile} || error "truncate failed"
9213         check_stats ost "punch" 1
9214
9215         rm -f ${testdir}/${tfile} || error "file remove failed"
9216         wait_delete_completed
9217         check_stats ost "destroy" 1
9218
9219         rm -rf $DIR/${tdir}
9220 }
9221 run_test 133c "Verifying OST stats ========================================"
9222
9223 order_2() {
9224         local value=$1
9225         local orig=$value
9226         local order=1
9227
9228         while [ $value -ge 2 ]; do
9229                 order=$((order*2))
9230                 value=$((value/2))
9231         done
9232
9233         if [ $orig -gt $order ]; then
9234                 order=$((order*2))
9235         fi
9236         echo $order
9237 }
9238
9239 size_in_KMGT() {
9240     local value=$1
9241     local size=('K' 'M' 'G' 'T');
9242     local i=0
9243     local size_string=$value
9244
9245     while [ $value -ge 1024 ]; do
9246         if [ $i -gt 3 ]; then
9247             #T is the biggest unit we get here, if that is bigger,
9248             #just return XXXT
9249             size_string=${value}T
9250             break
9251         fi
9252         value=$((value >> 10))
9253         if [ $value -lt 1024 ]; then
9254             size_string=${value}${size[$i]}
9255             break
9256         fi
9257         i=$((i + 1))
9258     done
9259
9260     echo $size_string
9261 }
9262
9263 get_rename_size() {
9264     local size=$1
9265     local context=${2:-.}
9266     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9267                 grep -A1 $context |
9268                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9269     echo $sample
9270 }
9271
9272 test_133d() {
9273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9274         remote_ost_nodsh && skip "remote OST with nodsh" && return
9275         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9276         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9277         { skip "MDS doesn't support rename stats"; return; }
9278
9279         local testdir1=$DIR/${tdir}/stats_testdir1
9280         local testdir2=$DIR/${tdir}/stats_testdir2
9281
9282         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9283
9284         mkdir -p ${testdir1} || error "mkdir failed"
9285         mkdir -p ${testdir2} || error "mkdir failed"
9286
9287         createmany -o $testdir1/test 512 || error "createmany failed"
9288
9289         # check samedir rename size
9290         mv ${testdir1}/test0 ${testdir1}/test_0
9291
9292         local testdir1_size=$(ls -l $DIR/${tdir} |
9293                 awk '/stats_testdir1/ {print $5}')
9294         local testdir2_size=$(ls -l $DIR/${tdir} |
9295                 awk '/stats_testdir2/ {print $5}')
9296
9297         testdir1_size=$(order_2 $testdir1_size)
9298         testdir2_size=$(order_2 $testdir2_size)
9299
9300         testdir1_size=$(size_in_KMGT $testdir1_size)
9301         testdir2_size=$(size_in_KMGT $testdir2_size)
9302
9303         echo "source rename dir size: ${testdir1_size}"
9304         echo "target rename dir size: ${testdir2_size}"
9305
9306         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9307         eval $cmd || error "$cmd failed"
9308         local samedir=$($cmd | grep 'same_dir')
9309         local same_sample=$(get_rename_size $testdir1_size)
9310         [ -z "$samedir" ] && error "samedir_rename_size count error"
9311         [[ $same_sample -eq 1 ]] ||
9312                 error "samedir_rename_size error $same_sample"
9313         echo "Check same dir rename stats success"
9314
9315         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9316
9317         # check crossdir rename size
9318         mv ${testdir1}/test_0 ${testdir2}/test_0
9319
9320         testdir1_size=$(ls -l $DIR/${tdir} |
9321                 awk '/stats_testdir1/ {print $5}')
9322         testdir2_size=$(ls -l $DIR/${tdir} |
9323                 awk '/stats_testdir2/ {print $5}')
9324
9325         testdir1_size=$(order_2 $testdir1_size)
9326         testdir2_size=$(order_2 $testdir2_size)
9327
9328         testdir1_size=$(size_in_KMGT $testdir1_size)
9329         testdir2_size=$(size_in_KMGT $testdir2_size)
9330
9331         echo "source rename dir size: ${testdir1_size}"
9332         echo "target rename dir size: ${testdir2_size}"
9333
9334         eval $cmd || error "$cmd failed"
9335         local crossdir=$($cmd | grep 'crossdir')
9336         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9337         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9338         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9339         [[ $src_sample -eq 1 ]] ||
9340                 error "crossdir_rename_size error $src_sample"
9341         [[ $tgt_sample -eq 1 ]] ||
9342                 error "crossdir_rename_size error $tgt_sample"
9343         echo "Check cross dir rename stats success"
9344         rm -rf $DIR/${tdir}
9345 }
9346 run_test 133d "Verifying rename_stats ========================================"
9347
9348 test_133e() {
9349         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9350         remote_ost_nodsh && skip "remote OST with nodsh" && return
9351         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9352         local testdir=$DIR/${tdir}/stats_testdir
9353         local ctr f0 f1 bs=32768 count=42 sum
9354
9355         mkdir -p ${testdir} || error "mkdir failed"
9356
9357         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9358
9359         for ctr in {write,read}_bytes; do
9360                 sync
9361                 cancel_lru_locks osc
9362
9363                 do_facet ost1 $LCTL set_param -n \
9364                         "obdfilter.*.exports.clear=clear"
9365
9366                 if [ $ctr = write_bytes ]; then
9367                         f0=/dev/zero
9368                         f1=${testdir}/${tfile}
9369                 else
9370                         f0=${testdir}/${tfile}
9371                         f1=/dev/null
9372                 fi
9373
9374                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9375                         error "dd failed"
9376                 sync
9377                 cancel_lru_locks osc
9378
9379                 sum=$(do_facet ost1 $LCTL get_param \
9380                         "obdfilter.*.exports.*.stats" |
9381                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9382                                 $1 == ctr { sum += $7 }
9383                                 END { printf("%0.0f", sum) }')
9384
9385                 if ((sum != bs * count)); then
9386                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9387                 fi
9388         done
9389
9390         rm -rf $DIR/${tdir}
9391 }
9392 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9393
9394 test_133f() {
9395         local proc_dirs
9396
9397         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9398 /sys/fs/lustre/ /sys/fs/lnet/"
9399         local dir
9400         for dir in $dirs; do
9401                 if [ -d $dir ]; then
9402                         proc_dirs="$proc_dirs $dir"
9403                 fi
9404         done
9405
9406         local facet
9407
9408         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9409         remote_ost_nodsh && skip "remote OST with nodsh" && return
9410         # First without trusting modes.
9411         find $proc_dirs -exec cat '{}' \; &> /dev/null
9412
9413         # Second verifying readability.
9414         find $proc_dirs \
9415                 -type f \
9416                 -exec cat '{}' \; &> /dev/null ||
9417                         error "proc file read failed"
9418
9419         for facet in $SINGLEMDS ost1; do
9420                 do_facet $facet find $proc_dirs \
9421                         ! -name req_history \
9422                         -exec cat '{}' \\\; &> /dev/null
9423
9424                 do_facet $facet find $proc_dirs \
9425                         ! -name req_history \
9426                         -type f \
9427                         -exec cat '{}' \\\; &> /dev/null ||
9428                                 error "proc file read failed"
9429         done
9430 }
9431 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9432
9433 test_133g() {
9434         local proc_dirs
9435
9436         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9437 /sys/fs/lustre/ /sys/fs/lnet/"
9438         local dir
9439         for dir in $dirs; do
9440                 if [ -d $dir ]; then
9441                         proc_dirs="$proc_dirs $dir"
9442                 fi
9443         done
9444
9445         local facet
9446
9447         # Second verifying readability.
9448         find $proc_dirs \
9449                 -type f \
9450                 -not -name force_lbug \
9451                 -not -name changelog_mask \
9452                 -exec badarea_io '{}' \; &> /dev/null ||
9453                 error "find $proc_dirs failed"
9454
9455         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9456                 skip "Too old lustre on MDS" && return
9457
9458         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9459                 skip "Too old lustre on ost1" && return
9460
9461         for facet in $SINGLEMDS ost1; do
9462                 do_facet $facet find $proc_dirs \
9463                         -type f \
9464                         -not -name force_lbug \
9465                         -not -name changelog_mask \
9466                         -exec badarea_io '{}' \\\; &> /dev/null ||
9467                 error "$facet find $proc_dirs failed"
9468
9469         done
9470
9471         # remount the FS in case writes/reads /proc break the FS
9472         cleanup || error "failed to unmount"
9473         setup || error "failed to setup"
9474         true
9475 }
9476 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9477
9478 test_134a() {
9479         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9480                 skip "Need MDS version at least 2.7.54" && return
9481
9482         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9483         cancel_lru_locks mdc
9484
9485         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9486         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9487         [ $unused -eq 0 ] || "$unused locks are not cleared"
9488
9489         local nr=1000
9490         createmany -o $DIR/$tdir/f $nr ||
9491                 error "failed to create $nr files in $DIR/$tdir"
9492         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9493
9494         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9495         do_facet mds1 $LCTL set_param fail_loc=0x327
9496         do_facet mds1 $LCTL set_param fail_val=500
9497         touch $DIR/$tdir/m
9498
9499         echo "sleep 10 seconds ..."
9500         sleep 10
9501         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9502
9503         do_facet mds1 $LCTL set_param fail_loc=0
9504         do_facet mds1 $LCTL set_param fail_val=0
9505         [ $lck_cnt -lt $unused ] ||
9506                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9507
9508         rm $DIR/$tdir/m
9509         unlinkmany $DIR/$tdir/f $nr
9510 }
9511 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9512
9513 test_134b() {
9514         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9515                 skip "Need MDS version at least 2.7.54" && return
9516
9517         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9518         cancel_lru_locks mdc
9519
9520         local low_wm=$(do_facet mds1 $LCTL get_param -n \
9521                         ldlm.lock_reclaim_threshold_mb)
9522         # disable reclaim temporarily
9523         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
9524
9525         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
9526         do_facet mds1 $LCTL set_param fail_loc=0x328
9527         do_facet mds1 $LCTL set_param fail_val=500
9528
9529         $LCTL set_param debug=+trace
9530
9531         local nr=600
9532         createmany -o $DIR/$tdir/f $nr &
9533         local create_pid=$!
9534
9535         echo "Sleep $TIMEOUT seconds ..."
9536         sleep $TIMEOUT
9537         if ! ps -p $create_pid  > /dev/null 2>&1; then
9538                 do_facet mds1 $LCTL set_param fail_loc=0
9539                 do_facet mds1 $LCTL set_param fail_val=0
9540                 do_facet mds1 $LCTL set_param \
9541                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
9542                 error "createmany finished incorrectly!"
9543         fi
9544         do_facet mds1 $LCTL set_param fail_loc=0
9545         do_facet mds1 $LCTL set_param fail_val=0
9546         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
9547         wait $create_pid || return 1
9548
9549         unlinkmany $DIR/$tdir/f $nr
9550 }
9551 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
9552
9553 test_140() { #bug-17379
9554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9555         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9556         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9557         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9558
9559         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9560         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9561         local i=0
9562         while i=`expr $i + 1`; do
9563                 test_mkdir -p $i || error "Creating dir $i"
9564                 cd $i || error "Changing to $i"
9565                 ln -s ../stat stat || error "Creating stat symlink"
9566                 # Read the symlink until ELOOP present,
9567                 # not LBUGing the system is considered success,
9568                 # we didn't overrun the stack.
9569                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9570                 [ $ret -ne 0 ] && {
9571                         if [ $ret -eq 40 ]; then
9572                                 break  # -ELOOP
9573                         else
9574                                 error "Open stat symlink"
9575                                 return
9576                         fi
9577                 }
9578         done
9579         i=`expr $i - 1`
9580         echo "The symlink depth = $i"
9581         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9582                                         error "Invalid symlink depth"
9583
9584         # Test recursive symlink
9585         ln -s symlink_self symlink_self
9586         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9587         echo "open symlink_self returns $ret"
9588         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9589 }
9590 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9591
9592 test_150() {
9593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9594         local TF="$TMP/$tfile"
9595
9596         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9597         cp $TF $DIR/$tfile
9598         cancel_lru_locks osc
9599         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9600         remount_client $MOUNT
9601         df -P $MOUNT
9602         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9603
9604         $TRUNCATE $TF 6000
9605         $TRUNCATE $DIR/$tfile 6000
9606         cancel_lru_locks osc
9607         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9608
9609         echo "12345" >>$TF
9610         echo "12345" >>$DIR/$tfile
9611         cancel_lru_locks osc
9612         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9613
9614         echo "12345" >>$TF
9615         echo "12345" >>$DIR/$tfile
9616         cancel_lru_locks osc
9617         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9618
9619         rm -f $TF
9620         true
9621 }
9622 run_test 150 "truncate/append tests"
9623
9624 #LU-2902 roc_hit was not able to read all values from lproc
9625 function roc_hit_init() {
9626         local list=$(comma_list $(osts_nodes))
9627         local dir=$DIR/$tdir-check
9628         local file=$dir/file
9629         local BEFORE
9630         local AFTER
9631         local idx
9632
9633         test_mkdir -p $dir
9634         #use setstripe to do a write to every ost
9635         for i in $(seq 0 $((OSTCOUNT-1))); do
9636                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9637                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9638                 idx=$(printf %04x $i)
9639                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9640                         awk '$1 == "cache_access" {sum += $7}
9641                                 END { printf("%0.0f", sum) }')
9642
9643                 cancel_lru_locks osc
9644                 cat $file >/dev/null
9645
9646                 AFTER=$(get_osd_param $list *OST*$idx stats |
9647                         awk '$1 == "cache_access" {sum += $7}
9648                                 END { printf("%0.0f", sum) }')
9649
9650                 echo BEFORE:$BEFORE AFTER:$AFTER
9651                 if ! let "AFTER - BEFORE == 4"; then
9652                         rm -rf $dir
9653                         error "roc_hit is not safe to use"
9654                 fi
9655                 rm $file
9656         done
9657
9658         rm -rf $dir
9659 }
9660
9661 function roc_hit() {
9662         local list=$(comma_list $(osts_nodes))
9663         echo $(get_osd_param $list '' stats |
9664                 awk '$1 == "cache_hit" {sum += $7}
9665                         END { printf("%0.0f", sum) }')
9666 }
9667
9668 function set_cache() {
9669         local on=1
9670
9671         if [ "$2" == "off" ]; then
9672                 on=0;
9673         fi
9674         local list=$(comma_list $(osts_nodes))
9675         set_osd_param $list '' $1_cache_enable $on
9676
9677         cancel_lru_locks osc
9678 }
9679
9680 test_151() {
9681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9682         remote_ost_nodsh && skip "remote OST with nodsh" && return
9683
9684         local CPAGES=3
9685         local list=$(comma_list $(osts_nodes))
9686
9687         # check whether obdfilter is cache capable at all
9688         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9689                 echo "not cache-capable obdfilter"
9690                 return 0
9691         fi
9692
9693         # check cache is enabled on all obdfilters
9694         if get_osd_param $list '' read_cache_enable | grep 0; then
9695                 echo "oss cache is disabled"
9696                 return 0
9697         fi
9698
9699         set_osd_param $list '' writethrough_cache_enable 1
9700
9701         # check write cache is enabled on all obdfilters
9702         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9703                 echo "oss write cache is NOT enabled"
9704                 return 0
9705         fi
9706
9707         roc_hit_init
9708
9709         #define OBD_FAIL_OBD_NO_LRU  0x609
9710         do_nodes $list $LCTL set_param fail_loc=0x609
9711
9712         # pages should be in the case right after write
9713         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9714                 error "dd failed"
9715
9716         local BEFORE=$(roc_hit)
9717         cancel_lru_locks osc
9718         cat $DIR/$tfile >/dev/null
9719         local AFTER=$(roc_hit)
9720
9721         do_nodes $list $LCTL set_param fail_loc=0
9722
9723         if ! let "AFTER - BEFORE == CPAGES"; then
9724                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9725         fi
9726
9727         # the following read invalidates the cache
9728         cancel_lru_locks osc
9729         set_osd_param $list '' read_cache_enable 0
9730         cat $DIR/$tfile >/dev/null
9731
9732         # now data shouldn't be found in the cache
9733         BEFORE=$(roc_hit)
9734         cancel_lru_locks osc
9735         cat $DIR/$tfile >/dev/null
9736         AFTER=$(roc_hit)
9737         if let "AFTER - BEFORE != 0"; then
9738                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9739         fi
9740
9741         set_osd_param $list '' read_cache_enable 1
9742         rm -f $DIR/$tfile
9743 }
9744 run_test 151 "test cache on oss and controls ==============================="
9745
9746 test_152() {
9747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9748         local TF="$TMP/$tfile"
9749
9750         # simulate ENOMEM during write
9751 #define OBD_FAIL_OST_NOMEM      0x226
9752         lctl set_param fail_loc=0x80000226
9753         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9754         cp $TF $DIR/$tfile
9755         sync || error "sync failed"
9756         lctl set_param fail_loc=0
9757
9758         # discard client's cache
9759         cancel_lru_locks osc
9760
9761         # simulate ENOMEM during read
9762         lctl set_param fail_loc=0x80000226
9763         cmp $TF $DIR/$tfile || error "cmp failed"
9764         lctl set_param fail_loc=0
9765
9766         rm -f $TF
9767 }
9768 run_test 152 "test read/write with enomem ============================"
9769
9770 test_153() {
9771         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9772 }
9773 run_test 153 "test if fdatasync does not crash ======================="
9774
9775 dot_lustre_fid_permission_check() {
9776         local fid=$1
9777         local ffid=$MOUNT/.lustre/fid/$fid
9778         local test_dir=$2
9779
9780         echo "stat fid $fid"
9781         stat $ffid > /dev/null || error "stat $ffid failed."
9782         echo "touch fid $fid"
9783         touch $ffid || error "touch $ffid failed."
9784         echo "write to fid $fid"
9785         cat /etc/hosts > $ffid || error "write $ffid failed."
9786         echo "read fid $fid"
9787         diff /etc/hosts $ffid || error "read $ffid failed."
9788         echo "append write to fid $fid"
9789         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9790         echo "rename fid $fid"
9791         mv $ffid $test_dir/$tfile.1 &&
9792                 error "rename $ffid to $tfile.1 should fail."
9793         touch $test_dir/$tfile.1
9794         mv $test_dir/$tfile.1 $ffid &&
9795                 error "rename $tfile.1 to $ffid should fail."
9796         rm -f $test_dir/$tfile.1
9797         echo "truncate fid $fid"
9798         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9799         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9800                 echo "link fid $fid"
9801                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9802         fi
9803         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9804                 echo "setfacl fid $fid"
9805                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9806                 echo "getfacl fid $fid"
9807                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9808         fi
9809         echo "unlink fid $fid"
9810         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9811         echo "mknod fid $fid"
9812         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9813
9814         fid=[0xf00000400:0x1:0x0]
9815         ffid=$MOUNT/.lustre/fid/$fid
9816
9817         echo "stat non-exist fid $fid"
9818         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9819         echo "write to non-exist fid $fid"
9820         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9821         echo "link new fid $fid"
9822         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9823
9824         mkdir -p $test_dir/$tdir
9825         touch $test_dir/$tdir/$tfile
9826         fid=$($LFS path2fid $test_dir/$tdir)
9827         rc=$?
9828         [ $rc -ne 0 ] &&
9829                 error "error: could not get fid for $test_dir/$dir/$tfile."
9830
9831         ffid=$MOUNT/.lustre/fid/$fid
9832
9833         echo "ls $fid"
9834         ls $ffid > /dev/null || error "ls $ffid failed."
9835         echo "touch $fid/$tfile.1"
9836         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9837
9838         echo "touch $MOUNT/.lustre/fid/$tfile"
9839         touch $MOUNT/.lustre/fid/$tfile && \
9840                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9841
9842         echo "setxattr to $MOUNT/.lustre/fid"
9843         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9844
9845         echo "listxattr for $MOUNT/.lustre/fid"
9846         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9847
9848         echo "delxattr from $MOUNT/.lustre/fid"
9849         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9850
9851         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9852         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9853                 error "touch invalid fid should fail."
9854
9855         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9856         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9857                 error "touch non-normal fid should fail."
9858
9859         echo "rename $tdir to $MOUNT/.lustre/fid"
9860         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9861                 error "rename to $MOUNT/.lustre/fid should fail."
9862
9863         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9864         then            # LU-3547
9865                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9866                 local new_obf_mode=777
9867
9868                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9869                 chmod $new_obf_mode $DIR/.lustre/fid ||
9870                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9871
9872                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9873                 [ $obf_mode -eq $new_obf_mode ] ||
9874                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9875
9876                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9877                 chmod $old_obf_mode $DIR/.lustre/fid ||
9878                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9879         fi
9880
9881         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9882         fid=$($LFS path2fid $test_dir/$tfile-2)
9883
9884         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9885         then # LU-5424
9886                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9887                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9888                         error "create lov data thru .lustre failed"
9889         fi
9890         echo "cp /etc/passwd $test_dir/$tfile-2"
9891         cp /etc/passwd $test_dir/$tfile-2 ||
9892                 error "copy to $test_dir/$tfile-2 failed."
9893         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9894         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9895                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9896
9897         rm -rf $test_dir/tfile.lnk
9898         rm -rf $test_dir/$tfile-2
9899 }
9900
9901 test_154A() {
9902         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9903                 skip "Need MDS version at least 2.4.1" && return
9904
9905         touch $DIR/$tfile
9906         local FID=$($LFS path2fid $DIR/$tfile)
9907         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9908
9909         # check that we get the same pathname back
9910         local FOUND=$($LFS fid2path $MOUNT "$FID")
9911         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9912         [ "$FOUND" != "$DIR/$tfile" ] &&
9913                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9914
9915         rm -rf $DIR/$tfile
9916 }
9917 run_test 154A "lfs path2fid and fid2path basic checks"
9918
9919 test_154a() {
9920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9921         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9922                 { skip "Need MDS version at least 2.2.51"; return 0; }
9923         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9924
9925         cp /etc/hosts $DIR/$tfile
9926
9927         fid=$($LFS path2fid $DIR/$tfile)
9928         rc=$?
9929         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9930
9931         dot_lustre_fid_permission_check "$fid" $DIR ||
9932                 error "dot lustre permission check $fid failed"
9933
9934         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9935
9936         touch $MOUNT/.lustre/file &&
9937                 error "creation is not allowed under .lustre"
9938
9939         mkdir $MOUNT/.lustre/dir &&
9940                 error "mkdir is not allowed under .lustre"
9941
9942         rm -rf $DIR/$tfile
9943 }
9944 run_test 154a "Open-by-FID"
9945
9946 test_154b() {
9947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9948         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9949                 { skip "Need MDS version at least 2.2.51"; return 0; }
9950
9951         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9952
9953         local remote_dir=$DIR/$tdir/remote_dir
9954         local MDTIDX=1
9955         local rc=0
9956
9957         mkdir -p $DIR/$tdir
9958         $LFS mkdir -i $MDTIDX $remote_dir ||
9959                 error "create remote directory failed"
9960
9961         cp /etc/hosts $remote_dir/$tfile
9962
9963         fid=$($LFS path2fid $remote_dir/$tfile)
9964         rc=$?
9965         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9966
9967         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9968                 error "dot lustre permission check $fid failed"
9969         rm -rf $DIR/$tdir
9970 }
9971 run_test 154b "Open-by-FID for remote directory"
9972
9973 test_154c() {
9974         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9975                 skip "Need MDS version at least 2.4.1" && return
9976
9977         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9978         local FID1=$($LFS path2fid $DIR/$tfile.1)
9979         local FID2=$($LFS path2fid $DIR/$tfile.2)
9980         local FID3=$($LFS path2fid $DIR/$tfile.3)
9981
9982         local N=1
9983         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9984                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9985                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9986                 local want=FID$N
9987                 [ "$FID" = "${!want}" ] ||
9988                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9989                 N=$((N + 1))
9990         done
9991
9992         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
9993         do
9994                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9995                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9996                 N=$((N + 1))
9997         done
9998 }
9999 run_test 154c "lfs path2fid and fid2path multiple arguments"
10000
10001 test_154d() {
10002         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10003                 skip "Need MDS version at least 2.5.53" && return
10004
10005         if remote_mds; then
10006                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10007         else
10008                 nid="0@lo"
10009         fi
10010         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10011         local fd
10012         local cmd
10013
10014         rm -f $DIR/$tfile
10015         touch $DIR/$tfile
10016
10017         local fid=$($LFS path2fid $DIR/$tfile)
10018         # Open the file
10019         fd=$(free_fd)
10020         cmd="exec $fd<$DIR/$tfile"
10021         eval $cmd
10022         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10023         echo "$fid_list" | grep "$fid"
10024         rc=$?
10025
10026         cmd="exec $fd>/dev/null"
10027         eval $cmd
10028         if [ $rc -ne 0 ]; then
10029                 error "FID $fid not found in open files list $fid_list"
10030         fi
10031 }
10032 run_test 154d "Verify open file fid"
10033
10034 test_154e()
10035 {
10036         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10037                 skip "Need MDS version at least 2.6.50" && return
10038
10039         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10040                 error ".lustre returned by readdir"
10041         fi
10042 }
10043 run_test 154e ".lustre is not returned by readdir"
10044
10045 test_154f() {
10046         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10047         test_mkdir -p -c1 $DIR/$tdir/d
10048         # test dirs inherit from its stripe
10049         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10050         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10051         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10052         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10053         touch $DIR/f
10054
10055         # get fid of parents
10056         local FID0=$($LFS path2fid $DIR/$tdir/d)
10057         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10058         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10059         local FID3=$($LFS path2fid $DIR)
10060
10061         # check that path2fid --parents returns expected <parent_fid>/name
10062         # 1) test for a directory (single parent)
10063         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10064         [ "$parent" == "$FID0/foo1" ] ||
10065                 error "expected parent: $FID0/foo1, got: $parent"
10066
10067         # 2) test for a file with nlink > 1 (multiple parents)
10068         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10069         echo "$parent" | grep -F "$FID1/$tfile" ||
10070                 error "$FID1/$tfile not returned in parent list"
10071         echo "$parent" | grep -F "$FID2/link" ||
10072                 error "$FID2/link not returned in parent list"
10073
10074         # 3) get parent by fid
10075         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10076         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10077         echo "$parent" | grep -F "$FID1/$tfile" ||
10078                 error "$FID1/$tfile not returned in parent list (by fid)"
10079         echo "$parent" | grep -F "$FID2/link" ||
10080                 error "$FID2/link not returned in parent list (by fid)"
10081
10082         # 4) test for entry in root directory
10083         parent=$($LFS path2fid --parents $DIR/f)
10084         echo "$parent" | grep -F "$FID3/f" ||
10085                 error "$FID3/f not returned in parent list"
10086
10087         # 5) test it on root directory
10088         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10089                 error "$MOUNT should not have parents"
10090
10091         # enable xattr caching and check that linkea is correctly updated
10092         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10093         save_lustre_params client "llite.*.xattr_cache" > $save
10094         lctl set_param llite.*.xattr_cache 1
10095
10096         # 6.1) linkea update on rename
10097         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10098
10099         # get parents by fid
10100         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10101         # foo1 should no longer be returned in parent list
10102         echo "$parent" | grep -F "$FID1" &&
10103                 error "$FID1 should no longer be in parent list"
10104         # the new path should appear
10105         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10106                 error "$FID2/$tfile.moved is not in parent list"
10107
10108         # 6.2) linkea update on unlink
10109         rm -f $DIR/$tdir/d/foo2/link
10110         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10111         # foo2/link should no longer be returned in parent list
10112         echo "$parent" | grep -F "$FID2/link" &&
10113                 error "$FID2/link should no longer be in parent list"
10114         true
10115
10116         rm -f $DIR/f
10117         restore_lustre_params < $save
10118 }
10119 run_test 154f "get parent fids by reading link ea"
10120
10121 test_154g()
10122 {
10123         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
10124                 { skip "Need MDS version at least 2.6.92"; return 0; }
10125
10126         mkdir -p $DIR/$tdir
10127         llapi_fid_test -d $DIR/$tdir
10128 }
10129 run_test 154g "various llapi FID tests"
10130
10131 test_155_small_load() {
10132     local temp=$TMP/$tfile
10133     local file=$DIR/$tfile
10134
10135     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10136         error "dd of=$temp bs=6096 count=1 failed"
10137     cp $temp $file
10138     cancel_lru_locks osc
10139     cmp $temp $file || error "$temp $file differ"
10140
10141     $TRUNCATE $temp 6000
10142     $TRUNCATE $file 6000
10143     cmp $temp $file || error "$temp $file differ (truncate1)"
10144
10145     echo "12345" >>$temp
10146     echo "12345" >>$file
10147     cmp $temp $file || error "$temp $file differ (append1)"
10148
10149     echo "12345" >>$temp
10150     echo "12345" >>$file
10151     cmp $temp $file || error "$temp $file differ (append2)"
10152
10153     rm -f $temp $file
10154     true
10155 }
10156
10157 test_155_big_load() {
10158     remote_ost_nodsh && skip "remote OST with nodsh" && return
10159     local temp=$TMP/$tfile
10160     local file=$DIR/$tfile
10161
10162     free_min_max
10163     local cache_size=$(do_facet ost$((MAXI+1)) \
10164         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10165     local large_file_size=$((cache_size * 2))
10166
10167     echo "OSS cache size: $cache_size KB"
10168     echo "Large file size: $large_file_size KB"
10169
10170     [ $MAXV -le $large_file_size ] && \
10171         skip_env "max available OST size needs > $large_file_size KB" && \
10172         return 0
10173
10174     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10175
10176     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10177         error "dd of=$temp bs=$large_file_size count=1k failed"
10178     cp $temp $file
10179     ls -lh $temp $file
10180     cancel_lru_locks osc
10181     cmp $temp $file || error "$temp $file differ"
10182
10183     rm -f $temp $file
10184     true
10185 }
10186
10187 test_155a() {
10188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10189         set_cache read on
10190         set_cache writethrough on
10191         test_155_small_load
10192 }
10193 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10194
10195 test_155b() {
10196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10197         set_cache read on
10198         set_cache writethrough off
10199         test_155_small_load
10200 }
10201 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10202
10203 test_155c() {
10204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10205         set_cache read off
10206         set_cache writethrough on
10207         test_155_small_load
10208 }
10209 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10210
10211 test_155d() {
10212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10213         set_cache read off
10214         set_cache writethrough off
10215         test_155_small_load
10216 }
10217 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10218
10219 test_155e() {
10220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10221         set_cache read on
10222         set_cache writethrough on
10223         test_155_big_load
10224 }
10225 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10226
10227 test_155f() {
10228         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10229         set_cache read on
10230         set_cache writethrough off
10231         test_155_big_load
10232 }
10233 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10234
10235 test_155g() {
10236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10237         set_cache read off
10238         set_cache writethrough on
10239         test_155_big_load
10240 }
10241 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10242
10243 test_155h() {
10244         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10245         set_cache read off
10246         set_cache writethrough off
10247         test_155_big_load
10248 }
10249 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10250
10251 test_156() {
10252         remote_ost_nodsh && skip "remote OST with nodsh" && return
10253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10254         local CPAGES=3
10255         local BEFORE
10256         local AFTER
10257         local file="$DIR/$tfile"
10258
10259         [ "$(facet_fstype ost1)" = "zfs" -a \
10260            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10261                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10262                 return
10263
10264         roc_hit_init
10265
10266         log "Turn on read and write cache"
10267         set_cache read on
10268         set_cache writethrough on
10269
10270         log "Write data and read it back."
10271         log "Read should be satisfied from the cache."
10272         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10273         BEFORE=$(roc_hit)
10274         cancel_lru_locks osc
10275         cat $file >/dev/null
10276         AFTER=$(roc_hit)
10277         if ! let "AFTER - BEFORE == CPAGES"; then
10278                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10279         else
10280                 log "cache hits:: before: $BEFORE, after: $AFTER"
10281         fi
10282
10283         log "Read again; it should be satisfied from the cache."
10284         BEFORE=$AFTER
10285         cancel_lru_locks osc
10286         cat $file >/dev/null
10287         AFTER=$(roc_hit)
10288         if ! let "AFTER - BEFORE == CPAGES"; then
10289                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10290         else
10291                 log "cache hits:: before: $BEFORE, after: $AFTER"
10292         fi
10293
10294         log "Turn off the read cache and turn on the write cache"
10295         set_cache read off
10296         set_cache writethrough on
10297
10298         log "Read again; it should be satisfied from the cache."
10299         BEFORE=$(roc_hit)
10300         cancel_lru_locks osc
10301         cat $file >/dev/null
10302         AFTER=$(roc_hit)
10303         if ! let "AFTER - BEFORE == CPAGES"; then
10304                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10305         else
10306                 log "cache hits:: before: $BEFORE, after: $AFTER"
10307         fi
10308
10309         log "Read again; it should not be satisfied from the cache."
10310         BEFORE=$AFTER
10311         cancel_lru_locks osc
10312         cat $file >/dev/null
10313         AFTER=$(roc_hit)
10314         if ! let "AFTER - BEFORE == 0"; then
10315                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10316         else
10317                 log "cache hits:: before: $BEFORE, after: $AFTER"
10318         fi
10319
10320         log "Write data and read it back."
10321         log "Read should be satisfied from the cache."
10322         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10323         BEFORE=$(roc_hit)
10324         cancel_lru_locks osc
10325         cat $file >/dev/null
10326         AFTER=$(roc_hit)
10327         if ! let "AFTER - BEFORE == CPAGES"; then
10328                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10329         else
10330                 log "cache hits:: before: $BEFORE, after: $AFTER"
10331         fi
10332
10333         log "Read again; it should not be satisfied from the cache."
10334         BEFORE=$AFTER
10335         cancel_lru_locks osc
10336         cat $file >/dev/null
10337         AFTER=$(roc_hit)
10338         if ! let "AFTER - BEFORE == 0"; then
10339                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10340         else
10341                 log "cache hits:: before: $BEFORE, after: $AFTER"
10342         fi
10343
10344         log "Turn off read and write cache"
10345         set_cache read off
10346         set_cache writethrough off
10347
10348         log "Write data and read it back"
10349         log "It should not be satisfied from the cache."
10350         rm -f $file
10351         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10352         cancel_lru_locks osc
10353         BEFORE=$(roc_hit)
10354         cat $file >/dev/null
10355         AFTER=$(roc_hit)
10356         if ! let "AFTER - BEFORE == 0"; then
10357                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10358         else
10359                 log "cache hits:: before: $BEFORE, after: $AFTER"
10360         fi
10361
10362         log "Turn on the read cache and turn off the write cache"
10363         set_cache read on
10364         set_cache writethrough off
10365
10366         log "Write data and read it back"
10367         log "It should not be satisfied from the cache."
10368         rm -f $file
10369         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10370         BEFORE=$(roc_hit)
10371         cancel_lru_locks osc
10372         cat $file >/dev/null
10373         AFTER=$(roc_hit)
10374         if ! let "AFTER - BEFORE == 0"; then
10375                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10376         else
10377                 log "cache hits:: before: $BEFORE, after: $AFTER"
10378         fi
10379
10380         log "Read again; it should be satisfied from the cache."
10381         BEFORE=$(roc_hit)
10382         cancel_lru_locks osc
10383         cat $file >/dev/null
10384         AFTER=$(roc_hit)
10385         if ! let "AFTER - BEFORE == CPAGES"; then
10386                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10387         else
10388                 log "cache hits:: before: $BEFORE, after: $AFTER"
10389         fi
10390
10391         rm -f $file
10392 }
10393 run_test 156 "Verification of tunables"
10394
10395 #Changelogs
10396 err17935 () {
10397         if [[ $MDSCOUNT -gt 1 ]]; then
10398                 error_ignore bz17935 $*
10399         else
10400                 error $*
10401         fi
10402 }
10403
10404 changelog_chmask()
10405 {
10406         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10407
10408         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10409
10410         if [ $MASK -eq 1 ]; then
10411                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10412         else
10413                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10414         fi
10415 }
10416
10417 changelog_extract_field() {
10418         local mdt=$1
10419         local cltype=$2
10420         local file=$3
10421         local identifier=$4
10422
10423         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10424                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10425                 tail -1
10426 }
10427
10428 test_160a() {
10429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10430         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10431         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10432                 { skip "Need MDS version at least 2.2.0"; return; }
10433
10434         local CL_USERS="mdd.$MDT0.changelog_users"
10435         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10436         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10437         echo "Registered as changelog user $USER"
10438         $GET_CL_USERS | grep -q $USER ||
10439                 error "User $USER not found in changelog_users"
10440
10441         # change something
10442         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10443         touch $DIR/$tdir/pics/2008/zachy/timestamp
10444         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10445         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10446         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10447         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10448         rm $DIR/$tdir/pics/desktop.jpg
10449
10450         $LFS changelog $MDT0 | tail -5
10451
10452         echo "verifying changelog mask"
10453         changelog_chmask "MKDIR"
10454         changelog_chmask "CLOSE"
10455
10456         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10457         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10458
10459         changelog_chmask "MKDIR"
10460         changelog_chmask "CLOSE"
10461
10462         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10463         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10464
10465         $LFS changelog $MDT0
10466         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10467         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10468         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10469         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10470
10471         # verify contents
10472         echo "verifying target fid"
10473         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10474         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10475         [ "$fidc" == "$fidf" ] ||
10476                 err17935 "fid in changelog $fidc != file fid $fidf"
10477         echo "verifying parent fid"
10478         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10479         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10480         [ "$fidc" == "$fidf" ] ||
10481                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10482
10483         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10484         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10485         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10486         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10487         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10488                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10489
10490         MIN_REC=$($GET_CL_USERS |
10491                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10492         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10493         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10494         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10495                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10496
10497         # LU-3446 changelog index reset on MDT restart
10498         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10499         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10500         $LFS changelog_clear $MDT0 $USER 0
10501         stop $SINGLEMDS || error "Fail to stop MDT."
10502         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10503         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10504         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10505         [ $CUR_REC1 == $CUR_REC2 ] ||
10506                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10507
10508         echo "verifying user deregister"
10509         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10510         $GET_CL_USERS | grep -q $USER &&
10511                 error "User $USER still in changelog_users"
10512
10513         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10514         if [ $USERS -eq 0 ]; then
10515                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10516                 touch $DIR/$tdir/chloe
10517                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10518                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10519                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10520         else
10521                 echo "$USERS other changelog users; can't verify off"
10522         fi
10523 }
10524 run_test 160a "changelog sanity"
10525
10526 test_160b() { # LU-3587
10527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10528         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10529         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10530                 { skip "Need MDS version at least 2.2.0"; return; }
10531
10532         local CL_USERS="mdd.$MDT0.changelog_users"
10533         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10534         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10535         echo "Registered as changelog user $USER"
10536         $GET_CL_USERS | grep -q $USER ||
10537                 error "User $USER not found in changelog_users"
10538
10539         local LONGNAME1=$(str_repeat a 255)
10540         local LONGNAME2=$(str_repeat b 255)
10541
10542         cd $DIR
10543         echo "creating very long named file"
10544         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10545         echo "moving very long named file"
10546         mv $LONGNAME1 $LONGNAME2
10547
10548         $LFS changelog $MDT0 | grep RENME
10549
10550         echo "deregistering $USER"
10551         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10552
10553         rm -f $LONGNAME2
10554 }
10555 run_test 160b "Verify that very long rename doesn't crash in changelog"
10556
10557 test_160c() {
10558         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10559
10560         local rc=0
10561         local server_version=$(lustre_version_code $SINGLEMDS)
10562
10563         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10564                 [[ $server_version -gt $(version_code 2.5.1) &&
10565                    $server_version -lt $(version_code 2.5.50) ]] ||
10566                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10567         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10568
10569         # Registration step
10570         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10571                 changelog_register -n)
10572
10573         rm -rf $DIR/$tdir
10574         mkdir -p $DIR/$tdir
10575         $MCREATE $DIR/$tdir/foo_160c
10576         changelog_chmask "TRUNC"
10577         $TRUNCATE $DIR/$tdir/foo_160c 200
10578         changelog_chmask "TRUNC"
10579         $TRUNCATE $DIR/$tdir/foo_160c 199
10580         $LFS changelog $MDT0
10581         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10582         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10583         $LFS changelog_clear $MDT0 $USER 0
10584
10585         # Deregistration step
10586         echo "deregistering $USER"
10587         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10588 }
10589 run_test 160c "verify that changelog log catch the truncate event"
10590
10591 test_160d() {
10592         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10593         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10594
10595         local server_version=$(lustre_version_code mds1)
10596         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10597
10598         [[ $server_version -ge $(version_code 2.7.60) ]] ||
10599                 { skip "Need MDS version at least 2.7.60+"; return; }
10600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10601
10602         # Registration step
10603         local USER=$(do_facet mds1 $LCTL --device $MDT0 \
10604                 changelog_register -n)
10605
10606         mkdir -p $DIR/$tdir/migrate_dir
10607         $LFS changelog_clear $MDT0 $USER 0
10608
10609         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
10610         $LFS changelog $MDT0
10611         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
10612         $LFS changelog_clear $MDT0 $USER 0
10613         [ $MIGRATES -eq 1 ] ||
10614                 error "MIGRATE changelog mask count $MIGRATES != 1"
10615
10616         # Deregistration step
10617         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $USER
10618 }
10619 run_test 160d "verify that changelog log catch the migrate event"
10620
10621 test_161a() {
10622         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10623         test_mkdir -p -c1 $DIR/$tdir
10624         cp /etc/hosts $DIR/$tdir/$tfile
10625         test_mkdir -c1 $DIR/$tdir/foo1
10626         test_mkdir -c1 $DIR/$tdir/foo2
10627         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10628         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10629         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10630         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10631         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10632         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10633                 $LFS fid2path $DIR $FID
10634                 err17935 "bad link ea"
10635         fi
10636     # middle
10637     rm $DIR/$tdir/foo2/zachary
10638     # last
10639     rm $DIR/$tdir/foo2/thor
10640     # first
10641     rm $DIR/$tdir/$tfile
10642     # rename
10643     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10644     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10645         then
10646         $LFS fid2path $DIR $FID
10647         err17935 "bad link rename"
10648     fi
10649     rm $DIR/$tdir/foo2/maggie
10650
10651         # overflow the EA
10652         local longname=filename_avg_len_is_thirty_two_
10653         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10654                 error "failed to hardlink many files"
10655         links=$($LFS fid2path $DIR $FID | wc -l)
10656         echo -n "${links}/1000 links in link EA"
10657         [[ $links -gt 60 ]] ||
10658                 err17935 "expected at least 60 links in link EA"
10659         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10660                 error "failed to unlink many hardlinks"
10661 }
10662 run_test 161a "link ea sanity"
10663
10664 test_161b() {
10665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10666         [ $MDSCOUNT -lt 2 ] &&
10667                 skip "skipping remote directory test" && return
10668         local MDTIDX=1
10669         local remote_dir=$DIR/$tdir/remote_dir
10670
10671         mkdir -p $DIR/$tdir
10672         $LFS mkdir -i $MDTIDX $remote_dir ||
10673                 error "create remote directory failed"
10674
10675         cp /etc/hosts $remote_dir/$tfile
10676         mkdir -p $remote_dir/foo1
10677         mkdir -p $remote_dir/foo2
10678         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10679         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10680         ln $remote_dir/$tfile $remote_dir/foo1/luna
10681         ln $remote_dir/$tfile $remote_dir/foo2/thor
10682
10683         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10684                      tr -d ']')
10685         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10686                 $LFS fid2path $DIR $FID
10687                 err17935 "bad link ea"
10688         fi
10689         # middle
10690         rm $remote_dir/foo2/zachary
10691         # last
10692         rm $remote_dir/foo2/thor
10693         # first
10694         rm $remote_dir/$tfile
10695         # rename
10696         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10697         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10698         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10699                 $LFS fid2path $DIR $FID
10700                 err17935 "bad link rename"
10701         fi
10702         rm $remote_dir/foo2/maggie
10703
10704         # overflow the EA
10705         local longname=filename_avg_len_is_thirty_two_
10706         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10707                 error "failed to hardlink many files"
10708         links=$($LFS fid2path $DIR $FID | wc -l)
10709         echo -n "${links}/1000 links in link EA"
10710         [[ ${links} -gt 60 ]] ||
10711                 err17935 "expected at least 60 links in link EA"
10712         unlinkmany $remote_dir/foo2/$longname 1000 ||
10713         error "failed to unlink many hardlinks"
10714 }
10715 run_test 161b "link ea sanity under remote directory"
10716
10717 test_161c() {
10718         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10720         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10721                 skip "Need MDS version at least 2.1.5" && return
10722
10723         # define CLF_RENAME_LAST 0x0001
10724         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10725         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10726                 changelog_register -n)
10727         rm -rf $DIR/$tdir
10728         mkdir -p $DIR/$tdir
10729         touch $DIR/$tdir/foo_161c
10730         touch $DIR/$tdir/bar_161c
10731         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10732         $LFS changelog $MDT0 | grep RENME
10733         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10734                 cut -f5 -d' ')
10735         $LFS changelog_clear $MDT0 $USER 0
10736         if [ x$flags != "x0x1" ]; then
10737                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10738                         $USER
10739                 error "flag $flags is not 0x1"
10740         fi
10741         echo "rename overwrite a target having nlink = 1," \
10742                 "changelog record has flags of $flags"
10743
10744         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10745         touch $DIR/$tdir/foo_161c
10746         touch $DIR/$tdir/bar_161c
10747         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10748         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10749         $LFS changelog $MDT0 | grep RENME
10750         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10751         $LFS changelog_clear $MDT0 $USER 0
10752         if [ x$flags != "x0x0" ]; then
10753                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10754                         $USER
10755                 error "flag $flags is not 0x0"
10756         fi
10757         echo "rename overwrite a target having nlink > 1," \
10758                 "changelog record has flags of $flags"
10759
10760         # rename doesn't overwrite a target (changelog flag 0x0)
10761         touch $DIR/$tdir/foo_161c
10762         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10763         $LFS changelog $MDT0 | grep RENME
10764         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10765         $LFS changelog_clear $MDT0 $USER 0
10766         if [ x$flags != "x0x0" ]; then
10767                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10768                         $USER
10769                 error "flag $flags is not 0x0"
10770         fi
10771         echo "rename doesn't overwrite a target," \
10772                 "changelog record has flags of $flags"
10773
10774         # define CLF_UNLINK_LAST 0x0001
10775         # unlink a file having nlink = 1 (changelog flag 0x1)
10776         rm -f $DIR/$tdir/foo2_161c
10777         $LFS changelog $MDT0 | grep UNLNK
10778         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10779         $LFS changelog_clear $MDT0 $USER 0
10780         if [ x$flags != "x0x1" ]; then
10781                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10782                         $USER
10783                 error "flag $flags is not 0x1"
10784         fi
10785         echo "unlink a file having nlink = 1," \
10786                 "changelog record has flags of $flags"
10787
10788         # unlink a file having nlink > 1 (changelog flag 0x0)
10789         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10790         rm -f $DIR/$tdir/foobar_161c
10791         $LFS changelog $MDT0 | grep UNLNK
10792         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10793         $LFS changelog_clear $MDT0 $USER 0
10794         if [ x$flags != "x0x0" ]; then
10795                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10796                         $USER
10797                 error "flag $flags is not 0x0"
10798         fi
10799         echo "unlink a file having nlink > 1," \
10800                 "changelog record has flags of $flags"
10801         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10802 }
10803 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10804
10805 check_path() {
10806     local expected=$1
10807     shift
10808     local fid=$2
10809
10810     local path=$(${LFS} fid2path $*)
10811     # Remove the '//' indicating a remote directory
10812     path=$(echo $path | sed 's#//#/#g')
10813     RC=$?
10814
10815     if [ $RC -ne 0 ]; then
10816         err17935 "path looked up of $expected failed. Error $RC"
10817         return $RC
10818     elif [ "${path}" != "${expected}" ]; then
10819         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10820         return 2
10821     fi
10822     echo "fid $fid resolves to path $path (expected $expected)"
10823 }
10824
10825 test_162a() { # was test_162
10826         # Make changes to filesystem
10827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10828         test_mkdir -p -c1 $DIR/$tdir/d2
10829         touch $DIR/$tdir/d2/$tfile
10830         touch $DIR/$tdir/d2/x1
10831         touch $DIR/$tdir/d2/x2
10832         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10833         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10834         # regular file
10835         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10836         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10837                 error "check path $tdir/d2/$tfile failed"
10838
10839         # softlink
10840         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10841         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10842         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10843                 error "check path $tdir/d2/p/q/r/slink failed"
10844
10845         # softlink to wrong file
10846         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10847         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10848         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10849                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10850
10851         # hardlink
10852         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10853         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10854         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10855         # fid2path dir/fsname should both work
10856         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10857                 error "check path $tdir/d2/a/b/c/new_file failed"
10858         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10859                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10860
10861         # hardlink count: check that there are 2 links
10862         # Doesnt work with CMD yet: 17935
10863         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10864                 err17935 "expected 2 links"
10865
10866         # hardlink indexing: remove the first link
10867         rm $DIR/$tdir/d2/p/q/r/hlink
10868         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10869                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10870
10871         return 0
10872 }
10873 run_test 162a "path lookup sanity"
10874
10875 test_162b() {
10876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10877         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10878
10879         mkdir $DIR/$tdir
10880         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10881                                 error "create striped dir failed"
10882
10883         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10884                                         tail -n 1 | awk '{print $2}')
10885         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10886
10887         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10888         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10889
10890         # regular file
10891         for ((i=0;i<5;i++)); do
10892                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10893                         error "get fid for f$i failed"
10894                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10895                         error "check path $tdir/striped_dir/f$i failed"
10896
10897                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10898                         error "get fid for d$i failed"
10899                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10900                         error "check path $tdir/striped_dir/d$i failed"
10901         done
10902
10903         return 0
10904 }
10905 run_test 162b "striped directory path lookup sanity"
10906
10907 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10908 test_162c() {
10909         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
10910                 skip "Need MDS version at least 2.7.51" && return
10911         test_mkdir $DIR/$tdir.local
10912         test_mkdir $DIR/$tdir.remote
10913         local lpath=$tdir.local
10914         local rpath=$tdir.remote
10915
10916         for ((i = 0; i <= 101; i++)); do
10917                 lpath="$lpath/$i"
10918                 mkdir $DIR/$lpath
10919                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10920                         error "get fid for local directory $DIR/$lpath failed"
10921                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10922                         error "check path for local directory $DIR/$lpath failed"
10923
10924                 rpath="$rpath/$i"
10925                 test_mkdir $DIR/$rpath
10926                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10927                         error "get fid for remote directory $DIR/$rpath failed"
10928                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10929                         error "check path for remote directory $DIR/$rpath failed"
10930         done
10931
10932         return 0
10933 }
10934 run_test 162c "fid2path works with paths 100 or more directories deep"
10935
10936 test_169() {
10937         # do directio so as not to populate the page cache
10938         log "creating a 10 Mb file"
10939         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10940         log "starting reads"
10941         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10942         log "truncating the file"
10943         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10944         log "killing dd"
10945         kill %+ || true # reads might have finished
10946         echo "wait until dd is finished"
10947         wait
10948         log "removing the temporary file"
10949         rm -rf $DIR/$tfile || error "tmp file removal failed"
10950 }
10951 run_test 169 "parallel read and truncate should not deadlock"
10952
10953 test_170() {
10954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10955         $LCTL clear     # bug 18514
10956         $LCTL debug_daemon start $TMP/${tfile}_log_good
10957         touch $DIR/$tfile
10958         $LCTL debug_daemon stop
10959         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10960                error "sed failed to read log_good"
10961
10962         $LCTL debug_daemon start $TMP/${tfile}_log_good
10963         rm -rf $DIR/$tfile
10964         $LCTL debug_daemon stop
10965
10966         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10967                error "lctl df log_bad failed"
10968
10969         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10970         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10971
10972         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10973         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10974
10975         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10976                 error "bad_line good_line1 good_line2 are empty"
10977
10978         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10979         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10980         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10981
10982         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10983         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10984         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10985
10986         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10987                 error "bad_line_new good_line_new are empty"
10988
10989         local expected_good=$((good_line1 + good_line2*2))
10990
10991         rm -f $TMP/${tfile}*
10992         # LU-231, short malformed line may not be counted into bad lines
10993         if [ $bad_line -ne $bad_line_new ] &&
10994                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10995                 error "expected $bad_line bad lines, but got $bad_line_new"
10996                 return 1
10997         fi
10998
10999         if [ $expected_good -ne $good_line_new ]; then
11000                 error "expected $expected_good good lines, but got $good_line_new"
11001                 return 2
11002         fi
11003         true
11004 }
11005 run_test 170 "test lctl df to handle corrupted log ====================="
11006
11007 test_171() { # bug20592
11008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11009 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11010         $LCTL set_param fail_loc=0x50e
11011         $LCTL set_param fail_val=3000
11012         multiop_bg_pause $DIR/$tfile O_s || true
11013         local MULTIPID=$!
11014         kill -USR1 $MULTIPID
11015         # cause log dump
11016         sleep 3
11017         wait $MULTIPID
11018         if dmesg | grep "recursive fault"; then
11019                 error "caught a recursive fault"
11020         fi
11021         $LCTL set_param fail_loc=0
11022         true
11023 }
11024 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11025
11026 # it would be good to share it with obdfilter-survey/iokit-libecho code
11027 setup_obdecho_osc () {
11028         local rc=0
11029         local ost_nid=$1
11030         local obdfilter_name=$2
11031         echo "Creating new osc for $obdfilter_name on $ost_nid"
11032         # make sure we can find loopback nid
11033         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11034
11035         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11036                            ${obdfilter_name}_osc_UUID || rc=2; }
11037         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11038                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11039         return $rc
11040 }
11041
11042 cleanup_obdecho_osc () {
11043         local obdfilter_name=$1
11044         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11045         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11046         return 0
11047 }
11048
11049 obdecho_test() {
11050         local OBD=$1
11051         local node=$2
11052         local pages=${3:-64}
11053         local rc=0
11054         local id
11055
11056         local count=10
11057         local obd_size=$(get_obd_size $node $OBD)
11058         local page_size=$(get_page_size $node)
11059         if [[ -n "$obd_size" ]]; then
11060                 local new_count=$((obd_size / (pages * page_size / 1024)))
11061                 [[ $new_count -ge $count ]] || count=$new_count
11062         fi
11063
11064         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11065         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11066                            rc=2; }
11067         if [ $rc -eq 0 ]; then
11068             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11069             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11070         fi
11071         echo "New object id is $id"
11072         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11073                            rc=4; }
11074         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11075                            "test_brw $count w v $pages $id" || rc=4; }
11076         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11077                            rc=4; }
11078         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11079                                         "cleanup" || rc=5; }
11080         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11081                                         "detach" || rc=6; }
11082         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11083         return $rc
11084 }
11085
11086 test_180a() {
11087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11088         remote_ost_nodsh && skip "remote OST with nodsh" && return
11089         local rc=0
11090         local rmmod_local=0
11091
11092         if ! module_loaded obdecho; then
11093             load_module obdecho/obdecho
11094             rmmod_local=1
11095         fi
11096
11097         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11098         local host=$(lctl get_param -n osc.$osc.import |
11099                              awk '/current_connection:/ {print $2}' )
11100         local target=$(lctl get_param -n osc.$osc.import |
11101                              awk '/target:/ {print $2}' )
11102         target=${target%_UUID}
11103
11104         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11105         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11106         [[ -n $target ]] && cleanup_obdecho_osc $target
11107         [ $rmmod_local -eq 1 ] && rmmod obdecho
11108         return $rc
11109 }
11110 run_test 180a "test obdecho on osc"
11111
11112 test_180b() {
11113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11114         remote_ost_nodsh && skip "remote OST with nodsh" && return
11115         local rc=0
11116         local rmmod_remote=0
11117
11118         do_facet ost1 "lsmod | grep -q obdecho || "                      \
11119                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
11120                       "modprobe obdecho; }" && rmmod_remote=1
11121         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11122         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11123         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
11124         return $rc
11125 }
11126 run_test 180b "test obdecho directly on obdfilter"
11127
11128 test_180c() { # LU-2598
11129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11130         remote_ost_nodsh && skip "remote OST with nodsh" && return
11131         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11132                 skip "Need MDS version at least 2.4.0" && return
11133
11134         local rc=0
11135         local rmmod_remote=false
11136         local pages=16384 # 64MB bulk I/O RPC size
11137         local target
11138
11139         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11140                 rmmod_remote=true || error "failed to load module obdecho"
11141
11142         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11143                 head -n1)
11144         if [ -n "$target" ]; then
11145                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11146         else
11147                 echo "there is no obdfilter target on ost1"
11148                 rc=2
11149         fi
11150         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11151         return $rc
11152 }
11153 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11154
11155 test_181() { # bug 22177
11156         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11157         # create enough files to index the directory
11158         createmany -o $DIR/$tdir/foobar 4000
11159         # print attributes for debug purpose
11160         lsattr -d .
11161         # open dir
11162         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11163         MULTIPID=$!
11164         # remove the files & current working dir
11165         unlinkmany $DIR/$tdir/foobar 4000
11166         rmdir $DIR/$tdir
11167         kill -USR1 $MULTIPID
11168         wait $MULTIPID
11169         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11170         return 0
11171 }
11172 run_test 181 "Test open-unlinked dir ========================"
11173
11174 test_182() {
11175         local fcount=1000
11176         local tcount=10
11177
11178         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11179
11180         $LCTL set_param mdc.*.rpc_stats=clear
11181
11182         for (( i = 0; i < $tcount; i++ )) ; do
11183                 mkdir $DIR/$tdir/$i
11184         done
11185
11186         for (( i = 0; i < $tcount; i++ )) ; do
11187                 createmany -o $DIR/$tdir/$i/f- $fcount &
11188         done
11189         wait
11190
11191         for (( i = 0; i < $tcount; i++ )) ; do
11192                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11193         done
11194         wait
11195
11196         $LCTL get_param mdc.*.rpc_stats
11197
11198         rm -rf $DIR/$tdir
11199 }
11200 run_test 182 "Test parallel modify metadata operations ================"
11201
11202 test_183() { # LU-2275
11203         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11204         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11205                 skip "Need MDS version at least 2.3.56" && return
11206
11207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11208         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11209         echo aaa > $DIR/$tdir/$tfile
11210
11211 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11212         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11213
11214         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11215         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11216
11217         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11218
11219         # Flush negative dentry cache
11220         touch $DIR/$tdir/$tfile
11221
11222         # We are not checking for any leaked references here, they'll
11223         # become evident next time we do cleanup with module unload.
11224         rm -rf $DIR/$tdir
11225 }
11226 run_test 183 "No crash or request leak in case of strange dispositions ========"
11227
11228 # test suite 184 is for LU-2016, LU-2017
11229 test_184a() {
11230         check_swap_layouts_support && return 0
11231
11232         dir0=$DIR/$tdir/$testnum
11233         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11234         ref1=/etc/passwd
11235         ref2=/etc/group
11236         file1=$dir0/f1
11237         file2=$dir0/f2
11238         $SETSTRIPE -c1 $file1
11239         cp $ref1 $file1
11240         $SETSTRIPE -c2 $file2
11241         cp $ref2 $file2
11242         gen1=$($GETSTRIPE -g $file1)
11243         gen2=$($GETSTRIPE -g $file2)
11244
11245         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11246         gen=$($GETSTRIPE -g $file1)
11247         [[ $gen1 != $gen ]] ||
11248                 "Layout generation on $file1 does not change"
11249         gen=$($GETSTRIPE -g $file2)
11250         [[ $gen2 != $gen ]] ||
11251                 "Layout generation on $file2 does not change"
11252
11253         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11254         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11255 }
11256 run_test 184a "Basic layout swap"
11257
11258 test_184b() {
11259         check_swap_layouts_support && return 0
11260
11261         dir0=$DIR/$tdir/$testnum
11262         mkdir -p $dir0 || error "creating dir $dir0"
11263         file1=$dir0/f1
11264         file2=$dir0/f2
11265         file3=$dir0/f3
11266         dir1=$dir0/d1
11267         dir2=$dir0/d2
11268         mkdir $dir1 $dir2
11269         $SETSTRIPE -c1 $file1
11270         $SETSTRIPE -c2 $file2
11271         $SETSTRIPE -c1 $file3
11272         chown $RUNAS_ID $file3
11273         gen1=$($GETSTRIPE -g $file1)
11274         gen2=$($GETSTRIPE -g $file2)
11275
11276         $LFS swap_layouts $dir1 $dir2 &&
11277                 error "swap of directories layouts should fail"
11278         $LFS swap_layouts $dir1 $file1 &&
11279                 error "swap of directory and file layouts should fail"
11280         $RUNAS $LFS swap_layouts $file1 $file2 &&
11281                 error "swap of file we cannot write should fail"
11282         $LFS swap_layouts $file1 $file3 &&
11283                 error "swap of file with different owner should fail"
11284         /bin/true # to clear error code
11285 }
11286 run_test 184b "Forbidden layout swap (will generate errors)"
11287
11288 test_184c() {
11289         check_swap_layouts_support && return 0
11290
11291         local dir0=$DIR/$tdir/$testnum
11292         mkdir -p $dir0 || error "creating dir $dir0"
11293
11294         local ref1=$dir0/ref1
11295         local ref2=$dir0/ref2
11296         local file1=$dir0/file1
11297         local file2=$dir0/file2
11298         # create a file large enough for the concurrent test
11299         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11300         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11301         echo "ref file size: ref1($(stat -c %s $ref1))," \
11302              "ref2($(stat -c %s $ref2))"
11303
11304         cp $ref2 $file2
11305         dd if=$ref1 of=$file1 bs=16k &
11306         local DD_PID=$!
11307
11308         # Make sure dd starts to copy file
11309         while [ ! -f $file1 ]; do sleep 0.1; done
11310
11311         $LFS swap_layouts $file1 $file2
11312         local rc=$?
11313         wait $DD_PID
11314         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11315         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11316
11317         # how many bytes copied before swapping layout
11318         local copied=$(stat -c %s $file2)
11319         local remaining=$(stat -c %s $ref1)
11320         remaining=$((remaining - copied))
11321         echo "Copied $copied bytes before swapping layout..."
11322
11323         cmp -n $copied $file1 $ref2 | grep differ &&
11324                 error "Content mismatch [0, $copied) of ref2 and file1"
11325         cmp -n $copied $file2 $ref1 ||
11326                 error "Content mismatch [0, $copied) of ref1 and file2"
11327         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11328                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11329
11330         # clean up
11331         rm -f $ref1 $ref2 $file1 $file2
11332 }
11333 run_test 184c "Concurrent write and layout swap"
11334
11335 test_184d() {
11336         check_swap_layouts_support && return 0
11337         [ -z "$(which getfattr 2>/dev/null)" ] &&
11338                 skip "no getfattr command" && return 0
11339
11340         local file1=$DIR/$tdir/$tfile-1
11341         local file2=$DIR/$tdir/$tfile-2
11342         local file3=$DIR/$tdir/$tfile-3
11343         local lovea1
11344         local lovea2
11345
11346         mkdir -p $DIR/$tdir
11347         touch $file1 || error "create $file1 failed"
11348         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11349                 error "create $file2 failed"
11350         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11351                 error "create $file3 failed"
11352         lovea1=$($LFS getstripe $file1 | sed 1d)
11353
11354         $LFS swap_layouts $file2 $file3 ||
11355                 error "swap $file2 $file3 layouts failed"
11356         $LFS swap_layouts $file1 $file2 ||
11357                 error "swap $file1 $file2 layouts failed"
11358
11359         lovea2=$($LFS getstripe $file2 | sed 1d)
11360         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11361
11362         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11363         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11364 }
11365 run_test 184d "allow stripeless layouts swap"
11366
11367 test_184e() {
11368         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11369                 { skip "Need MDS version at least 2.6.94"; return 0; }
11370         check_swap_layouts_support && return 0
11371         [ -z "$(which getfattr 2>/dev/null)" ] &&
11372                 skip "no getfattr command" && return 0
11373
11374         local file1=$DIR/$tdir/$tfile-1
11375         local file2=$DIR/$tdir/$tfile-2
11376         local file3=$DIR/$tdir/$tfile-3
11377         local lovea
11378
11379         mkdir -p $DIR/$tdir
11380         touch $file1 || error "create $file1 failed"
11381         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11382                 error "create $file2 failed"
11383         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11384                 error "create $file3 failed"
11385
11386         $LFS swap_layouts $file1 $file2 ||
11387                 error "swap $file1 $file2 layouts failed"
11388
11389         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11390         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11391
11392         echo 123 > $file1 || error "Should be able to write into $file1"
11393
11394         $LFS swap_layouts $file1 $file3 ||
11395                 error "swap $file1 $file3 layouts failed"
11396
11397         echo 123 > $file1 || error "Should be able to write into $file1"
11398
11399         rm -rf $file1 $file2 $file3
11400 }
11401 run_test 184e "Recreate layout after stripeless layout swaps"
11402
11403 test_185() { # LU-2441
11404         # LU-3553 - no volatile file support in old servers
11405         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11406                 { skip "Need MDS version at least 2.3.60"; return 0; }
11407
11408         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11409         touch $DIR/$tdir/spoo
11410         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11411         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11412                 error "cannot create/write a volatile file"
11413         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11414                 error "FID is still valid after close"
11415
11416         multiop_bg_pause $DIR/$tdir vVw4096_c
11417         local multi_pid=$!
11418
11419         local OLD_IFS=$IFS
11420         IFS=":"
11421         local fidv=($fid)
11422         IFS=$OLD_IFS
11423         # assume that the next FID for this client is sequential, since stdout
11424         # is unfortunately eaten by multiop_bg_pause
11425         local n=$((${fidv[1]} + 1))
11426         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11427         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11428                 error "FID is missing before close"
11429         kill -USR1 $multi_pid
11430         # 1 second delay, so if mtime change we will see it
11431         sleep 1
11432         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11433         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11434 }
11435 run_test 185 "Volatile file support"
11436
11437 test_187a() {
11438         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11439         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11440                 skip "Need MDS version at least 2.3.0" && return
11441
11442         local dir0=$DIR/$tdir/$testnum
11443         mkdir -p $dir0 || error "creating dir $dir0"
11444
11445         local file=$dir0/file1
11446         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11447         local dv1=$($LFS data_version $file)
11448         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11449         local dv2=$($LFS data_version $file)
11450         [[ $dv1 != $dv2 ]] ||
11451                 error "data version did not change on write $dv1 == $dv2"
11452
11453         # clean up
11454         rm -f $file1
11455 }
11456 run_test 187a "Test data version change"
11457
11458 test_187b() {
11459         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11460         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11461                 skip "Need MDS version at least 2.3.0" && return
11462
11463         local dir0=$DIR/$tdir/$testnum
11464         mkdir -p $dir0 || error "creating dir $dir0"
11465
11466         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11467         [[ ${DV[0]} != ${DV[1]} ]] ||
11468                 error "data version did not change on write"\
11469                       " ${DV[0]} == ${DV[1]}"
11470
11471         # clean up
11472         rm -f $file1
11473 }
11474 run_test 187b "Test data version change on volatile file"
11475
11476 test_200() {
11477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11478         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11479
11480         local POOL=${POOL:-cea1}
11481         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11482         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11483         # Pool OST targets
11484         local first_ost=0
11485         local last_ost=$(($OSTCOUNT - 1))
11486         local ost_step=2
11487         local ost_list=$(seq $first_ost $ost_step $last_ost)
11488         local ost_range="$first_ost $last_ost $ost_step"
11489         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11490         local file_dir=$POOL_ROOT/file_tst
11491         local subdir=$test_path/subdir
11492
11493         local rc=0
11494         while : ; do
11495                 # former test_200a test_200b
11496                 pool_add $POOL                          || { rc=$? ; break; }
11497                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11498                 # former test_200c test_200d
11499                 mkdir -p $test_path
11500                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11501                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11502                 mkdir -p $subdir
11503                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11504                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11505                                                         || { rc=$? ; break; }
11506                 # former test_200e test_200f
11507                 local files=$((OSTCOUNT*3))
11508                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11509                                                         || { rc=$? ; break; }
11510                 pool_create_files $POOL $file_dir $files "$ost_list" \
11511                                                         || { rc=$? ; break; }
11512                 # former test_200g test_200h
11513                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11514                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11515
11516                 # former test_201a test_201b test_201c
11517                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11518
11519                 local f=$test_path/$tfile
11520                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11521                 pool_remove $POOL $f                    || { rc=$? ; break; }
11522                 break
11523         done
11524
11525         cleanup_pools
11526         return $rc
11527 }
11528 run_test 200 "OST pools"
11529
11530 # usage: default_attr <count | size | offset>
11531 default_attr() {
11532         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11533 }
11534
11535 # usage: check_default_stripe_attr
11536 check_default_stripe_attr() {
11537         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11538         case $1 in
11539         --stripe-count|--count)
11540                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11541         --stripe-size|--size)
11542                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11543         --stripe-index|--index)
11544                 EXPECTED=-1;;
11545         *)
11546                 error "unknown getstripe attr '$1'"
11547         esac
11548
11549         [ $ACTUAL != $EXPECTED ] &&
11550                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11551 }
11552
11553 test_204a() {
11554         test_mkdir -p $DIR/$tdir
11555         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11556
11557         check_default_stripe_attr --stripe-count
11558         check_default_stripe_attr --stripe-size
11559         check_default_stripe_attr --stripe-index
11560
11561         return 0
11562 }
11563 run_test 204a "Print default stripe attributes ================="
11564
11565 test_204b() {
11566         test_mkdir -p $DIR/$tdir
11567         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11568
11569         check_default_stripe_attr --stripe-size
11570         check_default_stripe_attr --stripe-index
11571
11572         return 0
11573 }
11574 run_test 204b "Print default stripe size and offset  ==========="
11575
11576 test_204c() {
11577         test_mkdir -p $DIR/$tdir
11578         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11579
11580         check_default_stripe_attr --stripe-count
11581         check_default_stripe_attr --stripe-index
11582
11583         return 0
11584 }
11585 run_test 204c "Print default stripe count and offset ==========="
11586
11587 test_204d() {
11588         test_mkdir -p $DIR/$tdir
11589         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11590
11591         check_default_stripe_attr --stripe-count
11592         check_default_stripe_attr --stripe-size
11593
11594         return 0
11595 }
11596 run_test 204d "Print default stripe count and size ============="
11597
11598 test_204e() {
11599         test_mkdir -p $DIR/$tdir
11600         $SETSTRIPE -d $DIR/$tdir
11601
11602         check_default_stripe_attr --stripe-count --raw
11603         check_default_stripe_attr --stripe-size --raw
11604         check_default_stripe_attr --stripe-index --raw
11605
11606         return 0
11607 }
11608 run_test 204e "Print raw stripe attributes ================="
11609
11610 test_204f() {
11611         test_mkdir -p $DIR/$tdir
11612         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11613
11614         check_default_stripe_attr --stripe-size --raw
11615         check_default_stripe_attr --stripe-index --raw
11616
11617         return 0
11618 }
11619 run_test 204f "Print raw stripe size and offset  ==========="
11620
11621 test_204g() {
11622         test_mkdir -p $DIR/$tdir
11623         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11624
11625         check_default_stripe_attr --stripe-count --raw
11626         check_default_stripe_attr --stripe-index --raw
11627
11628         return 0
11629 }
11630 run_test 204g "Print raw stripe count and offset ==========="
11631
11632 test_204h() {
11633         test_mkdir -p $DIR/$tdir
11634         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11635
11636         check_default_stripe_attr --stripe-count --raw
11637         check_default_stripe_attr --stripe-size --raw
11638
11639         return 0
11640 }
11641 run_test 204h "Print raw stripe count and size ============="
11642
11643 # Figure out which job scheduler is being used, if any,
11644 # or use a fake one
11645 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11646         JOBENV=SLURM_JOB_ID
11647 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11648         JOBENV=LSB_JOBID
11649 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11650         JOBENV=PBS_JOBID
11651 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11652         JOBENV=LOADL_STEP_ID
11653 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11654         JOBENV=JOB_ID
11655 else
11656         $LCTL list_param jobid_name > /dev/null 2>&1
11657         if [ $? -eq 0 ]; then
11658                 JOBENV=nodelocal
11659         else
11660                 JOBENV=FAKE_JOBID
11661         fi
11662 fi
11663
11664 verify_jobstats() {
11665         local cmd=($1)
11666         shift
11667         local facets="$@"
11668
11669 # we don't really need to clear the stats for this test to work, since each
11670 # command has a unique jobid, but it makes debugging easier if needed.
11671 #       for facet in $facets; do
11672 #               local dev=$(convert_facet2label $facet)
11673 #               # clear old jobstats
11674 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
11675 #       done
11676
11677         # use a new JobID for each test, or we might see an old one
11678         [ "$JOBENV" = "FAKE_JOBID" ] &&
11679                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11680
11681         JOBVAL=${!JOBENV}
11682
11683         [ "$JOBENV" = "nodelocal" ] && {
11684                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11685                 $LCTL set_param jobid_name=$FAKE_JOBID
11686                 JOBVAL=$FAKE_JOBID
11687         }
11688
11689         log "Test: ${cmd[*]}"
11690         log "Using JobID environment variable $JOBENV=$JOBVAL"
11691
11692         if [ $JOBENV = "FAKE_JOBID" ]; then
11693                 FAKE_JOBID=$JOBVAL ${cmd[*]}
11694         else
11695                 ${cmd[*]}
11696         fi
11697
11698         # all files are created on OST0000
11699         for facet in $facets; do
11700                 local stats="*.$(convert_facet2label $facet).job_stats"
11701                 if [ $(do_facet $facet lctl get_param $stats |
11702                        grep -c $JOBVAL) -ne 1 ]; then
11703                         do_facet $facet lctl get_param $stats
11704                         error "No jobstats for $JOBVAL found on $facet::$stats"
11705                 fi
11706         done
11707 }
11708
11709 jobstats_set() {
11710         trap 0
11711         NEW_JOBENV=${1:-$OLD_JOBENV}
11712         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11713         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11714 }
11715
11716 cleanup_205() {
11717         do_facet $SINGLEMDS \
11718                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
11719         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11720         do_facet $SINGLEMDS lctl --device $MDT0 changelog_deregister $CL_USER
11721 }
11722
11723 test_205() { # Job stats
11724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11725         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11726         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11727         remote_ost_nodsh && skip "remote OST with nodsh" && return
11728
11729         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11730                 skip "Server doesn't support jobstats" && return 0
11731         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11732
11733         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11734         if [ $OLD_JOBENV != $JOBENV ]; then
11735                 jobstats_set $JOBENV
11736                 trap cleanup_205 EXIT
11737         fi
11738
11739         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
11740         echo "Registered as changelog user $CL_USER"
11741
11742         OLD_INTERVAL=$(do_facet $SINGLEMDS \
11743                        lctl get_param -n mdt.*.job_cleanup_interval)
11744         local interval_new=5
11745         do_facet $SINGLEMDS \
11746                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
11747         local start=$SECONDS
11748
11749         local cmd
11750         # mkdir
11751         cmd="mkdir $DIR/$tdir"
11752         verify_jobstats "$cmd" "$SINGLEMDS"
11753         # rmdir
11754         cmd="rmdir $DIR/$tdir"
11755         verify_jobstats "$cmd" "$SINGLEMDS"
11756         # mkdir on secondary MDT
11757         if [ $MDSCOUNT -gt 1 ]; then
11758                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
11759                 verify_jobstats "$cmd" "mds2"
11760         fi
11761         # mknod
11762         cmd="mknod $DIR/$tfile c 1 3"
11763         verify_jobstats "$cmd" "$SINGLEMDS"
11764         # unlink
11765         cmd="rm -f $DIR/$tfile"
11766         verify_jobstats "$cmd" "$SINGLEMDS"
11767         # create all files on OST0000 so verify_jobstats can find OST stats
11768         # open & close
11769         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11770         verify_jobstats "$cmd" "$SINGLEMDS"
11771         # setattr
11772         cmd="touch $DIR/$tfile"
11773         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11774         # write
11775         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11776         verify_jobstats "$cmd" "ost1"
11777         # read
11778         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11779         verify_jobstats "$cmd" "ost1"
11780         # truncate
11781         cmd="$TRUNCATE $DIR/$tfile 0"
11782         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11783         # rename
11784         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
11785         verify_jobstats "$cmd" "$SINGLEMDS"
11786         # jobstats expiry - sleep until old stats should be expired
11787         local left=$((interval_new + 2 - (SECONDS - start)))
11788         [ $left -ge 0 ] && echo "sleep $left for expiry" && sleep $((left + 1))
11789         cmd="mkdir $DIR/$tdir.expire"
11790         verify_jobstats "$cmd" "$SINGLEMDS"
11791         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
11792             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
11793
11794         # Ensure that jobid are present in changelog (if supported by MDS)
11795         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11796         then
11797                 $LFS changelog $MDT0 | tail -9
11798                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11799                 [ $jobids -eq 9 ] ||
11800                         error "Wrong changelog jobid count $jobids != 9"
11801
11802                 # LU-5862
11803                 JOBENV="disable"
11804                 jobstats_set $JOBENV
11805                 touch $DIR/$tfile
11806                 $LFS changelog $MDT0 | tail -1
11807                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11808                 [ $jobids -eq 0 ] ||
11809                         error "Unexpected jobids when jobid_var=$JOBENV"
11810         fi
11811
11812         cleanup_205
11813 }
11814 run_test 205 "Verify job stats"
11815
11816 # LU-1480, LU-1773 and LU-1657
11817 test_206() {
11818         mkdir -p $DIR/$tdir
11819         $SETSTRIPE -c -1 $DIR/$tdir
11820 #define OBD_FAIL_LOV_INIT 0x1403
11821         $LCTL set_param fail_loc=0xa0001403
11822         $LCTL set_param fail_val=1
11823         touch $DIR/$tdir/$tfile || true
11824 }
11825 run_test 206 "fail lov_init_raid0() doesn't lbug"
11826
11827 test_207a() {
11828         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11829         local fsz=`stat -c %s $DIR/$tfile`
11830         cancel_lru_locks mdc
11831
11832         # do not return layout in getattr intent
11833 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11834         $LCTL set_param fail_loc=0x170
11835         local sz=`stat -c %s $DIR/$tfile`
11836
11837         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11838
11839         rm -rf $DIR/$tfile
11840 }
11841 run_test 207a "can refresh layout at glimpse"
11842
11843 test_207b() {
11844         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11845         local cksum=`md5sum $DIR/$tfile`
11846         local fsz=`stat -c %s $DIR/$tfile`
11847         cancel_lru_locks mdc
11848         cancel_lru_locks osc
11849
11850         # do not return layout in getattr intent
11851 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11852         $LCTL set_param fail_loc=0x171
11853
11854         # it will refresh layout after the file is opened but before read issues
11855         echo checksum is "$cksum"
11856         echo "$cksum" |md5sum -c --quiet || error "file differs"
11857
11858         rm -rf $DIR/$tfile
11859 }
11860 run_test 207b "can refresh layout at open"
11861
11862 test_208() {
11863         # FIXME: in this test suite, only RD lease is used. This is okay
11864         # for now as only exclusive open is supported. After generic lease
11865         # is done, this test suite should be revised. - Jinshan
11866
11867         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11868         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11869                 { skip "Need MDS version at least 2.4.52"; return 0; }
11870
11871         echo "==== test 1: verify get lease work"
11872         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11873
11874         echo "==== test 2: verify lease can be broken by upcoming open"
11875         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11876         local PID=$!
11877         sleep 1
11878
11879         $MULTIOP $DIR/$tfile oO_RDONLY:c
11880         kill -USR1 $PID && wait $PID || error "break lease error"
11881
11882         echo "==== test 3: verify lease can't be granted if an open already exists"
11883         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11884         local PID=$!
11885         sleep 1
11886
11887         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11888         kill -USR1 $PID && wait $PID || error "open file error"
11889
11890         echo "==== test 4: lease can sustain over recovery"
11891         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11892         PID=$!
11893         sleep 1
11894
11895         fail mds1
11896
11897         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11898
11899         echo "==== test 5: lease broken can't be regained by replay"
11900         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11901         PID=$!
11902         sleep 1
11903
11904         # open file to break lease and then recovery
11905         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11906         fail mds1
11907
11908         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11909
11910         rm -f $DIR/$tfile
11911 }
11912 run_test 208 "Exclusive open"
11913
11914 test_209() {
11915         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11916                 skip_env "must have disp_stripe" && return
11917
11918         touch $DIR/$tfile
11919         sync; sleep 5; sync;
11920
11921         echo 3 > /proc/sys/vm/drop_caches
11922         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11923
11924         # open/close 500 times
11925         for i in $(seq 500); do
11926                 cat $DIR/$tfile
11927         done
11928
11929         echo 3 > /proc/sys/vm/drop_caches
11930         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11931
11932         echo "before: $req_before, after: $req_after"
11933         [ $((req_after - req_before)) -ge 300 ] &&
11934                 error "open/close requests are not freed"
11935         return 0
11936 }
11937 run_test 209 "read-only open/close requests should be freed promptly"
11938
11939 test_212() {
11940         size=`date +%s`
11941         size=$((size % 8192 + 1))
11942         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11943         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11944         rm -f $DIR/f212 $DIR/f212.xyz
11945 }
11946 run_test 212 "Sendfile test ============================================"
11947
11948 test_213() {
11949         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11950         cancel_lru_locks osc
11951         lctl set_param fail_loc=0x8000040f
11952         # generate a read lock
11953         cat $DIR/$tfile > /dev/null
11954         # write to the file, it will try to cancel the above read lock.
11955         cat /etc/hosts >> $DIR/$tfile
11956 }
11957 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11958
11959 test_214() { # for bug 20133
11960         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11961         for (( i=0; i < 340; i++ )) ; do
11962                 touch $DIR/$tdir/d214c/a$i
11963         done
11964
11965         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11966         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11967         ls $DIR/d214c || error "ls $DIR/d214c failed"
11968         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11969         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11970 }
11971 run_test 214 "hash-indexed directory test - bug 20133"
11972
11973 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11974 create_lnet_proc_files() {
11975         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11976         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11977
11978         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11979         rm -f "$TMP/lnet_$1.sys_tmp"
11980 }
11981
11982 # counterpart of create_lnet_proc_files
11983 remove_lnet_proc_files() {
11984         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11985 }
11986
11987 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11988 # 3rd arg as regexp for body
11989 check_lnet_proc_stats() {
11990         local l=$(cat "$TMP/lnet_$1" |wc -l)
11991         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11992
11993         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11994 }
11995
11996 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11997 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11998 # optional and can be regexp for 2nd line (lnet.routes case)
11999 check_lnet_proc_entry() {
12000         local blp=2          # blp stands for 'position of 1st line of body'
12001         [ -z "$5" ] || blp=3 # lnet.routes case
12002
12003         local l=$(cat "$TMP/lnet_$1" |wc -l)
12004         # subtracting one from $blp because the body can be empty
12005         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12006
12007         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12008                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12009
12010         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12011                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12012
12013         # bail out if any unexpected line happened
12014         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12015         [ "$?" != 0 ] || error "$2 misformatted"
12016 }
12017
12018 test_215() { # for bugs 18102, 21079, 21517
12019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12020         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12021         local P='[1-9][0-9]*'           # positive numeric
12022         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12023         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12024         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12025         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12026
12027         local L1 # regexp for 1st line
12028         local L2 # regexp for 2nd line (optional)
12029         local BR # regexp for the rest (body)
12030
12031         # lnet.stats should look as 11 space-separated non-negative numerics
12032         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12033         create_lnet_proc_files "stats"
12034         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12035         remove_lnet_proc_files "stats"
12036
12037         # lnet.routes should look like this:
12038         # Routing disabled/enabled
12039         # net hops priority state router
12040         # where net is a string like tcp0, hops > 0, priority >= 0,
12041         # state is up/down,
12042         # router is a string like 192.168.1.1@tcp2
12043         L1="^Routing (disabled|enabled)$"
12044         L2="^net +hops +priority +state +router$"
12045         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12046         create_lnet_proc_files "routes"
12047         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12048         remove_lnet_proc_files "routes"
12049
12050         # lnet.routers should look like this:
12051         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12052         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12053         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12054         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12055         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12056         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12057         create_lnet_proc_files "routers"
12058         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12059         remove_lnet_proc_files "routers"
12060
12061         # lnet.peers should look like this:
12062         # nid refs state last max rtr min tx min queue
12063         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12064         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12065         # numeric (0 or >0 or <0), queue >= 0.
12066         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12067         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12068         create_lnet_proc_files "peers"
12069         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12070         remove_lnet_proc_files "peers"
12071
12072         # lnet.buffers  should look like this:
12073         # pages count credits min
12074         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12075         L1="^pages +count +credits +min$"
12076         BR="^ +$N +$N +$I +$I$"
12077         create_lnet_proc_files "buffers"
12078         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12079         remove_lnet_proc_files "buffers"
12080
12081         # lnet.nis should look like this:
12082         # nid status alive refs peer rtr max tx min
12083         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12084         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12085         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12086         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12087         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12088         create_lnet_proc_files "nis"
12089         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12090         remove_lnet_proc_files "nis"
12091
12092         # can we successfully write to lnet.stats?
12093         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12094         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12095 }
12096 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12097
12098 test_216() { # bug 20317
12099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12100         remote_ost_nodsh && skip "remote OST with nodsh" && return
12101
12102         local node
12103         local facets=$(get_facets OST)
12104         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12105
12106         save_lustre_params client "osc.*.contention_seconds" > $p
12107         save_lustre_params $facets \
12108                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12109         save_lustre_params $facets \
12110                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12111         save_lustre_params $facets \
12112                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12113         clear_osc_stats
12114
12115         # agressive lockless i/o settings
12116         for node in $(osts_nodes); do
12117                 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'
12118         done
12119         lctl set_param -n osc.*.contention_seconds 60
12120
12121         $DIRECTIO write $DIR/$tfile 0 10 4096
12122         $CHECKSTAT -s 40960 $DIR/$tfile
12123
12124         # disable lockless i/o
12125         for node in $(osts_nodes); do
12126                 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'
12127         done
12128         lctl set_param -n osc.*.contention_seconds 0
12129         clear_osc_stats
12130
12131         dd if=/dev/zero of=$DIR/$tfile count=0
12132         $CHECKSTAT -s 0 $DIR/$tfile
12133
12134         restore_lustre_params <$p
12135         rm -f $p
12136         rm $DIR/$tfile
12137 }
12138 run_test 216 "check lockless direct write works and updates file size and kms correctly"
12139
12140 test_217() { # bug 22430
12141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12142         local node
12143         local nid
12144
12145         for node in $(nodes_list); do
12146                 nid=$(host_nids_address $node $NETTYPE)
12147                 if [[ $nid = *-* ]] ; then
12148                         echo "lctl ping $nid@$NETTYPE"
12149                         lctl ping $nid@$NETTYPE
12150                 else
12151                         echo "skipping $node (no hyphen detected)"
12152                 fi
12153         done
12154 }
12155 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12156
12157 test_218() {
12158        # do directio so as not to populate the page cache
12159        log "creating a 10 Mb file"
12160        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12161        log "starting reads"
12162        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12163        log "truncating the file"
12164        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12165        log "killing dd"
12166        kill %+ || true # reads might have finished
12167        echo "wait until dd is finished"
12168        wait
12169        log "removing the temporary file"
12170        rm -rf $DIR/$tfile || error "tmp file removal failed"
12171 }
12172 run_test 218 "parallel read and truncate should not deadlock ======================="
12173
12174 test_219() {
12175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12176         # write one partial page
12177         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12178         # set no grant so vvp_io_commit_write will do sync write
12179         $LCTL set_param fail_loc=0x411
12180         # write a full page at the end of file
12181         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12182
12183         $LCTL set_param fail_loc=0
12184         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12185         $LCTL set_param fail_loc=0x411
12186         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12187
12188         # LU-4201
12189         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12190         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12191 }
12192 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12193
12194 test_220() { #LU-325
12195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12196         remote_ost_nodsh && skip "remote OST with nodsh" && return
12197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12198         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12199         local OSTIDX=0
12200
12201         # create on MDT0000 so the last_id and next_id are correct
12202         mkdir $DIR/$tdir
12203         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12204         OST=${OST%_UUID}
12205
12206         # on the mdt's osc
12207         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12208         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12209                         osc.$mdtosc_proc1.prealloc_last_id)
12210         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12211                         osc.$mdtosc_proc1.prealloc_next_id)
12212
12213         $LFS df -i
12214
12215         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12216         #define OBD_FAIL_OST_ENOINO              0x229
12217         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12218         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
12219         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12220
12221         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12222
12223         MDSOBJS=$((last_id - next_id))
12224         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12225
12226         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12227         echo "OST still has $count kbytes free"
12228
12229         echo "create $MDSOBJS files @next_id..."
12230         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12231
12232         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12233                         osc.$mdtosc_proc1.prealloc_last_id)
12234         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12235                         osc.$mdtosc_proc1.prealloc_next_id)
12236
12237         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12238         $LFS df -i
12239
12240         echo "cleanup..."
12241
12242         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12243         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12244
12245         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12246         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12247         echo "unlink $MDSOBJS files @$next_id..."
12248         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12249 }
12250 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12251
12252 test_221() {
12253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12254         dd if=`which date` of=$MOUNT/date oflag=sync
12255         chmod +x $MOUNT/date
12256
12257         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12258         $LCTL set_param fail_loc=0x80001401
12259
12260         $MOUNT/date > /dev/null
12261         rm -f $MOUNT/date
12262 }
12263 run_test 221 "make sure fault and truncate race to not cause OOM"
12264
12265 test_222a () {
12266         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12267        rm -rf $DIR/$tdir
12268        test_mkdir -p $DIR/$tdir
12269        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12270        createmany -o $DIR/$tdir/$tfile 10
12271        cancel_lru_locks mdc
12272        cancel_lru_locks osc
12273        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12274        $LCTL set_param fail_loc=0x31a
12275        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12276        $LCTL set_param fail_loc=0
12277        rm -r $DIR/$tdir
12278 }
12279 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12280
12281 test_222b () {
12282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12283        rm -rf $DIR/$tdir
12284        test_mkdir -p $DIR/$tdir
12285        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12286        createmany -o $DIR/$tdir/$tfile 10
12287        cancel_lru_locks mdc
12288        cancel_lru_locks osc
12289        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12290        $LCTL set_param fail_loc=0x31a
12291        rm -r $DIR/$tdir || "AGL for rmdir failed"
12292        $LCTL set_param fail_loc=0
12293 }
12294 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12295
12296 test_223 () {
12297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12298        rm -rf $DIR/$tdir
12299        test_mkdir -p $DIR/$tdir
12300        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12301        createmany -o $DIR/$tdir/$tfile 10
12302        cancel_lru_locks mdc
12303        cancel_lru_locks osc
12304        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12305        $LCTL set_param fail_loc=0x31b
12306        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12307        $LCTL set_param fail_loc=0
12308        rm -r $DIR/$tdir
12309 }
12310 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12311
12312 test_224a() { # LU-1039, MRP-303
12313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12314         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12315         $LCTL set_param fail_loc=0x508
12316         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12317         $LCTL set_param fail_loc=0
12318         df $DIR
12319 }
12320 run_test 224a "Don't panic on bulk IO failure"
12321
12322 test_224b() { # LU-1039, MRP-303
12323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12324         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12325         cancel_lru_locks osc
12326         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12327         $LCTL set_param fail_loc=0x515
12328         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12329         $LCTL set_param fail_loc=0
12330         df $DIR
12331 }
12332 run_test 224b "Don't panic on bulk IO failure"
12333
12334 test_224c() { # LU-6441
12335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12336         local pages_per_rpc=$($LCTL get_param \
12337                                 osc.*.max_pages_per_rpc)
12338         local at_max=$($LCTL get_param -n at_max)
12339         local timeout=$($LCTL get_param -n timeout)
12340         local test_at="$LCTL get_param -n at_max"
12341         local param_at="$FSNAME.sys.at_max"
12342         local test_timeout="$LCTL get_param -n timeout"
12343         local param_timeout="$FSNAME.sys.timeout"
12344
12345         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12346
12347         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
12348                 error "conf_param at_max=0 failed"
12349         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
12350                 error "conf_param timeout=5 failed"
12351
12352         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
12353         $LCTL set_param fail_loc=0x520
12354         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
12355         sync
12356         $LCTL set_param fail_loc=0
12357
12358         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
12359                 error "conf_param at_max=$at_max failed"
12360         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
12361                 $timeout || error "conf_param timeout=$timeout failed"
12362
12363         $LCTL set_param -n $pages_per_rpc
12364 }
12365 run_test 224c "Don't hang if one of md lost during large bulk RPC"
12366
12367 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12368 test_225a () {
12369         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12370         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12371         if [ -z ${MDSSURVEY} ]; then
12372               skip_env "mds-survey not found" && return
12373         fi
12374
12375         [ $MDSCOUNT -ge 2 ] &&
12376                 skip "skipping now for more than one MDT" && return
12377
12378        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12379             { skip "Need MDS version at least 2.2.51"; return; }
12380
12381        local mds=$(facet_host $SINGLEMDS)
12382        local target=$(do_nodes $mds 'lctl dl' | \
12383                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12384
12385        local cmd1="file_count=1000 thrhi=4"
12386        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12387        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12388        local cmd="$cmd1 $cmd2 $cmd3"
12389
12390        rm -f ${TMP}/mds_survey*
12391        echo + $cmd
12392        eval $cmd || error "mds-survey with zero-stripe failed"
12393        cat ${TMP}/mds_survey*
12394        rm -f ${TMP}/mds_survey*
12395 }
12396 run_test 225a "Metadata survey sanity with zero-stripe"
12397
12398 test_225b () {
12399         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12400         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12401         if [ -z ${MDSSURVEY} ]; then
12402               skip_env "mds-survey not found" && return
12403         fi
12404         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12405             { skip "Need MDS version at least 2.2.51"; return; }
12406
12407         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12408               skip_env "Need to mount OST to test" && return
12409         fi
12410
12411         [ $MDSCOUNT -ge 2 ] &&
12412                 skip "skipping now for more than one MDT" && return
12413        local mds=$(facet_host $SINGLEMDS)
12414        local target=$(do_nodes $mds 'lctl dl' | \
12415                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12416
12417        local cmd1="file_count=1000 thrhi=4"
12418        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12419        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12420        local cmd="$cmd1 $cmd2 $cmd3"
12421
12422        rm -f ${TMP}/mds_survey*
12423        echo + $cmd
12424        eval $cmd || error "mds-survey with stripe_count failed"
12425        cat ${TMP}/mds_survey*
12426        rm -f ${TMP}/mds_survey*
12427 }
12428 run_test 225b "Metadata survey sanity with stripe_count = 1"
12429
12430 mcreate_path2fid () {
12431         local mode=$1
12432         local major=$2
12433         local minor=$3
12434         local name=$4
12435         local desc=$5
12436         local path=$DIR/$tdir/$name
12437         local fid
12438         local rc
12439         local fid_path
12440
12441         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12442                 error "cannot create $desc"
12443
12444         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12445         rc=$?
12446         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12447
12448         fid_path=$($LFS fid2path $MOUNT $fid)
12449         rc=$?
12450         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12451
12452         [ "$path" == "$fid_path" ] ||
12453                 error "fid2path returned $fid_path, expected $path"
12454
12455         echo "pass with $path and $fid"
12456 }
12457
12458 test_226a () {
12459         rm -rf $DIR/$tdir
12460         mkdir -p $DIR/$tdir
12461
12462         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12463         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12464         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12465         mcreate_path2fid 0040666 0 0 dir "directory"
12466         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12467         mcreate_path2fid 0100666 0 0 file "regular file"
12468         mcreate_path2fid 0120666 0 0 link "symbolic link"
12469         mcreate_path2fid 0140666 0 0 sock "socket"
12470 }
12471 run_test 226a "call path2fid and fid2path on files of all type"
12472
12473 test_226b () {
12474         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12475         rm -rf $DIR/$tdir
12476         local MDTIDX=1
12477
12478         mkdir -p $DIR/$tdir
12479         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12480                 error "create remote directory failed"
12481         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12482         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12483                                 "character special file (null)"
12484         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12485                                 "character special file (no device)"
12486         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12487         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12488                                 "block special file (loop)"
12489         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12490         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12491         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12492 }
12493 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12494
12495 # LU-1299 Executing or running ldd on a truncated executable does not
12496 # cause an out-of-memory condition.
12497 test_227() {
12498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12499         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12500         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12501         chmod +x $MOUNT/date
12502
12503         $MOUNT/date > /dev/null
12504         ldd $MOUNT/date > /dev/null
12505         rm -f $MOUNT/date
12506 }
12507 run_test 227 "running truncated executable does not cause OOM"
12508
12509 # LU-1512 try to reuse idle OI blocks
12510 test_228a() {
12511         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12512         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12513         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12514                 skip "non-ldiskfs backend" && return
12515
12516         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12517         local myDIR=$DIR/$tdir
12518
12519         mkdir -p $myDIR
12520         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12521         $LCTL set_param fail_loc=0x80001002
12522         createmany -o $myDIR/t- 10000
12523         $LCTL set_param fail_loc=0
12524         # The guard is current the largest FID holder
12525         touch $myDIR/guard
12526         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12527                     tr -d '[')
12528         local IDX=$(($SEQ % 64))
12529
12530         do_facet $SINGLEMDS sync
12531         # Make sure journal flushed.
12532         sleep 6
12533         local blk1=$(do_facet $SINGLEMDS \
12534                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12535                      grep Blockcount | awk '{print $4}')
12536
12537         # Remove old files, some OI blocks will become idle.
12538         unlinkmany $myDIR/t- 10000
12539         # Create new files, idle OI blocks should be reused.
12540         createmany -o $myDIR/t- 2000
12541         do_facet $SINGLEMDS sync
12542         # Make sure journal flushed.
12543         sleep 6
12544         local blk2=$(do_facet $SINGLEMDS \
12545                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12546                      grep Blockcount | awk '{print $4}')
12547
12548         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12549 }
12550 run_test 228a "try to reuse idle OI blocks"
12551
12552 test_228b() {
12553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12554         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12555         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12556                 skip "non-ldiskfs backend" && return
12557
12558         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12559         local myDIR=$DIR/$tdir
12560
12561         mkdir -p $myDIR
12562         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12563         $LCTL set_param fail_loc=0x80001002
12564         createmany -o $myDIR/t- 10000
12565         $LCTL set_param fail_loc=0
12566         # The guard is current the largest FID holder
12567         touch $myDIR/guard
12568         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12569                     tr -d '[')
12570         local IDX=$(($SEQ % 64))
12571
12572         do_facet $SINGLEMDS sync
12573         # Make sure journal flushed.
12574         sleep 6
12575         local blk1=$(do_facet $SINGLEMDS \
12576                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12577                      grep Blockcount | awk '{print $4}')
12578
12579         # Remove old files, some OI blocks will become idle.
12580         unlinkmany $myDIR/t- 10000
12581
12582         # stop the MDT
12583         stop $SINGLEMDS || error "Fail to stop MDT."
12584         # remount the MDT
12585         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12586
12587         df $MOUNT || error "Fail to df."
12588         # Create new files, idle OI blocks should be reused.
12589         createmany -o $myDIR/t- 2000
12590         do_facet $SINGLEMDS sync
12591         # Make sure journal flushed.
12592         sleep 6
12593         local blk2=$(do_facet $SINGLEMDS \
12594                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12595                      grep Blockcount | awk '{print $4}')
12596
12597         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12598 }
12599 run_test 228b "idle OI blocks can be reused after MDT restart"
12600
12601 #LU-1881
12602 test_228c() {
12603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12604         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12605         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12606                 skip "non-ldiskfs backend" && return
12607
12608         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12609         local myDIR=$DIR/$tdir
12610
12611         mkdir -p $myDIR
12612         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12613         $LCTL set_param fail_loc=0x80001002
12614         # 20000 files can guarantee there are index nodes in the OI file
12615         createmany -o $myDIR/t- 20000
12616         $LCTL set_param fail_loc=0
12617         # The guard is current the largest FID holder
12618         touch $myDIR/guard
12619         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12620                     tr -d '[')
12621         local IDX=$(($SEQ % 64))
12622
12623         do_facet $SINGLEMDS sync
12624         # Make sure journal flushed.
12625         sleep 6
12626         local blk1=$(do_facet $SINGLEMDS \
12627                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12628                      grep Blockcount | awk '{print $4}')
12629
12630         # Remove old files, some OI blocks will become idle.
12631         unlinkmany $myDIR/t- 20000
12632         rm -f $myDIR/guard
12633         # The OI file should become empty now
12634
12635         # Create new files, idle OI blocks should be reused.
12636         createmany -o $myDIR/t- 2000
12637         do_facet $SINGLEMDS sync
12638         # Make sure journal flushed.
12639         sleep 6
12640         local blk2=$(do_facet $SINGLEMDS \
12641                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12642                      grep Blockcount | awk '{print $4}')
12643
12644         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12645 }
12646 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12647
12648 test_229() { # LU-2482, LU-3448
12649         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12650                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
12651                 return
12652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12653         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12654
12655         rm -f $DIR/$tfile
12656
12657         # Create a file with a released layout and stripe count 2.
12658         $MULTIOP $DIR/$tfile H2c ||
12659                 error "failed to create file with released layout"
12660
12661         $GETSTRIPE -v $DIR/$tfile
12662
12663         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12664         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12665
12666         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12667         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12668         stat $DIR/$tfile || error "failed to stat released file"
12669
12670         chown $RUNAS_ID $DIR/$tfile ||
12671                 error "chown $RUNAS_ID $DIR/$tfile failed"
12672
12673         chgrp $RUNAS_ID $DIR/$tfile ||
12674                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12675
12676         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12677         rm $DIR/$tfile || error "failed to remove released file"
12678 }
12679 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12680
12681 test_230a() {
12682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12683         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12684         local MDTIDX=1
12685
12686         test_mkdir $DIR/$tdir
12687         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
12688         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12689         [ $mdt_idx -ne 0 ] &&
12690                 error "create local directory on wrong MDT $mdt_idx"
12691
12692         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12693                         error "create remote directory failed"
12694         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12695         [ $mdt_idx -ne $MDTIDX ] &&
12696                 error "create remote directory on wrong MDT $mdt_idx"
12697
12698         createmany -o $DIR/$tdir/test_230/t- 10 ||
12699                 error "create files on remote directory failed"
12700         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12701         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12702         rm -r $DIR/$tdir || error "unlink remote directory failed"
12703 }
12704 run_test 230a "Create remote directory and files under the remote directory"
12705
12706 test_230b() {
12707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12708         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12709         local MDTIDX=1
12710         local mdt_index
12711         local i
12712         local file
12713         local pid
12714         local stripe_count
12715         local migrate_dir=$DIR/$tdir/migrate_dir
12716         local other_dir=$DIR/$tdir/other_dir
12717
12718         test_mkdir $DIR/$tdir
12719         test_mkdir -i0 -c1 $migrate_dir
12720         test_mkdir -i0 -c1 $other_dir
12721         for ((i=0; i<10; i++)); do
12722                 mkdir -p $migrate_dir/dir_${i}
12723                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12724                         error "create files under remote dir failed $i"
12725         done
12726
12727         cp /etc/passwd $migrate_dir/$tfile
12728         cp /etc/passwd $other_dir/$tfile
12729         chattr +SAD $migrate_dir
12730         chattr +SAD $migrate_dir/$tfile
12731
12732         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12733         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12734         local old_dir_mode=$(stat -c%f $migrate_dir)
12735         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12736
12737         mkdir -p $migrate_dir/dir_default_stripe2
12738         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12739         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12740
12741         mkdir -p $other_dir
12742         ln $migrate_dir/$tfile $other_dir/luna
12743         ln $migrate_dir/$tfile $migrate_dir/sofia
12744         ln $other_dir/$tfile $migrate_dir/david
12745         ln -s $migrate_dir/$tfile $other_dir/zachary
12746         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12747         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12748
12749         $LFS migrate -m $MDTIDX $migrate_dir ||
12750                 error "fails on migrating remote dir to MDT1"
12751
12752         echo "migratate to MDT1, then checking.."
12753         for ((i = 0; i < 10; i++)); do
12754                 for file in $(find $migrate_dir/dir_${i}); do
12755                         mdt_index=$($LFS getstripe -M $file)
12756                         [ $mdt_index == $MDTIDX ] ||
12757                                 error "$file is not on MDT${MDTIDX}"
12758                 done
12759         done
12760
12761         # the multiple link file should still in MDT0
12762         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12763         [ $mdt_index == 0 ] ||
12764                 error "$file is not on MDT${MDTIDX}"
12765
12766         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12767         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12768                 error " expect $old_dir_flag get $new_dir_flag"
12769
12770         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12771         [ "$old_file_flag" = "$new_file_flag" ] ||
12772                 error " expect $old_file_flag get $new_file_flag"
12773
12774         local new_dir_mode=$(stat -c%f $migrate_dir)
12775         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12776                 error "expect mode $old_dir_mode get $new_dir_mode"
12777
12778         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12779         [ "$old_file_mode" = "$new_file_mode" ] ||
12780                 error "expect mode $old_file_mode get $new_file_mode"
12781
12782         diff /etc/passwd $migrate_dir/$tfile ||
12783                 error "$tfile different after migration"
12784
12785         diff /etc/passwd $other_dir/luna ||
12786                 error "luna different after migration"
12787
12788         diff /etc/passwd $migrate_dir/sofia ||
12789                 error "sofia different after migration"
12790
12791         diff /etc/passwd $migrate_dir/david ||
12792                 error "david different after migration"
12793
12794         diff /etc/passwd $other_dir/zachary ||
12795                 error "zachary different after migration"
12796
12797         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12798                 error "${tfile}_ln different after migration"
12799
12800         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12801                 error "${tfile}_ln_other different after migration"
12802
12803         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12804         [ $stripe_count = 2 ] ||
12805                 error "dir strpe_count $d != 2 after migration."
12806
12807         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12808         [ $stripe_count = 2 ] ||
12809                 error "file strpe_count $d != 2 after migration."
12810
12811         #migrate back to MDT0
12812         MDTIDX=0
12813
12814         $LFS migrate -m $MDTIDX $migrate_dir ||
12815                 error "fails on migrating remote dir to MDT0"
12816
12817         echo "migrate back to MDT0, checking.."
12818         for file in $(find $migrate_dir); do
12819                 mdt_index=$($LFS getstripe -M $file)
12820                 [ $mdt_index == $MDTIDX ] ||
12821                         error "$file is not on MDT${MDTIDX}"
12822         done
12823
12824         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12825         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12826                 error " expect $old_dir_flag get $new_dir_flag"
12827
12828         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12829         [ "$old_file_flag" = "$new_file_flag" ] ||
12830                 error " expect $old_file_flag get $new_file_flag"
12831
12832         local new_dir_mode=$(stat -c%f $migrate_dir)
12833         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12834                 error "expect mode $old_dir_mode get $new_dir_mode"
12835
12836         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12837         [ "$old_file_mode" = "$new_file_mode" ] ||
12838                 error "expect mode $old_file_mode get $new_file_mode"
12839
12840         diff /etc/passwd ${migrate_dir}/$tfile ||
12841                 error "$tfile different after migration"
12842
12843         diff /etc/passwd ${other_dir}/luna ||
12844                 error "luna different after migration"
12845
12846         diff /etc/passwd ${migrate_dir}/sofia ||
12847                 error "sofia different after migration"
12848
12849         diff /etc/passwd ${other_dir}/zachary ||
12850                 error "zachary different after migration"
12851
12852         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12853                 error "${tfile}_ln different after migration"
12854
12855         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12856                 error "${tfile}_ln_other different after migration"
12857
12858         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12859         [ $stripe_count = 2 ] ||
12860                 error "dir strpe_count $d != 2 after migration."
12861
12862         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12863         [ $stripe_count = 2 ] ||
12864                 error "file strpe_count $d != 2 after migration."
12865
12866         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12867 }
12868 run_test 230b "migrate directory"
12869
12870 test_230c() {
12871         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12872         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12873         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12874         local MDTIDX=1
12875         local mdt_index
12876         local file
12877         local migrate_dir=$DIR/$tdir/migrate_dir
12878
12879         #If migrating directory fails in the middle, all entries of
12880         #the directory is still accessiable.
12881         test_mkdir $DIR/$tdir
12882         test_mkdir -i0 -c1 $migrate_dir
12883         stat $migrate_dir
12884         createmany -o $migrate_dir/f 10 ||
12885                 error "create files under ${migrate_dir} failed"
12886
12887         #failed after migrating 5 entries
12888         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12889         do_facet mds1 lctl set_param fail_loc=0x20001801
12890         do_facet mds1 lctl  set_param fail_val=5
12891         local t=$(ls $migrate_dir | wc -l)
12892         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
12893                 error "migrate should fail after 5 entries"
12894
12895         mkdir $migrate_dir/dir &&
12896                 error "mkdir succeeds under migrating directory"
12897         touch $migrate_dir/file &&
12898                 error "touch file succeeds under migrating directory"
12899
12900         local u=$(ls $migrate_dir | wc -l)
12901         [ "$u" == "$t" ] || error "$u != $t during migration"
12902
12903         for file in $(find $migrate_dir); do
12904                 stat $file || error "stat $file failed"
12905         done
12906
12907         do_facet mds1 lctl set_param fail_loc=0
12908         do_facet mds1 lctl set_param fail_val=0
12909
12910         $LFS migrate -m $MDTIDX $migrate_dir ||
12911                 error "migrate open files should failed with open files"
12912
12913         echo "Finish migration, then checking.."
12914         for file in $(find $migrate_dir); do
12915                 mdt_index=$($LFS getstripe -M $file)
12916                 [ $mdt_index == $MDTIDX ] ||
12917                         error "$file is not on MDT${MDTIDX}"
12918         done
12919
12920         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12921 }
12922 run_test 230c "check directory accessiblity if migration is failed"
12923
12924 test_230d() {
12925         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12926         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12927         local MDTIDX=1
12928         local mdt_index
12929         local migrate_dir=$DIR/$tdir/migrate_dir
12930         local i
12931         local j
12932
12933         test_mkdir $DIR/$tdir
12934         test_mkdir -i0 -c1 $migrate_dir
12935
12936         for ((i=0; i<100; i++)); do
12937                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
12938                 createmany -o $migrate_dir/dir_${i}/f 100 ||
12939                         error "create files under remote dir failed $i"
12940         done
12941
12942         $LFS migrate -m $MDTIDX $migrate_dir ||
12943                 error "migrate remote dir error"
12944
12945         echo "Finish migration, then checking.."
12946         for file in $(find $migrate_dir); do
12947                 mdt_index=$($LFS getstripe -M $file)
12948                 [ $mdt_index == $MDTIDX ] ||
12949                         error "$file is not on MDT${MDTIDX}"
12950         done
12951
12952         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12953 }
12954 run_test 230d "check migrate big directory"
12955
12956 test_230e() {
12957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12958         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12959         local i
12960         local j
12961         local a_fid
12962         local b_fid
12963
12964         mkdir -p $DIR/$tdir
12965         mkdir $DIR/$tdir/migrate_dir
12966         mkdir $DIR/$tdir/other_dir
12967         touch $DIR/$tdir/migrate_dir/a
12968         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
12969
12970         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
12971                 error "migrate dir fails"
12972
12973         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
12974         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
12975
12976         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12977         [ $mdt_index == 0 ] || error "a is not on MDT0"
12978
12979         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
12980                 error "migrate dir fails"
12981
12982         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
12983         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
12984
12985         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12986         [ $mdt_index == 1 ] || error "a is not on MDT1"
12987
12988         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
12989         [ $mdt_index == 1 ] || error "b is not on MDT1"
12990
12991         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
12992         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
12993
12994         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
12995
12996         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12997 }
12998 run_test 230e "migrate mulitple local link files"
12999
13000 test_230f() {
13001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13002         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13003         local a_fid
13004         local ln_fid
13005
13006         mkdir -p $DIR/$tdir
13007         mkdir $DIR/$tdir/migrate_dir
13008         $LFS mkdir -i1 $DIR/$tdir/other_dir
13009         touch $DIR/$tdir/migrate_dir/a
13010         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13011         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13012
13013         # a should be migrated to MDT1, since no other links on MDT0
13014         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13015                 error "migrate dir fails"
13016         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13017         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13018         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13019         [ $mdt_index == 1 ] || error "a is not on MDT1"
13020
13021         # a should stay on MDT1, because it is a mulitple link file
13022         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13023                 error "migrate dir fails"
13024         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13025         [ $mdt_index == 1 ] || error "a is not on MDT1"
13026
13027         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13028                 error "migrate dir fails"
13029
13030         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13031         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13032         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13033
13034         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13035         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13036
13037         # a should be migrated to MDT0, since no other links on MDT1
13038         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13039                 error "migrate dir fails"
13040         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13041         [ $mdt_index == 0 ] || error "a is not on MDT0"
13042
13043         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13044 }
13045 run_test 230f "migrate mulitple remote link files"
13046
13047 test_231a()
13048 {
13049         # For simplicity this test assumes that max_pages_per_rpc
13050         # is the same across all OSCs
13051         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
13052         local bulk_size=$((max_pages * 4096))
13053
13054         mkdir -p $DIR/$tdir
13055
13056         # clear the OSC stats
13057         $LCTL set_param osc.*.stats=0 &>/dev/null
13058
13059         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
13060         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
13061                 oflag=direct &>/dev/null || error "dd failed"
13062
13063         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
13064         if [ x$nrpcs != "x1" ]; then
13065                 error "found $nrpc ost_write RPCs, not 1 as expected"
13066         fi
13067
13068         # Drop the OSC cache, otherwise we will read from it
13069         cancel_lru_locks osc
13070
13071         # clear the OSC stats
13072         $LCTL set_param osc.*.stats=0 &>/dev/null
13073
13074         # Client reads $bulk_size.
13075         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
13076                 iflag=direct &>/dev/null || error "dd failed"
13077
13078         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
13079         if [ x$nrpcs != "x1" ]; then
13080                 error "found $nrpc ost_read RPCs, not 1 as expected"
13081         fi
13082 }
13083 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
13084
13085 test_231b() {
13086         mkdir -p $DIR/$tdir
13087         local i
13088         for i in {0..1023}; do
13089                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
13090                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
13091                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
13092         done
13093         sync
13094 }
13095 run_test 231b "must not assert on fully utilized OST request buffer"
13096
13097 test_232() {
13098         mkdir -p $DIR/$tdir
13099         #define OBD_FAIL_LDLM_OST_LVB            0x31c
13100         $LCTL set_param fail_loc=0x31c
13101
13102         # ignore dd failure
13103         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
13104
13105         $LCTL set_param fail_loc=0
13106         umount_client $MOUNT || error "umount failed"
13107         mount_client $MOUNT || error "mount failed"
13108 }
13109 run_test 232 "failed lock should not block umount"
13110
13111 test_233a() {
13112         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13113         { skip "Need MDS version at least 2.3.64"; return; }
13114
13115         local fid=$($LFS path2fid $MOUNT)
13116         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13117                 error "cannot access $MOUNT using its FID '$fid'"
13118 }
13119 run_test 233a "checking that OBF of the FS root succeeds"
13120
13121 test_233b() {
13122         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13123         { skip "Need MDS version at least 2.5.90"; return; }
13124
13125         local fid=$($LFS path2fid $MOUNT/.lustre)
13126         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13127                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13128
13129         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13130         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13131                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13132 }
13133 run_test 233b "checking that OBF of the FS .lustre succeeds"
13134
13135 test_234() {
13136         local p="$TMP/sanityN-$TESTNAME.parameters"
13137         save_lustre_params client "llite.*.xattr_cache" > $p
13138         lctl set_param llite.*.xattr_cache 1 ||
13139                 { skip "xattr cache is not supported"; return 0; }
13140
13141         mkdir -p $DIR/$tdir || error "mkdir failed"
13142         touch $DIR/$tdir/$tfile || error "touch failed"
13143         # OBD_FAIL_LLITE_XATTR_ENOMEM
13144         $LCTL set_param fail_loc=0x1405
13145         # output of the form: attr 2 4 44 3 fc13 x86_64
13146         V=($(IFS=".-" rpm -q attr))
13147         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13148               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13149                 # attr pre-2.4.44-7 had a bug with rc
13150                 # LU-3703 - SLES 11 and FC13 clients have older attr
13151                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13152                         error "getfattr should have failed with ENOMEM"
13153         else
13154                 skip "LU-3703: attr version $(getfattr --version) too old"
13155         fi
13156         $LCTL set_param fail_loc=0x0
13157         rm -rf $DIR/$tdir
13158
13159         restore_lustre_params < $p
13160         rm -f $p
13161 }
13162 run_test 234 "xattr cache should not crash on ENOMEM"
13163
13164 test_235() {
13165         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13166                 skip "Need MDS version at least 2.4.52" && return
13167         flock_deadlock $DIR/$tfile
13168         local RC=$?
13169         case $RC in
13170                 0)
13171                 ;;
13172                 124) error "process hangs on a deadlock"
13173                 ;;
13174                 *) error "error executing flock_deadlock $DIR/$tfile"
13175                 ;;
13176         esac
13177 }
13178 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13179
13180 #LU-2935
13181 test_236() {
13182         check_swap_layouts_support && return 0
13183         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
13184
13185         local ref1=/etc/passwd
13186         local ref2=/etc/group
13187         local file1=$DIR/$tdir/f1
13188         local file2=$DIR/$tdir/f2
13189
13190         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13191         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13192         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13193         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13194         local fd=$(free_fd)
13195         local cmd="exec $fd<>$file2"
13196         eval $cmd
13197         rm $file2
13198         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13199                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13200         cmd="exec $fd>&-"
13201         eval $cmd
13202         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13203
13204         #cleanup
13205         rm -rf $DIR/$tdir
13206 }
13207 run_test 236 "Layout swap on open unlinked file"
13208
13209 # test to verify file handle related system calls
13210 # (name_to_handle_at/open_by_handle_at)
13211 # The new system calls are supported in glibc >= 2.14.
13212
13213 test_237() {
13214         echo "Test file_handle syscalls" > $DIR/$tfile ||
13215                 error "write failed"
13216         check_fhandle_syscalls $DIR/$tfile ||
13217                 error "check_fhandle_syscalls failed"
13218 }
13219 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13220
13221 # LU-4659 linkea consistency
13222 test_238() {
13223         local server_version=$(lustre_version_code $SINGLEMDS)
13224
13225         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13226                 [[ $server_version -gt $(version_code 2.5.1) &&
13227                    $server_version -lt $(version_code 2.5.50) ]] ||
13228                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13229
13230         touch $DIR/$tfile
13231         ln $DIR/$tfile $DIR/$tfile.lnk
13232         touch $DIR/$tfile.new
13233         mv $DIR/$tfile.new $DIR/$tfile
13234         local fid1=$($LFS path2fid $DIR/$tfile)
13235         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13236         local path1=$($LFS fid2path $FSNAME "$fid1")
13237         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13238         local path2=$($LFS fid2path $FSNAME "$fid2")
13239         [ $tfile.lnk == $path2 ] ||
13240                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13241         rm -f $DIR/$tfile*
13242 }
13243 run_test 238 "Verify linkea consistency"
13244
13245 test_239() {
13246         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13247                 skip "Need MDS version at least 2.5.60" && return
13248         local list=$(comma_list $(mdts_nodes))
13249
13250         mkdir -p $DIR/$tdir
13251         createmany -o $DIR/$tdir/f- 5000
13252         unlinkmany $DIR/$tdir/f- 5000
13253         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13254         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13255                         osc.*MDT*.sync_in_flight" | calc_sum)
13256         [ "$changes" -eq 0 ] || error "$changes not synced"
13257 }
13258 run_test 239 "osp_sync test"
13259
13260 test_239a() { #LU-5297
13261         touch $DIR/$tfile
13262         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13263         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13264         chgrp $RUNAS_GID $DIR/$tfile
13265         wait_delete_completed
13266 }
13267 run_test 239a "process invalid osp sync record correctly"
13268
13269 test_239b() { #LU-5297
13270         touch $DIR/$tfile1
13271         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13272         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13273         chgrp $RUNAS_GID $DIR/$tfile1
13274         wait_delete_completed
13275         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13276         touch $DIR/$tfile2
13277         chgrp $RUNAS_GID $DIR/$tfile2
13278         wait_delete_completed
13279 }
13280 run_test 239b "process osp sync record with ENOMEM error correctly"
13281
13282 test_240() {
13283         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13284
13285         mkdir -p $DIR/$tdir
13286
13287         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
13288                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
13289         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
13290                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
13291
13292         umount_client $MOUNT || error "umount failed"
13293         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
13294         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
13295         mount_client $MOUNT || error "failed to mount client"
13296
13297         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
13298         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
13299 }
13300 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
13301
13302 test_241_bio() {
13303         for LOOP in $(seq $1); do
13304                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
13305                 cancel_lru_locks osc
13306         done
13307 }
13308
13309 test_241_dio() {
13310         for LOOP in $(seq $1); do
13311                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
13312                                                 iflag=direct 2>/dev/null
13313         done
13314 }
13315
13316 test_241() {
13317         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13318         ls -la $DIR/$tfile
13319         cancel_lru_locks osc
13320         test_241_bio 1000 &
13321         PID=$!
13322         test_241_dio 1000
13323         wait $PID
13324 }
13325 run_test 241 "bio vs dio"
13326
13327 test_241b() {
13328         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13329         ls -la $DIR/$tfile
13330         test_241_dio 1000 &
13331         PID=$!
13332         test_241_dio 1000
13333         wait $PID
13334 }
13335 run_test 241b "dio vs dio"
13336
13337 test_242() {
13338         mkdir -p $DIR/$tdir
13339         touch $DIR/$tdir/$tfile
13340
13341         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
13342         do_facet mds1 lctl set_param fail_loc=0x105
13343         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
13344
13345         do_facet mds1 lctl set_param fail_loc=0
13346         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
13347 }
13348 run_test 242 "mdt_readpage failure should not cause directory unreadable"
13349
13350 test_243()
13351 {
13352         test_mkdir -p $DIR/$tdir
13353         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
13354 }
13355 run_test 243 "various group lock tests"
13356
13357 test_244()
13358 {
13359         test_mkdir -p $DIR/$tdir
13360         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
13361         sendfile_grouplock $DIR/$tdir/$tfile || \
13362                 error "sendfile+grouplock failed"
13363         rm -rf $DIR/$tdir
13364 }
13365 run_test 244 "sendfile with group lock tests"
13366
13367 test_245() {
13368         local flagname="multi_mod_rpcs"
13369         local connect_data_name="max_mod_rpcs"
13370         local out
13371
13372         # check if multiple modify RPCs flag is set
13373         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
13374                 grep "connect_flags:")
13375         echo "$out"
13376
13377         echo "$out" | grep -qw $flagname
13378         if [ $? -ne 0 ]; then
13379                 echo "connect flag $flagname is not set"
13380                 return
13381         fi
13382
13383         # check if multiple modify RPCs data is set
13384         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
13385         echo "$out"
13386
13387         echo "$out" | grep -qw $connect_data_name ||
13388                 error "import should have connect data $connect_data_name"
13389 }
13390 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
13391
13392 test_250() {
13393         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
13394          && skip "no 16TB file size limit on ZFS" && return
13395
13396         $SETSTRIPE -c 1 $DIR/$tfile
13397         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
13398         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
13399         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
13400         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
13401                 conv=notrunc,fsync && error "append succeeded"
13402         return 0
13403 }
13404 run_test 250 "Write above 16T limit"
13405
13406 test_251() {
13407         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
13408
13409         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
13410         #Skip once - writing the first stripe will succeed
13411         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13412         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
13413                 error "short write happened"
13414
13415         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13416         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
13417                 error "short read happened"
13418
13419         rm -f $DIR/$tfile
13420 }
13421 run_test 251 "Handling short read and write correctly"
13422
13423 test_252() {
13424         local tgt
13425         local dev
13426         local out
13427         local uuid
13428         local num
13429         local gen
13430
13431         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
13432              "$(facet_fstype mds1)" != "ldiskfs" ]; then
13433                 skip "can only run lr_reader on ldiskfs target"
13434                 return
13435         fi
13436
13437         # check lr_reader on OST0000
13438         tgt=ost1
13439         dev=$(facet_device $tgt)
13440         out=$(do_facet $tgt $LR_READER $dev)
13441         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13442         echo "$out"
13443         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
13444         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
13445                 error "Invalid uuid returned by $LR_READER on target $tgt"
13446         echo -e "uuid returned by $LR_READER is '$uuid'\n"
13447
13448         # check lr_reader -c on MDT0000
13449         tgt=mds1
13450         dev=$(facet_device $tgt)
13451         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
13452                 echo "$LR_READER does not support additional options"
13453                 return 0
13454         fi
13455         out=$(do_facet $tgt $LR_READER -c $dev)
13456         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13457         echo "$out"
13458         num=$(echo "$out" | grep -c "mdtlov")
13459         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
13460                 error "Invalid number of mdtlov clients returned by $LR_READER"
13461         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
13462
13463         # check lr_reader -cr on MDT0000
13464         out=$(do_facet $tgt $LR_READER -cr $dev)
13465         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13466         echo "$out"
13467         echo "$out" | grep -q "^reply_data:$" ||
13468                 error "$LR_READER should have returned 'reply_data' section"
13469         num=$(echo "$out" | grep -c "client_generation")
13470         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
13471 }
13472 run_test 252 "check lr_reader tool"
13473
13474
13475 cleanup_test_300() {
13476         trap 0
13477         umask $SAVE_UMASK
13478 }
13479 test_striped_dir() {
13480         local mdt_index=$1
13481         local stripe_count
13482         local stripe_index
13483
13484         mkdir -p $DIR/$tdir
13485
13486         SAVE_UMASK=$(umask)
13487         trap cleanup_test_300 RETURN EXIT
13488
13489         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
13490                                                 $DIR/$tdir/striped_dir ||
13491                 error "set striped dir error"
13492
13493         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
13494         [ "$mode" = "755" ] || error "expect 755 got $mode"
13495
13496         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
13497                 error "getdirstripe failed"
13498         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
13499         if [ "$stripe_count" != "2" ]; then
13500                 error "stripe_count is $stripe_count, expect 2"
13501         fi
13502
13503         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
13504         if [ "$stripe_index" != "$mdt_index" ]; then
13505                 error "stripe_index is $stripe_index, expect $mdt_index"
13506         fi
13507
13508         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13509                 error "nlink error after create striped dir"
13510
13511         mkdir $DIR/$tdir/striped_dir/a
13512         mkdir $DIR/$tdir/striped_dir/b
13513
13514         stat $DIR/$tdir/striped_dir/a ||
13515                 error "create dir under striped dir failed"
13516         stat $DIR/$tdir/striped_dir/b ||
13517                 error "create dir under striped dir failed"
13518
13519         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
13520                 error "nlink error after mkdir"
13521
13522         rmdir $DIR/$tdir/striped_dir/a
13523         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
13524                 error "nlink error after rmdir"
13525
13526         rmdir $DIR/$tdir/striped_dir/b
13527         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13528                 error "nlink error after rmdir"
13529
13530         chattr +i $DIR/$tdir/striped_dir
13531         createmany -o $DIR/$tdir/striped_dir/f 10 &&
13532                 error "immutable flags not working under striped dir!"
13533         chattr -i $DIR/$tdir/striped_dir
13534
13535         rmdir $DIR/$tdir/striped_dir ||
13536                 error "rmdir striped dir error"
13537
13538         cleanup_test_300
13539
13540         true
13541 }
13542
13543 test_300a() {
13544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13545         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13546
13547         test_striped_dir 0 || error "failed on striped dir on MDT0"
13548         test_striped_dir 1 || error "failed on striped dir on MDT0"
13549 }
13550 run_test 300a "basic striped dir sanity test"
13551
13552 test_300b() {
13553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13554         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13555         local i
13556         local mtime1
13557         local mtime2
13558         local mtime3
13559
13560         test_mkdir $DIR/$tdir || error "mkdir fail"
13561         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13562                 error "set striped dir error"
13563         for ((i=0; i<10; i++)); do
13564                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
13565                 sleep 1
13566                 touch $DIR/$tdir/striped_dir/file_$i ||
13567                                         error "touch error $i"
13568                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13569                 [ $mtime1 -eq $mtime2 ] &&
13570                         error "mtime not change after create"
13571                 sleep 1
13572                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13573                                         error "unlink error $i"
13574                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13575                 [ $mtime2 -eq $mtime3 ] &&
13576                         error "mtime did not change after unlink"
13577         done
13578         true
13579 }
13580 run_test 300b "check ctime/mtime for striped dir"
13581
13582 test_300c() {
13583         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13584         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13585         local file_count
13586
13587         mkdir -p $DIR/$tdir
13588         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13589                 error "set striped dir error"
13590
13591         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13592                 error "chown striped dir failed"
13593
13594         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13595                 error "create 5k files failed"
13596
13597         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13598
13599         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13600
13601         rm -rf $DIR/$tdir
13602 }
13603 run_test 300c "chown && check ls under striped directory"
13604
13605 test_300d() {
13606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13607         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13608         local stripe_count
13609         local file
13610
13611         mkdir -p $DIR/$tdir
13612         $SETSTRIPE -c 2 $DIR/$tdir
13613
13614         #local striped directory
13615         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13616                 error "set striped dir error"
13617         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13618                 error "create 10 files failed"
13619
13620         #remote striped directory
13621         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13622                 error "set striped dir error"
13623         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13624                 error "create 10 files failed"
13625
13626         for file in $(find $DIR/$tdir); do
13627                 stripe_count=$($GETSTRIPE -c $file)
13628                 [ $stripe_count -eq 2 ] ||
13629                         error "wrong stripe $stripe_count for $file"
13630         done
13631
13632         rm -rf $DIR/$tdir
13633 }
13634 run_test 300d "check default stripe under striped directory"
13635
13636 test_300e() {
13637         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13638                 skip "Need MDS version at least 2.7.55" && return
13639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13640         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13641         local stripe_count
13642         local file
13643
13644         mkdir -p $DIR/$tdir
13645
13646         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13647                 error "set striped dir error"
13648
13649         touch $DIR/$tdir/striped_dir/a
13650         touch $DIR/$tdir/striped_dir/b
13651         touch $DIR/$tdir/striped_dir/c
13652
13653         mkdir $DIR/$tdir/striped_dir/dir_a
13654         mkdir $DIR/$tdir/striped_dir/dir_b
13655         mkdir $DIR/$tdir/striped_dir/dir_c
13656
13657         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13658                 error "set striped dir under striped dir error"
13659
13660         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13661                 error "set striped dir under striped dir error"
13662
13663         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13664                 error "set striped dir under striped dir error"
13665
13666         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
13667                 error "rename dir under striped dir fails"
13668
13669         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
13670                 error "rename dir under different stripes fails"
13671
13672         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13673                 error "rename file under striped dir should succeed"
13674
13675         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
13676                 error "rename dir under striped dir should succeed"
13677
13678         rm -rf $DIR/$tdir
13679 }
13680 run_test 300e "check rename under striped directory"
13681
13682 test_300f() {
13683         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13684                 skip "Need MDS version at least 2.7.55" && return
13685         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13686         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13687         local stripe_count
13688         local file
13689
13690         rm -rf $DIR/$tdir
13691         mkdir -p $DIR/$tdir
13692
13693         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13694                 error "set striped dir error"
13695
13696         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13697                 error "set striped dir error"
13698
13699         touch $DIR/$tdir/striped_dir/a
13700         mkdir $DIR/$tdir/striped_dir/dir_a
13701         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13702                 error "create striped dir under striped dir fails"
13703
13704         touch $DIR/$tdir/striped_dir1/b
13705         mkdir $DIR/$tdir/striped_dir1/dir_b
13706         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13707                 error "create striped dir under striped dir fails"
13708
13709         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
13710                 error "rename dir under different striped dir should fail"
13711
13712         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
13713                 error "rename striped dir under diff striped dir should fail"
13714
13715         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13716                 error "rename file under diff striped dirs fails"
13717
13718         rm -rf $DIR/$tdir
13719 }
13720 run_test 300f "check rename cross striped directory"
13721
13722 test_300_check_default_striped_dir()
13723 {
13724         local dirname=$1
13725         local default_count=$2
13726         local default_index=$3
13727         local stripe_count
13728         local stripe_index
13729         local dir_stripe_index
13730         local dir
13731
13732         echo "checking $dirname $default_count $default_index"
13733         $LFS setdirstripe -D -c $default_count -i $default_index \
13734                                 -t all_char $DIR/$tdir/$dirname ||
13735                 error "set default stripe on striped dir error"
13736         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13737         [ $stripe_count -eq $default_count ] ||
13738                 error "expect $default_count get $stripe_count for $dirname"
13739
13740         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13741         [ $stripe_index -eq $default_index ] ||
13742                 error "expect $default_index get $stripe_index for $dirname"
13743
13744         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13745                                                 error "create dirs failed"
13746
13747         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
13748         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
13749         for dir in $(find $DIR/$tdir/$dirname/*); do
13750                 stripe_count=$($LFS getdirstripe -c $dir)
13751                 [ $stripe_count -eq $default_count ] ||
13752                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
13753                 error "stripe count $default_count != $stripe_count for $dir"
13754
13755                 stripe_index=$($LFS getdirstripe -i $dir)
13756                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13757                         error "$stripe_index != $default_index for $dir"
13758
13759                 #check default stripe
13760                 stripe_count=$($LFS getdirstripe -D -c $dir)
13761                 [ $stripe_count -eq $default_count ] ||
13762                 error "default count $default_count != $stripe_count for $dir"
13763
13764                 stripe_index=$($LFS getdirstripe -D -i $dir)
13765                 [ $stripe_index -eq $default_index ] ||
13766                 error "default index $default_index != $stripe_index for $dir"
13767         done
13768         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13769 }
13770
13771 test_300g() {
13772         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13773                 skip "Need MDS version at least 2.7.55" && return
13774         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13775         local dir
13776         local stripe_count
13777         local stripe_index
13778
13779         mkdir $DIR/$tdir
13780         mkdir $DIR/$tdir/normal_dir
13781
13782         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13783         test_300_check_default_striped_dir normal_dir 1 0
13784         test_300_check_default_striped_dir normal_dir 2 1
13785         test_300_check_default_striped_dir normal_dir 2 -1
13786
13787         #delete default stripe information
13788         echo "delete default stripeEA"
13789         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13790                 error "set default stripe on striped dir error"
13791
13792         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13793         for dir in $(find $DIR/$tdir/normal_dir/*); do
13794                 stripe_count=$($LFS getdirstripe -c $dir)
13795                 [ $stripe_count -eq 0 ] ||
13796                         error "expect 1 get $stripe_count for $dir"
13797                 stripe_index=$($LFS getdirstripe -i $dir)
13798                 [ $stripe_index -eq 0 ] ||
13799                         error "expect 0 get $stripe_index for $dir"
13800         done
13801 }
13802 run_test 300g "check default striped directory for normal directory"
13803
13804 test_300h() {
13805         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13806                 skip "Need MDS version at least 2.7.55" && return
13807         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13808         local dir
13809         local stripe_count
13810
13811         mkdir $DIR/$tdir
13812         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13813                                         $DIR/$tdir/striped_dir ||
13814                 error "set striped dir error"
13815
13816         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13817         test_300_check_default_striped_dir striped_dir 1 0
13818         test_300_check_default_striped_dir striped_dir 2 1
13819         test_300_check_default_striped_dir striped_dir 2 -1
13820
13821         #delete default stripe information
13822         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13823                 error "set default stripe on striped dir error"
13824
13825         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13826         for dir in $(find $DIR/$tdir/striped_dir/*); do
13827                 stripe_count=$($LFS getdirstripe -c $dir)
13828                 [ $stripe_count -eq 0 ] ||
13829                         error "expect 1 get $stripe_count for $dir"
13830         done
13831 }
13832 run_test 300h "check default striped directory for striped directory"
13833
13834 test_300i() {
13835         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13836                 skip "Need MDS version at least 2.7.55" && return
13837         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13838         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13839         local stripe_count
13840         local file
13841
13842         mkdir $DIR/$tdir
13843
13844         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13845                 error "set striped dir error"
13846
13847         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13848                 error "create files under striped dir failed"
13849
13850         # unfortunately, we need to umount to clear dir layout cache for now
13851         # once we fully implement dir layout, we can drop this
13852         umount_client $MOUNT || error "umount failed"
13853         mount_client $MOUNT || error "mount failed"
13854
13855         #set the stripe to be unknown hash type
13856         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13857         $LCTL set_param fail_loc=0x1901
13858         for ((i = 0; i < 10; i++)); do
13859                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13860                         error "stat f-$i failed"
13861                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13862         done
13863
13864         touch $DIR/$tdir/striped_dir/f0 &&
13865                 error "create under striped dir with unknown hash should fail"
13866
13867         $LCTL set_param fail_loc=0
13868
13869         umount_client $MOUNT || error "umount failed"
13870         mount_client $MOUNT || error "mount failed"
13871
13872         return 0
13873 }
13874 run_test 300i "client handle unknown hash type striped directory"
13875
13876 test_300j() {
13877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13878         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13879                 skip "Need MDS version at least 2.7.55" && return
13880         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13881         local stripe_count
13882         local file
13883
13884         mkdir $DIR/$tdir
13885
13886         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
13887         $LCTL set_param fail_loc=0x1702
13888         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13889                 error "set striped dir error"
13890
13891         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13892                 error "create files under striped dir failed"
13893
13894         $LCTL set_param fail_loc=0
13895
13896         rm -rf $DIR/$tdir || error "unlink striped dir fails"
13897
13898         return 0
13899 }
13900 run_test 300j "test large update record"
13901
13902 test_300k() {
13903         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13904                 skip "Need MDS version at least 2.7.55" && return
13905         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13906         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13907         local stripe_count
13908         local file
13909
13910         mkdir $DIR/$tdir
13911
13912         #define OBD_FAIL_LARGE_STRIPE   0x1703
13913         $LCTL set_param fail_loc=0x1703
13914         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
13915                 error "set striped dir error"
13916         $LCTL set_param fail_loc=0
13917
13918         $LFS getdirstripe $DIR/$tdir/striped_dir ||
13919                 error "getstripeddir fails"
13920         rm -rf $DIR/$tdir/striped_dir ||
13921                 error "unlink striped dir fails"
13922
13923         return 0
13924 }
13925 run_test 300k "test large striped directory"
13926
13927 test_300l() {
13928         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13929                 skip "Need MDS version at least 2.7.55" && return
13930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13931         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13932         local stripe_index
13933
13934         test_mkdir -p $DIR/$tdir/striped_dir
13935         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
13936                         error "chown $RUNAS_ID failed"
13937         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
13938                 error "set default striped dir failed"
13939
13940         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
13941         $LCTL set_param fail_loc=0x80000158
13942         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
13943
13944         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
13945         [ $stripe_index -eq 1 ] ||
13946                 error "expect 1 get $stripe_index for $dir"
13947 }
13948 run_test 300l "non-root user to create dir under striped dir with stale layout"
13949
13950 test_300m() {
13951         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13952                 skip "Need MDS version at least 2.7.55" && return
13953         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13954         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
13955
13956         mkdir -p $DIR/$tdir/striped_dir
13957         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
13958                 error "set default stripes dir error"
13959
13960         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
13961
13962         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
13963         [ $stripe_count -eq 0 ] ||
13964                         error "expect 0 get $stripe_count for a"
13965
13966         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
13967                 error "set default stripes dir error"
13968
13969         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
13970
13971         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
13972         [ $stripe_count -eq 0 ] ||
13973                         error "expect 0 get $stripe_count for b"
13974
13975         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
13976                 error "set default stripes dir error"
13977
13978         mkdir $DIR/$tdir/striped_dir/c &&
13979                 error "default stripe_index is invalid, mkdir c should fails"
13980
13981         rm -rf $DIR/$tdir || error "rmdir fails"
13982 }
13983 run_test 300m "setstriped directory on single MDT FS"
13984
13985 cleanup_300n() {
13986         local list=$(comma_list $(mdts_nodes))
13987
13988         trap 0
13989         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13990 }
13991
13992 test_300n() {
13993         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13994                 skip "Need MDS version at least 2.7.55" && return
13995         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13996         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13997         local stripe_index
13998         local list=$(comma_list $(mdts_nodes))
13999
14000         trap cleanup_300n RETURN EXIT
14001         mkdir -p $DIR/$tdir
14002         chmod 777 $DIR/$tdir
14003         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
14004                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
14005                 error "create striped dir succeeds with gid=0"
14006
14007         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
14008         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
14009                 error "create striped dir fails with gid=-1"
14010
14011         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
14012         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
14013                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
14014                 error "set default striped dir succeeds with gid=0"
14015
14016
14017         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
14018         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
14019                 error "set default striped dir fails with gid=-1"
14020
14021
14022         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
14023         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
14024                                         error "create test_dir fails"
14025         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
14026                                         error "create test_dir1 fails"
14027         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
14028                                         error "create test_dir2 fails"
14029         cleanup_300n
14030 }
14031 run_test 300n "non-root user to create dir under striped dir with default EA"
14032
14033 test_300o() {
14034         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14035                 skip "Need MDS version at least 2.7.55" && return
14036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14037         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14038         local numfree1
14039         local numfree2
14040
14041         mkdir -p $DIR/$tdir
14042
14043         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
14044         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
14045         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
14046                 skip "not enough free inodes $numfree1 $numfree2"
14047                 return
14048         fi
14049
14050         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
14051         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
14052         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
14053                 skip "not enough free space $numfree1 $numfree2"
14054                 return
14055         fi
14056
14057         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
14058                 error "setdirstripe fails"
14059
14060         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
14061                 error "create dirs fails"
14062
14063         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
14064         ls $DIR/$tdir/striped_dir > /dev/null ||
14065                 error "ls striped dir fails"
14066         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
14067                 error "unlink big striped dir fails"
14068 }
14069 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
14070
14071 prepare_remote_file() {
14072         mkdir $DIR/$tdir/src_dir ||
14073                 error "create remote source failed"
14074
14075         cp /etc/hosts $DIR/$tdir/src_dir/a || error
14076         touch $DIR/$tdir/src_dir/a
14077
14078         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
14079                 error "create remote target dir failed"
14080
14081         touch $DIR/$tdir/tgt_dir/b
14082
14083         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
14084                 error "rename dir cross MDT failed!"
14085
14086         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
14087                 error "src_child still exists after rename"
14088
14089         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
14090                 error "missing file(a) after rename"
14091
14092         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
14093                 error "diff after rename"
14094 }
14095
14096 test_310a() {
14097         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14099         local remote_file=$DIR/$tdir/tgt_dir/b
14100
14101         mkdir -p $DIR/$tdir
14102
14103         prepare_remote_file || error "prepare remote file failed"
14104
14105         #open-unlink file
14106         $OPENUNLINK $remote_file $remote_file || error
14107         $CHECKSTAT -a $remote_file || error
14108 }
14109 run_test 310a "open unlink remote file"
14110
14111 test_310b() {
14112         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14114         local remote_file=$DIR/$tdir/tgt_dir/b
14115
14116         mkdir -p $DIR/$tdir
14117
14118         prepare_remote_file || error "prepare remote file failed"
14119
14120         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14121         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
14122         $CHECKSTAT -t file $remote_file || error "check file failed"
14123 }
14124 run_test 310b "unlink remote file with multiple links while open"
14125
14126 test_310c() {
14127         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
14128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14129         local remote_file=$DIR/$tdir/tgt_dir/b
14130
14131         mkdir -p $DIR/$tdir
14132
14133         prepare_remote_file || error "prepare remote file failed"
14134
14135         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14136         multiop_bg_pause $remote_file O_uc ||
14137                         error "mulitop failed for remote file"
14138         MULTIPID=$!
14139         $MULTIOP $DIR/$tfile Ouc
14140         kill -USR1 $MULTIPID
14141         wait $MULTIPID
14142 }
14143 run_test 310c "open-unlink remote file with multiple links"
14144
14145 test_400a() { # LU-1606, was conf-sanity test_74
14146         local extra_flags=''
14147         local out=$TMP/$tfile
14148         local prefix=/usr/include/lustre
14149         local prog
14150
14151         if ! which $CC > /dev/null 2>&1; then
14152                 skip_env "$CC is not installed"
14153                 return 0
14154         fi
14155
14156         if ! [[ -d $prefix ]]; then
14157                 # Assume we're running in tree and fixup the include path.
14158                 extra_flags+=" -I$LUSTRE/../libcfs/include"
14159                 extra_flags+=" -I$LUSTRE/include"
14160                 extra_flags+=" -L$LUSTRE/utils"
14161         fi
14162
14163         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
14164                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
14165                         error "client api broken"
14166         done
14167 }
14168 run_test 400a "Lustre client api program can compile and link"
14169
14170 test_400b() { # LU-1606, LU-5011
14171         local header
14172         local out=$TMP/$tfile
14173         local prefix=/usr/include/lustre
14174
14175         # We use a hard coded prefix so that this test will not fail
14176         # when run in tree. There are headers in lustre/include/lustre/
14177         # that are not packaged (like lustre_idl.h) and have more
14178         # complicated include dependencies (like config.h and lnet/types.h).
14179         # Since this test about correct packaging we just skip them when
14180         # they don't exist (see below) rather than try to fixup cppflags.
14181
14182         if ! which $CC > /dev/null 2>&1; then
14183                 skip_env "$CC is not installed"
14184                 return 0
14185         fi
14186
14187         for header in $prefix/*.h; do
14188                 if ! [[ -f "$header" ]]; then
14189                         continue
14190                 fi
14191
14192                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
14193                         continue # liblustreapi.h is deprecated.
14194                 fi
14195
14196                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
14197                         error "cannot compile '$header'"
14198         done
14199 }
14200 run_test 400b "packaged headers can be compiled"
14201
14202 #
14203 # tests that do cleanup/setup should be run at the end
14204 #
14205
14206 test_900() {
14207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14208         local ls
14209         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
14210         $LCTL set_param fail_loc=0x903
14211
14212         cancel_lru_locks MGC
14213
14214         FAIL_ON_ERROR=true cleanup
14215         FAIL_ON_ERROR=true setup
14216 }
14217 run_test 900 "umount should not race with any mgc requeue thread"
14218
14219 complete $SECONDS
14220 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
14221 check_and_cleanup_lustre
14222 if [ "$I_MOUNTED" != "yes" ]; then
14223         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
14224 fi
14225 exit_status