Whamcloud - gitweb
41754a44148a32baaac9a14493a71c271699f5be
[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-8139 LU-8411
17 ALWAYS_EXCEPT="                 76      101g    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                 else
1913                         #
1914                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1915                         # into f_ver in this case.  See the comment on
1916                         # ff_parent.
1917                         #
1918                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1919                                 sed -e 's/\]//')
1920                 fi
1921
1922                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1923                 [ $ff_pseq = $lmm_seq ] ||
1924                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1925                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1926                 [ $ff_poid = $lmm_oid ] ||
1927                         error "FF parent OID $ff_poid != $lmm_oid"
1928                 (($ff_pstripe == $stripe_nr)) ||
1929                         error "FF stripe $ff_pstripe != $stripe_nr"
1930
1931                 stripe_nr=$((stripe_nr + 1))
1932         done
1933 }
1934
1935 test_27z() {
1936         remote_ost_nodsh && skip "remote OST with nodsh" && return
1937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1938         test_mkdir -p $DIR/$tdir
1939
1940         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1941                 { error "setstripe -c -1 failed"; return 1; }
1942         # We need to send a write to every object to get parent FID info set.
1943         # This _should_ also work for setattr, but does not currently.
1944         # touch $DIR/$tdir/$tfile-1 ||
1945         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1946                 { error "dd $tfile-1 failed"; return 2; }
1947         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1948                 { error "setstripe -c -1 failed"; return 3; }
1949         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1950                 { error "dd $tfile-2 failed"; return 4; }
1951
1952         # make sure write RPCs have been sent to OSTs
1953         sync; sleep 5; sync
1954
1955         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1956         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1957 }
1958 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1959
1960 test_27A() { # b=19102
1961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1962         local restore_size=$($GETSTRIPE -S $MOUNT)
1963         local restore_count=$($GETSTRIPE -c $MOUNT)
1964         local restore_offset=$($GETSTRIPE -i $MOUNT)
1965         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1966         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1967                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1968         local default_size=$($GETSTRIPE -S $MOUNT)
1969         local default_offset=$($GETSTRIPE -i $MOUNT)
1970         local dsize=$((1024 * 1024))
1971         [ $default_size -eq $dsize ] ||
1972                 error "stripe size $default_size != $dsize"
1973         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1974         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1975 }
1976 run_test 27A "check filesystem-wide default LOV EA values"
1977
1978 test_27B() { # LU-2523
1979         test_mkdir -p $DIR/$tdir
1980         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1981         touch $DIR/$tdir/f0
1982         # open f1 with O_LOV_DELAY_CREATE
1983         # rename f0 onto f1
1984         # call setstripe ioctl on open file descriptor for f1
1985         # close
1986         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1987                 $DIR/$tdir/f0
1988
1989         rm -f $DIR/$tdir/f1
1990         # open f1 with O_LOV_DELAY_CREATE
1991         # unlink f1
1992         # call setstripe ioctl on open file descriptor for f1
1993         # close
1994         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1995
1996         # Allow multiop to fail in imitation of NFS's busted semantics.
1997         true
1998 }
1999 run_test 27B "call setstripe on open unlinked file/rename victim"
2000
2001 test_27C() { #LU-2871
2002         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
2003
2004         declare -a ost_idx
2005         local index
2006         local found
2007         local i
2008         local j
2009
2010         test_mkdir -p $DIR/$tdir
2011         cd $DIR/$tdir
2012         for i in $(seq 0 $((OSTCOUNT - 1))); do
2013                 # set stripe across all OSTs starting from OST$i
2014                 $SETSTRIPE -i $i -c -1 $tfile$i
2015                 # get striping information
2016                 ost_idx=($($GETSTRIPE $tfile$i |
2017                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2018                 echo ${ost_idx[@]}
2019
2020                 # check the layout
2021                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2022                         error "${#ost_idx[@]} != $OSTCOUNT"
2023
2024                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2025                         found=0
2026                         for j in $(echo ${ost_idx[@]}); do
2027                                 if [ $index -eq $j ]; then
2028                                         found=1
2029                                         break
2030                                 fi
2031                         done
2032                         [ $found = 1 ] ||
2033                                 error "Can not find $index in ${ost_idx[@]}"
2034                 done
2035         done
2036 }
2037 run_test 27C "check full striping across all OSTs"
2038
2039 test_27D() {
2040         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2041         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2042         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2043         local POOL=${POOL:-testpool}
2044         local first_ost=0
2045         local last_ost=$(($OSTCOUNT - 1))
2046         local ost_step=1
2047         local ost_list=$(seq $first_ost $ost_step $last_ost)
2048         local ost_range="$first_ost $last_ost $ost_step"
2049
2050         test_mkdir -p $DIR/$tdir
2051         pool_add $POOL || error "pool_add failed"
2052         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2053
2054         local skip27D
2055         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
2056                 skip27D = "-s 29"
2057         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2058                 error "llapi_layout_test failed"
2059
2060         cleanup_pools || error "cleanup_pools failed"
2061 }
2062 run_test 27D "validate llapi_layout API"
2063
2064 # Verify that default_easize is increased from its initial value after
2065 # accessing a widely striped file.
2066 test_27E() {
2067         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2068         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2069                 skip "client does not have LU-3338 fix" && return
2070
2071         # 72 bytes is the minimum space required to store striping
2072         # information for a file striped across one OST:
2073         # (sizeof(struct lov_user_md_v3) +
2074         #  sizeof(struct lov_user_ost_data_v1))
2075         local min_easize=72
2076         $LCTL set_param -n llite.*.default_easize $min_easize ||
2077                 error "lctl set_param failed"
2078         local easize=$($LCTL get_param -n llite.*.default_easize)
2079
2080         [ $easize -eq $min_easize ] ||
2081                 error "failed to set default_easize"
2082
2083         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2084                 error "setstripe failed"
2085         cat $DIR/$tfile
2086         rm $DIR/$tfile
2087
2088         easize=$($LCTL get_param -n llite.*.default_easize)
2089
2090         [ $easize -gt $min_easize ] ||
2091                 error "default_easize not updated"
2092 }
2093 run_test 27E "check that default extended attribute size properly increases"
2094
2095 test_27F() { # LU-5346/LU-7975
2096
2097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2098
2099         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2100                 skip "Need MDS version at least 2.8.51" && return
2101
2102         test_mkdir -p $DIR/$tdir
2103         rm -f $DIR/$tdir/f0
2104         $SETSTRIPE -c 2 $DIR/$tdir
2105
2106         # stop all OSTs to reproduce situation for LU-7975 ticket
2107         for num in $(seq $OSTCOUNT); do
2108                 stop ost$num
2109         done
2110
2111         # open/create f0 with O_LOV_DELAY_CREATE
2112         # truncate f0 to a non-0 size
2113         # close
2114         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2115
2116         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2117         # open/write it again to force delayed layout creation
2118         cat /etc/hosts > $DIR/$tdir/f0 &
2119         catpid=$!
2120
2121         # restart OSTs
2122         for num in $(seq $OSTCOUNT); do
2123                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2124                         error "ost$num failed to start"
2125         done
2126
2127         wait $catpid || error "cat failed"
2128
2129         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2130         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2131
2132 }
2133 run_test 27F "Client resend delayed layout creation with non-zero size"
2134
2135 # createtest also checks that device nodes are created and
2136 # then visible correctly (#2091)
2137 test_28() { # bug 2091
2138         test_mkdir $DIR/d28
2139         $CREATETEST $DIR/d28/ct || error
2140 }
2141 run_test 28 "create/mknod/mkdir with bad file types ============"
2142
2143 test_29() {
2144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
2145         sync; sleep 1; sync # flush out any dirty pages from previous tests
2146         cancel_lru_locks
2147         test_mkdir $DIR/d29
2148         touch $DIR/d29/foo
2149         log 'first d29'
2150         ls -l $DIR/d29
2151
2152         declare -i LOCKCOUNTORIG=0
2153         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2154                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2155         done
2156         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2157
2158         declare -i LOCKUNUSEDCOUNTORIG=0
2159         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2160                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2161         done
2162
2163         log 'second d29'
2164         ls -l $DIR/d29
2165         log 'done'
2166
2167         declare -i LOCKCOUNTCURRENT=0
2168         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2169                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2170         done
2171
2172         declare -i LOCKUNUSEDCOUNTCURRENT=0
2173         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2174                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2175         done
2176
2177         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2178                 $LCTL set_param -n ldlm.dump_namespaces ""
2179                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2180                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2181                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2182                 return 2
2183         fi
2184         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2185                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2186                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2187                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2188                 return 3
2189         fi
2190 }
2191 run_test 29 "IT_GETATTR regression  ============================"
2192
2193 test_30a() { # was test_30
2194         cp $(which ls) $DIR || cp /bin/ls $DIR
2195         $DIR/ls / || error
2196         rm $DIR/ls
2197 }
2198 run_test 30a "execute binary from Lustre (execve) =============="
2199
2200 test_30b() {
2201         cp `which ls` $DIR || cp /bin/ls $DIR
2202         chmod go+rx $DIR/ls
2203         $RUNAS $DIR/ls / || error
2204         rm $DIR/ls
2205 }
2206 run_test 30b "execute binary from Lustre as non-root ==========="
2207
2208 test_30c() { # b=22376
2209         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2210         cp `which ls` $DIR || cp /bin/ls $DIR
2211         chmod a-rw $DIR/ls
2212         cancel_lru_locks mdc
2213         cancel_lru_locks osc
2214         $RUNAS $DIR/ls / || error
2215         rm -f $DIR/ls
2216 }
2217 run_test 30c "execute binary from Lustre without read perms ===="
2218
2219 test_31a() {
2220         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2221         $CHECKSTAT -a $DIR/f31 || error
2222 }
2223 run_test 31a "open-unlink file =================================="
2224
2225 test_31b() {
2226         touch $DIR/f31 || error
2227         ln $DIR/f31 $DIR/f31b || error
2228         $MULTIOP $DIR/f31b Ouc || error
2229         $CHECKSTAT -t file $DIR/f31 || error
2230 }
2231 run_test 31b "unlink file with multiple links while open ======="
2232
2233 test_31c() {
2234         touch $DIR/f31 || error
2235         ln $DIR/f31 $DIR/f31c || error
2236         multiop_bg_pause $DIR/f31 O_uc || return 1
2237         MULTIPID=$!
2238         $MULTIOP $DIR/f31c Ouc
2239         kill -USR1 $MULTIPID
2240         wait $MULTIPID
2241 }
2242 run_test 31c "open-unlink file with multiple links ============="
2243
2244 test_31d() {
2245         opendirunlink $DIR/d31d $DIR/d31d || error
2246         $CHECKSTAT -a $DIR/d31d || error
2247 }
2248 run_test 31d "remove of open directory ========================="
2249
2250 test_31e() { # bug 2904
2251         openfilleddirunlink $DIR/d31e || error
2252 }
2253 run_test 31e "remove of open non-empty directory ==============="
2254
2255 test_31f() { # bug 4554
2256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2257         set -vx
2258         test_mkdir $DIR/d31f
2259         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2260         cp /etc/hosts $DIR/d31f
2261         ls -l $DIR/d31f
2262         $GETSTRIPE $DIR/d31f/hosts
2263         multiop_bg_pause $DIR/d31f D_c || return 1
2264         MULTIPID=$!
2265
2266         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2267         test_mkdir $DIR/d31f
2268         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2269         cp /etc/hosts $DIR/d31f
2270         ls -l $DIR/d31f
2271         $GETSTRIPE $DIR/d31f/hosts
2272         multiop_bg_pause $DIR/d31f D_c || return 1
2273         MULTIPID2=$!
2274
2275         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2276         wait $MULTIPID || error "first opendir $MULTIPID failed"
2277
2278         sleep 6
2279
2280         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2281         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2282         set +vx
2283 }
2284 run_test 31f "remove of open directory with open-unlink file ==="
2285
2286 test_31g() {
2287         echo "-- cross directory link --"
2288         test_mkdir -c1 $DIR/${tdir}ga
2289         test_mkdir -c1 $DIR/${tdir}gb
2290         touch $DIR/${tdir}ga/f
2291         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2292         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2293         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2294         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2295         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2296 }
2297 run_test 31g "cross directory link==============="
2298
2299 test_31h() {
2300         echo "-- cross directory link --"
2301         test_mkdir -c1 $DIR/${tdir}
2302         test_mkdir -c1 $DIR/${tdir}/dir
2303         touch $DIR/${tdir}/f
2304         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2305         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2306         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2307         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2308         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2309 }
2310 run_test 31h "cross directory link under child==============="
2311
2312 test_31i() {
2313         echo "-- cross directory link --"
2314         test_mkdir -c1 $DIR/$tdir
2315         test_mkdir -c1 $DIR/$tdir/dir
2316         touch $DIR/$tdir/dir/f
2317         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2318         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2319         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2320         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2321         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2322 }
2323 run_test 31i "cross directory link under parent==============="
2324
2325 test_31j() {
2326         test_mkdir -c1 -p $DIR/$tdir
2327         test_mkdir -c1 -p $DIR/$tdir/dir1
2328         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2329         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2330         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2331         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2332         return 0
2333 }
2334 run_test 31j "link for directory==============="
2335
2336 test_31k() {
2337         test_mkdir -c1 -p $DIR/$tdir
2338         touch $DIR/$tdir/s
2339         touch $DIR/$tdir/exist
2340         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2341         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2342         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2343         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2344         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2345         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2346         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2347         return 0
2348 }
2349 run_test 31k "link to file: the same, non-existing, dir==============="
2350
2351 test_31m() {
2352         mkdir $DIR/d31m
2353         touch $DIR/d31m/s
2354         mkdir $DIR/d31m2
2355         touch $DIR/d31m2/exist
2356         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2357         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2358         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2359         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2360         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2361         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2362         return 0
2363 }
2364 run_test 31m "link to file: the same, non-existing, dir==============="
2365
2366 test_31n() {
2367         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2368         nlink=$(stat --format=%h $DIR/$tfile)
2369         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2370         local fd=$(free_fd)
2371         local cmd="exec $fd<$DIR/$tfile"
2372         eval $cmd
2373         cmd="exec $fd<&-"
2374         trap "eval $cmd" EXIT
2375         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2376         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2377         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2378         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2379         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2380         eval $cmd
2381 }
2382 run_test 31n "check link count of unlinked file"
2383
2384 link_one() {
2385         local TEMPNAME=$(mktemp $1_XXXXXX)
2386         mlink $TEMPNAME $1 2> /dev/null &&
2387                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2388         munlink $TEMPNAME
2389 }
2390
2391 test_31o() { # LU-2901
2392         test_mkdir -p $DIR/$tdir
2393         for LOOP in $(seq 100); do
2394                 rm -f $DIR/$tdir/$tfile*
2395                 for THREAD in $(seq 8); do
2396                         link_one $DIR/$tdir/$tfile.$LOOP &
2397                 done
2398                 wait
2399                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2400                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2401                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2402                         break || true
2403         done
2404 }
2405 run_test 31o "duplicate hard links with same filename"
2406
2407 test_31p() {
2408         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2409
2410         test_mkdir $DIR/$tdir
2411         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2412         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2413
2414         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2415                 error "open unlink test1 failed"
2416         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2417                 error "open unlink test2 failed"
2418
2419         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2420                 error "test1 still exists"
2421         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2422                 error "test2 still exists"
2423 }
2424 run_test 31p "remove of open striped directory"
2425
2426 cleanup_test32_mount() {
2427         local rc=0
2428         trap 0
2429         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2430         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2431         losetup -d $loopdev || true
2432         rm -rf $DIR/$tdir
2433         return $rc
2434 }
2435
2436 test_32a() {
2437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2438         echo "== more mountpoints and symlinks ================="
2439         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2440         trap cleanup_test32_mount EXIT
2441         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2442         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2443         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2444         cleanup_test32_mount
2445 }
2446 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2447
2448 test_32b() {
2449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2450         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2451         trap cleanup_test32_mount EXIT
2452         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2453         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2454         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2455         cleanup_test32_mount
2456 }
2457 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2458
2459 test_32c() {
2460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2461         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2462         trap cleanup_test32_mount EXIT
2463         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2464         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2465         test_mkdir -p $DIR/$tdir/d2/test_dir
2466         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2467         cleanup_test32_mount
2468 }
2469 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2470
2471 test_32d() {
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         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2479         cleanup_test32_mount
2480 }
2481 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2482
2483 test_32e() {
2484         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2485         test_mkdir -p $DIR/d32e/tmp
2486         TMP_DIR=$DIR/d32e/tmp
2487         ln -s $DIR/d32e $TMP_DIR/symlink11
2488         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2489         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2490         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2491 }
2492 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2493
2494 test_32f() {
2495         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2496         test_mkdir -p $DIR/d32f/tmp
2497         TMP_DIR=$DIR/d32f/tmp
2498         ln -s $DIR/d32f $TMP_DIR/symlink11
2499         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2500         ls $DIR/d32f/tmp/symlink11  || error
2501         ls $DIR/d32f/symlink01 || error
2502 }
2503 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2504
2505 test_32g() {
2506         TMP_DIR=$DIR/$tdir/tmp
2507         test_mkdir -p $DIR/$tdir/tmp
2508         test_mkdir $DIR/${tdir}2
2509         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2510         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2511         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2512         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2513         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2514         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2515 }
2516 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2517
2518 test_32h() {
2519         rm -fr $DIR/$tdir $DIR/${tdir}2
2520         TMP_DIR=$DIR/$tdir/tmp
2521         test_mkdir -p $DIR/$tdir/tmp
2522         test_mkdir $DIR/${tdir}2
2523         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2524         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2525         ls $TMP_DIR/symlink12 || error
2526         ls $DIR/$tdir/symlink02  || error
2527 }
2528 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2529
2530 test_32i() {
2531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2532         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2533         trap cleanup_test32_mount EXIT
2534         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2535         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2536         touch $DIR/$tdir/test_file
2537         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2538         cleanup_test32_mount
2539 }
2540 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2541
2542 test_32j() {
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         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2550         cleanup_test32_mount
2551 }
2552 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2553
2554 test_32k() {
2555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2556         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
2560         test_mkdir -p $DIR/$tdir/d2
2561         touch $DIR/$tdir/d2/test_file || error
2562         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2563         cleanup_test32_mount
2564 }
2565 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2566
2567 test_32l() {
2568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2569         rm -fr $DIR/$tdir
2570         trap cleanup_test32_mount EXIT
2571         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2572         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2573         test_mkdir -p $DIR/$tdir/d2
2574         touch $DIR/$tdir/d2/test_file
2575         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2576         cleanup_test32_mount
2577 }
2578 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2579
2580 test_32m() {
2581         rm -fr $DIR/d32m
2582         test_mkdir -p $DIR/d32m/tmp
2583         TMP_DIR=$DIR/d32m/tmp
2584         ln -s $DIR $TMP_DIR/symlink11
2585         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2586         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2587         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2588 }
2589 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2590
2591 test_32n() {
2592         rm -fr $DIR/d32n
2593         test_mkdir -p $DIR/d32n/tmp
2594         TMP_DIR=$DIR/d32n/tmp
2595         ln -s $DIR $TMP_DIR/symlink11
2596         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2597         ls -l $DIR/d32n/tmp/symlink11  || error
2598         ls -l $DIR/d32n/symlink01 || error
2599 }
2600 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2601
2602 test_32o() {
2603         touch $DIR/$tfile
2604         test_mkdir -p $DIR/d32o/tmp
2605         TMP_DIR=$DIR/d32o/tmp
2606         ln -s $DIR/$tfile $TMP_DIR/symlink12
2607         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2608         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2609         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2610         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2611         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2612 }
2613 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2614
2615 test_32p() {
2616         log 32p_1
2617         rm -fr $DIR/d32p
2618         log 32p_2
2619         rm -f $DIR/$tfile
2620         log 32p_3
2621         touch $DIR/$tfile
2622         log 32p_4
2623         test_mkdir -p $DIR/d32p/tmp
2624         log 32p_5
2625         TMP_DIR=$DIR/d32p/tmp
2626         log 32p_6
2627         ln -s $DIR/$tfile $TMP_DIR/symlink12
2628         log 32p_7
2629         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2630         log 32p_8
2631         cat $DIR/d32p/tmp/symlink12 || error
2632         log 32p_9
2633         cat $DIR/d32p/symlink02 || error
2634         log 32p_10
2635 }
2636 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2637
2638 test_32q() {
2639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2640         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2641         trap cleanup_test32_mount EXIT
2642         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2643         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2644         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2645         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2646         cleanup_test32_mount
2647 }
2648 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2649
2650 test_32r() {
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 -q under_the_mount && error || true
2658         cleanup_test32_mount
2659 }
2660 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2661
2662 test_33aa() {
2663         rm -f $DIR/$tfile
2664         touch $DIR/$tfile
2665         chmod 444 $DIR/$tfile
2666         chown $RUNAS_ID $DIR/$tfile
2667         log 33_1
2668         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2669         log 33_2
2670 }
2671 run_test 33aa "write file with mode 444 (should return error) ===="
2672
2673 test_33a() {
2674         rm -fr $DIR/d33
2675         test_mkdir -p $DIR/d33
2676         chown $RUNAS_ID $DIR/d33
2677         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2678         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2679                 error "open RDWR" || true
2680 }
2681 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2682
2683 test_33b() {
2684         rm -fr $DIR/d33
2685         test_mkdir -p $DIR/d33
2686         chown $RUNAS_ID $DIR/d33
2687         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2688 }
2689 run_test 33b "test open file with malformed flags (No panic)"
2690
2691 test_33c() {
2692         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2693         local ostnum
2694         local ostname
2695         local write_bytes
2696         local all_zeros
2697
2698         remote_ost_nodsh && skip "remote OST with nodsh" && return
2699         all_zeros=:
2700         rm -fr $DIR/d33
2701         test_mkdir -p $DIR/d33
2702         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2703
2704         sync
2705         for ostnum in $(seq $OSTCOUNT); do
2706                 # test-framework's OST numbering is one-based, while Lustre's
2707                 # is zero-based
2708                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2709                 # Parsing llobdstat's output sucks; we could grep the /proc
2710                 # path, but that's likely to not be as portable as using the
2711                 # llobdstat utility.  So we parse lctl output instead.
2712                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2713                         obdfilter/$ostname/stats |
2714                         awk '/^write_bytes/ {print $7}' )
2715                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2716                 if (( ${write_bytes:-0} > 0 ))
2717                 then
2718                         all_zeros=false
2719                         break;
2720                 fi
2721         done
2722
2723         $all_zeros || return 0
2724
2725         # Write four bytes
2726         echo foo > $DIR/d33/bar
2727         # Really write them
2728         sync
2729
2730         # Total up write_bytes after writing.  We'd better find non-zeros.
2731         for ostnum in $(seq $OSTCOUNT); do
2732                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2733                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2734                         obdfilter/$ostname/stats |
2735                         awk '/^write_bytes/ {print $7}' )
2736                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2737                 if (( ${write_bytes:-0} > 0 ))
2738                 then
2739                         all_zeros=false
2740                         break;
2741                 fi
2742         done
2743
2744         if $all_zeros
2745         then
2746                 for ostnum in $(seq $OSTCOUNT); do
2747                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2748                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2749                         do_facet ost$ostnum lctl get_param -n \
2750                                 obdfilter/$ostname/stats
2751                 done
2752                 error "OST not keeping write_bytes stats (b22312)"
2753         fi
2754 }
2755 run_test 33c "test llobdstat and write_bytes"
2756
2757 test_33d() {
2758         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2759         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2760         local MDTIDX=1
2761         local remote_dir=$DIR/$tdir/remote_dir
2762
2763         test_mkdir -p $DIR/$tdir
2764         $LFS mkdir -i $MDTIDX $remote_dir ||
2765                 error "create remote directory failed"
2766
2767         touch $remote_dir/$tfile
2768         chmod 444 $remote_dir/$tfile
2769         chown $RUNAS_ID $remote_dir/$tfile
2770
2771         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2772
2773         chown $RUNAS_ID $remote_dir
2774         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2775                                         error "create" || true
2776         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2777                                     error "open RDWR" || true
2778         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2779 }
2780 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2781
2782 test_33e() {
2783         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2784
2785         mkdir $DIR/$tdir
2786
2787         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2788         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2789         mkdir $DIR/$tdir/local_dir
2790
2791         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2792         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2793         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2794
2795         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2796                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2797
2798         rmdir $DIR/$tdir/* || error "rmdir failed"
2799
2800         umask 777
2801         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2802         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2803         mkdir $DIR/$tdir/local_dir
2804
2805         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2806         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2807         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2808
2809         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2810                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2811
2812         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2813
2814         umask 000
2815         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2816         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2817         mkdir $DIR/$tdir/local_dir
2818
2819         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2820         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2821         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2822
2823         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2824                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2825 }
2826 run_test 33e "mkdir and striped directory should have same mode"
2827
2828 cleanup_33f() {
2829         trap 0
2830         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2831 }
2832
2833 test_33f() {
2834         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2835         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2836
2837         mkdir $DIR/$tdir
2838         chmod go+rwx $DIR/$tdir
2839         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2840         trap cleanup_33f EXIT
2841
2842         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2843                 error "cannot create striped directory"
2844
2845         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2846                 error "cannot create files in striped directory"
2847
2848         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2849                 error "cannot remove files in striped directory"
2850
2851         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2852                 error "cannot remove striped directory"
2853
2854         cleanup_33f
2855 }
2856 run_test 33f "nonroot user can create, access, and remove a striped directory"
2857
2858 test_33g() {
2859         mkdir -p $DIR/$tdir/dir2
2860
2861         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
2862         echo $err
2863         [[ $err =~ "exists" ]] || error "Not exists error"
2864 }
2865 run_test 33g "nonroot user create already existing root created file"
2866
2867 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2868 test_34a() {
2869         rm -f $DIR/f34
2870         $MCREATE $DIR/f34 || error
2871         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2872         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2873         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2874         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2875 }
2876 run_test 34a "truncate file that has not been opened ==========="
2877
2878 test_34b() {
2879         [ ! -f $DIR/f34 ] && test_34a
2880         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2881         $OPENFILE -f O_RDONLY $DIR/f34
2882         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2883         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2884 }
2885 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2886
2887 test_34c() {
2888         [ ! -f $DIR/f34 ] && test_34a
2889         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2890         $OPENFILE -f O_RDWR $DIR/f34
2891         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2892         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2893 }
2894 run_test 34c "O_RDWR opening file-with-size works =============="
2895
2896 test_34d() {
2897         [ ! -f $DIR/f34 ] && test_34a
2898         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2899         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2900         rm $DIR/f34
2901 }
2902 run_test 34d "write to sparse file ============================="
2903
2904 test_34e() {
2905         rm -f $DIR/f34e
2906         $MCREATE $DIR/f34e || error
2907         $TRUNCATE $DIR/f34e 1000 || error
2908         $CHECKSTAT -s 1000 $DIR/f34e || error
2909         $OPENFILE -f O_RDWR $DIR/f34e
2910         $CHECKSTAT -s 1000 $DIR/f34e || error
2911 }
2912 run_test 34e "create objects, some with size and some without =="
2913
2914 test_34f() { # bug 6242, 6243
2915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2916         SIZE34F=48000
2917         rm -f $DIR/f34f
2918         $MCREATE $DIR/f34f || error
2919         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2920         dd if=$DIR/f34f of=$TMP/f34f
2921         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2922         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2923         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2924         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2925         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2926 }
2927 run_test 34f "read from a file with no objects until EOF ======="
2928
2929 test_34g() {
2930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2931         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2932         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2933         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2934         cancel_lru_locks osc
2935         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2936                 error "wrong size after lock cancel"
2937
2938         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2939         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2940                 error "expanding truncate failed"
2941         cancel_lru_locks osc
2942         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2943                 error "wrong expanded size after lock cancel"
2944 }
2945 run_test 34g "truncate long file ==============================="
2946
2947 test_34h() {
2948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2949         local gid=10
2950         local sz=1000
2951
2952         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2953         sync # Flush the cache so that multiop below does not block on cache
2954              # flush when getting the group lock
2955         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2956         MULTIPID=$!
2957
2958         # Since just timed wait is not good enough, let's do a sync write
2959         # that way we are sure enough time for a roundtrip + processing
2960         # passed + 2 seconds of extra margin.
2961         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2962         rm $DIR/${tfile}-1
2963         sleep 2
2964
2965         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2966                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2967                 kill -9 $MULTIPID
2968         fi
2969         wait $MULTIPID
2970         local nsz=`stat -c %s $DIR/$tfile`
2971         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2972 }
2973 run_test 34h "ftruncate file under grouplock should not block"
2974
2975 test_35a() {
2976         cp /bin/sh $DIR/f35a
2977         chmod 444 $DIR/f35a
2978         chown $RUNAS_ID $DIR/f35a
2979         $RUNAS $DIR/f35a && error || true
2980         rm $DIR/f35a
2981 }
2982 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2983
2984 test_36a() {
2985         rm -f $DIR/f36
2986         utime $DIR/f36 || error
2987 }
2988 run_test 36a "MDS utime check (mknod, utime) ==================="
2989
2990 test_36b() {
2991         echo "" > $DIR/f36
2992         utime $DIR/f36 || error
2993 }
2994 run_test 36b "OST utime check (open, utime) ===================="
2995
2996 test_36c() {
2997         rm -f $DIR/d36/f36
2998         test_mkdir $DIR/d36
2999         chown $RUNAS_ID $DIR/d36
3000         $RUNAS utime $DIR/d36/f36 || error
3001 }
3002 run_test 36c "non-root MDS utime check (mknod, utime) =========="
3003
3004 test_36d() {
3005         [ ! -d $DIR/d36 ] && test_36c
3006         echo "" > $DIR/d36/f36
3007         $RUNAS utime $DIR/d36/f36 || error
3008 }
3009 run_test 36d "non-root OST utime check (open, utime) ==========="
3010
3011 test_36e() {
3012         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3013         test_mkdir -p $DIR/$tdir
3014         touch $DIR/$tdir/$tfile
3015         $RUNAS utime $DIR/$tdir/$tfile && \
3016                 error "utime worked, expected failure" || true
3017 }
3018 run_test 36e "utime on non-owned file (should return error) ===="
3019
3020 subr_36fh() {
3021         local fl="$1"
3022         local LANG_SAVE=$LANG
3023         local LC_LANG_SAVE=$LC_LANG
3024         export LANG=C LC_LANG=C # for date language
3025
3026         DATESTR="Dec 20  2000"
3027         test_mkdir -p $DIR/$tdir
3028         lctl set_param fail_loc=$fl
3029         date; date +%s
3030         cp /etc/hosts $DIR/$tdir/$tfile
3031         sync & # write RPC generated with "current" inode timestamp, but delayed
3032         sleep 1
3033         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3034         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3035         cancel_lru_locks osc
3036         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3037         date; date +%s
3038         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3039                 echo "BEFORE: $LS_BEFORE" && \
3040                 echo "AFTER : $LS_AFTER" && \
3041                 echo "WANT  : $DATESTR" && \
3042                 error "$DIR/$tdir/$tfile timestamps changed" || true
3043
3044         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3045 }
3046
3047 test_36f() {
3048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3049         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3050         subr_36fh "0x80000214"
3051 }
3052 run_test 36f "utime on file racing with OST BRW write =========="
3053
3054 test_36g() {
3055         remote_ost_nodsh && skip "remote OST with nodsh" && return
3056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3057         local fmd_max_age
3058         local fmd_before
3059         local fmd_after
3060
3061         test_mkdir -p $DIR/$tdir
3062         fmd_max_age=$(do_facet ost1 \
3063                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3064                 head -n 1")
3065
3066         fmd_before=$(do_facet ost1 \
3067                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3068         touch $DIR/$tdir/$tfile
3069         sleep $((fmd_max_age + 12))
3070         fmd_after=$(do_facet ost1 \
3071                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3072
3073         echo "fmd_before: $fmd_before"
3074         echo "fmd_after: $fmd_after"
3075         [[ $fmd_after -gt $fmd_before ]] &&
3076                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3077                 error "fmd didn't expire after ping" || true
3078 }
3079 run_test 36g "filter mod data cache expiry ====================="
3080
3081 test_36h() {
3082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3083         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3084         subr_36fh "0x80000227"
3085 }
3086 run_test 36h "utime on file racing with OST BRW write =========="
3087
3088 test_36i() {
3089         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3090
3091         test_mkdir $DIR/$tdir
3092         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3093
3094         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3095         local new_mtime=$((mtime + 200))
3096
3097         #change Modify time of striped dir
3098         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3099                         error "change mtime failed"
3100
3101         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3102
3103         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3104 }
3105 run_test 36i "change mtime on striped directory"
3106
3107 # test_37 - duplicate with tests 32q 32r
3108
3109 test_38() {
3110         local file=$DIR/$tfile
3111         touch $file
3112         openfile -f O_DIRECTORY $file
3113         local RC=$?
3114         local ENOTDIR=20
3115         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3116         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3117 }
3118 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3119
3120 test_39a() { # was test_39
3121         touch $DIR/$tfile
3122         touch $DIR/${tfile}2
3123 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3124 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3125 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3126         sleep 2
3127         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3128         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3129                 echo "mtime"
3130                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3131                 echo "atime"
3132                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3133                 echo "ctime"
3134                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3135                 error "O_TRUNC didn't change timestamps"
3136         fi
3137 }
3138 run_test 39a "mtime changed on create ==========================="
3139
3140 test_39b() {
3141         test_mkdir -p -c1 $DIR/$tdir
3142         cp -p /etc/passwd $DIR/$tdir/fopen
3143         cp -p /etc/passwd $DIR/$tdir/flink
3144         cp -p /etc/passwd $DIR/$tdir/funlink
3145         cp -p /etc/passwd $DIR/$tdir/frename
3146         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3147
3148         sleep 1
3149         echo "aaaaaa" >> $DIR/$tdir/fopen
3150         echo "aaaaaa" >> $DIR/$tdir/flink
3151         echo "aaaaaa" >> $DIR/$tdir/funlink
3152         echo "aaaaaa" >> $DIR/$tdir/frename
3153
3154         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3155         local link_new=`stat -c %Y $DIR/$tdir/flink`
3156         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3157         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3158
3159         cat $DIR/$tdir/fopen > /dev/null
3160         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3161         rm -f $DIR/$tdir/funlink2
3162         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3163
3164         for (( i=0; i < 2; i++ )) ; do
3165                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3166                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3167                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3168                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3169
3170                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3171                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3172                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3173                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3174
3175                 cancel_lru_locks osc
3176                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3177         done
3178 }
3179 run_test 39b "mtime change on open, link, unlink, rename  ======"
3180
3181 # this should be set to past
3182 TEST_39_MTIME=`date -d "1 year ago" +%s`
3183
3184 # bug 11063
3185 test_39c() {
3186         touch $DIR1/$tfile
3187         sleep 2
3188         local mtime0=`stat -c %Y $DIR1/$tfile`
3189
3190         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3191         local mtime1=`stat -c %Y $DIR1/$tfile`
3192         [ "$mtime1" = $TEST_39_MTIME ] || \
3193                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3194
3195         local d1=`date +%s`
3196         echo hello >> $DIR1/$tfile
3197         local d2=`date +%s`
3198         local mtime2=`stat -c %Y $DIR1/$tfile`
3199         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3200                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3201
3202         mv $DIR1/$tfile $DIR1/$tfile-1
3203
3204         for (( i=0; i < 2; i++ )) ; do
3205                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3206                 [ "$mtime2" = "$mtime3" ] || \
3207                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3208
3209                 cancel_lru_locks osc
3210                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3211         done
3212 }
3213 run_test 39c "mtime change on rename ==========================="
3214
3215 # bug 21114
3216 test_39d() {
3217         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3218         touch $DIR1/$tfile
3219
3220         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3221
3222         for (( i=0; i < 2; i++ )) ; do
3223                 local mtime=`stat -c %Y $DIR1/$tfile`
3224                 [ $mtime = $TEST_39_MTIME ] || \
3225                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3226
3227                 cancel_lru_locks osc
3228                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3229         done
3230 }
3231 run_test 39d "create, utime, stat =============================="
3232
3233 # bug 21114
3234 test_39e() {
3235         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3236         touch $DIR1/$tfile
3237         local mtime1=`stat -c %Y $DIR1/$tfile`
3238
3239         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3240
3241         for (( i=0; i < 2; i++ )) ; do
3242                 local mtime2=`stat -c %Y $DIR1/$tfile`
3243                 [ $mtime2 = $TEST_39_MTIME ] || \
3244                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3245
3246                 cancel_lru_locks osc
3247                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3248         done
3249 }
3250 run_test 39e "create, stat, utime, stat ========================"
3251
3252 # bug 21114
3253 test_39f() {
3254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3255         touch $DIR1/$tfile
3256         mtime1=`stat -c %Y $DIR1/$tfile`
3257
3258         sleep 2
3259         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3260
3261         for (( i=0; i < 2; i++ )) ; do
3262                 local mtime2=`stat -c %Y $DIR1/$tfile`
3263                 [ $mtime2 = $TEST_39_MTIME ] || \
3264                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3265
3266                 cancel_lru_locks osc
3267                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3268         done
3269 }
3270 run_test 39f "create, stat, sleep, utime, stat ================="
3271
3272 # bug 11063
3273 test_39g() {
3274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3275         echo hello >> $DIR1/$tfile
3276         local mtime1=`stat -c %Y $DIR1/$tfile`
3277
3278         sleep 2
3279         chmod o+r $DIR1/$tfile
3280
3281         for (( i=0; i < 2; i++ )) ; do
3282                 local mtime2=`stat -c %Y $DIR1/$tfile`
3283                 [ "$mtime1" = "$mtime2" ] || \
3284                         error "lost mtime: $mtime2, should be $mtime1"
3285
3286                 cancel_lru_locks osc
3287                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3288         done
3289 }
3290 run_test 39g "write, chmod, stat ==============================="
3291
3292 # bug 11063
3293 test_39h() {
3294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3295         touch $DIR1/$tfile
3296         sleep 1
3297
3298         local d1=`date`
3299         echo hello >> $DIR1/$tfile
3300         local mtime1=`stat -c %Y $DIR1/$tfile`
3301
3302         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3303         local d2=`date`
3304         if [ "$d1" != "$d2" ]; then
3305                 echo "write and touch not within one second"
3306         else
3307                 for (( i=0; i < 2; i++ )) ; do
3308                         local mtime2=`stat -c %Y $DIR1/$tfile`
3309                         [ "$mtime2" = $TEST_39_MTIME ] || \
3310                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3311
3312                         cancel_lru_locks osc
3313                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3314                 done
3315         fi
3316 }
3317 run_test 39h "write, utime within one second, stat ============="
3318
3319 test_39i() {
3320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3321         touch $DIR1/$tfile
3322         sleep 1
3323
3324         echo hello >> $DIR1/$tfile
3325         local mtime1=`stat -c %Y $DIR1/$tfile`
3326
3327         mv $DIR1/$tfile $DIR1/$tfile-1
3328
3329         for (( i=0; i < 2; i++ )) ; do
3330                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3331
3332                 [ "$mtime1" = "$mtime2" ] || \
3333                         error "lost mtime: $mtime2, should be $mtime1"
3334
3335                 cancel_lru_locks osc
3336                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3337         done
3338 }
3339 run_test 39i "write, rename, stat =============================="
3340
3341 test_39j() {
3342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3343         start_full_debug_logging
3344         touch $DIR1/$tfile
3345         sleep 1
3346
3347         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3348         lctl set_param fail_loc=0x80000412
3349         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3350                 error "multiop failed"
3351         local multipid=$!
3352         local mtime1=`stat -c %Y $DIR1/$tfile`
3353
3354         mv $DIR1/$tfile $DIR1/$tfile-1
3355
3356         kill -USR1 $multipid
3357         wait $multipid || error "multiop close failed"
3358
3359         for (( i=0; i < 2; i++ )) ; do
3360                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3361                 [ "$mtime1" = "$mtime2" ] ||
3362                         error "mtime is lost on close: $mtime2, " \
3363                               "should be $mtime1"
3364
3365                 cancel_lru_locks osc
3366                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3367         done
3368         lctl set_param fail_loc=0
3369         stop_full_debug_logging
3370 }
3371 run_test 39j "write, rename, close, stat ======================="
3372
3373 test_39k() {
3374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3375         touch $DIR1/$tfile
3376         sleep 1
3377
3378         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3379         local multipid=$!
3380         local mtime1=`stat -c %Y $DIR1/$tfile`
3381
3382         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3383
3384         kill -USR1 $multipid
3385         wait $multipid || error "multiop close failed"
3386
3387         for (( i=0; i < 2; i++ )) ; do
3388                 local mtime2=`stat -c %Y $DIR1/$tfile`
3389
3390                 [ "$mtime2" = $TEST_39_MTIME ] || \
3391                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3392
3393                 cancel_lru_locks osc
3394                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3395         done
3396 }
3397 run_test 39k "write, utime, close, stat ========================"
3398
3399 # this should be set to future
3400 TEST_39_ATIME=`date -d "1 year" +%s`
3401
3402 test_39l() {
3403         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3404         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3405         local atime_diff=$(do_facet $SINGLEMDS \
3406                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3407         rm -rf $DIR/$tdir
3408         mkdir -p $DIR/$tdir
3409
3410         # test setting directory atime to future
3411         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3412         local atime=$(stat -c %X $DIR/$tdir)
3413         [ "$atime" = $TEST_39_ATIME ] ||
3414                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3415
3416         # test setting directory atime from future to now
3417         local now=$(date +%s)
3418         touch -a -d @$now $DIR/$tdir
3419
3420         atime=$(stat -c %X $DIR/$tdir)
3421         [ "$atime" -eq "$now"  ] ||
3422                 error "atime is not updated from future: $atime, $now"
3423
3424         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3425         sleep 3
3426
3427         # test setting directory atime when now > dir atime + atime_diff
3428         local d1=$(date +%s)
3429         ls $DIR/$tdir
3430         local d2=$(date +%s)
3431         cancel_lru_locks mdc
3432         atime=$(stat -c %X $DIR/$tdir)
3433         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3434                 error "atime is not updated  : $atime, should be $d2"
3435
3436         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3437         sleep 3
3438
3439         # test not setting directory atime when now < dir atime + atime_diff
3440         ls $DIR/$tdir
3441         cancel_lru_locks mdc
3442         atime=$(stat -c %X $DIR/$tdir)
3443         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3444                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3445
3446         do_facet $SINGLEMDS \
3447                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3448 }
3449 run_test 39l "directory atime update ==========================="
3450
3451 test_39m() {
3452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3453         touch $DIR1/$tfile
3454         sleep 2
3455         local far_past_mtime=$(date -d "May 29 1953" +%s)
3456         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3457
3458         touch -m -d @$far_past_mtime $DIR1/$tfile
3459         touch -a -d @$far_past_atime $DIR1/$tfile
3460
3461         for (( i=0; i < 2; i++ )) ; do
3462                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3463                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3464                         error "atime or mtime set incorrectly"
3465
3466                 cancel_lru_locks osc
3467                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3468         done
3469 }
3470 run_test 39m "test atime and mtime before 1970"
3471
3472 test_39n() { # LU-3832
3473         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3474         local atime_diff=$(do_facet $SINGLEMDS \
3475                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3476         local atime0
3477         local atime1
3478         local atime2
3479
3480         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3481
3482         rm -rf $DIR/$tfile
3483         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3484         atime0=$(stat -c %X $DIR/$tfile)
3485
3486         sleep 5
3487         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3488         atime1=$(stat -c %X $DIR/$tfile)
3489
3490         sleep 5
3491         cancel_lru_locks mdc
3492         cancel_lru_locks osc
3493         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3494         atime2=$(stat -c %X $DIR/$tfile)
3495
3496         do_facet $SINGLEMDS \
3497                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3498
3499         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3500         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3501 }
3502 run_test 39n "check that O_NOATIME is honored"
3503
3504 test_39o() {
3505         TESTDIR=$DIR/$tdir/$tfile
3506         [ -e $TESTDIR ] && rm -rf $TESTDIR
3507         mkdir -p $TESTDIR
3508         cd $TESTDIR
3509         links1=2
3510         ls
3511         mkdir a b
3512         ls
3513         links2=$(stat -c %h .)
3514         [ $(($links1 + 2)) != $links2 ] &&
3515                 error "wrong links count $(($links1 + 2)) != $links2"
3516         rmdir b
3517         links3=$(stat -c %h .)
3518         [ $(($links1 + 1)) != $links3 ] &&
3519                 error "wrong links count $links1 != $links3"
3520         return 0
3521 }
3522 run_test 39o "directory cached attributes updated after create ========"
3523
3524 test_39p() {
3525         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3526         local MDTIDX=1
3527         TESTDIR=$DIR/$tdir/$tfile
3528         [ -e $TESTDIR ] && rm -rf $TESTDIR
3529         test_mkdir -p $TESTDIR
3530         cd $TESTDIR
3531         links1=2
3532         ls
3533         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3534         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3535         ls
3536         links2=$(stat -c %h .)
3537         [ $(($links1 + 2)) != $links2 ] &&
3538                 error "wrong links count $(($links1 + 2)) != $links2"
3539         rmdir remote_dir2
3540         links3=$(stat -c %h .)
3541         [ $(($links1 + 1)) != $links3 ] &&
3542                 error "wrong links count $links1 != $links3"
3543         return 0
3544 }
3545 run_test 39p "remote directory cached attributes updated after create ========"
3546
3547
3548 test_39q() { # LU-8041
3549         local testdir=$DIR/$tdir
3550         mkdir -p $testdir
3551         multiop_bg_pause $testdir D_c || error "multiop failed"
3552         local multipid=$!
3553         cancel_lru_locks mdc
3554         kill -USR1 $multipid
3555         local atime=$(stat -c %X $testdir)
3556         [ "$atime" -ne 0 ] || error "atime is zero"
3557 }
3558 run_test 39q "close won't zero out atime"
3559
3560 test_40() {
3561         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3562         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3563                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3564         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3565                 error "$tfile is not 4096 bytes in size"
3566 }
3567 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3568
3569 test_41() {
3570         # bug 1553
3571         small_write $DIR/f41 18
3572 }
3573 run_test 41 "test small file write + fstat ====================="
3574
3575 count_ost_writes() {
3576         lctl get_param -n osc.*.stats |
3577                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3578                         END { printf("%0.0f", writes) }'
3579 }
3580
3581 # decent default
3582 WRITEBACK_SAVE=500
3583 DIRTY_RATIO_SAVE=40
3584 MAX_DIRTY_RATIO=50
3585 BG_DIRTY_RATIO_SAVE=10
3586 MAX_BG_DIRTY_RATIO=25
3587
3588 start_writeback() {
3589         trap 0
3590         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3591         # dirty_ratio, dirty_background_ratio
3592         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3593                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3594                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3595                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3596         else
3597                 # if file not here, we are a 2.4 kernel
3598                 kill -CONT `pidof kupdated`
3599         fi
3600 }
3601
3602 stop_writeback() {
3603         # setup the trap first, so someone cannot exit the test at the
3604         # exact wrong time and mess up a machine
3605         trap start_writeback EXIT
3606         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3607         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3608                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3609                 sysctl -w vm.dirty_writeback_centisecs=0
3610                 sysctl -w vm.dirty_writeback_centisecs=0
3611                 # save and increase /proc/sys/vm/dirty_ratio
3612                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3613                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3614                 # save and increase /proc/sys/vm/dirty_background_ratio
3615                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3616                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3617         else
3618                 # if file not here, we are a 2.4 kernel
3619                 kill -STOP `pidof kupdated`
3620         fi
3621 }
3622
3623 # ensure that all stripes have some grant before we test client-side cache
3624 setup_test42() {
3625         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3626                 dd if=/dev/zero of=$i bs=4k count=1
3627                 rm $i
3628         done
3629 }
3630
3631 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3632 # file truncation, and file removal.
3633 test_42a() {
3634         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3635         setup_test42
3636         cancel_lru_locks osc
3637         stop_writeback
3638         sync; sleep 1; sync # just to be safe
3639         BEFOREWRITES=`count_ost_writes`
3640         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3641         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3642         AFTERWRITES=`count_ost_writes`
3643         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3644                 error "$BEFOREWRITES < $AFTERWRITES"
3645         start_writeback
3646 }
3647 run_test 42a "ensure that we don't flush on close =============="
3648
3649 test_42b() {
3650         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3651         setup_test42
3652         cancel_lru_locks osc
3653         stop_writeback
3654         sync
3655         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3656         BEFOREWRITES=$(count_ost_writes)
3657         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3658         AFTERWRITES=$(count_ost_writes)
3659         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3660                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3661         fi
3662         BEFOREWRITES=$(count_ost_writes)
3663         sync || error "sync: $?"
3664         AFTERWRITES=$(count_ost_writes)
3665         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3666                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3667         fi
3668         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3669         start_writeback
3670         return 0
3671 }
3672 run_test 42b "test destroy of file with cached dirty data ======"
3673
3674 # if these tests just want to test the effect of truncation,
3675 # they have to be very careful.  consider:
3676 # - the first open gets a {0,EOF}PR lock
3677 # - the first write conflicts and gets a {0, count-1}PW
3678 # - the rest of the writes are under {count,EOF}PW
3679 # - the open for truncate tries to match a {0,EOF}PR
3680 #   for the filesize and cancels the PWs.
3681 # any number of fixes (don't get {0,EOF} on open, match
3682 # composite locks, do smarter file size management) fix
3683 # this, but for now we want these tests to verify that
3684 # the cancellation with truncate intent works, so we
3685 # start the file with a full-file pw lock to match against
3686 # until the truncate.
3687 trunc_test() {
3688         test=$1
3689         file=$DIR/$test
3690         offset=$2
3691         cancel_lru_locks osc
3692         stop_writeback
3693         # prime the file with 0,EOF PW to match
3694         touch $file
3695         $TRUNCATE $file 0
3696         sync; sync
3697         # now the real test..
3698         dd if=/dev/zero of=$file bs=1024 count=100
3699         BEFOREWRITES=`count_ost_writes`
3700         $TRUNCATE $file $offset
3701         cancel_lru_locks osc
3702         AFTERWRITES=`count_ost_writes`
3703         start_writeback
3704 }
3705
3706 test_42c() {
3707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3708         trunc_test 42c 1024
3709         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3710             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3711         rm $file
3712 }
3713 run_test 42c "test partial truncate of file with cached dirty data"
3714
3715 test_42d() {
3716         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3717         trunc_test 42d 0
3718         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3719             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3720         rm $file
3721 }
3722 run_test 42d "test complete truncate of file with cached dirty data"
3723
3724 test_42e() { # bug22074
3725         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3726         local TDIR=$DIR/${tdir}e
3727         local pagesz=$(page_size)
3728         local pages=16 # hardcoded 16 pages, don't change it.
3729         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3730         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3731         local max_dirty_mb
3732         local warmup_files
3733
3734         test_mkdir -p $DIR/${tdir}e
3735         $SETSTRIPE -c 1 $TDIR
3736         createmany -o $TDIR/f $files
3737
3738         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3739
3740         # we assume that with $OSTCOUNT files, at least one of them will
3741         # be allocated on OST0.
3742         warmup_files=$((OSTCOUNT * max_dirty_mb))
3743         createmany -o $TDIR/w $warmup_files
3744
3745         # write a large amount of data into one file and sync, to get good
3746         # avail_grant number from OST.
3747         for ((i=0; i<$warmup_files; i++)); do
3748                 idx=$($GETSTRIPE -i $TDIR/w$i)
3749                 [ $idx -ne 0 ] && continue
3750                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3751                 break
3752         done
3753         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3754         sync
3755         $LCTL get_param $proc_osc0/cur_dirty_bytes
3756         $LCTL get_param $proc_osc0/cur_grant_bytes
3757
3758         # create as much dirty pages as we can while not to trigger the actual
3759         # RPCs directly. but depends on the env, VFS may trigger flush during this
3760         # period, hopefully we are good.
3761         for ((i=0; i<$warmup_files; i++)); do
3762                 idx=$($GETSTRIPE -i $TDIR/w$i)
3763                 [ $idx -ne 0 ] && continue
3764                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3765         done
3766         $LCTL get_param $proc_osc0/cur_dirty_bytes
3767         $LCTL get_param $proc_osc0/cur_grant_bytes
3768
3769         # perform the real test
3770         $LCTL set_param $proc_osc0/rpc_stats 0
3771         for ((;i<$files; i++)); do
3772                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3773                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3774         done
3775         sync
3776         $LCTL get_param $proc_osc0/rpc_stats
3777
3778         local percent=0
3779         local have_ppr=false
3780         $LCTL get_param $proc_osc0/rpc_stats |
3781                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3782                         # skip lines until we are at the RPC histogram data
3783                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3784                         $have_ppr || continue
3785
3786                         # we only want the percent stat for < 16 pages
3787                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3788
3789                         percent=$((percent + WPCT))
3790                         if [[ $percent -gt 15 ]]; then
3791                                 error "less than 16-pages write RPCs" \
3792                                       "$percent% > 15%"
3793                                 break
3794                         fi
3795                 done
3796         rm -rf $TDIR
3797 }
3798 run_test 42e "verify sub-RPC writes are not done synchronously"
3799
3800 test_43A() { # was test_43
3801         test_mkdir -p $DIR/$tdir
3802         cp -p /bin/ls $DIR/$tdir/$tfile
3803         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3804         pid=$!
3805         # give multiop a chance to open
3806         sleep 1
3807
3808         $DIR/$tdir/$tfile && error || true
3809         kill -USR1 $pid
3810 }
3811 run_test 43A "execution of file opened for write should return -ETXTBSY"
3812
3813 test_43a() {
3814         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3815         test_mkdir -p $DIR/$tdir
3816         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3817                         cp -p multiop $DIR/$tdir/multiop
3818         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3819                         return 1
3820         MULTIOP_PID=$!
3821         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3822         kill -USR1 $MULTIOP_PID || return 2
3823         wait $MULTIOP_PID || return 3
3824         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3825 }
3826 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3827
3828 test_43b() {
3829         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3830         test_mkdir -p $DIR/$tdir
3831         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3832                         cp -p multiop $DIR/$tdir/multiop
3833         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3834                         return 1
3835         MULTIOP_PID=$!
3836         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3837         kill -USR1 $MULTIOP_PID || return 2
3838         wait $MULTIOP_PID || return 3
3839         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3840 }
3841 run_test 43b "truncate of file being executed should return -ETXTBSY"
3842
3843 test_43c() {
3844         local testdir="$DIR/$tdir"
3845         test_mkdir -p $DIR/$tdir
3846         cp $SHELL $testdir/
3847         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3848                 ( cd $testdir && md5sum -c)
3849 }
3850 run_test 43c "md5sum of copy into lustre========================"
3851
3852 test_44A() { # was test_44
3853         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3854         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3855         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3856 }
3857 run_test 44A "zero length read from a sparse stripe ============="
3858
3859 test_44a() {
3860         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3861                 awk '{ print $2 }')
3862         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3863         [[ $nstripe -gt $OSTCOUNT ]] &&
3864             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3865             return
3866         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3867                 awk '{ print $2 }')
3868         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3869                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3870                         awk '{ print $2 }')
3871         fi
3872
3873         OFFSETS="0 $((stride/2)) $((stride-1))"
3874         for offset in $OFFSETS; do
3875                 for i in $(seq 0 $((nstripe-1))); do
3876                         local GLOBALOFFSETS=""
3877                         # size in Bytes
3878                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3879                         local myfn=$DIR/d44a-$size
3880                         echo "--------writing $myfn at $size"
3881                         ll_sparseness_write $myfn $size ||
3882                                 error "ll_sparseness_write"
3883                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3884                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3885                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3886
3887                         for j in $(seq 0 $((nstripe-1))); do
3888                                 # size in Bytes
3889                                 size=$((((j + $nstripe )*$stride + $offset)))
3890                                 ll_sparseness_write $myfn $size ||
3891                                         error "ll_sparseness_write"
3892                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3893                         done
3894                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3895                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3896                         rm -f $myfn
3897                 done
3898         done
3899 }
3900 run_test 44a "test sparse pwrite ==============================="
3901
3902 dirty_osc_total() {
3903         tot=0
3904         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3905                 tot=$(($tot + $d))
3906         done
3907         echo $tot
3908 }
3909 do_dirty_record() {
3910         before=`dirty_osc_total`
3911         echo executing "\"$*\""
3912         eval $*
3913         after=`dirty_osc_total`
3914         echo before $before, after $after
3915 }
3916 test_45() {
3917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3918         f="$DIR/f45"
3919         # Obtain grants from OST if it supports it
3920         echo blah > ${f}_grant
3921         stop_writeback
3922         sync
3923         do_dirty_record "echo blah > $f"
3924         [[ $before -eq $after ]] && error "write wasn't cached"
3925         do_dirty_record "> $f"
3926         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3927         do_dirty_record "echo blah > $f"
3928         [[ $before -eq $after ]] && error "write wasn't cached"
3929         do_dirty_record "sync"
3930         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3931         do_dirty_record "echo blah > $f"
3932         [[ $before -eq $after ]] && error "write wasn't cached"
3933         do_dirty_record "cancel_lru_locks osc"
3934         [[ $before -gt $after ]] ||
3935                 error "lock cancellation didn't lower dirty count"
3936         start_writeback
3937 }
3938 run_test 45 "osc io page accounting ============================"
3939
3940 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3941 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3942 # objects offset and an assert hit when an rpc was built with 1023's mapped
3943 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3944 test_46() {
3945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3946         f="$DIR/f46"
3947         stop_writeback
3948         sync
3949         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3950         sync
3951         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3952         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3953         sync
3954         start_writeback
3955 }
3956 run_test 46 "dirtying a previously written page ================"
3957
3958 # test_47 is removed "Device nodes check" is moved to test_28
3959
3960 test_48a() { # bug 2399
3961         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3962         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3963                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3964                 return
3965         test_mkdir $DIR/$tdir
3966         cd $DIR/$tdir
3967         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3968         test_mkdir $DIR/$tdir || error "recreate directory failed"
3969         touch foo || error "'touch foo' failed after recreating cwd"
3970         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
3971         touch .foo || error "'touch .foo' failed after recreating cwd"
3972         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3973         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3974         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3975         cd . || error "'cd .' failed after recreating cwd"
3976         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3977         rmdir . && error "'rmdir .' worked after recreating cwd"
3978         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3979         cd .. || error "'cd ..' failed after recreating cwd"
3980 }
3981 run_test 48a "Access renamed working dir (should return errors)="
3982
3983 test_48b() { # bug 2399
3984         rm -rf $DIR/$tdir
3985         test_mkdir $DIR/$tdir
3986         cd $DIR/$tdir
3987         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3988         touch foo && error "'touch foo' worked after removing cwd"
3989         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3990         touch .foo && error "'touch .foo' worked after removing cwd"
3991         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3992         ls . > /dev/null && error "'ls .' worked after removing cwd"
3993         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3994         test_mkdir . && error "'mkdir .' worked after removing cwd"
3995         rmdir . && error "'rmdir .' worked after removing cwd"
3996         ln -s . foo && error "'ln -s .' worked after removing cwd"
3997         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3998 }
3999 run_test 48b "Access removed working dir (should return errors)="
4000
4001 test_48c() { # bug 2350
4002         #lctl set_param debug=-1
4003         #set -vx
4004         rm -rf $DIR/$tdir
4005         test_mkdir -p $DIR/$tdir/dir
4006         cd $DIR/$tdir/dir
4007         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4008         $TRACE touch foo && error "touch foo worked after removing cwd"
4009         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
4010         touch .foo && error "touch .foo worked after removing cwd"
4011         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
4012         $TRACE ls . && error "'ls .' worked after removing cwd"
4013         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4014         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
4015         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4016         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4017         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4018 }
4019 run_test 48c "Access removed working subdir (should return errors)"
4020
4021 test_48d() { # bug 2350
4022         #lctl set_param debug=-1
4023         #set -vx
4024         rm -rf $DIR/$tdir
4025         test_mkdir -p $DIR/$tdir/dir
4026         cd $DIR/$tdir/dir
4027         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4028         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4029         $TRACE touch foo && error "'touch foo' worked after removing parent"
4030         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
4031         touch .foo && error "'touch .foo' worked after removing parent"
4032         test_mkdir .foo && error "mkdir .foo worked after removing parent"
4033         $TRACE ls . && error "'ls .' worked after removing parent"
4034         $TRACE ls .. && error "'ls ..' worked after removing parent"
4035         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
4036         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4037         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4038         true
4039 }
4040 run_test 48d "Access removed parent subdir (should return errors)"
4041
4042 test_48e() { # bug 4134
4043         #lctl set_param debug=-1
4044         #set -vx
4045         rm -rf $DIR/$tdir
4046         test_mkdir -p $DIR/$tdir/dir
4047         cd $DIR/$tdir/dir
4048         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4049         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4050         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4051         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4052         # On a buggy kernel addition of "touch foo" after cd .. will
4053         # produce kernel oops in lookup_hash_it
4054         touch ../foo && error "'cd ..' worked after recreate parent"
4055         cd $DIR
4056         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4057 }
4058 run_test 48e "Access to recreated parent subdir (should return errors)"
4059
4060 test_49() { # LU-1030
4061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4062         remote_ost_nodsh && skip "remote OST with nodsh" && return
4063         # get ost1 size - lustre-OST0000
4064         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4065                 awk '{ print $4 }')
4066         # write 800M at maximum
4067         [[ $ost1_size -lt 2 ]] && ost1_size=2
4068         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4069
4070         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4071         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4072         local dd_pid=$!
4073
4074         # change max_pages_per_rpc while writing the file
4075         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4076         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4077         # loop until dd process exits
4078         while ps ax -opid | grep -wq $dd_pid; do
4079                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4080                 sleep $((RANDOM % 5 + 1))
4081         done
4082         # restore original max_pages_per_rpc
4083         $LCTL set_param $osc1_mppc=$orig_mppc
4084         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4085 }
4086 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4087
4088 test_50() {
4089         # bug 1485
4090         test_mkdir $DIR/$tdir
4091         cd $DIR/$tdir
4092         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4093 }
4094 run_test 50 "special situations: /proc symlinks  ==============="
4095
4096 test_51a() {    # was test_51
4097         # bug 1516 - create an empty entry right after ".." then split dir
4098         test_mkdir -c1 $DIR/$tdir
4099         touch $DIR/$tdir/foo
4100         $MCREATE $DIR/$tdir/bar
4101         rm $DIR/$tdir/foo
4102         createmany -m $DIR/$tdir/longfile 201
4103         FNUM=202
4104         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4105                 $MCREATE $DIR/$tdir/longfile$FNUM
4106                 FNUM=$(($FNUM + 1))
4107                 echo -n "+"
4108         done
4109         echo
4110         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4111 }
4112 run_test 51a "special situations: split htree with empty entry =="
4113
4114 cleanup_print_lfs_df () {
4115         trap 0
4116         $LFS df
4117         $LFS df -i
4118 }
4119
4120 test_51b() {
4121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4122         local dir=$DIR/$tdir
4123
4124         local nrdirs=$((65536 + 100))
4125
4126         # cleanup the directory
4127         rm -fr $dir
4128
4129         test_mkdir -p -c1 $dir
4130
4131         $LFS df
4132         $LFS df -i
4133         local mdtidx=$(printf "%04x" $($LFS getstripe -M $dir))
4134         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4135         [[ $numfree -lt $nrdirs ]] &&
4136                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4137                 return
4138
4139         # need to check free space for the directories as well
4140         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4141         numfree=$(( blkfree / $(fs_inode_ksize) ))
4142         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)" &&
4143                 return
4144
4145         trap cleanup_print_lfsdf EXIT
4146
4147         # create files
4148         createmany -d $dir/d $nrdirs ||
4149                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4150
4151         # really created :
4152         nrdirs=$(ls -U $dir | wc -l)
4153
4154         # unlink all but 100 subdirectories, then check it still works
4155         local left=100
4156         local delete=$((nrdirs - left))
4157
4158         $LFS df
4159         $LFS df -i
4160
4161         # for ldiskfs the nlink count should be 1, but this is OSD specific
4162         # and so this is listed for informational purposes only
4163         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4164         unlinkmany -d $dir/d $delete ||
4165                 error "unlink of first $delete subdirs failed"
4166
4167         echo "nlink between: $(stat -c %h $dir)"
4168         local found=$(ls -U $dir | wc -l)
4169         [ $found -ne $left ] &&
4170                 error "can't find subdirs: found only $found, expected $left"
4171
4172         unlinkmany -d $dir/d $delete $left ||
4173                 error "unlink of second $left subdirs failed"
4174         # regardless of whether the backing filesystem tracks nlink accurately
4175         # or not, the nlink count shouldn't be more than "." and ".." here
4176         local after=$(stat -c %h $dir)
4177         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4178                 echo "nlink after: $after"
4179
4180         cleanup_print_lfs_df
4181 }
4182 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4183
4184 test_51d() {
4185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4186         [[ $OSTCOUNT -lt 3 ]] &&
4187                 skip_env "skipping test with few OSTs" && return
4188         test_mkdir -p $DIR/$tdir
4189         createmany -o $DIR/$tdir/t- 1000
4190         $GETSTRIPE $DIR/$tdir > $TMP/$tfile
4191         for N in $(seq 0 $((OSTCOUNT - 1))); do
4192                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4193                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4194                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4195                         '($1 == '$N') { objs += 1 } \
4196                         END { printf("%0.0f", objs) }')
4197                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4198         done
4199         unlinkmany $DIR/$tdir/t- 1000
4200
4201         NLAST=0
4202         for N in $(seq 1 $((OSTCOUNT - 1))); do
4203                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4204                         error "OST $N has less objects vs OST $NLAST" \
4205                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4206                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4207                         error "OST $N has less objects vs OST $NLAST" \
4208                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4209
4210                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4211                         error "OST $N has less #0 objects vs OST $NLAST" \
4212                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4213                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4214                         error "OST $N has less #0 objects vs OST $NLAST" \
4215                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4216                 NLAST=$N
4217         done
4218         rm -f $TMP/$tfile
4219 }
4220 run_test 51d "check object distribution"
4221
4222 test_51e() {
4223         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4224                 skip "Only applicable to ldiskfs-based MDTs"
4225                 return
4226         fi
4227
4228         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4229         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4230
4231         touch $DIR/$tdir/d0/foo
4232         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4233                 error "file exceed 65000 nlink limit!"
4234         unlinkmany $DIR/$tdir/d0/f- 65001
4235         return 0
4236 }
4237 run_test 51e "check file nlink limit"
4238
4239 test_52a() {
4240         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4241         test_mkdir -p $DIR/$tdir
4242         touch $DIR/$tdir/foo
4243         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4244         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4245         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4246         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4247         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4248                                         error "link worked"
4249         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4250         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4251         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4252                                                      error "lsattr"
4253         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4254         cp -r $DIR/$tdir $TMP/
4255         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4256 }
4257 run_test 52a "append-only flag test (should return errors)"
4258
4259 test_52b() {
4260         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4261         test_mkdir -p $DIR/$tdir
4262         touch $DIR/$tdir/foo
4263         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4264         cat test > $DIR/$tdir/foo && error "cat test worked"
4265         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4266         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4267         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4268                                         error "link worked"
4269         echo foo >> $DIR/$tdir/foo && error "echo worked"
4270         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4271         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4272         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4273         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4274                                                         error "lsattr"
4275         chattr -i $DIR/$tdir/foo || error "chattr failed"
4276
4277         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4278 }
4279 run_test 52b "immutable flag test (should return errors) ======="
4280
4281 test_53() {
4282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4283         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4284         remote_ost_nodsh && skip "remote OST with nodsh" && return
4285
4286         local param
4287         local param_seq
4288         local ostname
4289         local mds_last
4290         local mds_last_seq
4291         local ost_last
4292         local ost_last_seq
4293         local ost_last_id
4294         local ostnum
4295         local node
4296         local found=false
4297         local support_last_seq=true
4298
4299         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4300                 support_last_seq=false
4301
4302         # only test MDT0000
4303         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4304         local value
4305         for value in $(do_facet $SINGLEMDS \
4306                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4307                 param=$(echo ${value[0]} | cut -d "=" -f1)
4308                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4309
4310                 if $support_last_seq; then
4311                         param_seq=$(echo $param |
4312                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4313                         mds_last_seq=$(do_facet $SINGLEMDS \
4314                                        $LCTL get_param -n $param_seq)
4315                 fi
4316                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4317
4318                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4319                 node=$(facet_active_host ost$((ostnum+1)))
4320                 param="obdfilter.$ostname.last_id"
4321                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4322                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4323                         ost_last_id=$ost_last
4324
4325                         if $support_last_seq; then
4326                                 ost_last_id=$(echo $ost_last |
4327                                               awk -F':' '{print $2}' |
4328                                               sed -e "s/^0x//g")
4329                                 ost_last_seq=$(echo $ost_last |
4330                                                awk -F':' '{print $1}')
4331                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4332                         fi
4333
4334                         if [[ $ost_last_id != $mds_last ]]; then
4335                                 error "$ost_last_id != $mds_last"
4336                         else
4337                                 found=true
4338                                 break
4339                         fi
4340                 done
4341         done
4342         $found || error "can not match last_seq/last_id for $mdtosc"
4343         return 0
4344 }
4345 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4346
4347 test_54a() {
4348         $SOCKETSERVER $DIR/socket ||
4349                 error "$SOCKETSERVER $DIR/socket failed: $?"
4350         $SOCKETCLIENT $DIR/socket ||
4351                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4352         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4353 }
4354 run_test 54a "unix domain socket test =========================="
4355
4356 test_54b() {
4357         f="$DIR/f54b"
4358         mknod $f c 1 3
4359         chmod 0666 $f
4360         dd if=/dev/zero of=$f bs=$(page_size) count=1
4361 }
4362 run_test 54b "char device works in lustre ======================"
4363
4364 find_loop_dev() {
4365         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4366         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4367         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4368
4369         for i in $(seq 3 7); do
4370                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4371                 LOOPDEV=$LOOPBASE$i
4372                 LOOPNUM=$i
4373                 break
4374         done
4375 }
4376
4377 cleanup_54c() {
4378         local rc=0
4379         loopdev="$DIR/loop54c"
4380
4381         trap 0
4382         $UMOUNT $DIR/$tdir || rc=$?
4383         losetup -d $loopdev || true
4384         losetup -d $LOOPDEV || true
4385         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4386         return $rc
4387 }
4388
4389 test_54c() {
4390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4391         loopdev="$DIR/loop54c"
4392
4393         find_loop_dev
4394         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4395         trap cleanup_54c EXIT
4396         mknod $loopdev b 7 $LOOPNUM
4397         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4398         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4399         losetup $loopdev $DIR/$tfile ||
4400                 error "can't set up $loopdev for $DIR/$tfile"
4401         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4402         test_mkdir -p $DIR/$tdir
4403         mount -t ext2 $loopdev $DIR/$tdir ||
4404                 error "error mounting $loopdev on $DIR/$tdir"
4405         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4406                 error "dd write"
4407         df $DIR/$tdir
4408         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4409                 error "dd read"
4410         cleanup_54c
4411 }
4412 run_test 54c "block device works in lustre ====================="
4413
4414 test_54d() {
4415         f="$DIR/f54d"
4416         string="aaaaaa"
4417         mknod $f p
4418         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4419 }
4420 run_test 54d "fifo device works in lustre ======================"
4421
4422 test_54e() {
4423         f="$DIR/f54e"
4424         string="aaaaaa"
4425         cp -aL /dev/console $f
4426         echo $string > $f || error "echo $string to $f failed"
4427 }
4428 run_test 54e "console/tty device works in lustre ======================"
4429
4430 #The test_55 used to be iopen test and it was removed by bz#24037.
4431 #run_test 55 "check iopen_connect_dentry() ======================"
4432
4433 test_56a() {    # was test_56
4434         rm -rf $DIR/$tdir
4435         $SETSTRIPE -d $DIR
4436         test_mkdir -p $DIR/$tdir/dir
4437         NUMFILES=3
4438         NUMFILESx2=$(($NUMFILES * 2))
4439         for i in $(seq 1 $NUMFILES); do
4440                 touch $DIR/$tdir/file$i
4441                 touch $DIR/$tdir/dir/file$i
4442         done
4443
4444         # test lfs getstripe with --recursive
4445         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4446         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4447                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4448         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4449         [[ $FILENUM -eq $NUMFILES ]] ||
4450                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4451         echo "$GETSTRIPE --recursive passed."
4452
4453         # test lfs getstripe with file instead of dir
4454         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4455         [[ $FILENUM -eq 1 ]] ||
4456                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4457         echo "$GETSTRIPE file1 passed."
4458
4459         #test lfs getstripe with --verbose
4460         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4461                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4462                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4463         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4464                 error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4465
4466         #test lfs getstripe with -v prints lmm_fid
4467         [[ $($GETSTRIPE -v $DIR/$tdir | grep -c lmm_fid) -eq $NUMFILES ]] ||
4468                 error "$GETSTRIPE -v $DIR/$tdir: want $NUMFILES lmm_fid: lines"
4469         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_fid) -eq 0 ]] ||
4470                 error "$GETSTRIPE $DIR/$tdir: showed lmm_fid"
4471         echo "$GETSTRIPE --verbose passed."
4472
4473         #check for FID information
4474         local fid1=$($GETSTRIPE --fid $DIR/$tdir/file1)
4475         local fid2=$($GETSTRIPE --verbose $DIR/$tdir/file1 |
4476                        awk '/lmm_fid: / { print $2 }')
4477         local fid3=$($LFS path2fid $DIR/$tdir/file1)
4478         [ "$fid1" != "$fid2" ] &&
4479                 error "getstripe --fid $fid1 != getstripe --verbose $fid2"
4480         [ "$fid1" != "$fid3" ] &&
4481                 error "getstripe --fid $fid1 != lfs path2fid $fid3"
4482         echo "$GETSTRIPE --fid passed."
4483
4484         #test lfs getstripe with --obd
4485         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4486                 grep -q "unknown obduuid" ||
4487                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4488
4489         [[ $OSTCOUNT -lt 2 ]] &&
4490                 skip_env "skipping other $GETSTRIPE --obd test" && return
4491
4492         OSTIDX=1
4493         OBDUUID=$(ostuuid_from_index $OSTIDX)
4494         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4495         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4496         [[ $FOUND -eq $FILENUM ]] ||
4497                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4498         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4499                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4500                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4501                 error "$GETSTRIPE --obd: should not show file on other obd"
4502         echo "$GETSTRIPE --obd passed"
4503 }
4504 run_test 56a "check $GETSTRIPE"
4505
4506 test_56b() {
4507         test_mkdir $DIR/$tdir
4508         NUMDIRS=3
4509         for i in $(seq 1 $NUMDIRS); do
4510                 test_mkdir $DIR/$tdir/dir$i
4511         done
4512
4513         # test lfs getdirstripe default mode is non-recursion, which is
4514         # different from lfs getstripe
4515         dircnt=$($LFS getdirstripe $DIR/$tdir | grep -c lmv_stripe_count)
4516         [[ $dircnt -eq 1 ]] ||
4517                 error "$LFS getdirstripe: found $dircnt, not 1"
4518         dircnt=$($LFS getdirstripe --recursive $DIR/$tdir |
4519                 grep -c lmv_stripe_count)
4520         [[ $dircnt -eq $((NUMDIRS + 1)) ]] ||
4521                 error "$LFS getdirstripe --recursive: found $dircnt, \
4522                         not $((NUMDIRS + 1))"
4523 }
4524 run_test 56b "check $LFS getdirstripe"
4525
4526 NUMFILES=3
4527 NUMDIRS=3
4528 setup_56() {
4529         local LOCAL_NUMFILES="$1"
4530         local LOCAL_NUMDIRS="$2"
4531         local MKDIR_PARAMS="$3"
4532         local DIR_STRIPE_PARAMS="$4"
4533
4534         if [ ! -d "$TDIR" ] ; then
4535                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4536                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4537                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4538                         touch $TDIR/file$i
4539                 done
4540                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4541                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4542                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4543                                 touch $TDIR/dir$i/file$j
4544                         done
4545                 done
4546         fi
4547 }
4548
4549 setup_56_special() {
4550         LOCAL_NUMFILES=$1
4551         LOCAL_NUMDIRS=$2
4552         setup_56 $1 $2
4553         if [ ! -e "$TDIR/loop1b" ] ; then
4554                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4555                         mknod $TDIR/loop${i}b b 7 $i
4556                         mknod $TDIR/null${i}c c 1 3
4557                         ln -s $TDIR/file1 $TDIR/link${i}l
4558                 done
4559                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4560                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4561                         mknod $TDIR/dir$i/null${i}c c 1 3
4562                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4563                 done
4564         fi
4565 }
4566
4567 test_56g() {
4568         $SETSTRIPE -d $DIR
4569
4570         TDIR=$DIR/${tdir}g
4571         setup_56 $NUMFILES $NUMDIRS
4572
4573         EXPECTED=$(($NUMDIRS + 2))
4574         # test lfs find with -name
4575         for i in $(seq 1 $NUMFILES) ; do
4576                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4577                 [ $NUMS -eq $EXPECTED ] ||
4578                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4579                               "found $NUMS, expected $EXPECTED"
4580         done
4581 }
4582 run_test 56g "check lfs find -name ============================="
4583
4584 test_56h() {
4585         $SETSTRIPE -d $DIR
4586
4587         TDIR=$DIR/${tdir}g
4588         setup_56 $NUMFILES $NUMDIRS
4589
4590         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4591         # test lfs find with ! -name
4592         for i in $(seq 1 $NUMFILES) ; do
4593                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4594                 [ $NUMS -eq $EXPECTED ] ||
4595                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4596                               "found $NUMS, expected $EXPECTED"
4597         done
4598 }
4599 run_test 56h "check lfs find ! -name ============================="
4600
4601 test_56i() {
4602        tdir=${tdir}i
4603        test_mkdir -p $DIR/$tdir
4604        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4605        CMD="$LFIND -ost $UUID $DIR/$tdir"
4606        OUT=$($CMD)
4607        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4608 }
4609 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4610
4611 test_56j() {
4612         TDIR=$DIR/${tdir}g
4613         setup_56_special $NUMFILES $NUMDIRS
4614
4615         EXPECTED=$((NUMDIRS + 1))
4616         CMD="$LFIND -type d $TDIR"
4617         NUMS=$($CMD | wc -l)
4618         [ $NUMS -eq $EXPECTED ] ||
4619                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4620 }
4621 run_test 56j "check lfs find -type d ============================="
4622
4623 test_56k() {
4624         TDIR=$DIR/${tdir}g
4625         setup_56_special $NUMFILES $NUMDIRS
4626
4627         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4628         CMD="$LFIND -type f $TDIR"
4629         NUMS=$($CMD | wc -l)
4630         [ $NUMS -eq $EXPECTED ] ||
4631                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4632 }
4633 run_test 56k "check lfs find -type f ============================="
4634
4635 test_56l() {
4636         TDIR=$DIR/${tdir}g
4637         setup_56_special $NUMFILES $NUMDIRS
4638
4639         EXPECTED=$((NUMDIRS + NUMFILES))
4640         CMD="$LFIND -type b $TDIR"
4641         NUMS=$($CMD | wc -l)
4642         [ $NUMS -eq $EXPECTED ] ||
4643                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4644 }
4645 run_test 56l "check lfs find -type b ============================="
4646
4647 test_56m() {
4648         TDIR=$DIR/${tdir}g
4649         setup_56_special $NUMFILES $NUMDIRS
4650
4651         EXPECTED=$((NUMDIRS + NUMFILES))
4652         CMD="$LFIND -type c $TDIR"
4653         NUMS=$($CMD | wc -l)
4654         [ $NUMS -eq $EXPECTED ] ||
4655                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4656 }
4657 run_test 56m "check lfs find -type c ============================="
4658
4659 test_56n() {
4660         TDIR=$DIR/${tdir}g
4661         setup_56_special $NUMFILES $NUMDIRS
4662
4663         EXPECTED=$((NUMDIRS + NUMFILES))
4664         CMD="$LFIND -type l $TDIR"
4665         NUMS=$($CMD | wc -l)
4666         [ $NUMS -eq $EXPECTED ] ||
4667                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4668 }
4669 run_test 56n "check lfs find -type l ============================="
4670
4671 test_56o() {
4672         TDIR=$DIR/${tdir}o
4673         setup_56 $NUMFILES $NUMDIRS
4674         utime $TDIR/file1 > /dev/null || error "utime (1)"
4675         utime $TDIR/file2 > /dev/null || error "utime (2)"
4676         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4677         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4678         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4679         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4680
4681         EXPECTED=4
4682         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4683         [ $NUMS -eq $EXPECTED ] || \
4684                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4685
4686         EXPECTED=12
4687         CMD="$LFIND -mtime 0 $TDIR"
4688         NUMS=$($CMD | wc -l)
4689         [ $NUMS -eq $EXPECTED ] ||
4690                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4691 }
4692 run_test 56o "check lfs find -mtime for old files =========================="
4693
4694 test_56p() {
4695         [ $RUNAS_ID -eq $UID ] &&
4696                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4697
4698         TDIR=$DIR/${tdir}p
4699         setup_56 $NUMFILES $NUMDIRS
4700
4701         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4702         EXPECTED=$NUMFILES
4703         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4704         NUMS=$($CMD | wc -l)
4705         [ $NUMS -eq $EXPECTED ] || \
4706                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4707
4708         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4709         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4710         NUMS=$($CMD | wc -l)
4711         [ $NUMS -eq $EXPECTED ] || \
4712                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4713 }
4714 run_test 56p "check lfs find -uid and ! -uid ==============================="
4715
4716 test_56q() {
4717         [ $RUNAS_ID -eq $UID ] &&
4718                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4719
4720         TDIR=$DIR/${tdir}q
4721         setup_56 $NUMFILES $NUMDIRS
4722
4723         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4724
4725         EXPECTED=$NUMFILES
4726         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4727         NUMS=$($CMD | wc -l)
4728         [ $NUMS -eq $EXPECTED ] ||
4729                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4730
4731         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4732         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4733         NUMS=$($CMD | wc -l)
4734         [ $NUMS -eq $EXPECTED ] ||
4735                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4736 }
4737 run_test 56q "check lfs find -gid and ! -gid ==============================="
4738
4739 test_56r() {
4740         TDIR=$DIR/${tdir}r
4741         setup_56 $NUMFILES $NUMDIRS
4742
4743         EXPECTED=12
4744         CMD="$LFIND -size 0 -type f $TDIR"
4745         NUMS=$($CMD | wc -l)
4746         [ $NUMS -eq $EXPECTED ] ||
4747                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4748         EXPECTED=0
4749         CMD="$LFIND ! -size 0 -type f $TDIR"
4750         NUMS=$($CMD | wc -l)
4751         [ $NUMS -eq $EXPECTED ] ||
4752                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4753         echo "test" > $TDIR/$tfile
4754         echo "test2" > $TDIR/$tfile.2 && sync
4755         EXPECTED=1
4756         CMD="$LFIND -size 5 -type f $TDIR"
4757         NUMS=$($CMD | wc -l)
4758         [ $NUMS -eq $EXPECTED ] ||
4759                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4760         EXPECTED=1
4761         CMD="$LFIND -size +5 -type f $TDIR"
4762         NUMS=$($CMD | wc -l)
4763         [ $NUMS -eq $EXPECTED ] ||
4764                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4765         EXPECTED=2
4766         CMD="$LFIND -size +0 -type f $TDIR"
4767         NUMS=$($CMD | wc -l)
4768         [ $NUMS -eq $EXPECTED ] ||
4769                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4770         EXPECTED=2
4771         CMD="$LFIND ! -size -5 -type f $TDIR"
4772         NUMS=$($CMD | wc -l)
4773         [ $NUMS -eq $EXPECTED ] ||
4774                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4775         EXPECTED=12
4776         CMD="$LFIND -size -5 -type f $TDIR"
4777         NUMS=$($CMD | wc -l)
4778         [ $NUMS -eq $EXPECTED ] ||
4779                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4780 }
4781 run_test 56r "check lfs find -size works =========================="
4782
4783 test_56s() { # LU-611
4784         TDIR=$DIR/${tdir}s
4785         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4786
4787         if [[ $OSTCOUNT -gt 1 ]]; then
4788                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4789                 ONESTRIPE=4
4790                 EXTRA=4
4791         else
4792                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4793                 EXTRA=0
4794         fi
4795
4796         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4797         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4798         NUMS=$($CMD | wc -l)
4799         [ $NUMS -eq $EXPECTED ] || {
4800                 $GETSTRIPE -R $TDIR
4801                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4802         }
4803
4804         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4805         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4806         NUMS=$($CMD | wc -l)
4807         [ $NUMS -eq $EXPECTED ] || {
4808                 $GETSTRIPE -R $TDIR
4809                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4810         }
4811
4812         EXPECTED=$ONESTRIPE
4813         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4814         NUMS=$($CMD | wc -l)
4815         [ $NUMS -eq $EXPECTED ] || {
4816                 $GETSTRIPE -R $TDIR
4817                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4818         }
4819
4820         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4821         NUMS=$($CMD | wc -l)
4822         [ $NUMS -eq $EXPECTED ] || {
4823                 $GETSTRIPE -R $TDIR
4824                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4825         }
4826
4827         EXPECTED=0
4828         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4829         NUMS=$($CMD | wc -l)
4830         [ $NUMS -eq $EXPECTED ] || {
4831                 $GETSTRIPE -R $TDIR
4832                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4833         }
4834 }
4835 run_test 56s "check lfs find -stripe-count works"
4836
4837 test_56t() { # LU-611
4838         TDIR=$DIR/${tdir}t
4839         setup_56 $NUMFILES $NUMDIRS "--stripe-size 512k"
4840
4841         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4842
4843         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4844         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4845         NUMS=$($CMD | wc -l)
4846         [ $NUMS -eq $EXPECTED ] ||
4847                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4848
4849         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4850         NUMS=$($CMD | wc -l)
4851         [ $NUMS -eq $EXPECTED ] ||
4852                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4853
4854         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4855         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4856         NUMS=$($CMD | wc -l)
4857         [ $NUMS -eq $EXPECTED ] ||
4858                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4859
4860         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4861         NUMS=$($CMD | wc -l)
4862         [ $NUMS -eq $EXPECTED ] ||
4863                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4864
4865         EXPECTED=4
4866         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4867         NUMS=$($CMD | wc -l)
4868         [ $NUMS -eq $EXPECTED ] ||
4869                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4870
4871         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4872         NUMS=$($CMD | wc -l)
4873         [ $NUMS -eq $EXPECTED ] ||
4874                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4875
4876         EXPECTED=0
4877         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4878         NUMS=$($CMD | wc -l)
4879         [ $NUMS -eq $EXPECTED ] ||
4880                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4881 }
4882 run_test 56t "check lfs find -stripe-size works"
4883
4884 test_56u() { # LU-611
4885         TDIR=$DIR/${tdir}u
4886         setup_56 $NUMFILES $NUMDIRS "-i 0"
4887
4888         if [[ $OSTCOUNT -gt 1 ]]; then
4889                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4890                 ONESTRIPE=4
4891         else
4892                 ONESTRIPE=0
4893         fi
4894
4895         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4896         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4897         NUMS=$($CMD | wc -l)
4898         [ $NUMS -eq $EXPECTED ] ||
4899                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4900
4901         EXPECTED=$ONESTRIPE
4902         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4903         NUMS=$($CMD | wc -l)
4904         [ $NUMS -eq $EXPECTED ] ||
4905                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4906
4907         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4908         NUMS=$($CMD | wc -l)
4909         [ $NUMS -eq $EXPECTED ] ||
4910                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4911
4912         EXPECTED=0
4913         # This should produce an error and not return any files
4914         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4915         NUMS=$($CMD 2>/dev/null | wc -l)
4916         [ $NUMS -eq $EXPECTED ] ||
4917                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4918
4919         if [[ $OSTCOUNT -gt 1 ]]; then
4920                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4921                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4922                 NUMS=$($CMD | wc -l)
4923                 [ $NUMS -eq $EXPECTED ] ||
4924                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4925         fi
4926 }
4927 run_test 56u "check lfs find -stripe-index works"
4928
4929 test_56v() {
4930     local MDT_IDX=0
4931
4932     TDIR=$DIR/${tdir}v
4933     rm -rf $TDIR
4934     setup_56 $NUMFILES $NUMDIRS
4935
4936     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4937     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4938
4939     for file in $($LFIND -mdt $UUID $TDIR); do
4940         file_mdt_idx=$($GETSTRIPE -M $file)
4941         [ $file_mdt_idx -eq $MDT_IDX ] ||
4942             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4943     done
4944 }
4945 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4946
4947 test_56w() {
4948         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4949                 return
4950         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4951         TDIR=$DIR/${tdir}w
4952
4953     rm -rf $TDIR || error "remove $TDIR failed"
4954     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4955
4956     local stripe_size
4957     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4958         error "$GETSTRIPE -S -d $TDIR failed"
4959     stripe_size=${stripe_size%% *}
4960
4961     local file_size=$((stripe_size * OSTCOUNT))
4962     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4963     local required_space=$((file_num * file_size))
4964
4965     local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
4966                         head -n1)
4967     [[ $free_space -le $((required_space / 1024)) ]] &&
4968         skip_env "need at least $required_space bytes free space," \
4969                  "have $free_space kbytes" && return
4970
4971     local dd_bs=65536
4972     local dd_count=$((file_size / dd_bs))
4973
4974     # write data into the files
4975     local i
4976     local j
4977     local file
4978     for i in $(seq 1 $NUMFILES); do
4979         file=$TDIR/file$i
4980         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4981             error "write data into $file failed"
4982     done
4983     for i in $(seq 1 $NUMDIRS); do
4984         for j in $(seq 1 $NUMFILES); do
4985             file=$TDIR/dir$i/file$j
4986             yes | dd bs=$dd_bs count=$dd_count of=$file \
4987                 >/dev/null 2>&1 ||
4988                 error "write data into $file failed"
4989         done
4990     done
4991
4992     local expected=-1
4993     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4994
4995     # lfs_migrate file
4996     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4997     echo "$cmd"
4998     eval $cmd || error "$cmd failed"
4999
5000     check_stripe_count $TDIR/file1 $expected
5001
5002         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5003         then
5004                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5005                 # OST 1 if it is on OST 0. This file is small enough to
5006                 # be on only one stripe.
5007                 file=$TDIR/migr_1_ost
5008                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5009                         error "write data into $file failed"
5010                 local obdidx=$($LFS getstripe -i $file)
5011                 local oldmd5=$(md5sum $file)
5012                 local newobdidx=0
5013                 [[ $obdidx -eq 0 ]] && newobdidx=1
5014                 cmd="$LFS migrate -i $newobdidx $file"
5015                 echo $cmd
5016                 eval $cmd || error "$cmd failed"
5017                 local realobdix=$($LFS getstripe -i $file)
5018                 local newmd5=$(md5sum $file)
5019                 [[ $newobdidx -ne $realobdix ]] &&
5020                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
5021                 [[ "$oldmd5" != "$newmd5" ]] &&
5022                         error "md5sum differ: $oldmd5, $newmd5"
5023         fi
5024
5025     # lfs_migrate dir
5026     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
5027     echo "$cmd"
5028     eval $cmd || error "$cmd failed"
5029
5030     for j in $(seq 1 $NUMFILES); do
5031         check_stripe_count $TDIR/dir1/file$j $expected
5032     done
5033
5034     # lfs_migrate works with lfs find
5035     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
5036          $LFS_MIGRATE -y -c $expected"
5037     echo "$cmd"
5038     eval $cmd || error "$cmd failed"
5039
5040     for i in $(seq 2 $NUMFILES); do
5041         check_stripe_count $TDIR/file$i $expected
5042     done
5043     for i in $(seq 2 $NUMDIRS); do
5044         for j in $(seq 1 $NUMFILES); do
5045             check_stripe_count $TDIR/dir$i/file$j $expected
5046         done
5047     done
5048 }
5049 run_test 56w "check lfs_migrate -c stripe_count works"
5050
5051 test_56x() {
5052         check_swap_layouts_support && return 0
5053         [[ $OSTCOUNT -lt 2 ]] &&
5054                 skip_env "need 2 OST, skipping test" && return
5055
5056         local dir0=$DIR/$tdir/$testnum
5057         test_mkdir -p $dir0 || error "creating dir $dir0"
5058
5059         local ref1=/etc/passwd
5060         local file1=$dir0/file1
5061
5062         $SETSTRIPE -c 2 $file1
5063         cp $ref1 $file1
5064         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5065         stripe=$($GETSTRIPE -c $file1)
5066         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5067         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5068
5069         # clean up
5070         rm -f $file1
5071 }
5072 run_test 56x "lfs migration support"
5073
5074 test_56xa() {
5075         check_swap_layouts_support && return 0
5076         [[ $OSTCOUNT -lt 2 ]] &&
5077                 skip_env "need 2 OST, skipping test" && return
5078
5079         local dir0=$DIR/$tdir/$testnum
5080         test_mkdir -p $dir0 || error "creating dir $dir0"
5081
5082         local ref1=/etc/passwd
5083         local file1=$dir0/file1
5084
5085         $SETSTRIPE -c 2 $file1
5086         cp $ref1 $file1
5087         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5088         local stripe=$($GETSTRIPE -c $file1)
5089         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5090         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5091
5092         # clean up
5093         rm -f $file1
5094 }
5095 run_test 56xa "lfs migration --block support"
5096
5097 test_56y() {
5098         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5099                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5100                 return
5101
5102         local res=""
5103         local dir0=$DIR/$tdir/$testnum
5104         test_mkdir -p $dir0 || error "creating dir $dir0"
5105         local f1=$dir0/file1
5106         local f2=$dir0/file2
5107
5108         touch $f1 || error "creating std file $f1"
5109         $MULTIOP $f2 H2c || error "creating released file $f2"
5110
5111         # a directory can be raid0, so ask only for files
5112         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
5113         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5114
5115         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5116         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5117
5118         # only files can be released, so no need to force file search
5119         res=$($LFIND $dir0 -L released)
5120         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5121
5122         res=$($LFIND $dir0 \! -L released)
5123         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5124
5125 }
5126 run_test 56y "lfs find -L raid0|released"
5127
5128 test_56z() { # LU-4824
5129         # This checks to make sure 'lfs find' continues after errors
5130         # There are two classes of errors that should be caught:
5131         # - If multiple paths are provided, all should be searched even if one
5132         #   errors out
5133         # - If errors are encountered during the search, it should not terminate
5134         #   early
5135         local i
5136         test_mkdir $DIR/$tdir
5137         for i in d{0..9}; do
5138                 test_mkdir $DIR/$tdir/$i
5139         done
5140         touch $DIR/$tdir/d{0..9}/$tfile
5141         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5142                 error "$LFS find did not return an error"
5143         # Make a directory unsearchable. This should NOT be the last entry in
5144         # directory order.  Arbitrarily pick the 6th entry
5145         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5146         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5147         # The user should be able to see 10 directories and 9 files
5148         [ $count == 19 ] || error "$LFS find did not continue after error"
5149 }
5150 run_test 56z "lfs find should continue after an error"
5151
5152 test_56aa() { # LU-5937
5153         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5154
5155         mkdir $DIR/$tdir
5156         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5157
5158         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5159         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5160
5161         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5162 }
5163 run_test 56aa "lfs find --size under striped dir"
5164
5165 test_57a() {
5166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5167         # note test will not do anything if MDS is not local
5168         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5169                 skip "Only applicable to ldiskfs-based MDTs"
5170                 return
5171         fi
5172
5173         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5174         local MNTDEV="osd*.*MDT*.mntdev"
5175         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5176         [ -z "$DEV" ] && error "can't access $MNTDEV"
5177         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5178                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5179                         error "can't access $DEV"
5180                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5181                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5182                 rm $TMP/t57a.dump
5183         done
5184 }
5185 run_test 57a "verify MDS filesystem created with large inodes =="
5186
5187 test_57b() {
5188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5189         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5190                 skip "Only applicable to ldiskfs-based MDTs"
5191                 return
5192         fi
5193
5194         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5195         local dir=$DIR/$tdir
5196
5197         local FILECOUNT=100
5198         local FILE1=$dir/f1
5199         local FILEN=$dir/f$FILECOUNT
5200
5201         rm -rf $dir || error "removing $dir"
5202         test_mkdir -p -c1 $dir || error "creating $dir"
5203         local mdtidx=$($LFS getstripe -M $dir)
5204         local mdtname=MDT$(printf %04x $mdtidx)
5205         local facet=mds$((mdtidx + 1))
5206
5207         echo "mcreating $FILECOUNT files"
5208         createmany -m $dir/f 1 $FILECOUNT || \
5209                 error "creating files in $dir"
5210
5211         # verify that files do not have EAs yet
5212         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5213         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5214
5215         sync
5216         sleep 1
5217         df $dir  #make sure we get new statfs data
5218         local MDSFREE=$(do_facet $facet \
5219                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5220         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5221         echo "opening files to create objects/EAs"
5222         local FILE
5223         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5224                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5225         done
5226
5227         # verify that files have EAs now
5228         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5229         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5230
5231         sleep 1  #make sure we get new statfs data
5232         df $dir
5233         local MDSFREE2=$(do_facet $facet \
5234                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5235         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5236         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5237                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5238                         error "MDC before $MDCFREE != after $MDCFREE2"
5239                 else
5240                         echo "MDC before $MDCFREE != after $MDCFREE2"
5241                         echo "unable to confirm if MDS has large inodes"
5242                 fi
5243         fi
5244         rm -rf $dir
5245 }
5246 run_test 57b "default LOV EAs are stored inside large inodes ==="
5247
5248 test_58() {
5249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5250         [ -z "$(which wiretest 2>/dev/null)" ] &&
5251                         skip_env "could not find wiretest" && return
5252         wiretest
5253 }
5254 run_test 58 "verify cross-platform wire constants =============="
5255
5256 test_59() {
5257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5258         echo "touch 130 files"
5259         createmany -o $DIR/f59- 130
5260         echo "rm 130 files"
5261         unlinkmany $DIR/f59- 130
5262         sync
5263         # wait for commitment of removal
5264         wait_delete_completed
5265 }
5266 run_test 59 "verify cancellation of llog records async ========="
5267
5268 TEST60_HEAD="test_60 run $RANDOM"
5269 test_60a() {
5270         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5271         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5272         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5273                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5274                         skip_env "missing subtest run-llog.sh" && return
5275
5276         log "$TEST60_HEAD - from kernel mode"
5277         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5278         do_facet mgs sh run-llog.sh
5279         do_facet mgs $LCTL dk > $TMP/$tfile
5280
5281         # LU-6388: test llog_reader
5282         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5283         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5284         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5285                         skip_env "missing llog_reader" && return
5286         local fstype=$(facet_fstype mgs)
5287         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5288                 skip_env "Only for ldiskfs or zfs type mgs" && return
5289
5290         local mntpt=$(facet_mntpt mgs)
5291         local mgsdev=$(mgsdevname 1)
5292         local fid_list
5293         local fid
5294         local rec_list
5295         local rec
5296         local rec_type
5297         local obj_file
5298         local path
5299         local seq
5300         local oid
5301         local pass=true
5302
5303         #get fid and record list
5304         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5305                 tail -n 4))
5306         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5307                 tail -n 4))
5308         #remount mgs as ldiskfs or zfs type
5309         stop mgs || error "stop mgs failed"
5310         mount_fstype mgs || error "remount mgs failed"
5311         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5312                 fid=${fid_list[i]}
5313                 rec=${rec_list[i]}
5314                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5315                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5316                 oid=$((16#$oid))
5317
5318                 case $fstype in
5319                         ldiskfs )
5320                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5321                         zfs )
5322                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5323                 esac
5324                 echo "obj_file is $obj_file"
5325                 do_facet mgs $llog_reader $obj_file
5326
5327                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5328                         awk '{ print $3 }' | sed -e "s/^type=//g")
5329                 if [ $rec_type != $rec ]; then
5330                         echo "FAILED test_60a wrong record type $rec_type," \
5331                               "should be $rec"
5332                         pass=false
5333                         break
5334                 fi
5335
5336                 #check obj path if record type is LLOG_LOGID_MAGIC
5337                 if [ "$rec" == "1064553b" ]; then
5338                         path=$(do_facet mgs $llog_reader $obj_file |
5339                                 grep "path=" | awk '{ print $NF }' |
5340                                 sed -e "s/^path=//g")
5341                         if [ $obj_file != $mntpt/$path ]; then
5342                                 echo "FAILED test_60a wrong obj path" \
5343                                       "$montpt/$path, should be $obj_file"
5344                                 pass=false
5345                                 break
5346                         fi
5347                 fi
5348         done
5349         rm -f $TMP/$tfile
5350         #restart mgs before "error", otherwise it will block the next test
5351         stop mgs || error "stop mgs failed"
5352         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5353         $pass || error "test failed, see FAILED test_60a messages for specifics"
5354 }
5355 run_test 60a "llog_test run from kernel module and test llog_reader =========="
5356
5357 test_60b() { # bug 6411
5358         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5359         dmesg > $DIR/$tfile
5360         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5361                                 /llog.test/ {
5362                                         if (marker)
5363                                                 from_marker++
5364                                         from_begin++
5365                                 }
5366                                 END {
5367                                         if (marker)
5368                                                 print from_marker
5369                                         else
5370                                                 print from_begin
5371                                 }")
5372         [[ $LLOG_COUNT -gt 100 ]] &&
5373                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5374 }
5375 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5376
5377 test_60c() {
5378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5379         echo "create 5000 files"
5380         createmany -o $DIR/f60c- 5000
5381 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5382         lctl set_param fail_loc=0x80000137
5383         unlinkmany $DIR/f60c- 5000
5384         lctl set_param fail_loc=0
5385 }
5386 run_test 60c "unlink file when mds full"
5387
5388 test_60d() {
5389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5390         SAVEPRINTK=$(lctl get_param -n printk)
5391
5392         # verify "lctl mark" is even working"
5393         MESSAGE="test message ID $RANDOM $$"
5394         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5395         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5396
5397         lctl set_param printk=0 || error "set lnet.printk failed"
5398         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5399         MESSAGE="new test message ID $RANDOM $$"
5400         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5401         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5402         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5403
5404         lctl set_param -n printk="$SAVEPRINTK"
5405 }
5406 run_test 60d "test printk console message masking"
5407
5408 test_60e() {
5409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5410         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5411         touch $DIR/$tfile
5412 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5413         do_facet mds1 lctl set_param fail_loc=0x15b
5414         rm $DIR/$tfile
5415 }
5416 run_test 60e "no space while new llog is being created"
5417
5418 test_61() {
5419         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5420         f="$DIR/f61"
5421         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5422         cancel_lru_locks osc
5423         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5424         sync
5425 }
5426 run_test 61 "mmap() writes don't make sync hang ================"
5427
5428 # bug 2330 - insufficient obd_match error checking causes LBUG
5429 test_62() {
5430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5431         f="$DIR/f62"
5432         echo foo > $f
5433         cancel_lru_locks osc
5434         lctl set_param fail_loc=0x405
5435         cat $f && error "cat succeeded, expect -EIO"
5436         lctl set_param fail_loc=0
5437 }
5438 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5439 # match every page all of the time.
5440 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5441
5442 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5443 # Though this test is irrelevant anymore, it helped to reveal some
5444 # other grant bugs (LU-4482), let's keep it.
5445 test_63a() {   # was test_63
5446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5447         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5448         for i in `seq 10` ; do
5449                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5450                 sleep 5
5451                 kill $!
5452                 sleep 1
5453         done
5454
5455         rm -f $DIR/f63 || true
5456 }
5457 run_test 63a "Verify oig_wait interruption does not crash ======="
5458
5459 # bug 2248 - async write errors didn't return to application on sync
5460 # bug 3677 - async write errors left page locked
5461 test_63b() {
5462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5463         debugsave
5464         lctl set_param debug=-1
5465
5466         # ensure we have a grant to do async writes
5467         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5468         rm $DIR/$tfile
5469
5470         sync    # sync lest earlier test intercept the fail_loc
5471
5472         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5473         lctl set_param fail_loc=0x80000406
5474         $MULTIOP $DIR/$tfile Owy && \
5475                 error "sync didn't return ENOMEM"
5476         sync; sleep 2; sync     # do a real sync this time to flush page
5477         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5478                 error "locked page left in cache after async error" || true
5479         debugrestore
5480 }
5481 run_test 63b "async write errors should be returned to fsync ==="
5482
5483 test_64a () {
5484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5485         df $DIR
5486         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5487 }
5488 run_test 64a "verify filter grant calculations (in kernel) ====="
5489
5490 test_64b () {
5491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5492         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5493 }
5494 run_test 64b "check out-of-space detection on client ==========="
5495
5496 test_64c() {
5497         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5498 }
5499 run_test 64c "verify grant shrink ========================------"
5500
5501 # bug 1414 - set/get directories' stripe info
5502 test_65a() {
5503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5504         test_mkdir -p $DIR/$tdir
5505         touch $DIR/$tdir/f1
5506         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5507 }
5508 run_test 65a "directory with no stripe info ===================="
5509
5510 test_65b() {
5511         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5512         test_mkdir -p $DIR/$tdir
5513         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5514
5515         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5516                                                 error "setstripe"
5517         touch $DIR/$tdir/f2
5518         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5519 }
5520 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
5521
5522 test_65c() {
5523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5524         if [[ $OSTCOUNT -gt 1 ]]; then
5525                 test_mkdir -p $DIR/$tdir
5526                 local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5527
5528                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5529                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5530                 touch $DIR/$tdir/f3
5531                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5532         fi
5533 }
5534 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
5535
5536 test_65d() {
5537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5538         test_mkdir -p $DIR/$tdir
5539         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
5540         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5541
5542         if [[ $STRIPECOUNT -le 0 ]]; then
5543                 sc=1
5544         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5545 #LOV_MAX_STRIPE_COUNT is 2000
5546                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5547         else
5548                 sc=$(($STRIPECOUNT - 1))
5549         fi
5550         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5551         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5552         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5553                 error "lverify failed"
5554 }
5555 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
5556
5557 test_65e() {
5558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5559         test_mkdir -p $DIR/$tdir
5560
5561         $SETSTRIPE $DIR/$tdir || error "setstripe"
5562         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5563                                         error "no stripe info failed"
5564         touch $DIR/$tdir/f6
5565         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5566 }
5567 run_test 65e "directory setstripe defaults ======================="
5568
5569 test_65f() {
5570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5571         test_mkdir -p $DIR/${tdir}f
5572         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5573 }
5574 run_test 65f "dir setstripe permission (should return error) ==="
5575
5576 test_65g() {
5577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5578         test_mkdir -p $DIR/$tdir
5579         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5580
5581         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5582                                                         error "setstripe"
5583         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5584         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5585                 error "delete default stripe failed"
5586 }
5587 run_test 65g "directory setstripe -d ==========================="
5588
5589 test_65h() {
5590         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5591         test_mkdir -p $DIR/$tdir
5592         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5593
5594         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5595                                                         error "setstripe"
5596         test_mkdir -p $DIR/$tdir/dd1
5597         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5598                 error "stripe info inherit failed"
5599 }
5600 run_test 65h "directory stripe info inherit ===================="
5601
5602 test_65i() { # bug6367
5603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5604         $SETSTRIPE -S 65536 -c -1 $MOUNT
5605 }
5606 run_test 65i "set non-default striping on root directory (bug 6367)="
5607
5608 test_65ia() { # bug12836
5609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5610         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5611 }
5612 run_test 65ia "getstripe on -1 default directory striping"
5613
5614 test_65ib() { # bug12836
5615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5616         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5617 }
5618 run_test 65ib "getstripe -v on -1 default directory striping"
5619
5620 test_65ic() { # bug12836
5621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5622         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5623 }
5624 run_test 65ic "new find on -1 default directory striping"
5625
5626 test_65j() { # bug6367
5627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5628         sync; sleep 1
5629         # if we aren't already remounting for each test, do so for this test
5630         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5631                 cleanup || error "failed to unmount"
5632                 setup
5633         fi
5634         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5635 }
5636 run_test 65j "set default striping on root directory (bug 6367)="
5637
5638 test_65k() { # bug11679
5639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5640         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5641         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5642
5643         local disable_precreate=true
5644         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
5645                 disable_precreate=false
5646
5647         echo "Check OST status: "
5648         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
5649                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
5650
5651         for OSC in $MDS_OSCS; do
5652                 echo $OSC "is active"
5653                 do_facet $SINGLEMDS lctl --device %$OSC activate
5654         done
5655
5656         for INACTIVE_OSC in $MDS_OSCS; do
5657                 local ost=$(osc_to_ost $INACTIVE_OSC)
5658                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
5659                                lov.*md*.target_obd |
5660                                awk -F: /$ost/'{ print $1 }' | head -n 1)
5661
5662                 mkdir -p $DIR/$tdir
5663                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
5664                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
5665
5666                 echo "Deactivate: " $INACTIVE_OSC
5667                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5668
5669                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
5670                               osp.$ost*MDT0000.create_count")
5671                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
5672                                   osp.$ost*MDT0000.max_create_count")
5673                 $disable_precreate &&
5674                         do_facet $SINGLEMDS "lctl set_param -n \
5675                                 osp.$ost*MDT0000.max_create_count=0"
5676
5677                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
5678                         [ -f $DIR/$tdir/$idx ] && continue
5679                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
5680                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
5681                                 error "setstripe $idx should succeed"
5682                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
5683                 done
5684                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
5685                 rmdir $DIR/$tdir
5686
5687                 do_facet $SINGLEMDS "lctl set_param -n \
5688                         osp.$ost*MDT0000.max_create_count=$max_count"
5689                 do_facet $SINGLEMDS "lctl set_param -n \
5690                         osp.$ost*MDT0000.create_count=$count"
5691                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5692                 echo $INACTIVE_OSC "is Activate"
5693
5694                 wait_osc_import_state mds ost$ostnum FULL
5695         done
5696 }
5697 run_test 65k "validate manual striping works properly with deactivated OSCs"
5698
5699 test_65l() { # bug 12836
5700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5701         test_mkdir -p $DIR/$tdir/test_dir
5702         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5703         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5704 }
5705 run_test 65l "lfs find on -1 stripe dir ========================"
5706
5707 test_65m() {
5708         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
5709         true
5710 }
5711 run_test 65m "normal user can't set filesystem default stripe"
5712
5713 # bug 2543 - update blocks count on client
5714 test_66() {
5715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5716         COUNT=${COUNT:-8}
5717         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5718         sync; sync_all_data; sync; sync_all_data
5719         cancel_lru_locks osc
5720         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5721         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5722 }
5723 run_test 66 "update inode blocks count on client ==============="
5724
5725 LLOOP=
5726 LLITELOOPLOAD=
5727 cleanup_68() {
5728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5729         trap 0
5730         if [ ! -z "$LLOOP" ]; then
5731                 if swapon -s | grep -q $LLOOP; then
5732                         swapoff $LLOOP || error "swapoff failed"
5733                 fi
5734
5735                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5736                 rm -f $LLOOP
5737                 unset LLOOP
5738         fi
5739         rm -f $DIR/f68*
5740 }
5741
5742 meminfo() {
5743         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5744 }
5745
5746 swap_used() {
5747         swapon -s | awk '($1 == "'$1'") { print $4 }'
5748 }
5749
5750 # bug5265, obdfilter oa2dentry return -ENOENT
5751 # #define OBD_FAIL_SRV_ENOENT 0x217
5752 test_69() {
5753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5754         remote_ost_nodsh && skip "remote OST with nodsh" && return
5755
5756         f="$DIR/$tfile"
5757         $SETSTRIPE -c 1 -i 0 $f
5758
5759         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5760
5761         do_facet ost1 lctl set_param fail_loc=0x217
5762         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5763         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5764
5765         do_facet ost1 lctl set_param fail_loc=0
5766         $DIRECTIO write $f 0 2 || error "write error"
5767
5768         cancel_lru_locks osc
5769         $DIRECTIO read $f 0 1 || error "read error"
5770
5771         do_facet ost1 lctl set_param fail_loc=0x217
5772         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5773
5774         do_facet ost1 lctl set_param fail_loc=0
5775         rm -f $f
5776 }
5777 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5778
5779 test_71() {
5780         test_mkdir -p $DIR/$tdir
5781         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5782         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5783 }
5784 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5785
5786 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5787         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5788         [ "$RUNAS_ID" = "$UID" ] &&
5789                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5790
5791         # Check that testing environment is properly set up. Skip if not
5792         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5793                 skip_env "User $RUNAS_ID does not exist - skipping"
5794                 return 0
5795         }
5796         touch $DIR/$tfile
5797         chmod 777 $DIR/$tfile
5798         chmod ug+s $DIR/$tfile
5799         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5800                 error "$RUNAS dd $DIR/$tfile failed"
5801         # See if we are still setuid/sgid
5802         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5803                 error "S/gid is not dropped on write"
5804         # Now test that MDS is updated too
5805         cancel_lru_locks mdc
5806         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5807                 error "S/gid is not dropped on MDS"
5808         rm -f $DIR/$tfile
5809 }
5810 run_test 72a "Test that remove suid works properly (bug5695) ===="
5811
5812 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5813         local perm
5814
5815         [ "$RUNAS_ID" = "$UID" ] && \
5816                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5817         [ "$RUNAS_ID" -eq 0 ] && \
5818                 skip_env "RUNAS_ID = 0 -- skipping" && return
5819
5820         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5821         # Check that testing environment is properly set up. Skip if not
5822         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5823                 skip_env "User $RUNAS_ID does not exist - skipping"
5824                 return 0
5825         }
5826         touch $DIR/${tfile}-f{g,u}
5827         test_mkdir $DIR/${tfile}-dg
5828         test_mkdir $DIR/${tfile}-du
5829         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5830         chmod g+s $DIR/${tfile}-{f,d}g
5831         chmod u+s $DIR/${tfile}-{f,d}u
5832         for perm in 777 2777 4777; do
5833                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5834                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5835                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5836                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5837         done
5838         true
5839 }
5840 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5841
5842 # bug 3462 - multiple simultaneous MDC requests
5843 test_73() {
5844         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5845         test_mkdir $DIR/d73-1
5846         test_mkdir $DIR/d73-2
5847         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5848         pid1=$!
5849
5850         lctl set_param fail_loc=0x80000129
5851         $MULTIOP $DIR/d73-1/f73-2 Oc &
5852         sleep 1
5853         lctl set_param fail_loc=0
5854
5855         $MULTIOP $DIR/d73-2/f73-3 Oc &
5856         pid3=$!
5857
5858         kill -USR1 $pid1
5859         wait $pid1 || return 1
5860
5861         sleep 25
5862
5863         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5864         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5865         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5866
5867         rm -rf $DIR/d73-*
5868 }
5869 run_test 73 "multiple MDC requests (should not deadlock)"
5870
5871 test_74a() { # bug 6149, 6184
5872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5873         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5874         #
5875         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5876         # will spin in a tight reconnection loop
5877         touch $DIR/f74a
5878         $LCTL set_param fail_loc=0x8000030e
5879         # get any lock that won't be difficult - lookup works.
5880         ls $DIR/f74a
5881         $LCTL set_param fail_loc=0
5882         rm -f $DIR/f74a
5883         true
5884 }
5885 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5886
5887 test_74b() { # bug 13310
5888         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5889         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5890         #
5891         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5892         # will spin in a tight reconnection loop
5893         $LCTL set_param fail_loc=0x8000030e
5894         # get a "difficult" lock
5895         touch $DIR/f74b
5896         $LCTL set_param fail_loc=0
5897         rm -f $DIR/f74b
5898         true
5899 }
5900 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5901
5902 test_74c() {
5903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5904         #define OBD_FAIL_LDLM_NEW_LOCK
5905         $LCTL set_param fail_loc=0x319
5906         touch $DIR/$tfile && error "touch successful"
5907         $LCTL set_param fail_loc=0
5908         true
5909 }
5910 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5911
5912 num_inodes() {
5913         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5914 }
5915
5916 get_inode_slab_tunables() {
5917         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5918 }
5919
5920 set_inode_slab_tunables() {
5921         echo "lustre_inode_cache $1" > /proc/slabinfo
5922 }
5923
5924 test_76() { # Now for bug 20433, added originally in bug 1443
5925         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5926         local SLAB_SETTINGS=$(get_inode_slab_tunables)
5927         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
5928         # we cannot set limit below 1 which means 1 inode in each
5929         # per-cpu cache is still allowed
5930         set_inode_slab_tunables "1 1 0"
5931         cancel_lru_locks osc
5932         BEFORE_INODES=$(num_inodes)
5933         echo "before inodes: $BEFORE_INODES"
5934         local COUNT=1000
5935         [ "$SLOW" = "no" ] && COUNT=100
5936         for i in $(seq $COUNT); do
5937                 touch $DIR/$tfile
5938                 rm -f $DIR/$tfile
5939         done
5940         cancel_lru_locks osc
5941         AFTER_INODES=$(num_inodes)
5942         echo "after inodes: $AFTER_INODES"
5943         local wait=0
5944         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
5945                 sleep 2
5946                 AFTER_INODES=$(num_inodes)
5947                 wait=$((wait+2))
5948                 echo "wait $wait seconds inodes: $AFTER_INODES"
5949                 if [ $wait -gt 30 ]; then
5950                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5951                 fi
5952         done
5953         set_inode_slab_tunables "$SLAB_SETTINGS"
5954 }
5955 run_test 76 "confirm clients recycle inodes properly ===="
5956
5957
5958 export ORIG_CSUM=""
5959 set_checksums()
5960 {
5961         # Note: in sptlrpc modes which enable its own bulk checksum, the
5962         # original crc32_le bulk checksum will be automatically disabled,
5963         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5964         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5965         # In this case set_checksums() will not be no-op, because sptlrpc
5966         # bulk checksum will be enabled all through the test.
5967
5968         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5969         lctl set_param -n osc.*.checksums $1
5970         return 0
5971 }
5972
5973 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5974                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5975 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5976 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5977 set_checksum_type()
5978 {
5979         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5980         log "set checksum type to $1"
5981         return 0
5982 }
5983 F77_TMP=$TMP/f77-temp
5984 F77SZ=8
5985 setup_f77() {
5986         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5987                 error "error writing to $F77_TMP"
5988 }
5989
5990 test_77a() { # bug 10889
5991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5992         $GSS && skip "could not run with gss" && return
5993         [ ! -f $F77_TMP ] && setup_f77
5994         set_checksums 1
5995         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5996         set_checksums 0
5997         rm -f $DIR/$tfile
5998 }
5999 run_test 77a "normal checksum read/write operation"
6000
6001 test_77b() { # bug 10889
6002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6003         $GSS && skip "could not run with gss" && return
6004         [ ! -f $F77_TMP ] && setup_f77
6005         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6006         $LCTL set_param fail_loc=0x80000409
6007         set_checksums 1
6008
6009         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6010                 error "dd error: $?"
6011         $LCTL set_param fail_loc=0
6012
6013         for algo in $CKSUM_TYPES; do
6014                 cancel_lru_locks osc
6015                 set_checksum_type $algo
6016                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6017                 $LCTL set_param fail_loc=0x80000408
6018                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6019                 $LCTL set_param fail_loc=0
6020         done
6021         set_checksums 0
6022         set_checksum_type $ORIG_CSUM_TYPE
6023         rm -f $DIR/$tfile
6024 }
6025 run_test 77b "checksum error on client write, read"
6026
6027 test_77d() { # bug 10889
6028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6029         $GSS && skip "could not run with gss" && return
6030         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6031         $LCTL set_param fail_loc=0x80000409
6032         set_checksums 1
6033         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6034                 error "direct write: rc=$?"
6035         $LCTL set_param fail_loc=0
6036         set_checksums 0
6037
6038         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6039         $LCTL set_param fail_loc=0x80000408
6040         set_checksums 1
6041         cancel_lru_locks osc
6042         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6043                 error "direct read: rc=$?"
6044         $LCTL set_param fail_loc=0
6045         set_checksums 0
6046 }
6047 run_test 77d "checksum error on OST direct write, read"
6048
6049 test_77f() { # bug 10889
6050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6051         $GSS && skip "could not run with gss" && return
6052         set_checksums 1
6053         for algo in $CKSUM_TYPES; do
6054                 cancel_lru_locks osc
6055                 set_checksum_type $algo
6056                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6057                 $LCTL set_param fail_loc=0x409
6058                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6059                         error "direct write succeeded"
6060                 $LCTL set_param fail_loc=0
6061         done
6062         set_checksum_type $ORIG_CSUM_TYPE
6063         set_checksums 0
6064 }
6065 run_test 77f "repeat checksum error on write (expect error)"
6066
6067 test_77g() { # bug 10889
6068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6069         $GSS && skip "could not run with gss" && return
6070         remote_ost_nodsh && skip "remote OST with nodsh" && return
6071
6072         [ ! -f $F77_TMP ] && setup_f77
6073
6074         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6075         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6076         do_facet ost1 lctl set_param fail_loc=0x8000021a
6077         set_checksums 1
6078         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6079                 error "write error: rc=$?"
6080         do_facet ost1 lctl set_param fail_loc=0
6081         set_checksums 0
6082
6083         cancel_lru_locks osc
6084         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6085         do_facet ost1 lctl set_param fail_loc=0x8000021b
6086         set_checksums 1
6087         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6088         do_facet ost1 lctl set_param fail_loc=0
6089         set_checksums 0
6090 }
6091 run_test 77g "checksum error on OST write, read"
6092
6093 test_77i() { # bug 13805
6094         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6095         $GSS && skip "could not run with gss" && return
6096         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
6097         lctl set_param fail_loc=0x40b
6098         remount_client $MOUNT
6099         lctl set_param fail_loc=0
6100         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
6101                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6102                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
6103                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6104         done
6105         remount_client $MOUNT
6106 }
6107 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
6108
6109 test_77j() { # bug 13805
6110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6111         $GSS && skip "could not run with gss" && return
6112         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6113         lctl set_param fail_loc=0x40c
6114         remount_client $MOUNT
6115         lctl set_param fail_loc=0
6116         sleep 2 # wait async osc connect to finish
6117         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
6118                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6119                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
6120                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6121         done
6122         remount_client $MOUNT
6123 }
6124 run_test 77j "client only supporting ADLER32"
6125
6126 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6127 rm -f $F77_TMP
6128 unset F77_TMP
6129
6130 cleanup_test_78() {
6131         trap 0
6132         rm -f $DIR/$tfile
6133 }
6134
6135 test_78() { # bug 10901
6136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6137         remote_ost || { skip_env "local OST" && return; }
6138
6139         NSEQ=5
6140         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6141         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6142         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6143         echo "MemTotal: $MEMTOTAL"
6144
6145         # reserve 256MB of memory for the kernel and other running processes,
6146         # and then take 1/2 of the remaining memory for the read/write buffers.
6147         if [ $MEMTOTAL -gt 512 ] ;then
6148                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6149         else
6150                 # for those poor memory-starved high-end clusters...
6151                 MEMTOTAL=$((MEMTOTAL / 2))
6152         fi
6153         echo "Mem to use for directio: $MEMTOTAL"
6154
6155         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6156         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6157         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6158         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6159                 head -n1)
6160         echo "Smallest OST: $SMALLESTOST"
6161         [[ $SMALLESTOST -lt 10240 ]] &&
6162                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6163
6164         trap cleanup_test_78 EXIT
6165
6166         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6167                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6168
6169         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6170         echo "File size: $F78SIZE"
6171         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6172         for i in $(seq 1 $NSEQ); do
6173                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6174                 echo directIO rdwr round $i of $NSEQ
6175                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6176         done
6177
6178         cleanup_test_78
6179 }
6180 run_test 78 "handle large O_DIRECT writes correctly ============"
6181
6182 test_79() { # bug 12743
6183         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6184         wait_delete_completed
6185
6186         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6187         BKFREE=$(calc_osc_kbytes kbytesfree)
6188         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6189
6190         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6191         DFTOTAL=`echo $STRING | cut -d, -f1`
6192         DFUSED=`echo $STRING  | cut -d, -f2`
6193         DFAVAIL=`echo $STRING | cut -d, -f3`
6194         DFFREE=$(($DFTOTAL - $DFUSED))
6195
6196         ALLOWANCE=$((64 * $OSTCOUNT))
6197
6198         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6199            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6200                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6201         fi
6202         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6203            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6204                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6205         fi
6206         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6207            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6208                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6209         fi
6210 }
6211 run_test 79 "df report consistency check ======================="
6212
6213 test_80() { # bug 10718
6214         remote_ost_nodsh && skip "remote OST with nodsh" && return
6215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6216         # relax strong synchronous semantics for slow backends like ZFS
6217         local soc="obdfilter.*.sync_on_lock_cancel"
6218         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6219         local hosts=
6220         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6221                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6222                           facet_active_host $host; done | sort -u)
6223                 do_nodes $hosts lctl set_param $soc=never
6224         fi
6225
6226         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6227         sync; sleep 1; sync
6228         local BEFORE=`date +%s`
6229         cancel_lru_locks osc
6230         local AFTER=`date +%s`
6231         local DIFF=$((AFTER-BEFORE))
6232         if [ $DIFF -gt 1 ] ; then
6233                 error "elapsed for 1M@1T = $DIFF"
6234         fi
6235
6236         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6237
6238         rm -f $DIR/$tfile
6239 }
6240 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6241
6242 test_81a() { # LU-456
6243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6244         remote_ost_nodsh && skip "remote OST with nodsh" && return
6245         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6246         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6247         do_facet ost1 lctl set_param fail_loc=0x80000228
6248
6249         # write should trigger a retry and success
6250         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6251         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6252         RC=$?
6253         if [ $RC -ne 0 ] ; then
6254                 error "write should success, but failed for $RC"
6255         fi
6256 }
6257 run_test 81a "OST should retry write when get -ENOSPC ==============="
6258
6259 test_81b() { # LU-456
6260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6261         remote_ost_nodsh && skip "remote OST with nodsh" && return
6262         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6263         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6264         do_facet ost1 lctl set_param fail_loc=0x228
6265
6266         # write should retry several times and return -ENOSPC finally
6267         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6268         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6269         RC=$?
6270         ENOSPC=28
6271         if [ $RC -ne $ENOSPC ] ; then
6272                 error "dd should fail for -ENOSPC, but succeed."
6273         fi
6274 }
6275 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6276
6277 test_82() { # LU-1031
6278         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6279         local gid1=14091995
6280         local gid2=16022000
6281
6282         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6283         local MULTIPID1=$!
6284         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6285         local MULTIPID2=$!
6286         kill -USR1 $MULTIPID2
6287         sleep 2
6288         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6289                 error "First grouplock does not block second one"
6290         else
6291                 echo "Second grouplock blocks first one"
6292         fi
6293         kill -USR1 $MULTIPID1
6294         wait $MULTIPID1
6295         wait $MULTIPID2
6296 }
6297 run_test 82 "Basic grouplock test ==============================="
6298
6299 test_99a() {
6300         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6301                 return
6302         test_mkdir -p $DIR/d99cvsroot
6303         chown $RUNAS_ID $DIR/d99cvsroot
6304         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6305         cd $TMP
6306
6307         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6308         cd $oldPWD
6309 }
6310 run_test 99a "cvs init ========================================="
6311
6312 test_99b() {
6313         [ -z "$(which cvs 2>/dev/null)" ] &&
6314                 skip_env "could not find cvs" && return
6315         [ ! -d $DIR/d99cvsroot ] && test_99a
6316         cd /etc/init.d
6317         # some versions of cvs import exit(1) when asked to import links or
6318         # files they can't read.  ignore those files.
6319         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6320                         ! -perm /4 -printf '-I %f\n')
6321         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6322                 d99reposname vtag rtag
6323 }
6324 run_test 99b "cvs import ======================================="
6325
6326 test_99c() {
6327         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6328         [ ! -d $DIR/d99cvsroot ] && test_99b
6329         cd $DIR
6330         test_mkdir -p $DIR/d99reposname
6331         chown $RUNAS_ID $DIR/d99reposname
6332         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6333 }
6334 run_test 99c "cvs checkout ====================================="
6335
6336 test_99d() {
6337         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6338         [ ! -d $DIR/d99cvsroot ] && test_99c
6339         cd $DIR/d99reposname
6340         $RUNAS touch foo99
6341         $RUNAS cvs add -m 'addmsg' foo99
6342 }
6343 run_test 99d "cvs add =========================================="
6344
6345 test_99e() {
6346         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6347         [ ! -d $DIR/d99cvsroot ] && test_99c
6348         cd $DIR/d99reposname
6349         $RUNAS cvs update
6350 }
6351 run_test 99e "cvs update ======================================="
6352
6353 test_99f() {
6354         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6355         [ ! -d $DIR/d99cvsroot ] && test_99d
6356         cd $DIR/d99reposname
6357         $RUNAS cvs commit -m 'nomsg' foo99
6358     rm -fr $DIR/d99cvsroot
6359 }
6360 run_test 99f "cvs commit ======================================="
6361
6362 test_100() {
6363         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6364         [ "$NETTYPE" = tcp ] || \
6365                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6366                         return ; }
6367
6368         remote_ost_nodsh && skip "remote OST with nodsh" && return
6369         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6370         remote_servers || \
6371                 { skip "useless for local single node setup" && return; }
6372
6373         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6374                 [ "$PROT" != "tcp" ] && continue
6375                 RPORT=$(echo $REMOTE | cut -d: -f2)
6376                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6377
6378                 rc=0
6379                 LPORT=`echo $LOCAL | cut -d: -f2`
6380                 if [ $LPORT -ge 1024 ]; then
6381                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6382                         netstat -tna
6383                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6384                 fi
6385         done
6386         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6387 }
6388 run_test 100 "check local port using privileged port ==========="
6389
6390 function get_named_value()
6391 {
6392     local tag
6393
6394     tag=$1
6395     while read ;do
6396         line=$REPLY
6397         case $line in
6398         $tag*)
6399             echo $line | sed "s/^$tag[ ]*//"
6400             break
6401             ;;
6402         esac
6403     done
6404 }
6405
6406 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6407                    awk '/^max_cached_mb/ { print $2 }')
6408
6409 cleanup_101a() {
6410         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6411         trap 0
6412 }
6413
6414 test_101a() {
6415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6416         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6417         local s
6418         local discard
6419         local nreads=10000
6420         local cache_limit=32
6421
6422         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6423         trap cleanup_101a EXIT
6424         $LCTL set_param -n llite.*.read_ahead_stats 0
6425         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6426
6427         #
6428         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6429         #
6430         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6431         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6432
6433         discard=0
6434         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6435                 get_named_value 'read but discarded' | cut -d" " -f1); do
6436                         discard=$(($discard + $s))
6437         done
6438         cleanup_101a
6439
6440         if [[ $(($discard * 10)) -gt $nreads ]]; then
6441                 $LCTL get_param osc.*-osc*.rpc_stats
6442                 $LCTL get_param llite.*.read_ahead_stats
6443                 error "too many ($discard) discarded pages"
6444         fi
6445         rm -f $DIR/$tfile || true
6446 }
6447 run_test 101a "check read-ahead for random reads ================"
6448
6449 setup_test101bc() {
6450         test_mkdir -p $DIR/$tdir
6451         local STRIPE_SIZE=$1
6452         local FILE_LENGTH=$2
6453         STRIPE_OFFSET=0
6454
6455         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6456
6457         local list=$(comma_list $(osts_nodes))
6458         set_osd_param $list '' read_cache_enable 0
6459         set_osd_param $list '' writethrough_cache_enable 0
6460
6461         trap cleanup_test101bc EXIT
6462         # prepare the read-ahead file
6463         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6464
6465         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6466                                 count=$FILE_SIZE_MB 2> /dev/null
6467
6468 }
6469
6470 cleanup_test101bc() {
6471         trap 0
6472         rm -rf $DIR/$tdir
6473         rm -f $DIR/$tfile
6474
6475         local list=$(comma_list $(osts_nodes))
6476         set_osd_param $list '' read_cache_enable 1
6477         set_osd_param $list '' writethrough_cache_enable 1
6478 }
6479
6480 calc_total() {
6481         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6482 }
6483
6484 ra_check_101() {
6485         local READ_SIZE=$1
6486         local STRIPE_SIZE=$2
6487         local FILE_LENGTH=$3
6488         local RA_INC=1048576
6489         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6490         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6491                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6492         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6493                         get_named_value 'read but discarded' |
6494                         cut -d" " -f1 | calc_total)
6495         if [[ $DISCARD -gt $discard_limit ]]; then
6496                 $LCTL get_param llite.*.read_ahead_stats
6497                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6498         else
6499                 echo "Read-ahead success for size ${READ_SIZE}"
6500         fi
6501 }
6502
6503 test_101b() {
6504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6505         [[ $OSTCOUNT -lt 2 ]] &&
6506                 skip_env "skipping stride IO stride-ahead test" && return
6507         local STRIPE_SIZE=1048576
6508         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6509         if [ $SLOW == "yes" ]; then
6510                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6511         else
6512                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6513         fi
6514
6515         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6516
6517         # prepare the read-ahead file
6518         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6519         cancel_lru_locks osc
6520         for BIDX in 2 4 8 16 32 64 128 256
6521         do
6522                 local BSIZE=$((BIDX*4096))
6523                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6524                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6525                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6526                 $LCTL set_param -n llite.*.read_ahead_stats 0
6527                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6528                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6529                 cancel_lru_locks osc
6530                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6531         done
6532         cleanup_test101bc
6533         true
6534 }
6535 run_test 101b "check stride-io mode read-ahead ================="
6536
6537 test_101c() {
6538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6539         local STRIPE_SIZE=1048576
6540         local FILE_LENGTH=$((STRIPE_SIZE*100))
6541         local nreads=10000
6542         local osc_rpc_stats
6543
6544         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6545
6546         cancel_lru_locks osc
6547         $LCTL set_param osc.*.rpc_stats 0
6548         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6549         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6550                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6551                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6552                 local size
6553
6554                 if [ $lines -le 20 ]; then
6555                         continue
6556                 fi
6557                 for size in 1 2 4 8; do
6558                         local rpc=$(echo "$stats" |
6559                                     awk '($1 == "'$size':") {print $2; exit; }')
6560                         [ $rpc != 0 ] &&
6561                                 error "Small $((size*4))k read IO $rpc !"
6562                 done
6563                 echo "$osc_rpc_stats check passed!"
6564         done
6565         cleanup_test101bc
6566         true
6567 }
6568 run_test 101c "check stripe_size aligned read-ahead ================="
6569
6570 set_read_ahead() {
6571         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6572         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6573 }
6574
6575 test_101d() {
6576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6577         local file=$DIR/$tfile
6578         local sz_MB=${FILESIZE_101d:-500}
6579         local ra_MB=${READAHEAD_MB:-40}
6580
6581         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6582         [ $free_MB -lt $sz_MB ] &&
6583                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6584
6585         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6586         $SETSTRIPE -c -1 $file || error "setstripe failed"
6587
6588         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6589         echo Cancel LRU locks on lustre client to flush the client cache
6590         cancel_lru_locks osc
6591
6592         echo Disable read-ahead
6593         local old_READAHEAD=$(set_read_ahead 0)
6594
6595         echo Reading the test file $file with read-ahead disabled
6596         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6597
6598         echo Cancel LRU locks on lustre client to flush the client cache
6599         cancel_lru_locks osc
6600         echo Enable read-ahead with ${ra_MB}MB
6601         set_read_ahead $ra_MB
6602
6603         echo Reading the test file $file with read-ahead enabled
6604         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6605
6606         echo "read-ahead disabled time read $raOFF"
6607         echo "read-ahead enabled  time read $raON"
6608
6609         set_read_ahead $old_READAHEAD
6610         rm -f $file
6611         wait_delete_completed
6612
6613         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6614                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6615 }
6616 run_test 101d "file read with and without read-ahead enabled"
6617
6618 test_101e() {
6619         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6620         local file=$DIR/$tfile
6621         local size_KB=500  #KB
6622         local count=100
6623         local bsize=1024
6624
6625         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6626         local need_KB=$((count * size_KB))
6627         [[ $free_KB -le $need_KB ]] &&
6628                 skip_env "Need free space $need_KB, have $free_KB" && return
6629
6630         echo "Creating $count ${size_KB}K test files"
6631         for ((i = 0; i < $count; i++)); do
6632                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6633         done
6634
6635         echo "Cancel LRU locks on lustre client to flush the client cache"
6636         cancel_lru_locks osc
6637
6638         echo "Reset readahead stats"
6639         $LCTL set_param -n llite.*.read_ahead_stats 0
6640
6641         for ((i = 0; i < $count; i++)); do
6642                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6643         done
6644
6645         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6646                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6647
6648         for ((i = 0; i < $count; i++)); do
6649                 rm -rf $file.$i 2>/dev/null
6650         done
6651
6652         #10000 means 20% reads are missing in readahead
6653         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6654 }
6655 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6656
6657 test_101f() {
6658         which iozone || { skip "no iozone installed" && return; }
6659
6660         local old_debug=$($LCTL get_param debug)
6661         old_debug=${old_debug#*=}
6662         $LCTL set_param debug="reada mmap"
6663
6664         # create a test file
6665         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
6666
6667         echo Cancel LRU locks on lustre client to flush the client cache
6668         cancel_lru_locks osc
6669
6670         echo Reset readahead stats
6671         $LCTL set_param -n llite.*.read_ahead_stats 0
6672
6673         echo mmap read the file with small block size
6674         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
6675                 > /dev/null 2>&1
6676
6677         echo checking missing pages
6678         $LCTL get_param llite.*.read_ahead_stats
6679         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6680                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
6681
6682         $LCTL set_param debug="$old_debug"
6683         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
6684         rm -f $DIR/$tfile
6685 }
6686 run_test 101f "check mmap read performance"
6687
6688 test_101g() {
6689         local rpcs
6690         local osts=$(get_facets OST)
6691         local list=$(comma_list $(osts_nodes))
6692         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6693
6694         save_lustre_params $osts "obdfilter.*.brw_size" > $p
6695
6696         $LFS setstripe -c 1 $DIR/$tfile
6697
6698         if [ $(lustre_version_code ost1) -ge $(version_code 2.8.52) ]; then
6699                 set_osd_param $list '' brw_size 16M
6700
6701                 echo "remount client to enable large RPC size"
6702                 remount_client $MOUNT || error "remount_client failed"
6703
6704                 for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
6705                         [ "$mp" -eq 4096 ] ||
6706                                 error "max_pages_per_rpc not correctly set"
6707                 done
6708
6709                 $LCTL set_param -n osc.*.rpc_stats=0
6710
6711                 # 10*16 MiB should be enough for the test
6712                 dd if=/dev/zero of=$DIR/$tfile bs=16M count=10
6713                 cancel_lru_locks osc
6714                 dd of=/dev/null if=$DIR/$tfile bs=16M count=10
6715
6716                 # calculate 16 MiB RPCs
6717                 rpcs=$($LCTL get_param 'osc.*.rpc_stats' |
6718                        sed -n '/pages per rpc/,/^$/p' |
6719                        awk 'BEGIN { sum = 0 }; /4096:/ { sum += $2 };
6720                             END { print sum }')
6721                 echo $rpcs RPCs
6722                 [ "$rpcs" -eq 10 ] || error "not all RPCs are 16 MiB BRW rpcs"
6723         fi
6724
6725         echo "set RPC size to 4MB"
6726
6727         $LCTL set_param -n osc.*.max_pages_per_rpc=4M osc.*.rpc_stats=0
6728         dd if=/dev/zero of=$DIR/$tfile bs=4M count=25
6729         cancel_lru_locks osc
6730         dd of=/dev/null if=$DIR/$tfile bs=4M count=25
6731
6732         # calculate 4 MiB RPCs
6733         rpcs=$($LCTL get_param 'osc.*.rpc_stats' |
6734                 sed -n '/pages per rpc/,/^$/p' |
6735                 awk 'BEGIN { sum = 0 }; /1024:/ { sum += $2 };
6736                      END { print sum }')
6737         echo $rpcs RPCs
6738         [ "$rpcs" -eq 25 ] || error "not all RPCs are 4 MiB BRW rpcs"
6739
6740         restore_lustre_params < $p
6741         remount_client $MOUNT || error "remount_client failed"
6742
6743         rm -f $p $DIR/$tfile
6744 }
6745 run_test 101g "Big bulk(4/16 MiB) readahead"
6746
6747 setup_test102() {
6748         test_mkdir -p $DIR/$tdir
6749         chown $RUNAS_ID $DIR/$tdir
6750         STRIPE_SIZE=65536
6751         STRIPE_OFFSET=1
6752         STRIPE_COUNT=$OSTCOUNT
6753         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6754
6755         trap cleanup_test102 EXIT
6756         cd $DIR
6757         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6758         cd $DIR/$tdir
6759         for num in 1 2 3 4; do
6760                 for count in $(seq 1 $STRIPE_COUNT); do
6761                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6762                                 local size=`expr $STRIPE_SIZE \* $num`
6763                                 local file=file"$num-$idx-$count"
6764                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6765                         done
6766                 done
6767         done
6768
6769         cd $DIR
6770         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6771 }
6772
6773 cleanup_test102() {
6774         trap 0
6775         rm -f $TMP/f102.tar
6776         rm -rf $DIR/d0.sanity/d102
6777 }
6778
6779 test_102a() {
6780         local testfile=$DIR/$tfile
6781
6782         touch $testfile
6783
6784         [ "$UID" != 0 ] && skip_env "must run as root" && return
6785         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6786                 skip_env "must have user_xattr" && return
6787
6788         [ -z "$(which setfattr 2>/dev/null)" ] &&
6789                 skip_env "could not find setfattr" && return
6790
6791         echo "set/get xattr..."
6792         setfattr -n trusted.name1 -v value1 $testfile ||
6793                 error "setfattr -n trusted.name1=value1 $testfile failed"
6794         getfattr -n trusted.name1 $testfile 2> /dev/null |
6795           grep "trusted.name1=.value1" ||
6796                 error "$testfile missing trusted.name1=value1"
6797
6798         setfattr -n user.author1 -v author1 $testfile ||
6799                 error "setfattr -n user.author1=author1 $testfile failed"
6800         getfattr -n user.author1 $testfile 2> /dev/null |
6801           grep "user.author1=.author1" ||
6802                 error "$testfile missing trusted.author1=author1"
6803
6804         echo "listxattr..."
6805         setfattr -n trusted.name2 -v value2 $testfile ||
6806                 error "$testfile unable to set trusted.name2"
6807         setfattr -n trusted.name3 -v value3 $testfile ||
6808                 error "$testfile unable to set trusted.name3"
6809         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6810             grep "trusted.name" | wc -l) -eq 3 ] ||
6811                 error "$testfile missing 3 trusted.name xattrs"
6812
6813         setfattr -n user.author2 -v author2 $testfile ||
6814                 error "$testfile unable to set user.author2"
6815         setfattr -n user.author3 -v author3 $testfile ||
6816                 error "$testfile unable to set user.author3"
6817         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6818             grep "user.author" | wc -l) -eq 3 ] ||
6819                 error "$testfile missing 3 user.author xattrs"
6820
6821         echo "remove xattr..."
6822         setfattr -x trusted.name1 $testfile ||
6823                 error "$testfile error deleting trusted.name1"
6824         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6825                 error "$testfile did not delete trusted.name1 xattr"
6826
6827         setfattr -x user.author1 $testfile ||
6828                 error "$testfile error deleting user.author1"
6829         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6830                 error "$testfile did not delete trusted.name1 xattr"
6831
6832         # b10667: setting lustre special xattr be silently discarded
6833         echo "set lustre special xattr ..."
6834         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6835                 error "$testfile allowed setting trusted.lov"
6836 }
6837 run_test 102a "user xattr test =================================="
6838
6839 test_102b() {
6840         [ -z "$(which setfattr 2>/dev/null)" ] &&
6841                 skip_env "could not find setfattr" && return
6842
6843         # b10930: get/set/list trusted.lov xattr
6844         echo "get/set/list trusted.lov xattr ..."
6845         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6846         local testfile=$DIR/$tfile
6847         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6848                 error "setstripe failed"
6849         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6850                 error "getstripe failed"
6851         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6852                 error "can't get trusted.lov from $testfile"
6853
6854         local testfile2=${testfile}2
6855         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6856                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6857
6858         $MCREATE $testfile2
6859         setfattr -n trusted.lov -v $value $testfile2
6860         local stripe_size=$($GETSTRIPE -S $testfile2)
6861         local stripe_count=$($GETSTRIPE -c $testfile2)
6862         [[ $stripe_size -eq 65536 ]] ||
6863                 error "stripe size $stripe_size != 65536"
6864         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6865                 error "stripe count $stripe_count != $STRIPECOUNT"
6866         rm -f $DIR/$tfile
6867 }
6868 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6869
6870 test_102c() {
6871         [ -z "$(which setfattr 2>/dev/null)" ] &&
6872                 skip_env "could not find setfattr" && return
6873
6874         # b10930: get/set/list lustre.lov xattr
6875         echo "get/set/list lustre.lov xattr ..."
6876         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6877         test_mkdir -p $DIR/$tdir
6878         chown $RUNAS_ID $DIR/$tdir
6879         local testfile=$DIR/$tdir/$tfile
6880         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6881                 error "setstripe failed"
6882         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6883                 error "getstripe failed"
6884         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6885         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6886
6887         local testfile2=${testfile}2
6888         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6889                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6890
6891         $RUNAS $MCREATE $testfile2
6892         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6893         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6894         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6895         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6896         [ $stripe_count -eq $STRIPECOUNT ] ||
6897                 error "stripe count $stripe_count != $STRIPECOUNT"
6898 }
6899 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6900
6901 compare_stripe_info1() {
6902         local stripe_index_all_zero=true
6903
6904         for num in 1 2 3 4; do
6905                 for count in $(seq 1 $STRIPE_COUNT); do
6906                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6907                                 local size=$((STRIPE_SIZE * num))
6908                                 local file=file"$num-$offset-$count"
6909                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6910                                 [[ $stripe_size -ne $size ]] &&
6911                                     error "$file: size $stripe_size != $size"
6912                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6913                                 # allow fewer stripes to be created, ORI-601
6914                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6915                                     error "$file: count $stripe_count != $count"
6916                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6917                                 [[ $stripe_index -ne 0 ]] &&
6918                                         stripe_index_all_zero=false
6919                         done
6920                 done
6921         done
6922         $stripe_index_all_zero &&
6923                 error "all files are being extracted starting from OST index 0"
6924         return 0
6925 }
6926
6927 find_lustre_tar() {
6928         [ -n "$(which tar 2>/dev/null)" ] &&
6929                 strings $(which tar) | grep -q "lustre" && echo tar
6930 }
6931
6932 test_102d() {
6933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6934         # b10930: tar test for trusted.lov xattr
6935         TAR=$(find_lustre_tar)
6936         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6937         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6938         setup_test102
6939         test_mkdir -p $DIR/d102d
6940         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6941         cd $DIR/d102d/$tdir
6942         compare_stripe_info1
6943 }
6944 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6945
6946 test_102f() {
6947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6948         # b10930: tar test for trusted.lov xattr
6949         TAR=$(find_lustre_tar)
6950         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6951         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6952         setup_test102
6953         test_mkdir -p $DIR/d102f
6954         cd $DIR
6955         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6956         cd $DIR/d102f/$tdir
6957         compare_stripe_info1
6958 }
6959 run_test 102f "tar copy files, not keep osts ==========="
6960
6961 grow_xattr() {
6962         local xsize=${1:-1024}  # in bytes
6963         local file=$DIR/$tfile
6964
6965         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
6966                 skip "must have user_xattr" && return 0
6967         [ -z "$(which setfattr 2>/dev/null)" ] &&
6968                 skip_env "could not find setfattr" && return 0
6969         [ -z "$(which getfattr 2>/dev/null)" ] &&
6970                 skip_env "could not find getfattr" && return 0
6971
6972         touch $file
6973
6974         local value="$(generate_string $xsize)"
6975
6976         local xbig=trusted.big
6977         log "save $xbig on $file"
6978         setfattr -n $xbig -v $value $file ||
6979                 error "saving $xbig on $file failed"
6980
6981         local orig=$(get_xattr_value $xbig $file)
6982         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6983
6984         local xsml=trusted.sml
6985         log "save $xsml on $file"
6986         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6987
6988         local new=$(get_xattr_value $xbig $file)
6989         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6990
6991         log "grow $xsml on $file"
6992         setfattr -n $xsml -v "$value" $file ||
6993                 error "growing $xsml on $file failed"
6994
6995         new=$(get_xattr_value $xbig $file)
6996         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6997         log "$xbig still valid after growing $xsml"
6998
6999         rm -f $file
7000 }
7001
7002 test_102h() { # bug 15777
7003         grow_xattr 1024
7004 }
7005 run_test 102h "grow xattr from inside inode to external block"
7006
7007 test_102ha() {
7008         large_xattr_enabled || { skip "large_xattr disabled" && return; }
7009         grow_xattr $(max_xattr_size)
7010 }
7011 run_test 102ha "grow xattr from inside inode to external inode"
7012
7013 test_102i() { # bug 17038
7014         [ -z "$(which getfattr 2>/dev/null)" ] &&
7015                 skip "could not find getfattr" && return
7016         touch $DIR/$tfile
7017         ln -s $DIR/$tfile $DIR/${tfile}link
7018         getfattr -n trusted.lov $DIR/$tfile ||
7019                 error "lgetxattr on $DIR/$tfile failed"
7020         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7021                 grep -i "no such attr" ||
7022                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7023         rm -f $DIR/$tfile $DIR/${tfile}link
7024 }
7025 run_test 102i "lgetxattr test on symbolic link ============"
7026
7027 test_102j() {
7028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
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 "$RUNAS"
7033         test_mkdir -p $DIR/d102j
7034         chown $RUNAS_ID $DIR/d102j
7035         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
7036         cd $DIR/d102j/$tdir
7037         compare_stripe_info1 "$RUNAS"
7038 }
7039 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7040
7041 test_102k() {
7042         [ -z "$(which setfattr 2>/dev/null)" ] &&
7043                 skip "could not find setfattr" && return
7044         touch $DIR/$tfile
7045         # b22187 just check that does not crash for regular file.
7046         setfattr -n trusted.lov $DIR/$tfile
7047         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
7048         local test_kdir=$DIR/d102k
7049         test_mkdir $test_kdir
7050         local default_size=`$GETSTRIPE -S $test_kdir`
7051         local default_count=`$GETSTRIPE -c $test_kdir`
7052         local default_offset=`$GETSTRIPE -i $test_kdir`
7053         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7054                 error 'dir setstripe failed'
7055         setfattr -n trusted.lov $test_kdir
7056         local stripe_size=`$GETSTRIPE -S $test_kdir`
7057         local stripe_count=`$GETSTRIPE -c $test_kdir`
7058         local stripe_offset=`$GETSTRIPE -i $test_kdir`
7059         [ $stripe_size -eq $default_size ] ||
7060                 error "stripe size $stripe_size != $default_size"
7061         [ $stripe_count -eq $default_count ] ||
7062                 error "stripe count $stripe_count != $default_count"
7063         [ $stripe_offset -eq $default_offset ] ||
7064                 error "stripe offset $stripe_offset != $default_offset"
7065         rm -rf $DIR/$tfile $test_kdir
7066 }
7067 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7068
7069 test_102l() {
7070         [ -z "$(which getfattr 2>/dev/null)" ] &&
7071                 skip "could not find getfattr" && return
7072
7073         # LU-532 trusted. xattr is invisible to non-root
7074         local testfile=$DIR/$tfile
7075
7076         touch $testfile
7077
7078         echo "listxattr as user..."
7079         chown $RUNAS_ID $testfile
7080         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7081             grep -q "trusted" &&
7082                 error "$testfile trusted xattrs are user visible"
7083
7084         return 0;
7085 }
7086 run_test 102l "listxattr size test =================================="
7087
7088 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7089         local path=$DIR/$tfile
7090         touch $path
7091
7092         listxattr_size_check $path || error "listattr_size_check $path failed"
7093 }
7094 run_test 102m "Ensure listxattr fails on small bufffer ========"
7095
7096 cleanup_test102
7097
7098 getxattr() { # getxattr path name
7099         # Return the base64 encoding of the value of xattr name on path.
7100         local path=$1
7101         local name=$2
7102
7103         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7104         # file: $path
7105         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7106         #
7107         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7108
7109         getfattr --absolute-names --encoding=base64 --name=$name $path |
7110                 awk -F= -v name=$name '$1 == name {
7111                         print substr($0, index($0, "=") + 1);
7112         }'
7113 }
7114
7115 test_102n() { # LU-4101 mdt: protect internal xattrs
7116         [ -z "$(which setfattr 2>/dev/null)" ] &&
7117                 skip "could not find setfattr" && return
7118         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7119         then
7120                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7121                 return
7122         fi
7123
7124         local file0=$DIR/$tfile.0
7125         local file1=$DIR/$tfile.1
7126         local xattr0=$TMP/$tfile.0
7127         local xattr1=$TMP/$tfile.1
7128         local namelist="lov lma lmv link fid version som hsm"
7129         local name
7130         local value
7131
7132         rm -rf $file0 $file1 $xattr0 $xattr1
7133         touch $file0 $file1
7134
7135         # Get 'before' xattrs of $file1.
7136         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7137
7138         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7139                 namelist+=" lfsck_namespace"
7140         for name in $namelist; do
7141                 # Try to copy xattr from $file0 to $file1.
7142                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7143
7144                 setfattr --name=trusted.$name --value="$value" $file1 ||
7145                         error "setxattr 'trusted.$name' failed"
7146
7147                 # Try to set a garbage xattr.
7148                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7149
7150                 setfattr --name=trusted.$name --value="$value" $file1 ||
7151                         error "setxattr 'trusted.$name' failed"
7152
7153                 # Try to remove the xattr from $file1. We don't care if this
7154                 # appears to succeed or fail, we just don't want there to be
7155                 # any changes or crashes.
7156                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7157         done
7158
7159         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7160         then
7161                 name="lfsck_ns"
7162                 # Try to copy xattr from $file0 to $file1.
7163                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7164
7165                 setfattr --name=trusted.$name --value="$value" $file1 ||
7166                         error "setxattr 'trusted.$name' failed"
7167
7168                 # Try to set a garbage xattr.
7169                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7170
7171                 setfattr --name=trusted.$name --value="$value" $file1 ||
7172                         error "setxattr 'trusted.$name' failed"
7173
7174                 # Try to remove the xattr from $file1. We don't care if this
7175                 # appears to succeed or fail, we just don't want there to be
7176                 # any changes or crashes.
7177                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7178         fi
7179
7180         # Get 'after' xattrs of file1.
7181         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7182
7183         if ! diff $xattr0 $xattr1; then
7184                 error "before and after xattrs of '$file1' differ"
7185         fi
7186
7187         rm -rf $file0 $file1 $xattr0 $xattr1
7188
7189         return 0
7190 }
7191 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7192
7193 test_102p() { # LU-4703 setxattr did not check ownership
7194         local testfile=$DIR/$tfile
7195
7196         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7197                 skip "MDS needs to be at least 2.5.56" && return
7198
7199         touch $testfile
7200
7201         echo "setfacl as user..."
7202         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7203         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7204
7205         echo "setfattr as user..."
7206         setfacl -m "u:$RUNAS_ID:---" $testfile
7207         $RUNAS setfattr -x system.posix_acl_access $testfile
7208         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7209 }
7210 run_test 102p "check setxattr(2) correctly fails without permission"
7211
7212 test_102q() {
7213         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7214                 skip "MDS needs to be at least 2.6.92" && return
7215         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7216 }
7217 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7218
7219 test_102r() {
7220         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7221                 skip "MDS needs to be at least 2.6.93" && return
7222         touch $DIR/$tfile || error "touch"
7223         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7224         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7225         rm $DIR/$tfile || error "rm"
7226
7227         #normal directory
7228         mkdir -p $DIR/$tdir || error "mkdir"
7229         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7230         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7231         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7232                 error "$testfile error deleting user.author1"
7233         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7234                 grep "user.$(basename $tdir)" &&
7235                 error "$tdir did not delete user.$(basename $tdir)"
7236         rmdir $DIR/$tdir || error "rmdir"
7237
7238         #striped directory
7239         test_mkdir -p $DIR/$tdir || error "make striped dir"
7240         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7241         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7242         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7243                 error "$testfile error deleting user.author1"
7244         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7245                 grep "user.$(basename $tdir)" &&
7246                 error "$tdir did not delete user.$(basename $tdir)"
7247         rmdir $DIR/$tdir || error "rm striped dir"
7248 }
7249 run_test 102r "set EAs with empty values"
7250
7251 run_acl_subtest()
7252 {
7253     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7254     return $?
7255 }
7256
7257 test_103a() {
7258         [ "$UID" != 0 ] && skip_env "must run as root" && return
7259         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7260                 skip "must have acl enabled" && return
7261         [ -z "$(which setfacl 2>/dev/null)" ] &&
7262                 skip_env "could not find setfacl" && return
7263         $GSS && skip "could not run under gss" && return
7264         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7265
7266         gpasswd -a daemon bin                           # LU-5641
7267         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7268
7269         declare -a identity_old
7270
7271         for num in $(seq $MDSCOUNT); do
7272                 switch_identity $num true || identity_old[$num]=$?
7273         done
7274
7275         SAVE_UMASK=$(umask)
7276         umask 0022
7277         mkdir -p $DIR/$tdir
7278         cd $DIR/$tdir
7279
7280         echo "performing cp ..."
7281         run_acl_subtest cp || error "run_acl_subtest cp failed"
7282         echo "performing getfacl-noacl..."
7283         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7284         echo "performing misc..."
7285         run_acl_subtest misc || error  "misc test failed"
7286         echo "performing permissions..."
7287         run_acl_subtest permissions || error "permissions failed"
7288         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7289         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7290              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7291              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7292         then
7293                 echo "performing permissions xattr..."
7294                 run_acl_subtest permissions_xattr ||
7295                         error "permissions_xattr failed"
7296         fi
7297         echo "performing setfacl..."
7298         run_acl_subtest setfacl || error  "setfacl test failed"
7299
7300         # inheritance test got from HP
7301         echo "performing inheritance..."
7302         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7303         chmod +x make-tree || error "chmod +x failed"
7304         run_acl_subtest inheritance || error "inheritance test failed"
7305         rm -f make-tree
7306
7307         echo "LU-974 ignore umask when acl is enabled..."
7308         run_acl_subtest 974 || error "LU-974 umask test failed"
7309         if [ $MDSCOUNT -ge 2 ]; then
7310                 run_acl_subtest 974_remote ||
7311                         error "LU-974 umask test failed under remote dir"
7312         fi
7313
7314         echo "LU-2561 newly created file is same size as directory..."
7315         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7316                 run_acl_subtest 2561 || error "LU-2561 test failed"
7317         else
7318                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7319         fi
7320
7321         run_acl_subtest 4924 || error "LU-4924 test failed"
7322
7323         cd $SAVE_PWD
7324         umask $SAVE_UMASK
7325
7326         for num in $(seq $MDSCOUNT); do
7327                 if [ "${identity_old[$num]}" = 1 ]; then
7328                         switch_identity $num false || identity_old[$num]=$?
7329                 fi
7330         done
7331 }
7332 run_test 103a "acl test ========================================="
7333
7334 test_103b() {
7335         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7336         local noacl=false
7337         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7338         local mountopts=$MDS_MOUNT_OPTS
7339
7340         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7341                 noacl=true
7342         else
7343                 # stop the MDT
7344                 stop $SINGLEMDS || error "failed to stop MDT."
7345                 # remount the MDT
7346                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7347                         MDS_MOUNT_OPTS="-o noacl"
7348                 else
7349                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7350                 fi
7351                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7352                         error "failed to start MDT."
7353                 MDS_MOUNT_OPTS=$mountopts
7354         fi
7355
7356         touch $DIR/$tfile
7357         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7358
7359         if ! $noacl; then
7360                 # stop the MDT
7361                 stop $SINGLEMDS || error "failed to stop MDT."
7362                 # remount the MDT
7363                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7364                         error "failed to start MDT."
7365         fi
7366
7367         true
7368 }
7369 run_test 103b "MDS mount option 'noacl'"
7370
7371 test_103c() {
7372         mkdir -p $DIR/$tdir
7373         cp -rp $DIR/$tdir $DIR/$tdir.bak
7374
7375         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7376                 error "$DIR/$tdir shouldn't contain default ACL"
7377         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7378                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7379         true
7380 }
7381 run_test 103c "'cp -rp' won't set empty acl"
7382
7383 test_104a() {
7384         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7385         touch $DIR/$tfile
7386         lfs df || error "lfs df failed"
7387         lfs df -ih || error "lfs df -ih failed"
7388         lfs df -h $DIR || error "lfs df -h $DIR failed"
7389         lfs df -i $DIR || error "lfs df -i $DIR failed"
7390         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7391         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7392
7393         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7394         lctl --device %$OSC deactivate
7395         lfs df || error "lfs df with deactivated OSC failed"
7396         lctl --device %$OSC activate
7397         # wait the osc back to normal
7398         wait_osc_import_state client ost FULL
7399
7400         lfs df || error "lfs df with reactivated OSC failed"
7401         rm -f $DIR/$tfile
7402 }
7403 run_test 104a "lfs df [-ih] [path] test ========================="
7404
7405 test_104b() {
7406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7407         [ $RUNAS_ID -eq $UID ] &&
7408                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7409         chmod 666 /dev/obd
7410         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7411                         grep "Permission denied" | wc -l)))
7412         if [ $denied_cnt -ne 0 ]; then
7413                 error "lfs check servers test failed"
7414         fi
7415 }
7416 run_test 104b "$RUNAS lfs check servers test ===================="
7417
7418 test_105a() {
7419         # doesn't work on 2.4 kernels
7420         touch $DIR/$tfile
7421         if $(flock_is_enabled); then
7422                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7423         else
7424                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7425         fi
7426         rm -f $DIR/$tfile
7427 }
7428 run_test 105a "flock when mounted without -o flock test ========"
7429
7430 test_105b() {
7431         touch $DIR/$tfile
7432         if $(flock_is_enabled); then
7433                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7434         else
7435                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7436         fi
7437         rm -f $DIR/$tfile
7438 }
7439 run_test 105b "fcntl when mounted without -o flock test ========"
7440
7441 test_105c() {
7442         touch $DIR/$tfile
7443         if $(flock_is_enabled); then
7444                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7445         else
7446                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7447         fi
7448         rm -f $DIR/$tfile
7449 }
7450 run_test 105c "lockf when mounted without -o flock test ========"
7451
7452 test_105d() { # bug 15924
7453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7454         test_mkdir -p $DIR/$tdir
7455         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7456         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7457         $LCTL set_param fail_loc=0x80000315
7458         flocks_test 2 $DIR/$tdir
7459 }
7460 run_test 105d "flock race (should not freeze) ========"
7461
7462 test_105e() { # bug 22660 && 22040
7463         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7464         touch $DIR/$tfile
7465         flocks_test 3 $DIR/$tfile
7466 }
7467 run_test 105e "Two conflicting flocks from same process ======="
7468
7469 test_106() { #bug 10921
7470         test_mkdir -p $DIR/$tdir
7471         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7472         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7473 }
7474 run_test 106 "attempt exec of dir followed by chown of that dir"
7475
7476 test_107() {
7477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7478         CDIR=`pwd`
7479         cd $DIR
7480
7481         local file=core
7482         rm -f $file
7483
7484         local save_pattern=$(sysctl -n kernel.core_pattern)
7485         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7486         sysctl -w kernel.core_pattern=$file
7487         sysctl -w kernel.core_uses_pid=0
7488
7489         ulimit -c unlimited
7490         sleep 60 &
7491         SLEEPPID=$!
7492
7493         sleep 1
7494
7495         kill -s 11 $SLEEPPID
7496         wait $SLEEPPID
7497         if [ -e $file ]; then
7498                 size=`stat -c%s $file`
7499                 [ $size -eq 0 ] && error "Fail to create core file $file"
7500         else
7501                 error "Fail to create core file $file"
7502         fi
7503         rm -f $file
7504         sysctl -w kernel.core_pattern=$save_pattern
7505         sysctl -w kernel.core_uses_pid=$save_uses_pid
7506         cd $CDIR
7507 }
7508 run_test 107 "Coredump on SIG"
7509
7510 test_110() {
7511         test_mkdir -p $DIR/$tdir
7512         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7513                 error "mkdir with 255 char failed"
7514         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7515                 error "mkdir with 256 char should fail, but did not"
7516         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7517                 error "create with 255 char failed"
7518         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7519                 error "create with 256 char should fail, but did not"
7520
7521         ls -l $DIR/$tdir
7522         rm -rf $DIR/$tdir
7523 }
7524 run_test 110 "filename length checking"
7525
7526 test_115() {
7527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7528         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7529                 tail -1 | cut -c11-20)
7530         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7531         echo "Starting with $OSTIO_pre threads"
7532
7533         NUMTEST=20000
7534         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7535         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7536         echo "$NUMTEST creates/unlinks"
7537         test_mkdir -p $DIR/$tdir
7538         createmany -o $DIR/$tdir/$tfile $NUMTEST
7539         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7540
7541         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7542                 tail -1 | cut -c11-20)
7543
7544         # don't return an error
7545         [ $OSTIO_post == $OSTIO_pre ] && echo \
7546             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7547             echo "This may be fine, depending on what ran before this test" &&
7548             echo "and how fast this system is." && return
7549
7550         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7551 }
7552 run_test 115 "verify dynamic thread creation===================="
7553
7554 free_min_max () {
7555         wait_delete_completed
7556         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7557         echo "OST kbytes available: ${AVAIL[@]}"
7558         MAXV=${AVAIL[0]}
7559         MAXI=0
7560         MINV=${AVAIL[0]}
7561         MINI=0
7562         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7563                 #echo OST $i: ${AVAIL[i]}kb
7564                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7565                         MAXV=${AVAIL[i]}
7566                         MAXI=$i
7567                 fi
7568                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7569                         MINV=${AVAIL[i]}
7570                         MINI=$i
7571                 fi
7572         done
7573         echo "Min free space: OST $MINI: $MINV"
7574         echo "Max free space: OST $MAXI: $MAXV"
7575 }
7576
7577 test_116a() { # was previously test_116()
7578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7579         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7580
7581         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7582
7583         echo -n "Free space priority "
7584         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7585                 head -n1
7586         declare -a AVAIL
7587         free_min_max
7588
7589         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7590         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7591                 && return
7592         trap simple_cleanup_common EXIT
7593
7594
7595         # Check if we need to generate uneven OSTs
7596         test_mkdir -p $DIR/$tdir/OST${MINI}
7597         local FILL=$((MINV / 4))
7598         local DIFF=$((MAXV - MINV))
7599         local DIFF2=$((DIFF * 100 / MINV))
7600
7601         local threshold=$(do_facet $SINGLEMDS \
7602                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7603         threshold=${threshold%%%}
7604         echo -n "Check for uneven OSTs: "
7605         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7606
7607         if [[ $DIFF2 -gt $threshold ]]; then
7608                 echo "ok"
7609                 echo "Don't need to fill OST$MINI"
7610         else
7611                 # generate uneven OSTs. Write 2% over the QOS threshold value
7612                 echo "no"
7613                 DIFF=$((threshold - DIFF2 + 2))
7614                 DIFF2=$((MINV * DIFF / 100))
7615                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
7616                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7617                         error "setstripe failed"
7618                 DIFF=$((DIFF2 / 2048))
7619                 i=0
7620                 while [ $i -lt $DIFF ]; do
7621                         i=$((i + 1))
7622                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7623                                 bs=2M count=1 2>/dev/null
7624                         echo -n .
7625                 done
7626                 echo .
7627                 sync
7628                 sleep_maxage
7629                 free_min_max
7630         fi
7631
7632         DIFF=$((MAXV - MINV))
7633         DIFF2=$((DIFF * 100 / MINV))
7634         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
7635         if [ $DIFF2 -gt $threshold ]; then
7636                 echo "ok"
7637         else
7638                 echo "failed - QOS mode won't be used"
7639                 skip "QOS imbalance criteria not met"
7640                 simple_cleanup_common
7641                 return
7642         fi
7643
7644         MINI1=$MINI
7645         MINV1=$MINV
7646         MAXI1=$MAXI
7647         MAXV1=$MAXV
7648
7649         # now fill using QOS
7650         $SETSTRIPE -c 1 $DIR/$tdir
7651         FILL=$((FILL / 200))
7652         if [ $FILL -gt 600 ]; then
7653                 FILL=600
7654         fi
7655         echo "writing $FILL files to QOS-assigned OSTs"
7656         i=0
7657         while [ $i -lt $FILL ]; do
7658                 i=$((i + 1))
7659                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7660                         count=1 2>/dev/null
7661                 echo -n .
7662         done
7663         echo "wrote $i 200k files"
7664         sync
7665         sleep_maxage
7666
7667         echo "Note: free space may not be updated, so measurements might be off"
7668         free_min_max
7669         DIFF2=$((MAXV - MINV))
7670         echo "free space delta: orig $DIFF final $DIFF2"
7671         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7672         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
7673         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7674         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
7675         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7676         if [[ $DIFF -gt 0 ]]; then
7677                 FILL=$((DIFF2 * 100 / DIFF - 100))
7678                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7679         fi
7680
7681         # Figure out which files were written where
7682         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7683                awk '/'$MINI1': / {print $2; exit}')
7684         echo $UUID
7685         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7686         echo "$MINC files created on smaller OST $MINI1"
7687         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7688                awk '/'$MAXI1': / {print $2; exit}')
7689         echo $UUID
7690         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7691         echo "$MAXC files created on larger OST $MAXI1"
7692         if [[ $MINC -gt 0 ]]; then
7693                 FILL=$((MAXC * 100 / MINC - 100))
7694                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7695         fi
7696         [[ $MAXC -gt $MINC ]] ||
7697                 error_ignore LU-9 "stripe QOS didn't balance free space"
7698         simple_cleanup_common
7699 }
7700 run_test 116a "stripe QOS: free space balance ==================="
7701
7702 test_116b() { # LU-2093
7703         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7704         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7705
7706 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7707         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7708                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7709         [ -z "$old_rr" ] && skip "no QOS" && return 0
7710         do_facet $SINGLEMDS lctl set_param \
7711                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7712         mkdir -p $DIR/$tdir
7713         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7714         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7715         do_facet $SINGLEMDS lctl set_param fail_loc=0
7716         rm -rf $DIR/$tdir
7717         do_facet $SINGLEMDS lctl set_param \
7718                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7719 }
7720 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7721
7722 test_117() # bug 10891
7723 {
7724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7725         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7726         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7727         lctl set_param fail_loc=0x21e
7728         > $DIR/$tfile || error "truncate failed"
7729         lctl set_param fail_loc=0
7730         echo "Truncate succeeded."
7731         rm -f $DIR/$tfile
7732 }
7733 run_test 117 "verify osd extend =========="
7734
7735 NO_SLOW_RESENDCOUNT=4
7736 export OLD_RESENDCOUNT=""
7737 set_resend_count () {
7738         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7739         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7740         lctl set_param -n $PROC_RESENDCOUNT $1
7741         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7742 }
7743
7744 # for reduce test_118* time (b=14842)
7745 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7746
7747 # Reset async IO behavior after error case
7748 reset_async() {
7749         FILE=$DIR/reset_async
7750
7751         # Ensure all OSCs are cleared
7752         $SETSTRIPE -c -1 $FILE
7753         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7754         sync
7755         rm $FILE
7756 }
7757
7758 test_118a() #bug 11710
7759 {
7760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7761         reset_async
7762
7763         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7764         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7765         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7766
7767         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7768                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7769                 return 1;
7770         fi
7771         rm -f $DIR/$tfile
7772 }
7773 run_test 118a "verify O_SYNC works =========="
7774
7775 test_118b()
7776 {
7777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7778         remote_ost_nodsh && skip "remote OST with nodsh" && return
7779
7780         reset_async
7781
7782         #define OBD_FAIL_SRV_ENOENT 0x217
7783         set_nodes_failloc "$(osts_nodes)" 0x217
7784         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7785         RC=$?
7786         set_nodes_failloc "$(osts_nodes)" 0
7787         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7788         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7789                     grep -c writeback)
7790
7791         if [[ $RC -eq 0 ]]; then
7792                 error "Must return error due to dropped pages, rc=$RC"
7793                 return 1;
7794         fi
7795
7796         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7797                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7798                 return 1;
7799         fi
7800
7801         echo "Dirty pages not leaked on ENOENT"
7802
7803         # Due to the above error the OSC will issue all RPCs syncronously
7804         # until a subsequent RPC completes successfully without error.
7805         $MULTIOP $DIR/$tfile Ow4096yc
7806         rm -f $DIR/$tfile
7807
7808         return 0
7809 }
7810 run_test 118b "Reclaim dirty pages on fatal error =========="
7811
7812 test_118c()
7813 {
7814         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7815
7816         # for 118c, restore the original resend count, LU-1940
7817         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7818                                 set_resend_count $OLD_RESENDCOUNT
7819         remote_ost_nodsh && skip "remote OST with nodsh" && return
7820
7821         reset_async
7822
7823         #define OBD_FAIL_OST_EROFS               0x216
7824         set_nodes_failloc "$(osts_nodes)" 0x216
7825
7826         # multiop should block due to fsync until pages are written
7827         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7828         MULTIPID=$!
7829         sleep 1
7830
7831         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7832                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7833         fi
7834
7835         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7836                     grep -c writeback)
7837         if [[ $WRITEBACK -eq 0 ]]; then
7838                 error "No page in writeback, writeback=$WRITEBACK"
7839         fi
7840
7841         set_nodes_failloc "$(osts_nodes)" 0
7842         wait $MULTIPID
7843         RC=$?
7844         if [[ $RC -ne 0 ]]; then
7845                 error "Multiop fsync failed, rc=$RC"
7846         fi
7847
7848         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7849         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7850                     grep -c writeback)
7851         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7852                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7853         fi
7854
7855         rm -f $DIR/$tfile
7856         echo "Dirty pages flushed via fsync on EROFS"
7857         return 0
7858 }
7859 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7860
7861 # continue to use small resend count to reduce test_118* time (b=14842)
7862 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7863
7864 test_118d()
7865 {
7866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7867         remote_ost_nodsh && skip "remote OST with nodsh" && return
7868
7869         reset_async
7870
7871         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7872         set_nodes_failloc "$(osts_nodes)" 0x214
7873         # multiop should block due to fsync until pages are written
7874         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7875         MULTIPID=$!
7876         sleep 1
7877
7878         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7879                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7880         fi
7881
7882         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7883                     grep -c writeback)
7884         if [[ $WRITEBACK -eq 0 ]]; then
7885                 error "No page in writeback, writeback=$WRITEBACK"
7886         fi
7887
7888         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7889         set_nodes_failloc "$(osts_nodes)" 0
7890
7891         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7892         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7893                     grep -c writeback)
7894         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7895                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7896         fi
7897
7898         rm -f $DIR/$tfile
7899         echo "Dirty pages gaurenteed flushed via fsync"
7900         return 0
7901 }
7902 run_test 118d "Fsync validation inject a delay of the bulk =========="
7903
7904 test_118f() {
7905         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7906         reset_async
7907
7908         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7909         lctl set_param fail_loc=0x8000040a
7910
7911         # Should simulate EINVAL error which is fatal
7912         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7913         RC=$?
7914         if [[ $RC -eq 0 ]]; then
7915                 error "Must return error due to dropped pages, rc=$RC"
7916         fi
7917
7918         lctl set_param fail_loc=0x0
7919
7920         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7921         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7922         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7923                     grep -c writeback)
7924         if [[ $LOCKED -ne 0 ]]; then
7925                 error "Locked pages remain in cache, locked=$LOCKED"
7926         fi
7927
7928         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7929                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7930         fi
7931
7932         rm -f $DIR/$tfile
7933         echo "No pages locked after fsync"
7934
7935         reset_async
7936         return 0
7937 }
7938 run_test 118f "Simulate unrecoverable OSC side error =========="
7939
7940 test_118g() {
7941         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7942         reset_async
7943
7944         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7945         lctl set_param fail_loc=0x406
7946
7947         # simulate local -ENOMEM
7948         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7949         RC=$?
7950
7951         lctl set_param fail_loc=0
7952         if [[ $RC -eq 0 ]]; then
7953                 error "Must return error due to dropped pages, rc=$RC"
7954         fi
7955
7956         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7957         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7958         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7959                         grep -c writeback)
7960         if [[ $LOCKED -ne 0 ]]; then
7961                 error "Locked pages remain in cache, locked=$LOCKED"
7962         fi
7963
7964         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7965                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7966         fi
7967
7968         rm -f $DIR/$tfile
7969         echo "No pages locked after fsync"
7970
7971         reset_async
7972         return 0
7973 }
7974 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7975
7976 test_118h() {
7977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7978         remote_ost_nodsh && skip "remote OST with nodsh" && return
7979
7980         reset_async
7981
7982         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7983         set_nodes_failloc "$(osts_nodes)" 0x20e
7984         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7985         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7986         RC=$?
7987
7988         set_nodes_failloc "$(osts_nodes)" 0
7989         if [[ $RC -eq 0 ]]; then
7990                 error "Must return error due to dropped pages, rc=$RC"
7991         fi
7992
7993         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7994         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7995         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7996                     grep -c writeback)
7997         if [[ $LOCKED -ne 0 ]]; then
7998                 error "Locked pages remain in cache, locked=$LOCKED"
7999         fi
8000
8001         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8002                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8003         fi
8004
8005         rm -f $DIR/$tfile
8006         echo "No pages locked after fsync"
8007
8008         return 0
8009 }
8010 run_test 118h "Verify timeout in handling recoverables errors  =========="
8011
8012 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8013
8014 test_118i() {
8015         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8016         remote_ost_nodsh && skip "remote OST with nodsh" && return
8017
8018         reset_async
8019
8020         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8021         set_nodes_failloc "$(osts_nodes)" 0x20e
8022
8023         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8024         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8025         PID=$!
8026         sleep 5
8027         set_nodes_failloc "$(osts_nodes)" 0
8028
8029         wait $PID
8030         RC=$?
8031         if [[ $RC -ne 0 ]]; then
8032                 error "got error, but should be not, rc=$RC"
8033         fi
8034
8035         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8036         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8037         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8038         if [[ $LOCKED -ne 0 ]]; then
8039                 error "Locked pages remain in cache, locked=$LOCKED"
8040         fi
8041
8042         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8043                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8044         fi
8045
8046         rm -f $DIR/$tfile
8047         echo "No pages locked after fsync"
8048
8049         return 0
8050 }
8051 run_test 118i "Fix error before timeout in recoverable error  =========="
8052
8053 [ "$SLOW" = "no" ] && set_resend_count 4
8054
8055 test_118j() {
8056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8057         remote_ost_nodsh && skip "remote OST with nodsh" && return
8058
8059         reset_async
8060
8061         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8062         set_nodes_failloc "$(osts_nodes)" 0x220
8063
8064         # return -EIO from OST
8065         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8066         RC=$?
8067         set_nodes_failloc "$(osts_nodes)" 0x0
8068         if [[ $RC -eq 0 ]]; then
8069                 error "Must return error due to dropped pages, rc=$RC"
8070         fi
8071
8072         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8073         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8074         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8075         if [[ $LOCKED -ne 0 ]]; then
8076                 error "Locked pages remain in cache, locked=$LOCKED"
8077         fi
8078
8079         # in recoverable error on OST we want resend and stay until it finished
8080         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8081                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8082         fi
8083
8084         rm -f $DIR/$tfile
8085         echo "No pages locked after fsync"
8086
8087         return 0
8088 }
8089 run_test 118j "Simulate unrecoverable OST side error =========="
8090
8091 test_118k()
8092 {
8093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8094         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8095
8096         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8097         set_nodes_failloc "$(osts_nodes)" 0x20e
8098         test_mkdir -p $DIR/$tdir
8099
8100         for ((i=0;i<10;i++)); do
8101                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8102                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8103                 SLEEPPID=$!
8104                 sleep 0.500s
8105                 kill $SLEEPPID
8106                 wait $SLEEPPID
8107         done
8108
8109         set_nodes_failloc "$(osts_nodes)" 0
8110         rm -rf $DIR/$tdir
8111 }
8112 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8113
8114 test_118l()
8115 {
8116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8117         # LU-646
8118         test_mkdir -p $DIR/$tdir
8119         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8120         rm -rf $DIR/$tdir
8121 }
8122 run_test 118l "fsync dir ========="
8123
8124 test_118m() # LU-3066
8125 {
8126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8127         test_mkdir -p $DIR/$tdir
8128         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8129         rm -rf $DIR/$tdir
8130 }
8131 run_test 118m "fdatasync dir ========="
8132
8133 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8134
8135 test_119a() # bug 11737
8136 {
8137         BSIZE=$((512 * 1024))
8138         directio write $DIR/$tfile 0 1 $BSIZE
8139         # We ask to read two blocks, which is more than a file size.
8140         # directio will indicate an error when requested and actual
8141         # sizes aren't equeal (a normal situation in this case) and
8142         # print actual read amount.
8143         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8144         if [ "$NOB" != "$BSIZE" ]; then
8145                 error "read $NOB bytes instead of $BSIZE"
8146         fi
8147         rm -f $DIR/$tfile
8148 }
8149 run_test 119a "Short directIO read must return actual read amount"
8150
8151 test_119b() # bug 11737
8152 {
8153         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
8154
8155         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8156         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8157         sync
8158         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8159                 error "direct read failed"
8160         rm -f $DIR/$tfile
8161 }
8162 run_test 119b "Sparse directIO read must return actual read amount"
8163
8164 test_119c() # bug 13099
8165 {
8166         BSIZE=1048576
8167         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8168         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8169         rm -f $DIR/$tfile
8170 }
8171 run_test 119c "Testing for direct read hitting hole"
8172
8173 test_119d() # bug 15950
8174 {
8175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8176         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8177         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8178         BSIZE=1048576
8179         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8180         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8181         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8182         lctl set_param fail_loc=0x40d
8183         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8184         pid_dio=$!
8185         sleep 1
8186         cat $DIR/$tfile > /dev/null &
8187         lctl set_param fail_loc=0
8188         pid_reads=$!
8189         wait $pid_dio
8190         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8191         sleep 2
8192         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8193         error "the read rpcs have not completed in 2s"
8194         rm -f $DIR/$tfile
8195         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8196 }
8197 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8198
8199 test_120a() {
8200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8201         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8202         test_mkdir -p $DIR/$tdir
8203         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8204                skip "no early lock cancel on server" && return 0
8205
8206         lru_resize_disable mdc
8207         lru_resize_disable osc
8208         cancel_lru_locks mdc
8209         # asynchronous object destroy at MDT could cause bl ast to client
8210         cancel_lru_locks osc
8211
8212         stat $DIR/$tdir > /dev/null
8213         can1=$(do_facet $SINGLEMDS \
8214                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8215                awk '/ldlm_cancel/ {print $2}')
8216         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8217                awk '/ldlm_bl_callback/ {print $2}')
8218         test_mkdir -c1 $DIR/$tdir/d1
8219         can2=$(do_facet $SINGLEMDS \
8220                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8221                awk '/ldlm_cancel/ {print $2}')
8222         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8223                awk '/ldlm_bl_callback/ {print $2}')
8224         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8225         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8226         lru_resize_enable mdc
8227         lru_resize_enable osc
8228 }
8229 run_test 120a "Early Lock Cancel: mkdir test"
8230
8231 test_120b() {
8232         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8233         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8234         test_mkdir $DIR/$tdir
8235         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8236                skip "no early lock cancel on server" && return 0
8237         lru_resize_disable mdc
8238         lru_resize_disable osc
8239         cancel_lru_locks mdc
8240         stat $DIR/$tdir > /dev/null
8241         can1=$(do_facet $SINGLEMDS \
8242                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8243                awk '/ldlm_cancel/ {print $2}')
8244         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8245                awk '/ldlm_bl_callback/ {print $2}')
8246         touch $DIR/$tdir/f1
8247         can2=$(do_facet $SINGLEMDS \
8248                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8249                awk '/ldlm_cancel/ {print $2}')
8250         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8251                awk '/ldlm_bl_callback/ {print $2}')
8252         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8253         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8254         lru_resize_enable mdc
8255         lru_resize_enable osc
8256 }
8257 run_test 120b "Early Lock Cancel: create test"
8258
8259 test_120c() {
8260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8261         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8262         test_mkdir -c1 $DIR/$tdir
8263         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8264                skip "no early lock cancel on server" && return 0
8265         lru_resize_disable mdc
8266         lru_resize_disable osc
8267         test_mkdir -p -c1 $DIR/$tdir/d1
8268         test_mkdir -p -c1 $DIR/$tdir/d2
8269         touch $DIR/$tdir/d1/f1
8270         cancel_lru_locks mdc
8271         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8272         can1=$(do_facet $SINGLEMDS \
8273                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8274                awk '/ldlm_cancel/ {print $2}')
8275         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8276                awk '/ldlm_bl_callback/ {print $2}')
8277         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8278         can2=$(do_facet $SINGLEMDS \
8279                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8280                awk '/ldlm_cancel/ {print $2}')
8281         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8282                awk '/ldlm_bl_callback/ {print $2}')
8283         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8284         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8285         lru_resize_enable mdc
8286         lru_resize_enable osc
8287 }
8288 run_test 120c "Early Lock Cancel: link test"
8289
8290 test_120d() {
8291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8292         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8293         test_mkdir -p -c1 $DIR/$tdir
8294         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8295                skip "no early lock cancel on server" && return 0
8296         lru_resize_disable mdc
8297         lru_resize_disable osc
8298         touch $DIR/$tdir
8299         cancel_lru_locks mdc
8300         stat $DIR/$tdir > /dev/null
8301         can1=$(do_facet $SINGLEMDS \
8302                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8303                awk '/ldlm_cancel/ {print $2}')
8304         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8305                awk '/ldlm_bl_callback/ {print $2}')
8306         chmod a+x $DIR/$tdir
8307         can2=$(do_facet $SINGLEMDS \
8308                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8309                awk '/ldlm_cancel/ {print $2}')
8310         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8311                awk '/ldlm_bl_callback/ {print $2}')
8312         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8313         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8314         lru_resize_enable mdc
8315         lru_resize_enable osc
8316 }
8317 run_test 120d "Early Lock Cancel: setattr test"
8318
8319 test_120e() {
8320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8321         ! $($LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_can) &&
8322                 skip "no early lock cancel on server" && return 0
8323         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8324         local dlmtrace_set=false
8325
8326         test_mkdir -p -c1 $DIR/$tdir
8327         lru_resize_disable mdc
8328         lru_resize_disable osc
8329         ! $LCTL get_param debug | grep -q dlmtrace &&
8330                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
8331         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8332         cancel_lru_locks mdc
8333         cancel_lru_locks osc
8334         dd if=$DIR/$tdir/f1 of=/dev/null
8335         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8336         # XXX client can not do early lock cancel of OST lock
8337         # during unlink (LU-4206), so cancel osc lock now.
8338         sleep 2
8339         cancel_lru_locks osc
8340         can1=$(do_facet $SINGLEMDS \
8341                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8342                awk '/ldlm_cancel/ {print $2}')
8343         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8344                awk '/ldlm_bl_callback/ {print $2}')
8345         unlink $DIR/$tdir/f1
8346         sleep 5
8347         can2=$(do_facet $SINGLEMDS \
8348                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8349                awk '/ldlm_cancel/ {print $2}')
8350         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8351                awk '/ldlm_bl_callback/ {print $2}')
8352         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
8353                 $LCTL dk $TMP/cancel.debug.txt
8354         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
8355                 $LCTL dk $TMP/blocking.debug.txt
8356         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
8357         lru_resize_enable mdc
8358         lru_resize_enable osc
8359 }
8360 run_test 120e "Early Lock Cancel: unlink test"
8361
8362 test_120f() {
8363         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8364         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8365                skip "no early lock cancel on server" && return 0
8366         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8367         test_mkdir -p -c1 $DIR/$tdir
8368         lru_resize_disable mdc
8369         lru_resize_disable osc
8370         test_mkdir -p -c1 $DIR/$tdir/d1
8371         test_mkdir -p -c1 $DIR/$tdir/d2
8372         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8373         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8374         cancel_lru_locks mdc
8375         cancel_lru_locks osc
8376         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8377         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8378         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8379         # XXX client can not do early lock cancel of OST lock
8380         # during rename (LU-4206), so cancel osc lock now.
8381         sleep 2
8382         cancel_lru_locks osc
8383         can1=$(do_facet $SINGLEMDS \
8384                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8385                awk '/ldlm_cancel/ {print $2}')
8386         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8387                awk '/ldlm_bl_callback/ {print $2}')
8388         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8389         sleep 5
8390         can2=$(do_facet $SINGLEMDS \
8391                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8392                awk '/ldlm_cancel/ {print $2}')
8393         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8394                awk '/ldlm_bl_callback/ {print $2}')
8395         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8396         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8397         lru_resize_enable mdc
8398         lru_resize_enable osc
8399 }
8400 run_test 120f "Early Lock Cancel: rename test"
8401
8402 test_120g() {
8403         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8404         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8405                skip "no early lock cancel on server" && return 0
8406         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8407         lru_resize_disable mdc
8408         lru_resize_disable osc
8409         count=10000
8410         echo create $count files
8411         test_mkdir -p $DIR/$tdir
8412         cancel_lru_locks mdc
8413         cancel_lru_locks osc
8414         t0=`date +%s`
8415
8416         can0=$(do_facet $SINGLEMDS \
8417                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8418                awk '/ldlm_cancel/ {print $2}')
8419         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8420                awk '/ldlm_bl_callback/ {print $2}')
8421         createmany -o $DIR/$tdir/f $count
8422         sync
8423         can1=$(do_facet $SINGLEMDS \
8424                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8425                awk '/ldlm_cancel/ {print $2}')
8426         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8427                awk '/ldlm_bl_callback/ {print $2}')
8428         t1=$(date +%s)
8429         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8430         echo rm $count files
8431         rm -r $DIR/$tdir
8432         sync
8433         can2=$(do_facet $SINGLEMDS \
8434                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8435                awk '/ldlm_cancel/ {print $2}')
8436         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8437                awk '/ldlm_bl_callback/ {print $2}')
8438         t2=$(date +%s)
8439         echo total: $count removes in $((t2-t1))
8440         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8441         sleep 2
8442         # wait for commitment of removal
8443         lru_resize_enable mdc
8444         lru_resize_enable osc
8445 }
8446 run_test 120g "Early Lock Cancel: performance test"
8447
8448 test_121() { #bug #10589
8449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8450         rm -rf $DIR/$tfile
8451         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8452 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8453         lctl set_param fail_loc=0x310
8454         cancel_lru_locks osc > /dev/null
8455         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8456         lctl set_param fail_loc=0
8457         [[ $reads -eq $writes ]] ||
8458                 error "read $reads blocks, must be $writes blocks"
8459 }
8460 run_test 121 "read cancel race ========="
8461
8462 test_123a() { # was test 123, statahead(bug 11401)
8463         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8464         SLOWOK=0
8465         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8466             log "testing on UP system. Performance may be not as good as expected."
8467                         SLOWOK=1
8468         fi
8469
8470         rm -rf $DIR/$tdir
8471         test_mkdir -p $DIR/$tdir
8472         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8473         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8474         MULT=10
8475         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8476                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8477
8478                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8479                 lctl set_param -n llite.*.statahead_max 0
8480                 lctl get_param llite.*.statahead_max
8481                 cancel_lru_locks mdc
8482                 cancel_lru_locks osc
8483                 stime=`date +%s`
8484                 time ls -l $DIR/$tdir | wc -l
8485                 etime=`date +%s`
8486                 delta=$((etime - stime))
8487                 log "ls $i files without statahead: $delta sec"
8488                 lctl set_param llite.*.statahead_max=$max
8489
8490                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8491                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8492                 cancel_lru_locks mdc
8493                 cancel_lru_locks osc
8494                 stime=`date +%s`
8495                 time ls -l $DIR/$tdir | wc -l
8496                 etime=`date +%s`
8497                 delta_sa=$((etime - stime))
8498                 log "ls $i files with statahead: $delta_sa sec"
8499                 lctl get_param -n llite.*.statahead_stats
8500                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8501
8502                 [[ $swrong -lt $ewrong ]] &&
8503                         log "statahead was stopped, maybe too many locks held!"
8504                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8505
8506                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8507                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8508                     lctl set_param -n llite.*.statahead_max 0
8509                     lctl get_param llite.*.statahead_max
8510                     cancel_lru_locks mdc
8511                     cancel_lru_locks osc
8512                     stime=`date +%s`
8513                     time ls -l $DIR/$tdir | wc -l
8514                     etime=`date +%s`
8515                     delta=$((etime - stime))
8516                     log "ls $i files again without statahead: $delta sec"
8517                     lctl set_param llite.*.statahead_max=$max
8518                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8519                         if [  $SLOWOK -eq 0 ]; then
8520                                 error "ls $i files is slower with statahead!"
8521                         else
8522                                 log "ls $i files is slower with statahead!"
8523                         fi
8524                         break
8525                     fi
8526                 fi
8527
8528                 [ $delta -gt 20 ] && break
8529                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8530                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8531         done
8532         log "ls done"
8533
8534         stime=`date +%s`
8535         rm -r $DIR/$tdir
8536         sync
8537         etime=`date +%s`
8538         delta=$((etime - stime))
8539         log "rm -r $DIR/$tdir/: $delta seconds"
8540         log "rm done"
8541         lctl get_param -n llite.*.statahead_stats
8542 }
8543 run_test 123a "verify statahead work"
8544
8545 test_123b () { # statahead(bug 15027)
8546         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8547         test_mkdir -p $DIR/$tdir
8548         createmany -o $DIR/$tdir/$tfile-%d 1000
8549
8550         cancel_lru_locks mdc
8551         cancel_lru_locks osc
8552
8553 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8554         lctl set_param fail_loc=0x80000803
8555         ls -lR $DIR/$tdir > /dev/null
8556         log "ls done"
8557         lctl set_param fail_loc=0x0
8558         lctl get_param -n llite.*.statahead_stats
8559         rm -r $DIR/$tdir
8560         sync
8561
8562 }
8563 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8564
8565 test_124a() {
8566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8567         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8568                 skip "no lru resize on server" && return 0
8569         local NR=2000
8570         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8571
8572         log "create $NR files at $DIR/$tdir"
8573         createmany -o $DIR/$tdir/f $NR ||
8574                 error "failed to create $NR files in $DIR/$tdir"
8575
8576         cancel_lru_locks mdc
8577         ls -l $DIR/$tdir > /dev/null
8578
8579         local NSDIR=""
8580         local LRU_SIZE=0
8581         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8582                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8583                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8584                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8585                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8586                         log "NSDIR=$NSDIR"
8587                         log "NS=$(basename $NSDIR)"
8588                         break
8589                 fi
8590         done
8591
8592         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8593                 skip "Not enough cached locks created!"
8594                 return 0
8595         fi
8596         log "LRU=$LRU_SIZE"
8597
8598         local SLEEP=30
8599
8600         # We know that lru resize allows one client to hold $LIMIT locks
8601         # for 10h. After that locks begin to be killed by client.
8602         local MAX_HRS=10
8603         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8604         log "LIMIT=$LIMIT"
8605         if [ $LIMIT -lt $LRU_SIZE ]; then
8606             skip "Limit is too small $LIMIT"
8607             return 0
8608         fi
8609
8610         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8611         # killing locks. Some time was spent for creating locks. This means
8612         # that up to the moment of sleep finish we must have killed some of
8613         # them (10-100 locks). This depends on how fast ther were created.
8614         # Many of them were touched in almost the same moment and thus will
8615         # be killed in groups.
8616         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8617
8618         # Use $LRU_SIZE_B here to take into account real number of locks
8619         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8620         local LRU_SIZE_B=$LRU_SIZE
8621         log "LVF=$LVF"
8622         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8623         log "OLD_LVF=$OLD_LVF"
8624         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8625
8626         # Let's make sure that we really have some margin. Client checks
8627         # cached locks every 10 sec.
8628         SLEEP=$((SLEEP+20))
8629         log "Sleep ${SLEEP} sec"
8630         local SEC=0
8631         while ((SEC<$SLEEP)); do
8632                 echo -n "..."
8633                 sleep 5
8634                 SEC=$((SEC+5))
8635                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8636                 echo -n "$LRU_SIZE"
8637         done
8638         echo ""
8639         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8640         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8641
8642         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8643                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8644                 unlinkmany $DIR/$tdir/f $NR
8645                 return
8646         }
8647
8648         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8649         log "unlink $NR files at $DIR/$tdir"
8650         unlinkmany $DIR/$tdir/f $NR
8651 }
8652 run_test 124a "lru resize ======================================="
8653
8654 get_max_pool_limit()
8655 {
8656         local limit=$($LCTL get_param \
8657                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8658         local max=0
8659         for l in $limit; do
8660                 if [[ $l -gt $max ]]; then
8661                         max=$l
8662                 fi
8663         done
8664         echo $max
8665 }
8666
8667 test_124b() {
8668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8669         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8670                 skip "no lru resize on server" && return 0
8671
8672         LIMIT=$(get_max_pool_limit)
8673
8674         NR=$(($(default_lru_size)*20))
8675         if [[ $NR -gt $LIMIT ]]; then
8676                 log "Limit lock number by $LIMIT locks"
8677                 NR=$LIMIT
8678         fi
8679
8680         IFree=$(mdsrate_inodes_available)
8681         if [ $IFree -lt $NR ]; then
8682                 log "Limit lock number by $IFree inodes"
8683                 NR=$IFree
8684         fi
8685
8686         lru_resize_disable mdc
8687         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8688                 error "failed to create $DIR/$tdir/disable_lru_resize"
8689
8690         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8691         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8692         cancel_lru_locks mdc
8693         stime=`date +%s`
8694         PID=""
8695         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8696         PID="$PID $!"
8697         sleep 2
8698         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8699         PID="$PID $!"
8700         sleep 2
8701         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8702         PID="$PID $!"
8703         wait $PID
8704         etime=`date +%s`
8705         nolruresize_delta=$((etime-stime))
8706         log "ls -la time: $nolruresize_delta seconds"
8707         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8708         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8709
8710         lru_resize_enable mdc
8711         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8712                 error "failed to create $DIR/$tdir/enable_lru_resize"
8713
8714         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8715         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8716         cancel_lru_locks mdc
8717         stime=`date +%s`
8718         PID=""
8719         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8720         PID="$PID $!"
8721         sleep 2
8722         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8723         PID="$PID $!"
8724         sleep 2
8725         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8726         PID="$PID $!"
8727         wait $PID
8728         etime=`date +%s`
8729         lruresize_delta=$((etime-stime))
8730         log "ls -la time: $lruresize_delta seconds"
8731         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8732
8733         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8734                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8735         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8736                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8737         else
8738                 log "lru resize performs the same with no lru resize"
8739         fi
8740         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8741 }
8742 run_test 124b "lru resize (performance test) ======================="
8743
8744 test_124c() {
8745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8746         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8747                 skip "no lru resize on server" && return 0
8748
8749         # cache ununsed locks on client
8750         local nr=100
8751         cancel_lru_locks mdc
8752         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8753         createmany -o $DIR/$tdir/f $nr ||
8754                 error "failed to create $nr files in $DIR/$tdir"
8755         ls -l $DIR/$tdir > /dev/null
8756
8757         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8758         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8759         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8760         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8761         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8762
8763         # set lru_max_age to 1 sec
8764         $LCTL set_param $nsdir.lru_max_age=1000 # jiffies
8765         echo "sleep $((recalc_p * 2)) seconds..."
8766         sleep $((recalc_p * 2))
8767
8768         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8769         # restore lru_max_age
8770         $LCTL set_param -n $nsdir.lru_max_age $max_age
8771         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8772         unlinkmany $DIR/$tdir/f $nr
8773 }
8774 run_test 124c "LRUR cancel very aged locks"
8775
8776 test_125() { # 13358
8777         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8778         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8779         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8780         test_mkdir -p $DIR/d125 || error "mkdir failed"
8781         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8782         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8783         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8784 }
8785 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8786
8787 test_126() { # bug 12829/13455
8788         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8789         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8790         $GSS && skip "must run as gss disabled" && return
8791
8792         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8793         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8794         rm -f $DIR/$tfile
8795         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8796 }
8797 run_test 126 "check that the fsgid provided by the client is taken into account"
8798
8799 test_127a() { # bug 15521
8800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8801         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8802         $LCTL set_param osc.*.stats=0
8803         FSIZE=$((2048 * 1024))
8804         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8805         cancel_lru_locks osc
8806         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8807
8808         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8809         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8810                 echo "got $COUNT $NAME"
8811                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8812                 eval $NAME=$COUNT || error "Wrong proc format"
8813
8814                 case $NAME in
8815                         read_bytes|write_bytes)
8816                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8817                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8818                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8819                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8820                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8821                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8822                                 error "sumsquare is too small: $SUMSQ"
8823                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8824                                 error "sumsquare is too big: $SUMSQ"
8825                         ;;
8826                         *) ;;
8827                 esac
8828         done < $DIR/${tfile}.tmp
8829
8830         #check that we actually got some stats
8831         [ "$read_bytes" ] || error "Missing read_bytes stats"
8832         [ "$write_bytes" ] || error "Missing write_bytes stats"
8833         [ "$read_bytes" != 0 ] || error "no read done"
8834         [ "$write_bytes" != 0 ] || error "no write done"
8835 }
8836 run_test 127a "verify the client stats are sane"
8837
8838 test_127b() { # bug LU-333
8839         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8840         $LCTL set_param llite.*.stats=0
8841         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8842         # perform 2 reads and writes so MAX is different from SUM.
8843         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8844         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8845         cancel_lru_locks osc
8846         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8847         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8848
8849         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8850         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8851                 echo "got $COUNT $NAME"
8852                 eval $NAME=$COUNT || error "Wrong proc format"
8853
8854         case $NAME in
8855                 read_bytes)
8856                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8857                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8858                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8859                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8860                         ;;
8861                 write_bytes)
8862                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8863                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8864                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8865                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8866                         ;;
8867                         *) ;;
8868                 esac
8869         done < $TMP/${tfile}.tmp
8870
8871         #check that we actually got some stats
8872         [ "$read_bytes" ] || error "Missing read_bytes stats"
8873         [ "$write_bytes" ] || error "Missing write_bytes stats"
8874         [ "$read_bytes" != 0 ] || error "no read done"
8875         [ "$write_bytes" != 0 ] || error "no write done"
8876
8877         rm -f $TMP/${tfile}.tmp
8878 }
8879 run_test 127b "verify the llite client stats are sane"
8880
8881 test_128() { # bug 15212
8882         touch $DIR/$tfile
8883         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8884                 find $DIR/$tfile
8885                 find $DIR/$tfile
8886         EOF
8887
8888         result=$(grep error $TMP/$tfile.log)
8889         rm -f $DIR/$tfile $TMP/$tfile.log
8890         [ -z "$result" ] ||
8891                 error "consecutive find's under interactive lfs failed"
8892 }
8893 run_test 128 "interactive lfs for 2 consecutive find's"
8894
8895 set_dir_limits () {
8896         local mntdev
8897         local canondev
8898         local node
8899
8900         local LDPROC=/proc/fs/ldiskfs
8901         local facets=$(get_facets MDS)
8902
8903         for facet in ${facets//,/ }; do
8904                 canondev=$(ldiskfs_canon \
8905                            *.$(convert_facet2label $facet).mntdev $facet)
8906                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8907                                                 LDPROC=/sys/fs/ldiskfs
8908                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8909                 do_facet $facet "test -e $LDPROC/$canondev/warning_dir_size" ||
8910                                                 LDPROC=/sys/fs/ldiskfs
8911                 do_facet $facet "echo $2 >$LDPROC/$canondev/warning_dir_size"
8912         done
8913 }
8914
8915 check_mds_dmesg() {
8916         local facets=$(get_facets MDS)
8917         for facet in ${facets//,/ }; do
8918                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
8919         done
8920         return 1
8921 }
8922
8923 test_129() {
8924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8925         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8926                 skip "Only applicable to ldiskfs-based MDTs"
8927                 return
8928         fi
8929         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8930         local ENOSPC=28
8931         local EFBIG=27
8932         local has_warning=0
8933
8934         rm -rf $DIR/$tdir
8935         mkdir -p $DIR/$tdir
8936
8937         # block size of mds1
8938         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8939         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8940         local MAX=$((MDSBLOCKSIZE * 5))
8941         set_dir_limits $MAX $MAX
8942         local I=$(stat -c%s "$DIR/$tdir")
8943         local J=0
8944         while [[ $I -le $MAX ]]; do
8945                 $MULTIOP $DIR/$tdir/$J Oc
8946                 rc=$?
8947                 if [ $has_warning -eq 0 ]; then
8948                         check_mds_dmesg '"is approaching"' &&
8949                                 has_warning=1
8950                 fi
8951                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8952                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8953                 #and EFBIG for previous versions
8954                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8955                         set_dir_limits 0 0
8956                         echo "return code $rc received as expected"
8957
8958                         createmany -o $DIR/$tdir/$J_file_ 1000 ||
8959                                 error_exit "create failed w/o dir size limit"
8960
8961                         check_mds_dmesg '"has reached"' ||
8962                                 error_exit "has reached message should be output"
8963
8964                         [ $has_warning -eq 0 ] &&
8965                                 error_exit "warning message should be output"
8966
8967                         I=$(stat -c%s "$DIR/$tdir")
8968
8969                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8970                                         $(version_code 2.4.51) ]
8971                         then
8972                                 [[ $I -eq $MAX ]] && return 0
8973                         else
8974                                 [[ $I -gt $MAX ]] && return 0
8975                         fi
8976                         error_exit "current dir size $I, previous limit $MAX"
8977                 elif [ $rc -ne 0 ]; then
8978                         set_dir_limits 0 0
8979                         error_exit "return code $rc received instead of expected " \
8980                                    "$EFBIG or $ENOSPC, files in dir $I"
8981                 fi
8982                 J=$((J+1))
8983                 I=$(stat -c%s "$DIR/$tdir")
8984         done
8985
8986         set_dir_limits 0 0
8987         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8988 }
8989 run_test 129 "test directory size limit ========================"
8990
8991 OLDIFS="$IFS"
8992 cleanup_130() {
8993         trap 0
8994         IFS="$OLDIFS"
8995 }
8996
8997 test_130a() {
8998         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8999         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9000                 return
9001
9002         trap cleanup_130 EXIT RETURN
9003
9004         local fm_file=$DIR/$tfile
9005         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9006         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9007                 error "dd failed for $fm_file"
9008
9009         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9010         filefrag -ves $fm_file
9011         RC=$?
9012         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9013                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9014         [ $RC != 0 ] && error "filefrag $fm_file failed"
9015
9016         filefrag_op=$(filefrag -ve $fm_file |
9017                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9018         lun=$($GETSTRIPE -i $fm_file)
9019
9020         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9021         IFS=$'\n'
9022         tot_len=0
9023         for line in $filefrag_op
9024         do
9025                 frag_lun=`echo $line | cut -d: -f5`
9026                 ext_len=`echo $line | cut -d: -f4`
9027                 if (( $frag_lun != $lun )); then
9028                         cleanup_130
9029                         error "FIEMAP on 1-stripe file($fm_file) failed"
9030                         return
9031                 fi
9032                 (( tot_len += ext_len ))
9033         done
9034
9035         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9036                 cleanup_130
9037                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9038                 return
9039         fi
9040
9041         cleanup_130
9042
9043         echo "FIEMAP on single striped file succeeded"
9044 }
9045 run_test 130a "FIEMAP (1-stripe file)"
9046
9047 test_130b() {
9048         [ "$OSTCOUNT" -lt "2" ] &&
9049                 skip_env "skipping FIEMAP on $OSTCOUNT-stripe file" && return
9050
9051         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9052         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9053                 return
9054
9055         trap cleanup_130 EXIT RETURN
9056
9057         local fm_file=$DIR/$tfile
9058         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9059                         error "setstripe on $fm_file"
9060         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9061                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9062
9063         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9064                 error "dd failed on $fm_file"
9065
9066         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9067         filefrag_op=$(filefrag -ve $fm_file |
9068                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9069
9070         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9071                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9072
9073         IFS=$'\n'
9074         tot_len=0
9075         num_luns=1
9076         for line in $filefrag_op
9077         do
9078                 frag_lun=$(echo $line | cut -d: -f5 |
9079                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9080                 ext_len=$(echo $line | cut -d: -f4)
9081                 if (( $frag_lun != $last_lun )); then
9082                         if (( tot_len != 1024 )); then
9083                                 cleanup_130
9084                                 error "FIEMAP on $fm_file failed; returned " \
9085                                 "len $tot_len for OST $last_lun instead of 1024"
9086                                 return
9087                         else
9088                                 (( num_luns += 1 ))
9089                                 tot_len=0
9090                         fi
9091                 fi
9092                 (( tot_len += ext_len ))
9093                 last_lun=$frag_lun
9094         done
9095         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9096                 cleanup_130
9097                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9098                         "luns or wrong len for OST $last_lun"
9099                 return
9100         fi
9101
9102         cleanup_130
9103
9104         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9105 }
9106 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9107
9108 test_130c() {
9109         [ "$OSTCOUNT" -lt "2" ] &&
9110                 skip_env "skipping FIEMAP on 2-stripe file" && return
9111
9112         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9113         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9114                 return
9115
9116         trap cleanup_130 EXIT RETURN
9117
9118         local fm_file=$DIR/$tfile
9119         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9120         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9121                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9122
9123         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9124                         error "dd failed on $fm_file"
9125
9126         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9127         filefrag_op=$(filefrag -ve $fm_file |
9128                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9129
9130         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9131                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9132
9133         IFS=$'\n'
9134         tot_len=0
9135         num_luns=1
9136         for line in $filefrag_op
9137         do
9138                 frag_lun=$(echo $line | cut -d: -f5 |
9139                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9140                 ext_len=$(echo $line | cut -d: -f4)
9141                 if (( $frag_lun != $last_lun )); then
9142                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9143                         if (( logical != 512 )); then
9144                                 cleanup_130
9145                                 error "FIEMAP on $fm_file failed; returned " \
9146                                 "logical start for lun $logical instead of 512"
9147                                 return
9148                         fi
9149                         if (( tot_len != 512 )); then
9150                                 cleanup_130
9151                                 error "FIEMAP on $fm_file failed; returned " \
9152                                 "len $tot_len for OST $last_lun instead of 1024"
9153                                 return
9154                         else
9155                                 (( num_luns += 1 ))
9156                                 tot_len=0
9157                         fi
9158                 fi
9159                 (( tot_len += ext_len ))
9160                 last_lun=$frag_lun
9161         done
9162         if (( num_luns != 2 || tot_len != 512 )); then
9163                 cleanup_130
9164                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9165                         "luns or wrong len for OST $last_lun"
9166                 return
9167         fi
9168
9169         cleanup_130
9170
9171         echo "FIEMAP on 2-stripe file with hole succeeded"
9172 }
9173 run_test 130c "FIEMAP (2-stripe file with hole)"
9174
9175 test_130d() {
9176         [ "$OSTCOUNT" -lt "3" ] &&
9177                 skip_env "skipping FIEMAP on N-stripe file test" && return
9178
9179         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9180         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9181                 return
9182
9183         trap cleanup_130 EXIT RETURN
9184
9185         local fm_file=$DIR/$tfile
9186         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9187                         error "setstripe on $fm_file"
9188         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9189                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9190
9191         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
9192         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
9193                 error "dd failed on $fm_file"
9194
9195         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9196         filefrag_op=$(filefrag -ve $fm_file |
9197                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9198
9199         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9200                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9201
9202         IFS=$'\n'
9203         tot_len=0
9204         num_luns=1
9205         for line in $filefrag_op
9206         do
9207                 frag_lun=$(echo $line | cut -d: -f5 |
9208                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9209                 ext_len=$(echo $line | cut -d: -f4)
9210                 if (( $frag_lun != $last_lun )); then
9211                         if (( tot_len != 1024 )); then
9212                                 cleanup_130
9213                                 error "FIEMAP on $fm_file failed; returned " \
9214                                 "len $tot_len for OST $last_lun instead of 1024"
9215                                 return
9216                         else
9217                                 (( num_luns += 1 ))
9218                                 tot_len=0
9219                         fi
9220                 fi
9221                 (( tot_len += ext_len ))
9222                 last_lun=$frag_lun
9223         done
9224         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
9225                 cleanup_130
9226                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9227                         "luns or wrong len for OST $last_lun"
9228                 return
9229         fi
9230
9231         cleanup_130
9232
9233         echo "FIEMAP on N-stripe file succeeded"
9234 }
9235 run_test 130d "FIEMAP (N-stripe file)"
9236
9237 test_130e() {
9238         [ "$OSTCOUNT" -lt "2" ] &&
9239                 skip_env "skipping continuation FIEMAP test" && return
9240
9241         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9242         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9243
9244         trap cleanup_130 EXIT RETURN
9245
9246         local fm_file=$DIR/$tfile
9247         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9248         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9249                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9250
9251         NUM_BLKS=512
9252         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9253         for ((i = 0; i < $NUM_BLKS; i++))
9254         do
9255                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9256         done
9257
9258         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9259         filefrag_op=$(filefrag -ve $fm_file |
9260                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9261
9262         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9263                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9264
9265         IFS=$'\n'
9266         tot_len=0
9267         num_luns=1
9268         for line in $filefrag_op
9269         do
9270                 frag_lun=$(echo $line | cut -d: -f5 |
9271                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9272                 ext_len=$(echo $line | cut -d: -f4)
9273                 if (( $frag_lun != $last_lun )); then
9274                         if (( tot_len != $EXPECTED_LEN )); then
9275                                 cleanup_130
9276                                 error "FIEMAP on $fm_file failed; returned " \
9277                                 "len $tot_len for OST $last_lun instead " \
9278                                 "of $EXPECTED_LEN"
9279                                 return
9280                         else
9281                                 (( num_luns += 1 ))
9282                                 tot_len=0
9283                         fi
9284                 fi
9285                 (( tot_len += ext_len ))
9286                 last_lun=$frag_lun
9287         done
9288         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9289                 cleanup_130
9290                 error "FIEMAP on $fm_file failed; returned wrong number " \
9291                         "of luns or wrong len for OST $last_lun"
9292                 return
9293         fi
9294
9295         cleanup_130
9296
9297         echo "FIEMAP with continuation calls succeeded"
9298 }
9299 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9300
9301 # Test for writev/readv
9302 test_131a() {
9303         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
9304         error "writev test failed"
9305         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
9306         error "readv failed"
9307         rm -f $DIR/$tfile
9308 }
9309 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9310
9311 test_131b() {
9312         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
9313         error "append writev test failed"
9314         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
9315         error "append writev test failed"
9316         rm -f $DIR/$tfile
9317 }
9318 run_test 131b "test append writev"
9319
9320 test_131c() {
9321         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9322         error "NOT PASS"
9323 }
9324 run_test 131c "test read/write on file w/o objects"
9325
9326 test_131d() {
9327         rwv -f $DIR/$tfile -w -n 1 1572864
9328         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9329         if [ "$NOB" != 1572864 ]; then
9330                 error "Short read filed: read $NOB bytes instead of 1572864"
9331         fi
9332         rm -f $DIR/$tfile
9333 }
9334 run_test 131d "test short read"
9335
9336 test_131e() {
9337         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9338         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9339         error "read hitting hole failed"
9340         rm -f $DIR/$tfile
9341 }
9342 run_test 131e "test read hitting hole"
9343
9344 check_stats() {
9345         local res
9346         local count
9347         case $1 in
9348         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9349                  ;;
9350         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9351                  ;;
9352         *) error "Wrong argument $1" ;;
9353         esac
9354         echo $res
9355         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9356         # if the argument $3 is zero, it means any stat increment is ok.
9357         if [[ $3 -gt 0 ]]; then
9358                 count=$(echo $res | awk '{ print $2 }')
9359                 [[ $count -ne $3 ]] &&
9360                         error "The $2 counter on $1 is wrong - expected $3"
9361         fi
9362 }
9363
9364 test_133a() {
9365         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9366         remote_ost_nodsh && skip "remote OST with nodsh" && return
9367         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9368
9369         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9370                 { skip "MDS doesn't support rename stats"; return; }
9371         local testdir=$DIR/${tdir}/stats_testdir
9372         mkdir -p $DIR/${tdir}
9373
9374         # clear stats.
9375         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9376         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9377
9378         # verify mdt stats first.
9379         mkdir ${testdir} || error "mkdir failed"
9380         check_stats $SINGLEMDS "mkdir" 1
9381         touch ${testdir}/${tfile} || error "touch failed"
9382         check_stats $SINGLEMDS "open" 1
9383         check_stats $SINGLEMDS "close" 1
9384         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
9385                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
9386                 check_stats $SINGLEMDS "mknod" 2
9387         }
9388         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
9389         check_stats $SINGLEMDS "unlink" 1
9390         rm -f ${testdir}/${tfile} || error "file remove failed"
9391         check_stats $SINGLEMDS "unlink" 2
9392
9393         # remove working dir and check mdt stats again.
9394         rmdir ${testdir} || error "rmdir failed"
9395         check_stats $SINGLEMDS "rmdir" 1
9396
9397         local testdir1=$DIR/${tdir}/stats_testdir1
9398         mkdir -p ${testdir}
9399         mkdir -p ${testdir1}
9400         touch ${testdir1}/test1
9401         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9402         check_stats $SINGLEMDS "crossdir_rename" 1
9403
9404         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9405         check_stats $SINGLEMDS "samedir_rename" 1
9406
9407         rm -rf $DIR/${tdir}
9408 }
9409 run_test 133a "Verifying MDT stats ========================================"
9410
9411 test_133b() {
9412         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9413         remote_ost_nodsh && skip "remote OST with nodsh" && return
9414         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9415         local testdir=$DIR/${tdir}/stats_testdir
9416         mkdir -p ${testdir} || error "mkdir failed"
9417         touch ${testdir}/${tfile} || error "touch failed"
9418         cancel_lru_locks mdc
9419
9420         # clear stats.
9421         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9422         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9423
9424         # extra mdt stats verification.
9425         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9426         check_stats $SINGLEMDS "setattr" 1
9427         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9428         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9429         then            # LU-1740
9430                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9431                 check_stats $SINGLEMDS "getattr" 1
9432         fi
9433         $LFS df || error "lfs failed"
9434         check_stats $SINGLEMDS "statfs" 1
9435
9436         rm -rf $DIR/${tdir}
9437 }
9438 run_test 133b "Verifying extra MDT stats =================================="
9439
9440 test_133c() {
9441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9442         remote_ost_nodsh && skip "remote OST with nodsh" && return
9443         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9444         local testdir=$DIR/${tdir}/stats_testdir
9445         test_mkdir -p ${testdir} || error "mkdir failed"
9446
9447         # verify obdfilter stats.
9448         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9449         sync
9450         cancel_lru_locks osc
9451         wait_delete_completed
9452
9453         # clear stats.
9454         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9455         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9456
9457         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9458         sync
9459         cancel_lru_locks osc
9460         check_stats ost "write" 1
9461
9462         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9463         check_stats ost "read" 1
9464
9465         > ${testdir}/${tfile} || error "truncate failed"
9466         check_stats ost "punch" 1
9467
9468         rm -f ${testdir}/${tfile} || error "file remove failed"
9469         wait_delete_completed
9470         check_stats ost "destroy" 1
9471
9472         rm -rf $DIR/${tdir}
9473 }
9474 run_test 133c "Verifying OST stats ========================================"
9475
9476 order_2() {
9477         local value=$1
9478         local orig=$value
9479         local order=1
9480
9481         while [ $value -ge 2 ]; do
9482                 order=$((order*2))
9483                 value=$((value/2))
9484         done
9485
9486         if [ $orig -gt $order ]; then
9487                 order=$((order*2))
9488         fi
9489         echo $order
9490 }
9491
9492 size_in_KMGT() {
9493     local value=$1
9494     local size=('K' 'M' 'G' 'T');
9495     local i=0
9496     local size_string=$value
9497
9498     while [ $value -ge 1024 ]; do
9499         if [ $i -gt 3 ]; then
9500             #T is the biggest unit we get here, if that is bigger,
9501             #just return XXXT
9502             size_string=${value}T
9503             break
9504         fi
9505         value=$((value >> 10))
9506         if [ $value -lt 1024 ]; then
9507             size_string=${value}${size[$i]}
9508             break
9509         fi
9510         i=$((i + 1))
9511     done
9512
9513     echo $size_string
9514 }
9515
9516 get_rename_size() {
9517     local size=$1
9518     local context=${2:-.}
9519     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9520                 grep -A1 $context |
9521                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9522     echo $sample
9523 }
9524
9525 test_133d() {
9526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9527         remote_ost_nodsh && skip "remote OST with nodsh" && return
9528         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9529         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9530         { skip "MDS doesn't support rename stats"; return; }
9531
9532         local testdir1=$DIR/${tdir}/stats_testdir1
9533         local testdir2=$DIR/${tdir}/stats_testdir2
9534
9535         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9536
9537         mkdir -p ${testdir1} || error "mkdir failed"
9538         mkdir -p ${testdir2} || error "mkdir failed"
9539
9540         createmany -o $testdir1/test 512 || error "createmany failed"
9541
9542         # check samedir rename size
9543         mv ${testdir1}/test0 ${testdir1}/test_0
9544
9545         local testdir1_size=$(ls -l $DIR/${tdir} |
9546                 awk '/stats_testdir1/ {print $5}')
9547         local testdir2_size=$(ls -l $DIR/${tdir} |
9548                 awk '/stats_testdir2/ {print $5}')
9549
9550         testdir1_size=$(order_2 $testdir1_size)
9551         testdir2_size=$(order_2 $testdir2_size)
9552
9553         testdir1_size=$(size_in_KMGT $testdir1_size)
9554         testdir2_size=$(size_in_KMGT $testdir2_size)
9555
9556         echo "source rename dir size: ${testdir1_size}"
9557         echo "target rename dir size: ${testdir2_size}"
9558
9559         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9560         eval $cmd || error "$cmd failed"
9561         local samedir=$($cmd | grep 'same_dir')
9562         local same_sample=$(get_rename_size $testdir1_size)
9563         [ -z "$samedir" ] && error "samedir_rename_size count error"
9564         [[ $same_sample -eq 1 ]] ||
9565                 error "samedir_rename_size error $same_sample"
9566         echo "Check same dir rename stats success"
9567
9568         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9569
9570         # check crossdir rename size
9571         mv ${testdir1}/test_0 ${testdir2}/test_0
9572
9573         testdir1_size=$(ls -l $DIR/${tdir} |
9574                 awk '/stats_testdir1/ {print $5}')
9575         testdir2_size=$(ls -l $DIR/${tdir} |
9576                 awk '/stats_testdir2/ {print $5}')
9577
9578         testdir1_size=$(order_2 $testdir1_size)
9579         testdir2_size=$(order_2 $testdir2_size)
9580
9581         testdir1_size=$(size_in_KMGT $testdir1_size)
9582         testdir2_size=$(size_in_KMGT $testdir2_size)
9583
9584         echo "source rename dir size: ${testdir1_size}"
9585         echo "target rename dir size: ${testdir2_size}"
9586
9587         eval $cmd || error "$cmd failed"
9588         local crossdir=$($cmd | grep 'crossdir')
9589         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9590         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9591         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9592         [[ $src_sample -eq 1 ]] ||
9593                 error "crossdir_rename_size error $src_sample"
9594         [[ $tgt_sample -eq 1 ]] ||
9595                 error "crossdir_rename_size error $tgt_sample"
9596         echo "Check cross dir rename stats success"
9597         rm -rf $DIR/${tdir}
9598 }
9599 run_test 133d "Verifying rename_stats ========================================"
9600
9601 test_133e() {
9602         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9603         remote_ost_nodsh && skip "remote OST with nodsh" && return
9604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9605         local testdir=$DIR/${tdir}/stats_testdir
9606         local ctr f0 f1 bs=32768 count=42 sum
9607
9608         mkdir -p ${testdir} || error "mkdir failed"
9609
9610         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9611
9612         for ctr in {write,read}_bytes; do
9613                 sync
9614                 cancel_lru_locks osc
9615
9616                 do_facet ost1 $LCTL set_param -n \
9617                         "obdfilter.*.exports.clear=clear"
9618
9619                 if [ $ctr = write_bytes ]; then
9620                         f0=/dev/zero
9621                         f1=${testdir}/${tfile}
9622                 else
9623                         f0=${testdir}/${tfile}
9624                         f1=/dev/null
9625                 fi
9626
9627                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9628                         error "dd failed"
9629                 sync
9630                 cancel_lru_locks osc
9631
9632                 sum=$(do_facet ost1 $LCTL get_param \
9633                         "obdfilter.*.exports.*.stats" |
9634                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9635                                 $1 == ctr { sum += $7 }
9636                                 END { printf("%0.0f", sum) }')
9637
9638                 if ((sum != bs * count)); then
9639                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9640                 fi
9641         done
9642
9643         rm -rf $DIR/${tdir}
9644 }
9645 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9646
9647 proc_dirs=""
9648 for dir in /proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9649            /sys/fs/lustre/ /sys/fs/lnet/ /sys/kernel/debug/lnet/ \
9650            /sys/kernel/debug/lustre/; do
9651         [[ -d $dir ]] && proc_dirs+=" $dir"
9652 done
9653
9654 test_133f() {
9655         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9656         remote_ost_nodsh && skip "remote OST with nodsh" && return
9657         # First without trusting modes.
9658         find $proc_dirs -exec cat '{}' \; &> /dev/null
9659
9660         # Second verifying readability.
9661         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
9662
9663         # eventually, this can also be replaced with "lctl get_param -R",
9664         # but not until that option is always available on the server
9665         local facet
9666         for facet in $SINGLEMDS ost1; do
9667                 do_facet $facet find $proc_dirs \
9668                         ! -name req_history \
9669                         -exec cat '{}' \\\; &> /dev/null
9670
9671                 do_facet $facet find $proc_dirs \
9672                         ! -name req_history \
9673                         -type f \
9674                         -exec cat '{}' \\\; &> /dev/null ||
9675                                 error "proc file read failed"
9676         done
9677 }
9678 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9679
9680 test_133g() {
9681         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9682         remote_ost_nodsh && skip "remote OST with nodsh" && return
9683         # Second verifying writability.
9684         find $proc_dirs \
9685                 -type f \
9686                 -not -name force_lbug \
9687                 -not -name changelog_mask \
9688                 -exec badarea_io '{}' \; &> /dev/null ||
9689                 error "find $proc_dirs failed"
9690
9691         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9692                 skip "Too old lustre on MDS" && return
9693
9694         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9695                 skip "Too old lustre on ost1" && return
9696
9697         local facet
9698         for facet in $SINGLEMDS ost1; do
9699                 do_facet $facet find $proc_dirs \
9700                         -type f \
9701                         -not -name force_lbug \
9702                         -not -name changelog_mask \
9703                         -exec badarea_io '{}' \\\; &> /dev/null ||
9704                 error "$facet find $proc_dirs failed"
9705
9706         done
9707
9708         # remount the FS in case writes/reads /proc break the FS
9709         cleanup || error "failed to unmount"
9710         setup || error "failed to setup"
9711         true
9712 }
9713 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9714
9715 test_134a() {
9716         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9717         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9718                 skip "Need MDS version at least 2.7.54" && return
9719
9720         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9721         cancel_lru_locks mdc
9722
9723         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9724         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9725         [ $unused -eq 0 ] || error "$unused locks are not cleared"
9726
9727         local nr=1000
9728         createmany -o $DIR/$tdir/f $nr ||
9729                 error "failed to create $nr files in $DIR/$tdir"
9730         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9731
9732         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9733         do_facet mds1 $LCTL set_param fail_loc=0x327
9734         do_facet mds1 $LCTL set_param fail_val=500
9735         touch $DIR/$tdir/m
9736
9737         echo "sleep 10 seconds ..."
9738         sleep 10
9739         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9740
9741         do_facet mds1 $LCTL set_param fail_loc=0
9742         do_facet mds1 $LCTL set_param fail_val=0
9743         [ $lck_cnt -lt $unused ] ||
9744                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9745
9746         rm $DIR/$tdir/m
9747         unlinkmany $DIR/$tdir/f $nr
9748 }
9749 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9750
9751 test_134b() {
9752         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9753         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9754                 skip "Need MDS version at least 2.7.54" && return
9755
9756         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9757         cancel_lru_locks mdc
9758
9759         local low_wm=$(do_facet mds1 $LCTL get_param -n \
9760                         ldlm.lock_reclaim_threshold_mb)
9761         # disable reclaim temporarily
9762         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
9763
9764         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
9765         do_facet mds1 $LCTL set_param fail_loc=0x328
9766         do_facet mds1 $LCTL set_param fail_val=500
9767
9768         $LCTL set_param debug=+trace
9769
9770         local nr=600
9771         createmany -o $DIR/$tdir/f $nr &
9772         local create_pid=$!
9773
9774         echo "Sleep $TIMEOUT seconds ..."
9775         sleep $TIMEOUT
9776         if ! ps -p $create_pid  > /dev/null 2>&1; then
9777                 do_facet mds1 $LCTL set_param fail_loc=0
9778                 do_facet mds1 $LCTL set_param fail_val=0
9779                 do_facet mds1 $LCTL set_param \
9780                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
9781                 error "createmany finished incorrectly!"
9782         fi
9783         do_facet mds1 $LCTL set_param fail_loc=0
9784         do_facet mds1 $LCTL set_param fail_val=0
9785         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
9786         wait $create_pid || return 1
9787
9788         unlinkmany $DIR/$tdir/f $nr
9789 }
9790 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
9791
9792 test_140() { #bug-17379
9793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9794         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9795         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9796         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
9797
9798         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9799         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9800         local i=0
9801         while i=`expr $i + 1`; do
9802                 test_mkdir -p $i || error "Creating dir $i"
9803                 cd $i || error "Changing to $i"
9804                 ln -s ../stat stat || error "Creating stat symlink"
9805                 # Read the symlink until ELOOP present,
9806                 # not LBUGing the system is considered success,
9807                 # we didn't overrun the stack.
9808                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9809                 [ $ret -ne 0 ] && {
9810                         if [ $ret -eq 40 ]; then
9811                                 break  # -ELOOP
9812                         else
9813                                 error "Open stat symlink"
9814                                 return
9815                         fi
9816                 }
9817         done
9818         i=`expr $i - 1`
9819         echo "The symlink depth = $i"
9820         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9821                                         error "Invalid symlink depth"
9822
9823         # Test recursive symlink
9824         ln -s symlink_self symlink_self
9825         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9826         echo "open symlink_self returns $ret"
9827         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9828 }
9829 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9830
9831 test_150() {
9832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9833         local TF="$TMP/$tfile"
9834
9835         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9836         cp $TF $DIR/$tfile
9837         cancel_lru_locks osc
9838         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9839         remount_client $MOUNT
9840         df -P $MOUNT
9841         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9842
9843         $TRUNCATE $TF 6000
9844         $TRUNCATE $DIR/$tfile 6000
9845         cancel_lru_locks osc
9846         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9847
9848         echo "12345" >>$TF
9849         echo "12345" >>$DIR/$tfile
9850         cancel_lru_locks osc
9851         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9852
9853         echo "12345" >>$TF
9854         echo "12345" >>$DIR/$tfile
9855         cancel_lru_locks osc
9856         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9857
9858         rm -f $TF
9859         true
9860 }
9861 run_test 150 "truncate/append tests"
9862
9863 #LU-2902 roc_hit was not able to read all values from lproc
9864 function roc_hit_init() {
9865         local list=$(comma_list $(osts_nodes))
9866         local dir=$DIR/$tdir-check
9867         local file=$dir/file
9868         local BEFORE
9869         local AFTER
9870         local idx
9871
9872         test_mkdir -p $dir
9873         #use setstripe to do a write to every ost
9874         for i in $(seq 0 $((OSTCOUNT-1))); do
9875                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9876                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9877                 idx=$(printf %04x $i)
9878                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9879                         awk '$1 == "cache_access" {sum += $7}
9880                                 END { printf("%0.0f", sum) }')
9881
9882                 cancel_lru_locks osc
9883                 cat $file >/dev/null
9884
9885                 AFTER=$(get_osd_param $list *OST*$idx stats |
9886                         awk '$1 == "cache_access" {sum += $7}
9887                                 END { printf("%0.0f", sum) }')
9888
9889                 echo BEFORE:$BEFORE AFTER:$AFTER
9890                 if ! let "AFTER - BEFORE == 4"; then
9891                         rm -rf $dir
9892                         error "roc_hit is not safe to use"
9893                 fi
9894                 rm $file
9895         done
9896
9897         rm -rf $dir
9898 }
9899
9900 function roc_hit() {
9901         local list=$(comma_list $(osts_nodes))
9902         echo $(get_osd_param $list '' stats |
9903                 awk '$1 == "cache_hit" {sum += $7}
9904                         END { printf("%0.0f", sum) }')
9905 }
9906
9907 function set_cache() {
9908         local on=1
9909
9910         if [ "$2" == "off" ]; then
9911                 on=0;
9912         fi
9913         local list=$(comma_list $(osts_nodes))
9914         set_osd_param $list '' $1_cache_enable $on
9915
9916         cancel_lru_locks osc
9917 }
9918
9919 test_151() {
9920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9921         remote_ost_nodsh && skip "remote OST with nodsh" && return
9922
9923         local CPAGES=3
9924         local list=$(comma_list $(osts_nodes))
9925
9926         # check whether obdfilter is cache capable at all
9927         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9928                 echo "not cache-capable obdfilter"
9929                 return 0
9930         fi
9931
9932         # check cache is enabled on all obdfilters
9933         if get_osd_param $list '' read_cache_enable | grep 0; then
9934                 echo "oss cache is disabled"
9935                 return 0
9936         fi
9937
9938         set_osd_param $list '' writethrough_cache_enable 1
9939
9940         # check write cache is enabled on all obdfilters
9941         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9942                 echo "oss write cache is NOT enabled"
9943                 return 0
9944         fi
9945
9946         roc_hit_init
9947
9948         #define OBD_FAIL_OBD_NO_LRU  0x609
9949         do_nodes $list $LCTL set_param fail_loc=0x609
9950
9951         # pages should be in the case right after write
9952         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9953                 error "dd failed"
9954
9955         local BEFORE=$(roc_hit)
9956         cancel_lru_locks osc
9957         cat $DIR/$tfile >/dev/null
9958         local AFTER=$(roc_hit)
9959
9960         do_nodes $list $LCTL set_param fail_loc=0
9961
9962         if ! let "AFTER - BEFORE == CPAGES"; then
9963                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9964         fi
9965
9966         # the following read invalidates the cache
9967         cancel_lru_locks osc
9968         set_osd_param $list '' read_cache_enable 0
9969         cat $DIR/$tfile >/dev/null
9970
9971         # now data shouldn't be found in the cache
9972         BEFORE=$(roc_hit)
9973         cancel_lru_locks osc
9974         cat $DIR/$tfile >/dev/null
9975         AFTER=$(roc_hit)
9976         if let "AFTER - BEFORE != 0"; then
9977                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9978         fi
9979
9980         set_osd_param $list '' read_cache_enable 1
9981         rm -f $DIR/$tfile
9982 }
9983 run_test 151 "test cache on oss and controls ==============================="
9984
9985 test_152() {
9986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9987         local TF="$TMP/$tfile"
9988
9989         # simulate ENOMEM during write
9990 #define OBD_FAIL_OST_NOMEM      0x226
9991         lctl set_param fail_loc=0x80000226
9992         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9993         cp $TF $DIR/$tfile
9994         sync || error "sync failed"
9995         lctl set_param fail_loc=0
9996
9997         # discard client's cache
9998         cancel_lru_locks osc
9999
10000         # simulate ENOMEM during read
10001         lctl set_param fail_loc=0x80000226
10002         cmp $TF $DIR/$tfile || error "cmp failed"
10003         lctl set_param fail_loc=0
10004
10005         rm -f $TF
10006 }
10007 run_test 152 "test read/write with enomem ============================"
10008
10009 test_153() {
10010         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10011 }
10012 run_test 153 "test if fdatasync does not crash ======================="
10013
10014 dot_lustre_fid_permission_check() {
10015         local fid=$1
10016         local ffid=$MOUNT/.lustre/fid/$fid
10017         local test_dir=$2
10018
10019         echo "stat fid $fid"
10020         stat $ffid > /dev/null || error "stat $ffid failed."
10021         echo "touch fid $fid"
10022         touch $ffid || error "touch $ffid failed."
10023         echo "write to fid $fid"
10024         cat /etc/hosts > $ffid || error "write $ffid failed."
10025         echo "read fid $fid"
10026         diff /etc/hosts $ffid || error "read $ffid failed."
10027         echo "append write to fid $fid"
10028         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10029         echo "rename fid $fid"
10030         mv $ffid $test_dir/$tfile.1 &&
10031                 error "rename $ffid to $tfile.1 should fail."
10032         touch $test_dir/$tfile.1
10033         mv $test_dir/$tfile.1 $ffid &&
10034                 error "rename $tfile.1 to $ffid should fail."
10035         rm -f $test_dir/$tfile.1
10036         echo "truncate fid $fid"
10037         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10038         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10039                 echo "link fid $fid"
10040                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10041         fi
10042         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10043                 echo "setfacl fid $fid"
10044                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10045                 echo "getfacl fid $fid"
10046                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10047         fi
10048         echo "unlink fid $fid"
10049         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10050         echo "mknod fid $fid"
10051         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10052
10053         fid=[0xf00000400:0x1:0x0]
10054         ffid=$MOUNT/.lustre/fid/$fid
10055
10056         echo "stat non-exist fid $fid"
10057         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10058         echo "write to non-exist fid $fid"
10059         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10060         echo "link new fid $fid"
10061         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10062
10063         mkdir -p $test_dir/$tdir
10064         touch $test_dir/$tdir/$tfile
10065         fid=$($LFS path2fid $test_dir/$tdir)
10066         rc=$?
10067         [ $rc -ne 0 ] &&
10068                 error "error: could not get fid for $test_dir/$dir/$tfile."
10069
10070         ffid=$MOUNT/.lustre/fid/$fid
10071
10072         echo "ls $fid"
10073         ls $ffid > /dev/null || error "ls $ffid failed."
10074         echo "touch $fid/$tfile.1"
10075         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10076
10077         echo "touch $MOUNT/.lustre/fid/$tfile"
10078         touch $MOUNT/.lustre/fid/$tfile && \
10079                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10080
10081         echo "setxattr to $MOUNT/.lustre/fid"
10082         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10083
10084         echo "listxattr for $MOUNT/.lustre/fid"
10085         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10086
10087         echo "delxattr from $MOUNT/.lustre/fid"
10088         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10089
10090         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10091         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10092                 error "touch invalid fid should fail."
10093
10094         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10095         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10096                 error "touch non-normal fid should fail."
10097
10098         echo "rename $tdir to $MOUNT/.lustre/fid"
10099         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10100                 error "rename to $MOUNT/.lustre/fid should fail."
10101
10102         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10103         then            # LU-3547
10104                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10105                 local new_obf_mode=777
10106
10107                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10108                 chmod $new_obf_mode $DIR/.lustre/fid ||
10109                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10110
10111                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10112                 [ $obf_mode -eq $new_obf_mode ] ||
10113                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10114
10115                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10116                 chmod $old_obf_mode $DIR/.lustre/fid ||
10117                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10118         fi
10119
10120         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10121         fid=$($LFS path2fid $test_dir/$tfile-2)
10122
10123         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10124         then # LU-5424
10125                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10126                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10127                         error "create lov data thru .lustre failed"
10128         fi
10129         echo "cp /etc/passwd $test_dir/$tfile-2"
10130         cp /etc/passwd $test_dir/$tfile-2 ||
10131                 error "copy to $test_dir/$tfile-2 failed."
10132         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10133         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10134                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10135
10136         rm -rf $test_dir/tfile.lnk
10137         rm -rf $test_dir/$tfile-2
10138 }
10139
10140 test_154A() {
10141         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10142                 skip "Need MDS version at least 2.4.1" && return
10143
10144         local tf=$DIR/$tfile
10145         touch $tf
10146
10147         local fid=$($LFS path2fid $tf)
10148         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10149
10150         # check that we get the same pathname back
10151         local found=$($LFS fid2path $MOUNT "$fid")
10152         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10153         [ "$found" == "$tf" ] ||
10154                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10155 }
10156 run_test 154A "lfs path2fid and fid2path basic checks"
10157
10158 test_154B() {
10159         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10160                 skip "Need MDS version at least 2.4.1" && return
10161
10162         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10163         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10164         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10165         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10166
10167         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10168         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10169
10170         # check that we get the same pathname
10171         echo "PFID: $PFID, name: $name"
10172         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10173         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10174         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10175                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10176
10177         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10178 }
10179 run_test 154B "verify the ll_decode_linkea tool"
10180
10181 test_154a() {
10182         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10183         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10184                 { skip "Need MDS version at least 2.2.51"; return 0; }
10185         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10186         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10187
10188         cp /etc/hosts $DIR/$tfile
10189
10190         fid=$($LFS path2fid $DIR/$tfile)
10191         rc=$?
10192         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10193
10194         dot_lustre_fid_permission_check "$fid" $DIR ||
10195                 error "dot lustre permission check $fid failed"
10196
10197         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10198
10199         touch $MOUNT/.lustre/file &&
10200                 error "creation is not allowed under .lustre"
10201
10202         mkdir $MOUNT/.lustre/dir &&
10203                 error "mkdir is not allowed under .lustre"
10204
10205         rm -rf $DIR/$tfile
10206 }
10207 run_test 154a "Open-by-FID"
10208
10209 test_154b() {
10210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10211         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10212                 { skip "Need MDS version at least 2.2.51"; return 0; }
10213         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10214
10215         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10216
10217         local remote_dir=$DIR/$tdir/remote_dir
10218         local MDTIDX=1
10219         local rc=0
10220
10221         mkdir -p $DIR/$tdir
10222         $LFS mkdir -i $MDTIDX $remote_dir ||
10223                 error "create remote directory failed"
10224
10225         cp /etc/hosts $remote_dir/$tfile
10226
10227         fid=$($LFS path2fid $remote_dir/$tfile)
10228         rc=$?
10229         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
10230
10231         dot_lustre_fid_permission_check "$fid" $remote_dir ||
10232                 error "dot lustre permission check $fid failed"
10233         rm -rf $DIR/$tdir
10234 }
10235 run_test 154b "Open-by-FID for remote directory"
10236
10237 test_154c() {
10238         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10239                 skip "Need MDS version at least 2.4.1" && return
10240
10241         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
10242         local FID1=$($LFS path2fid $DIR/$tfile.1)
10243         local FID2=$($LFS path2fid $DIR/$tfile.2)
10244         local FID3=$($LFS path2fid $DIR/$tfile.3)
10245
10246         local N=1
10247         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
10248                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
10249                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
10250                 local want=FID$N
10251                 [ "$FID" = "${!want}" ] ||
10252                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
10253                 N=$((N + 1))
10254         done
10255
10256         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
10257         do
10258                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
10259                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
10260                 N=$((N + 1))
10261         done
10262 }
10263 run_test 154c "lfs path2fid and fid2path multiple arguments"
10264
10265 test_154d() {
10266         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10267         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10268                 skip "Need MDS version at least 2.5.53" && return
10269
10270         if remote_mds; then
10271                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10272         else
10273                 nid="0@lo"
10274         fi
10275         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10276         local fd
10277         local cmd
10278
10279         rm -f $DIR/$tfile
10280         touch $DIR/$tfile
10281
10282         local fid=$($LFS path2fid $DIR/$tfile)
10283         # Open the file
10284         fd=$(free_fd)
10285         cmd="exec $fd<$DIR/$tfile"
10286         eval $cmd
10287         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10288         echo "$fid_list" | grep "$fid"
10289         rc=$?
10290
10291         cmd="exec $fd>/dev/null"
10292         eval $cmd
10293         if [ $rc -ne 0 ]; then
10294                 error "FID $fid not found in open files list $fid_list"
10295         fi
10296 }
10297 run_test 154d "Verify open file fid"
10298
10299 test_154e()
10300 {
10301         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10302                 skip "Need MDS version at least 2.6.50" && return
10303
10304         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10305                 error ".lustre returned by readdir"
10306         fi
10307 }
10308 run_test 154e ".lustre is not returned by readdir"
10309
10310 test_154f() {
10311         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10312         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10313         test_mkdir -p -c1 $DIR/$tdir/d
10314         # test dirs inherit from its stripe
10315         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10316         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10317         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10318         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10319         touch $DIR/f
10320
10321         # get fid of parents
10322         local FID0=$($LFS path2fid $DIR/$tdir/d)
10323         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10324         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10325         local FID3=$($LFS path2fid $DIR)
10326
10327         # check that path2fid --parents returns expected <parent_fid>/name
10328         # 1) test for a directory (single parent)
10329         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10330         [ "$parent" == "$FID0/foo1" ] ||
10331                 error "expected parent: $FID0/foo1, got: $parent"
10332
10333         # 2) test for a file with nlink > 1 (multiple parents)
10334         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10335         echo "$parent" | grep -F "$FID1/$tfile" ||
10336                 error "$FID1/$tfile not returned in parent list"
10337         echo "$parent" | grep -F "$FID2/link" ||
10338                 error "$FID2/link not returned in parent list"
10339
10340         # 3) get parent by fid
10341         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10342         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10343         echo "$parent" | grep -F "$FID1/$tfile" ||
10344                 error "$FID1/$tfile not returned in parent list (by fid)"
10345         echo "$parent" | grep -F "$FID2/link" ||
10346                 error "$FID2/link not returned in parent list (by fid)"
10347
10348         # 4) test for entry in root directory
10349         parent=$($LFS path2fid --parents $DIR/f)
10350         echo "$parent" | grep -F "$FID3/f" ||
10351                 error "$FID3/f not returned in parent list"
10352
10353         # 5) test it on root directory
10354         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10355                 error "$MOUNT should not have parents"
10356
10357         # enable xattr caching and check that linkea is correctly updated
10358         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10359         save_lustre_params client "llite.*.xattr_cache" > $save
10360         lctl set_param llite.*.xattr_cache 1
10361
10362         # 6.1) linkea update on rename
10363         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10364
10365         # get parents by fid
10366         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10367         # foo1 should no longer be returned in parent list
10368         echo "$parent" | grep -F "$FID1" &&
10369                 error "$FID1 should no longer be in parent list"
10370         # the new path should appear
10371         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10372                 error "$FID2/$tfile.moved is not in parent list"
10373
10374         # 6.2) linkea update on unlink
10375         rm -f $DIR/$tdir/d/foo2/link
10376         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10377         # foo2/link should no longer be returned in parent list
10378         echo "$parent" | grep -F "$FID2/link" &&
10379                 error "$FID2/link should no longer be in parent list"
10380         true
10381
10382         rm -f $DIR/f
10383         restore_lustre_params < $save
10384         rm -f $save
10385 }
10386 run_test 154f "get parent fids by reading link ea"
10387
10388 test_154g()
10389 {
10390         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
10391                 { skip "Need MDS version at least 2.6.92"; return 0; }
10392         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10393
10394         mkdir -p $DIR/$tdir
10395         llapi_fid_test -d $DIR/$tdir
10396 }
10397 run_test 154g "various llapi FID tests"
10398
10399 test_155_small_load() {
10400     local temp=$TMP/$tfile
10401     local file=$DIR/$tfile
10402
10403     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10404         error "dd of=$temp bs=6096 count=1 failed"
10405     cp $temp $file
10406     cancel_lru_locks osc
10407     cmp $temp $file || error "$temp $file differ"
10408
10409     $TRUNCATE $temp 6000
10410     $TRUNCATE $file 6000
10411     cmp $temp $file || error "$temp $file differ (truncate1)"
10412
10413     echo "12345" >>$temp
10414     echo "12345" >>$file
10415     cmp $temp $file || error "$temp $file differ (append1)"
10416
10417     echo "12345" >>$temp
10418     echo "12345" >>$file
10419     cmp $temp $file || error "$temp $file differ (append2)"
10420
10421     rm -f $temp $file
10422     true
10423 }
10424
10425 test_155_big_load() {
10426     remote_ost_nodsh && skip "remote OST with nodsh" && return
10427     local temp=$TMP/$tfile
10428     local file=$DIR/$tfile
10429
10430     free_min_max
10431     local cache_size=$(do_facet ost$((MAXI+1)) \
10432         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10433     local large_file_size=$((cache_size * 2))
10434
10435     echo "OSS cache size: $cache_size KB"
10436     echo "Large file size: $large_file_size KB"
10437
10438     [ $MAXV -le $large_file_size ] && \
10439         skip_env "max available OST size needs > $large_file_size KB" && \
10440         return 0
10441
10442     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10443
10444     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10445         error "dd of=$temp bs=$large_file_size count=1k failed"
10446     cp $temp $file
10447     ls -lh $temp $file
10448     cancel_lru_locks osc
10449     cmp $temp $file || error "$temp $file differ"
10450
10451     rm -f $temp $file
10452     true
10453 }
10454
10455 save_writethrough() {
10456         local facets=$(get_facets OST)
10457
10458         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
10459         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
10460 }
10461
10462 test_155a() {
10463         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10464         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10465         save_writethrough $p
10466
10467         set_cache read on
10468         set_cache writethrough on
10469         test_155_small_load
10470         restore_lustre_params < $p
10471         rm -f $p
10472 }
10473 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10474
10475 test_155b() {
10476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10477         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10478         save_writethrough $p
10479
10480         set_cache read on
10481         set_cache writethrough off
10482         test_155_small_load
10483         restore_lustre_params < $p
10484         rm -f $p
10485 }
10486 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10487
10488 test_155c() {
10489         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10490         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10491         save_writethrough $p
10492
10493         set_cache read off
10494         set_cache writethrough on
10495         test_155_small_load
10496         restore_lustre_params < $p
10497         rm -f $p
10498 }
10499 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10500
10501 test_155d() {
10502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10503         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10504         save_writethrough $p
10505
10506         set_cache read off
10507         set_cache writethrough off
10508         test_155_small_load
10509         restore_lustre_params < $p
10510         rm -f $p
10511 }
10512 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10513
10514 test_155e() {
10515         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10516         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10517         save_writethrough $p
10518
10519         set_cache read on
10520         set_cache writethrough on
10521         test_155_big_load
10522         restore_lustre_params < $p
10523         rm -f $p
10524 }
10525 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10526
10527 test_155f() {
10528         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10529         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10530         save_writethrough $p
10531
10532         set_cache read on
10533         set_cache writethrough off
10534         test_155_big_load
10535         restore_lustre_params < $p
10536         rm -f $p
10537 }
10538 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10539
10540 test_155g() {
10541         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10542         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10543         save_writethrough $p
10544
10545         set_cache read off
10546         set_cache writethrough on
10547         test_155_big_load
10548         restore_lustre_params < $p
10549         rm -f $p
10550 }
10551 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10552
10553 test_155h() {
10554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10555         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10556         save_writethrough $p
10557
10558         set_cache read off
10559         set_cache writethrough off
10560         test_155_big_load
10561         restore_lustre_params < $p
10562         rm -f $p
10563 }
10564 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10565
10566 test_156() {
10567         remote_ost_nodsh && skip "remote OST with nodsh" && return
10568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10569         local CPAGES=3
10570         local BEFORE
10571         local AFTER
10572         local file="$DIR/$tfile"
10573         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10574
10575         [ "$(facet_fstype ost1)" = "zfs" -a \
10576            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10577                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10578                 return
10579
10580         save_writethrough $p
10581         roc_hit_init
10582
10583         log "Turn on read and write cache"
10584         set_cache read on
10585         set_cache writethrough on
10586
10587         log "Write data and read it back."
10588         log "Read should be satisfied from the cache."
10589         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10590         BEFORE=$(roc_hit)
10591         cancel_lru_locks osc
10592         cat $file >/dev/null
10593         AFTER=$(roc_hit)
10594         if ! let "AFTER - BEFORE == CPAGES"; then
10595                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10596         else
10597                 log "cache hits:: before: $BEFORE, after: $AFTER"
10598         fi
10599
10600         log "Read again; it should be satisfied from the cache."
10601         BEFORE=$AFTER
10602         cancel_lru_locks osc
10603         cat $file >/dev/null
10604         AFTER=$(roc_hit)
10605         if ! let "AFTER - BEFORE == CPAGES"; then
10606                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10607         else
10608                 log "cache hits:: before: $BEFORE, after: $AFTER"
10609         fi
10610
10611         log "Turn off the read cache and turn on the write cache"
10612         set_cache read off
10613         set_cache writethrough on
10614
10615         log "Read again; it should be satisfied from the cache."
10616         BEFORE=$(roc_hit)
10617         cancel_lru_locks osc
10618         cat $file >/dev/null
10619         AFTER=$(roc_hit)
10620         if ! let "AFTER - BEFORE == CPAGES"; then
10621                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10622         else
10623                 log "cache hits:: before: $BEFORE, after: $AFTER"
10624         fi
10625
10626         log "Read again; it should not be satisfied from the cache."
10627         BEFORE=$AFTER
10628         cancel_lru_locks osc
10629         cat $file >/dev/null
10630         AFTER=$(roc_hit)
10631         if ! let "AFTER - BEFORE == 0"; then
10632                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10633         else
10634                 log "cache hits:: before: $BEFORE, after: $AFTER"
10635         fi
10636
10637         log "Write data and read it back."
10638         log "Read should be satisfied from the cache."
10639         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10640         BEFORE=$(roc_hit)
10641         cancel_lru_locks osc
10642         cat $file >/dev/null
10643         AFTER=$(roc_hit)
10644         if ! let "AFTER - BEFORE == CPAGES"; then
10645                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10646         else
10647                 log "cache hits:: before: $BEFORE, after: $AFTER"
10648         fi
10649
10650         log "Read again; it should not be satisfied from the cache."
10651         BEFORE=$AFTER
10652         cancel_lru_locks osc
10653         cat $file >/dev/null
10654         AFTER=$(roc_hit)
10655         if ! let "AFTER - BEFORE == 0"; then
10656                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10657         else
10658                 log "cache hits:: before: $BEFORE, after: $AFTER"
10659         fi
10660
10661         log "Turn off read and write cache"
10662         set_cache read off
10663         set_cache writethrough off
10664
10665         log "Write data and read it back"
10666         log "It should not be satisfied from the cache."
10667         rm -f $file
10668         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10669         cancel_lru_locks osc
10670         BEFORE=$(roc_hit)
10671         cat $file >/dev/null
10672         AFTER=$(roc_hit)
10673         if ! let "AFTER - BEFORE == 0"; then
10674                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10675         else
10676                 log "cache hits:: before: $BEFORE, after: $AFTER"
10677         fi
10678
10679         log "Turn on the read cache and turn off the write cache"
10680         set_cache read on
10681         set_cache writethrough off
10682
10683         log "Write data and read it back"
10684         log "It should not be satisfied from the cache."
10685         rm -f $file
10686         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10687         BEFORE=$(roc_hit)
10688         cancel_lru_locks osc
10689         cat $file >/dev/null
10690         AFTER=$(roc_hit)
10691         if ! let "AFTER - BEFORE == 0"; then
10692                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10693         else
10694                 log "cache hits:: before: $BEFORE, after: $AFTER"
10695         fi
10696
10697         log "Read again; it should be satisfied from the cache."
10698         BEFORE=$(roc_hit)
10699         cancel_lru_locks osc
10700         cat $file >/dev/null
10701         AFTER=$(roc_hit)
10702         if ! let "AFTER - BEFORE == CPAGES"; then
10703                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10704         else
10705                 log "cache hits:: before: $BEFORE, after: $AFTER"
10706         fi
10707
10708         rm -f $file
10709         restore_lustre_params < $p
10710         rm -f $p
10711 }
10712 run_test 156 "Verification of tunables"
10713
10714 #Changelogs
10715 cleanup_changelog () {
10716         trap 0
10717         echo "Deregistering changelog client $CL_USER"
10718         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
10719 }
10720
10721 err17935 () {
10722         if [[ $MDSCOUNT -gt 1 ]]; then
10723                 error_ignore bz17935 $*
10724         else
10725                 error $*
10726         fi
10727 }
10728
10729 changelog_chmask()
10730 {
10731         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10732
10733         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10734
10735         if [ $MASK -eq 1 ]; then
10736                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10737         else
10738                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10739         fi
10740 }
10741
10742 changelog_extract_field() {
10743         local mdt=$1
10744         local cltype=$2
10745         local file=$3
10746         local identifier=$4
10747
10748         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10749                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10750                 tail -1
10751 }
10752
10753 test_160a() {
10754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10755         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10756         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10757                 { skip "Need MDS version at least 2.2.0"; return; }
10758
10759         local CL_USERS="mdd.$MDT0.changelog_users"
10760         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10761         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10762                 changelog_register -n)
10763         echo "Registered as changelog user $CL_USER"
10764         trap cleanup_changelog EXIT
10765         $GET_CL_USERS | grep -q $CL_USER ||
10766                 error "User $CL_USER not found in changelog_users"
10767
10768         # change something
10769         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10770         touch $DIR/$tdir/pics/2008/zachy/timestamp
10771         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10772         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10773         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10774         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10775         rm $DIR/$tdir/pics/desktop.jpg
10776
10777         $LFS changelog $MDT0 | tail -5
10778
10779         echo "verifying changelog mask"
10780         changelog_chmask "MKDIR"
10781         changelog_chmask "CLOSE"
10782
10783         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10784         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10785
10786         changelog_chmask "MKDIR"
10787         changelog_chmask "CLOSE"
10788
10789         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10790         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10791
10792         $LFS changelog $MDT0
10793         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10794         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10795         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10796         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10797
10798         # verify contents
10799         echo "verifying target fid"
10800         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10801         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10802         [ "$fidc" == "$fidf" ] ||
10803                 err17935 "fid in changelog $fidc != file fid $fidf"
10804         echo "verifying parent fid"
10805         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10806         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10807         [ "$fidc" == "$fidf" ] ||
10808                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10809
10810         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
10811         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
10812         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
10813         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10814         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10815                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10816
10817         MIN_REC=$($GET_CL_USERS |
10818                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10819         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10820         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10821         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10822                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10823
10824         # LU-3446 changelog index reset on MDT restart
10825         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10826         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10827         $LFS changelog_clear $MDT0 $CL_USER 0
10828         stop $SINGLEMDS || error "Fail to stop MDT."
10829         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10830         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10831         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10832         [ $CUR_REC1 == $CUR_REC2 ] ||
10833                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10834
10835         echo "verifying user deregister"
10836         cleanup_changelog
10837         $GET_CL_USERS | grep -q $CL_USER &&
10838                 error "User $CL_USER still in changelog_users"
10839
10840         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10841         if [ $CL_USER -eq 0 ]; then
10842                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10843                 touch $DIR/$tdir/chloe
10844                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10845                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10846                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10847         else
10848                 echo "$CL_USER other changelog users; can't verify off"
10849         fi
10850 }
10851 run_test 160a "changelog sanity"
10852
10853 test_160b() { # LU-3587
10854         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10855         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10856         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10857                 { skip "Need MDS version at least 2.2.0"; return; }
10858
10859         local CL_USERS="mdd.$MDT0.changelog_users"
10860         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10861         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10862                 changelog_register -n)
10863         echo "Registered as changelog user $CL_USER"
10864         trap cleanup_changelog EXIT
10865         $GET_CL_USERS | grep -q $CL_USER ||
10866                 error "User $CL_USER not found in changelog_users"
10867
10868         local LONGNAME1=$(str_repeat a 255)
10869         local LONGNAME2=$(str_repeat b 255)
10870
10871         cd $DIR
10872         echo "creating very long named file"
10873         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10874         echo "moving very long named file"
10875         mv $LONGNAME1 $LONGNAME2
10876
10877         $LFS changelog $MDT0 | grep RENME
10878         rm -f $LONGNAME2
10879         cleanup_changelog
10880 }
10881 run_test 160b "Verify that very long rename doesn't crash in changelog"
10882
10883 test_160c() {
10884         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10885
10886         local rc=0
10887         local server_version=$(lustre_version_code $SINGLEMDS)
10888
10889         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10890                 [[ $server_version -gt $(version_code 2.5.1) &&
10891                    $server_version -lt $(version_code 2.5.50) ]] ||
10892                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10894
10895         # Registration step
10896         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10897                 changelog_register -n)
10898         trap cleanup_changelog EXIT
10899
10900         rm -rf $DIR/$tdir
10901         mkdir -p $DIR/$tdir
10902         $MCREATE $DIR/$tdir/foo_160c
10903         changelog_chmask "TRUNC"
10904         $TRUNCATE $DIR/$tdir/foo_160c 200
10905         changelog_chmask "TRUNC"
10906         $TRUNCATE $DIR/$tdir/foo_160c 199
10907         $LFS changelog $MDT0
10908         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10909         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10910         $LFS changelog_clear $MDT0 $CL_USER 0
10911
10912         # Deregistration step
10913         cleanup_changelog
10914 }
10915 run_test 160c "verify that changelog log catch the truncate event"
10916
10917 test_160d() {
10918         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10919         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10920
10921         local server_version=$(lustre_version_code mds1)
10922         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10923
10924         [[ $server_version -ge $(version_code 2.7.60) ]] ||
10925                 { skip "Need MDS version at least 2.7.60+"; return; }
10926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10927
10928         # Registration step
10929         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
10930                 changelog_register -n)
10931
10932         trap cleanup_changelog EXIT
10933         mkdir -p $DIR/$tdir/migrate_dir
10934         $LFS changelog_clear $MDT0 $CL_USER 0
10935
10936         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
10937         $LFS changelog $MDT0
10938         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
10939         $LFS changelog_clear $MDT0 $CL_USER 0
10940         [ $MIGRATES -eq 1 ] ||
10941                 error "MIGRATE changelog mask count $MIGRATES != 1"
10942
10943         # Deregistration step
10944         cleanup_changelog
10945 }
10946 run_test 160d "verify that changelog log catch the migrate event"
10947
10948 test_160e() {
10949         # Create a user
10950         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10951                 changelog_register -n)
10952         echo "Registered as changelog user $CL_USER"
10953         trap cleanup_changelog EXIT
10954
10955         # Delete a future user (expect fail)
10956         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
10957         local rc=$?
10958
10959         if [ $rc -eq 0 ]; then
10960                 error "Deleted non-existant user cl77"
10961         elif [ $rc -ne 2 ]; then
10962                 error "changelog_deregister failed with $rc, " \
10963                         "expected 2 (ENOENT)"
10964         fi
10965
10966         # Clear to a bad index (1 billion should be safe)
10967         $LFS changelog_clear $MDT0 $CL_USER 1000000000
10968         rc=$?
10969
10970         if [ $rc -eq 0 ]; then
10971                 error "Successfully cleared to invalid CL index"
10972         elif [ $rc -ne 22 ]; then
10973                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
10974         fi
10975 }
10976 run_test 160e "changelog negative testing"
10977
10978 test_161a() {
10979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10980         test_mkdir -p -c1 $DIR/$tdir
10981         cp /etc/hosts $DIR/$tdir/$tfile
10982         test_mkdir -c1 $DIR/$tdir/foo1
10983         test_mkdir -c1 $DIR/$tdir/foo2
10984         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10985         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10986         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10987         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10988         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10989         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10990                 $LFS fid2path $DIR $FID
10991                 err17935 "bad link ea"
10992         fi
10993     # middle
10994     rm $DIR/$tdir/foo2/zachary
10995     # last
10996     rm $DIR/$tdir/foo2/thor
10997     # first
10998     rm $DIR/$tdir/$tfile
10999     # rename
11000     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11001     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11002         then
11003         $LFS fid2path $DIR $FID
11004         err17935 "bad link rename"
11005     fi
11006     rm $DIR/$tdir/foo2/maggie
11007
11008         # overflow the EA
11009         local longname=filename_avg_len_is_thirty_two_
11010         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11011                 error "failed to hardlink many files"
11012         links=$($LFS fid2path $DIR $FID | wc -l)
11013         echo -n "${links}/1000 links in link EA"
11014         [[ $links -gt 60 ]] ||
11015                 err17935 "expected at least 60 links in link EA"
11016         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11017                 error "failed to unlink many hardlinks"
11018 }
11019 run_test 161a "link ea sanity"
11020
11021 test_161b() {
11022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11023         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11024         local MDTIDX=1
11025         local remote_dir=$DIR/$tdir/remote_dir
11026
11027         mkdir -p $DIR/$tdir
11028         $LFS mkdir -i $MDTIDX $remote_dir ||
11029                 error "create remote directory failed"
11030
11031         cp /etc/hosts $remote_dir/$tfile
11032         mkdir -p $remote_dir/foo1
11033         mkdir -p $remote_dir/foo2
11034         ln $remote_dir/$tfile $remote_dir/foo1/sofia
11035         ln $remote_dir/$tfile $remote_dir/foo2/zachary
11036         ln $remote_dir/$tfile $remote_dir/foo1/luna
11037         ln $remote_dir/$tfile $remote_dir/foo2/thor
11038
11039         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
11040                      tr -d ']')
11041         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11042                 $LFS fid2path $DIR $FID
11043                 err17935 "bad link ea"
11044         fi
11045         # middle
11046         rm $remote_dir/foo2/zachary
11047         # last
11048         rm $remote_dir/foo2/thor
11049         # first
11050         rm $remote_dir/$tfile
11051         # rename
11052         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
11053         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
11054         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
11055                 $LFS fid2path $DIR $FID
11056                 err17935 "bad link rename"
11057         fi
11058         rm $remote_dir/foo2/maggie
11059
11060         # overflow the EA
11061         local longname=filename_avg_len_is_thirty_two_
11062         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
11063                 error "failed to hardlink many files"
11064         links=$($LFS fid2path $DIR $FID | wc -l)
11065         echo -n "${links}/1000 links in link EA"
11066         [[ ${links} -gt 60 ]] ||
11067                 err17935 "expected at least 60 links in link EA"
11068         unlinkmany $remote_dir/foo2/$longname 1000 ||
11069         error "failed to unlink many hardlinks"
11070 }
11071 run_test 161b "link ea sanity under remote directory"
11072
11073 test_161c() {
11074         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11075         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11076         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
11077                 skip "Need MDS version at least 2.1.5" && return
11078
11079         # define CLF_RENAME_LAST 0x0001
11080         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
11081         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11082                 changelog_register -n)
11083
11084         trap cleanup_changelog EXIT
11085         rm -rf $DIR/$tdir
11086         mkdir -p $DIR/$tdir
11087         touch $DIR/$tdir/foo_161c
11088         touch $DIR/$tdir/bar_161c
11089         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11090         $LFS changelog $MDT0 | grep RENME
11091         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
11092                 cut -f5 -d' ')
11093         $LFS changelog_clear $MDT0 $CL_USER 0
11094         if [ x$flags != "x0x1" ]; then
11095                 error "flag $flags is not 0x1"
11096         fi
11097         echo "rename overwrite a target having nlink = 1," \
11098                 "changelog record has flags of $flags"
11099
11100         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
11101         touch $DIR/$tdir/foo_161c
11102         touch $DIR/$tdir/bar_161c
11103         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11104         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11105         $LFS changelog $MDT0 | grep RENME
11106         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11107         $LFS changelog_clear $MDT0 $CL_USER 0
11108         if [ x$flags != "x0x0" ]; then
11109                 error "flag $flags is not 0x0"
11110         fi
11111         echo "rename overwrite a target having nlink > 1," \
11112                 "changelog record has flags of $flags"
11113
11114         # rename doesn't overwrite a target (changelog flag 0x0)
11115         touch $DIR/$tdir/foo_161c
11116         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
11117         $LFS changelog $MDT0 | grep RENME
11118         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11119         $LFS changelog_clear $MDT0 $CL_USER 0
11120         if [ x$flags != "x0x0" ]; then
11121                 error "flag $flags is not 0x0"
11122         fi
11123         echo "rename doesn't overwrite a target," \
11124                 "changelog record has flags of $flags"
11125
11126         # define CLF_UNLINK_LAST 0x0001
11127         # unlink a file having nlink = 1 (changelog flag 0x1)
11128         rm -f $DIR/$tdir/foo2_161c
11129         $LFS changelog $MDT0 | grep UNLNK
11130         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11131         $LFS changelog_clear $MDT0 $CL_USER 0
11132         if [ x$flags != "x0x1" ]; then
11133                 error "flag $flags is not 0x1"
11134         fi
11135         echo "unlink a file having nlink = 1," \
11136                 "changelog record has flags of $flags"
11137
11138         # unlink a file having nlink > 1 (changelog flag 0x0)
11139         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11140         rm -f $DIR/$tdir/foobar_161c
11141         $LFS changelog $MDT0 | grep UNLNK
11142         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11143         $LFS changelog_clear $MDT0 $CL_USER 0
11144         if [ x$flags != "x0x0" ]; then
11145                 error "flag $flags is not 0x0"
11146         fi
11147         echo "unlink a file having nlink > 1," \
11148                 "changelog record has flags of $flags"
11149         cleanup_changelog
11150 }
11151 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
11152
11153 check_path() {
11154     local expected=$1
11155     shift
11156     local fid=$2
11157
11158     local path=$(${LFS} fid2path $*)
11159     # Remove the '//' indicating a remote directory
11160     path=$(echo $path | sed 's#//#/#g')
11161     RC=$?
11162
11163     if [ $RC -ne 0 ]; then
11164         err17935 "path looked up of $expected failed. Error $RC"
11165         return $RC
11166     elif [ "${path}" != "${expected}" ]; then
11167         err17935 "path looked up \"${path}\" instead of \"${expected}\""
11168         return 2
11169     fi
11170     echo "fid $fid resolves to path $path (expected $expected)"
11171 }
11172
11173 test_162a() { # was test_162
11174         # Make changes to filesystem
11175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11176         test_mkdir -p -c1 $DIR/$tdir/d2
11177         touch $DIR/$tdir/d2/$tfile
11178         touch $DIR/$tdir/d2/x1
11179         touch $DIR/$tdir/d2/x2
11180         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
11181         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
11182         # regular file
11183         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
11184         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
11185                 error "check path $tdir/d2/$tfile failed"
11186
11187         # softlink
11188         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
11189         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
11190         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
11191                 error "check path $tdir/d2/p/q/r/slink failed"
11192
11193         # softlink to wrong file
11194         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
11195         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
11196         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
11197                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
11198
11199         # hardlink
11200         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
11201         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
11202         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
11203         # fid2path dir/fsname should both work
11204         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
11205                 error "check path $tdir/d2/a/b/c/new_file failed"
11206         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
11207                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
11208
11209         # hardlink count: check that there are 2 links
11210         # Doesnt work with CMD yet: 17935
11211         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
11212                 err17935 "expected 2 links"
11213
11214         # hardlink indexing: remove the first link
11215         rm $DIR/$tdir/d2/p/q/r/hlink
11216         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
11217                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
11218
11219         return 0
11220 }
11221 run_test 162a "path lookup sanity"
11222
11223 test_162b() {
11224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11225         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11226
11227         mkdir $DIR/$tdir
11228         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
11229                                 error "create striped dir failed"
11230
11231         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
11232                                         tail -n 1 | awk '{print $2}')
11233         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
11234
11235         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
11236         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
11237
11238         # regular file
11239         for ((i=0;i<5;i++)); do
11240                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
11241                         error "get fid for f$i failed"
11242                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
11243                         error "check path $tdir/striped_dir/f$i failed"
11244
11245                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
11246                         error "get fid for d$i failed"
11247                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
11248                         error "check path $tdir/striped_dir/d$i failed"
11249         done
11250
11251         return 0
11252 }
11253 run_test 162b "striped directory path lookup sanity"
11254
11255 # LU-4239: Verify fid2path works with paths 100 or more directories deep
11256 test_162c() {
11257         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
11258                 skip "Need MDS version at least 2.7.51" && return
11259         test_mkdir $DIR/$tdir.local
11260         test_mkdir $DIR/$tdir.remote
11261         local lpath=$tdir.local
11262         local rpath=$tdir.remote
11263
11264         for ((i = 0; i <= 101; i++)); do
11265                 lpath="$lpath/$i"
11266                 mkdir $DIR/$lpath
11267                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
11268                         error "get fid for local directory $DIR/$lpath failed"
11269                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
11270                         error "check path for local directory $DIR/$lpath failed"
11271
11272                 rpath="$rpath/$i"
11273                 test_mkdir $DIR/$rpath
11274                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
11275                         error "get fid for remote directory $DIR/$rpath failed"
11276                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
11277                         error "check path for remote directory $DIR/$rpath failed"
11278         done
11279
11280         return 0
11281 }
11282 run_test 162c "fid2path works with paths 100 or more directories deep"
11283
11284 test_169() {
11285         # do directio so as not to populate the page cache
11286         log "creating a 10 Mb file"
11287         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11288         log "starting reads"
11289         dd if=$DIR/$tfile of=/dev/null bs=4096 &
11290         log "truncating the file"
11291         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11292         log "killing dd"
11293         kill %+ || true # reads might have finished
11294         echo "wait until dd is finished"
11295         wait
11296         log "removing the temporary file"
11297         rm -rf $DIR/$tfile || error "tmp file removal failed"
11298 }
11299 run_test 169 "parallel read and truncate should not deadlock"
11300
11301 test_170() {
11302         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11303         $LCTL clear     # bug 18514
11304         $LCTL debug_daemon start $TMP/${tfile}_log_good
11305         touch $DIR/$tfile
11306         $LCTL debug_daemon stop
11307         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
11308                error "sed failed to read log_good"
11309
11310         $LCTL debug_daemon start $TMP/${tfile}_log_good
11311         rm -rf $DIR/$tfile
11312         $LCTL debug_daemon stop
11313
11314         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
11315                error "lctl df log_bad failed"
11316
11317         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11318         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11319
11320         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
11321         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
11322
11323         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
11324                 error "bad_line good_line1 good_line2 are empty"
11325
11326         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11327         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
11328         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11329
11330         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
11331         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11332         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11333
11334         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
11335                 error "bad_line_new good_line_new are empty"
11336
11337         local expected_good=$((good_line1 + good_line2*2))
11338
11339         rm -f $TMP/${tfile}*
11340         # LU-231, short malformed line may not be counted into bad lines
11341         if [ $bad_line -ne $bad_line_new ] &&
11342                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
11343                 error "expected $bad_line bad lines, but got $bad_line_new"
11344                 return 1
11345         fi
11346
11347         if [ $expected_good -ne $good_line_new ]; then
11348                 error "expected $expected_good good lines, but got $good_line_new"
11349                 return 2
11350         fi
11351         true
11352 }
11353 run_test 170 "test lctl df to handle corrupted log ====================="
11354
11355 test_171() { # bug20592
11356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11357 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11358         $LCTL set_param fail_loc=0x50e
11359         $LCTL set_param fail_val=3000
11360         multiop_bg_pause $DIR/$tfile O_s || true
11361         local MULTIPID=$!
11362         kill -USR1 $MULTIPID
11363         # cause log dump
11364         sleep 3
11365         wait $MULTIPID
11366         if dmesg | grep "recursive fault"; then
11367                 error "caught a recursive fault"
11368         fi
11369         $LCTL set_param fail_loc=0
11370         true
11371 }
11372 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11373
11374 # it would be good to share it with obdfilter-survey/iokit-libecho code
11375 setup_obdecho_osc () {
11376         local rc=0
11377         local ost_nid=$1
11378         local obdfilter_name=$2
11379         echo "Creating new osc for $obdfilter_name on $ost_nid"
11380         # make sure we can find loopback nid
11381         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11382
11383         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11384                            ${obdfilter_name}_osc_UUID || rc=2; }
11385         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11386                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11387         return $rc
11388 }
11389
11390 cleanup_obdecho_osc () {
11391         local obdfilter_name=$1
11392         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11393         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11394         return 0
11395 }
11396
11397 obdecho_test() {
11398         local OBD=$1
11399         local node=$2
11400         local pages=${3:-64}
11401         local rc=0
11402         local id
11403
11404         local count=10
11405         local obd_size=$(get_obd_size $node $OBD)
11406         local page_size=$(get_page_size $node)
11407         if [[ -n "$obd_size" ]]; then
11408                 local new_count=$((obd_size / (pages * page_size / 1024)))
11409                 [[ $new_count -ge $count ]] || count=$new_count
11410         fi
11411
11412         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11413         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11414                            rc=2; }
11415         if [ $rc -eq 0 ]; then
11416             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11417             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11418         fi
11419         echo "New object id is $id"
11420         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11421                            rc=4; }
11422         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11423                            "test_brw $count w v $pages $id" || rc=4; }
11424         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11425                            rc=4; }
11426         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11427                                         "cleanup" || rc=5; }
11428         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11429                                         "detach" || rc=6; }
11430         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11431         return $rc
11432 }
11433
11434 test_180a() {
11435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11436         remote_ost_nodsh && skip "remote OST with nodsh" && return
11437         local rc=0
11438         local rmmod_local=0
11439
11440         if ! module_loaded obdecho; then
11441             load_module obdecho/obdecho
11442             rmmod_local=1
11443         fi
11444
11445         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11446         local host=$(lctl get_param -n osc.$osc.import |
11447                              awk '/current_connection:/ {print $2}' )
11448         local target=$(lctl get_param -n osc.$osc.import |
11449                              awk '/target:/ {print $2}' )
11450         target=${target%_UUID}
11451
11452         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11453         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11454         [[ -n $target ]] && cleanup_obdecho_osc $target
11455         [ $rmmod_local -eq 1 ] && rmmod obdecho
11456         return $rc
11457 }
11458 run_test 180a "test obdecho on osc"
11459
11460 test_180b() {
11461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11462         remote_ost_nodsh && skip "remote OST with nodsh" && return
11463         local rc=0
11464         local rmmod_remote=0
11465
11466         do_facet ost1 "lsmod | grep -q obdecho || "                      \
11467                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
11468                       "modprobe obdecho; }" && rmmod_remote=1
11469         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11470         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11471         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
11472         return $rc
11473 }
11474 run_test 180b "test obdecho directly on obdfilter"
11475
11476 test_180c() { # LU-2598
11477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11478         remote_ost_nodsh && skip "remote OST with nodsh" && return
11479         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11480                 skip "Need MDS version at least 2.4.0" && return
11481
11482         local rc=0
11483         local rmmod_remote=false
11484         local pages=16384 # 64MB bulk I/O RPC size
11485         local target
11486
11487         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11488                 rmmod_remote=true || error "failed to load module obdecho"
11489
11490         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11491                 head -n1)
11492         if [ -n "$target" ]; then
11493                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11494         else
11495                 echo "there is no obdfilter target on ost1"
11496                 rc=2
11497         fi
11498         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11499         return $rc
11500 }
11501 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11502
11503 test_181() { # bug 22177
11504         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11505         # create enough files to index the directory
11506         createmany -o $DIR/$tdir/foobar 4000
11507         # print attributes for debug purpose
11508         lsattr -d .
11509         # open dir
11510         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11511         MULTIPID=$!
11512         # remove the files & current working dir
11513         unlinkmany $DIR/$tdir/foobar 4000
11514         rmdir $DIR/$tdir
11515         kill -USR1 $MULTIPID
11516         wait $MULTIPID
11517         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11518         return 0
11519 }
11520 run_test 181 "Test open-unlinked dir ========================"
11521
11522 test_182() {
11523         local fcount=1000
11524         local tcount=10
11525
11526         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11527
11528         $LCTL set_param mdc.*.rpc_stats=clear
11529
11530         for (( i = 0; i < $tcount; i++ )) ; do
11531                 mkdir $DIR/$tdir/$i
11532         done
11533
11534         for (( i = 0; i < $tcount; i++ )) ; do
11535                 createmany -o $DIR/$tdir/$i/f- $fcount &
11536         done
11537         wait
11538
11539         for (( i = 0; i < $tcount; i++ )) ; do
11540                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11541         done
11542         wait
11543
11544         $LCTL get_param mdc.*.rpc_stats
11545
11546         rm -rf $DIR/$tdir
11547 }
11548 run_test 182 "Test parallel modify metadata operations ================"
11549
11550 test_183() { # LU-2275
11551         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11552         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11553                 skip "Need MDS version at least 2.3.56" && return
11554
11555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11556         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11557         echo aaa > $DIR/$tdir/$tfile
11558
11559 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11560         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11561
11562         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11563         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11564
11565         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11566
11567         # Flush negative dentry cache
11568         touch $DIR/$tdir/$tfile
11569
11570         # We are not checking for any leaked references here, they'll
11571         # become evident next time we do cleanup with module unload.
11572         rm -rf $DIR/$tdir
11573 }
11574 run_test 183 "No crash or request leak in case of strange dispositions ========"
11575
11576 # test suite 184 is for LU-2016, LU-2017
11577 test_184a() {
11578         check_swap_layouts_support && return 0
11579
11580         dir0=$DIR/$tdir/$testnum
11581         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11582         ref1=/etc/passwd
11583         ref2=/etc/group
11584         file1=$dir0/f1
11585         file2=$dir0/f2
11586         $SETSTRIPE -c1 $file1
11587         cp $ref1 $file1
11588         $SETSTRIPE -c2 $file2
11589         cp $ref2 $file2
11590         gen1=$($GETSTRIPE -g $file1)
11591         gen2=$($GETSTRIPE -g $file2)
11592
11593         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11594         gen=$($GETSTRIPE -g $file1)
11595         [[ $gen1 != $gen ]] ||
11596                 "Layout generation on $file1 does not change"
11597         gen=$($GETSTRIPE -g $file2)
11598         [[ $gen2 != $gen ]] ||
11599                 "Layout generation on $file2 does not change"
11600
11601         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11602         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11603 }
11604 run_test 184a "Basic layout swap"
11605
11606 test_184b() {
11607         check_swap_layouts_support && return 0
11608
11609         dir0=$DIR/$tdir/$testnum
11610         mkdir -p $dir0 || error "creating dir $dir0"
11611         file1=$dir0/f1
11612         file2=$dir0/f2
11613         file3=$dir0/f3
11614         dir1=$dir0/d1
11615         dir2=$dir0/d2
11616         mkdir $dir1 $dir2
11617         $SETSTRIPE -c1 $file1
11618         $SETSTRIPE -c2 $file2
11619         $SETSTRIPE -c1 $file3
11620         chown $RUNAS_ID $file3
11621         gen1=$($GETSTRIPE -g $file1)
11622         gen2=$($GETSTRIPE -g $file2)
11623
11624         $LFS swap_layouts $dir1 $dir2 &&
11625                 error "swap of directories layouts should fail"
11626         $LFS swap_layouts $dir1 $file1 &&
11627                 error "swap of directory and file layouts should fail"
11628         $RUNAS $LFS swap_layouts $file1 $file2 &&
11629                 error "swap of file we cannot write should fail"
11630         $LFS swap_layouts $file1 $file3 &&
11631                 error "swap of file with different owner should fail"
11632         /bin/true # to clear error code
11633 }
11634 run_test 184b "Forbidden layout swap (will generate errors)"
11635
11636 test_184c() {
11637         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
11638         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
11639         check_swap_layouts_support && return 0
11640
11641         local dir0=$DIR/$tdir/$testnum
11642         mkdir -p $dir0 || error "creating dir $dir0"
11643
11644         local ref1=$dir0/ref1
11645         local ref2=$dir0/ref2
11646         local file1=$dir0/file1
11647         local file2=$dir0/file2
11648         # create a file large enough for the concurrent test
11649         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11650         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11651         echo "ref file size: ref1($(stat -c %s $ref1))," \
11652              "ref2($(stat -c %s $ref2))"
11653
11654         cp $ref2 $file2
11655         dd if=$ref1 of=$file1 bs=16k &
11656         local DD_PID=$!
11657
11658         # Make sure dd starts to copy file
11659         while [ ! -f $file1 ]; do sleep 0.1; done
11660
11661         $LFS swap_layouts $file1 $file2
11662         local rc=$?
11663         wait $DD_PID
11664         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11665         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11666
11667         # how many bytes copied before swapping layout
11668         local copied=$(stat -c %s $file2)
11669         local remaining=$(stat -c %s $ref1)
11670         remaining=$((remaining - copied))
11671         echo "Copied $copied bytes before swapping layout..."
11672
11673         cmp -n $copied $file1 $ref2 | grep differ &&
11674                 error "Content mismatch [0, $copied) of ref2 and file1"
11675         cmp -n $copied $file2 $ref1 ||
11676                 error "Content mismatch [0, $copied) of ref1 and file2"
11677         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11678                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11679
11680         # clean up
11681         rm -f $ref1 $ref2 $file1 $file2
11682 }
11683 run_test 184c "Concurrent write and layout swap"
11684
11685 test_184d() {
11686         check_swap_layouts_support && return 0
11687         [ -z "$(which getfattr 2>/dev/null)" ] &&
11688                 skip "no getfattr command" && return 0
11689
11690         local file1=$DIR/$tdir/$tfile-1
11691         local file2=$DIR/$tdir/$tfile-2
11692         local file3=$DIR/$tdir/$tfile-3
11693         local lovea1
11694         local lovea2
11695
11696         mkdir -p $DIR/$tdir
11697         touch $file1 || error "create $file1 failed"
11698         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11699                 error "create $file2 failed"
11700         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11701                 error "create $file3 failed"
11702         lovea1=$($LFS getstripe $file1 | sed 1d)
11703
11704         $LFS swap_layouts $file2 $file3 ||
11705                 error "swap $file2 $file3 layouts failed"
11706         $LFS swap_layouts $file1 $file2 ||
11707                 error "swap $file1 $file2 layouts failed"
11708
11709         lovea2=$($LFS getstripe $file2 | sed 1d)
11710         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11711
11712         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11713         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11714 }
11715 run_test 184d "allow stripeless layouts swap"
11716
11717 test_184e() {
11718         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11719                 { skip "Need MDS version at least 2.6.94"; return 0; }
11720         check_swap_layouts_support && return 0
11721         [ -z "$(which getfattr 2>/dev/null)" ] &&
11722                 skip "no getfattr command" && return 0
11723
11724         local file1=$DIR/$tdir/$tfile-1
11725         local file2=$DIR/$tdir/$tfile-2
11726         local file3=$DIR/$tdir/$tfile-3
11727         local lovea
11728
11729         mkdir -p $DIR/$tdir
11730         touch $file1 || error "create $file1 failed"
11731         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11732                 error "create $file2 failed"
11733         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11734                 error "create $file3 failed"
11735
11736         $LFS swap_layouts $file1 $file2 ||
11737                 error "swap $file1 $file2 layouts failed"
11738
11739         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11740         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11741
11742         echo 123 > $file1 || error "Should be able to write into $file1"
11743
11744         $LFS swap_layouts $file1 $file3 ||
11745                 error "swap $file1 $file3 layouts failed"
11746
11747         echo 123 > $file1 || error "Should be able to write into $file1"
11748
11749         rm -rf $file1 $file2 $file3
11750 }
11751 run_test 184e "Recreate layout after stripeless layout swaps"
11752
11753 test_185() { # LU-2441
11754         # LU-3553 - no volatile file support in old servers
11755         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11756                 { skip "Need MDS version at least 2.3.60"; return 0; }
11757
11758         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11759         touch $DIR/$tdir/spoo
11760         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11761         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11762                 error "cannot create/write a volatile file"
11763         [ "$FILESET" == "" ] &&
11764         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11765                 error "FID is still valid after close"
11766
11767         multiop_bg_pause $DIR/$tdir vVw4096_c
11768         local multi_pid=$!
11769
11770         local OLD_IFS=$IFS
11771         IFS=":"
11772         local fidv=($fid)
11773         IFS=$OLD_IFS
11774         # assume that the next FID for this client is sequential, since stdout
11775         # is unfortunately eaten by multiop_bg_pause
11776         local n=$((${fidv[1]} + 1))
11777         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11778         if [ "$FILESET" == "" ]; then
11779                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11780                         error "FID is missing before close"
11781         fi
11782         kill -USR1 $multi_pid
11783         # 1 second delay, so if mtime change we will see it
11784         sleep 1
11785         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11786         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11787 }
11788 run_test 185 "Volatile file support"
11789
11790 test_187a() {
11791         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11792         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11793                 skip "Need MDS version at least 2.3.0" && return
11794
11795         local dir0=$DIR/$tdir/$testnum
11796         mkdir -p $dir0 || error "creating dir $dir0"
11797
11798         local file=$dir0/file1
11799         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11800         local dv1=$($LFS data_version $file)
11801         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11802         local dv2=$($LFS data_version $file)
11803         [[ $dv1 != $dv2 ]] ||
11804                 error "data version did not change on write $dv1 == $dv2"
11805
11806         # clean up
11807         rm -f $file1
11808 }
11809 run_test 187a "Test data version change"
11810
11811 test_187b() {
11812         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11813         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11814                 skip "Need MDS version at least 2.3.0" && return
11815
11816         local dir0=$DIR/$tdir/$testnum
11817         mkdir -p $dir0 || error "creating dir $dir0"
11818
11819         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11820         [[ ${DV[0]} != ${DV[1]} ]] ||
11821                 error "data version did not change on write"\
11822                       " ${DV[0]} == ${DV[1]}"
11823
11824         # clean up
11825         rm -f $file1
11826 }
11827 run_test 187b "Test data version change on volatile file"
11828
11829 test_200() {
11830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11831         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11832         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11833
11834         local POOL=${POOL:-cea1}
11835         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11836         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11837         # Pool OST targets
11838         local first_ost=0
11839         local last_ost=$(($OSTCOUNT - 1))
11840         local ost_step=2
11841         local ost_list=$(seq $first_ost $ost_step $last_ost)
11842         local ost_range="$first_ost $last_ost $ost_step"
11843         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11844         local file_dir=$POOL_ROOT/file_tst
11845         local subdir=$test_path/subdir
11846
11847         local rc=0
11848         while : ; do
11849                 # former test_200a test_200b
11850                 pool_add $POOL                          || { rc=$? ; break; }
11851                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11852                 # former test_200c test_200d
11853                 mkdir -p $test_path
11854                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11855                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11856                 mkdir -p $subdir
11857                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11858                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11859                                                         || { rc=$? ; break; }
11860                 # former test_200e test_200f
11861                 local files=$((OSTCOUNT*3))
11862                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11863                                                         || { rc=$? ; break; }
11864                 pool_create_files $POOL $file_dir $files "$ost_list" \
11865                                                         || { rc=$? ; break; }
11866                 # former test_200g test_200h
11867                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11868                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11869
11870                 # former test_201a test_201b test_201c
11871                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11872
11873                 local f=$test_path/$tfile
11874                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11875                 pool_remove $POOL $f                    || { rc=$? ; break; }
11876                 break
11877         done
11878
11879         cleanup_pools
11880         return $rc
11881 }
11882 run_test 200 "OST pools"
11883
11884 # usage: default_attr <count | size | offset>
11885 default_attr() {
11886         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11887 }
11888
11889 # usage: check_default_stripe_attr
11890 check_default_stripe_attr() {
11891         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11892         case $1 in
11893         --stripe-count|--count)
11894                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11895         --stripe-size|--size)
11896                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11897         --stripe-index|--index)
11898                 EXPECTED=-1;;
11899         *)
11900                 error "unknown getstripe attr '$1'"
11901         esac
11902
11903         [ $ACTUAL != $EXPECTED ] &&
11904                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11905 }
11906
11907 test_204a() {
11908         test_mkdir -p $DIR/$tdir
11909         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11910
11911         check_default_stripe_attr --stripe-count
11912         check_default_stripe_attr --stripe-size
11913         check_default_stripe_attr --stripe-index
11914
11915         return 0
11916 }
11917 run_test 204a "Print default stripe attributes ================="
11918
11919 test_204b() {
11920         test_mkdir -p $DIR/$tdir
11921         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11922
11923         check_default_stripe_attr --stripe-size
11924         check_default_stripe_attr --stripe-index
11925
11926         return 0
11927 }
11928 run_test 204b "Print default stripe size and offset  ==========="
11929
11930 test_204c() {
11931         test_mkdir -p $DIR/$tdir
11932         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11933
11934         check_default_stripe_attr --stripe-count
11935         check_default_stripe_attr --stripe-index
11936
11937         return 0
11938 }
11939 run_test 204c "Print default stripe count and offset ==========="
11940
11941 test_204d() {
11942         test_mkdir -p $DIR/$tdir
11943         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11944
11945         check_default_stripe_attr --stripe-count
11946         check_default_stripe_attr --stripe-size
11947
11948         return 0
11949 }
11950 run_test 204d "Print default stripe count and size ============="
11951
11952 test_204e() {
11953         test_mkdir -p $DIR/$tdir
11954         $SETSTRIPE -d $DIR/$tdir
11955
11956         check_default_stripe_attr --stripe-count --raw
11957         check_default_stripe_attr --stripe-size --raw
11958         check_default_stripe_attr --stripe-index --raw
11959
11960         return 0
11961 }
11962 run_test 204e "Print raw stripe attributes ================="
11963
11964 test_204f() {
11965         test_mkdir -p $DIR/$tdir
11966         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11967
11968         check_default_stripe_attr --stripe-size --raw
11969         check_default_stripe_attr --stripe-index --raw
11970
11971         return 0
11972 }
11973 run_test 204f "Print raw stripe size and offset  ==========="
11974
11975 test_204g() {
11976         test_mkdir -p $DIR/$tdir
11977         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11978
11979         check_default_stripe_attr --stripe-count --raw
11980         check_default_stripe_attr --stripe-index --raw
11981
11982         return 0
11983 }
11984 run_test 204g "Print raw stripe count and offset ==========="
11985
11986 test_204h() {
11987         test_mkdir -p $DIR/$tdir
11988         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11989
11990         check_default_stripe_attr --stripe-count --raw
11991         check_default_stripe_attr --stripe-size --raw
11992
11993         return 0
11994 }
11995 run_test 204h "Print raw stripe count and size ============="
11996
11997 # Figure out which job scheduler is being used, if any,
11998 # or use a fake one
11999 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
12000         JOBENV=SLURM_JOB_ID
12001 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
12002         JOBENV=LSB_JOBID
12003 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
12004         JOBENV=PBS_JOBID
12005 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
12006         JOBENV=LOADL_STEP_ID
12007 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
12008         JOBENV=JOB_ID
12009 else
12010         $LCTL list_param jobid_name > /dev/null 2>&1
12011         if [ $? -eq 0 ]; then
12012                 JOBENV=nodelocal
12013         else
12014                 JOBENV=FAKE_JOBID
12015         fi
12016 fi
12017
12018 verify_jobstats() {
12019         local cmd=($1)
12020         shift
12021         local facets="$@"
12022
12023 # we don't really need to clear the stats for this test to work, since each
12024 # command has a unique jobid, but it makes debugging easier if needed.
12025 #       for facet in $facets; do
12026 #               local dev=$(convert_facet2label $facet)
12027 #               # clear old jobstats
12028 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
12029 #       done
12030
12031         # use a new JobID for each test, or we might see an old one
12032         [ "$JOBENV" = "FAKE_JOBID" ] &&
12033                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12034
12035         JOBVAL=${!JOBENV}
12036
12037         [ "$JOBENV" = "nodelocal" ] && {
12038                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12039                 $LCTL set_param jobid_name=$FAKE_JOBID
12040                 JOBVAL=$FAKE_JOBID
12041         }
12042
12043         log "Test: ${cmd[*]}"
12044         log "Using JobID environment variable $JOBENV=$JOBVAL"
12045
12046         if [ $JOBENV = "FAKE_JOBID" ]; then
12047                 FAKE_JOBID=$JOBVAL ${cmd[*]}
12048         else
12049                 ${cmd[*]}
12050         fi
12051
12052         # all files are created on OST0000
12053         for facet in $facets; do
12054                 local stats="*.$(convert_facet2label $facet).job_stats"
12055                 if [ $(do_facet $facet lctl get_param $stats |
12056                        grep -c $JOBVAL) -ne 1 ]; then
12057                         do_facet $facet lctl get_param $stats
12058                         error "No jobstats for $JOBVAL found on $facet::$stats"
12059                 fi
12060         done
12061 }
12062
12063 jobstats_set() {
12064         trap 0
12065         NEW_JOBENV=${1:-$OLD_JOBENV}
12066         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
12067         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
12068 }
12069
12070 cleanup_205() {
12071         trap 0
12072         do_facet $SINGLEMDS \
12073                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
12074         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
12075         cleanup_changelog
12076 }
12077
12078 test_205() { # Job stats
12079         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12080         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12081         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12082         remote_ost_nodsh && skip "remote OST with nodsh" && return
12083
12084         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
12085                 skip "Server doesn't support jobstats" && return 0
12086         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
12087
12088         OLD_JOBENV=$($LCTL get_param -n jobid_var)
12089         if [ $OLD_JOBENV != $JOBENV ]; then
12090                 jobstats_set $JOBENV
12091                 trap cleanup_205 EXIT
12092         fi
12093
12094         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
12095         echo "Registered as changelog user $CL_USER"
12096
12097         OLD_INTERVAL=$(do_facet $SINGLEMDS \
12098                        lctl get_param -n mdt.*.job_cleanup_interval)
12099         local interval_new=5
12100         do_facet $SINGLEMDS \
12101                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
12102         local start=$SECONDS
12103
12104         local cmd
12105         # mkdir
12106         cmd="mkdir $DIR/$tdir"
12107         verify_jobstats "$cmd" "$SINGLEMDS"
12108         # rmdir
12109         cmd="rmdir $DIR/$tdir"
12110         verify_jobstats "$cmd" "$SINGLEMDS"
12111         # mkdir on secondary MDT
12112         if [ $MDSCOUNT -gt 1 ]; then
12113                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
12114                 verify_jobstats "$cmd" "mds2"
12115         fi
12116         # mknod
12117         cmd="mknod $DIR/$tfile c 1 3"
12118         verify_jobstats "$cmd" "$SINGLEMDS"
12119         # unlink
12120         cmd="rm -f $DIR/$tfile"
12121         verify_jobstats "$cmd" "$SINGLEMDS"
12122         # create all files on OST0000 so verify_jobstats can find OST stats
12123         # open & close
12124         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
12125         verify_jobstats "$cmd" "$SINGLEMDS"
12126         # setattr
12127         cmd="touch $DIR/$tfile"
12128         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12129         # write
12130         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
12131         verify_jobstats "$cmd" "ost1"
12132         # read
12133         cancel_lru_locks osc
12134         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
12135         verify_jobstats "$cmd" "ost1"
12136         # truncate
12137         cmd="$TRUNCATE $DIR/$tfile 0"
12138         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12139         # rename
12140         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
12141         verify_jobstats "$cmd" "$SINGLEMDS"
12142         # jobstats expiry - sleep until old stats should be expired
12143         local left=$((interval_new + 5 - (SECONDS - start)))
12144         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
12145                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
12146                         "0" $left
12147         cmd="mkdir $DIR/$tdir.expire"
12148         verify_jobstats "$cmd" "$SINGLEMDS"
12149         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
12150             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
12151
12152         # Ensure that jobid are present in changelog (if supported by MDS)
12153         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
12154         then
12155                 $LFS changelog $MDT0 | tail -9
12156                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
12157                 [ $jobids -eq 9 ] ||
12158                         error "Wrong changelog jobid count $jobids != 9"
12159
12160                 # LU-5862
12161                 JOBENV="disable"
12162                 jobstats_set $JOBENV
12163                 touch $DIR/$tfile
12164                 $LFS changelog $MDT0 | tail -1
12165                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
12166                 [ $jobids -eq 0 ] ||
12167                         error "Unexpected jobids when jobid_var=$JOBENV"
12168         fi
12169
12170         cleanup_205
12171 }
12172 run_test 205 "Verify job stats"
12173
12174 # LU-1480, LU-1773 and LU-1657
12175 test_206() {
12176         mkdir -p $DIR/$tdir
12177         $SETSTRIPE -c -1 $DIR/$tdir
12178 #define OBD_FAIL_LOV_INIT 0x1403
12179         $LCTL set_param fail_loc=0xa0001403
12180         $LCTL set_param fail_val=1
12181         touch $DIR/$tdir/$tfile || true
12182 }
12183 run_test 206 "fail lov_init_raid0() doesn't lbug"
12184
12185 test_207a() {
12186         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12187         local fsz=`stat -c %s $DIR/$tfile`
12188         cancel_lru_locks mdc
12189
12190         # do not return layout in getattr intent
12191 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
12192         $LCTL set_param fail_loc=0x170
12193         local sz=`stat -c %s $DIR/$tfile`
12194
12195         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
12196
12197         rm -rf $DIR/$tfile
12198 }
12199 run_test 207a "can refresh layout at glimpse"
12200
12201 test_207b() {
12202         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12203         local cksum=`md5sum $DIR/$tfile`
12204         local fsz=`stat -c %s $DIR/$tfile`
12205         cancel_lru_locks mdc
12206         cancel_lru_locks osc
12207
12208         # do not return layout in getattr intent
12209 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
12210         $LCTL set_param fail_loc=0x171
12211
12212         # it will refresh layout after the file is opened but before read issues
12213         echo checksum is "$cksum"
12214         echo "$cksum" |md5sum -c --quiet || error "file differs"
12215
12216         rm -rf $DIR/$tfile
12217 }
12218 run_test 207b "can refresh layout at open"
12219
12220 test_208() {
12221         # FIXME: in this test suite, only RD lease is used. This is okay
12222         # for now as only exclusive open is supported. After generic lease
12223         # is done, this test suite should be revised. - Jinshan
12224
12225         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12226         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
12227                 { skip "Need MDS version at least 2.4.52"; return 0; }
12228
12229         echo "==== test 1: verify get lease work"
12230         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
12231
12232         echo "==== test 2: verify lease can be broken by upcoming open"
12233         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12234         local PID=$!
12235         sleep 1
12236
12237         $MULTIOP $DIR/$tfile oO_RDONLY:c
12238         kill -USR1 $PID && wait $PID || error "break lease error"
12239
12240         echo "==== test 3: verify lease can't be granted if an open already exists"
12241         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
12242         local PID=$!
12243         sleep 1
12244
12245         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
12246         kill -USR1 $PID && wait $PID || error "open file error"
12247
12248         echo "==== test 4: lease can sustain over recovery"
12249         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
12250         PID=$!
12251         sleep 1
12252
12253         fail mds1
12254
12255         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
12256
12257         echo "==== test 5: lease broken can't be regained by replay"
12258         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12259         PID=$!
12260         sleep 1
12261
12262         # open file to break lease and then recovery
12263         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
12264         fail mds1
12265
12266         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
12267
12268         rm -f $DIR/$tfile
12269 }
12270 run_test 208 "Exclusive open"
12271
12272 test_209() {
12273         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
12274                 skip_env "must have disp_stripe" && return
12275
12276         touch $DIR/$tfile
12277         sync; sleep 5; sync;
12278
12279         echo 3 > /proc/sys/vm/drop_caches
12280         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12281
12282         # open/close 500 times
12283         for i in $(seq 500); do
12284                 cat $DIR/$tfile
12285         done
12286
12287         echo 3 > /proc/sys/vm/drop_caches
12288         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12289
12290         echo "before: $req_before, after: $req_after"
12291         [ $((req_after - req_before)) -ge 300 ] &&
12292                 error "open/close requests are not freed"
12293         return 0
12294 }
12295 run_test 209 "read-only open/close requests should be freed promptly"
12296
12297 test_212() {
12298         size=`date +%s`
12299         size=$((size % 8192 + 1))
12300         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
12301         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
12302         rm -f $DIR/f212 $DIR/f212.xyz
12303 }
12304 run_test 212 "Sendfile test ============================================"
12305
12306 test_213() {
12307         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
12308         cancel_lru_locks osc
12309         lctl set_param fail_loc=0x8000040f
12310         # generate a read lock
12311         cat $DIR/$tfile > /dev/null
12312         # write to the file, it will try to cancel the above read lock.
12313         cat /etc/hosts >> $DIR/$tfile
12314 }
12315 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
12316
12317 test_214() { # for bug 20133
12318         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
12319         for (( i=0; i < 340; i++ )) ; do
12320                 touch $DIR/$tdir/d214c/a$i
12321         done
12322
12323         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
12324         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
12325         ls $DIR/d214c || error "ls $DIR/d214c failed"
12326         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
12327         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
12328 }
12329 run_test 214 "hash-indexed directory test - bug 20133"
12330
12331 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
12332 create_lnet_proc_files() {
12333         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
12334         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
12335
12336         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
12337         rm -f "$TMP/lnet_$1.sys_tmp"
12338 }
12339
12340 # counterpart of create_lnet_proc_files
12341 remove_lnet_proc_files() {
12342         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
12343 }
12344
12345 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12346 # 3rd arg as regexp for body
12347 check_lnet_proc_stats() {
12348         local l=$(cat "$TMP/lnet_$1" |wc -l)
12349         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
12350
12351         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
12352 }
12353
12354 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12355 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
12356 # optional and can be regexp for 2nd line (lnet.routes case)
12357 check_lnet_proc_entry() {
12358         local blp=2          # blp stands for 'position of 1st line of body'
12359         [ -z "$5" ] || blp=3 # lnet.routes case
12360
12361         local l=$(cat "$TMP/lnet_$1" |wc -l)
12362         # subtracting one from $blp because the body can be empty
12363         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12364
12365         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12366                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12367
12368         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12369                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12370
12371         # bail out if any unexpected line happened
12372         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12373         [ "$?" != 0 ] || error "$2 misformatted"
12374 }
12375
12376 test_215() { # for bugs 18102, 21079, 21517
12377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12378         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12379         local P='[1-9][0-9]*'           # positive numeric
12380         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12381         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12382         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12383         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12384
12385         local L1 # regexp for 1st line
12386         local L2 # regexp for 2nd line (optional)
12387         local BR # regexp for the rest (body)
12388
12389         # lnet.stats should look as 11 space-separated non-negative numerics
12390         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12391         create_lnet_proc_files "stats"
12392         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12393         remove_lnet_proc_files "stats"
12394
12395         # lnet.routes should look like this:
12396         # Routing disabled/enabled
12397         # net hops priority state router
12398         # where net is a string like tcp0, hops > 0, priority >= 0,
12399         # state is up/down,
12400         # router is a string like 192.168.1.1@tcp2
12401         L1="^Routing (disabled|enabled)$"
12402         L2="^net +hops +priority +state +router$"
12403         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12404         create_lnet_proc_files "routes"
12405         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12406         remove_lnet_proc_files "routes"
12407
12408         # lnet.routers should look like this:
12409         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12410         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12411         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12412         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12413         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12414         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12415         create_lnet_proc_files "routers"
12416         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12417         remove_lnet_proc_files "routers"
12418
12419         # lnet.peers should look like this:
12420         # nid refs state last max rtr min tx min queue
12421         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12422         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12423         # numeric (0 or >0 or <0), queue >= 0.
12424         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12425         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12426         create_lnet_proc_files "peers"
12427         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12428         remove_lnet_proc_files "peers"
12429
12430         # lnet.buffers  should look like this:
12431         # pages count credits min
12432         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12433         L1="^pages +count +credits +min$"
12434         BR="^ +$N +$N +$I +$I$"
12435         create_lnet_proc_files "buffers"
12436         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12437         remove_lnet_proc_files "buffers"
12438
12439         # lnet.nis should look like this:
12440         # nid status alive refs peer rtr max tx min
12441         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12442         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12443         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12444         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12445         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12446         create_lnet_proc_files "nis"
12447         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12448         remove_lnet_proc_files "nis"
12449
12450         # can we successfully write to lnet.stats?
12451         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12452         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12453 }
12454 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12455
12456 test_216() { # bug 20317
12457         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12458         remote_ost_nodsh && skip "remote OST with nodsh" && return
12459
12460         local node
12461         local facets=$(get_facets OST)
12462         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12463
12464         save_lustre_params client "osc.*.contention_seconds" > $p
12465         save_lustre_params $facets \
12466                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12467         save_lustre_params $facets \
12468                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12469         save_lustre_params $facets \
12470                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12471         clear_osc_stats
12472
12473         # agressive lockless i/o settings
12474         for node in $(osts_nodes); do
12475                 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'
12476         done
12477         lctl set_param -n osc.*.contention_seconds 60
12478
12479         $DIRECTIO write $DIR/$tfile 0 10 4096
12480         $CHECKSTAT -s 40960 $DIR/$tfile
12481
12482         # disable lockless i/o
12483         for node in $(osts_nodes); do
12484                 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'
12485         done
12486         lctl set_param -n osc.*.contention_seconds 0
12487         clear_osc_stats
12488
12489         dd if=/dev/zero of=$DIR/$tfile count=0
12490         $CHECKSTAT -s 0 $DIR/$tfile
12491
12492         restore_lustre_params <$p
12493         rm -f $p
12494         rm $DIR/$tfile
12495 }
12496 run_test 216 "check lockless direct write works and updates file size and kms correctly"
12497
12498 test_217() { # bug 22430
12499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12500         local node
12501         local nid
12502
12503         for node in $(nodes_list); do
12504                 nid=$(host_nids_address $node $NETTYPE)
12505                 if [[ $nid = *-* ]] ; then
12506                         echo "lctl ping $nid@$NETTYPE"
12507                         lctl ping $nid@$NETTYPE
12508                 else
12509                         echo "skipping $node (no hyphen detected)"
12510                 fi
12511         done
12512 }
12513 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12514
12515 test_218() {
12516        # do directio so as not to populate the page cache
12517        log "creating a 10 Mb file"
12518        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12519        log "starting reads"
12520        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12521        log "truncating the file"
12522        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12523        log "killing dd"
12524        kill %+ || true # reads might have finished
12525        echo "wait until dd is finished"
12526        wait
12527        log "removing the temporary file"
12528        rm -rf $DIR/$tfile || error "tmp file removal failed"
12529 }
12530 run_test 218 "parallel read and truncate should not deadlock ======================="
12531
12532 test_219() {
12533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12534         # write one partial page
12535         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12536         # set no grant so vvp_io_commit_write will do sync write
12537         $LCTL set_param fail_loc=0x411
12538         # write a full page at the end of file
12539         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12540
12541         $LCTL set_param fail_loc=0
12542         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12543         $LCTL set_param fail_loc=0x411
12544         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12545
12546         # LU-4201
12547         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12548         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12549 }
12550 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12551
12552 test_220() { #LU-325
12553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12554         remote_ost_nodsh && skip "remote OST with nodsh" && return
12555         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12556         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12557         local OSTIDX=0
12558
12559         # create on MDT0000 so the last_id and next_id are correct
12560         mkdir $DIR/$tdir
12561         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12562         OST=${OST%_UUID}
12563
12564         # on the mdt's osc
12565         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12566         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12567                         osc.$mdtosc_proc1.prealloc_last_id)
12568         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12569                         osc.$mdtosc_proc1.prealloc_next_id)
12570
12571         $LFS df -i
12572
12573         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12574         #define OBD_FAIL_OST_ENOINO              0x229
12575         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12576         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
12577         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12578
12579         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12580
12581         MDSOBJS=$((last_id - next_id))
12582         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12583
12584         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12585         echo "OST still has $count kbytes free"
12586
12587         echo "create $MDSOBJS files @next_id..."
12588         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12589
12590         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12591                         osc.$mdtosc_proc1.prealloc_last_id)
12592         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12593                         osc.$mdtosc_proc1.prealloc_next_id)
12594
12595         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12596         $LFS df -i
12597
12598         echo "cleanup..."
12599
12600         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12601         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12602
12603         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12604         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12605         echo "unlink $MDSOBJS files @$next_id..."
12606         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12607 }
12608 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12609
12610 test_221() {
12611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12612         dd if=`which date` of=$MOUNT/date oflag=sync
12613         chmod +x $MOUNT/date
12614
12615         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12616         $LCTL set_param fail_loc=0x80001401
12617
12618         $MOUNT/date > /dev/null
12619         rm -f $MOUNT/date
12620 }
12621 run_test 221 "make sure fault and truncate race to not cause OOM"
12622
12623 test_222a () {
12624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12625        rm -rf $DIR/$tdir
12626        test_mkdir -p $DIR/$tdir
12627        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12628        createmany -o $DIR/$tdir/$tfile 10
12629        cancel_lru_locks mdc
12630        cancel_lru_locks osc
12631        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12632        $LCTL set_param fail_loc=0x31a
12633        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12634        $LCTL set_param fail_loc=0
12635        rm -r $DIR/$tdir
12636 }
12637 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12638
12639 test_222b () {
12640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12641         rm -rf $DIR/$tdir
12642         test_mkdir -p $DIR/$tdir
12643         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12644         createmany -o $DIR/$tdir/$tfile 10
12645         cancel_lru_locks mdc
12646         cancel_lru_locks osc
12647         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12648         $LCTL set_param fail_loc=0x31a
12649         rm -r $DIR/$tdir || error "AGL for rmdir failed"
12650         $LCTL set_param fail_loc=0
12651 }
12652 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12653
12654 test_223 () {
12655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12656        rm -rf $DIR/$tdir
12657        test_mkdir -p $DIR/$tdir
12658        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12659        createmany -o $DIR/$tdir/$tfile 10
12660        cancel_lru_locks mdc
12661        cancel_lru_locks osc
12662        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12663        $LCTL set_param fail_loc=0x31b
12664        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12665        $LCTL set_param fail_loc=0
12666        rm -r $DIR/$tdir
12667 }
12668 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12669
12670 test_224a() { # LU-1039, MRP-303
12671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12672         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12673         $LCTL set_param fail_loc=0x508
12674         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12675         $LCTL set_param fail_loc=0
12676         df $DIR
12677 }
12678 run_test 224a "Don't panic on bulk IO failure"
12679
12680 test_224b() { # LU-1039, MRP-303
12681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12682         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12683         cancel_lru_locks osc
12684         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12685         $LCTL set_param fail_loc=0x515
12686         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12687         $LCTL set_param fail_loc=0
12688         df $DIR
12689 }
12690 run_test 224b "Don't panic on bulk IO failure"
12691
12692 test_224c() { # LU-6441
12693         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12694         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12695
12696         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12697         save_writethrough $p
12698         set_cache writethrough on
12699
12700         local pages_per_rpc=$($LCTL get_param \
12701                                 osc.*.max_pages_per_rpc)
12702         local at_max=$($LCTL get_param -n at_max)
12703         local timeout=$($LCTL get_param -n timeout)
12704         local test_at="$LCTL get_param -n at_max"
12705         local param_at="$FSNAME.sys.at_max"
12706         local test_timeout="$LCTL get_param -n timeout"
12707         local param_timeout="$FSNAME.sys.timeout"
12708
12709         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12710
12711         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
12712                 error "conf_param at_max=0 failed"
12713         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
12714                 error "conf_param timeout=5 failed"
12715
12716         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
12717         do_facet ost1 $LCTL set_param fail_loc=0x520
12718         $LFS setstripe -c 1 -i 0 $DIR/$tfile
12719         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
12720         sync
12721         do_facet ost1 $LCTL set_param fail_loc=0
12722
12723         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
12724                 error "conf_param at_max=$at_max failed"
12725         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
12726                 $timeout || error "conf_param timeout=$timeout failed"
12727
12728         $LCTL set_param -n $pages_per_rpc
12729         restore_lustre_params < $p
12730         rm -f $p
12731 }
12732 run_test 224c "Don't hang if one of md lost during large bulk RPC"
12733
12734 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12735 test_225a () {
12736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12737         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12738         if [ -z ${MDSSURVEY} ]; then
12739               skip_env "mds-survey not found" && return
12740         fi
12741
12742         [ $MDSCOUNT -ge 2 ] &&
12743                 skip "skipping now for more than one MDT" && return
12744
12745        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12746             { skip "Need MDS version at least 2.2.51"; return; }
12747
12748        local mds=$(facet_host $SINGLEMDS)
12749        local target=$(do_nodes $mds 'lctl dl' | \
12750                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12751
12752        local cmd1="file_count=1000 thrhi=4"
12753        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12754        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12755        local cmd="$cmd1 $cmd2 $cmd3"
12756
12757        rm -f ${TMP}/mds_survey*
12758        echo + $cmd
12759        eval $cmd || error "mds-survey with zero-stripe failed"
12760        cat ${TMP}/mds_survey*
12761        rm -f ${TMP}/mds_survey*
12762 }
12763 run_test 225a "Metadata survey sanity with zero-stripe"
12764
12765 test_225b () {
12766         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12767         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12768         if [ -z ${MDSSURVEY} ]; then
12769               skip_env "mds-survey not found" && return
12770         fi
12771         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12772             { skip "Need MDS version at least 2.2.51"; return; }
12773
12774         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12775               skip_env "Need to mount OST to test" && return
12776         fi
12777
12778         [ $MDSCOUNT -ge 2 ] &&
12779                 skip "skipping now for more than one MDT" && return
12780        local mds=$(facet_host $SINGLEMDS)
12781        local target=$(do_nodes $mds 'lctl dl' | \
12782                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12783
12784        local cmd1="file_count=1000 thrhi=4"
12785        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12786        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12787        local cmd="$cmd1 $cmd2 $cmd3"
12788
12789        rm -f ${TMP}/mds_survey*
12790        echo + $cmd
12791        eval $cmd || error "mds-survey with stripe_count failed"
12792        cat ${TMP}/mds_survey*
12793        rm -f ${TMP}/mds_survey*
12794 }
12795 run_test 225b "Metadata survey sanity with stripe_count = 1"
12796
12797 mcreate_path2fid () {
12798         local mode=$1
12799         local major=$2
12800         local minor=$3
12801         local name=$4
12802         local desc=$5
12803         local path=$DIR/$tdir/$name
12804         local fid
12805         local rc
12806         local fid_path
12807
12808         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12809                 error "cannot create $desc"
12810
12811         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12812         rc=$?
12813         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12814
12815         fid_path=$($LFS fid2path $MOUNT $fid)
12816         rc=$?
12817         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12818
12819         [ "$path" == "$fid_path" ] ||
12820                 error "fid2path returned $fid_path, expected $path"
12821
12822         echo "pass with $path and $fid"
12823 }
12824
12825 test_226a () {
12826         rm -rf $DIR/$tdir
12827         mkdir -p $DIR/$tdir
12828
12829         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12830         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12831         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12832         mcreate_path2fid 0040666 0 0 dir "directory"
12833         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12834         mcreate_path2fid 0100666 0 0 file "regular file"
12835         mcreate_path2fid 0120666 0 0 link "symbolic link"
12836         mcreate_path2fid 0140666 0 0 sock "socket"
12837 }
12838 run_test 226a "call path2fid and fid2path on files of all type"
12839
12840 test_226b () {
12841         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12842         rm -rf $DIR/$tdir
12843         local MDTIDX=1
12844
12845         mkdir -p $DIR/$tdir
12846         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12847                 error "create remote directory failed"
12848         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12849         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12850                                 "character special file (null)"
12851         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12852                                 "character special file (no device)"
12853         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12854         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12855                                 "block special file (loop)"
12856         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12857         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12858         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12859 }
12860 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12861
12862 # LU-1299 Executing or running ldd on a truncated executable does not
12863 # cause an out-of-memory condition.
12864 test_227() {
12865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12866         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12867         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12868         chmod +x $MOUNT/date
12869
12870         $MOUNT/date > /dev/null
12871         ldd $MOUNT/date > /dev/null
12872         rm -f $MOUNT/date
12873 }
12874 run_test 227 "running truncated executable does not cause OOM"
12875
12876 # LU-1512 try to reuse idle OI blocks
12877 test_228a() {
12878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12879         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12880         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12881                 skip "non-ldiskfs backend" && return
12882
12883         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12884         local myDIR=$DIR/$tdir
12885
12886         mkdir -p $myDIR
12887         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12888         $LCTL set_param fail_loc=0x80001002
12889         createmany -o $myDIR/t- 10000
12890         $LCTL set_param fail_loc=0
12891         # The guard is current the largest FID holder
12892         touch $myDIR/guard
12893         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12894                     tr -d '[')
12895         local IDX=$(($SEQ % 64))
12896
12897         do_facet $SINGLEMDS sync
12898         # Make sure journal flushed.
12899         sleep 6
12900         local blk1=$(do_facet $SINGLEMDS \
12901                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12902                      grep Blockcount | awk '{print $4}')
12903
12904         # Remove old files, some OI blocks will become idle.
12905         unlinkmany $myDIR/t- 10000
12906         # Create new files, idle OI blocks should be reused.
12907         createmany -o $myDIR/t- 2000
12908         do_facet $SINGLEMDS sync
12909         # Make sure journal flushed.
12910         sleep 6
12911         local blk2=$(do_facet $SINGLEMDS \
12912                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12913                      grep Blockcount | awk '{print $4}')
12914
12915         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12916 }
12917 run_test 228a "try to reuse idle OI blocks"
12918
12919 test_228b() {
12920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12921         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12922         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12923                 skip "non-ldiskfs backend" && return
12924
12925         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12926         local myDIR=$DIR/$tdir
12927
12928         mkdir -p $myDIR
12929         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12930         $LCTL set_param fail_loc=0x80001002
12931         createmany -o $myDIR/t- 10000
12932         $LCTL set_param fail_loc=0
12933         # The guard is current the largest FID holder
12934         touch $myDIR/guard
12935         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12936                     tr -d '[')
12937         local IDX=$(($SEQ % 64))
12938
12939         do_facet $SINGLEMDS sync
12940         # Make sure journal flushed.
12941         sleep 6
12942         local blk1=$(do_facet $SINGLEMDS \
12943                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12944                      grep Blockcount | awk '{print $4}')
12945
12946         # Remove old files, some OI blocks will become idle.
12947         unlinkmany $myDIR/t- 10000
12948
12949         # stop the MDT
12950         stop $SINGLEMDS || error "Fail to stop MDT."
12951         # remount the MDT
12952         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12953
12954         df $MOUNT || error "Fail to df."
12955         # Create new files, idle OI blocks should be reused.
12956         createmany -o $myDIR/t- 2000
12957         do_facet $SINGLEMDS sync
12958         # Make sure journal flushed.
12959         sleep 6
12960         local blk2=$(do_facet $SINGLEMDS \
12961                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12962                      grep Blockcount | awk '{print $4}')
12963
12964         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12965 }
12966 run_test 228b "idle OI blocks can be reused after MDT restart"
12967
12968 #LU-1881
12969 test_228c() {
12970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12971         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12972         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12973                 skip "non-ldiskfs backend" && return
12974
12975         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12976         local myDIR=$DIR/$tdir
12977
12978         mkdir -p $myDIR
12979         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12980         $LCTL set_param fail_loc=0x80001002
12981         # 20000 files can guarantee there are index nodes in the OI file
12982         createmany -o $myDIR/t- 20000
12983         $LCTL set_param fail_loc=0
12984         # The guard is current the largest FID holder
12985         touch $myDIR/guard
12986         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12987                     tr -d '[')
12988         local IDX=$(($SEQ % 64))
12989
12990         do_facet $SINGLEMDS sync
12991         # Make sure journal flushed.
12992         sleep 6
12993         local blk1=$(do_facet $SINGLEMDS \
12994                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12995                      grep Blockcount | awk '{print $4}')
12996
12997         # Remove old files, some OI blocks will become idle.
12998         unlinkmany $myDIR/t- 20000
12999         rm -f $myDIR/guard
13000         # The OI file should become empty now
13001
13002         # Create new files, idle OI blocks should be reused.
13003         createmany -o $myDIR/t- 2000
13004         do_facet $SINGLEMDS sync
13005         # Make sure journal flushed.
13006         sleep 6
13007         local blk2=$(do_facet $SINGLEMDS \
13008                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13009                      grep Blockcount | awk '{print $4}')
13010
13011         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13012 }
13013 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
13014
13015 test_229() { # LU-2482, LU-3448
13016         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
13017                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
13018                 return
13019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13020         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
13021
13022         rm -f $DIR/$tfile
13023
13024         # Create a file with a released layout and stripe count 2.
13025         $MULTIOP $DIR/$tfile H2c ||
13026                 error "failed to create file with released layout"
13027
13028         $GETSTRIPE -v $DIR/$tfile
13029
13030         local pattern=$($GETSTRIPE -L $DIR/$tfile)
13031         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
13032
13033         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
13034         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
13035         stat $DIR/$tfile || error "failed to stat released file"
13036
13037         chown $RUNAS_ID $DIR/$tfile ||
13038                 error "chown $RUNAS_ID $DIR/$tfile failed"
13039
13040         chgrp $RUNAS_ID $DIR/$tfile ||
13041                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
13042
13043         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
13044         rm $DIR/$tfile || error "failed to remove released file"
13045 }
13046 run_test 229 "getstripe/stat/rm/attr changes work on released files"
13047
13048 test_230a() {
13049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13050         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13051         local MDTIDX=1
13052
13053         test_mkdir $DIR/$tdir
13054         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
13055         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
13056         [ $mdt_idx -ne 0 ] &&
13057                 error "create local directory on wrong MDT $mdt_idx"
13058
13059         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
13060                         error "create remote directory failed"
13061         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
13062         [ $mdt_idx -ne $MDTIDX ] &&
13063                 error "create remote directory on wrong MDT $mdt_idx"
13064
13065         createmany -o $DIR/$tdir/test_230/t- 10 ||
13066                 error "create files on remote directory failed"
13067         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
13068         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
13069         rm -r $DIR/$tdir || error "unlink remote directory failed"
13070 }
13071 run_test 230a "Create remote directory and files under the remote directory"
13072
13073 test_230b() {
13074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13075         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13076         local MDTIDX=1
13077         local mdt_index
13078         local i
13079         local file
13080         local pid
13081         local stripe_count
13082         local migrate_dir=$DIR/$tdir/migrate_dir
13083         local other_dir=$DIR/$tdir/other_dir
13084
13085         test_mkdir $DIR/$tdir
13086         test_mkdir -i0 -c1 $migrate_dir
13087         test_mkdir -i0 -c1 $other_dir
13088         for ((i=0; i<10; i++)); do
13089                 mkdir -p $migrate_dir/dir_${i}
13090                 createmany -o $migrate_dir/dir_${i}/f 10 ||
13091                         error "create files under remote dir failed $i"
13092         done
13093
13094         cp /etc/passwd $migrate_dir/$tfile
13095         cp /etc/passwd $other_dir/$tfile
13096         chattr +SAD $migrate_dir
13097         chattr +SAD $migrate_dir/$tfile
13098
13099         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13100         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13101         local old_dir_mode=$(stat -c%f $migrate_dir)
13102         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
13103
13104         mkdir -p $migrate_dir/dir_default_stripe2
13105         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
13106         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
13107
13108         mkdir -p $other_dir
13109         ln $migrate_dir/$tfile $other_dir/luna
13110         ln $migrate_dir/$tfile $migrate_dir/sofia
13111         ln $other_dir/$tfile $migrate_dir/david
13112         ln -s $migrate_dir/$tfile $other_dir/zachary
13113         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
13114         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
13115
13116         $LFS migrate -m $MDTIDX $migrate_dir ||
13117                 error "fails on migrating remote dir to MDT1"
13118
13119         echo "migratate to MDT1, then checking.."
13120         for ((i = 0; i < 10; i++)); do
13121                 for file in $(find $migrate_dir/dir_${i}); do
13122                         mdt_index=$($LFS getstripe -M $file)
13123                         [ $mdt_index == $MDTIDX ] ||
13124                                 error "$file is not on MDT${MDTIDX}"
13125                 done
13126         done
13127
13128         # the multiple link file should still in MDT0
13129         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
13130         [ $mdt_index == 0 ] ||
13131                 error "$file is not on MDT${MDTIDX}"
13132
13133         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13134         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13135                 error " expect $old_dir_flag get $new_dir_flag"
13136
13137         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13138         [ "$old_file_flag" = "$new_file_flag" ] ||
13139                 error " expect $old_file_flag get $new_file_flag"
13140
13141         local new_dir_mode=$(stat -c%f $migrate_dir)
13142         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13143                 error "expect mode $old_dir_mode get $new_dir_mode"
13144
13145         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13146         [ "$old_file_mode" = "$new_file_mode" ] ||
13147                 error "expect mode $old_file_mode get $new_file_mode"
13148
13149         diff /etc/passwd $migrate_dir/$tfile ||
13150                 error "$tfile different after migration"
13151
13152         diff /etc/passwd $other_dir/luna ||
13153                 error "luna different after migration"
13154
13155         diff /etc/passwd $migrate_dir/sofia ||
13156                 error "sofia different after migration"
13157
13158         diff /etc/passwd $migrate_dir/david ||
13159                 error "david different after migration"
13160
13161         diff /etc/passwd $other_dir/zachary ||
13162                 error "zachary different after migration"
13163
13164         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13165                 error "${tfile}_ln different after migration"
13166
13167         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13168                 error "${tfile}_ln_other different after migration"
13169
13170         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
13171         [ $stripe_count = 2 ] ||
13172                 error "dir strpe_count $d != 2 after migration."
13173
13174         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
13175         [ $stripe_count = 2 ] ||
13176                 error "file strpe_count $d != 2 after migration."
13177
13178         #migrate back to MDT0
13179         MDTIDX=0
13180
13181         $LFS migrate -m $MDTIDX $migrate_dir ||
13182                 error "fails on migrating remote dir to MDT0"
13183
13184         echo "migrate back to MDT0, checking.."
13185         for file in $(find $migrate_dir); do
13186                 mdt_index=$($LFS getstripe -M $file)
13187                 [ $mdt_index == $MDTIDX ] ||
13188                         error "$file is not on MDT${MDTIDX}"
13189         done
13190
13191         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13192         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13193                 error " expect $old_dir_flag get $new_dir_flag"
13194
13195         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13196         [ "$old_file_flag" = "$new_file_flag" ] ||
13197                 error " expect $old_file_flag get $new_file_flag"
13198
13199         local new_dir_mode=$(stat -c%f $migrate_dir)
13200         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13201                 error "expect mode $old_dir_mode get $new_dir_mode"
13202
13203         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13204         [ "$old_file_mode" = "$new_file_mode" ] ||
13205                 error "expect mode $old_file_mode get $new_file_mode"
13206
13207         diff /etc/passwd ${migrate_dir}/$tfile ||
13208                 error "$tfile different after migration"
13209
13210         diff /etc/passwd ${other_dir}/luna ||
13211                 error "luna different after migration"
13212
13213         diff /etc/passwd ${migrate_dir}/sofia ||
13214                 error "sofia different after migration"
13215
13216         diff /etc/passwd ${other_dir}/zachary ||
13217                 error "zachary different after migration"
13218
13219         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13220                 error "${tfile}_ln different after migration"
13221
13222         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13223                 error "${tfile}_ln_other different after migration"
13224
13225         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
13226         [ $stripe_count = 2 ] ||
13227                 error "dir strpe_count $d != 2 after migration."
13228
13229         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
13230         [ $stripe_count = 2 ] ||
13231                 error "file strpe_count $d != 2 after migration."
13232
13233         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13234 }
13235 run_test 230b "migrate directory"
13236
13237 test_230c() {
13238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13239         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13240         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13241         local MDTIDX=1
13242         local mdt_index
13243         local file
13244         local migrate_dir=$DIR/$tdir/migrate_dir
13245
13246         #If migrating directory fails in the middle, all entries of
13247         #the directory is still accessiable.
13248         test_mkdir $DIR/$tdir
13249         test_mkdir -i0 -c1 $migrate_dir
13250         stat $migrate_dir
13251         createmany -o $migrate_dir/f 10 ||
13252                 error "create files under ${migrate_dir} failed"
13253
13254         #failed after migrating 5 entries
13255         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
13256         do_facet mds1 lctl set_param fail_loc=0x20001801
13257         do_facet mds1 lctl  set_param fail_val=5
13258         local t=$(ls $migrate_dir | wc -l)
13259         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
13260                 error "migrate should fail after 5 entries"
13261
13262         mkdir $migrate_dir/dir &&
13263                 error "mkdir succeeds under migrating directory"
13264         touch $migrate_dir/file &&
13265                 error "touch file succeeds under migrating directory"
13266
13267         local u=$(ls $migrate_dir | wc -l)
13268         [ "$u" == "$t" ] || error "$u != $t during migration"
13269
13270         for file in $(find $migrate_dir); do
13271                 stat $file || error "stat $file failed"
13272         done
13273
13274         do_facet mds1 lctl set_param fail_loc=0
13275         do_facet mds1 lctl set_param fail_val=0
13276
13277         $LFS migrate -m $MDTIDX $migrate_dir ||
13278                 error "migrate open files should failed with open files"
13279
13280         echo "Finish migration, then checking.."
13281         for file in $(find $migrate_dir); do
13282                 mdt_index=$($LFS getstripe -M $file)
13283                 [ $mdt_index == $MDTIDX ] ||
13284                         error "$file is not on MDT${MDTIDX}"
13285         done
13286
13287         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13288 }
13289 run_test 230c "check directory accessiblity if migration is failed"
13290
13291 test_230d() {
13292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13293         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13294         local MDTIDX=1
13295         local mdt_index
13296         local migrate_dir=$DIR/$tdir/migrate_dir
13297         local i
13298         local j
13299
13300         test_mkdir $DIR/$tdir
13301         test_mkdir -i0 -c1 $migrate_dir
13302
13303         for ((i=0; i<100; i++)); do
13304                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
13305                 createmany -o $migrate_dir/dir_${i}/f 100 ||
13306                         error "create files under remote dir failed $i"
13307         done
13308
13309         $LFS migrate -m $MDTIDX $migrate_dir ||
13310                 error "migrate remote dir error"
13311
13312         echo "Finish migration, then checking.."
13313         for file in $(find $migrate_dir); do
13314                 mdt_index=$($LFS getstripe -M $file)
13315                 [ $mdt_index == $MDTIDX ] ||
13316                         error "$file is not on MDT${MDTIDX}"
13317         done
13318
13319         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13320 }
13321 run_test 230d "check migrate big directory"
13322
13323 test_230e() {
13324         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13325         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13326         local i
13327         local j
13328         local a_fid
13329         local b_fid
13330
13331         mkdir -p $DIR/$tdir
13332         mkdir $DIR/$tdir/migrate_dir
13333         mkdir $DIR/$tdir/other_dir
13334         touch $DIR/$tdir/migrate_dir/a
13335         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
13336         ls $DIR/$tdir/other_dir
13337
13338         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13339                 error "migrate dir fails"
13340
13341         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13342         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13343
13344         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13345         [ $mdt_index == 0 ] || error "a is not on MDT0"
13346
13347         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
13348                 error "migrate dir fails"
13349
13350         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
13351         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
13352
13353         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13354         [ $mdt_index == 1 ] || error "a is not on MDT1"
13355
13356         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
13357         [ $mdt_index == 1 ] || error "b is not on MDT1"
13358
13359         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13360         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
13361
13362         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
13363
13364         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13365 }
13366 run_test 230e "migrate mulitple local link files"
13367
13368 test_230f() {
13369         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13370         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13371         local a_fid
13372         local ln_fid
13373
13374         mkdir -p $DIR/$tdir
13375         mkdir $DIR/$tdir/migrate_dir
13376         $LFS mkdir -i1 $DIR/$tdir/other_dir
13377         touch $DIR/$tdir/migrate_dir/a
13378         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13379         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13380         ls $DIR/$tdir/other_dir
13381
13382         # a should be migrated to MDT1, since no other links on MDT0
13383         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13384                 error "#1 migrate dir fails"
13385         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13386         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13387         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13388         [ $mdt_index == 1 ] || error "a is not on MDT1"
13389
13390         # a should stay on MDT1, because it is a mulitple link file
13391         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13392                 error "#2 migrate dir fails"
13393         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13394         [ $mdt_index == 1 ] || error "a is not on MDT1"
13395
13396         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13397                 error "#3 migrate dir fails"
13398
13399         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13400         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13401         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13402
13403         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13404         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13405
13406         # a should be migrated to MDT0, since no other links on MDT1
13407         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13408                 error "#4 migrate dir fails"
13409         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13410         [ $mdt_index == 0 ] || error "a is not on MDT0"
13411
13412         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13413 }
13414 run_test 230f "migrate mulitple remote link files"
13415
13416 test_230g() {
13417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13418         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13419
13420         mkdir -p $DIR/$tdir/migrate_dir
13421
13422         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
13423                 error "migrating dir to non-exist MDT succeeds"
13424         true
13425 }
13426 run_test 230g "migrate dir to non-exist MDT"
13427
13428 test_230h() {
13429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13430         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13431         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
13432                 skip "Need MDS version at least 2.7.64" && return
13433         local mdt_index
13434
13435         mkdir -p $DIR/$tdir/migrate_dir
13436
13437         $LFS migrate -m1 $DIR &&
13438                 error "migrating mountpoint1 should fail"
13439
13440         $LFS migrate -m1 $DIR/$tdir/.. &&
13441                 error "migrating mountpoint2 should fail"
13442
13443         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
13444                 error "migrating $tdir fail"
13445
13446         mdt_index=$($LFS getstripe -M $DIR/$tdir)
13447         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
13448
13449         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13450         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
13451
13452 }
13453 run_test 230h "migrate .. and root"
13454
13455 test_230i() {
13456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13457         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13458
13459         mkdir -p $DIR/$tdir/migrate_dir
13460
13461         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
13462                 error "migration fails with a tailing slash"
13463
13464         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
13465                 error "migration fails with two tailing slashes"
13466 }
13467 run_test 230i "lfs migrate -m tolerates trailing slashes"
13468
13469 test_231a()
13470 {
13471         # For simplicity this test assumes that max_pages_per_rpc
13472         # is the same across all OSCs
13473         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
13474         local bulk_size=$((max_pages * 4096))
13475
13476         mkdir -p $DIR/$tdir
13477
13478         # clear the OSC stats
13479         $LCTL set_param osc.*.stats=0 &>/dev/null
13480         stop_writeback
13481
13482         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
13483         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
13484                 oflag=direct &>/dev/null || error "dd failed"
13485
13486         sync; sleep 1; sync # just to be safe
13487         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
13488         if [ x$nrpcs != "x1" ]; then
13489                 $LCTL get_param osc.*.stats
13490                 error "found $nrpcs ost_write RPCs, not 1 as expected"
13491         fi
13492
13493         start_writeback
13494         # Drop the OSC cache, otherwise we will read from it
13495         cancel_lru_locks osc
13496
13497         # clear the OSC stats
13498         $LCTL set_param osc.*.stats=0 &>/dev/null
13499
13500         # Client reads $bulk_size.
13501         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
13502                 iflag=direct &>/dev/null || error "dd failed"
13503
13504         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
13505         if [ x$nrpcs != "x1" ]; then
13506                 $LCTL get_param osc.*.stats
13507                 error "found $nrpcs ost_read RPCs, not 1 as expected"
13508         fi
13509 }
13510 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
13511
13512 test_231b() {
13513         mkdir -p $DIR/$tdir
13514         local i
13515         for i in {0..1023}; do
13516                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
13517                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
13518                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
13519         done
13520         sync
13521 }
13522 run_test 231b "must not assert on fully utilized OST request buffer"
13523
13524 test_232() {
13525         mkdir -p $DIR/$tdir
13526         #define OBD_FAIL_LDLM_OST_LVB            0x31c
13527         $LCTL set_param fail_loc=0x31c
13528
13529         # ignore dd failure
13530         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
13531
13532         $LCTL set_param fail_loc=0
13533         umount_client $MOUNT || error "umount failed"
13534         mount_client $MOUNT || error "mount failed"
13535 }
13536 run_test 232 "failed lock should not block umount"
13537
13538 test_233a() {
13539         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13540         { skip "Need MDS version at least 2.3.64"; return; }
13541         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
13542
13543         local fid=$($LFS path2fid $MOUNT)
13544         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13545                 error "cannot access $MOUNT using its FID '$fid'"
13546 }
13547 run_test 233a "checking that OBF of the FS root succeeds"
13548
13549 test_233b() {
13550         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13551         { skip "Need MDS version at least 2.5.90"; return; }
13552         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
13553
13554         local fid=$($LFS path2fid $MOUNT/.lustre)
13555         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13556                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13557
13558         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13559         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13560                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13561 }
13562 run_test 233b "checking that OBF of the FS .lustre succeeds"
13563
13564 test_234() {
13565         local p="$TMP/sanityN-$TESTNAME.parameters"
13566         save_lustre_params client "llite.*.xattr_cache" > $p
13567         lctl set_param llite.*.xattr_cache 1 ||
13568                 { skip "xattr cache is not supported"; return 0; }
13569
13570         mkdir -p $DIR/$tdir || error "mkdir failed"
13571         touch $DIR/$tdir/$tfile || error "touch failed"
13572         # OBD_FAIL_LLITE_XATTR_ENOMEM
13573         $LCTL set_param fail_loc=0x1405
13574         # output of the form: attr 2 4 44 3 fc13 x86_64
13575         V=($(IFS=".-" rpm -q attr))
13576         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13577               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13578                 # attr pre-2.4.44-7 had a bug with rc
13579                 # LU-3703 - SLES 11 and FC13 clients have older attr
13580                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13581                         error "getfattr should have failed with ENOMEM"
13582         else
13583                 skip "LU-3703: attr version $(getfattr --version) too old"
13584         fi
13585         $LCTL set_param fail_loc=0x0
13586         rm -rf $DIR/$tdir
13587
13588         restore_lustre_params < $p
13589         rm -f $p
13590 }
13591 run_test 234 "xattr cache should not crash on ENOMEM"
13592
13593 test_235() {
13594         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13595                 skip "Need MDS version at least 2.4.52" && return
13596         flock_deadlock $DIR/$tfile
13597         local RC=$?
13598         case $RC in
13599                 0)
13600                 ;;
13601                 124) error "process hangs on a deadlock"
13602                 ;;
13603                 *) error "error executing flock_deadlock $DIR/$tfile"
13604                 ;;
13605         esac
13606 }
13607 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13608
13609 #LU-2935
13610 test_236() {
13611         check_swap_layouts_support && return 0
13612         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
13613
13614         local ref1=/etc/passwd
13615         local ref2=/etc/group
13616         local file1=$DIR/$tdir/f1
13617         local file2=$DIR/$tdir/f2
13618
13619         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13620         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13621         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13622         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13623         local fd=$(free_fd)
13624         local cmd="exec $fd<>$file2"
13625         eval $cmd
13626         rm $file2
13627         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13628                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13629         cmd="exec $fd>&-"
13630         eval $cmd
13631         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13632
13633         #cleanup
13634         rm -rf $DIR/$tdir
13635 }
13636 run_test 236 "Layout swap on open unlinked file"
13637
13638 # test to verify file handle related system calls
13639 # (name_to_handle_at/open_by_handle_at)
13640 # The new system calls are supported in glibc >= 2.14.
13641
13642 test_237() {
13643         echo "Test file_handle syscalls" > $DIR/$tfile ||
13644                 error "write failed"
13645         check_fhandle_syscalls $DIR/$tfile ||
13646                 error "check_fhandle_syscalls failed"
13647 }
13648 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13649
13650 # LU-4659 linkea consistency
13651 test_238() {
13652         local server_version=$(lustre_version_code $SINGLEMDS)
13653
13654         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13655                 [[ $server_version -gt $(version_code 2.5.1) &&
13656                    $server_version -lt $(version_code 2.5.50) ]] ||
13657                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13658
13659         touch $DIR/$tfile
13660         ln $DIR/$tfile $DIR/$tfile.lnk
13661         touch $DIR/$tfile.new
13662         mv $DIR/$tfile.new $DIR/$tfile
13663         local fid1=$($LFS path2fid $DIR/$tfile)
13664         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13665         local path1=$($LFS fid2path $FSNAME "$fid1")
13666         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13667         local path2=$($LFS fid2path $FSNAME "$fid2")
13668         [ $tfile.lnk == $path2 ] ||
13669                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13670         rm -f $DIR/$tfile*
13671 }
13672 run_test 238 "Verify linkea consistency"
13673
13674 test_239() {
13675         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13676                 skip "Need MDS version at least 2.5.60" && return
13677         local list=$(comma_list $(mdts_nodes))
13678
13679         mkdir -p $DIR/$tdir
13680         createmany -o $DIR/$tdir/f- 5000
13681         unlinkmany $DIR/$tdir/f- 5000
13682         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13683         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13684                         osc.*MDT*.sync_in_flight" | calc_sum)
13685         [ "$changes" -eq 0 ] || error "$changes not synced"
13686 }
13687 run_test 239 "osp_sync test"
13688
13689 test_239a() { #LU-5297
13690         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13691         touch $DIR/$tfile
13692         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13693         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13694         chgrp $RUNAS_GID $DIR/$tfile
13695         wait_delete_completed
13696 }
13697 run_test 239a "process invalid osp sync record correctly"
13698
13699 test_239b() { #LU-5297
13700         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13701         touch $DIR/$tfile1
13702         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13703         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13704         chgrp $RUNAS_GID $DIR/$tfile1
13705         wait_delete_completed
13706         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13707         touch $DIR/$tfile2
13708         chgrp $RUNAS_GID $DIR/$tfile2
13709         wait_delete_completed
13710 }
13711 run_test 239b "process osp sync record with ENOMEM error correctly"
13712
13713 test_240() {
13714         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13715         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13716
13717         mkdir -p $DIR/$tdir
13718
13719         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
13720                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
13721         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
13722                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
13723
13724         umount_client $MOUNT || error "umount failed"
13725         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
13726         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
13727         mount_client $MOUNT || error "failed to mount client"
13728
13729         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
13730         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
13731 }
13732 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
13733
13734 test_241_bio() {
13735         for LOOP in $(seq $1); do
13736                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
13737                 cancel_lru_locks osc || true
13738         done
13739 }
13740
13741 test_241_dio() {
13742         for LOOP in $(seq $1); do
13743                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
13744                                                 iflag=direct 2>/dev/null
13745         done
13746 }
13747
13748 test_241a() { # was test_241
13749         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13750         ls -la $DIR/$tfile
13751         cancel_lru_locks osc
13752         test_241_bio 1000 &
13753         PID=$!
13754         test_241_dio 1000
13755         wait $PID
13756 }
13757 run_test 241a "bio vs dio"
13758
13759 test_241b() {
13760         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13761         ls -la $DIR/$tfile
13762         test_241_dio 1000 &
13763         PID=$!
13764         test_241_dio 1000
13765         wait $PID
13766 }
13767 run_test 241b "dio vs dio"
13768
13769 test_242() {
13770         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13771         mkdir -p $DIR/$tdir
13772         touch $DIR/$tdir/$tfile
13773
13774         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
13775         do_facet mds1 lctl set_param fail_loc=0x105
13776         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
13777
13778         do_facet mds1 lctl set_param fail_loc=0
13779         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
13780 }
13781 run_test 242 "mdt_readpage failure should not cause directory unreadable"
13782
13783 test_243()
13784 {
13785         test_mkdir -p $DIR/$tdir
13786         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
13787 }
13788 run_test 243 "various group lock tests"
13789
13790 test_244()
13791 {
13792         test_mkdir -p $DIR/$tdir
13793         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
13794         sendfile_grouplock $DIR/$tdir/$tfile || \
13795                 error "sendfile+grouplock failed"
13796         rm -rf $DIR/$tdir
13797 }
13798 run_test 244 "sendfile with group lock tests"
13799
13800 test_245() {
13801         local flagname="multi_mod_rpcs"
13802         local connect_data_name="max_mod_rpcs"
13803         local out
13804
13805         # check if multiple modify RPCs flag is set
13806         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
13807                 grep "connect_flags:")
13808         echo "$out"
13809
13810         echo "$out" | grep -qw $flagname
13811         if [ $? -ne 0 ]; then
13812                 echo "connect flag $flagname is not set"
13813                 return
13814         fi
13815
13816         # check if multiple modify RPCs data is set
13817         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
13818         echo "$out"
13819
13820         echo "$out" | grep -qw $connect_data_name ||
13821                 error "import should have connect data $connect_data_name"
13822 }
13823 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
13824
13825 test_246() { # LU-7371
13826         remote_ost_nodsh && skip "remote OST with nodsh" && return
13827         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
13828                 skip "Need OST version >= 2.7.62" && return 0
13829         do_facet ost1 $LCTL set_param fail_val=4095
13830 #define OBD_FAIL_OST_READ_SIZE          0x234
13831         do_facet ost1 $LCTL set_param fail_loc=0x234
13832         $LFS setstripe $DIR/$tfile -i 0 -c 1
13833         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
13834         cancel_lru_locks $FSNAME-OST0000
13835         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
13836 }
13837 run_test 246 "Read file of size 4095 should return right length"
13838
13839 test_247a() {
13840         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
13841                 grep -q subtree ||
13842                 { skip "Fileset feature is not supported"; return; }
13843
13844         local submount=${MOUNT}_$tdir
13845
13846         mkdir $MOUNT/$tdir
13847         mkdir -p $submount || error "mkdir $submount failed"
13848         FILESET="$FILESET/$tdir" mount_client $submount ||
13849                 error "mount $submount failed"
13850         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
13851         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
13852                 error "read $MOUNT/$tdir/$tfile failed"
13853         umount_client $submount || error "umount $submount failed"
13854         rmdir $submount
13855 }
13856 run_test 247a "mount subdir as fileset"
13857
13858 test_247b() {
13859         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
13860                 { skip "Fileset feature is not supported"; return; }
13861
13862         local submount=${MOUNT}_$tdir
13863
13864         rm -rf $MOUNT/$tdir
13865         mkdir -p $submount || error "mkdir $submount failed"
13866         SKIP_FILESET=1
13867         FILESET="$FILESET/$tdir" mount_client $submount &&
13868                 error "mount $submount should fail"
13869         rmdir $submount
13870 }
13871 run_test 247b "mount subdir that dose not exist"
13872
13873 test_247c() {
13874         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
13875                 { skip "Fileset feature is not supported"; return; }
13876
13877         local submount=${MOUNT}_$tdir
13878
13879         mkdir -p $MOUNT/$tdir/dir1
13880         mkdir -p $submount || error "mkdir $submount failed"
13881         FILESET="$FILESET/$tdir" mount_client $submount ||
13882                 error "mount $submount failed"
13883         local fid=$($LFS path2fid $MOUNT/)
13884         $LFS fid2path $submount $fid && error "fid2path should fail"
13885         umount_client $submount || error "umount $submount failed"
13886         rmdir $submount
13887 }
13888 run_test 247c "running fid2path outside root"
13889
13890 test_247d() {
13891         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
13892                 { skip "Fileset feature is not supported"; return; }
13893
13894         local submount=${MOUNT}_$tdir
13895
13896         mkdir -p $MOUNT/$tdir/dir1
13897         mkdir -p $submount || error "mkdir $submount failed"
13898         FILESET="$FILESET/$tdir" mount_client $submount ||
13899                 error "mount $submount failed"
13900         local fid=$($LFS path2fid $submount/dir1)
13901         $LFS fid2path $submount $fid || error "fid2path should succeed"
13902         umount_client $submount || error "umount $submount failed"
13903         rmdir $submount
13904 }
13905 run_test 247d "running fid2path inside root"
13906
13907 # LU-8037
13908 test_247e() {
13909         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
13910                 grep -q subtree ||
13911                 { skip "Fileset feature is not supported"; return; }
13912
13913         local submount=${MOUNT}_$tdir
13914
13915         mkdir $MOUNT/$tdir
13916         mkdir -p $submount || error "mkdir $submount failed"
13917         FILESET="$FILESET/.." mount_client $submount &&
13918                 error "mount $submount should fail"
13919         rmdir $submount
13920 }
13921 run_test 247e "mount .. as fileset"
13922
13923 test_248() {
13924         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
13925         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
13926
13927         # create a large file for fast read verification
13928         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
13929
13930         # make sure the file is created correctly
13931         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
13932                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
13933
13934         echo "Test 1: verify that fast read is 4 times faster on cache read"
13935
13936         # small read with fast read enabled
13937         $LCTL set_param -n llite.*.fast_read=1
13938         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
13939                 awk '/copied/ { print $6 }')
13940
13941         # small read with fast read disabled
13942         $LCTL set_param -n llite.*.fast_read=0
13943         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
13944                 awk '/copied/ { print $6 }')
13945
13946         # verify that fast read is 4 times faster for cache read
13947         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
13948                 error_not_in_vm "fast read was not 4 times faster: " \
13949                            "$t_fast vs $t_slow"
13950
13951         echo "Test 2: verify the performance between big and small read"
13952         $LCTL set_param -n llite.*.fast_read=1
13953
13954         # 1k non-cache read
13955         cancel_lru_locks osc
13956         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
13957                 awk '/copied/ { print $6 }')
13958
13959         # 1M non-cache read
13960         cancel_lru_locks osc
13961         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
13962                 awk '/copied/ { print $6 }')
13963
13964         # verify that big IO is not 4 times faster than small IO
13965         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
13966                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
13967
13968         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
13969         rm -f $DIR/$tfile
13970 }
13971 run_test 248 "fast read verification"
13972
13973 test_249() { # LU-7890
13974         rm -f $DIR/$tfile
13975         $SETSTRIPE -c 1 $DIR/$tfile
13976
13977         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
13978         skip "Need at least version 2.8.54"
13979
13980         # Offset 2T == 4k * 512M
13981         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
13982                 error "dd to 2T offset failed"
13983 }
13984 run_test 249 "Write above 2T file size"
13985
13986 test_250() {
13987         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
13988          && skip "no 16TB file size limit on ZFS" && return
13989
13990         $SETSTRIPE -c 1 $DIR/$tfile
13991         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
13992         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
13993         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
13994         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
13995                 conv=notrunc,fsync && error "append succeeded"
13996         return 0
13997 }
13998 run_test 250 "Write above 16T limit"
13999
14000 test_251() {
14001         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
14002
14003         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
14004         #Skip once - writing the first stripe will succeed
14005         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14006         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
14007                 error "short write happened"
14008
14009         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14010         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
14011                 error "short read happened"
14012
14013         rm -f $DIR/$tfile
14014 }
14015 run_test 251 "Handling short read and write correctly"
14016
14017 test_252() {
14018         local tgt
14019         local dev
14020         local out
14021         local uuid
14022         local num
14023         local gen
14024
14025         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14026         remote_ost_nodsh && skip "remote OST with nodsh" && return
14027         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
14028              "$(facet_fstype mds1)" != "ldiskfs" ]; then
14029                 skip "can only run lr_reader on ldiskfs target"
14030                 return
14031         fi
14032
14033         # check lr_reader on OST0000
14034         tgt=ost1
14035         dev=$(facet_device $tgt)
14036         out=$(do_facet $tgt $LR_READER $dev)
14037         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14038         echo "$out"
14039         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
14040         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
14041                 error "Invalid uuid returned by $LR_READER on target $tgt"
14042         echo -e "uuid returned by $LR_READER is '$uuid'\n"
14043
14044         # check lr_reader -c on MDT0000
14045         tgt=mds1
14046         dev=$(facet_device $tgt)
14047         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
14048                 echo "$LR_READER does not support additional options"
14049                 return 0
14050         fi
14051         out=$(do_facet $tgt $LR_READER -c $dev)
14052         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14053         echo "$out"
14054         num=$(echo "$out" | grep -c "mdtlov")
14055         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
14056                 error "Invalid number of mdtlov clients returned by $LR_READER"
14057         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
14058
14059         # check lr_reader -cr on MDT0000
14060         out=$(do_facet $tgt $LR_READER -cr $dev)
14061         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14062         echo "$out"
14063         echo "$out" | grep -q "^reply_data:$" ||
14064                 error "$LR_READER should have returned 'reply_data' section"
14065         num=$(echo "$out" | grep -c "client_generation")
14066         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
14067 }
14068 run_test 252 "check lr_reader tool"
14069
14070 test_253_fill_ost() {
14071         local size_mb #how many MB should we write to pass watermark
14072         local lwm=$3  #low watermark
14073         local free_10mb #10% of free space
14074
14075         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14076         size_mb=$((free_kb / 1024 - lwm))
14077         free_10mb=$((free_kb / 10240))
14078         #If 10% of free space cross low watermark use it
14079         if (( free_10mb > size_mb )); then
14080                 size_mb=$free_10mb
14081         else
14082                 #At least we need to store 1.1 of difference between
14083                 #free space and low watermark
14084                 size_mb=$((size_mb + size_mb / 10))
14085         fi
14086         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
14087                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
14088                          oflag=append conv=notrunc
14089         fi
14090
14091         sleep_maxage
14092
14093         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14094         echo "OST still has $((free_kb / 1024)) mbytes free"
14095 }
14096
14097 test_253() {
14098         local ostidx=0
14099         local rc=0
14100
14101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14102         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14103         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
14104
14105         local ost_name=$($LFS osts |
14106                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
14107         # on the mdt's osc
14108         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
14109         do_facet $SINGLEMDS $LCTL get_param -n \
14110                 osp.$mdtosc_proc1.reserved_mb_high ||
14111                 { skip  "remote MDS does not support reserved_mb_high" &&
14112                   return; }
14113
14114         rm -rf $DIR/$tdir
14115         wait_mds_ost_sync
14116         wait_delete_completed
14117         mkdir $DIR/$tdir
14118
14119         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
14120                         osp.$mdtosc_proc1.reserved_mb_high)
14121         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
14122                         osp.$mdtosc_proc1.reserved_mb_low)
14123         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
14124
14125         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME ||
14126                 error "Pool creation failed"
14127         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
14128                 error "Adding $ost_name to pool failed"
14129
14130         # Wait for client to see a OST at pool
14131         wait_update $HOSTNAME "$LCTL get_param -n
14132                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
14133                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
14134                 error "Client can not see the pool"
14135         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
14136                 error "Setstripe failed"
14137
14138         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
14139         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
14140         echo "OST still has $((blocks/1024)) mbytes free"
14141
14142         local new_lwm=$((blocks/1024-10))
14143         do_facet $SINGLEMDS $LCTL set_param \
14144                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
14145         do_facet $SINGLEMDS $LCTL set_param \
14146                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
14147
14148         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14149
14150         #First enospc could execute orphan deletion so repeat.
14151         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14152
14153         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14154                         osp.$mdtosc_proc1.prealloc_status)
14155         echo "prealloc_status $oa_status"
14156
14157         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
14158                 error "File creation should fail"
14159         #object allocation was stopped, but we still able to append files
14160         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
14161                 error "Append failed"
14162         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
14163
14164         wait_delete_completed
14165
14166         sleep_maxage
14167
14168         for i in $(seq 10 12); do
14169                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
14170                         error "File creation failed after rm";
14171         done
14172
14173         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14174                         osp.$mdtosc_proc1.prealloc_status)
14175         echo "prealloc_status $oa_status"
14176
14177         if (( oa_status != 0 )); then
14178                 error "Object allocation still disable after rm"
14179         fi
14180         do_facet $SINGLEMDS $LCTL set_param \
14181                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
14182         do_facet $SINGLEMDS $LCTL set_param \
14183                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
14184
14185
14186         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
14187                 error "Remove $ost_name from pool failed"
14188         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14189                 error "Pool destroy fialed"
14190 }
14191 run_test 253 "Check object allocation limit"
14192
14193 test_254() {
14194         local cl_user
14195
14196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14198         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
14199                 { skip "MDS does not support changelog_size" && return; }
14200
14201         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14202         echo "Registered as changelog user $cl_user"
14203
14204         $LFS changelog_clear $MDT0 $cl_user 0
14205
14206         local size1=$(do_facet mds1 \
14207                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14208         echo "Changelog size $size1"
14209
14210         rm -rf $DIR/$tdir
14211         $LFS mkdir -i 0 $DIR/$tdir
14212         # change something
14213         mkdir -p $DIR/$tdir/pics/2008/zachy
14214         touch $DIR/$tdir/pics/2008/zachy/timestamp
14215         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
14216         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14217         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14218         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14219         rm $DIR/$tdir/pics/desktop.jpg
14220
14221         local size2=$(do_facet mds1 \
14222                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14223         echo "Changelog size after work $size2"
14224
14225         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14226
14227         if (( size2 <= size1 )); then
14228                 error "Changelog size after work should be greater than original"
14229         fi
14230         return 0
14231 }
14232 run_test 254 "Check changelog size"
14233
14234 ladvise_no_type()
14235 {
14236         local type=$1
14237         local file=$2
14238
14239         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
14240                 awk -F: '{print $2}' | grep $type > /dev/null
14241         if [ $? -ne 0 ]; then
14242                 return 0
14243         fi
14244         return 1
14245 }
14246
14247 ladvise_no_ioctl()
14248 {
14249         local file=$1
14250
14251         lfs ladvise -a willread $file > /dev/null 2>&1
14252         if [ $? -eq 0 ]; then
14253                 return 1
14254         fi
14255
14256         lfs ladvise -a willread $file 2>&1 |
14257                 grep "Inappropriate ioctl for device" > /dev/null
14258         if [ $? -eq 0 ]; then
14259                 return 0
14260         fi
14261         return 1
14262 }
14263
14264 ladvise_willread_performance()
14265 {
14266         local repeat=10
14267         local average_cache=0
14268         local average_ladvise=0
14269
14270         for ((i = 1; i <= $repeat; i++)); do
14271                 echo "Iter $i/$repeat: reading without willread hint"
14272                 cancel_lru_locks osc
14273                 do_nodes $(comma_list $(osts_nodes)) \
14274                         "echo 3 > /proc/sys/vm/drop_caches"
14275                 local speed_origin=$($READS -f $DIR/$tfile -s $size \
14276                         -b 4096 -n $((size / 4096)) -t 60 |
14277                         sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##')
14278
14279                 echo "Iter $i/$repeat: Reading again without willread hint"
14280                 cancel_lru_locks osc
14281                 local speed_cache=$($READS -f $DIR/$tfile -s $size \
14282                         -b 4096 -n $((size / 4096)) -t 60 |
14283                         sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##')
14284
14285                 echo "Iter $i/$repeat: reading with willread hint"
14286                 cancel_lru_locks osc
14287                 do_nodes $(comma_list $(osts_nodes)) \
14288                         "echo 3 > /proc/sys/vm/drop_caches"
14289                 lfs ladvise -a willread $DIR/$tfile ||
14290                         error "Ladvise failed"
14291                 local speed_ladvise=$($READS -f $DIR/$tfile -s $size \
14292                         -b 4096 -n $((size / 4096)) -t 60 |
14293                         sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##')
14294
14295                 local cache_speedup=$(echo "scale=2; \
14296                         ($speed_cache-$speed_origin)/$speed_origin*100" | bc)
14297                 cache_speedup=$(echo ${cache_speedup%.*})
14298                 echo "Iter $i/$repeat: cache speedup: $cache_speedup%"
14299                 average_cache=$((average_cache + cache_speedup))
14300
14301                 local ladvise_speedup=$(echo "scale=2; \
14302                         ($speed_ladvise-$speed_origin)/$speed_origin*100" | bc)
14303                 ladvise_speedup=$(echo ${ladvise_speedup%.*})
14304                 echo "Iter $i/$repeat: ladvise speedup: $ladvise_speedup%"
14305                 average_ladvise=$((average_ladvise + ladvise_speedup))
14306         done
14307         average_cache=$((average_cache / repeat))
14308         average_ladvise=$((average_ladvise / repeat))
14309
14310         if [ $average_cache -lt 20 ]; then
14311                 echo "Speedup with cache is less than 20% ($average_cache%),"\
14312                         "skipping check of speedup with willread:"\
14313                         "$average_ladvise%"
14314                 return 0
14315         fi
14316
14317         local lowest_speedup=$((average_cache / 2))
14318         [ $average_ladvise -gt $lowest_speedup ] ||
14319                 error_not_in_vm "Speedup with willread is less than " \
14320                            "$lowest_speedup%, got $average_ladvise%"
14321         echo "Speedup with willread ladvise: $average_ladvise%"
14322         echo "Speedup with cache: $average_cache%"
14323 }
14324
14325 test_255a() {
14326         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
14327
14328         ladvise_no_type willread $DIR/$tfile &&
14329                 skip "willread ladvise is not supported" && return
14330
14331         ladvise_no_ioctl $DIR/$tfile &&
14332                 skip "ladvise ioctl is not supported" && return
14333
14334         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14335                 skip "lustre < 2.8.54 does not support ladvise " && return
14336
14337         local size_mb=100
14338         local size=$((size_mb * 1048576))
14339         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14340                 error "dd to $DIR/$tfile failed"
14341
14342         lfs ladvise -a willread $DIR/$tfile ||
14343                 error "Ladvise failed with no range argument"
14344
14345         lfs ladvise -a willread -s 0 $DIR/$tfile ||
14346                 error "Ladvise failed with no -l or -e argument"
14347
14348         lfs ladvise -a willread -e 1 $DIR/$tfile ||
14349                 error "Ladvise failed with only -e argument"
14350
14351         lfs ladvise -a willread -l 1 $DIR/$tfile ||
14352                 error "Ladvise failed with only -l argument"
14353
14354         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
14355                 error "End offset should not be smaller than start offset"
14356
14357         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
14358                 error "End offset should not be equal to start offset"
14359
14360         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
14361                 error "Ladvise failed with overflowing -s argument"
14362
14363         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
14364                 error "Ladvise failed with overflowing -e argument"
14365
14366         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
14367                 error "Ladvise failed with overflowing -l argument"
14368
14369         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
14370                 error "Ladvise succeeded with conflicting -l and -e arguments"
14371
14372         echo "Synchronous ladvise should wait"
14373         local delay=4
14374 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
14375         do_nodes $(comma_list $(osts_nodes)) \
14376                 $LCTL set_param fail_val=$delay fail_loc=0x237
14377
14378         local start_ts=$SECONDS
14379         lfs ladvise -a willread $DIR/$tfile ||
14380                 error "Ladvise failed with no range argument"
14381         local end_ts=$SECONDS
14382         local inteval_ts=$((end_ts - start_ts))
14383
14384         if [ $inteval_ts -lt $(($delay - 1)) ]; then
14385                 error "Synchronous advice didn't wait reply"
14386         fi
14387
14388         echo "Asynchronous ladvise shouldn't wait"
14389         local start_ts=$SECONDS
14390         lfs ladvise -a willread -b $DIR/$tfile ||
14391                 error "Ladvise failed with no range argument"
14392         local end_ts=$SECONDS
14393         local inteval_ts=$((end_ts - start_ts))
14394
14395         if [ $inteval_ts -gt $(($delay / 2)) ]; then
14396                 error "Asynchronous advice blocked"
14397         fi
14398
14399         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
14400         ladvise_willread_performance
14401 }
14402 run_test 255a "check 'lfs ladvise -a willread'"
14403
14404 facet_meminfo() {
14405         local facet=$1
14406         local info=$2
14407
14408         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
14409 }
14410
14411 test_255b() {
14412         ladvise_no_type dontneed $DIR/$tfile &&
14413                 skip "dontneed ladvise is not supported" && return
14414
14415         ladvise_no_ioctl $DIR/$tfile &&
14416                 skip "ladvise ioctl is not supported" && return
14417
14418         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14419                 skip "lustre < 2.8.54 does not support ladvise" && return
14420
14421         [ "$(facet_fstype ost1)" = "zfs" ] &&
14422                 skip "zfs-osd does not support dontneed advice" && return
14423
14424         lfs setstripe -c 1 -i 0 $DIR/$tfile
14425
14426         local size_mb=100
14427         local size=$((size_mb * 1048576))
14428         # In order to prevent disturbance of other processes, only check 3/4
14429         # of the memory usage
14430         local kibibytes=$((size_mb * 1024 * 3 / 4))
14431
14432         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14433                 error "dd to $DIR/$tfile failed"
14434
14435         local total=$(facet_meminfo ost1 MemTotal)
14436         echo "Total memory: $total KiB"
14437
14438         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
14439         local before_read=$(facet_meminfo ost1 Cached)
14440         echo "Cache used before read: $before_read KiB"
14441
14442         lfs ladvise -a willread $DIR/$tfile ||
14443                 error "Ladvise willread failed"
14444         local after_read=$(facet_meminfo ost1 Cached)
14445         echo "Cache used after read: $after_read KiB"
14446
14447         lfs ladvise -a dontneed $DIR/$tfile ||
14448                 error "Ladvise dontneed again failed"
14449         local no_read=$(facet_meminfo ost1 Cached)
14450         echo "Cache used after dontneed ladvise: $no_read KiB"
14451
14452         if [ $total -lt $((before_read + kibibytes)) ]; then
14453                 echo "Memory is too small, abort checking"
14454                 return 0
14455         fi
14456
14457         if [ $((before_read + kibibytes)) -gt $after_read ]; then
14458                 error "Ladvise willread should use more memory" \
14459                         "than $kibibytes KiB"
14460         fi
14461
14462         if [ $((no_read + kibibytes)) -gt $after_read ]; then
14463                 error "Ladvise dontneed should release more memory" \
14464                         "than $kibibytes KiB"
14465         fi
14466 }
14467 run_test 255b "check 'lfs ladvise -a dontneed'"
14468
14469 test_256() {
14470         local cl_user
14471         local cat_sl
14472         local mdt_dev
14473
14474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14475         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14476         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
14477                 skip "non-ldiskfs backend" && return
14478
14479         mdt_dev=$(mdsdevname 1)
14480         echo $mdt_dev
14481         cl_user=$(do_facet mds1 \
14482         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
14483         if [[ -n $cl_user ]]; then
14484                 skip "active changelog user"
14485                 return
14486         fi
14487
14488         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14489         echo "Registered as changelog user $cl_user"
14490
14491         rm -rf $DIR/$tdir
14492         mkdir -p $DIR/$tdir
14493
14494         $LFS changelog_clear $MDT0 $cl_user 0
14495
14496         # change something
14497         touch $DIR/$tdir/{1..10}
14498
14499         # stop the MDT
14500         stop mds1 || error "Fail to stop MDT."
14501
14502         # remount the MDT
14503         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
14504
14505         #after mount new plainllog is used
14506         touch $DIR/$tdir/{11..19}
14507         cat_sl=$(do_facet mds1 \
14508         "$DEBUGFS -R \\\"dump changelog_catalog cat.dmp\\\" $mdt_dev; \
14509          llog_reader cat.dmp | grep \\\"type=1064553b\\\" | wc -l")
14510
14511         if (( cat_sl != 2 )); then
14512                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14513                 error "Changelog catalog has wrong number of slots $cat_sl"
14514         fi
14515
14516         $LFS changelog_clear $MDT0 $cl_user 0
14517
14518         cat_sl=$(do_facet mds1 \
14519         "$DEBUGFS -R \\\"dump changelog_catalog cat.dmp\\\" $mdt_dev; \
14520          llog_reader cat.dmp | grep \\\"type=1064553b\\\" | wc -l")
14521
14522         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14523
14524         if (( cat_sl == 2 )); then
14525                 error "Empty plain llog was not deleted from changelog catalog"
14526         fi
14527         if (( cat_sl != 1 )); then
14528                 error "Active plain llog shouldn\`t be deleted from catalog"
14529         fi
14530 }
14531 run_test 256 "Check llog delete for empty and not full state"
14532
14533 test_257() {
14534         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14535         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
14536                 skip "Need MDS version at least 2.8.55" && return
14537
14538         test_mkdir -p $DIR/$tdir
14539
14540         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
14541                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
14542         stat $DIR/$tdir
14543
14544 #define OBD_FAIL_MDS_XATTR_REP                  0x161
14545         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
14546         local facet=mds$((mdtidx + 1))
14547         set_nodes_failloc $(facet_active_host $facet) 0x80000161
14548         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
14549
14550         stop $facet || error "stop MDS failed"
14551         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
14552                 error "start MDS fail"
14553 }
14554 run_test 257 "xattr locks are not lost"
14555
14556 test_260() {
14557 #define OBD_FAIL_MDC_CLOSE               0x806
14558         $LCTL set_param fail_loc=0x80000806
14559         touch $DIR/$tfile
14560
14561 }
14562 run_test 260 "Check mdc_close fail"
14563
14564 cleanup_test_300() {
14565         trap 0
14566         umask $SAVE_UMASK
14567 }
14568 test_striped_dir() {
14569         local mdt_index=$1
14570         local stripe_count
14571         local stripe_index
14572
14573         mkdir -p $DIR/$tdir
14574
14575         SAVE_UMASK=$(umask)
14576         trap cleanup_test_300 RETURN EXIT
14577
14578         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
14579                                                 $DIR/$tdir/striped_dir ||
14580                 error "set striped dir error"
14581
14582         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
14583         [ "$mode" = "755" ] || error "expect 755 got $mode"
14584
14585         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
14586                 error "getdirstripe failed"
14587         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
14588         if [ "$stripe_count" != "2" ]; then
14589                 error "stripe_count is $stripe_count, expect 2"
14590         fi
14591
14592         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
14593         if [ "$stripe_index" != "$mdt_index" ]; then
14594                 error "stripe_index is $stripe_index, expect $mdt_index"
14595         fi
14596
14597         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
14598                 error "nlink error after create striped dir"
14599
14600         mkdir $DIR/$tdir/striped_dir/a
14601         mkdir $DIR/$tdir/striped_dir/b
14602
14603         stat $DIR/$tdir/striped_dir/a ||
14604                 error "create dir under striped dir failed"
14605         stat $DIR/$tdir/striped_dir/b ||
14606                 error "create dir under striped dir failed"
14607
14608         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
14609                 error "nlink error after mkdir"
14610
14611         rmdir $DIR/$tdir/striped_dir/a
14612         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
14613                 error "nlink error after rmdir"
14614
14615         rmdir $DIR/$tdir/striped_dir/b
14616         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
14617                 error "nlink error after rmdir"
14618
14619         chattr +i $DIR/$tdir/striped_dir
14620         createmany -o $DIR/$tdir/striped_dir/f 10 &&
14621                 error "immutable flags not working under striped dir!"
14622         chattr -i $DIR/$tdir/striped_dir
14623
14624         rmdir $DIR/$tdir/striped_dir ||
14625                 error "rmdir striped dir error"
14626
14627         cleanup_test_300
14628
14629         true
14630 }
14631
14632 test_300a() {
14633         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
14634                 skip "skipped for lustre < 2.7.0" && return
14635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14636         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14637
14638         test_striped_dir 0 || error "failed on striped dir on MDT0"
14639         test_striped_dir 1 || error "failed on striped dir on MDT0"
14640 }
14641 run_test 300a "basic striped dir sanity test"
14642
14643 test_300b() {
14644         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
14645                 skip "skipped for lustre < 2.7.0" && return
14646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14647         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14648         local i
14649         local mtime1
14650         local mtime2
14651         local mtime3
14652
14653         test_mkdir $DIR/$tdir || error "mkdir fail"
14654         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
14655                 error "set striped dir error"
14656         for ((i=0; i<10; i++)); do
14657                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
14658                 sleep 1
14659                 touch $DIR/$tdir/striped_dir/file_$i ||
14660                                         error "touch error $i"
14661                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
14662                 [ $mtime1 -eq $mtime2 ] &&
14663                         error "mtime not change after create"
14664                 sleep 1
14665                 rm -f $DIR/$tdir/striped_dir/file_$i ||
14666                                         error "unlink error $i"
14667                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
14668                 [ $mtime2 -eq $mtime3 ] &&
14669                         error "mtime did not change after unlink"
14670         done
14671         true
14672 }
14673 run_test 300b "check ctime/mtime for striped dir"
14674
14675 test_300c() {
14676         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
14677                 skip "skipped for lustre < 2.7.0" && return
14678         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14679         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14680         local file_count
14681
14682         mkdir -p $DIR/$tdir
14683         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
14684                 error "set striped dir error"
14685
14686         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
14687                 error "chown striped dir failed"
14688
14689         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
14690                 error "create 5k files failed"
14691
14692         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
14693
14694         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
14695
14696         rm -rf $DIR/$tdir
14697 }
14698 run_test 300c "chown && check ls under striped directory"
14699
14700 test_300d() {
14701         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
14702                 skip "skipped for lustre < 2.7.0" && return
14703         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14704         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14705         local stripe_count
14706         local file
14707
14708         mkdir -p $DIR/$tdir
14709         $SETSTRIPE -c 2 $DIR/$tdir
14710
14711         #local striped directory
14712         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
14713                 error "set striped dir error"
14714         createmany -o $DIR/$tdir/striped_dir/f 10 ||
14715                 error "create 10 files failed"
14716
14717         #remote striped directory
14718         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
14719                 error "set striped dir error"
14720         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
14721                 error "create 10 files failed"
14722
14723         for file in $(find $DIR/$tdir); do
14724                 stripe_count=$($GETSTRIPE -c $file)
14725                 [ $stripe_count -eq 2 ] ||
14726                         error "wrong stripe $stripe_count for $file"
14727         done
14728
14729         rm -rf $DIR/$tdir
14730 }
14731 run_test 300d "check default stripe under striped directory"
14732
14733 test_300e() {
14734         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14735                 skip "Need MDS version at least 2.7.55" && return
14736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14737         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14738         local stripe_count
14739         local file
14740
14741         mkdir -p $DIR/$tdir
14742
14743         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
14744                 error "set striped dir error"
14745
14746         touch $DIR/$tdir/striped_dir/a
14747         touch $DIR/$tdir/striped_dir/b
14748         touch $DIR/$tdir/striped_dir/c
14749
14750         mkdir $DIR/$tdir/striped_dir/dir_a
14751         mkdir $DIR/$tdir/striped_dir/dir_b
14752         mkdir $DIR/$tdir/striped_dir/dir_c
14753
14754         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
14755                 error "set striped dir under striped dir error"
14756
14757         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
14758                 error "set striped dir under striped dir error"
14759
14760         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
14761                 error "set striped dir under striped dir error"
14762
14763         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
14764                 error "rename dir under striped dir fails"
14765
14766         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
14767                 error "rename dir under different stripes fails"
14768
14769         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
14770                 error "rename file under striped dir should succeed"
14771
14772         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
14773                 error "rename dir under striped dir should succeed"
14774
14775         rm -rf $DIR/$tdir
14776 }
14777 run_test 300e "check rename under striped directory"
14778
14779 test_300f() {
14780         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14781                 skip "Need MDS version at least 2.7.55" && return
14782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14783         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14784         local stripe_count
14785         local file
14786
14787         rm -rf $DIR/$tdir
14788         mkdir -p $DIR/$tdir
14789
14790         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
14791                 error "set striped dir error"
14792
14793         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
14794                 error "set striped dir error"
14795
14796         touch $DIR/$tdir/striped_dir/a
14797         mkdir $DIR/$tdir/striped_dir/dir_a
14798         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
14799                 error "create striped dir under striped dir fails"
14800
14801         touch $DIR/$tdir/striped_dir1/b
14802         mkdir $DIR/$tdir/striped_dir1/dir_b
14803         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
14804                 error "create striped dir under striped dir fails"
14805
14806         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
14807                 error "rename dir under different striped dir should fail"
14808
14809         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
14810                 error "rename striped dir under diff striped dir should fail"
14811
14812         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
14813                 error "rename file under diff striped dirs fails"
14814
14815         rm -rf $DIR/$tdir
14816 }
14817 run_test 300f "check rename cross striped directory"
14818
14819 test_300_check_default_striped_dir()
14820 {
14821         local dirname=$1
14822         local default_count=$2
14823         local default_index=$3
14824         local stripe_count
14825         local stripe_index
14826         local dir_stripe_index
14827         local dir
14828
14829         echo "checking $dirname $default_count $default_index"
14830         $LFS setdirstripe -D -c $default_count -i $default_index \
14831                                 -t all_char $DIR/$tdir/$dirname ||
14832                 error "set default stripe on striped dir error"
14833         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
14834         [ $stripe_count -eq $default_count ] ||
14835                 error "expect $default_count get $stripe_count for $dirname"
14836
14837         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
14838         [ $stripe_index -eq $default_index ] ||
14839                 error "expect $default_index get $stripe_index for $dirname"
14840
14841         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
14842                                                 error "create dirs failed"
14843
14844         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
14845         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
14846         for dir in $(find $DIR/$tdir/$dirname/*); do
14847                 stripe_count=$($LFS getdirstripe -c $dir)
14848                 [ $stripe_count -eq $default_count ] ||
14849                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
14850                 error "stripe count $default_count != $stripe_count for $dir"
14851
14852                 stripe_index=$($LFS getdirstripe -i $dir)
14853                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
14854                         error "$stripe_index != $default_index for $dir"
14855
14856                 #check default stripe
14857                 stripe_count=$($LFS getdirstripe -D -c $dir)
14858                 [ $stripe_count -eq $default_count ] ||
14859                 error "default count $default_count != $stripe_count for $dir"
14860
14861                 stripe_index=$($LFS getdirstripe -D -i $dir)
14862                 [ $stripe_index -eq $default_index ] ||
14863                 error "default index $default_index != $stripe_index for $dir"
14864         done
14865         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
14866 }
14867
14868 test_300g() {
14869         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14870                 skip "Need MDS version at least 2.7.55" && return
14871         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14872         local dir
14873         local stripe_count
14874         local stripe_index
14875
14876         mkdir $DIR/$tdir
14877         mkdir $DIR/$tdir/normal_dir
14878
14879         #Checking when client cache stripe index
14880         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
14881         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
14882                 error "create striped_dir failed"
14883
14884         mkdir $DIR/$tdir/striped_dir/dir1 ||
14885                 error "create dir1 fails"
14886         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
14887         [ $stripe_index -eq 1 ] ||
14888                 error "dir1 expect 1 got $stripe_index"
14889
14890         $LFS setdirstripe -i2 $DIR/$tdir/striped_dir/dir2 ||
14891                 error "create dir2 fails"
14892         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir2)
14893         [ $stripe_index -eq 2 ] ||
14894                 error "dir2 expect 2 got $stripe_index"
14895
14896         #check default stripe count/stripe index
14897         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
14898         test_300_check_default_striped_dir normal_dir 1 0
14899         test_300_check_default_striped_dir normal_dir 2 1
14900         test_300_check_default_striped_dir normal_dir 2 -1
14901
14902         #delete default stripe information
14903         echo "delete default stripeEA"
14904         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
14905                 error "set default stripe on striped dir error"
14906
14907         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
14908         for dir in $(find $DIR/$tdir/normal_dir/*); do
14909                 stripe_count=$($LFS getdirstripe -c $dir)
14910                 [ $stripe_count -eq 0 ] ||
14911                         error "expect 1 get $stripe_count for $dir"
14912                 stripe_index=$($LFS getdirstripe -i $dir)
14913                 [ $stripe_index -eq 0 ] ||
14914                         error "expect 0 get $stripe_index for $dir"
14915         done
14916 }
14917 run_test 300g "check default striped directory for normal directory"
14918
14919 test_300h() {
14920         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14921                 skip "Need MDS version at least 2.7.55" && return
14922         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14923         local dir
14924         local stripe_count
14925
14926         mkdir $DIR/$tdir
14927         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
14928                                         $DIR/$tdir/striped_dir ||
14929                 error "set striped dir error"
14930
14931         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
14932         test_300_check_default_striped_dir striped_dir 1 0
14933         test_300_check_default_striped_dir striped_dir 2 1
14934         test_300_check_default_striped_dir striped_dir 2 -1
14935
14936         #delete default stripe information
14937         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
14938                 error "set default stripe on striped dir error"
14939
14940         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
14941         for dir in $(find $DIR/$tdir/striped_dir/*); do
14942                 stripe_count=$($LFS getdirstripe -c $dir)
14943                 [ $stripe_count -eq 0 ] ||
14944                         error "expect 1 get $stripe_count for $dir"
14945         done
14946 }
14947 run_test 300h "check default striped directory for striped directory"
14948
14949 test_300i() {
14950         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14951                 skip "Need MDS version at least 2.7.55" && return
14952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14953         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14954         local stripe_count
14955         local file
14956
14957         mkdir $DIR/$tdir
14958
14959         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
14960                 error "set striped dir error"
14961
14962         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
14963                 error "create files under striped dir failed"
14964
14965         # unfortunately, we need to umount to clear dir layout cache for now
14966         # once we fully implement dir layout, we can drop this
14967         umount_client $MOUNT || error "umount failed"
14968         mount_client $MOUNT || error "mount failed"
14969
14970         #set the stripe to be unknown hash type
14971         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
14972         $LCTL set_param fail_loc=0x1901
14973         for ((i = 0; i < 10; i++)); do
14974                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
14975                         error "stat f-$i failed"
14976                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
14977         done
14978
14979         touch $DIR/$tdir/striped_dir/f0 &&
14980                 error "create under striped dir with unknown hash should fail"
14981
14982         $LCTL set_param fail_loc=0
14983
14984         umount_client $MOUNT || error "umount failed"
14985         mount_client $MOUNT || error "mount failed"
14986
14987         return 0
14988 }
14989 run_test 300i "client handle unknown hash type striped directory"
14990
14991 test_300j() {
14992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14993         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14994                 skip "Need MDS version at least 2.7.55" && return
14995         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14996         local stripe_count
14997         local file
14998
14999         mkdir $DIR/$tdir
15000
15001         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
15002         $LCTL set_param fail_loc=0x1702
15003         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15004                 error "set striped dir error"
15005
15006         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15007                 error "create files under striped dir failed"
15008
15009         $LCTL set_param fail_loc=0
15010
15011         rm -rf $DIR/$tdir || error "unlink striped dir fails"
15012
15013         return 0
15014 }
15015 run_test 300j "test large update record"
15016
15017 test_300k() {
15018         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15019                 skip "Need MDS version at least 2.7.55" && return
15020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15021         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15022         local stripe_count
15023         local file
15024
15025         mkdir $DIR/$tdir
15026
15027         #define OBD_FAIL_LARGE_STRIPE   0x1703
15028         $LCTL set_param fail_loc=0x1703
15029         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
15030                 error "set striped dir error"
15031         $LCTL set_param fail_loc=0
15032
15033         $LFS getdirstripe $DIR/$tdir/striped_dir ||
15034                 error "getstripeddir fails"
15035         rm -rf $DIR/$tdir/striped_dir ||
15036                 error "unlink striped dir fails"
15037
15038         return 0
15039 }
15040 run_test 300k "test large striped directory"
15041
15042 test_300l() {
15043         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15044                 skip "Need MDS version at least 2.7.55" && return
15045         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15046         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15047         local stripe_index
15048
15049         test_mkdir -p $DIR/$tdir/striped_dir
15050         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
15051                         error "chown $RUNAS_ID failed"
15052         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
15053                 error "set default striped dir failed"
15054
15055         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
15056         $LCTL set_param fail_loc=0x80000158
15057         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
15058
15059         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
15060         [ $stripe_index -eq 1 ] ||
15061                 error "expect 1 get $stripe_index for $dir"
15062 }
15063 run_test 300l "non-root user to create dir under striped dir with stale layout"
15064
15065 test_300m() {
15066         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15067                 skip "Need MDS version at least 2.7.55" && return
15068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15069         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
15070
15071         mkdir -p $DIR/$tdir/striped_dir
15072         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
15073                 error "set default stripes dir error"
15074
15075         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
15076
15077         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
15078         [ $stripe_count -eq 0 ] ||
15079                         error "expect 0 get $stripe_count for a"
15080
15081         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
15082                 error "set default stripes dir error"
15083
15084         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
15085
15086         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
15087         [ $stripe_count -eq 0 ] ||
15088                         error "expect 0 get $stripe_count for b"
15089
15090         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
15091                 error "set default stripes dir error"
15092
15093         mkdir $DIR/$tdir/striped_dir/c &&
15094                 error "default stripe_index is invalid, mkdir c should fails"
15095
15096         rm -rf $DIR/$tdir || error "rmdir fails"
15097 }
15098 run_test 300m "setstriped directory on single MDT FS"
15099
15100 cleanup_300n() {
15101         local list=$(comma_list $(mdts_nodes))
15102
15103         trap 0
15104         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15105 }
15106
15107 test_300n() {
15108         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15109                 skip "Need MDS version at least 2.7.55" && return
15110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15111         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15112         local stripe_index
15113         local list=$(comma_list $(mdts_nodes))
15114
15115         trap cleanup_300n RETURN EXIT
15116         mkdir -p $DIR/$tdir
15117         chmod 777 $DIR/$tdir
15118         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
15119                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15120                 error "create striped dir succeeds with gid=0"
15121
15122         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15123         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
15124                 error "create striped dir fails with gid=-1"
15125
15126         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15127         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
15128                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15129                 error "set default striped dir succeeds with gid=0"
15130
15131
15132         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15133         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
15134                 error "set default striped dir fails with gid=-1"
15135
15136
15137         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15138         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
15139                                         error "create test_dir fails"
15140         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
15141                                         error "create test_dir1 fails"
15142         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
15143                                         error "create test_dir2 fails"
15144         cleanup_300n
15145 }
15146 run_test 300n "non-root user to create dir under striped dir with default EA"
15147
15148 test_300o() {
15149         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15150                 skip "Need MDS version at least 2.7.55" && return
15151         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15152         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15153         local numfree1
15154         local numfree2
15155
15156         mkdir -p $DIR/$tdir
15157
15158         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
15159         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
15160         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
15161                 skip "not enough free inodes $numfree1 $numfree2"
15162                 return
15163         fi
15164
15165         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
15166         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
15167         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
15168                 skip "not enough free space $numfree1 $numfree2"
15169                 return
15170         fi
15171
15172         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
15173                 error "setdirstripe fails"
15174
15175         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
15176                 error "create dirs fails"
15177
15178         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
15179         ls $DIR/$tdir/striped_dir > /dev/null ||
15180                 error "ls striped dir fails"
15181         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
15182                 error "unlink big striped dir fails"
15183 }
15184 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
15185
15186 test_300p() {
15187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15188         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15189         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15190
15191         mkdir -p $DIR/$tdir
15192
15193         #define OBD_FAIL_OUT_ENOSPC     0x1704
15194         do_facet mds2 lctl set_param fail_loc=0x80001704
15195         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
15196                         error "create striped directory should fail"
15197
15198         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
15199
15200         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
15201         true
15202 }
15203 run_test 300p "create striped directory without space"
15204
15205 test_300q() {
15206         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15207         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15208
15209         local fd=$(free_fd)
15210         local cmd="exec $fd<$tdir"
15211         cd $DIR
15212         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
15213         eval $cmd
15214         cmd="exec $fd<&-"
15215         trap "eval $cmd" EXIT
15216         cd $tdir || error "cd $tdir fails"
15217         rmdir  ../$tdir || error "rmdir $tdir fails"
15218         mkdir local_dir && error "create dir succeeds"
15219         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
15220         eval $cmd
15221         return 0
15222 }
15223 run_test 300q "create remote directory under orphan directory"
15224
15225 prepare_remote_file() {
15226         mkdir $DIR/$tdir/src_dir ||
15227                 error "create remote source failed"
15228
15229         cp /etc/hosts $DIR/$tdir/src_dir/a || error
15230         touch $DIR/$tdir/src_dir/a
15231
15232         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
15233                 error "create remote target dir failed"
15234
15235         touch $DIR/$tdir/tgt_dir/b
15236
15237         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
15238                 error "rename dir cross MDT failed!"
15239
15240         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
15241                 error "src_child still exists after rename"
15242
15243         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
15244                 error "missing file(a) after rename"
15245
15246         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
15247                 error "diff after rename"
15248 }
15249
15250 test_310a() {
15251         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
15252         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15253         local remote_file=$DIR/$tdir/tgt_dir/b
15254
15255         mkdir -p $DIR/$tdir
15256
15257         prepare_remote_file || error "prepare remote file failed"
15258
15259         #open-unlink file
15260         $OPENUNLINK $remote_file $remote_file || error
15261         $CHECKSTAT -a $remote_file || error
15262 }
15263 run_test 310a "open unlink remote file"
15264
15265 test_310b() {
15266         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
15267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15268         local remote_file=$DIR/$tdir/tgt_dir/b
15269
15270         mkdir -p $DIR/$tdir
15271
15272         prepare_remote_file || error "prepare remote file failed"
15273
15274         ln $remote_file $DIR/$tfile || error "link failed for remote file"
15275         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
15276         $CHECKSTAT -t file $remote_file || error "check file failed"
15277 }
15278 run_test 310b "unlink remote file with multiple links while open"
15279
15280 test_310c() {
15281         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
15282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15283         local remote_file=$DIR/$tdir/tgt_dir/b
15284
15285         mkdir -p $DIR/$tdir
15286
15287         prepare_remote_file || error "prepare remote file failed"
15288
15289         ln $remote_file $DIR/$tfile || error "link failed for remote file"
15290         multiop_bg_pause $remote_file O_uc ||
15291                         error "mulitop failed for remote file"
15292         MULTIPID=$!
15293         $MULTIOP $DIR/$tfile Ouc
15294         kill -USR1 $MULTIPID
15295         wait $MULTIPID
15296 }
15297 run_test 310c "open-unlink remote file with multiple links"
15298
15299 #LU-4825
15300 test_311() {
15301         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
15302                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
15303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15304
15305         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
15306
15307         mkdir -p $DIR/$tdir
15308         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
15309         createmany -o $DIR/$tdir/$tfile. 1000
15310
15311         # statfs data is not real time, let's just calculate it
15312         old_iused=$((old_iused + 1000))
15313
15314         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
15315                         osp.*OST0000*MDT0000.create_count")
15316         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
15317                                 osp.*OST0000*MDT0000.max_create_count")
15318         for idx in $(seq $MDSCOUNT); do
15319                 do_facet mds$idx "lctl set_param -n \
15320                         osp.*OST0000*MDT000?.max_create_count=0"
15321         done
15322
15323         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
15324         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
15325         [ $index -ne 0 ] || error "$tfile stripe index is 0"
15326
15327         unlinkmany $DIR/$tdir/$tfile. 1000
15328
15329         for idx in $(seq $MDSCOUNT); do
15330                 do_facet mds$idx "lctl set_param -n \
15331                         osp.*OST0000*MDT000?.max_create_count=$max_count"
15332                 do_facet mds$idx "lctl set_param -n \
15333                         osp.*OST0000*MDT000?.create_count=$count"
15334         done
15335
15336         local new_iused
15337         for i in $(seq 120); do
15338                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
15339                 # system may be too busy to destroy all objs in time, use
15340                 # a somewhat small value to not fail autotest
15341                 [ $((old_iused - new_iused)) -gt 400 ] && break
15342                 sleep 1
15343         done
15344
15345         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
15346         [ $((old_iused - new_iused)) -gt 400 ] ||
15347                 error "objs not destroyed after unlink"
15348 }
15349 run_test 311 "disable OSP precreate, and unlink should destroy objs"
15350
15351 zfs_oid_to_objid()
15352 {
15353         local ost=$1
15354         local objid=$2
15355
15356         local vdevdir=$(dirname $(facet_vdevice $ost))
15357         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
15358         local zfs_zapid=$(do_facet $ost $cmd |
15359                           grep -w "/O/0/d$((objid%32))" -C 5 |
15360                           awk '/Object/{getline; print $1}')
15361         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
15362                           awk "/$objid = /"'{printf $3}')
15363
15364         echo $zfs_objid
15365 }
15366
15367 zfs_object_blksz() {
15368         local ost=$1
15369         local objid=$2
15370
15371         local vdevdir=$(dirname $(facet_vdevice $ost))
15372         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
15373         local blksz=$(do_facet $ost $cmd $objid |
15374                       awk '/dblk/{getline; printf $4}')
15375
15376         case "${blksz: -1}" in
15377                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
15378                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
15379                 *) ;;
15380         esac
15381
15382         echo $blksz
15383 }
15384
15385 test_312() { # LU-4856
15386         [ $(facet_fstype ost1) = "zfs" ] ||
15387                 { skip "the test only applies to zfs" && return; }
15388
15389         local max_blksz=$(do_facet ost1 \
15390                           $ZFS get -p recordsize $(facet_device ost1) |
15391                           awk '!/VALUE/{print $3}')
15392
15393         # to make life a little bit easier
15394         $LFS mkdir -c 1 -i 0 $DIR/$tdir
15395         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15396
15397         local tf=$DIR/$tdir/$tfile
15398         touch $tf
15399         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
15400
15401         # Get ZFS object id
15402         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
15403
15404         # block size change by sequential over write
15405         local blksz
15406         for ((bs=4096; bs <= max_blksz; bs <<= 2)); do
15407                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
15408
15409                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
15410                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
15411         done
15412         rm -f $tf
15413
15414         # block size change by sequential append write
15415         dd if=/dev/zero of=$tf bs=4K count=1 oflag=sync conv=notrunc
15416         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
15417         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
15418
15419         for ((count = 1; count < $((max_blksz / 4096)); count *= 2)); do
15420                 dd if=/dev/zero of=$tf bs=4K count=$count seek=$count \
15421                         oflag=sync conv=notrunc
15422
15423                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
15424                 blksz=$((blksz / 8192)) # in 2*4K unit
15425                 [ $blksz -eq $count ] ||
15426                         error "blksz error(in 8k): $blksz, expected: $count"
15427         done
15428         rm -f $tf
15429
15430         # random write
15431         touch $tf
15432         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
15433         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
15434
15435         dd if=/dev/zero of=$tf bs=8K count=1 oflag=sync conv=notrunc
15436         blksz=$(zfs_object_blksz ost1 $zfs_objid)
15437         [ $blksz -eq 8192 ] || error "blksz error: $blksz, expected: 8k"
15438
15439         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
15440         blksz=$(zfs_object_blksz ost1 $zfs_objid)
15441         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
15442
15443         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
15444         blksz=$(zfs_object_blksz ost1 $zfs_objid)
15445         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
15446 }
15447 run_test 312 "make sure ZFS adjusts its block size by write pattern"
15448
15449 test_313() {
15450         local file=$DIR/$tfile
15451         rm -f $file
15452         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
15453
15454         # define OBD_FAIL_TGT_RCVD_EIO           0x720
15455         do_facet ost1 "$LCTL set_param fail_loc=0x720"
15456         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
15457                 error "write should failed"
15458         do_facet ost1 "$LCTL set_param fail_loc=0"
15459         rm -f $file
15460 }
15461 run_test 313 "io should fail after last_rcvd update fail"
15462
15463 test_399() { # LU-7655 for OST fake write
15464         # turn off debug for performance testing
15465         local saved_debug=$($LCTL get_param -n debug)
15466         $LCTL set_param debug=0
15467
15468         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
15469
15470         # get ost1 size - lustre-OST0000
15471         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
15472         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
15473         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
15474
15475         local start_time=$(date +%s.%N)
15476         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$blocks oflag=sync ||
15477                 error "real dd writing error"
15478         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
15479         rm -f $DIR/$tfile
15480
15481         # define OBD_FAIL_OST_FAKE_WRITE        0x238
15482         do_facet ost1 $LCTL set_param fail_loc=0x238
15483
15484         local start_time=$(date +%s.%N)
15485         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$blocks oflag=sync ||
15486                 error "fake dd writing error"
15487         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
15488
15489         # verify file size
15490         cancel_lru_locks osc
15491         $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
15492                 error "$tfile size not $blocks MB"
15493
15494         do_facet ost1 $LCTL set_param fail_loc=0
15495
15496         echo "fake write $duration_fake vs. normal write $duration in seconds"
15497         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
15498                 error_not_in_vm "fake write is slower"
15499
15500         $LCTL set_param -n debug="$saved_debug"
15501         rm -f $DIR/$tfile
15502 }
15503 run_test 399 "fake write should not be slower than normal write"
15504
15505 test_400a() { # LU-1606, was conf-sanity test_74
15506         local extra_flags=''
15507         local out=$TMP/$tfile
15508         local prefix=/usr/include/lustre
15509         local prog
15510
15511         if ! which $CC > /dev/null 2>&1; then
15512                 skip_env "$CC is not installed"
15513                 return 0
15514         fi
15515
15516         if ! [[ -d $prefix ]]; then
15517                 # Assume we're running in tree and fixup the include path.
15518                 extra_flags+=" -I$LUSTRE/../libcfs/include"
15519                 extra_flags+=" -I$LUSTRE/include"
15520                 extra_flags+=" -L$LUSTRE/utils"
15521         fi
15522
15523         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
15524                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
15525                         error "client api broken"
15526         done
15527         rm -f $out
15528 }
15529 run_test 400a "Lustre client api program can compile and link"
15530
15531 test_400b() { # LU-1606, LU-5011
15532         local header
15533         local out=$TMP/$tfile
15534         local prefix=/usr/include/lustre
15535
15536         # We use a hard coded prefix so that this test will not fail
15537         # when run in tree. There are headers in lustre/include/lustre/
15538         # that are not packaged (like lustre_idl.h) and have more
15539         # complicated include dependencies (like config.h and lnet/types.h).
15540         # Since this test about correct packaging we just skip them when
15541         # they don't exist (see below) rather than try to fixup cppflags.
15542
15543         if ! which $CC > /dev/null 2>&1; then
15544                 skip_env "$CC is not installed"
15545                 return 0
15546         fi
15547
15548         for header in $prefix/*.h; do
15549                 if ! [[ -f "$header" ]]; then
15550                         continue
15551                 fi
15552
15553                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
15554                         continue # liblustreapi.h is deprecated.
15555                 fi
15556
15557                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
15558                         error "cannot compile '$header'"
15559         done
15560         rm -f $out
15561 }
15562 run_test 400b "packaged headers can be compiled"
15563
15564 test_401a() { #LU-7437
15565         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
15566         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
15567                 return
15568         #count the number of parameters by "list_param -R"
15569         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
15570         #count the number of parameters by listing proc files
15571         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
15572                       sort -u | wc -l)
15573
15574         [ $params -eq $procs ] ||
15575                 error "found $params parameters vs. $procs proc files"
15576
15577         # test the list_param -D option only returns directories
15578         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
15579         #count the number of parameters by listing proc directories
15580         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
15581                 sort -u | wc -l)
15582
15583         [ $params -eq $procs ] ||
15584                 error "found $params parameters vs. $procs proc files"
15585 }
15586 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
15587
15588 test_401b() {
15589         local save=$($LCTL get_param -n jobid_var)
15590         local tmp=testing
15591
15592         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
15593                 error "no error returned when setting bad parameters"
15594
15595         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
15596         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
15597
15598         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
15599         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
15600         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
15601 }
15602 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
15603
15604 test_401c() {
15605         local jobid_var_old=$($LCTL get_param -n jobid_var)
15606         local jobid_var_new
15607
15608         $LCTL set_param jobid_var= &&
15609                 error "no error returned for 'set_param a='"
15610
15611         jobid_var_new=$($LCTL get_param -n jobid_var)
15612         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
15613                 error "jobid_var was changed by setting without value"
15614
15615         $LCTL set_param jobid_var &&
15616                 error "no error returned for 'set_param a'"
15617
15618         jobid_var_new=$($LCTL get_param -n jobid_var)
15619         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
15620                 error "jobid_var was changed by setting without value"
15621 }
15622 run_test 401c "Verify 'lctl set_param' without value fails in either format."
15623
15624 test_401d() {
15625         local jobid_var_old=$($LCTL get_param -n jobid_var)
15626         local jobid_var_new
15627         local new_value="foo=bar"
15628
15629         $LCTL set_param jobid_var=$new_value ||
15630                 error "'set_param a=b' did not accept a value containing '='"
15631
15632         jobid_var_new=$($LCTL get_param -n jobid_var)
15633         [[ "$jobid_var_new" == "$new_value" ]] ||
15634                 error "'set_param a=b' failed on a value containing '='"
15635
15636         # Reset the jobid_var to test the other format
15637         $LCTL set_param jobid_var=$jobid_var_old
15638         jobid_var_new=$($LCTL get_param -n jobid_var)
15639         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
15640                 error "failed to reset jobid_var"
15641
15642         $LCTL set_param jobid_var $new_value ||
15643                 error "'set_param a b' did not accept a value containing '='"
15644
15645         jobid_var_new=$($LCTL get_param -n jobid_var)
15646         [[ "$jobid_var_new" == "$new_value" ]] ||
15647                 error "'set_param a b' failed on a value containing '='"
15648
15649         $LCTL set_param jobid_var $jobid_var_old
15650         jobid_var_new=$($LCTL get_param -n jobid_var)
15651         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
15652                 error "failed to reset jobid_var"
15653 }
15654 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
15655
15656 test_402() {
15657         local server_version=$(lustre_version_code $SINGLEMDS)
15658         [[ $server_version -ge $(version_code 2.7.66) ]] ||
15659         [[ $server_version -ge $(version_code 2.7.18.4) &&
15660                 $server_version -lt $(version_code 2.7.50) ]] ||
15661         [[ $server_version -ge $(version_code 2.7.2) &&
15662                 $server_version -lt $(version_code 2.7.11) ]] ||
15663                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
15664                         return; }
15665         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15666         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
15667 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
15668         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
15669         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
15670                 echo "Touch failed - OK"
15671 }
15672 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
15673
15674 test_403() {
15675         local file1=$DIR/$tfile.1
15676         local file2=$DIR/$tfile.2
15677         local tfile=$TMP/$tfile
15678
15679         rm -f $file1 $file2 $tfile
15680
15681         touch $file1
15682         ln $file1 $file2
15683
15684         # 30 sec OBD_TIMEOUT in ll_getattr()
15685         # right before populating st_nlink
15686         $LCTL set_param fail_loc=0x80001409
15687         stat -c %h $file1 > $tfile &
15688
15689         # create an alias, drop all locks and reclaim the dentry
15690         < $file2
15691         cancel_lru_locks mdc
15692         cancel_lru_locks osc
15693         sysctl -w vm.drop_caches=2
15694
15695         wait
15696
15697         [ `cat $tfile` -gt 0 ] || error "wrong nlink count: `cat $tfile`"
15698
15699         rm -f $tfile $file1 $file2
15700 }
15701 run_test 403 "i_nlink should not drop to zero due to aliasing"
15702
15703 test_404() { # LU-6601
15704         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15705         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
15706                 awk '/osp .*-osc-MDT/ { print $4}')
15707
15708         local osp
15709         for osp in $mosps; do
15710                 echo "Deactivate: " $osp
15711                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
15712                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
15713                         awk -vp=$osp '$4 == p { print $2 }')
15714                 [ $stat = IN ] || {
15715                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
15716                         error "deactivate error"
15717                 }
15718                 echo "Activate: " $osp
15719                 do_facet $SINGLEMDS $LCTL --device %$osp activate
15720                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
15721                         awk -vp=$osp '$4 == p { print $2 }')
15722                 [ $stat = UP ] || {
15723                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
15724                         error "activate error"
15725                 }
15726         done
15727 }
15728 run_test 404 "validate manual {de}activated works properly for OSPs"
15729
15730 test_405() {
15731         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
15732                 skip "Layout swap lock is not supported" && return
15733
15734         check_swap_layouts_support && return 0
15735
15736         test_mkdir -p $DIR/$tdir
15737         swap_lock_test -d $DIR/$tdir ||
15738                 error "One layout swap locked test failed"
15739 }
15740 run_test 405 "Various layout swap lock tests"
15741
15742 test_406() {
15743         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15744         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
15745         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
15746         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15747         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
15748                 skip "Need MDS version at least 2.8.50" && return
15749
15750         local def_stripenr=$($GETSTRIPE -c $MOUNT)
15751         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
15752         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
15753         local def_pool=$($GETSTRIPE -p $MOUNT)
15754
15755         local test_pool=$TESTNAME
15756         pool_add $test_pool || error "pool_add failed"
15757         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
15758                 error "pool_add_targets failed"
15759
15760         # parent set default stripe count only, child will stripe from both
15761         # parent and fs default
15762         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
15763                 error "setstripe $MOUNT failed"
15764         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
15765         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
15766         for i in $(seq 10); do
15767                 local f=$DIR/$tdir/$tfile.$i
15768                 touch $f || error "touch failed"
15769                 local count=$($GETSTRIPE -c $f)
15770                 [ $count -eq $OSTCOUNT ] ||
15771                         error "$f stripe count $count != $OSTCOUNT"
15772                 local offset=$($GETSTRIPE -i $f)
15773                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
15774                 local size=$($GETSTRIPE -S $f)
15775                 [ $size -eq $((def_stripe_size * 2)) ] ||
15776                         error "$f stripe size $size != $((def_stripe_size * 2))"
15777                 local pool=$($GETSTRIPE -p $f)
15778                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
15779         done
15780
15781         # change fs default striping, delete parent default striping, now child
15782         # will stripe from new fs default striping only
15783         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
15784                 error "change $MOUNT default stripe failed"
15785         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
15786         for i in $(seq 11 20); do
15787                 local f=$DIR/$tdir/$tfile.$i
15788                 touch $f || error "touch $f failed"
15789                 local count=$($GETSTRIPE -c $f)
15790                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
15791                 local offset=$($GETSTRIPE -i $f)
15792                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
15793                 local size=$($GETSTRIPE -S $f)
15794                 [ $size -eq $def_stripe_size ] ||
15795                         error "$f stripe size $size != $def_stripe_size"
15796                 local pool=$($GETSTRIPE -p $f)
15797                 [ "#$pool" == "#" ] || error "$f pool $pool is set"
15798
15799         done
15800
15801         unlinkmany $DIR/$tdir/$tfile. 1 20
15802
15803         # restore FS default striping
15804         if [ -z $def_pool ]; then
15805                 $SETSTRIPE -c $def_stripenr -S $def_stripe_size \
15806                         -i $def_stripe_offset $MOUNT ||
15807                         error "restore default striping failed"
15808         else
15809                 $SETSTRIPE -c $def_stripenr -S $def_stripe_size -p $def_pool \
15810                         -i $def_stripe_offset $MOUNT ||
15811                         error "restore default striping with $def_pool failed"
15812         fi
15813
15814         local f=$DIR/$tdir/$tfile
15815         pool_remove_all_targets $test_pool $f
15816         pool_remove $test_pool $f
15817 }
15818 run_test 406 "DNE support fs default striping"
15819
15820 test_407() {
15821         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15822
15823         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
15824                 skip "Need MDS version at least 2.8.55" && return
15825
15826         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
15827                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
15828         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
15829                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
15830         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
15831
15832         #define OBD_FAIL_DT_TXN_STOP    0x2019
15833         for idx in $(seq $MDSCOUNT); do
15834                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
15835         done
15836         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
15837         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
15838                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
15839         true
15840 }
15841 run_test 407 "transaction fail should cause operation fail"
15842
15843 test_408() {
15844         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
15845
15846         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
15847         lctl set_param fail_loc=0x8000040a
15848         # let ll_prepare_partial_page() fail
15849         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
15850
15851         rm -f $DIR/$tfile
15852
15853         # create at least 100 unused inodes so that
15854         # shrink_icache_memory(0) should not return 0
15855         touch $DIR/$tfile-{0..100}
15856         rm -f $DIR/$tfile-{0..100}
15857         sync
15858
15859         echo 2 > /proc/sys/vm/drop_caches
15860 }
15861 run_test 408 "drop_caches should not hang due to page leaks"
15862
15863 test_409()
15864 {
15865         [ $MDSCOUNT -lt 2 ] &&
15866                 skip "We need at least 2 MDTs for this test" && return
15867
15868         check_mount_and_prep
15869
15870         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
15871         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
15872         touch $DIR/$tdir/guard || error "(2) Fail to create"
15873
15874         local PREFIX=$(str_repeat 'A' 128)
15875         echo "Create 1K hard links start at $(date)"
15876         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
15877                 error "(3) Fail to hard link"
15878
15879         echo "Links count should be right although linkEA overflow"
15880         stat $DIR/$tdir/guard || error "(4) Fail to stat"
15881         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
15882         [ $linkcount -eq 1001 ] ||
15883                 error "(5) Unexpected hard links count: $linkcount"
15884
15885         echo "List all links start at $(date)"
15886         ls -l $DIR/$tdir/foo > /dev/null ||
15887                 error "(6) Fail to list $DIR/$tdir/foo"
15888
15889         echo "Unlink hard links start at $(date)"
15890         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
15891                 error "(7) Fail to unlink"
15892 }
15893 run_test 409 "Large amount of cross-MDTs hard links on the same file"
15894
15895 #
15896 # tests that do cleanup/setup should be run at the end
15897 #
15898
15899 test_900() {
15900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15901         local ls
15902         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
15903         $LCTL set_param fail_loc=0x903
15904
15905         cancel_lru_locks MGC
15906
15907         FAIL_ON_ERROR=true cleanup
15908         FAIL_ON_ERROR=true setup
15909 }
15910 run_test 900 "umount should not race with any mgc requeue thread"
15911
15912 complete $SECONDS
15913 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
15914 check_and_cleanup_lustre
15915 if [ "$I_MOUNTED" != "yes" ]; then
15916         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
15917 fi
15918 exit_status