Whamcloud - gitweb
LU-9166 mdt: Add trailing newlines to proc files
[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 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   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 LU-8411
17 ALWAYS_EXCEPT="                 76      407 $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 #                                  5              12          (min)"
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24D 27m 64b 68 71 115 300o"
80
81 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
82         # bug number for skipped test: LU-4536 LU-1957
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  65ic    180"
84         #                                               13    (min)"
85         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
86 fi
87
88 FAIL_ON_ERROR=false
89
90 cleanup() {
91         echo -n "cln.."
92         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
93         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
94 }
95 setup() {
96         echo -n "mnt.."
97         load_modules
98         setupall || exit 10
99         echo "done"
100 }
101
102 check_swap_layouts_support()
103 {
104         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
105                 { skip "Does not support layout lock."; return 0; }
106         return 1
107 }
108
109 check_and_setup_lustre
110 DIR=${DIR:-$MOUNT}
111 assert_DIR
112
113 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
114         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
115 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
116
117 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
118 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
119 rm -rf $DIR/[Rdfs][0-9]*
120
121 # $RUNAS_ID may get set incorrectly somewhere else
122 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
123
124 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
125
126 build_test_filter
127
128 if [ "${ONLY}" = "MOUNT" ] ; then
129         echo "Lustre is up, please go on"
130         exit
131 fi
132
133 echo "preparing for tests involving mounts"
134 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
135 touch $EXT2_DEV
136 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
137 echo # add a newline after mke2fs.
138
139 umask 077
140
141 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
142 lctl set_param debug=-1 2> /dev/null || true
143 test_0a() {
144         touch $DIR/$tfile
145         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
146         rm $DIR/$tfile
147         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
148 }
149 run_test 0a "touch; rm ====================="
150
151 test_0b() {
152         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
153         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
154 }
155 run_test 0b "chmod 0755 $DIR ============================="
156
157 test_0c() {
158         $LCTL get_param mdc.*.import | grep "state: FULL" ||
159                 error "import not FULL"
160         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
161                 error "bad target"
162 }
163 run_test 0c "check import proc ============================="
164
165 test_1() {
166         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
167         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
168         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
169         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
170         rmdir $DIR/$tdir/d2
171         rmdir $DIR/$tdir
172         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
173 }
174 run_test 1 "mkdir; remkdir; rmdir =============================="
175
176 test_2() {
177         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
178         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
179         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
180         rm -r $DIR/$tdir
181         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
182 }
183 run_test 2 "mkdir; touch; rmdir; check file ===================="
184
185 test_3() {
186         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
187         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
188         touch $DIR/$tdir/$tfile
189         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
190         rm -r $DIR/$tdir
191         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
192 }
193 run_test 3 "mkdir; touch; rmdir; check dir ====================="
194
195 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
196 test_4() {
197         local MDTIDX=1
198
199         test_mkdir $DIR/$tdir ||
200                 error "Create remote directory failed"
201
202         touch $DIR/$tdir/$tfile ||
203                 error "Create file under remote directory failed"
204
205         rmdir $DIR/$tdir &&
206                 error "Expect error removing in-use dir $DIR/$tdir"
207
208         test -d $DIR/$tdir || error "Remote directory disappeared"
209
210         rm -rf $DIR/$tdir || error "remove remote dir error"
211 }
212 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
213
214 test_5() {
215         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
216         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
217         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
218         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
219         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
220 }
221 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
222
223 test_6a() {
224         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
225         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
226         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
227                 error "$tfile does not have perm 0666 or UID $UID"
228         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile should be 0666 and owned by UID $UID"
231 }
232 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
233
234 test_6c() {
235         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
236         touch $DIR/$tfile
237         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
238         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
239                 error "$tfile should be owned by UID $RUNAS_ID"
240         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243 }
244 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
245
246 test_6e() {
247         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
248         touch $DIR/$tfile
249         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
250         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
251                 error "$tfile should be owned by GID $UID"
252         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
255 }
256 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
257
258 test_6g() {
259         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
260         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
261         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
262         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
263         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
264         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
265         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
266                 error "$tdir/d/subdir should be GID $RUNAS_GID"
267         if [[ $MDSCOUNT -gt 1 ]]; then
268                 # check remote dir sgid inherite
269                 $LFS mkdir -i 0 $DIR/$tdir.local ||
270                         error "mkdir $tdir.local failed"
271                 chmod g+s $DIR/$tdir.local ||
272                         error "chmod $tdir.local failed"
273                 chgrp $RUNAS_GID $DIR/$tdir.local ||
274                         error "chgrp $tdir.local failed"
275                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
276                         error "mkdir $tdir.remote failed"
277                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
278                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
279                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
280                         error "$tdir.remote should be mode 02755"
281         fi
282 }
283 run_test 6g "Is new dir in sgid dir inheriting group?"
284
285 test_6h() { # bug 7331
286         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
287         touch $DIR/$tfile || error "touch failed"
288         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
289         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
290                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
291         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
292                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
293 }
294 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
295
296 test_7a() {
297         test_mkdir $DIR/$tdir
298         $MCREATE $DIR/$tdir/$tfile
299         chmod 0666 $DIR/$tdir/$tfile
300         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
301                 error "$tdir/$tfile should be mode 0666"
302 }
303 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
304
305 test_7b() {
306         if [ ! -d $DIR/$tdir ]; then
307                 test_mkdir $DIR/$tdir
308         fi
309         $MCREATE $DIR/$tdir/$tfile
310         echo -n foo > $DIR/$tdir/$tfile
311         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
312         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
313 }
314 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
315
316 test_8() {
317         test_mkdir $DIR/$tdir
318         touch $DIR/$tdir/$tfile
319         chmod 0666 $DIR/$tdir/$tfile
320         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
321                 error "$tfile mode not 0666"
322 }
323 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
324
325 test_9() {
326         test_mkdir $DIR/$tdir
327         test_mkdir $DIR/$tdir/d2
328         test_mkdir $DIR/$tdir/d2/d3
329         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
330 }
331 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
332
333 test_10() {
334         test_mkdir $DIR/$tdir
335         test_mkdir $DIR/$tdir/d2
336         touch $DIR/$tdir/d2/$tfile
337         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
338                 error "$tdir/d2/$tfile not a file"
339 }
340 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
341
342 test_11() {
343         test_mkdir $DIR/$tdir
344         test_mkdir $DIR/$tdir/d2
345         chmod 0666 $DIR/$tdir/d2
346         chmod 0705 $DIR/$tdir/d2
347         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
348                 error "$tdir/d2 mode not 0705"
349 }
350 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
351
352 test_12() {
353         test_mkdir $DIR/$tdir
354         touch $DIR/$tdir/$tfile
355         chmod 0666 $DIR/$tdir/$tfile
356         chmod 0654 $DIR/$tdir/$tfile
357         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
358                 error "$tdir/d2 mode not 0654"
359 }
360 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
361
362 test_13() {
363         test_mkdir $DIR/$tdir
364         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
365         >  $DIR/$tdir/$tfile
366         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
367                 error "$tdir/$tfile size not 0 after truncate"
368 }
369 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
370
371 test_14() {
372         test_mkdir $DIR/$tdir
373         touch $DIR/$tdir/$tfile
374         rm $DIR/$tdir/$tfile
375         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
376 }
377 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
378
379 test_15() {
380         test_mkdir $DIR/$tdir
381         touch $DIR/$tdir/$tfile
382         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
383         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
384                 error "$tdir/${tfile_2} not a file after rename"
385         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
386 }
387 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
388
389 test_16() {
390         test_mkdir $DIR/$tdir
391         touch $DIR/$tdir/$tfile
392         rm -rf $DIR/$tdir/$tfile
393         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
394 }
395 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
396
397 test_17a() {
398         test_mkdir -p $DIR/$tdir
399         touch $DIR/$tdir/$tfile
400         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
401         ls -l $DIR/$tdir
402         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
403                 error "$tdir/l-exist not a symlink"
404         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
405                 error "$tdir/l-exist not referencing a file"
406         rm -f $DIR/$tdir/l-exist
407         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
408 }
409 run_test 17a "symlinks: create, remove (real) =================="
410
411 test_17b() {
412         test_mkdir -p $DIR/$tdir
413         ln -s no-such-file $DIR/$tdir/l-dangle
414         ls -l $DIR/$tdir
415         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
416                 error "$tdir/l-dangle not referencing no-such-file"
417         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
418                 error "$tdir/l-dangle not referencing non-existent file"
419         rm -f $DIR/$tdir/l-dangle
420         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
421 }
422 run_test 17b "symlinks: create, remove (dangling) =============="
423
424 test_17c() { # bug 3440 - don't save failed open RPC for replay
425         test_mkdir -p $DIR/$tdir
426         ln -s foo $DIR/$tdir/$tfile
427         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
428 }
429 run_test 17c "symlinks: open dangling (should return error) ===="
430
431 test_17d() {
432         test_mkdir -p $DIR/$tdir
433         ln -s foo $DIR/$tdir/$tfile
434         touch $DIR/$tdir/$tfile || error "creating to new symlink"
435 }
436 run_test 17d "symlinks: create dangling ========================"
437
438 test_17e() {
439         test_mkdir -p $DIR/$tdir
440         local foo=$DIR/$tdir/$tfile
441         ln -s $foo $foo || error "create symlink failed"
442         ls -l $foo || error "ls -l failed"
443         ls $foo && error "ls not failed" || true
444 }
445 run_test 17e "symlinks: create recursive symlink (should return error) ===="
446
447 test_17f() {
448         test_mkdir -p $DIR/$tdir
449         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
454         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
455         ls -l  $DIR/$tdir
456 }
457 run_test 17f "symlinks: long and very long symlink name ========================"
458
459 # str_repeat(S, N) generate a string that is string S repeated N times
460 str_repeat() {
461         local s=$1
462         local n=$2
463         local ret=''
464         while [ $((n -= 1)) -ge 0 ]; do
465                 ret=$ret$s
466         done
467         echo $ret
468 }
469
470 # Long symlinks and LU-2241
471 test_17g() {
472         test_mkdir -p $DIR/$tdir
473         local TESTS="59 60 61 4094 4095"
474
475         # Fix for inode size boundary in 2.1.4
476         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
477                 TESTS="4094 4095"
478
479         # Patch not applied to 2.2 or 2.3 branches
480         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
481         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
482                 TESTS="4094 4095"
483
484         # skip long symlink name for rhel6.5.
485         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
486         grep -q '6.5' /etc/redhat-release &>/dev/null &&
487                 TESTS="59 60 61 4062 4063"
488
489         for i in $TESTS; do
490                 local SYMNAME=$(str_repeat 'x' $i)
491                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
492                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
493         done
494 }
495 run_test 17g "symlinks: really long symlink name and inode boundaries"
496
497 test_17h() { #bug 17378
498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
500         local mdt_idx
501         test_mkdir -p $DIR/$tdir
502         if [[ $MDSCOUNT -gt 1 ]]; then
503                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
504         else
505                 mdt_idx=0
506         fi
507         $SETSTRIPE -c -1 $DIR/$tdir
508 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
509         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
510         touch $DIR/$tdir/$tfile || true
511 }
512 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
513
514 test_17i() { #bug 20018
515         remote_mds_nodsh && skip "remote MDS with nodsh" && return
516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
517         test_mkdir -c1 $DIR/$tdir
518         local foo=$DIR/$tdir/$tfile
519         local mdt_idx
520         if [[ $MDSCOUNT -gt 1 ]]; then
521                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
522         else
523                 mdt_idx=0
524         fi
525         ln -s $foo $foo || error "create symlink failed"
526 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
527         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
528         ls -l $foo && error "error not detected"
529         return 0
530 }
531 run_test 17i "don't panic on short symlink"
532
533 test_17k() { #bug 22301
534         [[ -z "$(which rsync 2>/dev/null)" ]] &&
535                 skip "no rsync command" && return 0
536         rsync --help | grep -q xattr ||
537                 skip_env "$(rsync --version | head -n1) does not support xattrs"
538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
539         test_mkdir -p $DIR/$tdir
540         test_mkdir -p $DIR/$tdir.new
541         touch $DIR/$tdir/$tfile
542         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
543         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
544                 error "rsync failed with xattrs enabled"
545 }
546 run_test 17k "symlinks: rsync with xattrs enabled ========================="
547
548 test_17l() { # LU-279
549         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
550                 skip "no getfattr command" && return 0
551         test_mkdir -p $DIR/$tdir
552         touch $DIR/$tdir/$tfile
553         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
554         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
555                 # -h to not follow symlinks. -m '' to list all the xattrs.
556                 # grep to remove first line: '# file: $path'.
557                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
558                 do
559                         lgetxattr_size_check $path $xattr ||
560                                 error "lgetxattr_size_check $path $xattr failed"
561                 done
562         done
563 }
564 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
565
566 # LU-1540
567 test_17m() {
568         local short_sym="0123456789"
569         local WDIR=$DIR/${tdir}m
570         local i
571
572         remote_mds_nodsh && skip "remote MDS with nodsh" && return
573         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
574         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
575                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
576
577         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
578                 skip "only for ldiskfs MDT" && return 0
579
580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
581
582         test_mkdir -p $WDIR
583         long_sym=$short_sym
584         # create a long symlink file
585         for ((i = 0; i < 4; ++i)); do
586                 long_sym=${long_sym}${long_sym}
587         done
588
589         echo "create 512 short and long symlink files under $WDIR"
590         for ((i = 0; i < 256; ++i)); do
591                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
592                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
593         done
594
595         echo "erase them"
596         rm -f $WDIR/*
597         sync
598         wait_delete_completed
599
600         echo "recreate the 512 symlink files with a shorter string"
601         for ((i = 0; i < 512; ++i)); do
602                 # rewrite the symlink file with a shorter string
603                 ln -sf ${long_sym} $WDIR/long-$i || error "long_sym failed"
604                 ln -sf ${short_sym} $WDIR/short-$i || error "short_sym failed"
605         done
606
607         local mds_index=$(($($LFS getstripe -M $WDIR) + 1))
608         local devname=$(mdsdevname $mds_index)
609
610         echo "stop and checking mds${mds_index}:"
611         # e2fsck should not return error
612         stop mds${mds_index}
613         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
614         rc=$?
615
616         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
617         df $MOUNT > /dev/null 2>&1
618         [ $rc -eq 0 ] ||
619                 error "e2fsck detected error for short/long symlink: rc=$rc"
620 }
621 run_test 17m "run e2fsck against MDT which contains short/long symlink"
622
623 check_fs_consistency_17n() {
624         local mdt_index
625         local rc=0
626
627         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
628         # so it only check MDT1/MDT2 instead of all of MDTs.
629         for mdt_index in 1 2; do
630                 local devname=$(mdsdevname $mdt_index)
631                 # e2fsck should not return error
632                 stop mds${mdt_index}
633                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
634                         rc=$((rc + $?))
635
636                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
637                         error "mount mds$mdt_index failed"
638                 df $MOUNT > /dev/null 2>&1
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         local size
1036         size=$(getconf PAGE_SIZE 2>/dev/null)
1037         echo -n ${size:-4096}
1038 }
1039
1040 simple_cleanup_common() {
1041         trap 0
1042         rm -rf $DIR/$tdir
1043         wait_delete_completed
1044 }
1045
1046 max_pages_per_rpc() {
1047         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1048 }
1049
1050 test_24v() {
1051         local NRFILES=100000
1052         local FREE_INODES=$(mdt_free_inodes 0)
1053         [[ $FREE_INODES -lt $NRFILES ]] &&
1054                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1055                 return
1056
1057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1058         trap simple_cleanup_common EXIT
1059
1060         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1061         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1062
1063         test_mkdir -p $DIR/$tdir
1064         createmany -m $DIR/$tdir/$tfile $NRFILES
1065
1066         cancel_lru_locks mdc
1067         lctl set_param mdc.*.stats clear
1068
1069         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1070
1071         # LU-5 large readdir
1072         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1073         #               8 bytes for name(filename is mostly 5 in this test) +
1074         #               8 bytes for luda_type
1075         # take into account of overhead in lu_dirpage header and end mark in
1076         # each page, plus one in RPC_NUM calculation.
1077         DIRENT_SIZE=48
1078         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1079         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1080         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1081                                 awk '/^mds_readpage/ {print $2}')
1082         [[ $mds_readpage -gt $RPC_NUM ]] &&
1083                 error "large readdir doesn't take effect"
1084
1085         simple_cleanup_common
1086 }
1087 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1088
1089 test_24w() { # bug21506
1090         SZ1=234852
1091         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1092         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1093         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1094         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1095         [[ "$SZ1" -eq "$SZ2" ]] ||
1096                 error "Error reading at the end of the file $tfile"
1097 }
1098 run_test 24w "Reading a file larger than 4Gb"
1099
1100 test_24x() {
1101         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1102
1103         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1104                 skip "Need MDS version at least 2.7.56" && return
1105
1106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1107         local MDTIDX=1
1108         local remote_dir=$DIR/$tdir/remote_dir
1109
1110         test_mkdir -p $DIR/$tdir
1111         $LFS mkdir -i $MDTIDX $remote_dir ||
1112                 error "create remote directory failed"
1113
1114         test_mkdir -p $DIR/$tdir/src_dir
1115         touch $DIR/$tdir/src_file
1116         test_mkdir -p $remote_dir/tgt_dir
1117         touch $remote_dir/tgt_file
1118
1119         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1120                 error "rename dir cross MDT failed!"
1121
1122         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1123                 error "rename file cross MDT failed!"
1124
1125         touch $DIR/$tdir/ln_file
1126         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1127                 error "ln file cross MDT failed"
1128
1129         rm -rf $DIR/$tdir || error "Can not delete directories"
1130 }
1131 run_test 24x "cross MDT rename/link"
1132
1133 test_24y() {
1134         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1135         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1136         local MDTIDX=1
1137         local remote_dir=$DIR/$tdir/remote_dir
1138
1139         test_mkdir -p $DIR/$tdir
1140         $LFS mkdir -i $MDTIDX $remote_dir ||
1141                    error "create remote directory failed"
1142
1143         test_mkdir -p $remote_dir/src_dir
1144         touch $remote_dir/src_file
1145         test_mkdir -p $remote_dir/tgt_dir
1146         touch $remote_dir/tgt_file
1147
1148         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1149                 error "rename subdir in the same remote dir failed!"
1150
1151         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1152                 error "rename files in the same remote dir failed!"
1153
1154         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1155                 error "link files in the same remote dir failed!"
1156
1157         rm -rf $DIR/$tdir || error "Can not delete directories"
1158 }
1159 run_test 24y "rename/link on the same dir should succeed"
1160
1161 test_24A() { # LU-3182
1162         local NFILES=5000
1163
1164         rm -rf $DIR/$tdir
1165         test_mkdir -p $DIR/$tdir
1166         createmany -m $DIR/$tdir/$tfile $NFILES
1167         local t=$(ls $DIR/$tdir | wc -l)
1168         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1169         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1170         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1171                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1172         fi
1173
1174         rm -rf $DIR/$tdir || error "Can not delete directories"
1175 }
1176 run_test 24A "readdir() returns correct number of entries."
1177
1178 test_24B() { # LU-4805
1179         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1180         local count
1181
1182         test_mkdir $DIR/$tdir
1183         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1184                 error "create striped dir failed"
1185
1186         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1187         [ $count -eq 2 ] || error "Expected 2, got $count"
1188
1189         touch $DIR/$tdir/striped_dir/a
1190
1191         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1192         [ $count -eq 3 ] || error "Expected 3, got $count"
1193
1194         touch $DIR/$tdir/striped_dir/.f
1195
1196         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1197         [ $count -eq 4 ] || error "Expected 4, got $count"
1198
1199         rm -rf $DIR/$tdir || error "Can not delete directories"
1200 }
1201 run_test 24B "readdir for striped dir return correct number of entries"
1202
1203 test_24C() {
1204         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1205
1206         mkdir $DIR/$tdir
1207         mkdir $DIR/$tdir/d0
1208         mkdir $DIR/$tdir/d1
1209
1210         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1211                 error "create striped dir failed"
1212
1213         cd $DIR/$tdir/d0/striped_dir
1214
1215         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1216         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1217         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1218
1219         [ "$d0_ino" = "$parent_ino" ] ||
1220                 error ".. wrong, expect $d0_ino, get $parent_ino"
1221
1222         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1223                 error "mv striped dir failed"
1224
1225         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1226
1227         [ "$d1_ino" = "$parent_ino" ] ||
1228                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1229 }
1230 run_test 24C "check .. in striped dir"
1231
1232 test_24D() { # LU-6101
1233         local NFILES=50000
1234
1235         rm -rf $DIR/$tdir
1236         mkdir -p $DIR/$tdir
1237         createmany -m $DIR/$tdir/$tfile $NFILES
1238         local t=$(ls $DIR/$tdir | wc -l)
1239         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1240         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1241         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1242                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1243         fi
1244
1245         rm -rf $DIR/$tdir || error "Can not delete directories"
1246 }
1247 run_test 24D "readdir() returns correct number of entries after cursor reload"
1248
1249 test_24E() {
1250         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1252
1253         mkdir -p $DIR/$tdir
1254         mkdir $DIR/$tdir/src_dir
1255         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1256                 error "create remote source failed"
1257
1258         touch $DIR/$tdir/src_dir/src_child/a
1259
1260         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1261                 error "create remote target dir failed"
1262
1263         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1264                 error "create remote target child failed"
1265
1266         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1267                 error "rename dir cross MDT failed!"
1268
1269         find $DIR/$tdir
1270
1271         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1272                 error "src_child still exists after rename"
1273
1274         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1275                 error "missing file(a) after rename"
1276
1277         rm -rf $DIR/$tdir || error "Can not delete directories"
1278 }
1279 run_test 24E "cross MDT rename/link"
1280
1281 test_25a() {
1282         echo '== symlink sanity ============================================='
1283
1284         test_mkdir $DIR/d25
1285         ln -s d25 $DIR/s25
1286         touch $DIR/s25/foo || error
1287 }
1288 run_test 25a "create file in symlinked directory ==============="
1289
1290 test_25b() {
1291         [ ! -d $DIR/d25 ] && test_25a
1292         $CHECKSTAT -t file $DIR/s25/foo || error
1293 }
1294 run_test 25b "lookup file in symlinked directory ==============="
1295
1296 test_26a() {
1297         test_mkdir $DIR/d26
1298         test_mkdir $DIR/d26/d26-2
1299         ln -s d26/d26-2 $DIR/s26
1300         touch $DIR/s26/foo || error
1301 }
1302 run_test 26a "multiple component symlink ======================="
1303
1304 test_26b() {
1305         test_mkdir -p $DIR/d26b/d26-2
1306         ln -s d26b/d26-2/foo $DIR/s26-2
1307         touch $DIR/s26-2 || error
1308 }
1309 run_test 26b "multiple component symlink at end of lookup ======"
1310
1311 test_26c() {
1312         test_mkdir $DIR/d26.2
1313         touch $DIR/d26.2/foo
1314         ln -s d26.2 $DIR/s26.2-1
1315         ln -s s26.2-1 $DIR/s26.2-2
1316         ln -s s26.2-2 $DIR/s26.2-3
1317         chmod 0666 $DIR/s26.2-3/foo
1318 }
1319 run_test 26c "chain of symlinks ================================"
1320
1321 # recursive symlinks (bug 439)
1322 test_26d() {
1323         ln -s d26-3/foo $DIR/d26-3
1324 }
1325 run_test 26d "create multiple component recursive symlink ======"
1326
1327 test_26e() {
1328         [ ! -h $DIR/d26-3 ] && test_26d
1329         rm $DIR/d26-3
1330 }
1331 run_test 26e "unlink multiple component recursive symlink ======"
1332
1333 # recursive symlinks (bug 7022)
1334 test_26f() {
1335         test_mkdir -p $DIR/$tdir
1336         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1337         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1338         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1339         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1340         cd $tfile                || error "cd $tfile failed"
1341         ln -s .. dotdot          || error "ln dotdot failed"
1342         ln -s dotdot/lndir lndir || error "ln lndir failed"
1343         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1344         output=`ls $tfile/$tfile/lndir/bar1`
1345         [ "$output" = bar1 ] && error "unexpected output"
1346         rm -r $tfile             || error "rm $tfile failed"
1347         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1348 }
1349 run_test 26f "rm -r of a directory which has recursive symlink ="
1350
1351 test_27a() {
1352         echo '== stripe sanity =============================================='
1353         test_mkdir -p $DIR/d27 || error "mkdir failed"
1354         $GETSTRIPE $DIR/d27
1355         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1356         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1357         log "== test_27a: write to one stripe file ========================="
1358         cp /etc/hosts $DIR/d27/f0 || error
1359 }
1360 run_test 27a "one stripe file =================================="
1361
1362 test_27b() {
1363         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1364         test_mkdir -p $DIR/d27
1365         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1366         $GETSTRIPE -c $DIR/d27/f01
1367         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1368                 error "two-stripe file doesn't have two stripes"
1369
1370         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1371 }
1372 run_test 27b "create and write to two stripe file"
1373
1374 test_27d() {
1375         test_mkdir -p $DIR/d27
1376         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1377         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1378         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1379 }
1380 run_test 27d "create file with default settings ================"
1381
1382 test_27e() {
1383         # LU-5839 adds check for existed layout before setting it
1384         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1385                 skip "Need MDS version at least 2.7.56" && return
1386         test_mkdir -p $DIR/d27
1387         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1388         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1389         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1390 }
1391 run_test 27e "setstripe existing file (should return error) ======"
1392
1393 test_27f() {
1394         test_mkdir $DIR/$tdir
1395         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1396                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1397         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1398                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1399         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1400         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1401 }
1402 run_test 27f "setstripe with bad stripe size (should return error)"
1403
1404 test_27g() {
1405         test_mkdir -p $DIR/d27
1406         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1407         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1408                 error "$DIR/d27/fnone has object"
1409 }
1410 run_test 27g "$GETSTRIPE with no objects"
1411
1412 test_27i() {
1413         test_mkdir $DIR/$tdir
1414         touch $DIR/$tdir/$tfile || error "touch failed"
1415         [[ $($GETSTRIPE -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1416                 error "missing objects"
1417 }
1418 run_test 27i "$GETSTRIPE with some objects"
1419
1420 test_27j() {
1421         test_mkdir -p $DIR/d27
1422         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1423 }
1424 run_test 27j "setstripe with bad stripe offset (should return error)"
1425
1426 test_27k() { # bug 2844
1427         test_mkdir -p $DIR/d27
1428         FILE=$DIR/d27/f27k
1429         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1430         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1431         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1432         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1433         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1434         dd if=/dev/zero of=$FILE bs=4k count=1
1435         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1436         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1437 }
1438 run_test 27k "limit i_blksize for broken user apps ============="
1439
1440 test_27l() {
1441         test_mkdir -p $DIR/d27
1442         mcreate $DIR/f27l || error "creating file"
1443         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1444                 error "setstripe should have failed" || true
1445 }
1446 run_test 27l "check setstripe permissions (should return error)"
1447
1448 test_27m() {
1449         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1450                 return
1451
1452         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1453                    head -n1)
1454         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1455                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1456                 return
1457         fi
1458         trap simple_cleanup_common EXIT
1459         test_mkdir -p $DIR/$tdir
1460         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1461         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1462                 error "dd should fill OST0"
1463         i=2
1464         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1465                 i=$((i + 1))
1466                 [ $i -gt 256 ] && break
1467         done
1468         i=$((i + 1))
1469         touch $DIR/$tdir/f27m_$i
1470         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1471                 error "OST0 was full but new created file still use it"
1472         i=$((i + 1))
1473         touch $DIR/$tdir/f27m_$i
1474         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1475                 error "OST0 was full but new created file still use it"
1476         simple_cleanup_common
1477 }
1478 run_test 27m "create file while OST0 was full =================="
1479
1480 sleep_maxage() {
1481         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1482         sleep $DELAY
1483 }
1484
1485 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1486 # if the OST isn't full anymore.
1487 reset_enospc() {
1488         local OSTIDX=${1:-""}
1489
1490         local list=$(comma_list $(osts_nodes))
1491         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1492
1493         do_nodes $list lctl set_param fail_loc=0
1494         sync    # initiate all OST_DESTROYs from MDS to OST
1495         sleep_maxage
1496 }
1497
1498 exhaust_precreations() {
1499         local OSTIDX=$1
1500         local FAILLOC=$2
1501         local FAILIDX=${3:-$OSTIDX}
1502         local ofacet=ost$((OSTIDX + 1))
1503
1504         test_mkdir -p -c1 $DIR/$tdir
1505         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1506         local mfacet=mds$((mdtidx + 1))
1507         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1508
1509         local OST=$(ostname_from_index $OSTIDX)
1510
1511         # on the mdt's osc
1512         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1513         local last_id=$(do_facet $mfacet lctl get_param -n \
1514                         osc.$mdtosc_proc1.prealloc_last_id)
1515         local next_id=$(do_facet $mfacet lctl get_param -n \
1516                         osc.$mdtosc_proc1.prealloc_next_id)
1517
1518         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1519         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1520
1521         test_mkdir -p $DIR/$tdir/${OST}
1522         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1523 #define OBD_FAIL_OST_ENOSPC              0x215
1524         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1525         echo "Creating to objid $last_id on ost $OST..."
1526         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1527         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1528         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1529         sleep_maxage
1530 }
1531
1532 exhaust_all_precreations() {
1533         local i
1534         for (( i=0; i < OSTCOUNT; i++ )) ; do
1535                 exhaust_precreations $i $1 -1
1536         done
1537 }
1538
1539 test_27n() {
1540         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1541         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1542         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1543         remote_ost_nodsh && skip "remote OST with nodsh" && return
1544
1545         reset_enospc
1546         rm -f $DIR/$tdir/$tfile
1547         exhaust_precreations 0 0x80000215
1548         $SETSTRIPE -c -1 $DIR/$tdir
1549         touch $DIR/$tdir/$tfile || error
1550         $GETSTRIPE $DIR/$tdir/$tfile
1551         reset_enospc
1552 }
1553 run_test 27n "create file with some full OSTs =================="
1554
1555 test_27o() {
1556         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1557         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1558         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1559         remote_ost_nodsh && skip "remote OST with nodsh" && return
1560
1561         reset_enospc
1562         rm -f $DIR/$tdir/$tfile
1563         exhaust_all_precreations 0x215
1564
1565         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1566
1567         reset_enospc
1568         rm -rf $DIR/$tdir/*
1569 }
1570 run_test 27o "create file with all full OSTs (should error) ===="
1571
1572 test_27p() {
1573         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1575         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1576         remote_ost_nodsh && skip "remote OST with nodsh" && return
1577
1578         reset_enospc
1579         rm -f $DIR/$tdir/$tfile
1580         test_mkdir -p $DIR/$tdir
1581
1582         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1583         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1584         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1585
1586         exhaust_precreations 0 0x80000215
1587         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1588         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1589         $GETSTRIPE $DIR/$tdir/$tfile
1590
1591         reset_enospc
1592 }
1593 run_test 27p "append to a truncated file with some full OSTs ==="
1594
1595 test_27q() {
1596         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1598         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1599         remote_ost_nodsh && skip "remote OST with nodsh" && return
1600
1601         reset_enospc
1602         rm -f $DIR/$tdir/$tfile
1603
1604         test_mkdir -p $DIR/$tdir
1605         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1606         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1607         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1608
1609         exhaust_all_precreations 0x215
1610
1611         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1612         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1613
1614         reset_enospc
1615 }
1616 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1617
1618 test_27r() {
1619         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1620         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1621         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1622         remote_ost_nodsh && skip "remote OST with nodsh" && return
1623
1624         reset_enospc
1625         rm -f $DIR/$tdir/$tfile
1626         exhaust_precreations 0 0x80000215
1627
1628         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1629
1630         reset_enospc
1631 }
1632 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1633
1634 test_27s() { # bug 10725
1635         test_mkdir -p $DIR/$tdir
1636         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1637         local stripe_count=0
1638         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1639         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1640                 error "stripe width >= 2^32 succeeded" || true
1641
1642 }
1643 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1644
1645 test_27t() { # bug 10864
1646         WDIR=$(pwd)
1647         WLFS=$(which lfs)
1648         cd $DIR
1649         touch $tfile
1650         $WLFS getstripe $tfile
1651         cd $WDIR
1652 }
1653 run_test 27t "check that utils parse path correctly"
1654
1655 test_27u() { # bug 4900
1656         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1657         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1658         local index
1659         local list=$(comma_list $(mdts_nodes))
1660
1661 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1662         do_nodes $list $LCTL set_param fail_loc=0x139
1663         test_mkdir -p $DIR/$tdir
1664         rm -rf $DIR/$tdir/*
1665         createmany -o $DIR/$tdir/t- 1000
1666         do_nodes $list $LCTL set_param fail_loc=0
1667
1668         TLOG=$TMP/$tfile.getstripe
1669         $GETSTRIPE $DIR/$tdir > $TLOG
1670         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1671         unlinkmany $DIR/$tdir/t- 1000
1672         [[ $OBJS -gt 0 ]] &&
1673                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1674 }
1675 run_test 27u "skip object creation on OSC w/o objects =========="
1676
1677 test_27v() { # bug 4900
1678         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1680         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1681         remote_ost_nodsh && skip "remote OST with nodsh" && return
1682
1683         exhaust_all_precreations 0x215
1684         reset_enospc
1685
1686         test_mkdir -p $DIR/$tdir
1687         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1688
1689         touch $DIR/$tdir/$tfile
1690         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1691         # all except ost1
1692         for (( i=1; i < OSTCOUNT; i++ )); do
1693                 do_facet ost$i lctl set_param fail_loc=0x705
1694         done
1695         local START=`date +%s`
1696         createmany -o $DIR/$tdir/$tfile 32
1697
1698         local FINISH=`date +%s`
1699         local TIMEOUT=`lctl get_param -n timeout`
1700         local PROCESS=$((FINISH - START))
1701         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1702                error "$FINISH - $START >= $TIMEOUT / 2"
1703         sleep $((TIMEOUT / 2 - PROCESS))
1704         reset_enospc
1705 }
1706 run_test 27v "skip object creation on slow OST ================="
1707
1708 test_27w() { # bug 10997
1709         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1710         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1711         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1712                 error "stripe size $size != 65536" || true
1713         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1714                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1715 }
1716 run_test 27w "check $SETSTRIPE -S option"
1717
1718 test_27wa() {
1719         [[ $OSTCOUNT -lt 2 ]] &&
1720                 skip_env "skipping multiple stripe count/offset test" && return
1721
1722         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1723         for i in $(seq 1 $OSTCOUNT); do
1724                 offset=$((i - 1))
1725                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1726                         error "setstripe -c $i -i $offset failed"
1727                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1728                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1729                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1730                 [ $index -ne $offset ] &&
1731                         error "stripe offset $index != $offset" || true
1732         done
1733 }
1734 run_test 27wa "check $SETSTRIPE -c -i options"
1735
1736 test_27x() {
1737         remote_ost_nodsh && skip "remote OST with nodsh" && return
1738         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1740         OFFSET=$(($OSTCOUNT - 1))
1741         OSTIDX=0
1742         local OST=$(ostname_from_index $OSTIDX)
1743
1744         test_mkdir -p $DIR/$tdir
1745         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1746         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1747         sleep_maxage
1748         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1749         for i in `seq 0 $OFFSET`; do
1750                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1751                 error "OST0 was degraded but new created file still use it"
1752         done
1753         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1754 }
1755 run_test 27x "create files while OST0 is degraded"
1756
1757 test_27y() {
1758         [[ $OSTCOUNT -lt 2 ]] &&
1759                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1760         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1761         remote_ost_nodsh && skip "remote OST with nodsh" && return
1762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1763
1764         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1765         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1766             osc.$mdtosc.prealloc_last_id)
1767         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1768             osc.$mdtosc.prealloc_next_id)
1769         local fcount=$((last_id - next_id))
1770         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1771         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1772
1773         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1774                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1775         local OST_DEACTIVE_IDX=-1
1776         local OSC
1777         local OSTIDX
1778         local OST
1779
1780         for OSC in $MDS_OSCS; do
1781                 OST=$(osc_to_ost $OSC)
1782                 OSTIDX=$(index_from_ostuuid $OST)
1783                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1784                         OST_DEACTIVE_IDX=$OSTIDX
1785                 fi
1786                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1787                         echo $OSC "is Deactivated:"
1788                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1789                 fi
1790         done
1791
1792         OSTIDX=$(index_from_ostuuid $OST)
1793         test_mkdir -p $DIR/$tdir
1794         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1795
1796         for OSC in $MDS_OSCS; do
1797                 OST=$(osc_to_ost $OSC)
1798                 OSTIDX=$(index_from_ostuuid $OST)
1799                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1800                         echo $OST "is degraded:"
1801                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1802                                                 obdfilter.$OST.degraded=1
1803                 fi
1804         done
1805
1806         sleep_maxage
1807         createmany -o $DIR/$tdir/$tfile $fcount
1808
1809         for OSC in $MDS_OSCS; do
1810                 OST=$(osc_to_ost $OSC)
1811                 OSTIDX=$(index_from_ostuuid $OST)
1812                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1813                         echo $OST "is recovered from degraded:"
1814                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1815                                                 obdfilter.$OST.degraded=0
1816                 else
1817                         do_facet $SINGLEMDS lctl --device %$OSC activate
1818                 fi
1819         done
1820
1821         # all osp devices get activated, hence -1 stripe count restored
1822         local stripecnt=0
1823
1824         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1825         # devices get activated.
1826         sleep_maxage
1827         $SETSTRIPE -c -1 $DIR/$tfile
1828         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1829         rm -f $DIR/$tfile
1830         [ $stripecnt -ne $OSTCOUNT ] &&
1831                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1832         return 0
1833 }
1834 run_test 27y "create files while OST0 is degraded and the rest inactive"
1835
1836 check_seq_oid()
1837 {
1838         log "check file $1"
1839
1840         lmm_count=$($GETSTRIPE -c $1)
1841         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1842         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1843
1844         local old_ifs="$IFS"
1845         IFS=$'[:]'
1846         fid=($($LFS path2fid $1))
1847         IFS="$old_ifs"
1848
1849         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1850         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1851
1852         # compare lmm_seq and lu_fid->f_seq
1853         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1854         # compare lmm_object_id and lu_fid->oid
1855         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1856
1857         # check the trusted.fid attribute of the OST objects of the file
1858         local have_obdidx=false
1859         local stripe_nr=0
1860         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1861                 # skip lines up to and including "obdidx"
1862                 [ -z "$obdidx" ] && break
1863                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1864                 $have_obdidx || continue
1865
1866                 local ost=$((obdidx + 1))
1867                 local dev=$(ostdevname $ost)
1868                 local oid_hex
1869
1870                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1871
1872                 seq=$(echo $seq | sed -e "s/^0x//g")
1873                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1874                         oid_hex=$(echo $oid)
1875                 else
1876                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1877                 fi
1878                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1879
1880                 local ff
1881                 #
1882                 # Don't unmount/remount the OSTs if we don't need to do that.
1883                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1884                 # update too, until that use mount/ll_decode_filter_fid/mount.
1885                 # Re-enable when debugfs will understand new filter_fid.
1886                 #
1887                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1888                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1889                                 $dev 2>/dev/null" | grep "parent=")
1890                 else
1891                         stop ost$ost
1892                         mount_fstype ost$ost
1893                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1894                                 $(facet_mntpt ost$ost)/$obj_file)
1895                         unmount_fstype ost$ost
1896                         start ost$ost $dev $OST_MOUNT_OPTS
1897                         clients_up
1898                 fi
1899
1900                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1901
1902                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1903
1904                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1905                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1906                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1907                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1908                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1909                 local ff_pstripe
1910                 if echo $ff_parent | grep -q 'stripe='; then
1911                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1912                         if echo $ff_pstripe | grep -q 'stripe_size='; then
1913                                 ff_pstripe=$(echo $ff_pstripe | cut -d' ' -f1)
1914                         fi
1915                 else
1916                         #
1917                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1918                         # into f_ver in this case.  See the comment on
1919                         # ff_parent.
1920                         #
1921                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1922                                 sed -e 's/\]//')
1923                 fi
1924
1925                 if echo $ff_parent | grep -q 'stripe_count='; then
1926                         local ff_scnt=$(echo $ff_parent |
1927                                         sed -e 's/.*stripe_count=//' |
1928                                         cut -d' ' -f1)
1929
1930                         [ $lmm_count -eq $ff_scnt ] ||
1931                                 error "FF stripe count $lmm_count != $ff_scnt"
1932                 fi
1933
1934                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1935                 [ $ff_pseq = $lmm_seq ] ||
1936                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1937                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1938                 [ $ff_poid = $lmm_oid ] ||
1939                         error "FF parent OID $ff_poid != $lmm_oid"
1940                 (($ff_pstripe == $stripe_nr)) ||
1941                         error "FF stripe $ff_pstripe != $stripe_nr"
1942
1943                 stripe_nr=$((stripe_nr + 1))
1944         done
1945 }
1946
1947 test_27z() {
1948         remote_ost_nodsh && skip "remote OST with nodsh" && return
1949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1950         test_mkdir -p $DIR/$tdir
1951
1952         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1953                 { error "setstripe -c -1 failed"; return 1; }
1954         # We need to send a write to every object to get parent FID info set.
1955         # This _should_ also work for setattr, but does not currently.
1956         # touch $DIR/$tdir/$tfile-1 ||
1957         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1958                 { error "dd $tfile-1 failed"; return 2; }
1959         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1960                 { error "setstripe -c -1 failed"; return 3; }
1961         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1962                 { error "dd $tfile-2 failed"; return 4; }
1963
1964         # make sure write RPCs have been sent to OSTs
1965         sync; sleep 5; sync
1966
1967         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1968         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1969 }
1970 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1971
1972 test_27A() { # b=19102
1973         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1974         local restore_size=$($GETSTRIPE -S $MOUNT)
1975         local restore_count=$($GETSTRIPE -c $MOUNT)
1976         local restore_offset=$($GETSTRIPE -i $MOUNT)
1977         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1978         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1979                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1980         local default_size=$($GETSTRIPE -S $MOUNT)
1981         local default_offset=$($GETSTRIPE -i $MOUNT)
1982         local dsize=$((1024 * 1024))
1983         [ $default_size -eq $dsize ] ||
1984                 error "stripe size $default_size != $dsize"
1985         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1986         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1987 }
1988 run_test 27A "check filesystem-wide default LOV EA values"
1989
1990 test_27B() { # LU-2523
1991         test_mkdir -p $DIR/$tdir
1992         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1993         touch $DIR/$tdir/f0
1994         # open f1 with O_LOV_DELAY_CREATE
1995         # rename f0 onto f1
1996         # call setstripe ioctl on open file descriptor for f1
1997         # close
1998         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1999                 $DIR/$tdir/f0
2000
2001         rm -f $DIR/$tdir/f1
2002         # open f1 with O_LOV_DELAY_CREATE
2003         # unlink f1
2004         # call setstripe ioctl on open file descriptor for f1
2005         # close
2006         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2007
2008         # Allow multiop to fail in imitation of NFS's busted semantics.
2009         true
2010 }
2011 run_test 27B "call setstripe on open unlinked file/rename victim"
2012
2013 test_27C() { #LU-2871
2014         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
2015
2016         declare -a ost_idx
2017         local index
2018         local found
2019         local i
2020         local j
2021
2022         test_mkdir -p $DIR/$tdir
2023         cd $DIR/$tdir
2024         for i in $(seq 0 $((OSTCOUNT - 1))); do
2025                 # set stripe across all OSTs starting from OST$i
2026                 $SETSTRIPE -i $i -c -1 $tfile$i
2027                 # get striping information
2028                 ost_idx=($($GETSTRIPE $tfile$i |
2029                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2030                 echo ${ost_idx[@]}
2031
2032                 # check the layout
2033                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2034                         error "${#ost_idx[@]} != $OSTCOUNT"
2035
2036                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2037                         found=0
2038                         for j in $(echo ${ost_idx[@]}); do
2039                                 if [ $index -eq $j ]; then
2040                                         found=1
2041                                         break
2042                                 fi
2043                         done
2044                         [ $found = 1 ] ||
2045                                 error "Can not find $index in ${ost_idx[@]}"
2046                 done
2047         done
2048 }
2049 run_test 27C "check full striping across all OSTs"
2050
2051 test_27D() {
2052         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2053         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2054         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2055         local POOL=${POOL:-testpool}
2056         local first_ost=0
2057         local last_ost=$(($OSTCOUNT - 1))
2058         local ost_step=1
2059         local ost_list=$(seq $first_ost $ost_step $last_ost)
2060         local ost_range="$first_ost $last_ost $ost_step"
2061
2062         test_mkdir -p $DIR/$tdir
2063         pool_add $POOL || error "pool_add failed"
2064         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2065
2066         local skip27D
2067         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
2068                 skip27D = "-s 29,30,31"
2069         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2070                 error "llapi_layout_test failed"
2071
2072         destroy_test_pools || error "destroy test pools failed"
2073 }
2074 run_test 27D "validate llapi_layout API"
2075
2076 # Verify that default_easize is increased from its initial value after
2077 # accessing a widely striped file.
2078 test_27E() {
2079         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2080         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2081                 skip "client does not have LU-3338 fix" && return
2082
2083         # 72 bytes is the minimum space required to store striping
2084         # information for a file striped across one OST:
2085         # (sizeof(struct lov_user_md_v3) +
2086         #  sizeof(struct lov_user_ost_data_v1))
2087         local min_easize=72
2088         $LCTL set_param -n llite.*.default_easize $min_easize ||
2089                 error "lctl set_param failed"
2090         local easize=$($LCTL get_param -n llite.*.default_easize)
2091
2092         [ $easize -eq $min_easize ] ||
2093                 error "failed to set default_easize"
2094
2095         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2096                 error "setstripe failed"
2097         cat $DIR/$tfile
2098         rm $DIR/$tfile
2099
2100         easize=$($LCTL get_param -n llite.*.default_easize)
2101
2102         [ $easize -gt $min_easize ] ||
2103                 error "default_easize not updated"
2104 }
2105 run_test 27E "check that default extended attribute size properly increases"
2106
2107 test_27F() { # LU-5346/LU-7975
2108
2109         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2110
2111         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2112                 skip "Need MDS version at least 2.8.51" && return
2113
2114         test_mkdir -p $DIR/$tdir
2115         rm -f $DIR/$tdir/f0
2116         $SETSTRIPE -c 2 $DIR/$tdir
2117
2118         # stop all OSTs to reproduce situation for LU-7975 ticket
2119         for num in $(seq $OSTCOUNT); do
2120                 stop ost$num
2121         done
2122
2123         # open/create f0 with O_LOV_DELAY_CREATE
2124         # truncate f0 to a non-0 size
2125         # close
2126         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2127
2128         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2129         # open/write it again to force delayed layout creation
2130         cat /etc/hosts > $DIR/$tdir/f0 &
2131         catpid=$!
2132
2133         # restart OSTs
2134         for num in $(seq $OSTCOUNT); do
2135                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2136                         error "ost$num failed to start"
2137         done
2138
2139         wait $catpid || error "cat failed"
2140
2141         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2142         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2143
2144 }
2145 run_test 27F "Client resend delayed layout creation with non-zero size"
2146
2147 # createtest also checks that device nodes are created and
2148 # then visible correctly (#2091)
2149 test_28() { # bug 2091
2150         test_mkdir $DIR/d28
2151         $CREATETEST $DIR/d28/ct || error
2152 }
2153 run_test 28 "create/mknod/mkdir with bad file types ============"
2154
2155 test_29() {
2156         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
2157         sync; sleep 1; sync # flush out any dirty pages from previous tests
2158         cancel_lru_locks
2159         test_mkdir $DIR/d29
2160         touch $DIR/d29/foo
2161         log 'first d29'
2162         ls -l $DIR/d29
2163
2164         declare -i LOCKCOUNTORIG=0
2165         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2166                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2167         done
2168         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2169
2170         declare -i LOCKUNUSEDCOUNTORIG=0
2171         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2172                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2173         done
2174
2175         log 'second d29'
2176         ls -l $DIR/d29
2177         log 'done'
2178
2179         declare -i LOCKCOUNTCURRENT=0
2180         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2181                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2182         done
2183
2184         declare -i LOCKUNUSEDCOUNTCURRENT=0
2185         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2186                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2187         done
2188
2189         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2190                 $LCTL set_param -n ldlm.dump_namespaces ""
2191                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2192                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2193                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2194                 return 2
2195         fi
2196         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2197                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2198                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2199                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2200                 return 3
2201         fi
2202 }
2203 run_test 29 "IT_GETATTR regression  ============================"
2204
2205 test_30a() { # was test_30
2206         cp $(which ls) $DIR || cp /bin/ls $DIR
2207         $DIR/ls / || error
2208         rm $DIR/ls
2209 }
2210 run_test 30a "execute binary from Lustre (execve) =============="
2211
2212 test_30b() {
2213         cp `which ls` $DIR || cp /bin/ls $DIR
2214         chmod go+rx $DIR/ls
2215         $RUNAS $DIR/ls / || error
2216         rm $DIR/ls
2217 }
2218 run_test 30b "execute binary from Lustre as non-root ==========="
2219
2220 test_30c() { # b=22376
2221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2222         cp `which ls` $DIR || cp /bin/ls $DIR
2223         chmod a-rw $DIR/ls
2224         cancel_lru_locks mdc
2225         cancel_lru_locks osc
2226         $RUNAS $DIR/ls / || error
2227         rm -f $DIR/ls
2228 }
2229 run_test 30c "execute binary from Lustre without read perms ===="
2230
2231 test_31a() {
2232         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2233         $CHECKSTAT -a $DIR/f31 || error
2234 }
2235 run_test 31a "open-unlink file =================================="
2236
2237 test_31b() {
2238         touch $DIR/f31 || error
2239         ln $DIR/f31 $DIR/f31b || error
2240         $MULTIOP $DIR/f31b Ouc || error
2241         $CHECKSTAT -t file $DIR/f31 || error
2242 }
2243 run_test 31b "unlink file with multiple links while open ======="
2244
2245 test_31c() {
2246         touch $DIR/f31 || error
2247         ln $DIR/f31 $DIR/f31c || error
2248         multiop_bg_pause $DIR/f31 O_uc || return 1
2249         MULTIPID=$!
2250         $MULTIOP $DIR/f31c Ouc
2251         kill -USR1 $MULTIPID
2252         wait $MULTIPID
2253 }
2254 run_test 31c "open-unlink file with multiple links ============="
2255
2256 test_31d() {
2257         opendirunlink $DIR/d31d $DIR/d31d || error
2258         $CHECKSTAT -a $DIR/d31d || error
2259 }
2260 run_test 31d "remove of open directory ========================="
2261
2262 test_31e() { # bug 2904
2263         openfilleddirunlink $DIR/d31e || error
2264 }
2265 run_test 31e "remove of open non-empty directory ==============="
2266
2267 test_31f() { # bug 4554
2268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2269         set -vx
2270         test_mkdir $DIR/d31f
2271         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2272         cp /etc/hosts $DIR/d31f
2273         ls -l $DIR/d31f
2274         $GETSTRIPE $DIR/d31f/hosts
2275         multiop_bg_pause $DIR/d31f D_c || return 1
2276         MULTIPID=$!
2277
2278         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2279         test_mkdir $DIR/d31f
2280         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2281         cp /etc/hosts $DIR/d31f
2282         ls -l $DIR/d31f
2283         $GETSTRIPE $DIR/d31f/hosts
2284         multiop_bg_pause $DIR/d31f D_c || return 1
2285         MULTIPID2=$!
2286
2287         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2288         wait $MULTIPID || error "first opendir $MULTIPID failed"
2289
2290         sleep 6
2291
2292         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2293         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2294         set +vx
2295 }
2296 run_test 31f "remove of open directory with open-unlink file ==="
2297
2298 test_31g() {
2299         echo "-- cross directory link --"
2300         test_mkdir -c1 $DIR/${tdir}ga
2301         test_mkdir -c1 $DIR/${tdir}gb
2302         touch $DIR/${tdir}ga/f
2303         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2304         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2305         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2306         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2307         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2308 }
2309 run_test 31g "cross directory link==============="
2310
2311 test_31h() {
2312         echo "-- cross directory link --"
2313         test_mkdir -c1 $DIR/${tdir}
2314         test_mkdir -c1 $DIR/${tdir}/dir
2315         touch $DIR/${tdir}/f
2316         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2317         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2318         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2319         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2320         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2321 }
2322 run_test 31h "cross directory link under child==============="
2323
2324 test_31i() {
2325         echo "-- cross directory link --"
2326         test_mkdir -c1 $DIR/$tdir
2327         test_mkdir -c1 $DIR/$tdir/dir
2328         touch $DIR/$tdir/dir/f
2329         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2330         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2331         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2332         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2333         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2334 }
2335 run_test 31i "cross directory link under parent==============="
2336
2337 test_31j() {
2338         test_mkdir -c1 -p $DIR/$tdir
2339         test_mkdir -c1 -p $DIR/$tdir/dir1
2340         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2341         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2342         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2343         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2344         return 0
2345 }
2346 run_test 31j "link for directory==============="
2347
2348 test_31k() {
2349         test_mkdir -c1 -p $DIR/$tdir
2350         touch $DIR/$tdir/s
2351         touch $DIR/$tdir/exist
2352         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2353         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2354         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2355         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2356         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2357         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2358         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2359         return 0
2360 }
2361 run_test 31k "link to file: the same, non-existing, dir==============="
2362
2363 test_31m() {
2364         mkdir $DIR/d31m
2365         touch $DIR/d31m/s
2366         mkdir $DIR/d31m2
2367         touch $DIR/d31m2/exist
2368         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2369         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2370         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2371         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2372         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2373         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2374         return 0
2375 }
2376 run_test 31m "link to file: the same, non-existing, dir==============="
2377
2378 test_31n() {
2379         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2380         nlink=$(stat --format=%h $DIR/$tfile)
2381         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2382         local fd=$(free_fd)
2383         local cmd="exec $fd<$DIR/$tfile"
2384         eval $cmd
2385         cmd="exec $fd<&-"
2386         trap "eval $cmd" EXIT
2387         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2388         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2389         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2390         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2391         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2392         eval $cmd
2393 }
2394 run_test 31n "check link count of unlinked file"
2395
2396 link_one() {
2397         local TEMPNAME=$(mktemp $1_XXXXXX)
2398         mlink $TEMPNAME $1 2> /dev/null &&
2399                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2400         munlink $TEMPNAME
2401 }
2402
2403 test_31o() { # LU-2901
2404         test_mkdir -p $DIR/$tdir
2405         for LOOP in $(seq 100); do
2406                 rm -f $DIR/$tdir/$tfile*
2407                 for THREAD in $(seq 8); do
2408                         link_one $DIR/$tdir/$tfile.$LOOP &
2409                 done
2410                 wait
2411                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2412                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2413                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2414                         break || true
2415         done
2416 }
2417 run_test 31o "duplicate hard links with same filename"
2418
2419 test_31p() {
2420         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2421
2422         test_mkdir $DIR/$tdir
2423         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2424         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2425
2426         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2427                 error "open unlink test1 failed"
2428         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2429                 error "open unlink test2 failed"
2430
2431         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2432                 error "test1 still exists"
2433         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2434                 error "test2 still exists"
2435 }
2436 run_test 31p "remove of open striped directory"
2437
2438 cleanup_test32_mount() {
2439         local rc=0
2440         trap 0
2441         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2442         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2443         losetup -d $loopdev || true
2444         rm -rf $DIR/$tdir
2445         return $rc
2446 }
2447
2448 test_32a() {
2449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2450         echo "== more mountpoints and symlinks ================="
2451         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2452         trap cleanup_test32_mount EXIT
2453         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2454         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2455         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2456         cleanup_test32_mount
2457 }
2458 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2459
2460 test_32b() {
2461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2462         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2463         trap cleanup_test32_mount EXIT
2464         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2465         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2466         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2467         cleanup_test32_mount
2468 }
2469 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2470
2471 test_32c() {
2472         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2473         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2474         trap cleanup_test32_mount EXIT
2475         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2476         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2477         test_mkdir -p $DIR/$tdir/d2/test_dir
2478         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2479         cleanup_test32_mount
2480 }
2481 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2482
2483 test_32d() {
2484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2485         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2486         trap cleanup_test32_mount EXIT
2487         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2488         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2489         test_mkdir -p $DIR/$tdir/d2/test_dir
2490         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2491         cleanup_test32_mount
2492 }
2493 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2494
2495 test_32e() {
2496         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2497         test_mkdir -p $DIR/d32e/tmp
2498         TMP_DIR=$DIR/d32e/tmp
2499         ln -s $DIR/d32e $TMP_DIR/symlink11
2500         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2501         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2502         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2503 }
2504 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2505
2506 test_32f() {
2507         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2508         test_mkdir -p $DIR/d32f/tmp
2509         TMP_DIR=$DIR/d32f/tmp
2510         ln -s $DIR/d32f $TMP_DIR/symlink11
2511         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2512         ls $DIR/d32f/tmp/symlink11  || error
2513         ls $DIR/d32f/symlink01 || error
2514 }
2515 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2516
2517 test_32g() {
2518         TMP_DIR=$DIR/$tdir/tmp
2519         test_mkdir -p $DIR/$tdir/tmp
2520         test_mkdir $DIR/${tdir}2
2521         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2522         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2523         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2524         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2525         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2526         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2527 }
2528 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2529
2530 test_32h() {
2531         rm -fr $DIR/$tdir $DIR/${tdir}2
2532         TMP_DIR=$DIR/$tdir/tmp
2533         test_mkdir -p $DIR/$tdir/tmp
2534         test_mkdir $DIR/${tdir}2
2535         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2536         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2537         ls $TMP_DIR/symlink12 || error
2538         ls $DIR/$tdir/symlink02  || error
2539 }
2540 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2541
2542 test_32i() {
2543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2544         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2545         trap cleanup_test32_mount EXIT
2546         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2547         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2548         touch $DIR/$tdir/test_file
2549         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2550         cleanup_test32_mount
2551 }
2552 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2553
2554 test_32j() {
2555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2556         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2557         trap cleanup_test32_mount EXIT
2558         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2559         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2560         touch $DIR/$tdir/test_file
2561         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2562         cleanup_test32_mount
2563 }
2564 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2565
2566 test_32k() {
2567         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2568         rm -fr $DIR/$tdir
2569         trap cleanup_test32_mount EXIT
2570         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2571         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2572         test_mkdir -p $DIR/$tdir/d2
2573         touch $DIR/$tdir/d2/test_file || error
2574         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2575         cleanup_test32_mount
2576 }
2577 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2578
2579 test_32l() {
2580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2581         rm -fr $DIR/$tdir
2582         trap cleanup_test32_mount EXIT
2583         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2584         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2585         test_mkdir -p $DIR/$tdir/d2
2586         touch $DIR/$tdir/d2/test_file
2587         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2588         cleanup_test32_mount
2589 }
2590 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2591
2592 test_32m() {
2593         rm -fr $DIR/d32m
2594         test_mkdir -p $DIR/d32m/tmp
2595         TMP_DIR=$DIR/d32m/tmp
2596         ln -s $DIR $TMP_DIR/symlink11
2597         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2598         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2599         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2600 }
2601 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2602
2603 test_32n() {
2604         rm -fr $DIR/d32n
2605         test_mkdir -p $DIR/d32n/tmp
2606         TMP_DIR=$DIR/d32n/tmp
2607         ln -s $DIR $TMP_DIR/symlink11
2608         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2609         ls -l $DIR/d32n/tmp/symlink11  || error
2610         ls -l $DIR/d32n/symlink01 || error
2611 }
2612 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2613
2614 test_32o() {
2615         touch $DIR/$tfile
2616         test_mkdir -p $DIR/d32o/tmp
2617         TMP_DIR=$DIR/d32o/tmp
2618         ln -s $DIR/$tfile $TMP_DIR/symlink12
2619         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2620         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2621         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2622         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2623         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2624 }
2625 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2626
2627 test_32p() {
2628         log 32p_1
2629         rm -fr $DIR/d32p
2630         log 32p_2
2631         rm -f $DIR/$tfile
2632         log 32p_3
2633         touch $DIR/$tfile
2634         log 32p_4
2635         test_mkdir -p $DIR/d32p/tmp
2636         log 32p_5
2637         TMP_DIR=$DIR/d32p/tmp
2638         log 32p_6
2639         ln -s $DIR/$tfile $TMP_DIR/symlink12
2640         log 32p_7
2641         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2642         log 32p_8
2643         cat $DIR/d32p/tmp/symlink12 || error
2644         log 32p_9
2645         cat $DIR/d32p/symlink02 || error
2646         log 32p_10
2647 }
2648 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2649
2650 test_32q() {
2651         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2652         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2653         trap cleanup_test32_mount EXIT
2654         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2655         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2656         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2657         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2658         cleanup_test32_mount
2659 }
2660 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2661
2662 test_32r() {
2663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2664         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2665         trap cleanup_test32_mount EXIT
2666         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2667         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2668         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2669         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2670         cleanup_test32_mount
2671 }
2672 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2673
2674 test_33aa() {
2675         rm -f $DIR/$tfile
2676         touch $DIR/$tfile
2677         chmod 444 $DIR/$tfile
2678         chown $RUNAS_ID $DIR/$tfile
2679         log 33_1
2680         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2681         log 33_2
2682 }
2683 run_test 33aa "write file with mode 444 (should return error) ===="
2684
2685 test_33a() {
2686         rm -fr $DIR/d33
2687         test_mkdir -p $DIR/d33
2688         chown $RUNAS_ID $DIR/d33
2689         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2690         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2691                 error "open RDWR" || true
2692 }
2693 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2694
2695 test_33b() {
2696         rm -fr $DIR/d33
2697         test_mkdir -p $DIR/d33
2698         chown $RUNAS_ID $DIR/d33
2699         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2700 }
2701 run_test 33b "test open file with malformed flags (No panic)"
2702
2703 test_33c() {
2704         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2705         local ostnum
2706         local ostname
2707         local write_bytes
2708         local all_zeros
2709
2710         remote_ost_nodsh && skip "remote OST with nodsh" && return
2711         all_zeros=:
2712         rm -fr $DIR/d33
2713         test_mkdir -p $DIR/d33
2714         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2715
2716         sync
2717         for ostnum in $(seq $OSTCOUNT); do
2718                 # test-framework's OST numbering is one-based, while Lustre's
2719                 # is zero-based
2720                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2721                 # Parsing llobdstat's output sucks; we could grep the /proc
2722                 # path, but that's likely to not be as portable as using the
2723                 # llobdstat utility.  So we parse lctl output instead.
2724                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2725                         obdfilter/$ostname/stats |
2726                         awk '/^write_bytes/ {print $7}' )
2727                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2728                 if (( ${write_bytes:-0} > 0 ))
2729                 then
2730                         all_zeros=false
2731                         break;
2732                 fi
2733         done
2734
2735         $all_zeros || return 0
2736
2737         # Write four bytes
2738         echo foo > $DIR/d33/bar
2739         # Really write them
2740         sync
2741
2742         # Total up write_bytes after writing.  We'd better find non-zeros.
2743         for ostnum in $(seq $OSTCOUNT); do
2744                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2745                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2746                         obdfilter/$ostname/stats |
2747                         awk '/^write_bytes/ {print $7}' )
2748                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2749                 if (( ${write_bytes:-0} > 0 ))
2750                 then
2751                         all_zeros=false
2752                         break;
2753                 fi
2754         done
2755
2756         if $all_zeros
2757         then
2758                 for ostnum in $(seq $OSTCOUNT); do
2759                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2760                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2761                         do_facet ost$ostnum lctl get_param -n \
2762                                 obdfilter/$ostname/stats
2763                 done
2764                 error "OST not keeping write_bytes stats (b22312)"
2765         fi
2766 }
2767 run_test 33c "test llobdstat and write_bytes"
2768
2769 test_33d() {
2770         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2772         local MDTIDX=1
2773         local remote_dir=$DIR/$tdir/remote_dir
2774
2775         test_mkdir -p $DIR/$tdir
2776         $LFS mkdir -i $MDTIDX $remote_dir ||
2777                 error "create remote directory failed"
2778
2779         touch $remote_dir/$tfile
2780         chmod 444 $remote_dir/$tfile
2781         chown $RUNAS_ID $remote_dir/$tfile
2782
2783         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2784
2785         chown $RUNAS_ID $remote_dir
2786         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2787                                         error "create" || true
2788         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2789                                     error "open RDWR" || true
2790         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2791 }
2792 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2793
2794 test_33e() {
2795         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2796
2797         mkdir $DIR/$tdir
2798
2799         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2800         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2801         mkdir $DIR/$tdir/local_dir
2802
2803         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2804         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2805         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2806
2807         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2808                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2809
2810         rmdir $DIR/$tdir/* || error "rmdir failed"
2811
2812         umask 777
2813         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2814         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2815         mkdir $DIR/$tdir/local_dir
2816
2817         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2818         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2819         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2820
2821         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2822                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2823
2824         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2825
2826         umask 000
2827         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2828         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2829         mkdir $DIR/$tdir/local_dir
2830
2831         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2832         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2833         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2834
2835         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2836                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2837 }
2838 run_test 33e "mkdir and striped directory should have same mode"
2839
2840 cleanup_33f() {
2841         trap 0
2842         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2843 }
2844
2845 test_33f() {
2846         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2847         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2848
2849         mkdir $DIR/$tdir
2850         chmod go+rwx $DIR/$tdir
2851         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2852         trap cleanup_33f EXIT
2853
2854         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2855                 error "cannot create striped directory"
2856
2857         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2858                 error "cannot create files in striped directory"
2859
2860         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2861                 error "cannot remove files in striped directory"
2862
2863         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2864                 error "cannot remove striped directory"
2865
2866         cleanup_33f
2867 }
2868 run_test 33f "nonroot user can create, access, and remove a striped directory"
2869
2870 test_33g() {
2871         mkdir -p $DIR/$tdir/dir2
2872
2873         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
2874         echo $err
2875         [[ $err =~ "exists" ]] || error "Not exists error"
2876 }
2877 run_test 33g "nonroot user create already existing root created file"
2878
2879 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2880 test_34a() {
2881         rm -f $DIR/f34
2882         $MCREATE $DIR/f34 || error
2883         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2884         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2885         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2886         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2887 }
2888 run_test 34a "truncate file that has not been opened ==========="
2889
2890 test_34b() {
2891         [ ! -f $DIR/f34 ] && test_34a
2892         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2893         $OPENFILE -f O_RDONLY $DIR/f34
2894         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2895         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2896 }
2897 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2898
2899 test_34c() {
2900         [ ! -f $DIR/f34 ] && test_34a
2901         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2902         $OPENFILE -f O_RDWR $DIR/f34
2903         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2904         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2905 }
2906 run_test 34c "O_RDWR opening file-with-size works =============="
2907
2908 test_34d() {
2909         [ ! -f $DIR/f34 ] && test_34a
2910         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2911         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2912         rm $DIR/f34
2913 }
2914 run_test 34d "write to sparse file ============================="
2915
2916 test_34e() {
2917         rm -f $DIR/f34e
2918         $MCREATE $DIR/f34e || error
2919         $TRUNCATE $DIR/f34e 1000 || error
2920         $CHECKSTAT -s 1000 $DIR/f34e || error
2921         $OPENFILE -f O_RDWR $DIR/f34e
2922         $CHECKSTAT -s 1000 $DIR/f34e || error
2923 }
2924 run_test 34e "create objects, some with size and some without =="
2925
2926 test_34f() { # bug 6242, 6243
2927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2928         SIZE34F=48000
2929         rm -f $DIR/f34f
2930         $MCREATE $DIR/f34f || error
2931         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2932         dd if=$DIR/f34f of=$TMP/f34f
2933         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2934         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2935         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2936         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2937         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2938 }
2939 run_test 34f "read from a file with no objects until EOF ======="
2940
2941 test_34g() {
2942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2943         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2944         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2945         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2946         cancel_lru_locks osc
2947         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2948                 error "wrong size after lock cancel"
2949
2950         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2951         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2952                 error "expanding truncate failed"
2953         cancel_lru_locks osc
2954         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2955                 error "wrong expanded size after lock cancel"
2956 }
2957 run_test 34g "truncate long file ==============================="
2958
2959 test_34h() {
2960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2961         local gid=10
2962         local sz=1000
2963
2964         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2965         sync # Flush the cache so that multiop below does not block on cache
2966              # flush when getting the group lock
2967         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2968         MULTIPID=$!
2969
2970         # Since just timed wait is not good enough, let's do a sync write
2971         # that way we are sure enough time for a roundtrip + processing
2972         # passed + 2 seconds of extra margin.
2973         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2974         rm $DIR/${tfile}-1
2975         sleep 2
2976
2977         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2978                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2979                 kill -9 $MULTIPID
2980         fi
2981         wait $MULTIPID
2982         local nsz=`stat -c %s $DIR/$tfile`
2983         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2984 }
2985 run_test 34h "ftruncate file under grouplock should not block"
2986
2987 test_35a() {
2988         cp /bin/sh $DIR/f35a
2989         chmod 444 $DIR/f35a
2990         chown $RUNAS_ID $DIR/f35a
2991         $RUNAS $DIR/f35a && error || true
2992         rm $DIR/f35a
2993 }
2994 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2995
2996 test_36a() {
2997         rm -f $DIR/f36
2998         utime $DIR/f36 || error
2999 }
3000 run_test 36a "MDS utime check (mknod, utime) ==================="
3001
3002 test_36b() {
3003         echo "" > $DIR/f36
3004         utime $DIR/f36 || error
3005 }
3006 run_test 36b "OST utime check (open, utime) ===================="
3007
3008 test_36c() {
3009         rm -f $DIR/d36/f36
3010         test_mkdir $DIR/d36
3011         chown $RUNAS_ID $DIR/d36
3012         $RUNAS utime $DIR/d36/f36 || error
3013 }
3014 run_test 36c "non-root MDS utime check (mknod, utime) =========="
3015
3016 test_36d() {
3017         [ ! -d $DIR/d36 ] && test_36c
3018         echo "" > $DIR/d36/f36
3019         $RUNAS utime $DIR/d36/f36 || error
3020 }
3021 run_test 36d "non-root OST utime check (open, utime) ==========="
3022
3023 test_36e() {
3024         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3025         test_mkdir -p $DIR/$tdir
3026         touch $DIR/$tdir/$tfile
3027         $RUNAS utime $DIR/$tdir/$tfile && \
3028                 error "utime worked, expected failure" || true
3029 }
3030 run_test 36e "utime on non-owned file (should return error) ===="
3031
3032 subr_36fh() {
3033         local fl="$1"
3034         local LANG_SAVE=$LANG
3035         local LC_LANG_SAVE=$LC_LANG
3036         export LANG=C LC_LANG=C # for date language
3037
3038         DATESTR="Dec 20  2000"
3039         test_mkdir -p $DIR/$tdir
3040         lctl set_param fail_loc=$fl
3041         date; date +%s
3042         cp /etc/hosts $DIR/$tdir/$tfile
3043         sync & # write RPC generated with "current" inode timestamp, but delayed
3044         sleep 1
3045         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3046         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3047         cancel_lru_locks osc
3048         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3049         date; date +%s
3050         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3051                 echo "BEFORE: $LS_BEFORE" && \
3052                 echo "AFTER : $LS_AFTER" && \
3053                 echo "WANT  : $DATESTR" && \
3054                 error "$DIR/$tdir/$tfile timestamps changed" || true
3055
3056         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3057 }
3058
3059 test_36f() {
3060         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3061         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3062         subr_36fh "0x80000214"
3063 }
3064 run_test 36f "utime on file racing with OST BRW write =========="
3065
3066 test_36g() {
3067         remote_ost_nodsh && skip "remote OST with nodsh" && return
3068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3069         local fmd_max_age
3070         local fmd_before
3071         local fmd_after
3072
3073         test_mkdir -p $DIR/$tdir
3074         fmd_max_age=$(do_facet ost1 \
3075                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3076                 head -n 1")
3077
3078         fmd_before=$(do_facet ost1 \
3079                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3080         touch $DIR/$tdir/$tfile
3081         sleep $((fmd_max_age + 12))
3082         fmd_after=$(do_facet ost1 \
3083                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3084
3085         echo "fmd_before: $fmd_before"
3086         echo "fmd_after: $fmd_after"
3087         [[ $fmd_after -gt $fmd_before ]] &&
3088                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3089                 error "fmd didn't expire after ping" || true
3090 }
3091 run_test 36g "filter mod data cache expiry ====================="
3092
3093 test_36h() {
3094         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3095         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3096         subr_36fh "0x80000227"
3097 }
3098 run_test 36h "utime on file racing with OST BRW write =========="
3099
3100 test_36i() {
3101         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3102
3103         test_mkdir $DIR/$tdir
3104         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3105
3106         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3107         local new_mtime=$((mtime + 200))
3108
3109         #change Modify time of striped dir
3110         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3111                         error "change mtime failed"
3112
3113         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3114
3115         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3116 }
3117 run_test 36i "change mtime on striped directory"
3118
3119 # test_37 - duplicate with tests 32q 32r
3120
3121 test_38() {
3122         local file=$DIR/$tfile
3123         touch $file
3124         openfile -f O_DIRECTORY $file
3125         local RC=$?
3126         local ENOTDIR=20
3127         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3128         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3129 }
3130 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3131
3132 test_39a() { # was test_39
3133         touch $DIR/$tfile
3134         touch $DIR/${tfile}2
3135 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3136 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3137 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3138         sleep 2
3139         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3140         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3141                 echo "mtime"
3142                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3143                 echo "atime"
3144                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3145                 echo "ctime"
3146                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3147                 error "O_TRUNC didn't change timestamps"
3148         fi
3149 }
3150 run_test 39a "mtime changed on create ==========================="
3151
3152 test_39b() {
3153         test_mkdir -p -c1 $DIR/$tdir
3154         cp -p /etc/passwd $DIR/$tdir/fopen
3155         cp -p /etc/passwd $DIR/$tdir/flink
3156         cp -p /etc/passwd $DIR/$tdir/funlink
3157         cp -p /etc/passwd $DIR/$tdir/frename
3158         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3159
3160         sleep 1
3161         echo "aaaaaa" >> $DIR/$tdir/fopen
3162         echo "aaaaaa" >> $DIR/$tdir/flink
3163         echo "aaaaaa" >> $DIR/$tdir/funlink
3164         echo "aaaaaa" >> $DIR/$tdir/frename
3165
3166         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3167         local link_new=`stat -c %Y $DIR/$tdir/flink`
3168         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3169         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3170
3171         cat $DIR/$tdir/fopen > /dev/null
3172         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3173         rm -f $DIR/$tdir/funlink2
3174         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3175
3176         for (( i=0; i < 2; i++ )) ; do
3177                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3178                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3179                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3180                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3181
3182                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3183                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3184                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3185                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3186
3187                 cancel_lru_locks osc
3188                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3189         done
3190 }
3191 run_test 39b "mtime change on open, link, unlink, rename  ======"
3192
3193 # this should be set to past
3194 TEST_39_MTIME=`date -d "1 year ago" +%s`
3195
3196 # bug 11063
3197 test_39c() {
3198         touch $DIR1/$tfile
3199         sleep 2
3200         local mtime0=`stat -c %Y $DIR1/$tfile`
3201
3202         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3203         local mtime1=`stat -c %Y $DIR1/$tfile`
3204         [ "$mtime1" = $TEST_39_MTIME ] || \
3205                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3206
3207         local d1=`date +%s`
3208         echo hello >> $DIR1/$tfile
3209         local d2=`date +%s`
3210         local mtime2=`stat -c %Y $DIR1/$tfile`
3211         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3212                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3213
3214         mv $DIR1/$tfile $DIR1/$tfile-1
3215
3216         for (( i=0; i < 2; i++ )) ; do
3217                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3218                 [ "$mtime2" = "$mtime3" ] || \
3219                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3220
3221                 cancel_lru_locks osc
3222                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3223         done
3224 }
3225 run_test 39c "mtime change on rename ==========================="
3226
3227 # bug 21114
3228 test_39d() {
3229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3230         touch $DIR1/$tfile
3231
3232         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3233
3234         for (( i=0; i < 2; i++ )) ; do
3235                 local mtime=`stat -c %Y $DIR1/$tfile`
3236                 [ $mtime = $TEST_39_MTIME ] || \
3237                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3238
3239                 cancel_lru_locks osc
3240                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3241         done
3242 }
3243 run_test 39d "create, utime, stat =============================="
3244
3245 # bug 21114
3246 test_39e() {
3247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3248         touch $DIR1/$tfile
3249         local mtime1=`stat -c %Y $DIR1/$tfile`
3250
3251         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3252
3253         for (( i=0; i < 2; i++ )) ; do
3254                 local mtime2=`stat -c %Y $DIR1/$tfile`
3255                 [ $mtime2 = $TEST_39_MTIME ] || \
3256                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3257
3258                 cancel_lru_locks osc
3259                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3260         done
3261 }
3262 run_test 39e "create, stat, utime, stat ========================"
3263
3264 # bug 21114
3265 test_39f() {
3266         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3267         touch $DIR1/$tfile
3268         mtime1=`stat -c %Y $DIR1/$tfile`
3269
3270         sleep 2
3271         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3272
3273         for (( i=0; i < 2; i++ )) ; do
3274                 local mtime2=`stat -c %Y $DIR1/$tfile`
3275                 [ $mtime2 = $TEST_39_MTIME ] || \
3276                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3277
3278                 cancel_lru_locks osc
3279                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3280         done
3281 }
3282 run_test 39f "create, stat, sleep, utime, stat ================="
3283
3284 # bug 11063
3285 test_39g() {
3286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3287         echo hello >> $DIR1/$tfile
3288         local mtime1=`stat -c %Y $DIR1/$tfile`
3289
3290         sleep 2
3291         chmod o+r $DIR1/$tfile
3292
3293         for (( i=0; i < 2; i++ )) ; do
3294                 local mtime2=`stat -c %Y $DIR1/$tfile`
3295                 [ "$mtime1" = "$mtime2" ] || \
3296                         error "lost mtime: $mtime2, should be $mtime1"
3297
3298                 cancel_lru_locks osc
3299                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3300         done
3301 }
3302 run_test 39g "write, chmod, stat ==============================="
3303
3304 # bug 11063
3305 test_39h() {
3306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3307         touch $DIR1/$tfile
3308         sleep 1
3309
3310         local d1=`date`
3311         echo hello >> $DIR1/$tfile
3312         local mtime1=`stat -c %Y $DIR1/$tfile`
3313
3314         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3315         local d2=`date`
3316         if [ "$d1" != "$d2" ]; then
3317                 echo "write and touch not within one second"
3318         else
3319                 for (( i=0; i < 2; i++ )) ; do
3320                         local mtime2=`stat -c %Y $DIR1/$tfile`
3321                         [ "$mtime2" = $TEST_39_MTIME ] || \
3322                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3323
3324                         cancel_lru_locks osc
3325                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3326                 done
3327         fi
3328 }
3329 run_test 39h "write, utime within one second, stat ============="
3330
3331 test_39i() {
3332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3333         touch $DIR1/$tfile
3334         sleep 1
3335
3336         echo hello >> $DIR1/$tfile
3337         local mtime1=`stat -c %Y $DIR1/$tfile`
3338
3339         mv $DIR1/$tfile $DIR1/$tfile-1
3340
3341         for (( i=0; i < 2; i++ )) ; do
3342                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3343
3344                 [ "$mtime1" = "$mtime2" ] || \
3345                         error "lost mtime: $mtime2, should be $mtime1"
3346
3347                 cancel_lru_locks osc
3348                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3349         done
3350 }
3351 run_test 39i "write, rename, stat =============================="
3352
3353 test_39j() {
3354         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3355         start_full_debug_logging
3356         touch $DIR1/$tfile
3357         sleep 1
3358
3359         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3360         lctl set_param fail_loc=0x80000412
3361         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3362                 error "multiop failed"
3363         local multipid=$!
3364         local mtime1=`stat -c %Y $DIR1/$tfile`
3365
3366         mv $DIR1/$tfile $DIR1/$tfile-1
3367
3368         kill -USR1 $multipid
3369         wait $multipid || error "multiop close failed"
3370
3371         for (( i=0; i < 2; i++ )) ; do
3372                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3373                 [ "$mtime1" = "$mtime2" ] ||
3374                         error "mtime is lost on close: $mtime2, " \
3375                               "should be $mtime1"
3376
3377                 cancel_lru_locks osc
3378                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3379         done
3380         lctl set_param fail_loc=0
3381         stop_full_debug_logging
3382 }
3383 run_test 39j "write, rename, close, stat ======================="
3384
3385 test_39k() {
3386         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3387         touch $DIR1/$tfile
3388         sleep 1
3389
3390         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3391         local multipid=$!
3392         local mtime1=`stat -c %Y $DIR1/$tfile`
3393
3394         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3395
3396         kill -USR1 $multipid
3397         wait $multipid || error "multiop close failed"
3398
3399         for (( i=0; i < 2; i++ )) ; do
3400                 local mtime2=`stat -c %Y $DIR1/$tfile`
3401
3402                 [ "$mtime2" = $TEST_39_MTIME ] || \
3403                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3404
3405                 cancel_lru_locks osc
3406                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3407         done
3408 }
3409 run_test 39k "write, utime, close, stat ========================"
3410
3411 # this should be set to future
3412 TEST_39_ATIME=`date -d "1 year" +%s`
3413
3414 test_39l() {
3415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3416         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3417         local atime_diff=$(do_facet $SINGLEMDS \
3418                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3419         rm -rf $DIR/$tdir
3420         mkdir -p $DIR/$tdir
3421
3422         # test setting directory atime to future
3423         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3424         local atime=$(stat -c %X $DIR/$tdir)
3425         [ "$atime" = $TEST_39_ATIME ] ||
3426                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3427
3428         # test setting directory atime from future to now
3429         local now=$(date +%s)
3430         touch -a -d @$now $DIR/$tdir
3431
3432         atime=$(stat -c %X $DIR/$tdir)
3433         [ "$atime" -eq "$now"  ] ||
3434                 error "atime is not updated from future: $atime, $now"
3435
3436         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3437         sleep 3
3438
3439         # test setting directory atime when now > dir atime + atime_diff
3440         local d1=$(date +%s)
3441         ls $DIR/$tdir
3442         local d2=$(date +%s)
3443         cancel_lru_locks mdc
3444         atime=$(stat -c %X $DIR/$tdir)
3445         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3446                 error "atime is not updated  : $atime, should be $d2"
3447
3448         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3449         sleep 3
3450
3451         # test not setting directory atime when now < dir atime + atime_diff
3452         ls $DIR/$tdir
3453         cancel_lru_locks mdc
3454         atime=$(stat -c %X $DIR/$tdir)
3455         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3456                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3457
3458         do_facet $SINGLEMDS \
3459                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3460 }
3461 run_test 39l "directory atime update ==========================="
3462
3463 test_39m() {
3464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3465         touch $DIR1/$tfile
3466         sleep 2
3467         local far_past_mtime=$(date -d "May 29 1953" +%s)
3468         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3469
3470         touch -m -d @$far_past_mtime $DIR1/$tfile
3471         touch -a -d @$far_past_atime $DIR1/$tfile
3472
3473         for (( i=0; i < 2; i++ )) ; do
3474                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3475                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3476                         error "atime or mtime set incorrectly"
3477
3478                 cancel_lru_locks osc
3479                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3480         done
3481 }
3482 run_test 39m "test atime and mtime before 1970"
3483
3484 test_39n() { # LU-3832
3485         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3486         local atime_diff=$(do_facet $SINGLEMDS \
3487                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3488         local atime0
3489         local atime1
3490         local atime2
3491
3492         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3493
3494         rm -rf $DIR/$tfile
3495         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3496         atime0=$(stat -c %X $DIR/$tfile)
3497
3498         sleep 5
3499         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3500         atime1=$(stat -c %X $DIR/$tfile)
3501
3502         sleep 5
3503         cancel_lru_locks mdc
3504         cancel_lru_locks osc
3505         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3506         atime2=$(stat -c %X $DIR/$tfile)
3507
3508         do_facet $SINGLEMDS \
3509                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3510
3511         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3512         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3513 }
3514 run_test 39n "check that O_NOATIME is honored"
3515
3516 test_39o() {
3517         TESTDIR=$DIR/$tdir/$tfile
3518         [ -e $TESTDIR ] && rm -rf $TESTDIR
3519         mkdir -p $TESTDIR
3520         cd $TESTDIR
3521         links1=2
3522         ls
3523         mkdir a b
3524         ls
3525         links2=$(stat -c %h .)
3526         [ $(($links1 + 2)) != $links2 ] &&
3527                 error "wrong links count $(($links1 + 2)) != $links2"
3528         rmdir b
3529         links3=$(stat -c %h .)
3530         [ $(($links1 + 1)) != $links3 ] &&
3531                 error "wrong links count $links1 != $links3"
3532         return 0
3533 }
3534 run_test 39o "directory cached attributes updated after create ========"
3535
3536 test_39p() {
3537         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3538         local MDTIDX=1
3539         TESTDIR=$DIR/$tdir/$tfile
3540         [ -e $TESTDIR ] && rm -rf $TESTDIR
3541         test_mkdir -p $TESTDIR
3542         cd $TESTDIR
3543         links1=2
3544         ls
3545         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3546         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3547         ls
3548         links2=$(stat -c %h .)
3549         [ $(($links1 + 2)) != $links2 ] &&
3550                 error "wrong links count $(($links1 + 2)) != $links2"
3551         rmdir remote_dir2
3552         links3=$(stat -c %h .)
3553         [ $(($links1 + 1)) != $links3 ] &&
3554                 error "wrong links count $links1 != $links3"
3555         return 0
3556 }
3557 run_test 39p "remote directory cached attributes updated after create ========"
3558
3559
3560 test_39q() { # LU-8041
3561         local testdir=$DIR/$tdir
3562         mkdir -p $testdir
3563         multiop_bg_pause $testdir D_c || error "multiop failed"
3564         local multipid=$!
3565         cancel_lru_locks mdc
3566         kill -USR1 $multipid
3567         local atime=$(stat -c %X $testdir)
3568         [ "$atime" -ne 0 ] || error "atime is zero"
3569 }
3570 run_test 39q "close won't zero out atime"
3571
3572 test_40() {
3573         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3574         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3575                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3576         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3577                 error "$tfile is not 4096 bytes in size"
3578 }
3579 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3580
3581 test_41() {
3582         # bug 1553
3583         small_write $DIR/f41 18
3584 }
3585 run_test 41 "test small file write + fstat ====================="
3586
3587 count_ost_writes() {
3588         lctl get_param -n osc.*.stats |
3589                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3590                         END { printf("%0.0f", writes) }'
3591 }
3592
3593 # decent default
3594 WRITEBACK_SAVE=500
3595 DIRTY_RATIO_SAVE=40
3596 MAX_DIRTY_RATIO=50
3597 BG_DIRTY_RATIO_SAVE=10
3598 MAX_BG_DIRTY_RATIO=25
3599
3600 start_writeback() {
3601         trap 0
3602         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3603         # dirty_ratio, dirty_background_ratio
3604         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3605                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3606                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3607                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3608         else
3609                 # if file not here, we are a 2.4 kernel
3610                 kill -CONT `pidof kupdated`
3611         fi
3612 }
3613
3614 stop_writeback() {
3615         # setup the trap first, so someone cannot exit the test at the
3616         # exact wrong time and mess up a machine
3617         trap start_writeback EXIT
3618         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3619         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3620                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3621                 sysctl -w vm.dirty_writeback_centisecs=0
3622                 sysctl -w vm.dirty_writeback_centisecs=0
3623                 # save and increase /proc/sys/vm/dirty_ratio
3624                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3625                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3626                 # save and increase /proc/sys/vm/dirty_background_ratio
3627                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3628                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3629         else
3630                 # if file not here, we are a 2.4 kernel
3631                 kill -STOP `pidof kupdated`
3632         fi
3633 }
3634
3635 # ensure that all stripes have some grant before we test client-side cache
3636 setup_test42() {
3637         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3638                 dd if=/dev/zero of=$i bs=4k count=1
3639                 rm $i
3640         done
3641 }
3642
3643 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3644 # file truncation, and file removal.
3645 test_42a() {
3646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3647         setup_test42
3648         cancel_lru_locks osc
3649         stop_writeback
3650         sync; sleep 1; sync # just to be safe
3651         BEFOREWRITES=`count_ost_writes`
3652         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3653         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3654         AFTERWRITES=`count_ost_writes`
3655         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3656                 error "$BEFOREWRITES < $AFTERWRITES"
3657         start_writeback
3658 }
3659 run_test 42a "ensure that we don't flush on close"
3660
3661 test_42b() {
3662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3663         setup_test42
3664         cancel_lru_locks osc
3665         stop_writeback
3666         sync
3667         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3668         BEFOREWRITES=$(count_ost_writes)
3669         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3670         AFTERWRITES=$(count_ost_writes)
3671         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3672                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3673         fi
3674         BEFOREWRITES=$(count_ost_writes)
3675         sync || error "sync: $?"
3676         AFTERWRITES=$(count_ost_writes)
3677         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3678                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3679         fi
3680         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3681         start_writeback
3682         return 0
3683 }
3684 run_test 42b "test destroy of file with cached dirty data ======"
3685
3686 # if these tests just want to test the effect of truncation,
3687 # they have to be very careful.  consider:
3688 # - the first open gets a {0,EOF}PR lock
3689 # - the first write conflicts and gets a {0, count-1}PW
3690 # - the rest of the writes are under {count,EOF}PW
3691 # - the open for truncate tries to match a {0,EOF}PR
3692 #   for the filesize and cancels the PWs.
3693 # any number of fixes (don't get {0,EOF} on open, match
3694 # composite locks, do smarter file size management) fix
3695 # this, but for now we want these tests to verify that
3696 # the cancellation with truncate intent works, so we
3697 # start the file with a full-file pw lock to match against
3698 # until the truncate.
3699 trunc_test() {
3700         test=$1
3701         file=$DIR/$test
3702         offset=$2
3703         cancel_lru_locks osc
3704         stop_writeback
3705         # prime the file with 0,EOF PW to match
3706         touch $file
3707         $TRUNCATE $file 0
3708         sync; sync
3709         # now the real test..
3710         dd if=/dev/zero of=$file bs=1024 count=100
3711         BEFOREWRITES=`count_ost_writes`
3712         $TRUNCATE $file $offset
3713         cancel_lru_locks osc
3714         AFTERWRITES=`count_ost_writes`
3715         start_writeback
3716 }
3717
3718 test_42c() {
3719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3720         trunc_test 42c 1024
3721         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3722             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3723         rm $file
3724 }
3725 run_test 42c "test partial truncate of file with cached dirty data"
3726
3727 test_42d() {
3728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3729         trunc_test 42d 0
3730         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3731             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3732         rm $file
3733 }
3734 run_test 42d "test complete truncate of file with cached dirty data"
3735
3736 test_42e() { # bug22074
3737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3738         local TDIR=$DIR/${tdir}e
3739         local pagesz=$(page_size)
3740         local pages=16 # hardcoded 16 pages, don't change it.
3741         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3742         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3743         local max_dirty_mb
3744         local warmup_files
3745
3746         test_mkdir -p $DIR/${tdir}e
3747         $SETSTRIPE -c 1 $TDIR
3748         createmany -o $TDIR/f $files
3749
3750         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3751
3752         # we assume that with $OSTCOUNT files, at least one of them will
3753         # be allocated on OST0.
3754         warmup_files=$((OSTCOUNT * max_dirty_mb))
3755         createmany -o $TDIR/w $warmup_files
3756
3757         # write a large amount of data into one file and sync, to get good
3758         # avail_grant number from OST.
3759         for ((i=0; i<$warmup_files; i++)); do
3760                 idx=$($GETSTRIPE -i $TDIR/w$i)
3761                 [ $idx -ne 0 ] && continue
3762                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3763                 break
3764         done
3765         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3766         sync
3767         $LCTL get_param $proc_osc0/cur_dirty_bytes
3768         $LCTL get_param $proc_osc0/cur_grant_bytes
3769
3770         # create as much dirty pages as we can while not to trigger the actual
3771         # RPCs directly. but depends on the env, VFS may trigger flush during this
3772         # period, hopefully we are good.
3773         for ((i=0; i<$warmup_files; i++)); do
3774                 idx=$($GETSTRIPE -i $TDIR/w$i)
3775                 [ $idx -ne 0 ] && continue
3776                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3777         done
3778         $LCTL get_param $proc_osc0/cur_dirty_bytes
3779         $LCTL get_param $proc_osc0/cur_grant_bytes
3780
3781         # perform the real test
3782         $LCTL set_param $proc_osc0/rpc_stats 0
3783         for ((;i<$files; i++)); do
3784                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3785                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3786         done
3787         sync
3788         $LCTL get_param $proc_osc0/rpc_stats
3789
3790         local percent=0
3791         local have_ppr=false
3792         $LCTL get_param $proc_osc0/rpc_stats |
3793                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3794                         # skip lines until we are at the RPC histogram data
3795                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3796                         $have_ppr || continue
3797
3798                         # we only want the percent stat for < 16 pages
3799                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3800
3801                         percent=$((percent + WPCT))
3802                         if [[ $percent -gt 15 ]]; then
3803                                 error "less than 16-pages write RPCs" \
3804                                       "$percent% > 15%"
3805                                 break
3806                         fi
3807                 done
3808         rm -rf $TDIR
3809 }
3810 run_test 42e "verify sub-RPC writes are not done synchronously"
3811
3812 test_43A() { # was test_43
3813         test_mkdir -p $DIR/$tdir
3814         cp -p /bin/ls $DIR/$tdir/$tfile
3815         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3816         pid=$!
3817         # give multiop a chance to open
3818         sleep 1
3819
3820         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
3821         kill -USR1 $pid
3822 }
3823 run_test 43A "execution of file opened for write should return -ETXTBSY"
3824
3825 test_43a() {
3826         test_mkdir $DIR/$tdir
3827         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3828                 cp -p multiop $DIR/$tdir/multiop
3829         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3830                 error "multiop open $TMP/$tfile.junk failed"
3831         MULTIOP_PID=$!
3832         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3833         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3834         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3835 }
3836 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3837
3838 test_43b() {
3839         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3840         test_mkdir $DIR/$tdir
3841         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3842                 cp -p multiop $DIR/$tdir/multiop
3843         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3844                 error "multiop open $TMP/$tfile.junk failed"
3845         MULTIOP_PID=$!
3846         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3847         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3848         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3849 }
3850 run_test 43b "truncate of file being executed should return -ETXTBSY"
3851
3852 test_43c() {
3853         local testdir="$DIR/$tdir"
3854         test_mkdir $testdir
3855         cp $SHELL $testdir/
3856         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
3857                 ( cd $testdir && md5sum -c )
3858 }
3859 run_test 43c "md5sum of copy into lustre"
3860
3861 test_44A() { # was test_44
3862         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3863         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3864         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3865 }
3866 run_test 44A "zero length read from a sparse stripe"
3867
3868 test_44a() {
3869         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3870                 awk '{ print $2 }')
3871         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3872         [[ $nstripe -gt $OSTCOUNT ]] &&
3873             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3874             return
3875         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3876                 awk '{ print $2 }')
3877         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3878                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3879                         awk '{ print $2 }')
3880         fi
3881
3882         OFFSETS="0 $((stride/2)) $((stride-1))"
3883         for offset in $OFFSETS; do
3884                 for i in $(seq 0 $((nstripe-1))); do
3885                         local GLOBALOFFSETS=""
3886                         # size in Bytes
3887                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3888                         local myfn=$DIR/d44a-$size
3889                         echo "--------writing $myfn at $size"
3890                         ll_sparseness_write $myfn $size ||
3891                                 error "ll_sparseness_write"
3892                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3893                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3894                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3895
3896                         for j in $(seq 0 $((nstripe-1))); do
3897                                 # size in Bytes
3898                                 size=$((((j + $nstripe )*$stride + $offset)))
3899                                 ll_sparseness_write $myfn $size ||
3900                                         error "ll_sparseness_write"
3901                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3902                         done
3903                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3904                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3905                         rm -f $myfn
3906                 done
3907         done
3908 }
3909 run_test 44a "test sparse pwrite ==============================="
3910
3911 dirty_osc_total() {
3912         tot=0
3913         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3914                 tot=$(($tot + $d))
3915         done
3916         echo $tot
3917 }
3918 do_dirty_record() {
3919         before=`dirty_osc_total`
3920         echo executing "\"$*\""
3921         eval $*
3922         after=`dirty_osc_total`
3923         echo before $before, after $after
3924 }
3925 test_45() {
3926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3927         f="$DIR/f45"
3928         # Obtain grants from OST if it supports it
3929         echo blah > ${f}_grant
3930         stop_writeback
3931         sync
3932         do_dirty_record "echo blah > $f"
3933         [[ $before -eq $after ]] && error "write wasn't cached"
3934         do_dirty_record "> $f"
3935         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3936         do_dirty_record "echo blah > $f"
3937         [[ $before -eq $after ]] && error "write wasn't cached"
3938         do_dirty_record "sync"
3939         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3940         do_dirty_record "echo blah > $f"
3941         [[ $before -eq $after ]] && error "write wasn't cached"
3942         do_dirty_record "cancel_lru_locks osc"
3943         [[ $before -gt $after ]] ||
3944                 error "lock cancellation didn't lower dirty count"
3945         start_writeback
3946 }
3947 run_test 45 "osc io page accounting ============================"
3948
3949 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3950 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3951 # objects offset and an assert hit when an rpc was built with 1023's mapped
3952 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3953 test_46() {
3954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3955         f="$DIR/f46"
3956         stop_writeback
3957         sync
3958         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3959         sync
3960         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3961         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3962         sync
3963         start_writeback
3964 }
3965 run_test 46 "dirtying a previously written page ================"
3966
3967 # test_47 is removed "Device nodes check" is moved to test_28
3968
3969 test_48a() { # bug 2399
3970         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3971         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3972                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3973                 return
3974         test_mkdir $DIR/$tdir
3975         cd $DIR/$tdir
3976         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3977         test_mkdir $DIR/$tdir || error "recreate directory failed"
3978         touch foo || error "'touch foo' failed after recreating cwd"
3979         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
3980         touch .foo || error "'touch .foo' failed after recreating cwd"
3981         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3982         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3983         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3984         cd . || error "'cd .' failed after recreating cwd"
3985         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3986         rmdir . && error "'rmdir .' worked after recreating cwd"
3987         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3988         cd .. || error "'cd ..' failed after recreating cwd"
3989 }
3990 run_test 48a "Access renamed working dir (should return errors)="
3991
3992 test_48b() { # bug 2399
3993         rm -rf $DIR/$tdir
3994         test_mkdir $DIR/$tdir
3995         cd $DIR/$tdir
3996         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3997         touch foo && error "'touch foo' worked after removing cwd"
3998         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3999         touch .foo && error "'touch .foo' worked after removing cwd"
4000         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4001         ls . > /dev/null && error "'ls .' worked after removing cwd"
4002         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4003         test_mkdir . && error "'mkdir .' worked after removing cwd"
4004         rmdir . && error "'rmdir .' worked after removing cwd"
4005         ln -s . foo && error "'ln -s .' worked after removing cwd"
4006         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4007 }
4008 run_test 48b "Access removed working dir (should return errors)="
4009
4010 test_48c() { # bug 2350
4011         #lctl set_param debug=-1
4012         #set -vx
4013         rm -rf $DIR/$tdir
4014         test_mkdir -p $DIR/$tdir/dir
4015         cd $DIR/$tdir/dir
4016         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4017         $TRACE touch foo && error "touch foo worked after removing cwd"
4018         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
4019         touch .foo && error "touch .foo worked after removing cwd"
4020         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
4021         $TRACE ls . && error "'ls .' worked after removing cwd"
4022         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4023         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
4024         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4025         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4026         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4027 }
4028 run_test 48c "Access removed working subdir (should return errors)"
4029
4030 test_48d() { # bug 2350
4031         #lctl set_param debug=-1
4032         #set -vx
4033         rm -rf $DIR/$tdir
4034         test_mkdir -p $DIR/$tdir/dir
4035         cd $DIR/$tdir/dir
4036         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4037         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4038         $TRACE touch foo && error "'touch foo' worked after removing parent"
4039         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
4040         touch .foo && error "'touch .foo' worked after removing parent"
4041         test_mkdir .foo && error "mkdir .foo worked after removing parent"
4042         $TRACE ls . && error "'ls .' worked after removing parent"
4043         $TRACE ls .. && error "'ls ..' worked after removing parent"
4044         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
4045         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4046         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4047         true
4048 }
4049 run_test 48d "Access removed parent subdir (should return errors)"
4050
4051 test_48e() { # bug 4134
4052         #lctl set_param debug=-1
4053         #set -vx
4054         rm -rf $DIR/$tdir
4055         test_mkdir -p $DIR/$tdir/dir
4056         cd $DIR/$tdir/dir
4057         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4058         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4059         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4060         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4061         # On a buggy kernel addition of "touch foo" after cd .. will
4062         # produce kernel oops in lookup_hash_it
4063         touch ../foo && error "'cd ..' worked after recreate parent"
4064         cd $DIR
4065         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4066 }
4067 run_test 48e "Access to recreated parent subdir (should return errors)"
4068
4069 test_49() { # LU-1030
4070         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4071         remote_ost_nodsh && skip "remote OST with nodsh" && return
4072         # get ost1 size - lustre-OST0000
4073         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4074                 awk '{ print $4 }')
4075         # write 800M at maximum
4076         [[ $ost1_size -lt 2 ]] && ost1_size=2
4077         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4078
4079         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4080         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4081         local dd_pid=$!
4082
4083         # change max_pages_per_rpc while writing the file
4084         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4085         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4086         # loop until dd process exits
4087         while ps ax -opid | grep -wq $dd_pid; do
4088                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4089                 sleep $((RANDOM % 5 + 1))
4090         done
4091         # restore original max_pages_per_rpc
4092         $LCTL set_param $osc1_mppc=$orig_mppc
4093         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4094 }
4095 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4096
4097 test_50() {
4098         # bug 1485
4099         test_mkdir $DIR/$tdir
4100         cd $DIR/$tdir
4101         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4102 }
4103 run_test 50 "special situations: /proc symlinks  ==============="
4104
4105 test_51a() {    # was test_51
4106         # bug 1516 - create an empty entry right after ".." then split dir
4107         test_mkdir -c1 $DIR/$tdir
4108         touch $DIR/$tdir/foo
4109         $MCREATE $DIR/$tdir/bar
4110         rm $DIR/$tdir/foo
4111         createmany -m $DIR/$tdir/longfile 201
4112         FNUM=202
4113         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4114                 $MCREATE $DIR/$tdir/longfile$FNUM
4115                 FNUM=$(($FNUM + 1))
4116                 echo -n "+"
4117         done
4118         echo
4119         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4120 }
4121 run_test 51a "special situations: split htree with empty entry =="
4122
4123 cleanup_print_lfs_df () {
4124         trap 0
4125         $LFS df
4126         $LFS df -i
4127 }
4128
4129 test_51b() {
4130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4131         local dir=$DIR/$tdir
4132
4133         local nrdirs=$((65536 + 100))
4134
4135         # cleanup the directory
4136         rm -fr $dir
4137
4138         test_mkdir -p -c1 $dir
4139
4140         $LFS df
4141         $LFS df -i
4142         local mdtidx=$(printf "%04x" $($LFS getstripe -M $dir))
4143         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4144         [[ $numfree -lt $nrdirs ]] &&
4145                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4146                 return
4147
4148         # need to check free space for the directories as well
4149         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4150         numfree=$(( blkfree / $(fs_inode_ksize) ))
4151         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)" &&
4152                 return
4153
4154         trap cleanup_print_lfsdf EXIT
4155
4156         # create files
4157         createmany -d $dir/d $nrdirs ||
4158                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4159
4160         # really created :
4161         nrdirs=$(ls -U $dir | wc -l)
4162
4163         # unlink all but 100 subdirectories, then check it still works
4164         local left=100
4165         local delete=$((nrdirs - left))
4166
4167         $LFS df
4168         $LFS df -i
4169
4170         # for ldiskfs the nlink count should be 1, but this is OSD specific
4171         # and so this is listed for informational purposes only
4172         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4173         unlinkmany -d $dir/d $delete ||
4174                 error "unlink of first $delete subdirs failed"
4175
4176         echo "nlink between: $(stat -c %h $dir)"
4177         local found=$(ls -U $dir | wc -l)
4178         [ $found -ne $left ] &&
4179                 error "can't find subdirs: found only $found, expected $left"
4180
4181         unlinkmany -d $dir/d $delete $left ||
4182                 error "unlink of second $left subdirs failed"
4183         # regardless of whether the backing filesystem tracks nlink accurately
4184         # or not, the nlink count shouldn't be more than "." and ".." here
4185         local after=$(stat -c %h $dir)
4186         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4187                 echo "nlink after: $after"
4188
4189         cleanup_print_lfs_df
4190 }
4191 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4192
4193 test_51d() {
4194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4195         [[ $OSTCOUNT -lt 3 ]] &&
4196                 skip_env "skipping test with few OSTs" && return
4197         test_mkdir -p $DIR/$tdir
4198         createmany -o $DIR/$tdir/t- 1000
4199         $GETSTRIPE $DIR/$tdir > $TMP/$tfile
4200         for N in $(seq 0 $((OSTCOUNT - 1))); do
4201                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4202                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4203                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4204                         '($1 == '$N') { objs += 1 } \
4205                         END { printf("%0.0f", objs) }')
4206                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4207         done
4208         unlinkmany $DIR/$tdir/t- 1000
4209
4210         NLAST=0
4211         for N in $(seq 1 $((OSTCOUNT - 1))); do
4212                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4213                         error "OST $N has less objects vs OST $NLAST" \
4214                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4215                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4216                         error "OST $N has less objects vs OST $NLAST" \
4217                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4218
4219                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4220                         error "OST $N has less #0 objects vs OST $NLAST" \
4221                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4222                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4223                         error "OST $N has less #0 objects vs OST $NLAST" \
4224                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4225                 NLAST=$N
4226         done
4227         rm -f $TMP/$tfile
4228 }
4229 run_test 51d "check object distribution"
4230
4231 test_51e() {
4232         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4233                 skip "Only applicable to ldiskfs-based MDTs"
4234                 return
4235         fi
4236
4237         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4238         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4239
4240         touch $DIR/$tdir/d0/foo
4241         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4242                 error "file exceed 65000 nlink limit!"
4243         unlinkmany $DIR/$tdir/d0/f- 65001
4244         return 0
4245 }
4246 run_test 51e "check file nlink limit"
4247
4248 test_51f() {
4249         test_mkdir $DIR/$tdir
4250
4251         local max=100000
4252         local ulimit_old=$(ulimit -n)
4253         local spare=20 # number of spare fd's for scripts/libraries, etc.
4254         local mdt=$(lfs getstripe -M $DIR/$tdir)
4255         local numfree=$(lfs df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4256
4257         echo "MDT$mdt numfree=$numfree, max=$max"
4258         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4259         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4260                 while ! ulimit -n $((numfree + spare)); do
4261                         numfree=$((numfree * 3 / 4))
4262                 done
4263                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4264         else
4265                 echo "left ulimit at $ulimit_old"
4266         fi
4267
4268         createmany -o -k -t 120 $DIR/$tdir/f $numfree ||
4269                 error "create+open $numfree files in $DIR/$tdir failed"
4270         ulimit -n $ulimit_old
4271
4272         # if createmany exits at 120s there will be fewer than $numfree files
4273         unlinkmany $DIR/$tdir/f $numfree || true
4274 }
4275 run_test 51f "check many open files limit"
4276
4277 test_52a() {
4278         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4279         test_mkdir -p $DIR/$tdir
4280         touch $DIR/$tdir/foo
4281         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4282         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4283         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4284         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4285         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4286                                         error "link worked"
4287         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4288         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4289         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4290                                                      error "lsattr"
4291         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4292         cp -r $DIR/$tdir $TMP/
4293         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4294 }
4295 run_test 52a "append-only flag test (should return errors)"
4296
4297 test_52b() {
4298         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4299         test_mkdir -p $DIR/$tdir
4300         touch $DIR/$tdir/foo
4301         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4302         cat test > $DIR/$tdir/foo && error "cat test worked"
4303         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4304         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4305         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4306                                         error "link worked"
4307         echo foo >> $DIR/$tdir/foo && error "echo worked"
4308         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4309         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4310         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4311         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4312                                                         error "lsattr"
4313         chattr -i $DIR/$tdir/foo || error "chattr failed"
4314
4315         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4316 }
4317 run_test 52b "immutable flag test (should return errors) ======="
4318
4319 test_53() {
4320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4321         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4322         remote_ost_nodsh && skip "remote OST with nodsh" && return
4323
4324         local param
4325         local param_seq
4326         local ostname
4327         local mds_last
4328         local mds_last_seq
4329         local ost_last
4330         local ost_last_seq
4331         local ost_last_id
4332         local ostnum
4333         local node
4334         local found=false
4335         local support_last_seq=true
4336
4337         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4338                 support_last_seq=false
4339
4340         # only test MDT0000
4341         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4342         local value
4343         for value in $(do_facet $SINGLEMDS \
4344                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4345                 param=$(echo ${value[0]} | cut -d "=" -f1)
4346                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4347
4348                 if $support_last_seq; then
4349                         param_seq=$(echo $param |
4350                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4351                         mds_last_seq=$(do_facet $SINGLEMDS \
4352                                        $LCTL get_param -n $param_seq)
4353                 fi
4354                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4355
4356                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4357                 node=$(facet_active_host ost$((ostnum+1)))
4358                 param="obdfilter.$ostname.last_id"
4359                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4360                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4361                         ost_last_id=$ost_last
4362
4363                         if $support_last_seq; then
4364                                 ost_last_id=$(echo $ost_last |
4365                                               awk -F':' '{print $2}' |
4366                                               sed -e "s/^0x//g")
4367                                 ost_last_seq=$(echo $ost_last |
4368                                                awk -F':' '{print $1}')
4369                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4370                         fi
4371
4372                         if [[ $ost_last_id != $mds_last ]]; then
4373                                 error "$ost_last_id != $mds_last"
4374                         else
4375                                 found=true
4376                                 break
4377                         fi
4378                 done
4379         done
4380         $found || error "can not match last_seq/last_id for $mdtosc"
4381         return 0
4382 }
4383 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4384
4385 test_54a() {
4386         $SOCKETSERVER $DIR/socket ||
4387                 error "$SOCKETSERVER $DIR/socket failed: $?"
4388         $SOCKETCLIENT $DIR/socket ||
4389                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4390         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4391 }
4392 run_test 54a "unix domain socket test =========================="
4393
4394 test_54b() {
4395         f="$DIR/f54b"
4396         mknod $f c 1 3
4397         chmod 0666 $f
4398         dd if=/dev/zero of=$f bs=$(page_size) count=1
4399 }
4400 run_test 54b "char device works in lustre ======================"
4401
4402 find_loop_dev() {
4403         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4404         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4405         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4406
4407         for i in $(seq 3 7); do
4408                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4409                 LOOPDEV=$LOOPBASE$i
4410                 LOOPNUM=$i
4411                 break
4412         done
4413 }
4414
4415 cleanup_54c() {
4416         local rc=0
4417         loopdev="$DIR/loop54c"
4418
4419         trap 0
4420         $UMOUNT $DIR/$tdir || rc=$?
4421         losetup -d $loopdev || true
4422         losetup -d $LOOPDEV || true
4423         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4424         return $rc
4425 }
4426
4427 test_54c() {
4428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4429         loopdev="$DIR/loop54c"
4430
4431         find_loop_dev
4432         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4433         trap cleanup_54c EXIT
4434         mknod $loopdev b 7 $LOOPNUM
4435         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4436         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4437         losetup $loopdev $DIR/$tfile ||
4438                 error "can't set up $loopdev for $DIR/$tfile"
4439         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4440         test_mkdir -p $DIR/$tdir
4441         mount -t ext2 $loopdev $DIR/$tdir ||
4442                 error "error mounting $loopdev on $DIR/$tdir"
4443         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4444                 error "dd write"
4445         df $DIR/$tdir
4446         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4447                 error "dd read"
4448         cleanup_54c
4449 }
4450 run_test 54c "block device works in lustre ====================="
4451
4452 test_54d() {
4453         f="$DIR/f54d"
4454         string="aaaaaa"
4455         mknod $f p
4456         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4457 }
4458 run_test 54d "fifo device works in lustre ======================"
4459
4460 test_54e() {
4461         f="$DIR/f54e"
4462         string="aaaaaa"
4463         cp -aL /dev/console $f
4464         echo $string > $f || error "echo $string to $f failed"
4465 }
4466 run_test 54e "console/tty device works in lustre ======================"
4467
4468 #The test_55 used to be iopen test and it was removed by bz#24037.
4469 #run_test 55 "check iopen_connect_dentry() ======================"
4470
4471 test_56a() {    # was test_56
4472         rm -rf $DIR/$tdir
4473         $SETSTRIPE -d $DIR
4474         test_mkdir -p $DIR/$tdir/dir
4475         NUMFILES=3
4476         NUMFILESx2=$(($NUMFILES * 2))
4477         for i in $(seq 1 $NUMFILES); do
4478                 touch $DIR/$tdir/file$i
4479                 touch $DIR/$tdir/dir/file$i
4480         done
4481
4482         # test lfs getstripe with --recursive
4483         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4484         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4485                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4486         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4487         [[ $FILENUM -eq $NUMFILES ]] ||
4488                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4489         echo "$GETSTRIPE --recursive passed."
4490
4491         # test lfs getstripe with file instead of dir
4492         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4493         [[ $FILENUM -eq 1 ]] ||
4494                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4495         echo "$GETSTRIPE file1 passed."
4496
4497         #test lfs getstripe with --verbose
4498         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4499                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4500                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4501         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4502                 error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4503
4504         #test lfs getstripe with -v prints lmm_fid
4505         [[ $($GETSTRIPE -v $DIR/$tdir | grep -c lmm_fid) -eq $NUMFILES ]] ||
4506                 error "$GETSTRIPE -v $DIR/$tdir: want $NUMFILES lmm_fid: lines"
4507         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_fid) -eq 0 ]] ||
4508                 error "$GETSTRIPE $DIR/$tdir: showed lmm_fid"
4509         echo "$GETSTRIPE --verbose passed."
4510
4511         #check for FID information
4512         local fid1=$($GETSTRIPE --fid $DIR/$tdir/file1)
4513         local fid2=$($GETSTRIPE --verbose $DIR/$tdir/file1 |
4514                        awk '/lmm_fid: / { print $2 }')
4515         local fid3=$($LFS path2fid $DIR/$tdir/file1)
4516         [ "$fid1" != "$fid2" ] &&
4517                 error "getstripe --fid $fid1 != getstripe --verbose $fid2"
4518         [ "$fid1" != "$fid3" ] &&
4519                 error "getstripe --fid $fid1 != lfs path2fid $fid3"
4520         echo "$GETSTRIPE --fid passed."
4521
4522         #test lfs getstripe with --obd
4523         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4524                 grep -q "unknown obduuid" ||
4525                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4526
4527         [[ $OSTCOUNT -lt 2 ]] &&
4528                 skip_env "skipping other $GETSTRIPE --obd test" && return
4529
4530         OSTIDX=1
4531         OBDUUID=$(ostuuid_from_index $OSTIDX)
4532         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4533         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4534         [[ $FOUND -eq $FILENUM ]] ||
4535                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4536         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4537                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4538                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4539                 error "$GETSTRIPE --obd: should not show file on other obd"
4540         echo "$GETSTRIPE --obd passed"
4541 }
4542 run_test 56a "check $GETSTRIPE"
4543
4544 test_56b() {
4545         test_mkdir $DIR/$tdir
4546         NUMDIRS=3
4547         for i in $(seq 1 $NUMDIRS); do
4548                 test_mkdir $DIR/$tdir/dir$i
4549         done
4550
4551         # test lfs getdirstripe default mode is non-recursion, which is
4552         # different from lfs getstripe
4553         dircnt=$($LFS getdirstripe $DIR/$tdir | grep -c lmv_stripe_count)
4554         [[ $dircnt -eq 1 ]] ||
4555                 error "$LFS getdirstripe: found $dircnt, not 1"
4556         dircnt=$($LFS getdirstripe --recursive $DIR/$tdir |
4557                 grep -c lmv_stripe_count)
4558         [[ $dircnt -eq $((NUMDIRS + 1)) ]] ||
4559                 error "$LFS getdirstripe --recursive: found $dircnt, \
4560                         not $((NUMDIRS + 1))"
4561 }
4562 run_test 56b "check $LFS getdirstripe"
4563
4564 test_56c() {
4565         local ost_idx=0
4566         local ost_name=$(ostname_from_index $ost_idx)
4567
4568         local old_status=$(ost_dev_status $ost_idx)
4569         [[ -z "$old_status" ]] ||
4570                 { skip_env "OST $ost_name is in $old_status status"; return 0; }
4571
4572         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4573         sleep_maxage
4574
4575         local new_status=$(ost_dev_status $ost_idx)
4576         [[ "$new_status" = "D" ]] ||
4577                 error "OST $ost_name is in status of '$new_status', not 'D'"
4578
4579         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4580         sleep_maxage
4581
4582         new_status=$(ost_dev_status $ost_idx)
4583         [[ -z "$new_status" ]] ||
4584                 error "OST $ost_name is in status of '$new_status', not ''"
4585 }
4586 run_test 56c "check 'lfs df' showing device status"
4587
4588 NUMFILES=3
4589 NUMDIRS=3
4590 setup_56() {
4591         local LOCAL_NUMFILES="$1"
4592         local LOCAL_NUMDIRS="$2"
4593         local MKDIR_PARAMS="$3"
4594         local DIR_STRIPE_PARAMS="$4"
4595
4596         if [ ! -d "$TDIR" ] ; then
4597                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4598                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4599                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4600                         touch $TDIR/file$i
4601                 done
4602                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4603                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4604                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4605                                 touch $TDIR/dir$i/file$j
4606                         done
4607                 done
4608         fi
4609 }
4610
4611 setup_56_special() {
4612         LOCAL_NUMFILES=$1
4613         LOCAL_NUMDIRS=$2
4614         setup_56 $1 $2
4615         if [ ! -e "$TDIR/loop1b" ] ; then
4616                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4617                         mknod $TDIR/loop${i}b b 7 $i
4618                         mknod $TDIR/null${i}c c 1 3
4619                         ln -s $TDIR/file1 $TDIR/link${i}l
4620                 done
4621                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4622                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4623                         mknod $TDIR/dir$i/null${i}c c 1 3
4624                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4625                 done
4626         fi
4627 }
4628
4629 test_56g() {
4630         $SETSTRIPE -d $DIR
4631
4632         TDIR=$DIR/${tdir}g
4633         setup_56 $NUMFILES $NUMDIRS
4634
4635         EXPECTED=$(($NUMDIRS + 2))
4636         # test lfs find with -name
4637         for i in $(seq 1 $NUMFILES) ; do
4638                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4639                 [ $NUMS -eq $EXPECTED ] ||
4640                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4641                               "found $NUMS, expected $EXPECTED"
4642         done
4643 }
4644 run_test 56g "check lfs find -name ============================="
4645
4646 test_56h() {
4647         $SETSTRIPE -d $DIR
4648
4649         TDIR=$DIR/${tdir}g
4650         setup_56 $NUMFILES $NUMDIRS
4651
4652         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4653         # test lfs find with ! -name
4654         for i in $(seq 1 $NUMFILES) ; do
4655                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4656                 [ $NUMS -eq $EXPECTED ] ||
4657                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4658                               "found $NUMS, expected $EXPECTED"
4659         done
4660 }
4661 run_test 56h "check lfs find ! -name ============================="
4662
4663 test_56i() {
4664        tdir=${tdir}i
4665        test_mkdir -p $DIR/$tdir
4666        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4667        CMD="$LFIND -ost $UUID $DIR/$tdir"
4668        OUT=$($CMD)
4669        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4670 }
4671 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4672
4673 test_56j() {
4674         TDIR=$DIR/${tdir}g
4675         setup_56_special $NUMFILES $NUMDIRS
4676
4677         EXPECTED=$((NUMDIRS + 1))
4678         CMD="$LFIND -type d $TDIR"
4679         NUMS=$($CMD | wc -l)
4680         [ $NUMS -eq $EXPECTED ] ||
4681                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4682 }
4683 run_test 56j "check lfs find -type d ============================="
4684
4685 test_56k() {
4686         TDIR=$DIR/${tdir}g
4687         setup_56_special $NUMFILES $NUMDIRS
4688
4689         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4690         CMD="$LFIND -type f $TDIR"
4691         NUMS=$($CMD | wc -l)
4692         [ $NUMS -eq $EXPECTED ] ||
4693                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4694 }
4695 run_test 56k "check lfs find -type f ============================="
4696
4697 test_56l() {
4698         TDIR=$DIR/${tdir}g
4699         setup_56_special $NUMFILES $NUMDIRS
4700
4701         EXPECTED=$((NUMDIRS + NUMFILES))
4702         CMD="$LFIND -type b $TDIR"
4703         NUMS=$($CMD | wc -l)
4704         [ $NUMS -eq $EXPECTED ] ||
4705                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4706 }
4707 run_test 56l "check lfs find -type b ============================="
4708
4709 test_56m() {
4710         TDIR=$DIR/${tdir}g
4711         setup_56_special $NUMFILES $NUMDIRS
4712
4713         EXPECTED=$((NUMDIRS + NUMFILES))
4714         CMD="$LFIND -type c $TDIR"
4715         NUMS=$($CMD | wc -l)
4716         [ $NUMS -eq $EXPECTED ] ||
4717                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4718 }
4719 run_test 56m "check lfs find -type c ============================="
4720
4721 test_56n() {
4722         TDIR=$DIR/${tdir}g
4723         setup_56_special $NUMFILES $NUMDIRS
4724
4725         EXPECTED=$((NUMDIRS + NUMFILES))
4726         CMD="$LFIND -type l $TDIR"
4727         NUMS=$($CMD | wc -l)
4728         [ $NUMS -eq $EXPECTED ] ||
4729                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4730 }
4731 run_test 56n "check lfs find -type l ============================="
4732
4733 test_56o() {
4734         TDIR=$DIR/${tdir}o
4735         setup_56 $NUMFILES $NUMDIRS
4736         utime $TDIR/file1 > /dev/null || error "utime (1)"
4737         utime $TDIR/file2 > /dev/null || error "utime (2)"
4738         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4739         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4740         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4741         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4742
4743         EXPECTED=4
4744         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4745         [ $NUMS -eq $EXPECTED ] || \
4746                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4747
4748         EXPECTED=12
4749         CMD="$LFIND -mtime 0 $TDIR"
4750         NUMS=$($CMD | wc -l)
4751         [ $NUMS -eq $EXPECTED ] ||
4752                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4753 }
4754 run_test 56o "check lfs find -mtime for old files =========================="
4755
4756 test_56p() {
4757         [ $RUNAS_ID -eq $UID ] &&
4758                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4759
4760         TDIR=$DIR/${tdir}p
4761         setup_56 $NUMFILES $NUMDIRS
4762
4763         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4764         EXPECTED=$NUMFILES
4765         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4766         NUMS=$($CMD | wc -l)
4767         [ $NUMS -eq $EXPECTED ] || \
4768                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4769
4770         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4771         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4772         NUMS=$($CMD | wc -l)
4773         [ $NUMS -eq $EXPECTED ] || \
4774                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4775 }
4776 run_test 56p "check lfs find -uid and ! -uid ==============================="
4777
4778 test_56q() {
4779         [ $RUNAS_ID -eq $UID ] &&
4780                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4781
4782         TDIR=$DIR/${tdir}q
4783         setup_56 $NUMFILES $NUMDIRS
4784
4785         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4786
4787         EXPECTED=$NUMFILES
4788         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4789         NUMS=$($CMD | wc -l)
4790         [ $NUMS -eq $EXPECTED ] ||
4791                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4792
4793         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4794         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4795         NUMS=$($CMD | wc -l)
4796         [ $NUMS -eq $EXPECTED ] ||
4797                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4798 }
4799 run_test 56q "check lfs find -gid and ! -gid ==============================="
4800
4801 test_56r() {
4802         TDIR=$DIR/${tdir}r
4803         setup_56 $NUMFILES $NUMDIRS
4804
4805         EXPECTED=12
4806         CMD="$LFIND -size 0 -type f $TDIR"
4807         NUMS=$($CMD | wc -l)
4808         [ $NUMS -eq $EXPECTED ] ||
4809                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4810         EXPECTED=0
4811         CMD="$LFIND ! -size 0 -type f $TDIR"
4812         NUMS=$($CMD | wc -l)
4813         [ $NUMS -eq $EXPECTED ] ||
4814                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4815         echo "test" > $TDIR/$tfile
4816         echo "test2" > $TDIR/$tfile.2 && sync
4817         EXPECTED=1
4818         CMD="$LFIND -size 5 -type f $TDIR"
4819         NUMS=$($CMD | wc -l)
4820         [ $NUMS -eq $EXPECTED ] ||
4821                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4822         EXPECTED=1
4823         CMD="$LFIND -size +5 -type f $TDIR"
4824         NUMS=$($CMD | wc -l)
4825         [ $NUMS -eq $EXPECTED ] ||
4826                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4827         EXPECTED=2
4828         CMD="$LFIND -size +0 -type f $TDIR"
4829         NUMS=$($CMD | wc -l)
4830         [ $NUMS -eq $EXPECTED ] ||
4831                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4832         EXPECTED=2
4833         CMD="$LFIND ! -size -5 -type f $TDIR"
4834         NUMS=$($CMD | wc -l)
4835         [ $NUMS -eq $EXPECTED ] ||
4836                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4837         EXPECTED=12
4838         CMD="$LFIND -size -5 -type f $TDIR"
4839         NUMS=$($CMD | wc -l)
4840         [ $NUMS -eq $EXPECTED ] ||
4841                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4842 }
4843 run_test 56r "check lfs find -size works =========================="
4844
4845 test_56s() { # LU-611
4846         TDIR=$DIR/${tdir}s
4847         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4848
4849         if [[ $OSTCOUNT -gt 1 ]]; then
4850                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4851                 ONESTRIPE=4
4852                 EXTRA=4
4853         else
4854                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4855                 EXTRA=0
4856         fi
4857
4858         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4859         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4860         NUMS=$($CMD | wc -l)
4861         [ $NUMS -eq $EXPECTED ] || {
4862                 $GETSTRIPE -R $TDIR
4863                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4864         }
4865
4866         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4867         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4868         NUMS=$($CMD | wc -l)
4869         [ $NUMS -eq $EXPECTED ] || {
4870                 $GETSTRIPE -R $TDIR
4871                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4872         }
4873
4874         EXPECTED=$ONESTRIPE
4875         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4876         NUMS=$($CMD | wc -l)
4877         [ $NUMS -eq $EXPECTED ] || {
4878                 $GETSTRIPE -R $TDIR
4879                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4880         }
4881
4882         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4883         NUMS=$($CMD | wc -l)
4884         [ $NUMS -eq $EXPECTED ] || {
4885                 $GETSTRIPE -R $TDIR
4886                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4887         }
4888
4889         EXPECTED=0
4890         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4891         NUMS=$($CMD | wc -l)
4892         [ $NUMS -eq $EXPECTED ] || {
4893                 $GETSTRIPE -R $TDIR
4894                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4895         }
4896 }
4897 run_test 56s "check lfs find -stripe-count works"
4898
4899 test_56t() { # LU-611
4900         TDIR=$DIR/${tdir}t
4901         setup_56 $NUMFILES $NUMDIRS "--stripe-size 512k"
4902
4903         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4904
4905         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4906         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4907         NUMS=$($CMD | wc -l)
4908         [ $NUMS -eq $EXPECTED ] ||
4909                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4910
4911         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4912         NUMS=$($CMD | wc -l)
4913         [ $NUMS -eq $EXPECTED ] ||
4914                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4915
4916         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4917         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4918         NUMS=$($CMD | wc -l)
4919         [ $NUMS -eq $EXPECTED ] ||
4920                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4921
4922         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4923         NUMS=$($CMD | wc -l)
4924         [ $NUMS -eq $EXPECTED ] ||
4925                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4926
4927         EXPECTED=4
4928         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4929         NUMS=$($CMD | wc -l)
4930         [ $NUMS -eq $EXPECTED ] ||
4931                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4932
4933         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4934         NUMS=$($CMD | wc -l)
4935         [ $NUMS -eq $EXPECTED ] ||
4936                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4937
4938         EXPECTED=0
4939         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4940         NUMS=$($CMD | wc -l)
4941         [ $NUMS -eq $EXPECTED ] ||
4942                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4943 }
4944 run_test 56t "check lfs find -stripe-size works"
4945
4946 test_56u() { # LU-611
4947         TDIR=$DIR/${tdir}u
4948         setup_56 $NUMFILES $NUMDIRS "-i 0"
4949
4950         if [[ $OSTCOUNT -gt 1 ]]; then
4951                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4952                 ONESTRIPE=4
4953         else
4954                 ONESTRIPE=0
4955         fi
4956
4957         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4958         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4959         NUMS=$($CMD | wc -l)
4960         [ $NUMS -eq $EXPECTED ] ||
4961                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4962
4963         EXPECTED=$ONESTRIPE
4964         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4965         NUMS=$($CMD | wc -l)
4966         [ $NUMS -eq $EXPECTED ] ||
4967                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4968
4969         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4970         NUMS=$($CMD | wc -l)
4971         [ $NUMS -eq $EXPECTED ] ||
4972                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4973
4974         EXPECTED=0
4975         # This should produce an error and not return any files
4976         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4977         NUMS=$($CMD 2>/dev/null | wc -l)
4978         [ $NUMS -eq $EXPECTED ] ||
4979                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4980
4981         if [[ $OSTCOUNT -gt 1 ]]; then
4982                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4983                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4984                 NUMS=$($CMD | wc -l)
4985                 [ $NUMS -eq $EXPECTED ] ||
4986                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4987         fi
4988 }
4989 run_test 56u "check lfs find -stripe-index works"
4990
4991 test_56v() {
4992     local MDT_IDX=0
4993
4994     TDIR=$DIR/${tdir}v
4995     rm -rf $TDIR
4996     setup_56 $NUMFILES $NUMDIRS
4997
4998     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4999     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
5000
5001     for file in $($LFIND -mdt $UUID $TDIR); do
5002         file_mdt_idx=$($GETSTRIPE -M $file)
5003         [ $file_mdt_idx -eq $MDT_IDX ] ||
5004             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
5005     done
5006 }
5007 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
5008
5009 test_56w() {
5010         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
5011                 return
5012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5013         TDIR=$DIR/${tdir}w
5014
5015     rm -rf $TDIR || error "remove $TDIR failed"
5016     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5017
5018     local stripe_size
5019     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
5020         error "$GETSTRIPE -S -d $TDIR failed"
5021     stripe_size=${stripe_size%% *}
5022
5023     local file_size=$((stripe_size * OSTCOUNT))
5024     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5025     local required_space=$((file_num * file_size))
5026
5027     local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5028                         head -n1)
5029     [[ $free_space -le $((required_space / 1024)) ]] &&
5030         skip_env "need at least $required_space bytes free space," \
5031                  "have $free_space kbytes" && return
5032
5033     local dd_bs=65536
5034     local dd_count=$((file_size / dd_bs))
5035
5036     # write data into the files
5037     local i
5038     local j
5039     local file
5040     for i in $(seq 1 $NUMFILES); do
5041         file=$TDIR/file$i
5042         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
5043             error "write data into $file failed"
5044     done
5045     for i in $(seq 1 $NUMDIRS); do
5046         for j in $(seq 1 $NUMFILES); do
5047             file=$TDIR/dir$i/file$j
5048             yes | dd bs=$dd_bs count=$dd_count of=$file \
5049                 >/dev/null 2>&1 ||
5050                 error "write data into $file failed"
5051         done
5052     done
5053
5054     local expected=-1
5055     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5056
5057     # lfs_migrate file
5058     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
5059     echo "$cmd"
5060     eval $cmd || error "$cmd failed"
5061
5062     check_stripe_count $TDIR/file1 $expected
5063
5064         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5065         then
5066                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5067                 # OST 1 if it is on OST 0. This file is small enough to
5068                 # be on only one stripe.
5069                 file=$TDIR/migr_1_ost
5070                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5071                         error "write data into $file failed"
5072                 local obdidx=$($LFS getstripe -i $file)
5073                 local oldmd5=$(md5sum $file)
5074                 local newobdidx=0
5075                 [[ $obdidx -eq 0 ]] && newobdidx=1
5076                 cmd="$LFS migrate -i $newobdidx $file"
5077                 echo $cmd
5078                 eval $cmd || error "$cmd failed"
5079                 local realobdix=$($LFS getstripe -i $file)
5080                 local newmd5=$(md5sum $file)
5081                 [[ $newobdidx -ne $realobdix ]] &&
5082                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
5083                 [[ "$oldmd5" != "$newmd5" ]] &&
5084                         error "md5sum differ: $oldmd5, $newmd5"
5085         fi
5086
5087     # lfs_migrate dir
5088     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
5089     echo "$cmd"
5090     eval $cmd || error "$cmd failed"
5091
5092     for j in $(seq 1 $NUMFILES); do
5093         check_stripe_count $TDIR/dir1/file$j $expected
5094     done
5095
5096     # lfs_migrate works with lfs find
5097     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
5098          $LFS_MIGRATE -y -c $expected"
5099     echo "$cmd"
5100     eval $cmd || error "$cmd failed"
5101
5102     for i in $(seq 2 $NUMFILES); do
5103         check_stripe_count $TDIR/file$i $expected
5104     done
5105     for i in $(seq 2 $NUMDIRS); do
5106         for j in $(seq 1 $NUMFILES); do
5107             check_stripe_count $TDIR/dir$i/file$j $expected
5108         done
5109     done
5110 }
5111 run_test 56w "check lfs_migrate -c stripe_count works"
5112
5113 test_56x() {
5114         check_swap_layouts_support && return 0
5115         [[ $OSTCOUNT -lt 2 ]] &&
5116                 skip_env "need 2 OST, skipping test" && return
5117
5118         local dir0=$DIR/$tdir/$testnum
5119         test_mkdir -p $dir0 || error "creating dir $dir0"
5120
5121         local ref1=/etc/passwd
5122         local file1=$dir0/file1
5123
5124         $SETSTRIPE -c 2 $file1
5125         cp $ref1 $file1
5126         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5127         stripe=$($GETSTRIPE -c $file1)
5128         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5129         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5130
5131         # clean up
5132         rm -f $file1
5133 }
5134 run_test 56x "lfs migration support"
5135
5136 test_56xa() {
5137         check_swap_layouts_support && return 0
5138         [[ $OSTCOUNT -lt 2 ]] &&
5139                 skip_env "need 2 OST, skipping test" && return
5140
5141         local dir0=$DIR/$tdir/$testnum
5142         test_mkdir -p $dir0 || error "creating dir $dir0"
5143
5144         local ref1=/etc/passwd
5145         local file1=$dir0/file1
5146
5147         $SETSTRIPE -c 2 $file1
5148         cp $ref1 $file1
5149         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5150         local stripe=$($GETSTRIPE -c $file1)
5151         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5152         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5153
5154         # clean up
5155         rm -f $file1
5156 }
5157 run_test 56xa "lfs migration --block support"
5158
5159 test_56y() {
5160         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5161                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5162                 return
5163
5164         local res=""
5165         local dir0=$DIR/$tdir/$testnum
5166         test_mkdir -p $dir0 || error "creating dir $dir0"
5167         local f1=$dir0/file1
5168         local f2=$dir0/file2
5169
5170         touch $f1 || error "creating std file $f1"
5171         $MULTIOP $f2 H2c || error "creating released file $f2"
5172
5173         # a directory can be raid0, so ask only for files
5174         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
5175         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5176
5177         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5178         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5179
5180         # only files can be released, so no need to force file search
5181         res=$($LFIND $dir0 -L released)
5182         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5183
5184         res=$($LFIND $dir0 \! -L released)
5185         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5186
5187 }
5188 run_test 56y "lfs find -L raid0|released"
5189
5190 test_56z() { # LU-4824
5191         # This checks to make sure 'lfs find' continues after errors
5192         # There are two classes of errors that should be caught:
5193         # - If multiple paths are provided, all should be searched even if one
5194         #   errors out
5195         # - If errors are encountered during the search, it should not terminate
5196         #   early
5197         local i
5198         test_mkdir $DIR/$tdir
5199         for i in d{0..9}; do
5200                 test_mkdir $DIR/$tdir/$i
5201         done
5202         touch $DIR/$tdir/d{0..9}/$tfile
5203         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5204                 error "$LFS find did not return an error"
5205         # Make a directory unsearchable. This should NOT be the last entry in
5206         # directory order.  Arbitrarily pick the 6th entry
5207         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5208         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5209         # The user should be able to see 10 directories and 9 files
5210         [ $count == 19 ] || error "$LFS find did not continue after error"
5211 }
5212 run_test 56z "lfs find should continue after an error"
5213
5214 test_56aa() { # LU-5937
5215         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5216
5217         mkdir $DIR/$tdir
5218         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5219
5220         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5221         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5222
5223         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5224 }
5225 run_test 56aa "lfs find --size under striped dir"
5226
5227 test_57a() {
5228         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5229         # note test will not do anything if MDS is not local
5230         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5231                 skip "Only applicable to ldiskfs-based MDTs"
5232                 return
5233         fi
5234
5235         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5236         local MNTDEV="osd*.*MDT*.mntdev"
5237         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5238         [ -z "$DEV" ] && error "can't access $MNTDEV"
5239         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5240                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5241                         error "can't access $DEV"
5242                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5243                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5244                 rm $TMP/t57a.dump
5245         done
5246 }
5247 run_test 57a "verify MDS filesystem created with large inodes =="
5248
5249 test_57b() {
5250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5251         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5252                 skip "Only applicable to ldiskfs-based MDTs"
5253                 return
5254         fi
5255
5256         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5257         local dir=$DIR/$tdir
5258
5259         local FILECOUNT=100
5260         local FILE1=$dir/f1
5261         local FILEN=$dir/f$FILECOUNT
5262
5263         rm -rf $dir || error "removing $dir"
5264         test_mkdir -p -c1 $dir || error "creating $dir"
5265         local mdtidx=$($LFS getstripe -M $dir)
5266         local mdtname=MDT$(printf %04x $mdtidx)
5267         local facet=mds$((mdtidx + 1))
5268
5269         echo "mcreating $FILECOUNT files"
5270         createmany -m $dir/f 1 $FILECOUNT || \
5271                 error "creating files in $dir"
5272
5273         # verify that files do not have EAs yet
5274         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5275         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5276
5277         sync
5278         sleep 1
5279         df $dir  #make sure we get new statfs data
5280         local MDSFREE=$(do_facet $facet \
5281                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5282         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5283         echo "opening files to create objects/EAs"
5284         local FILE
5285         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5286                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5287         done
5288
5289         # verify that files have EAs now
5290         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5291         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5292
5293         sleep 1  #make sure we get new statfs data
5294         df $dir
5295         local MDSFREE2=$(do_facet $facet \
5296                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5297         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5298         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5299                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5300                         error "MDC before $MDCFREE != after $MDCFREE2"
5301                 else
5302                         echo "MDC before $MDCFREE != after $MDCFREE2"
5303                         echo "unable to confirm if MDS has large inodes"
5304                 fi
5305         fi
5306         rm -rf $dir
5307 }
5308 run_test 57b "default LOV EAs are stored inside large inodes ==="
5309
5310 test_58() {
5311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5312         [ -z "$(which wiretest 2>/dev/null)" ] &&
5313                         skip_env "could not find wiretest" && return
5314         wiretest
5315 }
5316 run_test 58 "verify cross-platform wire constants =============="
5317
5318 test_59() {
5319         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5320         echo "touch 130 files"
5321         createmany -o $DIR/f59- 130
5322         echo "rm 130 files"
5323         unlinkmany $DIR/f59- 130
5324         sync
5325         # wait for commitment of removal
5326         wait_delete_completed
5327 }
5328 run_test 59 "verify cancellation of llog records async ========="
5329
5330 TEST60_HEAD="test_60 run $RANDOM"
5331 test_60a() {
5332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5333         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5334         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5335                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5336                         skip_env "missing subtest run-llog.sh" && return
5337
5338         log "$TEST60_HEAD - from kernel mode"
5339         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5340         do_facet mgs sh run-llog.sh
5341         do_facet mgs $LCTL dk > $TMP/$tfile
5342
5343         # LU-6388: test llog_reader
5344         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5345         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5346         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5347                         skip_env "missing llog_reader" && return
5348         local fstype=$(facet_fstype mgs)
5349         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5350                 skip_env "Only for ldiskfs or zfs type mgs" && return
5351
5352         local mntpt=$(facet_mntpt mgs)
5353         local mgsdev=$(mgsdevname 1)
5354         local fid_list
5355         local fid
5356         local rec_list
5357         local rec
5358         local rec_type
5359         local obj_file
5360         local path
5361         local seq
5362         local oid
5363         local pass=true
5364
5365         #get fid and record list
5366         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5367                 tail -n 4))
5368         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5369                 tail -n 4))
5370         #remount mgs as ldiskfs or zfs type
5371         stop mgs || error "stop mgs failed"
5372         mount_fstype mgs || error "remount mgs failed"
5373         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5374                 fid=${fid_list[i]}
5375                 rec=${rec_list[i]}
5376                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5377                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5378                 oid=$((16#$oid))
5379
5380                 case $fstype in
5381                         ldiskfs )
5382                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5383                         zfs )
5384                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5385                 esac
5386                 echo "obj_file is $obj_file"
5387                 do_facet mgs $llog_reader $obj_file
5388
5389                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5390                         awk '{ print $3 }' | sed -e "s/^type=//g")
5391                 if [ $rec_type != $rec ]; then
5392                         echo "FAILED test_60a wrong record type $rec_type," \
5393                               "should be $rec"
5394                         pass=false
5395                         break
5396                 fi
5397
5398                 #check obj path if record type is LLOG_LOGID_MAGIC
5399                 if [ "$rec" == "1064553b" ]; then
5400                         path=$(do_facet mgs $llog_reader $obj_file |
5401                                 grep "path=" | awk '{ print $NF }' |
5402                                 sed -e "s/^path=//g")
5403                         if [ $obj_file != $mntpt/$path ]; then
5404                                 echo "FAILED test_60a wrong obj path" \
5405                                       "$montpt/$path, should be $obj_file"
5406                                 pass=false
5407                                 break
5408                         fi
5409                 fi
5410         done
5411         rm -f $TMP/$tfile
5412         #restart mgs before "error", otherwise it will block the next test
5413         stop mgs || error "stop mgs failed"
5414         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5415         $pass || error "test failed, see FAILED test_60a messages for specifics"
5416 }
5417 run_test 60a "llog_test run from kernel module and test llog_reader"
5418
5419 test_60aa() {
5420         # test old logid format
5421         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
5422                 do_facet mgs $LCTL dl | grep MGS
5423                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
5424                         error "old llog_print failed"
5425         fi
5426
5427         # test new logid format
5428         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
5429                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
5430                         error "new llog_print failed"
5431         fi
5432 }
5433 run_test 60aa "llog_print works with FIDs and simple names"
5434
5435 test_60b() { # bug 6411
5436         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5437         dmesg > $DIR/$tfile
5438         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5439                                 /llog.test/ {
5440                                         if (marker)
5441                                                 from_marker++
5442                                         from_begin++
5443                                 }
5444                                 END {
5445                                         if (marker)
5446                                                 print from_marker
5447                                         else
5448                                                 print from_begin
5449                                 }")
5450         [[ $LLOG_COUNT -gt 100 ]] &&
5451                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5452 }
5453 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5454
5455 test_60c() {
5456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5457         echo "create 5000 files"
5458         createmany -o $DIR/f60c- 5000
5459 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5460         lctl set_param fail_loc=0x80000137
5461         unlinkmany $DIR/f60c- 5000
5462         lctl set_param fail_loc=0
5463 }
5464 run_test 60c "unlink file when mds full"
5465
5466 test_60d() {
5467         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5468         SAVEPRINTK=$(lctl get_param -n printk)
5469
5470         # verify "lctl mark" is even working"
5471         MESSAGE="test message ID $RANDOM $$"
5472         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5473         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5474
5475         lctl set_param printk=0 || error "set lnet.printk failed"
5476         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5477         MESSAGE="new test message ID $RANDOM $$"
5478         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5479         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5480         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5481
5482         lctl set_param -n printk="$SAVEPRINTK"
5483 }
5484 run_test 60d "test printk console message masking"
5485
5486 test_60e() {
5487         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5488         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5489         touch $DIR/$tfile
5490 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5491         do_facet mds1 lctl set_param fail_loc=0x15b
5492         rm $DIR/$tfile
5493 }
5494 run_test 60e "no space while new llog is being created"
5495
5496 test_61() {
5497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5498         f="$DIR/f61"
5499         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5500         cancel_lru_locks osc
5501         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5502         sync
5503 }
5504 run_test 61 "mmap() writes don't make sync hang ================"
5505
5506 # bug 2330 - insufficient obd_match error checking causes LBUG
5507 test_62() {
5508         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5509         f="$DIR/f62"
5510         echo foo > $f
5511         cancel_lru_locks osc
5512         lctl set_param fail_loc=0x405
5513         cat $f && error "cat succeeded, expect -EIO"
5514         lctl set_param fail_loc=0
5515 }
5516 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5517 # match every page all of the time.
5518 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5519
5520 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5521 # Though this test is irrelevant anymore, it helped to reveal some
5522 # other grant bugs (LU-4482), let's keep it.
5523 test_63a() {   # was test_63
5524         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5525         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5526         for i in `seq 10` ; do
5527                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5528                 sleep 5
5529                 kill $!
5530                 sleep 1
5531         done
5532
5533         rm -f $DIR/f63 || true
5534 }
5535 run_test 63a "Verify oig_wait interruption does not crash ======="
5536
5537 # bug 2248 - async write errors didn't return to application on sync
5538 # bug 3677 - async write errors left page locked
5539 test_63b() {
5540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5541         debugsave
5542         lctl set_param debug=-1
5543
5544         # ensure we have a grant to do async writes
5545         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5546         rm $DIR/$tfile
5547
5548         sync    # sync lest earlier test intercept the fail_loc
5549
5550         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5551         lctl set_param fail_loc=0x80000406
5552         $MULTIOP $DIR/$tfile Owy && \
5553                 error "sync didn't return ENOMEM"
5554         sync; sleep 2; sync     # do a real sync this time to flush page
5555         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5556                 error "locked page left in cache after async error" || true
5557         debugrestore
5558 }
5559 run_test 63b "async write errors should be returned to fsync ==="
5560
5561 test_64a () {
5562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5563         df $DIR
5564         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5565 }
5566 run_test 64a "verify filter grant calculations (in kernel) ====="
5567
5568 test_64b () {
5569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5570         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5571 }
5572 run_test 64b "check out-of-space detection on client ==========="
5573
5574 test_64c() {
5575         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5576 }
5577 run_test 64c "verify grant shrink ========================------"
5578
5579 # bug 1414 - set/get directories' stripe info
5580 test_65a() {
5581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5582         test_mkdir -p $DIR/$tdir
5583         touch $DIR/$tdir/f1
5584         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5585 }
5586 run_test 65a "directory with no stripe info ===================="
5587
5588 test_65b() {
5589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5590         test_mkdir -p $DIR/$tdir
5591         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5592
5593         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5594                                                 error "setstripe"
5595         touch $DIR/$tdir/f2
5596         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5597 }
5598 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
5599
5600 test_65c() {
5601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5602         if [[ $OSTCOUNT -gt 1 ]]; then
5603                 test_mkdir -p $DIR/$tdir
5604                 local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5605
5606                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5607                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5608                 touch $DIR/$tdir/f3
5609                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5610         fi
5611 }
5612 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
5613
5614 test_65d() {
5615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5616         test_mkdir -p $DIR/$tdir
5617         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
5618         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5619
5620         if [[ $STRIPECOUNT -le 0 ]]; then
5621                 sc=1
5622         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5623 #LOV_MAX_STRIPE_COUNT is 2000
5624                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5625         else
5626                 sc=$(($STRIPECOUNT - 1))
5627         fi
5628         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5629         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5630         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5631                 error "lverify failed"
5632 }
5633 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
5634
5635 test_65e() {
5636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5637         test_mkdir -p $DIR/$tdir
5638
5639         $SETSTRIPE $DIR/$tdir || error "setstripe"
5640         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5641                                         error "no stripe info failed"
5642         touch $DIR/$tdir/f6
5643         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5644 }
5645 run_test 65e "directory setstripe defaults ======================="
5646
5647 test_65f() {
5648         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5649         test_mkdir -p $DIR/${tdir}f
5650         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5651 }
5652 run_test 65f "dir setstripe permission (should return error) ==="
5653
5654 test_65g() {
5655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5656         test_mkdir -p $DIR/$tdir
5657         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5658
5659         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5660                                                         error "setstripe"
5661         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5662         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5663                 error "delete default stripe failed"
5664 }
5665 run_test 65g "directory setstripe -d ==========================="
5666
5667 test_65h() {
5668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5669         test_mkdir -p $DIR/$tdir
5670         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5671
5672         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5673                                                         error "setstripe"
5674         test_mkdir -p $DIR/$tdir/dd1
5675         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5676                 error "stripe info inherit failed"
5677 }
5678 run_test 65h "directory stripe info inherit ===================="
5679
5680 test_65i() { # bug6367
5681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5682         $SETSTRIPE -S 65536 -c -1 $MOUNT
5683 }
5684 run_test 65i "set non-default striping on root directory (bug 6367)="
5685
5686 test_65ia() { # bug12836
5687         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5688         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5689 }
5690 run_test 65ia "getstripe on -1 default directory striping"
5691
5692 test_65ib() { # bug12836
5693         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5694         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5695 }
5696 run_test 65ib "getstripe -v on -1 default directory striping"
5697
5698 test_65ic() { # bug12836
5699         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5700         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5701 }
5702 run_test 65ic "new find on -1 default directory striping"
5703
5704 test_65j() { # bug6367
5705         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5706         sync; sleep 1
5707         # if we aren't already remounting for each test, do so for this test
5708         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5709                 cleanup || error "failed to unmount"
5710                 setup
5711         fi
5712         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5713 }
5714 run_test 65j "set default striping on root directory (bug 6367)="
5715
5716 test_65k() { # bug11679
5717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5718         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5719         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5720
5721         local disable_precreate=true
5722         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
5723                 disable_precreate=false
5724
5725         echo "Check OST status: "
5726         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
5727                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
5728
5729         for OSC in $MDS_OSCS; do
5730                 echo $OSC "is active"
5731                 do_facet $SINGLEMDS lctl --device %$OSC activate
5732         done
5733
5734         for INACTIVE_OSC in $MDS_OSCS; do
5735                 local ost=$(osc_to_ost $INACTIVE_OSC)
5736                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
5737                                lov.*md*.target_obd |
5738                                awk -F: /$ost/'{ print $1 }' | head -n 1)
5739
5740                 mkdir -p $DIR/$tdir
5741                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
5742                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
5743
5744                 echo "Deactivate: " $INACTIVE_OSC
5745                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5746
5747                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
5748                               osp.$ost*MDT0000.create_count")
5749                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
5750                                   osp.$ost*MDT0000.max_create_count")
5751                 $disable_precreate &&
5752                         do_facet $SINGLEMDS "lctl set_param -n \
5753                                 osp.$ost*MDT0000.max_create_count=0"
5754
5755                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
5756                         [ -f $DIR/$tdir/$idx ] && continue
5757                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
5758                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
5759                                 error "setstripe $idx should succeed"
5760                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
5761                 done
5762                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
5763                 rmdir $DIR/$tdir
5764
5765                 do_facet $SINGLEMDS "lctl set_param -n \
5766                         osp.$ost*MDT0000.max_create_count=$max_count"
5767                 do_facet $SINGLEMDS "lctl set_param -n \
5768                         osp.$ost*MDT0000.create_count=$count"
5769                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5770                 echo $INACTIVE_OSC "is Activate"
5771
5772                 wait_osc_import_state mds ost$ostnum FULL
5773         done
5774 }
5775 run_test 65k "validate manual striping works properly with deactivated OSCs"
5776
5777 test_65l() { # bug 12836
5778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5779         test_mkdir -p $DIR/$tdir/test_dir
5780         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5781         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5782 }
5783 run_test 65l "lfs find on -1 stripe dir ========================"
5784
5785 test_65m() {
5786         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
5787         true
5788 }
5789 run_test 65m "normal user can't set filesystem default stripe"
5790
5791 # bug 2543 - update blocks count on client
5792 test_66() {
5793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5794         COUNT=${COUNT:-8}
5795         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5796         sync; sync_all_data; sync; sync_all_data
5797         cancel_lru_locks osc
5798         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5799         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5800 }
5801 run_test 66 "update inode blocks count on client ==============="
5802
5803 LLOOP=
5804 LLITELOOPLOAD=
5805 cleanup_68() {
5806         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5807         trap 0
5808         if [ ! -z "$LLOOP" ]; then
5809                 if swapon -s | grep -q $LLOOP; then
5810                         swapoff $LLOOP || error "swapoff failed"
5811                 fi
5812
5813                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5814                 rm -f $LLOOP
5815                 unset LLOOP
5816         fi
5817         rm -f $DIR/f68*
5818 }
5819
5820 meminfo() {
5821         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5822 }
5823
5824 swap_used() {
5825         swapon -s | awk '($1 == "'$1'") { print $4 }'
5826 }
5827
5828 # bug5265, obdfilter oa2dentry return -ENOENT
5829 # #define OBD_FAIL_SRV_ENOENT 0x217
5830 test_69() {
5831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5832         remote_ost_nodsh && skip "remote OST with nodsh" && return
5833
5834         f="$DIR/$tfile"
5835         $SETSTRIPE -c 1 -i 0 $f
5836
5837         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5838
5839         do_facet ost1 lctl set_param fail_loc=0x217
5840         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5841         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5842
5843         do_facet ost1 lctl set_param fail_loc=0
5844         $DIRECTIO write $f 0 2 || error "write error"
5845
5846         cancel_lru_locks osc
5847         $DIRECTIO read $f 0 1 || error "read error"
5848
5849         do_facet ost1 lctl set_param fail_loc=0x217
5850         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5851
5852         do_facet ost1 lctl set_param fail_loc=0
5853         rm -f $f
5854 }
5855 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5856
5857 test_71() {
5858         test_mkdir -p $DIR/$tdir
5859         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5860         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5861 }
5862 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5863
5864 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5866         [ "$RUNAS_ID" = "$UID" ] &&
5867                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5868
5869         # Check that testing environment is properly set up. Skip if not
5870         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5871                 skip_env "User $RUNAS_ID does not exist - skipping"
5872                 return 0
5873         }
5874         touch $DIR/$tfile
5875         chmod 777 $DIR/$tfile
5876         chmod ug+s $DIR/$tfile
5877         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5878                 error "$RUNAS dd $DIR/$tfile failed"
5879         # See if we are still setuid/sgid
5880         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5881                 error "S/gid is not dropped on write"
5882         # Now test that MDS is updated too
5883         cancel_lru_locks mdc
5884         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5885                 error "S/gid is not dropped on MDS"
5886         rm -f $DIR/$tfile
5887 }
5888 run_test 72a "Test that remove suid works properly (bug5695) ===="
5889
5890 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5891         local perm
5892
5893         [ "$RUNAS_ID" = "$UID" ] && \
5894                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5895         [ "$RUNAS_ID" -eq 0 ] && \
5896                 skip_env "RUNAS_ID = 0 -- skipping" && return
5897
5898         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5899         # Check that testing environment is properly set up. Skip if not
5900         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5901                 skip_env "User $RUNAS_ID does not exist - skipping"
5902                 return 0
5903         }
5904         touch $DIR/${tfile}-f{g,u}
5905         test_mkdir $DIR/${tfile}-dg
5906         test_mkdir $DIR/${tfile}-du
5907         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5908         chmod g+s $DIR/${tfile}-{f,d}g
5909         chmod u+s $DIR/${tfile}-{f,d}u
5910         for perm in 777 2777 4777; do
5911                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5912                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5913                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5914                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5915         done
5916         true
5917 }
5918 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5919
5920 # bug 3462 - multiple simultaneous MDC requests
5921 test_73() {
5922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5923         test_mkdir $DIR/d73-1
5924         test_mkdir $DIR/d73-2
5925         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5926         pid1=$!
5927
5928         lctl set_param fail_loc=0x80000129
5929         $MULTIOP $DIR/d73-1/f73-2 Oc &
5930         sleep 1
5931         lctl set_param fail_loc=0
5932
5933         $MULTIOP $DIR/d73-2/f73-3 Oc &
5934         pid3=$!
5935
5936         kill -USR1 $pid1
5937         wait $pid1 || return 1
5938
5939         sleep 25
5940
5941         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5942         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5943         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5944
5945         rm -rf $DIR/d73-*
5946 }
5947 run_test 73 "multiple MDC requests (should not deadlock)"
5948
5949 test_74a() { # bug 6149, 6184
5950         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5951         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5952         #
5953         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5954         # will spin in a tight reconnection loop
5955         touch $DIR/f74a
5956         $LCTL set_param fail_loc=0x8000030e
5957         # get any lock that won't be difficult - lookup works.
5958         ls $DIR/f74a
5959         $LCTL set_param fail_loc=0
5960         rm -f $DIR/f74a
5961         true
5962 }
5963 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5964
5965 test_74b() { # bug 13310
5966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5967         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5968         #
5969         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5970         # will spin in a tight reconnection loop
5971         $LCTL set_param fail_loc=0x8000030e
5972         # get a "difficult" lock
5973         touch $DIR/f74b
5974         $LCTL set_param fail_loc=0
5975         rm -f $DIR/f74b
5976         true
5977 }
5978 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5979
5980 test_74c() {
5981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5982         #define OBD_FAIL_LDLM_NEW_LOCK
5983         $LCTL set_param fail_loc=0x319
5984         touch $DIR/$tfile && error "touch successful"
5985         $LCTL set_param fail_loc=0
5986         true
5987 }
5988 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5989
5990 num_inodes() {
5991         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5992 }
5993
5994 get_inode_slab_tunables() {
5995         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5996 }
5997
5998 set_inode_slab_tunables() {
5999         echo "lustre_inode_cache $1" > /proc/slabinfo
6000 }
6001
6002 test_76() { # Now for bug 20433, added originally in bug 1443
6003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6004         local SLAB_SETTINGS=$(get_inode_slab_tunables)
6005         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6006         # we cannot set limit below 1 which means 1 inode in each
6007         # per-cpu cache is still allowed
6008         set_inode_slab_tunables "1 1 0"
6009         cancel_lru_locks osc
6010         BEFORE_INODES=$(num_inodes)
6011         echo "before inodes: $BEFORE_INODES"
6012         local COUNT=1000
6013         [ "$SLOW" = "no" ] && COUNT=100
6014         for i in $(seq $COUNT); do
6015                 touch $DIR/$tfile
6016                 rm -f $DIR/$tfile
6017         done
6018         cancel_lru_locks osc
6019         AFTER_INODES=$(num_inodes)
6020         echo "after inodes: $AFTER_INODES"
6021         local wait=0
6022         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6023                 sleep 2
6024                 AFTER_INODES=$(num_inodes)
6025                 wait=$((wait+2))
6026                 echo "wait $wait seconds inodes: $AFTER_INODES"
6027                 if [ $wait -gt 30 ]; then
6028                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6029                 fi
6030         done
6031         set_inode_slab_tunables "$SLAB_SETTINGS"
6032 }
6033 run_test 76 "confirm clients recycle inodes properly ===="
6034
6035
6036 export ORIG_CSUM=""
6037 set_checksums()
6038 {
6039         # Note: in sptlrpc modes which enable its own bulk checksum, the
6040         # original crc32_le bulk checksum will be automatically disabled,
6041         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6042         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6043         # In this case set_checksums() will not be no-op, because sptlrpc
6044         # bulk checksum will be enabled all through the test.
6045
6046         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6047         lctl set_param -n osc.*.checksums $1
6048         return 0
6049 }
6050
6051 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6052                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6053 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
6054 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
6055 set_checksum_type()
6056 {
6057         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6058         log "set checksum type to $1"
6059         return 0
6060 }
6061 F77_TMP=$TMP/f77-temp
6062 F77SZ=8
6063 setup_f77() {
6064         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6065                 error "error writing to $F77_TMP"
6066 }
6067
6068 test_77a() { # bug 10889
6069         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6070         $GSS && skip "could not run with gss" && return
6071         [ ! -f $F77_TMP ] && setup_f77
6072         set_checksums 1
6073         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6074         set_checksums 0
6075         rm -f $DIR/$tfile
6076 }
6077 run_test 77a "normal checksum read/write operation"
6078
6079 test_77b() { # bug 10889
6080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6081         $GSS && skip "could not run with gss" && return
6082         [ ! -f $F77_TMP ] && setup_f77
6083         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6084         $LCTL set_param fail_loc=0x80000409
6085         set_checksums 1
6086
6087         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6088                 error "dd error: $?"
6089         $LCTL set_param fail_loc=0
6090
6091         for algo in $CKSUM_TYPES; do
6092                 cancel_lru_locks osc
6093                 set_checksum_type $algo
6094                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6095                 $LCTL set_param fail_loc=0x80000408
6096                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6097                 $LCTL set_param fail_loc=0
6098         done
6099         set_checksums 0
6100         set_checksum_type $ORIG_CSUM_TYPE
6101         rm -f $DIR/$tfile
6102 }
6103 run_test 77b "checksum error on client write, read"
6104
6105 test_77d() { # bug 10889
6106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6107         $GSS && skip "could not run with gss" && return
6108         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6109         $LCTL set_param fail_loc=0x80000409
6110         set_checksums 1
6111         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6112                 error "direct write: rc=$?"
6113         $LCTL set_param fail_loc=0
6114         set_checksums 0
6115
6116         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6117         $LCTL set_param fail_loc=0x80000408
6118         set_checksums 1
6119         cancel_lru_locks osc
6120         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6121                 error "direct read: rc=$?"
6122         $LCTL set_param fail_loc=0
6123         set_checksums 0
6124 }
6125 run_test 77d "checksum error on OST direct write, read"
6126
6127 test_77f() { # bug 10889
6128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6129         $GSS && skip "could not run with gss" && return
6130         set_checksums 1
6131         for algo in $CKSUM_TYPES; do
6132                 cancel_lru_locks osc
6133                 set_checksum_type $algo
6134                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6135                 $LCTL set_param fail_loc=0x409
6136                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6137                         error "direct write succeeded"
6138                 $LCTL set_param fail_loc=0
6139         done
6140         set_checksum_type $ORIG_CSUM_TYPE
6141         set_checksums 0
6142 }
6143 run_test 77f "repeat checksum error on write (expect error)"
6144
6145 test_77g() { # bug 10889
6146         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6147         $GSS && skip "could not run with gss" && return
6148         remote_ost_nodsh && skip "remote OST with nodsh" && return
6149
6150         [ ! -f $F77_TMP ] && setup_f77
6151
6152         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6153         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6154         do_facet ost1 lctl set_param fail_loc=0x8000021a
6155         set_checksums 1
6156         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6157                 error "write error: rc=$?"
6158         do_facet ost1 lctl set_param fail_loc=0
6159         set_checksums 0
6160
6161         cancel_lru_locks osc
6162         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6163         do_facet ost1 lctl set_param fail_loc=0x8000021b
6164         set_checksums 1
6165         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6166         do_facet ost1 lctl set_param fail_loc=0
6167         set_checksums 0
6168 }
6169 run_test 77g "checksum error on OST write, read"
6170
6171 test_77j() { # bug 13805
6172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6173         $GSS && skip "could not run with gss" && return
6174         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6175         lctl set_param fail_loc=0x40c
6176         remount_client $MOUNT
6177         lctl set_param fail_loc=0
6178         # wait async osc connect to finish and reflect updated state value
6179         local i
6180         for (( i=0; i < OSTCOUNT; i++ )) ; do
6181                 wait_osc_import_state client ost$((i+1)) FULL
6182         done
6183
6184         for VALUE in $(lctl get_param osc.*osc-[^mM]*.checksum_type); do
6185                 PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
6186                 algo=$(lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g')
6187                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6188         done
6189         remount_client $MOUNT
6190 }
6191 run_test 77j "client only supporting ADLER32"
6192
6193 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6194 rm -f $F77_TMP
6195 unset F77_TMP
6196
6197 cleanup_test_78() {
6198         trap 0
6199         rm -f $DIR/$tfile
6200 }
6201
6202 test_78() { # bug 10901
6203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6204         remote_ost || { skip_env "local OST" && return; }
6205
6206         NSEQ=5
6207         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6208         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6209         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6210         echo "MemTotal: $MEMTOTAL"
6211
6212         # reserve 256MB of memory for the kernel and other running processes,
6213         # and then take 1/2 of the remaining memory for the read/write buffers.
6214         if [ $MEMTOTAL -gt 512 ] ;then
6215                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6216         else
6217                 # for those poor memory-starved high-end clusters...
6218                 MEMTOTAL=$((MEMTOTAL / 2))
6219         fi
6220         echo "Mem to use for directio: $MEMTOTAL"
6221
6222         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6223         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6224         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6225         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6226                 head -n1)
6227         echo "Smallest OST: $SMALLESTOST"
6228         [[ $SMALLESTOST -lt 10240 ]] &&
6229                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6230
6231         trap cleanup_test_78 EXIT
6232
6233         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6234                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6235
6236         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6237         echo "File size: $F78SIZE"
6238         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6239         for i in $(seq 1 $NSEQ); do
6240                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6241                 echo directIO rdwr round $i of $NSEQ
6242                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6243         done
6244
6245         cleanup_test_78
6246 }
6247 run_test 78 "handle large O_DIRECT writes correctly ============"
6248
6249 test_79() { # bug 12743
6250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6251         wait_delete_completed
6252
6253         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6254         BKFREE=$(calc_osc_kbytes kbytesfree)
6255         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6256
6257         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6258         DFTOTAL=`echo $STRING | cut -d, -f1`
6259         DFUSED=`echo $STRING  | cut -d, -f2`
6260         DFAVAIL=`echo $STRING | cut -d, -f3`
6261         DFFREE=$(($DFTOTAL - $DFUSED))
6262
6263         ALLOWANCE=$((64 * $OSTCOUNT))
6264
6265         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6266            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6267                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6268         fi
6269         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6270            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6271                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6272         fi
6273         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6274            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6275                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6276         fi
6277 }
6278 run_test 79 "df report consistency check ======================="
6279
6280 test_80() { # bug 10718
6281         remote_ost_nodsh && skip "remote OST with nodsh" && return
6282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6283         # relax strong synchronous semantics for slow backends like ZFS
6284         local soc="obdfilter.*.sync_on_lock_cancel"
6285         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6286         local hosts=
6287         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6288                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6289                           facet_active_host $host; done | sort -u)
6290                 do_nodes $hosts lctl set_param $soc=never
6291         fi
6292
6293         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6294         sync; sleep 1; sync
6295         local BEFORE=`date +%s`
6296         cancel_lru_locks osc
6297         local AFTER=`date +%s`
6298         local DIFF=$((AFTER-BEFORE))
6299         if [ $DIFF -gt 1 ] ; then
6300                 error "elapsed for 1M@1T = $DIFF"
6301         fi
6302
6303         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6304
6305         rm -f $DIR/$tfile
6306 }
6307 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6308
6309 test_81a() { # LU-456
6310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6311         remote_ost_nodsh && skip "remote OST with nodsh" && return
6312         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6313         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6314         do_facet ost1 lctl set_param fail_loc=0x80000228
6315
6316         # write should trigger a retry and success
6317         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6318         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6319         RC=$?
6320         if [ $RC -ne 0 ] ; then
6321                 error "write should success, but failed for $RC"
6322         fi
6323 }
6324 run_test 81a "OST should retry write when get -ENOSPC ==============="
6325
6326 test_81b() { # LU-456
6327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6328         remote_ost_nodsh && skip "remote OST with nodsh" && return
6329         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6330         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6331         do_facet ost1 lctl set_param fail_loc=0x228
6332
6333         # write should retry several times and return -ENOSPC finally
6334         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6335         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6336         RC=$?
6337         ENOSPC=28
6338         if [ $RC -ne $ENOSPC ] ; then
6339                 error "dd should fail for -ENOSPC, but succeed."
6340         fi
6341 }
6342 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6343
6344 test_82() { # LU-1031
6345         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6346         local gid1=14091995
6347         local gid2=16022000
6348
6349         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6350         local MULTIPID1=$!
6351         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6352         local MULTIPID2=$!
6353         kill -USR1 $MULTIPID2
6354         sleep 2
6355         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6356                 error "First grouplock does not block second one"
6357         else
6358                 echo "Second grouplock blocks first one"
6359         fi
6360         kill -USR1 $MULTIPID1
6361         wait $MULTIPID1
6362         wait $MULTIPID2
6363 }
6364 run_test 82 "Basic grouplock test ==============================="
6365
6366 test_99a() {
6367         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6368                 return
6369         test_mkdir -p $DIR/d99cvsroot
6370         chown $RUNAS_ID $DIR/d99cvsroot
6371         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6372         cd $TMP
6373
6374         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6375         cd $oldPWD
6376 }
6377 run_test 99a "cvs init ========================================="
6378
6379 test_99b() {
6380         [ -z "$(which cvs 2>/dev/null)" ] &&
6381                 skip_env "could not find cvs" && return
6382         [ ! -d $DIR/d99cvsroot ] && test_99a
6383         cd /etc/init.d
6384         # some versions of cvs import exit(1) when asked to import links or
6385         # files they can't read.  ignore those files.
6386         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6387                         ! -perm /4 -printf '-I %f\n')
6388         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6389                 d99reposname vtag rtag
6390 }
6391 run_test 99b "cvs import ======================================="
6392
6393 test_99c() {
6394         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6395         [ ! -d $DIR/d99cvsroot ] && test_99b
6396         cd $DIR
6397         test_mkdir -p $DIR/d99reposname
6398         chown $RUNAS_ID $DIR/d99reposname
6399         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6400 }
6401 run_test 99c "cvs checkout ====================================="
6402
6403 test_99d() {
6404         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6405         [ ! -d $DIR/d99cvsroot ] && test_99c
6406         cd $DIR/d99reposname
6407         $RUNAS touch foo99
6408         $RUNAS cvs add -m 'addmsg' foo99
6409 }
6410 run_test 99d "cvs add =========================================="
6411
6412 test_99e() {
6413         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6414         [ ! -d $DIR/d99cvsroot ] && test_99c
6415         cd $DIR/d99reposname
6416         $RUNAS cvs update
6417 }
6418 run_test 99e "cvs update ======================================="
6419
6420 test_99f() {
6421         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6422         [ ! -d $DIR/d99cvsroot ] && test_99d
6423         cd $DIR/d99reposname
6424         $RUNAS cvs commit -m 'nomsg' foo99
6425     rm -fr $DIR/d99cvsroot
6426 }
6427 run_test 99f "cvs commit ======================================="
6428
6429 test_100() {
6430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6431         [ "$NETTYPE" = tcp ] || \
6432                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6433                         return ; }
6434
6435         remote_ost_nodsh && skip "remote OST with nodsh" && return
6436         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6437         remote_servers || \
6438                 { skip "useless for local single node setup" && return; }
6439
6440         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6441                 [ "$PROT" != "tcp" ] && continue
6442                 RPORT=$(echo $REMOTE | cut -d: -f2)
6443                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6444
6445                 rc=0
6446                 LPORT=`echo $LOCAL | cut -d: -f2`
6447                 if [ $LPORT -ge 1024 ]; then
6448                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6449                         netstat -tna
6450                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6451                 fi
6452         done
6453         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6454 }
6455 run_test 100 "check local port using privileged port ==========="
6456
6457 function get_named_value()
6458 {
6459     local tag
6460
6461     tag=$1
6462     while read ;do
6463         line=$REPLY
6464         case $line in
6465         $tag*)
6466             echo $line | sed "s/^$tag[ ]*//"
6467             break
6468             ;;
6469         esac
6470     done
6471 }
6472
6473 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6474                    awk '/^max_cached_mb/ { print $2 }')
6475
6476 cleanup_101a() {
6477         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6478         trap 0
6479 }
6480
6481 test_101a() {
6482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6483         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6484         local s
6485         local discard
6486         local nreads=10000
6487         local cache_limit=32
6488
6489         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6490         trap cleanup_101a EXIT
6491         $LCTL set_param -n llite.*.read_ahead_stats 0
6492         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6493
6494         #
6495         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6496         #
6497         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6498         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6499
6500         discard=0
6501         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6502                 get_named_value 'read but discarded' | cut -d" " -f1); do
6503                         discard=$(($discard + $s))
6504         done
6505         cleanup_101a
6506
6507         if [[ $(($discard * 10)) -gt $nreads ]]; then
6508                 $LCTL get_param osc.*-osc*.rpc_stats
6509                 $LCTL get_param llite.*.read_ahead_stats
6510                 error "too many ($discard) discarded pages"
6511         fi
6512         rm -f $DIR/$tfile || true
6513 }
6514 run_test 101a "check read-ahead for random reads ================"
6515
6516 setup_test101bc() {
6517         test_mkdir -p $DIR/$tdir
6518         local STRIPE_SIZE=$1
6519         local FILE_LENGTH=$2
6520         STRIPE_OFFSET=0
6521
6522         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6523
6524         local list=$(comma_list $(osts_nodes))
6525         set_osd_param $list '' read_cache_enable 0
6526         set_osd_param $list '' writethrough_cache_enable 0
6527
6528         trap cleanup_test101bc EXIT
6529         # prepare the read-ahead file
6530         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6531
6532         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6533                                 count=$FILE_SIZE_MB 2> /dev/null
6534
6535 }
6536
6537 cleanup_test101bc() {
6538         trap 0
6539         rm -rf $DIR/$tdir
6540         rm -f $DIR/$tfile
6541
6542         local list=$(comma_list $(osts_nodes))
6543         set_osd_param $list '' read_cache_enable 1
6544         set_osd_param $list '' writethrough_cache_enable 1
6545 }
6546
6547 calc_total() {
6548         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6549 }
6550
6551 ra_check_101() {
6552         local READ_SIZE=$1
6553         local STRIPE_SIZE=$2
6554         local FILE_LENGTH=$3
6555         local RA_INC=1048576
6556         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6557         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6558                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6559         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6560                         get_named_value 'read but discarded' |
6561                         cut -d" " -f1 | calc_total)
6562         if [[ $DISCARD -gt $discard_limit ]]; then
6563                 $LCTL get_param llite.*.read_ahead_stats
6564                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6565         else
6566                 echo "Read-ahead success for size ${READ_SIZE}"
6567         fi
6568 }
6569
6570 test_101b() {
6571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6572         [[ $OSTCOUNT -lt 2 ]] &&
6573                 skip_env "skipping stride IO stride-ahead test" && return
6574         local STRIPE_SIZE=1048576
6575         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6576         if [ $SLOW == "yes" ]; then
6577                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6578         else
6579                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6580         fi
6581
6582         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6583
6584         # prepare the read-ahead file
6585         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6586         cancel_lru_locks osc
6587         for BIDX in 2 4 8 16 32 64 128 256
6588         do
6589                 local BSIZE=$((BIDX*4096))
6590                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6591                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6592                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6593                 $LCTL set_param -n llite.*.read_ahead_stats 0
6594                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6595                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6596                 cancel_lru_locks osc
6597                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6598         done
6599         cleanup_test101bc
6600         true
6601 }
6602 run_test 101b "check stride-io mode read-ahead ================="
6603
6604 test_101c() {
6605         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6606         local STRIPE_SIZE=1048576
6607         local FILE_LENGTH=$((STRIPE_SIZE*100))
6608         local nreads=10000
6609         local osc_rpc_stats
6610
6611         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6612
6613         cancel_lru_locks osc
6614         $LCTL set_param osc.*.rpc_stats 0
6615         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6616         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6617                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6618                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6619                 local size
6620
6621                 if [ $lines -le 20 ]; then
6622                         continue
6623                 fi
6624                 for size in 1 2 4 8; do
6625                         local rpc=$(echo "$stats" |
6626                                     awk '($1 == "'$size':") {print $2; exit; }')
6627                         [ $rpc != 0 ] &&
6628                                 error "Small $((size*4))k read IO $rpc !"
6629                 done
6630                 echo "$osc_rpc_stats check passed!"
6631         done
6632         cleanup_test101bc
6633         true
6634 }
6635 run_test 101c "check stripe_size aligned read-ahead ================="
6636
6637 set_read_ahead() {
6638         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6639         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6640 }
6641
6642 test_101d() {
6643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6644         local file=$DIR/$tfile
6645         local sz_MB=${FILESIZE_101d:-500}
6646         local ra_MB=${READAHEAD_MB:-40}
6647
6648         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6649         [ $free_MB -lt $sz_MB ] &&
6650                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6651
6652         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6653         $SETSTRIPE -c -1 $file || error "setstripe failed"
6654
6655         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6656         echo Cancel LRU locks on lustre client to flush the client cache
6657         cancel_lru_locks osc
6658
6659         echo Disable read-ahead
6660         local old_READAHEAD=$(set_read_ahead 0)
6661
6662         echo Reading the test file $file with read-ahead disabled
6663         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6664
6665         echo Cancel LRU locks on lustre client to flush the client cache
6666         cancel_lru_locks osc
6667         echo Enable read-ahead with ${ra_MB}MB
6668         set_read_ahead $ra_MB
6669
6670         echo Reading the test file $file with read-ahead enabled
6671         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6672
6673         echo "read-ahead disabled time read $raOFF"
6674         echo "read-ahead enabled  time read $raON"
6675
6676         set_read_ahead $old_READAHEAD
6677         rm -f $file
6678         wait_delete_completed
6679
6680         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6681                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6682 }
6683 run_test 101d "file read with and without read-ahead enabled"
6684
6685 test_101e() {
6686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6687         local file=$DIR/$tfile
6688         local size_KB=500  #KB
6689         local count=100
6690         local bsize=1024
6691
6692         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6693         local need_KB=$((count * size_KB))
6694         [[ $free_KB -le $need_KB ]] &&
6695                 skip_env "Need free space $need_KB, have $free_KB" && return
6696
6697         echo "Creating $count ${size_KB}K test files"
6698         for ((i = 0; i < $count; i++)); do
6699                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6700         done
6701
6702         echo "Cancel LRU locks on lustre client to flush the client cache"
6703         cancel_lru_locks osc
6704
6705         echo "Reset readahead stats"
6706         $LCTL set_param -n llite.*.read_ahead_stats 0
6707
6708         for ((i = 0; i < $count; i++)); do
6709                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6710         done
6711
6712         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6713                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6714
6715         for ((i = 0; i < $count; i++)); do
6716                 rm -rf $file.$i 2>/dev/null
6717         done
6718
6719         #10000 means 20% reads are missing in readahead
6720         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6721 }
6722 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6723
6724 test_101f() {
6725         which iozone || { skip "no iozone installed" && return; }
6726
6727         local old_debug=$($LCTL get_param debug)
6728         old_debug=${old_debug#*=}
6729         $LCTL set_param debug="reada mmap"
6730
6731         # create a test file
6732         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
6733
6734         echo Cancel LRU locks on lustre client to flush the client cache
6735         cancel_lru_locks osc
6736
6737         echo Reset readahead stats
6738         $LCTL set_param -n llite.*.read_ahead_stats 0
6739
6740         echo mmap read the file with small block size
6741         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
6742                 > /dev/null 2>&1
6743
6744         echo checking missing pages
6745         $LCTL get_param llite.*.read_ahead_stats
6746         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6747                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
6748
6749         $LCTL set_param debug="$old_debug"
6750         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
6751         rm -f $DIR/$tfile
6752 }
6753 run_test 101f "check mmap read performance"
6754
6755 test_101g_brw_size_test() {
6756         local mb=$1
6757         local pages=$((mb * 1048576 / $(page_size)))
6758
6759         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
6760                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
6761         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
6762                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
6763                         return 2
6764         done
6765
6766         $LCTL set_param -n osc.*.rpc_stats=0
6767
6768         # 10 RPCs should be enough for the test
6769         local count=10
6770         dd if=/dev/zero of=$DIR/$tfile bs=${mb}M count=$count ||
6771                 { error "dd write ${mb} MB blocks failed"; return 3; }
6772         cancel_lru_locks osc
6773         dd of=/dev/null if=$DIR/$tfile bs=${mb}M count=$count ||
6774                 { error "dd write ${mb} MB blocks failed"; return 4; }
6775
6776         # calculate number of full-sized read and write RPCs
6777         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
6778                 sed -n '/pages per rpc/,/^$/p' |
6779                 awk '/'$pages':/ { reads += $2; writes += $5 };' \
6780                 'END { print reads,writes }'))
6781         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
6782                 return 5
6783         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
6784                 return 6
6785
6786         return 0
6787 }
6788
6789 test_101g() {
6790         local rpcs
6791         local osts=$(get_facets OST)
6792         local list=$(comma_list $(osts_nodes))
6793         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6794         local brw_size="obdfilter.*.brw_size"
6795
6796         $LFS setstripe -i 0 -c 1 $DIR/$tfile
6797
6798         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
6799         if [ $(lustre_version_code ost1) -ge $(version_code 2.8.52) ]; then
6800                 [ $(lustre_version_code ost1) -ge $(version_code 2.9.52) ] &&
6801                         suffix="M"
6802                 if [[ $orig_mb < 16 ]]; then
6803                         save_lustre_params $osts "$brw_size" > $p
6804                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
6805                                 error "set 16MB RPC size failed"
6806
6807                         echo "remount client to enable new RPC size"
6808                         remount_client $MOUNT || error "remount_client failed"
6809                 fi
6810
6811                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
6812                 # should be able to set brw_size=12, but no rpc_stats for that
6813                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
6814         fi
6815
6816         test_101g_brw_size_test 4 || error "4MB RPC test failed"
6817
6818         if [[ $orig_mb < 16 ]]; then
6819                 restore_lustre_params < $p
6820                 remount_client $MOUNT || error "remount_client restore failed"
6821         fi
6822
6823         rm -f $p $DIR/$tfile
6824 }
6825 run_test 101g "Big bulk(4/16 MiB) readahead"
6826
6827 setup_test102() {
6828         test_mkdir -p $DIR/$tdir
6829         chown $RUNAS_ID $DIR/$tdir
6830         STRIPE_SIZE=65536
6831         STRIPE_OFFSET=1
6832         STRIPE_COUNT=$OSTCOUNT
6833         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6834
6835         trap cleanup_test102 EXIT
6836         cd $DIR
6837         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6838         cd $DIR/$tdir
6839         for num in 1 2 3 4; do
6840                 for count in $(seq 1 $STRIPE_COUNT); do
6841                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6842                                 local size=`expr $STRIPE_SIZE \* $num`
6843                                 local file=file"$num-$idx-$count"
6844                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6845                         done
6846                 done
6847         done
6848
6849         cd $DIR
6850         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6851 }
6852
6853 cleanup_test102() {
6854         trap 0
6855         rm -f $TMP/f102.tar
6856         rm -rf $DIR/d0.sanity/d102
6857 }
6858
6859 test_102a() {
6860         local testfile=$DIR/$tfile
6861
6862         touch $testfile
6863
6864         [ "$UID" != 0 ] && skip_env "must run as root" && return
6865         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6866                 skip_env "must have user_xattr" && return
6867
6868         [ -z "$(which setfattr 2>/dev/null)" ] &&
6869                 skip_env "could not find setfattr" && return
6870
6871         echo "set/get xattr..."
6872         setfattr -n trusted.name1 -v value1 $testfile ||
6873                 error "setfattr -n trusted.name1=value1 $testfile failed"
6874         getfattr -n trusted.name1 $testfile 2> /dev/null |
6875           grep "trusted.name1=.value1" ||
6876                 error "$testfile missing trusted.name1=value1"
6877
6878         setfattr -n user.author1 -v author1 $testfile ||
6879                 error "setfattr -n user.author1=author1 $testfile failed"
6880         getfattr -n user.author1 $testfile 2> /dev/null |
6881           grep "user.author1=.author1" ||
6882                 error "$testfile missing trusted.author1=author1"
6883
6884         echo "listxattr..."
6885         setfattr -n trusted.name2 -v value2 $testfile ||
6886                 error "$testfile unable to set trusted.name2"
6887         setfattr -n trusted.name3 -v value3 $testfile ||
6888                 error "$testfile unable to set trusted.name3"
6889         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6890             grep "trusted.name" | wc -l) -eq 3 ] ||
6891                 error "$testfile missing 3 trusted.name xattrs"
6892
6893         setfattr -n user.author2 -v author2 $testfile ||
6894                 error "$testfile unable to set user.author2"
6895         setfattr -n user.author3 -v author3 $testfile ||
6896                 error "$testfile unable to set user.author3"
6897         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6898             grep "user.author" | wc -l) -eq 3 ] ||
6899                 error "$testfile missing 3 user.author xattrs"
6900
6901         echo "remove xattr..."
6902         setfattr -x trusted.name1 $testfile ||
6903                 error "$testfile error deleting trusted.name1"
6904         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6905                 error "$testfile did not delete trusted.name1 xattr"
6906
6907         setfattr -x user.author1 $testfile ||
6908                 error "$testfile error deleting user.author1"
6909         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6910                 error "$testfile did not delete trusted.name1 xattr"
6911
6912         # b10667: setting lustre special xattr be silently discarded
6913         echo "set lustre special xattr ..."
6914         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6915                 error "$testfile allowed setting trusted.lov"
6916 }
6917 run_test 102a "user xattr test =================================="
6918
6919 test_102b() {
6920         [ -z "$(which setfattr 2>/dev/null)" ] &&
6921                 skip_env "could not find setfattr" && return
6922
6923         # b10930: get/set/list trusted.lov xattr
6924         echo "get/set/list trusted.lov xattr ..."
6925         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6926         local testfile=$DIR/$tfile
6927         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6928                 error "setstripe failed"
6929         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6930                 error "getstripe failed"
6931         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6932                 error "can't get trusted.lov from $testfile"
6933
6934         local testfile2=${testfile}2
6935         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6936                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6937
6938         $MCREATE $testfile2
6939         setfattr -n trusted.lov -v $value $testfile2
6940         local stripe_size=$($GETSTRIPE -S $testfile2)
6941         local stripe_count=$($GETSTRIPE -c $testfile2)
6942         [[ $stripe_size -eq 65536 ]] ||
6943                 error "stripe size $stripe_size != 65536"
6944         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6945                 error "stripe count $stripe_count != $STRIPECOUNT"
6946         rm -f $DIR/$tfile
6947 }
6948 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6949
6950 test_102c() {
6951         [ -z "$(which setfattr 2>/dev/null)" ] &&
6952                 skip_env "could not find setfattr" && return
6953
6954         # b10930: get/set/list lustre.lov xattr
6955         echo "get/set/list lustre.lov xattr ..."
6956         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6957         test_mkdir -p $DIR/$tdir
6958         chown $RUNAS_ID $DIR/$tdir
6959         local testfile=$DIR/$tdir/$tfile
6960         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6961                 error "setstripe failed"
6962         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6963                 error "getstripe failed"
6964         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6965         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6966
6967         local testfile2=${testfile}2
6968         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6969                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6970
6971         $RUNAS $MCREATE $testfile2
6972         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6973         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6974         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6975         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6976         [ $stripe_count -eq $STRIPECOUNT ] ||
6977                 error "stripe count $stripe_count != $STRIPECOUNT"
6978 }
6979 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6980
6981 compare_stripe_info1() {
6982         local stripe_index_all_zero=true
6983
6984         for num in 1 2 3 4; do
6985                 for count in $(seq 1 $STRIPE_COUNT); do
6986                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6987                                 local size=$((STRIPE_SIZE * num))
6988                                 local file=file"$num-$offset-$count"
6989                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6990                                 [[ $stripe_size -ne $size ]] &&
6991                                     error "$file: size $stripe_size != $size"
6992                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6993                                 # allow fewer stripes to be created, ORI-601
6994                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6995                                     error "$file: count $stripe_count != $count"
6996                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6997                                 [[ $stripe_index -ne 0 ]] &&
6998                                         stripe_index_all_zero=false
6999                         done
7000                 done
7001         done
7002         $stripe_index_all_zero &&
7003                 error "all files are being extracted starting from OST index 0"
7004         return 0
7005 }
7006
7007 find_lustre_tar() {
7008         [ -n "$(which tar 2>/dev/null)" ] &&
7009                 strings $(which tar) | grep -q "lustre" && echo tar
7010 }
7011
7012 test_102d() {
7013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7014         # b10930: tar test for trusted.lov xattr
7015         TAR=$(find_lustre_tar)
7016         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
7017         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
7018         setup_test102
7019         test_mkdir -p $DIR/d102d
7020         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
7021         cd $DIR/d102d/$tdir
7022         compare_stripe_info1
7023 }
7024 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
7025
7026 test_102f() {
7027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7028         # b10930: tar test for trusted.lov xattr
7029         TAR=$(find_lustre_tar)
7030         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
7031         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
7032         setup_test102
7033         test_mkdir -p $DIR/d102f
7034         cd $DIR
7035         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
7036         cd $DIR/d102f/$tdir
7037         compare_stripe_info1
7038 }
7039 run_test 102f "tar copy files, not keep osts ==========="
7040
7041 grow_xattr() {
7042         local xsize=${1:-1024}  # in bytes
7043         local file=$DIR/$tfile
7044
7045         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
7046                 skip "must have user_xattr" && return 0
7047         [ -z "$(which setfattr 2>/dev/null)" ] &&
7048                 skip_env "could not find setfattr" && return 0
7049         [ -z "$(which getfattr 2>/dev/null)" ] &&
7050                 skip_env "could not find getfattr" && return 0
7051
7052         touch $file
7053
7054         local value="$(generate_string $xsize)"
7055
7056         local xbig=trusted.big
7057         log "save $xbig on $file"
7058         setfattr -n $xbig -v $value $file ||
7059                 error "saving $xbig on $file failed"
7060
7061         local orig=$(get_xattr_value $xbig $file)
7062         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
7063
7064         local xsml=trusted.sml
7065         log "save $xsml on $file"
7066         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
7067
7068         local new=$(get_xattr_value $xbig $file)
7069         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
7070
7071         log "grow $xsml on $file"
7072         setfattr -n $xsml -v "$value" $file ||
7073                 error "growing $xsml on $file failed"
7074
7075         new=$(get_xattr_value $xbig $file)
7076         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
7077         log "$xbig still valid after growing $xsml"
7078
7079         rm -f $file
7080 }
7081
7082 test_102h() { # bug 15777
7083         grow_xattr 1024
7084 }
7085 run_test 102h "grow xattr from inside inode to external block"
7086
7087 test_102ha() {
7088         large_xattr_enabled || { skip "large_xattr disabled" && return; }
7089         grow_xattr $(max_xattr_size)
7090 }
7091 run_test 102ha "grow xattr from inside inode to external inode"
7092
7093 test_102i() { # bug 17038
7094         [ -z "$(which getfattr 2>/dev/null)" ] &&
7095                 skip "could not find getfattr" && return
7096         touch $DIR/$tfile
7097         ln -s $DIR/$tfile $DIR/${tfile}link
7098         getfattr -n trusted.lov $DIR/$tfile ||
7099                 error "lgetxattr on $DIR/$tfile failed"
7100         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7101                 grep -i "no such attr" ||
7102                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7103         rm -f $DIR/$tfile $DIR/${tfile}link
7104 }
7105 run_test 102i "lgetxattr test on symbolic link ============"
7106
7107 test_102j() {
7108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7109         TAR=$(find_lustre_tar)
7110         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
7111         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
7112         setup_test102 "$RUNAS"
7113         test_mkdir -p $DIR/d102j
7114         chown $RUNAS_ID $DIR/d102j
7115         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
7116         cd $DIR/d102j/$tdir
7117         compare_stripe_info1 "$RUNAS"
7118 }
7119 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7120
7121 test_102k() {
7122         [ -z "$(which setfattr 2>/dev/null)" ] &&
7123                 skip "could not find setfattr" && return
7124         touch $DIR/$tfile
7125         # b22187 just check that does not crash for regular file.
7126         setfattr -n trusted.lov $DIR/$tfile
7127         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
7128         local test_kdir=$DIR/d102k
7129         test_mkdir $test_kdir
7130         local default_size=`$GETSTRIPE -S $test_kdir`
7131         local default_count=`$GETSTRIPE -c $test_kdir`
7132         local default_offset=`$GETSTRIPE -i $test_kdir`
7133         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7134                 error 'dir setstripe failed'
7135         setfattr -n trusted.lov $test_kdir
7136         local stripe_size=`$GETSTRIPE -S $test_kdir`
7137         local stripe_count=`$GETSTRIPE -c $test_kdir`
7138         local stripe_offset=`$GETSTRIPE -i $test_kdir`
7139         [ $stripe_size -eq $default_size ] ||
7140                 error "stripe size $stripe_size != $default_size"
7141         [ $stripe_count -eq $default_count ] ||
7142                 error "stripe count $stripe_count != $default_count"
7143         [ $stripe_offset -eq $default_offset ] ||
7144                 error "stripe offset $stripe_offset != $default_offset"
7145         rm -rf $DIR/$tfile $test_kdir
7146 }
7147 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7148
7149 test_102l() {
7150         [ -z "$(which getfattr 2>/dev/null)" ] &&
7151                 skip "could not find getfattr" && return
7152
7153         # LU-532 trusted. xattr is invisible to non-root
7154         local testfile=$DIR/$tfile
7155
7156         touch $testfile
7157
7158         echo "listxattr as user..."
7159         chown $RUNAS_ID $testfile
7160         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7161             grep -q "trusted" &&
7162                 error "$testfile trusted xattrs are user visible"
7163
7164         return 0;
7165 }
7166 run_test 102l "listxattr size test =================================="
7167
7168 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7169         local path=$DIR/$tfile
7170         touch $path
7171
7172         listxattr_size_check $path || error "listattr_size_check $path failed"
7173 }
7174 run_test 102m "Ensure listxattr fails on small bufffer ========"
7175
7176 cleanup_test102
7177
7178 getxattr() { # getxattr path name
7179         # Return the base64 encoding of the value of xattr name on path.
7180         local path=$1
7181         local name=$2
7182
7183         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7184         # file: $path
7185         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7186         #
7187         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7188
7189         getfattr --absolute-names --encoding=base64 --name=$name $path |
7190                 awk -F= -v name=$name '$1 == name {
7191                         print substr($0, index($0, "=") + 1);
7192         }'
7193 }
7194
7195 test_102n() { # LU-4101 mdt: protect internal xattrs
7196         [ -z "$(which setfattr 2>/dev/null)" ] &&
7197                 skip "could not find setfattr" && return
7198         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7199         then
7200                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7201                 return
7202         fi
7203
7204         local file0=$DIR/$tfile.0
7205         local file1=$DIR/$tfile.1
7206         local xattr0=$TMP/$tfile.0
7207         local xattr1=$TMP/$tfile.1
7208         local namelist="lov lma lmv link fid version som hsm"
7209         local name
7210         local value
7211
7212         rm -rf $file0 $file1 $xattr0 $xattr1
7213         touch $file0 $file1
7214
7215         # Get 'before' xattrs of $file1.
7216         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7217
7218         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7219                 namelist+=" lfsck_namespace"
7220         for name in $namelist; do
7221                 # Try to copy xattr from $file0 to $file1.
7222                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7223
7224                 setfattr --name=trusted.$name --value="$value" $file1 ||
7225                         error "setxattr 'trusted.$name' failed"
7226
7227                 # Try to set a garbage xattr.
7228                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7229
7230                 setfattr --name=trusted.$name --value="$value" $file1 ||
7231                         error "setxattr 'trusted.$name' failed"
7232
7233                 # Try to remove the xattr from $file1. We don't care if this
7234                 # appears to succeed or fail, we just don't want there to be
7235                 # any changes or crashes.
7236                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7237         done
7238
7239         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7240         then
7241                 name="lfsck_ns"
7242                 # Try to copy xattr from $file0 to $file1.
7243                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7244
7245                 setfattr --name=trusted.$name --value="$value" $file1 ||
7246                         error "setxattr 'trusted.$name' failed"
7247
7248                 # Try to set a garbage xattr.
7249                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7250
7251                 setfattr --name=trusted.$name --value="$value" $file1 ||
7252                         error "setxattr 'trusted.$name' failed"
7253
7254                 # Try to remove the xattr from $file1. We don't care if this
7255                 # appears to succeed or fail, we just don't want there to be
7256                 # any changes or crashes.
7257                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7258         fi
7259
7260         # Get 'after' xattrs of file1.
7261         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7262
7263         if ! diff $xattr0 $xattr1; then
7264                 error "before and after xattrs of '$file1' differ"
7265         fi
7266
7267         rm -rf $file0 $file1 $xattr0 $xattr1
7268
7269         return 0
7270 }
7271 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7272
7273 test_102p() { # LU-4703 setxattr did not check ownership
7274         local testfile=$DIR/$tfile
7275
7276         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7277                 skip "MDS needs to be at least 2.5.56" && return
7278
7279         touch $testfile
7280
7281         echo "setfacl as user..."
7282         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7283         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7284
7285         echo "setfattr as user..."
7286         setfacl -m "u:$RUNAS_ID:---" $testfile
7287         $RUNAS setfattr -x system.posix_acl_access $testfile
7288         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7289 }
7290 run_test 102p "check setxattr(2) correctly fails without permission"
7291
7292 test_102q() {
7293         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7294                 skip "MDS needs to be at least 2.6.92" && return
7295         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7296 }
7297 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7298
7299 test_102r() {
7300         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7301                 skip "MDS needs to be at least 2.6.93" && return
7302         touch $DIR/$tfile || error "touch"
7303         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7304         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7305         rm $DIR/$tfile || error "rm"
7306
7307         #normal directory
7308         mkdir -p $DIR/$tdir || error "mkdir"
7309         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7310         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7311         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7312                 error "$testfile error deleting user.author1"
7313         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7314                 grep "user.$(basename $tdir)" &&
7315                 error "$tdir did not delete user.$(basename $tdir)"
7316         rmdir $DIR/$tdir || error "rmdir"
7317
7318         #striped directory
7319         test_mkdir -p $DIR/$tdir || error "make striped dir"
7320         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7321         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7322         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7323                 error "$testfile error deleting user.author1"
7324         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7325                 grep "user.$(basename $tdir)" &&
7326                 error "$tdir did not delete user.$(basename $tdir)"
7327         rmdir $DIR/$tdir || error "rm striped dir"
7328 }
7329 run_test 102r "set EAs with empty values"
7330
7331 run_acl_subtest()
7332 {
7333     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7334     return $?
7335 }
7336
7337 test_103a() {
7338         [ "$UID" != 0 ] && skip_env "must run as root" && return
7339         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7340                 skip "must have acl enabled" && return
7341         [ -z "$(which setfacl 2>/dev/null)" ] &&
7342                 skip_env "could not find setfacl" && return
7343         $GSS && skip "could not run under gss" && return
7344         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7345
7346         gpasswd -a daemon bin                           # LU-5641
7347         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7348
7349         declare -a identity_old
7350
7351         for num in $(seq $MDSCOUNT); do
7352                 switch_identity $num true || identity_old[$num]=$?
7353         done
7354
7355         SAVE_UMASK=$(umask)
7356         umask 0022
7357         mkdir -p $DIR/$tdir
7358         cd $DIR/$tdir
7359
7360         echo "performing cp ..."
7361         run_acl_subtest cp || error "run_acl_subtest cp failed"
7362         echo "performing getfacl-noacl..."
7363         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7364         echo "performing misc..."
7365         run_acl_subtest misc || error  "misc test failed"
7366         echo "performing permissions..."
7367         run_acl_subtest permissions || error "permissions failed"
7368         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7369         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7370              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7371              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7372         then
7373                 echo "performing permissions xattr..."
7374                 run_acl_subtest permissions_xattr ||
7375                         error "permissions_xattr failed"
7376         fi
7377         echo "performing setfacl..."
7378         run_acl_subtest setfacl || error  "setfacl test failed"
7379
7380         # inheritance test got from HP
7381         echo "performing inheritance..."
7382         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7383         chmod +x make-tree || error "chmod +x failed"
7384         run_acl_subtest inheritance || error "inheritance test failed"
7385         rm -f make-tree
7386
7387         echo "LU-974 ignore umask when acl is enabled..."
7388         run_acl_subtest 974 || error "LU-974 umask test failed"
7389         if [ $MDSCOUNT -ge 2 ]; then
7390                 run_acl_subtest 974_remote ||
7391                         error "LU-974 umask test failed under remote dir"
7392         fi
7393
7394         echo "LU-2561 newly created file is same size as directory..."
7395         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7396                 run_acl_subtest 2561 || error "LU-2561 test failed"
7397         else
7398                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7399         fi
7400
7401         run_acl_subtest 4924 || error "LU-4924 test failed"
7402
7403         cd $SAVE_PWD
7404         umask $SAVE_UMASK
7405
7406         for num in $(seq $MDSCOUNT); do
7407                 if [ "${identity_old[$num]}" = 1 ]; then
7408                         switch_identity $num false || identity_old[$num]=$?
7409                 fi
7410         done
7411 }
7412 run_test 103a "acl test ========================================="
7413
7414 test_103b() {
7415         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7416         local noacl=false
7417         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7418         local mountopts=$MDS_MOUNT_OPTS
7419
7420         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7421                 noacl=true
7422         else
7423                 # stop the MDT
7424                 stop $SINGLEMDS || error "failed to stop MDT."
7425                 # remount the MDT
7426                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7427                         MDS_MOUNT_OPTS="-o noacl"
7428                 else
7429                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7430                 fi
7431                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7432                         error "failed to start MDT."
7433                 MDS_MOUNT_OPTS=$mountopts
7434         fi
7435
7436         touch $DIR/$tfile
7437         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7438
7439         if ! $noacl; then
7440                 # stop the MDT
7441                 stop $SINGLEMDS || error "failed to stop MDT."
7442                 # remount the MDT
7443                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7444                         error "failed to start MDT."
7445         fi
7446
7447         true
7448 }
7449 run_test 103b "MDS mount option 'noacl'"
7450
7451 test_103c() {
7452         mkdir -p $DIR/$tdir
7453         cp -rp $DIR/$tdir $DIR/$tdir.bak
7454
7455         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7456                 error "$DIR/$tdir shouldn't contain default ACL"
7457         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7458                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7459         true
7460 }
7461 run_test 103c "'cp -rp' won't set empty acl"
7462
7463 test_104a() {
7464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7465         touch $DIR/$tfile
7466         lfs df || error "lfs df failed"
7467         lfs df -ih || error "lfs df -ih failed"
7468         lfs df -h $DIR || error "lfs df -h $DIR failed"
7469         lfs df -i $DIR || error "lfs df -i $DIR failed"
7470         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7471         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7472
7473         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7474         lctl --device %$OSC deactivate
7475         lfs df || error "lfs df with deactivated OSC failed"
7476         lctl --device %$OSC activate
7477         # wait the osc back to normal
7478         wait_osc_import_state client ost FULL
7479
7480         lfs df || error "lfs df with reactivated OSC failed"
7481         rm -f $DIR/$tfile
7482 }
7483 run_test 104a "lfs df [-ih] [path] test ========================="
7484
7485 test_104b() {
7486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7487         [ $RUNAS_ID -eq $UID ] &&
7488                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7489         chmod 666 /dev/obd
7490         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7491                         grep "Permission denied" | wc -l)))
7492         if [ $denied_cnt -ne 0 ]; then
7493                 error "lfs check servers test failed"
7494         fi
7495 }
7496 run_test 104b "$RUNAS lfs check servers test ===================="
7497
7498 test_105a() {
7499         # doesn't work on 2.4 kernels
7500         touch $DIR/$tfile
7501         if $(flock_is_enabled); then
7502                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7503         else
7504                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7505         fi
7506         rm -f $DIR/$tfile
7507 }
7508 run_test 105a "flock when mounted without -o flock test ========"
7509
7510 test_105b() {
7511         touch $DIR/$tfile
7512         if $(flock_is_enabled); then
7513                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7514         else
7515                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7516         fi
7517         rm -f $DIR/$tfile
7518 }
7519 run_test 105b "fcntl when mounted without -o flock test ========"
7520
7521 test_105c() {
7522         touch $DIR/$tfile
7523         if $(flock_is_enabled); then
7524                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7525         else
7526                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7527         fi
7528         rm -f $DIR/$tfile
7529 }
7530 run_test 105c "lockf when mounted without -o flock test ========"
7531
7532 test_105d() { # bug 15924
7533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7534         test_mkdir -p $DIR/$tdir
7535         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7536         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7537         $LCTL set_param fail_loc=0x80000315
7538         flocks_test 2 $DIR/$tdir
7539 }
7540 run_test 105d "flock race (should not freeze) ========"
7541
7542 test_105e() { # bug 22660 && 22040
7543         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7544         touch $DIR/$tfile
7545         flocks_test 3 $DIR/$tfile
7546 }
7547 run_test 105e "Two conflicting flocks from same process ======="
7548
7549 test_106() { #bug 10921
7550         test_mkdir -p $DIR/$tdir
7551         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7552         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7553 }
7554 run_test 106 "attempt exec of dir followed by chown of that dir"
7555
7556 test_107() {
7557         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7558         CDIR=`pwd`
7559         cd $DIR
7560
7561         local file=core
7562         rm -f $file
7563
7564         local save_pattern=$(sysctl -n kernel.core_pattern)
7565         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7566         sysctl -w kernel.core_pattern=$file
7567         sysctl -w kernel.core_uses_pid=0
7568
7569         ulimit -c unlimited
7570         sleep 60 &
7571         SLEEPPID=$!
7572
7573         sleep 1
7574
7575         kill -s 11 $SLEEPPID
7576         wait $SLEEPPID
7577         if [ -e $file ]; then
7578                 size=`stat -c%s $file`
7579                 [ $size -eq 0 ] && error "Fail to create core file $file"
7580         else
7581                 error "Fail to create core file $file"
7582         fi
7583         rm -f $file
7584         sysctl -w kernel.core_pattern=$save_pattern
7585         sysctl -w kernel.core_uses_pid=$save_uses_pid
7586         cd $CDIR
7587 }
7588 run_test 107 "Coredump on SIG"
7589
7590 test_110() {
7591         test_mkdir -p $DIR/$tdir
7592         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7593                 error "mkdir with 255 char failed"
7594         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7595                 error "mkdir with 256 char should fail, but did not"
7596         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7597                 error "create with 255 char failed"
7598         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7599                 error "create with 256 char should fail, but did not"
7600
7601         ls -l $DIR/$tdir
7602         rm -rf $DIR/$tdir
7603 }
7604 run_test 110 "filename length checking"
7605
7606 test_115() {
7607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7608         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7609                 tail -1 | cut -c11-20)
7610         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7611         echo "Starting with $OSTIO_pre threads"
7612
7613         NUMTEST=20000
7614         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7615         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7616         echo "$NUMTEST creates/unlinks"
7617         test_mkdir -p $DIR/$tdir
7618         createmany -o $DIR/$tdir/$tfile $NUMTEST
7619         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7620
7621         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7622                 tail -1 | cut -c11-20)
7623
7624         # don't return an error
7625         [ $OSTIO_post == $OSTIO_pre ] && echo \
7626             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7627             echo "This may be fine, depending on what ran before this test" &&
7628             echo "and how fast this system is." && return
7629
7630         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7631 }
7632 run_test 115 "verify dynamic thread creation===================="
7633
7634 free_min_max () {
7635         wait_delete_completed
7636         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7637         echo "OST kbytes available: ${AVAIL[@]}"
7638         MAXV=${AVAIL[0]}
7639         MAXI=0
7640         MINV=${AVAIL[0]}
7641         MINI=0
7642         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7643                 #echo OST $i: ${AVAIL[i]}kb
7644                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7645                         MAXV=${AVAIL[i]}
7646                         MAXI=$i
7647                 fi
7648                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7649                         MINV=${AVAIL[i]}
7650                         MINI=$i
7651                 fi
7652         done
7653         echo "Min free space: OST $MINI: $MINV"
7654         echo "Max free space: OST $MAXI: $MAXV"
7655 }
7656
7657 test_116a() { # was previously test_116()
7658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7659         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7660
7661         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7662
7663         echo -n "Free space priority "
7664         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7665                 head -n1
7666         declare -a AVAIL
7667         free_min_max
7668
7669         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7670         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7671                 && return
7672         trap simple_cleanup_common EXIT
7673
7674
7675         # Check if we need to generate uneven OSTs
7676         test_mkdir -p $DIR/$tdir/OST${MINI}
7677         local FILL=$((MINV / 4))
7678         local DIFF=$((MAXV - MINV))
7679         local DIFF2=$((DIFF * 100 / MINV))
7680
7681         local threshold=$(do_facet $SINGLEMDS \
7682                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7683         threshold=${threshold%%%}
7684         echo -n "Check for uneven OSTs: "
7685         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7686
7687         if [[ $DIFF2 -gt $threshold ]]; then
7688                 echo "ok"
7689                 echo "Don't need to fill OST$MINI"
7690         else
7691                 # generate uneven OSTs. Write 2% over the QOS threshold value
7692                 echo "no"
7693                 DIFF=$((threshold - DIFF2 + 2))
7694                 DIFF2=$((MINV * DIFF / 100))
7695                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
7696                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7697                         error "setstripe failed"
7698                 DIFF=$((DIFF2 / 2048))
7699                 i=0
7700                 while [ $i -lt $DIFF ]; do
7701                         i=$((i + 1))
7702                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7703                                 bs=2M count=1 2>/dev/null
7704                         echo -n .
7705                 done
7706                 echo .
7707                 sync
7708                 sleep_maxage
7709                 free_min_max
7710         fi
7711
7712         DIFF=$((MAXV - MINV))
7713         DIFF2=$((DIFF * 100 / MINV))
7714         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
7715         if [ $DIFF2 -gt $threshold ]; then
7716                 echo "ok"
7717         else
7718                 echo "failed - QOS mode won't be used"
7719                 skip "QOS imbalance criteria not met"
7720                 simple_cleanup_common
7721                 return
7722         fi
7723
7724         MINI1=$MINI
7725         MINV1=$MINV
7726         MAXI1=$MAXI
7727         MAXV1=$MAXV
7728
7729         # now fill using QOS
7730         $SETSTRIPE -c 1 $DIR/$tdir
7731         FILL=$((FILL / 200))
7732         if [ $FILL -gt 600 ]; then
7733                 FILL=600
7734         fi
7735         echo "writing $FILL files to QOS-assigned OSTs"
7736         i=0
7737         while [ $i -lt $FILL ]; do
7738                 i=$((i + 1))
7739                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7740                         count=1 2>/dev/null
7741                 echo -n .
7742         done
7743         echo "wrote $i 200k files"
7744         sync
7745         sleep_maxage
7746
7747         echo "Note: free space may not be updated, so measurements might be off"
7748         free_min_max
7749         DIFF2=$((MAXV - MINV))
7750         echo "free space delta: orig $DIFF final $DIFF2"
7751         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7752         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
7753         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7754         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
7755         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7756         if [[ $DIFF -gt 0 ]]; then
7757                 FILL=$((DIFF2 * 100 / DIFF - 100))
7758                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7759         fi
7760
7761         # Figure out which files were written where
7762         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7763                awk '/'$MINI1': / {print $2; exit}')
7764         echo $UUID
7765         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7766         echo "$MINC files created on smaller OST $MINI1"
7767         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7768                awk '/'$MAXI1': / {print $2; exit}')
7769         echo $UUID
7770         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7771         echo "$MAXC files created on larger OST $MAXI1"
7772         if [[ $MINC -gt 0 ]]; then
7773                 FILL=$((MAXC * 100 / MINC - 100))
7774                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7775         fi
7776         [[ $MAXC -gt $MINC ]] ||
7777                 error_ignore LU-9 "stripe QOS didn't balance free space"
7778         simple_cleanup_common
7779 }
7780 run_test 116a "stripe QOS: free space balance ==================="
7781
7782 test_116b() { # LU-2093
7783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7784         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7785
7786 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7787         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7788                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7789         [ -z "$old_rr" ] && skip "no QOS" && return 0
7790         do_facet $SINGLEMDS lctl set_param \
7791                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7792         mkdir -p $DIR/$tdir
7793         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7794         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7795         do_facet $SINGLEMDS lctl set_param fail_loc=0
7796         rm -rf $DIR/$tdir
7797         do_facet $SINGLEMDS lctl set_param \
7798                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7799 }
7800 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7801
7802 test_117() # bug 10891
7803 {
7804         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7805         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7806         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7807         lctl set_param fail_loc=0x21e
7808         > $DIR/$tfile || error "truncate failed"
7809         lctl set_param fail_loc=0
7810         echo "Truncate succeeded."
7811         rm -f $DIR/$tfile
7812 }
7813 run_test 117 "verify osd extend =========="
7814
7815 NO_SLOW_RESENDCOUNT=4
7816 export OLD_RESENDCOUNT=""
7817 set_resend_count () {
7818         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7819         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7820         lctl set_param -n $PROC_RESENDCOUNT $1
7821         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7822 }
7823
7824 # for reduce test_118* time (b=14842)
7825 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7826
7827 # Reset async IO behavior after error case
7828 reset_async() {
7829         FILE=$DIR/reset_async
7830
7831         # Ensure all OSCs are cleared
7832         $SETSTRIPE -c -1 $FILE
7833         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7834         sync
7835         rm $FILE
7836 }
7837
7838 test_118a() #bug 11710
7839 {
7840         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7841         reset_async
7842
7843         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7844         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7845         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7846
7847         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7848                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7849                 return 1;
7850         fi
7851         rm -f $DIR/$tfile
7852 }
7853 run_test 118a "verify O_SYNC works =========="
7854
7855 test_118b()
7856 {
7857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7858         remote_ost_nodsh && skip "remote OST with nodsh" && return
7859
7860         reset_async
7861
7862         #define OBD_FAIL_SRV_ENOENT 0x217
7863         set_nodes_failloc "$(osts_nodes)" 0x217
7864         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7865         RC=$?
7866         set_nodes_failloc "$(osts_nodes)" 0
7867         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7868         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7869                     grep -c writeback)
7870
7871         if [[ $RC -eq 0 ]]; then
7872                 error "Must return error due to dropped pages, rc=$RC"
7873                 return 1;
7874         fi
7875
7876         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7877                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7878                 return 1;
7879         fi
7880
7881         echo "Dirty pages not leaked on ENOENT"
7882
7883         # Due to the above error the OSC will issue all RPCs syncronously
7884         # until a subsequent RPC completes successfully without error.
7885         $MULTIOP $DIR/$tfile Ow4096yc
7886         rm -f $DIR/$tfile
7887
7888         return 0
7889 }
7890 run_test 118b "Reclaim dirty pages on fatal error =========="
7891
7892 test_118c()
7893 {
7894         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7895
7896         # for 118c, restore the original resend count, LU-1940
7897         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7898                                 set_resend_count $OLD_RESENDCOUNT
7899         remote_ost_nodsh && skip "remote OST with nodsh" && return
7900
7901         reset_async
7902
7903         #define OBD_FAIL_OST_EROFS               0x216
7904         set_nodes_failloc "$(osts_nodes)" 0x216
7905
7906         # multiop should block due to fsync until pages are written
7907         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7908         MULTIPID=$!
7909         sleep 1
7910
7911         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7912                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7913         fi
7914
7915         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7916                     grep -c writeback)
7917         if [[ $WRITEBACK -eq 0 ]]; then
7918                 error "No page in writeback, writeback=$WRITEBACK"
7919         fi
7920
7921         set_nodes_failloc "$(osts_nodes)" 0
7922         wait $MULTIPID
7923         RC=$?
7924         if [[ $RC -ne 0 ]]; then
7925                 error "Multiop fsync failed, rc=$RC"
7926         fi
7927
7928         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7929         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7930                     grep -c writeback)
7931         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7932                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7933         fi
7934
7935         rm -f $DIR/$tfile
7936         echo "Dirty pages flushed via fsync on EROFS"
7937         return 0
7938 }
7939 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7940
7941 # continue to use small resend count to reduce test_118* time (b=14842)
7942 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7943
7944 test_118d()
7945 {
7946         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7947         remote_ost_nodsh && skip "remote OST with nodsh" && return
7948
7949         reset_async
7950
7951         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7952         set_nodes_failloc "$(osts_nodes)" 0x214
7953         # multiop should block due to fsync until pages are written
7954         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7955         MULTIPID=$!
7956         sleep 1
7957
7958         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7959                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7960         fi
7961
7962         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7963                     grep -c writeback)
7964         if [[ $WRITEBACK -eq 0 ]]; then
7965                 error "No page in writeback, writeback=$WRITEBACK"
7966         fi
7967
7968         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7969         set_nodes_failloc "$(osts_nodes)" 0
7970
7971         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7972         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7973                     grep -c writeback)
7974         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7975                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7976         fi
7977
7978         rm -f $DIR/$tfile
7979         echo "Dirty pages gaurenteed flushed via fsync"
7980         return 0
7981 }
7982 run_test 118d "Fsync validation inject a delay of the bulk =========="
7983
7984 test_118f() {
7985         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7986         reset_async
7987
7988         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7989         lctl set_param fail_loc=0x8000040a
7990
7991         # Should simulate EINVAL error which is fatal
7992         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7993         RC=$?
7994         if [[ $RC -eq 0 ]]; then
7995                 error "Must return error due to dropped pages, rc=$RC"
7996         fi
7997
7998         lctl set_param fail_loc=0x0
7999
8000         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8001         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8002         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8003                     grep -c writeback)
8004         if [[ $LOCKED -ne 0 ]]; then
8005                 error "Locked pages remain in cache, locked=$LOCKED"
8006         fi
8007
8008         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8009                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8010         fi
8011
8012         rm -f $DIR/$tfile
8013         echo "No pages locked after fsync"
8014
8015         reset_async
8016         return 0
8017 }
8018 run_test 118f "Simulate unrecoverable OSC side error =========="
8019
8020 test_118g() {
8021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8022         reset_async
8023
8024         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8025         lctl set_param fail_loc=0x406
8026
8027         # simulate local -ENOMEM
8028         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8029         RC=$?
8030
8031         lctl set_param fail_loc=0
8032         if [[ $RC -eq 0 ]]; then
8033                 error "Must return error due to dropped pages, rc=$RC"
8034         fi
8035
8036         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8037         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8038         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8039                         grep -c writeback)
8040         if [[ $LOCKED -ne 0 ]]; then
8041                 error "Locked pages remain in cache, locked=$LOCKED"
8042         fi
8043
8044         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8045                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8046         fi
8047
8048         rm -f $DIR/$tfile
8049         echo "No pages locked after fsync"
8050
8051         reset_async
8052         return 0
8053 }
8054 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
8055
8056 test_118h() {
8057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8058         remote_ost_nodsh && skip "remote OST with nodsh" && return
8059
8060         reset_async
8061
8062         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8063         set_nodes_failloc "$(osts_nodes)" 0x20e
8064         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8065         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8066         RC=$?
8067
8068         set_nodes_failloc "$(osts_nodes)" 0
8069         if [[ $RC -eq 0 ]]; then
8070                 error "Must return error due to dropped pages, rc=$RC"
8071         fi
8072
8073         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8074         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8075         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8076                     grep -c writeback)
8077         if [[ $LOCKED -ne 0 ]]; then
8078                 error "Locked pages remain in cache, locked=$LOCKED"
8079         fi
8080
8081         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8082                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8083         fi
8084
8085         rm -f $DIR/$tfile
8086         echo "No pages locked after fsync"
8087
8088         return 0
8089 }
8090 run_test 118h "Verify timeout in handling recoverables errors  =========="
8091
8092 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8093
8094 test_118i() {
8095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8096         remote_ost_nodsh && skip "remote OST with nodsh" && return
8097
8098         reset_async
8099
8100         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8101         set_nodes_failloc "$(osts_nodes)" 0x20e
8102
8103         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8104         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8105         PID=$!
8106         sleep 5
8107         set_nodes_failloc "$(osts_nodes)" 0
8108
8109         wait $PID
8110         RC=$?
8111         if [[ $RC -ne 0 ]]; then
8112                 error "got error, but should be not, rc=$RC"
8113         fi
8114
8115         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8116         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8117         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8118         if [[ $LOCKED -ne 0 ]]; then
8119                 error "Locked pages remain in cache, locked=$LOCKED"
8120         fi
8121
8122         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8123                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8124         fi
8125
8126         rm -f $DIR/$tfile
8127         echo "No pages locked after fsync"
8128
8129         return 0
8130 }
8131 run_test 118i "Fix error before timeout in recoverable error  =========="
8132
8133 [ "$SLOW" = "no" ] && set_resend_count 4
8134
8135 test_118j() {
8136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8137         remote_ost_nodsh && skip "remote OST with nodsh" && return
8138
8139         reset_async
8140
8141         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8142         set_nodes_failloc "$(osts_nodes)" 0x220
8143
8144         # return -EIO from OST
8145         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8146         RC=$?
8147         set_nodes_failloc "$(osts_nodes)" 0x0
8148         if [[ $RC -eq 0 ]]; then
8149                 error "Must return error due to dropped pages, rc=$RC"
8150         fi
8151
8152         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8153         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8154         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8155         if [[ $LOCKED -ne 0 ]]; then
8156                 error "Locked pages remain in cache, locked=$LOCKED"
8157         fi
8158
8159         # in recoverable error on OST we want resend and stay until it finished
8160         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8161                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8162         fi
8163
8164         rm -f $DIR/$tfile
8165         echo "No pages locked after fsync"
8166
8167         return 0
8168 }
8169 run_test 118j "Simulate unrecoverable OST side error =========="
8170
8171 test_118k()
8172 {
8173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8174         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8175
8176         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8177         set_nodes_failloc "$(osts_nodes)" 0x20e
8178         test_mkdir -p $DIR/$tdir
8179
8180         for ((i=0;i<10;i++)); do
8181                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8182                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8183                 SLEEPPID=$!
8184                 sleep 0.500s
8185                 kill $SLEEPPID
8186                 wait $SLEEPPID
8187         done
8188
8189         set_nodes_failloc "$(osts_nodes)" 0
8190         rm -rf $DIR/$tdir
8191 }
8192 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8193
8194 test_118l()
8195 {
8196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8197         # LU-646
8198         test_mkdir -p $DIR/$tdir
8199         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8200         rm -rf $DIR/$tdir
8201 }
8202 run_test 118l "fsync dir ========="
8203
8204 test_118m() # LU-3066
8205 {
8206         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8207         test_mkdir -p $DIR/$tdir
8208         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8209         rm -rf $DIR/$tdir
8210 }
8211 run_test 118m "fdatasync dir ========="
8212
8213 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8214
8215 test_119a() # bug 11737
8216 {
8217         BSIZE=$((512 * 1024))
8218         directio write $DIR/$tfile 0 1 $BSIZE
8219         # We ask to read two blocks, which is more than a file size.
8220         # directio will indicate an error when requested and actual
8221         # sizes aren't equeal (a normal situation in this case) and
8222         # print actual read amount.
8223         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8224         if [ "$NOB" != "$BSIZE" ]; then
8225                 error "read $NOB bytes instead of $BSIZE"
8226         fi
8227         rm -f $DIR/$tfile
8228 }
8229 run_test 119a "Short directIO read must return actual read amount"
8230
8231 test_119b() # bug 11737
8232 {
8233         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
8234
8235         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8236         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8237         sync
8238         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8239                 error "direct read failed"
8240         rm -f $DIR/$tfile
8241 }
8242 run_test 119b "Sparse directIO read must return actual read amount"
8243
8244 test_119c() # bug 13099
8245 {
8246         BSIZE=1048576
8247         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8248         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8249         rm -f $DIR/$tfile
8250 }
8251 run_test 119c "Testing for direct read hitting hole"
8252
8253 test_119d() # bug 15950
8254 {
8255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8256         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8257         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8258         BSIZE=1048576
8259         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8260         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8261         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8262         lctl set_param fail_loc=0x40d
8263         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8264         pid_dio=$!
8265         sleep 1
8266         cat $DIR/$tfile > /dev/null &
8267         lctl set_param fail_loc=0
8268         pid_reads=$!
8269         wait $pid_dio
8270         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8271         sleep 2
8272         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8273         error "the read rpcs have not completed in 2s"
8274         rm -f $DIR/$tfile
8275         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8276 }
8277 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8278
8279 test_120a() {
8280         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8281         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8282         test_mkdir -p $DIR/$tdir
8283         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8284                skip "no early lock cancel on server" && return 0
8285
8286         lru_resize_disable mdc
8287         lru_resize_disable osc
8288         cancel_lru_locks mdc
8289         # asynchronous object destroy at MDT could cause bl ast to client
8290         cancel_lru_locks osc
8291
8292         stat $DIR/$tdir > /dev/null
8293         can1=$(do_facet $SINGLEMDS \
8294                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8295                awk '/ldlm_cancel/ {print $2}')
8296         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8297                awk '/ldlm_bl_callback/ {print $2}')
8298         test_mkdir -c1 $DIR/$tdir/d1
8299         can2=$(do_facet $SINGLEMDS \
8300                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8301                awk '/ldlm_cancel/ {print $2}')
8302         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8303                awk '/ldlm_bl_callback/ {print $2}')
8304         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8305         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8306         lru_resize_enable mdc
8307         lru_resize_enable osc
8308 }
8309 run_test 120a "Early Lock Cancel: mkdir test"
8310
8311 test_120b() {
8312         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8313         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8314         test_mkdir $DIR/$tdir
8315         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8316                skip "no early lock cancel on server" && return 0
8317         lru_resize_disable mdc
8318         lru_resize_disable osc
8319         cancel_lru_locks mdc
8320         stat $DIR/$tdir > /dev/null
8321         can1=$(do_facet $SINGLEMDS \
8322                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8323                awk '/ldlm_cancel/ {print $2}')
8324         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8325                awk '/ldlm_bl_callback/ {print $2}')
8326         touch $DIR/$tdir/f1
8327         can2=$(do_facet $SINGLEMDS \
8328                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8329                awk '/ldlm_cancel/ {print $2}')
8330         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8331                awk '/ldlm_bl_callback/ {print $2}')
8332         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8333         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8334         lru_resize_enable mdc
8335         lru_resize_enable osc
8336 }
8337 run_test 120b "Early Lock Cancel: create test"
8338
8339 test_120c() {
8340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8341         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8342         test_mkdir -c1 $DIR/$tdir
8343         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8344                skip "no early lock cancel on server" && return 0
8345         lru_resize_disable mdc
8346         lru_resize_disable osc
8347         test_mkdir -p -c1 $DIR/$tdir/d1
8348         test_mkdir -p -c1 $DIR/$tdir/d2
8349         touch $DIR/$tdir/d1/f1
8350         cancel_lru_locks mdc
8351         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8352         can1=$(do_facet $SINGLEMDS \
8353                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8354                awk '/ldlm_cancel/ {print $2}')
8355         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8356                awk '/ldlm_bl_callback/ {print $2}')
8357         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8358         can2=$(do_facet $SINGLEMDS \
8359                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8360                awk '/ldlm_cancel/ {print $2}')
8361         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8362                awk '/ldlm_bl_callback/ {print $2}')
8363         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8364         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8365         lru_resize_enable mdc
8366         lru_resize_enable osc
8367 }
8368 run_test 120c "Early Lock Cancel: link test"
8369
8370 test_120d() {
8371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8372         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8373         test_mkdir -p -c1 $DIR/$tdir
8374         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8375                skip "no early lock cancel on server" && return 0
8376         lru_resize_disable mdc
8377         lru_resize_disable osc
8378         touch $DIR/$tdir
8379         cancel_lru_locks mdc
8380         stat $DIR/$tdir > /dev/null
8381         can1=$(do_facet $SINGLEMDS \
8382                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8383                awk '/ldlm_cancel/ {print $2}')
8384         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8385                awk '/ldlm_bl_callback/ {print $2}')
8386         chmod a+x $DIR/$tdir
8387         can2=$(do_facet $SINGLEMDS \
8388                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8389                awk '/ldlm_cancel/ {print $2}')
8390         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8391                awk '/ldlm_bl_callback/ {print $2}')
8392         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8393         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8394         lru_resize_enable mdc
8395         lru_resize_enable osc
8396 }
8397 run_test 120d "Early Lock Cancel: setattr test"
8398
8399 test_120e() {
8400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8401         ! $($LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_can) &&
8402                 skip "no early lock cancel on server" && return 0
8403         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8404         local dlmtrace_set=false
8405
8406         test_mkdir -p -c1 $DIR/$tdir
8407         lru_resize_disable mdc
8408         lru_resize_disable osc
8409         ! $LCTL get_param debug | grep -q dlmtrace &&
8410                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
8411         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8412         cancel_lru_locks mdc
8413         cancel_lru_locks osc
8414         dd if=$DIR/$tdir/f1 of=/dev/null
8415         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8416         # XXX client can not do early lock cancel of OST lock
8417         # during unlink (LU-4206), so cancel osc lock now.
8418         sleep 2
8419         cancel_lru_locks osc
8420         can1=$(do_facet $SINGLEMDS \
8421                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8422                awk '/ldlm_cancel/ {print $2}')
8423         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8424                awk '/ldlm_bl_callback/ {print $2}')
8425         unlink $DIR/$tdir/f1
8426         sleep 5
8427         can2=$(do_facet $SINGLEMDS \
8428                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8429                awk '/ldlm_cancel/ {print $2}')
8430         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8431                awk '/ldlm_bl_callback/ {print $2}')
8432         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
8433                 $LCTL dk $TMP/cancel.debug.txt
8434         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
8435                 $LCTL dk $TMP/blocking.debug.txt
8436         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
8437         lru_resize_enable mdc
8438         lru_resize_enable osc
8439 }
8440 run_test 120e "Early Lock Cancel: unlink test"
8441
8442 test_120f() {
8443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8444         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8445                skip "no early lock cancel on server" && return 0
8446         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8447         test_mkdir -p -c1 $DIR/$tdir
8448         lru_resize_disable mdc
8449         lru_resize_disable osc
8450         test_mkdir -p -c1 $DIR/$tdir/d1
8451         test_mkdir -p -c1 $DIR/$tdir/d2
8452         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8453         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8454         cancel_lru_locks mdc
8455         cancel_lru_locks osc
8456         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8457         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8458         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8459         # XXX client can not do early lock cancel of OST lock
8460         # during rename (LU-4206), so cancel osc lock now.
8461         sleep 2
8462         cancel_lru_locks osc
8463         can1=$(do_facet $SINGLEMDS \
8464                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8465                awk '/ldlm_cancel/ {print $2}')
8466         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8467                awk '/ldlm_bl_callback/ {print $2}')
8468         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8469         sleep 5
8470         can2=$(do_facet $SINGLEMDS \
8471                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8472                awk '/ldlm_cancel/ {print $2}')
8473         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8474                awk '/ldlm_bl_callback/ {print $2}')
8475         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8476         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8477         lru_resize_enable mdc
8478         lru_resize_enable osc
8479 }
8480 run_test 120f "Early Lock Cancel: rename test"
8481
8482 test_120g() {
8483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8484         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8485                skip "no early lock cancel on server" && return 0
8486         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8487         lru_resize_disable mdc
8488         lru_resize_disable osc
8489         count=10000
8490         echo create $count files
8491         test_mkdir -p $DIR/$tdir
8492         cancel_lru_locks mdc
8493         cancel_lru_locks osc
8494         t0=`date +%s`
8495
8496         can0=$(do_facet $SINGLEMDS \
8497                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8498                awk '/ldlm_cancel/ {print $2}')
8499         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8500                awk '/ldlm_bl_callback/ {print $2}')
8501         createmany -o $DIR/$tdir/f $count
8502         sync
8503         can1=$(do_facet $SINGLEMDS \
8504                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8505                awk '/ldlm_cancel/ {print $2}')
8506         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8507                awk '/ldlm_bl_callback/ {print $2}')
8508         t1=$(date +%s)
8509         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8510         echo rm $count files
8511         rm -r $DIR/$tdir
8512         sync
8513         can2=$(do_facet $SINGLEMDS \
8514                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8515                awk '/ldlm_cancel/ {print $2}')
8516         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8517                awk '/ldlm_bl_callback/ {print $2}')
8518         t2=$(date +%s)
8519         echo total: $count removes in $((t2-t1))
8520         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8521         sleep 2
8522         # wait for commitment of removal
8523         lru_resize_enable mdc
8524         lru_resize_enable osc
8525 }
8526 run_test 120g "Early Lock Cancel: performance test"
8527
8528 test_121() { #bug #10589
8529         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8530         rm -rf $DIR/$tfile
8531         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8532 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8533         lctl set_param fail_loc=0x310
8534         cancel_lru_locks osc > /dev/null
8535         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8536         lctl set_param fail_loc=0
8537         [[ $reads -eq $writes ]] ||
8538                 error "read $reads blocks, must be $writes blocks"
8539 }
8540 run_test 121 "read cancel race ========="
8541
8542 test_123a() { # was test 123, statahead(bug 11401)
8543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8544         SLOWOK=0
8545         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8546             log "testing on UP system. Performance may be not as good as expected."
8547                         SLOWOK=1
8548         fi
8549
8550         rm -rf $DIR/$tdir
8551         test_mkdir -p $DIR/$tdir
8552         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8553         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8554         MULT=10
8555         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8556                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8557
8558                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8559                 lctl set_param -n llite.*.statahead_max 0
8560                 lctl get_param llite.*.statahead_max
8561                 cancel_lru_locks mdc
8562                 cancel_lru_locks osc
8563                 stime=`date +%s`
8564                 time ls -l $DIR/$tdir | wc -l
8565                 etime=`date +%s`
8566                 delta=$((etime - stime))
8567                 log "ls $i files without statahead: $delta sec"
8568                 lctl set_param llite.*.statahead_max=$max
8569
8570                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8571                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8572                 cancel_lru_locks mdc
8573                 cancel_lru_locks osc
8574                 stime=`date +%s`
8575                 time ls -l $DIR/$tdir | wc -l
8576                 etime=`date +%s`
8577                 delta_sa=$((etime - stime))
8578                 log "ls $i files with statahead: $delta_sa sec"
8579                 lctl get_param -n llite.*.statahead_stats
8580                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8581
8582                 [[ $swrong -lt $ewrong ]] &&
8583                         log "statahead was stopped, maybe too many locks held!"
8584                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8585
8586                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8587                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8588                     lctl set_param -n llite.*.statahead_max 0
8589                     lctl get_param llite.*.statahead_max
8590                     cancel_lru_locks mdc
8591                     cancel_lru_locks osc
8592                     stime=`date +%s`
8593                     time ls -l $DIR/$tdir | wc -l
8594                     etime=`date +%s`
8595                     delta=$((etime - stime))
8596                     log "ls $i files again without statahead: $delta sec"
8597                     lctl set_param llite.*.statahead_max=$max
8598                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8599                         if [  $SLOWOK -eq 0 ]; then
8600                                 error "ls $i files is slower with statahead!"
8601                         else
8602                                 log "ls $i files is slower with statahead!"
8603                         fi
8604                         break
8605                     fi
8606                 fi
8607
8608                 [ $delta -gt 20 ] && break
8609                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8610                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8611         done
8612         log "ls done"
8613
8614         stime=`date +%s`
8615         rm -r $DIR/$tdir
8616         sync
8617         etime=`date +%s`
8618         delta=$((etime - stime))
8619         log "rm -r $DIR/$tdir/: $delta seconds"
8620         log "rm done"
8621         lctl get_param -n llite.*.statahead_stats
8622 }
8623 run_test 123a "verify statahead work"
8624
8625 test_123b () { # statahead(bug 15027)
8626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8627         test_mkdir -p $DIR/$tdir
8628         createmany -o $DIR/$tdir/$tfile-%d 1000
8629
8630         cancel_lru_locks mdc
8631         cancel_lru_locks osc
8632
8633 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8634         lctl set_param fail_loc=0x80000803
8635         ls -lR $DIR/$tdir > /dev/null
8636         log "ls done"
8637         lctl set_param fail_loc=0x0
8638         lctl get_param -n llite.*.statahead_stats
8639         rm -r $DIR/$tdir
8640         sync
8641
8642 }
8643 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8644
8645 test_124a() {
8646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8647         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8648                 skip "no lru resize on server" && return 0
8649         local NR=2000
8650         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8651
8652         log "create $NR files at $DIR/$tdir"
8653         createmany -o $DIR/$tdir/f $NR ||
8654                 error "failed to create $NR files in $DIR/$tdir"
8655
8656         cancel_lru_locks mdc
8657         ls -l $DIR/$tdir > /dev/null
8658
8659         local NSDIR=""
8660         local LRU_SIZE=0
8661         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8662                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8663                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8664                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8665                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8666                         log "NSDIR=$NSDIR"
8667                         log "NS=$(basename $NSDIR)"
8668                         break
8669                 fi
8670         done
8671
8672         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8673                 skip "Not enough cached locks created!"
8674                 return 0
8675         fi
8676         log "LRU=$LRU_SIZE"
8677
8678         local SLEEP=30
8679
8680         # We know that lru resize allows one client to hold $LIMIT locks
8681         # for 10h. After that locks begin to be killed by client.
8682         local MAX_HRS=10
8683         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8684         log "LIMIT=$LIMIT"
8685         if [ $LIMIT -lt $LRU_SIZE ]; then
8686             skip "Limit is too small $LIMIT"
8687             return 0
8688         fi
8689
8690         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8691         # killing locks. Some time was spent for creating locks. This means
8692         # that up to the moment of sleep finish we must have killed some of
8693         # them (10-100 locks). This depends on how fast ther were created.
8694         # Many of them were touched in almost the same moment and thus will
8695         # be killed in groups.
8696         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8697
8698         # Use $LRU_SIZE_B here to take into account real number of locks
8699         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8700         local LRU_SIZE_B=$LRU_SIZE
8701         log "LVF=$LVF"
8702         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8703         log "OLD_LVF=$OLD_LVF"
8704         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8705
8706         # Let's make sure that we really have some margin. Client checks
8707         # cached locks every 10 sec.
8708         SLEEP=$((SLEEP+20))
8709         log "Sleep ${SLEEP} sec"
8710         local SEC=0
8711         while ((SEC<$SLEEP)); do
8712                 echo -n "..."
8713                 sleep 5
8714                 SEC=$((SEC+5))
8715                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8716                 echo -n "$LRU_SIZE"
8717         done
8718         echo ""
8719         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8720         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8721
8722         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8723                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8724                 unlinkmany $DIR/$tdir/f $NR
8725                 return
8726         }
8727
8728         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8729         log "unlink $NR files at $DIR/$tdir"
8730         unlinkmany $DIR/$tdir/f $NR
8731 }
8732 run_test 124a "lru resize ======================================="
8733
8734 get_max_pool_limit()
8735 {
8736         local limit=$($LCTL get_param \
8737                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8738         local max=0
8739         for l in $limit; do
8740                 if [[ $l -gt $max ]]; then
8741                         max=$l
8742                 fi
8743         done
8744         echo $max
8745 }
8746
8747 test_124b() {
8748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8749         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8750                 skip "no lru resize on server" && return 0
8751
8752         LIMIT=$(get_max_pool_limit)
8753
8754         NR=$(($(default_lru_size)*20))
8755         if [[ $NR -gt $LIMIT ]]; then
8756                 log "Limit lock number by $LIMIT locks"
8757                 NR=$LIMIT
8758         fi
8759
8760         IFree=$(mdsrate_inodes_available)
8761         if [ $IFree -lt $NR ]; then
8762                 log "Limit lock number by $IFree inodes"
8763                 NR=$IFree
8764         fi
8765
8766         lru_resize_disable mdc
8767         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8768                 error "failed to create $DIR/$tdir/disable_lru_resize"
8769
8770         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8771         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8772         cancel_lru_locks mdc
8773         stime=`date +%s`
8774         PID=""
8775         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8776         PID="$PID $!"
8777         sleep 2
8778         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8779         PID="$PID $!"
8780         sleep 2
8781         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8782         PID="$PID $!"
8783         wait $PID
8784         etime=`date +%s`
8785         nolruresize_delta=$((etime-stime))
8786         log "ls -la time: $nolruresize_delta seconds"
8787         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8788         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8789
8790         lru_resize_enable mdc
8791         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8792                 error "failed to create $DIR/$tdir/enable_lru_resize"
8793
8794         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8795         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8796         cancel_lru_locks mdc
8797         stime=`date +%s`
8798         PID=""
8799         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8800         PID="$PID $!"
8801         sleep 2
8802         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8803         PID="$PID $!"
8804         sleep 2
8805         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8806         PID="$PID $!"
8807         wait $PID
8808         etime=`date +%s`
8809         lruresize_delta=$((etime-stime))
8810         log "ls -la time: $lruresize_delta seconds"
8811         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8812
8813         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8814                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8815         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8816                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8817         else
8818                 log "lru resize performs the same with no lru resize"
8819         fi
8820         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8821 }
8822 run_test 124b "lru resize (performance test) ======================="
8823
8824 test_124c() {
8825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8826         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8827                 skip "no lru resize on server" && return 0
8828
8829         # cache ununsed locks on client
8830         local nr=100
8831         cancel_lru_locks mdc
8832         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8833         createmany -o $DIR/$tdir/f $nr ||
8834                 error "failed to create $nr files in $DIR/$tdir"
8835         ls -l $DIR/$tdir > /dev/null
8836
8837         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8838         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8839         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8840         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8841         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8842
8843         # set lru_max_age to 1 sec
8844         $LCTL set_param $nsdir.lru_max_age=1000 # jiffies
8845         echo "sleep $((recalc_p * 2)) seconds..."
8846         sleep $((recalc_p * 2))
8847
8848         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8849         # restore lru_max_age
8850         $LCTL set_param -n $nsdir.lru_max_age $max_age
8851         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8852         unlinkmany $DIR/$tdir/f $nr
8853 }
8854 run_test 124c "LRUR cancel very aged locks"
8855
8856 test_125() { # 13358
8857         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8858         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8859         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8860         test_mkdir -p $DIR/d125 || error "mkdir failed"
8861         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8862         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8863         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8864 }
8865 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8866
8867 test_126() { # bug 12829/13455
8868         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8869         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8870         $GSS && skip "must run as gss disabled" && return
8871
8872         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8873         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8874         rm -f $DIR/$tfile
8875         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8876 }
8877 run_test 126 "check that the fsgid provided by the client is taken into account"
8878
8879 test_127a() { # bug 15521
8880         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8881         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8882         $LCTL set_param osc.*.stats=0
8883         FSIZE=$((2048 * 1024))
8884         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8885         cancel_lru_locks osc
8886         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8887
8888         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8889         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8890                 echo "got $COUNT $NAME"
8891                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8892                 eval $NAME=$COUNT || error "Wrong proc format"
8893
8894                 case $NAME in
8895                         read_bytes|write_bytes)
8896                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8897                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8898                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8899                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8900                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8901                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8902                                 error "sumsquare is too small: $SUMSQ"
8903                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8904                                 error "sumsquare is too big: $SUMSQ"
8905                         ;;
8906                         *) ;;
8907                 esac
8908         done < $DIR/${tfile}.tmp
8909
8910         #check that we actually got some stats
8911         [ "$read_bytes" ] || error "Missing read_bytes stats"
8912         [ "$write_bytes" ] || error "Missing write_bytes stats"
8913         [ "$read_bytes" != 0 ] || error "no read done"
8914         [ "$write_bytes" != 0 ] || error "no write done"
8915 }
8916 run_test 127a "verify the client stats are sane"
8917
8918 test_127b() { # bug LU-333
8919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8920         $LCTL set_param llite.*.stats=0
8921         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8922         # perform 2 reads and writes so MAX is different from SUM.
8923         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8924         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8925         cancel_lru_locks osc
8926         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8927         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8928
8929         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8930         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8931                 echo "got $COUNT $NAME"
8932                 eval $NAME=$COUNT || error "Wrong proc format"
8933
8934         case $NAME in
8935                 read_bytes)
8936                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8937                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8938                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8939                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8940                         ;;
8941                 write_bytes)
8942                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8943                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8944                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8945                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8946                         ;;
8947                         *) ;;
8948                 esac
8949         done < $TMP/${tfile}.tmp
8950
8951         #check that we actually got some stats
8952         [ "$read_bytes" ] || error "Missing read_bytes stats"
8953         [ "$write_bytes" ] || error "Missing write_bytes stats"
8954         [ "$read_bytes" != 0 ] || error "no read done"
8955         [ "$write_bytes" != 0 ] || error "no write done"
8956
8957         rm -f $TMP/${tfile}.tmp
8958 }
8959 run_test 127b "verify the llite client stats are sane"
8960
8961 test_128() { # bug 15212
8962         touch $DIR/$tfile
8963         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8964                 find $DIR/$tfile
8965                 find $DIR/$tfile
8966         EOF
8967
8968         result=$(grep error $TMP/$tfile.log)
8969         rm -f $DIR/$tfile $TMP/$tfile.log
8970         [ -z "$result" ] ||
8971                 error "consecutive find's under interactive lfs failed"
8972 }
8973 run_test 128 "interactive lfs for 2 consecutive find's"
8974
8975 set_dir_limits () {
8976         local mntdev
8977         local canondev
8978         local node
8979
8980         local LDPROC=/proc/fs/ldiskfs
8981         local facets=$(get_facets MDS)
8982
8983         for facet in ${facets//,/ }; do
8984                 canondev=$(ldiskfs_canon \
8985                            *.$(convert_facet2label $facet).mntdev $facet)
8986                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8987                                                 LDPROC=/sys/fs/ldiskfs
8988                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8989                 do_facet $facet "test -e $LDPROC/$canondev/warning_dir_size" ||
8990                                                 LDPROC=/sys/fs/ldiskfs
8991                 do_facet $facet "echo $2 >$LDPROC/$canondev/warning_dir_size"
8992         done
8993 }
8994
8995 check_mds_dmesg() {
8996         local facets=$(get_facets MDS)
8997         for facet in ${facets//,/ }; do
8998                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
8999         done
9000         return 1
9001 }
9002
9003 test_129() {
9004         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
9005                 { skip "Need MDS version with at least 2.5.56"; return 0; }
9006
9007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9008         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
9009                 skip "Only applicable to ldiskfs-based MDTs"
9010                 return
9011         fi
9012         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9013         local ENOSPC=28
9014         local EFBIG=27
9015         local has_warning=0
9016
9017         rm -rf $DIR/$tdir
9018         mkdir -p $DIR/$tdir
9019
9020         # block size of mds1
9021         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9022         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
9023         local MAX=$((MDSBLOCKSIZE * 5))
9024         set_dir_limits $MAX $MAX
9025         local I=$(stat -c%s "$DIR/$tdir")
9026         local J=0
9027         while [[ $I -le $MAX ]]; do
9028                 $MULTIOP $DIR/$tdir/$J Oc
9029                 rc=$?
9030                 if [ $has_warning -eq 0 ]; then
9031                         check_mds_dmesg '"is approaching"' &&
9032                                 has_warning=1
9033                 fi
9034                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
9035                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
9036                 #and EFBIG for previous versions
9037                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
9038                         set_dir_limits 0 0
9039                         echo "return code $rc received as expected"
9040
9041                         createmany -o $DIR/$tdir/$J_file_ 1000 ||
9042                                 error_exit "create failed w/o dir size limit"
9043
9044                         check_mds_dmesg '"has reached"' ||
9045                                 error_exit "has reached message should be output"
9046
9047                         [ $has_warning -eq 0 ] &&
9048                                 error_exit "warning message should be output"
9049
9050                         I=$(stat -c%s "$DIR/$tdir")
9051
9052                         if [ $(lustre_version_code $SINGLEMDS) -lt \
9053                                         $(version_code 2.4.51) ]
9054                         then
9055                                 [[ $I -eq $MAX ]] && return 0
9056                         else
9057                                 [[ $I -gt $MAX ]] && return 0
9058                         fi
9059                         error_exit "current dir size $I, previous limit $MAX"
9060                 elif [ $rc -ne 0 ]; then
9061                         set_dir_limits 0 0
9062                         error_exit "return code $rc received instead of expected " \
9063                                    "$EFBIG or $ENOSPC, files in dir $I"
9064                 fi
9065                 J=$((J+1))
9066                 I=$(stat -c%s "$DIR/$tdir")
9067         done
9068
9069         set_dir_limits 0 0
9070         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
9071 }
9072 run_test 129 "test directory size limit ========================"
9073
9074 OLDIFS="$IFS"
9075 cleanup_130() {
9076         trap 0
9077         IFS="$OLDIFS"
9078 }
9079
9080 test_130a() {
9081         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9082         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9083                 return
9084
9085         trap cleanup_130 EXIT RETURN
9086
9087         local fm_file=$DIR/$tfile
9088         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9089         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9090                 error "dd failed for $fm_file"
9091
9092         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9093         filefrag -ves $fm_file
9094         RC=$?
9095         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9096                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9097         [ $RC != 0 ] && error "filefrag $fm_file failed"
9098
9099         filefrag_op=$(filefrag -ve $fm_file |
9100                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9101         lun=$($GETSTRIPE -i $fm_file)
9102
9103         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9104         IFS=$'\n'
9105         tot_len=0
9106         for line in $filefrag_op
9107         do
9108                 frag_lun=`echo $line | cut -d: -f5`
9109                 ext_len=`echo $line | cut -d: -f4`
9110                 if (( $frag_lun != $lun )); then
9111                         cleanup_130
9112                         error "FIEMAP on 1-stripe file($fm_file) failed"
9113                         return
9114                 fi
9115                 (( tot_len += ext_len ))
9116         done
9117
9118         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9119                 cleanup_130
9120                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9121                 return
9122         fi
9123
9124         cleanup_130
9125
9126         echo "FIEMAP on single striped file succeeded"
9127 }
9128 run_test 130a "FIEMAP (1-stripe file)"
9129
9130 test_130b() {
9131         [ "$OSTCOUNT" -lt "2" ] &&
9132                 skip_env "skipping FIEMAP on $OSTCOUNT-stripe file" && return
9133
9134         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9135         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9136                 return
9137
9138         trap cleanup_130 EXIT RETURN
9139
9140         local fm_file=$DIR/$tfile
9141         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9142                         error "setstripe on $fm_file"
9143         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9144                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9145
9146         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9147                 error "dd failed on $fm_file"
9148
9149         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9150         filefrag_op=$(filefrag -ve $fm_file |
9151                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9152
9153         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9154                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9155
9156         IFS=$'\n'
9157         tot_len=0
9158         num_luns=1
9159         for line in $filefrag_op
9160         do
9161                 frag_lun=$(echo $line | cut -d: -f5 |
9162                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9163                 ext_len=$(echo $line | cut -d: -f4)
9164                 if (( $frag_lun != $last_lun )); then
9165                         if (( tot_len != 1024 )); then
9166                                 cleanup_130
9167                                 error "FIEMAP on $fm_file failed; returned " \
9168                                 "len $tot_len for OST $last_lun instead of 1024"
9169                                 return
9170                         else
9171                                 (( num_luns += 1 ))
9172                                 tot_len=0
9173                         fi
9174                 fi
9175                 (( tot_len += ext_len ))
9176                 last_lun=$frag_lun
9177         done
9178         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9179                 cleanup_130
9180                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9181                         "luns or wrong len for OST $last_lun"
9182                 return
9183         fi
9184
9185         cleanup_130
9186
9187         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9188 }
9189 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9190
9191 test_130c() {
9192         [ "$OSTCOUNT" -lt "2" ] &&
9193                 skip_env "skipping FIEMAP on 2-stripe file" && return
9194
9195         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9196         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9197                 return
9198
9199         trap cleanup_130 EXIT RETURN
9200
9201         local fm_file=$DIR/$tfile
9202         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9203         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9204                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9205
9206         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9207                         error "dd failed on $fm_file"
9208
9209         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9210         filefrag_op=$(filefrag -ve $fm_file |
9211                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9212
9213         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9214                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9215
9216         IFS=$'\n'
9217         tot_len=0
9218         num_luns=1
9219         for line in $filefrag_op
9220         do
9221                 frag_lun=$(echo $line | cut -d: -f5 |
9222                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9223                 ext_len=$(echo $line | cut -d: -f4)
9224                 if (( $frag_lun != $last_lun )); then
9225                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9226                         if (( logical != 512 )); then
9227                                 cleanup_130
9228                                 error "FIEMAP on $fm_file failed; returned " \
9229                                 "logical start for lun $logical instead of 512"
9230                                 return
9231                         fi
9232                         if (( tot_len != 512 )); then
9233                                 cleanup_130
9234                                 error "FIEMAP on $fm_file failed; returned " \
9235                                 "len $tot_len for OST $last_lun instead of 1024"
9236                                 return
9237                         else
9238                                 (( num_luns += 1 ))
9239                                 tot_len=0
9240                         fi
9241                 fi
9242                 (( tot_len += ext_len ))
9243                 last_lun=$frag_lun
9244         done
9245         if (( num_luns != 2 || tot_len != 512 )); then
9246                 cleanup_130
9247                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9248                         "luns or wrong len for OST $last_lun"
9249                 return
9250         fi
9251
9252         cleanup_130
9253
9254         echo "FIEMAP on 2-stripe file with hole succeeded"
9255 }
9256 run_test 130c "FIEMAP (2-stripe file with hole)"
9257
9258 test_130d() {
9259         [ "$OSTCOUNT" -lt "3" ] &&
9260                 skip_env "skipping FIEMAP on N-stripe file test" && return
9261
9262         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9263         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9264                 return
9265
9266         trap cleanup_130 EXIT RETURN
9267
9268         local fm_file=$DIR/$tfile
9269         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9270                         error "setstripe on $fm_file"
9271         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9272                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9273
9274         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
9275         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
9276                 error "dd failed on $fm_file"
9277
9278         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9279         filefrag_op=$(filefrag -ve $fm_file |
9280                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9281
9282         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9283                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9284
9285         IFS=$'\n'
9286         tot_len=0
9287         num_luns=1
9288         for line in $filefrag_op
9289         do
9290                 frag_lun=$(echo $line | cut -d: -f5 |
9291                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9292                 ext_len=$(echo $line | cut -d: -f4)
9293                 if (( $frag_lun != $last_lun )); then
9294                         if (( tot_len != 1024 )); then
9295                                 cleanup_130
9296                                 error "FIEMAP on $fm_file failed; returned " \
9297                                 "len $tot_len for OST $last_lun instead of 1024"
9298                                 return
9299                         else
9300                                 (( num_luns += 1 ))
9301                                 tot_len=0
9302                         fi
9303                 fi
9304                 (( tot_len += ext_len ))
9305                 last_lun=$frag_lun
9306         done
9307         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
9308                 cleanup_130
9309                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9310                         "luns or wrong len for OST $last_lun"
9311                 return
9312         fi
9313
9314         cleanup_130
9315
9316         echo "FIEMAP on N-stripe file succeeded"
9317 }
9318 run_test 130d "FIEMAP (N-stripe file)"
9319
9320 test_130e() {
9321         [ "$OSTCOUNT" -lt "2" ] &&
9322                 skip_env "skipping continuation FIEMAP test" && return
9323
9324         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9325         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9326
9327         trap cleanup_130 EXIT RETURN
9328
9329         local fm_file=$DIR/$tfile
9330         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9331         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9332                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9333
9334         NUM_BLKS=512
9335         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9336         for ((i = 0; i < $NUM_BLKS; i++))
9337         do
9338                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9339         done
9340
9341         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9342         filefrag_op=$(filefrag -ve $fm_file |
9343                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9344
9345         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9346                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9347
9348         IFS=$'\n'
9349         tot_len=0
9350         num_luns=1
9351         for line in $filefrag_op
9352         do
9353                 frag_lun=$(echo $line | cut -d: -f5 |
9354                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9355                 ext_len=$(echo $line | cut -d: -f4)
9356                 if (( $frag_lun != $last_lun )); then
9357                         if (( tot_len != $EXPECTED_LEN )); then
9358                                 cleanup_130
9359                                 error "FIEMAP on $fm_file failed; returned " \
9360                                 "len $tot_len for OST $last_lun instead " \
9361                                 "of $EXPECTED_LEN"
9362                                 return
9363                         else
9364                                 (( num_luns += 1 ))
9365                                 tot_len=0
9366                         fi
9367                 fi
9368                 (( tot_len += ext_len ))
9369                 last_lun=$frag_lun
9370         done
9371         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9372                 cleanup_130
9373                 error "FIEMAP on $fm_file failed; returned wrong number " \
9374                         "of luns or wrong len for OST $last_lun"
9375                 return
9376         fi
9377
9378         cleanup_130
9379
9380         echo "FIEMAP with continuation calls succeeded"
9381 }
9382 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9383
9384 # Test for writev/readv
9385 test_131a() {
9386         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
9387         error "writev test failed"
9388         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
9389         error "readv failed"
9390         rm -f $DIR/$tfile
9391 }
9392 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9393
9394 test_131b() {
9395         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
9396         error "append writev test failed"
9397         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
9398         error "append writev test failed"
9399         rm -f $DIR/$tfile
9400 }
9401 run_test 131b "test append writev"
9402
9403 test_131c() {
9404         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9405         error "NOT PASS"
9406 }
9407 run_test 131c "test read/write on file w/o objects"
9408
9409 test_131d() {
9410         rwv -f $DIR/$tfile -w -n 1 1572864
9411         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9412         if [ "$NOB" != 1572864 ]; then
9413                 error "Short read filed: read $NOB bytes instead of 1572864"
9414         fi
9415         rm -f $DIR/$tfile
9416 }
9417 run_test 131d "test short read"
9418
9419 test_131e() {
9420         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9421         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9422         error "read hitting hole failed"
9423         rm -f $DIR/$tfile
9424 }
9425 run_test 131e "test read hitting hole"
9426
9427 check_stats() {
9428         local facet=$1
9429         local op=$2
9430         local want=${3:-0}
9431         local res
9432
9433         case $facet in
9434         mds*) res=$(do_facet $facet \
9435                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
9436                  ;;
9437         ost*) res=$(do_facet $facet \
9438                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
9439                  ;;
9440         *) error "Wrong facet '$facet'" ;;
9441         esac
9442         echo $res
9443         [ "$res" ] || error "The counter for $op on $facet was not incremented"
9444         # if the argument $3 is zero, it means any stat increment is ok.
9445         if [[ $want -gt 0 ]]; then
9446                 local count=$(echo $res | awk '{ print $2 }')
9447                 [[ $count -ne $want ]] &&
9448                         error "The $op counter on $facet is $count, not $want"
9449         fi
9450 }
9451
9452 test_133a() {
9453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9454         remote_ost_nodsh && skip "remote OST with nodsh" && return
9455         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9456
9457         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9458                 { skip "MDS doesn't support rename stats"; return; }
9459         local testdir=$DIR/${tdir}/stats_testdir
9460         mkdir -p $DIR/${tdir}
9461
9462         # clear stats.
9463         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9464         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9465
9466         # verify mdt stats first.
9467         mkdir ${testdir} || error "mkdir failed"
9468         check_stats $SINGLEMDS "mkdir" 1
9469         touch ${testdir}/${tfile} || error "touch failed"
9470         check_stats $SINGLEMDS "open" 1
9471         check_stats $SINGLEMDS "close" 1
9472         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
9473                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
9474                 check_stats $SINGLEMDS "mknod" 2
9475         }
9476         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
9477         check_stats $SINGLEMDS "unlink" 1
9478         rm -f ${testdir}/${tfile} || error "file remove failed"
9479         check_stats $SINGLEMDS "unlink" 2
9480
9481         # remove working dir and check mdt stats again.
9482         rmdir ${testdir} || error "rmdir failed"
9483         check_stats $SINGLEMDS "rmdir" 1
9484
9485         local testdir1=$DIR/${tdir}/stats_testdir1
9486         mkdir -p ${testdir}
9487         mkdir -p ${testdir1}
9488         touch ${testdir1}/test1
9489         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9490         check_stats $SINGLEMDS "crossdir_rename" 1
9491
9492         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9493         check_stats $SINGLEMDS "samedir_rename" 1
9494
9495         rm -rf $DIR/${tdir}
9496 }
9497 run_test 133a "Verifying MDT stats ========================================"
9498
9499 test_133b() {
9500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9501         remote_ost_nodsh && skip "remote OST with nodsh" && return
9502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9503         local testdir=$DIR/${tdir}/stats_testdir
9504         mkdir -p ${testdir} || error "mkdir failed"
9505         touch ${testdir}/${tfile} || error "touch failed"
9506         cancel_lru_locks mdc
9507
9508         # clear stats.
9509         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9510         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9511
9512         # extra mdt stats verification.
9513         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9514         check_stats $SINGLEMDS "setattr" 1
9515         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9516         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9517         then            # LU-1740
9518                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9519                 check_stats $SINGLEMDS "getattr" 1
9520         fi
9521         $LFS df || error "lfs failed"
9522         check_stats $SINGLEMDS "statfs" 1
9523
9524         rm -rf $DIR/${tdir}
9525 }
9526 run_test 133b "Verifying extra MDT stats =================================="
9527
9528 test_133c() {
9529         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9530         remote_ost_nodsh && skip "remote OST with nodsh" && return
9531         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9532         local testdir=$DIR/${tdir}/stats_testdir
9533         test_mkdir -p ${testdir} || error "mkdir failed"
9534
9535         # verify obdfilter stats.
9536         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9537         sync
9538         cancel_lru_locks osc
9539         wait_delete_completed
9540
9541         # clear stats.
9542         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9543         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9544
9545         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9546         sync
9547         cancel_lru_locks osc
9548         check_stats ost1 "write" 1
9549
9550         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9551         check_stats ost1 "read" 1
9552
9553         > ${testdir}/${tfile} || error "truncate failed"
9554         check_stats ost1 "punch" 1
9555
9556         rm -f ${testdir}/${tfile} || error "file remove failed"
9557         wait_delete_completed
9558         check_stats ost1 "destroy" 1
9559
9560         rm -rf $DIR/${tdir}
9561 }
9562 run_test 133c "Verifying OST stats ========================================"
9563
9564 order_2() {
9565         local value=$1
9566         local orig=$value
9567         local order=1
9568
9569         while [ $value -ge 2 ]; do
9570                 order=$((order*2))
9571                 value=$((value/2))
9572         done
9573
9574         if [ $orig -gt $order ]; then
9575                 order=$((order*2))
9576         fi
9577         echo $order
9578 }
9579
9580 size_in_KMGT() {
9581     local value=$1
9582     local size=('K' 'M' 'G' 'T');
9583     local i=0
9584     local size_string=$value
9585
9586     while [ $value -ge 1024 ]; do
9587         if [ $i -gt 3 ]; then
9588             #T is the biggest unit we get here, if that is bigger,
9589             #just return XXXT
9590             size_string=${value}T
9591             break
9592         fi
9593         value=$((value >> 10))
9594         if [ $value -lt 1024 ]; then
9595             size_string=${value}${size[$i]}
9596             break
9597         fi
9598         i=$((i + 1))
9599     done
9600
9601     echo $size_string
9602 }
9603
9604 get_rename_size() {
9605     local size=$1
9606     local context=${2:-.}
9607     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9608                 grep -A1 $context |
9609                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9610     echo $sample
9611 }
9612
9613 test_133d() {
9614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9615         remote_ost_nodsh && skip "remote OST with nodsh" && return
9616         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9617         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9618         { skip "MDS doesn't support rename stats"; return; }
9619
9620         local testdir1=$DIR/${tdir}/stats_testdir1
9621         local testdir2=$DIR/${tdir}/stats_testdir2
9622
9623         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9624
9625         mkdir -p ${testdir1} || error "mkdir failed"
9626         mkdir -p ${testdir2} || error "mkdir failed"
9627
9628         createmany -o $testdir1/test 512 || error "createmany failed"
9629
9630         # check samedir rename size
9631         mv ${testdir1}/test0 ${testdir1}/test_0
9632
9633         local testdir1_size=$(ls -l $DIR/${tdir} |
9634                 awk '/stats_testdir1/ {print $5}')
9635         local testdir2_size=$(ls -l $DIR/${tdir} |
9636                 awk '/stats_testdir2/ {print $5}')
9637
9638         testdir1_size=$(order_2 $testdir1_size)
9639         testdir2_size=$(order_2 $testdir2_size)
9640
9641         testdir1_size=$(size_in_KMGT $testdir1_size)
9642         testdir2_size=$(size_in_KMGT $testdir2_size)
9643
9644         echo "source rename dir size: ${testdir1_size}"
9645         echo "target rename dir size: ${testdir2_size}"
9646
9647         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9648         eval $cmd || error "$cmd failed"
9649         local samedir=$($cmd | grep 'same_dir')
9650         local same_sample=$(get_rename_size $testdir1_size)
9651         [ -z "$samedir" ] && error "samedir_rename_size count error"
9652         [[ $same_sample -eq 1 ]] ||
9653                 error "samedir_rename_size error $same_sample"
9654         echo "Check same dir rename stats success"
9655
9656         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9657
9658         # check crossdir rename size
9659         mv ${testdir1}/test_0 ${testdir2}/test_0
9660
9661         testdir1_size=$(ls -l $DIR/${tdir} |
9662                 awk '/stats_testdir1/ {print $5}')
9663         testdir2_size=$(ls -l $DIR/${tdir} |
9664                 awk '/stats_testdir2/ {print $5}')
9665
9666         testdir1_size=$(order_2 $testdir1_size)
9667         testdir2_size=$(order_2 $testdir2_size)
9668
9669         testdir1_size=$(size_in_KMGT $testdir1_size)
9670         testdir2_size=$(size_in_KMGT $testdir2_size)
9671
9672         echo "source rename dir size: ${testdir1_size}"
9673         echo "target rename dir size: ${testdir2_size}"
9674
9675         eval $cmd || error "$cmd failed"
9676         local crossdir=$($cmd | grep 'crossdir')
9677         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9678         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9679         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9680         [[ $src_sample -eq 1 ]] ||
9681                 error "crossdir_rename_size error $src_sample"
9682         [[ $tgt_sample -eq 1 ]] ||
9683                 error "crossdir_rename_size error $tgt_sample"
9684         echo "Check cross dir rename stats success"
9685         rm -rf $DIR/${tdir}
9686 }
9687 run_test 133d "Verifying rename_stats ========================================"
9688
9689 test_133e() {
9690         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9691         remote_ost_nodsh && skip "remote OST with nodsh" && return
9692         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9693         local testdir=$DIR/${tdir}/stats_testdir
9694         local ctr f0 f1 bs=32768 count=42 sum
9695
9696         mkdir -p ${testdir} || error "mkdir failed"
9697
9698         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9699
9700         for ctr in {write,read}_bytes; do
9701                 sync
9702                 cancel_lru_locks osc
9703
9704                 do_facet ost1 $LCTL set_param -n \
9705                         "obdfilter.*.exports.clear=clear"
9706
9707                 if [ $ctr = write_bytes ]; then
9708                         f0=/dev/zero
9709                         f1=${testdir}/${tfile}
9710                 else
9711                         f0=${testdir}/${tfile}
9712                         f1=/dev/null
9713                 fi
9714
9715                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9716                         error "dd failed"
9717                 sync
9718                 cancel_lru_locks osc
9719
9720                 sum=$(do_facet ost1 $LCTL get_param \
9721                         "obdfilter.*.exports.*.stats" |
9722                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9723                                 $1 == ctr { sum += $7 }
9724                                 END { printf("%0.0f", sum) }')
9725
9726                 if ((sum != bs * count)); then
9727                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9728                 fi
9729         done
9730
9731         rm -rf $DIR/${tdir}
9732 }
9733 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9734
9735 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
9736
9737 test_133f() {
9738         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9739         remote_ost_nodsh && skip "remote OST with nodsh" && return
9740         # First without trusting modes.
9741         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
9742         echo "proc_dirs='$proc_dirs'"
9743         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
9744         find $proc_dirs -exec cat '{}' \; &> /dev/null
9745
9746         # Second verifying readability.
9747         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
9748
9749         # eventually, this can also be replaced with "lctl get_param -R",
9750         # but not until that option is always available on the server
9751         local facet
9752         for facet in mds1 ost1; do
9753                 local facet_proc_dirs=$(do_facet $facet \
9754                                         \\\ls -d $proc_regexp 2>/dev/null)
9755                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
9756                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
9757                 do_facet $facet find $facet_proc_dirs \
9758                         ! -name req_history \
9759                         -exec cat '{}' \\\; &> /dev/null
9760
9761                 do_facet $facet find $facet_proc_dirs \
9762                         ! -name req_history \
9763                         -type f \
9764                         -exec cat '{}' \\\; &> /dev/null ||
9765                                 error "proc file read failed"
9766         done
9767 }
9768 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9769
9770 test_133g() {
9771         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9772         remote_ost_nodsh && skip "remote OST with nodsh" && return
9773         # Second verifying writability.
9774         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
9775         echo "proc_dirs='$proc_dirs'"
9776         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
9777         find $proc_dirs \
9778                 -type f \
9779                 -not -name force_lbug \
9780                 -not -name changelog_mask \
9781                 -exec badarea_io '{}' \; &> /dev/null ||
9782                 error "find $proc_dirs failed"
9783
9784         local facet
9785         for facet in mds1 ost1; do
9786                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
9787                         skip "Too old lustre on $facet" && continue
9788                 local facet_proc_dirs=$(do_facet $facet \
9789                                         \\\ls -d $proc_regexp 2> /dev/null)
9790                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
9791                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
9792                 do_facet $facet find $facet_proc_dirs \
9793                         -type f \
9794                         -not -name force_lbug \
9795                         -not -name changelog_mask \
9796                         -exec badarea_io '{}' \\\; &> /dev/null ||
9797                                 error "$facet find $facet_proc_dirs failed"
9798         done
9799
9800         # remount the FS in case writes/reads /proc break the FS
9801         cleanup || error "failed to unmount"
9802         setup || error "failed to setup"
9803         true
9804 }
9805 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9806
9807 test_133h() {
9808         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9809         remote_ost_nodsh && skip "remote OST with nodsh" && return
9810         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
9811                 skip "Need MDS version at least 2.9.54" && return
9812
9813         local facet
9814         for facet in client mds1 ost1; do
9815                 local facet_proc_dirs=$(do_facet $facet \
9816                                         \\\ls -d $proc_regexp 2> /dev/null)
9817                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
9818                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
9819                 # Get the list of files that are missing the terminating newline
9820                 local missing=($(do_facet $facet \
9821                         find ${facet_proc_dirs} -type f \|              \
9822                                 while read F\; do                       \
9823                                         awk -v FS='\v' -v RS='\v\v'     \
9824                                         "'END { if(NR>0 &&              \
9825                                         \\\$NF !~ /.*\\\n\$/)           \
9826                                                 print FILENAME}'"       \
9827                                         '\$F'\;                         \
9828                                 done 2>/dev/null))
9829                 [ ${#missing[*]} -eq 0 ] ||
9830                         error "files do not end with newline: ${missing[*]}"
9831         done
9832 }
9833 run_test 133h "Proc files should end with newlines"
9834
9835 test_134a() {
9836         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9837         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9838                 skip "Need MDS version at least 2.7.54" && return
9839
9840         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9841         cancel_lru_locks mdc
9842
9843         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9844         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9845         [ $unused -eq 0 ] || error "$unused locks are not cleared"
9846
9847         local nr=1000
9848         createmany -o $DIR/$tdir/f $nr ||
9849                 error "failed to create $nr files in $DIR/$tdir"
9850         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9851
9852         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9853         do_facet mds1 $LCTL set_param fail_loc=0x327
9854         do_facet mds1 $LCTL set_param fail_val=500
9855         touch $DIR/$tdir/m
9856
9857         echo "sleep 10 seconds ..."
9858         sleep 10
9859         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9860
9861         do_facet mds1 $LCTL set_param fail_loc=0
9862         do_facet mds1 $LCTL set_param fail_val=0
9863         [ $lck_cnt -lt $unused ] ||
9864                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9865
9866         rm $DIR/$tdir/m
9867         unlinkmany $DIR/$tdir/f $nr
9868 }
9869 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9870
9871 test_134b() {
9872         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9873         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9874                 skip "Need MDS version at least 2.7.54" && return
9875
9876         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9877         cancel_lru_locks mdc
9878
9879         local low_wm=$(do_facet mds1 $LCTL get_param -n \
9880                         ldlm.lock_reclaim_threshold_mb)
9881         # disable reclaim temporarily
9882         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
9883
9884         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
9885         do_facet mds1 $LCTL set_param fail_loc=0x328
9886         do_facet mds1 $LCTL set_param fail_val=500
9887
9888         $LCTL set_param debug=+trace
9889
9890         local nr=600
9891         createmany -o $DIR/$tdir/f $nr &
9892         local create_pid=$!
9893
9894         echo "Sleep $TIMEOUT seconds ..."
9895         sleep $TIMEOUT
9896         if ! ps -p $create_pid  > /dev/null 2>&1; then
9897                 do_facet mds1 $LCTL set_param fail_loc=0
9898                 do_facet mds1 $LCTL set_param fail_val=0
9899                 do_facet mds1 $LCTL set_param \
9900                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
9901                 error "createmany finished incorrectly!"
9902         fi
9903         do_facet mds1 $LCTL set_param fail_loc=0
9904         do_facet mds1 $LCTL set_param fail_val=0
9905         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
9906         wait $create_pid || return 1
9907
9908         unlinkmany $DIR/$tdir/f $nr
9909 }
9910 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
9911
9912 test_140() { #bug-17379
9913         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9914         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9915         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9916         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
9917
9918         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9919         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9920         local i=0
9921         while i=`expr $i + 1`; do
9922                 test_mkdir -p $i || error "Creating dir $i"
9923                 cd $i || error "Changing to $i"
9924                 ln -s ../stat stat || error "Creating stat symlink"
9925                 # Read the symlink until ELOOP present,
9926                 # not LBUGing the system is considered success,
9927                 # we didn't overrun the stack.
9928                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9929                 [ $ret -ne 0 ] && {
9930                         if [ $ret -eq 40 ]; then
9931                                 break  # -ELOOP
9932                         else
9933                                 error "Open stat symlink"
9934                                 return
9935                         fi
9936                 }
9937         done
9938         i=`expr $i - 1`
9939         echo "The symlink depth = $i"
9940         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9941                                         error "Invalid symlink depth"
9942
9943         # Test recursive symlink
9944         ln -s symlink_self symlink_self
9945         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9946         echo "open symlink_self returns $ret"
9947         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9948 }
9949 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9950
9951 test_150() {
9952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9953         local TF="$TMP/$tfile"
9954
9955         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9956         cp $TF $DIR/$tfile
9957         cancel_lru_locks osc
9958         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9959         remount_client $MOUNT
9960         df -P $MOUNT
9961         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9962
9963         $TRUNCATE $TF 6000
9964         $TRUNCATE $DIR/$tfile 6000
9965         cancel_lru_locks osc
9966         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9967
9968         echo "12345" >>$TF
9969         echo "12345" >>$DIR/$tfile
9970         cancel_lru_locks osc
9971         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9972
9973         echo "12345" >>$TF
9974         echo "12345" >>$DIR/$tfile
9975         cancel_lru_locks osc
9976         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9977
9978         rm -f $TF
9979         true
9980 }
9981 run_test 150 "truncate/append tests"
9982
9983 #LU-2902 roc_hit was not able to read all values from lproc
9984 function roc_hit_init() {
9985         local list=$(comma_list $(osts_nodes))
9986         local dir=$DIR/$tdir-check
9987         local file=$dir/file
9988         local BEFORE
9989         local AFTER
9990         local idx
9991
9992         test_mkdir -p $dir
9993         #use setstripe to do a write to every ost
9994         for i in $(seq 0 $((OSTCOUNT-1))); do
9995                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9996                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9997                 idx=$(printf %04x $i)
9998                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9999                         awk '$1 == "cache_access" {sum += $7}
10000                                 END { printf("%0.0f", sum) }')
10001
10002                 cancel_lru_locks osc
10003                 cat $file >/dev/null
10004
10005                 AFTER=$(get_osd_param $list *OST*$idx stats |
10006                         awk '$1 == "cache_access" {sum += $7}
10007                                 END { printf("%0.0f", sum) }')
10008
10009                 echo BEFORE:$BEFORE AFTER:$AFTER
10010                 if ! let "AFTER - BEFORE == 4"; then
10011                         rm -rf $dir
10012                         error "roc_hit is not safe to use"
10013                 fi
10014                 rm $file
10015         done
10016
10017         rm -rf $dir
10018 }
10019
10020 function roc_hit() {
10021         local list=$(comma_list $(osts_nodes))
10022         echo $(get_osd_param $list '' stats |
10023                 awk '$1 == "cache_hit" {sum += $7}
10024                         END { printf("%0.0f", sum) }')
10025 }
10026
10027 function set_cache() {
10028         local on=1
10029
10030         if [ "$2" == "off" ]; then
10031                 on=0;
10032         fi
10033         local list=$(comma_list $(osts_nodes))
10034         set_osd_param $list '' $1_cache_enable $on
10035
10036         cancel_lru_locks osc
10037 }
10038
10039 test_151() {
10040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10041         remote_ost_nodsh && skip "remote OST with nodsh" && return
10042
10043         local CPAGES=3
10044         local list=$(comma_list $(osts_nodes))
10045
10046         # check whether obdfilter is cache capable at all
10047         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10048                 echo "not cache-capable obdfilter"
10049                 return 0
10050         fi
10051
10052         # check cache is enabled on all obdfilters
10053         if get_osd_param $list '' read_cache_enable | grep 0; then
10054                 echo "oss cache is disabled"
10055                 return 0
10056         fi
10057
10058         set_osd_param $list '' writethrough_cache_enable 1
10059
10060         # check write cache is enabled on all obdfilters
10061         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10062                 echo "oss write cache is NOT enabled"
10063                 return 0
10064         fi
10065
10066         roc_hit_init
10067
10068         #define OBD_FAIL_OBD_NO_LRU  0x609
10069         do_nodes $list $LCTL set_param fail_loc=0x609
10070
10071         # pages should be in the case right after write
10072         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10073                 error "dd failed"
10074
10075         local BEFORE=$(roc_hit)
10076         cancel_lru_locks osc
10077         cat $DIR/$tfile >/dev/null
10078         local AFTER=$(roc_hit)
10079
10080         do_nodes $list $LCTL set_param fail_loc=0
10081
10082         if ! let "AFTER - BEFORE == CPAGES"; then
10083                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10084         fi
10085
10086         # the following read invalidates the cache
10087         cancel_lru_locks osc
10088         set_osd_param $list '' read_cache_enable 0
10089         cat $DIR/$tfile >/dev/null
10090
10091         # now data shouldn't be found in the cache
10092         BEFORE=$(roc_hit)
10093         cancel_lru_locks osc
10094         cat $DIR/$tfile >/dev/null
10095         AFTER=$(roc_hit)
10096         if let "AFTER - BEFORE != 0"; then
10097                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10098         fi
10099
10100         set_osd_param $list '' read_cache_enable 1
10101         rm -f $DIR/$tfile
10102 }
10103 run_test 151 "test cache on oss and controls ==============================="
10104
10105 test_152() {
10106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10107         local TF="$TMP/$tfile"
10108
10109         # simulate ENOMEM during write
10110 #define OBD_FAIL_OST_NOMEM      0x226
10111         lctl set_param fail_loc=0x80000226
10112         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10113         cp $TF $DIR/$tfile
10114         sync || error "sync failed"
10115         lctl set_param fail_loc=0
10116
10117         # discard client's cache
10118         cancel_lru_locks osc
10119
10120         # simulate ENOMEM during read
10121         lctl set_param fail_loc=0x80000226
10122         cmp $TF $DIR/$tfile || error "cmp failed"
10123         lctl set_param fail_loc=0
10124
10125         rm -f $TF
10126 }
10127 run_test 152 "test read/write with enomem ============================"
10128
10129 test_153() {
10130         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10131 }
10132 run_test 153 "test if fdatasync does not crash ======================="
10133
10134 dot_lustre_fid_permission_check() {
10135         local fid=$1
10136         local ffid=$MOUNT/.lustre/fid/$fid
10137         local test_dir=$2
10138
10139         echo "stat fid $fid"
10140         stat $ffid > /dev/null || error "stat $ffid failed."
10141         echo "touch fid $fid"
10142         touch $ffid || error "touch $ffid failed."
10143         echo "write to fid $fid"
10144         cat /etc/hosts > $ffid || error "write $ffid failed."
10145         echo "read fid $fid"
10146         diff /etc/hosts $ffid || error "read $ffid failed."
10147         echo "append write to fid $fid"
10148         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10149         echo "rename fid $fid"
10150         mv $ffid $test_dir/$tfile.1 &&
10151                 error "rename $ffid to $tfile.1 should fail."
10152         touch $test_dir/$tfile.1
10153         mv $test_dir/$tfile.1 $ffid &&
10154                 error "rename $tfile.1 to $ffid should fail."
10155         rm -f $test_dir/$tfile.1
10156         echo "truncate fid $fid"
10157         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10158         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10159                 echo "link fid $fid"
10160                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10161         fi
10162         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10163                 echo "setfacl fid $fid"
10164                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10165                 echo "getfacl fid $fid"
10166                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10167         fi
10168         echo "unlink fid $fid"
10169         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10170         echo "mknod fid $fid"
10171         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10172
10173         fid=[0xf00000400:0x1:0x0]
10174         ffid=$MOUNT/.lustre/fid/$fid
10175
10176         echo "stat non-exist fid $fid"
10177         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10178         echo "write to non-exist fid $fid"
10179         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10180         echo "link new fid $fid"
10181         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10182
10183         mkdir -p $test_dir/$tdir
10184         touch $test_dir/$tdir/$tfile
10185         fid=$($LFS path2fid $test_dir/$tdir)
10186         rc=$?
10187         [ $rc -ne 0 ] &&
10188                 error "error: could not get fid for $test_dir/$dir/$tfile."
10189
10190         ffid=$MOUNT/.lustre/fid/$fid
10191
10192         echo "ls $fid"
10193         ls $ffid > /dev/null || error "ls $ffid failed."
10194         echo "touch $fid/$tfile.1"
10195         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10196
10197         echo "touch $MOUNT/.lustre/fid/$tfile"
10198         touch $MOUNT/.lustre/fid/$tfile && \
10199                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10200
10201         echo "setxattr to $MOUNT/.lustre/fid"
10202         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10203
10204         echo "listxattr for $MOUNT/.lustre/fid"
10205         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10206
10207         echo "delxattr from $MOUNT/.lustre/fid"
10208         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10209
10210         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10211         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10212                 error "touch invalid fid should fail."
10213
10214         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10215         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10216                 error "touch non-normal fid should fail."
10217
10218         echo "rename $tdir to $MOUNT/.lustre/fid"
10219         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10220                 error "rename to $MOUNT/.lustre/fid should fail."
10221
10222         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10223         then            # LU-3547
10224                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10225                 local new_obf_mode=777
10226
10227                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10228                 chmod $new_obf_mode $DIR/.lustre/fid ||
10229                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10230
10231                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10232                 [ $obf_mode -eq $new_obf_mode ] ||
10233                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10234
10235                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10236                 chmod $old_obf_mode $DIR/.lustre/fid ||
10237                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10238         fi
10239
10240         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10241         fid=$($LFS path2fid $test_dir/$tfile-2)
10242
10243         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10244         then # LU-5424
10245                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10246                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10247                         error "create lov data thru .lustre failed"
10248         fi
10249         echo "cp /etc/passwd $test_dir/$tfile-2"
10250         cp /etc/passwd $test_dir/$tfile-2 ||
10251                 error "copy to $test_dir/$tfile-2 failed."
10252         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10253         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10254                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10255
10256         rm -rf $test_dir/tfile.lnk
10257         rm -rf $test_dir/$tfile-2
10258 }
10259
10260 test_154A() {
10261         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10262                 skip "Need MDS version at least 2.4.1" && return
10263
10264         local tf=$DIR/$tfile
10265         touch $tf
10266
10267         local fid=$($LFS path2fid $tf)
10268         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10269
10270         # check that we get the same pathname back
10271         local found=$($LFS fid2path $MOUNT "$fid")
10272         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10273         [ "$found" == "$tf" ] ||
10274                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10275 }
10276 run_test 154A "lfs path2fid and fid2path basic checks"
10277
10278 test_154B() {
10279         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10280                 skip "Need MDS version at least 2.4.1" && return
10281
10282         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10283         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10284         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10285         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10286
10287         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10288         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10289
10290         # check that we get the same pathname
10291         echo "PFID: $PFID, name: $name"
10292         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10293         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10294         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10295                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10296
10297         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10298 }
10299 run_test 154B "verify the ll_decode_linkea tool"
10300
10301 test_154a() {
10302         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10303         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10304                 { skip "Need MDS version at least 2.2.51"; return 0; }
10305         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10306         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10307
10308         cp /etc/hosts $DIR/$tfile
10309
10310         fid=$($LFS path2fid $DIR/$tfile)
10311         rc=$?
10312         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10313
10314         dot_lustre_fid_permission_check "$fid" $DIR ||
10315                 error "dot lustre permission check $fid failed"
10316
10317         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10318
10319         touch $MOUNT/.lustre/file &&
10320                 error "creation is not allowed under .lustre"
10321
10322         mkdir $MOUNT/.lustre/dir &&
10323                 error "mkdir is not allowed under .lustre"
10324
10325         rm -rf $DIR/$tfile
10326 }
10327 run_test 154a "Open-by-FID"
10328
10329 test_154b() {
10330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10331         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10332                 { skip "Need MDS version at least 2.2.51"; return 0; }
10333         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10334
10335         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10336
10337         local remote_dir=$DIR/$tdir/remote_dir
10338         local MDTIDX=1
10339         local rc=0
10340
10341         mkdir -p $DIR/$tdir
10342         $LFS mkdir -i $MDTIDX $remote_dir ||
10343                 error "create remote directory failed"
10344
10345         cp /etc/hosts $remote_dir/$tfile
10346
10347         fid=$($LFS path2fid $remote_dir/$tfile)
10348         rc=$?
10349         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
10350
10351         dot_lustre_fid_permission_check "$fid" $remote_dir ||
10352                 error "dot lustre permission check $fid failed"
10353         rm -rf $DIR/$tdir
10354 }
10355 run_test 154b "Open-by-FID for remote directory"
10356
10357 test_154c() {
10358         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10359                 skip "Need MDS version at least 2.4.1" && return
10360
10361         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
10362         local FID1=$($LFS path2fid $DIR/$tfile.1)
10363         local FID2=$($LFS path2fid $DIR/$tfile.2)
10364         local FID3=$($LFS path2fid $DIR/$tfile.3)
10365
10366         local N=1
10367         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
10368                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
10369                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
10370                 local want=FID$N
10371                 [ "$FID" = "${!want}" ] ||
10372                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
10373                 N=$((N + 1))
10374         done
10375
10376         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
10377         do
10378                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
10379                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
10380                 N=$((N + 1))
10381         done
10382 }
10383 run_test 154c "lfs path2fid and fid2path multiple arguments"
10384
10385 test_154d() {
10386         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10387         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10388                 skip "Need MDS version at least 2.5.53" && return
10389
10390         if remote_mds; then
10391                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10392         else
10393                 nid="0@lo"
10394         fi
10395         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10396         local fd
10397         local cmd
10398
10399         rm -f $DIR/$tfile
10400         touch $DIR/$tfile
10401
10402         local fid=$($LFS path2fid $DIR/$tfile)
10403         # Open the file
10404         fd=$(free_fd)
10405         cmd="exec $fd<$DIR/$tfile"
10406         eval $cmd
10407         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10408         echo "$fid_list" | grep "$fid"
10409         rc=$?
10410
10411         cmd="exec $fd>/dev/null"
10412         eval $cmd
10413         if [ $rc -ne 0 ]; then
10414                 error "FID $fid not found in open files list $fid_list"
10415         fi
10416 }
10417 run_test 154d "Verify open file fid"
10418
10419 test_154e()
10420 {
10421         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10422                 skip "Need MDS version at least 2.6.50" && return
10423
10424         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10425                 error ".lustre returned by readdir"
10426         fi
10427 }
10428 run_test 154e ".lustre is not returned by readdir"
10429
10430 test_154f() {
10431         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10432         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10433         test_mkdir -p -c1 $DIR/$tdir/d
10434         # test dirs inherit from its stripe
10435         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10436         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10437         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10438         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10439         touch $DIR/f
10440
10441         # get fid of parents
10442         local FID0=$($LFS path2fid $DIR/$tdir/d)
10443         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10444         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10445         local FID3=$($LFS path2fid $DIR)
10446
10447         # check that path2fid --parents returns expected <parent_fid>/name
10448         # 1) test for a directory (single parent)
10449         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10450         [ "$parent" == "$FID0/foo1" ] ||
10451                 error "expected parent: $FID0/foo1, got: $parent"
10452
10453         # 2) test for a file with nlink > 1 (multiple parents)
10454         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10455         echo "$parent" | grep -F "$FID1/$tfile" ||
10456                 error "$FID1/$tfile not returned in parent list"
10457         echo "$parent" | grep -F "$FID2/link" ||
10458                 error "$FID2/link not returned in parent list"
10459
10460         # 3) get parent by fid
10461         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10462         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10463         echo "$parent" | grep -F "$FID1/$tfile" ||
10464                 error "$FID1/$tfile not returned in parent list (by fid)"
10465         echo "$parent" | grep -F "$FID2/link" ||
10466                 error "$FID2/link not returned in parent list (by fid)"
10467
10468         # 4) test for entry in root directory
10469         parent=$($LFS path2fid --parents $DIR/f)
10470         echo "$parent" | grep -F "$FID3/f" ||
10471                 error "$FID3/f not returned in parent list"
10472
10473         # 5) test it on root directory
10474         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10475                 error "$MOUNT should not have parents"
10476
10477         # enable xattr caching and check that linkea is correctly updated
10478         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10479         save_lustre_params client "llite.*.xattr_cache" > $save
10480         lctl set_param llite.*.xattr_cache 1
10481
10482         # 6.1) linkea update on rename
10483         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10484
10485         # get parents by fid
10486         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10487         # foo1 should no longer be returned in parent list
10488         echo "$parent" | grep -F "$FID1" &&
10489                 error "$FID1 should no longer be in parent list"
10490         # the new path should appear
10491         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10492                 error "$FID2/$tfile.moved is not in parent list"
10493
10494         # 6.2) linkea update on unlink
10495         rm -f $DIR/$tdir/d/foo2/link
10496         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10497         # foo2/link should no longer be returned in parent list
10498         echo "$parent" | grep -F "$FID2/link" &&
10499                 error "$FID2/link should no longer be in parent list"
10500         true
10501
10502         rm -f $DIR/f
10503         restore_lustre_params < $save
10504         rm -f $save
10505 }
10506 run_test 154f "get parent fids by reading link ea"
10507
10508 test_154g()
10509 {
10510         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
10511                 { skip "Need MDS version at least 2.6.92"; return 0; }
10512         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10513
10514         mkdir -p $DIR/$tdir
10515         llapi_fid_test -d $DIR/$tdir
10516 }
10517 run_test 154g "various llapi FID tests"
10518
10519 test_155_small_load() {
10520     local temp=$TMP/$tfile
10521     local file=$DIR/$tfile
10522
10523     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10524         error "dd of=$temp bs=6096 count=1 failed"
10525     cp $temp $file
10526     cancel_lru_locks osc
10527     cmp $temp $file || error "$temp $file differ"
10528
10529     $TRUNCATE $temp 6000
10530     $TRUNCATE $file 6000
10531     cmp $temp $file || error "$temp $file differ (truncate1)"
10532
10533     echo "12345" >>$temp
10534     echo "12345" >>$file
10535     cmp $temp $file || error "$temp $file differ (append1)"
10536
10537     echo "12345" >>$temp
10538     echo "12345" >>$file
10539     cmp $temp $file || error "$temp $file differ (append2)"
10540
10541     rm -f $temp $file
10542     true
10543 }
10544
10545 test_155_big_load() {
10546     remote_ost_nodsh && skip "remote OST with nodsh" && return
10547     local temp=$TMP/$tfile
10548     local file=$DIR/$tfile
10549
10550     free_min_max
10551     local cache_size=$(do_facet ost$((MAXI+1)) \
10552         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10553     local large_file_size=$((cache_size * 2))
10554
10555     echo "OSS cache size: $cache_size KB"
10556     echo "Large file size: $large_file_size KB"
10557
10558     [ $MAXV -le $large_file_size ] && \
10559         skip_env "max available OST size needs > $large_file_size KB" && \
10560         return 0
10561
10562     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10563
10564     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10565         error "dd of=$temp bs=$large_file_size count=1k failed"
10566     cp $temp $file
10567     ls -lh $temp $file
10568     cancel_lru_locks osc
10569     cmp $temp $file || error "$temp $file differ"
10570
10571     rm -f $temp $file
10572     true
10573 }
10574
10575 save_writethrough() {
10576         local facets=$(get_facets OST)
10577
10578         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
10579         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
10580 }
10581
10582 test_155a() {
10583         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10584         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10585         save_writethrough $p
10586
10587         set_cache read on
10588         set_cache writethrough on
10589         test_155_small_load
10590         restore_lustre_params < $p
10591         rm -f $p
10592 }
10593 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10594
10595 test_155b() {
10596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10597         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10598         save_writethrough $p
10599
10600         set_cache read on
10601         set_cache writethrough off
10602         test_155_small_load
10603         restore_lustre_params < $p
10604         rm -f $p
10605 }
10606 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10607
10608 test_155c() {
10609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10610         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10611         save_writethrough $p
10612
10613         set_cache read off
10614         set_cache writethrough on
10615         test_155_small_load
10616         restore_lustre_params < $p
10617         rm -f $p
10618 }
10619 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10620
10621 test_155d() {
10622         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10623         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10624         save_writethrough $p
10625
10626         set_cache read off
10627         set_cache writethrough off
10628         test_155_small_load
10629         restore_lustre_params < $p
10630         rm -f $p
10631 }
10632 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10633
10634 test_155e() {
10635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10636         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10637         save_writethrough $p
10638
10639         set_cache read on
10640         set_cache writethrough on
10641         test_155_big_load
10642         restore_lustre_params < $p
10643         rm -f $p
10644 }
10645 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10646
10647 test_155f() {
10648         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10649         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10650         save_writethrough $p
10651
10652         set_cache read on
10653         set_cache writethrough off
10654         test_155_big_load
10655         restore_lustre_params < $p
10656         rm -f $p
10657 }
10658 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10659
10660 test_155g() {
10661         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10662         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10663         save_writethrough $p
10664
10665         set_cache read off
10666         set_cache writethrough on
10667         test_155_big_load
10668         restore_lustre_params < $p
10669         rm -f $p
10670 }
10671 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10672
10673 test_155h() {
10674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10675         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10676         save_writethrough $p
10677
10678         set_cache read off
10679         set_cache writethrough off
10680         test_155_big_load
10681         restore_lustre_params < $p
10682         rm -f $p
10683 }
10684 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10685
10686 test_156() {
10687         remote_ost_nodsh && skip "remote OST with nodsh" && return
10688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10689         local CPAGES=3
10690         local BEFORE
10691         local AFTER
10692         local file="$DIR/$tfile"
10693         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10694
10695         [ "$(facet_fstype ost1)" = "zfs" -a \
10696            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10697                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10698                 return
10699
10700         save_writethrough $p
10701         roc_hit_init
10702
10703         log "Turn on read and write cache"
10704         set_cache read on
10705         set_cache writethrough on
10706
10707         log "Write data and read it back."
10708         log "Read should be satisfied from the cache."
10709         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10710         BEFORE=$(roc_hit)
10711         cancel_lru_locks osc
10712         cat $file >/dev/null
10713         AFTER=$(roc_hit)
10714         if ! let "AFTER - BEFORE == CPAGES"; then
10715                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10716         else
10717                 log "cache hits:: before: $BEFORE, after: $AFTER"
10718         fi
10719
10720         log "Read again; it should be satisfied from the cache."
10721         BEFORE=$AFTER
10722         cancel_lru_locks osc
10723         cat $file >/dev/null
10724         AFTER=$(roc_hit)
10725         if ! let "AFTER - BEFORE == CPAGES"; then
10726                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10727         else
10728                 log "cache hits:: before: $BEFORE, after: $AFTER"
10729         fi
10730
10731         log "Turn off the read cache and turn on the write cache"
10732         set_cache read off
10733         set_cache writethrough on
10734
10735         log "Read again; it should be satisfied from the cache."
10736         BEFORE=$(roc_hit)
10737         cancel_lru_locks osc
10738         cat $file >/dev/null
10739         AFTER=$(roc_hit)
10740         if ! let "AFTER - BEFORE == CPAGES"; then
10741                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10742         else
10743                 log "cache hits:: before: $BEFORE, after: $AFTER"
10744         fi
10745
10746         log "Read again; it should not be satisfied from the cache."
10747         BEFORE=$AFTER
10748         cancel_lru_locks osc
10749         cat $file >/dev/null
10750         AFTER=$(roc_hit)
10751         if ! let "AFTER - BEFORE == 0"; then
10752                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10753         else
10754                 log "cache hits:: before: $BEFORE, after: $AFTER"
10755         fi
10756
10757         log "Write data and read it back."
10758         log "Read should be satisfied from the cache."
10759         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10760         BEFORE=$(roc_hit)
10761         cancel_lru_locks osc
10762         cat $file >/dev/null
10763         AFTER=$(roc_hit)
10764         if ! let "AFTER - BEFORE == CPAGES"; then
10765                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10766         else
10767                 log "cache hits:: before: $BEFORE, after: $AFTER"
10768         fi
10769
10770         log "Read again; it should not be satisfied from the cache."
10771         BEFORE=$AFTER
10772         cancel_lru_locks osc
10773         cat $file >/dev/null
10774         AFTER=$(roc_hit)
10775         if ! let "AFTER - BEFORE == 0"; then
10776                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10777         else
10778                 log "cache hits:: before: $BEFORE, after: $AFTER"
10779         fi
10780
10781         log "Turn off read and write cache"
10782         set_cache read off
10783         set_cache writethrough off
10784
10785         log "Write data and read it back"
10786         log "It should not be satisfied from the cache."
10787         rm -f $file
10788         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10789         cancel_lru_locks osc
10790         BEFORE=$(roc_hit)
10791         cat $file >/dev/null
10792         AFTER=$(roc_hit)
10793         if ! let "AFTER - BEFORE == 0"; then
10794                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10795         else
10796                 log "cache hits:: before: $BEFORE, after: $AFTER"
10797         fi
10798
10799         log "Turn on the read cache and turn off the write cache"
10800         set_cache read on
10801         set_cache writethrough off
10802
10803         log "Write data and read it back"
10804         log "It should not be satisfied from the cache."
10805         rm -f $file
10806         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10807         BEFORE=$(roc_hit)
10808         cancel_lru_locks osc
10809         cat $file >/dev/null
10810         AFTER=$(roc_hit)
10811         if ! let "AFTER - BEFORE == 0"; then
10812                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10813         else
10814                 log "cache hits:: before: $BEFORE, after: $AFTER"
10815         fi
10816
10817         log "Read again; it should be satisfied from the cache."
10818         BEFORE=$(roc_hit)
10819         cancel_lru_locks osc
10820         cat $file >/dev/null
10821         AFTER=$(roc_hit)
10822         if ! let "AFTER - BEFORE == CPAGES"; then
10823                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10824         else
10825                 log "cache hits:: before: $BEFORE, after: $AFTER"
10826         fi
10827
10828         rm -f $file
10829         restore_lustre_params < $p
10830         rm -f $p
10831 }
10832 run_test 156 "Verification of tunables"
10833
10834 #Changelogs
10835 cleanup_changelog () {
10836         trap 0
10837         echo "Deregistering changelog client $CL_USER"
10838         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
10839 }
10840
10841 err17935 () {
10842         if [[ $MDSCOUNT -gt 1 ]]; then
10843                 error_ignore bz17935 $*
10844         else
10845                 error $*
10846         fi
10847 }
10848
10849 changelog_chmask()
10850 {
10851         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10852
10853         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10854
10855         if [ $MASK -eq 1 ]; then
10856                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10857         else
10858                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10859         fi
10860 }
10861
10862 changelog_extract_field() {
10863         local mdt=$1
10864         local cltype=$2
10865         local file=$3
10866         local identifier=$4
10867
10868         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10869                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10870                 tail -1
10871 }
10872
10873 test_160a() {
10874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10875         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10876         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10877                 { skip "Need MDS version at least 2.2.0"; return; }
10878
10879         local CL_USERS="mdd.$MDT0.changelog_users"
10880         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10881         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10882                 changelog_register -n)
10883         echo "Registered as changelog user $CL_USER"
10884         trap cleanup_changelog EXIT
10885         $GET_CL_USERS | grep -q $CL_USER ||
10886                 error "User $CL_USER not found in changelog_users"
10887
10888         # change something
10889         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10890         touch $DIR/$tdir/pics/2008/zachy/timestamp
10891         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10892         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10893         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10894         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10895         rm $DIR/$tdir/pics/desktop.jpg
10896
10897         $LFS changelog $MDT0 | tail -5
10898
10899         echo "verifying changelog mask"
10900         changelog_chmask "MKDIR"
10901         changelog_chmask "CLOSE"
10902
10903         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10904         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10905
10906         changelog_chmask "MKDIR"
10907         changelog_chmask "CLOSE"
10908
10909         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10910         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10911
10912         $LFS changelog $MDT0
10913         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10914         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10915         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10916         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10917
10918         # verify contents
10919         echo "verifying target fid"
10920         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10921         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10922         [ "$fidc" == "$fidf" ] ||
10923                 err17935 "fid in changelog $fidc != file fid $fidf"
10924         echo "verifying parent fid"
10925         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10926         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10927         [ "$fidc" == "$fidf" ] ||
10928                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10929
10930         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
10931         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
10932         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
10933         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10934         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10935                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10936
10937         MIN_REC=$($GET_CL_USERS |
10938                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10939         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10940         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10941         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10942                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10943
10944         # LU-3446 changelog index reset on MDT restart
10945         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10946         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10947         $LFS changelog_clear $MDT0 $CL_USER 0
10948         stop $SINGLEMDS || error "Fail to stop MDT."
10949         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10950         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10951         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10952         [ $CUR_REC1 == $CUR_REC2 ] ||
10953                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10954
10955         echo "verifying user deregister"
10956         cleanup_changelog
10957         $GET_CL_USERS | grep -q $CL_USER &&
10958                 error "User $CL_USER still in changelog_users"
10959
10960         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10961         if [ $CL_USER -eq 0 ]; then
10962                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10963                 touch $DIR/$tdir/chloe
10964                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10965                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10966                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10967         else
10968                 echo "$CL_USER other changelog users; can't verify off"
10969         fi
10970 }
10971 run_test 160a "changelog sanity"
10972
10973 test_160b() { # LU-3587
10974         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10975         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10976         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10977                 { skip "Need MDS version at least 2.2.0"; return; }
10978
10979         local CL_USERS="mdd.$MDT0.changelog_users"
10980         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10981         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10982                 changelog_register -n)
10983         echo "Registered as changelog user $CL_USER"
10984         trap cleanup_changelog EXIT
10985         $GET_CL_USERS | grep -q $CL_USER ||
10986                 error "User $CL_USER not found in changelog_users"
10987
10988         local LONGNAME1=$(str_repeat a 255)
10989         local LONGNAME2=$(str_repeat b 255)
10990
10991         cd $DIR
10992         echo "creating very long named file"
10993         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10994         echo "moving very long named file"
10995         mv $LONGNAME1 $LONGNAME2
10996
10997         $LFS changelog $MDT0 | grep RENME
10998         rm -f $LONGNAME2
10999         cleanup_changelog
11000 }
11001 run_test 160b "Verify that very long rename doesn't crash in changelog"
11002
11003 test_160c() {
11004         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11005
11006         local rc=0
11007         local server_version=$(lustre_version_code $SINGLEMDS)
11008
11009         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11010                 [[ $server_version -gt $(version_code 2.5.1) &&
11011                    $server_version -lt $(version_code 2.5.50) ]] ||
11012                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11014
11015         # Registration step
11016         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11017                 changelog_register -n)
11018         trap cleanup_changelog EXIT
11019
11020         rm -rf $DIR/$tdir
11021         mkdir -p $DIR/$tdir
11022         $MCREATE $DIR/$tdir/foo_160c
11023         changelog_chmask "TRUNC"
11024         $TRUNCATE $DIR/$tdir/foo_160c 200
11025         changelog_chmask "TRUNC"
11026         $TRUNCATE $DIR/$tdir/foo_160c 199
11027         $LFS changelog $MDT0
11028         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11029         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11030         $LFS changelog_clear $MDT0 $CL_USER 0
11031
11032         # Deregistration step
11033         cleanup_changelog
11034 }
11035 run_test 160c "verify that changelog log catch the truncate event"
11036
11037 test_160d() {
11038         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11039         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11040
11041         local server_version=$(lustre_version_code mds1)
11042         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11043
11044         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11045                 { skip "Need MDS version at least 2.7.60+"; return; }
11046         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11047
11048         # Registration step
11049         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11050                 changelog_register -n)
11051
11052         trap cleanup_changelog EXIT
11053         mkdir -p $DIR/$tdir/migrate_dir
11054         $LFS changelog_clear $MDT0 $CL_USER 0
11055
11056         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11057         $LFS changelog $MDT0
11058         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11059         $LFS changelog_clear $MDT0 $CL_USER 0
11060         [ $MIGRATES -eq 1 ] ||
11061                 error "MIGRATE changelog mask count $MIGRATES != 1"
11062
11063         # Deregistration step
11064         cleanup_changelog
11065 }
11066 run_test 160d "verify that changelog log catch the migrate event"
11067
11068 test_160e() {
11069         # Create a user
11070         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11071                 changelog_register -n)
11072         echo "Registered as changelog user $CL_USER"
11073         trap cleanup_changelog EXIT
11074
11075         # Delete a future user (expect fail)
11076         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11077         local rc=$?
11078
11079         if [ $rc -eq 0 ]; then
11080                 error "Deleted non-existant user cl77"
11081         elif [ $rc -ne 2 ]; then
11082                 error "changelog_deregister failed with $rc, " \
11083                         "expected 2 (ENOENT)"
11084         fi
11085
11086         # Clear to a bad index (1 billion should be safe)
11087         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11088         rc=$?
11089
11090         if [ $rc -eq 0 ]; then
11091                 error "Successfully cleared to invalid CL index"
11092         elif [ $rc -ne 22 ]; then
11093                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11094         fi
11095 }
11096 run_test 160e "changelog negative testing"
11097
11098 test_161a() {
11099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11100         test_mkdir -p -c1 $DIR/$tdir
11101         cp /etc/hosts $DIR/$tdir/$tfile
11102         test_mkdir -c1 $DIR/$tdir/foo1
11103         test_mkdir -c1 $DIR/$tdir/foo2
11104         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11105         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11106         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11107         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11108         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11109         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11110                 $LFS fid2path $DIR $FID
11111                 err17935 "bad link ea"
11112         fi
11113     # middle
11114     rm $DIR/$tdir/foo2/zachary
11115     # last
11116     rm $DIR/$tdir/foo2/thor
11117     # first
11118     rm $DIR/$tdir/$tfile
11119     # rename
11120     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11121     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11122         then
11123         $LFS fid2path $DIR $FID
11124         err17935 "bad link rename"
11125     fi
11126     rm $DIR/$tdir/foo2/maggie
11127
11128         # overflow the EA
11129         local longname=filename_avg_len_is_thirty_two_
11130         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11131                 error "failed to hardlink many files"
11132         links=$($LFS fid2path $DIR $FID | wc -l)
11133         echo -n "${links}/1000 links in link EA"
11134         [[ $links -gt 60 ]] ||
11135                 err17935 "expected at least 60 links in link EA"
11136         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11137                 error "failed to unlink many hardlinks"
11138 }
11139 run_test 161a "link ea sanity"
11140
11141 test_161b() {
11142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11143         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11144         local MDTIDX=1
11145         local remote_dir=$DIR/$tdir/remote_dir
11146
11147         mkdir -p $DIR/$tdir
11148         $LFS mkdir -i $MDTIDX $remote_dir ||
11149                 error "create remote directory failed"
11150
11151         cp /etc/hosts $remote_dir/$tfile
11152         mkdir -p $remote_dir/foo1
11153         mkdir -p $remote_dir/foo2
11154         ln $remote_dir/$tfile $remote_dir/foo1/sofia
11155         ln $remote_dir/$tfile $remote_dir/foo2/zachary
11156         ln $remote_dir/$tfile $remote_dir/foo1/luna
11157         ln $remote_dir/$tfile $remote_dir/foo2/thor
11158
11159         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
11160                      tr -d ']')
11161         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11162                 $LFS fid2path $DIR $FID
11163                 err17935 "bad link ea"
11164         fi
11165         # middle
11166         rm $remote_dir/foo2/zachary
11167         # last
11168         rm $remote_dir/foo2/thor
11169         # first
11170         rm $remote_dir/$tfile
11171         # rename
11172         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
11173         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
11174         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
11175                 $LFS fid2path $DIR $FID
11176                 err17935 "bad link rename"
11177         fi
11178         rm $remote_dir/foo2/maggie
11179
11180         # overflow the EA
11181         local longname=filename_avg_len_is_thirty_two_
11182         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
11183                 error "failed to hardlink many files"
11184         links=$($LFS fid2path $DIR $FID | wc -l)
11185         echo -n "${links}/1000 links in link EA"
11186         [[ ${links} -gt 60 ]] ||
11187                 err17935 "expected at least 60 links in link EA"
11188         unlinkmany $remote_dir/foo2/$longname 1000 ||
11189         error "failed to unlink many hardlinks"
11190 }
11191 run_test 161b "link ea sanity under remote directory"
11192
11193 test_161c() {
11194         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11196         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
11197                 skip "Need MDS version at least 2.1.5" && return
11198
11199         # define CLF_RENAME_LAST 0x0001
11200         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
11201         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11202                 changelog_register -n)
11203
11204         trap cleanup_changelog EXIT
11205         rm -rf $DIR/$tdir
11206         mkdir -p $DIR/$tdir
11207         touch $DIR/$tdir/foo_161c
11208         touch $DIR/$tdir/bar_161c
11209         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11210         $LFS changelog $MDT0 | grep RENME
11211         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
11212                 cut -f5 -d' ')
11213         $LFS changelog_clear $MDT0 $CL_USER 0
11214         if [ x$flags != "x0x1" ]; then
11215                 error "flag $flags is not 0x1"
11216         fi
11217         echo "rename overwrite a target having nlink = 1," \
11218                 "changelog record has flags of $flags"
11219
11220         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
11221         touch $DIR/$tdir/foo_161c
11222         touch $DIR/$tdir/bar_161c
11223         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11224         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11225         $LFS changelog $MDT0 | grep RENME
11226         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11227         $LFS changelog_clear $MDT0 $CL_USER 0
11228         if [ x$flags != "x0x0" ]; then
11229                 error "flag $flags is not 0x0"
11230         fi
11231         echo "rename overwrite a target having nlink > 1," \
11232                 "changelog record has flags of $flags"
11233
11234         # rename doesn't overwrite a target (changelog flag 0x0)
11235         touch $DIR/$tdir/foo_161c
11236         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
11237         $LFS changelog $MDT0 | grep RENME
11238         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11239         $LFS changelog_clear $MDT0 $CL_USER 0
11240         if [ x$flags != "x0x0" ]; then
11241                 error "flag $flags is not 0x0"
11242         fi
11243         echo "rename doesn't overwrite a target," \
11244                 "changelog record has flags of $flags"
11245
11246         # define CLF_UNLINK_LAST 0x0001
11247         # unlink a file having nlink = 1 (changelog flag 0x1)
11248         rm -f $DIR/$tdir/foo2_161c
11249         $LFS changelog $MDT0 | grep UNLNK
11250         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11251         $LFS changelog_clear $MDT0 $CL_USER 0
11252         if [ x$flags != "x0x1" ]; then
11253                 error "flag $flags is not 0x1"
11254         fi
11255         echo "unlink a file having nlink = 1," \
11256                 "changelog record has flags of $flags"
11257
11258         # unlink a file having nlink > 1 (changelog flag 0x0)
11259         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11260         rm -f $DIR/$tdir/foobar_161c
11261         $LFS changelog $MDT0 | grep UNLNK
11262         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11263         $LFS changelog_clear $MDT0 $CL_USER 0
11264         if [ x$flags != "x0x0" ]; then
11265                 error "flag $flags is not 0x0"
11266         fi
11267         echo "unlink a file having nlink > 1," \
11268                 "changelog record has flags of $flags"
11269         cleanup_changelog
11270 }
11271 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
11272
11273 test_161d() {
11274         local user
11275         local pid
11276         local fid
11277
11278         # cleanup previous run
11279         rm -rf $DIR/$tdir/$tfile
11280
11281         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11282                 changelog_register -n)
11283         [[ $? -eq 0 ]] || error "changelog_register failed"
11284
11285         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
11286         # interfer with $MOUNT/.lustre/fid/ access
11287         mkdir $DIR/$tdir
11288         [[ $? -eq 0 ]] || error "mkdir failed"
11289
11290         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
11291         $LCTL set_param fail_loc=0x8000140c
11292         # 5s pause
11293         $LCTL set_param fail_val=5
11294
11295         # create file
11296         echo foofoo > $DIR/$tdir/$tfile &
11297         pid=$!
11298
11299         # wait for create to be delayed
11300         sleep 2
11301
11302         ps -p $pid
11303         [[ $? -eq 0 ]] || error "create should be blocked"
11304
11305         local tempfile=$(mktemp)
11306         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
11307         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
11308         # some delay may occur during ChangeLog publishing and file read just
11309         # above, that could allow file write to happen finally
11310         [[ -s $tempfile ]] && echo "file should be empty"
11311
11312         $LCTL set_param fail_loc=0
11313
11314         wait $pid
11315         [[ $? -eq 0 ]] || error "create failed"
11316
11317         $LFS changelog_clear $MDT0 $user 0
11318         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
11319 }
11320 run_test 161d "create with concurrent .lustre/fid access"
11321
11322 check_path() {
11323     local expected=$1
11324     shift
11325     local fid=$2
11326
11327     local path=$(${LFS} fid2path $*)
11328     # Remove the '//' indicating a remote directory
11329     path=$(echo $path | sed 's#//#/#g')
11330     RC=$?
11331
11332     if [ $RC -ne 0 ]; then
11333         err17935 "path looked up of $expected failed. Error $RC"
11334         return $RC
11335     elif [ "${path}" != "${expected}" ]; then
11336         err17935 "path looked up \"${path}\" instead of \"${expected}\""
11337         return 2
11338     fi
11339     echo "fid $fid resolves to path $path (expected $expected)"
11340 }
11341
11342 test_162a() { # was test_162
11343         # Make changes to filesystem
11344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11345         test_mkdir -p -c1 $DIR/$tdir/d2
11346         touch $DIR/$tdir/d2/$tfile
11347         touch $DIR/$tdir/d2/x1
11348         touch $DIR/$tdir/d2/x2
11349         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
11350         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
11351         # regular file
11352         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
11353         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
11354                 error "check path $tdir/d2/$tfile failed"
11355
11356         # softlink
11357         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
11358         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
11359         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
11360                 error "check path $tdir/d2/p/q/r/slink failed"
11361
11362         # softlink to wrong file
11363         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
11364         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
11365         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
11366                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
11367
11368         # hardlink
11369         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
11370         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
11371         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
11372         # fid2path dir/fsname should both work
11373         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
11374                 error "check path $tdir/d2/a/b/c/new_file failed"
11375         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
11376                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
11377
11378         # hardlink count: check that there are 2 links
11379         # Doesnt work with CMD yet: 17935
11380         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
11381                 err17935 "expected 2 links"
11382
11383         # hardlink indexing: remove the first link
11384         rm $DIR/$tdir/d2/p/q/r/hlink
11385         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
11386                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
11387
11388         return 0
11389 }
11390 run_test 162a "path lookup sanity"
11391
11392 test_162b() {
11393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11394         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11395
11396         mkdir $DIR/$tdir
11397         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
11398                                 error "create striped dir failed"
11399
11400         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
11401                                         tail -n 1 | awk '{print $2}')
11402         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
11403
11404         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
11405         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
11406
11407         # regular file
11408         for ((i=0;i<5;i++)); do
11409                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
11410                         error "get fid for f$i failed"
11411                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
11412                         error "check path $tdir/striped_dir/f$i failed"
11413
11414                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
11415                         error "get fid for d$i failed"
11416                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
11417                         error "check path $tdir/striped_dir/d$i failed"
11418         done
11419
11420         return 0
11421 }
11422 run_test 162b "striped directory path lookup sanity"
11423
11424 # LU-4239: Verify fid2path works with paths 100 or more directories deep
11425 test_162c() {
11426         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
11427                 skip "Need MDS version at least 2.7.51" && return
11428         test_mkdir $DIR/$tdir.local
11429         test_mkdir $DIR/$tdir.remote
11430         local lpath=$tdir.local
11431         local rpath=$tdir.remote
11432
11433         for ((i = 0; i <= 101; i++)); do
11434                 lpath="$lpath/$i"
11435                 mkdir $DIR/$lpath
11436                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
11437                         error "get fid for local directory $DIR/$lpath failed"
11438                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
11439                         error "check path for local directory $DIR/$lpath failed"
11440
11441                 rpath="$rpath/$i"
11442                 test_mkdir $DIR/$rpath
11443                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
11444                         error "get fid for remote directory $DIR/$rpath failed"
11445                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
11446                         error "check path for remote directory $DIR/$rpath failed"
11447         done
11448
11449         return 0
11450 }
11451 run_test 162c "fid2path works with paths 100 or more directories deep"
11452
11453 test_169() {
11454         # do directio so as not to populate the page cache
11455         log "creating a 10 Mb file"
11456         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11457         log "starting reads"
11458         dd if=$DIR/$tfile of=/dev/null bs=4096 &
11459         log "truncating the file"
11460         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11461         log "killing dd"
11462         kill %+ || true # reads might have finished
11463         echo "wait until dd is finished"
11464         wait
11465         log "removing the temporary file"
11466         rm -rf $DIR/$tfile || error "tmp file removal failed"
11467 }
11468 run_test 169 "parallel read and truncate should not deadlock"
11469
11470 test_170() {
11471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11472         $LCTL clear     # bug 18514
11473         $LCTL debug_daemon start $TMP/${tfile}_log_good
11474         touch $DIR/$tfile
11475         $LCTL debug_daemon stop
11476         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
11477                error "sed failed to read log_good"
11478
11479         $LCTL debug_daemon start $TMP/${tfile}_log_good
11480         rm -rf $DIR/$tfile
11481         $LCTL debug_daemon stop
11482
11483         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
11484                error "lctl df log_bad failed"
11485
11486         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11487         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11488
11489         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
11490         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
11491
11492         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
11493                 error "bad_line good_line1 good_line2 are empty"
11494
11495         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11496         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
11497         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11498
11499         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
11500         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11501         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11502
11503         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
11504                 error "bad_line_new good_line_new are empty"
11505
11506         local expected_good=$((good_line1 + good_line2*2))
11507
11508         rm -f $TMP/${tfile}*
11509         # LU-231, short malformed line may not be counted into bad lines
11510         if [ $bad_line -ne $bad_line_new ] &&
11511                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
11512                 error "expected $bad_line bad lines, but got $bad_line_new"
11513                 return 1
11514         fi
11515
11516         if [ $expected_good -ne $good_line_new ]; then
11517                 error "expected $expected_good good lines, but got $good_line_new"
11518                 return 2
11519         fi
11520         true
11521 }
11522 run_test 170 "test lctl df to handle corrupted log ====================="
11523
11524 test_171() { # bug20592
11525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11526 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11527         $LCTL set_param fail_loc=0x50e
11528         $LCTL set_param fail_val=3000
11529         multiop_bg_pause $DIR/$tfile O_s || true
11530         local MULTIPID=$!
11531         kill -USR1 $MULTIPID
11532         # cause log dump
11533         sleep 3
11534         wait $MULTIPID
11535         if dmesg | grep "recursive fault"; then
11536                 error "caught a recursive fault"
11537         fi
11538         $LCTL set_param fail_loc=0
11539         true
11540 }
11541 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11542
11543 # it would be good to share it with obdfilter-survey/iokit-libecho code
11544 setup_obdecho_osc () {
11545         local rc=0
11546         local ost_nid=$1
11547         local obdfilter_name=$2
11548         echo "Creating new osc for $obdfilter_name on $ost_nid"
11549         # make sure we can find loopback nid
11550         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11551
11552         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11553                            ${obdfilter_name}_osc_UUID || rc=2; }
11554         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11555                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11556         return $rc
11557 }
11558
11559 cleanup_obdecho_osc () {
11560         local obdfilter_name=$1
11561         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11562         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11563         return 0
11564 }
11565
11566 obdecho_test() {
11567         local OBD=$1
11568         local node=$2
11569         local pages=${3:-64}
11570         local rc=0
11571         local id
11572
11573         local count=10
11574         local obd_size=$(get_obd_size $node $OBD)
11575         local page_size=$(get_page_size $node)
11576         if [[ -n "$obd_size" ]]; then
11577                 local new_count=$((obd_size / (pages * page_size / 1024)))
11578                 [[ $new_count -ge $count ]] || count=$new_count
11579         fi
11580
11581         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11582         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11583                            rc=2; }
11584         if [ $rc -eq 0 ]; then
11585             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11586             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11587         fi
11588         echo "New object id is $id"
11589         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11590                            rc=4; }
11591         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11592                            "test_brw $count w v $pages $id" || rc=4; }
11593         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11594                            rc=4; }
11595         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11596                                         "cleanup" || rc=5; }
11597         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11598                                         "detach" || rc=6; }
11599         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11600         return $rc
11601 }
11602
11603 test_180a() {
11604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11605         remote_ost_nodsh && skip "remote OST with nodsh" && return
11606         local rc=0
11607         local rmmod_local=0
11608
11609         if ! module_loaded obdecho; then
11610             load_module obdecho/obdecho
11611             rmmod_local=1
11612         fi
11613
11614         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11615         local host=$(lctl get_param -n osc.$osc.import |
11616                              awk '/current_connection:/ {print $2}' )
11617         local target=$(lctl get_param -n osc.$osc.import |
11618                              awk '/target:/ {print $2}' )
11619         target=${target%_UUID}
11620
11621         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11622         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11623         [[ -n $target ]] && cleanup_obdecho_osc $target
11624         [ $rmmod_local -eq 1 ] && rmmod obdecho
11625         return $rc
11626 }
11627 run_test 180a "test obdecho on osc"
11628
11629 test_180b() {
11630         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11631         remote_ost_nodsh && skip "remote OST with nodsh" && return
11632         local rc=0
11633         local rmmod_remote=0
11634
11635         do_facet ost1 "lsmod | grep -q obdecho || "                      \
11636                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
11637                       "modprobe obdecho; }" && rmmod_remote=1
11638         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11639         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11640         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
11641         return $rc
11642 }
11643 run_test 180b "test obdecho directly on obdfilter"
11644
11645 test_180c() { # LU-2598
11646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11647         remote_ost_nodsh && skip "remote OST with nodsh" && return
11648         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11649                 skip "Need MDS version at least 2.4.0" && return
11650
11651         local rc=0
11652         local rmmod_remote=false
11653         local pages=16384 # 64MB bulk I/O RPC size
11654         local target
11655
11656         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11657                 rmmod_remote=true || error "failed to load module obdecho"
11658
11659         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11660                 head -n1)
11661         if [ -n "$target" ]; then
11662                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11663         else
11664                 echo "there is no obdfilter target on ost1"
11665                 rc=2
11666         fi
11667         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11668         return $rc
11669 }
11670 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11671
11672 test_181() { # bug 22177
11673         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11674         # create enough files to index the directory
11675         createmany -o $DIR/$tdir/foobar 4000
11676         # print attributes for debug purpose
11677         lsattr -d .
11678         # open dir
11679         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11680         MULTIPID=$!
11681         # remove the files & current working dir
11682         unlinkmany $DIR/$tdir/foobar 4000
11683         rmdir $DIR/$tdir
11684         kill -USR1 $MULTIPID
11685         wait $MULTIPID
11686         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11687         return 0
11688 }
11689 run_test 181 "Test open-unlinked dir ========================"
11690
11691 test_182() {
11692         local fcount=1000
11693         local tcount=10
11694
11695         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11696
11697         $LCTL set_param mdc.*.rpc_stats=clear
11698
11699         for (( i = 0; i < $tcount; i++ )) ; do
11700                 mkdir $DIR/$tdir/$i
11701         done
11702
11703         for (( i = 0; i < $tcount; i++ )) ; do
11704                 createmany -o $DIR/$tdir/$i/f- $fcount &
11705         done
11706         wait
11707
11708         for (( i = 0; i < $tcount; i++ )) ; do
11709                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11710         done
11711         wait
11712
11713         $LCTL get_param mdc.*.rpc_stats
11714
11715         rm -rf $DIR/$tdir
11716 }
11717 run_test 182 "Test parallel modify metadata operations ================"
11718
11719 test_183() { # LU-2275
11720         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11721         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11722                 skip "Need MDS version at least 2.3.56" && return
11723
11724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11725         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11726         echo aaa > $DIR/$tdir/$tfile
11727
11728 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11729         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11730
11731         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11732         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11733
11734         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11735
11736         # Flush negative dentry cache
11737         touch $DIR/$tdir/$tfile
11738
11739         # We are not checking for any leaked references here, they'll
11740         # become evident next time we do cleanup with module unload.
11741         rm -rf $DIR/$tdir
11742 }
11743 run_test 183 "No crash or request leak in case of strange dispositions ========"
11744
11745 # test suite 184 is for LU-2016, LU-2017
11746 test_184a() {
11747         check_swap_layouts_support && return 0
11748
11749         dir0=$DIR/$tdir/$testnum
11750         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11751         ref1=/etc/passwd
11752         ref2=/etc/group
11753         file1=$dir0/f1
11754         file2=$dir0/f2
11755         $SETSTRIPE -c1 $file1
11756         cp $ref1 $file1
11757         $SETSTRIPE -c2 $file2
11758         cp $ref2 $file2
11759         gen1=$($GETSTRIPE -g $file1)
11760         gen2=$($GETSTRIPE -g $file2)
11761
11762         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11763         gen=$($GETSTRIPE -g $file1)
11764         [[ $gen1 != $gen ]] ||
11765                 "Layout generation on $file1 does not change"
11766         gen=$($GETSTRIPE -g $file2)
11767         [[ $gen2 != $gen ]] ||
11768                 "Layout generation on $file2 does not change"
11769
11770         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11771         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11772 }
11773 run_test 184a "Basic layout swap"
11774
11775 test_184b() {
11776         check_swap_layouts_support && return 0
11777
11778         dir0=$DIR/$tdir/$testnum
11779         mkdir -p $dir0 || error "creating dir $dir0"
11780         file1=$dir0/f1
11781         file2=$dir0/f2
11782         file3=$dir0/f3
11783         dir1=$dir0/d1
11784         dir2=$dir0/d2
11785         mkdir $dir1 $dir2
11786         $SETSTRIPE -c1 $file1
11787         $SETSTRIPE -c2 $file2
11788         $SETSTRIPE -c1 $file3
11789         chown $RUNAS_ID $file3
11790         gen1=$($GETSTRIPE -g $file1)
11791         gen2=$($GETSTRIPE -g $file2)
11792
11793         $LFS swap_layouts $dir1 $dir2 &&
11794                 error "swap of directories layouts should fail"
11795         $LFS swap_layouts $dir1 $file1 &&
11796                 error "swap of directory and file layouts should fail"
11797         $RUNAS $LFS swap_layouts $file1 $file2 &&
11798                 error "swap of file we cannot write should fail"
11799         $LFS swap_layouts $file1 $file3 &&
11800                 error "swap of file with different owner should fail"
11801         /bin/true # to clear error code
11802 }
11803 run_test 184b "Forbidden layout swap (will generate errors)"
11804
11805 test_184c() {
11806         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
11807         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
11808         check_swap_layouts_support && return 0
11809
11810         local dir0=$DIR/$tdir/$testnum
11811         mkdir -p $dir0 || error "creating dir $dir0"
11812
11813         local ref1=$dir0/ref1
11814         local ref2=$dir0/ref2
11815         local file1=$dir0/file1
11816         local file2=$dir0/file2
11817         # create a file large enough for the concurrent test
11818         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11819         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11820         echo "ref file size: ref1($(stat -c %s $ref1))," \
11821              "ref2($(stat -c %s $ref2))"
11822
11823         cp $ref2 $file2
11824         dd if=$ref1 of=$file1 bs=16k &
11825         local DD_PID=$!
11826
11827         # Make sure dd starts to copy file
11828         while [ ! -f $file1 ]; do sleep 0.1; done
11829
11830         $LFS swap_layouts $file1 $file2
11831         local rc=$?
11832         wait $DD_PID
11833         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11834         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11835
11836         # how many bytes copied before swapping layout
11837         local copied=$(stat -c %s $file2)
11838         local remaining=$(stat -c %s $ref1)
11839         remaining=$((remaining - copied))
11840         echo "Copied $copied bytes before swapping layout..."
11841
11842         cmp -n $copied $file1 $ref2 | grep differ &&
11843                 error "Content mismatch [0, $copied) of ref2 and file1"
11844         cmp -n $copied $file2 $ref1 ||
11845                 error "Content mismatch [0, $copied) of ref1 and file2"
11846         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11847                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11848
11849         # clean up
11850         rm -f $ref1 $ref2 $file1 $file2
11851 }
11852 run_test 184c "Concurrent write and layout swap"
11853
11854 test_184d() {
11855         check_swap_layouts_support && return 0
11856         [ -z "$(which getfattr 2>/dev/null)" ] &&
11857                 skip "no getfattr command" && return 0
11858
11859         local file1=$DIR/$tdir/$tfile-1
11860         local file2=$DIR/$tdir/$tfile-2
11861         local file3=$DIR/$tdir/$tfile-3
11862         local lovea1
11863         local lovea2
11864
11865         mkdir -p $DIR/$tdir
11866         touch $file1 || error "create $file1 failed"
11867         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11868                 error "create $file2 failed"
11869         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11870                 error "create $file3 failed"
11871         lovea1=$($LFS getstripe $file1 | sed 1d)
11872
11873         $LFS swap_layouts $file2 $file3 ||
11874                 error "swap $file2 $file3 layouts failed"
11875         $LFS swap_layouts $file1 $file2 ||
11876                 error "swap $file1 $file2 layouts failed"
11877
11878         lovea2=$($LFS getstripe $file2 | sed 1d)
11879         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11880
11881         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11882         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11883 }
11884 run_test 184d "allow stripeless layouts swap"
11885
11886 test_184e() {
11887         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11888                 { skip "Need MDS version at least 2.6.94"; return 0; }
11889         check_swap_layouts_support && return 0
11890         [ -z "$(which getfattr 2>/dev/null)" ] &&
11891                 skip "no getfattr command" && return 0
11892
11893         local file1=$DIR/$tdir/$tfile-1
11894         local file2=$DIR/$tdir/$tfile-2
11895         local file3=$DIR/$tdir/$tfile-3
11896         local lovea
11897
11898         mkdir -p $DIR/$tdir
11899         touch $file1 || error "create $file1 failed"
11900         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11901                 error "create $file2 failed"
11902         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11903                 error "create $file3 failed"
11904
11905         $LFS swap_layouts $file1 $file2 ||
11906                 error "swap $file1 $file2 layouts failed"
11907
11908         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11909         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11910
11911         echo 123 > $file1 || error "Should be able to write into $file1"
11912
11913         $LFS swap_layouts $file1 $file3 ||
11914                 error "swap $file1 $file3 layouts failed"
11915
11916         echo 123 > $file1 || error "Should be able to write into $file1"
11917
11918         rm -rf $file1 $file2 $file3
11919 }
11920 run_test 184e "Recreate layout after stripeless layout swaps"
11921
11922 test_185() { # LU-2441
11923         # LU-3553 - no volatile file support in old servers
11924         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11925                 { skip "Need MDS version at least 2.3.60"; return 0; }
11926
11927         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11928         touch $DIR/$tdir/spoo
11929         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11930         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11931                 error "cannot create/write a volatile file"
11932         [ "$FILESET" == "" ] &&
11933         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11934                 error "FID is still valid after close"
11935
11936         multiop_bg_pause $DIR/$tdir vVw4096_c
11937         local multi_pid=$!
11938
11939         local OLD_IFS=$IFS
11940         IFS=":"
11941         local fidv=($fid)
11942         IFS=$OLD_IFS
11943         # assume that the next FID for this client is sequential, since stdout
11944         # is unfortunately eaten by multiop_bg_pause
11945         local n=$((${fidv[1]} + 1))
11946         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11947         if [ "$FILESET" == "" ]; then
11948                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11949                         error "FID is missing before close"
11950         fi
11951         kill -USR1 $multi_pid
11952         # 1 second delay, so if mtime change we will see it
11953         sleep 1
11954         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11955         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11956 }
11957 run_test 185 "Volatile file support"
11958
11959 test_187a() {
11960         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11961         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11962                 skip "Need MDS version at least 2.3.0" && return
11963
11964         local dir0=$DIR/$tdir/$testnum
11965         mkdir -p $dir0 || error "creating dir $dir0"
11966
11967         local file=$dir0/file1
11968         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11969         local dv1=$($LFS data_version $file)
11970         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11971         local dv2=$($LFS data_version $file)
11972         [[ $dv1 != $dv2 ]] ||
11973                 error "data version did not change on write $dv1 == $dv2"
11974
11975         # clean up
11976         rm -f $file1
11977 }
11978 run_test 187a "Test data version change"
11979
11980 test_187b() {
11981         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11982         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11983                 skip "Need MDS version at least 2.3.0" && return
11984
11985         local dir0=$DIR/$tdir/$testnum
11986         mkdir -p $dir0 || error "creating dir $dir0"
11987
11988         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11989         [[ ${DV[0]} != ${DV[1]} ]] ||
11990                 error "data version did not change on write"\
11991                       " ${DV[0]} == ${DV[1]}"
11992
11993         # clean up
11994         rm -f $file1
11995 }
11996 run_test 187b "Test data version change on volatile file"
11997
11998 test_200() {
11999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12000         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12001         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12002
12003         local POOL=${POOL:-cea1}
12004         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12005         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12006         # Pool OST targets
12007         local first_ost=0
12008         local last_ost=$(($OSTCOUNT - 1))
12009         local ost_step=2
12010         local ost_list=$(seq $first_ost $ost_step $last_ost)
12011         local ost_range="$first_ost $last_ost $ost_step"
12012         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12013         local file_dir=$POOL_ROOT/file_tst
12014         local subdir=$test_path/subdir
12015
12016         local rc=0
12017         while : ; do
12018                 # former test_200a test_200b
12019                 pool_add $POOL                          || { rc=$? ; break; }
12020                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12021                 # former test_200c test_200d
12022                 mkdir -p $test_path
12023                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12024                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12025                 mkdir -p $subdir
12026                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12027                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12028                                                         || { rc=$? ; break; }
12029                 # former test_200e test_200f
12030                 local files=$((OSTCOUNT*3))
12031                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12032                                                         || { rc=$? ; break; }
12033                 pool_create_files $POOL $file_dir $files "$ost_list" \
12034                                                         || { rc=$? ; break; }
12035                 # former test_200g test_200h
12036                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12037                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12038
12039                 # former test_201a test_201b test_201c
12040                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12041
12042                 local f=$test_path/$tfile
12043                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12044                 pool_remove $POOL $f                    || { rc=$? ; break; }
12045                 break
12046         done
12047
12048         destroy_test_pools
12049         return $rc
12050 }
12051 run_test 200 "OST pools"
12052
12053 # usage: default_attr <count | size | offset>
12054 default_attr() {
12055         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12056 }
12057
12058 # usage: check_default_stripe_attr
12059 check_default_stripe_attr() {
12060         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12061         case $1 in
12062         --stripe-count|--count)
12063                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12064         --stripe-size|--size)
12065                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12066         --stripe-index|--index)
12067                 EXPECTED=-1;;
12068         *)
12069                 error "unknown getstripe attr '$1'"
12070         esac
12071
12072         [ $ACTUAL != $EXPECTED ] &&
12073                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12074 }
12075
12076 test_204a() {
12077         test_mkdir -p $DIR/$tdir
12078         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12079
12080         check_default_stripe_attr --stripe-count
12081         check_default_stripe_attr --stripe-size
12082         check_default_stripe_attr --stripe-index
12083
12084         return 0
12085 }
12086 run_test 204a "Print default stripe attributes ================="
12087
12088 test_204b() {
12089         test_mkdir -p $DIR/$tdir
12090         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12091
12092         check_default_stripe_attr --stripe-size
12093         check_default_stripe_attr --stripe-index
12094
12095         return 0
12096 }
12097 run_test 204b "Print default stripe size and offset  ==========="
12098
12099 test_204c() {
12100         test_mkdir -p $DIR/$tdir
12101         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12102
12103         check_default_stripe_attr --stripe-count
12104         check_default_stripe_attr --stripe-index
12105
12106         return 0
12107 }
12108 run_test 204c "Print default stripe count and offset ==========="
12109
12110 test_204d() {
12111         test_mkdir -p $DIR/$tdir
12112         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12113
12114         check_default_stripe_attr --stripe-count
12115         check_default_stripe_attr --stripe-size
12116
12117         return 0
12118 }
12119 run_test 204d "Print default stripe count and size ============="
12120
12121 test_204e() {
12122         test_mkdir -p $DIR/$tdir
12123         $SETSTRIPE -d $DIR/$tdir
12124
12125         check_default_stripe_attr --stripe-count --raw
12126         check_default_stripe_attr --stripe-size --raw
12127         check_default_stripe_attr --stripe-index --raw
12128
12129         return 0
12130 }
12131 run_test 204e "Print raw stripe attributes ================="
12132
12133 test_204f() {
12134         test_mkdir -p $DIR/$tdir
12135         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12136
12137         check_default_stripe_attr --stripe-size --raw
12138         check_default_stripe_attr --stripe-index --raw
12139
12140         return 0
12141 }
12142 run_test 204f "Print raw stripe size and offset  ==========="
12143
12144 test_204g() {
12145         test_mkdir -p $DIR/$tdir
12146         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12147
12148         check_default_stripe_attr --stripe-count --raw
12149         check_default_stripe_attr --stripe-index --raw
12150
12151         return 0
12152 }
12153 run_test 204g "Print raw stripe count and offset ==========="
12154
12155 test_204h() {
12156         test_mkdir -p $DIR/$tdir
12157         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12158
12159         check_default_stripe_attr --stripe-count --raw
12160         check_default_stripe_attr --stripe-size --raw
12161
12162         return 0
12163 }
12164 run_test 204h "Print raw stripe count and size ============="
12165
12166 # Figure out which job scheduler is being used, if any,
12167 # or use a fake one
12168 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
12169         JOBENV=SLURM_JOB_ID
12170 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
12171         JOBENV=LSB_JOBID
12172 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
12173         JOBENV=PBS_JOBID
12174 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
12175         JOBENV=LOADL_STEP_ID
12176 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
12177         JOBENV=JOB_ID
12178 else
12179         $LCTL list_param jobid_name > /dev/null 2>&1
12180         if [ $? -eq 0 ]; then
12181                 JOBENV=nodelocal
12182         else
12183                 JOBENV=FAKE_JOBID
12184         fi
12185 fi
12186
12187 verify_jobstats() {
12188         local cmd=($1)
12189         shift
12190         local facets="$@"
12191
12192 # we don't really need to clear the stats for this test to work, since each
12193 # command has a unique jobid, but it makes debugging easier if needed.
12194 #       for facet in $facets; do
12195 #               local dev=$(convert_facet2label $facet)
12196 #               # clear old jobstats
12197 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
12198 #       done
12199
12200         # use a new JobID for each test, or we might see an old one
12201         [ "$JOBENV" = "FAKE_JOBID" ] &&
12202                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12203
12204         JOBVAL=${!JOBENV}
12205
12206         [ "$JOBENV" = "nodelocal" ] && {
12207                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12208                 $LCTL set_param jobid_name=$FAKE_JOBID
12209                 JOBVAL=$FAKE_JOBID
12210         }
12211
12212         log "Test: ${cmd[*]}"
12213         log "Using JobID environment variable $JOBENV=$JOBVAL"
12214
12215         if [ $JOBENV = "FAKE_JOBID" ]; then
12216                 FAKE_JOBID=$JOBVAL ${cmd[*]}
12217         else
12218                 ${cmd[*]}
12219         fi
12220
12221         # all files are created on OST0000
12222         for facet in $facets; do
12223                 local stats="*.$(convert_facet2label $facet).job_stats"
12224                 if [ $(do_facet $facet lctl get_param $stats |
12225                        grep -c $JOBVAL) -ne 1 ]; then
12226                         do_facet $facet lctl get_param $stats
12227                         error "No jobstats for $JOBVAL found on $facet::$stats"
12228                 fi
12229         done
12230 }
12231
12232 jobstats_set() {
12233         trap 0
12234         NEW_JOBENV=${1:-$OLD_JOBENV}
12235         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
12236         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
12237 }
12238
12239 cleanup_205() {
12240         trap 0
12241         do_facet $SINGLEMDS \
12242                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
12243         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
12244         cleanup_changelog
12245 }
12246
12247 test_205() { # Job stats
12248         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
12249                 { skip "Need MDS version with at least 2.7.1"; return 0; }
12250
12251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12252         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12253         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12254         remote_ost_nodsh && skip "remote OST with nodsh" && return
12255
12256         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
12257                 skip "Server doesn't support jobstats" && return 0
12258         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
12259
12260         OLD_JOBENV=$($LCTL get_param -n jobid_var)
12261         if [ $OLD_JOBENV != $JOBENV ]; then
12262                 jobstats_set $JOBENV
12263                 trap cleanup_205 EXIT
12264         fi
12265
12266         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
12267         echo "Registered as changelog user $CL_USER"
12268
12269         OLD_INTERVAL=$(do_facet $SINGLEMDS \
12270                        lctl get_param -n mdt.*.job_cleanup_interval)
12271         local interval_new=5
12272         do_facet $SINGLEMDS \
12273                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
12274         local start=$SECONDS
12275
12276         local cmd
12277         # mkdir
12278         cmd="mkdir $DIR/$tdir"
12279         verify_jobstats "$cmd" "$SINGLEMDS"
12280         # rmdir
12281         cmd="rmdir $DIR/$tdir"
12282         verify_jobstats "$cmd" "$SINGLEMDS"
12283         # mkdir on secondary MDT
12284         if [ $MDSCOUNT -gt 1 ]; then
12285                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
12286                 verify_jobstats "$cmd" "mds2"
12287         fi
12288         # mknod
12289         cmd="mknod $DIR/$tfile c 1 3"
12290         verify_jobstats "$cmd" "$SINGLEMDS"
12291         # unlink
12292         cmd="rm -f $DIR/$tfile"
12293         verify_jobstats "$cmd" "$SINGLEMDS"
12294         # create all files on OST0000 so verify_jobstats can find OST stats
12295         # open & close
12296         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
12297         verify_jobstats "$cmd" "$SINGLEMDS"
12298         # setattr
12299         cmd="touch $DIR/$tfile"
12300         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12301         # write
12302         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
12303         verify_jobstats "$cmd" "ost1"
12304         # read
12305         cancel_lru_locks osc
12306         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
12307         verify_jobstats "$cmd" "ost1"
12308         # truncate
12309         cmd="$TRUNCATE $DIR/$tfile 0"
12310         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12311         # rename
12312         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
12313         verify_jobstats "$cmd" "$SINGLEMDS"
12314         # jobstats expiry - sleep until old stats should be expired
12315         local left=$((interval_new + 5 - (SECONDS - start)))
12316         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
12317                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
12318                         "0" $left
12319         cmd="mkdir $DIR/$tdir.expire"
12320         verify_jobstats "$cmd" "$SINGLEMDS"
12321         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
12322             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
12323
12324         # Ensure that jobid are present in changelog (if supported by MDS)
12325         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
12326         then
12327                 $LFS changelog $MDT0 | tail -9
12328                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
12329                 [ $jobids -eq 9 ] ||
12330                         error "Wrong changelog jobid count $jobids != 9"
12331
12332                 # LU-5862
12333                 JOBENV="disable"
12334                 jobstats_set $JOBENV
12335                 touch $DIR/$tfile
12336                 $LFS changelog $MDT0 | tail -1
12337                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
12338                 [ $jobids -eq 0 ] ||
12339                         error "Unexpected jobids when jobid_var=$JOBENV"
12340         fi
12341
12342         cleanup_205
12343 }
12344 run_test 205 "Verify job stats"
12345
12346 # LU-1480, LU-1773 and LU-1657
12347 test_206() {
12348         mkdir -p $DIR/$tdir
12349         $SETSTRIPE -c -1 $DIR/$tdir
12350 #define OBD_FAIL_LOV_INIT 0x1403
12351         $LCTL set_param fail_loc=0xa0001403
12352         $LCTL set_param fail_val=1
12353         touch $DIR/$tdir/$tfile || true
12354 }
12355 run_test 206 "fail lov_init_raid0() doesn't lbug"
12356
12357 test_207a() {
12358         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12359         local fsz=`stat -c %s $DIR/$tfile`
12360         cancel_lru_locks mdc
12361
12362         # do not return layout in getattr intent
12363 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
12364         $LCTL set_param fail_loc=0x170
12365         local sz=`stat -c %s $DIR/$tfile`
12366
12367         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
12368
12369         rm -rf $DIR/$tfile
12370 }
12371 run_test 207a "can refresh layout at glimpse"
12372
12373 test_207b() {
12374         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12375         local cksum=`md5sum $DIR/$tfile`
12376         local fsz=`stat -c %s $DIR/$tfile`
12377         cancel_lru_locks mdc
12378         cancel_lru_locks osc
12379
12380         # do not return layout in getattr intent
12381 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
12382         $LCTL set_param fail_loc=0x171
12383
12384         # it will refresh layout after the file is opened but before read issues
12385         echo checksum is "$cksum"
12386         echo "$cksum" |md5sum -c --quiet || error "file differs"
12387
12388         rm -rf $DIR/$tfile
12389 }
12390 run_test 207b "can refresh layout at open"
12391
12392 test_208() {
12393         # FIXME: in this test suite, only RD lease is used. This is okay
12394         # for now as only exclusive open is supported. After generic lease
12395         # is done, this test suite should be revised. - Jinshan
12396
12397         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12398         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
12399                 { skip "Need MDS version at least 2.4.52"; return 0; }
12400
12401         echo "==== test 1: verify get lease work"
12402         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
12403
12404         echo "==== test 2: verify lease can be broken by upcoming open"
12405         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12406         local PID=$!
12407         sleep 1
12408
12409         $MULTIOP $DIR/$tfile oO_RDONLY:c
12410         kill -USR1 $PID && wait $PID || error "break lease error"
12411
12412         echo "==== test 3: verify lease can't be granted if an open already exists"
12413         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
12414         local PID=$!
12415         sleep 1
12416
12417         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
12418         kill -USR1 $PID && wait $PID || error "open file error"
12419
12420         echo "==== test 4: lease can sustain over recovery"
12421         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
12422         PID=$!
12423         sleep 1
12424
12425         fail mds1
12426
12427         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
12428
12429         echo "==== test 5: lease broken can't be regained by replay"
12430         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12431         PID=$!
12432         sleep 1
12433
12434         # open file to break lease and then recovery
12435         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
12436         fail mds1
12437
12438         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
12439
12440         rm -f $DIR/$tfile
12441 }
12442 run_test 208 "Exclusive open"
12443
12444 test_209() {
12445         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
12446                 skip_env "must have disp_stripe" && return
12447
12448         touch $DIR/$tfile
12449         sync; sleep 5; sync;
12450
12451         echo 3 > /proc/sys/vm/drop_caches
12452         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12453
12454         # open/close 500 times
12455         for i in $(seq 500); do
12456                 cat $DIR/$tfile
12457         done
12458
12459         echo 3 > /proc/sys/vm/drop_caches
12460         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12461
12462         echo "before: $req_before, after: $req_after"
12463         [ $((req_after - req_before)) -ge 300 ] &&
12464                 error "open/close requests are not freed"
12465         return 0
12466 }
12467 run_test 209 "read-only open/close requests should be freed promptly"
12468
12469 test_212() {
12470         size=`date +%s`
12471         size=$((size % 8192 + 1))
12472         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
12473         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
12474         rm -f $DIR/f212 $DIR/f212.xyz
12475 }
12476 run_test 212 "Sendfile test ============================================"
12477
12478 test_213() {
12479         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
12480         cancel_lru_locks osc
12481         lctl set_param fail_loc=0x8000040f
12482         # generate a read lock
12483         cat $DIR/$tfile > /dev/null
12484         # write to the file, it will try to cancel the above read lock.
12485         cat /etc/hosts >> $DIR/$tfile
12486 }
12487 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
12488
12489 test_214() { # for bug 20133
12490         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
12491         for (( i=0; i < 340; i++ )) ; do
12492                 touch $DIR/$tdir/d214c/a$i
12493         done
12494
12495         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
12496         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
12497         ls $DIR/d214c || error "ls $DIR/d214c failed"
12498         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
12499         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
12500 }
12501 run_test 214 "hash-indexed directory test - bug 20133"
12502
12503 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
12504 create_lnet_proc_files() {
12505         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
12506         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
12507
12508         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
12509         rm -f "$TMP/lnet_$1.sys_tmp"
12510 }
12511
12512 # counterpart of create_lnet_proc_files
12513 remove_lnet_proc_files() {
12514         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
12515 }
12516
12517 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12518 # 3rd arg as regexp for body
12519 check_lnet_proc_stats() {
12520         local l=$(cat "$TMP/lnet_$1" |wc -l)
12521         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
12522
12523         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
12524 }
12525
12526 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12527 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
12528 # optional and can be regexp for 2nd line (lnet.routes case)
12529 check_lnet_proc_entry() {
12530         local blp=2          # blp stands for 'position of 1st line of body'
12531         [ -z "$5" ] || blp=3 # lnet.routes case
12532
12533         local l=$(cat "$TMP/lnet_$1" |wc -l)
12534         # subtracting one from $blp because the body can be empty
12535         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12536
12537         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12538                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12539
12540         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12541                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12542
12543         # bail out if any unexpected line happened
12544         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12545         [ "$?" != 0 ] || error "$2 misformatted"
12546 }
12547
12548 test_215() { # for bugs 18102, 21079, 21517
12549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12550         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12551         local P='[1-9][0-9]*'           # positive numeric
12552         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12553         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12554         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12555         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12556
12557         local L1 # regexp for 1st line
12558         local L2 # regexp for 2nd line (optional)
12559         local BR # regexp for the rest (body)
12560
12561         # lnet.stats should look as 11 space-separated non-negative numerics
12562         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12563         create_lnet_proc_files "stats"
12564         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12565         remove_lnet_proc_files "stats"
12566
12567         # lnet.routes should look like this:
12568         # Routing disabled/enabled
12569         # net hops priority state router
12570         # where net is a string like tcp0, hops > 0, priority >= 0,
12571         # state is up/down,
12572         # router is a string like 192.168.1.1@tcp2
12573         L1="^Routing (disabled|enabled)$"
12574         L2="^net +hops +priority +state +router$"
12575         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12576         create_lnet_proc_files "routes"
12577         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12578         remove_lnet_proc_files "routes"
12579
12580         # lnet.routers should look like this:
12581         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12582         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12583         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12584         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12585         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12586         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12587         create_lnet_proc_files "routers"
12588         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12589         remove_lnet_proc_files "routers"
12590
12591         # lnet.peers should look like this:
12592         # nid refs state last max rtr min tx min queue
12593         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12594         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12595         # numeric (0 or >0 or <0), queue >= 0.
12596         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12597         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12598         create_lnet_proc_files "peers"
12599         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12600         remove_lnet_proc_files "peers"
12601
12602         # lnet.buffers  should look like this:
12603         # pages count credits min
12604         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12605         L1="^pages +count +credits +min$"
12606         BR="^ +$N +$N +$I +$I$"
12607         create_lnet_proc_files "buffers"
12608         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12609         remove_lnet_proc_files "buffers"
12610
12611         # lnet.nis should look like this:
12612         # nid status alive refs peer rtr max tx min
12613         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12614         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12615         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12616         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12617         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12618         create_lnet_proc_files "nis"
12619         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12620         remove_lnet_proc_files "nis"
12621
12622         # can we successfully write to lnet.stats?
12623         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12624         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12625 }
12626 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12627
12628 test_216() { # bug 20317
12629         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12630         remote_ost_nodsh && skip "remote OST with nodsh" && return
12631
12632         local node
12633         local facets=$(get_facets OST)
12634         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12635
12636         save_lustre_params client "osc.*.contention_seconds" > $p
12637         save_lustre_params $facets \
12638                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12639         save_lustre_params $facets \
12640                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12641         save_lustre_params $facets \
12642                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12643         clear_osc_stats
12644
12645         # agressive lockless i/o settings
12646         for node in $(osts_nodes); do
12647                 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'
12648         done
12649         lctl set_param -n osc.*.contention_seconds 60
12650
12651         $DIRECTIO write $DIR/$tfile 0 10 4096
12652         $CHECKSTAT -s 40960 $DIR/$tfile
12653
12654         # disable lockless i/o
12655         for node in $(osts_nodes); do
12656                 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'
12657         done
12658         lctl set_param -n osc.*.contention_seconds 0
12659         clear_osc_stats
12660
12661         dd if=/dev/zero of=$DIR/$tfile count=0
12662         $CHECKSTAT -s 0 $DIR/$tfile
12663
12664         restore_lustre_params <$p
12665         rm -f $p
12666         rm $DIR/$tfile
12667 }
12668 run_test 216 "check lockless direct write works and updates file size and kms correctly"
12669
12670 test_217() { # bug 22430
12671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12672         local node
12673         local nid
12674
12675         for node in $(nodes_list); do
12676                 nid=$(host_nids_address $node $NETTYPE)
12677                 if [[ $nid = *-* ]] ; then
12678                         echo "lctl ping $nid@$NETTYPE"
12679                         lctl ping $nid@$NETTYPE
12680                 else
12681                         echo "skipping $node (no hyphen detected)"
12682                 fi
12683         done
12684 }
12685 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12686
12687 test_218() {
12688        # do directio so as not to populate the page cache
12689        log "creating a 10 Mb file"
12690        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12691        log "starting reads"
12692        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12693        log "truncating the file"
12694        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12695        log "killing dd"
12696        kill %+ || true # reads might have finished
12697        echo "wait until dd is finished"
12698        wait
12699        log "removing the temporary file"
12700        rm -rf $DIR/$tfile || error "tmp file removal failed"
12701 }
12702 run_test 218 "parallel read and truncate should not deadlock ======================="
12703
12704 test_219() {
12705         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12706         # write one partial page
12707         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12708         # set no grant so vvp_io_commit_write will do sync write
12709         $LCTL set_param fail_loc=0x411
12710         # write a full page at the end of file
12711         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12712
12713         $LCTL set_param fail_loc=0
12714         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12715         $LCTL set_param fail_loc=0x411
12716         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12717
12718         # LU-4201
12719         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12720         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12721 }
12722 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12723
12724 test_220() { #LU-325
12725         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12726         remote_ost_nodsh && skip "remote OST with nodsh" && return
12727         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12728         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12729         local OSTIDX=0
12730
12731         # create on MDT0000 so the last_id and next_id are correct
12732         mkdir $DIR/$tdir
12733         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12734         OST=${OST%_UUID}
12735
12736         # on the mdt's osc
12737         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12738         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12739                         osc.$mdtosc_proc1.prealloc_last_id)
12740         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12741                         osc.$mdtosc_proc1.prealloc_next_id)
12742
12743         $LFS df -i
12744
12745         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12746         #define OBD_FAIL_OST_ENOINO              0x229
12747         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12748         create_pool $FSNAME.$TESTNAME || return 1
12749         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12750
12751         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12752
12753         MDSOBJS=$((last_id - next_id))
12754         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12755
12756         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12757         echo "OST still has $count kbytes free"
12758
12759         echo "create $MDSOBJS files @next_id..."
12760         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12761
12762         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12763                         osc.$mdtosc_proc1.prealloc_last_id)
12764         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12765                         osc.$mdtosc_proc1.prealloc_next_id)
12766
12767         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12768         $LFS df -i
12769
12770         echo "cleanup..."
12771
12772         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12773         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12774
12775         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12776         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12777         echo "unlink $MDSOBJS files @$next_id..."
12778         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12779 }
12780 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12781
12782 test_221() {
12783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12784         dd if=`which date` of=$MOUNT/date oflag=sync
12785         chmod +x $MOUNT/date
12786
12787         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12788         $LCTL set_param fail_loc=0x80001401
12789
12790         $MOUNT/date > /dev/null
12791         rm -f $MOUNT/date
12792 }
12793 run_test 221 "make sure fault and truncate race to not cause OOM"
12794
12795 test_222a () {
12796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12797        rm -rf $DIR/$tdir
12798        test_mkdir -p $DIR/$tdir
12799        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12800        createmany -o $DIR/$tdir/$tfile 10
12801        cancel_lru_locks mdc
12802        cancel_lru_locks osc
12803        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12804        $LCTL set_param fail_loc=0x31a
12805        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12806        $LCTL set_param fail_loc=0
12807        rm -r $DIR/$tdir
12808 }
12809 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12810
12811 test_222b () {
12812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12813         rm -rf $DIR/$tdir
12814         test_mkdir -p $DIR/$tdir
12815         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12816         createmany -o $DIR/$tdir/$tfile 10
12817         cancel_lru_locks mdc
12818         cancel_lru_locks osc
12819         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12820         $LCTL set_param fail_loc=0x31a
12821         rm -r $DIR/$tdir || error "AGL for rmdir failed"
12822         $LCTL set_param fail_loc=0
12823 }
12824 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12825
12826 test_223 () {
12827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12828        rm -rf $DIR/$tdir
12829        test_mkdir -p $DIR/$tdir
12830        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12831        createmany -o $DIR/$tdir/$tfile 10
12832        cancel_lru_locks mdc
12833        cancel_lru_locks osc
12834        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12835        $LCTL set_param fail_loc=0x31b
12836        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12837        $LCTL set_param fail_loc=0
12838        rm -r $DIR/$tdir
12839 }
12840 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12841
12842 test_224a() { # LU-1039, MRP-303
12843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12844         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12845         $LCTL set_param fail_loc=0x508
12846         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12847         $LCTL set_param fail_loc=0
12848         df $DIR
12849 }
12850 run_test 224a "Don't panic on bulk IO failure"
12851
12852 test_224b() { # LU-1039, MRP-303
12853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12854         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12855         cancel_lru_locks osc
12856         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12857         $LCTL set_param fail_loc=0x515
12858         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12859         $LCTL set_param fail_loc=0
12860         df $DIR
12861 }
12862 run_test 224b "Don't panic on bulk IO failure"
12863
12864 test_224c() { # LU-6441
12865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12866         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12867
12868         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12869         save_writethrough $p
12870         set_cache writethrough on
12871
12872         local pages_per_rpc=$($LCTL get_param \
12873                                 osc.*.max_pages_per_rpc)
12874         local at_max=$($LCTL get_param -n at_max)
12875         local timeout=$($LCTL get_param -n timeout)
12876         local test_at="$LCTL get_param -n at_max"
12877         local param_at="$FSNAME.sys.at_max"
12878         local test_timeout="$LCTL get_param -n timeout"
12879         local param_timeout="$FSNAME.sys.timeout"
12880
12881         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12882
12883         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
12884                 error "conf_param at_max=0 failed"
12885         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
12886                 error "conf_param timeout=5 failed"
12887
12888         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
12889         do_facet ost1 $LCTL set_param fail_loc=0x520
12890         $LFS setstripe -c 1 -i 0 $DIR/$tfile
12891         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
12892         sync
12893         do_facet ost1 $LCTL set_param fail_loc=0
12894
12895         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
12896                 error "conf_param at_max=$at_max failed"
12897         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
12898                 $timeout || error "conf_param timeout=$timeout failed"
12899
12900         $LCTL set_param -n $pages_per_rpc
12901         restore_lustre_params < $p
12902         rm -f $p
12903 }
12904 run_test 224c "Don't hang if one of md lost during large bulk RPC"
12905
12906 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12907 test_225a () {
12908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12909         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12910         if [ -z ${MDSSURVEY} ]; then
12911               skip_env "mds-survey not found" && return
12912         fi
12913
12914         [ $MDSCOUNT -ge 2 ] &&
12915                 skip "skipping now for more than one MDT" && return
12916
12917        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12918             { skip "Need MDS version at least 2.2.51"; return; }
12919
12920        local mds=$(facet_host $SINGLEMDS)
12921        local target=$(do_nodes $mds 'lctl dl' | \
12922                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12923
12924        local cmd1="file_count=1000 thrhi=4"
12925        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12926        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12927        local cmd="$cmd1 $cmd2 $cmd3"
12928
12929        rm -f ${TMP}/mds_survey*
12930        echo + $cmd
12931        eval $cmd || error "mds-survey with zero-stripe failed"
12932        cat ${TMP}/mds_survey*
12933        rm -f ${TMP}/mds_survey*
12934 }
12935 run_test 225a "Metadata survey sanity with zero-stripe"
12936
12937 test_225b () {
12938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12939         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12940         if [ -z ${MDSSURVEY} ]; then
12941               skip_env "mds-survey not found" && return
12942         fi
12943         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12944             { skip "Need MDS version at least 2.2.51"; return; }
12945
12946         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12947               skip_env "Need to mount OST to test" && return
12948         fi
12949
12950         [ $MDSCOUNT -ge 2 ] &&
12951                 skip "skipping now for more than one MDT" && return
12952        local mds=$(facet_host $SINGLEMDS)
12953        local target=$(do_nodes $mds 'lctl dl' | \
12954                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12955
12956        local cmd1="file_count=1000 thrhi=4"
12957        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12958        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12959        local cmd="$cmd1 $cmd2 $cmd3"
12960
12961        rm -f ${TMP}/mds_survey*
12962        echo + $cmd
12963        eval $cmd || error "mds-survey with stripe_count failed"
12964        cat ${TMP}/mds_survey*
12965        rm -f ${TMP}/mds_survey*
12966 }
12967 run_test 225b "Metadata survey sanity with stripe_count = 1"
12968
12969 mcreate_path2fid () {
12970         local mode=$1
12971         local major=$2
12972         local minor=$3
12973         local name=$4
12974         local desc=$5
12975         local path=$DIR/$tdir/$name
12976         local fid
12977         local rc
12978         local fid_path
12979
12980         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12981                 error "cannot create $desc"
12982
12983         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12984         rc=$?
12985         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12986
12987         fid_path=$($LFS fid2path $MOUNT $fid)
12988         rc=$?
12989         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12990
12991         [ "$path" == "$fid_path" ] ||
12992                 error "fid2path returned $fid_path, expected $path"
12993
12994         echo "pass with $path and $fid"
12995 }
12996
12997 test_226a () {
12998         rm -rf $DIR/$tdir
12999         mkdir -p $DIR/$tdir
13000
13001         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13002         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13003         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13004         mcreate_path2fid 0040666 0 0 dir "directory"
13005         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13006         mcreate_path2fid 0100666 0 0 file "regular file"
13007         mcreate_path2fid 0120666 0 0 link "symbolic link"
13008         mcreate_path2fid 0140666 0 0 sock "socket"
13009 }
13010 run_test 226a "call path2fid and fid2path on files of all type"
13011
13012 test_226b () {
13013         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13014         rm -rf $DIR/$tdir
13015         local MDTIDX=1
13016
13017         mkdir -p $DIR/$tdir
13018         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13019                 error "create remote directory failed"
13020         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13021         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13022                                 "character special file (null)"
13023         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13024                                 "character special file (no device)"
13025         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13026         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13027                                 "block special file (loop)"
13028         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13029         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13030         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13031 }
13032 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13033
13034 # LU-1299 Executing or running ldd on a truncated executable does not
13035 # cause an out-of-memory condition.
13036 test_227() {
13037         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13038         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13039         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13040         chmod +x $MOUNT/date
13041
13042         $MOUNT/date > /dev/null
13043         ldd $MOUNT/date > /dev/null
13044         rm -f $MOUNT/date
13045 }
13046 run_test 227 "running truncated executable does not cause OOM"
13047
13048 # LU-1512 try to reuse idle OI blocks
13049 test_228a() {
13050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13051         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13052         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13053                 skip "non-ldiskfs backend" && return
13054
13055         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13056         local myDIR=$DIR/$tdir
13057
13058         mkdir -p $myDIR
13059         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13060         $LCTL set_param fail_loc=0x80001002
13061         createmany -o $myDIR/t- 10000
13062         $LCTL set_param fail_loc=0
13063         # The guard is current the largest FID holder
13064         touch $myDIR/guard
13065         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13066                     tr -d '[')
13067         local IDX=$(($SEQ % 64))
13068
13069         do_facet $SINGLEMDS sync
13070         # Make sure journal flushed.
13071         sleep 6
13072         local blk1=$(do_facet $SINGLEMDS \
13073                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13074                      grep Blockcount | awk '{print $4}')
13075
13076         # Remove old files, some OI blocks will become idle.
13077         unlinkmany $myDIR/t- 10000
13078         # Create new files, idle OI blocks should be reused.
13079         createmany -o $myDIR/t- 2000
13080         do_facet $SINGLEMDS sync
13081         # Make sure journal flushed.
13082         sleep 6
13083         local blk2=$(do_facet $SINGLEMDS \
13084                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13085                      grep Blockcount | awk '{print $4}')
13086
13087         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13088 }
13089 run_test 228a "try to reuse idle OI blocks"
13090
13091 test_228b() {
13092         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13093         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13094         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13095                 skip "non-ldiskfs backend" && return
13096
13097         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13098         local myDIR=$DIR/$tdir
13099
13100         mkdir -p $myDIR
13101         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13102         $LCTL set_param fail_loc=0x80001002
13103         createmany -o $myDIR/t- 10000
13104         $LCTL set_param fail_loc=0
13105         # The guard is current the largest FID holder
13106         touch $myDIR/guard
13107         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13108                     tr -d '[')
13109         local IDX=$(($SEQ % 64))
13110
13111         do_facet $SINGLEMDS sync
13112         # Make sure journal flushed.
13113         sleep 6
13114         local blk1=$(do_facet $SINGLEMDS \
13115                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13116                      grep Blockcount | awk '{print $4}')
13117
13118         # Remove old files, some OI blocks will become idle.
13119         unlinkmany $myDIR/t- 10000
13120
13121         # stop the MDT
13122         stop $SINGLEMDS || error "Fail to stop MDT."
13123         # remount the MDT
13124         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13125
13126         df $MOUNT || error "Fail to df."
13127         # Create new files, idle OI blocks should be reused.
13128         createmany -o $myDIR/t- 2000
13129         do_facet $SINGLEMDS sync
13130         # Make sure journal flushed.
13131         sleep 6
13132         local blk2=$(do_facet $SINGLEMDS \
13133                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13134                      grep Blockcount | awk '{print $4}')
13135
13136         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13137 }
13138 run_test 228b "idle OI blocks can be reused after MDT restart"
13139
13140 #LU-1881
13141 test_228c() {
13142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13143         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13144         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13145                 skip "non-ldiskfs backend" && return
13146
13147         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13148         local myDIR=$DIR/$tdir
13149
13150         mkdir -p $myDIR
13151         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13152         $LCTL set_param fail_loc=0x80001002
13153         # 20000 files can guarantee there are index nodes in the OI file
13154         createmany -o $myDIR/t- 20000
13155         $LCTL set_param fail_loc=0
13156         # The guard is current the largest FID holder
13157         touch $myDIR/guard
13158         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13159                     tr -d '[')
13160         local IDX=$(($SEQ % 64))
13161
13162         do_facet $SINGLEMDS sync
13163         # Make sure journal flushed.
13164         sleep 6
13165         local blk1=$(do_facet $SINGLEMDS \
13166                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13167                      grep Blockcount | awk '{print $4}')
13168
13169         # Remove old files, some OI blocks will become idle.
13170         unlinkmany $myDIR/t- 20000
13171         rm -f $myDIR/guard
13172         # The OI file should become empty now
13173
13174         # Create new files, idle OI blocks should be reused.
13175         createmany -o $myDIR/t- 2000
13176         do_facet $SINGLEMDS sync
13177         # Make sure journal flushed.
13178         sleep 6
13179         local blk2=$(do_facet $SINGLEMDS \
13180                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13181                      grep Blockcount | awk '{print $4}')
13182
13183         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13184 }
13185 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
13186
13187 test_229() { # LU-2482, LU-3448
13188         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
13189                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
13190                 return
13191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13192         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
13193
13194         rm -f $DIR/$tfile
13195
13196         # Create a file with a released layout and stripe count 2.
13197         $MULTIOP $DIR/$tfile H2c ||
13198                 error "failed to create file with released layout"
13199
13200         $GETSTRIPE -v $DIR/$tfile
13201
13202         local pattern=$($GETSTRIPE -L $DIR/$tfile)
13203         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
13204
13205         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
13206         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
13207         stat $DIR/$tfile || error "failed to stat released file"
13208
13209         chown $RUNAS_ID $DIR/$tfile ||
13210                 error "chown $RUNAS_ID $DIR/$tfile failed"
13211
13212         chgrp $RUNAS_ID $DIR/$tfile ||
13213                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
13214
13215         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
13216         rm $DIR/$tfile || error "failed to remove released file"
13217 }
13218 run_test 229 "getstripe/stat/rm/attr changes work on released files"
13219
13220 test_230a() {
13221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13222         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13223         local MDTIDX=1
13224
13225         test_mkdir $DIR/$tdir
13226         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
13227         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
13228         [ $mdt_idx -ne 0 ] &&
13229                 error "create local directory on wrong MDT $mdt_idx"
13230
13231         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
13232                         error "create remote directory failed"
13233         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
13234         [ $mdt_idx -ne $MDTIDX ] &&
13235                 error "create remote directory on wrong MDT $mdt_idx"
13236
13237         createmany -o $DIR/$tdir/test_230/t- 10 ||
13238                 error "create files on remote directory failed"
13239         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
13240         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
13241         rm -r $DIR/$tdir || error "unlink remote directory failed"
13242 }
13243 run_test 230a "Create remote directory and files under the remote directory"
13244
13245 test_230b() {
13246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13247         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13248         local MDTIDX=1
13249         local mdt_index
13250         local i
13251         local file
13252         local pid
13253         local stripe_count
13254         local migrate_dir=$DIR/$tdir/migrate_dir
13255         local other_dir=$DIR/$tdir/other_dir
13256
13257         test_mkdir $DIR/$tdir
13258         test_mkdir -i0 -c1 $migrate_dir
13259         test_mkdir -i0 -c1 $other_dir
13260         for ((i=0; i<10; i++)); do
13261                 mkdir -p $migrate_dir/dir_${i}
13262                 createmany -o $migrate_dir/dir_${i}/f 10 ||
13263                         error "create files under remote dir failed $i"
13264         done
13265
13266         cp /etc/passwd $migrate_dir/$tfile
13267         cp /etc/passwd $other_dir/$tfile
13268         chattr +SAD $migrate_dir
13269         chattr +SAD $migrate_dir/$tfile
13270
13271         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13272         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13273         local old_dir_mode=$(stat -c%f $migrate_dir)
13274         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
13275
13276         mkdir -p $migrate_dir/dir_default_stripe2
13277         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
13278         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
13279
13280         mkdir -p $other_dir
13281         ln $migrate_dir/$tfile $other_dir/luna
13282         ln $migrate_dir/$tfile $migrate_dir/sofia
13283         ln $other_dir/$tfile $migrate_dir/david
13284         ln -s $migrate_dir/$tfile $other_dir/zachary
13285         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
13286         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
13287
13288         $LFS migrate -m $MDTIDX $migrate_dir ||
13289                 error "fails on migrating remote dir to MDT1"
13290
13291         echo "migratate to MDT1, then checking.."
13292         for ((i = 0; i < 10; i++)); do
13293                 for file in $(find $migrate_dir/dir_${i}); do
13294                         mdt_index=$($LFS getstripe -M $file)
13295                         [ $mdt_index == $MDTIDX ] ||
13296                                 error "$file is not on MDT${MDTIDX}"
13297                 done
13298         done
13299
13300         # the multiple link file should still in MDT0
13301         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
13302         [ $mdt_index == 0 ] ||
13303                 error "$file is not on MDT${MDTIDX}"
13304
13305         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13306         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13307                 error " expect $old_dir_flag get $new_dir_flag"
13308
13309         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13310         [ "$old_file_flag" = "$new_file_flag" ] ||
13311                 error " expect $old_file_flag get $new_file_flag"
13312
13313         local new_dir_mode=$(stat -c%f $migrate_dir)
13314         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13315                 error "expect mode $old_dir_mode get $new_dir_mode"
13316
13317         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13318         [ "$old_file_mode" = "$new_file_mode" ] ||
13319                 error "expect mode $old_file_mode get $new_file_mode"
13320
13321         diff /etc/passwd $migrate_dir/$tfile ||
13322                 error "$tfile different after migration"
13323
13324         diff /etc/passwd $other_dir/luna ||
13325                 error "luna different after migration"
13326
13327         diff /etc/passwd $migrate_dir/sofia ||
13328                 error "sofia different after migration"
13329
13330         diff /etc/passwd $migrate_dir/david ||
13331                 error "david different after migration"
13332
13333         diff /etc/passwd $other_dir/zachary ||
13334                 error "zachary different after migration"
13335
13336         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13337                 error "${tfile}_ln different after migration"
13338
13339         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13340                 error "${tfile}_ln_other different after migration"
13341
13342         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
13343         [ $stripe_count = 2 ] ||
13344                 error "dir strpe_count $d != 2 after migration."
13345
13346         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
13347         [ $stripe_count = 2 ] ||
13348                 error "file strpe_count $d != 2 after migration."
13349
13350         #migrate back to MDT0
13351         MDTIDX=0
13352
13353         $LFS migrate -m $MDTIDX $migrate_dir ||
13354                 error "fails on migrating remote dir to MDT0"
13355
13356         echo "migrate back to MDT0, checking.."
13357         for file in $(find $migrate_dir); do
13358                 mdt_index=$($LFS getstripe -M $file)
13359                 [ $mdt_index == $MDTIDX ] ||
13360                         error "$file is not on MDT${MDTIDX}"
13361         done
13362
13363         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13364         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13365                 error " expect $old_dir_flag get $new_dir_flag"
13366
13367         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13368         [ "$old_file_flag" = "$new_file_flag" ] ||
13369                 error " expect $old_file_flag get $new_file_flag"
13370
13371         local new_dir_mode=$(stat -c%f $migrate_dir)
13372         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13373                 error "expect mode $old_dir_mode get $new_dir_mode"
13374
13375         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13376         [ "$old_file_mode" = "$new_file_mode" ] ||
13377                 error "expect mode $old_file_mode get $new_file_mode"
13378
13379         diff /etc/passwd ${migrate_dir}/$tfile ||
13380                 error "$tfile different after migration"
13381
13382         diff /etc/passwd ${other_dir}/luna ||
13383                 error "luna different after migration"
13384
13385         diff /etc/passwd ${migrate_dir}/sofia ||
13386                 error "sofia different after migration"
13387
13388         diff /etc/passwd ${other_dir}/zachary ||
13389                 error "zachary different after migration"
13390
13391         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13392                 error "${tfile}_ln different after migration"
13393
13394         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13395                 error "${tfile}_ln_other different after migration"
13396
13397         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
13398         [ $stripe_count = 2 ] ||
13399                 error "dir strpe_count $d != 2 after migration."
13400
13401         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
13402         [ $stripe_count = 2 ] ||
13403                 error "file strpe_count $d != 2 after migration."
13404
13405         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13406 }
13407 run_test 230b "migrate directory"
13408
13409 test_230c() {
13410         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13411         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13412         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13413         local MDTIDX=1
13414         local mdt_index
13415         local file
13416         local migrate_dir=$DIR/$tdir/migrate_dir
13417
13418         #If migrating directory fails in the middle, all entries of
13419         #the directory is still accessiable.
13420         test_mkdir $DIR/$tdir
13421         test_mkdir -i0 -c1 $migrate_dir
13422         stat $migrate_dir
13423         createmany -o $migrate_dir/f 10 ||
13424                 error "create files under ${migrate_dir} failed"
13425
13426         #failed after migrating 5 entries
13427         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
13428         do_facet mds1 lctl set_param fail_loc=0x20001801
13429         do_facet mds1 lctl  set_param fail_val=5
13430         local t=$(ls $migrate_dir | wc -l)
13431         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
13432                 error "migrate should fail after 5 entries"
13433
13434         mkdir $migrate_dir/dir &&
13435                 error "mkdir succeeds under migrating directory"
13436         touch $migrate_dir/file &&
13437                 error "touch file succeeds under migrating directory"
13438
13439         local u=$(ls $migrate_dir | wc -l)
13440         [ "$u" == "$t" ] || error "$u != $t during migration"
13441
13442         for file in $(find $migrate_dir); do
13443                 stat $file || error "stat $file failed"
13444         done
13445
13446         do_facet mds1 lctl set_param fail_loc=0
13447         do_facet mds1 lctl set_param fail_val=0
13448
13449         $LFS migrate -m $MDTIDX $migrate_dir ||
13450                 error "migrate open files should failed with open files"
13451
13452         echo "Finish migration, then checking.."
13453         for file in $(find $migrate_dir); do
13454                 mdt_index=$($LFS getstripe -M $file)
13455                 [ $mdt_index == $MDTIDX ] ||
13456                         error "$file is not on MDT${MDTIDX}"
13457         done
13458
13459         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13460 }
13461 run_test 230c "check directory accessiblity if migration is failed"
13462
13463 test_230d() {
13464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13465         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13466         local MDTIDX=1
13467         local mdt_index
13468         local migrate_dir=$DIR/$tdir/migrate_dir
13469         local i
13470         local j
13471
13472         test_mkdir $DIR/$tdir
13473         test_mkdir -i0 -c1 $migrate_dir
13474
13475         for ((i=0; i<100; i++)); do
13476                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
13477                 createmany -o $migrate_dir/dir_${i}/f 100 ||
13478                         error "create files under remote dir failed $i"
13479         done
13480
13481         $LFS migrate -m $MDTIDX $migrate_dir ||
13482                 error "migrate remote dir error"
13483
13484         echo "Finish migration, then checking.."
13485         for file in $(find $migrate_dir); do
13486                 mdt_index=$($LFS getstripe -M $file)
13487                 [ $mdt_index == $MDTIDX ] ||
13488                         error "$file is not on MDT${MDTIDX}"
13489         done
13490
13491         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13492 }
13493 run_test 230d "check migrate big directory"
13494
13495 test_230e() {
13496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13497         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13498         local i
13499         local j
13500         local a_fid
13501         local b_fid
13502
13503         mkdir -p $DIR/$tdir
13504         mkdir $DIR/$tdir/migrate_dir
13505         mkdir $DIR/$tdir/other_dir
13506         touch $DIR/$tdir/migrate_dir/a
13507         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
13508         ls $DIR/$tdir/other_dir
13509
13510         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13511                 error "migrate dir fails"
13512
13513         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13514         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13515
13516         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13517         [ $mdt_index == 0 ] || error "a is not on MDT0"
13518
13519         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
13520                 error "migrate dir fails"
13521
13522         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
13523         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
13524
13525         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13526         [ $mdt_index == 1 ] || error "a is not on MDT1"
13527
13528         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
13529         [ $mdt_index == 1 ] || error "b is not on MDT1"
13530
13531         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13532         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
13533
13534         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
13535
13536         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13537 }
13538 run_test 230e "migrate mulitple local link files"
13539
13540 test_230f() {
13541         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13542         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13543         local a_fid
13544         local ln_fid
13545
13546         mkdir -p $DIR/$tdir
13547         mkdir $DIR/$tdir/migrate_dir
13548         $LFS mkdir -i1 $DIR/$tdir/other_dir
13549         touch $DIR/$tdir/migrate_dir/a
13550         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13551         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13552         ls $DIR/$tdir/other_dir
13553
13554         # a should be migrated to MDT1, since no other links on MDT0
13555         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13556                 error "#1 migrate dir fails"
13557         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13558         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13559         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13560         [ $mdt_index == 1 ] || error "a is not on MDT1"
13561
13562         # a should stay on MDT1, because it is a mulitple link file
13563         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13564                 error "#2 migrate dir fails"
13565         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13566         [ $mdt_index == 1 ] || error "a is not on MDT1"
13567
13568         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13569                 error "#3 migrate dir fails"
13570
13571         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13572         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13573         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13574
13575         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13576         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13577
13578         # a should be migrated to MDT0, since no other links on MDT1
13579         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13580                 error "#4 migrate dir fails"
13581         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13582         [ $mdt_index == 0 ] || error "a is not on MDT0"
13583
13584         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13585 }
13586 run_test 230f "migrate mulitple remote link files"
13587
13588 test_230g() {
13589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13590         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13591
13592         mkdir -p $DIR/$tdir/migrate_dir
13593
13594         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
13595                 error "migrating dir to non-exist MDT succeeds"
13596         true
13597 }
13598 run_test 230g "migrate dir to non-exist MDT"
13599
13600 test_230h() {
13601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13602         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13603         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
13604                 skip "Need MDS version at least 2.7.64" && return
13605         local mdt_index
13606
13607         mkdir -p $DIR/$tdir/migrate_dir
13608
13609         $LFS migrate -m1 $DIR &&
13610                 error "migrating mountpoint1 should fail"
13611
13612         $LFS migrate -m1 $DIR/$tdir/.. &&
13613                 error "migrating mountpoint2 should fail"
13614
13615         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
13616                 error "migrating $tdir fail"
13617
13618         mdt_index=$($LFS getstripe -M $DIR/$tdir)
13619         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
13620
13621         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13622         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
13623
13624 }
13625 run_test 230h "migrate .. and root"
13626
13627 test_230i() {
13628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13629         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13630
13631         mkdir -p $DIR/$tdir/migrate_dir
13632
13633         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
13634                 error "migration fails with a tailing slash"
13635
13636         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
13637                 error "migration fails with two tailing slashes"
13638 }
13639 run_test 230i "lfs migrate -m tolerates trailing slashes"
13640
13641 test_231a()
13642 {
13643         # For simplicity this test assumes that max_pages_per_rpc
13644         # is the same across all OSCs
13645         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
13646         local bulk_size=$((max_pages * 4096))
13647
13648         mkdir -p $DIR/$tdir
13649
13650         # clear the OSC stats
13651         $LCTL set_param osc.*.stats=0 &>/dev/null
13652         stop_writeback
13653
13654         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
13655         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
13656                 oflag=direct &>/dev/null || error "dd failed"
13657
13658         sync; sleep 1; sync # just to be safe
13659         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
13660         if [ x$nrpcs != "x1" ]; then
13661                 $LCTL get_param osc.*.stats
13662                 error "found $nrpcs ost_write RPCs, not 1 as expected"
13663         fi
13664
13665         start_writeback
13666         # Drop the OSC cache, otherwise we will read from it
13667         cancel_lru_locks osc
13668
13669         # clear the OSC stats
13670         $LCTL set_param osc.*.stats=0 &>/dev/null
13671
13672         # Client reads $bulk_size.
13673         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
13674                 iflag=direct &>/dev/null || error "dd failed"
13675
13676         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
13677         if [ x$nrpcs != "x1" ]; then
13678                 $LCTL get_param osc.*.stats
13679                 error "found $nrpcs ost_read RPCs, not 1 as expected"
13680         fi
13681 }
13682 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
13683
13684 test_231b() {
13685         mkdir -p $DIR/$tdir
13686         local i
13687         for i in {0..1023}; do
13688                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
13689                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
13690                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
13691         done
13692         sync
13693 }
13694 run_test 231b "must not assert on fully utilized OST request buffer"
13695
13696 test_232() {
13697         mkdir -p $DIR/$tdir
13698         #define OBD_FAIL_LDLM_OST_LVB            0x31c
13699         $LCTL set_param fail_loc=0x31c
13700
13701         # ignore dd failure
13702         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
13703
13704         $LCTL set_param fail_loc=0
13705         umount_client $MOUNT || error "umount failed"
13706         mount_client $MOUNT || error "mount failed"
13707 }
13708 run_test 232 "failed lock should not block umount"
13709
13710 test_233a() {
13711         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13712         { skip "Need MDS version at least 2.3.64"; return; }
13713         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
13714
13715         local fid=$($LFS path2fid $MOUNT)
13716         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13717                 error "cannot access $MOUNT using its FID '$fid'"
13718 }
13719 run_test 233a "checking that OBF of the FS root succeeds"
13720
13721 test_233b() {
13722         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13723         { skip "Need MDS version at least 2.5.90"; return; }
13724         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
13725
13726         local fid=$($LFS path2fid $MOUNT/.lustre)
13727         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13728                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13729
13730         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13731         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13732                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13733 }
13734 run_test 233b "checking that OBF of the FS .lustre succeeds"
13735
13736 test_234() {
13737         local p="$TMP/sanityN-$TESTNAME.parameters"
13738         save_lustre_params client "llite.*.xattr_cache" > $p
13739         lctl set_param llite.*.xattr_cache 1 ||
13740                 { skip "xattr cache is not supported"; return 0; }
13741
13742         mkdir -p $DIR/$tdir || error "mkdir failed"
13743         touch $DIR/$tdir/$tfile || error "touch failed"
13744         # OBD_FAIL_LLITE_XATTR_ENOMEM
13745         $LCTL set_param fail_loc=0x1405
13746         # output of the form: attr 2 4 44 3 fc13 x86_64
13747         V=($(IFS=".-" rpm -q attr))
13748         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13749               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13750                 # attr pre-2.4.44-7 had a bug with rc
13751                 # LU-3703 - SLES 11 and FC13 clients have older attr
13752                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13753                         error "getfattr should have failed with ENOMEM"
13754         else
13755                 skip "LU-3703: attr version $(getfattr --version) too old"
13756         fi
13757         $LCTL set_param fail_loc=0x0
13758         rm -rf $DIR/$tdir
13759
13760         restore_lustre_params < $p
13761         rm -f $p
13762 }
13763 run_test 234 "xattr cache should not crash on ENOMEM"
13764
13765 test_235() {
13766         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13767                 skip "Need MDS version at least 2.4.52" && return
13768         flock_deadlock $DIR/$tfile
13769         local RC=$?
13770         case $RC in
13771                 0)
13772                 ;;
13773                 124) error "process hangs on a deadlock"
13774                 ;;
13775                 *) error "error executing flock_deadlock $DIR/$tfile"
13776                 ;;
13777         esac
13778 }
13779 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13780
13781 #LU-2935
13782 test_236() {
13783         check_swap_layouts_support && return 0
13784         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
13785
13786         local ref1=/etc/passwd
13787         local ref2=/etc/group
13788         local file1=$DIR/$tdir/f1
13789         local file2=$DIR/$tdir/f2
13790
13791         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13792         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13793         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13794         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13795         local fd=$(free_fd)
13796         local cmd="exec $fd<>$file2"
13797         eval $cmd
13798         rm $file2
13799         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13800                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13801         cmd="exec $fd>&-"
13802         eval $cmd
13803         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13804
13805         #cleanup
13806         rm -rf $DIR/$tdir
13807 }
13808 run_test 236 "Layout swap on open unlinked file"
13809
13810 # test to verify file handle related system calls
13811 # (name_to_handle_at/open_by_handle_at)
13812 # The new system calls are supported in glibc >= 2.14.
13813
13814 test_237() {
13815         echo "Test file_handle syscalls" > $DIR/$tfile ||
13816                 error "write failed"
13817         check_fhandle_syscalls $DIR/$tfile ||
13818                 error "check_fhandle_syscalls failed"
13819 }
13820 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13821
13822 # LU-4659 linkea consistency
13823 test_238() {
13824         local server_version=$(lustre_version_code $SINGLEMDS)
13825
13826         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13827                 [[ $server_version -gt $(version_code 2.5.1) &&
13828                    $server_version -lt $(version_code 2.5.50) ]] ||
13829                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13830
13831         touch $DIR/$tfile
13832         ln $DIR/$tfile $DIR/$tfile.lnk
13833         touch $DIR/$tfile.new
13834         mv $DIR/$tfile.new $DIR/$tfile
13835         local fid1=$($LFS path2fid $DIR/$tfile)
13836         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13837         local path1=$($LFS fid2path $FSNAME "$fid1")
13838         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13839         local path2=$($LFS fid2path $FSNAME "$fid2")
13840         [ $tfile.lnk == $path2 ] ||
13841                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13842         rm -f $DIR/$tfile*
13843 }
13844 run_test 238 "Verify linkea consistency"
13845
13846 test_239() {
13847         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13848                 skip "Need MDS version at least 2.5.60" && return
13849         local list=$(comma_list $(mdts_nodes))
13850
13851         mkdir -p $DIR/$tdir
13852         createmany -o $DIR/$tdir/f- 5000
13853         unlinkmany $DIR/$tdir/f- 5000
13854         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13855         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13856                         osc.*MDT*.sync_in_flight" | calc_sum)
13857         [ "$changes" -eq 0 ] || error "$changes not synced"
13858 }
13859 run_test 239 "osp_sync test"
13860
13861 test_239a() { #LU-5297
13862         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13863         touch $DIR/$tfile
13864         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13865         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13866         chgrp $RUNAS_GID $DIR/$tfile
13867         wait_delete_completed
13868 }
13869 run_test 239a "process invalid osp sync record correctly"
13870
13871 test_239b() { #LU-5297
13872         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13873         touch $DIR/$tfile1
13874         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13875         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13876         chgrp $RUNAS_GID $DIR/$tfile1
13877         wait_delete_completed
13878         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13879         touch $DIR/$tfile2
13880         chgrp $RUNAS_GID $DIR/$tfile2
13881         wait_delete_completed
13882 }
13883 run_test 239b "process osp sync record with ENOMEM error correctly"
13884
13885 test_240() {
13886         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13887         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13888
13889         mkdir -p $DIR/$tdir
13890
13891         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
13892                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
13893         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
13894                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
13895
13896         umount_client $MOUNT || error "umount failed"
13897         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
13898         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
13899         mount_client $MOUNT || error "failed to mount client"
13900
13901         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
13902         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
13903 }
13904 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
13905
13906 test_241_bio() {
13907         for LOOP in $(seq $1); do
13908                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
13909                 cancel_lru_locks osc || true
13910         done
13911 }
13912
13913 test_241_dio() {
13914         for LOOP in $(seq $1); do
13915                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
13916                                                 iflag=direct 2>/dev/null
13917         done
13918 }
13919
13920 test_241a() { # was test_241
13921         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13922         ls -la $DIR/$tfile
13923         cancel_lru_locks osc
13924         test_241_bio 1000 &
13925         PID=$!
13926         test_241_dio 1000
13927         wait $PID
13928 }
13929 run_test 241a "bio vs dio"
13930
13931 test_241b() {
13932         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13933         ls -la $DIR/$tfile
13934         test_241_dio 1000 &
13935         PID=$!
13936         test_241_dio 1000
13937         wait $PID
13938 }
13939 run_test 241b "dio vs dio"
13940
13941 test_242() {
13942         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13943         mkdir -p $DIR/$tdir
13944         touch $DIR/$tdir/$tfile
13945
13946         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
13947         do_facet mds1 lctl set_param fail_loc=0x105
13948         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
13949
13950         do_facet mds1 lctl set_param fail_loc=0
13951         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
13952 }
13953 run_test 242 "mdt_readpage failure should not cause directory unreadable"
13954
13955 test_243()
13956 {
13957         test_mkdir -p $DIR/$tdir
13958         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
13959 }
13960 run_test 243 "various group lock tests"
13961
13962 test_244()
13963 {
13964         test_mkdir -p $DIR/$tdir
13965         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
13966         sendfile_grouplock $DIR/$tdir/$tfile || \
13967                 error "sendfile+grouplock failed"
13968         rm -rf $DIR/$tdir
13969 }
13970 run_test 244 "sendfile with group lock tests"
13971
13972 test_245() {
13973         local flagname="multi_mod_rpcs"
13974         local connect_data_name="max_mod_rpcs"
13975         local out
13976
13977         # check if multiple modify RPCs flag is set
13978         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
13979                 grep "connect_flags:")
13980         echo "$out"
13981
13982         echo "$out" | grep -qw $flagname
13983         if [ $? -ne 0 ]; then
13984                 echo "connect flag $flagname is not set"
13985                 return
13986         fi
13987
13988         # check if multiple modify RPCs data is set
13989         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
13990         echo "$out"
13991
13992         echo "$out" | grep -qw $connect_data_name ||
13993                 error "import should have connect data $connect_data_name"
13994 }
13995 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
13996
13997 test_246() { # LU-7371
13998         remote_ost_nodsh && skip "remote OST with nodsh" && return
13999         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14000                 skip "Need OST version >= 2.7.62" && return 0
14001         do_facet ost1 $LCTL set_param fail_val=4095
14002 #define OBD_FAIL_OST_READ_SIZE          0x234
14003         do_facet ost1 $LCTL set_param fail_loc=0x234
14004         $LFS setstripe $DIR/$tfile -i 0 -c 1
14005         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14006         cancel_lru_locks $FSNAME-OST0000
14007         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14008 }
14009 run_test 246 "Read file of size 4095 should return right length"
14010
14011 test_247a() {
14012         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14013                 grep -q subtree ||
14014                 { skip "Fileset feature is not supported"; return; }
14015
14016         local submount=${MOUNT}_$tdir
14017
14018         mkdir $MOUNT/$tdir
14019         mkdir -p $submount || error "mkdir $submount failed"
14020         FILESET="$FILESET/$tdir" mount_client $submount ||
14021                 error "mount $submount failed"
14022         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14023         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14024                 error "read $MOUNT/$tdir/$tfile failed"
14025         umount_client $submount || error "umount $submount failed"
14026         rmdir $submount
14027 }
14028 run_test 247a "mount subdir as fileset"
14029
14030 test_247b() {
14031         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14032                 { skip "Fileset feature is not supported"; return; }
14033
14034         local submount=${MOUNT}_$tdir
14035
14036         rm -rf $MOUNT/$tdir
14037         mkdir -p $submount || error "mkdir $submount failed"
14038         SKIP_FILESET=1
14039         FILESET="$FILESET/$tdir" mount_client $submount &&
14040                 error "mount $submount should fail"
14041         rmdir $submount
14042 }
14043 run_test 247b "mount subdir that dose not exist"
14044
14045 test_247c() {
14046         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14047                 { skip "Fileset feature is not supported"; return; }
14048
14049         local submount=${MOUNT}_$tdir
14050
14051         mkdir -p $MOUNT/$tdir/dir1
14052         mkdir -p $submount || error "mkdir $submount failed"
14053         FILESET="$FILESET/$tdir" mount_client $submount ||
14054                 error "mount $submount failed"
14055         local fid=$($LFS path2fid $MOUNT/)
14056         $LFS fid2path $submount $fid && error "fid2path should fail"
14057         umount_client $submount || error "umount $submount failed"
14058         rmdir $submount
14059 }
14060 run_test 247c "running fid2path outside root"
14061
14062 test_247d() {
14063         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14064                 { skip "Fileset feature is not supported"; return; }
14065
14066         local submount=${MOUNT}_$tdir
14067
14068         mkdir -p $MOUNT/$tdir/dir1
14069         mkdir -p $submount || error "mkdir $submount failed"
14070         FILESET="$FILESET/$tdir" mount_client $submount ||
14071                 error "mount $submount failed"
14072         local fid=$($LFS path2fid $submount/dir1)
14073         $LFS fid2path $submount $fid || error "fid2path should succeed"
14074         umount_client $submount || error "umount $submount failed"
14075         rmdir $submount
14076 }
14077 run_test 247d "running fid2path inside root"
14078
14079 # LU-8037
14080 test_247e() {
14081         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14082                 grep -q subtree ||
14083                 { skip "Fileset feature is not supported"; return; }
14084
14085         local submount=${MOUNT}_$tdir
14086
14087         mkdir $MOUNT/$tdir
14088         mkdir -p $submount || error "mkdir $submount failed"
14089         FILESET="$FILESET/.." mount_client $submount &&
14090                 error "mount $submount should fail"
14091         rmdir $submount
14092 }
14093 run_test 247e "mount .. as fileset"
14094
14095 test_248() {
14096         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14097         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14098
14099         # create a large file for fast read verification
14100         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
14101
14102         # make sure the file is created correctly
14103         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
14104                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
14105
14106         echo "Test 1: verify that fast read is 4 times faster on cache read"
14107
14108         # small read with fast read enabled
14109         $LCTL set_param -n llite.*.fast_read=1
14110         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14111                 awk '/copied/ { print $6 }')
14112
14113         # small read with fast read disabled
14114         $LCTL set_param -n llite.*.fast_read=0
14115         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14116                 awk '/copied/ { print $6 }')
14117
14118         # verify that fast read is 4 times faster for cache read
14119         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
14120                 error_not_in_vm "fast read was not 4 times faster: " \
14121                            "$t_fast vs $t_slow"
14122
14123         echo "Test 2: verify the performance between big and small read"
14124         $LCTL set_param -n llite.*.fast_read=1
14125
14126         # 1k non-cache read
14127         cancel_lru_locks osc
14128         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14129                 awk '/copied/ { print $6 }')
14130
14131         # 1M non-cache read
14132         cancel_lru_locks osc
14133         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14134                 awk '/copied/ { print $6 }')
14135
14136         # verify that big IO is not 4 times faster than small IO
14137         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
14138                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
14139
14140         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
14141         rm -f $DIR/$tfile
14142 }
14143 run_test 248 "fast read verification"
14144
14145 test_249() { # LU-7890
14146         rm -f $DIR/$tfile
14147         $SETSTRIPE -c 1 $DIR/$tfile
14148
14149         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
14150         skip "Need at least version 2.8.54"
14151
14152         # Offset 2T == 4k * 512M
14153         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
14154                 error "dd to 2T offset failed"
14155 }
14156 run_test 249 "Write above 2T file size"
14157
14158 test_250() {
14159         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
14160          && skip "no 16TB file size limit on ZFS" && return
14161
14162         $SETSTRIPE -c 1 $DIR/$tfile
14163         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
14164         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
14165         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
14166         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
14167                 conv=notrunc,fsync && error "append succeeded"
14168         return 0
14169 }
14170 run_test 250 "Write above 16T limit"
14171
14172 test_251() {
14173         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
14174
14175         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
14176         #Skip once - writing the first stripe will succeed
14177         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14178         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
14179                 error "short write happened"
14180
14181         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14182         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
14183                 error "short read happened"
14184
14185         rm -f $DIR/$tfile
14186 }
14187 run_test 251 "Handling short read and write correctly"
14188
14189 test_252() {
14190         local tgt
14191         local dev
14192         local out
14193         local uuid
14194         local num
14195         local gen
14196
14197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14198         remote_ost_nodsh && skip "remote OST with nodsh" && return
14199         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
14200              "$(facet_fstype mds1)" != "ldiskfs" ]; then
14201                 skip "can only run lr_reader on ldiskfs target"
14202                 return
14203         fi
14204
14205         # check lr_reader on OST0000
14206         tgt=ost1
14207         dev=$(facet_device $tgt)
14208         out=$(do_facet $tgt $LR_READER $dev)
14209         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14210         echo "$out"
14211         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
14212         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
14213                 error "Invalid uuid returned by $LR_READER on target $tgt"
14214         echo -e "uuid returned by $LR_READER is '$uuid'\n"
14215
14216         # check lr_reader -c on MDT0000
14217         tgt=mds1
14218         dev=$(facet_device $tgt)
14219         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
14220                 echo "$LR_READER does not support additional options"
14221                 return 0
14222         fi
14223         out=$(do_facet $tgt $LR_READER -c $dev)
14224         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14225         echo "$out"
14226         num=$(echo "$out" | grep -c "mdtlov")
14227         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
14228                 error "Invalid number of mdtlov clients returned by $LR_READER"
14229         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
14230
14231         # check lr_reader -cr on MDT0000
14232         out=$(do_facet $tgt $LR_READER -cr $dev)
14233         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14234         echo "$out"
14235         echo "$out" | grep -q "^reply_data:$" ||
14236                 error "$LR_READER should have returned 'reply_data' section"
14237         num=$(echo "$out" | grep -c "client_generation")
14238         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
14239 }
14240 run_test 252 "check lr_reader tool"
14241
14242 test_253_fill_ost() {
14243         local size_mb #how many MB should we write to pass watermark
14244         local lwm=$3  #low watermark
14245         local free_10mb #10% of free space
14246
14247         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14248         size_mb=$((free_kb / 1024 - lwm))
14249         free_10mb=$((free_kb / 10240))
14250         #If 10% of free space cross low watermark use it
14251         if (( free_10mb > size_mb )); then
14252                 size_mb=$free_10mb
14253         else
14254                 #At least we need to store 1.1 of difference between
14255                 #free space and low watermark
14256                 size_mb=$((size_mb + size_mb / 10))
14257         fi
14258         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
14259                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
14260                          oflag=append conv=notrunc
14261         fi
14262
14263         sleep_maxage
14264
14265         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14266         echo "OST still has $((free_kb / 1024)) mbytes free"
14267 }
14268
14269 test_253() {
14270         local ostidx=0
14271         local rc=0
14272
14273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14274         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14275         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
14276
14277         local ost_name=$($LFS osts |
14278                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
14279         # on the mdt's osc
14280         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
14281         do_facet $SINGLEMDS $LCTL get_param -n \
14282                 osp.$mdtosc_proc1.reserved_mb_high ||
14283                 { skip  "remote MDS does not support reserved_mb_high" &&
14284                   return; }
14285
14286         rm -rf $DIR/$tdir
14287         wait_mds_ost_sync
14288         wait_delete_completed
14289         mkdir $DIR/$tdir
14290
14291         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
14292                         osp.$mdtosc_proc1.reserved_mb_high)
14293         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
14294                         osp.$mdtosc_proc1.reserved_mb_low)
14295         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
14296
14297         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
14298         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
14299                 error "Adding $ost_name to pool failed"
14300
14301         # Wait for client to see a OST at pool
14302         wait_update $HOSTNAME "$LCTL get_param -n
14303                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
14304                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
14305                 error "Client can not see the pool"
14306         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
14307                 error "Setstripe failed"
14308
14309         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
14310         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
14311         echo "OST still has $((blocks/1024)) mbytes free"
14312
14313         local new_lwm=$((blocks/1024-10))
14314         do_facet $SINGLEMDS $LCTL set_param \
14315                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
14316         do_facet $SINGLEMDS $LCTL set_param \
14317                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
14318
14319         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14320
14321         #First enospc could execute orphan deletion so repeat.
14322         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14323
14324         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14325                         osp.$mdtosc_proc1.prealloc_status)
14326         echo "prealloc_status $oa_status"
14327
14328         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
14329                 error "File creation should fail"
14330         #object allocation was stopped, but we still able to append files
14331         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
14332                 error "Append failed"
14333         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
14334
14335         wait_delete_completed
14336
14337         sleep_maxage
14338
14339         for i in $(seq 10 12); do
14340                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
14341                         error "File creation failed after rm";
14342         done
14343
14344         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14345                         osp.$mdtosc_proc1.prealloc_status)
14346         echo "prealloc_status $oa_status"
14347
14348         if (( oa_status != 0 )); then
14349                 error "Object allocation still disable after rm"
14350         fi
14351         do_facet $SINGLEMDS $LCTL set_param \
14352                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
14353         do_facet $SINGLEMDS $LCTL set_param \
14354                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
14355
14356
14357         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
14358                 error "Remove $ost_name from pool failed"
14359         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14360                 error "Pool destroy fialed"
14361 }
14362 run_test 253 "Check object allocation limit"
14363
14364 test_254() {
14365         local cl_user
14366
14367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14368         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14369         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
14370                 { skip "MDS does not support changelog_size" && return; }
14371
14372         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14373         echo "Registered as changelog user $cl_user"
14374
14375         $LFS changelog_clear $MDT0 $cl_user 0
14376
14377         local size1=$(do_facet mds1 \
14378                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14379         echo "Changelog size $size1"
14380
14381         rm -rf $DIR/$tdir
14382         $LFS mkdir -i 0 $DIR/$tdir
14383         # change something
14384         mkdir -p $DIR/$tdir/pics/2008/zachy
14385         touch $DIR/$tdir/pics/2008/zachy/timestamp
14386         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
14387         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14388         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14389         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14390         rm $DIR/$tdir/pics/desktop.jpg
14391
14392         local size2=$(do_facet mds1 \
14393                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14394         echo "Changelog size after work $size2"
14395
14396         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14397
14398         if (( size2 <= size1 )); then
14399                 error "Changelog size after work should be greater than original"
14400         fi
14401         return 0
14402 }
14403 run_test 254 "Check changelog size"
14404
14405 ladvise_no_type()
14406 {
14407         local type=$1
14408         local file=$2
14409
14410         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
14411                 awk -F: '{print $2}' | grep $type > /dev/null
14412         if [ $? -ne 0 ]; then
14413                 return 0
14414         fi
14415         return 1
14416 }
14417
14418 ladvise_no_ioctl()
14419 {
14420         local file=$1
14421
14422         lfs ladvise -a willread $file > /dev/null 2>&1
14423         if [ $? -eq 0 ]; then
14424                 return 1
14425         fi
14426
14427         lfs ladvise -a willread $file 2>&1 |
14428                 grep "Inappropriate ioctl for device" > /dev/null
14429         if [ $? -eq 0 ]; then
14430                 return 0
14431         fi
14432         return 1
14433 }
14434
14435 ladvise_willread_performance()
14436 {
14437         local repeat=10
14438         local average_cache=0
14439         local average_ladvise=0
14440
14441         for ((i = 1; i <= $repeat; i++)); do
14442                 echo "Iter $i/$repeat: reading without willread hint"
14443                 cancel_lru_locks osc
14444                 do_nodes $(comma_list $(osts_nodes)) \
14445                         "echo 3 > /proc/sys/vm/drop_caches"
14446                 local speed_origin=$($READS -f $DIR/$tfile -s $size \
14447                         -b 4096 -n $((size / 4096)) -t 60 |
14448                         sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##')
14449
14450                 echo "Iter $i/$repeat: Reading again without willread hint"
14451                 cancel_lru_locks osc
14452                 local speed_cache=$($READS -f $DIR/$tfile -s $size \
14453                         -b 4096 -n $((size / 4096)) -t 60 |
14454                         sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##')
14455
14456                 echo "Iter $i/$repeat: reading with willread hint"
14457                 cancel_lru_locks osc
14458                 do_nodes $(comma_list $(osts_nodes)) \
14459                         "echo 3 > /proc/sys/vm/drop_caches"
14460                 lfs ladvise -a willread $DIR/$tfile ||
14461                         error "Ladvise failed"
14462                 local speed_ladvise=$($READS -f $DIR/$tfile -s $size \
14463                         -b 4096 -n $((size / 4096)) -t 60 |
14464                         sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##')
14465
14466                 local cache_speedup=$(echo "scale=2; \
14467                         ($speed_cache-$speed_origin)/$speed_origin*100" | bc)
14468                 cache_speedup=$(echo ${cache_speedup%.*})
14469                 echo "Iter $i/$repeat: cache speedup: $cache_speedup%"
14470                 average_cache=$((average_cache + cache_speedup))
14471
14472                 local ladvise_speedup=$(echo "scale=2; \
14473                         ($speed_ladvise-$speed_origin)/$speed_origin*100" | bc)
14474                 ladvise_speedup=$(echo ${ladvise_speedup%.*})
14475                 echo "Iter $i/$repeat: ladvise speedup: $ladvise_speedup%"
14476                 average_ladvise=$((average_ladvise + ladvise_speedup))
14477         done
14478         average_cache=$((average_cache / repeat))
14479         average_ladvise=$((average_ladvise / repeat))
14480
14481         if [ $average_cache -lt 20 ]; then
14482                 echo "Speedup with cache is less than 20% ($average_cache%),"\
14483                         "skipping check of speedup with willread:"\
14484                         "$average_ladvise%"
14485                 return 0
14486         fi
14487
14488         local lowest_speedup=$((average_cache / 2))
14489         [ $average_ladvise -gt $lowest_speedup ] ||
14490                 error_not_in_vm "Speedup with willread is less than " \
14491                            "$lowest_speedup%, got $average_ladvise%"
14492         echo "Speedup with willread ladvise: $average_ladvise%"
14493         echo "Speedup with cache: $average_cache%"
14494 }
14495
14496 test_255a() {
14497         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14498                 skip "lustre < 2.8.54 does not support ladvise " && return
14499
14500         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
14501
14502         ladvise_no_type willread $DIR/$tfile &&
14503                 skip "willread ladvise is not supported" && return
14504
14505         ladvise_no_ioctl $DIR/$tfile &&
14506                 skip "ladvise ioctl is not supported" && return
14507
14508         local size_mb=100
14509         local size=$((size_mb * 1048576))
14510         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14511                 error "dd to $DIR/$tfile failed"
14512
14513         lfs ladvise -a willread $DIR/$tfile ||
14514                 error "Ladvise failed with no range argument"
14515
14516         lfs ladvise -a willread -s 0 $DIR/$tfile ||
14517                 error "Ladvise failed with no -l or -e argument"
14518
14519         lfs ladvise -a willread -e 1 $DIR/$tfile ||
14520                 error "Ladvise failed with only -e argument"
14521
14522         lfs ladvise -a willread -l 1 $DIR/$tfile ||
14523                 error "Ladvise failed with only -l argument"
14524
14525         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
14526                 error "End offset should not be smaller than start offset"
14527
14528         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
14529                 error "End offset should not be equal to start offset"
14530
14531         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
14532                 error "Ladvise failed with overflowing -s argument"
14533
14534         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
14535                 error "Ladvise failed with overflowing -e argument"
14536
14537         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
14538                 error "Ladvise failed with overflowing -l argument"
14539
14540         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
14541                 error "Ladvise succeeded with conflicting -l and -e arguments"
14542
14543         echo "Synchronous ladvise should wait"
14544         local delay=4
14545 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
14546         do_nodes $(comma_list $(osts_nodes)) \
14547                 $LCTL set_param fail_val=$delay fail_loc=0x237
14548
14549         local start_ts=$SECONDS
14550         lfs ladvise -a willread $DIR/$tfile ||
14551                 error "Ladvise failed with no range argument"
14552         local end_ts=$SECONDS
14553         local inteval_ts=$((end_ts - start_ts))
14554
14555         if [ $inteval_ts -lt $(($delay - 1)) ]; then
14556                 error "Synchronous advice didn't wait reply"
14557         fi
14558
14559         echo "Asynchronous ladvise shouldn't wait"
14560         local start_ts=$SECONDS
14561         lfs ladvise -a willread -b $DIR/$tfile ||
14562                 error "Ladvise failed with no range argument"
14563         local end_ts=$SECONDS
14564         local inteval_ts=$((end_ts - start_ts))
14565
14566         if [ $inteval_ts -gt $(($delay / 2)) ]; then
14567                 error "Asynchronous advice blocked"
14568         fi
14569
14570         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
14571         ladvise_willread_performance
14572 }
14573 run_test 255a "check 'lfs ladvise -a willread'"
14574
14575 facet_meminfo() {
14576         local facet=$1
14577         local info=$2
14578
14579         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
14580 }
14581
14582 test_255b() {
14583         ladvise_no_type dontneed $DIR/$tfile &&
14584                 skip "dontneed ladvise is not supported" && return
14585
14586         ladvise_no_ioctl $DIR/$tfile &&
14587                 skip "ladvise ioctl is not supported" && return
14588
14589         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14590                 skip "lustre < 2.8.54 does not support ladvise" && return
14591
14592         [ "$(facet_fstype ost1)" = "zfs" ] &&
14593                 skip "zfs-osd does not support dontneed advice" && return
14594
14595         lfs setstripe -c 1 -i 0 $DIR/$tfile
14596
14597         local size_mb=100
14598         local size=$((size_mb * 1048576))
14599         # In order to prevent disturbance of other processes, only check 3/4
14600         # of the memory usage
14601         local kibibytes=$((size_mb * 1024 * 3 / 4))
14602
14603         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14604                 error "dd to $DIR/$tfile failed"
14605
14606         local total=$(facet_meminfo ost1 MemTotal)
14607         echo "Total memory: $total KiB"
14608
14609         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
14610         local before_read=$(facet_meminfo ost1 Cached)
14611         echo "Cache used before read: $before_read KiB"
14612
14613         lfs ladvise -a willread $DIR/$tfile ||
14614                 error "Ladvise willread failed"
14615         local after_read=$(facet_meminfo ost1 Cached)
14616         echo "Cache used after read: $after_read KiB"
14617
14618         lfs ladvise -a dontneed $DIR/$tfile ||
14619                 error "Ladvise dontneed again failed"
14620         local no_read=$(facet_meminfo ost1 Cached)
14621         echo "Cache used after dontneed ladvise: $no_read KiB"
14622
14623         if [ $total -lt $((before_read + kibibytes)) ]; then
14624                 echo "Memory is too small, abort checking"
14625                 return 0
14626         fi
14627
14628         if [ $((before_read + kibibytes)) -gt $after_read ]; then
14629                 error "Ladvise willread should use more memory" \
14630                         "than $kibibytes KiB"
14631         fi
14632
14633         if [ $((no_read + kibibytes)) -gt $after_read ]; then
14634                 error "Ladvise dontneed should release more memory" \
14635                         "than $kibibytes KiB"
14636         fi
14637 }
14638 run_test 255b "check 'lfs ladvise -a dontneed'"
14639
14640 test_256() {
14641         local cl_user
14642         local cat_sl
14643         local mdt_dev
14644
14645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14646         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14647         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
14648                 skip "non-ldiskfs backend" && return
14649
14650         mdt_dev=$(mdsdevname 1)
14651         echo $mdt_dev
14652         cl_user=$(do_facet mds1 \
14653         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
14654         if [[ -n $cl_user ]]; then
14655                 skip "active changelog user"
14656                 return
14657         fi
14658
14659         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14660         echo "Registered as changelog user $cl_user"
14661
14662         rm -rf $DIR/$tdir
14663         mkdir -p $DIR/$tdir
14664
14665         $LFS changelog_clear $MDT0 $cl_user 0
14666
14667         # change something
14668         touch $DIR/$tdir/{1..10}
14669
14670         # stop the MDT
14671         stop mds1 || error "Fail to stop MDT."
14672
14673         # remount the MDT
14674         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
14675
14676         #after mount new plainllog is used
14677         touch $DIR/$tdir/{11..19}
14678         cat_sl=$(do_facet mds1 \
14679         "$DEBUGFS -R \\\"dump changelog_catalog cat.dmp\\\" $mdt_dev; \
14680          llog_reader cat.dmp | grep \\\"type=1064553b\\\" | wc -l")
14681
14682         if (( cat_sl != 2 )); then
14683                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14684                 error "Changelog catalog has wrong number of slots $cat_sl"
14685         fi
14686
14687         $LFS changelog_clear $MDT0 $cl_user 0
14688
14689         cat_sl=$(do_facet mds1 \
14690         "$DEBUGFS -R \\\"dump changelog_catalog cat.dmp\\\" $mdt_dev; \
14691          llog_reader cat.dmp | grep \\\"type=1064553b\\\" | wc -l")
14692
14693         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14694
14695         if (( cat_sl == 2 )); then
14696                 error "Empty plain llog was not deleted from changelog catalog"
14697         fi
14698         if (( cat_sl != 1 )); then
14699                 error "Active plain llog shouldn\`t be deleted from catalog"
14700         fi
14701 }
14702 run_test 256 "Check llog delete for empty and not full state"
14703
14704 test_257() {
14705         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14706         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
14707                 skip "Need MDS version at least 2.8.55" && return
14708
14709         test_mkdir -p $DIR/$tdir
14710
14711         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
14712                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
14713         stat $DIR/$tdir
14714
14715 #define OBD_FAIL_MDS_XATTR_REP                  0x161
14716         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
14717         local facet=mds$((mdtidx + 1))
14718         set_nodes_failloc $(facet_active_host $facet) 0x80000161
14719         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
14720
14721         stop $facet || error "stop MDS failed"
14722         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
14723                 error "start MDS fail"
14724 }
14725 run_test 257 "xattr locks are not lost"
14726
14727 test_260() {
14728 #define OBD_FAIL_MDC_CLOSE               0x806
14729         $LCTL set_param fail_loc=0x80000806
14730         touch $DIR/$tfile
14731
14732 }
14733 run_test 260 "Check mdc_close fail"
14734
14735 cleanup_test_300() {
14736         trap 0
14737         umask $SAVE_UMASK
14738 }
14739 test_striped_dir() {
14740         local mdt_index=$1
14741         local stripe_count
14742         local stripe_index
14743
14744         mkdir -p $DIR/$tdir
14745
14746         SAVE_UMASK=$(umask)
14747         trap cleanup_test_300 RETURN EXIT
14748
14749         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
14750                                                 $DIR/$tdir/striped_dir ||
14751                 error "set striped dir error"
14752
14753         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
14754         [ "$mode" = "755" ] || error "expect 755 got $mode"
14755
14756         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
14757                 error "getdirstripe failed"
14758         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
14759         if [ "$stripe_count" != "2" ]; then
14760                 error "1:stripe_count is $stripe_count, expect 2"
14761         fi
14762         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
14763         if [ "$stripe_count" != "2" ]; then
14764                 error "2:stripe_count is $stripe_count, expect 2"
14765         fi
14766
14767         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
14768         if [ "$stripe_index" != "$mdt_index" ]; then
14769                 error "stripe_index is $stripe_index, expect $mdt_index"
14770         fi
14771
14772         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
14773                 error "nlink error after create striped dir"
14774
14775         mkdir $DIR/$tdir/striped_dir/a
14776         mkdir $DIR/$tdir/striped_dir/b
14777
14778         stat $DIR/$tdir/striped_dir/a ||
14779                 error "create dir under striped dir failed"
14780         stat $DIR/$tdir/striped_dir/b ||
14781                 error "create dir under striped dir failed"
14782
14783         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
14784                 error "nlink error after mkdir"
14785
14786         rmdir $DIR/$tdir/striped_dir/a
14787         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
14788                 error "nlink error after rmdir"
14789
14790         rmdir $DIR/$tdir/striped_dir/b
14791         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
14792                 error "nlink error after rmdir"
14793
14794         chattr +i $DIR/$tdir/striped_dir
14795         createmany -o $DIR/$tdir/striped_dir/f 10 &&
14796                 error "immutable flags not working under striped dir!"
14797         chattr -i $DIR/$tdir/striped_dir
14798
14799         rmdir $DIR/$tdir/striped_dir ||
14800                 error "rmdir striped dir error"
14801
14802         cleanup_test_300
14803
14804         true
14805 }
14806
14807 test_300a() {
14808         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
14809                 skip "skipped for lustre < 2.7.0" && return
14810         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14811         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14812
14813         test_striped_dir 0 || error "failed on striped dir on MDT0"
14814         test_striped_dir 1 || error "failed on striped dir on MDT0"
14815 }
14816 run_test 300a "basic striped dir sanity test"
14817
14818 test_300b() {
14819         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
14820                 skip "skipped for lustre < 2.7.0" && return
14821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14822         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14823         local i
14824         local mtime1
14825         local mtime2
14826         local mtime3
14827
14828         test_mkdir $DIR/$tdir || error "mkdir fail"
14829         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
14830                 error "set striped dir error"
14831         for ((i=0; i<10; i++)); do
14832                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
14833                 sleep 1
14834                 touch $DIR/$tdir/striped_dir/file_$i ||
14835                                         error "touch error $i"
14836                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
14837                 [ $mtime1 -eq $mtime2 ] &&
14838                         error "mtime not change after create"
14839                 sleep 1
14840                 rm -f $DIR/$tdir/striped_dir/file_$i ||
14841                                         error "unlink error $i"
14842                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
14843                 [ $mtime2 -eq $mtime3 ] &&
14844                         error "mtime did not change after unlink"
14845         done
14846         true
14847 }
14848 run_test 300b "check ctime/mtime for striped dir"
14849
14850 test_300c() {
14851         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
14852                 skip "skipped for lustre < 2.7.0" && return
14853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14854         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14855         local file_count
14856
14857         mkdir -p $DIR/$tdir
14858         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
14859                 error "set striped dir error"
14860
14861         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
14862                 error "chown striped dir failed"
14863
14864         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
14865                 error "create 5k files failed"
14866
14867         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
14868
14869         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
14870
14871         rm -rf $DIR/$tdir
14872 }
14873 run_test 300c "chown && check ls under striped directory"
14874
14875 test_300d() {
14876         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
14877                 skip "skipped for lustre < 2.7.0" && return
14878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14879         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14880         local stripe_count
14881         local file
14882
14883         mkdir -p $DIR/$tdir
14884         $SETSTRIPE -c 2 $DIR/$tdir
14885
14886         #local striped directory
14887         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
14888                 error "set striped dir error"
14889         createmany -o $DIR/$tdir/striped_dir/f 10 ||
14890                 error "create 10 files failed"
14891
14892         #remote striped directory
14893         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
14894                 error "set striped dir error"
14895         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
14896                 error "create 10 files failed"
14897
14898         for file in $(find $DIR/$tdir); do
14899                 stripe_count=$($GETSTRIPE -c $file)
14900                 [ $stripe_count -eq 2 ] ||
14901                         error "wrong stripe $stripe_count for $file"
14902         done
14903
14904         rm -rf $DIR/$tdir
14905 }
14906 run_test 300d "check default stripe under striped directory"
14907
14908 test_300e() {
14909         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14910                 skip "Need MDS version at least 2.7.55" && return
14911         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14912         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14913         local stripe_count
14914         local file
14915
14916         mkdir -p $DIR/$tdir
14917
14918         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
14919                 error "set striped dir error"
14920
14921         touch $DIR/$tdir/striped_dir/a
14922         touch $DIR/$tdir/striped_dir/b
14923         touch $DIR/$tdir/striped_dir/c
14924
14925         mkdir $DIR/$tdir/striped_dir/dir_a
14926         mkdir $DIR/$tdir/striped_dir/dir_b
14927         mkdir $DIR/$tdir/striped_dir/dir_c
14928
14929         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
14930                 error "set striped adir under striped dir error"
14931
14932         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
14933                 error "set striped bdir under striped dir error"
14934
14935         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
14936                 error "set striped cdir under striped dir error"
14937
14938         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
14939                 error "rename dir under striped dir fails"
14940
14941         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
14942                 error "rename dir under different stripes fails"
14943
14944         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
14945                 error "rename file under striped dir should succeed"
14946
14947         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
14948                 error "rename dir under striped dir should succeed"
14949
14950         rm -rf $DIR/$tdir
14951 }
14952 run_test 300e "check rename under striped directory"
14953
14954 test_300f() {
14955         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14956                 skip "Need MDS version at least 2.7.55" && return
14957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14958         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14959         local stripe_count
14960         local file
14961
14962         rm -rf $DIR/$tdir
14963         mkdir -p $DIR/$tdir
14964
14965         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
14966                 error "set striped dir error"
14967
14968         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
14969                 error "set striped dir error"
14970
14971         touch $DIR/$tdir/striped_dir/a
14972         mkdir $DIR/$tdir/striped_dir/dir_a
14973         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
14974                 error "create striped dir under striped dir fails"
14975
14976         touch $DIR/$tdir/striped_dir1/b
14977         mkdir $DIR/$tdir/striped_dir1/dir_b
14978         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
14979                 error "create striped dir under striped dir fails"
14980
14981         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
14982                 error "rename dir under different striped dir should fail"
14983
14984         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
14985                 error "rename striped dir under diff striped dir should fail"
14986
14987         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
14988                 error "rename file under diff striped dirs fails"
14989
14990         rm -rf $DIR/$tdir
14991 }
14992 run_test 300f "check rename cross striped directory"
14993
14994 test_300_check_default_striped_dir()
14995 {
14996         local dirname=$1
14997         local default_count=$2
14998         local default_index=$3
14999         local stripe_count
15000         local stripe_index
15001         local dir_stripe_index
15002         local dir
15003
15004         echo "checking $dirname $default_count $default_index"
15005         $LFS setdirstripe -D -c $default_count -i $default_index \
15006                                 -t all_char $DIR/$tdir/$dirname ||
15007                 error "set default stripe on striped dir error"
15008         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
15009         [ $stripe_count -eq $default_count ] ||
15010                 error "expect $default_count get $stripe_count for $dirname"
15011
15012         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
15013         [ $stripe_index -eq $default_index ] ||
15014                 error "expect $default_index get $stripe_index for $dirname"
15015
15016         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
15017                                                 error "create dirs failed"
15018
15019         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
15020         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
15021         for dir in $(find $DIR/$tdir/$dirname/*); do
15022                 stripe_count=$($LFS getdirstripe -c $dir)
15023                 [ $stripe_count -eq $default_count ] ||
15024                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
15025                 error "stripe count $default_count != $stripe_count for $dir"
15026
15027                 stripe_index=$($LFS getdirstripe -i $dir)
15028                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
15029                         error "$stripe_index != $default_index for $dir"
15030
15031                 #check default stripe
15032                 stripe_count=$($LFS getdirstripe -D -c $dir)
15033                 [ $stripe_count -eq $default_count ] ||
15034                 error "default count $default_count != $stripe_count for $dir"
15035
15036                 stripe_index=$($LFS getdirstripe -D -i $dir)
15037                 [ $stripe_index -eq $default_index ] ||
15038                 error "default index $default_index != $stripe_index for $dir"
15039         done
15040         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
15041 }
15042
15043 test_300g() {
15044         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15045                 skip "Need MDS version at least 2.7.55" && return
15046         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15047         local dir
15048         local stripe_count
15049         local stripe_index
15050
15051         mkdir $DIR/$tdir
15052         mkdir $DIR/$tdir/normal_dir
15053
15054         #Checking when client cache stripe index
15055         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
15056         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
15057                 error "create striped_dir failed"
15058
15059         mkdir $DIR/$tdir/striped_dir/dir1 ||
15060                 error "create dir1 fails"
15061         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
15062         [ $stripe_index -eq 1 ] ||
15063                 error "dir1 expect 1 got $stripe_index"
15064
15065         $LFS setdirstripe -i2 $DIR/$tdir/striped_dir/dir2 ||
15066                 error "create dir2 fails"
15067         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir2)
15068         [ $stripe_index -eq 2 ] ||
15069                 error "dir2 expect 2 got $stripe_index"
15070
15071         #check default stripe count/stripe index
15072         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
15073         test_300_check_default_striped_dir normal_dir 1 0
15074         test_300_check_default_striped_dir normal_dir 2 1
15075         test_300_check_default_striped_dir normal_dir 2 -1
15076
15077         #delete default stripe information
15078         echo "delete default stripeEA"
15079         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
15080                 error "set default stripe on striped dir error"
15081
15082         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
15083         for dir in $(find $DIR/$tdir/normal_dir/*); do
15084                 stripe_count=$($LFS getdirstripe -c $dir)
15085                 [ $stripe_count -eq 0 ] ||
15086                         error "expect 1 get $stripe_count for $dir"
15087                 stripe_index=$($LFS getdirstripe -i $dir)
15088                 [ $stripe_index -eq 0 ] ||
15089                         error "expect 0 get $stripe_index for $dir"
15090         done
15091 }
15092 run_test 300g "check default striped directory for normal directory"
15093
15094 test_300h() {
15095         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15096                 skip "Need MDS version at least 2.7.55" && return
15097         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15098         local dir
15099         local stripe_count
15100
15101         mkdir $DIR/$tdir
15102         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
15103                                         $DIR/$tdir/striped_dir ||
15104                 error "set striped dir error"
15105
15106         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
15107         test_300_check_default_striped_dir striped_dir 1 0
15108         test_300_check_default_striped_dir striped_dir 2 1
15109         test_300_check_default_striped_dir striped_dir 2 -1
15110
15111         #delete default stripe information
15112         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
15113                 error "set default stripe on striped dir error"
15114
15115         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
15116         for dir in $(find $DIR/$tdir/striped_dir/*); do
15117                 stripe_count=$($LFS getdirstripe -c $dir)
15118                 [ $stripe_count -eq 0 ] ||
15119                         error "expect 1 get $stripe_count for $dir"
15120         done
15121 }
15122 run_test 300h "check default striped directory for striped directory"
15123
15124 test_300i() {
15125         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15126                 skip "Need MDS version at least 2.7.55" && return
15127         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15128         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15129         local stripe_count
15130         local file
15131
15132         mkdir $DIR/$tdir
15133
15134         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15135                 error "set striped dir error"
15136
15137         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15138                 error "create files under striped dir failed"
15139
15140         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
15141                 error "set striped hashdir error"
15142
15143         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
15144                 error "create dir0 under hash dir failed"
15145         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
15146                 error "create dir1 under hash dir failed"
15147
15148         # unfortunately, we need to umount to clear dir layout cache for now
15149         # once we fully implement dir layout, we can drop this
15150         umount_client $MOUNT || error "umount failed"
15151         mount_client $MOUNT || error "mount failed"
15152
15153         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
15154         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
15155         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
15156
15157         #set the stripe to be unknown hash type
15158         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
15159         $LCTL set_param fail_loc=0x1901
15160         for ((i = 0; i < 10; i++)); do
15161                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
15162                         error "stat f-$i failed"
15163                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
15164         done
15165
15166         touch $DIR/$tdir/striped_dir/f0 &&
15167                 error "create under striped dir with unknown hash should fail"
15168
15169         $LCTL set_param fail_loc=0
15170
15171         umount_client $MOUNT || error "umount failed"
15172         mount_client $MOUNT || error "mount failed"
15173
15174         return 0
15175 }
15176 run_test 300i "client handle unknown hash type striped directory"
15177
15178 test_300j() {
15179         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15180         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15181                 skip "Need MDS version at least 2.7.55" && return
15182         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15183         local stripe_count
15184         local file
15185
15186         mkdir $DIR/$tdir
15187
15188         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
15189         $LCTL set_param fail_loc=0x1702
15190         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15191                 error "set striped dir error"
15192
15193         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15194                 error "create files under striped dir failed"
15195
15196         $LCTL set_param fail_loc=0
15197
15198         rm -rf $DIR/$tdir || error "unlink striped dir fails"
15199
15200         return 0
15201 }
15202 run_test 300j "test large update record"
15203
15204 test_300k() {
15205         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15206                 skip "Need MDS version at least 2.7.55" && return
15207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15208         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15209         local stripe_count
15210         local file
15211
15212         mkdir $DIR/$tdir
15213
15214         #define OBD_FAIL_LARGE_STRIPE   0x1703
15215         $LCTL set_param fail_loc=0x1703
15216         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
15217                 error "set striped dir error"
15218         $LCTL set_param fail_loc=0
15219
15220         $LFS getdirstripe $DIR/$tdir/striped_dir ||
15221                 error "getstripeddir fails"
15222         rm -rf $DIR/$tdir/striped_dir ||
15223                 error "unlink striped dir fails"
15224
15225         return 0
15226 }
15227 run_test 300k "test large striped directory"
15228
15229 test_300l() {
15230         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15231                 skip "Need MDS version at least 2.7.55" && return
15232         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15233         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15234         local stripe_index
15235
15236         test_mkdir -p $DIR/$tdir/striped_dir
15237         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
15238                         error "chown $RUNAS_ID failed"
15239         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
15240                 error "set default striped dir failed"
15241
15242         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
15243         $LCTL set_param fail_loc=0x80000158
15244         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
15245
15246         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
15247         [ $stripe_index -eq 1 ] ||
15248                 error "expect 1 get $stripe_index for $dir"
15249 }
15250 run_test 300l "non-root user to create dir under striped dir with stale layout"
15251
15252 test_300m() {
15253         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15254                 skip "Need MDS version at least 2.7.55" && return
15255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15256         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
15257
15258         mkdir -p $DIR/$tdir/striped_dir
15259         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
15260                 error "set default stripes dir error"
15261
15262         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
15263
15264         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
15265         [ $stripe_count -eq 0 ] ||
15266                         error "expect 0 get $stripe_count for a"
15267
15268         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
15269                 error "set default stripes dir error"
15270
15271         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
15272
15273         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
15274         [ $stripe_count -eq 0 ] ||
15275                         error "expect 0 get $stripe_count for b"
15276
15277         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
15278                 error "set default stripes dir error"
15279
15280         mkdir $DIR/$tdir/striped_dir/c &&
15281                 error "default stripe_index is invalid, mkdir c should fails"
15282
15283         rm -rf $DIR/$tdir || error "rmdir fails"
15284 }
15285 run_test 300m "setstriped directory on single MDT FS"
15286
15287 cleanup_300n() {
15288         local list=$(comma_list $(mdts_nodes))
15289
15290         trap 0
15291         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15292 }
15293
15294 test_300n() {
15295         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15296                 skip "Need MDS version at least 2.7.55" && return
15297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15298         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15299         local stripe_index
15300         local list=$(comma_list $(mdts_nodes))
15301
15302         trap cleanup_300n RETURN EXIT
15303         mkdir -p $DIR/$tdir
15304         chmod 777 $DIR/$tdir
15305         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
15306                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15307                 error "create striped dir succeeds with gid=0"
15308
15309         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15310         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
15311                 error "create striped dir fails with gid=-1"
15312
15313         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15314         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
15315                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15316                 error "set default striped dir succeeds with gid=0"
15317
15318
15319         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15320         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
15321                 error "set default striped dir fails with gid=-1"
15322
15323
15324         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15325         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
15326                                         error "create test_dir fails"
15327         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
15328                                         error "create test_dir1 fails"
15329         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
15330                                         error "create test_dir2 fails"
15331         cleanup_300n
15332 }
15333 run_test 300n "non-root user to create dir under striped dir with default EA"
15334
15335 test_300o() {
15336         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15337                 skip "Need MDS version at least 2.7.55" && return
15338         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15339         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15340         local numfree1
15341         local numfree2
15342
15343         mkdir -p $DIR/$tdir
15344
15345         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
15346         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
15347         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
15348                 skip "not enough free inodes $numfree1 $numfree2"
15349                 return
15350         fi
15351
15352         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
15353         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
15354         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
15355                 skip "not enough free space $numfree1 $numfree2"
15356                 return
15357         fi
15358
15359         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
15360                 error "setdirstripe fails"
15361
15362         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
15363                 error "create dirs fails"
15364
15365         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
15366         ls $DIR/$tdir/striped_dir > /dev/null ||
15367                 error "ls striped dir fails"
15368         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
15369                 error "unlink big striped dir fails"
15370 }
15371 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
15372
15373 test_300p() {
15374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15375         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15376         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15377
15378         mkdir -p $DIR/$tdir
15379
15380         #define OBD_FAIL_OUT_ENOSPC     0x1704
15381         do_facet mds2 lctl set_param fail_loc=0x80001704
15382         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
15383                         error "create striped directory should fail"
15384
15385         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
15386
15387         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
15388         true
15389 }
15390 run_test 300p "create striped directory without space"
15391
15392 test_300q() {
15393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15394         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15395
15396         local fd=$(free_fd)
15397         local cmd="exec $fd<$tdir"
15398         cd $DIR
15399         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
15400         eval $cmd
15401         cmd="exec $fd<&-"
15402         trap "eval $cmd" EXIT
15403         cd $tdir || error "cd $tdir fails"
15404         rmdir  ../$tdir || error "rmdir $tdir fails"
15405         mkdir local_dir && error "create dir succeeds"
15406         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
15407         eval $cmd
15408         return 0
15409 }
15410 run_test 300q "create remote directory under orphan directory"
15411
15412 prepare_remote_file() {
15413         mkdir $DIR/$tdir/src_dir ||
15414                 error "create remote source failed"
15415
15416         cp /etc/hosts $DIR/$tdir/src_dir/a || error
15417         touch $DIR/$tdir/src_dir/a
15418
15419         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
15420                 error "create remote target dir failed"
15421
15422         touch $DIR/$tdir/tgt_dir/b
15423
15424         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
15425                 error "rename dir cross MDT failed!"
15426
15427         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
15428                 error "src_child still exists after rename"
15429
15430         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
15431                 error "missing file(a) after rename"
15432
15433         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
15434                 error "diff after rename"
15435 }
15436
15437 test_310a() {
15438         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
15439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15440         local remote_file=$DIR/$tdir/tgt_dir/b
15441
15442         mkdir -p $DIR/$tdir
15443
15444         prepare_remote_file || error "prepare remote file failed"
15445
15446         #open-unlink file
15447         $OPENUNLINK $remote_file $remote_file || error
15448         $CHECKSTAT -a $remote_file || error
15449 }
15450 run_test 310a "open unlink remote file"
15451
15452 test_310b() {
15453         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
15454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15455         local remote_file=$DIR/$tdir/tgt_dir/b
15456
15457         mkdir -p $DIR/$tdir
15458
15459         prepare_remote_file || error "prepare remote file failed"
15460
15461         ln $remote_file $DIR/$tfile || error "link failed for remote file"
15462         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
15463         $CHECKSTAT -t file $remote_file || error "check file failed"
15464 }
15465 run_test 310b "unlink remote file with multiple links while open"
15466
15467 test_310c() {
15468         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
15469         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15470         local remote_file=$DIR/$tdir/tgt_dir/b
15471
15472         mkdir -p $DIR/$tdir
15473
15474         prepare_remote_file || error "prepare remote file failed"
15475
15476         ln $remote_file $DIR/$tfile || error "link failed for remote file"
15477         multiop_bg_pause $remote_file O_uc ||
15478                         error "mulitop failed for remote file"
15479         MULTIPID=$!
15480         $MULTIOP $DIR/$tfile Ouc
15481         kill -USR1 $MULTIPID
15482         wait $MULTIPID
15483 }
15484 run_test 310c "open-unlink remote file with multiple links"
15485
15486 #LU-4825
15487 test_311() {
15488         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
15489                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
15490         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15491
15492         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
15493
15494         mkdir -p $DIR/$tdir
15495         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
15496         createmany -o $DIR/$tdir/$tfile. 1000
15497
15498         # statfs data is not real time, let's just calculate it
15499         old_iused=$((old_iused + 1000))
15500
15501         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
15502                         osp.*OST0000*MDT0000.create_count")
15503         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
15504                                 osp.*OST0000*MDT0000.max_create_count")
15505         for idx in $(seq $MDSCOUNT); do
15506                 do_facet mds$idx "lctl set_param -n \
15507                         osp.*OST0000*MDT000?.max_create_count=0"
15508         done
15509
15510         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
15511         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
15512         [ $index -ne 0 ] || error "$tfile stripe index is 0"
15513
15514         unlinkmany $DIR/$tdir/$tfile. 1000
15515
15516         for idx in $(seq $MDSCOUNT); do
15517                 do_facet mds$idx "lctl set_param -n \
15518                         osp.*OST0000*MDT000?.max_create_count=$max_count"
15519                 do_facet mds$idx "lctl set_param -n \
15520                         osp.*OST0000*MDT000?.create_count=$count"
15521         done
15522
15523         local new_iused
15524         for i in $(seq 120); do
15525                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
15526                 # system may be too busy to destroy all objs in time, use
15527                 # a somewhat small value to not fail autotest
15528                 [ $((old_iused - new_iused)) -gt 400 ] && break
15529                 sleep 1
15530         done
15531
15532         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
15533         [ $((old_iused - new_iused)) -gt 400 ] ||
15534                 error "objs not destroyed after unlink"
15535 }
15536 run_test 311 "disable OSP precreate, and unlink should destroy objs"
15537
15538 zfs_oid_to_objid()
15539 {
15540         local ost=$1
15541         local objid=$2
15542
15543         local vdevdir=$(dirname $(facet_vdevice $ost))
15544         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
15545         local zfs_zapid=$(do_facet $ost $cmd |
15546                           grep -w "/O/0/d$((objid%32))" -C 5 |
15547                           awk '/Object/{getline; print $1}')
15548         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
15549                           awk "/$objid = /"'{printf $3}')
15550
15551         echo $zfs_objid
15552 }
15553
15554 zfs_object_blksz() {
15555         local ost=$1
15556         local objid=$2
15557
15558         local vdevdir=$(dirname $(facet_vdevice $ost))
15559         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
15560         local blksz=$(do_facet $ost $cmd $objid |
15561                       awk '/dblk/{getline; printf $4}')
15562
15563         case "${blksz: -1}" in
15564                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
15565                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
15566                 *) ;;
15567         esac
15568
15569         echo $blksz
15570 }
15571
15572 test_312() { # LU-4856
15573         [ $(facet_fstype ost1) = "zfs" ] ||
15574                 { skip "the test only applies to zfs" && return; }
15575
15576         local max_blksz=$(do_facet ost1 \
15577                           $ZFS get -p recordsize $(facet_device ost1) |
15578                           awk '!/VALUE/{print $3}')
15579
15580         # to make life a little bit easier
15581         $LFS mkdir -c 1 -i 0 $DIR/$tdir
15582         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15583
15584         local tf=$DIR/$tdir/$tfile
15585         touch $tf
15586         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
15587
15588         # Get ZFS object id
15589         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
15590
15591         # block size change by sequential over write
15592         local blksz
15593         for ((bs=4096; bs <= max_blksz; bs <<= 2)); do
15594                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
15595
15596                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
15597                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
15598         done
15599         rm -f $tf
15600
15601         # block size change by sequential append write
15602         dd if=/dev/zero of=$tf bs=4K count=1 oflag=sync conv=notrunc
15603         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
15604         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
15605
15606         for ((count = 1; count < $((max_blksz / 4096)); count *= 2)); do
15607                 dd if=/dev/zero of=$tf bs=4K count=$count seek=$count \
15608                         oflag=sync conv=notrunc
15609
15610                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
15611                 blksz=$((blksz / 8192)) # in 2*4K unit
15612                 [ $blksz -eq $count ] ||
15613                         error "blksz error(in 8k): $blksz, expected: $count"
15614         done
15615         rm -f $tf
15616
15617         # random write
15618         touch $tf
15619         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
15620         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
15621
15622         dd if=/dev/zero of=$tf bs=8K count=1 oflag=sync conv=notrunc
15623         blksz=$(zfs_object_blksz ost1 $zfs_objid)
15624         [ $blksz -eq 8192 ] || error "blksz error: $blksz, expected: 8k"
15625
15626         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
15627         blksz=$(zfs_object_blksz ost1 $zfs_objid)
15628         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
15629
15630         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
15631         blksz=$(zfs_object_blksz ost1 $zfs_objid)
15632         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
15633 }
15634 run_test 312 "make sure ZFS adjusts its block size by write pattern"
15635
15636 test_313() {
15637         local file=$DIR/$tfile
15638         rm -f $file
15639         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
15640
15641         # define OBD_FAIL_TGT_RCVD_EIO           0x720
15642         do_facet ost1 "$LCTL set_param fail_loc=0x720"
15643         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
15644                 error "write should failed"
15645         do_facet ost1 "$LCTL set_param fail_loc=0"
15646         rm -f $file
15647 }
15648 run_test 313 "io should fail after last_rcvd update fail"
15649
15650 test_fake_rw() {
15651         local read_write=$1
15652         if [ "$read_write" = "write" ]; then
15653                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
15654         elif [ "$read_write" = "read" ]; then
15655                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
15656         else
15657                 error "argument error"
15658         fi
15659
15660         # turn off debug for performance testing
15661         local saved_debug=$($LCTL get_param -n debug)
15662         $LCTL set_param debug=0
15663
15664         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
15665
15666         # get ost1 size - lustre-OST0000
15667         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
15668         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
15669         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
15670
15671         if [ "$read_write" = "read" ]; then
15672                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
15673         fi
15674
15675         local start_time=$(date +%s.%N)
15676         $dd_cmd bs=1M count=$blocks oflag=sync ||
15677                 error "real dd $read_write error"
15678         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
15679
15680         if [ "$read_write" = "write" ]; then
15681                 rm -f $DIR/$tfile
15682         fi
15683
15684         # define OBD_FAIL_OST_FAKE_RW           0x238
15685         do_facet ost1 $LCTL set_param fail_loc=0x238
15686
15687         local start_time=$(date +%s.%N)
15688         $dd_cmd bs=1M count=$blocks oflag=sync ||
15689                 error "fake dd $read_write error"
15690         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
15691
15692         if [ "$read_write" = "write" ]; then
15693                 # verify file size
15694                 cancel_lru_locks osc
15695                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
15696                         error "$tfile size not $blocks MB"
15697         fi
15698         do_facet ost1 $LCTL set_param fail_loc=0
15699
15700         echo "fake $read_write $duration_fake vs. normal $read_write" \
15701                 "$duration in seconds"
15702         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
15703                 error_not_in_vm "fake write is slower"
15704
15705         $LCTL set_param -n debug="$saved_debug"
15706         rm -f $DIR/$tfile
15707 }
15708 test_399a() { # LU-7655 for OST fake write
15709         test_fake_rw write
15710 }
15711 run_test 399a "fake write should not be slower than normal write"
15712
15713
15714 test_399b() { # LU-8726 for OST fake read
15715         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
15716                 skip "only for ldiskfs" && return 0
15717         fi
15718         test_fake_rw read
15719 }
15720 run_test 399b "fake read should not be slower than normal read"
15721
15722 test_400a() { # LU-1606, was conf-sanity test_74
15723         local extra_flags=''
15724         local out=$TMP/$tfile
15725         local prefix=/usr/include/lustre
15726         local prog
15727
15728         if ! which $CC > /dev/null 2>&1; then
15729                 skip_env "$CC is not installed"
15730                 return 0
15731         fi
15732
15733         if ! [[ -d $prefix ]]; then
15734                 # Assume we're running in tree and fixup the include path.
15735                 extra_flags+=" -I$LUSTRE/../libcfs/include"
15736                 extra_flags+=" -I$LUSTRE/include"
15737                 extra_flags+=" -L$LUSTRE/utils"
15738         fi
15739
15740         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
15741                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
15742                         error "client api broken"
15743         done
15744         rm -f $out
15745 }
15746 run_test 400a "Lustre client api program can compile and link"
15747
15748 test_400b() { # LU-1606, LU-5011
15749         local header
15750         local out=$TMP/$tfile
15751         local prefix=/usr/include/lustre
15752
15753         # We use a hard coded prefix so that this test will not fail
15754         # when run in tree. There are headers in lustre/include/lustre/
15755         # that are not packaged (like lustre_idl.h) and have more
15756         # complicated include dependencies (like config.h and lnet/types.h).
15757         # Since this test about correct packaging we just skip them when
15758         # they don't exist (see below) rather than try to fixup cppflags.
15759
15760         if ! which $CC > /dev/null 2>&1; then
15761                 skip_env "$CC is not installed"
15762                 return 0
15763         fi
15764
15765         for header in $prefix/*.h; do
15766                 if ! [[ -f "$header" ]]; then
15767                         continue
15768                 fi
15769
15770                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
15771                         continue # liblustreapi.h is deprecated.
15772                 fi
15773
15774                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
15775                         error "cannot compile '$header'"
15776         done
15777         rm -f $out
15778 }
15779 run_test 400b "packaged headers can be compiled"
15780
15781 test_401a() { #LU-7437
15782         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
15783         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
15784                 return
15785         #count the number of parameters by "list_param -R"
15786         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
15787         #count the number of parameters by listing proc files
15788         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
15789         echo "proc_dirs='$proc_dirs'"
15790         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
15791         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
15792                       sort -u | wc -l)
15793
15794         [ $params -eq $procs ] ||
15795                 error "found $params parameters vs. $procs proc files"
15796
15797         # test the list_param -D option only returns directories
15798         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
15799         #count the number of parameters by listing proc directories
15800         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
15801                 sort -u | wc -l)
15802
15803         [ $params -eq $procs ] ||
15804                 error "found $params parameters vs. $procs proc files"
15805 }
15806 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
15807
15808 test_401b() {
15809         local save=$($LCTL get_param -n jobid_var)
15810         local tmp=testing
15811
15812         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
15813                 error "no error returned when setting bad parameters"
15814
15815         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
15816         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
15817
15818         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
15819         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
15820         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
15821 }
15822 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
15823
15824 test_401c() {
15825         local jobid_var_old=$($LCTL get_param -n jobid_var)
15826         local jobid_var_new
15827
15828         $LCTL set_param jobid_var= &&
15829                 error "no error returned for 'set_param a='"
15830
15831         jobid_var_new=$($LCTL get_param -n jobid_var)
15832         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
15833                 error "jobid_var was changed by setting without value"
15834
15835         $LCTL set_param jobid_var &&
15836                 error "no error returned for 'set_param a'"
15837
15838         jobid_var_new=$($LCTL get_param -n jobid_var)
15839         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
15840                 error "jobid_var was changed by setting without value"
15841 }
15842 run_test 401c "Verify 'lctl set_param' without value fails in either format."
15843
15844 test_401d() {
15845         local jobid_var_old=$($LCTL get_param -n jobid_var)
15846         local jobid_var_new
15847         local new_value="foo=bar"
15848
15849         $LCTL set_param jobid_var=$new_value ||
15850                 error "'set_param a=b' did not accept a value containing '='"
15851
15852         jobid_var_new=$($LCTL get_param -n jobid_var)
15853         [[ "$jobid_var_new" == "$new_value" ]] ||
15854                 error "'set_param a=b' failed on a value containing '='"
15855
15856         # Reset the jobid_var to test the other format
15857         $LCTL set_param jobid_var=$jobid_var_old
15858         jobid_var_new=$($LCTL get_param -n jobid_var)
15859         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
15860                 error "failed to reset jobid_var"
15861
15862         $LCTL set_param jobid_var $new_value ||
15863                 error "'set_param a b' did not accept a value containing '='"
15864
15865         jobid_var_new=$($LCTL get_param -n jobid_var)
15866         [[ "$jobid_var_new" == "$new_value" ]] ||
15867                 error "'set_param a b' failed on a value containing '='"
15868
15869         $LCTL set_param jobid_var $jobid_var_old
15870         jobid_var_new=$($LCTL get_param -n jobid_var)
15871         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
15872                 error "failed to reset jobid_var"
15873 }
15874 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
15875
15876 test_402() {
15877         local server_version=$(lustre_version_code $SINGLEMDS)
15878         [[ $server_version -ge $(version_code 2.7.66) ]] ||
15879         [[ $server_version -ge $(version_code 2.7.18.4) &&
15880                 $server_version -lt $(version_code 2.7.50) ]] ||
15881         [[ $server_version -ge $(version_code 2.7.2) &&
15882                 $server_version -lt $(version_code 2.7.11) ]] ||
15883                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
15884                         return; }
15885         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15886         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
15887 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
15888         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
15889         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
15890                 echo "Touch failed - OK"
15891 }
15892 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
15893
15894 test_403() {
15895         local file1=$DIR/$tfile.1
15896         local file2=$DIR/$tfile.2
15897         local tfile=$TMP/$tfile
15898
15899         rm -f $file1 $file2 $tfile
15900
15901         touch $file1
15902         ln $file1 $file2
15903
15904         # 30 sec OBD_TIMEOUT in ll_getattr()
15905         # right before populating st_nlink
15906         $LCTL set_param fail_loc=0x80001409
15907         stat -c %h $file1 > $tfile &
15908
15909         # create an alias, drop all locks and reclaim the dentry
15910         < $file2
15911         cancel_lru_locks mdc
15912         cancel_lru_locks osc
15913         sysctl -w vm.drop_caches=2
15914
15915         wait
15916
15917         [ `cat $tfile` -gt 0 ] || error "wrong nlink count: `cat $tfile`"
15918
15919         rm -f $tfile $file1 $file2
15920 }
15921 run_test 403 "i_nlink should not drop to zero due to aliasing"
15922
15923 test_404() { # LU-6601
15924         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15925         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
15926                 awk '/osp .*-osc-MDT/ { print $4}')
15927
15928         local osp
15929         for osp in $mosps; do
15930                 echo "Deactivate: " $osp
15931                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
15932                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
15933                         awk -vp=$osp '$4 == p { print $2 }')
15934                 [ $stat = IN ] || {
15935                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
15936                         error "deactivate error"
15937                 }
15938                 echo "Activate: " $osp
15939                 do_facet $SINGLEMDS $LCTL --device %$osp activate
15940                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
15941                         awk -vp=$osp '$4 == p { print $2 }')
15942                 [ $stat = UP ] || {
15943                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
15944                         error "activate error"
15945                 }
15946         done
15947 }
15948 run_test 404 "validate manual {de}activated works properly for OSPs"
15949
15950 test_405() {
15951         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
15952                 skip "Layout swap lock is not supported" && return
15953
15954         check_swap_layouts_support && return 0
15955
15956         test_mkdir -p $DIR/$tdir
15957         swap_lock_test -d $DIR/$tdir ||
15958                 error "One layout swap locked test failed"
15959 }
15960 run_test 405 "Various layout swap lock tests"
15961
15962 test_406() {
15963         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15964         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
15965         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
15966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15967         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
15968                 skip "Need MDS version at least 2.8.50" && return
15969
15970         local def_stripenr=$($GETSTRIPE -c $MOUNT)
15971         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
15972         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
15973         local def_pool=$($GETSTRIPE -p $MOUNT)
15974
15975         local test_pool=$TESTNAME
15976         pool_add $test_pool || error "pool_add failed"
15977         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
15978                 error "pool_add_targets failed"
15979
15980         # parent set default stripe count only, child will stripe from both
15981         # parent and fs default
15982         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
15983                 error "setstripe $MOUNT failed"
15984         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
15985         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
15986         for i in $(seq 10); do
15987                 local f=$DIR/$tdir/$tfile.$i
15988                 touch $f || error "touch failed"
15989                 local count=$($GETSTRIPE -c $f)
15990                 [ $count -eq $OSTCOUNT ] ||
15991                         error "$f stripe count $count != $OSTCOUNT"
15992                 local offset=$($GETSTRIPE -i $f)
15993                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
15994                 local size=$($GETSTRIPE -S $f)
15995                 [ $size -eq $((def_stripe_size * 2)) ] ||
15996                         error "$f stripe size $size != $((def_stripe_size * 2))"
15997                 local pool=$($GETSTRIPE -p $f)
15998                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
15999         done
16000
16001         # change fs default striping, delete parent default striping, now child
16002         # will stripe from new fs default striping only
16003         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
16004                 error "change $MOUNT default stripe failed"
16005         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
16006         for i in $(seq 11 20); do
16007                 local f=$DIR/$tdir/$tfile.$i
16008                 touch $f || error "touch $f failed"
16009                 local count=$($GETSTRIPE -c $f)
16010                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
16011                 local offset=$($GETSTRIPE -i $f)
16012                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
16013                 local size=$($GETSTRIPE -S $f)
16014                 [ $size -eq $def_stripe_size ] ||
16015                         error "$f stripe size $size != $def_stripe_size"
16016                 local pool=$($GETSTRIPE -p $f)
16017                 [ "#$pool" == "#" ] || error "$f pool $pool is set"
16018
16019         done
16020
16021         unlinkmany $DIR/$tdir/$tfile. 1 20
16022
16023         # restore FS default striping
16024         if [ -z $def_pool ]; then
16025                 $SETSTRIPE -c $def_stripenr -S $def_stripe_size \
16026                         -i $def_stripe_offset $MOUNT ||
16027                         error "restore default striping failed"
16028         else
16029                 $SETSTRIPE -c $def_stripenr -S $def_stripe_size -p $def_pool \
16030                         -i $def_stripe_offset $MOUNT ||
16031                         error "restore default striping with $def_pool failed"
16032         fi
16033
16034         local f=$DIR/$tdir/$tfile
16035         pool_remove_all_targets $test_pool $f
16036         pool_remove $test_pool $f
16037 }
16038 run_test 406 "DNE support fs default striping"
16039
16040 test_407() {
16041         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16042
16043         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
16044                 skip "Need MDS version at least 2.8.55" && return
16045
16046         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
16047                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
16048         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
16049                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
16050         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
16051
16052         #define OBD_FAIL_DT_TXN_STOP    0x2019
16053         for idx in $(seq $MDSCOUNT); do
16054                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
16055         done
16056         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
16057         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
16058                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
16059         true
16060 }
16061 run_test 407 "transaction fail should cause operation fail"
16062
16063 test_408() {
16064         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
16065
16066         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
16067         lctl set_param fail_loc=0x8000040a
16068         # let ll_prepare_partial_page() fail
16069         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
16070
16071         rm -f $DIR/$tfile
16072
16073         # create at least 100 unused inodes so that
16074         # shrink_icache_memory(0) should not return 0
16075         touch $DIR/$tfile-{0..100}
16076         rm -f $DIR/$tfile-{0..100}
16077         sync
16078
16079         echo 2 > /proc/sys/vm/drop_caches
16080 }
16081 run_test 408 "drop_caches should not hang due to page leaks"
16082
16083 test_409()
16084 {
16085         [ $MDSCOUNT -lt 2 ] &&
16086                 skip "We need at least 2 MDTs for this test" && return
16087
16088         check_mount_and_prep
16089
16090         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
16091         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
16092         touch $DIR/$tdir/guard || error "(2) Fail to create"
16093
16094         local PREFIX=$(str_repeat 'A' 128)
16095         echo "Create 1K hard links start at $(date)"
16096         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16097                 error "(3) Fail to hard link"
16098
16099         echo "Links count should be right although linkEA overflow"
16100         stat $DIR/$tdir/guard || error "(4) Fail to stat"
16101         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
16102         [ $linkcount -eq 1001 ] ||
16103                 error "(5) Unexpected hard links count: $linkcount"
16104
16105         echo "List all links start at $(date)"
16106         ls -l $DIR/$tdir/foo > /dev/null ||
16107                 error "(6) Fail to list $DIR/$tdir/foo"
16108
16109         echo "Unlink hard links start at $(date)"
16110         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16111                 error "(7) Fail to unlink"
16112 }
16113 run_test 409 "Large amount of cross-MDTs hard links on the same file"
16114
16115 prep_801() {
16116         start_full_debug_logging
16117         # cleanup unused barrier locks before test
16118         do_facet mgs $LCTL barrier_rescan $FSNAME ||
16119                 error "Fail to prep barrier test env"
16120 }
16121
16122 post_801() {
16123         stop_full_debug_logging
16124 }
16125
16126 test_801a() {
16127         prep_801
16128
16129         #define OBD_FAIL_BARRIER_DELAY          0x2202
16130         do_facet mgs $LCTL set_param fail_val=3 fail_loc=0x2202
16131         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
16132
16133         sleep 1
16134         local b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16135                                awk '/The barrier for/ { print $7 }')
16136         [ "$b_status" = "'freezing_p1'" ] ||
16137                 error "(1) unexpected barrier status $b_status"
16138
16139         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
16140         wait
16141         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16142                          awk '/The barrier for/ { print $7 }')
16143         [ "$b_status" = "'frozen'" ] ||
16144                 error "(2) unexpected barrier status $b_status"
16145
16146         local expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16147                         awk '/will be expired/ { print $7 }')
16148         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
16149         sleep $((expired + 3))
16150
16151         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16152                          awk '/The barrier for/ { print $7 }')
16153         [ "$b_status" = "'expired'" ] ||
16154                 error "(3) unexpected barrier status $b_status"
16155
16156         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
16157                 error "(4) fail to freeze barrier"
16158
16159         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16160                          awk '/The barrier for/ { print $7 }')
16161         [ "$b_status" = "'frozen'" ] ||
16162                 error "(5) unexpected barrier status $b_status"
16163
16164         #define OBD_FAIL_BARRIER_DELAY          0x2202
16165         do_facet mgs $LCTL set_param fail_val=3 fail_loc=0x2202
16166         do_facet mgs $LCTL barrier_thaw $FSNAME &
16167
16168         sleep 1
16169         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16170                          awk '/The barrier for/ { print $7 }')
16171         [ "$b_status" = "'thawing'" ] ||
16172                 error "(6) unexpected barrier status $b_status"
16173
16174         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
16175         wait
16176         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16177                          awk '/The barrier for/ { print $7 }')
16178         [ "$b_status" = "'thawed'" ] ||
16179                 error "(7) unexpected barrier status $b_status"
16180
16181         #define OBD_FAIL_BARRIER_FAILURE        0x2203
16182         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
16183         do_facet mgs $LCTL barrier_freeze $FSNAME
16184
16185         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16186                                awk '/The barrier for/ { print $7 }')
16187         [ "$b_status" = "'failed'" ] ||
16188                 error "(8) unexpected barrier status $b_status"
16189
16190         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
16191         do_facet mgs $LCTL barrier_thaw $FSNAME
16192
16193         post_801
16194 }
16195 run_test 801a "write barrier user interfaces and stat machine"
16196
16197 test_801b() {
16198         prep_801
16199
16200         mkdir $DIR/$tdir || error "(1) fail to mkdir"
16201         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
16202         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
16203         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
16204         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
16205
16206         cancel_lru_locks mdc
16207
16208         # 180 seconds should be long enough
16209         do_facet mgs $LCTL barrier_freeze $FSNAME 180
16210
16211         local b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16212                                awk '/The barrier for/ { print $7 }')
16213         [ "$b_status" = "'frozen'" ] ||
16214                 error "(6) unexpected barrier status $b_status"
16215
16216         mkdir $DIR/$tdir/d0/d10 &
16217         mkdir_pid=$!
16218
16219         touch $DIR/$tdir/d1/f13 &
16220         touch_pid=$!
16221
16222         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
16223         ln_pid=$!
16224
16225         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
16226         mv_pid=$!
16227
16228         rm -f $DIR/$tdir/d4/f12 &
16229         rm_pid=$!
16230
16231         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
16232
16233         # To guarantee taht the 'stat' is not blocked
16234         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16235                          awk '/The barrier for/ { print $7 }')
16236         [ "$b_status" = "'frozen'" ] ||
16237                 error "(8) unexpected barrier status $b_status"
16238
16239         # let above commands to run at background
16240         sleep 5
16241
16242         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
16243         ps -p $touch_pid || error "(10) touch should be blocked"
16244         ps -p $ln_pid || error "(11) link should be blocked"
16245         ps -p $mv_pid || error "(12) rename should be blocked"
16246         ps -p $rm_pid || error "(13) unlink should be blocked"
16247
16248         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16249                          awk '/The barrier for/ { print $7 }')
16250         [ "$b_status" = "'frozen'" ] ||
16251                 error "(14) unexpected barrier status $b_status"
16252
16253         do_facet mgs $LCTL barrier_thaw $FSNAME
16254         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16255                          awk '/The barrier for/ { print $7 }')
16256         [ "$b_status" = "'thawed'" ] ||
16257                 error "(15) unexpected barrier status $b_status"
16258
16259         wait $mkdir_pid || error "(16) mkdir should succeed"
16260         wait $touch_pid || error "(17) touch should succeed"
16261         wait $ln_pid || error "(18) link should succeed"
16262         wait $mv_pid || error "(19) rename should succeed"
16263         wait $rm_pid || error "(20) unlink should succeed"
16264
16265         post_801
16266 }
16267 run_test 801b "modification will be blocked by write barrier"
16268
16269 test_801c() {
16270         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
16271
16272         prep_801
16273
16274         stop mds2 || error "(1) Fail to stop mds2"
16275
16276         do_facet mgs $LCTL barrier_freeze $FSNAME 30
16277
16278         local b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16279                                awk '/The barrier for/ { print $7 }')
16280         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
16281                 do_facet mgs $LCTL barrier_thaw $FSNAME
16282                 error "(2) unexpected barrier status $b_status"
16283         }
16284
16285         do_facet mgs $LCTL barrier_rescan $FSNAME ||
16286                 error "(3) Fail to rescan barrier bitmap"
16287
16288         do_facet mgs $LCTL barrier_freeze $FSNAME 10
16289
16290         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16291                          awk '/The barrier for/ { print $7 }')
16292         [ "$b_status" = "'frozen'" ] ||
16293                 error "(4) unexpected barrier status $b_status"
16294
16295         do_facet mgs $LCTL barrier_thaw $FSNAME
16296         b_status=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16297                          awk '/The barrier for/ { print $7 }')
16298         [ "$b_status" = "'thawed'" ] ||
16299                 error "(5) unexpected barrier status $b_status"
16300
16301         local devname=$(mdsdevname 2)
16302
16303         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
16304
16305         do_facet mgs $LCTL barrier_rescan $FSNAME ||
16306                 error "(7) Fail to rescan barrier bitmap"
16307
16308         post_801
16309 }
16310 run_test 801c "rescan barrier bitmap"
16311
16312 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
16313 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
16314 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
16315
16316 cleanup_802() {
16317         trap 0
16318
16319         stopall
16320         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
16321         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
16322         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
16323         setupall
16324 }
16325
16326 test_802() {
16327         mkdir $DIR/$tdir || error "(1) fail to mkdir"
16328
16329         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
16330                 error "(2) Fail to copy"
16331
16332         trap cleanup_802 EXIT
16333
16334         # sync by force before remount as readonly
16335         sync; sync_all_data; sleep 3; sync_all_data
16336
16337         stopall
16338
16339         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
16340         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
16341         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
16342
16343         echo "Mount the server as read only"
16344         setupall server_only || error "(3) Fail to start servers"
16345
16346         echo "Mount client without ro should fail"
16347         mount_client $MOUNT &&
16348                 error "(4) Mount client without 'ro' should fail"
16349
16350         echo "Mount client with ro should succeed"
16351         mount_client $MOUNT ro ||
16352                 error "(5) Mount client with 'ro' should succeed"
16353
16354         echo "Modify should be refused"
16355         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
16356
16357         echo "Read should be allowed"
16358         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
16359                 error "(7) Read should succeed under ro mode"
16360
16361         cleanup_802
16362 }
16363 run_test 802 "simulate readonly device"
16364
16365 #
16366 # tests that do cleanup/setup should be run at the end
16367 #
16368
16369 test_900() {
16370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16371         local ls
16372         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
16373         $LCTL set_param fail_loc=0x903
16374
16375         cancel_lru_locks MGC
16376
16377         FAIL_ON_ERROR=true cleanup
16378         FAIL_ON_ERROR=true setup
16379 }
16380 run_test 900 "umount should not race with any mgc requeue thread"
16381
16382 complete $SECONDS
16383 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
16384 check_and_cleanup_lustre
16385 if [ "$I_MOUNTED" != "yes" ]; then
16386         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
16387 fi
16388 exit_status