Whamcloud - gitweb
LU-16025 llite: adjust read count as file got truncated
[fs/lustre-release.git] / lustre / tests / sanityn.sh
1 #!/bin/bash
2
3 set -e
4
5 ONLY=${ONLY:-"$*"}
6
7 SIZE=${SIZE:-40960}
8 OPENFILE=${OPENFILE:-openfile}
9 OPENUNLINK=${OPENUNLINK:-openunlink}
10 export TMP=${TMP:-/tmp}
11 MOUNT_2=${MOUNT_2:-"yes"}
12 CHECK_GRANT=${CHECK_GRANT:-"yes"}
13 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
14
15 LUSTRE=${LUSTRE:-$(dirname $0)/..}
16 . $LUSTRE/tests/test-framework.sh
17 init_test_env $@
18 init_logging
19
20 ALWAYS_EXCEPT="$SANITYN_EXCEPT "
21 # bug number for skipped test:  LU-7105 LU-14541
22 ALWAYS_EXCEPT+="                28      16f"
23 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
24
25 if [ $mds1_FSTYPE = "zfs" ]; then
26         # bug number:    LU-15757 (test_102() causes crash in umount later)
27         ALWAYS_EXCEPT+=" 102"
28         # LU-2829 / LU-2887 - make allowances for ZFS slowness
29         TEST33_NFILES=${TEST33_NFILES:-1000}
30 fi
31
32 #                                  23   (min)"
33 [ "$SLOW" = "no" ] && EXCEPT_SLOW="33a"
34
35 build_test_filter
36
37 FAIL_ON_ERROR=false
38
39 SETUP=${SETUP:-:}
40 TRACE=${TRACE:-""}
41
42 check_and_setup_lustre
43
44 OSC=${OSC:-"osc"}
45
46 assert_DIR
47 rm -rf $DIR1/[df][0-9]* $DIR1/lnk $DIR/[df].${TESTSUITE}*
48
49 SAMPLE_FILE=$TMP/$(basename $0 .sh).junk
50 dd if=/dev/urandom of=$SAMPLE_FILE bs=1M count=1
51
52 # $RUNAS_ID may get set incorrectly somewhere else
53 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
54
55 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
56
57 test_0() {
58         local client2=${CLIENT2:-$HOSTNAME}
59         local tmp=$(mktemp)
60
61         printf 'a b\n' > $tmp
62
63         do_node_vp "$HOSTNAME" printf 'a b\n' |
64         diff $tmp - || error "do_node_vp mismatch"
65
66         do_node_vp "$client2" printf 'a b\n' |
67         diff $tmp - || error "do_node_vp mismatch"
68
69         do_facet_vp mds1 printf 'a b\n' |
70         diff $tmp - || error "do_facet_vp mismatch"
71
72         printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' > $tmp
73
74         do_node_vp "$HOSTNAME" printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
75         diff $tmp - || error "do_node_vp mismatch"
76
77         do_node_vp "$client2" printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
78         diff $tmp - || error "do_node_vp mismatch"
79
80         do_facet_vp mds1 printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
81         diff $tmp - || error "do_facet_vp mismatch"
82 }
83 run_test 0 "do_node_vp() and do_facet_vp() do the right thing"
84
85 test_1() {
86         touch $DIR1/$tfile
87         [ -f $DIR2/$tfile ] || error "Check create"
88         chmod 777 $DIR2/$tfile
89         $CHECKSTAT -t file -p 0777 $DIR1/$tfile ||
90                 error "Check attribute update for 0777"
91
92         chmod a-x $DIR2/$tfile
93         $CHECKSTAT -t file -p 0666 $DIR1/$tfile ||
94                 error "Check attribute update for 0666"
95
96         rm $DIR2/$tfile
97         $CHECKSTAT -a $DIR1/$tfile ||
98                 error "Check unlink - removes file on other mountpoint"
99 }
100 run_test 1 "Check attribute updates on 2 mount points"
101
102 test_2a() {
103         touch $DIR1/f2a
104         ls -l $DIR2/f2a
105         chmod 777 $DIR2/f2a
106         $CHECKSTAT -t file -p 0777 $DIR1/f2a ||
107                 error "Either not file type or perms not 0777"
108 }
109 run_test 2a "check cached attribute updates on 2 mtpt's ========"
110
111 test_2b() {
112         touch $DIR1/f2b
113         ls -l $DIR2/f2b
114         chmod 777 $DIR1/f2b
115         $CHECKSTAT -t file -p 0777 $DIR2/f2b ||
116                 error "Either not file type or perms not 0777"
117 }
118 run_test 2b "check cached attribute updates on 2 mtpt's ========"
119
120 # NEED TO SAVE ROOT DIR MODE
121 test_2c() {
122         chmod 777 $DIR1
123         $CHECKSTAT -t dir -p 0777 $DIR2 ||
124                 error "Either not dir type or perms not 0777"
125 }
126 run_test 2c "check cached attribute updates on 2 mtpt's root ==="
127
128 test_2d() {
129         chmod 755 $DIR1
130         $CHECKSTAT -t dir -p 0755 $DIR2 ||
131                 error "Either not file type or perms not 0775"
132 }
133 run_test 2d "check cached attribute updates on 2 mtpt's root ==="
134
135 test_2e() {
136         chmod 755 $DIR1
137         ls -l $DIR1
138         ls -l $DIR2
139         chmod 777 $DIR1
140                 $RUNAS dd if=/dev/zero of=$DIR2/$tfile count=1 ||
141                         error "dd failed"
142 }
143 run_test 2e "check chmod on root is propagated to others"
144
145 test_2f() {
146         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
147         local MDTIDX=1
148         local remote_dir=$tdir/remote_dir
149
150         mkdir -p $DIR1/$tdir
151         $LFS mkdir -i $MDTIDX $DIR1/$remote_dir ||
152                    error "Create remote directory failed"
153
154         touch $DIR1/$remote_dir/$tfile ||
155                 error "Create file under remote directory failed"
156         chmod 777 $DIR1/$remote_dir/$tfile ||
157                 error "Chmod file under remote directory failed"
158
159         $CHECKSTAT -t file -p 0777 $DIR2/$remote_dir/$tfile ||
160                 error "Check attr of file under remote directory failed"
161
162         chown $RUNAS_ID:$RUNAS_GID $DIR1/$remote_dir/$tfile ||
163                 error "Chown file under remote directory failed"
164
165         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR2/$remote_dir/$tfile ||
166                 error "Check owner of file under remote directory failed"
167
168         cd $DIR2/$remote_dir || error "enter remote dir"
169         rm -rf $DIR1/$remote_dir/$tfile ||
170                 error "Unlink remote directory failed"
171
172         $CHECKSTAT -t file $DIR2/$remote_dir/$tfile &&
173                 error "unlink file still exists!"
174
175         cd $DIR2/$tdir || error "exit remote dir"
176         rm -rf $DIR1/$tdir || error "unlink directory failed"
177 }
178 run_test 2f "check attr/owner updates on DNE with 2 mtpt's"
179
180 test_2g() {
181         dd if=/dev/zero of=$DIR1/$tfile oflag=sync bs=1M count=2
182
183         local block1=$(stat $DIR1/$tfile | awk '/Blocks/ {print $4} ')
184         cancel_lru_locks osc
185         local block2=$(stat $DIR2/$tfile | awk '/Blocks/ {print $4} ')
186         echo "$DIR1/$tfile has $block1 blocks"
187         echo "$DIR2/$tfile has $block2 blocks"
188         [ $block1 -eq $block2 ] || error "$block1 not equal to $block2"
189 }
190 run_test 2g "check blocks update on sync write"
191
192 test_3() {
193         local target="this/is/good"
194         ln -s $target $DIR1/$tfile || error "ln -s $target $DIR1/$tfile failed"
195         [ "$(ls -l $DIR2/$tfile | sed -e 's/.* -> //')" = "$target" ] ||
196                 error "link $DIR2/$tfile not as expected"
197 }
198 run_test 3 "symlink on one mtpt, readlink on another ==========="
199
200 test_4() {
201         multifstat $DIR1/f4 $DIR2/f4
202 }
203 run_test 4 "fstat validation on multiple mount points =========="
204
205 test_5() {
206         mcreate $DIR1/f5
207         $TRUNCATE $DIR2/f5 100
208         $CHECKSTAT -t file -s 100 $DIR1/f5 ||
209                 error "Either not file type or size not equal to 100 bytes"
210         rm $DIR1/f5
211 }
212 run_test 5 "create a file on one mount, truncate it on the other"
213
214 test_6() {
215         openunlink $DIR1/$tfile $DIR2/$tfile || \
216                 error "openunlink $DIR1/$tfile $DIR2/$tfile"
217 }
218 run_test 6 "remove of open file on other node =================="
219
220 test_7() {
221         local dir=d7
222         opendirunlink $DIR1/$dir $DIR2/$dir || \
223                 error "opendirunlink $DIR1/$dir $DIR2/$dir"
224 }
225 run_test 7 "remove of open directory on other node ============="
226
227 test_8() {
228         opendevunlink $DIR1/$tfile $DIR2/$tfile || \
229                 error "opendevunlink $DIR1/$tfile $DIR2/$tfile"
230 }
231 run_test 8 "remove of open special file on other node =========="
232
233 test_9a() {
234         MTPT=1
235         local dir
236         > $DIR2/f9
237         for C in a b c d e f g h i j k l; do
238                 dir=`eval echo \\$DIR$MTPT`
239                 echo -n $C >> $dir/f9
240                 [ "$MTPT" -eq 1 ] && MTPT=2 || MTPT=1
241         done
242         [ "`cat $DIR1/f9`" = "abcdefghijkl" ] || \
243                 error "`od -a $DIR1/f9` != abcdefghijkl"
244 }
245 run_test 9a "append of file with sub-page size on multiple mounts"
246
247 #LU-10681 - tiny writes & appending to sparse striped file
248 test_9b() {
249         [[ $OSTCOUNT -ge 2 ]] || { skip "needs >= 2 OSTs"; return; }
250
251         $LFS setstripe -c 2 -S 1M $DIR/$tfile
252         echo "foo" >> $DIR/$tfile
253         dd if=/dev/zero of=$DIR2/$tfile bs=1M count=1 seek=1 conv=notrunc ||
254                 error "sparse dd $DIR2/$tfile failed"
255         echo "foo" >> $DIR/$tfile
256
257         data=$(dd if=$DIR2/$tfile bs=1 count=3 skip=$((2 * 1048576)) conv=notrunc)
258         echo "Data read (expecting 'foo')": $data
259         [ "$data" = "foo" ] || error "append to sparse striped file failed"
260 }
261 run_test 9b "append to striped sparse file"
262
263 test_10a() {
264         MTPT=1
265         local dir
266         OFFSET=0
267         > $DIR2/f10
268         for C in a b c d e f g h i j k l; do
269                 dir=`eval echo \\$DIR$MTPT`
270                 echo -n $C | dd of=$dir/f10 bs=1 seek=$OFFSET count=1
271                 [ "$MTPT" -eq 1 ] && MTPT=2 || MTPT=1
272                 OFFSET=`expr $OFFSET + 1`
273         done
274         [ "`cat $DIR1/f10`" = "abcdefghijkl" ] || \
275                 error "`od -a $DIR1/f10` != abcdefghijkl"
276 }
277 run_test 10a "write of file with sub-page size on multiple mounts "
278
279 test_10b() {
280         # create a seed file
281         yes "R" | head -c 4000 >$TMP/f10b-seed
282         dd if=$TMP/f10b-seed of=$DIR1/f10b bs=3k count=1 || error "dd $DIR1"
283
284         $TRUNCATE $DIR1/f10b 4096 || error "truncate 4096"
285
286         dd if=$DIR2/f10b of=$TMP/f10b-lustre bs=4k count=1 || error "dd $DIR2"
287
288         # create a test file locally to compare
289         dd if=$TMP/f10b-seed of=$TMP/f10b bs=3k count=1 || error "dd random"
290         $TRUNCATE $TMP/f10b 4096 || error "truncate 4096"
291         cmp $TMP/f10b $TMP/f10b-lustre || error "file miscompare"
292         rm $TMP/f10b $TMP/f10b-lustre $TMP/f10b-seed
293 }
294 run_test 10b "write of file with sub-page size on multiple mounts "
295
296 test_11() {
297         test_mkdir $DIR1/d11
298         multiop_bg_pause $DIR1/d11/f O_c || return 1
299         MULTIPID=$!
300         cp -p /bin/ls $DIR1/d11/f
301         $DIR2/d11/f
302         RC=$?
303         kill -USR1 $MULTIPID
304         wait $MULTIPID || error "wait for PID $MULTIPID failed"
305         [ $RC -eq 0 ] && error || true
306 }
307 run_test 11 "execution of file opened for write should return error ===="
308
309 test_12() {
310         DIR=$DIR DIR2=$DIR2 bash lockorder.sh
311 }
312 run_test 12 "test lock ordering (link, stat, unlink)"
313
314 test_13() {     # bug 2451 - directory coherency
315         test_mkdir $DIR1/d13
316         cd $DIR1/d13 || error "cd to $DIR1/d13 failed"
317         ls
318         ( touch $DIR1/d13/f13 ) # needs to be a separate shell
319         ls
320         rm -f $DIR2/d13/f13 || error "Cannot remove $DIR2/d13/f13"
321         ls 2>&1 | grep f13 && error "f13 shouldn't return an error (1)" || true
322         # need to run it twice
323         ( touch $DIR1/d13/f13 ) # needs to be a separate shell
324         ls
325         rm -f $DIR2/d13/f13 || error "Cannot remove $DIR2/d13/f13"
326         ls 2>&1 | grep f13 && error "f13 shouldn't return an error (2)" || true
327 }
328 run_test 13 "test directory page revocation"
329
330 test_14aa() {
331         test_mkdir $DIR1/$tdir
332         cp -p /bin/ls $DIR1/$tdir/$tfile
333         multiop_bg_pause $DIR1/$tdir/$tfile Ow_c || return 1
334         MULTIPID=$!
335
336         $DIR2/$tdir/$tfile && error || true
337         kill $MULTIPID
338 }
339 run_test 14aa "execution of file open for write returns -ETXTBSY"
340
341 test_14ab() {
342         test_mkdir $DIR1/$tdir
343         cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
344         $DIR1/$tdir/sleep 60 &
345         SLEEP_PID=$!
346         $MULTIOP $DIR2/$tdir/sleep Oc && error "expected error, got success"
347         kill $SLEEP_PID
348 }
349 run_test 14ab "open(RDWR) of executing file returns -ETXTBSY"
350
351 test_14b() { # bug 3192, 7040
352         test_mkdir $DIR1/$tdir
353         cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
354         $DIR1/$tdir/sleep 60 &
355         SLEEP_PID=$!
356         $TRUNCATE $DIR2/$tdir/sleep 60 && kill -9 $SLEEP_PID && \
357                 error "expected truncate error, got success"
358         kill $SLEEP_PID
359         cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
360 }
361 run_test 14b "truncate of executing file returns -ETXTBSY ======"
362
363 test_14c() { # bug 3430, 7040
364         test_mkdir $DIR1/$tdir
365         cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
366         $DIR1/$tdir/sleep 60 &
367         SLEEP_PID=$!
368         cp /etc/hosts $DIR2/$tdir/sleep && error "expected error, got success"
369         kill $SLEEP_PID
370         cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
371 }
372 run_test 14c "open(O_TRUNC) of executing file return -ETXTBSY =="
373
374 test_14d() { # bug 10921
375         test_mkdir $DIR1/$tdir
376         cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
377         $DIR1/$tdir/sleep 60 &
378         SLEEP_PID=$!
379         log chmod
380         chmod 600 $DIR1/$tdir/sleep || error "chmod failed"
381         kill $SLEEP_PID
382         cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
383 }
384 run_test 14d "chmod of executing file is still possible ========"
385
386 test_15() {     # bug 974 - ENOSPC
387         echo "PATH=$PATH"
388         bash oos2.sh $MOUNT1 $MOUNT2
389         wait_delete_completed
390         grant_error=$(dmesg | grep "< tot_grant")
391         [ -z "$grant_error" ] || error "$grant_error"
392 }
393 run_test 15 "test out-of-space with multiple writers ==========="
394
395 COUNT=${COUNT:-2500}
396 # The FSXNUM reduction for ZFS is needed until ORI-487 is fixed.
397 # We don't want to skip it entirely, but ZFS is VERY slow and cannot
398 # pass a 2500 operation dual-mount run within the time limit.
399 if [ "$ost1_FSTYPE" = "zfs" ]; then
400         FSXNUM=$((COUNT / 5))
401         FSXP=1
402 elif [ "$SLOW" = "yes" ]; then
403         FSXNUM=$((COUNT * 5))
404         FSXP=500
405 else
406         FSXNUM=$COUNT
407         FSXP=100
408 fi
409
410 test_16a() {
411         local file1=$DIR1/$tfile
412         local file2=$DIR2/$tfile
413         local stripe_size=$(do_facet $SINGLEMDS \
414                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
415
416         check_set_fallocate
417
418         # to allocate grant because it may run out due to test_15.
419         $LFS setstripe -c -1 $file1
420         dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
421         dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
422         rm -f $file1
423
424         $LFS setstripe -c -1 $file1 # b=10919
425         $FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 $file1 $file2 ||
426                 error "fsx failed"
427         rm -f $file1
428
429         # O_DIRECT reads and writes must be aligned to the PAGE_SIZE.
430         $FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 -Z -r $PAGE_SIZE \
431                 -w $PAGE_SIZE $file1 $file2 || error "fsx with O_DIRECT failed."
432 }
433 run_test 16a "$FSXNUM iterations of dual-mount fsx"
434
435 # Consistency check for tiny writes, LU-9409
436 test_16b() {
437         local file1=$DIR1/$tfile
438         local file2=$DIR2/$tfile
439         local stripe_size=($($LFS getstripe -S $DIR))
440
441         check_set_fallocate
442
443         # to allocate grant because it may run out due to test_15.
444         lfs setstripe -c -1 $file1
445         dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync ||
446                 error "dd failed writing to file=$file1"
447         dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync ||
448                 error "dd failed writing to file=$file2"
449         rm -f $file1
450
451         lfs setstripe -c -1 $file1 # b=10919
452         # -o is set to 8192 because writes < 1 page and between 1 and 2 pages
453         # create a mix of tiny writes & normal writes
454         $FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -o 8192 -S 0 \
455                 $file1 $file2 || error "fsx with tiny write failed."
456 }
457 run_test 16b "$FSXNUM iterations of dual-mount fsx at small size"
458
459 test_16c() {
460         local file1=$DIR1/$tfile
461         local file2=$DIR2/$tfile
462         local stripe_size=$(do_facet $SINGLEMDS \
463                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
464
465         [ "$ost1_FSTYPE" != ldiskfs ] && skip "dio on ldiskfs only"
466
467         check_set_fallocate
468
469         # to allocate grant because it may run out due to test_15.
470         $LFS setstripe -c -1 $file1
471         dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
472         dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
473         rm -f $file1
474         wait_delete_completed
475
476         local list=$(comma_list $(osts_nodes))
477         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
478                 skip "not cache-capable obdfilter"
479         fi
480
481         set_osd_param $list '' read_cache_enable 0
482         set_osd_param $list '' writethrough_cache_enable 0
483
484         $LFS setstripe -c -1 $file1 # b=10919
485         $FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 $file1 $file2 ||
486                 error "fsx failed"
487         rm -f $file1
488
489         set_osd_param $list '' read_cache_enable 1
490         set_osd_param $list '' writethrough_cache_enable 1
491
492         return 0
493 }
494 run_test 16c "verify data consistency on ldiskfs with cache disabled (b=17397)"
495
496 test_16d() {
497         local file1=$DIR1/$tfile
498         local file2=$DIR2/$tfile
499         local file3=$DIR1/file
500         local tmpfile=$(mktemp)
501         local stripe_size=$(do_facet $SINGLEMDS \
502                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
503
504         # to allocate grant because it may run out due to test_15.
505         $LFS setstripe -c -1 $file1
506         stack_trap "rm -f $file1 $file2 $file3 $tmpfile"
507         dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
508         dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
509         rm -f $file1
510
511         $LFS setstripe -c -1 $file1 # b=10919
512         $LCTL set_param ldlm.namespaces.*.lru_size=clear
513
514         # direct write on one client and direct read from another
515         dd if=/dev/urandom of=$file1 bs=1M count=100 oflag=direct
516         dd if=$file2 of=$tmpfile iflag=direct bs=1M
517         diff $file1 $tmpfile || error "file different(1)"
518         rm -f $file1
519
520         # buffer write on one client, but direct read from another
521         dd if=$tmpfile of=$file1 bs=1M count=100
522         dd if=$file2 of=$file3 bs=1M iflag=direct count=100
523         diff $file3 $tmpfile || error "file different(2)"
524
525         rm -f $file3 $file2 $file1
526         # direct write on one client
527         dd if=$tmpfile of=$file1 bs=1M count=100 oflag=direct
528         # buffer read from another client
529         dd if=$file2 of=$file3 bs=1M count=100
530         diff $file3 $tmpfile || error "file different(3)"
531 }
532 run_test 16d "Verify DIO and buffer IO with two clients"
533
534 test_16e() { # LU-13227
535         # issue:        LU-14314
536
537         (( "$MDS1_VERSION" >= $(version_code 2.13.53) )) ||
538                 skip "Need MDS version at least 2.13.53"
539
540         local file1=$DIR1/$tfile
541         local file2=$DIR2/$tfile
542
543         # client1 write 10M data
544         dd if=/dev/zero of=$file1 bs=1M count=10
545         # drop locks
546         cancel_lru_locks osc > /dev/null
547         # use lockahead to generate one PW lock to keep LVB loaded.
548         $LFS ladvise -a lockahead --start 0 --length 1M \
549                 --mode WRITE $file1
550         # direct write to extend file size on client2
551         dd if=/dev/zero of=$file2 bs=1M seek=20 count=1 \
552                 oflag=direct conv=notrunc
553         local filesize=$(stat -c %s $file2)
554         [ "$filesize" -eq 22020096 ] ||
555                 error "expected filesize 22020096 got $filesize"
556         rm -f $file1
557 }
558 run_test 16e "Verify size consistency for O_DIRECT write"
559
560 test_16f() { # LU-14541
561         local file1=$DIR1/$tfile
562         local file2=$DIR2/$tfile
563         local duration=20
564         local status
565
566         timeout --preserve-status --signal=USR1 $duration \
567                 rw_seq_cst_vs_drop_caches $file1 $file2
568         status=$?
569
570         case $((status & 0x7f)) in
571                 0)
572                         echo OK # Computers must be fast now.
573                         ;;
574                 6) # SIGABRT
575                         error "sequential consistency violation detected"
576                         ;;
577                 10) # SIGUSR1
578                         echo TIMEOUT # This is fine.
579                         ;;
580                 *)
581                         error "strange status '$status'"
582                         ;;
583         esac
584
585         rm -f $file1
586 }
587 run_test 16f "rw sequential consistency vs drop_caches"
588
589 test_16h() {
590         local tf=$DIR/$tdir/$tfile
591         local tf2=$DIR2/$tdir/$tfile
592         local cmd="$MMAP_CAT $tf | od -x | tail -q -n4"
593         local cmd2="$MMAP_CAT $tf2 | od -x | tail -q -n4"
594
595         test_mkdir $DIR/$tdir
596
597         # create file and populate data
598         cp /etc/passwd $tf || error "cp failed"
599
600         local size=$(stat -c %s $tf)
601         c1=$(eval $cmd)
602         c2=$(eval $cmd2)
603         if [[ "$c1" != "$c2" ]]; then
604                 echo "  ------- mount 1 read --------"
605                 echo $c1
606                 echo "  ------- mount 2 read --------"
607                 echo $c2
608                 error "content mismatch"
609         fi
610
611         echo "  ------- before truncate --------"
612         echo $c1
613
614         # truncate file
615         $TRUNCATE $tf $((size / 2)) || error "truncate file"
616
617         #cancel_lru_locks
618         echo "  ------- after truncate --------"
619
620         # repeat the comparison
621         c1=$(eval $cmd)
622         c2=$(eval $cmd2)
623         if [[ "$c1" != "$c2" ]]; then
624                 echo "  ------- mount 1 read --------"
625                 echo $c1
626                 echo "  ------- mount 2 read --------"
627                 echo $c2
628                 error "content mismatch after truncate"
629         fi
630         echo $c2
631 }
632 run_test 16h "mmap read after truncate file"
633
634 test_16i() {
635         local tf=$DIR/$tdir/$tfile
636         local tf2=$DIR2/$tdir/$tfile
637
638         test_mkdir $DIR/$tdir
639
640         # create file and populate data
641         cp /etc/passwd $tf || error "cp failed"
642
643         local size=$(stat -c %s $tf)
644
645         c1=$(dd if=$tf bs=1 2>/dev/null | od -x | tail -q -n4)
646         c2=$(dd if=$tf2 bs=1 2>/dev/null | od -x | tail -q -n4)
647
648         if [[ "$c1" != "$c2" ]]; then
649                 echo "  ------- mount 1 read --------"
650                 echo $c1
651                 echo "  ------- mount 2 read --------"
652                 echo $c2
653                 error "content mismatch"
654         fi
655
656         echo "  ------- before truncate --------"
657         echo $c1
658
659         # truncate file
660         $TRUNCATE $tf $((size / 2)) || error "truncate file"
661
662         echo "  ------- after truncate --------"
663
664         # repeat the comparison
665         c1=$(dd if=$tf bs=1 2>/dev/null | od -x | tail -q -n4)
666         c2=$(dd if=$tf2 bs=1 2>/dev/null | od -x | tail -q -n4)
667
668         if [[ "$c1" != "$c2" ]]; then
669                 echo "  ------- mount 1 read --------"
670                 echo $c1
671                 echo "  ------- mount 2 read --------"
672                 echo $c2
673                 error "content mismatch after truncate"
674         fi
675         echo $c2
676 }
677 run_test 16i "read after truncate file"
678
679 test_17() { # bug 3513, 3667
680         remote_ost_nodsh && skip "remote OST with nodsh" && return
681
682         lfs setstripe $DIR1/$tfile -i 0 -c 1
683         cp $SAMPLE_FILE $DIR1/$tfile
684         cancel_lru_locks osc > /dev/null
685         #define OBD_FAIL_ONCE|OBD_FAIL_LDLM_CREATE_RESOURCE    0x30a
686         do_facet ost1 lctl set_param fail_loc=0x8000030a
687         ls -ls $DIR1/$tfile | awk '{ print $1,$6 }' > $DIR1/$tfile-1 & \
688         ls -ls $DIR2/$tfile | awk '{ print $1,$6 }' > $DIR2/$tfile-2
689         wait
690         diff -u $DIR1/$tfile-1 $DIR2/$tfile-2 || error "files are different"
691 }
692 run_test 17 "resource creation/LVB creation race ==============="
693
694 test_18() {
695         # turn e.g. ALWAYS_EXCEPT="18c" into "-e 3"
696         local idx
697         local excepts=
698         for idx in {a..z}; do
699                 local ptr=EXCEPT_ALWAYS_18$idx
700                 [ x${!ptr} = xtrue ] || continue
701
702                 excepts="$excepts -e $(($(printf %d \'$idx)-96))"
703         done
704
705         excepts="$excepts -e 7 -e 8 -e 9"
706         $LUSTRE/tests/mmap_sanity -d $MOUNT1 -m $MOUNT2 $excepts ||
707                 error "mmap_sanity test failed"
708         sync; sleep 1; sync
709 }
710 run_test 18 "mmap sanity check ================================="
711
712 test_19() { # bug3811
713         local node=$(facet_active_host ost1)
714         local device="$FSNAME-OST*"
715
716         [ "x$DOM" = "xyes" ] && node=$(facet_active_host $SINGLEMDS) &&
717                 device="$FSNAME-MDT*"
718
719         # check whether obdfilter is cache capable at all
720         get_osd_param $node $device read_cache_enable >/dev/null ||
721                 skip "not cache-capable obdfilter"
722
723         local max=$(get_osd_param $node $device readcache_max_filesize |\
724                 head -n 1)
725         set_osd_param $node $device readcache_max_filesize 4096
726         dd if=/dev/urandom of=$TMP/$tfile bs=512k count=32
727         local SUM=$(cksum $TMP/$tfile | cut -d" " -f 1,2)
728         cp $TMP/$tfile $DIR1/$tfile
729         for i in `seq 1 20`; do
730                 [ $((i % 5)) -eq 0 ] && log "$testname loop $i"
731                 cancel_lru_locks $OSC > /dev/null
732                 cksum $DIR1/$tfile | cut -d" " -f 1,2 > $TMP/sum1 & \
733                 cksum $DIR2/$tfile | cut -d" " -f 1,2 > $TMP/sum2
734                 wait
735                 [ "$(cat $TMP/sum1)" = "$SUM" ] || \
736                         error "$DIR1/$tfile $(cat $TMP/sum1) != $SUM"
737                 [ "$(cat $TMP/sum2)" = "$SUM" ] || \
738                         error "$DIR2/$tfile $(cat $TMP/sum2) != $SUM"
739         done
740         set_osd_param $node $device readcache_max_filesize $max
741         rm $DIR1/$tfile
742 }
743 run_test 19 "test concurrent uncached read races ==============="
744
745 test_20() {
746         test_mkdir $DIR1/$tdir
747         cancel_lru_locks
748         CNT=$($LCTL get_param -n llite.*.dump_page_cache | wc -l)
749         $MULTIOP $DIR1/$tdir/$tfile Ow8190c
750         $MULTIOP $DIR2/$tdir/$tfile Oz8194w8190c
751         $MULTIOP $DIR1/$tdir/$tfile Oz0r8190c
752         cancel_lru_locks
753         CNT2=$($LCTL get_param -n llite.*.dump_page_cache | wc -l)
754         [[ $CNT2 == $CNT ]] ||
755                 error $((CNT2 - CNT))" page left in cache after lock cancel"
756 }
757 run_test 20 "test extra readahead page left in cache ===="
758
759 cleanup_21() {
760         trap 0
761         umount $DIR1/$tdir
762 }
763
764 test_21() { # Bug 5907
765         test_mkdir $DIR1/$tdir
766         mount /etc $DIR1/$tdir --bind || error "mount failed" # Poor man's mount.
767         trap cleanup_21 EXIT
768         rmdir -v $DIR1/$tdir && error "Removed mounted directory"
769         rmdir -v $DIR2/$tdir && echo "Removed mounted directory from another mountpoint, needs to be fixed"
770         test -d $DIR1/$tdir || error "Mounted directory disappeared"
771         cleanup_21
772         test -d $DIR2/$tdir || test -d $DIR1/$tdir && error "Removed dir still visible after umount"
773         true
774 }
775 run_test 21 " Try to remove mountpoint on another dir ===="
776
777 test_23() { # Bug 5972
778         local at_diff=$(do_facet $SINGLEMDS \
779                 $LCTL get_param -n mdd.*MDT0000*.atime_diff | head -n1)
780         echo "atime should be updated while another read" > $DIR1/$tfile
781
782         # clear the lock(mode: LCK_PW) gotten from creating operation
783         cancel_lru_locks $OSC
784         time1=$(date +%s)
785         echo "now is $time1"
786         sleep $((at_diff + 1))
787
788         echo "starting reads"
789         multiop_bg_pause $DIR1/$tfile or20_c || return 1
790         # with SOM and opencache enabled, we need to close a file and cancel
791         # open lock to get atime propogated to MDS
792         kill -USR1 $! || return 2
793         cancel_lru_locks mdc
794
795         time2=$(stat -c "%X" $DIR/$tfile)
796         echo "new atime is $time2"
797
798         [ $time2 -gt $time1 ] || error "atime was not updated"
799         rm -f $DIR1/$tfile || error "rm -f $DIR1/$tfile failed"
800         true
801 }
802 run_test 23 " others should see updated atime while another read===="
803
804 test_24a() {
805         touch $DIR1/$tfile
806         lfs df || error "lfs df failed"
807         lfs df -ih || error "lfs df -ih failed"
808         lfs df -h $DIR1 || error "lfs df -h $DIR1 failed"
809         lfs df -i $DIR2 || error "lfs df -i $DIR2 failed"
810         lfs df $DIR1/$tfile || error "lfs df $DIR1/$tfile failed"
811         lfs df -ih $DIR2/$tfile || error "lfs df -ih $DIR2/$tfile failed"
812
813         OSC=`lctl dl | awk '/-osc-|OSC.*MNT/ {print $4}' | head -n 1`
814 #       OSC=`lctl dl | awk '/-osc-/ {print $4}' | head -n 1`
815         lctl --device %osc deactivate
816         lfs df -i || error "lfs df -i with deactivated OSC failed"
817         lctl --device %osc activate
818         lfs df || error "lfs df with reactivated OSC failed"
819 }
820 run_test 24a "lfs df [-ih] [path] test ========================="
821
822 test_24b() {
823         touch $DIR1/$tfile
824         fsnum=$(lfs_df | grep -c "summary")
825         [ $fsnum -eq 2 ] || error "lfs df shows $fsnum != 2 filesystems."
826 }
827 run_test 24b "lfs df should show both filesystems ==============="
828
829 test_25a() {
830         local acl=$(lctl get_param -n mdc.*MDT0000-mdc-*.connect_flags |
831                                                                 grep -c acl)
832         [ "$acl" -lt 1 ] && skip "must have acl, skipping" && return
833
834         mkdir -p $DIR1/$tdir
835         touch $DIR1/$tdir/f1 || error "touch $DIR1/$tdir/f1"
836         chmod 0755 $DIR1/$tdir/f1 || error "chmod 0755 $DIR1/$tdir/f1"
837
838         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #1"
839         setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
840                 error "setfacl $DIR2/$tdir #1"
841         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #2"
842         setfacl -m u:$RUNAS_ID:r-x -m g:$RUNAS_GID:r-x $DIR1/$tdir ||
843                 error "setfacl $DIR2/$tdir #2"
844         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #3"
845         setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
846                 error "setfacl $DIR2/$tdir #3"
847         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #4"
848         setfacl -x u:$RUNAS_ID: -x g:$RUNAS_GID: $DIR1/$tdir ||
849                 error "setfacl $DIR2/$tdir #4"
850         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #5"
851
852         rm -rf $DIR1/$tdir
853 }
854 run_test 25a "change ACL on one mountpoint be seen on another ==="
855
856 test_25b() {
857         local acl=$(lctl get_param -n mdc.*MDT0000-mdc-*.connect_flags |
858                                                         grep -c acl)
859         [ "$acl" -lt 1 ] && skip "must have acl, skipping" && return
860
861         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
862
863         rm -rf $DIR1/$tdir
864         $LFS mkdir -i 1 $DIR1/$tdir
865         touch $DIR1/$tdir/f1 || error "touch $DIR1/$tdir/f1"
866         chmod 0755 $DIR1/$tdir/f1 || error "chmod 0755 $DIR1/$tdir/f1"
867
868         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #1"
869         setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
870                 error "setfacl $DIR2/$tdir #1"
871         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #2"
872         setfacl -m u:$RUNAS_ID:r-x -m g:$RUNAS_GID:r-x $DIR1/$tdir ||
873                 error "setfacl $DIR2/$tdir #2"
874         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #3"
875         setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
876                 error "setfacl $DIR2/$tdir #3"
877         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #4"
878         setfacl -x u:$RUNAS_ID: -x g:$RUNAS_GID: $DIR1/$tdir ||
879                 error "setfacl $DIR2/$tdir #4"
880         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #5"
881
882         rm -rf $DIR1/$tdir
883 }
884 run_test 25b "change ACL under remote dir on one mountpoint be seen on another"
885
886 test_26a() {
887         utime $DIR1/f26a -s $DIR2/f26a || error "utime failed for $DIR1/f26a"
888 }
889 run_test 26a "allow mtime to get older"
890
891 test_26b() {
892         touch $DIR1/$tfile
893         sleep 1
894         echo "aaa" >> $DIR1/$tfile
895         sleep 1
896         chmod a+x $DIR2/$tfile
897         mt1=`stat -c %Y $DIR1/$tfile`
898         mt2=`stat -c %Y $DIR2/$tfile`
899
900         if [ x"$mt1" != x"$mt2" ]; then
901                 error "not equal mtime, client1: "$mt1", client2: "$mt2"."
902         fi
903 }
904 run_test 26b "sync mtime between ost and mds"
905
906 test_27() {
907         cancel_lru_locks $OSC
908         lctl clear
909         dd if=/dev/zero of=$DIR2/$tfile bs=$((4096+4))k conv=notrunc count=4 seek=3 &
910         DD2_PID=$!
911         sleep 0.5
912         log "dd 1 started"
913
914         dd if=/dev/zero of=$DIR1/$tfile bs=$((16384-1024))k conv=notrunc count=1 seek=4 &
915         DD1_PID=$!
916         log "dd 2 started"
917
918         sleep 1
919         dd if=/dev/zero of=$DIR1/$tfile bs=8k conv=notrunc count=1 seek=0
920         log "dd 3 finished"
921         lctl set_param -n ldlm.dump_namespaces ""
922         wait $DD1_PID $DD2_PID
923         [ $? -ne 0 ] && lctl dk $TMP/debug || true
924 }
925 run_test 27 "align non-overlapping extent locks from request ==="
926
927 test_28() { # bug 9977
928         skip "echo_client on osc is no longer supported"
929 }
930 run_test 28 "read/write/truncate file with lost stripes"
931
932 test_30() { #b=11110, LU-2523
933         test_mkdir $DIR1/$tdir
934         cp -f /bin/bash $DIR1/$tdir/bash
935         bash -c 'sleep 1; rm -f $DIR2/$tdir/bash; cp /bin/bash $DIR2/$tdir' &
936         $DIR1/$tdir/bash -c 'sleep 2;
937                 openfile -f O_RDONLY /proc/$$/exe >& /dev/null; echo $?'
938         wait
939         true
940 }
941 run_test 30 "recreate file race"
942
943 test_31a() {
944         test_mkdir $DIR1/$tdir
945         local writes=$(LANG=C dd if=/dev/zero of=$DIR/$tdir/$tfile \
946                        count=1 2>&1 | awk 'BEGIN { FS="+" } /out/ {print $1}')
947         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE   0x314
948         lctl set_param fail_loc=0x314
949         local reads=$(LANG=C dd if=$DIR2/$tdir/$tfile of=/dev/null 2>&1 |
950                       awk 'BEGIN { FS="+" } /in/ {print $1}')
951         [ $reads -eq $writes ] || error "read" $reads "blocks, must be" $writes
952 }
953 run_test 31a "voluntary cancel / blocking ast race=============="
954
955 test_31b() {
956         remote_ost || { skip "local OST" && return 0; }
957         remote_ost_nodsh && skip "remote OST w/o dsh" && return 0
958
959         # make sure there is no local locks due to destroy
960         wait_mds_ost_sync || error "wait_mds_ost_sync()"
961         wait_delete_completed || error "wait_delete_completed()"
962
963         test_mkdir $DIR1/$tdir
964         lfs setstripe $DIR/$tdir/$tfile -i 0 -c 1
965         cp /etc/hosts $DIR/$tdir/$tfile
966         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE   0x314
967         lctl set_param fail_loc=0x314
968         #define OBD_FAIL_LDLM_OST_FAIL_RACE      0x316
969         do_facet ost1 lctl set_param fail_loc=0x316
970         # Don't crash kernel
971         cat $DIR2/$tdir/$tfile > /dev/null 2>&1
972         lctl set_param fail_loc=0
973         do_facet ost1 lctl set_param fail_loc=0
974         # cleanup: reconnect the client back
975         df $DIR2
976 }
977 run_test 31b "voluntary OST cancel / blocking ast race=============="
978
979 #LU-14949 - multi-client version of the test 31r in sanity.
980 test_31r() {
981         touch $DIR/$tfile.target
982         touch $DIR/$tfile.source
983
984         ls -l $DIR/$tfile.target # cache it for sure
985
986         #OBD_FAIL_LLITE_OPEN_DELAY 0x1419
987         $LCTL set_param fail_loc=0x1419 fail_val=3
988         cat $DIR/$tfile.target &
989         CATPID=$!
990
991         # Guarantee open is waiting before we get here
992         sleep 1
993         mv $DIR2/$tfile.source $DIR2/$tfile.target
994
995         wait $CATPID
996         RC=$?
997         if [[ $RC -ne 0 ]]; then
998                 error "open with cat failed, rc=$RC"
999         fi
1000 }
1001 run_test 31r "open-rename(replace) race"
1002
1003 test_32b() { # bug 11270
1004         remote_ost_nodsh && skip "remote OST with nodsh" && return
1005
1006         local node
1007         local facets=$(get_facets OST)
1008         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
1009
1010         save_lustre_params client "osc.*.contention_seconds" > $p
1011         save_lustre_params $facets \
1012                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
1013         save_lustre_params $facets \
1014                 "ldlm.namespaces.filter-*.contended_locks" >> $p
1015         save_lustre_params $facets \
1016                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
1017         clear_stats $OSC.*.${OSC}_stats
1018
1019         # agressive lockless i/o settings
1020         do_nodes $(comma_list $(osts_nodes)) \
1021                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
1022                         ldlm.namespaces.filter-*.contended_locks=0 \
1023                         ldlm.namespaces.filter-*.contention_seconds=60"
1024         lctl set_param -n $OSC.*.contention_seconds=60
1025         for i in {1..5}; do
1026                 dd if=/dev/zero of=$DIR1/$tfile bs=4k count=1 conv=notrunc > \
1027                         /dev/null 2>&1
1028                 dd if=/dev/zero of=$DIR2/$tfile bs=4k count=1 conv=notrunc > \
1029                         /dev/null 2>&1
1030         done
1031         [ $(calc_stats $OSC.*.${OSC}_stats lockless_write_bytes) -ne 0 ] ||
1032                 error "lockless i/o was not triggered"
1033         # disable lockless i/o (it is disabled by default)
1034         do_nodes $(comma_list $(osts_nodes)) \
1035                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
1036                         ldlm.namespaces.filter-*.contended_locks=32 \
1037                         ldlm.namespaces.filter-*.contention_seconds=0"
1038         # set contention_seconds to 0 at client too, otherwise Lustre still
1039         # remembers lock contention
1040         lctl set_param -n $OSC.*.contention_seconds=0
1041         clear_stats $OSC.*.${OSC}_stats
1042         for i in {1..1}; do
1043                 dd if=/dev/zero of=$DIR1/$tfile bs=4k count=1 conv=notrunc > \
1044                         /dev/null 2>&1
1045                 dd if=/dev/zero of=$DIR2/$tfile bs=4k count=1 conv=notrunc > \
1046                         /dev/null 2>&1
1047         done
1048         [ $(calc_stats $OSC.*.${OSC}_stats lockless_write_bytes) -eq 0 ] ||
1049                 error "lockless i/o works when disabled"
1050         rm -f $DIR1/$tfile
1051         restore_lustre_params <$p
1052         rm -f $p
1053 }
1054 # Disable test 32b prior to full removal
1055 #run_test 32b "lockless i/o"
1056
1057 print_jbd_stat () {
1058         local mdts=$(get_facets MDS)
1059         local stat=0
1060         local varsvc
1061         local dev
1062         local mds
1063
1064         for mds in ${mdts//,/ }; do
1065                 varsvc=${mds}_svc
1066
1067                 dev=$(basename $(do_facet $mds "lctl get_param -n \
1068                         osd*.${!varsvc}.mntdev | xargs readlink -f"))
1069                 val=$(do_facet $mds "cat /proc/fs/jbd*/${dev}{,:*,-*}/info \
1070                         2>/dev/null | head -n1")
1071                 val=${val%% *};
1072                 stat=$((stat + val))
1073         done
1074         echo $stat
1075 }
1076
1077 # commit on sharing tests
1078 test_33a() {
1079         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1080
1081         [ $CLIENTCOUNT -lt 2 ] &&
1082                 skip "Need two or more clients, have $CLIENTCOUNT"
1083
1084         local nfiles=${TEST33_NFILES:-10000}
1085         local param_file=$TMP/$tfile-params
1086         local COS
1087         local jbdold="N/A"
1088         local jbdnew="N/A"
1089         local jbd
1090
1091         save_lustre_params $(get_facets MDS) \
1092                 "mdt.*.commit_on_sharing" > $param_file
1093
1094         for COS in 0 1; do
1095                 do_facet $SINGLEMDS lctl set_param mdt.*.commit_on_sharing=$COS
1096                 avgjbd=0
1097                 avgtime=0
1098                 for i in 1 2 3; do
1099                         do_nodes $CLIENT1,$CLIENT2 "mkdir -p $DIR1/$tdir-\\\$(hostname)-$i"
1100
1101                 [ "$mds1_FSTYPE" = ldiskfs ] && jbdold=$(print_jbd_stat)
1102                 echo "=== START createmany old: $jbdold transaction"
1103                 local elapsed=$(do_and_time "do_nodes $CLIENT1,$CLIENT2 createmany -o $DIR1/$tdir-\\\$(hostname)-$i/f- -r$DIR2/$tdir-\\\$(hostname)-$i/f- $nfiles > /dev/null 2>&1")
1104                 [ "$mds1_FSTYPE" = ldiskfs ] && jbdnew=$(print_jbd_stat)
1105                 [ "$mds1_FSTYPE" = ldiskfs ] && jbd=$(( jbdnew - jbdold ))
1106                 echo "=== END   createmany new: $jbdnew transaction :  $jbd transactions  nfiles $nfiles time $elapsed COS=$COS"
1107                 [ "$mds1_FSTYPE" = ldiskfs ] && avgjbd=$(( avgjbd + jbd ))
1108                 avgtime=$(( avgtime + elapsed ))
1109                 done
1110         eval cos${COS}_jbd=$((avgjbd / 3))
1111         eval cos${COS}_time=$((avgtime / 3))
1112         done
1113
1114         echo "COS=0 transactions (avg): $cos0_jbd  time (avg): $cos0_time"
1115         echo "COS=1 transactions (avg): $cos1_jbd  time (avg): $cos1_time"
1116         [ "$cos0_jbd" != 0 ] &&
1117                 echo "COS=1 vs COS=0 jbd:  $((((cos1_jbd/cos0_jbd - 1)) * 100 )) %"
1118         [ "$cos0_time" != 0 ] &&
1119                 echo "COS=1 vs COS=0 time: $((((cos1_time/cos0_time - 1)) * 100 )) %"
1120
1121         restore_lustre_params < $param_file
1122         rm -f $param_file
1123         return 0
1124 }
1125 run_test 33a "commit on sharing, cross crete/delete, 2 clients, benchmark"
1126
1127 # commit on sharing tests
1128 test_33b() {
1129         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1130
1131         [ $CLIENTCOUNT -ge 2 ] ||
1132                 { skip "Need two or more clients, have $CLIENTCOUNT" &&
1133                                                                 return 0; }
1134         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1135
1136         local nfiles=${TEST33_NFILES:-10000}
1137         local param_file=$TMP/$tfile-params
1138
1139         save_lustre_params $(get_facets MDS) \
1140                 "mdt.*.commit_on_sharing" > $param_file
1141
1142         local COS
1143         local jbdold
1144         local jbdnew
1145         local jbd
1146         local MDTIDX=1
1147
1148         for COS in 0 1; do
1149                 do_facet $SINGLEMDS lctl set_param mdt.*.commit_on_sharing=$COS
1150                 avgjbd=0
1151                 avgtime=0
1152                 for i in 1 2 3; do
1153                         do_node $CLIENT1 "$LFS mkdir -i $MDTIDX \
1154                                           $DIR1/$tdir-\\\$(hostname)-$i"
1155
1156                         jbdold=$(print_jbd_stat)
1157                         echo "=== START createmany old: $jbdold transaction"
1158                         local elapsed=$(do_and_time "do_nodes $CLIENT1,$CLIENT2\
1159                                 createmany -o $DIR1/$tdir-\\\$(hostname)-$i/f- \
1160                                 -r$DIR2/$tdir-\\\$(hostname)-$i/f- $nfiles > \
1161                                                                 /dev/null 2>&1")
1162                         jbdnew=$(print_jbd_stat)
1163                         jbd=$(( jbdnew - jbdold ))
1164                         echo "=== END   createmany new: $jbdnew transaction : \
1165                         $jbd transactions nfiles $nfiles time $elapsed COS=$COS"
1166                         avgjbd=$(( avgjbd + jbd ))
1167                         avgtime=$(( avgtime + elapsed ))
1168                 done
1169                 eval cos${COS}_jbd=$((avgjbd / 3))
1170                 eval cos${COS}_time=$((avgtime / 3))
1171         done
1172
1173         echo "COS=0 transactions (avg): $cos0_jbd  time (avg): $cos0_time"
1174         echo "COS=1 transactions (avg): $cos1_jbd  time (avg): $cos1_time"
1175         [ "$cos0_jbd" != 0 ] &&
1176             echo "COS=1 vs COS=0 jbd: $(((cos1_jbd/cos0_jbd - 1) * 100)) %"
1177         [ "$cos0_time" != 0 ] &&
1178             echo "COS=1 vs COS=0 time: $(((cos1_time/cos0_time - 1) * 100)) %"
1179
1180         restore_lustre_params < $param_file
1181         rm -f $param_file
1182         return 0
1183 }
1184 run_test 33b "COS: cross create/delete, 2 clients, benchmark under remote dir"
1185
1186 test_33c() {
1187         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
1188         [ "$MDS1_VERSION" -lt $(version_code 2.7.63) ] &&
1189                 skip "DNE CoS not supported"
1190
1191         # LU-13522
1192         stop mds1
1193         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS || error "start mds1 failed"
1194
1195         local sync_count
1196
1197         mkdir_on_mdt0 $DIR/$tdir
1198         sync_all_data
1199         do_facet mds1 "lctl set_param -n mdt.*.sync_count=0"
1200         # do twice in case transaction is committed before unlock, see LU-8200
1201         for i in 1 2; do
1202                 # remote dir is created on MDT1, which enqueued lock of $tdir on
1203                 # MDT0
1204                 $LFS mkdir -i 1 $DIR/$tdir/remote.$i
1205                 mkdir $DIR/$tdir/local.$i
1206         done
1207         sync_count=$(do_facet mds1 "lctl get_param -n mdt.*MDT0000.sync_count")
1208         echo "sync_count $sync_count"
1209         [ $sync_count -eq 0 ] && error "Sync-Lock-Cancel not triggered"
1210
1211         sync_all_data
1212         do_facet mds1 "lctl set_param -n mdt.*.sync_count=0"
1213         $LFS mkdir -i 1 $DIR/$tdir/remote.3
1214         # during sleep remote mkdir should have been committed and canceled
1215         # remote lock spontaneously, which shouldn't trigger sync
1216         sleep 6
1217         mkdir $DIR/$tdir/local.3
1218         sync_count=$(do_facet mds1 "lctl get_param -n mdt.*MDT0000.sync_count")
1219         echo "sync_count $sync_count"
1220         [ $sync_count -eq 0 ] || error "Sync-Lock-Cancel triggered"
1221 }
1222 run_test 33c "Cancel cross-MDT lock should trigger Sync-Lock-Cancel"
1223
1224 # arg1 is operations done before CoS, arg2 is the operation that triggers CoS
1225 op_trigger_cos() {
1226         local commit_nr
1227         local total=0
1228         local nodes=$(comma_list $(mdts_nodes))
1229
1230         sync_all_data
1231
1232         # trigger CoS twice in case transaction commit before unlock
1233         for i in 1 2; do
1234                 bash -c "$1"
1235                 do_nodes $nodes "lctl set_param -n mdt.*.async_commit_count=0"
1236                 bash -c "$2"
1237                 commit_nr=$(do_nodes $nodes \
1238                         "lctl get_param -n mdt.*.async_commit_count" | calc_sum)
1239                 total=$((total + commit_nr));
1240                 rm -rf $DIR/$tdir
1241                 sync_all_data
1242         done
1243
1244         echo "CoS count $total"
1245         [ $total -gt 0 ] || error "$2 didn't trigger CoS"
1246 }
1247
1248 test_33d() {
1249         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
1250         [ "$MDS1_VERSION" -lt $(version_code 2.7.63) ] &&
1251                 skip "DNE CoS not supported"
1252
1253         # remote directory create
1254         op_trigger_cos "$LFS mkdir -i 0 $DIR/$tdir" "$LFS mkdir -i 1 $DIR/$tdir/subdir"
1255         # remote directory unlink
1256         op_trigger_cos "$LFS mkdir -i 1 $DIR/$tdir" "rmdir $DIR/$tdir"
1257         # striped directory create
1258         op_trigger_cos "mkdir $DIR/$tdir" "$LFS mkdir -c 2 $DIR/$tdir/subdir"
1259         # striped directory setattr
1260         op_trigger_cos "$LFS mkdir -c 2 $DIR/$tdir; touch $DIR/$tdir" \
1261                 "chmod 713 $DIR/$tdir"
1262         # striped directory unlink
1263         op_trigger_cos "$LFS mkdir -c 2 $DIR/$tdir; touch $DIR/$tdir" \
1264                 "rmdir $DIR/$tdir"
1265         # cross-MDT link
1266         op_trigger_cos "$LFS mkdir -c 2 $DIR/$tdir; \
1267                         $LFS mkdir -i 0 $DIR/$tdir/d1; \
1268                         $LFS mkdir -i 1 $DIR/$tdir/d2; \
1269                         touch $DIR/$tdir/d1/tgt" \
1270                 "ln $DIR/$tdir/d1/tgt $DIR/$tdir/d2/src"
1271         # cross-MDT rename
1272         op_trigger_cos "$LFS mkdir -c 2 $DIR/$tdir; \
1273                         $LFS mkdir -i 0 $DIR/$tdir/d1; \
1274                         $LFS mkdir -i 1 $DIR/$tdir/d2; \
1275                         touch $DIR/$tdir/d1/src" \
1276                 "mv $DIR/$tdir/d1/src $DIR/$tdir/d2/tgt"
1277         # migrate
1278         op_trigger_cos "$LFS mkdir -i 0 $DIR/$tdir" \
1279                 "$LFS migrate -m 1 $DIR/$tdir"
1280
1281         return 0
1282 }
1283 run_test 33d "DNE distributed operation should trigger COS"
1284
1285 test_33e() {
1286         [ $CLIENTCOUNT -ge 2 ] ||
1287                 skip "Need two or more clients, have $CLIENTCOUNT"
1288         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
1289         [ "$MDS1_VERSION" -lt $(version_code 2.7.63) ] &&
1290                 skip "DNE CoS not supported"
1291
1292         local client2=${CLIENT2:-$(hostname)}
1293
1294         sync
1295
1296         local nodes=$(comma_list $(mdts_nodes))
1297         do_nodes $nodes "lctl set_param -n mdt.*.async_commit_count=0"
1298
1299         $LFS mkdir -c 2 $DIR/$tdir
1300         mkdir $DIR/$tdir/subdir
1301         echo abc > $DIR/$tdir/$tfile
1302         do_node $client2 echo dfg >> $DIR/$tdir/$tfile
1303         do_node $client2 touch $DIR/$tdir/subdir
1304
1305         local async_commit_count=$(do_nodes $nodes \
1306                 "lctl get_param -n mdt.*.async_commit_count" | calc_sum)
1307         [ $async_commit_count -gt 0 ] && error "CoS triggerred"
1308
1309         return 0
1310 }
1311 run_test 33e "DNE local operation shouldn't trigger COS"
1312
1313 # End commit on sharing tests
1314
1315 get_ost_lock_timeouts() {
1316     local nodes=${1:-$(comma_list $(osts_nodes))}
1317
1318     local locks=$(do_nodes $nodes \
1319         "lctl get_param -n ldlm.namespaces.filter-*.lock_timeouts" | calc_sum)
1320
1321     echo $locks
1322 }
1323
1324 cleanup_34() {
1325         local i
1326         trap 0
1327         do_nodes $(comma_list $(osts_nodes)) \
1328                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1329         for i in $(seq $OSTCOUNT); do
1330                 wait_osc_import_ready client ost$i
1331         done
1332 }
1333
1334 test_34() { #16129
1335         remote_ost_nodsh && skip "remote OST with nodsh" && return
1336         local OPER
1337         local lock_in
1338         local lock_out
1339         trap cleanup_34 EXIT RETURN
1340         for OPER in notimeout timeout ; do
1341                 rm $DIR1/$tfile 2>/dev/null
1342                 lock_in=$(get_ost_lock_timeouts)
1343                 if [ $OPER == "timeout" ] ; then
1344                         for j in `seq $OSTCOUNT`; do
1345                                 #define OBD_FAIL_PTLRPC_HPREQ_TIMEOUT    0x511
1346                                 do_facet ost$j lctl set_param fail_loc=0x511
1347                         done
1348                         echo lock should expire
1349                 else
1350                         for j in `seq $OSTCOUNT`; do
1351                                 #define OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT  0x512
1352                                 do_facet ost$j lctl set_param fail_loc=0x512
1353                         done
1354                         echo lock should not expire
1355                 fi
1356                 echo writing on client1
1357                 dd if=/dev/zero of=$DIR1/$tfile count=100 conv=notrunc > /dev/null 2>&1
1358                 sync &
1359                 echo reading on client2
1360                 dd of=/dev/null if=$DIR2/$tfile > /dev/null 2>&1
1361                 # wait for a lock timeout
1362                 sleep 4
1363                 lock_out=$(get_ost_lock_timeouts)
1364                 if [ $OPER == "timeout" ] ; then
1365                         if [ $lock_in == $lock_out ]; then
1366                                 error "no lock timeout happened"
1367                         else
1368                                 echo "success"
1369                         fi
1370                 else
1371                         if [ $lock_in != $lock_out ]; then
1372                                 error "lock timeout happened"
1373                         else
1374                                 echo "success"
1375                         fi
1376                 fi
1377         done
1378         cleanup_34
1379 }
1380 run_test 34 "no lock timeout under IO"
1381
1382 test_35() { # bug 17645
1383         local generation=[]
1384         local count=0
1385         gen=$(lctl get_param mdc.$FSNAME-MDT*-mdc-*.import | grep generation |
1386               awk '/generation/{print $2}')
1387         for g in $gen; do
1388                 generation[count]=$g
1389                 let count=count+1
1390         done
1391
1392         test_mkdir $MOUNT1/$tdir
1393         cancel_lru_locks mdc
1394
1395         # Let's initiate -EINTR situation by setting fail_loc and take
1396         # write lock on same file from same client. This will not cause
1397         # bl_ast yet as lock is already in local cache.
1398         #define OBD_FAIL_LDLM_INTR_CP_AST        0x317
1399         do_facet client "lctl set_param fail_loc=0x80000317"
1400         local timeout=$(do_facet $SINGLEMDS lctl get_param  -n timeout)
1401         let timeout=timeout*3
1402         local nr=0
1403         while test $nr -lt 10; do
1404                 log "Race attempt $nr"
1405                 local blk1=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
1406                              awk '/ldlm_bl_callback/ { print $2 }')
1407                 test "x$blk1" = "x" && blk1=0
1408                 createmany -o $MOUNT2/$tdir/a 4000 &
1409                 pid1=$!
1410                 sleep 1
1411
1412                 # Let's make conflict and bl_ast
1413                 ls -la $MOUNT1/$tdir > /dev/null &
1414                 pid2=$!
1415
1416                 log "Wait for $pid1 $pid2 for $timeout sec..."
1417                 sleep $timeout
1418                 kill -9 $pid1 $pid2 > /dev/null 2>&1
1419                 wait
1420                 local blk2=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
1421                              awk '/ldlm_bl_callback/ { print $2 }')
1422                 test "x$blk2" = "x" && blk2=0
1423                 test $blk2 -gt $blk1 && break
1424                 rm -fr $MOUNT1/$tdir
1425                 cancel_lru_locks mdc
1426                 let nr=nr+1
1427         done
1428         do_facet client "lctl set_param fail_loc=0x0"
1429         df -h $MOUNT1 $MOUNT2
1430         count=0
1431         gen=$(lctl get_param mdc.$FSNAME-MDT*-mdc-*.import | grep generation |
1432                 awk '/generation/{print $2}')
1433         for g in $gen; do
1434             if ! test "$g" -eq "${generation[count]}"; then
1435                 list=$(lctl list_param mdc.$FSNAME-MDT*-mdc-*.import)
1436                 local c=0
1437                 for imp in $list; do
1438                         if [ $c = $count ]; then
1439                                 break
1440                         fi
1441                         c=c+1
1442                 done
1443                 imp=$(echo "$imp" | awk -F"." '{print $2}')
1444                 error "Eviction happened on import $imp"
1445             fi
1446             let count=count+1
1447         done
1448 }
1449 run_test 35 "-EINTR cp_ast vs. bl_ast race does not evict client"
1450
1451 test_36() { #bug 16417
1452         local SIZE
1453         local SIZE_B
1454         local i
1455
1456         test_mkdir $DIR1/$tdir
1457         $LFS setstripe -c -1 $DIR1/$tdir
1458         i=0
1459         SIZE=50
1460         let SIZE_B=SIZE*1024*1024
1461         sync; sleep 2; sync # wait for delete thread
1462         wait_mds_ost_sync || error "wait_mds_ost_sync failed"
1463         wait_destroy_complete || error "wait_destroy_complete failed"
1464
1465         while [ $i -le 10 ]; do
1466                 lctl mark "start test - cycle ($i)"
1467                 local before=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
1468                 dd if=/dev/zero of=$DIR1/$tdir/$tfile bs=1M count=$SIZE ||
1469                         error "dd $DIR1/$tdir/$tfile ${SIZE}MB failed"
1470                 sync          # sync data from client cache
1471                 sync_all_data # sync data from server cache (delayed allocation)
1472                 sleep 2
1473                 local after_dd=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
1474                 multiop_bg_pause $DIR2/$tdir/$tfile O_r${SIZE_B}c || return 3
1475                 read_pid=$!
1476                 rm -f $DIR1/$tdir/$tfile
1477                 kill -USR1 $read_pid
1478                 wait $read_pid
1479                 sync; sleep 2; sync # Ensure new statfs
1480                 wait_delete_completed
1481                 local after=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
1482                 echo "*** cycle($i) *** before($before) after_dd($after_dd)" \
1483                         "after($after)"
1484                 # this free space! not used
1485                 (( $after_dd <= $after)) ||
1486                         error "space leaked after_dd:$after_dd > after:$after"
1487                 let i=i+1
1488         done
1489 }
1490 run_test 36 "handle ESTALE/open-unlink correctly"
1491
1492 test_37() { # bug 18695
1493         test_mkdir $DIR1/$tdir
1494         multiop_bg_pause $DIR1/$tdir D_c || return 1
1495         MULTIPID=$!
1496         # create large directory (32kB seems enough from e2fsck, ~= 1000 files)
1497         createmany -m $DIR2/$tdir/f 10000
1498         # set mtime/atime backward
1499         touch -t 198001010000 $DIR2/$tdir
1500         kill -USR1 $MULTIPID
1501         nr_files=`lfs find $DIR1/$tdir -type f | wc -l`
1502         [ $nr_files -eq 10000 ] || error "$nr_files != 10000 truncated directory?"
1503
1504 }
1505 run_test 37 "check i_size is not updated for directory on close (bug 18695) =============="
1506
1507 # this should be set to past
1508 TEST_39_MTIME=`date -d "1 year ago" +%s`
1509
1510 # bug 11063
1511 test_39a() {
1512         local client1=${CLIENT1:-`hostname`}
1513         local client2=${CLIENT2:-`hostname`}
1514
1515         do_node $client1 "touch $DIR1/$tfile"
1516
1517         do_node $client1 "touch -m -d @$TEST_39_MTIME $DIR1/$tfile"
1518         local mtime1=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1519         [ "$mtime1" = $TEST_39_MTIME ] || \
1520                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
1521
1522         local d1=`do_node $client1 date +%s`
1523         do_node $client1 'echo hello >> '$DIR1/$tfile
1524         local d2=`do_node $client1 date +%s`
1525
1526         local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1527         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
1528                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
1529
1530         do_node $client1 "mv $DIR1/$tfile $DIR1/$tfile-1"
1531
1532         for (( i=0; i < 2; i++ )) ; do
1533                 local mtime3=`do_node $client2 "stat -c %Y $DIR1/$tfile-1"`
1534                 [ "$mtime2" = "$mtime3" ] || \
1535                         error "mtime ($mtime2) changed (to $mtime3) on rename"
1536
1537                 cancel_lru_locks osc
1538                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
1539         done
1540 }
1541 run_test 39a "test from 11063 =================================="
1542
1543 test_39b() {
1544         local client1=${CLIENT1:-`hostname`}
1545         local client2=${CLIENT2:-`hostname`}
1546
1547         touch $DIR1/$tfile
1548
1549         local mtime1=`stat -c %Y $DIR1/$tfile`
1550         local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1551
1552         sleep 1
1553         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
1554
1555         for (( i=0; i < 2; i++ )) ; do
1556                 local mtime3=`stat -c %Y $DIR1/$tfile`
1557                 local mtime4=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1558
1559                 [ "$mtime3" = "$mtime4" ] || \
1560                         error "different mtime on clients: $mtime3, $mtime4"
1561                 [ "$mtime3" = $TEST_39_MTIME ] || \
1562                         error "lost mtime: $mtime3, should be $TEST_39_MTIME"
1563
1564                 cancel_lru_locks osc
1565                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
1566         done
1567 }
1568 run_test 39b "11063 problem 1 =================================="
1569
1570 test_39c() {
1571         local client1=${CLIENT1:-`hostname`}
1572         local client2=${CLIENT2:-`hostname`}
1573
1574         echo hello > $DIR1/$tfile
1575
1576         local mtime1=`stat -c %Y $DIR1/$tfile`
1577         local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1578         [ "$mtime1" = "$mtime2" ] || \
1579                 error "create: different mtime on clients: $mtime1, $mtime2"
1580
1581         sleep 1
1582         $TRUNCATE $DIR1/$tfile 1
1583
1584         for (( i=0; i < 2; i++ )) ; do
1585                 local mtime3=`stat -c %Y $DIR1/$tfile`
1586                 local mtime4=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1587
1588                 [ "$mtime3" = "$mtime4" ] || \
1589                         error "different mtime on clients: $mtime3, $mtime4"
1590                 [ "$mtime3" -gt $mtime2 ] || \
1591                         error "truncate did not update mtime: $mtime2, $mtime3"
1592
1593                 cancel_lru_locks osc
1594                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
1595         done
1596 }
1597 run_test 39c "check truncate mtime update ======================"
1598
1599 test_39d() { # LU-7310
1600         touch $DIR1/$tfile
1601         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
1602
1603         local mtime1=$(stat -c %Y $DIR2/$tfile)
1604         [ "$mtime1" = $TEST_39_MTIME ] ||
1605                 error "mtime: $mtime1, should be $TEST_39_MTIME"
1606
1607         # force sync write
1608         # define OBD_FAIL_OSC_NO_GRANT 0x411
1609         $LCTL set_param fail_loc=0x411
1610
1611         local d1=$(date +%s)
1612         echo hello >> $DIR1/$tfile
1613         local d2=$(date +%s)
1614
1615         $LCTL set_param fail_loc=0
1616
1617         cancel_lru_locks $OSC
1618
1619         local mtime2=$(stat -c %Y $DIR2/$tfile)
1620         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] ||
1621                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
1622 }
1623 run_test 39d "sync write should update mtime"
1624
1625 pdo_sched() {
1626         # how long 40-47 take with specific delay
1627         # sleep 0.1 # 78s
1628         # sleep 0.2 # 103s
1629         # sleep 0.3 # 124s
1630         sleep 0.5 # 164s
1631 }
1632
1633 # for pdo testing, we must cancel MDT-MDT locks as well as client locks to
1634 # avoid unexpected delays due to previous tests
1635 pdo_lru_clear() {
1636         cancel_lru_locks mdc
1637         do_nodes $(comma_list $(mdts_nodes)) \
1638                 $LCTL set_param -n ldlm.namespaces.*mdt*.lru_size=clear
1639         do_nodes $(comma_list $(mdts_nodes)) \
1640                 $LCTL get_param ldlm.namespaces.*mdt*.lock_unused_count \
1641                         ldlm.namespaces.*mdt*.lock_count | grep -v '=0'
1642 }
1643
1644 # check that pid exists hence second operation wasn't blocked by first one
1645 # if it is so then there is no conflict, return 0
1646 # else second operation is conflicting with first one, return 1
1647 check_pdo_conflict() {
1648         local pid=$1
1649         local conflict=0
1650         pdo_sched # to ensure OP1 is finished on client if OP2 is blocked by OP1
1651         if [[ `ps --pid $pid | wc -l` == 1 ]]; then
1652                 conflict=1
1653                 echo "Conflict"
1654         else
1655                 echo "No conflict"
1656         fi
1657         return $conflict
1658 }
1659
1660 # pdirop tests
1661 # test 40: check non-blocking operations
1662 test_40a() {
1663         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1664
1665         mkdir_on_mdt0 $DIR2/$tdir
1666         pdo_lru_clear
1667 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1668         do_nodes $(comma_list $(mdts_nodes)) \
1669                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1670         mkdir $DIR1/$tdir/$tfile &
1671         PID1=$!; pdo_sched
1672         touch $DIR2/$tdir/$tfile-2
1673         check_pdo_conflict $PID1 || error "create is blocked"
1674         mkdir $DIR2/$tdir/$tfile-3
1675         check_pdo_conflict $PID1 || error "mkdir is blocked"
1676         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
1677         check_pdo_conflict $PID1 || error "link is blocked"
1678         mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
1679         check_pdo_conflict $PID1 || error "rename is blocked"
1680         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
1681         check_pdo_conflict $PID1 || error "getattr is blocked"
1682         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
1683         rmdir $DIR2/$tdir/$tfile-3
1684         check_pdo_conflict $PID1 || error "unlink is blocked"
1685
1686         #  all operations above shouldn't wait the first one
1687         check_pdo_conflict $PID1 || error "parallel operation is blocked"
1688         do_nodes $(comma_list $(mdts_nodes)) \
1689                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1690         wait $PID1
1691         rm -rf $DIR/$tdir
1692         return 0
1693 }
1694 run_test 40a "pdirops: create vs others =============="
1695
1696 test_40b() {
1697         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1698
1699         mkdir_on_mdt0 $DIR2/$tdir
1700         pdo_lru_clear
1701 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1702         do_nodes $(comma_list $(mdts_nodes)) \
1703                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1704         touch $DIR1/$tdir/$tfile &
1705         PID1=$!; pdo_sched
1706         # open|create
1707         touch $DIR2/$tdir/$tfile-2
1708         check_pdo_conflict $PID1 || error "create is blocked"
1709         mkdir $DIR2/$tdir/$tfile-3
1710         check_pdo_conflict $PID1 || error "mkdir is blocked"
1711         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
1712         check_pdo_conflict $PID1 || error "link is blocked"
1713         mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
1714         check_pdo_conflict $PID1 || error "rename is blocked"
1715         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
1716         check_pdo_conflict $PID1 || error "getattr is blocked"
1717         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
1718         rmdir $DIR2/$tdir/$tfile-3
1719         check_pdo_conflict $PID1 || error "unlink is blocked"
1720         # all operations above shouldn't wait the first one
1721
1722         check_pdo_conflict $PID1 || error "parallel operation is blocked"
1723         do_nodes $(comma_list $(mdts_nodes)) \
1724                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1725         wait $PID1
1726         rm -rf $DIR/$tdir
1727         return 0
1728 }
1729 run_test 40b "pdirops: open|create and others =============="
1730
1731 test_40c() {
1732         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1733
1734         mkdir_on_mdt0 $DIR2/$tdir
1735         pdo_lru_clear
1736         touch $DIR1/$tdir/$tfile
1737 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1738         do_nodes $(comma_list $(mdts_nodes)) \
1739                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1740         link $DIR1/$tdir/$tfile $DIR1/$tdir/$tfile-0 &
1741         PID1=$!; pdo_sched
1742         # open|create
1743         touch $DIR2/$tdir/$tfile-2
1744         check_pdo_conflict $PID1 || error "create is blocked"
1745         mkdir $DIR2/$tdir/$tfile-3
1746         check_pdo_conflict $PID1 || error "mkdir is blocked"
1747         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
1748         check_pdo_conflict $PID1 || error "link is blocked"
1749         mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
1750         check_pdo_conflict $PID1 || error "rename is blocked"
1751         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
1752         check_pdo_conflict $PID1 || error "getattr is blocked"
1753         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
1754         rmdir $DIR2/$tdir/$tfile-3
1755         check_pdo_conflict $PID1 || error "unlink is blocked"
1756
1757         # all operations above shouldn't wait the first one
1758         check_pdo_conflict $PID1 || error "parallel operation is blocked"
1759         do_nodes $(comma_list $(mdts_nodes)) \
1760                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1761         wait $PID1
1762         rm -rf $DIR/$tdir
1763         return 0
1764 }
1765 run_test 40c "pdirops: link and others =============="
1766
1767 test_40d() {
1768         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1769
1770         mkdir_on_mdt0 $DIR2/$tdir
1771         pdo_lru_clear
1772         touch $DIR1/$tdir/$tfile
1773 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1774         do_nodes $(comma_list $(mdts_nodes)) \
1775                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1776         rm $DIR1/$tdir/$tfile &
1777         PID1=$!; pdo_sched
1778         # open|create
1779         touch $DIR2/$tdir/$tfile-2
1780         check_pdo_conflict $PID1 || error "create is blocked"
1781         mkdir $DIR2/$tdir/$tfile-3
1782         check_pdo_conflict $PID1 || error "mkdir is blocked"
1783         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
1784         check_pdo_conflict $PID1 || error "link is blocked"
1785         mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
1786         check_pdo_conflict $PID1 || error "rename is blocked"
1787         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
1788         check_pdo_conflict $PID1 || error "getattr is blocked"
1789         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
1790         rmdir $DIR2/$tdir/$tfile-3
1791         check_pdo_conflict $PID1 || error "unlink is blocked"
1792
1793         # all operations above shouldn't wait the first one
1794         check_pdo_conflict $PID1 || error "parallel operation is blocked"
1795         do_nodes $(comma_list $(mdts_nodes)) \
1796                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1797         wait $PID1
1798         return 0
1799 }
1800 run_test 40d "pdirops: unlink and others =============="
1801
1802 test_40e() {
1803         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1804
1805         mkdir_on_mdt0 $DIR2/$tdir
1806         pdo_lru_clear
1807         touch $DIR1/$tdir/$tfile
1808 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1809         do_nodes $(comma_list $(mdts_nodes)) \
1810                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1811         mv $DIR1/$tdir/$tfile $DIR1/$tdir/$tfile-0 &
1812         PID1=$!; pdo_sched
1813         # open|create
1814         touch $DIR2/$tdir/$tfile-2
1815         check_pdo_conflict $PID1 || error "create is blocked"
1816         mkdir $DIR2/$tdir/$tfile-3
1817         check_pdo_conflict $PID1 || error "mkdir is blocked"
1818         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
1819         check_pdo_conflict $PID1 || error "link is blocked"
1820         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
1821         check_pdo_conflict $PID1 || error "getattr is blocked"
1822         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-2
1823         rmdir $DIR2/$tdir/$tfile-3
1824         check_pdo_conflict $PID1 || error "unlink is blocked"
1825
1826        # all operations above shouldn't wait the first one
1827         check_pdo_conflict $PID1 || error "parallel operation is blocked"
1828         do_nodes $(comma_list $(mdts_nodes)) \
1829                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1830         wait $PID1
1831         rm -rf $DIR/$tdir
1832         return 0
1833 }
1834 run_test 40e "pdirops: rename and others =============="
1835
1836 # test 41: create blocking operations
1837 test_41a() {
1838         pdo_lru_clear
1839 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1840         do_nodes $(comma_list $(mdts_nodes)) \
1841                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1842         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
1843         PID1=$! ; pdo_sched
1844         mkdir $DIR2/$tfile &
1845         PID2=$! ; pdo_sched
1846         do_nodes $(comma_list $(mdts_nodes)) \
1847                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1848         check_pdo_conflict $PID1 && { wait $PID1; echo "mkdir isn't blocked"; }
1849         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
1850         rm -rf $DIR/$tfile*
1851         return 0
1852 }
1853 run_test 41a "pdirops: create vs mkdir =============="
1854
1855 test_41b() {
1856         pdo_lru_clear
1857 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1858         do_nodes $(comma_list $(mdts_nodes)) \
1859                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1860         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
1861         PID1=$! ; pdo_sched
1862         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
1863         PID2=$! ; pdo_sched
1864         do_nodes $(comma_list $(mdts_nodes)) \
1865                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1866         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
1867         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
1868         rm -rf $DIR/$tfile*
1869         return 0
1870 }
1871 run_test 41b "pdirops: create vs create =============="
1872
1873 test_41c() {
1874         pdo_lru_clear
1875         touch $DIR1/$tfile-2
1876 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1877         do_nodes $(comma_list $(mdts_nodes)) \
1878                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1879         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
1880         PID1=$! ; pdo_sched
1881         link $DIR2/$tfile-2 $DIR2/$tfile &
1882         PID2=$! ; pdo_sched
1883         do_nodes $(comma_list $(mdts_nodes)) \
1884                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1885         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
1886         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
1887         rm -rf $DIR/$tfile*
1888         return 0
1889 }
1890 run_test 41c "pdirops: create vs link =============="
1891
1892 test_41d() {
1893         pdo_lru_clear
1894 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1895         do_nodes $(comma_list $(mdts_nodes)) \
1896                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1897         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
1898         PID1=$! ; pdo_sched
1899         rm $DIR2/$tfile &
1900         PID2=$! ; pdo_sched
1901         do_nodes $(comma_list $(mdts_nodes)) \
1902                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1903         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
1904         wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
1905         rm -rf $DIR/$tfile*
1906         return 0
1907 }
1908 run_test 41d "pdirops: create vs unlink =============="
1909
1910 test_41e() {
1911         pdo_lru_clear
1912         touch $DIR1/$tfile-2
1913 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1914         do_nodes $(comma_list $(mdts_nodes)) \
1915                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1916         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
1917         PID1=$! ; pdo_sched
1918         mv $DIR2/$tfile-2 $DIR2/$tfile &
1919         PID2=$! ; pdo_sched
1920         do_nodes $(comma_list $(mdts_nodes)) \
1921                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1922         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
1923         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
1924         rm -rf $DIR/$tfile*
1925         return 0
1926 }
1927 run_test 41e "pdirops: create and rename (tgt) =============="
1928
1929 test_41f() {
1930         pdo_lru_clear
1931 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1932         do_nodes $(comma_list $(mdts_nodes)) \
1933                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1934         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
1935         PID1=$! ; pdo_sched
1936         mv $DIR2/$tfile $DIR2/$tfile-2 &
1937         PID2=$! ; pdo_sched
1938         do_nodes $(comma_list $(mdts_nodes)) \
1939                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1940         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
1941         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
1942         rm -rf $DIR/$tfile*
1943         return 0
1944 }
1945 run_test 41f "pdirops: create and rename (src) =============="
1946
1947 test_41g() {
1948         pdo_lru_clear
1949 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1950         do_nodes $(comma_list $(mdts_nodes)) \
1951                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1952         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
1953         PID1=$! ; pdo_sched
1954         stat $DIR2/$tfile > /dev/null &
1955         PID2=$! ; pdo_sched
1956         do_nodes $(comma_list $(mdts_nodes)) \
1957                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1958         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
1959         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
1960         rm -rf $DIR/$tfile*
1961         return 0
1962 }
1963 run_test 41g "pdirops: create vs getattr =============="
1964
1965 test_41h() {
1966         pdo_lru_clear
1967 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1968         do_nodes $(comma_list $(mdts_nodes)) \
1969                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1970         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
1971         PID1=$! ; pdo_sched
1972         ls -lia $DIR2/ > /dev/null &
1973         PID2=$! ; pdo_sched
1974         do_nodes $(comma_list $(mdts_nodes)) \
1975                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1976         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
1977         wait $PID2
1978         rm -rf $DIR/$tfile*
1979         return 0
1980 }
1981 run_test 41h "pdirops: create vs readdir =============="
1982
1983 sub_test_41i() {
1984         local PID1 PID2
1985         local fail_loc="$1"
1986         local ret=0
1987
1988         do_nodes $(comma_list $(mdts_nodes)) \
1989                 "lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null
1990
1991         $MULTIOP $DIR1/$tfile oO_CREAT:O_EXCL:c 2>/dev/null &
1992         PID1=$!
1993         sleep 0.2
1994         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c 2>/dev/null &
1995         PID2=$!
1996
1997         if ! wait $PID1 && ! wait $PID2; then
1998                 echo "Both creates failed (1 should fail, 1 should succeed)"
1999                 ret=1
2000         elif wait $PID1 && wait $PID2; then
2001                 echo "Both creates succeeded (1 should fail, 1 should succeed)"
2002                 ret=2
2003         fi
2004
2005         #Clean
2006         do_nodes $(comma_list $(mdts_nodes)) \
2007                 "lctl set_param -n fail_loc=0x0 || true" &>/dev/null
2008         rm -f $DIR/$tfile
2009
2010         return $ret
2011 }
2012
2013 test_41i() {
2014         (( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
2015                 skip "Need MDS version newer than 2.13.56"
2016         local msg fail_loc
2017
2018 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
2019 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
2020         for fail_loc in "0x80000169" "0x8000016a"; do
2021                 echo "Begin 100 tests with fail_loc=$fail_loc"
2022                 printf "Progress: "
2023                 for i in {1..100}; do
2024                         printf "*"
2025                         msg=$(sub_test_41i "$fail_loc") ||
2026                                 { echo; error "iter=$i : $msg"; }
2027                 done
2028                 echo
2029         done
2030 }
2031 run_test 41i "reint_open: create vs create"
2032
2033
2034 # test 42: unlink and blocking operations
2035 test_42a() {
2036         pdo_lru_clear
2037 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2038         do_nodes $(comma_list $(mdts_nodes)) \
2039                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2040         mkdir $DIR1/$tfile &
2041         PID1=$! ; pdo_sched
2042         mkdir $DIR2/$tfile &
2043         PID2=$! ; pdo_sched
2044         do_nodes $(comma_list $(mdts_nodes)) \
2045                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2046         check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
2047         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
2048         rm -rf $DIR/$tfile*
2049         return 0
2050 }
2051 run_test 42a "pdirops: mkdir vs mkdir =============="
2052
2053 test_42b() {
2054         pdo_lru_clear
2055 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2056         do_nodes $(comma_list $(mdts_nodes)) \
2057                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2058         mkdir $DIR1/$tfile &
2059         PID1=$! ; pdo_sched
2060         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2061         PID2=$! ; pdo_sched
2062         do_nodes $(comma_list $(mdts_nodes)) \
2063                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2064         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2065         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
2066         rm -rf $DIR/$tfile*
2067         return 0
2068 }
2069 run_test 42b "pdirops: mkdir vs create =============="
2070
2071 test_42c() {
2072         pdo_lru_clear
2073         touch $DIR1/$tfile-2
2074 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2075         do_nodes $(comma_list $(mdts_nodes)) \
2076                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2077         mkdir $DIR1/$tfile &
2078         PID1=$! ; pdo_sched
2079         link $DIR2/$tfile-2 $DIR2/$tfile &
2080         PID2=$! ; pdo_sched
2081         do_nodes $(comma_list $(mdts_nodes)) \
2082                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2083         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2084         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
2085         rm -rf $DIR/$tfile*
2086         return 0
2087 }
2088 run_test 42c "pdirops: mkdir vs link =============="
2089
2090 test_42d() {
2091         pdo_lru_clear
2092 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2093         do_nodes $(comma_list $(mdts_nodes)) \
2094                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2095         mkdir $DIR1/$tfile &
2096         PID1=$! ; pdo_sched
2097         rmdir $DIR2/$tfile &
2098         PID2=$! ; pdo_sched
2099         do_nodes $(comma_list $(mdts_nodes)) \
2100                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2101         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2102         wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
2103         rm -rf $DIR/$tfile*
2104         return 0
2105 }
2106 run_test 42d "pdirops: mkdir vs unlink =============="
2107
2108 test_42e() {
2109         pdo_lru_clear
2110         touch $DIR1/$tfile-2
2111 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2112         do_nodes $(comma_list $(mdts_nodes)) \
2113                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2114         mkdir $DIR1/$tfile &
2115         PID1=$! ; pdo_sched
2116         mv -T $DIR2/$tfile-2 $DIR2/$tfile &
2117         PID2=$! ; pdo_sched
2118         do_nodes $(comma_list $(mdts_nodes)) \
2119                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2120         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2121         wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
2122         rm -rf $DIR/$tfile*
2123         return 0
2124 }
2125 run_test 42e "pdirops: mkdir and rename (tgt) =============="
2126
2127 test_42f() {
2128         pdo_lru_clear
2129 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2130         do_nodes $(comma_list $(mdts_nodes)) \
2131                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2132         mkdir $DIR1/$tfile &
2133         PID1=$! ; pdo_sched
2134         mv $DIR2/$tfile $DIR2/$tfile-2 &
2135         PID2=$! ; pdo_sched
2136         do_nodes $(comma_list $(mdts_nodes)) \
2137                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2138         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2139         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2140         rm -rf $DIR/$tfile*
2141         return 0
2142 }
2143 run_test 42f "pdirops: mkdir and rename (src) =============="
2144
2145 test_42g() {
2146         mkdir_on_mdt0 $DIR1/$tdir
2147         pdo_lru_clear
2148 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2149         do_nodes $(comma_list $(mdts_nodes)) \
2150                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2151         mkdir $DIR1/$tdir/$tfile &
2152         PID1=$! ; pdo_sched
2153         stat $DIR2/$tdir/$tfile > /dev/null &
2154         PID2=$! ; pdo_sched
2155         do_nodes $(comma_list $(mdts_nodes)) \
2156                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2157         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2158         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
2159         rm -rf $DIR/$tdir
2160 }
2161 run_test 42g "pdirops: mkdir vs getattr =============="
2162
2163 test_42h() {
2164         pdo_lru_clear
2165 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2166         do_nodes $(comma_list $(mdts_nodes)) \
2167                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2168         mkdir $DIR1/$tfile &
2169         PID1=$! ; pdo_sched
2170         ls -lia $DIR2/ > /dev/null &
2171         PID2=$! ; pdo_sched
2172         do_nodes $(comma_list $(mdts_nodes)) \
2173                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2174         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2175         wait $PID2
2176         rm -rf $DIR/$tfile*
2177         return 0
2178 }
2179 run_test 42h "pdirops: mkdir vs readdir =============="
2180
2181 # test 43: rmdir,mkdir won't return -EEXIST
2182 test_43a() {
2183         for i in {1..1000}; do
2184                 mkdir $DIR1/$tdir || error "mkdir $tdir failed"
2185                 rmdir $DIR2/$tdir || error "rmdir $tdir failed"
2186         done
2187         return 0
2188 }
2189 run_test 43a "rmdir,mkdir doesn't return -EEXIST =============="
2190
2191 test_43b() {
2192         pdo_lru_clear
2193         touch $DIR1/$tfile
2194 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2195         do_nodes $(comma_list $(mdts_nodes)) \
2196                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2197         rm $DIR1/$tfile &
2198         PID1=$! ; pdo_sched
2199         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2200         PID2=$! ; pdo_sched
2201         do_nodes $(comma_list $(mdts_nodes)) \
2202                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2203         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2204         wait $PID2 ; [ $? -eq 0 ] || error "create must succeed"
2205         rm -rf $DIR/$tfile*
2206         return 0
2207 }
2208 run_test 43b "pdirops: unlink vs create =============="
2209
2210 test_43c() {
2211         pdo_lru_clear
2212         touch $DIR1/$tfile
2213         touch $DIR1/$tfile-2
2214 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2215         do_nodes $(comma_list $(mdts_nodes)) \
2216                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2217         rm $DIR1/$tfile &
2218         PID1=$! ; pdo_sched
2219         link $DIR2/$tfile-2 $DIR2/$tfile &
2220         PID2=$! ; pdo_sched
2221         do_nodes $(comma_list $(mdts_nodes)) \
2222                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2223         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2224         wait $PID2 ; [ $? -eq 0 ] || error "link must succeed"
2225         rm -rf $DIR/$tfile*
2226         return 0
2227 }
2228 run_test 43c "pdirops: unlink vs link =============="
2229
2230 test_43d() {
2231         pdo_lru_clear
2232         touch $DIR1/$tfile
2233 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2234         do_nodes $(comma_list $(mdts_nodes)) \
2235                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2236         rm $DIR1/$tfile &
2237         PID1=$! ; pdo_sched
2238         rm $DIR2/$tfile &
2239         PID2=$! ; pdo_sched
2240         do_nodes $(comma_list $(mdts_nodes)) \
2241                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2242         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2243         wait $PID2 ; [ $? -ne 0 ] || error "unlink must fail"
2244         rm -rf $DIR/$tfile*
2245         return 0
2246 }
2247 run_test 43d "pdirops: unlink vs unlink =============="
2248
2249 test_43e() {
2250         pdo_lru_clear
2251         touch $DIR1/$tfile
2252         touch $DIR1/$tfile-2
2253 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2254         do_nodes $(comma_list $(mdts_nodes)) \
2255                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2256         rm $DIR1/$tfile &
2257         PID1=$! ; pdo_sched
2258         mv -u $DIR2/$tfile-2 $DIR2/$tfile &
2259         PID2=$! ; pdo_sched
2260         do_nodes $(comma_list $(mdts_nodes)) \
2261                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2262         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2263         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2264         rm -rf $DIR/$tfile*
2265         return 0
2266 }
2267 run_test 43e "pdirops: unlink and rename (tgt) =============="
2268
2269 test_43f() {
2270         pdo_lru_clear
2271         touch $DIR1/$tfile
2272 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2273         do_nodes $(comma_list $(mdts_nodes)) \
2274                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2275         rm $DIR1/$tfile &
2276         PID1=$! ; pdo_sched
2277         mv $DIR2/$tfile $DIR2/$tfile-2 &
2278         PID2=$! ; pdo_sched
2279         do_nodes $(comma_list $(mdts_nodes)) \
2280                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2281         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2282         wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
2283         rm -rf $DIR/$tfile*
2284         return 0
2285 }
2286 run_test 43f "pdirops: unlink and rename (src) =============="
2287
2288 test_43g() {
2289         pdo_lru_clear
2290         touch $DIR1/$tfile
2291 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2292         do_nodes $(comma_list $(mdts_nodes)) \
2293                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2294         rm $DIR1/$tfile &
2295         PID1=$! ; pdo_sched
2296         stat $DIR2/$tfile > /dev/null &
2297         PID2=$! ; pdo_sched
2298         do_nodes $(comma_list $(mdts_nodes)) \
2299                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2300         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2301         wait $PID2 ; [ $? -ne 0 ] || error "stat must fail"
2302         rm -rf $DIR/$tfile*
2303         return 0
2304 }
2305 run_test 43g "pdirops: unlink vs getattr =============="
2306
2307 test_43h() {
2308         pdo_lru_clear
2309         touch $DIR1/$tfile
2310 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2311         do_nodes $(comma_list $(mdts_nodes)) \
2312                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2313         rm $DIR1/$tfile &
2314         PID1=$! ; pdo_sched
2315         ls -lia $DIR2/ > /dev/null &
2316         PID2=$! ; pdo_sched
2317         do_nodes $(comma_list $(mdts_nodes)) \
2318                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2319         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2320         wait $PID2
2321         rm -rf $DIR/$tfile*
2322         return 0
2323 }
2324 run_test 43h "pdirops: unlink vs readdir =============="
2325
2326 test_43i() {
2327         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2328         pdo_lru_clear
2329         touch $DIR1/$tfile
2330 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2331         do_nodes $(comma_list $(mdts_nodes)) \
2332                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2333         rm $DIR1/$tfile &
2334         PID1=$! ; pdo_sched
2335         $LFS mkdir -i 1 $DIR2/$tfile &
2336         PID2=$! ; pdo_sched
2337         do_nodes $(comma_list $(mdts_nodes)) \
2338                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2339         check_pdo_conflict $PID1 &&
2340                 { wait $PID1; error "remote mkdir isn't blocked"; }
2341         wait $PID2 ; [ $? -eq 0 ] || error "remote mkdir must succeed"
2342         rm -rf $DIR/$tfile*
2343         return 0
2344 }
2345 run_test 43i "pdirops: unlink vs remote mkdir"
2346
2347 test_43j() {
2348         [[ $MDS1_VERSION -lt $(version_code 2.13.52) ]] &&
2349                 skip "Need MDS version newer than 2.13.52"
2350
2351         mkdir_on_mdt0 $DIR1/$tdir
2352         for i in {1..100}; do
2353 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_CREATE_RACE         0x167
2354                 do_nodes $(comma_list $(mdts_nodes)) \
2355                         "lctl set_param -n fail_loc=0x80000167 2>/dev/null ||
2356                                 true"
2357                 OK=0
2358                 mkdir $DIR1/$tdir/sub &
2359                 PID1=$!
2360                 mkdir $DIR2/$tdir/sub && ((OK++))
2361                 wait $PID1 && ((OK++))
2362                 (( OK == 1 )) || error "exactly one mkdir should succeed"
2363
2364                 rmdir $DIR1/$tdir/sub || error "rmdir failed"
2365         done
2366         return 0
2367 }
2368 run_test 43j "racy mkdir return EEXIST =============="
2369
2370 sub_test_43k() {
2371         local PID1 PID2
2372         local fail_loc="$1"
2373         local ret=0
2374
2375         # We test in a separate directory to be able to unblock server thread in
2376         # cfs_race() if LCK_PW is taken on the parent by mdt_reint_unlink.
2377         test_mkdir $DIR2/$tdir
2378         touch $DIR2/$tdir/$tfile
2379         pdo_lru_clear
2380
2381         do_nodes $(comma_list $(mdts_nodes)) \
2382                 "lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null
2383         echo content > $DIR1/$tdir/$tfile & PID1=$!
2384         pdo_sched
2385         multiop $DIR2/$tdir/$tfile u & PID2=$!
2386
2387         wait $PID1 ||
2388                 { ret=$?; \
2389                 echo -n "overwriting $tfile should succeed (err=$ret); "; }
2390         wait $PID2 ||
2391                 { ret=$?; \
2392                 echo -n "unlinking $tfile should succeed (err=$ret);"; }
2393
2394         #Clean
2395         do_nodes $(comma_list $(mdts_nodes)) \
2396                 "lctl set_param -n fail_loc=0x0 || true" &>/dev/null
2397         rm -rf $DIR/$tdir
2398
2399         return $ret
2400 }
2401
2402 test_43k() {
2403         (( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
2404                 skip "Need MDS version newer than 2.13.56"
2405         local msg fail_loc
2406
2407 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
2408 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
2409         for fail_loc in "0x80000169" "0x8000016a"; do
2410                 echo "Begin 100 tests with fail_loc=$fail_loc"
2411                 printf "Progress: "
2412                 for i in {1..100}; do
2413                         printf "*"
2414                         msg=$(sub_test_43k "$fail_loc") ||
2415                                 { echo; error "iter=$i : $msg"; }
2416                 done
2417                 echo
2418         done
2419
2420         #Clean
2421         reset_fail_loc
2422
2423         return 0
2424 }
2425 run_test 43k "unlink vs create"
2426
2427 # test 44: rename tgt and blocking operations
2428 test_44a() {
2429         pdo_lru_clear
2430         touch $DIR1/$tfile-2
2431 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2   0x146
2432         do_nodes $(comma_list $(mdts_nodes)) \
2433                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2434         mv $DIR1/$tfile-2 $DIR1/$tfile &
2435         PID1=$! ; pdo_sched
2436         mkdir $DIR2/$tfile &
2437         PID2=$! ; pdo_sched
2438         do_nodes $(comma_list $(mdts_nodes)) \
2439                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2440         check_pdo_conflict $PID1 && { wait $PID1; date;error "mkdir isn't blocked"; }
2441         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
2442         date
2443         rm -rf $DIR/$tfile*
2444         return 0
2445 }
2446 run_test 44a "pdirops: rename tgt vs mkdir =============="
2447
2448 test_44b() {
2449         pdo_lru_clear
2450         touch $DIR1/$tfile-2
2451 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2452         do_nodes $(comma_list $(mdts_nodes)) \
2453                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2454         mv $DIR1/$tfile-2 $DIR1/$tfile &
2455         PID1=$! ; pdo_sched
2456         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2457         PID2=$! ; pdo_sched
2458         do_nodes $(comma_list $(mdts_nodes)) \
2459                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2460         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2461         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
2462         rm -rf $DIR/$tfile*
2463         return 0
2464 }
2465 run_test 44b "pdirops: rename tgt vs create =============="
2466
2467 test_44c() {
2468         pdo_lru_clear
2469         touch $DIR1/$tfile-2
2470         touch $DIR1/$tfile-3
2471 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2472         do_nodes $(comma_list $(mdts_nodes)) \
2473                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2474         mv $DIR1/$tfile-2 $DIR1/$tfile &
2475         PID1=$! ; pdo_sched
2476         link $DIR2/$tfile-3 $DIR2/$tfile &
2477         PID2=$! ; pdo_sched
2478         do_nodes $(comma_list $(mdts_nodes)) \
2479                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2480         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2481         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
2482         rm -rf $DIR/$tfile*
2483         return 0
2484 }
2485 run_test 44c "pdirops: rename tgt vs link =============="
2486
2487 test_44d() {
2488         pdo_lru_clear
2489         touch $DIR1/$tfile-2
2490 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2491         do_nodes $(comma_list $(mdts_nodes)) \
2492                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2493         mv $DIR1/$tfile-2 $DIR1/$tfile &
2494         PID1=$! ; pdo_sched
2495         rm $DIR2/$tfile &
2496         PID2=$! ; pdo_sched
2497         do_nodes $(comma_list $(mdts_nodes)) \
2498                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2499         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2500         wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
2501         rm -rf $DIR/$tfile*
2502         return 0
2503 }
2504 run_test 44d "pdirops: rename tgt vs unlink =============="
2505
2506 test_44e() {
2507         pdo_lru_clear
2508         touch $DIR1/$tfile
2509         touch $DIR1/$tfile-2
2510         touch $DIR1/$tfile-3
2511 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2512         do_nodes $(comma_list $(mdts_nodes)) \
2513                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2514         mv $DIR1/$tfile-2 $DIR1/$tfile &
2515         PID1=$! ; pdo_sched
2516         mv $DIR2/$tfile-3 $DIR2/$tfile &
2517         PID2=$! ; pdo_sched
2518         do_nodes $(comma_list $(mdts_nodes)) \
2519                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2520         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2521         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2522         rm -rf $DIR/$tfile*
2523         return 0
2524 }
2525 run_test 44e "pdirops: rename tgt and rename (tgt) =============="
2526
2527 test_44f() {
2528         pdo_lru_clear
2529         touch $DIR1/$tfile-2
2530         touch $DIR1/$tfile-3
2531 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2532         do_nodes $(comma_list $(mdts_nodes)) \
2533                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2534         mv $DIR1/$tfile-2 $DIR1/$tfile &
2535         PID1=$! ; pdo_sched
2536         mv $DIR2/$tfile $DIR2/$tfile-3 &
2537         PID2=$! ; pdo_sched
2538         do_nodes $(comma_list $(mdts_nodes)) \
2539                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2540         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2541         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2542         rm -rf $DIR/$tfile*
2543         return 0
2544 }
2545 run_test 44f "pdirops: rename tgt and rename (src) =============="
2546
2547 test_44g() {
2548         pdo_lru_clear
2549         touch $DIR1/$tfile-2
2550 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2551         do_nodes $(comma_list $(mdts_nodes)) \
2552                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2553         mv $DIR1/$tfile-2 $DIR1/$tfile &
2554         PID1=$! ; pdo_sched
2555         stat $DIR2/$tfile > /dev/null &
2556         PID2=$! ; pdo_sched
2557         do_nodes $(comma_list $(mdts_nodes)) \
2558                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2559         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2560         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
2561         rm -rf $DIR/$tfile*
2562         return 0
2563 }
2564 run_test 44g "pdirops: rename tgt vs getattr =============="
2565
2566 test_44h() {
2567         pdo_lru_clear
2568         touch $DIR1/$tfile-2
2569 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2570         do_nodes $(comma_list $(mdts_nodes)) \
2571                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2572         mv $DIR1/$tfile-2 $DIR1/$tfile &
2573         PID1=$! ; pdo_sched
2574         ls -lia $DIR2/ > /dev/null &
2575         PID2=$! ; pdo_sched
2576         do_nodes $(comma_list $(mdts_nodes)) \
2577                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2578         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2579         wait $PID2
2580         rm -rf $DIR/$tfile*
2581         return 0
2582 }
2583 run_test 44h "pdirops: rename tgt vs readdir =============="
2584
2585 # test 44: rename tgt and blocking operations
2586 test_44i() {
2587         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2588         pdo_lru_clear
2589         touch $DIR1/$tfile-2
2590 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2   0x146
2591         do_nodes $(comma_list $(mdts_nodes)) \
2592                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2593         mv $DIR1/$tfile-2 $DIR1/$tfile &
2594         PID1=$! ; pdo_sched
2595         $LFS mkdir -i 1 $DIR2/$tfile &
2596         PID2=$! ; pdo_sched
2597         do_nodes $(comma_list $(mdts_nodes)) \
2598                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2599         check_pdo_conflict $PID1 && { wait $PID1;
2600                                 error "remote mkdir isn't blocked"; }
2601         wait $PID2 ; [ $? -ne 0 ] || error "remote mkdir must fail"
2602         rm -rf $DIR/$tfile*
2603         return 0
2604 }
2605 run_test 44i "pdirops: rename tgt vs remote mkdir"
2606
2607 # test 45: rename,mkdir doesn't fail with -EEXIST
2608 test_45a() {
2609         for i in {1..1000}; do
2610                 mkdir $DIR1/$tdir || error "mkdir $tdir failed"
2611                 mrename $DIR2/$tdir $DIR2/$tdir.$i > /dev/null ||
2612                         error "mrename to $tdir.$i failed"
2613         done
2614         rm -rf $DIR/$tdir*
2615         return 0
2616 }
2617 run_test 45a "rename,mkdir doesn't return -EEXIST =============="
2618
2619 test_45b() {
2620         pdo_lru_clear
2621         touch $DIR1/$tfile
2622 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2623         do_nodes $(comma_list $(mdts_nodes)) \
2624                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2625         mv $DIR1/$tfile $DIR1/$tfile-2 &
2626         PID1=$! ; pdo_sched
2627         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2628         PID2=$! ; pdo_sched
2629         do_nodes $(comma_list $(mdts_nodes)) \
2630                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2631         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2632         wait $PID2 ; [ $? -eq 0 ] || error "create must succeed"
2633         rm -rf $DIR/$tfile*
2634         return 0
2635 }
2636 run_test 45b "pdirops: rename src vs create =============="
2637
2638 test_45c() {
2639         pdo_lru_clear
2640         touch $DIR1/$tfile
2641         touch $DIR1/$tfile-3
2642 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2643         do_nodes $(comma_list $(mdts_nodes)) \
2644                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2645         mv $DIR1/$tfile $DIR1/$tfile-2 &
2646         PID1=$! ; pdo_sched
2647         link $DIR2/$tfile-3 $DIR2/$tfile &
2648         PID2=$! ; pdo_sched
2649         do_nodes $(comma_list $(mdts_nodes)) \
2650                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2651         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2652         wait $PID2 ; [ $? -eq 0 ] || error "link must succeed"
2653         rm -rf $DIR/$tfile*
2654         return 0
2655 }
2656 run_test 45c "pdirops: rename src vs link =============="
2657
2658 test_45d() {
2659         pdo_lru_clear
2660         touch $DIR1/$tfile
2661 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2662         do_nodes $(comma_list $(mdts_nodes)) \
2663                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2664         mv $DIR1/$tfile $DIR1/$tfile-2 &
2665         PID1=$! ; pdo_sched
2666         rm $DIR2/$tfile &
2667         PID2=$! ; pdo_sched
2668         do_nodes $(comma_list $(mdts_nodes)) \
2669                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2670         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2671         wait $PID2 ; [ $? -ne 0 ] || error "unlink must fail"
2672         rm -rf $DIR/$tfile*
2673         return 0
2674 }
2675 run_test 45d "pdirops: rename src vs unlink =============="
2676
2677 test_45e() {
2678         pdo_lru_clear
2679         touch $DIR1/$tfile
2680         touch $DIR1/$tfile-3
2681 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2682         do_nodes $(comma_list $(mdts_nodes)) \
2683                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2684         mv $DIR1/$tfile $DIR1/$tfile-2 &
2685         PID1=$! ; pdo_sched
2686         mv $DIR2/$tfile-3 $DIR2/$tfile &
2687         PID2=$! ; pdo_sched
2688         do_nodes $(comma_list $(mdts_nodes)) \
2689                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2690         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2691         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2692         rm -rf $DIR/$tfile*
2693         return 0
2694 }
2695 run_test 45e "pdirops: rename src and rename (tgt) =============="
2696
2697 test_45f() {
2698         pdo_lru_clear
2699         touch $DIR1/$tfile
2700 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2701         do_nodes $(comma_list $(mdts_nodes)) \
2702                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2703         mv $DIR1/$tfile $DIR1/$tfile-2 &
2704         PID1=$! ; pdo_sched
2705         mv $DIR2/$tfile $DIR2/$tfile-3 &
2706         PID2=$! ; pdo_sched
2707         do_nodes $(comma_list $(mdts_nodes)) \
2708                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2709         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2710         wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
2711         rm -rf $DIR/$tfile*
2712         return 0
2713 }
2714 run_test 45f "pdirops: rename src and rename (src) =============="
2715
2716 test_45g() {
2717         pdo_lru_clear
2718         touch $DIR1/$tfile
2719 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2720         do_nodes $(comma_list $(mdts_nodes)) \
2721                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2722         mv $DIR1/$tfile $DIR1/$tfile-2 &
2723         PID1=$! ; pdo_sched
2724         stat $DIR2/$tfile > /dev/null &
2725         PID2=$! ; pdo_sched
2726         do_nodes $(comma_list $(mdts_nodes)) \
2727                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2728         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2729         wait $PID2 ; [ $? -ne 0 ] || error "stat must fail"
2730         rm -rf $DIR/$tfile*
2731         return 0
2732 }
2733 run_test 45g "pdirops: rename src vs getattr =============="
2734
2735 test_45h() {
2736         pdo_lru_clear
2737         touch $DIR1/$tfile
2738 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2739         do_nodes $(comma_list $(mdts_nodes)) \
2740                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2741         mv $DIR1/$tfile $DIR1/$tfile-2 &
2742         PID1=$! ; pdo_sched
2743         ls -lia $DIR2/ > /dev/null &
2744         do_nodes $(comma_list $(mdts_nodes)) \
2745                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2746         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2747         wait $PID2
2748         rm -rf $DIR/$tfile*
2749         return 0
2750 }
2751 run_test 45h "pdirops: unlink vs readdir =============="
2752
2753 test_45i() {
2754         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2755         pdo_lru_clear
2756         touch $DIR1/$tfile
2757 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2758         do_nodes $(comma_list $(mdts_nodes)) \
2759                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2760         mv $DIR1/$tfile $DIR1/$tfile-2 &
2761         PID1=$! ; pdo_sched
2762         $LFS mkdir -i 1 $DIR2/$tfile &
2763         PID2=$! ; pdo_sched
2764         do_nodes $(comma_list $(mdts_nodes)) \
2765                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2766         check_pdo_conflict $PID1 && { wait $PID1;
2767                                 error "create remote dir isn't blocked"; }
2768         wait $PID2 ; [ $? -eq 0 ] || error "create remote dir must succeed"
2769         rm -rf $DIR/$tfile*
2770         return 0
2771 }
2772 run_test 45i "pdirops: rename src vs remote mkdir"
2773
2774 sub_test_45j() {
2775         local PID1 PID2
2776         local fail_loc="$1"
2777         local ret=0
2778
2779         # We test in a sparate directory to be able to unblock server thread in
2780         # cfs_race if LCK_PW is taken on the parent by mdt_reint_rename.
2781         test_mkdir $DIR2/$tdir
2782         echo file1 > $DIR2/$tdir/$tfile
2783         echo file2 > $DIR2/$tdir/$tfile-2
2784         pdo_lru_clear
2785
2786         do_nodes $(comma_list $(mdts_nodes)) \
2787                 "lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null
2788
2789         cat $DIR1/$tdir/$tfile >/dev/null &
2790         PID1=$!
2791         pdo_sched
2792         mrename $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile > /dev/null &
2793         PID2=$!
2794
2795         wait $PID1 ||
2796                 { ret=$?; echo -n "cat $tfile should succeed (err=$ret); "; }
2797         wait $PID2 ||
2798                 { ret=$?; \
2799                 echo -n "mrename $tfile-2 to $tfile failed (err=$ret);"; }
2800
2801         #Clean
2802         do_nodes $(comma_list $(mdts_nodes)) \
2803                 "lctl set_param -n fail_loc=0x0 || true" &>/dev/null
2804         rm -rf $DIR/$tdir
2805
2806         return $ret
2807 }
2808
2809 test_45j() {
2810         (( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
2811                 skip "Need MDS version newer than 2.13.56"
2812         local msg fail_loc
2813
2814 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
2815 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
2816         for fail_loc in "0x80000169" "0x8000016a"; do
2817                 echo "Begin 100 tests with fail_loc=$fail_loc"
2818                 printf "Progress: "
2819                 for i in {1..100}; do
2820                         printf "*"
2821                         msg=$(sub_test_45j "$fail_loc") ||
2822                                 { echo; error "iter=$i : $msg"; }
2823                 done
2824                 echo
2825         done
2826 }
2827 run_test 45j "read vs rename =============="
2828
2829 # test 46: link and blocking operations
2830 test_46a() {
2831         pdo_lru_clear
2832         touch $DIR1/$tfile-2
2833 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2834         do_nodes $(comma_list $(mdts_nodes)) \
2835                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2836         link $DIR1/$tfile-2 $DIR1/$tfile &
2837         PID1=$! ; pdo_sched
2838         mkdir $DIR2/$tfile &
2839         PID2=$! ; pdo_sched
2840         do_nodes $(comma_list $(mdts_nodes)) \
2841                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2842         check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
2843         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
2844         rm -rf $DIR/$tfile*
2845         return 0
2846 }
2847 run_test 46a "pdirops: link vs mkdir =============="
2848
2849 test_46b() {
2850         pdo_lru_clear
2851         touch $DIR1/$tfile-2
2852 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2853         do_nodes $(comma_list $(mdts_nodes)) \
2854                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2855         link $DIR1/$tfile-2 $DIR1/$tfile &
2856         PID1=$! ; pdo_sched
2857         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2858         PID2=$! ; pdo_sched
2859         do_nodes $(comma_list $(mdts_nodes)) \
2860                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2861         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2862         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
2863         rm -rf $DIR/$tfile*
2864         return 0
2865 }
2866 run_test 46b "pdirops: link vs create =============="
2867
2868 test_46c() {
2869         pdo_lru_clear
2870         touch $DIR1/$tfile-2
2871 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2872         do_nodes $(comma_list $(mdts_nodes)) \
2873                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2874         link $DIR1/$tfile-2 $DIR1/$tfile &
2875         PID1=$! ; pdo_sched
2876         link $DIR2/$tfile $DIR2/$tfile &
2877         PID2=$! ; pdo_sched
2878         do_nodes $(comma_list $(mdts_nodes)) \
2879                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2880         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2881         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
2882         rm -rf $DIR/$tfile*
2883         return 0
2884 }
2885 run_test 46c "pdirops: link vs link =============="
2886
2887 test_46d() {
2888         pdo_lru_clear
2889         touch $DIR1/$tfile-2
2890 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2891         do_nodes $(comma_list $(mdts_nodes)) \
2892                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2893         link $DIR1/$tfile-2 $DIR1/$tfile &
2894         PID1=$! ; pdo_sched
2895         rm $DIR2/$tfile &
2896         PID2=$! ; pdo_sched
2897         do_nodes $(comma_list $(mdts_nodes)) \
2898                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2899         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2900         wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
2901         rm -rf $DIR/$tfile*
2902         return 0
2903 }
2904 run_test 46d "pdirops: link vs unlink =============="
2905
2906 test_46e() {
2907         pdo_lru_clear
2908         touch $DIR1/$tfile-2
2909         touch $DIR1/$tfile-3
2910 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2911         do_nodes $(comma_list $(mdts_nodes)) \
2912                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2913         link $DIR1/$tfile-2 $DIR1/$tfile &
2914         PID1=$! ; pdo_sched
2915         mv $DIR2/$tfile-3 $DIR2/$tfile &
2916         PID2=$! ; pdo_sched
2917         do_nodes $(comma_list $(mdts_nodes)) \
2918                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2919         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2920         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2921         rm -rf $DIR/$tfile*
2922         return 0
2923 }
2924 run_test 46e "pdirops: link and rename (tgt) =============="
2925
2926 test_46f() {
2927         pdo_lru_clear
2928         touch $DIR1/$tfile-2
2929         touch $DIR1/$tfile-3
2930 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2931         do_nodes $(comma_list $(mdts_nodes)) \
2932                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2933         link $DIR1/$tfile-2 $DIR1/$tfile &
2934         PID1=$! ; pdo_sched
2935         mv $DIR2/$tfile $DIR2/$tfile-3 &
2936         PID2=$! ; pdo_sched
2937         do_nodes $(comma_list $(mdts_nodes)) \
2938                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2939         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2940         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2941         rm -rf $DIR/$tfile*
2942         return 0
2943 }
2944 run_test 46f "pdirops: link and rename (src) =============="
2945
2946 test_46g() {
2947         pdo_lru_clear
2948         touch $DIR1/$tfile-2
2949 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2950         do_nodes $(comma_list $(mdts_nodes)) \
2951                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2952         link $DIR1/$tfile-2 $DIR1/$tfile &
2953         PID1=$! ; pdo_sched
2954         stat $DIR2/$tfile > /dev/null &
2955         PID2=$! ; pdo_sched
2956         do_nodes $(comma_list $(mdts_nodes)) \
2957                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2958         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2959         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
2960         rm -rf $DIR/$tfile*
2961         return 0
2962 }
2963 run_test 46g "pdirops: link vs getattr =============="
2964
2965 test_46h() {
2966         pdo_lru_clear
2967         touch $DIR1/$tfile-2
2968 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2969         do_nodes $(comma_list $(mdts_nodes)) \
2970                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2971         link $DIR1/$tfile-2 $DIR1/$tfile &
2972         PID1=$! ; pdo_sched
2973         ls -lia $DIR2/ > /dev/null &
2974         PID2=$! ; pdo_sched
2975         do_nodes $(comma_list $(mdts_nodes)) \
2976                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2977         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2978         wait $PID2
2979         rm -rf $DIR/$tfile*
2980         return 0
2981 }
2982 run_test 46h "pdirops: link vs readdir =============="
2983
2984 test_46i() {
2985         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2986         pdo_lru_clear
2987         touch $DIR1/$tfile-2
2988 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2989         do_nodes $(comma_list $(mdts_nodes)) \
2990                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2991         link $DIR1/$tfile-2 $DIR1/$tfile &
2992         PID1=$! ; pdo_sched
2993         $LFS mkdir -i 1 $DIR2/$tfile &
2994         PID2=$! ; pdo_sched
2995         do_nodes $(comma_list $(mdts_nodes)) \
2996                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2997         check_pdo_conflict $PID1 && { wait $PID1;
2998                                 error "remote mkdir isn't blocked"; }
2999         wait $PID2 ; [ $? -ne 0 ] || error "remote mkdir must fail"
3000         rm -rf $DIR/$tfile*
3001         return 0
3002 }
3003 run_test 46i "pdirops: link vs remote mkdir"
3004
3005 # test 47: remote mkdir and blocking operations
3006 test_47a() {
3007 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3008         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3009         pdo_lru_clear
3010         do_nodes $(comma_list $(mdts_nodes)) \
3011                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3012         $LFS mkdir -i 1 $DIR1/$tfile &
3013         PID1=$! ; pdo_sched
3014         mkdir $DIR2/$tfile &
3015         PID2=$! ; pdo_sched
3016         do_nodes $(comma_list $(mdts_nodes)) \
3017                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3018         check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
3019         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
3020         rm -rf $DIR/$tfile*
3021         return 0
3022 }
3023 run_test 47a "pdirops: remote mkdir vs mkdir"
3024
3025 test_47b() {
3026 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3027         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3028         pdo_lru_clear
3029         do_nodes $(comma_list $(mdts_nodes)) \
3030                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3031         $LFS mkdir -i 1 $DIR1/$tfile &
3032         PID1=$! ; pdo_sched
3033         sleep 1 # please do not remove this sleep, see LU-10754
3034         multiop $DIR2/$tfile oO_CREAT:O_EXCL:c &
3035         PID2=$! ; pdo_sched
3036         do_nodes $(comma_list $(mdts_nodes)) \
3037                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3038         check_pdo_conflict $PID1 && { wait $PID1;
3039                                         error "create isn't blocked"; }
3040         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
3041         rm -rf $DIR/$tfile*
3042         return 0
3043 }
3044 run_test 47b "pdirops: remote mkdir vs create"
3045
3046 test_47c() {
3047         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3048         pdo_lru_clear
3049         touch $DIR1/$tfile-2
3050 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3051         do_nodes $(comma_list $(mdts_nodes)) \
3052                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3053         $LFS mkdir -i 1 $DIR1/$tfile &
3054         PID1=$! ; pdo_sched
3055         link $DIR2/$tfile-2 $DIR2/$tfile &
3056         PID2=$! ; pdo_sched
3057         do_nodes $(comma_list $(mdts_nodes)) \
3058                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3059         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
3060         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
3061         rm -rf $DIR/$tfile*
3062         return 0
3063 }
3064 run_test 47c "pdirops: remote mkdir vs link"
3065
3066 test_47d() {
3067         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3068         pdo_lru_clear
3069 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3070         do_nodes $(comma_list $(mdts_nodes)) \
3071                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3072         $LFS mkdir -i 1 $DIR1/$tfile &
3073         PID1=$! ; pdo_sched
3074         rmdir $DIR2/$tfile &
3075         PID2=$! ; pdo_sched
3076         do_nodes $(comma_list $(mdts_nodes)) \
3077                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3078         check_pdo_conflict $PID1 && { wait $PID1;
3079                                         error "unlink isn't blocked"; }
3080         wait $PID2 ; [ $? -eq 0 ] || error "rmdir must succeed"
3081         rm -rf $DIR/$tfile*
3082         return 0
3083 }
3084 run_test 47d "pdirops: remote mkdir vs unlink"
3085
3086 test_47e() {
3087         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3088         pdo_lru_clear
3089         touch $DIR1/$tfile-2
3090 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3091         do_nodes $(comma_list $(mdts_nodes)) \
3092                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3093         $LFS mkdir -i 1 $DIR1/$tfile &
3094         PID1=$! ; pdo_sched
3095         mv -T $DIR2/$tfile-2 $DIR2/$tfile &
3096         PID2=$! ; pdo_sched
3097         do_nodes $(comma_list $(mdts_nodes)) \
3098                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3099         check_pdo_conflict $PID1 && { wait $PID1;
3100                                         error "rename isn't blocked"; }
3101         wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
3102         rm -rf $DIR/$tfile*
3103         return 0
3104 }
3105 run_test 47e "pdirops: remote mkdir and rename (tgt)"
3106
3107 test_47f() {
3108         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3109         pdo_lru_clear
3110 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3111         do_nodes $(comma_list $(mdts_nodes)) \
3112                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3113         $LFS mkdir -i 1 $DIR1/$tfile &
3114         PID1=$! ; pdo_sched
3115         mv $DIR2/$tfile $DIR2/$tfile-2 &
3116         PID2=$! ; pdo_sched
3117         do_nodes $(comma_list $(mdts_nodes)) \
3118                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3119         check_pdo_conflict $PID1 && { wait $PID1;
3120                                         error "rename isn't blocked"; }
3121         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
3122         rm -rf $DIR/$tfile*
3123         return 0
3124 }
3125 run_test 47f "pdirops: remote mkdir and rename (src)"
3126
3127 test_47g() {
3128         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3129         sync
3130         sync_all_data
3131         pdo_lru_clear
3132 #define OBD_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3133         do_nodes $(comma_list $(mdts_nodes)) \
3134                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3135         $LFS mkdir -i 1 $DIR1/$tfile &
3136         PID1=$! ; pdo_sched
3137         stat $DIR2/$tfile > /dev/null &
3138         PID2=$! ; pdo_sched
3139         do_nodes $(comma_list $(mdts_nodes)) \
3140                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3141         check_pdo_conflict $PID1 && { wait $PID1;
3142                                         error "getattr isn't blocked"; }
3143         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
3144         rm -rf $DIR/$tfile*
3145         return 0
3146 }
3147 run_test 47g "pdirops: remote mkdir vs getattr"
3148
3149 test_50() {
3150         trunc_size=4096
3151         dd if=/dev/zero of=$DIR1/$tfile bs=1K count=10
3152 #define OBD_FAIL_OSC_CP_ENQ_RACE         0x410
3153         do_facet client "lctl set_param fail_loc=0x410"
3154         $TRUNCATE $DIR2/$tfile $trunc_size
3155         do_facet client "lctl set_param fail_loc=0x0"
3156         sleep 3
3157         size=`stat -c %s $DIR2/$tfile`
3158         [ $size -eq $trunc_size ] || error "wrong size"
3159 }
3160 run_test 50 "osc lvb attrs: enqueue vs. CP AST =============="
3161
3162 test_51a() {
3163         local filesize
3164         local origfile=/etc/hosts
3165
3166         filesize=$(stat -c %s $origfile)
3167
3168         # create an empty file
3169         $MCREATE $DIR1/$tfile || error "can't create $DIR1/$tfile"
3170         # cache layout lock on both mount point
3171         stat $DIR1/$tfile > /dev/null || error "stat $DIR1/$tfile failed"
3172         stat $DIR2/$tfile > /dev/null || error "stat $DIR2/$tfile failed"
3173
3174         # open and sleep 2 seconds then read
3175         $MULTIOP $DIR2/$tfile o_2r${filesize}c &
3176         local pid=$!
3177         sleep 1
3178
3179         # create the layout of testing file
3180         dd if=$origfile of=$DIR1/$tfile conv=notrunc > /dev/null ||
3181                 error "dd $DIR1/$tfile failed"
3182
3183         # MULTIOP proc should be able to read enough bytes and exit
3184         for ((i = 0; i < 6; i++)); do
3185                 sleep 1
3186                 kill -0 $pid || break
3187         done
3188         kill -0 $pid 2> /dev/null && error "multiop is still there"
3189         cmp $origfile $DIR2/$tfile || error "$origfile and $DIR2/$tfile differs"
3190
3191         rm -f $DIR1/$tfile
3192 }
3193 run_test 51a "layout lock: refresh layout should work"
3194
3195 test_51b() {
3196         (( $MDS1_VERSION >= $(version_code 2.3.59) )) ||
3197                 skip "Need MDS version at least 2.3.59"
3198
3199         local tmpfile=`mktemp`
3200
3201         $LFS setstripe -E 1m -S 1M -c 1 -E -1 -c 1 $DIR1/$tfile ||
3202                 error "Create $DIR1/$tfile failed"
3203
3204         dd if=/dev/zero of=$DIR1/$tfile bs=1k count=1 conv=notrunc ||
3205                 error "dd $DIR1/$tfile failed"
3206
3207         # delay glimpse so that layout has changed when glimpse finish
3208 #define OBD_FAIL_GLIMPSE_DELAY 0x1404
3209         $LCTL set_param fail_loc=0x1404 fail_val=4
3210         stat -c %s $DIR2/$tfile |tee $tmpfile &
3211         local pid=$!
3212         sleep 0.2
3213
3214         # extend layout of testing file
3215         dd if=/dev/zero of=$DIR1/$tfile bs=1M count=1 seek=2 conv=notrunc ||
3216                 error "dd $DIR1/$tfile failed"
3217
3218         wait $pid
3219         local fsize=$(cat $tmpfile)
3220
3221         [ x$fsize = x3145728 ] || error "file size is $fsize, should be 3145728"
3222
3223         rm -f $DIR1/$tfile $tmpfile
3224 }
3225 run_test 51b "layout lock: glimpse should be able to restart if layout changed"
3226
3227 test_51c() {
3228         [ $OSTCOUNT -ge 2 ] || { skip "needs >= 2 osts"; return; }
3229
3230         # set default layout to have 1 stripe
3231         mkdir $DIR1/$tdir
3232         $LFS setstripe -c 1 $DIR1/$tdir
3233
3234         # create a file with empty layout
3235         $MCREATE $DIR1/$tdir/$tfile ||
3236                 error "$MCREATE $DIR1/$tdir/$tfile failed"
3237
3238 #define OBD_FAIL_MDS_LL_BLOCK 0x172
3239         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x172
3240
3241         # change the layout of testing file
3242         echo "Setting layout to have $OSTCOUNT stripes ..."
3243         $LFS setstripe -c $OSTCOUNT $DIR1/$tdir/$tfile &
3244         pid=$!
3245         sleep 1
3246
3247         # write something to the file, it should be blocked on fetching layout
3248         dd if=/dev/zero of=$DIR2/$tdir/$tfile bs=1k count=1 conv=notrunc
3249         local stripe_count=$($LFS getstripe -c $DIR2/$tdir/$tfile)
3250         wait $pid
3251
3252         # lod_qos.c::min_stripe_count() allows setstripe with a default stripe
3253         # count to succeed with only 3/4 of the number of stripes (rounded up),
3254         # so creating striped files does not fail if an OST is offline or full
3255         [ $stripe_count -ge $((OSTCOUNT - $OSTCOUNT / 4)) ] ||
3256                 error "bad layout: getstripe -c $stripe_count < $OSTCOUNT * 3/4"
3257
3258         rm -fr $DIR1/$tdir
3259 }
3260 run_test 51c "layout lock: IT_LAYOUT blocked and correct layout can be returned"
3261
3262 test_51d() {
3263         dd if=/dev/zero of=/$DIR1/$tfile bs=1M count=1
3264         cancel_lru_locks mdc
3265
3266         # open should grant LAYOUT lock, mmap and read will install pages
3267         $MULTIOP $DIR1/$tfile oO_RDWR:SMR_Uc &
3268         local PID=$!
3269         sleep 1
3270
3271         # rss before revoking
3272         local br=$(grep -A 10 $tfile /proc/$PID/smaps | awk '/^Rss/{print $2}')
3273         echo "Before revoking layout lock: $br KB mapped"
3274
3275         # cancel layout lock manually
3276         cancel_lru_locks mdc
3277
3278         # rss after revoking
3279         local ar=$(grep -A 10 $tfile /proc/$PID/smaps | awk '/^Rss/{print $2}')
3280
3281         kill -USR1 $PID
3282         wait $PID || error "wait PID $PID failed"
3283
3284         [ $ar -eq 0 ] || error "rss before: $br, after $ar, some pages remained"
3285 }
3286 run_test 51d "layout lock: losing layout lock should clean up memory map region"
3287
3288 test_51e() {
3289         (( $MDS1_VERSION >= $(version_code 2.13.54.148) )) ||
3290                 skip "MDS version must be at least 2.13.54.148"
3291
3292         local pid
3293
3294         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
3295         pid=$!
3296         sleep 1
3297
3298         $LFS getstripe $DIR2/$tfile
3299         kill -USR1 $pid
3300         wait $pid || error "multiop failed"
3301
3302         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
3303         pid=$!
3304         sleep 1
3305
3306         $LFS getstripe $DIR2/$tfile
3307         kill -USR1 $pid
3308         wait $pid || error "multiop failed"
3309 }
3310 run_test 51e "lfs getstripe does not break leases, part 2"
3311
3312 test_54_part1()
3313 {
3314         echo "==> rename vs getattr vs setxattr should not deadlock"
3315         mkdir -p $DIR/d1/d2/d3 || error "(1) mkdir failed"
3316
3317         do_facet mds1 $LCTL set_param fail_loc=$1
3318
3319         mv -T $DIR/d1/d2/d3 $DIR/d1/d3 &
3320         PID1=$!
3321         sleep 1
3322
3323         stat $DIR/d1/d2 &
3324         PID2=$!
3325         sleep 1
3326
3327         setfattr -n user.attr1 -v value1 $DIR2/d1 || error "(2) setfattr failed"
3328         wait $PID1 || error "(3) mv failed"
3329         wait $PID2 || error "(4) stat failed"
3330         echo
3331
3332         rm -rf $DIR/d1
3333 }
3334
3335 test_54_part2() {
3336         echo "==> rename vs getattr vs open vs getattr should not deadlock"
3337         mkdir -p $DIR/d1/d2/d3 || error "(1) mkdir failed"
3338
3339         do_facet mds1 $LCTL set_param fail_loc=$1
3340
3341         mv -T $DIR/d1/d2/d3 $DIR/d1/d3 &
3342         PID1=$!
3343         sleep 1
3344
3345         stat $DIR/d1/d2 &
3346         PID2=$!
3347         sleep 1
3348
3349         $MULTIOP $DIR2/d1/d2 Oc &
3350         PID3=$!
3351         sleep 1
3352
3353         stat $DIR/d1 || error "(2) stat failed"
3354
3355         wait $PID1 || error "(3) mv failed"
3356         wait $PID2 || error "(4) stat failed"
3357         wait $PID3 && error "(5) multiop failed"
3358         echo
3359         rm -rf $DIR/d1
3360 }
3361
3362 test_54() {
3363         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
3364         save_lustre_params client "llite.*.xattr_cache" > $p
3365         lctl set_param llite.*.xattr_cache 1 ||
3366                 { skip "xattr cache is not supported"; return 0; }
3367
3368 #define OBD_FAIL_MDS_RENAME              0x153
3369 #define OBD_FAIL_MDS_RENAME2             0x154
3370         test_54_part1 0x80000153 || error 10
3371         test_54_part1 0x80000154 || error 11
3372         test_54_part2 0x80000153 || error 12
3373         test_54_part2 0x80000154 || error 13
3374
3375         restore_lustre_params < $p
3376         rm -f $p
3377 }
3378 run_test 54 "rename locking"
3379
3380 test_55a() {
3381         mkdir_on_mdt0 $DIR/$tdir
3382         mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"
3383
3384 #define OBD_FAIL_MDS_RENAME4              0x156
3385         do_facet mds1 $LCTL set_param fail_loc=0x80000156
3386
3387         mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
3388         PID1=$!
3389         sleep 1
3390
3391         rm -r $DIR2/$tdir/d3
3392         wait $PID1 && error "(2) mv succeeded"
3393
3394         rm -rf $DIR/$tdir
3395 }
3396 run_test 55a "rename vs unlink target dir"
3397
3398 test_55b()
3399 {
3400         mkdir_on_mdt0 $DIR/$tdir
3401         mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"
3402
3403 #define OBD_FAIL_MDS_RENAME4             0x156
3404         do_facet mds1 $LCTL set_param fail_loc=0x80000156
3405
3406         mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
3407         PID1=$!
3408         sleep 1
3409
3410         rm -r $DIR2/$tdir/d1
3411         wait $PID1 && error "(2) mv succeeded"
3412
3413         rm -rf $DIR/$tdir
3414 }
3415 run_test 55b "rename vs unlink source dir"
3416
3417 test_55c()
3418 {
3419         mkdir_on_mdt0 $DIR/$tdir
3420         mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"
3421
3422 #define OBD_FAIL_MDS_RENAME4              0x156
3423         do_facet mds1 $LCTL set_param fail_loc=0x156
3424
3425         mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
3426         PID1=$!
3427         sleep 1
3428
3429         # while rename is sleeping, open and remove d3
3430         $MULTIOP $DIR2/$tdir/d3 D_c &
3431         PID2=$!
3432         sleep 1
3433         rm -rf $DIR2/$tdir/d3
3434         sleep 5
3435
3436         # while rename is sleeping 2nd time, close d3
3437         kill -USR1 $PID2
3438         wait $PID2 || error "(3) multiop failed"
3439
3440         wait $PID1 && error "(2) mv succeeded"
3441
3442         rm -rf $DIR/$tdir
3443 }
3444 run_test 55c "rename vs unlink orphan target dir"
3445
3446 test_55d()
3447 {
3448         mkdir_on_mdt0 $DIR/$tdir
3449
3450         touch $DIR/$tdir/f1
3451
3452 #define OBD_FAIL_MDS_RENAME3              0x155
3453         do_facet mds1 $LCTL set_param fail_loc=0x155
3454         mv $DIR/$tdir/f1 $DIR/$tdir/$tdir &
3455         PID1=$!
3456         sleep 2
3457
3458         # while rename is sleeping, create $tdir, but as a directory
3459         mkdir -p $DIR2/$tdir/$tdir || error "(1) mkdir failed"
3460
3461         # link in reverse locking order
3462         ln $DIR2/$tdir/f1 $DIR2/$tdir/$tdir/
3463
3464         wait $PID1 && error "(2) mv succeeded"
3465         rm -rf $DIR/$tdir
3466 }
3467 run_test 55d "rename file vs link"
3468
3469 test_56a() {
3470         $LFS setstripe -c 1 $MOUNT/$tfile || error "creating $MOUNT/$tfile"
3471         stack_trap "rm -f $MOUNT/$tfile"
3472         echo "run_llverdev $MOUNT/$tfile -p -s $((16000000)) -c 4k"
3473         run_llverdev $MOUNT/$tfile -p -s $((16000000)) -c 4k ||
3474                 error "llverdev failed with rc=$?"
3475 }
3476 run_test 56a "test llverdev with single large stripe"
3477
3478 test_56b() {
3479         $LFS setstripe -C 2000 $MOUNT/$tfile || error "creating $MOUNT/$tfile"
3480         stack_trap "rm -f $MOUNT/$tfile"
3481         echo "run_llverdev $MOUNT/$tfile -p -s $((16000000 * OSTCOUNT)) -c 4k"
3482         run_llverdev $MOUNT/$tfile -p -s $((16000000 * OSTCOUNT)) -c 4k ||
3483                 error "llverdev failed with rc=$?"
3484 }
3485 run_test 56b "test llverdev and partial verify of wide stripe file"
3486
3487 test_60() {
3488         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
3489                 skip "MDS version must be >= 2.3.0"
3490
3491         # Create a file
3492         test_mkdir $DIR1/$tdir
3493         file1=$DIR1/$tdir/file
3494         file2=$DIR2/$tdir/file
3495
3496         echo orig > $file2 || error "Could not create $file2"
3497         version=$($LFS data_version $file1)
3498
3499         # Append data
3500         echo append >> $file2 || error "Could not append to $file2"
3501         version2=$($LFS data_version $file1)
3502         [ "$version" != "$version2" ] ||
3503             error "append did not change data version: $version"
3504
3505         # Overwrite data
3506         echo overwrite > $file2 || error "Could not overwrite $file2"
3507         version3=$($LFS data_version $file1)
3508         [ "$version2" != "$version3" ] ||
3509             error "overwrite did not change data version: $version2"
3510
3511         # Truncate before EOF
3512         $TRUNCATE $file2 3 || error "Could not truncate $file2"
3513         version4=$($LFS data_version $file1)
3514         [ "$version3" != "$version4" ] ||
3515             error "truncate did not change data version: $version3"
3516
3517         # Truncate after EOF
3518         $TRUNCATE $file2 123456 || error "Could not truncate $file2"
3519         version5=$($LFS data_version $file1)
3520         [ "$version4" != "$version5" ] ||
3521             error "truncate did not change data version: $version4"
3522
3523         # Chmod do not change version
3524         chmod 400 $file2 || error "Could not chmod 400 $file2"
3525         version6=$($LFS data_version $file1)
3526         [ "$version5" == "$version6" ] ||
3527             error "chmod should not change data version: $version5 != $version6"
3528
3529         # Chown do not change version
3530         chown $RUNAS_ID $file2 || error "Could not chown $RUNAS_ID $file2"
3531         version7=$($LFS data_version $file1)
3532         [ "$version5" == "$version7" ] ||
3533             error "chown should not change data version: $version5 != $version7"
3534 }
3535 run_test 60 "Verify data_version behaviour"
3536
3537 test_70a() {
3538         local test_dir=$tdir/test_dir
3539
3540         mkdir -p $DIR1/$tdir
3541         if [ $MDSCOUNT -ge 2 ]; then
3542                 local MDTIDX=1
3543                 $LFS mkdir -i $MDTIDX $DIR1/$test_dir ||
3544                         error "Create remote directory failed"
3545         else
3546                 mkdir -p $DIR1/$test_dir
3547         fi
3548         cd $DIR2/$test_dir || error "cd directory failed"
3549         rm -rf $DIR1/$test_dir || error "unlink directory failed"
3550
3551         cd $DIR2/$tdir || error "exit directory"
3552 }
3553 run_test 70a "cd directory && rm directory"
3554
3555 test_70b() { # LU-2781
3556         local i
3557         mkdir -p $DIR1/$tdir
3558
3559         touch $DIR1/$tdir/file
3560         for ((i = 0; i < 32; i++)); do
3561             $LFS rm_entry $DIR1/$tdir/non_existent_dir &>/dev/null
3562         done
3563         rm $DIR1/$tdir/file || error "cannot remove file after rm_entry"
3564
3565         touch $DIR1/$tdir/file
3566         $LFS mkdir -i0 $DIR1/$tdir/test_dir
3567         $LFS rm_entry $DIR1/$tdir/test_dir &>/dev/null
3568         rm -rf $DIR1/$tdir/test_dir ||
3569                 error "cannot remove directory after rm_entry"
3570         rm $DIR1/$tdir/file || error "cannot remove file after rm_entry"
3571 }
3572 run_test 70b "remove files after calling rm_entry"
3573
3574 test_71a() {
3575         [[ "$MDS1_VERSION" -lt $(version_code 2.1.6) ]] &&
3576                 skip "Need MDS version at least 2.1.6"
3577
3578         # Patch not applied to 2.2 and 2.3 branches
3579         [[ "$MDS1_VERSION" -ge $(version_code 2.2.0) ]] &&
3580         [[ "$MDS1_VERSION" -lt $(version_code 2.4.0) ]] &&
3581                 skip "Need MDS version earlier than 2.2.0 or at least 2.4.0"
3582
3583         checkfiemap --test ||
3584                 skip "checkfiemap not runnable: $?"
3585         # write data this way: hole - data - hole - data
3586         dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=1 count=1
3587         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR1/$tfile) + 1)))" = \
3588                 "zfs" ] &&
3589                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return 0
3590         dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=3 count=1
3591         GET_STAT="lctl get_param -n ldlm.services.ldlm_cbd.stats"
3592         stat $DIR2/$tfile
3593         local can1=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
3594         echo $can1
3595         checkfiemap $DIR2/$tfile 131072 ||
3596                 error "data is not flushed from client"
3597         local can2=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
3598         echo $can2
3599
3600         # common case of "create file, copy file" on a single node
3601         # should not flush data from ost
3602         dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=1 count=1
3603         dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=3 count=1
3604         stat $DIR1/$tfile
3605         local can3=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
3606         echo $can3
3607         checkfiemap $DIR1/$tfile 131072 ||
3608         error 4
3609         local can4=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
3610         echo $can2
3611         [ $can3 -eq $can4 ] || error $((can2-can1)) "cancel RPC occured."
3612 }
3613 run_test 71a "correct file map just after write operation is finished"
3614
3615 test_71b() {
3616         [[ "$MDS1_VERSION" -lt $(version_code 2.1.6) ]] &&
3617                 skip "Need MDS version at least 2.1.6"
3618
3619         # Patch not applied to 2.2 and 2.3 branches
3620         [[ "$MDS1_VERSION" -ge $(version_code 2.2.0) ]] &&
3621         [[ "$MDS1_VERSION" -lt $(version_code 2.4.0) ]] &&
3622                 skip "Need MDS version earlier than 2.2.0 or at least 2.4.0"
3623         [[ $OSTCOUNT -ge 2 ]] || skip "needs >= 2 OSTs"
3624
3625         checkfiemap --test ||
3626                 skip "error $?: checkfiemap failed"
3627
3628         mkdir -p $DIR1/$tdir
3629
3630         $LFS setstripe -c -1 $DIR1/$tdir || error "setstripe failed"
3631         dd if=/dev/urandom of=$DIR1/$tdir/$tfile bs=64K count=1
3632         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR1/$tdir/$tfile) + 1)))" = \
3633                 "zfs" ] &&
3634                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return 0
3635         checkfiemap $DIR1/$tdir/$tfile 65536 || error "checkfiemap failed"
3636 }
3637 run_test 71b "check fiemap support for stripecount > 1"
3638
3639 test_72() {
3640         local p="$TMP/sanityN-$TESTNAME.parameters"
3641         local tlink1
3642         local tlink2
3643         save_lustre_params client "llite.*.xattr_cache" > $p
3644         lctl set_param llite.*.xattr_cache 1 ||
3645                 { skip "xattr cache is not supported"; return 0; }
3646
3647         touch $DIR1/$tfile
3648         setfattr -n user.attr1 -v value1 $DIR1/$tfile ||
3649                 error "setfattr1 failed"
3650         getfattr -n user.attr1 $DIR2/$tfile | grep value1 ||
3651                 error "getfattr1 failed"
3652         setfattr -n user.attr1 -v value2 $DIR2/$tfile ||
3653                 error "setfattr2 failed"
3654         getfattr -n user.attr1 $DIR1/$tfile | grep value2 ||
3655                 error "getfattr2 failed"
3656
3657         # check that trusted.link is consistent
3658         tlink1=$(getfattr -n trusted.link $DIR1/$tfile | md5sum)
3659         ln $DIR2/$tfile $DIR2/$tfile-2 || error "failed to link"
3660         tlink2=$(getfattr -n trusted.link $DIR1/$tfile | md5sum)
3661         echo "$tlink1 $tlink2"
3662         [ "$tlink1" = "$tlink2" ] && error "trusted.link should have changed!"
3663
3664         rm -f $DIR2/$tfile
3665
3666         restore_lustre_params < $p
3667         rm -f $p
3668 }
3669 run_test 72 "getxattr/setxattr cache should be consistent between nodes"
3670
3671 test_73() {
3672         local p="$TMP/sanityN-$TESTNAME.parameters"
3673         save_lustre_params client "llite.*.xattr_cache" > $p
3674         lctl set_param llite.*.xattr_cache 1 ||
3675                 { skip "xattr cache is not supported"; return 0; }
3676
3677         touch $DIR1/$tfile
3678         setfattr -n user.attr1 -v value1 $DIR1/$tfile ||
3679                 error "setfattr1 failed"
3680         getfattr -n user.attr1 $DIR2/$tfile || error "getfattr1 failed"
3681         getfattr -n user.attr1 $DIR1/$tfile || error "getfattr2 failed"
3682         clear_stats llite.*.stats
3683         # PR lock should be cached by now on both clients
3684         getfattr -n user.attr1 $DIR1/$tfile || error "getfattr3 failed"
3685         # 2 hits for getfattr(0)+getfattr(size)
3686         [ $(calc_stats llite.*.stats getxattr_hits) -eq 2 ] ||
3687                 error "not cached in $DIR1"
3688         getfattr -n user.attr1 $DIR2/$tfile || error "getfattr4 failed"
3689         # 4 hits for more getfattr(0)+getfattr(size)
3690         [ $(calc_stats llite.*.stats getxattr_hits) -eq 4 ] ||
3691                 error "not cached in $DIR2"
3692         rm -f $DIR2/$tfile
3693
3694         restore_lustre_params < $p
3695         rm -f $p
3696 }
3697 run_test 73 "getxattr should not cause xattr lock cancellation"
3698
3699 test_74() {
3700         [ "$MDS1_VERSION" -lt $(version_code 2.4.93) ] &&
3701                 skip "Need MDS version at least 2.4.93"
3702
3703         dd if=/dev/zero of=$DIR1/$tfile-1 bs=1K count=1
3704         dd if=/dev/zero of=$DIR1/$tfile-2 bs=1K count=1
3705         flocks_test 4 $DIR1/$tfile-1 $DIR2/$tfile-2
3706 }
3707 run_test 74 "flock deadlock: different mounts =============="
3708
3709 # LU-3889
3710 test_75() {
3711         $LFS setstripe -c 2 -S 1m -i 0 $DIR1/$tfile
3712         dd if=/dev/zero of=$DIR1/$tfile bs=1M count=2
3713         cancel_lru_locks osc
3714
3715         dd of=$DIR1/$tfile if=/dev/zero bs=1M count=1 seek=1 conv=notrunc
3716         sync
3717
3718         # define OBD_FAIL_LDLM_ENQUEUE_HANG 0x31d
3719         $LCTL set_param fail_loc=0x31d
3720         stat -c %s $DIR1/$tfile &
3721         local pid=$!
3722         sleep 1
3723         kill -9 $pid
3724
3725         # For bad lock error handler we should ASSERT and got kernel panic here
3726         sleep 4
3727         $LCTL set_param fail_loc=0
3728 }
3729 run_test 75 "osc: upcall after unuse lock==================="
3730
3731 test_76() { #LU-946
3732         [[ "$MDS1_VERSION" -lt $(version_code 2.5.53) ]] &&
3733                 skip "Need MDS version at least 2.5.53"
3734
3735         remote_mds_nodsh && skip "remote MDS with nodsh"
3736         local fcount=$((MDSCOUNT * 256))
3737         declare -a fd_list
3738         declare -a fid_list
3739
3740         if remote_mds; then
3741                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
3742         else
3743                 nid="0@lo"
3744         fi
3745
3746         rm -rf $DIR/$tdir
3747         test_mkdir $DIR/$tdir
3748
3749         # drop all open locks and close any cached "open" files on the client
3750         cancel_lru_locks mdc
3751
3752         local open_fids_cmd="$LCTL get_param -n mdt.*.exports.'$nid'.open_files"
3753         local fid_list=($(do_nodes $(comma_list $(mdts_nodes)) $open_fids_cmd))
3754         local already=${#fid_list[@]}
3755         for (( i = 0; i < $already; i++ )) ; do
3756                 log "already open[$i]: $($LFS fid2path $DIR2 ${fid_list[i]})"
3757         done
3758
3759         echo -n "opening files: "
3760         ulimit -n $((fcount + 50))
3761         for ((i = 0; i < $fcount; i++)); do
3762                 touch $DIR/$tdir/f_$i
3763                 local fd=$(free_fd ${fd_list[i]})
3764                 local open_cmd="exec $fd<$DIR/$tdir/f_$i"
3765                 eval $open_cmd
3766
3767                 fd_list[i]=$fd
3768
3769                 (( $i % 32 == 0 )) && echo -n "."
3770         done
3771         echo
3772
3773         fid_list=($(do_nodes $(comma_list $(mdts_nodes)) $open_fids_cmd))
3774
3775         # Possible errors in openfiles FID list.
3776         # 1. Missing FIDs. Check 1
3777         # 2. Extra FIDs. Check 1
3778         # 3. Duplicated FID. Check 2
3779         # 4. Invalid FIDs. Check 2
3780         # 5. Valid FID, points to some other file. Check 3
3781
3782         # Check 1
3783         [ ${#fid_list[@]} -ne $((fcount + already)) ] &&
3784                 error "${#fid_list[@]} != $fcount (+$already old) open files"
3785
3786         echo -n "closing files: "
3787         for (( fd = 0, fid = 0; fd < $fcount; fd++, fid++ )) ; do
3788                 local close_cmd="exec ${fd_list[fd]}<&-"
3789                 eval $close_cmd
3790                 filename=$($LFS fid2path $DIR2 ${fid_list[fid]})
3791
3792                 while [[ ! "$filename" =~ "$DIR2/$tdir/f_" ]]; do
3793                         echo "skip old open file $filename"
3794                         ((fid++))
3795                         filename=$($LFS fid2path $DIR2 ${fid_list[fid]})
3796                 done
3797
3798                 # Check 2
3799                 rm --interactive=no $filename
3800                 [ $? -ne 0 ] &&
3801                         error "Nonexisting fid ${fid_list[fid]} listed."
3802                 (( $fd % 32 == 0 )) && echo -n "."
3803         done
3804         echo
3805
3806         # Check 3
3807         ls_op=$(ls $DIR2/$tdir | wc -l)
3808         [ $ls_op -ne 0 ] &&
3809                 error "Some openfiles are missing in lproc output"
3810
3811         rm -rf $DIR/$tdir
3812 }
3813 run_test 76 "Verify MDT open_files listing"
3814
3815 nrs_write_read() {
3816         local n=16
3817         local dir=$DIR/$tdir
3818         local myRUNAS="$1"
3819
3820         mkdir $dir || error "mkdir $dir failed"
3821         $LFS setstripe -c $OSTCOUNT $dir || error "setstripe to $dir failed"
3822         chmod 777 $dir
3823
3824         do_nodes $CLIENTS $myRUNAS \
3825                 dd if=/dev/zero of="$dir/nrs_r_\$HOSTNAME" bs=1M count=$n ||
3826                 error "dd at 0 on client failed (1)"
3827
3828         do_nodes $CLIENTS $myRUNAS \
3829                 "declare -a pids_w;
3830                 for ((i = 0; i < $n; i++)); do
3831                         dd if=/dev/zero of=$dir/nrs_w_\$HOSTNAME bs=1M \
3832 seek=\\\$i count=1 conv=notrunc &
3833                         pids_w[\\\$i]=\\\$!;
3834                 done;
3835                 rc_w=0;
3836                 for ((i = 0; i < $n; i++)); do
3837                         wait \\\${pids_w[\\\$i]};
3838                         newrc=\\\$?;
3839                         [ \\\$newrc -gt \\\$rc_w ] && rc_w=\\\$newrc;
3840                 done;
3841                 exit \\\$rc_w" &
3842         local pid_w=$!
3843         do_nodes $CLIENTS sync;
3844         cancel_lru_locks osc
3845
3846         do_nodes $CLIENTS $myRUNAS \
3847                 "declare -a pids_r;
3848                 for ((i = 0; i < $n; i++)); do
3849                         dd if=$dir/nrs_r_\$HOSTNAME bs=1M of=/dev/null \
3850 seek=\\\$i count=1 &
3851                         pids_r[\\\$i]=\\\$!;
3852                 done;
3853                 rc_r=0;
3854                 for ((i = 0; i < $n; i++)); do
3855                         wait \\\${pids_r[\\\$i]};
3856                         newrc=\\\$?;
3857                         [ \\\$newrc -gt \\\$rc_r ] && rc_r=\\\$newrc;
3858                 done;
3859                 exit \\\$rc_r" &
3860         local pid_r=$!
3861         cancel_lru_locks osc
3862
3863         wait $pid_w || error "dd (write) failed (2)"
3864         wait $pid_r || error "dd (read) failed (3)"
3865         rm -rvf $dir || error "rm -rf $dir failed"
3866 }
3867
3868 test_77a() { #LU-3266
3869         local rc
3870
3871         oss=$(comma_list $(osts_nodes))
3872         do_nodes $oss lctl set_param ost.OSS.*.nrs_policies="fifo" ||
3873                 rc=$?
3874         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
3875         [[ $rc -ne 0 ]] && error "failed to set fifo policy"
3876         nrs_write_read
3877
3878         return 0
3879 }
3880 run_test 77a "check FIFO NRS policy"
3881
3882 test_77b() { #LU-3266
3883         local rc
3884
3885         oss=$(comma_list $(osts_nodes))
3886
3887         do_nodes $oss lctl set_param ost.OSS.*.nrs_policies="crrn" \
3888                 ost.OSS.*.nrs_crrn_quantum=1 || rc=$?
3889         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
3890         [[ $rc -ne 0 ]] && error "failed to set crrn_quantum to 1"
3891
3892         echo "policy: crr-n, crrn_quantum 1"
3893         nrs_write_read
3894
3895         do_nodes $oss lctl set_param \
3896                 ost.OSS.*.nrs_crrn_quantum=64 || rc=$?
3897         [[ $rc -ne 0 ]] && error "failed to set crrn_quantum to 64"
3898
3899         echo "policy: crr-n, crrn_quantum 64"
3900         nrs_write_read
3901
3902         # cleanup
3903         do_nodes $oss lctl set_param \
3904                 ost.OSS.ost_io.nrs_policies="fifo" || rc=$?
3905         [[ $rc -ne 0 ]] && error "failed to set fifo policy"
3906         return 0
3907 }
3908 run_test 77b "check CRR-N NRS policy"
3909
3910 orr_trr() {
3911         local policy=$1
3912
3913         oss=$(comma_list $(osts_nodes))
3914
3915         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies=$policy \
3916                 ost.OSS.*.nrs_"$policy"_quantum=1 \
3917                 ost.OSS.*.nrs_"$policy"_offset_type="physical" \
3918                 ost.OSS.*.nrs_"$policy"_supported="reads" || return $?
3919
3920         echo "policy: $policy, ${policy}_quantum 1, ${policy}_offset_type " \
3921                 "physical, ${policy}_supported reads"
3922         nrs_write_read
3923
3924         do_nodes $oss lctl set_param \
3925                 ost.OSS.*.nrs_${policy}_supported="writes" \
3926                 ost.OSS.*.nrs_${policy}_quantum=64 || return $?
3927
3928         echo "policy: $policy, ${policy}_quantum 64, ${policy}_offset_type " \
3929                 "physical, ${policy}_supported writes"
3930         nrs_write_read
3931
3932         do_nodes $oss lctl set_param \
3933                 ost.OSS.*.nrs_${policy}_supported="reads_and_writes" \
3934                 ost.OSS.*.nrs_${policy}_offset_type="logical" || return $?
3935         echo "policy: $policy, ${policy}_quantum 64, ${policy}_offset_type " \
3936                 "logical, ${policy}_supported reads_and_writes"
3937         nrs_write_read
3938
3939         # cleanup
3940         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo" ||
3941                 return $?
3942         return 0
3943 }
3944
3945 test_77c() { #LU-3266
3946         local rc
3947         orr_trr "orr" || rc=$?
3948         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
3949         [[ $rc -ne 0 ]] && error "orr_trr failed rc:$rc"
3950         return 0
3951 }
3952 run_test 77c "check ORR NRS policy"
3953
3954 test_77d() { #LU-3266
3955         local rc
3956         orr_trr "trr" || rc=$?
3957         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
3958         [[ $rc -ne 0 ]] && error "orr_trr failed rc:$rc"
3959         return 0
3960 }
3961 run_test 77d "check TRR nrs policy"
3962
3963 tbf_rule_operate()
3964 {
3965         local facet=$1
3966         shift 1
3967
3968         do_facet $facet lctl set_param \
3969                 ost.OSS.ost_io.nrs_tbf_rule="$*"
3970         [ $? -ne 0 ] &&
3971                 error "failed to run operate '$*' on TBF rules"
3972 }
3973
3974 cleanup_tbf_verify()
3975 {
3976         local rc=0
3977         trap 0
3978         echo "cleanup_tbf $DIR/$tdir"
3979         rm -rf $DIR/$tdir
3980         rc=$?
3981         wait_delete_completed
3982         return $rc
3983 }
3984
3985 tbf_verify() {
3986         local dir=$DIR/$tdir
3987         local client1=${CLIENT1:-$(hostname)}
3988         local myRUNAS="$3"
3989
3990         local np=$(check_cpt_number ost1)
3991         [ $np -gt 0 ] || error "CPU partitions should not be $np."
3992         echo "cpu_npartitions on ost1 is $np"
3993
3994         mkdir $dir || error "mkdir $dir failed"
3995         $LFS setstripe -c 1 -i 0 $dir || error "setstripe to $dir failed"
3996         chmod 777 $dir
3997
3998         trap cleanup_tbf_verify EXIT
3999         echo "Limited write rate: $1, read rate: $2"
4000         echo "Verify the write rate is under TBF control"
4001         local start=$SECONDS
4002         do_node $client1 $myRUNAS dd if=/dev/zero of=$dir/tbf \
4003                 bs=1M count=100 oflag=direct 2>&1
4004         local runtime=$((SECONDS - start + 1))
4005         local rate=$(bc <<< "scale=6; 100 / $runtime")
4006         echo "Write runtime is $runtime s, speed is $rate IOPS"
4007
4008         # verify the write rate does not exceed TBF rate limit
4009         [ $(bc <<< "$rate < 1.1 * $np * $1") -eq 1 ] ||
4010                 error "The write rate ($rate) exceeds 110% of rate limit ($1 * $np)"
4011
4012         cancel_lru_locks osc
4013
4014         echo "Verify the read rate is under TBF control"
4015         start=$SECONDS
4016         do_node $client1 $myRUNAS dd if=$dir/tbf of=/dev/null \
4017                 bs=1M count=100 iflag=direct 2>&1
4018         runtime=$((SECONDS - start + 1))
4019         rate=$(bc <<< "scale=6; 100 / $runtime")
4020         echo "Read runtime is $runtime s, speed is $rate IOPS"
4021
4022         # verify the read rate does not exceed TBF rate limit
4023         [ $(bc <<< "$rate < 1.1 * $np * $2") -eq 1 ] ||
4024                 error "The read rate ($rate) exceeds 110% of rate limit ($2 * $np)"
4025
4026         cancel_lru_locks osc
4027         cleanup_tbf_verify || error "rm -rf $dir failed"
4028 }
4029
4030 test_77e() {
4031         local rc
4032
4033         oss=$(comma_list $(osts_nodes))
4034
4035         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid" ||
4036                 rc=$?
4037         [[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
4038         [[ $rc -ne 0 ]] && error "failed to set TBF NID policy"
4039
4040         local idis
4041         local rateis
4042         if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
4043                 idis="nid="
4044                 rateis="rate="
4045         fi
4046
4047         # Only operate rules on ost1 since OSTs might run on the same OSS
4048         # Add some rules
4049         tbf_rule_operate ost1 "start\ localhost\ ${idis}{0@lo}\ ${rateis}1000"
4050         local address=$(comma_list "$(host_nids_address $CLIENTS $NETTYPE)")
4051         local client_nids=$(nids_list $address "\\")
4052         tbf_rule_operate ost1 "start\ clients\ ${idis}{$client_nids}\ ${rateis}100"
4053         tbf_rule_operate ost1 "start\ others\ ${idis}{*.*.*.*@$NETTYPE}\ ${rateis}50"
4054         nrs_write_read
4055
4056         # Change the rules
4057         tbf_rule_operate ost1 "change\ localhost\ ${rateis}1001"
4058         tbf_rule_operate ost1 "change\ clients\ ${rateis}101"
4059         tbf_rule_operate ost1 "change\ others\ ${rateis}51"
4060         nrs_write_read
4061
4062         # Stop the rules
4063         tbf_rule_operate ost1 "stop\ localhost"
4064         tbf_rule_operate ost1 "stop\ clients"
4065         tbf_rule_operate ost1 "stop\ others"
4066         nrs_write_read
4067
4068         # Cleanup the TBF policy
4069         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4070         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4071         nrs_write_read
4072         return 0
4073 }
4074 run_test 77e "check TBF NID nrs policy"
4075
4076 test_77f() {
4077         local rc
4078
4079         oss=$(comma_list $(osts_nodes))
4080
4081         do_nodes $oss $LCTL set_param \
4082                 ost.OSS.ost_io.nrs_policies="tbf\ jobid" || rc=$?
4083         [[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
4084         [[ $rc -ne 0 ]] && error "failed to set TBF JOBID policy"
4085
4086         # Configure jobid_var
4087         local saved_jobid_var=$($LCTL get_param -n jobid_var)
4088         rc=$?
4089         [[ $rc -eq 3 ]] && skip "jobid_var not found" && return
4090         [[ $rc -ne 0 ]] && error "failed to get param jobid_var"
4091         if [ $saved_jobid_var != procname_uid ]; then
4092                 set_persistent_param_and_check client \
4093                         "jobid_var" "$FSNAME.sys.jobid_var" procname_uid
4094         fi
4095
4096         local idis
4097         local rateis
4098         if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
4099                 idis="jobid="
4100                 rateis="rate="
4101         fi
4102
4103         # Only operate rules on ost1 since OSTs might run on the same OSS
4104         # Add some rules
4105         tbf_rule_operate ost1 "start\ runas\ ${idis}{iozone.$RUNAS_ID\ dd.$RUNAS_ID\ tiotest.$RUNAS_ID}\ ${rateis}1000"
4106         tbf_rule_operate ost1 "start\ iozone_runas\ ${idis}{iozone.$RUNAS_ID}\ ${rateis}100"
4107         tbf_rule_operate ost1 "start\ dd_runas\ ${idis}{dd.$RUNAS_ID}\ ${rateis}50"
4108         nrs_write_read "$RUNAS"
4109
4110         # Change the rules
4111         tbf_rule_operate ost1 "change\ runas\ ${rateis}1001"
4112         tbf_rule_operate ost1 "change\ iozone_runas\ ${rateis}101"
4113         tbf_rule_operate ost1 "change\ dd_runas\ ${rateis}51"
4114         nrs_write_read "$RUNAS"
4115
4116         # Stop the rules
4117         tbf_rule_operate ost1 "stop\ runas"
4118         tbf_rule_operate ost1 "stop\ iozone_runas"
4119         tbf_rule_operate ost1 "stop\ dd_runas"
4120         nrs_write_read "$RUNAS"
4121
4122         # Cleanup the TBF policy
4123         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4124         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4125         nrs_write_read "$RUNAS"
4126
4127         local current_jobid_var=$($LCTL get_param -n jobid_var)
4128         [[ $? -ne 0 ]] && error "failed to get param jobid_var"
4129         if [ $saved_jobid_var != $current_jobid_var ]; then
4130                 set_persistent_param_and_check client \
4131                         "jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid_var
4132         fi
4133         return 0
4134 }
4135 run_test 77f "check TBF JobID nrs policy"
4136
4137 test_77g() {
4138         local rc=0
4139
4140         oss=$(comma_list $(osts_nodes))
4141
4142         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid" ||
4143                 rc=$?
4144         [[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
4145         [[ $rc -ne 0 ]] && error "failed to set TBF NID policy"
4146
4147         do_nodes $oss lctl set_param \
4148                 ost.OSS.ost_io.nrs_policies="tbf\ jobid" || rc=$?
4149         [[ $rc -ne 0 ]] && error "failed to set TBF JOBID policy"
4150
4151         local idis
4152         local rateis
4153         if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
4154                 idis="jobid="
4155                 rateis="rate="
4156         fi
4157
4158         # Add a rule that only valid for Jobid TBF. If direct change between
4159         # TBF types is not supported, this operation will fail.
4160         tbf_rule_operate ost1 "start\ dd_runas\ ${idis}{dd.$RUNAS_ID}\ ${rateis}50"
4161
4162         # Cleanup the TBF policy
4163         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4164         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4165         return 0
4166 }
4167 run_test 77g "Change TBF type directly"
4168
4169 test_77h() {
4170         [ "$OST1_VERSION" -ge $(version_code 2.8.55) ] ||
4171                 skip "Need OST version at least 2.8.55"
4172
4173         local old_policy=$(do_facet ost1 \
4174                 lctl get_param ost.OSS.ost_io.nrs_policies)
4175         local new_policy
4176
4177         do_facet ost1 lctl set_param \
4178                 ost.OSS.ost_io.nrs_policies="abc"
4179         [ $? -eq 0 ] && error "should return error"
4180
4181         do_facet ost1 lctl set_param \
4182                 ost.OSS.ost_io.nrs_policies="tbf\ abc"
4183         [ $? -eq 0 ] && error "should return error"
4184
4185         do_facet ost1 lctl set_param \
4186                 ost.OSS.ost_io.nrs_policies="tbf\ reg\ abc"
4187         [ $? -eq 0 ] && error "should return error"
4188
4189         do_facet ost1 lctl set_param \
4190                 ost.OSS.ost_io.nrs_policies="tbf\ abc\ efg"
4191         [ $? -eq 0 ] && error "should return error"
4192
4193         new_policy=$(do_facet ost1 lctl get_param ost.OSS.ost_io.nrs_policies)
4194         [ $? -eq 0 ] || error "shouldn't LBUG"
4195
4196         [ "$old_policy" = "$new_policy" ] || error "NRS policy should be same"
4197
4198         return 0
4199 }
4200 run_test 77h "Wrong policy name should report error, not LBUG"
4201
4202 tbf_rule_check()
4203 {
4204         local facet=$1
4205         local expected=$2
4206         local error_message=$3
4207         local rule_number=0
4208         for rule in $expected; do
4209                 rule_number=$((rule_number + 1))
4210         done
4211         local stop_line=$(($rule_number + 3))
4212         local awk_command="awk 'NR >= 4 && NR <= $stop_line {print \$1}'"
4213
4214         local output=$(do_facet $facet lctl get_param \
4215                 ost.OSS.ost_io.nrs_tbf_rule |
4216                 eval $awk_command |
4217                 tr "\n" " " |
4218                 sed 's/[ ]*$//')
4219         if [ "$output" != "$expected" ]; then
4220                 error "$error_message, expected '$expected', got '$output'"
4221         fi
4222 }
4223
4224 test_77i() {
4225         [ "$OST1_VERSION" -ge $(version_code 2.8.55) ] ||
4226                 skip "Need OST version at least 2.8.55"
4227
4228         for i in $(seq 1 $OSTCOUNT)
4229         do
4230                 do_facet ost"$i" lctl set_param \
4231                         ost.OSS.ost_io.nrs_policies="tbf\ jobid"
4232                 [ $? -ne 0 ] &&
4233                         error "failed to set TBF policy"
4234         done
4235
4236         tbf_rule_check ost1 "default" "error before inserting any rule"
4237
4238         tbf_rule_operate ost1 "start\ before\ jobid={jobid}\ rate=1000"
4239         tbf_rule_check ost1 "before default" \
4240                 "error when inserting rule 'before'"
4241
4242         tbf_rule_operate ost1 "start\ after\ jobid={jobid}\ rate=1000\ rank=default"
4243         tbf_rule_check ost1 "before after default" \
4244                 "error when inserting rule 'after'"
4245
4246         tbf_rule_operate ost1 "start\ target\ jobid={jobid}\ rate=1000\ rank=after"
4247         tbf_rule_check ost1 "before target after default" \
4248                 "error when inserting rule 'target'"
4249
4250         echo "Move before itself"
4251         tbf_rule_operate ost1 "change\ target\ rank=target"
4252         tbf_rule_check ost1 "before target after default" \
4253                 "error when moving before itself"
4254
4255         echo "Move to higher rank"
4256         tbf_rule_operate ost1 "change\ target\ rank=before"
4257         tbf_rule_check ost1 "target before after default" \
4258                 "error when moving to higher rank"
4259
4260         echo "Move to lower rank"
4261         tbf_rule_operate ost1 "change\ target\ rank=after"
4262         tbf_rule_check ost1 "before target after default" \
4263                 "error when moving to lower rank"
4264
4265         echo "Move before default"
4266         tbf_rule_operate ost1 "change\ target\ rank=default"
4267         tbf_rule_check ost1 "before after target default" \
4268                 error "error when moving before default"
4269
4270         # Cleanup the TBF policy
4271         do_nodes $(comma_list $(osts_nodes)) \
4272                 $LCTL set_param ost.OSS.ost_io.nrs_policies=fifo
4273         return 0
4274 }
4275 run_test 77i "Change rank of TBF rule"
4276
4277 test_77j() {
4278         local idis
4279         local rateis
4280
4281         [ "$OST1_VERSION" -ge $(version_code 2.9.53) ] ||
4282                 skip "Need OST version at least 2.9.53"
4283         if [ "$OST1_VERSION" -ge $(version_code 2.8.60) ]; then
4284                 idis="opcode="
4285                 rateis="rate="
4286         fi
4287
4288         do_nodes $(comma_list $(osts_nodes)) \
4289                 lctl set_param jobid_var=procname_uid \
4290                         ost.OSS.ost_io.nrs_policies="tbf\ opcode" \
4291                         ost.OSS.ost_io.nrs_tbf_rule="start\ ost_r\ ${idis}{ost_read}\ ${rateis}5" \
4292                         ost.OSS.ost_io.nrs_tbf_rule="start\ ost_w\ ${idis}{ost_write}\ ${rateis}20"
4293         [ $? -ne 0 ] && error "failed to set TBF OPCode policy"
4294
4295         nrs_write_read
4296         tbf_verify 20 5
4297
4298         do_nodes $(comma_list $(osts_nodes)) \
4299                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_r" \
4300                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_w" \
4301                         ost.OSS.ost_io.nrs_policies="fifo"
4302
4303         # sleep 3 seconds to wait the tbf policy stop completely,
4304         # or the next test case is possible get -EAGAIN when
4305         # setting the tbf policy
4306         sleep 3
4307 }
4308 run_test 77j "check TBF-OPCode NRS policy"
4309
4310 test_id() {
4311         local idstr="${1}id"
4312         local policy="${idstr}={$2}"
4313         local rate="rate=$3"
4314
4315         do_nodes $(comma_list $(osts_nodes)) \
4316                 lctl set_param jobid_var=procname_uid \
4317                         ost.OSS.ost_io.nrs_policies="tbf\ ${idstr}" \
4318                         ost.OSS.ost_io.nrs_tbf_rule="start\ ost_${idstr}\ ${policy}\ ${rate}"
4319         [ $? -ne 0 ] && error "failed to set tbf ${idstr} policy"
4320
4321         nrs_write_read "runas $4"
4322         tbf_verify $3 $3 "runas $4"
4323
4324         do_nodes $(comma_list $(osts_nodes)) \
4325                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_${idstr}" \
4326                         ost.OSS.ost_io.nrs_policies="fifo"
4327
4328         # sleep 3 seconds to wait the tbf policy stop completely,
4329         # or the next test case is possible get -eagain when
4330         # setting the tbf policy
4331         sleep 3
4332 }
4333
4334 test_77ja(){
4335         if [ "$OST1_VERSION" -lt $(version_code 2.11.50) ]; then
4336                 skip "Need OST version at least 2.11.50"
4337         fi
4338
4339         test_id "u" "500" "5" "-u 500"
4340         test_id "g" "500" "5" "-u 500 -g 500"
4341 }
4342 run_test 77ja "check TBF-UID/GID NRS policy"
4343
4344 cleanup_77k()
4345 {
4346         local rule_lists=$1
4347         local old_nrs=$2
4348
4349         trap 0
4350         for rule in $rule_lists; do
4351                 do_nodes $(comma_list $(osts_nodes)) \
4352                         lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ $rule"
4353         done
4354
4355         do_nodes $(comma_list $(osts_nodes)) \
4356                 lctl set_param ost.OSS.ost_io.nrs_policies="$old_nrs"
4357
4358         sleep 3
4359 }
4360
4361 test_77k() {
4362         [[ "$OST1_VERSION" -ge $(version_code 2.9.53) ]] ||
4363                 skip "Need OST version at least 2.9.53"
4364
4365         do_nodes $(comma_list $(osts_nodes)) \
4366                 lctl set_param ost.OSS.ost_io.nrs_policies="tbf" \
4367                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ jobid={dd.$RUNAS_ID}\&opcode={ost_write}\ rate=20" \
4368                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ jobid={dd.$RUNAS_ID}\&opcode={ost_read}\ rate=10"
4369
4370         nrs_write_read "$RUNAS"
4371         tbf_verify 20 10 "$RUNAS"
4372
4373         local address=$(comma_list "$(host_nids_address $CLIENTS $NETTYPE)")
4374         local client_nids=$(nids_list $address "\\")
4375         do_nodes $(comma_list $(osts_nodes)) \
4376                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_w" \
4377                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_r" \
4378                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ nid={0@lo\ $client_nids}\&opcode={ost_write}\ rate=20" \
4379                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ nid={0@lo\ $client_nids}\&opcode={ost_read}\ rate=10"
4380
4381         nrs_write_read
4382         tbf_verify 20 10
4383
4384         do_nodes $(comma_list $(osts_nodes)) \
4385                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_w" \
4386                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_r" \
4387                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext\ nid={0@lo\ $client_nids}\&jobid={dd.$RUNAS_ID}\ rate=20"
4388
4389         nrs_write_read "$RUNAS"
4390         tbf_verify 20 20 "$RUNAS"
4391
4392         do_nodes $(comma_list $(osts_nodes)) \
4393                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext" \
4394                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_a\ jobid={dd.$RUNAS_ID},opcode={ost_write}\ rate=20" \
4395                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_b\ jobid={dd.$RUNAS_ID},opcode={ost_read}\ rate=10"
4396
4397         nrs_write_read "$RUNAS"
4398         # with parameter "RUNAS", it will match the latest rule
4399         # "ext_b" first, so the limited write rate is 10.
4400         tbf_verify 10 10 "$RUNAS"
4401         tbf_verify 20 10
4402
4403         trap "cleanup_77k \"ext_a ext_b\" \"fifo\"" EXIT
4404
4405         [[ "$OST1_VERSION" -ge $(version_code 2.10.58) ]] ||
4406                 skip "Need OST version at least 2.10.58"
4407
4408         do_nodes $(comma_list $(osts_nodes)) \
4409                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_a" \
4410                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_b" \
4411                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_ug\ uid={500}\&gid={1000}\ rate=5"
4412         nrs_write_read "runas -u 500 -g 1000"
4413         tbf_verify 5 5 "runas -u 500 -g 1000"
4414
4415         do_nodes $(comma_list $(osts_nodes)) \
4416                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_ug" \
4417                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_uw\ uid={500}\&opcode={ost_write}\ rate=20" \
4418                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_ur\ uid={500}\&opcode={ost_read}\ rate=10"
4419
4420         nrs_write_read "runas -u 500"
4421         tbf_verify 20 10 "runas -u 500"
4422
4423         do_nodes $(comma_list $(osts_nodes)) \
4424                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_uw" \
4425                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_ur" \
4426                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_a\ uid={500},opcode={ost_write}\ rate=20" \
4427                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_b\ uid={500},opcode={ost_read}\ rate=10"
4428         nrs_write_read "runas -u 500"
4429         tbf_verify 10 10 "runas -u 500"
4430         tbf_verify 20 10 "runas -u 500"
4431         cleanup_77k "ext_a ext_b" "fifo"
4432 }
4433 run_test 77k "check TBF policy with NID/JobID/OPCode expression"
4434
4435 test_77l() {
4436         [[ "$OST1_VERSION" -ge $(version_code 2.10.56) ]] ||
4437                 skip "Need OST version at least 2.10.56"
4438
4439         do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid"
4440         do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="tbf"
4441
4442         local output=$(do_facet ost1 lctl get_param \
4443                         ost.OSS.ost_io.nrs_policies | \
4444                         awk '/name: tbf/ {print;exit}' | \
4445                         awk -F ': ' '{print $2}')
4446
4447         if [ "$output" != "tbf" ]; then
4448                 error "The generic TBF output is '$output', not 'tbf'"
4449         fi
4450
4451         do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4452 }
4453 run_test 77l "check the output of NRS policies for generic TBF"
4454
4455 test_77m() {
4456         if [ "$OST1_VERSION" -lt $(version_code 2.9.54) ]; then
4457                 skip "Need OST version at least 2.9.54"
4458         fi
4459
4460         local dir=$DIR/$tdir
4461
4462         mkdir $dir || error "mkdir $dir failed"
4463         $LFS setstripe -c $OSTCOUNT $dir || error "setstripe to $dir failed"
4464         chmod 777 $dir
4465
4466         local nodes=$(comma_list $(osts_nodes))
4467         do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies=delay \
4468                                        ost.OSS.ost_io.nrs_delay_min=4 \
4469                                        ost.OSS.ost_io.nrs_delay_max=4 \
4470                                        ost.OSS.ost_io.nrs_delay_pct=100
4471         [ $? -ne 0 ] && error "Failed to set delay policy"
4472
4473         local start=$SECONDS
4474         do_nodes "${SINGLECLIENT:-$HOSTNAME}" "$RUNAS" \
4475                  dd if=/dev/zero of="$dir/nrs_delay_$HOSTNAME" bs=1M count=1 \
4476                    oflag=direct conv=fdatasync ||
4477                 { do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
4478                   error "dd on client failed (1)"; }
4479         local elapsed=$((SECONDS - start))
4480
4481         # NRS delay doesn't do sub-second timing, so a request enqueued at
4482         # 0.9 seconds can be dequeued at 4.0
4483         [ $elapsed -lt 3 ] &&
4484                 { do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
4485                   error "Single 1M write should take at least 3 seconds"; }
4486
4487         start=$SECONDS
4488         do_nodes "${SINGLECLIENT:-$HOSTNAME}" "$RUNAS" \
4489                  dd if=/dev/zero of="$dir/nrs_delay_$HOSTNAME" bs=1M count=10 \
4490                    oflag=direct conv=fdatasync ||
4491                 { do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
4492                   error "dd on client failed (2)"; }
4493         elapsed=$((SECONDS - start))
4494
4495         [ $elapsed -lt 30 ] &&
4496                 { do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
4497                   error "Ten 1M writes should take at least 30 seconds"; }
4498
4499         do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4500         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4501
4502         return 0
4503 }
4504 run_test 77m "check NRS Delay slows write RPC processing"
4505
4506 test_77n() { #LU-10802
4507         if [ "$OST1_VERSION" -lt $(version_code 2.10.58) ]; then
4508                 skip "Need OST version at least 2.10.58"
4509         fi
4510
4511         # Configure jobid_var
4512         local saved_jobid_var=$($LCTL get_param -n jobid_var)
4513         if [ $saved_jobid_var != procname_uid ]; then
4514                 set_persistent_param_and_check client \
4515                         "jobid_var" "$FSNAME.sys.jobid_var" procname_uid
4516         fi
4517
4518         do_nodes $(comma_list $(osts_nodes)) \
4519                 lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ jobid" \
4520                         ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
4521                         ost.OSS.ost_io.nrs_tbf_rule="start\ dd_runas\ jobid={*.$RUNAS_ID}\ rate=20"
4522
4523         nrs_write_read
4524         tbf_verify 20 20 "$RUNAS"
4525
4526         do_nodes $(comma_list $(osts_nodes)) \
4527                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
4528                         ost.OSS.ost_io.nrs_tbf_rule="start\ dd_runas\ jobid={dd.*}\ rate=20"
4529
4530         nrs_write_read
4531         tbf_verify 20 20
4532
4533         do_nodes $(comma_list $(osts_nodes)) \
4534                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
4535                         ost.OSS.ost_io.nrs_policies="fifo"
4536
4537         sleep 3
4538
4539         local current_jobid_var=$($LCTL get_param -n jobid_var)
4540         if [ $saved_jobid_var != $current_jobid_var ]; then
4541                 set_persistent_param_and_check client \
4542                         "jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid_var
4543         fi
4544 }
4545 run_test 77n "check wildcard support for TBF JobID NRS policy"
4546
4547 test_77o() {
4548         (( $OST1_VERSION > $(version_code 2.14.54) )) ||
4549                 skip "need OST > 2.14.54"
4550
4551         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf\ nid"
4552         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ name\ nid={192.168.*.*@tcp}\ rate=10000"
4553         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ name1\ nid={192.168.*.*@tcp}\ rate=10000"
4554         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="change\ name1\ rank=name"
4555         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="stop\ name"
4556         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="fifo"
4557 }
4558 run_test 77o "Changing rank should not panic"
4559
4560 test_77q() {
4561         local i
4562
4563         (( $MDS1_VERSION > $(version_code 2.14.54) )) ||
4564                 skip "need MDS >= 2.14.54"
4565
4566         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf"
4567         stack_trap "do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies=fifo"
4568
4569         for i in {1..50}; do
4570                 local pid1 pid2
4571
4572                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start rule77q_1 uid={500}&gid={500} rate=100'" &
4573                 pid1=$!
4574                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start rule77q_2 uid={1000}&gid={1000} rate=100'" &
4575                 pid2=$!
4576                 wait $pid1 || error "$i: Fail to start TBF rule 'rule77q_1'"
4577                 wait $pid2 || error "$i: Fail to start TBF rule 'rule77q_2'"
4578
4579                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'stop rule77q_1'" &
4580                 pid1=$!
4581                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'stop rule77q_2'" &
4582                 pid2=$!
4583                 wait $pid1 || error "$i: Fail to stop TBF rule 'rule77q_1'"
4584                 wait $pid2 || error "$i: Fail to stop TBF rule 'rule77q_2'"
4585         done
4586 }
4587 run_test 77q "Parallel TBF rule definitions should not panic"
4588
4589 test_77p() {
4590         local c
4591         local -a spec_chars=(
4592                 '@' '.' '~' '#' '/' '^' '%' '*' ';' ',' '?' '<' '>' ':'
4593                 '+' '=' ')' '(' '{' '}' '|' '[' ']' '!' '&' '\$' '\`' '\\')
4594
4595         (( $MDS1_VERSION > $(version_code 2.14.54) )) ||
4596                 skip "need MDS >= 2.14.54"
4597
4598         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf"
4599         stack_trap "do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies=fifo"
4600
4601         # TBF rule name size is 16 bytes
4602         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ test_77p_overflo\ uid={500}\ rate=500" &&
4603                 error "The length of tbf rule name is not checked" || true
4604         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ \ uid={500}\ rate=500" &&
4605                 error "The server should not accept empty tbf rule name" || true
4606         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ test_77p_empty" &&
4607                 error "The server should not accept 'start <tbf_rule_name>' without an expression" || true
4608
4609         # Test with special chars
4610         for c in "${spec_chars[@]}"; do
4611                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start test77p${c}spec uid={500} rate=500'" &&
4612                 error "Special char '${c}' should not be accepted in a tbf rule name" || true
4613         done
4614
4615 }
4616 run_test 77p "Check validity of rule names for TBF policies"
4617
4618 test_78() { #LU-6673
4619         local rc
4620
4621         oss=$(comma_list $(osts_nodes))
4622         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="orr" &
4623         do_nodes $oss lctl set_param ost.OSS.*.nrs_orr_quantum=1
4624         rc=$?
4625         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
4626         # Valid return codes are:
4627         # 0: Tuning succeeded
4628         # ENODEV: Policy is still stopped
4629         # EAGAIN: Policy is being initialized
4630         [ $rc -eq 0 -o $rc -eq 19 -o $rc -eq 11 ] ||
4631                 error "Expected set_param to return 0|ENODEV|EAGAIN"
4632
4633         # Cleanup the ORR policy
4634         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4635         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4636         return 0
4637 }
4638 run_test 78 "Enable policy and specify tunings right away"
4639
4640 test_79() {
4641         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4642         test_mkdir $DIR/$tdir
4643
4644         # Prevent interference from layout intent RPCs due to
4645         # asynchronous writeback. These will be tested in 130c below.
4646         do_nodes ${CLIENTS:-$HOSTNAME} sync
4647
4648         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
4649                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
4650
4651 #define OBD_FAIL_MDS_INTENT_DELAY               0x160
4652         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
4653         local facet=mds$((mdtidx + 1))
4654         stat $DIR/$tdir
4655         set_nodes_failloc $(facet_active_host $facet) 0x80000160
4656         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null  &
4657         local pid=$!
4658         sleep 2
4659
4660 #define OBD_FAIL_MDS_GETXATTR_PACK       0x131
4661         set_nodes_failloc $(facet_active_host $facet) 0x80000131
4662
4663         wait $pid
4664         return 0
4665 }
4666 run_test 79 "xattr: intent error"
4667
4668 test_80a() {
4669         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4670         local MDTIDX=1
4671         local mdt_index
4672         local i
4673         local file
4674         local pid
4675
4676         mkdir_on_mdt0 $DIR1/$tdir
4677         mkdir -p $DIR1/$tdir/dir
4678         createmany -o $DIR1/$tdir/dir/f 10 ||
4679                 error "create files under remote dir failed $i"
4680
4681         cp /etc/passwd $DIR1/$tdir/$tfile
4682
4683         #migrate open file should fails
4684         multiop_bg_pause $DIR2/$tdir/$tfile O_c || error "open $file failed"
4685         pid=$!
4686         # give multiop a chance to open
4687         sleep 1
4688
4689         $LFS migrate -m $MDTIDX $DIR1/$tdir &&
4690                 error "migrate open files should failed with open files"
4691
4692         kill -USR1 $pid
4693
4694         $LFS migrate -m $MDTIDX $DIR1/$tdir ||
4695                         error "migrate remote dir error"
4696
4697         echo "Finish migration, then checking.."
4698         for file in $(find $DIR1/$tdir); do
4699                 mdt_index=$($LFS getstripe -m $file)
4700                 [ $mdt_index == $MDTIDX ] ||
4701                         error "$file is not on MDT${MDTIDX}"
4702         done
4703
4704         diff /etc/passwd $DIR1/$tdir/$tfile ||
4705                 error "file different after migration"
4706
4707         rm -rf $DIR1/$tdir || error "rm dir failed after migration"
4708 }
4709 run_test 80a "migrate directory when some children is being opened"
4710
4711 cleanup_80b() {
4712         trap 0
4713         kill -9 $migrate_pid
4714 }
4715
4716 test_80b() {
4717         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4718         local migrate_dir1=$DIR1/$tdir/migrate_dir
4719         local migrate_dir2=$DIR2/$tdir/migrate_dir
4720         local migrate_run=$LUSTRE/tests/migrate.sh
4721         local start_time
4722         local show_time=1
4723         local mdt_idx
4724         local rc=0
4725         local rc1=0
4726
4727         trap cleanup_80b EXIT
4728         #prepare migrate directory
4729         mkdir -p $migrate_dir1
4730         for F in {1,2,3,4,5}; do
4731                 echo "$F$F$F$F$F" > $migrate_dir1/file$F
4732                 echo "$F$F$F$F$F" > $DIR/$tdir/file$F
4733         done
4734
4735         #migrate the directories among MDTs
4736         (
4737                 while true; do
4738                         mdt_idx=$((RANDOM % MDSCOUNT))
4739                         $LFS migrate -m $mdt_idx $migrate_dir1 &>/dev/null ||
4740                                 rc=$?
4741                         (( $rc != 0 && $rc != 16 )) || break
4742                 done
4743         ) &
4744         migrate_pid=$!
4745
4746         echo "start migration thread $migrate_pid"
4747         #Access the files at the same time
4748         start_time=$SECONDS
4749         echo "accessing the migrating directory for 5 minutes..."
4750         while true; do
4751                 ls $migrate_dir2 > /dev/null || {
4752                         echo "read dir fails"
4753                         break
4754                 }
4755                 diff -u $DIR2/$tdir/file1 $migrate_dir2/file1 || {
4756                         echo "access file1 fails"
4757                         break
4758                 }
4759
4760                 cat $migrate_dir2/file2 > $migrate_dir2/file3 || {
4761                         echo "access file2/3 fails"
4762                         break
4763                 }
4764
4765                 echo "aaaaa" > $migrate_dir2/file4 > /dev/null || {
4766                         echo "access file4 fails"
4767                         break
4768                 }
4769
4770                 stat $migrate_dir2/file5 > /dev/null || {
4771                         echo "stat file5 fails"
4772                         break
4773                 }
4774
4775                 touch $migrate_dir2/source_file > /dev/null || rc1=$?
4776                 (( $rc != 0 && $rc != 1 )) || {
4777                         echo "touch file failed with $rc1"
4778                         break;
4779                 }
4780
4781                 if [ -e $migrate_dir2/source_file ]; then
4782                         ln $migrate_dir2/source_file $migrate_dir2/link_file \
4783                                         &>/dev/null || rc1=$?
4784                         if [ -e $migrate_dir2/link_file ]; then
4785                                 rm -rf $migrate_dir2/link_file
4786                         fi
4787
4788                         mrename $migrate_dir2/source_file \
4789                                 $migrate_dir2/target_file &>/dev/null || rc1=$?
4790                         (( $rc != 0 && $rc != 1 )) || {
4791                                 echo "rename failed with $rc1"
4792                                 break
4793                         }
4794
4795                         if [ -e $migrate_dir2/target_file ]; then
4796                                 rm -rf $migrate_dir2/target_file &>/dev/null ||
4797                                                                 rc1=$?
4798                         else
4799                                 rm -rf $migrate_dir2/source_file &>/dev/null ||
4800                                                                 rc1=$?
4801                         fi
4802                         (( $rc != 0 && $rc != 1 )) || {
4803                                 echo "unlink failed with $rc1"
4804                                 break
4805                         }
4806                 fi
4807
4808                 local duration=$((SECONDS - start_time))
4809                 if (( duration % 10 == 0 )); then
4810                         if (( $show_time == 1 )); then
4811                                 echo "...$duration seconds"
4812                                 show_time=0
4813                         fi
4814                 else
4815                         show_time=1
4816                 fi
4817
4818                 kill -0 $migrate_pid || {
4819                         echo "migration stopped 1"
4820                         break
4821                 }
4822
4823                 (( $duration < 300 )) || break
4824         done
4825
4826         #check migration are still there
4827         kill -0 $migrate_pid || error "migration stopped 2"
4828         cleanup_80b
4829 }
4830 run_test 80b "Accessing directory during migration"
4831
4832 test_81a() {
4833         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4834
4835         rm -rf $DIR1/$tdir
4836
4837         mkdir -p $DIR1/$tdir
4838
4839         $LFS setdirstripe -i0 -c$MDSCOUNT  $DIR1/$tdir/d0
4840         $LFS setdirstripe -i0 -c$MDSCOUNT  $DIR1/$tdir/d1
4841
4842         cd $DIR1/$tdir
4843         touch d0/0      || error "create 0 failed"
4844         mv d0/0 d1/0    || error "rename d0/0 d1/0 failed"
4845         stat d0/0       && error "stat mv filed succeed"
4846         mv $DIR2/$tdir/d1/0 $DIR2/$tdir/d0/0 || error "rename d1/0 d0/0 failed"
4847         stat d0/0       || error "stat failed"
4848
4849         local t=$(ls -ai $DIR1/$tdir/d0 | sort -u | wc -l)
4850
4851         if [ $t -ne 3 ]; then
4852                 ls -ai $DIR1/$tdir/d0
4853                 error "expect 3 get $t"
4854         fi
4855
4856         return 0
4857 }
4858 run_test 81a "rename and stat under striped directory"
4859
4860 test_81b() {
4861         [ $MDSCOUNT -lt 2 ] &&
4862                 skip "We need at least 2 MDTs for this test"
4863
4864         local total
4865         local setattr_pid
4866
4867         total=1000
4868
4869         $LFS mkdir -c $MDSCOUNT $DIR1/$tdir || error "$LFS mkdir"
4870         createmany -o $DIR1/$tdir/$tfile. $total || error "createmany"
4871
4872         (
4873                 while true; do
4874                         touch $DIR1/$tdir
4875                 done
4876         ) &
4877         setattr_pid=$!
4878
4879         for i in $(seq $total); do
4880                 mrename $DIR2/$tdir/$tfile.$i $DIR2/$tdir/$tfile-new.$i \
4881                         > /dev/null
4882         done
4883
4884         kill -9 $setattr_pid
4885 }
4886 run_test 81b "rename under striped directory doesn't deadlock"
4887
4888 test_81c() {
4889         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
4890         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
4891                 skip "Need MDS version at least 2.13.52"
4892
4893         # source is local, source parent is remote
4894         $LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
4895         $LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
4896         $LFS mkdir -i 3 $DIR1/${tdir}_src/sub || error "mkdir sub"
4897         $LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
4898         stat $DIR2/${tdir}_src/sub || error "stat sub failed"
4899         mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
4900         [ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
4901         rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt
4902
4903         # source is remote, source parent is local
4904         $LFS mkdir -i 3 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
4905         $LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
4906         $LFS mkdir -i 0 $DIR1/${tdir}_src/sub || error "mkdir sub"
4907         $LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
4908         stat $DIR2/${tdir}_src/sub || error "stat sub failed"
4909         mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
4910         [ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
4911         rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt
4912
4913         # source and source parent are remote
4914         $LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
4915         $LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
4916         mkdir $DIR1/${tdir}_src/sub || error "mkdir sub"
4917         $LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
4918         stat $DIR2/${tdir}_src/sub || error "stat sub failed"
4919         mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
4920         [ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
4921         rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt
4922
4923         # source and source parent are remote, and source is remote object
4924         $LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
4925         $LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
4926         $LFS mkdir -i 2 $DIR1/${tdir}_src/sub || error "mkdir sub"
4927         $LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
4928         stat $DIR2/${tdir}_src/sub || error "stat sub failed"
4929         mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
4930         [ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone" || true
4931 }
4932 run_test 81c "rename revoke LOOKUP lock for remote object"
4933
4934 test_82() {
4935         [[ "$MDS1_VERSION" -gt $(version_code 2.6.91) ]] ||
4936                 skip "Need MDS version at least 2.6.92"
4937
4938         # Client 1 creates a file.
4939         multiop_bg_pause $DIR1/$tfile O_ac || error "multiop_bg_pause 1"
4940         pid1=$!
4941         # Client 2 opens the file.
4942         multiop_bg_pause $DIR2/$tfile o_Ac || error "multiop_bg_pause 2"
4943         pid2=$!
4944         # Client 1 makes the file an orphan.
4945         rm $DIR1/$tfile || error "rm"
4946         # Client 2 sets EA "user.multiop".
4947         kill -s USR1 $pid2
4948         wait $pid2 || error "multiop 2"
4949         # Client 1 gets EA "user.multiop".  This used to fail because the EA
4950         # cache refill would get "trusted.link" from mdd_xattr_list() but
4951         # -ENOENT when trying to get "trusted.link"'s value.  See also sanity
4952         # 102q.
4953         kill -s USR1 $pid1
4954         wait $pid1 || error "multiop 1"
4955 }
4956 run_test 82 "fsetxattr and fgetxattr on orphan files"
4957
4958 test_83() {
4959         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4960         local pid1
4961         local pid2
4962
4963         (
4964                 cd $DIR1
4965                 while true; do
4966                         $LFS mkdir -i1 -c2 $tdir
4967                         rmdir $tdir
4968                 done
4969         ) &
4970         pid1=$!
4971         echo "start pid $pid1 to create/unlink striped directory"
4972
4973         # Access the directory at the same time
4974         (
4975                 cd $DIR2
4976                 while true; do
4977                         stat $tdir > /dev/null 2>&1
4978                 done
4979         ) &
4980         pid2=$!
4981         echo "start pid $pid2 to stat striped directory"
4982
4983         sleep 120
4984         kill $pid1 $pid2
4985         wait $pid1 $pid2
4986
4987         return 0
4988 }
4989 run_test 83 "access striped directory while it is being created/unlinked"
4990
4991 test_84() {
4992         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
4993                 skip "lustre < 2.12.55 does not contain LU-12485 fix"
4994
4995         local mtime
4996
4997         $MULTIOP $DIR/$tfile oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:c ||
4998                 error "create $tfile failed"
4999         mtime=$(stat -c%Y $DIR/$tfile)
5000         mtime=$((mtime + 200))
5001
5002         #define OBD_FAIL_OBD_0NLINK_RACE  0x60b
5003         do_facet mds1 $LCTL set_param fail_loc=0x8000060b
5004
5005         touch -c -m $mtime $DIR/$tfile &
5006         setattr_pid=$!
5007         # sleep a while to let 'touch' run first
5008         sleep 5
5009         rm -f $DIR2/$tfile || error "unlink $tfile failed"
5010
5011         # touch may fail
5012         wait $setattr_pid || true
5013 }
5014 run_test 84 "0-nlink race in lu_object_find()"
5015
5016 test_90() {
5017         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5018         local pid1
5019         local pid2
5020         local duration=180
5021
5022         [ "$SLOW" = "yes" ] && duration=600
5023         # Open/Create under striped directory
5024         (
5025                 cd $DIR1
5026                 while true; do
5027                         $LFS mkdir -c$MDSCOUNT $tdir > /dev/null 2>&1
5028                         touch $tdir/f{0..3} > /dev/null 2>&1
5029                 done
5030         ) &
5031         pid1=$!
5032         echo "start pid $pid1 to open/create under striped directory"
5033
5034         # unlink the striped directory at the same time
5035         (
5036                 cd $DIR2
5037                 while true; do
5038                         rm -rf $tdir > /dev/null 2>&1
5039                 done
5040         ) &
5041         pid2=$!
5042         echo "start pid $pid2 to unlink striped directory"
5043
5044         sleep $duration
5045
5046         kill $pid1 $pid2
5047         wait $pid1 $pid2
5048
5049         return 0
5050 }
5051 run_test 90 "open/create and unlink striped directory"
5052
5053 test_91() {
5054         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5055         local pid1
5056         local pid2
5057         local duration=180
5058
5059         [ "$SLOW" = "yes" ] && duration=600
5060         # chmod striped directory
5061         (
5062                 cd $DIR1
5063                 while true; do
5064                         $LFS mkdir -c$MDSCOUNT $tdir > /dev/null 2>&1
5065                         chmod go+w $tdir > /dev/null 2>&1
5066                 done
5067         ) &
5068         pid1=$!
5069         echo "start pid $pid1 to chmod striped directory"
5070
5071         # unlink the striped directory at the same time
5072         (
5073                 cd $DIR2
5074                 while true; do
5075                         rm -rf $tdir > /dev/null 2>&1
5076                 done
5077         ) &
5078         pid2=$!
5079         echo "start pid $pid2 to unlink striped directory"
5080
5081         sleep $duration
5082
5083         kill $pid1 $pid2
5084         wait $pid1 $pid2
5085
5086         return 0
5087 }
5088 run_test 91 "chmod and unlink striped directory"
5089
5090 test_92() {
5091         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5092
5093         local fd=$(free_fd)
5094         local cmd="exec $fd<$DIR1/$tdir"
5095         $LFS setdirstripe -c$MDSCOUNT $DIR1/$tdir || error "mkdir $tdir fails"
5096         eval $cmd
5097         cmd="exec $fd<&-"
5098         trap "eval $cmd" EXIT
5099         cd $DIR1/$tdir || error "cd $DIR1/$tdir fails"
5100         rmdir ../$tdir || error "rmdir ../$tdir fails"
5101
5102         #define OBD_FAIL_LLITE_NO_CHECK_DEAD  0x1408
5103         $LCTL set_param fail_loc=0x1408
5104         mkdir $DIR2/$tdir/dir && error "create dir succeeds"
5105         $LFS setdirstripe -i1 $DIR2/$tdir/remote_dir &&
5106                 error "create remote dir succeeds"
5107         $LCTL set_param fail_loc=0
5108         eval $cmd
5109         return 0
5110 }
5111 run_test 92 "create remote directory under orphan directory"
5112
5113 test_93() {
5114         local rc1=0
5115         local rc2=0
5116         local old_rr
5117
5118         mkdir -p $DIR1/$tfile-1/
5119         mkdir -p $DIR2/$tfile-2/
5120         local old_rr=$(do_facet $SINGLEMDS "$LCTL get_param -n \
5121                 lod.$FSNAME-MDT0000-*/qos_threshold_rr" | sed -e 's/%//')
5122         do_facet $SINGLEMDS "$LCTL set_param -n \
5123                 lod.$FSNAME-MDT0000-*/qos_threshold_rr=100"
5124         #define OBD_FAIL_MDS_LOV_CREATE_RACE     0x163
5125         do_facet $SINGLEMDS "$LCTL set_param fail_loc=0x00000163"
5126
5127         $LFS setstripe -c -1 $DIR1/$tfile-1/file1 &
5128         local PID1=$!
5129         sleep 1
5130         $LFS setstripe -c -1 $DIR2/$tfile-2/file2 &
5131         local PID2=$!
5132         wait $PID2
5133         wait $PID1
5134         do_facet $SINGLEMDS "$LCTL set_param fail_loc=0x0"
5135         do_facet $SINGLEMDS "$LCTL set_param -n \
5136                 lod.$FSNAME-MDT0000-*/qos_threshold_rr=$old_rr"
5137
5138         $LFS getstripe $DIR1/$tfile-1/file1
5139         rc1=$($LFS getstripe -q $DIR1/$tfile-1/file1 |
5140                 awk '{if (/[0-9]/) print $1 }' | sort | uniq -d | wc -l)
5141         $LFS getstripe $DIR2/$tfile-2/file2
5142         rc2=$($LFS getstripe -q $DIR2/$tfile-2/file2 |
5143                 awk '{if (/[0-9]/) print $1 }' | sort | uniq -d | wc -l)
5144         echo "rc1=$rc1 and rc2=$rc2 "
5145         [ $rc1 -eq 0 ] && [ $rc2 -eq 0 ] ||
5146                 error "object allocate on same ost detected"
5147 }
5148 run_test 93 "alloc_rr should not allocate on same ost"
5149
5150 test_94() {
5151         $LCTL set_param osc.*.idle_timeout=0
5152         dd if=/dev/zero of=$DIR2/$tfile bs=4k count=2 conv=fsync
5153
5154         local before=$(date +%s)
5155         local evict
5156
5157         $LCTL mark write
5158 #define OBD_FAIL_LDLM_PAUSE_CANCEL       0x312
5159         $LCTL set_param fail_val=5 fail_loc=0x80000312
5160         dd if=/dev/zero of=$DIR/$tfile conv=notrunc oflag=append bs=4k count=1 &
5161         local pid=$!
5162         sleep 2
5163
5164 #define OBD_FAIL_LDLM_PAUSE_CANCEL_LOCAL 0x329
5165         $LCTL set_param fail_val=6 fail_loc=0x80000329
5166         $LCTL mark kill $pid
5167         kill -ALRM $pid
5168
5169         dd if=/dev/zero of=$DIR2/$tfile conv=notrunc oflag=append bs=4k count=1
5170
5171         wait $pid
5172         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 conv=fsync
5173
5174         evict=$(do_facet client $LCTL get_param \
5175                 osc.$FSNAME-OST*-osc-*/state |
5176             awk -F"[ [,]" '/EVICTED ]$/ { if (t<$5) {t=$5;} } END { print t }')
5177
5178         [ -z "$evict" ] || [[ $evict -le $before ]] ||
5179                 (do_facet client $LCTL get_param \
5180                         osc.$FSNAME-OST*-osc-*/state;
5181                     error "eviction happened: $evict before:$before")
5182         $LCTL set_param osc.*.idle_timeout=debug
5183 }
5184 run_test 94 "signal vs CP callback race"
5185
5186 # Data-on-MDT tests
5187 test_100a() {
5188         skip "Reserved for glimpse-ahead" && return
5189         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5190                 skip "Need MDS version at least 2.10.55"
5191
5192         mkdir -p $DIR/$tdir
5193
5194         $LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom
5195
5196         lctl set_param -n mdc.*.stats=clear
5197         dd if=/dev/zero of=$DIR2/$tdir/dom bs=4096 count=1 || return 1
5198
5199         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #1"
5200         # first stat from server should return size data and save glimpse
5201         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
5202         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
5203         # second stat to check size is NOT cached on client without IO lock
5204         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #2"
5205
5206         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
5207         [ $gls -ge 1 ] || error "Expect glimpse RPCs but none"
5208         rm -f $dom
5209 }
5210 run_test 100a "DoM: glimpse RPCs for stat without IO lock (DoM only file)"
5211
5212 test_100b() {
5213         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5214                 skip "Need MDS version at least 2.10.55"
5215
5216         mkdir -p $DIR/$tdir
5217
5218         $LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom
5219
5220         lctl set_param -n mdc.*.stats=clear
5221         dd if=/dev/zero of=$DIR2/$tdir/dom bs=4096 count=1 || return 1
5222         cancel_lru_locks mdc
5223         # first stat data from server should have size
5224         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #1"
5225         # second stat to check size is cached on client
5226         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #2"
5227
5228         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
5229         # both stats should cause no glimpse requests
5230         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
5231         rm -f $dom
5232 }
5233 run_test 100b "DoM: no glimpse RPC for stat with IO lock (DoM only file)"
5234
5235 test_100c() {
5236         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5237                 skip "Need MDS version at least 2.10.55"
5238
5239         mkdir -p $DIR/$tdir
5240
5241         $LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom
5242
5243         lctl set_param -n mdc.*.stats=clear
5244         lctl set_param -n osc.*.stats=clear
5245         dd if=/dev/zero of=$DIR2/$tdir/dom bs=2048K count=1 || return 1
5246
5247         # check that size is merged from MDT and OST correctly
5248         $CHECKSTAT -t file -s 2097152 $DIR/$tdir/dom ||
5249                 error "Wrong size from stat #1"
5250
5251         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
5252         [ $gls -eq 0 ] && error "Expect OST glimpse RPCs but got none"
5253
5254         rm -f $dom
5255 }
5256 run_test 100c "DoM: write vs stat without IO lock (combined file)"
5257
5258 test_100d() {
5259         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5260                 skip "Need MDS version at least 2.10.55"
5261
5262         mkdir -p $DIR/$tdir
5263
5264         $LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom
5265
5266
5267         dd if=/dev/zero of=$DIR2/$tdir/dom bs=2048K count=1 || return 1
5268         lctl set_param -n mdc.*.stats=clear
5269         $TRUNCATE $DIR2/$tdir/dom 4096
5270
5271         # check that reported size is valid after file grows to OST and
5272         # is truncated back to MDT stripe size
5273         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom ||
5274                 error "Wrong size from stat #1"
5275
5276         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
5277         [ $gls -eq 0 ] && error "Expect OST glimpse but got none"
5278
5279         rm -f $dom
5280 }
5281 run_test 100d "DoM: write+truncate vs stat without IO lock (combined file)"
5282
5283 test_100e() {
5284         [ "$MDS1_VERSION" -lt $(version_code 2.11.50) ] &&
5285                 skip "Need MDS version at least 2.11.50"
5286
5287         local dom=$DIR/$tdir/dom
5288         local dom2=$DIR2/$tdir/dom
5289         mkdir -p $DIR/$tdir
5290
5291         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
5292
5293         cancel_lru_locks mdc
5294         dd if=/dev/urandom of=$dom bs=12000 count=1
5295         $TRUNCATE $dom2 6000
5296         cancel_lru_locks mdc
5297         lctl set_param -n mdc.*.stats=clear
5298         # expect read-on-open to return all data before write
5299         cat /etc/hosts >> $dom
5300         local read=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
5301         [[ $read -eq 0 ]] || error "Unexpected $read READ RPCs"
5302 }
5303 run_test 100e "DoM: read on open and file size"
5304
5305 test_101a() {
5306         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5307                 skip "Need MDS version at least 2.10.55"
5308
5309         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
5310         # to get layout
5311         $CHECKSTAT -t file $DIR1/$tfile
5312
5313         local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
5314         sysctl -wq vm.dirty_writeback_centisecs=0
5315
5316         trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT
5317
5318         # open + IO lock
5319         dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 ||
5320                 error_noexit "Write fails"
5321         # must discard pages
5322         lctl set_param -n mdc.*.stats=clear
5323         rm $DIR2/$tfile || error "Unlink fails"
5324
5325         local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
5326         [ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
5327 }
5328 run_test 101a "Discard DoM data on unlink"
5329
5330 test_101b() {
5331         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5332                 skip "Need MDS version at least 2.10.55"
5333
5334         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
5335         touch $DIR1/${tfile}_2
5336         # to get layout
5337         $CHECKSTAT -t file $DIR1/$tfile
5338
5339         local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
5340         sysctl -wq vm.dirty_writeback_centisecs=0
5341
5342         trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT
5343
5344         # open + IO lock
5345         dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 || error "Write fails"
5346         # must discard pages
5347         lctl set_param -n mdc.*.stats=clear
5348         mv $DIR2/${tfile}_2 $DIR2/$tfile || error "Rename fails"
5349
5350         local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
5351         [ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
5352 }
5353 run_test 101b "Discard DoM data on rename"
5354
5355 test_101c() {
5356         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5357                 skip "Need MDS version at least 2.10.55"
5358
5359         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
5360         # to get layout
5361         $CHECKSTAT -t file $DIR1/$tfile
5362
5363         local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
5364         sysctl -wq vm.dirty_writeback_centisecs=0
5365
5366         trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT
5367
5368         # open + IO lock
5369         dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 || error "Write fails"
5370         $MULTIOP $DIR1/$tfile O_c &
5371         MULTIOP_PID=$!
5372         sleep 1
5373         lctl set_param -n mdc.*.stats=clear
5374         rm $DIR2/$tfile > /dev/null || error "Unlink fails for opened file"
5375         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
5376
5377         local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
5378         [ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
5379 }
5380 run_test 101c "Discard DoM data on close-unlink"
5381
5382 # test to verify file handle related system calls
5383 # (name_to_handle_at/open_by_handle_at)
5384 # The new system calls are supported in glibc >= 2.14.
5385
5386 # test to verify we can open by handle an unlinked file from > 1 client
5387 # This test opens the file normally on $DIR1, which is on one mount, and then
5388 # opens it by handle on $DIR2, which is on a different mount.
5389 test_102() {
5390         (( "$MDS1_VERSION" >= $(version_code 2.11.57) )) ||
5391                 skip "Needs MDS version 2.11.57 or later"
5392
5393         echo "Test file_handle syscalls" > $DIR/$tfile ||
5394                 error "write failed"
5395         check_fhandle_syscalls $DIR/$tfile $DIR2 ||
5396                 error "check_fhandle_syscalls $tfile failed"
5397
5398         # test this is working on DNE directories also
5399         if (( MDSCOUNT > 1 && MDS1_VERSION >= $(version_code 2.14.52) )); then
5400                 $LFS mkdir -i 1 $DIR/$tdir.remote
5401                 cancel_lru_locks mdc
5402                 check_fhandle_syscalls $DIR/$tdir.remote $DIR2 ||
5403                         error "check_fhandle_syscalls $tdir.remote failed"
5404                 $LFS mkdir -c -1 $DIR/$tdir.remote/subdir
5405                 cancel_lru_locks mdc
5406                 check_fhandle_syscalls $DIR/$tdir.remote/subdir $DIR2 ||
5407                         error "check_fhandle_syscalls $tdir.remote/subdir fail"
5408
5409                 $LFS mkdir -c -1 $DIR/$tdir.stripe
5410                 cancel_lru_locks mdc
5411                 check_fhandle_syscalls $DIR/$tdir.stripe $DIR2 ||
5412                         error "check_fhandle_syscalls $tdir.stripe failed"
5413                 $LFS mkdir -c -1 $DIR/$tdir.stripe/subdir
5414                 cancel_lru_locks mdc
5415                 check_fhandle_syscalls $DIR/$tdir.stripe/subdir $DIR2 ||
5416                         error "check_fhandle_syscalls $tdir.stripe/subdir fail"
5417         fi
5418 }
5419 run_test 102 "Test open by handle of unlinked file"
5420
5421 # Compare file size between first & second mount, ensuring the client correctly
5422 # glimpses even with unused speculative locks - LU-11670
5423 test_103() {
5424         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
5425                 skip "Lockahead needs OST version at least 2.10.50"
5426
5427         local locktest=23
5428
5429         test_mkdir -p $DIR/$tdir
5430
5431         # Force file on to OST0
5432         $LFS setstripe -i 0 $DIR/$tdir
5433
5434         # Do not check multiple locks on glimpse
5435         # OBD_FAIL_OSC_NO_SIZE_DATA 0x415
5436         $LCTL set_param fail_loc=0x415
5437
5438         # Delay write commit by 2 seconds to guarantee glimpse wins race
5439         # The same fail_loc is used on client & server so it can work in the
5440         # single node sanity setup
5441         do_facet ost1 $LCTL set_param fail_loc=0x415 fail_val=2
5442
5443         echo "Incorrect size expected (no glimpse fix):"
5444         lockahead_test -d $DIR/$tdir -D $DIR2/$tdir -t $locktest -f $tfile
5445         rc=$?
5446         if [ $rc -eq 0 ]; then
5447                 echo "This doesn't work 100%, but this is just reproducing the bug, not testing the fix, so OK to not fail test."
5448         fi
5449
5450         # guarantee write commit timeout has expired
5451         sleep 2
5452
5453         # Clear fail_loc on client
5454         $LCTL set_param fail_loc=0
5455
5456         # Delay write commit by 2 seconds to guarantee glimpse wins race
5457         # OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
5458         do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
5459
5460         # Write commit is still delayed by 2 seconds
5461         lockahead_test -d $DIR/$tdir -D $DIR2/$tdir -t $locktest -f $tfile
5462         rc=$?
5463         [ $rc -eq 0 ] || error "Lockahead test$locktest failed, $rc"
5464
5465         # guarantee write commit timeout has expired
5466         sleep 2
5467
5468         rm -f $DIR/$tfile || error "unable to delete $DIR/$tfile"
5469 }
5470 run_test 103 "Test size correctness with lockahead"
5471
5472 get_stat_xtimes()
5473 {
5474         local xtimes
5475
5476         xtimes=$(stat -c "%X %Y %Z" $DIR/$tfile)
5477
5478         echo ${xtimes[*]}
5479 }
5480
5481 get_mdt_xtimes()
5482 {
5483         local mdtdev=$1
5484         local output
5485         local xtimes
5486
5487         output=$(do_facet mds1 "$DEBUGFS -c -R 'stat ROOT/$tfile' $mdtdev")
5488         ((xtimes[0]=$(awk -F ':' /atime/'{ print $2 }' <<< "$output")))
5489         ((xtimes[1]=$(awk -F ':' /mtime/'{ print $2 }' <<< "$output")))
5490         ((xtimes[2]=$(awk -F ':' /ctime/'{ print $2 }' <<< "$output")))
5491
5492         echo ${xtimes[*]}
5493 }
5494
5495 check_mdt_xtimes()
5496 {
5497         local mdtdev=$1
5498         local xtimes=($(get_stat_xtimes))
5499         local mdt_xtimes=($(get_mdt_xtimes $mdtdev))
5500
5501         echo "STAT a|m|ctime ${xtimes[*]}"
5502         echo "MDT a|m|ctime ${mdt_xtimes[*]}"
5503         [[ ${xtimes[0]} == ${mdt_xtimes[0]} ]] ||
5504                 error "$DIR/$tfile atime (${xtimes[0]}:${mdt_xtimes[0]}) diff"
5505         [[ ${xtimes[1]} == ${mdt_xtimes[1]} ]] ||
5506                 error "$DIR/$tfile mtime (${xtimes[1]}:${mdt_xtimes[1]}) diff"
5507         [[ ${xtimes[2]} == ${mdt_xtimes[2]} ]] ||
5508                 error "$DIR/$tfile ctime (${xtimes[2]}:${mdt_xtimes[2]}) diff"
5509 }
5510
5511 test_104() {
5512         [ "$mds1_FSTYPE" == "ldiskfs" ] || skip_env "ldiskfs only test"
5513         [ $MDS1_VERSION -lt $(version_code 2.12.4) ] &&
5514                 skip "Need MDS version at least 2.12.4"
5515
5516         local pid
5517         local mdtdev=$(mdsdevname ${SINGLEMDS//mds/})
5518         local atime_diff=$(do_facet $SINGLEMDS \
5519                 lctl get_param -n mdd.*MDT0000*.atime_diff)
5520
5521         do_facet $SINGLEMDS \
5522                 lctl set_param -n mdd.*MDT0000*.atime_diff=0
5523
5524         stack_trap "do_facet $SINGLEMDS \
5525                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff" EXIT
5526
5527         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
5528         check_mdt_xtimes $mdtdev
5529         sleep 2
5530
5531         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
5532         check_mdt_xtimes $mdtdev
5533         sleep 2
5534         $MULTIOP $DIR2/$tfile Oz8192w8192_c &
5535         pid=$!
5536         sleep 2
5537         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
5538         sleep 2
5539         kill -USR1 $pid && wait $pid || error "multiop failure"
5540         check_mdt_xtimes $mdtdev
5541
5542         local xtimes
5543         local mdt_xtimes
5544
5545         # Verify mtime/ctime is NOT upated on MDS when there is no modification
5546         # on the client side
5547         xtimes=($(get_stat_xtimes))
5548         $MULTIOP $DIR/$tfile O_c &
5549         pid=$!
5550         sleep 2
5551         kill -USR1 $pid && wait $pid || error "multiop failure"
5552         mdt_xtimes=($(get_mdt_xtimes $mdtdev))
5553         [[ ${xtimes[1]} == ${mdt_xtimes[1]} ]] ||
5554                 error "$DIR/$tfile mtime (${xtimes[1]}:${mdt_xtimes[1]}) diff"
5555         [[ ${xtimes[2]} == ${mdt_xtimes[2]} ]] ||
5556                 error "$DIR/$tfile ctime (${xtimes[2]}:${mdt_xtimes[2]}) diff"
5557         check_mdt_xtimes $mdtdev
5558
5559         sleep 2
5560         # Change ctime via chmod
5561         $MULTIOP $DIR/$tfile o_tc &
5562         pid=$!
5563         sleep 2
5564         kill -USR1 $pid && wait $pid || error "multiop failure"
5565         check_mdt_xtimes $mdtdev
5566 }
5567 run_test 104 "Verify that MDS stores atime/mtime/ctime during close"
5568
5569 test_105() {
5570         test_mkdir -p $DIR/$tdir
5571         echo test > $DIR/$tdir/$tfile
5572         $LCTL set_param fail_loc=0x416
5573         cancel_lru_locks osc & sleep 1
5574         fsize1=$(stat -c %s $DIR2/$tdir/$tfile)
5575         wait
5576         [[ $fsize1 = 5 ]] ||  error "Glimpse returned wrong file size $fsize1"
5577 }
5578 run_test 105 "Glimpse and lock cancel race"
5579
5580 test_106a() {
5581         [ "$mds1_FSTYPE" == "ldiskfs" ] && statx_supported ||
5582                 skip_env "Test only for ldiskfs and statx() supported"
5583
5584         local btime
5585         local mdt_btime
5586         local output
5587         local mdtdev=$(mdsdevname ${SINGLEMDS//mds/})
5588
5589         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
5590         btime=$($STATX -c %W $DIR/$tfile)
5591         output=$(do_facet mds1 "$DEBUGFS -c -R 'stat ROOT/$tfile' $mdtdev")
5592         echo $output
5593         ((mdt_btime=$(awk -F ':' /crtime/'{ print $2 }' <<< "$output")))
5594         [[ $btime == $mdt_btime ]] ||
5595                 error "$DIR/$tfile btime ($btime:$mdt_btime) diff"
5596
5597 }
5598 run_test 106a "Verify the btime via statx()"
5599
5600 test_106b() {
5601         statx_supported || skip_env "statx() only test"
5602
5603         local rpcs_before
5604         local rpcs_after
5605
5606         $LFS setstripe -c 1 $DIR/$tfile || error "$DIR/$tfile setstripe failed"
5607         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
5608         cancel_lru_locks $OSC
5609         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5610         $STATX $DIR/$tfile
5611         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5612         [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
5613                 error "$STATX should send 1 glimpse RPC to $OSC"
5614
5615         cancel_lru_locks $OSC
5616         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5617         # %n: FILENAME; %i: STATX_INO; %A STATX_MODE; %h STATX_NLINK;
5618         # %u: STATX_UID; %g: STATX_GID; %W STATX_BTIME; %X STATX_ATIME;
5619         # %Z: STATX_CTIME
5620         $STATX -c "%n %i %A %h %u %g %W %X %Z" $DIR/$tfile
5621         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5622         [ $rpcs_after -eq $rpcs_before ] ||
5623                 error "$STATX should not send glimpse RPCs to $OSC"
5624
5625         cancel_lru_locks $OSC
5626         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5627         $STATX --cached=always $DIR/$tfile
5628         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5629         [ $rpcs_after -eq $rpcs_before ] ||
5630                 error "$STATX should not send glimpse RPCs to $OSC"
5631
5632         cancel_lru_locks $OSC
5633         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5634         $STATX -c %Y $DIR/$tfile
5635         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5636         [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
5637                 error "$STATX -c %Y should send 1 glimpse RPC to $OSC"
5638
5639         cancel_lru_locks $OSC
5640         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5641         $STATX -c %s $DIR/$tfile
5642         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5643         [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
5644                 error "$STATX -c %s should send 1 glimpse RPC to $OSC"
5645
5646         cancel_lru_locks $OSC
5647         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5648         $STATX -c %b $DIR/$tfile
5649         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5650         [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
5651                 error "$STATX -c %b should send 1 glimpse RPC to $OSC"
5652 }
5653 run_test 106b "Glimpse RPCs test for statx"
5654
5655 test_106c() {
5656         statx_supported || skip_env "statx() only test"
5657
5658         local mask
5659
5660         touch $DIR/$tfile
5661         # Mask supported in stx_attributes by Lustre is
5662         # STATX_ATTR_IMMUTABLE(0x10) | STATX_ATTR_APPEND(0x20) : (0x30).
5663         mask=$($STATX -c %p $DIR/$tfile)
5664         (( (0x$mask & 0x30) == 0x30 )) ||
5665                 error "supported stx_attributes: got '0x$mask', expected '0x30' at least"
5666         chattr +i $DIR/$tfile || error "chattr +i $DIR/$tfile failed"
5667         mask=$($STATX -c %r $DIR/$tfile)
5668         [[ $mask == "10" ]] ||
5669                 error "got immutable flags '$mask', expected '10'"
5670         chattr -i $DIR/$tfile || error "chattr -i $DIR/$tfile failed"
5671         mask=$($STATX -c %r $DIR/$tfile)
5672         [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
5673         chattr +a $DIR/$tfile || error "chattr +a $DIR/$tfile failed"
5674         mask=$($STATX -c %r $DIR/$tfile)
5675         [[ $mask == "20" ]] || error "got flags '$mask', expected '20'"
5676         chattr -a $DIR/$tfile || error "chattr -a $DIR/$tfile failed"
5677         mask=$($STATX -c %r $DIR/$tfile)
5678         [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
5679         chattr +ia $DIR/$tfile || error "chattr +ia $DIR/$tfile failed"
5680         mask=$($STATX -c %r $DIR/$tfile)
5681         [[ $mask == "30" ]] || error "got flags '$mask', expected '30'"
5682         chattr -ia $DIR/$tfile || error "chattr -ia $DIR/$tfile failed"
5683         mask=$($STATX -c %r $DIR/$tfile)
5684         [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
5685 }
5686 run_test 106c "Verify statx attributes mask"
5687
5688 test_107a() { # LU-1031
5689         dd if=/dev/zero of=$DIR1/$tfile bs=1M count=10
5690         local gid1=14091995
5691         local gid2=16022000
5692
5693         $LFS getstripe $DIR1/$tfile
5694
5695         multiop_bg_pause $DIR1/$tfile OG${gid1}_g${gid1}c || return 1
5696         local MULTIPID1=$!
5697         multiop_bg_pause $DIR2/$tfile O_G${gid2}r10g${gid2}c || return 2
5698         local MULTIPID2=$!
5699         kill -USR1 $MULTIPID2
5700         sleep 2
5701         if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
5702                 error "First grouplock does not block second one"
5703         else
5704                 echo "First grouplock blocks second one"
5705         fi
5706         kill -USR1 $MULTIPID1
5707         wait $MULTIPID1
5708         wait $MULTIPID2
5709 }
5710 run_test 107a "Basic grouplock conflict"
5711
5712 test_107b() {
5713         dd if=/dev/zero of=$DIR1/$tfile bs=1M count=10
5714         local gid1=14091995
5715         local gid2=16022000
5716
5717         $LFS getstripe $DIR1/$tfile
5718
5719         multiop_bg_pause $DIR1/$tfile OG${gid1}_g${gid1}c || return 1
5720         local MULTIPID1=$!
5721         multiop $DIR2/$tfile Or10c &
5722         local MULTIPID2=$!
5723         sleep 2
5724
5725         if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
5726                 error "Grouplock does not block IO"
5727         else
5728                 echo "Grouplock blocks IO"
5729         fi
5730
5731         multiop $DIR2/$tfile OG${gid2}_g${gid2}c &
5732         local MULTIPID3=$!
5733         sleep 2
5734         if [[ $(ps h -o comm -p $MULTIPID3) == "" ]]; then
5735                 error "First grouplock does not block second one"
5736         else
5737                 echo "First grouplock blocks second one"
5738         fi
5739
5740         kill -USR1 $MULTIPID1
5741         sleep 2
5742
5743         if [[ $(ps h -o comm -p $MULTIPID3) == "" ]]; then
5744                 error "Second grouplock thread disappeared"
5745         fi
5746
5747         if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
5748                 error "Second grouplock does not block IO"
5749         else
5750                 echo "Second grouplock blocks IO"
5751         fi
5752
5753         kill -USR1 $MULTIPID3
5754         wait $MULTIPID1
5755         wait $MULTIPID2
5756         wait $MULTIPID3
5757 }
5758 run_test 107b "Grouplock is added to the head of waiting list"
5759
5760 test_108a() {
5761         local offset
5762
5763         $LFS setstripe -E 1M -c 1 -E -1 $DIR1/$tfile ||
5764                 error "Create $DIR1/$tfile failed"
5765
5766         dd if=/dev/zero of=$DIR1/$tfile bs=10000 count=1 ||
5767                 error "dd $DIR1/$tfile failed"
5768         offset=$(lseek_test -d 5000 $DIR2/$tfile)
5769         [[ $offset == 5000 ]] || error "offset $offset != 5000"
5770
5771         $TRUNCATE $DIR1/$tfile 2000
5772         offset=$(lseek_test -l 1000 $DIR2/$tfile)
5773         [[ $offset == 2000 ]] || error "offset $offset != 2000"
5774
5775         #define OBD_FAIL_OSC_DELAY_IO 0x414
5776         $LCTL set_param fail_val=4 fail_loc=0x80000414
5777         dd if=/dev/zero of=$DIR1/$tfile count=1 bs=8M conv=notrunc oflag=dsync &
5778         local pid=$!
5779         sleep 2
5780
5781         offset=$(lseek_test -l 8000 $DIR2/$tfile)
5782         wait $pid
5783         [[ $offset == 8388608 ]] || error "offset $offset != 8388608"
5784 }
5785 run_test 108a "lseek: parallel updates"
5786
5787 # LU-14110
5788 test_109() {
5789         local i
5790         local pid1 pid2
5791
5792         ! local_mode ||
5793                 skip "Clients need to be on different nodes than the servers"
5794
5795         umount_client $MOUNT
5796         umount_client $MOUNT2
5797
5798         echo "Starting race between client mount instances (50 iterations):"
5799         for i in {1..50}; do
5800                 log "Iteration $i"
5801
5802 #define OBD_FAIL_ONCE|OBD_FAIL_LLITE_RACE_MOUNT        0x80001417
5803                 $LCTL set_param -n fail_loc=0x80001417
5804
5805                 mount_client $MOUNT  & pid1=$!
5806                 mount_client $MOUNT2 & pid2=$!
5807                 wait $pid1 || error "Mount $MOUNT fails with $?"
5808                 wait $pid2 || error "Mount $MOUNT2 fails with $?"
5809
5810                 umount_client $MOUNT  & pid1=$!
5811                 umount_client $MOUNT2 & pid2=$!
5812                 wait $pid1 || error "Umount $MOUNT fails with $?"
5813                 wait $pid2 || error "Umount $MOUNT2 fails with $?"
5814
5815                 $LUSTRE_RMMOD || error "Fail to remove lustre modules"
5816                 load_modules
5817                 echo
5818         done
5819
5820         mount_client $MOUNT
5821         mount_client $MOUNT2
5822 }
5823
5824 run_test 109 "Race with several mount instances on 1 node"
5825
5826 test_110() {
5827         local before=$(date +%s)
5828         local evict
5829
5830         mkdir -p $DIR/$tdir
5831         touch $DIR/$tdir/f1
5832         touch $DIR/$tfile
5833
5834         #define OBD_FAIL_PTLRPC_RESEND_RACE      0x525
5835         do_facet mds1 lctl set_param fail_loc=0x525 fail_val=3
5836
5837         # disable last_xid logic by dropping link reply
5838         ln $DIR/$tdir/f1 $DIR/$tdir/f2 &
5839         sleep 1
5840
5841         #define OBD_FAIL_PTLRPC_ENQ_RESEND      0x534
5842         do_facet mds1 lctl set_param fail_loc=0x534
5843
5844         # RPC will race with its Resend and the Resend will sleep to let
5845         # the original lock to get granted & cancelled.
5846         #
5847         # AST_SENT is set artificially, so an explicit conflict is not needed
5848         #
5849         # The woken up Resend gets a new lock, but client does not wait for it
5850         stat $DIR/$tfile
5851         sleep $TIMEOUT
5852         do_facet mds1 lctl set_param fail_loc=0 fail_val=0
5853
5854         # Take a conflict to wait long enough to see the eviction
5855         touch $DIR2/$tfile
5856
5857         # let the client reconnect
5858         client_reconnect
5859         evict=$(do_facet client $LCTL get_param mdc.$FSNAME-MDT*.state |
5860           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
5861
5862         [ -z "$evict" ] || [[ $evict -le $before ]] ||
5863                 (do_facet client $LCTL get_param mdc.$FSNAME-MDT*.state;
5864                     error "eviction happened: $evict before:$before")
5865 }
5866 run_test 110 "do not grant another lock on resend"
5867
5868 test_111() {
5869         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
5870         [[ $(facet_active_host mds1) = $(facet_active_host mds2) ]] ||
5871                 skip "MDT0 and MDT1 should be on the same node"
5872
5873         mkdir $DIR1/$tdir
5874         $LFS mkdir -i 0 $DIR1/$tdir/mdt0dir
5875         $LFS mkdir -i 1 $DIR1/$tdir/mdt1dir
5876
5877         mkdir $DIR1/$tdir/mdt0dir/foodir
5878         touch $DIR1/$tdir/mdt0dir/foodir/{file1,file2}
5879
5880         $MULTIOP $DIR2/$tdir/mdt0dir/foodir/file2 Ow4096_c &
5881         MULTIOP_PID=$!
5882         ln $DIR1/$tdir/mdt0dir/foodir/file2 $DIR1/$tdir/mdt1dir/file2
5883
5884         #define OBD_FAIL_MDS_LINK_RENAME_RACE   0x18a
5885         do_facet mds1 $LCTL set_param fail_loc=0x8000018a
5886
5887         ln $DIR1/$tdir/mdt0dir/foodir/file2 $DIR1/$tdir/mdt1dir/file2x &
5888         sleep 1
5889
5890         rm $DIR2/$tdir/mdt1dir/file2
5891         sleep 1
5892
5893         mv $DIR2/$tdir/mdt0dir/foodir/file1 $DIR2/$tdir/mdt0dir/foodir/file2
5894         sleep 1
5895
5896         kill $MULTIOP_PID
5897         wait
5898         rm -r $DIR1/$tdir || error "Removing test dir failed"
5899 }
5900 run_test 111 "A racy rename/link an open file should not cause fs corruption"
5901
5902 test_112() {
5903         (( MDSCOUNT >= 2 )) ||
5904                 skip "We need at least 2 MDTs for this test"
5905
5906         (( MDS1_VERSION >= $(version_code 2.14.54) )) ||
5907                 skip "Need server version at least 2.14.54"
5908
5909         local rr
5910         local count
5911
5912         rr=$($LCTL get_param -n lmv.*.qos_threshold_rr | head -n1)
5913         rr=${rr%%%}
5914         stack_trap "$LCTL set_param lmv.*.qos_threshold_rr=$rr > /dev/null"
5915
5916         mkdir -p $DIR1/$tdir/s1/s2 || error "mkdir s2 failed"
5917         $LFS mkdir -i 0 $DIR1/$tdir/s1/s2/s3 || error "mkdir s3 failed"
5918         $LFS setdirstripe -D -i -1 --max-inherit-rr=0 $DIR1/$tdir/s1/s2/s3 ||
5919                 error "setdirstripe s3 failed"
5920         $LCTL set_param lmv.*.qos_threshold_rr=90
5921         mkdir $DIR2/$tdir/s1/s2/s3/d{1..64}
5922         count=$($LFS getstripe -m $DIR2/$tdir/s1/s2/s3/d* | grep ^0 | wc -l)
5923         (( count == 64 )) || error "only $count subdirs created on MDT0"
5924
5925         $LFS setdirstripe -D -i -1 --max-inherit-rr=3 $DIR1/$tdir/s1/s2/s3 ||
5926                 error "setdirstripe s3 failed"
5927         mkdir $DIR2/$tdir/s1/s2/s3/s{1..64}
5928         count=$($LFS getstripe -m $DIR2/$tdir/s1/s2/s3/s* | grep ^0 | wc -l)
5929         (( count == 64 / MDSCOUNT )) || error "$count subdirs created on MDT0"
5930 }
5931 run_test 112 "update max-inherit in default LMV"
5932
5933 test_113 () {
5934         (( MDS1_VERSION >= $(version_code 2.15.50) )) ||
5935                 skip "Need server version at least 2.15.50"
5936
5937         local instance
5938         local nid
5939
5940         instance=$($LFS getname -i $DIR1) ||
5941                 error "cannot get instance of $DIR1"
5942
5943         $LFS check osts $DIR1 | grep $instance ||
5944                 error "cannot find OSTs of instance $instance"
5945
5946         $LFS check osts $DIR1 | grep -v $instance
5947         if (( $? == 0 )); then
5948                 error "find OSTs other than instance $instance"
5949         fi
5950
5951         $LFS check osts | grep $instance ||
5952                 error "cannot find other OSTs"
5953
5954         nid=$(df $DIR2 | tail -1 | sed 's%:/.*%%') ||
5955                 error "cannot parse nid for $DIR2"
5956
5957         $LFS check mgts $DIR2 | grep MGC$nid ||
5958                 error "cannot find mgc of $nid"
5959
5960         $LFS check mgts $DIR2 | grep -v MGC$nid
5961         if (( $? == 0 )); then
5962                 error "find MGTs other than nid $nid"
5963         fi
5964 }
5965 run_test 113 "check servers of specified fs"
5966
5967 log "cleanup: ======================================================"
5968
5969 # kill and wait in each test only guarentee script finish, but command in script
5970 # like 'rm' 'chmod' may still be running, wait for all commands to finish
5971 # otherwise umount below will fail
5972 [ "$(mount | grep $MOUNT2)" ] && wait_update $HOSTNAME "fuser -m $MOUNT2" "" ||
5973         true
5974
5975 complete $SECONDS
5976 rm -f $SAMPLE_FILE
5977 check_and_cleanup_lustre
5978 exit_status