Viewing: sanityn.sh

#!/usr/bin/bash

set -e

ONLY=${ONLY:-"$*"}

SIZE=${SIZE:-40960}
OPENFILE=${OPENFILE:-openfile}
OPENUNLINK=${OPENUNLINK:-openunlink}
export TMP=${TMP:-/tmp}
MOUNT_2=${MOUNT_2:-"yes"}
CHECK_GRANT=${CHECK_GRANT:-"yes"}
GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}

LUSTRE=${LUSTRE:-$(dirname $0)/..}
. $LUSTRE/tests/test-framework.sh
init_test_env $@
init_logging

ALWAYS_EXCEPT="$SANITYN_EXCEPT "
[[ $(uname -r) = *"debug" ]] &&
	always_except LU-10870	40a

if [ $mds1_FSTYPE = "zfs" ]; then
	# LU-2829 / LU-2887 - make allowances for ZFS slowness
	TEST33_NFILES=${TEST33_NFILES:-1000}
fi

#                                  23   (min)"
[ "$SLOW" = "no" ] && EXCEPT_SLOW="33a"

build_test_filter

FAIL_ON_ERROR=false

SETUP=${SETUP:-:}
TRACE=${TRACE:-""}

check_and_setup_lustre

OSC=${OSC:-"osc"}

assert_DIR
rm -rf $DIR1/[df][0-9]* $DIR1/lnk $DIR/[df].${TESTSUITE}*

SAMPLE_FILE=$TMP/$(basename $0 .sh).junk
dd if=/dev/urandom of=$SAMPLE_FILE bs=1M count=1

# $RUNAS_ID may get set incorrectly somewhere else
[ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"

check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS

test_0() {
	local client2=${CLIENT2:-$HOSTNAME}
	local tmp=$(mktemp)

	printf 'a b\n' > $tmp

	do_node_vp "$HOSTNAME" printf 'a b\n' |
	diff $tmp - || error "do_node_vp mismatch"

	do_node_vp "$client2" printf 'a b\n' |
	diff $tmp - || error "do_node_vp mismatch"

	do_facet_vp mds1 printf 'a b\n' |
	diff $tmp - || error "do_facet_vp mismatch"

	printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' > $tmp

	do_node_vp "$HOSTNAME" printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
	diff $tmp - || error "do_node_vp mismatch"

	do_node_vp "$client2" printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
	diff $tmp - || error "do_node_vp mismatch"

	do_facet_vp mds1 printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
	diff $tmp - || error "do_facet_vp mismatch"
}
run_test 0 "do_node_vp() and do_facet_vp() do the right thing"

test_1() {
	touch $DIR1/$tfile
	[ -f $DIR2/$tfile ] || error "Check create"
	chmod 777 $DIR2/$tfile
	$CHECKSTAT -t file -p 0777 $DIR1/$tfile ||
		error "Check attribute update for 0777"

	chmod a-x $DIR2/$tfile
	$CHECKSTAT -t file -p 0666 $DIR1/$tfile ||
		error "Check attribute update for 0666"

	rm $DIR2/$tfile
	$CHECKSTAT -a $DIR1/$tfile ||
		error "Check unlink - removes file on other mountpoint"
}
run_test 1 "Check attribute updates on 2 mount points"

test_2a() {
	touch $DIR1/f2a
	ls -l $DIR2/f2a
	chmod 777 $DIR2/f2a
	$CHECKSTAT -t file -p 0777 $DIR1/f2a ||
		error "Either not file type or perms not 0777"
}
run_test 2a "check cached attribute updates on 2 mtpt's ========"

test_2b() {
	touch $DIR1/f2b
	ls -l $DIR2/f2b
	chmod 777 $DIR1/f2b
	$CHECKSTAT -t file -p 0777 $DIR2/f2b ||
		error "Either not file type or perms not 0777"
}
run_test 2b "check cached attribute updates on 2 mtpt's ========"

# NEED TO SAVE ROOT DIR MODE
test_2c() {
	chmod 777 $DIR1
	$CHECKSTAT -t dir -p 0777 $DIR2 ||
		error "Either not dir type or perms not 0777"
}
run_test 2c "check cached attribute updates on 2 mtpt's root ==="

test_2d() {
	chmod 755 $DIR1
	$CHECKSTAT -t dir -p 0755 $DIR2 ||
		error "Either not file type or perms not 0775"
}
run_test 2d "check cached attribute updates on 2 mtpt's root ==="

test_2e() {
	chmod 755 $DIR1
	ls -l $DIR1
	ls -l $DIR2
	chmod 777 $DIR1
	$RUNAS dd if=/dev/zero of=$DIR2/$tfile count=1 ||
		error "dd failed"
}
run_test 2e "check chmod on root is propagated to others"

test_2f() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	local MDTIDX=1
	local remote_dir=$tdir/remote_dir

	mkdir -p $DIR1/$tdir
	$LFS mkdir -i $MDTIDX $DIR1/$remote_dir ||
	           error "Create remote directory failed"

	touch $DIR1/$remote_dir/$tfile ||
		error "Create file under remote directory failed"
	chmod 777 $DIR1/$remote_dir/$tfile ||
		error "Chmod file under remote directory failed"

	$CHECKSTAT -t file -p 0777 $DIR2/$remote_dir/$tfile ||
		error "Check attr of file under remote directory failed"

	chown $RUNAS_ID:$RUNAS_GID $DIR1/$remote_dir/$tfile ||
		error "Chown file under remote directory failed"

	$CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR2/$remote_dir/$tfile ||
		error "Check owner of file under remote directory failed"

	cd $DIR2/$remote_dir || error "enter remote dir"
	rm -rf $DIR1/$remote_dir/$tfile ||
		error "Unlink remote directory failed"

	$CHECKSTAT -t file $DIR2/$remote_dir/$tfile &&
		error "unlink file still exists!"

	cd $DIR2/$tdir || error "exit remote dir"
	rm -rf $DIR1/$tdir || error "unlink directory failed"
}
run_test 2f "check attr/owner updates on DNE with 2 mtpt's"

test_2g() {
	dd if=/dev/zero of=$DIR1/$tfile oflag=sync bs=1M count=2

	local block1=$(stat $DIR1/$tfile | awk '/Blocks/ {print $4} ')
	cancel_lru_locks osc
	local block2=$(stat $DIR2/$tfile | awk '/Blocks/ {print $4} ')
	echo "$DIR1/$tfile has $block1 blocks"
	echo "$DIR2/$tfile has $block2 blocks"
	[ $block1 -eq $block2 ] || error "$block1 not equal to $block2"
}
run_test 2g "check blocks update on sync write"

test_3() {
	local target="this/is/good"
	ln -s $target $DIR1/$tfile || error "ln -s $target $DIR1/$tfile failed"
	[ "$(ls -l $DIR2/$tfile | sed -e 's/.* -> //')" = "$target" ] ||
		error "link $DIR2/$tfile not as expected"
}
run_test 3 "symlink on one mtpt, readlink on another ==========="

test_4() {
	multifstat $DIR1/f4 $DIR2/f4
}
run_test 4 "fstat validation on multiple mount points =========="

test_5() {
	mcreate $DIR1/f5
	$TRUNCATE $DIR2/f5 100
	$CHECKSTAT -t file -s 100 $DIR1/f5 ||
		error "Either not file type or size not equal to 100 bytes"
	rm $DIR1/f5
}
run_test 5 "create a file on one mount, truncate it on the other"

test_6() {
	openunlink $DIR1/$tfile $DIR2/$tfile || \
		error "openunlink $DIR1/$tfile $DIR2/$tfile"
}
run_test 6 "remove of open file on other node =================="

test_7() {
	local dir=d7
	opendirunlink $DIR1/$dir $DIR2/$dir || \
		error "opendirunlink $DIR1/$dir $DIR2/$dir"
}
run_test 7 "remove of open directory on other node ============="

test_8() {
	opendevunlink $DIR1/$tfile $DIR2/$tfile || \
		error "opendevunlink $DIR1/$tfile $DIR2/$tfile"
}
run_test 8 "remove of open special file on other node =========="

test_9a() {
	MTPT=1
	local dir
	> $DIR2/f9
	for C in a b c d e f g h i j k l; do
		dir=`eval echo \\$DIR$MTPT`
		echo -n $C >> $dir/f9
		[ "$MTPT" -eq 1 ] && MTPT=2 || MTPT=1
	done
	[ "`cat $DIR1/f9`" = "abcdefghijkl" ] || \
		error "`od -a $DIR1/f9` != abcdefghijkl"
}
run_test 9a "append of file with sub-page size on multiple mounts"

#LU-10681 - tiny writes & appending to sparse striped file
test_9b() {
	[[ $OSTCOUNT -ge 2 ]] || { skip "needs >= 2 OSTs"; return; }

	$LFS setstripe -c 2 -S 1M $DIR/$tfile
	echo "foo" >> $DIR/$tfile
	dd if=/dev/zero of=$DIR2/$tfile bs=1M count=1 seek=1 conv=notrunc ||
		error "sparse dd $DIR2/$tfile failed"
	echo "foo" >> $DIR/$tfile

	data=$(dd if=$DIR2/$tfile bs=1 count=3 skip=$((2 * 1048576)) conv=notrunc)
	echo "Data read (expecting 'foo')": $data
	[ "$data" = "foo" ] || error "append to sparse striped file failed"
}
run_test 9b "append to striped sparse file"

test_10a() {
	MTPT=1
	local dir
	OFFSET=0
	> $DIR2/f10
	for C in a b c d e f g h i j k l; do
		dir=`eval echo \\$DIR$MTPT`
		echo -n $C | dd of=$dir/f10 bs=1 seek=$OFFSET count=1
		[ "$MTPT" -eq 1 ] && MTPT=2 || MTPT=1
		OFFSET=`expr $OFFSET + 1`
	done
	[ "`cat $DIR1/f10`" = "abcdefghijkl" ] || \
		error "`od -a $DIR1/f10` != abcdefghijkl"
}
run_test 10a "write of file with sub-page size on multiple mounts "

test_10b() {
	# create a seed file
	yes "R" | head -c 4000 >$TMP/f10b-seed
	dd if=$TMP/f10b-seed of=$DIR1/f10b bs=3k count=1 || error "dd $DIR1"

	$TRUNCATE $DIR1/f10b 4096 || error "truncate 4096"

	dd if=$DIR2/f10b of=$TMP/f10b-lustre bs=4k count=1 || error "dd $DIR2"

	# create a test file locally to compare
	dd if=$TMP/f10b-seed of=$TMP/f10b bs=3k count=1 || error "dd random"
	$TRUNCATE $TMP/f10b 4096 || error "truncate 4096"
	cmp $TMP/f10b $TMP/f10b-lustre || error "file miscompare"
	rm $TMP/f10b $TMP/f10b-lustre $TMP/f10b-seed
}
run_test 10b "write of file with sub-page size on multiple mounts "

test_11() {
	test_mkdir $DIR1/d11
	multiop_bg_pause $DIR1/d11/f O_c || return 1
	MULTIPID=$!
	cp -p /bin/ls $DIR1/d11/f
	$DIR2/d11/f
	RC=$?
	kill -USR1 $MULTIPID
	wait $MULTIPID || error "wait for PID $MULTIPID failed"
	[ $RC -eq 0 ] && error || true
}
run_test 11 "execution of file opened for write should return error ===="

test_12() {
	DIR=$DIR DIR2=$DIR2 bash lockorder.sh
}
run_test 12 "test lock ordering (link, stat, unlink)"

test_13() {	# bug 2451 - directory coherency
	test_mkdir $DIR1/d13
	cd $DIR1/d13 || error "cd to $DIR1/d13 failed"
	ls
	( touch $DIR1/d13/f13 ) # needs to be a separate shell
	ls
	rm -f $DIR2/d13/f13 || error "Cannot remove $DIR2/d13/f13"
	ls 2>&1 | grep f13 && error "f13 shouldn't return an error (1)" || true
	# need to run it twice
	( touch $DIR1/d13/f13 ) # needs to be a separate shell
	ls
	rm -f $DIR2/d13/f13 || error "Cannot remove $DIR2/d13/f13"
	ls 2>&1 | grep f13 && error "f13 shouldn't return an error (2)" || true
}
run_test 13 "test directory page revocation"

test_14aa() {
	test_mkdir $DIR1/$tdir
	cp -p /bin/ls $DIR1/$tdir/$tfile
	multiop_bg_pause $DIR1/$tdir/$tfile Ow_c || return 1
	MULTIPID=$!

	$DIR2/$tdir/$tfile && error || true
	kill $MULTIPID
}
run_test 14aa "execution of file open for write returns -ETXTBSY"

test_14ab() {
	test_mkdir $DIR1/$tdir
	cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
	$DIR1/$tdir/sleep 60 &
	SLEEP_PID=$!
	$MULTIOP $DIR2/$tdir/sleep Oc && error "expected error, got success"
	kill $SLEEP_PID
}
run_test 14ab "open(RDWR) of executing file returns -ETXTBSY"

test_14b() { # bug 3192, 7040
	test_mkdir $DIR1/$tdir
	cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
	$DIR1/$tdir/sleep 60 &
	SLEEP_PID=$!
	$TRUNCATE $DIR2/$tdir/sleep 60 && kill -9 $SLEEP_PID && \
		error "expected truncate error, got success"
	kill $SLEEP_PID
	cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
}
run_test 14b "truncate of executing file returns -ETXTBSY ======"

test_14c() { # bug 3430, 7040
	test_mkdir $DIR1/$tdir
	cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
	$DIR1/$tdir/sleep 60 &
	SLEEP_PID=$!
	cp /etc/hosts $DIR2/$tdir/sleep && error "expected error, got success"
	kill $SLEEP_PID
	cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
}
run_test 14c "open(O_TRUNC) of executing file return -ETXTBSY =="

test_14d() { # bug 10921
	test_mkdir $DIR1/$tdir
	cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
	$DIR1/$tdir/sleep 60 &
	SLEEP_PID=$!
	log chmod
	chmod 600 $DIR1/$tdir/sleep || error "chmod failed"
	kill $SLEEP_PID
	cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
}
run_test 14d "chmod of executing file is still possible ========"

test_15() {	# bug 974 - ENOSPC
	echo "PATH=$PATH"
	bash oos2.sh $MOUNT1 $MOUNT2
	wait_delete_completed
	grant_error=$(dmesg | grep "< tot_grant")
	[ -z "$grant_error" ] || error "$grant_error"
}
run_test 15 "test out-of-space with multiple writers ==========="

COUNT=${COUNT:-2500}
# The FSXNUM reduction for ZFS is needed until ORI-487 is fixed.
# We don't want to skip it entirely, but ZFS is VERY slow and cannot
# pass a 2500 operation dual-mount run within the time limit.
if [ "$ost1_FSTYPE" = "zfs" ]; then
	FSXNUM=$((COUNT / 5))
	FSXP=1
elif [ "$SLOW" = "yes" ]; then
	FSXNUM=$((COUNT * 5))
	FSXP=500
else
	FSXNUM=$COUNT
	FSXP=100
fi

test_16a() {
	local file1=$DIR1/$tfile
	local file2=$DIR2/$tfile
	local stripe_size=$(do_facet $SINGLEMDS \
		"$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")

	check_set_fallocate

	# to allocate grant because it may run out due to test_15.
	$LFS setstripe -c -1 $file1
	dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
	dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
	rm -f $file1

	$LFS setstripe -c -1 $file1 # b=10919
	$FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 $file1 $file2 ||
		error "fsx failed"
	rm -f $file1

	# O_DIRECT reads and writes must be aligned to the PAGE_SIZE.
	$FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 -Z -r $PAGE_SIZE \
		-w $PAGE_SIZE $file1 $file2 || error "fsx with O_DIRECT failed."
}
run_test 16a "$FSXNUM iterations of dual-mount fsx"

# Consistency check for tiny writes, LU-9409
test_16b() {
	local file1=$DIR1/$tfile
	local file2=$DIR2/$tfile
	local stripe_size=($($LFS getstripe -S $DIR))

	check_set_fallocate

	# to allocate grant because it may run out due to test_15.
	lfs setstripe -c -1 $file1
	dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync ||
		error "dd failed writing to file=$file1"
	dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync ||
		error "dd failed writing to file=$file2"
	rm -f $file1

	lfs setstripe -c -1 $file1 # b=10919
	# -o is set to 8192 because writes < 1 page and between 1 and 2 pages
	# create a mix of tiny writes & normal writes
	$FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -o 8192 -S 0 \
		$file1 $file2 || error "fsx with tiny write failed."
}
run_test 16b "$FSXNUM iterations of dual-mount fsx at small size"

test_16c() {
	local file1=$DIR1/$tfile
	local file2=$DIR2/$tfile
	local stripe_size=$(do_facet $SINGLEMDS \
		"$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")

	[ "$ost1_FSTYPE" != ldiskfs ] && skip "dio on ldiskfs only"

	check_set_fallocate

	# to allocate grant because it may run out due to test_15.
	$LFS setstripe -c -1 $file1
	dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
	dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
	rm -f $file1
	wait_delete_completed

	local osts=$(osts_nodes)
	if ! get_osd_param $osts '' read_cache_enable >/dev/null; then
		skip "not cache-capable obdfilter"
	fi

	set_osd_param $osts '' read_cache_enable 0
	set_osd_param $osts '' writethrough_cache_enable 0

	$LFS setstripe -c -1 $file1 # b=10919
	$FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 $file1 $file2 ||
		error "fsx failed"
	rm -f $file1

	set_osd_param $osts '' read_cache_enable 1
	set_osd_param $osts '' writethrough_cache_enable 1

	return 0
}
run_test 16c "verify data consistency on ldiskfs with cache disabled (b=17397)"

test_16d() {
	local file1=$DIR1/$tfile
	local file2=$DIR2/$tfile
	local file3=$DIR1/file
	local tmpfile=$(mktemp)
	local stripe_size=$(do_facet $SINGLEMDS \
		"$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")

	# to allocate grant because it may run out due to test_15.
	$LFS setstripe -c -1 $file1
	stack_trap "rm -f $file1 $file2 $file3 $tmpfile"
	dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
	dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
	rm -f $file1

	$LFS setstripe -c -1 $file1 # b=10919
	$LCTL set_param ldlm.namespaces.*.lru_size=clear

	# direct write on one client and direct read from another
	dd if=/dev/urandom of=$file1 bs=1M count=100 oflag=direct
	dd if=$file2 of=$tmpfile iflag=direct bs=1M
	diff $file1 $tmpfile || error "file different(1)"
	rm -f $file1

	# buffer write on one client, but direct read from another
	dd if=$tmpfile of=$file1 bs=1M count=100
	dd if=$file2 of=$file3 bs=1M iflag=direct count=100
	diff $file3 $tmpfile || error "file different(2)"

	rm -f $file3 $file2 $file1
	# direct write on one client
	dd if=$tmpfile of=$file1 bs=1M count=100 oflag=direct
	# buffer read from another client
	dd if=$file2 of=$file3 bs=1M count=100
	diff $file3 $tmpfile || error "file different(3)"
}
run_test 16d "Verify DIO and buffer IO with two clients"

test_16e() { # LU-13227
	# issue:	LU-14314

	(( "$MDS1_VERSION" >= $(version_code 2.13.53) )) ||
		skip "Need MDS version at least 2.13.53"

	local file1=$DIR1/$tfile
	local file2=$DIR2/$tfile

	# client1 write 10M data
	dd if=/dev/zero of=$file1 bs=1M count=10
	# drop locks
	cancel_lru_locks osc > /dev/null
	# use lockahead to generate one PW lock to keep LVB loaded.
	$LFS ladvise -a lockahead --start 0 --length 1M \
		--mode WRITE $file1
	# direct write to extend file size on client2
	dd if=/dev/zero of=$file2 bs=1M seek=20 count=1 \
		oflag=direct conv=notrunc
	local filesize=$(stat -c %s $file2)
	[ "$filesize" -eq 22020096 ] ||
		error "expected filesize 22020096 got $filesize"
	rm -f $file1
}
run_test 16e "Verify size consistency for O_DIRECT write"

test_16f() { # LU-14541
	local file1=$DIR1/$tfile
	local file2=$DIR2/$tfile
	local duration=20
	local status

	(( $MDS1_VERSION > $(version_code 2.15.51) )) ||
		skip "Need MDS version at least 2.15.51"

	timeout --preserve-status --signal=USR1 $duration \
		rw_seq_cst_vs_drop_caches $file1 $file2
	status=$?

	case $((status & 0x7f)) in
		0)
			echo OK # Computers must be fast now.
			;;
		6) # SIGABRT
			error "sequential consistency violation detected"
			;;
		10) # SIGUSR1
			echo TIMEOUT # This is fine.
			;;
		*)
			error "strange status '$status'"
			;;
	esac

	rm -f $file1
}
run_test 16f "rw sequential consistency vs drop_caches"

test_16g() {
	local file1=$DIR1/$tfile
	local file2=$DIR2/$tfile
	local duration=20
	local status

	timeout --preserve-status --signal=USR1 $duration \
		rw_seq_cst_vs_drop_caches -m $file1 $file2
	status=$?

	case $((status & 0x7f)) in
		0)
			echo OK # Computers must be fast now.
			;;
		6) # SIGABRT
			error "sequential consistency violation detected"
			;;
		10) # SIGUSR1
			echo TIMEOUT # This is fine.
			;;
		*)
			error "strange status '$status'"
			;;
	esac

	rm -f $file1
}
run_test 16g "mmap rw sequential consistency vs drop_caches"

test_16h() {
	local tf=$DIR/$tdir/$tfile
	local tf2=$DIR2/$tdir/$tfile
	local cmd="$MMAP_CAT $tf | od -x | tail -q -n4"
	local cmd2="$MMAP_CAT $tf2 | od -x | tail -q -n4"

	test_mkdir $DIR/$tdir

	# create file and populate data
	cp /etc/passwd $tf || error "cp failed"

	local size=$(stat -c %s $tf)
	c1=$(eval $cmd)
	c2=$(eval $cmd2)
	if [[ "$c1" != "$c2" ]]; then
		echo "  ------- mount 1 read --------"
		echo $c1
		echo "  ------- mount 2 read --------"
		echo $c2
		error "content mismatch"
	fi

	echo "  ------- before truncate --------"
	echo $c1

	# truncate file
	$TRUNCATE $tf $((size / 2)) || error "truncate file"

	#cancel_lru_locks
	echo "  ------- after truncate --------"

	# repeat the comparison
	c1=$(eval $cmd)
	c2=$(eval $cmd2)
	if [[ "$c1" != "$c2" ]]; then
		echo "  ------- mount 1 read --------"
		echo $c1
		echo "  ------- mount 2 read --------"
		echo $c2
		error "content mismatch after truncate"
	fi
	echo $c2
}
run_test 16h "mmap read after truncate file"

test_16i() {
	local tf=$DIR/$tdir/$tfile
	local tf2=$DIR2/$tdir/$tfile

	test_mkdir $DIR/$tdir

	# create file and populate data
	cp /etc/passwd $tf || error "cp failed"

	local size=$(stat -c %s $tf)

	c1=$(dd if=$tf bs=1 2>/dev/null | od -x | tail -q -n4)
	c2=$(dd if=$tf2 bs=1 2>/dev/null | od -x | tail -q -n4)

	if [[ "$c1" != "$c2" ]]; then
		echo "  ------- mount 1 read --------"
		echo $c1
		echo "  ------- mount 2 read --------"
		echo $c2
		error "content mismatch"
	fi

	echo "  ------- before truncate --------"
	echo $c1

	# truncate file
	$TRUNCATE $tf $((size / 2)) || error "truncate file"

	echo "  ------- after truncate --------"

	# repeat the comparison
	c1=$(dd if=$tf bs=1 2>/dev/null | od -x | tail -q -n4)
	c2=$(dd if=$tf2 bs=1 2>/dev/null | od -x | tail -q -n4)

	if [[ "$c1" != "$c2" ]]; then
		echo "  ------- mount 1 read --------"
		echo $c1
		echo "  ------- mount 2 read --------"
		echo $c2
		error "content mismatch after truncate"
	fi
	echo $c2
}
run_test 16i "read after truncate file"

test_16j()
{
	(( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
	(( $OST1_VERSION >= $(version_code v2_15_57-52-g43c3a804fe) )) ||
		skip "Need OST version at least 2.15.57.52 for unaligned DIO"

	local stripe_size=$((1024 * 1024)) #1 MiB
	# Max i/o below is ~ 4 * stripe_size, so this gives ~5 i/os
	local file_size=$((25 * stripe_size))
	local bsizes

	$LFS setstripe -c 2 -S $stripe_size $DIR/$tfile.1
	stack_trap "rm -f $DIR/$tfile.1"

	# Just a bit bigger than the largest size in the test set below
	dd if=/dev/urandom bs=$file_size count=1 of=$DIR/$tfile.1 ||
		error "buffered i/o to create file failed"

	if zfs_or_rotational; then
		# DIO on ZFS can take up to 2 seconds per IO
		# rotational is better, but still slow.
		# Limit testing on those media to larger sizes
		bsizes="$((stripe_size - PAGE_SIZE)) $stripe_size \
			$((stripe_size + 1024))"
	else
		bsizes="$((PAGE_SIZE / 4)) $((PAGE_SIZE - 1024)) \
			$((PAGE_SIZE - 1)) $PAGE_SIZE $((PAGE_SIZE + 1024)) \
			$((PAGE_SIZE * 3/2)) $((PAGE_SIZE * 4)) \
			$((stripe_size - 1)) $stripe_size \
			$((stripe_size + 1)) $((stripe_size * 3/2)) \
			$((stripe_size * 4)) $((stripe_size * 4 + 1))"
	fi

	# 1 process (BIO or DIO) on each client
	for bs in $bsizes; do
		$LFS setstripe -c 2 -S $stripe_size $DIR/$tfile.2
		echo "bs: $bs, file_size $file_size"
		dd if=$DIR/$tfile.1 bs=$((bs *2 )) of=$DIR/tfile.2 \
			conv=notrunc oflag=direct iflag=direct &
		pid_dio1=$!
		# Buffered I/O with similar but not the same block size
		dd if=$DIR2/$tfile.1 bs=$((bs * 2)) of=$DIR2/$tfile.2 \
			conv=notrunc &
		pid_bio2=$!
		wait $pid_dio1
		rc1=$?
		wait $pid_bio2
		rc2=$?
		if (( rc1 != 0 )); then
			error "dio copy 1 w/bsize $bs failed: $rc1"
		fi
		if (( rc2 != 0 )); then
			error "buffered copy 2 w/bsize $bs failed: $rc2"
		fi

		$CHECKSTAT -t file -s $file_size $DIR/$tfile.2 ||
			error "size incorrect"
		$CHECKSTAT -t file -s $file_size $DIR2/$tfile.2 ||
			error "size incorrect - mount 2"
		cmp --verbose $DIR/$tfile.1 $DIR/$tfile.2 ||
			error "files differ, bsize $bs"
		cmp --verbose $DIR2/$tfile.1 $DIR2/$tfile.2 ||
			error "files differ, bsize $bs - mount 2"
		rm -f $DIR/$tfile.2
	done

	# 2 processes - both DIO and BIO - on each client
	for bs in $bsizes; do
		$LFS setstripe -c 2 -S $stripe_size $DIR/$tfile.2
		echo "bs: $bs, file_size $file_size"
		# Client 1
		dd if=$DIR/$tfile.1 bs=$((bs *2 )) of=$DIR/tfile.2 \
			conv=notrunc oflag=direct iflag=direct &
		pid_dio1=$!
		# Buffered I/O with similar but not the same block size
		dd if=$DIR/$tfile.1 bs=$((bs * 2)) of=$DIR/$tfile.2 \
			conv=notrunc &
		pid_bio2=$!
		# Client 2
		dd if=$DIR2/$tfile.1 bs=$((bs *2 )) of=$DIR2/tfile.2 \
			conv=notrunc oflag=direct iflag=direct &
		pid_dio3=$!
		# Buffered I/O with similar but not the same block size
		dd if=$DIR2/$tfile.1 bs=$((bs * 2)) of=$DIR2/$tfile.2 \
			conv=notrunc &
		pid_bio4=$!
		wait $pid_dio1
		rc1=$?
		wait $pid_bio2
		rc2=$?
		wait $pid_dio3
		rc3=$?
		wait $pid_bio4
		rc4=$?

		if (( rc1 != 0 )); then
			error "dio copy 1 w/bsize $bs failed: $rc1"
		fi
		if (( rc2 != 0 )); then
			error "buffered copy 2 w/bsize $bs failed: $rc2"
		fi
		if (( rc3 != 0 )); then
			error "dio copy 3 w/bsize $bs failed: $rc1"
		fi
		if (( rc4 != 0 )); then
			error "buffered copy 4 w/bsize $bs failed: $rc2"
		fi

		$CHECKSTAT -t file -s $file_size $DIR/$tfile.2 ||
			error "size incorrect"
		$CHECKSTAT -t file -s $file_size $DIR2/$tfile.2 ||
			error "size incorrect - mount 2"
		cmp --verbose $DIR/$tfile.1 $DIR/$tfile.2 ||
			error "files differ, bsize $bs"
		cmp --verbose $DIR2/$tfile.1 $DIR2/$tfile.2 ||
			error "files differ, bsize $bs - mount 2"
		rm -f $DIR/$tfile.2
	done
}
run_test 16j "race dio with buffered i/o"

test_16k() {
	local fsxN=${FSX_NP:-5}
	local fsxNops=${FSX_NOPS:-10000}
	local fsxNparams=${FSXPARAMS_16k:-""}
	local dropsleep=${DROP_SLEEP:-3}
	local dpipd
	local -a fsxpids
	local cmd

	[ "$SLOW" = "no" ] && fsxNops=1000

	while true; do
		echo 3 > /proc/sys/vm/drop_caches
		sleep $dropsleep
	done &
	dpipd=$!
	stack_trap "kill -9 $dpipd"

	for ((i = 1; i <= fsxN; i++)); do
		cmd="$FSX $fsxNparams -N $fsxNops $DIR/fsxfile.${i} -l $((64 * 1024 * 1024))"
		echo "+ $cmd"
		eval $cmd &
		fsxpids[$i]=$!
	done
	for ((i = 1; i <= fsxN; i++)); do
		wait ${fsxpids[$i]} && echo "+ fsx $i done: rc=$?" ||
			error "- fsx $i FAILURE! rc=$?"
		date
	done
}
run_test 16k "Parallel FSX and drop caches should not panic"

test_17() { # bug 3513, 3667
	remote_ost_nodsh && skip "remote OST with nodsh" && return

	lfs setstripe $DIR1/$tfile -i 0 -c 1
	cp $SAMPLE_FILE $DIR1/$tfile
	cancel_lru_locks osc > /dev/null
	#define CFS_FAIL_ONCE|OBD_FAIL_LDLM_CREATE_RESOURCE    0x30a
	do_facet ost1 lctl set_param fail_loc=0x8000030a
	ls -ls $DIR1/$tfile | awk '{ print $1,$6 }' > $DIR1/$tfile-1 & \
	ls -ls $DIR2/$tfile | awk '{ print $1,$6 }' > $DIR2/$tfile-2
	wait
	diff -u $DIR1/$tfile-1 $DIR2/$tfile-2 || error "files are different"
}
run_test 17 "resource creation/LVB creation race ==============="

test_18() {
	# turn e.g. ALWAYS_EXCEPT="18c" into "-e 3"
	local idx
	local excepts=
	for idx in {a..z}; do
		local ptr=EXCEPT_ALWAYS_18$idx
		[ x${!ptr} = xtrue ] || continue

		excepts="$excepts -e $(($(printf %d \'$idx)-96))"
			done

	excepts="$excepts -e 7 -e 8 -e 9"
	$LUSTRE/tests/mmap_sanity -d $MOUNT1 -m $MOUNT2 $excepts ||
		error "mmap_sanity test failed"
	sync; sleep 1; sync
}
run_test 18 "mmap sanity check ================================="

test_19() { # bug3811
	local node=$(facet_active_host ost1)
	local device="$FSNAME-OST*"

	[ "x$DOM" = "xyes" ] && node=$(facet_active_host $SINGLEMDS) &&
		device="$FSNAME-MDT*"

	# check whether obdfilter is cache capable at all
	get_osd_param $node $device read_cache_enable >/dev/null ||
		skip "not cache-capable obdfilter"

	local max=$(get_osd_param $node $device readcache_max_filesize |\
		head -n 1)
	set_osd_param $node $device readcache_max_filesize 4096
	dd if=/dev/urandom of=$TMP/$tfile bs=512k count=32
	local SUM=$(cksum $TMP/$tfile | cut -d" " -f 1,2)
	cp $TMP/$tfile $DIR1/$tfile
	for i in `seq 1 20`; do
		[ $((i % 5)) -eq 0 ] && log "$testname loop $i"
		cancel_lru_locks $OSC > /dev/null
		cksum $DIR1/$tfile | cut -d" " -f 1,2 > $TMP/sum1 & \
		cksum $DIR2/$tfile | cut -d" " -f 1,2 > $TMP/sum2
		wait
		[ "$(cat $TMP/sum1)" = "$SUM" ] || \
			error "$DIR1/$tfile $(cat $TMP/sum1) != $SUM"
		[ "$(cat $TMP/sum2)" = "$SUM" ] || \
			error "$DIR2/$tfile $(cat $TMP/sum2) != $SUM"
	done
	set_osd_param $node $device readcache_max_filesize $max
	rm $DIR1/$tfile
}
run_test 19 "test concurrent uncached read races ==============="

test_20() {
	test_mkdir $DIR1/$tdir
	cancel_lru_locks
	CNT=$($LCTL get_param -n llite.*.dump_page_cache | wc -l)
	$MULTIOP $DIR1/$tdir/$tfile Ow8190c
	$MULTIOP $DIR2/$tdir/$tfile Oz8194w8190c
	$MULTIOP $DIR1/$tdir/$tfile Oz0r8190c
	cancel_lru_locks
	CNT2=$($LCTL get_param -n llite.*.dump_page_cache | wc -l)
	[[ $CNT2 == $CNT ]] ||
		error $((CNT2 - CNT))" page left in cache after lock cancel"
}
run_test 20 "test extra readahead page left in cache ===="

cleanup_21() {
	trap 0
	umount $DIR1/$tdir
}

test_21() { # Bug 5907
	test_mkdir $DIR1/$tdir
	mount /etc $DIR1/$tdir --bind || error "mount failed" # Poor man's mount.
	trap cleanup_21 EXIT
	rmdir -v $DIR1/$tdir && error "Removed mounted directory"
	rmdir -v $DIR2/$tdir && echo "Removed mounted directory from another mountpoint, needs to be fixed"
	test -d $DIR1/$tdir || error "Mounted directory disappeared"
	cleanup_21
	test -d $DIR2/$tdir || test -d $DIR1/$tdir && error "Removed dir still visible after umount"
	true
}
run_test 21 " Try to remove mountpoint on another dir ===="

test_23() { # Bug 5972
	local at_diff=$(do_facet $SINGLEMDS \
		$LCTL get_param -n mdd.*MDT0000*.atime_diff | head -n1)
	echo "atime should be updated while another read" > $DIR1/$tfile

	# clear the lock(mode: LCK_PW) gotten from creating operation
	cancel_lru_locks $OSC
	time1=$(date +%s)
	echo "now is $time1"
	sleep $((at_diff + 1))

	echo "starting reads"
	multiop_bg_pause $DIR1/$tfile or20_c || return 1
	# with SOM and opencache enabled, we need to close a file and cancel
	# open lock to get atime propogated to MDS
	kill -USR1 $! || return 2
	cancel_lru_locks mdc

	time2=$(stat -c "%X" $DIR/$tfile)
	echo "new atime is $time2"

	[ $time2 -gt $time1 ] || error "atime was not updated"
	rm -f $DIR1/$tfile || error "rm -f $DIR1/$tfile failed"
	true
}
run_test 23 " others should see updated atime while another read===="

test_24a() {
	touch $DIR1/$tfile
	lfs df || error "lfs df failed"
	lfs df -ih || error "lfs df -ih failed"
	lfs df -h $DIR1 || error "lfs df -h $DIR1 failed"
	lfs df -i $DIR2 || error "lfs df -i $DIR2 failed"
	lfs df $DIR1/$tfile || error "lfs df $DIR1/$tfile failed"
	lfs df -ih $DIR2/$tfile || error "lfs df -ih $DIR2/$tfile failed"

	OSC=`lctl dl | awk '/-osc-|OSC.*MNT/ {print $4}' | head -n 1`
#	OSC=`lctl dl | awk '/-osc-/ {print $4}' | head -n 1`
	lctl --device %osc deactivate
	lfs df -i || error "lfs df -i with deactivated OSC failed"
	lctl --device %osc activate
	lfs df || error "lfs df with reactivated OSC failed"
}
run_test 24a "lfs df [-ih] [path] test ========================="

test_24b() {
	touch $DIR1/$tfile
	fsnum=$(lfs_df | grep -c "summary")
	[ $fsnum -eq 2 ] || error "lfs df shows $fsnum != 2 filesystems."
}
run_test 24b "lfs df should show both filesystems ==============="

test_25a() {
	local acl=$(lctl get_param -n mdc.*MDT0000-mdc-*.connect_flags |
								grep -c acl)
	[ "$acl" -lt 1 ] && skip "must have acl, skipping" && return

	mkdir -p $DIR1/$tdir
	touch $DIR1/$tdir/f1 || error "touch $DIR1/$tdir/f1"
	chmod 0755 $DIR1/$tdir/f1 || error "chmod 0755 $DIR1/$tdir/f1"

	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #1"
	setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
		error "setfacl $DIR2/$tdir #1"
	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #2"
	setfacl -m u:$RUNAS_ID:r-x -m g:$RUNAS_GID:r-x $DIR1/$tdir ||
		error "setfacl $DIR2/$tdir #2"
	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #3"
	setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
		error "setfacl $DIR2/$tdir #3"
	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #4"
	setfacl -x u:$RUNAS_ID: -x g:$RUNAS_GID: $DIR1/$tdir ||
		error "setfacl $DIR2/$tdir #4"
	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #5"

	rm -rf $DIR1/$tdir
}
run_test 25a "change ACL on one mountpoint be seen on another ==="

test_25b() {
	local acl=$(lctl get_param -n mdc.*MDT0000-mdc-*.connect_flags |
							grep -c acl)
	[ "$acl" -lt 1 ] && skip "must have acl, skipping" && return

	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return

	rm -rf $DIR1/$tdir
	$LFS mkdir -i 1 $DIR1/$tdir
	touch $DIR1/$tdir/f1 || error "touch $DIR1/$tdir/f1"
	chmod 0755 $DIR1/$tdir/f1 || error "chmod 0755 $DIR1/$tdir/f1"

	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #1"
	setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
		error "setfacl $DIR2/$tdir #1"
	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #2"
	setfacl -m u:$RUNAS_ID:r-x -m g:$RUNAS_GID:r-x $DIR1/$tdir ||
		error "setfacl $DIR2/$tdir #2"
	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #3"
	setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
		error "setfacl $DIR2/$tdir #3"
	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #4"
	setfacl -x u:$RUNAS_ID: -x g:$RUNAS_GID: $DIR1/$tdir ||
		error "setfacl $DIR2/$tdir #4"
	$RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #5"

	rm -rf $DIR1/$tdir
}
run_test 25b "change ACL under remote dir on one mountpoint be seen on another"

test_26a() {
	utime $DIR1/f26a -s $DIR2/f26a || error "utime failed for $DIR1/f26a"
}
run_test 26a "allow mtime to get older"

test_26b() {
	touch $DIR1/$tfile
	sleep 1
	echo "aaa" >> $DIR1/$tfile
	sleep 1
	chmod a+x $DIR2/$tfile
	mt1=$(stat -c %Y $DIR1/$tfile)
	mt2=$(stat -c %Y $DIR2/$tfile)

        if [[ "$mt1" != "$mt2" ]]; then
                error "not equal mtime, client1: '$mt1', client2: '$mt2'."
        fi
}
run_test 26b "sync mtime between ost and mds"

test_26c() {
	(( $MDS1_VERSION >= $(version_code 2.15.61) )) ||
		skip "Need MDS version at least 2.15.61"

	multiop_bg_pause $DIR1/$tfile O_c || error "multiop failed"
	MULTIPID=$!
	touch -am -d @978261179 $DIR2/$tfile
	kill -USR1 $MULTIPID
	wait $MULTIPID || error "wait for PID $MULTIPID failed"
	sleep 1

	echo
	stat $DIR/$tfile
	local times="$(stat -c"%X %Y" $DIR/$tfile)"
	[[ "$times" == "978261179 978261179" ]] ||
		error "[am]times are not set in past on $DIR: $times"
	echo
	stat $DIR2/$tfile
	local times="$(stat -c"%X %Y" $DIR2/$tfile)"
	[[ "$times" == "978261179 978261179" ]] ||
		error "[am]times are not set in past on $DIR2: $times"
	echo
}
run_test 26c "set-in-past on open file is not lost on close"

test_27() {
	cancel_lru_locks $OSC
	lctl clear
	dd if=/dev/zero of=$DIR2/$tfile bs=$((4096+4))k conv=notrunc count=4 seek=3 &
	DD2_PID=$!
	sleep 0.5
	log "dd 1 started"

	dd if=/dev/zero of=$DIR1/$tfile bs=$((16384-1024))k conv=notrunc count=1 seek=4 &
	DD1_PID=$!
	log "dd 2 started"

	sleep 1
	dd if=/dev/zero of=$DIR1/$tfile bs=8k conv=notrunc count=1 seek=0
	log "dd 3 finished"
	lctl set_param -n ldlm.dump_namespaces ""
	wait $DD1_PID $DD2_PID
	[ $? -ne 0 ] && lctl dk $TMP/debug || true
}
run_test 27 "align non-overlapping extent locks from request ==="

test_30() { #b=11110, LU-2523
	test_mkdir $DIR1/$tdir
	cp -f /bin/bash $DIR1/$tdir/bash
	bash -c 'sleep 1; rm -f $DIR2/$tdir/bash; cp /bin/bash $DIR2/$tdir' &
	$DIR1/$tdir/bash -c 'sleep 2;
		openfile -f O_RDONLY /proc/$$/exe >& /dev/null; echo $?'
	wait
	true
}
run_test 30 "recreate file race"

test_31a() {
	test_mkdir $DIR1/$tdir
	local writes=$(LANG=C dd if=/dev/zero of=$DIR/$tdir/$tfile \
		       count=1 2>&1 | awk 'BEGIN { FS="+" } /out/ {print $1}')
	#define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE   0x314
	lctl set_param fail_loc=0x314
	local reads=$(LANG=C dd if=$DIR2/$tdir/$tfile of=/dev/null 2>&1 |
		      awk 'BEGIN { FS="+" } /in/ {print $1}')
	[ $reads -eq $writes ] || error "read" $reads "blocks, must be" $writes
}
run_test 31a "voluntary cancel / blocking ast race=============="

test_31b() {
	remote_ost || { skip "local OST" && return 0; }
	remote_ost_nodsh && skip "remote OST w/o dsh" && return 0

	# make sure there is no local locks due to destroy
	wait_mds_ost_sync || error "wait_mds_ost_sync()"
	wait_delete_completed || error "wait_delete_completed()"

	test_mkdir $DIR1/$tdir
        lfs setstripe $DIR/$tdir/$tfile -i 0 -c 1
        cp /etc/hosts $DIR/$tdir/$tfile
        #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE   0x314
        lctl set_param fail_loc=0x314
        #define OBD_FAIL_LDLM_OST_FAIL_RACE      0x316
        do_facet ost1 lctl set_param fail_loc=0x316
        # Don't crash kernel
        cat $DIR2/$tdir/$tfile > /dev/null 2>&1
        lctl set_param fail_loc=0
        do_facet ost1 lctl set_param fail_loc=0
        # cleanup: reconnect the client back
        df $DIR2
}
run_test 31b "voluntary OST cancel / blocking ast race=============="

#LU-14949 - multi-client version of the test 31r in sanity.
test_31r() {
	touch $DIR/$tfile.target
	touch $DIR/$tfile.source

	ls -l $DIR/$tfile.target # cache it for sure

	#OBD_FAIL_LLITE_OPEN_DELAY 0x1419
	$LCTL set_param fail_loc=0x1419 fail_val=3
	cat $DIR/$tfile.target &
	CATPID=$!

	# Guarantee open is waiting before we get here
	sleep 1
	mv $DIR2/$tfile.source $DIR2/$tfile.target

	wait $CATPID
	RC=$?
	if [[ $RC -ne 0 ]]; then
		error "open with cat failed, rc=$RC"
	fi
}
run_test 31r "open-rename(replace) race"

test_31s() {
	local pid ls_str

	mkdir_on_mdt0 $DIR/$tdir
	touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
	( cd $DIR/$tdir; tail -f $tfile || $MULTIOP . Dc) & pid=$!
	sleep 0.5

	stack_trap "pkill -P $pid 2> /dev/null" ERR

	mv $DIR2/$tdir $DIR2/$tdir.old || error "mv $tdir $tdir.old failed"
	mkdir_on_mdt0 $DIR2/$tdir || error "mkdir $DIR2/$tdir failed"
	mv $DIR2/$tdir.old $DIR2/$tdir/ || error "mv $tdir.old $tdir/ failed"

	pkill -P $pid || error "fail to stop $pid"
	wait $pid

	ls_str="$(ls  $DIR/$tdir)" || error "ls $DIR/$tdir failed"
	[[ "$ls_str" == "$tdir.old" ]] ||
		error "invalid dentry $DIR/$tdir: $ls_str != $tdir.old"
}
run_test 31s "open should not revalidate invalid dentry"

test_31t() {
	local pid i i_new

	touch $DIR/$tfile || error "touch $tfile failed"

	$MULTIOP $DIR/$tfile o_Sc & pid=$!
	stack_trap "kill $pid 2> /dev/null" ERR
	sleep 1

	mv $DIR2/$tfile $DIR2/$tfile.old || error "mv $tfile $tfile.old failed"
	touch $DIR2/$tfile || error "touch $tfile failed"
	i_new=$(stat -c "%i" $DIR2/$tfile) || error "stat $tfile failed"

	kill -SIGUSR1 $pid || error "fail to stop $pid"
	wait $pid

	i=$(stat -c "%i" $DIR/$tfile) || error "stat $tfile failed"
	(( i == i_new )) ||
		error "invalid dentry $DIR/$tfile, not updated (inode: $i != $i_new)"
}
run_test 31t "getattr should not revalidate invalid dentry"

test_32b() { # bug 11270
	remote_ost_nodsh && skip "remote OST with nodsh" && return
	(( $OST1_VERSION < $(version_code 2.17.53) )) ||
		skip "max_nolock_bytes is removed >= 2.17.53"

	local node
	local facets=$(get_facets OST)
	local p="$TMP/$TESTSUITE-$TESTNAME.parameters"

	save_lustre_params $facets \
		"ldlm.namespaces.filter-*.max_nolock_bytes" > $p
	save_lustre_params $facets \
		"ldlm.namespaces.filter-*.contended_locks" >> $p
	save_lustre_params $facets \
		"ldlm.namespaces.filter-*.contention_seconds" >> $p
	clear_stats $OSC.*.${OSC}_stats
	local osts=$(osts_nodes)

	# agressive lockless i/o settings
	do_nodes $osts \
		"lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
			ldlm.namespaces.filter-*.contended_locks=0 \
			ldlm.namespaces.filter-*.contention_seconds=60"
	for i in {1..5}; do
		dd if=/dev/zero of=$DIR1/$tfile bs=4k count=1 conv=notrunc > \
			/dev/null 2>&1
		dd if=/dev/zero of=$DIR2/$tfile bs=4k count=1 conv=notrunc > \
			/dev/null 2>&1
	done
	[ $(calc_stats $OSC.*.${OSC}_stats lockless_write_bytes) -ne 0 ] ||
		error "lockless i/o was not triggered"
	# disable lockless i/o (it is disabled by default)
	# set contention_seconds to 0 at client too, otherwise Lustre still
	# remembers lock contention
	do_nodes $osts \
		"lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
			ldlm.namespaces.filter-*.contended_locks=32 \
			ldlm.namespaces.filter-*.contention_seconds=0"
	clear_stats $OSC.*.${OSC}_stats
	for i in {1..1}; do
		dd if=/dev/zero of=$DIR1/$tfile bs=4k count=1 conv=notrunc > \
			/dev/null 2>&1
		dd if=/dev/zero of=$DIR2/$tfile bs=4k count=1 conv=notrunc > \
			/dev/null 2>&1
	done
	[ $(calc_stats $OSC.*.${OSC}_stats lockless_write_bytes) -eq 0 ] ||
		error "lockless i/o works when disabled"
	rm -f $DIR1/$tfile
	restore_lustre_params <$p
	rm -f $p
}
run_test 32b "lockless i/o"

test_32c() {
	# need only one client & no parallel, to keep 'contentions' correct
	(( ${CLIENTCOUNT:-1} == 1 )) || skip "need only one client"
	[[ $PARALLEL != "yes" ]] || skip "skip parallel run"
	(( $OST1_VERSION >= $(version_code 2.17.53) )) ||
		skip "contention detection is broken < 2.17.53"
	local dd1
	local dd2
	local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
	# bs=1M: count is file size in MB
	local count=${TEST32c_COUNT:-50}

	save_lustre_params ost1 \
		"ldlm.namespaces.filter-lustre-OST*.contended_locks" > $p
	stack_trap "restore_lustre_params < $p; rm -f $p" EXIT
	stack_trap "rm -f $DIR/$tfile"
	$LFS setstripe -i 0 $DIR/$tfile

	do_facet ost1 "$LCTL set_param \
		ldlm.namespaces.filter-lustre-OST*.contended_locks=2"
	do_facet ost1 "$LCTL set_param \
		ldlm.namespaces.filter-lustre-OST*.contention_events=0"

	# Write to file from both clients at different times and verify it
	# does not register as contention
	dd if=/dev/zero of=$DIR1/$tfile bs=1M count=$count conv=fsync
	dd if=/dev/zero of=$DIR2/$tfile bs=1M count=$count

	conevents=$(do_facet ost1 "$LCTL get_param -n \
		ldlm.namespaces.filter-lustre-OST0000*.contention_events")
	(( conevents == 0 )) || error "(1) $conevents detected, expected 0"

	# Write to file from both clients at the same time and verify it
	# generates some contention events
	dd if=/dev/zero of=$DIR1/$tfile bs=1M count=$count &
	dd1=$!
	dd if=/dev/zero of=$DIR2/$tfile bs=1M count=$count &
	dd2=$!
	wait $dd1 $dd2

	conevents=$(do_facet ost1 "$LCTL get_param -n \
		ldlm.namespaces.filter-lustre-OST0000*.contention_events")

	seconds=$(do_facet ost1 "$LCTL get_param -n \
		ldlm.namespaces.filter-lustre-OST0000*.contention_seconds")
	echo "contention_seconds $seconds, contention_events $conevents"

	# It's impossible to predict how many events we'll get, because of the
	# nature of racing processes like this, but we should definitely see
	# more than one
	(( conevents > 1 )) ||
		error "(2) $conevents contention events detected, expected > 1"

	# Let's continue doing i/o - We should see more contention events
	dd if=/dev/zero of=$DIR1/$tfile bs=1M count=$count &
	dd1=$!
	dd if=/dev/zero of=$DIR2/$tfile bs=1M count=$count &
	dd2=$!
	wait $dd1 $dd2

	conevents2=$(do_facet ost1 "$LCTL get_param -n \
		ldlm.namespaces.filter-lustre-OST0000*.contention_events")
	(( conevents2 > conevents )) ||
		error "(3) no added contention events, $conevents2 <= $conevents"

	# When the last server-side lock is released, the OST ldlm_resource is
	# freed and lr_contention_hist (contention hold state) is destroyed with
	# it. Which means the following read operations would not be affected by
	# the previous writes.
	cancel_lru_locks osc

	do_facet ost1 "$LCTL set_param \
		ldlm.namespaces.filter-lustre-OST*.contention_events=0"

	# Read from the file from both clients at the same time & verify it
	# doesn't generate contention events
	dd of=/dev/null if=$DIR1/$tfile bs=1M count=$count &
	dd1=$!
	dd of=/dev/null if=$DIR2/$tfile bs=1M count=$count &
	dd2=$!
	wait $dd1 $dd2

	conevents=$(do_facet ost1 "$LCTL get_param -n \
		ldlm.namespaces.filter-lustre-OST0000*.contention_events")
	(( conevents == 0 )) || error "(4) $conevents detected, expected 0"

	# Now test read lockahead - This should NOT cause contention because
	# they will be granted.
	for i in {1..50}; do
		$LFS ladvise -a lockahead -b -s 0M -l ${i}M -m READ $DIR/$tfile
	done

	conevents=$(do_facet ost1 "$LCTL get_param -n \
		ldlm.namespaces.filter-lustre-OST0000*.contention_events")
	(( conevents == 0 )) || error "(5) $conevents detected, expected == 0"

	# Finally, test detection of contention events with lockahead, using
	# incompatible async write lock requests, which are non-blocking.
	# These *should* generate contention events because they are explicit
	# requests which are being blocked, which counts as contention.
	for i in {1..50}; do
		$LFS ladvise -a lockahead -b -s 0M -l ${i}M -m WRITE $DIR/$tfile
	done

	conevents=$(do_facet ost1 "$LCTL get_param -n \
		ldlm.namespaces.filter-lustre-OST0000*.contention_events")
	(( conevents > 0 )) || error "(6) $conevents detected, expected > 0"
}
run_test 32c "contention detection"

print_jbd_stat () {
	local mds_facets=$(get_facets MDS)
	local stat=0
	local varsvc
	local dev
	local mds

	for mds in ${mds_facets//,/ }; do
		varsvc=${mds}_svc

		dev=$(basename $(do_facet $mds "lctl get_param -n \
			osd*.${!varsvc}.mntdev | xargs readlink -f"))
		val=$(do_facet $mds "cat /proc/fs/jbd*/${dev}{,:*,-*}/info \
			2>/dev/null | head -n1")
		val=${val%% *};
		stat=$((stat + val))
	done
	echo $stat
}

# commit on sharing tests
test_33a() {
	remote_mds_nodsh && skip "remote MDS with nodsh" && return

	[ $CLIENTCOUNT -lt 2 ] &&
		skip "Need two or more clients, have $CLIENTCOUNT"

	local nfiles=${TEST33_NFILES:-10000}
	local param_file=$TMP/$tfile-params
	local COS
	local jbdold="N/A"
	local jbdnew="N/A"
	local jbd

	save_lustre_params $(get_facets MDS) \
		"mdt.*.commit_on_sharing" > $param_file

	for COS in 0 1; do
		do_facet $SINGLEMDS lctl set_param mdt.*.commit_on_sharing=$COS
		avgjbd=0
		avgtime=0
		for i in 1 2 3; do
			do_nodes $CLIENT1,$CLIENT2 "mkdir -p $DIR1/$tdir-\\\$(hostname)-$i"

		[ "$mds1_FSTYPE" = ldiskfs ] && jbdold=$(print_jbd_stat)
		echo "=== START createmany old: $jbdold transaction"
		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")
		[ "$mds1_FSTYPE" = ldiskfs ] && jbdnew=$(print_jbd_stat)
		[ "$mds1_FSTYPE" = ldiskfs ] && jbd=$(( jbdnew - jbdold ))
		echo "=== END   createmany new: $jbdnew transaction :  $jbd transactions  nfiles $nfiles time $elapsed COS=$COS"
		[ "$mds1_FSTYPE" = ldiskfs ] && avgjbd=$(( avgjbd + jbd ))
		avgtime=$(( avgtime + elapsed ))
		done
	eval cos${COS}_jbd=$((avgjbd / 3))
	eval cos${COS}_time=$((avgtime / 3))
	done

	echo "COS=0 transactions (avg): $cos0_jbd  time (avg): $cos0_time"
	echo "COS=1 transactions (avg): $cos1_jbd  time (avg): $cos1_time"
	[ "$cos0_jbd" != 0 ] &&
		echo "COS=1 vs COS=0 jbd:  $((((cos1_jbd/cos0_jbd - 1)) * 100 )) %"
	[ "$cos0_time" != 0 ] &&
		echo "COS=1 vs COS=0 time: $((((cos1_time/cos0_time - 1)) * 100 )) %"

	restore_lustre_params < $param_file
	rm -f $param_file
	return 0
}
run_test 33a "commit on sharing, cross crete/delete, 2 clients, benchmark"

# commit on sharing tests
test_33b() {
	remote_mds_nodsh && skip "remote MDS with nodsh" && return

	[ $CLIENTCOUNT -ge 2 ] ||
		{ skip "Need two or more clients, have $CLIENTCOUNT" &&
								return 0; }
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return

	local nfiles=${TEST33_NFILES:-10000}
	local param_file=$TMP/$tfile-params

	save_lustre_params $(get_facets MDS) \
		"mdt.*.commit_on_sharing" > $param_file

	local COS
	local jbdold
	local jbdnew
	local jbd
	local MDTIDX=1

	for COS in 0 1; do
		do_facet $SINGLEMDS lctl set_param mdt.*.commit_on_sharing=$COS
		avgjbd=0
		avgtime=0
		for i in 1 2 3; do
			do_node $CLIENT1 "$LFS mkdir -i $MDTIDX \
					  $DIR1/$tdir-\\\$(hostname)-$i"

			jbdold=$(print_jbd_stat)
			echo "=== START createmany old: $jbdold transaction"
			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")
			jbdnew=$(print_jbd_stat)
			jbd=$(( jbdnew - jbdold ))
			echo "=== END   createmany new: $jbdnew transaction : \
			$jbd transactions nfiles $nfiles time $elapsed COS=$COS"
			avgjbd=$(( avgjbd + jbd ))
			avgtime=$(( avgtime + elapsed ))
		done
		eval cos${COS}_jbd=$((avgjbd / 3))
		eval cos${COS}_time=$((avgtime / 3))
	done

	echo "COS=0 transactions (avg): $cos0_jbd  time (avg): $cos0_time"
	echo "COS=1 transactions (avg): $cos1_jbd  time (avg): $cos1_time"
	[ "$cos0_jbd" != 0 ] &&
	    echo "COS=1 vs COS=0 jbd: $(((cos1_jbd/cos0_jbd - 1) * 100)) %"
	[ "$cos0_time" != 0 ] &&
	    echo "COS=1 vs COS=0 time: $(((cos1_time/cos0_time - 1) * 100)) %"

	restore_lustre_params < $param_file
	rm -f $param_file
	return 0
}
run_test 33b "COS: cross create/delete, 2 clients, benchmark under remote dir"

# arg1 is description, arg2 is operations before Sync-on-Lock-Cancel, arg3 is
# the operation that triggers SoLC
op_trigger_solc() {
	local sync_count
	local total=0
	local mdts=$(mdts_nodes)

	sync_all_data

	# trigger CoS twice in case transaction commit before unlock
	for i in 1 2; do
		bash -c "$2"
		do_nodes $mdts "$LCTL set_param -n mdt.*.sync_count=0"
		bash -c "$3"
		sync_count=$(do_nodes $mdts \
			"lctl get_param -n mdt.*MDT*.sync_count" | calc_sum)
		total=$((total + sync_count));
		rm -rf $DIR/$tdir/*
		sync_all_data
	done

	echo $1
	echo "  $2"
	echo "  $3"
	echo "  SoLC count $total"
	(( total > 0 )) || error "$3 didn't trigger SoLC"
}

test_33_run() {
	echo $1
	echo "  $2"
	eval $2
}

test_33c() {
	(( MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
	(( MDS1_VERSION >= $(version_code 2.7.63) )) ||
		skip "DNE CoS not supported"

	# LU-13522
	stop mds1
	start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS || error "start mds1 failed"

	mkdir_on_mdt0 $DIR/$tdir
	sync_all_data

	if (( MDS1_VERSION < $(version_code 2.15.55.204) )); then
		op_trigger_solc "create remote dir and local dir" \
			"$LFS mkdir -i 1 $DIR/$tdir/remote" \
			"$LFS mkdir -i 0 $DIR/$tdir/local"
		(( MDSCOUNT > 2 )) &&
		op_trigger_solc "create remote dirs on different MDTs" \
			"$LFS mkdir -i 1 $DIR/$tdir/remote.1" \
			"$LFS mkdir -i 2 $DIR/$tdir/remote.2"
	fi
	op_trigger_solc "create file on 2nd stripe under striped directory" \
		"$LFS mkdir -i 0 -c 2 $DIR/$tdir/striped" \
		"touch $DIR2/$tdir/striped/subfile"

	echo
	echo "Below operations shouldn't trigger Solc:"
	$LFS mkdir -i 0 -c 2 $DIR/$tdir/striped
	sync_all_data
	do_facet mds1 "lctl set_param -n mdt.*.sync_count=0"
	if (( MDS1_VERSION >= $(version_code 2.15.55.204) )); then
		test_33_run "create file on 2nd stripe after setattr" \
			"chmod 777 $DIR/$tdir/striped; \
			 touch $DIR2/$tdir/striped/subfile"
		test_33_run "create remote dir and local dir" \
			"$LFS mkdir -i 1 $DIR/$tdir/remote" \
			"$LFS mkdir -i 0 $DIR/$tdir/local"
		(( MDSCOUNT > 2 )) &&
		test_33_run "create remote dirs on different MDTs" \
			"$LFS mkdir -i 1 $DIR/$tdir/remote.1" \
			"$LFS mkdir -i 2 $DIR/$tdir/remote.2"
	fi
	test_33_run "create local dir after remote dir creation transaction commit" \
		"$LFS mkdir -i 1 $DIR/$tdir/remote.3; \
		 do_facet mds2 $LCTL set_param -n osd*.*MDT0001.force_sync 1;
		 mkdir $DIR/$tdir/local.3"
	sync_count=$(do_facet mds1 "lctl get_param -n mdt.*MDT0000.sync_count")
	echo "Solc count $sync_count"
	[ $sync_count -eq 0 ] || error "Sync-Lock-Cancel triggered"
}
run_test 33c "Cancel cross-MDT lock should trigger Sync-on-Lock-Cancel"

# arg1 is description, arg2 is operations done before CoS, arg3 is the operation
# that triggers CoS
op_trigger_cos() {
	local commit_nr
	local total=0
	local nodes=$(mdts_nodes)

	sync_all_data

	# trigger CoS twice in case transaction commit before unlock
	for i in 1 2; do
		bash -c "$2"
		do_nodes $nodes "lctl set_param -n mdt.*.async_commit_count=0"
		bash -c "$3"
		commit_nr=$(do_nodes $nodes \
			"lctl get_param -n mdt.*.async_commit_count" | calc_sum)
		total=$((total + commit_nr));
		rm -rf $DIR/$tdir
		sync_all_data
	done

	echo $1
	echo "  $2"
	echo "  $3"
	echo "  CoS count $total"
	(( total > 0 )) || error "$3 didn't trigger CoS"
}

test_33d() {
	(( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
	(( MDS1_VERSION >= $(version_code 2.7.63) )) ||
		skip "DNE CoS not supported"

	if (( $MDS1_VERSION < $(version_code 2.15.55.133) )); then
		op_trigger_cos "remote directory unlink" \
			"$LFS mkdir -i 1 $DIR/$tdir" "rmdir $DIR2/$tdir"
		op_trigger_cos "striped directory create" "mkdir $DIR/$tdir" \
			"$LFS mkdir -c 2 $DIR2/$tdir/subdir"
		op_trigger_cos "striped directory setattr" \
			"$LFS mkdir -c 2 $DIR/$tdir" "chmod 713 $DIR2/$tdir"
		op_trigger_cos "striped directory unlink" \
			"$LFS mkdir -c 2 $DIR/$tdir" "rmdir $DIR2/$tdir"
		op_trigger_cos "cross-MDT link" \
			"mkdir $DIR/$tdir; \
			$LFS mkdir -i 0 $DIR/$tdir/d1; \
			$LFS mkdir -i 1 $DIR/$tdir/d2; \
			touch $DIR/$tdir/d1/tgt" \
			"ln $DIR2/$tdir/d1/tgt $DIR2/$tdir/d2/src"
	fi

	op_trigger_cos "remote directory create" "$LFS mkdir -i 0 $DIR/$tdir" \
		"$LFS mkdir -i 1 $DIR2/$tdir/subdir"
	op_trigger_cos "cross-MDT rename" \
		"mkdir $DIR/$tdir; \
		$LFS mkdir -i 0 $DIR/$tdir/d1; \
		$LFS mkdir -i 1 $DIR/$tdir/d2; \
		touch $DIR/$tdir/d1/src" \
		"mv $DIR2/$tdir/d1/src $DIR2/$tdir/d2/tgt"
	op_trigger_cos "migrate" \
		"$LFS mkdir -i 0 $DIR/$tdir" \
		"$LFS migrate -m 1 $DIR2/$tdir"

	return 0
}
run_test 33d "dependent transactions should trigger COS"

test_33e() {
	(( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
	(( MDS1_VERSION >= $(version_code 2.7.63) )) ||
		skip "DNE CoS not supported"
	(( MDS1_VERSION >= $(version_code 2.15.56.127) )) ||
		skip "need MDS >= v2_15_56-127-g2a78a9e2cda1 for CoS fix"

	$LFS mkdir -i 0 $DIR/$tdir
	$LFS mkdir -i 0 $DIR/$tdir/d1
	$LFS mkdir -i 1 $DIR/$tdir/d2

	local mdts=$(mdts_nodes)
	do_nodes $mdts "lctl set_param -n mdt.*.async_commit_count=0"

	test_33_run "plain dir creation" "mkdir $DIR2/$tdir/plain"
	test_33_run "open file and write" "echo abc > $DIR2/$tdir/$tfile"
	test_33_run "append write" "echo dfg >> $DIR2/$tdir/$tfile"
	test_33_run "setattr" "touch $DIR2/$tdir/$tfile"
	test_33_run "file unlink" "rm $DIR2/$tdir/$tfile"
	test_33_run "plain dir unlink" "rmdir $DIR2/$tdir/plain"
	if (( MDS1_VERSION >= $(version_code 2.15.55.133) )); then
		test_33_run "striped directory creation" \
			"$LFS mkdir -i 0 -c 2 $DIR2/$tdir/striped"
		test_33_run "set default LMV to create striped subdir" \
			"$LFS setdirstripe -D -c 2 $DIR/$tdir"
		test_33_run "striped subdir creation" \
			"createmany -d $DIR/$tdir/subdir 100"
		test_33_run "sub file creation and write" \
			"createmany -o $DIR/$tdir/subfile 100; \
			echo abc > $DIR/$tdir/subfile1"
		test_33_run "sub file append write" \
			"echo dfg >> $DIR2/$tdir/subfile2"
		test_33_run "subdir setatttr" "touch $DIR2/$tdir/subdir1"
		test_33_run "subdir unlink" \
			"unlinkmany -d $DIR/$tdir/subdir 100"
		test_33_run "sub file unlink" \
			"unlinkmany $DIR2/$tdir/subfile 100"
		test_33_run "sub file creation follows striped dir chmod" \
			"chmod 777 $DIR/$tdir/striped; \
			 touch $DIR/$tdir/striped/subfile"
		test_33_run "striped directory unlink" \
			"rm -rf $DIR2/$tdir/striped"
	fi

	test_33_run "directory unlink" "rm -rf $DIR2/$tdir"

	local async_commit_count=$(do_nodes $mdts \
		"lctl get_param -n mdt.*.async_commit_count" | calc_sum)
	echo "CoS count $async_commit_count"
	(( async_commit_count == 0 )) || error "CoS triggerred"
}
run_test 33e "independent transactions shouldn't trigger COS"

# End commit on sharing tests

get_ost_lock_timeouts() {
    local osts=${1:-$(osts_nodes)}

    local locks=$(do_nodes $osts \
        "lctl get_param -n ldlm.namespaces.filter-*.lock_timeouts" | calc_sum)

    echo $locks
}

cleanup_34() {
	local i
	trap 0
	do_nodes $(osts_nodes) \
		"lctl set_param -n fail_loc=0 2>/dev/null || true"
	for ((i=1; i <= $OSTCOUNT; i++)); do
		wait_osc_import_ready client ost$i
	done
}

test_34() { #16129
	remote_ost_nodsh && skip "remote OST with nodsh" && return
	local OPER
	local lock_in
	local lock_out
	local osts=$(osts_nodes)

	trap cleanup_34 EXIT RETURN
        for OPER in notimeout timeout ; do
                rm $DIR1/$tfile 2>/dev/null
                lock_in=$(get_ost_lock_timeouts $osts)
                if [ $OPER == "timeout" ] ; then
                        for ((j = 1; j <= $OSTCOUNT; j++)); do
                                #define OBD_FAIL_PTLRPC_HPREQ_TIMEOUT    0x511
                                do_facet ost$j lctl set_param fail_loc=0x511
                        done
                        echo lock should expire
                else
                        for ((j = 1; j <= $OSTCOUNT; j++)); do
                                #define OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT  0x512
                                do_facet ost$j lctl set_param fail_loc=0x512
                        done
                        echo lock should not expire
                fi
                echo writing on client1
                dd if=/dev/zero of=$DIR1/$tfile count=100 conv=notrunc > /dev/null 2>&1
                sync &
                echo reading on client2
                dd of=/dev/null if=$DIR2/$tfile > /dev/null 2>&1
                # wait for a lock timeout
                sleep 4
                lock_out=$(get_ost_lock_timeouts)
                if [ $OPER == "timeout" ] ; then
                        if [ $lock_in == $lock_out ]; then
                                error "no lock timeout happened"
                        else
                                echo "success"
                        fi
                else
                        if [ $lock_in != $lock_out ]; then
                                error "lock timeout happened"
                        else
                                echo "success"
                        fi
                fi
        done
	cleanup_34
}
run_test 34 "no lock timeout under IO"

test_35() { # bug 17645
	local generation=[]
	local count=0
	gen=$(lctl get_param mdc.$FSNAME-MDT*-mdc-*.import | grep generation |
	      awk '/generation/{print $2}')
	for g in $gen; do
		generation[count]=$g
		let count=count+1
	done

	test_mkdir $MOUNT1/$tdir
	cancel_lru_locks mdc

	# Let's initiate -EINTR situation by setting fail_loc and take
	# write lock on same file from same client. This will not cause
	# bl_ast yet as lock is already in local cache.
	#define OBD_FAIL_LDLM_INTR_CP_AST        0x317
	do_facet client "lctl set_param fail_loc=0x80000317"
	local timeout=$(do_facet $SINGLEMDS lctl get_param  -n timeout)
	let timeout=timeout*3
	local nr=0
	while test $nr -lt 10; do
		log "Race attempt $nr"
		local blk1=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
			     awk '/ldlm_bl_callback/ { print $2 }')
		test "x$blk1" = "x" && blk1=0
		createmany -o $MOUNT2/$tdir/a 4000 &
		pid1=$!
		sleep 1

		# Let's make conflict and bl_ast
		ls -la $MOUNT1/$tdir > /dev/null &
		pid2=$!

		log "Wait for $pid1 $pid2 for $timeout sec..."
		sleep $timeout
		kill -9 $pid1 $pid2 > /dev/null 2>&1
		wait
		local blk2=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
			     awk '/ldlm_bl_callback/ { print $2 }')
		test "x$blk2" = "x" && blk2=0
		test $blk2 -gt $blk1 && break
		rm -fr $MOUNT1/$tdir
		cancel_lru_locks mdc
		let nr=nr+1
	done
	do_facet client "lctl set_param fail_loc=0x0"
	df -h $MOUNT1 $MOUNT2
	count=0
	gen=$(lctl get_param mdc.$FSNAME-MDT*-mdc-*.import | grep generation |
		awk '/generation/{print $2}')
	for g in $gen; do
	    if ! test "$g" -eq "${generation[count]}"; then
		list=$(lctl list_param mdc.$FSNAME-MDT*-mdc-*.import)
		local c=0
		for imp in $list; do
			if [ $c = $count ]; then
				break
			fi
			c=c+1
		done
		imp=$(echo "$imp" | awk -F"." '{print $2}')
		error "Eviction happened on import $imp"
            fi
            let count=count+1
        done
}
run_test 35 "-EINTR cp_ast vs. bl_ast race does not evict client"

test_36() { #bug 16417
	local SIZE
	local SIZE_B
	local i

	test_mkdir $DIR1/$tdir
	$LFS setstripe -c -1 $DIR1/$tdir
	i=0
	SIZE=50
	let SIZE_B=SIZE*1024*1024
	sync; sleep 2; sync # wait for delete thread
	wait_mds_ost_sync || error "wait_mds_ost_sync failed"
	wait_destroy_complete || error "wait_destroy_complete failed"

	while [ $i -le 10 ]; do
		lctl mark "start test - cycle ($i)"
		local before=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
		dd if=/dev/zero of=$DIR1/$tdir/$tfile bs=1M count=$SIZE ||
			error "dd $DIR1/$tdir/$tfile ${SIZE}MB failed"
		sync          # sync data from client cache
		sync_all_data # sync data from server cache (delayed allocation)
		sleep 2
		local after_dd=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
		multiop_bg_pause $DIR2/$tdir/$tfile O_r${SIZE_B}c || return 3
		read_pid=$!
		rm -f $DIR1/$tdir/$tfile
		kill -USR1 $read_pid
		wait $read_pid
		sync; sleep 2; sync # Ensure new statfs
		wait_delete_completed
		local after=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
		echo "*** cycle($i) *** before($before) after_dd($after_dd)" \
			"after($after)"
		# this free space! not used
		(( $after_dd <= $after)) ||
			error "space leaked after_dd:$after_dd > after:$after"
		let i=i+1
	done
}
run_test 36 "handle ESTALE/open-unlink correctly"

test_37() { # bug 18695
	test_mkdir $DIR1/$tdir
	multiop_bg_pause $DIR1/$tdir D_c || return 1
	MULTIPID=$!
	# create large directory (32kB seems enough from e2fsck, ~= 1000 files)
	createmany -m $DIR2/$tdir/f 10000
	# set mtime/atime backward
	touch -t 198001010000 $DIR2/$tdir
	kill -USR1 $MULTIPID
	nr_files=`lfs find $DIR1/$tdir -type f | wc -l`
	[ $nr_files -eq 10000 ] || error "$nr_files != 10000 truncated directory?"

}
run_test 37 "check i_size is not updated for directory on close (bug 18695) =============="

# this should be set to past
TEST_39_MTIME=`date -d "1 year ago" +%s`

# bug 11063
test_39a() {
	local client1=${CLIENT1:-`hostname`}
	local client2=${CLIENT2:-`hostname`}

	do_node $client1 "touch $DIR1/$tfile"

	do_node $client1 "touch -m -d @$TEST_39_MTIME $DIR1/$tfile"
	local mtime1=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
	[ "$mtime1" = $TEST_39_MTIME ] || \
		error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"

	local d1=`do_node $client1 date +%s`
	do_node $client1 'echo hello >> '$DIR1/$tfile
	local d2=`do_node $client1 date +%s`

	local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
	[ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
		error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"

	do_node $client1 "mv $DIR1/$tfile $DIR1/$tfile-1"

	for (( i=0; i < 2; i++ )) ; do
		local mtime3=`do_node $client2 "stat -c %Y $DIR1/$tfile-1"`
		[ "$mtime2" = "$mtime3" ] || \
			error "mtime ($mtime2) changed (to $mtime3) on rename"

		cancel_lru_locks osc
		if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
	done
}
run_test 39a "file mtime does not change after rename"

test_39b() {
	local client1=${CLIENT1:-`hostname`}
	local client2=${CLIENT2:-`hostname`}

	touch $DIR1/$tfile

	local mtime1=`stat -c %Y $DIR1/$tfile`
	local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`

	sleep 1
	touch -m -d @$TEST_39_MTIME $DIR1/$tfile

	for (( i=0; i < 2; i++ )) ; do
		local mtime3=`stat -c %Y $DIR1/$tfile`
		local mtime4=`do_node $client2 "stat -c %Y $DIR1/$tfile"`

		[ "$mtime3" = "$mtime4" ] || \
			error "different mtime on clients: $mtime3, $mtime4"
		[ "$mtime3" = $TEST_39_MTIME ] || \
			error "lost mtime: $mtime3, should be $TEST_39_MTIME"

		cancel_lru_locks osc
		if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
	done
}
run_test 39b "file mtime the same on clients with/out lock"

test_39c() {
	local client1=${CLIENT1:-`hostname`}
	local client2=${CLIENT2:-`hostname`}

	echo hello > $DIR1/$tfile

	local mtime1=`stat -c %Y $DIR1/$tfile`
	local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
	[ "$mtime1" = "$mtime2" ] || \
		error "create: different mtime on clients: $mtime1, $mtime2"

	sleep 1
	$TRUNCATE $DIR1/$tfile 1

	for (( i=0; i < 2; i++ )) ; do
		local mtime3=`stat -c %Y $DIR1/$tfile`
		local mtime4=`do_node $client2 "stat -c %Y $DIR1/$tfile"`

		[ "$mtime3" = "$mtime4" ] || \
			error "different mtime on clients: $mtime3, $mtime4"
		[ "$mtime3" -gt $mtime2 ] || \
			error "truncate did not update mtime: $mtime2, $mtime3"

		cancel_lru_locks osc
		if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
	done
}
run_test 39c "check truncate mtime update ======================"

test_39d() { # LU-7310
	touch $DIR1/$tfile
	touch -m -d @$TEST_39_MTIME $DIR1/$tfile

	local mtime1=$(stat -c %Y $DIR2/$tfile)
	[ "$mtime1" = $TEST_39_MTIME ] ||
		error "mtime: $mtime1, should be $TEST_39_MTIME"

	# force sync write
	# define OBD_FAIL_OSC_NO_GRANT 0x411
	$LCTL set_param fail_loc=0x411

	local d1=$(date +%s)
	echo hello >> $DIR1/$tfile
	local d2=$(date +%s)

	$LCTL set_param fail_loc=0

	cancel_lru_locks $OSC

	local mtime2=$(stat -c %Y $DIR2/$tfile)
	[ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] ||
		error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
}
run_test 39d "sync write should update mtime"

pdo_sched() {
	# how long 40-47 take with specific delay
	# sleep 0.1 # 78s
	# sleep 0.2 # 103s
	# sleep 0.3 # 124s
	sleep 0.5 # 164s
}

# for pdo testing, we must cancel MDT-MDT locks as well as client locks to
# avoid unexpected delays due to previous tests
pdo_lru_clear() {
	cancel_lru_locks mdc
	local mdts=$(mdts_nodes)

	do_nodes $mdts \
		$LCTL set_param -n ldlm.namespaces.*mdt*.lru_size=clear
	do_nodes $mdts \
		$LCTL get_param ldlm.namespaces.*mdt*.lock_unused_count \
			ldlm.namespaces.*mdt*.lock_count | grep -v '=0'
}

# check that pid exists hence second operation wasn't blocked by first one
# if it is so then there is no conflict, return 0
# else second operation is conflicting with first one, return 1
check_pdo_conflict() {
	local pid=$1
	local conflict=0
	pdo_sched # to ensure OP1 is finished on client if OP2 is blocked by OP1
	if [[ `ps --pid $pid | wc -l` == 1 ]]; then
		conflict=1
		echo "Conflict"
	else
		echo "No conflict"
	fi
	return $conflict
}

# pdirop tests
# test 40: check non-blocking operations
test_40a() {
	remote_mds_nodsh && skip "remote MDS with nodsh" && return

	mkdir_on_mdt0 $DIR2/$tdir
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tdir/$tfile &
	PID1=$!; pdo_sched
	touch $DIR2/$tdir/$tfile-2
	check_pdo_conflict $PID1 || error "create is blocked"
	mkdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "mkdir is blocked"
	link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
	check_pdo_conflict $PID1 || error "link is blocked"
	mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
	check_pdo_conflict $PID1 || error "rename is blocked"
	stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
	check_pdo_conflict $PID1 || error "getattr is blocked"
	rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
	rmdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "unlink is blocked"

	#  all operations above shouldn't wait the first one
	check_pdo_conflict $PID1 || error "parallel operation is blocked"
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	wait $PID1
	rm -rf $DIR/$tdir
	return 0
}
run_test 40a "pdirops: create vs others =============="

test_40b() {
	remote_mds_nodsh && skip "remote MDS with nodsh" && return

	mkdir_on_mdt0 $DIR2/$tdir
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	touch $DIR1/$tdir/$tfile &
	PID1=$!; pdo_sched
	# open|create
	touch $DIR2/$tdir/$tfile-2
	check_pdo_conflict $PID1 || error "create is blocked"
	mkdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "mkdir is blocked"
	link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
	check_pdo_conflict $PID1 || error "link is blocked"
	mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
	check_pdo_conflict $PID1 || error "rename is blocked"
	stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
	check_pdo_conflict $PID1 || error "getattr is blocked"
	rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
	rmdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "unlink is blocked"
	# all operations above shouldn't wait the first one

	check_pdo_conflict $PID1 || error "parallel operation is blocked"
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	wait $PID1
	rm -rf $DIR/$tdir
	return 0
}
run_test 40b "pdirops: open|create and others =============="

test_40c() {
	remote_mds_nodsh && skip "remote MDS with nodsh" && return

	mkdir_on_mdt0 $DIR2/$tdir
	pdo_lru_clear
	touch $DIR1/$tdir/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tdir/$tfile $DIR1/$tdir/$tfile-0 &
	PID1=$!; pdo_sched
	# open|create
	touch $DIR2/$tdir/$tfile-2
	check_pdo_conflict $PID1 || error "create is blocked"
	mkdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "mkdir is blocked"
	link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
	check_pdo_conflict $PID1 || error "link is blocked"
	mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
	check_pdo_conflict $PID1 || error "rename is blocked"
	stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
	check_pdo_conflict $PID1 || error "getattr is blocked"
	rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
	rmdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "unlink is blocked"

	# all operations above shouldn't wait the first one
	check_pdo_conflict $PID1 || error "parallel operation is blocked"
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	wait $PID1
	rm -rf $DIR/$tdir
	return 0
}
run_test 40c "pdirops: link and others =============="

test_40d() {
	remote_mds_nodsh && skip "remote MDS with nodsh" && return

	mkdir_on_mdt0 $DIR2/$tdir
	pdo_lru_clear
	touch $DIR1/$tdir/$tfile
#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	local mdts=$(mdts_nodes)

	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tdir/$tfile &
	PID1=$!; pdo_sched
	# open|create
	touch $DIR2/$tdir/$tfile-2
	check_pdo_conflict $PID1 || error "create is blocked"
	mkdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "mkdir is blocked"
	link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
	check_pdo_conflict $PID1 || error "link is blocked"
	mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
	check_pdo_conflict $PID1 || error "rename is blocked"
	stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
	check_pdo_conflict $PID1 || error "getattr is blocked"
	rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
	rmdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "unlink is blocked"

	# all operations above shouldn't wait the first one
	check_pdo_conflict $PID1 || error "parallel operation is blocked"
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	wait $PID1
	return 0
}
run_test 40d "pdirops: unlink and others =============="

test_40e() {
	remote_mds_nodsh && skip "remote MDS with nodsh" && return

	mkdir_on_mdt0 $DIR2/$tdir
	pdo_lru_clear
	touch $DIR1/$tdir/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tdir/$tfile $DIR1/$tdir/$tfile-0 &
	PID1=$!; pdo_sched
	# open|create
	touch $DIR2/$tdir/$tfile-2
	check_pdo_conflict $PID1 || error "create is blocked"
	mkdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "mkdir is blocked"
	link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
	check_pdo_conflict $PID1 || error "link is blocked"
	stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
	check_pdo_conflict $PID1 || error "getattr is blocked"
	rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-2
	rmdir $DIR2/$tdir/$tfile-3
	check_pdo_conflict $PID1 || error "unlink is blocked"

	# all operations above shouldn't wait the first one
	check_pdo_conflict $PID1 || error "parallel operation is blocked"
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	wait $PID1
	rm -rf $DIR/$tdir
	return 0
}
run_test 40e "pdirops: rename and others =============="

# test 41: create blocking operations
test_41a() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|CFS_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
	PID1=$! ; pdo_sched
	mkdir $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; echo "mkdir isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 41a "pdirops: create vs mkdir =============="

test_41b() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
	PID1=$! ; pdo_sched
	$MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 41b "pdirops: create vs create =============="

test_41c() {
	local mdts=$(mdts_nodes)

	pdo_lru_clear
	touch $DIR1/$tfile-2
	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
	PID1=$! ; pdo_sched
	link $DIR2/$tfile-2 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 41c "pdirops: create vs link =============="

test_41d() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
	PID1=$! ; pdo_sched
	rm $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 41d "pdirops: create vs unlink =============="

test_41e() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile-2 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 41e "pdirops: create and rename (tgt) =============="

test_41f() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile $DIR2/$tfile-2 &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 41f "pdirops: create and rename (src) =============="

test_41g() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
	PID1=$! ; pdo_sched
	stat $DIR2/$tfile > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 41g "pdirops: create vs getattr =============="

test_41h() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
	PID1=$! ; pdo_sched
	ls -lia $DIR2/ > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
		"lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
	wait $PID2
	rm -rf $DIR/$tfile*
	return 0
}
run_test 41h "pdirops: create vs readdir =============="

sub_test_41i() {
	local PID1 PID2
	local fail_loc="$1"
	local ret=0
	local mdts=$(mdts_nodes)

	do_nodes $mdts \
		"lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null

	$MULTIOP $DIR1/$tfile oO_CREAT:O_EXCL:c 2>/dev/null &
	PID1=$!
	sleep 0.2
	$MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c 2>/dev/null &
	PID2=$!

	if ! wait $PID1 && ! wait $PID2; then
		echo "Both creates failed (1 should fail, 1 should succeed)"
		ret=1
	elif wait $PID1 && wait $PID2; then
		echo "Both creates succeeded (1 should fail, 1 should succeed)"
		ret=2
	fi

	#Clean
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	rm -f $DIR/$tfile

	return $ret
}

test_41i() {
	(( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
		skip "Need MDS version newer than 2.13.56"
	local msg fail_loc

#define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
#define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
	for fail_loc in "0x80000169" "0x8000016a"; do
		echo "Begin 100 tests with fail_loc=$fail_loc"
		printf "Progress: "
		for i in {1..100}; do
			printf "*"
			msg=$(sub_test_41i "$fail_loc") ||
				{ echo; error "iter=$i : $msg"; }
		done
		echo
	done
}
run_test 41i "reint_open: create vs create"


# test 42: unlink and blocking operations
test_42a() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mkdir $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 42a "pdirops: mkdir vs mkdir =============="

test_42b() {
	local mdts=$(mdts_nodes)

	pdo_lru_clear
	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tfile &
	PID1=$! ; pdo_sched
	$MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
	PID2=$! ; pdo_sched
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 42b "pdirops: mkdir vs create =============="

test_42c() {
	local mdts=$(mdts_nodes)

	pdo_lru_clear
	touch $DIR1/$tfile-2
	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tfile &
	PID1=$! ; pdo_sched
	link $DIR2/$tfile-2 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 42c "pdirops: mkdir vs link =============="

test_42d() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tfile &
	PID1=$! ; pdo_sched
	rmdir $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 42d "pdirops: mkdir vs unlink =============="

test_42e() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv -T $DIR2/$tfile-2 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 42e "pdirops: mkdir and rename (tgt) =============="

test_42f() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile $DIR2/$tfile-2 &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 42f "pdirops: mkdir and rename (src) =============="

test_42g() {
	mkdir_on_mdt0 $DIR1/$tdir
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tdir/$tfile &
	PID1=$! ; pdo_sched
	stat $DIR2/$tdir/$tfile > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
	rm -rf $DIR/$tdir
}
run_test 42g "pdirops: mkdir vs getattr =============="

test_42h() {
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mkdir $DIR1/$tfile &
	PID1=$! ; pdo_sched
	ls -lia $DIR2/ > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
	wait $PID2
	rm -rf $DIR/$tfile*
	return 0
}
run_test 42h "pdirops: mkdir vs readdir =============="

# test 43: rmdir,mkdir won't return -EEXIST
test_43a() {
	for i in {1..1000}; do
		mkdir $DIR1/$tdir || error "mkdir $tdir failed"
		rmdir $DIR2/$tdir || error "rmdir $tdir failed"
	done
	return 0
}
run_test 43a "rmdir,mkdir doesn't return -EEXIST =============="

test_43b() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tfile &
	PID1=$! ; pdo_sched
	$MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "create must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 43b "pdirops: unlink vs create =============="

test_43c() {
	pdo_lru_clear
	touch $DIR1/$tfile
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tfile &
	PID1=$! ; pdo_sched
	link $DIR2/$tfile-2 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "link must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 43c "pdirops: unlink vs link =============="

test_43d() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tfile &
	PID1=$! ; pdo_sched
	rm $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "unlink must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 43d "pdirops: unlink vs unlink =============="

test_43e() {
	pdo_lru_clear
	touch $DIR1/$tfile
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv -u $DIR2/$tfile-2 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 43e "pdirops: unlink and rename (tgt) =============="

test_43f() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile $DIR2/$tfile-2 &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 43f "pdirops: unlink and rename (src) =============="

test_43g() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tfile &
	PID1=$! ; pdo_sched
	stat $DIR2/$tfile > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "stat must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 43g "pdirops: unlink vs getattr =============="

test_43h() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tfile &
	PID1=$! ; pdo_sched
	ls -lia $DIR2/ > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
	wait $PID2
	rm -rf $DIR/$tfile*
	return 0
}
run_test 43h "pdirops: unlink vs readdir =============="

test_43i() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	rm $DIR1/$tfile &
	PID1=$! ; pdo_sched
	$LFS mkdir -i 1 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 &&
		{ wait $PID1; error "remote mkdir isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "remote mkdir must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 43i "pdirops: unlink vs remote mkdir"

test_43j() {
	[[ $MDS1_VERSION -lt $(version_code 2.13.52) ]] &&
		skip "Need MDS version newer than 2.13.52"
	local mdts=$(mdts_nodes)

	mkdir_on_mdt0 $DIR1/$tdir
	for i in {1..100}; do
		#define CFS_FAIL_ONCE|OBD_FAIL_MDS_CREATE_RACE 0x167
		do_nodes $mdts \
			"lctl set_param -n fail_loc=0x80000167 2>/dev/null ||
				true"
		OK=0
		mkdir $DIR1/$tdir/sub &
		PID1=$!
		mkdir $DIR2/$tdir/sub && ((OK++))
		wait $PID1 && ((OK++))
		(( OK == 1 )) || error "exactly one mkdir should succeed"

		rmdir $DIR1/$tdir/sub || error "rmdir failed"
	done
	return 0
}
run_test 43j "racy mkdir return EEXIST =============="

sub_test_43k() {
	local PID1 PID2
	local fail_loc="$1"
	local ret=0

	# We test in a separate directory to be able to unblock server thread in
	# cfs_race() if LCK_PW is taken on the parent by mdt_reint_unlink.
	test_mkdir $DIR2/$tdir
	touch $DIR2/$tdir/$tfile
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	do_nodes $mdts \
		"lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null
	echo content > $DIR1/$tdir/$tfile & PID1=$!
	pdo_sched
	multiop $DIR2/$tdir/$tfile u & PID2=$!

	wait $PID1 ||
		{ ret=$?; \
		echo -n "overwriting $tfile should succeed (err=$ret); "; }
	wait $PID2 ||
		{ ret=$?; \
		echo -n "unlinking $tfile should succeed (err=$ret);"; }

	#Clean
	do_nodes $mdts "lctl set_param -n fail_loc=0x0 || true" &>/dev/null
	rm -rf $DIR/$tdir

	return $ret
}

test_43k() {
	(( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
		skip "Need MDS version newer than 2.13.56"
	local msg fail_loc

#define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
#define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
	for fail_loc in "0x80000169" "0x8000016a"; do
		echo "Begin 100 tests with fail_loc=$fail_loc"
		printf "Progress: "
		for i in {1..100}; do
			printf "*"
			msg=$(sub_test_43k "$fail_loc") ||
				{ echo; error "iter=$i : $msg"; }
		done
		echo
	done

	#Clean
	reset_fail_loc

	return 0
}
run_test 43k "unlink vs create"

# test 44: rename tgt and blocking operations
test_44a() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2   0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mkdir $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; date;error "mkdir isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
	date
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44a "pdirops: rename tgt vs mkdir =============="

test_44b() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	$MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44b "pdirops: rename tgt vs create =============="

test_44c() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	touch $DIR1/$tfile-3
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	link $DIR2/$tfile-3 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44c "pdirops: rename tgt vs link =============="

test_44d() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	rm $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44d "pdirops: rename tgt vs unlink =============="

test_44e() {
	pdo_lru_clear
	touch $DIR1/$tfile
	touch $DIR1/$tfile-2
	touch $DIR1/$tfile-3
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile-3 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44e "pdirops: rename tgt and rename (tgt) =============="

test_44f() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	touch $DIR1/$tfile-3
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile $DIR2/$tfile-3 &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44f "pdirops: rename tgt and rename (src) =============="

test_44g() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	stat $DIR2/$tfile > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44g "pdirops: rename tgt vs getattr =============="

test_44h() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	ls -lia $DIR2/ > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
	wait $PID2
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44h "pdirops: rename tgt vs readdir =============="

# test 44: rename tgt and blocking operations
test_44i() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2   0x146
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
	mv $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	$LFS mkdir -i 1 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1;
				error "remote mkdir isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "remote mkdir must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 44i "pdirops: rename tgt vs remote mkdir"

# test 45: rename,mkdir doesn't fail with -EEXIST
test_45a() {
	for i in {1..1000}; do
		mkdir $DIR1/$tdir || error "mkdir $tdir failed"
		mrename $DIR2/$tdir $DIR2/$tdir.$i > /dev/null ||
			error "mrename to $tdir.$i failed"
	done
	rm -rf $DIR/$tdir*
	return 0
}
run_test 45a "rename,mkdir doesn't return -EEXIST =============="

test_45b() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tfile $DIR1/$tfile-2 &
	PID1=$! ; pdo_sched
	$MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "create must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 45b "pdirops: rename src vs create =============="

test_45c() {
	pdo_lru_clear
	touch $DIR1/$tfile
	touch $DIR1/$tfile-3
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tfile $DIR1/$tfile-2 &
	PID1=$! ; pdo_sched
	link $DIR2/$tfile-3 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "link must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 45c "pdirops: rename src vs link =============="

test_45d() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tfile $DIR1/$tfile-2 &
	PID1=$! ; pdo_sched
	rm $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "unlink must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 45d "pdirops: rename src vs unlink =============="

test_45e() {
	pdo_lru_clear
	touch $DIR1/$tfile
	touch $DIR1/$tfile-3
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tfile $DIR1/$tfile-2 &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile-3 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 45e "pdirops: rename src and rename (tgt) =============="

test_45f() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tfile $DIR1/$tfile-2 &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile $DIR2/$tfile-3 &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 45f "pdirops: rename src and rename (src) =============="

test_45g() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tfile $DIR1/$tfile-2 &
	PID1=$! ; pdo_sched
	stat $DIR2/$tfile > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "stat must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 45g "pdirops: rename src vs getattr =============="

test_45h() {
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tfile $DIR1/$tfile-2 &
	PID1=$! ; pdo_sched
	ls -lia $DIR2/ > /dev/null &
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
	wait $PID2
	rm -rf $DIR/$tfile*
	return 0
}
run_test 45h "pdirops: unlink vs readdir =============="

test_45i() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	touch $DIR1/$tfile
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	mv $DIR1/$tfile $DIR1/$tfile-2 &
	PID1=$! ; pdo_sched
	$LFS mkdir -i 1 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 &&
		{ wait $PID1; error "create remote dir isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "create remote dir must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 45i "pdirops: rename src vs remote mkdir"

sub_test_45j() {
	local PID1 PID2
	local fail_loc="$1"
	local ret=0

	# We test in a sparate directory to be able to unblock server thread in
	# cfs_race if LCK_PW is taken on the parent by mdt_reint_rename.
	test_mkdir $DIR2/$tdir
	echo file1 > $DIR2/$tdir/$tfile
	echo file2 > $DIR2/$tdir/$tfile-2
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	do_nodes $mdts \
		"lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null

	cat $DIR1/$tdir/$tfile >/dev/null &
	PID1=$!
	pdo_sched
	mrename $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile > /dev/null &
	PID2=$!

	wait $PID1 ||
		{ ret=$?; echo -n "cat $tfile should succeed (err=$ret); "; }
	wait $PID2 ||
		{ ret=$?; \
		echo -n "mrename $tfile-2 to $tfile failed (err=$ret);"; }

	#Clean
	do_nodes $mdts "lctl set_param -n fail_loc=0x0 || true" &>/dev/null
	rm -rf $DIR/$tdir

	return $ret
}

test_45j() {
	(( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
		skip "Need MDS version newer than 2.13.56"
	local msg fail_loc

#define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
#define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
	for fail_loc in "0x80000169" "0x8000016a"; do
		echo "Begin 100 tests with fail_loc=$fail_loc"
		printf "Progress: "
		for i in {1..100}; do
			printf "*"
			msg=$(sub_test_45j "$fail_loc") ||
				{ echo; error "iter=$i : $msg"; }
		done
		echo
	done
}
run_test 45j "read vs rename =============="

# test 46: link and blocking operations
test_46a() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mkdir $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46a "pdirops: link vs mkdir =============="

test_46b() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	$MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46b "pdirops: link vs create =============="

test_46c() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	link $DIR2/$tfile $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46c "pdirops: link vs link =============="

test_46d() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	rm $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46d "pdirops: link vs unlink =============="

test_46e() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	touch $DIR1/$tfile-3
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile-3 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46e "pdirops: link and rename (tgt) =============="

test_46f() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	touch $DIR1/$tfile-3
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile $DIR2/$tfile-3 &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46f "pdirops: link and rename (src) =============="

test_46g() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	stat $DIR2/$tfile > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46g "pdirops: link vs getattr =============="

test_46h() {
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	ls -lia $DIR2/ > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
	wait $PID2
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46h "pdirops: link vs readdir =============="

test_46i() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	link $DIR1/$tfile-2 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	$LFS mkdir -i 1 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1;
				error "remote mkdir isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "remote mkdir must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 46i "pdirops: link vs remote mkdir"

# test 47: remote mkdir and blocking operations
test_47a() {
	(( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$LFS mkdir -i 1 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mkdir $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 47a "pdirops: remote mkdir vs mkdir"

test_47b() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$LFS mkdir -i 1 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	sleep 1 # please do not remove this sleep, see LU-10754
	multiop $DIR2/$tfile oO_CREAT:O_EXCL:c &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1;
					error "create isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 47b "pdirops: remote mkdir vs create"

test_47c() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$LFS mkdir -i 1 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	link $DIR2/$tfile-2 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 47c "pdirops: remote mkdir vs link"

test_47d() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$LFS mkdir -i 1 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	rmdir $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1;
					error "unlink isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rmdir must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 47d "pdirops: remote mkdir vs unlink"

test_47e() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	touch $DIR1/$tfile-2
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$LFS mkdir -i 1 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv -T $DIR2/$tfile-2 $DIR2/$tfile &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 &&
		{ wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 47e "pdirops: remote mkdir and rename (tgt)"

test_47f() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$LFS mkdir -i 1 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	mv $DIR2/$tfile $DIR2/$tfile-2 &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 &&
		{ wait $PID1; error "rename isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 47f "pdirops: remote mkdir and rename (src)"

test_47g() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	sync
	sync_all_data
	pdo_lru_clear
	local mdts=$(mdts_nodes)

	#define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
	do_nodes $mdts \
		"lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
	$LFS mkdir -i 1 $DIR1/$tfile &
	PID1=$! ; pdo_sched
	stat $DIR2/$tfile > /dev/null &
	PID2=$! ; pdo_sched
	do_nodes $mdts "lctl set_param -n fail_loc=0 2>/dev/null || true"
	check_pdo_conflict $PID1 && { wait $PID1;
					error "getattr isn't blocked"; }
	wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
	rm -rf $DIR/$tfile*
	return 0
}
run_test 47g "pdirops: remote mkdir vs getattr"

test_50() {
	local trunc_size=4096

	dd if=/dev/zero of=$DIR1/$tfile bs=1K count=10
	#define CFS_FAIL_OSC_CP_ENQ_RACE         0x410
	do_facet client "lctl set_param fail_loc=0x410"
	$TRUNCATE $DIR2/$tfile $trunc_size
	do_facet client "lctl set_param fail_loc=0x0"
	sleep 3
	size=$(stat -c %s $DIR2/$tfile)
	(( $size == $trunc_size )) || error "wrong size"
}
run_test 50 "osc lvb attrs: enqueue vs. CP AST =============="

test_51a() {
	local filesize
	local origfile=/etc/hosts

	filesize=$(stat -c %s $origfile)

	# create an empty file
	$MCREATE $DIR1/$tfile || error "can't create $DIR1/$tfile"
	# cache layout lock on both mount point
	stat $DIR1/$tfile > /dev/null || error "stat $DIR1/$tfile failed"
	stat $DIR2/$tfile > /dev/null || error "stat $DIR2/$tfile failed"

	# open and sleep 2 seconds then read
	$MULTIOP $DIR2/$tfile o_2r${filesize}c &
	local pid=$!
	sleep 1

	# create the layout of testing file
	dd if=$origfile of=$DIR1/$tfile conv=notrunc > /dev/null ||
		error "dd $DIR1/$tfile failed"

	# MULTIOP proc should be able to read enough bytes and exit
	for ((i = 0; i < 6; i++)); do
		sleep 1
		kill -0 $pid || break
	done
	kill -0 $pid 2> /dev/null && error "multiop is still there"
	cmp $origfile $DIR2/$tfile || error "$origfile and $DIR2/$tfile differs"

	rm -f $DIR1/$tfile
}
run_test 51a "layout lock: refresh layout should work"

test_51b() {
	(( $MDS1_VERSION >= $(version_code 2.3.59) )) ||
		skip "Need MDS version at least 2.3.59"

	local tmpfile=`mktemp`

	$LFS setstripe -E 1m -S 1M -c 1 -E -1 -c 1 $DIR1/$tfile ||
		error "Create $DIR1/$tfile failed"

	dd if=/dev/zero of=$DIR1/$tfile bs=1k count=1 conv=notrunc ||
		error "dd $DIR1/$tfile failed"

	# delay glimpse so that layout has changed when glimpse finish
#define OBD_FAIL_GLIMPSE_DELAY 0x1404
	$LCTL set_param fail_loc=0x1404 fail_val=4
	stat -c %s $DIR2/$tfile |tee $tmpfile &
	local pid=$!
	sleep 0.2

	# extend layout of testing file
	dd if=/dev/zero of=$DIR1/$tfile bs=1M count=1 seek=2 conv=notrunc ||
		error "dd $DIR1/$tfile failed"

	wait $pid
	local fsize=$(cat $tmpfile)

	[ x$fsize = x3145728 ] || error "file size is $fsize, should be 3145728"

	rm -f $DIR1/$tfile $tmpfile
}
run_test 51b "layout lock: glimpse should be able to restart if layout changed"

test_51c() {
	[ $OSTCOUNT -ge 2 ] || { skip "needs >= 2 osts"; return; }

	# set default layout to have 1 stripe
	mkdir $DIR1/$tdir
	$LFS setstripe -c 1 $DIR1/$tdir

	# create a file with empty layout
	$MCREATE $DIR1/$tdir/$tfile ||
		error "$MCREATE $DIR1/$tdir/$tfile failed"

#define OBD_FAIL_MDS_LL_BLOCK 0x172
	do_facet $SINGLEMDS $LCTL set_param fail_loc=0x172

	# change the layout of testing file
	echo "Setting layout to have $OSTCOUNT stripes ..."
	$LFS setstripe -c $OSTCOUNT $DIR1/$tdir/$tfile &
	pid=$!
	sleep 1

	# write something to the file, it should be blocked on fetching layout
	dd if=/dev/zero of=$DIR2/$tdir/$tfile bs=1k count=1 conv=notrunc
	local stripe_count=$($LFS getstripe -c $DIR2/$tdir/$tfile)
	wait $pid

	# lod_qos.c::min_stripe_count() allows setstripe with a default stripe
	# count to succeed with only 3/4 of the number of stripes (rounded up),
	# so creating striped files does not fail if an OST is offline or full
	[ $stripe_count -ge $((OSTCOUNT - $OSTCOUNT / 4)) ] ||
		error "bad layout: getstripe -c $stripe_count < $OSTCOUNT * 3/4"

	rm -fr $DIR1/$tdir
}
run_test 51c "layout lock: IT_LAYOUT blocked and correct layout can be returned"

test_51d() {
	dd if=/dev/zero of=/$DIR1/$tfile bs=1M count=1
	cancel_lru_locks mdc

	# open should grant LAYOUT lock, mmap and read will install pages
	$MULTIOP $DIR1/$tfile oO_RDWR:SMR_Uc &
	local PID=$!
	sleep 1

	# rss before revoking
	local br=$(grep -A 10 $tfile /proc/$PID/smaps | awk '/^Rss/{print $2}')
	echo "Before revoking layout lock: $br KB mapped"

	# cancel layout lock manually
	cancel_lru_locks mdc

	# rss after revoking
	local ar=$(grep -A 10 $tfile /proc/$PID/smaps | awk '/^Rss/{print $2}')

	kill -USR1 $PID
	wait $PID || error "wait PID $PID failed"

	[ $ar -eq 0 ] || error "rss before: $br, after $ar, some pages remained"
}
run_test 51d "layout lock: losing layout lock should clean up memory map region"

test_51e() {
	(( $MDS1_VERSION >= $(version_code 2.13.54.148) )) ||
		skip "MDS version must be at least 2.13.54.148"

	local pid

	$MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
	pid=$!
	sleep 1

	$LFS getstripe $DIR2/$tfile
	kill -USR1 $pid
	wait $pid || error "multiop failed"

	$MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
	pid=$!
	sleep 1

	$LFS getstripe $DIR2/$tfile
	kill -USR1 $pid
	wait $pid || error "multiop failed"
}
run_test 51e "lfs getstripe does not break leases, part 2"

test_54_part1()
{
	echo "==> rename vs getattr vs setxattr should not deadlock"
	mkdir -p $DIR/d1/d2/d3 || error "(1) mkdir failed"

	do_facet mds1 $LCTL set_param fail_loc=$1

	mv -T $DIR/d1/d2/d3 $DIR/d1/d3 &
	PID1=$!
	sleep 1

	stat $DIR/d1/d2 &
	PID2=$!
	sleep 1

	setfattr -n user.attr1 -v value1 $DIR2/d1 || error "(2) setfattr failed"
	wait $PID1 || error "(3) mv failed"
	wait $PID2 || error "(4) stat failed"
	echo

	rm -rf $DIR/d1
}

test_54_part2() {
	echo "==> rename vs getattr vs open vs getattr should not deadlock"
	mkdir -p $DIR/d1/d2/d3 || error "(1) mkdir failed"

	do_facet mds1 $LCTL set_param fail_loc=$1

	mv -T $DIR/d1/d2/d3 $DIR/d1/d3 &
	PID1=$!
	sleep 1

	stat $DIR/d1/d2 &
	PID2=$!
	sleep 1

	$MULTIOP $DIR2/d1/d2 Oc &
	PID3=$!
	sleep 1

	stat $DIR/d1 || error "(2) stat failed"

	wait $PID1 || error "(3) mv failed"
	wait $PID2 || error "(4) stat failed"
	wait $PID3 && error "(5) multiop failed"
	echo
	rm -rf $DIR/d1
}

test_54() {
	local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
	save_lustre_params client "llite.*.xattr_cache" > $p
	lctl set_param llite.*.xattr_cache 1 ||
		{ skip "xattr cache is not supported"; return 0; }

#define OBD_FAIL_MDS_RENAME              0x153
#define OBD_FAIL_MDS_RENAME2             0x154
	test_54_part1 0x80000153 || error 10
	test_54_part1 0x80000154 || error 11
	test_54_part2 0x80000153 || error 12
	test_54_part2 0x80000154 || error 13

	restore_lustre_params < $p
	rm -f $p
}
run_test 54 "rename locking"

test_55a() {
	mkdir_on_mdt0 $DIR/$tdir
	mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"

#define OBD_FAIL_MDS_RENAME4              0x156
	do_facet mds1 $LCTL set_param fail_loc=0x80000156

	mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
	PID1=$!
	sleep 1

	rm -r $DIR2/$tdir/d3
	wait $PID1 && error "(2) mv succeeded"

	rm -rf $DIR/$tdir
}
run_test 55a "rename vs unlink target dir"

test_55b()
{
	mkdir_on_mdt0 $DIR/$tdir
	mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"

#define OBD_FAIL_MDS_RENAME4             0x156
	do_facet mds1 $LCTL set_param fail_loc=0x80000156

	mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
	PID1=$!
	sleep 1

	rm -r $DIR2/$tdir/d1
	wait $PID1 && error "(2) mv succeeded"

	rm -rf $DIR/$tdir
}
run_test 55b "rename vs unlink source dir"

test_55c()
{
	mkdir_on_mdt0 $DIR/$tdir
	mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"

#define OBD_FAIL_MDS_RENAME4              0x156
	do_facet mds1 $LCTL set_param fail_loc=0x156

	mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
	PID1=$!
	sleep 1

	# while rename is sleeping, open and remove d3
	$MULTIOP $DIR2/$tdir/d3 D_c &
	PID2=$!
	sleep 1
	rm -rf $DIR2/$tdir/d3
	sleep 5

	# while rename is sleeping 2nd time, close d3
	kill -USR1 $PID2
	wait $PID2 || error "(3) multiop failed"

	wait $PID1 && error "(2) mv succeeded"

	rm -rf $DIR/$tdir
}
run_test 55c "rename vs unlink orphan target dir"

test_55d()
{
	mkdir_on_mdt0 $DIR/$tdir

	touch $DIR/$tdir/f1

#define OBD_FAIL_MDS_RENAME3              0x155
	do_facet mds1 $LCTL set_param fail_loc=0x155
	mv $DIR/$tdir/f1 $DIR/$tdir/$tdir &
	PID1=$!
	sleep 2

	# while rename is sleeping, create $tdir, but as a directory
	mkdir -p $DIR2/$tdir/$tdir || error "(1) mkdir failed"

	# link in reverse locking order
	ln $DIR2/$tdir/f1 $DIR2/$tdir/$tdir/f1 || error "(2) ln failed"

	! wait $PID1 || error "(3) mv succeeded"
	rm -rf $DIR/$tdir
}
run_test 55d "rename file vs link"

test_55e()
{
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"

	# set children remote to parent, to trigger bfl
	$LFS mkdir -i0 -c2 $DIR/$tdir || error "fail to create striped dir"
	$LFS mkdir -i1 $DIR/$tdir/A || error "fail to create subdir A"
	$LFS mkdir -i1 $DIR/$tdir/B || error "fail to create subdir B"

	#define OBD_FAIL_MDS_RENAME3	0x155
	# before any lock GOT, racing for parent locks
	do_facet mds1 $LCTL set_param fail_loc=0x155
	stack_trap "do_facet mds1 $LCTL set_param fail_loc=0x0"

	mv -T $DIR/$tdir/A $DIR/$tdir/B &
	PID1=$!
	mv -T $DIR2/$tdir/B $DIR2/$tdir/A &
	PID2=$!

	sleep 10
	wait $PID1; STATUS1=$?
	wait $PID2; STATUS2=$?

	SUCCESS=$(( !$STATUS1 + !$STATUS2 ))
	(( SUCCESS >= 1 )) ||
		error "expect at least one succ, actual: $SUCCESS"
	rm -rf $DIR/$tdir
	echo "We survived after AB/BA race test"
}
run_test 55e "rename race AB/BA under the same parent dir"

test_55f()
{
	mkdir_on_mdt0 $DIR/$tdir
	mkdir -p $DIR/$tdir/P1/A $DIR/$tdir/P2/B

	#define OBD_FAIL_MDS_RENAME3	0x155
	# before any lock GOT, racing for parent locks
	do_facet mds1 $LCTL set_param fail_loc=0x155
	stack_trap "do_facet mds1 $LCTL set_param fail_loc=0x0"

	mv -T $DIR/$tdir/P1/A $DIR/$tdir/P2/B &
	PID1=$!
	mv -T $DIR2/$tdir/P2/B $DIR2/$tdir/P1/A &
	PID2=$!

	sleep 10
	wait $PID1; STATUS1=$?
	wait $PID2; STATUS2=$?

	SUCCESS=$(( !$STATUS1 + !$STATUS2 ))
	(( SUCCESS >= 1 )) ||
		error "expect at least one succ, actual: $SUCCESS"
	rm -rf $DIR/$tdir
	echo "We survived after P1A_P2B/P2B_P1A race test"
}
run_test 55f "rename: (P1/A -> P2/B) race with (P2/B -> P1/A)"

test_55g()
{
	mkdir_on_mdt0 $DIR/$tdir
	mkdir -p $DIR/$tdir/a1/a2/a3/a4
	mkdir -p $DIR/$tdir/b1/b2/b3/b4

	local param_file=$TMP/$tfile-params
	save_lustre_params mds1 \
		"mdt.*.enable_rename_trylock" > $param_file
	do_facet mds1 $LCTL set_param mdt.*.enable_rename_trylock=1
	#define OBD_FAIL_MDS_RENAME3	0x155
	do_facet mds1 $LCTL set_param fail_loc=0x155
	stack_trap "do_facet mds1 $LCTL set_param fail_loc=0x0"

	mv $DIR/$tdir/a1/a2 $DIR/$tdir/b1/b2/b3/b4/ &
	PID1=$!
	mv $DIR2/$tdir/b1/b2 $DIR2/$tdir/a1/a2/a3/a4/ &
	PID2=$!

	sleep 10
	wait $PID1; STATUS1=$?
	wait $PID2; STATUS2=$?

	SUCCESS=$(( !$STATUS1 + !$STATUS2 ))
	(( SUCCESS >= 1 )) ||
		error "expect at least one succ, actual: $SUCCESS"
	[[ -e $DIR/$tdir/b1/b2/b3/b4/a2 ]] ||
		[[ -e $DIR2/$tdir/a1/a2/a3/a4/b2 ]] ||
			error "expect at least one valid dir"

	restore_lustre_params <$param_file
	rm -rf $DIR/$tdir
	echo "We survived race with trylock test"
}
run_test 55g "rename: race with trylock"

test_56a() {
	$LFS setstripe -c 1 $MOUNT/$tfile || error "creating $MOUNT/$tfile"
	stack_trap "rm -f $MOUNT/$tfile"
	echo "run_llverdev $MOUNT/$tfile -p -s $((16000000)) -c 4k"
	run_llverdev $MOUNT/$tfile -p -s $((16000000)) -c 4k ||
		error "llverdev failed with rc=$?"
}
run_test 56a "test llverdev with single large stripe"

test_56b() {
	$LFS setstripe -C 2000 $MOUNT/$tfile || error "creating $MOUNT/$tfile"
	stack_trap "rm -f $MOUNT/$tfile"
	echo "run_llverdev $MOUNT/$tfile -p -s $((16000000 * OSTCOUNT)) -c 4k"
	run_llverdev $MOUNT/$tfile -p -s $((16000000 * OSTCOUNT)) -c 4k ||
		error "llverdev failed with rc=$?"
}
run_test 56b "test llverdev and partial verify of wide stripe file"

test_60() {
	[ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
		skip "MDS version must be >= 2.3.0"

	# Create a file
	test_mkdir $DIR1/$tdir
	file1=$DIR1/$tdir/file
	file2=$DIR2/$tdir/file

	echo orig > $file2 || error "Could not create $file2"
	version=$($LFS data_version $file1)

	# Append data
	echo append >> $file2 || error "Could not append to $file2"
	version2=$($LFS data_version $file1)
	[ "$version" != "$version2" ] ||
	    error "append did not change data version: $version"

	# Overwrite data
	echo overwrite > $file2 || error "Could not overwrite $file2"
	version3=$($LFS data_version $file1)
	[ "$version2" != "$version3" ] ||
	    error "overwrite did not change data version: $version2"

	# Truncate before EOF
	$TRUNCATE $file2 3 || error "Could not truncate $file2"
	version4=$($LFS data_version $file1)
	[ "$version3" != "$version4" ] ||
	    error "truncate did not change data version: $version3"

	# Truncate after EOF
	$TRUNCATE $file2 123456 || error "Could not truncate $file2"
	version5=$($LFS data_version $file1)
	[ "$version4" != "$version5" ] ||
	    error "truncate did not change data version: $version4"

	# Chmod do not change version
	chmod 400 $file2 || error "Could not chmod 400 $file2"
	version6=$($LFS data_version $file1)
	[ "$version5" == "$version6" ] ||
	    error "chmod should not change data version: $version5 != $version6"

	# Chown do not change version
	chown $RUNAS_ID $file2 || error "Could not chown $RUNAS_ID $file2"
	version7=$($LFS data_version $file1)
	[ "$version5" == "$version7" ] ||
	    error "chown should not change data version: $version5 != $version7"
}
run_test 60 "Verify data_version behaviour"

test_70a() {
	local test_dir=$tdir/test_dir

	mkdir -p $DIR1/$tdir
	if [ $MDSCOUNT -ge 2 ]; then
		local MDTIDX=1
		$LFS mkdir -i $MDTIDX $DIR1/$test_dir ||
			error "Create remote directory failed"
	else
		mkdir -p $DIR1/$test_dir
	fi
	cd $DIR2/$test_dir || error "cd directory failed"
	rm -rf $DIR1/$test_dir || error "unlink directory failed"

	cd $DIR2/$tdir || error "exit directory"
}
run_test 70a "cd directory && rm directory"

test_70b() { # LU-2781
	local i
	mkdir -p $DIR1/$tdir

	touch $DIR1/$tdir/file
	for ((i = 0; i < 32; i++)); do
	    $LFS rm_entry $DIR1/$tdir/non_existent_dir &>/dev/null
	done
	rm $DIR1/$tdir/file || error "cannot remove file after rm_entry"

	touch $DIR1/$tdir/file
	$LFS mkdir -i0 $DIR1/$tdir/test_dir
	$LFS rm_entry $DIR1/$tdir/test_dir &>/dev/null
	rm -rf $DIR1/$tdir/test_dir ||
		error "cannot remove directory after rm_entry"
	rm $DIR1/$tdir/file || error "cannot remove file after rm_entry"
}
run_test 70b "remove files after calling rm_entry"

test_71a() {
	[[ "$MDS1_VERSION" -lt $(version_code 2.1.6) ]] &&
		skip "Need MDS version at least 2.1.6"

	# Patch not applied to 2.2 and 2.3 branches
	[[ "$MDS1_VERSION" -ge $(version_code 2.2.0) ]] &&
	[[ "$MDS1_VERSION" -lt $(version_code 2.4.0) ]] &&
		skip "Need MDS version earlier than 2.2.0 or at least 2.4.0"

	checkfiemap --test ||
		skip "checkfiemap not runnable: $?"
	# write data this way: hole - data - hole - data
	dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=1 count=1
	[ "$(facet_fstype ost$(($($LFS getstripe -i $DIR1/$tfile) + 1)))" = \
		"zfs" ] &&
		skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return 0
	dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=3 count=1
	GET_STAT="lctl get_param -n ldlm.services.ldlm_cbd.stats"
	stat $DIR2/$tfile
	local can1=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
	echo $can1
	checkfiemap $DIR2/$tfile 131072 ||
		error "data is not flushed from client"
	local can2=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
	echo $can2

	# common case of "create file, copy file" on a single node
	# should not flush data from ost
	dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=1 count=1
	dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=3 count=1
	stat $DIR1/$tfile
	local can3=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
	echo $can3
	checkfiemap $DIR1/$tfile 131072 ||
	error 4
	local can4=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
	echo $can2
	[ $can3 -eq $can4 ] || error $((can2-can1)) "cancel RPC occured."
}
run_test 71a "correct file map just after write operation is finished"

test_71b() {
	[[ "$MDS1_VERSION" -lt $(version_code 2.1.6) ]] &&
		skip "Need MDS version at least 2.1.6"

	# Patch not applied to 2.2 and 2.3 branches
	[[ "$MDS1_VERSION" -ge $(version_code 2.2.0) ]] &&
	[[ "$MDS1_VERSION" -lt $(version_code 2.4.0) ]] &&
		skip "Need MDS version earlier than 2.2.0 or at least 2.4.0"
	[[ $OSTCOUNT -ge 2 ]] || skip "needs >= 2 OSTs"

	checkfiemap --test ||
		skip "error $?: checkfiemap failed"

	mkdir -p $DIR1/$tdir

	$LFS setstripe -c -1 $DIR1/$tdir || error "setstripe failed"
	dd if=/dev/urandom of=$DIR1/$tdir/$tfile bs=64K count=1
	[ "$(facet_fstype ost$(($($LFS getstripe -i $DIR1/$tdir/$tfile) + 1)))" = \
		"zfs" ] &&
		skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return 0
	checkfiemap $DIR1/$tdir/$tfile 65536 || error "checkfiemap failed"
}
run_test 71b "check fiemap support for stripecount > 1"

_check_last_flag_with_filefrag()
{
	local file=$1
	local count=$2
	local i
	local offset

	echo "check last flag for file with $count extents"
	rm -f $file
	for ((i=0; i<$count; i++)); do
		offset=$((i * 256))
		dd if=/dev/zero of=$file bs=4K count=1 seek=$offset 2> /dev/null
	done

	filefrag -s -v $file | grep "last" ||
		error "test file with $i extents failed"

	rm -f $file
}

test_71c() {
	local file="$DIR1/$tdir/$tfile"

	(( $CLIENT_VERSION >= $(version_code 2.15.57) )) ||
		skip "Need client version >= 2.15.57"
	[ $(facet_fstype ost1) = "ldiskfs" ] ||
		skip "support only ldiskfs ost"
	filefrag -V | grep wc ||
		skip "need whamcloud version of e2fsprogs"

	mkdir -p $DIR1/$tdir

	# filefrag uses u64[2028] buffer to fetch fiemap. The number of extents
	# in the buffer is (8 * 2048 - 32) / 56 = 292. Test file with 291, 292
	# and 293 extents
	_check_last_flag_with_filefrag $file 291
	_check_last_flag_with_filefrag $file 292
	_check_last_flag_with_filefrag $file 293
}
run_test 71c "check FIEMAP_EXTENT_LAST flag with different extents number"

test_71d() { #LU-17110
	checkfiemap --test ||
		skip "error $?: checkfiemap failed"

	local f=$DIR/$tfile

	# write data this way: hole - data - hole - data
	dd if=/dev/urandom of=$f bs=64K count=1
	[[ "$(facet_fstype ost$(($($LFS getstripe -i $f) + 1)))" != "zfs" ]] ||
		skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
	dd if=/dev/urandom of=$f bs=64K seek=2 count=1
	dd if=/dev/urandom of=$f bs=64K seek=4 count=1
	dd if=/dev/urandom of=$f bs=64K seek=6 count=1 conv=fsync
	echo "disk usage: $(du -B1 $f)"
	echo "file size: $(du -b $f)"

	checkfiemap --corruption_test $f $((4 * 64 *1024)) ||
		error "checkfiemap failed"
}
run_test 71d "fiemap corruption test with fm_extent_count=0"

test_72() {
	local p="$TMP/sanityN-$TESTNAME.parameters"
	local tlink1
	local tlink2
	save_lustre_params client "llite.*.xattr_cache" > $p
	lctl set_param llite.*.xattr_cache 1 ||
		{ skip "xattr cache is not supported"; return 0; }

	touch $DIR1/$tfile
	setfattr -n user.attr1 -v value1 $DIR1/$tfile ||
		error "setfattr1 failed"
	getfattr -n user.attr1 $DIR2/$tfile | grep value1 ||
		error "getfattr1 failed"
	setfattr -n user.attr1 -v value2 $DIR2/$tfile ||
		error "setfattr2 failed"
	getfattr -n user.attr1 $DIR1/$tfile | grep value2 ||
		error "getfattr2 failed"

	# check that trusted.link is consistent
	tlink1=$(getfattr -n trusted.link $DIR1/$tfile | md5sum)
	ln $DIR2/$tfile $DIR2/$tfile-2 || error "failed to link"
	tlink2=$(getfattr -n trusted.link $DIR1/$tfile | md5sum)
	echo "$tlink1 $tlink2"
	[ "$tlink1" = "$tlink2" ] && error "trusted.link should have changed!"

	rm -f $DIR2/$tfile

	restore_lustre_params < $p
	rm -f $p
}
run_test 72 "getxattr/setxattr cache should be consistent between nodes"

test_73() {
	local p="$TMP/sanityN-$TESTNAME.parameters"
	save_lustre_params client "llite.*.xattr_cache" > $p
	lctl set_param llite.*.xattr_cache 1 ||
		{ skip "xattr cache is not supported"; return 0; }

	touch $DIR1/$tfile
	setfattr -n user.attr1 -v value1 $DIR1/$tfile ||
		error "setfattr1 failed"
	getfattr -n user.attr1 $DIR2/$tfile || error "getfattr1 failed"
	getfattr -n user.attr1 $DIR1/$tfile || error "getfattr2 failed"
	clear_stats llite.*.stats
	# PR lock should be cached by now on both clients
	getfattr -n user.attr1 $DIR1/$tfile || error "getfattr3 failed"
	# At least 2 hits for getfattr(0)+getfattr(size)
	# There may be more if auditd has a filesystem-related rule enabled
	(( $(calc_stats llite.*.stats getxattr_hits) >= 2 )) ||
		error "not cached in $DIR1"
	clear_stats llite.*.stats
	getfattr -n user.attr1 $DIR2/$tfile || error "getfattr4 failed"
	# At least 2 hits for getfattr(0)+getfattr(size)
	# There may be more if auditd has a filesystem-related rule enabled
	(( $(calc_stats llite.*.stats getxattr_hits) >= 2 )) ||
		error "not cached in $DIR2"
	rm -f $DIR2/$tfile

	restore_lustre_params < $p
	rm -f $p
}
run_test 73 "getxattr should not cause xattr lock cancellation"

test_74() {
	[ "$MDS1_VERSION" -lt $(version_code 2.4.93) ] &&
		skip "Need MDS version at least 2.4.93"

	dd if=/dev/zero of=$DIR1/$tfile-1 bs=1K count=1
	dd if=/dev/zero of=$DIR1/$tfile-2 bs=1K count=1
	flocks_test 4 $DIR1/$tfile-1 $DIR2/$tfile-2
}
run_test 74 "flock deadlock: different mounts =============="

# LU-3889
test_75() {
	$LFS setstripe -c 2 -S 1m -i 0 $DIR1/$tfile
	dd if=/dev/zero of=$DIR1/$tfile bs=1M count=2
	cancel_lru_locks osc

	dd of=$DIR1/$tfile if=/dev/zero bs=1M count=1 seek=1 conv=notrunc
	sync

	# define OBD_FAIL_LDLM_ENQUEUE_HANG 0x31d
	$LCTL set_param fail_loc=0x31d
	stat -c %s $DIR1/$tfile &
	local pid=$!
	sleep 1
	kill -9 $pid

	# For bad lock error handler we should ASSERT and got kernel panic here
	sleep 4
	$LCTL set_param fail_loc=0
}
run_test 75 "osc: upcall after unuse lock==================="

test_76() { #LU-946
	[[ "$MDS1_VERSION" -lt $(version_code 2.5.53) ]] &&
		skip "Need MDS version at least 2.5.53"

	remote_mds_nodsh && skip "remote MDS with nodsh"
	local fcount=$((MDSCOUNT * 256))
	declare -a fd_list
	declare -a fid_list

	if remote_mds; then
		nid=$($LCTL list_nids | sed  "s/\./\\\./g")
	else
		nid="0@lo"
	fi

	rm -rf $DIR/$tdir
	test_mkdir $DIR/$tdir

	# drop all open locks and close any cached "open" files on the client
	cancel_lru_locks mdc
	local mdts=$(mdts_nodes)

	local open_fids_cmd="$LCTL get_param -n mdt.*.exports.'$nid'.open_files"
	local fid_list=($(do_nodes $mdts $open_fids_cmd))
	local already=${#fid_list[@]}
	for (( i = 0; i < $already; i++ )) ; do
		log "already open[$i]: $($LFS fid2path $DIR2 ${fid_list[i]})"
	done

	echo -n "opening files: "
	ulimit -n $((fcount + 50))
	for ((i = 0; i < $fcount; i++)); do
		touch $DIR/$tdir/f_$i
		local fd=$(free_fd ${fd_list[i]})
		local open_cmd="exec $fd<$DIR/$tdir/f_$i"
		eval $open_cmd

		fd_list[i]=$fd

		(( $i % 32 == 0 )) && echo -n "."
	done
	echo

	fid_list=($(do_nodes $mdts $open_fids_cmd))

	# Possible errors in openfiles FID list.
	# 1. Missing FIDs. Check 1
	# 2. Extra FIDs. Check 1
	# 3. Duplicated FID. Check 2
	# 4. Invalid FIDs. Check 2
	# 5. Valid FID, points to some other file. Check 3

	# Check 1
	[ ${#fid_list[@]} -ne $((fcount + already)) ] &&
		error "${#fid_list[@]} != $fcount (+$already old) open files"

	echo -n "closing files: "
	for (( fd = 0, fid = 0; fd < $fcount; fd++, fid++ )) ; do
		local close_cmd="exec ${fd_list[fd]}<&-"
		eval $close_cmd
		filename=$($LFS fid2path $DIR2 ${fid_list[fid]})

		while [[ ! "$filename" =~ "$DIR2/$tdir/f_" ]]; do
			echo "skip old open file $filename"
			((fid++))
			filename=$($LFS fid2path $DIR2 ${fid_list[fid]})
		done

		# Check 2
		rm --interactive=no $filename
		[ $? -ne 0 ] &&
			error "Nonexisting fid ${fid_list[fid]} listed."
		(( $fd % 32 == 0 )) && echo -n "."
	done
	echo

	# Check 3
	ls_op=$(ls $DIR2/$tdir | wc -l)
	[ $ls_op -ne 0 ] &&
		error "Some openfiles are missing in lproc output"

	rm -rf $DIR/$tdir
}
run_test 76 "Verify MDT open_files listing"

nrs_write_read() {
	local n=16
	local dir=$DIR/$tdir
	local myRUNAS="$1"
	local create_as="$2"

	mkdir -p $dir || error "mkdir $dir failed"
	$LFS setstripe -c $OSTCOUNT $dir || error "setstripe to $dir failed"
	chmod 777 $dir

	if [[ -n "$create_as" ]]; then
		do_nodes $CLIENTS $create_as "touch $dir/nrs_r_\$HOSTNAME;" ||
			error "touch failed for $dir/nrs_r_*"
		do_nodes $CLIENTS $create_as touch "$dir/nrs_w_\$HOSTNAME" ||
			error "touch failed for $dir/nrs_w_*"
		do_nodes $CLIENTS $create_as "chmod 777 $dir/nrs_*_\$HOSTNAME;" ||
			error "chmod failed for $dir/nrs_*"
	fi

	do_nodes $CLIENTS $myRUNAS \
		dd if=/dev/zero of="$dir/nrs_r_\$HOSTNAME" bs=1M count=$n ||
		error "dd at 0 on client failed (1)"

	do_nodes $CLIENTS $myRUNAS \
		"declare -a pids_w;
		for ((i = 0; i < $n; i++)); do
			dd if=/dev/zero of=$dir/nrs_w_\$HOSTNAME bs=1M \
seek=\\\$i count=1 conv=notrunc &
			pids_w[\\\$i]=\\\$!;
		done;
		rc_w=0;
		for ((i = 0; i < $n; i++)); do
			wait \\\${pids_w[\\\$i]};
			newrc=\\\$?;
			[ \\\$newrc -gt \\\$rc_w ] && rc_w=\\\$newrc;
		done;
		exit \\\$rc_w" &
	local pid_w=$!
	do_nodes $CLIENTS sync;
	cancel_lru_locks osc

	do_nodes $CLIENTS $myRUNAS \
		"declare -a pids_r;
		for ((i = 0; i < $n; i++)); do
			dd if=$dir/nrs_r_\$HOSTNAME bs=1M of=/dev/null \
seek=\\\$i count=1 &
			pids_r[\\\$i]=\\\$!;
		done;
		rc_r=0;
		for ((i = 0; i < $n; i++)); do
			wait \\\${pids_r[\\\$i]};
			newrc=\\\$?;
			[ \\\$newrc -gt \\\$rc_r ] && rc_r=\\\$newrc;
		done;
		exit \\\$rc_r" &
	local pid_r=$!
	cancel_lru_locks osc

	wait $pid_w || error "dd (write) failed (2)"
	wait $pid_r || error "dd (read) failed (3)"
	rm -rvf $dir || error "rm -rf $dir failed"
}

test_77a() { #LU-3266
	local rc

	do_nodes $(osts_nodes) lctl set_param ost.OSS.*.nrs_policies="fifo" ||
		rc=$?
	[[ $rc -eq 3 ]] && skip "no NRS exists" && return
	[[ $rc -ne 0 ]] && error "failed to set fifo policy"
	nrs_write_read

	return 0
}
run_test 77a "check FIFO NRS policy"

test_77b() { #LU-3266
	local rc
	local osts=$(osts_nodes)

	do_nodes $osts lctl set_param ost.OSS.*.nrs_policies="crrn" \
		ost.OSS.*.nrs_crrn_quantum=1 || rc=$?
	[[ $rc -eq 3 ]] && skip "no NRS exists" && return
	[[ $rc -ne 0 ]] && error "failed to set crrn_quantum to 1"

	echo "policy: crr-n, crrn_quantum 1"
	nrs_write_read

	do_nodes $osts lctl set_param \
		ost.OSS.*.nrs_crrn_quantum=64 || rc=$?
	[[ $rc -ne 0 ]] && error "failed to set crrn_quantum to 64"

	echo "policy: crr-n, crrn_quantum 64"
	nrs_write_read

	# cleanup
	do_nodes $osts lctl set_param \
		ost.OSS.ost_io.nrs_policies="fifo" || rc=$?
	[[ $rc -ne 0 ]] && error "failed to set fifo policy"
	return 0
}
run_test 77b "check CRR-N NRS policy"

orr_trr() {
	local policy=$1
	local osts=$(osts_nodes)

	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies=$policy \
		ost.OSS.*.nrs_"$policy"_quantum=1 \
		ost.OSS.*.nrs_"$policy"_offset_type="physical" \
		ost.OSS.*.nrs_"$policy"_supported="reads" || return $?

	echo "policy: $policy, ${policy}_quantum 1, ${policy}_offset_type " \
		"physical, ${policy}_supported reads"
	nrs_write_read

	do_nodes $osts lctl set_param \
		ost.OSS.*.nrs_${policy}_supported="writes" \
		ost.OSS.*.nrs_${policy}_quantum=64 || return $?

	echo "policy: $policy, ${policy}_quantum 64, ${policy}_offset_type " \
		"physical, ${policy}_supported writes"
	nrs_write_read

	do_nodes $osts lctl set_param \
		ost.OSS.*.nrs_${policy}_supported="reads_and_writes" \
		ost.OSS.*.nrs_${policy}_offset_type="logical" || return $?
	echo "policy: $policy, ${policy}_quantum 64, ${policy}_offset_type " \
		"logical, ${policy}_supported reads_and_writes"
	nrs_write_read

	# cleanup
	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo" ||
		return $?
	return 0
}

test_77c() { #LU-3266
	local rc
	orr_trr "orr" || rc=$?
	[[ $rc -eq 3 ]] && skip "no NRS exists" && return
	[[ $rc -ne 0 ]] && error "orr_trr failed rc:$rc"
	return 0
}
run_test 77c "check ORR NRS policy"

test_77d() { #LU-3266
	local rc
	orr_trr "trr" || rc=$?
	[[ $rc -eq 3 ]] && skip "no NRS exists" && return
	[[ $rc -ne 0 ]] && error "orr_trr failed rc:$rc"
	return 0
}
run_test 77d "check TRR nrs policy"

tbf_rule_operate()
{
	local facet=$1
	shift 1

	do_facet $facet lctl set_param \
		ost.OSS.ost_io.nrs_tbf_rule="$*"
	[ $? -ne 0 ] &&
		error "failed to run operate '$*' on TBF rules"
}

cleanup_tbf_verify()
{
	local rc=0
	trap 0
	echo "cleanup_tbf $DIR/$tdir"
	rm -rf $DIR/$tdir
	rc=$?
	wait_delete_completed
	return $rc
}

tbf_verify() {
	local dir=$DIR/$tdir
	local client1=${CLIENT1:-$(hostname)}
	local myRUNAS="$3"
	local create_as="$4"
	local not_under_control=${5:-0}

	echo "not_under_control: $not_under_control"
	local np=$(check_cpt_number ost1)
	[ $np -gt 0 ] || error "CPU partitions should not be $np."
	echo "cpu_npartitions on ost1 is $np"

	mkdir -p $dir || error "mkdir $dir failed"
	$LFS setstripe -c 1 -i 0 $dir || error "setstripe to $dir failed"
	chmod 777 $dir

	if [[ -n "$create_as" ]]; then
		$create_as touch $dir/tbf
		chmod 777 $dir/tbf
	fi

	trap cleanup_tbf_verify EXIT
	echo "Limited write rate: $1, read rate: $2"
	echo "Verify the write rate is under TBF control"
	local start=$SECONDS
	do_node $client1 $myRUNAS dd if=/dev/zero of=$dir/tbf \
		bs=1M count=100 oflag=direct 2>&1
	local runtime=$((SECONDS - start + 1))
	local rate=$(bc <<< "scale=6; 100 / $runtime")
	echo "Write runtime is $runtime s, speed is $rate IOPS"

	# verify the write rate does not exceed TBF rate limit
	[ $(bc <<< "$rate < 1.1 * $np * $1") -eq 1 ] || {
		if (( not_under_control == 1 )); then
			echo "write rate ($rate) should exceed limit ($1 * $np)"
		else
			error "write rate ($rate) exceeds limit ($1 * $np)"
		fi
	}

	cancel_lru_locks osc

	echo "Verify the read rate is under TBF control"
	start=$SECONDS
	do_node $client1 $myRUNAS dd if=$dir/tbf of=/dev/null \
		bs=1M count=100 iflag=direct 2>&1
	runtime=$((SECONDS - start + 1))
	rate=$(bc <<< "scale=6; 100 / $runtime")
	echo "Read runtime is $runtime s, speed is $rate IOPS"

	# verify the read rate does not exceed TBF rate limit
	[ $(bc <<< "$rate < 1.1 * $np * $2") -eq 1 ] || {
		if (( not_under_control == 1 )); then
			echo "read rate ($rate) should exceed limit ($2 * $np)"
		else
			error "read rate ($rate) exceeds limit ($2 * $np)"
		fi
	}

	cancel_lru_locks osc
	cleanup_tbf_verify || error "rm -rf $dir failed"
}

test_77e() {
	local rc

	local osts=$(osts_nodes)

	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid" ||
		rc=$?
	[[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
	[[ $rc -ne 0 ]] && error "failed to set TBF NID policy"

	local idis
	local rateis
	if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
		idis="nid="
		rateis="rate="
	fi

	# Only operate rules on ost1 since OSTs might run on the same OSS
	# Add some rules
	tbf_rule_operate ost1 "start\ localhost\ ${idis}{0@lo}\ ${rateis}1000"
	local address=$(comma_list "$(host_nids_address $CLIENTS $NETTYPE)")
	local client_nids=$(nids_list $address "\\")
	tbf_rule_operate ost1 "start\ clients\ ${idis}{$client_nids}\ ${rateis}100"
	tbf_rule_operate ost1 "start\ others\ ${idis}{*.*.*.*@$NETTYPE}\ ${rateis}50"
	nrs_write_read

	# Change the rules
	tbf_rule_operate ost1 "change\ localhost\ ${rateis}1001"
	tbf_rule_operate ost1 "change\ clients\ ${rateis}101"
	tbf_rule_operate ost1 "change\ others\ ${rateis}51"
	nrs_write_read

	# Stop the rules
	tbf_rule_operate ost1 "stop\ localhost"
	tbf_rule_operate ost1 "stop\ clients"
	tbf_rule_operate ost1 "stop\ others"
	nrs_write_read

	# Cleanup the TBF policy
	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
	[ $? -ne 0 ] && error "failed to set policy back to fifo"
	nrs_write_read
	return 0
}
run_test 77e "check TBF NID nrs policy"

test_77f() {
	local rc
	local osts=$(osts_nodes)

	do_nodes $osts $LCTL set_param \
		ost.OSS.ost_io.nrs_policies="tbf\ jobid" || rc=$?
	[[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
	[[ $rc -ne 0 ]] && error "failed to set TBF JOBID policy"

	# Configure jobid_var
	local saved_jobid_var=$($LCTL get_param -n jobid_var)
	rc=$?
	[[ $rc -eq 3 ]] && skip "jobid_var not found" && return
	[[ $rc -ne 0 ]] && error "failed to get param jobid_var"
	if [ $saved_jobid_var != procname_uid ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" procname_uid
	fi

	local idis
	local rateis
	if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
		idis="jobid="
		rateis="rate="
	fi

	# Only operate rules on ost1 since OSTs might run on the same OSS
	# Add some rules
	tbf_rule_operate ost1 "start\ runas\ ${idis}{iozone.$RUNAS_ID\ dd.$RUNAS_ID\ tiotest.$RUNAS_ID}\ ${rateis}1000"
	tbf_rule_operate ost1 "start\ iozone_runas\ ${idis}{iozone.$RUNAS_ID}\ ${rateis}100"
	tbf_rule_operate ost1 "start\ dd_runas\ ${idis}{dd.$RUNAS_ID}\ ${rateis}50"
	nrs_write_read "$RUNAS"

	# Change the rules
	tbf_rule_operate ost1 "change\ runas\ ${rateis}1001"
	tbf_rule_operate ost1 "change\ iozone_runas\ ${rateis}101"
	tbf_rule_operate ost1 "change\ dd_runas\ ${rateis}51"
	nrs_write_read "$RUNAS"

	# Stop the rules
	tbf_rule_operate ost1 "stop\ runas"
	tbf_rule_operate ost1 "stop\ iozone_runas"
	tbf_rule_operate ost1 "stop\ dd_runas"
	nrs_write_read "$RUNAS"

	# Cleanup the TBF policy
	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
	[ $? -ne 0 ] && error "failed to set policy back to fifo"
	nrs_write_read "$RUNAS"

	local current_jobid_var=$($LCTL get_param -n jobid_var)
	[[ $? -ne 0 ]] && error "failed to get param jobid_var"
	if [ $saved_jobid_var != $current_jobid_var ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid_var
	fi
	return 0
}
run_test 77f "check TBF JobID nrs policy"

test_77g() {
	local rc=0
	local osts=$(osts_nodes)

	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid" ||
		rc=$?
	[[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
	[[ $rc -ne 0 ]] && error "failed to set TBF NID policy"

	do_nodes $osts lctl set_param \
		ost.OSS.ost_io.nrs_policies="tbf\ jobid" || rc=$?
	[[ $rc -ne 0 ]] && error "failed to set TBF JOBID policy"

	local idis
	local rateis
	if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
		idis="jobid="
		rateis="rate="
	fi

	# Add a rule that only valid for Jobid TBF. If direct change between
	# TBF types is not supported, this operation will fail.
	tbf_rule_operate ost1 "start\ dd_runas\ ${idis}{dd.$RUNAS_ID}\ ${rateis}50"

	# Cleanup the TBF policy
	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
	[ $? -ne 0 ] && error "failed to set policy back to fifo"
	return 0
}
run_test 77g "Change TBF type directly"

test_77h() {
	[ "$OST1_VERSION" -ge $(version_code 2.8.55) ] ||
		skip "Need OST version at least 2.8.55"

	local old_policy=$(do_facet ost1 \
		lctl get_param ost.OSS.ost_io.nrs_policies)
	local new_policy

	do_facet ost1 lctl set_param \
		ost.OSS.ost_io.nrs_policies="abc"
	[ $? -eq 0 ] && error "should return error"

	do_facet ost1 lctl set_param \
		ost.OSS.ost_io.nrs_policies="tbf\ abc"
	[ $? -eq 0 ] && error "should return error"

	do_facet ost1 lctl set_param \
		ost.OSS.ost_io.nrs_policies="tbf\ reg\ abc"
	[ $? -eq 0 ] && error "should return error"

	do_facet ost1 lctl set_param \
		ost.OSS.ost_io.nrs_policies="tbf\ abc\ efg"
	[ $? -eq 0 ] && error "should return error"

	new_policy=$(do_facet ost1 lctl get_param ost.OSS.ost_io.nrs_policies)
	[ $? -eq 0 ] || error "shouldn't LBUG"

	[ "$old_policy" = "$new_policy" ] || error "NRS policy should be same"

	return 0
}
run_test 77h "Wrong policy name should report error, not LBUG"

tbf_rule_check()
{
	local facet=$1
	local expected=$2
	local error_message=$3
	local rule_number=0
	for rule in $expected; do
		rule_number=$((rule_number + 1))
	done
	local stop_line=$(($rule_number + 3))
	local awk_command="awk 'NR >= 4 && NR <= $stop_line {print \$1}'"

	local output=$(do_facet $facet lctl get_param \
		ost.OSS.ost_io.nrs_tbf_rule |
		eval $awk_command |
		tr "\n" " " |
		sed 's/[ ]*$//')
	if [ "$output" != "$expected" ]; then
		error "$error_message, expected '$expected', got '$output'"
	fi
}

test_77i() {
	[ "$OST1_VERSION" -ge $(version_code 2.8.55) ] ||
		skip "Need OST version at least 2.8.55"
	local osts=$(osts_nodes)

	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ jobid"||
		error "failed to set TBF policy"

	tbf_rule_check ost1 "default" "error before inserting any rule"

	tbf_rule_operate ost1 "start\ before\ jobid={jobid}\ rate=1000"
	tbf_rule_check ost1 "before default" \
		"error when inserting rule 'before'"

	tbf_rule_operate ost1 "start\ after\ jobid={jobid}\ rate=1000\ rank=default"
	tbf_rule_check ost1 "before after default" \
		"error when inserting rule 'after'"

	tbf_rule_operate ost1 "start\ target\ jobid={jobid}\ rate=1000\ rank=after"
	tbf_rule_check ost1 "before target after default" \
		"error when inserting rule 'target'"

	echo "Move before itself"
	tbf_rule_operate ost1 "change\ target\ rank=target"
	tbf_rule_check ost1 "before target after default" \
		"error when moving before itself"

	echo "Move to higher rank"
	tbf_rule_operate ost1 "change\ target\ rank=before"
	tbf_rule_check ost1 "target before after default" \
		"error when moving to higher rank"

	echo "Move to lower rank"
	tbf_rule_operate ost1 "change\ target\ rank=after"
	tbf_rule_check ost1 "before target after default" \
		"error when moving to lower rank"

	echo "Move before default"
	tbf_rule_operate ost1 "change\ target\ rank=default"
	tbf_rule_check ost1 "before after target default" \
		error "error when moving before default"

	# Cleanup the TBF policy
	do_nodes $osts "$LCTL set_param ost.OSS.ost_io.nrs_policies=fifo"
	return 0
}
run_test 77i "Change rank of TBF rule"

test_77j() {
	local idis
	local rateis

	[ "$OST1_VERSION" -ge $(version_code 2.9.53) ] ||
		skip "Need OST version at least 2.9.53"
	if [ "$OST1_VERSION" -ge $(version_code 2.8.60) ]; then
		idis="opcode="
		rateis="rate="
	fi
	local osts=$(osts_nodes)

	do_nodes $osts \
		lctl set_param jobid_var=procname_uid \
			ost.OSS.ost_io.nrs_policies="tbf\ opcode" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ost_r\ ${idis}{ost_read}\ ${rateis}5" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ost_w\ ${idis}{ost_write}\ ${rateis}20"
	[ $? -ne 0 ] && error "failed to set TBF OPCode policy"

	nrs_write_read
	tbf_verify 20 5

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_r" \
			ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_w" \
			ost.OSS.ost_io.nrs_policies="fifo"

	# sleep 3 seconds to wait the tbf policy stop completely,
	# or the next test case is possible get -EAGAIN when
	# setting the tbf policy
	sleep 3
}
run_test 77j "check TBF-OPCode NRS policy"

test_id() {
	local idstr="${1}id"
	local policy="${idstr}={$2}"
	local rate="rate=$3"
	local runas_args="$4"
	local createas_args="${5:-$runas_args}"
	local osts=$(osts_nodes)

	do_nodes $osts \
		lctl set_param jobid_var=procname_uid \
			ost.OSS.ost_io.nrs_policies="tbf\ ${idstr}" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ost_${idstr}\ ${policy}\ ${rate}"
	[ $? -ne 0 ] && error "failed to set tbf ${idstr} policy"

	nrs_write_read "runas $runas_args" "runas $createas_args"
	tbf_verify $3 $3 "runas $runas_args" "runas $createas_args"

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_${idstr}" \
			ost.OSS.ost_io.nrs_policies="fifo"

	# sleep 3 seconds to wait the tbf policy stop completely,
	# or the next test case is possible get -eagain when
	# setting the tbf policy
	sleep 3
}

test_77ja(){
	if [ "$OST1_VERSION" -lt $(version_code 2.11.50) ]; then
		skip "Need OST version at least 2.11.50"
	fi

	test_id "u" "$RUNAS_ID" "5" "-u $RUNAS_ID"
	test_id "g" "$RUNAS_GID" "5" "-u $RUNAS_ID -g $RUNAS_GID"
}
run_test 77ja "check TBF-UID/GID NRS policy"

test_77jb() { # LU-16077
	(( "$OST1_VERSION" >= $(version_code 2.15.51) )) ||
		skip "Need OST version at least 2.15.51"

	test_id "u" "$RUNAS_ID" "5" "-u $RUNAS_ID" "-u 0 -g $RUNAS_GID"
	test_id "g" "$RUNAS_GID" "5" "-u $RUNAS_ID -g $RUNAS_GID" "-u $RUNAS_ID -g 0"
}
run_test 77jb "check TBF-UID/GID NRS policy on files that don't belong to us"

cleanup_77k()
{
	local rule_lists=$1
	local old_nrs=$2
	local osts=$(osts_nodes)

	trap 0
	for rule in $rule_lists; do
		do_nodes $osts \
			lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ $rule"
	done

	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="$old_nrs"

	sleep 3
}

test_77k() {
	[[ "$OST1_VERSION" -ge $(version_code 2.9.53) ]] ||
		skip "Need OST version at least 2.9.53"
	local osts=$(osts_nodes)

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_policies="tbf" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ jobid={dd.$RUNAS_ID}\&opcode={ost_write}\ rate=20" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ jobid={dd.$RUNAS_ID}\&opcode={ost_read}\ rate=10"

	nrs_write_read "$RUNAS"
	tbf_verify 20 10 "$RUNAS"

	local address=$(comma_list "$(host_nids_address $CLIENTS $NETTYPE)")
	local client_nids=$(nids_list $address "\\")
	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_w" \
			ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_r" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ nid={0@lo\ $client_nids}\&opcode={ost_write}\ rate=20" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ nid={0@lo\ $client_nids}\&opcode={ost_read}\ rate=10"

	nrs_write_read
	tbf_verify 20 10

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_w" \
			ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_r" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext\ nid={0@lo\ $client_nids}\&jobid={dd.$RUNAS_ID}\ rate=20"

	nrs_write_read "$RUNAS"
	tbf_verify 20 20 "$RUNAS"

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_a\ jobid={dd.$RUNAS_ID},opcode={ost_write}\ rate=20" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_b\ jobid={dd.$RUNAS_ID},opcode={ost_read}\ rate=10"

	nrs_write_read "$RUNAS"
	# with parameter "RUNAS", it will match the latest rule
	# "ext_b" first, so the limited write rate is 10.
	tbf_verify 10 10 "$RUNAS"
	tbf_verify 20 10

	trap "cleanup_77k \"ext_a ext_b\" \"fifo\"" EXIT

	[[ "$OST1_VERSION" -ge $(version_code 2.10.58) ]] ||
		skip "Need OST version at least 2.10.58"

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_a" \
			ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_b" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_ug\ uid={$RUNAS_ID}\&gid={$RUNAS_GID}\ rate=5"
	nrs_write_read "runas -u $RUNAS_ID -g $RUNAS_GID"
	tbf_verify 5 5 "runas -u $RUNAS_ID -g $RUNAS_GID"

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_ug" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_uw\ uid={$RUNAS_ID}\&opcode={ost_write}\ rate=20" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_ur\ uid={$RUNAS_ID}\&opcode={ost_read}\ rate=10"

	nrs_write_read "runas -u $RUNAS_ID"
	tbf_verify 20 10 "runas -u $RUNAS_ID"

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_uw" \
			ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_ur" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_a\ uid={$RUNAS_ID},opcode={ost_write}\ rate=20" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_b\ uid={$RUNAS_ID},opcode={ost_read}\ rate=10"
	nrs_write_read "runas -u $RUNAS_ID"
	tbf_verify 10 10 "runas -u $RUNAS_ID"
	tbf_verify 20 10 "runas -u $RUNAS_ID"
	cleanup_77k "ext_a ext_b" "fifo"
}
run_test 77k "check TBF policy with UID/GID/JobID/OPCode expression"

cleanup_77k_jobid()
{
	local saved_jobid_var=$1
	local rule_list=$2
	local old_nrs=${3:-"fifo"}

	local current_jobid_var=$($LCTL get_param -n jobid_var)

	if [ $saved_jobid_var != $current_jobid_var ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid_var
	fi

	cleanup_77k $rule_list $old_nrs
}
test_77kb() {
	(( "$OST1_VERSION" >= $(version_code 2.16.52) )) ||
		skip "Need OST version at least 2.16.52"

	local osts=$(osts_nodes)
	local saved_jobid_var=$($LCTL get_param -n jobid_var)

	# Configure jobid_var
	if [ $saved_jobid_var != procname_uid ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" procname_uid
	fi

	do_nodes $osts $LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ jobid+opcode" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ jobid={dd.$RUNAS_ID}\&opcode={ost_write}\ rate=20" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ jobid={dd.$RUNAS_ID}\&opcode={ost_read}\ rate=10"
	stack_trap "cleanup_77k_jobid $saved_jobid 'ext_w ext_r'"
	nrs_write_read "$RUNAS"
	tbf_verify 20 10 "$RUNAS"
}
run_test 77kb "Check different granular TBF type combination: jobid+opcode"

test_77kc() {
	(( "$OST1_VERSION" >= $(version_code 2.16.52) )) ||
		skip "Need OST version at least 2.16.52"

	local osts=$(osts_nodes)
	local address=$(comma_list "$(host_nids_address $CLIENTS $NETTYPE)")
	local client_nids=$(nids_list $address "\\")

	do_nodes $osts $LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ nid+opcode" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ nid={0@lo\ $client_nids}\&opcode={ost_write}\ rate=20" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ nid={0@lo\ $client_nids}\&opcode={ost_read}\ rate=10"
	stack_trap "cleanup_77k 'ext_w ext_r' 'fifo'"
	nrs_write_read
	tbf_verify 20 10
}
run_test 77kc "Check different granular TBF type combination: nid+opcode"

test_77kd() {
	(( "$OST1_VERSION" >= $(version_code 2.16.52) )) ||
		skip "Need OST version at least 2.16.52"

	local osts=$(osts_nodes)
	local address=$(comma_list "$(host_nids_address $CLIENTS $NETTYPE)")
	local client_nids=$(nids_list $address "\\")
	local saved_jobid_var=$($LCTL get_param -n jobid_var)

	# Configure jobid_var
	if [ $saved_jobid_var != procname_uid ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" procname_uid
	fi

	do_nodes $osts $LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ nid+jobid" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext\ nid={0@lo\ $client_nids}\&jobid={dd.$RUNAS_ID}\ rate=20"
	stack_trap "cleanup_77k_jobid $saved_jobid_var 'ext'"
	nrs_write_read "$RUNAS"
	tbf_verify 20 20 "$RUNAS"
}
run_test 77kd "Check different granular TBF type combination: nid+jobid"

test_77ke() {
	(( "$OST1_VERSION" >= $(version_code 2.16.52) )) ||
		skip "Need OST version at least 2.16.52"

	local osts=$(osts_nodes)
	local saved_jobid_var=$($LCTL get_param -n jobid_var)

	# Configure jobid_var
	if [ $saved_jobid_var != procname_uid ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" procname_uid
	fi

	do_nodes $osts $LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ jobid+opcode" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_a\ jobid={dd.$RUNAS_ID},opcode={ost_write}\ rate=20" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_b\ jobid={dd.$RUNAS_ID},opcode={ost_read}\ rate=10"
	stack_trap "cleanup_77k_jobid $saved_jobid_var 'ext_a ext_b'"
	nrs_write_read "$RUNAS"
	# with parameter "RUNAS", it will match the latest rule
	# "ext_b" first, so the limited write rate is 10.
	tbf_verify 10 10 "$RUNAS"
	tbf_verify 20 10
}
run_test 77ke "Check different granular TBF type combination: jobid+opcode"

test_77kf() {
	(( "$OST1_VERSION" >= $(version_code 2.16.52) )) ||
		skip "Need OST version at least 2.16.52"

	local osts=$(osts_nodes)

	do_nodes $osts $LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ uid+gid" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_ug\ uid={$RUNAS_ID}\&gid={$RUNAS_GID}\ rate=5"
	stack_trap "cleanup_77k 'ext_ug' 'fifo'"
	nrs_write_read "runas -u $RUNAS_ID -g $RUNAS_GID"
	tbf_verify 5 5 "runas -u $RUNAS_ID -g $RUNAS_GID"
}
run_test 77kf "Check different granular TBF type combination: uid+gid"

test_77kg() {
	(( "$OST1_VERSION" >= $(version_code 2.16.52) )) ||
		skip "Need OST version at least 2.16.52"

	local osts=$(osts_nodes)

	stack_trap "cleanup_77k 'ext_uw ext_ur ext_a ext_b' 'fifo'"
	do_nodes $osts $LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ uid+opcode" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_uw\ uid={$RUNAS_ID}\&opcode={ost_write}\ rate=20" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_ur\ uid={$RUNAS_ID}\&opcode={ost_read}\ rate=10"
	nrs_write_read "runas -u $RUNAS_ID"
	tbf_verify 20 10 "runas -u $RUNAS_ID"

	do_nodes $osts $LCTL set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_uw" \
		ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_ur" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_a\ uid={$RUNAS_ID},opcode={ost_write}\ rate=20" \
		ost.OSS.ost_io.nrs_tbf_rule="start\ ext_b\ uid={$RUNAS_ID},opcode={ost_read}\ rate=10"
	nrs_write_read "runas -u $RUNAS_ID"
	tbf_verify 10 10 "runas -u $RUNAS_ID"
	tbf_verify 20 10 "runas -u $RUNAS_ID"
}
run_test 77kg "check different granular TBF type combination: uid+opcode"

test_77kh() {
	(( "$OST1_VERSION" >= $(version_code 2.16.58) )) ||
		skip "Need OST version at least 2.16.58 for NRS PROJID rules"

	is_project_quota_supported || skip "Project quota is not supported"

	enable_project_quota

	local dir=$DIR/$tdir
	local projid=100
	local osts=$(osts_nodes)

	stack_trap "cleanup_77k \"ext_w ext_r\" \"fifo\"" EXIT

	do_nodes $osts \
		$LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ projid" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_proj_rw\ projid={$projid}\ rate=20"

	mkdir $dir || error "failed to mkdir $dir"
	$LFS project -sp $projid $dir ||
		error "failed to set porject ID ($projid) on $dir"
	nrs_write_read

	mkdir $dir || error "failed to mkdir $dir"
	$LFS project -sp $projid $dir ||
		error "failed to set porject ID ($projid) on $dir"
	tbf_verify 20 20

	cleanup_77k "ext_proj_rw" "fifo"

	do_nodes $osts \
		$LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ projid+opcode" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ projid={$projid}\&opcode={ost_write}\ rate=20" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ projid={$projid}\&opcode={ost_read}\ rate=10"

	mkdir $dir || error "failed to mkdir $dir"
	$LFS project -sp $projid $dir ||
		error "failed to set porject ID ($projid) on $dir"
	nrs_write_read

	mkdir $dir || error "failed to mkdir $dir"
	$LFS project -sp $projid $dir ||
		error "failed to set porject ID ($projid) on $dir"
	tbf_verify 20 10

	cleanup_77k "ext_w ext_r" "fifo"
}
run_test 77kh "Verify that Project ID support for NRS TBF rule"

test_77ki() {
	(( "$OST1_VERSION" >= $(version_code 2.16.58) )) ||
		skip "Need OST version at least 2.16.58 for NRS PROJID rules"

	local osts=$(osts_nodes)

	stack_trap "cleanup_77k \"ext_w ext_r\" \"fifo\"" EXIT

	do_nodes $osts \
		$LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ jobid+opcode" ||
		error "failed to set NRS policies with 'jobid+opcode'"
	do_nodes $osts \
		$LCTL set_param ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ jobid={dd.$RUNAS_ID}\&opcode={ost_write}\ rate=20" ||
		error "failed to set NRS TBF rules with jobid and write opcode"
	do_nodes $osts \
		$LCTL set_param ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ jobid={dd.$RUNAS_ID}\&opcode={ost_read}\ rate=10" ||
		error "failed to set NRS TBF rules with jobid and read opcode"

	do_nodes $osts \
		$LCTL set_param ost.OSS.ost_io.nrs_tbf_rule="start\ ext_fail\ projid={100}\&opcode={ost_write}\ rate=20" &&
		error "Adding unsupported projid rule (projid) should fail"
	cleanup_77k "ext_w ext_r" "fifo"
}
run_test 77ki "Add rule with unsupported TBF type should fail for generic TBF"

setup_nodemap_77kj() {
	local nm=$1

	do_facet mgs $LCTL nodemap_activate 1
	wait_nm_sync active
	do_facet mgs $LCTL nodemap_add $nm
	do_facet mgs $LCTL nodemap_add_range \
			--name $nm --range $client_nid
	do_facet mgs $LCTL nodemap_modify --name $nm \
			--property admin --value 1
	do_facet mgs $LCTL nodemap_modify --name $nm \
			--property trusted --value 1
	wait_nm_sync $nm
}

cleanup_nodemap_77kj() {
	local nm=$1

	# tolerate a nodemap already removed by the test body
	do_facet mgs $LCTL nodemap_del $nm 2>/dev/null
	wait_nm_sync $nm id ''
	do_facet mgs $LCTL nodemap_activate 0
	wait_nm_sync active
}

# Stop the given TBF rules and restore the default policy. Unlike
# cleanup_77k() this neither overwrites nor clears the EXIT trap, so it
# is safe to call both explicitly and from a stack_trap handler.
cleanup_tbf_77kj() {
	local osts=$(osts_nodes)
	local rule

	for rule in $1; do
		do_nodes $osts $LCTL set_param \
			ost.OSS.ost_io.nrs_tbf_rule="stop\ $rule" 2>/dev/null
	done
	do_nodes $osts $LCTL set_param ost.OSS.ost_io.nrs_policies="fifo"
	sleep 3
}

test_77kj() {
	(( "$OST1_VERSION" >= $(version_code 2.17.53) )) ||
		skip "Need OST version at least 2.17.53"

	local nm="TBF"
	local client_ip=$(host_nids_address $HOSTNAME $NETTYPE)
	local client_nid=$(h2nettype $client_ip)
	local osts=$(osts_nodes)

	stack_trap "cleanup_nodemap_77kj $nm"
	setup_nodemap_77kj $nm

	# TBF rule limiting a single nodemap
	stack_trap "cleanup_tbf_77kj ext_nm_rw"
	do_nodes $osts \
		$LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ nodemap" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_nm_rw\ nodemap={$nm}\ rate=20"
	nrs_write_read
	tbf_verify 20 20

	# removing the nodemap lifts the rate limit
	cleanup_nodemap_77kj $nm
	nrs_write_read
	tbf_verify 20 20 "" "" 1
	cleanup_tbf_77kj "ext_nm_rw"

	# nodemap+opcode rules must fail to start while the nodemap is gone
	do_nodes $osts \
		$LCTL set_param ost.OSS.ost_io.nrs_policies="tbf\ nodemap+opcode" ||
		error "failed to setup NRS TBF policy for nodemap+opcode"
	stack_trap "cleanup_tbf_77kj 'ext_w ext_r'"
	do_nodes $osts \
		$LCTL set_param \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ nodemap={$nm}\&opcode={ost_write}\ rate=20" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ nodemap={$nm}\&opcode={ost_read}\ rate=10" &&
			error "Start TBF rule should fail as the nodemap does not exist"
	setup_nodemap_77kj $nm
	do_nodes $osts \
		$LCTL set_param \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ nodemap={$nm}\&opcode={ost_write}\ rate=20" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ nodemap={$nm}\&opcode={ost_read}\ rate=10" ||
			error "Failed to start NRS TBF rule"
	nrs_write_read
	tbf_verify 20 10

	cleanup_tbf_77kj "ext_w ext_r"
	cleanup_nodemap_77kj $nm
}
run_test 77kj "Verify nodemap support for NRS TBF rule"

test_77l() {
	[[ "$OST1_VERSION" -ge $(version_code 2.10.56) ]] ||
		skip "Need OST version at least 2.10.56"

	do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid"
	do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="tbf"

	local output=$(do_facet ost1 lctl get_param \
			ost.OSS.ost_io.nrs_policies | \
			awk '/name: tbf/ {print;exit}' | \
			awk -F ': ' '{print $2}')

	if [ "$output" != "tbf" ]; then
		error "The generic TBF output is '$output', not 'tbf'"
	fi

	do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
}
run_test 77l "check the output of NRS policies for generic TBF"

test_77m() {
	if [ "$OST1_VERSION" -lt $(version_code 2.9.54) ]; then
		skip "Need OST version at least 2.9.54"
	fi

	local dir=$DIR/$tdir
	local osts=$(osts_nodes)

	mkdir $dir || error "mkdir $dir failed"
	$LFS setstripe -c $OSTCOUNT $dir || error "setstripe to $dir failed"
	chmod 777 $dir

	local osts=$osts
	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies=delay \
				       ost.OSS.ost_io.nrs_delay_min=4 \
				       ost.OSS.ost_io.nrs_delay_max=4 \
				       ost.OSS.ost_io.nrs_delay_pct=100
	[ $? -ne 0 ] && error "Failed to set delay policy"

	local start=$SECONDS
	do_nodes "${SINGLECLIENT:-$HOSTNAME}" "$RUNAS" \
		 dd if=/dev/zero of="$dir/nrs_delay_$HOSTNAME" bs=1M count=1 \
		   oflag=direct conv=fdatasync ||
		{ do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
		  error "dd on client failed (1)"; }
	local elapsed=$((SECONDS - start))

	# NRS delay doesn't do sub-second timing, so a request enqueued at
	# 0.9 seconds can be dequeued at 4.0
	[ $elapsed -lt 3 ] &&
		{ do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
		  error "Single 1M write should take at least 3 seconds"; }

	start=$SECONDS
	do_nodes "${SINGLECLIENT:-$HOSTNAME}" "$RUNAS" \
		 dd if=/dev/zero of="$dir/nrs_delay_$HOSTNAME" bs=1M count=10 \
		   oflag=direct conv=fdatasync ||
		{ do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
		  error "dd on client failed (2)"; }
	elapsed=$((SECONDS - start))

	[ $elapsed -lt 30 ] &&
		{ do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
		  error "Ten 1M writes should take at least 30 seconds"; }

	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
	[ $? -ne 0 ] && error "failed to set policy back to fifo"

	return 0
}
run_test 77m "check NRS Delay slows write RPC processing"

test_77n() { #LU-10802
	if [ "$OST1_VERSION" -lt $(version_code 2.10.58) ]; then
		skip "Need OST version at least 2.10.58"
	fi

	# Configure jobid_var
	local saved_jobid_var=$($LCTL get_param -n jobid_var)
	if [ $saved_jobid_var != procname_uid ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" procname_uid
	fi
	local osts=$(osts_nodes)

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ jobid" \
			ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ dd_runas\ jobid={*.$RUNAS_ID}\ rate=20"

	nrs_write_read
	tbf_verify 20 20 "$RUNAS"

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
			ost.OSS.ost_io.nrs_tbf_rule="start\ dd_runas\ jobid={dd.*}\ rate=20"

	nrs_write_read
	tbf_verify 20 20

	do_nodes $osts \
		lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
			ost.OSS.ost_io.nrs_policies="fifo"

	sleep 3

	local current_jobid_var=$($LCTL get_param -n jobid_var)
	if [ $saved_jobid_var != $current_jobid_var ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid_var
	fi
}
run_test 77n "check wildcard support for TBF JobID NRS policy"

test_77o() {
	(( $OST1_VERSION > $(version_code 2.14.54) )) ||
		skip "need OST > 2.14.54"

	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf\ nid"
	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ name\ nid={192.168.*.*@tcp}\ rate=10000"
	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ name1\ nid={192.168.*.*@tcp}\ rate=10000"
	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="change\ name1\ rank=name"
	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="stop\ name"
	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="fifo"
}
run_test 77o "Changing rank should not panic"

test_77q() {
	local i
	local gidlist="500 10 33   100 "
	local uidlist=" 500  11 3"

	(( $MDS1_VERSION > $(version_code 2.14.54) )) ||
		skip "need MDS >= 2.14.54"

	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf"
	stack_trap "do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies=fifo"

	# require 44cc782/LU-9859 to support list with unexpected spaces
	(( MDS1_VERSION >= $(version_code 2.15.57) )) ||
		gidlist=$(echo $gidlist) uidlist=$(echo $uidlist)

	for i in {1..50}; do
		local pid1 pid2

		do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start rule77q_1 uid={$uidlist}&gid={$gidlist} rate=100'" &
		pid1=$!
		do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start rule77q_2 uid={1000}&gid={1000} rate=100'" &
		pid2=$!
		wait $pid1 || error "$i: Fail to start TBF rule 'rule77q_1'"
		wait $pid2 || error "$i: Fail to start TBF rule 'rule77q_2'"

		do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'stop rule77q_1'" &
		pid1=$!
		do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'stop rule77q_2'" &
		pid2=$!
		wait $pid1 || error "$i: Fail to stop TBF rule 'rule77q_1'"
		wait $pid2 || error "$i: Fail to stop TBF rule 'rule77q_2'"
	done
}
run_test 77q "Parallel TBF rule definitions should not panic"

test_77p() {
	local c
	local -a spec_chars=(
		'@' '.' '~' '#' '/' '^' '%' '*' ';' ',' '?' '<' '>' ':'
		'+' '=' ')' '(' '{' '}' '|' '[' ']' '!' '&' '\$' '\`' '\\')

	(( $MDS1_VERSION > $(version_code 2.14.54) )) ||
		skip "need MDS >= 2.14.54"

	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf"
	stack_trap "do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies=fifo"

	# TBF rule name size is 16 bytes
	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ test_77p_overflo\ uid={500}\ rate=500" &&
		error "The length of tbf rule name is not checked" || true
	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ \ uid={500}\ rate=500" &&
		error "The server should not accept empty tbf rule name" || true
	do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ test_77p_empty" &&
		error "The server should not accept 'start <tbf_rule_name>' without an expression" || true

	# Test with special chars
	for c in "${spec_chars[@]}"; do
		do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start test77p${c}spec uid={500} rate=500'" &&
		error "Special char '${c}' should not be accepted in a tbf rule name" || true
	done

}
run_test 77p "Check validity of rule names for TBF policies"

cleanup_77r() {
	local pid=$1
	local saved_jobid=$2
	local current_jobid_var

	echo "cleanup 77r $pid"

	do_facet mds1 $LCTL set_param -n mds.MDS.mdt.nrs_policies=fifo
	kill $pid || echo "fail to kill md thread"

	current_jobid_var=$($LCTL get_param -n jobid_var)
	if [ $saved_jobid != $current_jobid_var ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid
	fi

	sleep 2
	rm -rf $DIR1/$tdir
}

md_thread_run="true"
md_thread_77r() {
	local pid

	while $md_thread_run; do
		printf '%s\n' {$DIR1,$DIR2}/$tdir/${tfile}-{01..20} |
			xargs -P20 -I{} $RUNAS bash -c 'touch {}; rm -f {}' \
				&> /dev/null & pid=$!
		trap "echo kill md_thread xargs; md_thread_run=false; kill $pid" INT TERM
		wait $pid
	done
}

wait_policy_state() {
	local state="$1"
	local policy="$2"
	local change_pid="$3"
	local time

	for time in {1..60}; do
		local nbr_started

		nbr_started=$(do_facet mds1 \
			      $LCTL get_param mds.MDS.mdt.nrs_policies |
			      grep -E -A2 "name: ${policy}$" |
			      grep -c "state: $state")

		[[ "$nbr_started" != 2 ]] || return 0
		sleep 1
	done

	[[ -z "$change_pid" ]] || kill $change_pid || true
	return 1
}

test_77r() { #LU-14976

	(( MDS1_VERSION > $(version_code 2.15.56) )) ||
		skip "need MDS >= 2.15.56 c098c095 change nrs policies at run time"

	local pid
	local -A rules
	local -a policies
	local saved_jobid_var

	rules["tbf uid"]="start md_rule uid={$RUNAS_ID} rate=1"
	rules["tbf gid"]="start md_rule gid={$RUNAS_GID} rate=1"
	rules["tbf jobid"]="start md_rule jobid={*.$RUNAS_ID} rate=1"
	rules["tbf"]="start md_rule uid={$RUNAS_ID} rate=1"
	policies=(
	"tbf uid"
	"tbf gid"
	"tbf jobid"
	"tbf"
	"fifo"
	)

	test_mkdir -i 0 -c 1 $DIR1/$tdir
	chmod 777 $DIR1/$tdir

	# Configure jobid_var
	saved_jobid_var=$($LCTL get_param -n jobid_var)
	if [ $saved_jobid_var != procname_uid ]; then
		set_persistent_param_and_check client \
			"jobid_var" "$FSNAME.sys.jobid_var" procname_uid
	fi

	# start md thread
	md_thread_77r & pid=$!
	stack_trap "cleanup_77r $pid '$saved_jobid_var'"

	local policy
	for policy in "${policies[@]}"; do
		local change_pid

		# wait to queue requests
		sleep 5

		do_facet mds1 "$LCTL set_param mds.MDS.mdt.nrs_policies='$policy'" &
		change_pid=$!

		wait_policy_state "started" "$policy" "$change_pid" ||
			error "timeout to start '$policy' policy"

		[[ -n "${rules[$policy]}" ]] || continue

		do_facet mds1 "$LCTL set_param mds.MDS.mdt.nrs_tbf_rule='${rules[$policy]}'" ||
			error "fail to set rule '${rules[$policy]}' to '$policy'"
	done

	wait_policy_state "stopped" "tbf" ||
		error "fail to stop tbf policy"

	echo "check the number of requests in queue:"
	local awkcmd='/name: / {last = $3} '
	awkcmd+='/queued: / {printf "    %s: %d\n", last, $2;'
	awkcmd+='	if (last == "tbf" && $2 > 0) exit 1;}'
	do_facet mds1 $LCTL get_param mds.MDS.mdt.nrs_policies | awk "$awkcmd" ||
		error "request leak in tbf policies"
}
run_test 77r "Change type of tbf policy at run time"

test_77s() { #LU-19597
	(( MDS1_VERSION >= $(version_code 2.16.61-40) )) ||
		skip "need MDS >= 2.16.61-40"

	mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"

	local nrs_policies=mds.MDS.mdt.nrs_policies
	local nrs_tbf_rule=mds.MDS.mdt.nrs_tbf_rule
	local cache_path=/sys/module/ptlrpc/parameters/tbf_jobid_cache_size
	local cache_size=$(do_facet mds1 "cat $cache_path")
	local -a saved_params=( $($LCTL get_param jobid_var jobid_name ) )

	stack_trap "$LCTL set_param ${saved_params[*]}"
	$LCTL set_param jobid_name=%e.%p jobid_var=TEST_77s_JOBVAR

	stack_trap "do_facet mds1 'echo $cache_size > $cache_path'"
	cache_size=10
	do_facet mds1 "echo $cache_size > $cache_path"

	stack_trap "do_facet mds1 $LCTL set_param $nrs_policies=fifo"
	do_facet mds1 "$LCTL set_param $nrs_policies='tbf jobid'" ||
		error "fail to start 'tbf jobid' policy"

	do_facet mds1 "$LCTL set_param $nrs_tbf_rule='start touch jobid={touch.*} rate=1000'" ||
		error "fail to start rule 'touch'"
	do_facet mds1 "$LCTL set_param $nrs_tbf_rule='start unlink jobid={unlink.*} rate=1000'" ||
		error "fail to start rule 'unlink'"

	printf '%s\n' {$DIR1,$DIR2}/$tdir/${tfile}-{0001..1000} |
		xargs -P20 -n5 touch
	do_facet mds1 "$LCTL get_param $nrs_tbf_rule" |
		awk '/^touch/ {print $0; if ($NF > '$cache_size') exit(1);}' ||
		error "TBF LRU shrinker failed (too much refs for 'touch' rule)"

	printf '%s\n' $DIR1/$tdir/${tfile}-{0001..1000} |
		xargs -P20 -n1 unlink
	do_facet mds1 "$LCTL get_param $nrs_tbf_rule" |
		awk '/^unlink/ {print $0; if ($NF > '$cache_size') exit(1);}' ||
		error "TBF LRU shrinker failed (too much refs for 'unlink' rule)"

}
run_test 77s "Check TBF LRU shrinker"

test_78() { #LU-6673
	local rc
	local osts=$(osts_nodes)

	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="orr" &
	do_nodes $osts lctl set_param ost.OSS.*.nrs_orr_quantum=1
	rc=$?
	[[ $rc -eq 3 ]] && skip "no NRS exists" && return
	# Valid return codes are:
	# 0: Tuning succeeded
	# ENODEV: Policy is still stopped
	# EAGAIN: Policy is being initialized
	[ $rc -eq 0 -o $rc -eq 19 -o $rc -eq 11 ] ||
		error "Expected set_param to return 0|ENODEV|EAGAIN"

	# Cleanup the ORR policy
	do_nodes $osts lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
	[ $? -ne 0 ] && error "failed to set policy back to fifo"
	return 0
}
run_test 78 "Enable policy and specify tunings right away"

test_79() {
	remote_mds_nodsh && skip "remote MDS with nodsh" && return
	test_mkdir $DIR/$tdir

	# Prevent interference from layout intent RPCs due to
	# asynchronous writeback. These will be tested in 130c below.
	do_nodes ${CLIENTS:-$HOSTNAME} sync

	setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
		error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"

#define OBD_FAIL_MDS_INTENT_DELAY		0x160
	local mdtidx=$($LFS getstripe -m $DIR/$tdir)
	local facet=mds$((mdtidx + 1))
	local node=$(facet_active_host $facet)

	stat $DIR/$tdir
	set_nodes_failloc $node 0x80000160
	getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null  &
	local pid=$!
	sleep 2

#define OBD_FAIL_MDS_GETXATTR_PACK       0x131
	set_nodes_failloc $node 0x80000131

	wait $pid
	return 0
}
run_test 79 "xattr: intent error"

test_80a() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	local MDTIDX=1
	local mdt_index
	local i
	local file
	local pid

	mkdir_on_mdt0 $DIR1/$tdir
	mkdir -p $DIR1/$tdir/dir
	createmany -o $DIR1/$tdir/dir/f 10 ||
		error "create files under remote dir failed $i"

	cp /etc/passwd $DIR1/$tdir/$tfile

	# attempt to migrate an open file
	multiop_bg_pause $DIR2/$tdir/$tfile O_c || error "open $file failed"
	pid=$!
	# give multiop a chance to open
	sleep 1

	local open_file_migrate=false
	(($MDS1_VERSION >= $(version_code 2.16.50) )) && open_file_migrate=true

	if $open_file_migrate; then
		local oldfid=$($LFS path2fid $DIR1/$tdir/$tfile)

		$LFS migrate -m $MDTIDX $DIR1/$tdir ||
			error "migrate open files should not fail"

		kill -USR1 $pid

		local newfid=$($LFS path2fid $DIR1/$tdir/$tfile)

		[[ "$oldfid" == "$newfid" ]] ||
			error "FID of the open file changed from $oldfid to $newfid"

	else
		$LFS migrate -m $MDTIDX $DIR1/$tdir &&
			error "migrate open files should failed with open files"

		kill -USR1 $pid

		$LFS migrate -m $MDTIDX $DIR1/$tdir ||
			error "migrate remote dir error"
	fi

	echo "Finish migration, then checking.."
	for file in $(find $DIR1/$tdir); do
		$open_file_migrate && [[ "$file" == "$DIR1/$tdir/$tfile" ]] &&
			continue
		mdt_index=$($LFS getstripe -m $file)
		[ $mdt_index == $MDTIDX ] ||
			error "$file is not on MDT${MDTIDX}"
	done

	diff /etc/passwd $DIR1/$tdir/$tfile ||
		error "file different after migration"

	rm -rf $DIR1/$tdir || error "rm dir failed after migration"
}
run_test 80a "migrate directory when some children is being opened"

cleanup_80b() {
	trap 0
	kill -9 $migrate_pid
	wait $migrate_pid 2>/dev/null
	wait_delete_completed
}

success_count=0
failure_count=0

run_and_count()
{
	eval $@ &>/dev/null && success_count=$((success_count + 1)) ||
		failure_count=$((failure_count + 1))
}

test_80b() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	local migdir1=$DIR1/$tdir/migrate_dir
	local migdir2=$DIR2/$tdir/migrate_dir
	local start_time
	local mdt_idx
	local rc=0
	local rc1=0

	trap cleanup_80b EXIT
	# prepare migrate directory
	mkdir -p $migdir1
	for F in {1,2,3,4,5}; do
		echo "$F$F$F$F$F" > $migdir1/file$F
		echo "$F$F$F$F$F" > $DIR/$tdir/file$F
	done

	# migrate the directories among MDTs
	(
		while true; do
			mdt_idx=$((RANDOM % MDSCOUNT))
			# migrate may fail
			$LFS migrate -m $mdt_idx $migdir1 &>/dev/null
		done
	) &
	migrate_pid=$!

	echo "start migration thread $migrate_pid"
	# access the files at the same time
	start_time=$SECONDS
	echo "accessing the migrating directory for 1 minute..."
	while ((SECONDS - start_time < 60)); do
		run_and_count ls $migdir2
		run_and_count diff -u $DIR2/$tdir/file1 $migdir2/file1
		run_and_count "cat $migdir2/file2 > $migdir2/file3"
		run_and_count "echo "aaaaa" > $migdir2/file4"
		run_and_count stat $migdir2/file5
		run_and_count touch $migdir2/source_file
		if [ -e $migdir2/source_file ]; then
			run_and_count ln $migdir2/source_file \
				$migdir2/link_file

			[ -e $migdir2/link_file ] &&
				rm -rf $migdir2/link_file

			run_and_count mrename $migdir2/source_file \
				$migdir2/target_file

			[ -e $migdir2/target_file ] &&
				run_and_count rm -rf $migdir2/target_file ||
				run_and_count rm -rf $migdir2/source_file
		fi

		kill -0 $migrate_pid || {
			echo "migration stopped 1"
			break
		}
	done

	# check migration are still there
	kill -0 $migrate_pid || error "migration stopped 2"
	cleanup_80b
	# access during migration may fail
	echo "concurrent access $failure_count failures, $success_count successes"
}
run_test 80b "Accessing directory during migration"

test_81a() {
	(( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"

	rm -rf $DIR1/$tdir
	mkdir -p $DIR1/$tdir || error "mkdir $tdir failed"

	$LFS setdirstripe -i0 -c$MDSCOUNT $DIR1/$tdir/d0 ||
		error "setdirstripe d0 failed"
	$LFS setdirstripe -i0 -c$MDSCOUNT $DIR1/$tdir/d1 ||
		error "setdirstripe d1 failed"

	touch $DIR1/$tdir/d0/0 || error "create 0 failed"
	mv $DIR1/$tdir/d0/0 $DIR1/$tdir/d1/0 || error "rename d0/0 d1/0 failed"
	stat $DIR1/$tdir/d0/0 && error "stat mv file succeed"
	mv $DIR2/$tdir/d1/0 $DIR2/$tdir/d0/0 || error "rename d1/0 d0/0 failed"
	stat $DIR1/$tdir/d0/0 || error "stat failed"

	local files=$(ls -ai $DIR1/$tdir/d0 | sort -u | wc -l)

	if (( $files != 3 )); then
		ls -ai $DIR1/$tdir/d0
		error "expect 3 files, got $files"
	fi

	return 0
}
run_test 81a "rename and stat under striped directory"

test_81b() {
	[ $MDSCOUNT -lt 2 ] &&
		skip "We need at least 2 MDTs for this test"

	local setattr_pid

	$LFS mkdir -c $MDSCOUNT $DIR1/$tdir || error "$LFS mkdir"
	createmany -o $DIR1/$tdir/$tfile. $COUNT || error "createmany"

	(
		while true; do
			touch $DIR1/$tdir
		done
	) &
	setattr_pid=$!

	for ((i = 0; i < COUNT; i++)); do
		mrename $DIR2/$tdir/$tfile.$i $DIR2/$tdir/$tfile-new.$i \
			> /dev/null
	done

	kill -9 $setattr_pid
}
run_test 81b "rename under striped directory doesn't deadlock"

test_81c() {
	[ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
	[ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
		skip "Need MDS version at least 2.13.52"

	# source is local, source parent is remote
	$LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
	$LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
	$LFS mkdir -i 3 $DIR1/${tdir}_src/sub || error "mkdir sub"
	$LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
	stat $DIR2/${tdir}_src/sub || error "stat sub failed"
	mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
	[ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
	rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt

	# source is remote, source parent is local
	$LFS mkdir -i 3 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
	$LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
	$LFS mkdir -i 0 $DIR1/${tdir}_src/sub || error "mkdir sub"
	$LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
	stat $DIR2/${tdir}_src/sub || error "stat sub failed"
	mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
	[ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
	rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt

	# source and source parent are remote
	$LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
	$LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
	mkdir $DIR1/${tdir}_src/sub || error "mkdir sub"
	$LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
	stat $DIR2/${tdir}_src/sub || error "stat sub failed"
	mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
	[ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
	rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt

	# source and source parent are remote, and source is remote object
	$LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
	$LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
	$LFS mkdir -i 2 $DIR1/${tdir}_src/sub || error "mkdir sub"
	$LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
	stat $DIR2/${tdir}_src/sub || error "stat sub failed"
	mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
	[ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone" || true
}
run_test 81c "rename revoke LOOKUP lock for remote object"

cleanup_81d() {
	for ((mds = 0; mds < $MDSCOUNT; mds++)); do
		local d2=$DIR2/$tdir-$mds

		rm -rf $d2 &
	done
	wait || error "rm failed"
}

test_81d() {
	local setattr_pid
	local mdts=$(mdts_nodes)

	do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"

	stack_trap cleanup_81d
	for ((mds = 0; mds < $MDSCOUNT; mds++)); do
		local d1=$DIR1/$tdir-$mds

		$LFS mkdir -i $mds $d1 $d1/_temporary || error "mkdir failed"
		createmany -o $d1/_temporary/$tfile. $COUNT ||
			error "createmany failed for $d1/_temporary"
	done

	for ((mds = 0; mds < $MDSCOUNT; mds++)); do
		local d1=$DIR1/$tdir-$mds
		local d2=$DIR2/$tdir-$mds

		for ((i = 0; i < COUNT; i++)); do
			mrename $d1/_temporary/$tfile.$i $d1/$tfile.$i &
			((i++))
			mrename $d2/_temporary/$tfile.$i $d2/$tfile.$i &
		done
	done
	wait || error "rename failed"

	cleanup_81d
	local stats=$DIR1/md_stats
	local total=$((MDSCOUNT * COUNT))

	do_nodes $mdts "$LCTL get_param -n mdt.*.md_stats" > $stats
	cat $stats
	crossdir=$(awk '/crossdir_rename/ {sum+=$2} END {print sum}' $stats)
	(( crossdir == total )) ||
		error "not crossdir: $crossdir != $total"
	samedir=$(awk '/samedir_rename/ {sum+=$2} END {print sum}' $stats)
	(( samedir == 0 )) || error "considered samedir: $samedir"
	pardir=$(awk '/parallel_rename_dir/ {sum+=$2} END {print sum}' $stats)
	(( pardir == 0 )) || error "considered directory: $pardir"


	(( MDS1_VERSION >= $(version_code 2.15.60) )) ||
		{ echo "need MDS >= 2.15.60 for parallel cross-dir"; return 0; }

	parfile=$(awk '/parallel_rename_file/ {sum+=$2} END {print sum}' $stats)
	(( parfile == total )) ||
		error "not considered file: $parfile != $total"
}
run_test 81d "parallel rename file cross-dir on same MDT"

test_82() {
	[[ "$MDS1_VERSION" -gt $(version_code 2.6.91) ]] ||
		skip "Need MDS version at least 2.6.92"

	# Client 1 creates a file.
	multiop_bg_pause $DIR1/$tfile O_ac || error "multiop_bg_pause 1"
	pid1=$!
	# Client 2 opens the file.
	multiop_bg_pause $DIR2/$tfile o_Ac || error "multiop_bg_pause 2"
	pid2=$!
	# Client 1 makes the file an orphan.
	rm $DIR1/$tfile || error "rm"
	# Client 2 sets EA "user.multiop".
	kill -s USR1 $pid2
	wait $pid2 || error "multiop 2"
	# Client 1 gets EA "user.multiop".  This used to fail because the EA
	# cache refill would get "trusted.link" from mdd_xattr_list() but
	# -ENOENT when trying to get "trusted.link"'s value.  See also sanity
	# 102q.
	kill -s USR1 $pid1
	wait $pid1 || error "multiop 1"
}
run_test 82 "fsetxattr and fgetxattr on orphan files"

test_83() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	local pid1
	local pid2

	(
		cd $DIR1
		while true; do
			$LFS mkdir -i1 -c2 $tdir
			rmdir $tdir
		done
	) &
	pid1=$!
	echo "start pid $pid1 to create/unlink striped directory"

	# Access the directory at the same time
	(
		cd $DIR2
		while true; do
			stat $tdir > /dev/null 2>&1
		done
	) &
	pid2=$!
	echo "start pid $pid2 to stat striped directory"

	sleep 120
	kill $pid1 $pid2
	wait $pid1 $pid2

	return 0
}
run_test 83 "access striped directory while it is being created/unlinked"

test_84() {
	[ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
		skip "lustre < 2.12.55 does not contain LU-12485 fix"

	local mtime

	$MULTIOP $DIR/$tfile oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:c ||
		error "create $tfile failed"
	mtime=$(stat -c%Y $DIR/$tfile)
	mtime=$((mtime + 200))

	#define OBD_FAIL_OBD_0NLINK_RACE  0x60b
	do_facet mds1 $LCTL set_param fail_loc=0x8000060b

	touch -c -m $mtime $DIR/$tfile &
	setattr_pid=$!
	# sleep a while to let 'touch' run first
	sleep 5
	rm -f $DIR2/$tfile || error "unlink $tfile failed"

	# touch may fail
	wait $setattr_pid || true
}
run_test 84 "0-nlink race in lu_object_find()"

test_85() {
	llapi_root_test $DIR/$tfile $DIR2/$tfile
}
run_test 85 "Lustre API root cache race"

test_90() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	local pid1
	local pid2
	local duration=180

	[ "$SLOW" = "yes" ] && duration=600
	# Open/Create under striped directory
	(
		cd $DIR1
		while true; do
			$LFS mkdir -c$MDSCOUNT $tdir > /dev/null 2>&1
			touch $tdir/f{0..3} > /dev/null 2>&1
		done
	) &
	pid1=$!
	echo "start pid $pid1 to open/create under striped directory"

	# unlink the striped directory at the same time
	(
		cd $DIR2
		while true; do
			rm -rf $tdir > /dev/null 2>&1
		done
	) &
	pid2=$!
	echo "start pid $pid2 to unlink striped directory"

	sleep $duration

	kill $pid1 $pid2
	wait $pid1 $pid2

	return 0
}
run_test 90 "open/create and unlink striped directory"

test_91() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
	local pid1
	local pid2
	local duration=180

	[ "$SLOW" = "yes" ] && duration=600
	# chmod striped directory
	(
		cd $DIR1
		while true; do
			$LFS mkdir -c$MDSCOUNT $tdir > /dev/null 2>&1
			chmod go+w $tdir > /dev/null 2>&1
		done
	) &
	pid1=$!
	echo "start pid $pid1 to chmod striped directory"

	# unlink the striped directory at the same time
	(
		cd $DIR2
		while true; do
			rm -rf $tdir > /dev/null 2>&1
		done
	) &
	pid2=$!
	echo "start pid $pid2 to unlink striped directory"

	sleep $duration

	kill $pid1 $pid2
	wait $pid1 $pid2

	return 0
}
run_test 91 "chmod and unlink striped directory"

test_92() {
	[ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return

	local fd=$(free_fd)
	local cmd="exec $fd<$DIR1/$tdir"
	$LFS setdirstripe -c$MDSCOUNT $DIR1/$tdir || error "mkdir $tdir fails"
	eval $cmd
	cmd="exec $fd<&-"
	trap "eval $cmd" EXIT
	cd $DIR1/$tdir || error "cd $DIR1/$tdir fails"
	rmdir ../$tdir || error "rmdir ../$tdir fails"

	#define OBD_FAIL_LLITE_NO_CHECK_DEAD  0x1408
	$LCTL set_param fail_loc=0x1408
	mkdir $DIR2/$tdir/dir && error "create dir succeeds"
	$LFS setdirstripe -i1 $DIR2/$tdir/remote_dir &&
		error "create remote dir succeeds"
	$LCTL set_param fail_loc=0
	eval $cmd
	return 0
}
run_test 92 "create remote directory under orphan directory"

test_93() {
	local rc1=0
	local rc2=0
	local old_rr

	mkdir -p $DIR1/$tfile-1/
	mkdir -p $DIR2/$tfile-2/
	local old_rr=$(do_facet $SINGLEMDS "$LCTL get_param -n \
		lod.$FSNAME-MDT0000-*/qos_threshold_rr" | sed -e 's/%//')
	do_facet $SINGLEMDS "$LCTL set_param -n \
		lod.$FSNAME-MDT0000-*/qos_threshold_rr=100"
	#define OBD_FAIL_MDS_LOV_CREATE_RACE     0x163
	do_facet $SINGLEMDS "$LCTL set_param fail_loc=0x00000163"

	$LFS setstripe -c -1 $DIR1/$tfile-1/file1 &
	local PID1=$!
	sleep 1
	$LFS setstripe -c -1 $DIR2/$tfile-2/file2 &
	local PID2=$!
	wait $PID2
	wait $PID1
	do_facet $SINGLEMDS "$LCTL set_param fail_loc=0x0"
	do_facet $SINGLEMDS "$LCTL set_param -n \
		lod.$FSNAME-MDT0000-*/qos_threshold_rr=$old_rr"

	$LFS getstripe $DIR1/$tfile-1/file1
	rc1=$($LFS getstripe -q $DIR1/$tfile-1/file1 |
		awk '{if (/[0-9]/) print $1 }' | sort | uniq -d | wc -l)
	$LFS getstripe $DIR2/$tfile-2/file2
	rc2=$($LFS getstripe -q $DIR2/$tfile-2/file2 |
		awk '{if (/[0-9]/) print $1 }' | sort | uniq -d | wc -l)
	echo "rc1=$rc1 and rc2=$rc2 "
	[ $rc1 -eq 0 ] && [ $rc2 -eq 0 ] ||
		error "object allocate on same ost detected"
}
run_test 93 "alloc_rr should not allocate on same ost"

test_94() {
	$LCTL set_param osc.*.idle_timeout=0
	dd if=/dev/zero of=$DIR2/$tfile bs=4k count=2 conv=fsync

	local before=$(date +%s)
	local evict

	$LCTL mark write
#define OBD_FAIL_LDLM_PAUSE_CANCEL       0x312
	$LCTL set_param fail_val=5 fail_loc=0x80000312
	dd if=/dev/zero of=$DIR/$tfile conv=notrunc oflag=append bs=4k count=1 &
	local pid=$!
	sleep 2

#define OBD_FAIL_LDLM_PAUSE_CANCEL_LOCAL 0x329
	$LCTL set_param fail_val=6 fail_loc=0x80000329
	$LCTL mark kill $pid
	kill -ALRM $pid

	dd if=/dev/zero of=$DIR2/$tfile conv=notrunc oflag=append bs=4k count=1

	wait $pid
	dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 conv=fsync

	evict=$(do_facet client $LCTL get_param \
		osc.$FSNAME-OST*-osc-*/state |
	    awk -F"[ [,]" '/EVICTED ]$/ { if (t<$5) {t=$5;} } END { print t }')

	[ -z "$evict" ] || [[ $evict -le $before ]] ||
		(do_facet client $LCTL get_param \
			osc.$FSNAME-OST*-osc-*/state;
		    error "eviction happened: $evict before:$before")
	$LCTL set_param osc.*.idle_timeout=debug
}
run_test 94 "signal vs CP callback race"

test_95a() {
	local file=$DIR/$tfile
	local file2=$DIR2/$tfile
	local fast_read_save
	local pid

	fast_read_save=$($LCTL get_param -n llite.*.fast_read | head -n 1)
	[ -z "$fast_read_save" ] && skip "no fast read support"

	stack_trap "$LCTL set_param llite.*.fast_read=$fast_read_save" EXIT
	$LCTL set_param llite.*.fast_read=0

	$LFS setstripe -c $OSTCOUNT $file || error "failed to setstripe $file"
	dd if=/dev/zero of=$file bs=1M count=2 || error "failed to write $file"
	cancel_lru_locks $OSC
	$MULTIOP $file Oz1048576w4096c || error "failed to write $file"
	$MULTIOP $file oz1044480r4096c || error "failed to read $file"

	# OBD_FAIL_LLITE_PAGE_INVALIDATE_PAUSE	0x1421
	$LCTL set_param fail_loc=0x80001421 fail_val=7
	$MULTIOP $file2 Oz1048576w4096_c &
	pid=$!

	sleep 2
	# OBD_FAIL_LLITE_READPAGE_PAUSE		0x1422
	$LCTL set_param fail_loc=0x80001422 fail_val=10
	$MULTIOP $file oz1044480r4096c || error "failed to read $file"

	kill -USR1 $pid && wait $pid || error "wait for PID $pid failed"
}
run_test 95a "Check readpage() on a page that was removed from page cache"

test_95b() {
	local file=$DIR/$tfile
	local file2=$DIR2/$tfile
	local fast_read_save
	local pid

	fast_read_save=$($LCTL get_param -n llite.*.fast_read | head -n 1)
	[ -z "$fast_read_save" ] && skip "no fast read support"

	stack_trap "$LCTL set_param llite.*.fast_read=$fast_read_save" EXIT
	$LCTL set_param llite.*.fast_read=0

	$LFS setstripe -c $OSTCOUNT $file || error "failed to setstripe $file"
	dd if=/dev/zero of=$file bs=$((PAGE_SIZE * 3)) count=1 ||
		error "failed to write $file"

	# This does the read from the second mount, so this flushes the pages
	# the first mount and creates new ones on the second mount
	# OBD_FAIL_LLITE_READPAGE_PAUSE2	0x1424
	$LCTL set_param fail_loc=0x80001424 fail_val=5
	$MULTIOP $file2 or${PAGE_SIZE}c &
	pid=$!

	sleep 2
	fadvise_dontneed_helper $file2
	$LCTL set_param fail_loc=0
	sleep 4
	wait $pid || error "failed to read file"
}
run_test 95b "Check readpage() on a page that is no longer uptodate"

# Data-on-MDT tests
test_100a() {
	skip "Reserved for glimpse-ahead" && return
	[ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
		skip "Need MDS version at least 2.10.55"

	mkdir -p $DIR/$tdir

	$LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom

	lctl set_param -n mdc.*.stats=clear
	dd if=/dev/zero of=$DIR2/$tdir/dom bs=4096 count=1 || return 1

	$CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #1"
	# first stat from server should return size data and save glimpse
	local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
	[ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
	# second stat to check size is NOT cached on client without IO lock
	$CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #2"

	local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
	[ $gls -ge 1 ] || error "Expect glimpse RPCs but none"
	rm -f $dom
}
run_test 100a "DoM: glimpse RPCs for stat without IO lock (DoM only file)"

test_100b() {
	[ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
		skip "Need MDS version at least 2.10.55"

	mkdir -p $DIR/$tdir

	$LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom

	lctl set_param -n mdc.*.stats=clear
	dd if=/dev/zero of=$DIR2/$tdir/dom bs=4096 count=1 || return 1
	cancel_lru_locks mdc
	# first stat data from server should have size
	$CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #1"
	# second stat to check size is cached on client
	$CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #2"

	local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
	# both stats should cause no glimpse requests
	[ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
	rm -f $dom
}
run_test 100b "DoM: no glimpse RPC for stat with IO lock (DoM only file)"

test_100c() {
	[ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
		skip "Need MDS version at least 2.10.55"

	mkdir -p $DIR/$tdir

	$LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom

	lctl set_param -n mdc.*.stats=clear
	lctl set_param -n osc.*.stats=clear
	dd if=/dev/zero of=$DIR2/$tdir/dom bs=2048K count=1 || return 1

	# check that size is merged from MDT and OST correctly
	$CHECKSTAT -t file -s 2097152 $DIR/$tdir/dom ||
		error "Wrong size from stat #1"

	local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
	[ $gls -eq 0 ] && error "Expect OST glimpse RPCs but got none"

	rm -f $dom
}
run_test 100c "DoM: write vs stat without IO lock (combined file)"

test_100d() {
	[ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
		skip "Need MDS version at least 2.10.55"

	mkdir -p $DIR/$tdir

	$LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom


	dd if=/dev/zero of=$DIR2/$tdir/dom bs=2048K count=1 || return 1
	lctl set_param -n mdc.*.stats=clear
	$TRUNCATE $DIR2/$tdir/dom 4096

	# check that reported size is valid after file grows to OST and
	# is truncated back to MDT stripe size
	$CHECKSTAT -t file -s 4096 $DIR/$tdir/dom ||
		error "Wrong size from stat #1"

	local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
	[ $gls -eq 0 ] && error "Expect OST glimpse but got none"

	rm -f $dom
}
run_test 100d "DoM: write+truncate vs stat without IO lock (combined file)"

test_100e() {
	[ "$MDS1_VERSION" -lt $(version_code 2.11.50) ] &&
		skip "Need MDS version at least 2.11.50"

	local dom=$DIR/$tdir/dom
	local dom2=$DIR2/$tdir/dom
	mkdir -p $DIR/$tdir

	$LFS setstripe -E 1024K -L mdt $DIR/$tdir

	cancel_lru_locks mdc
	dd if=/dev/urandom of=$dom bs=12000 count=1
	$TRUNCATE $dom2 6000
	cancel_lru_locks mdc
	lctl set_param -n mdc.*.stats=clear
	# expect read-on-open to return all data before write
	cat /etc/hosts >> $dom
	local read=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
	[[ $read -eq 0 ]] || error "Unexpected $read READ RPCs"
}
run_test 100e "DoM: read on open and file size"

test_101a() {
	[ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
		skip "Need MDS version at least 2.10.55"

	$LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
	# to get layout
	$CHECKSTAT -t file $DIR1/$tfile

	local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
	sysctl -wq vm.dirty_writeback_centisecs=0

	trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT

	# open + IO lock
	dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 ||
		error_noexit "Write fails"
	# must discard pages
	lctl set_param -n mdc.*.stats=clear
	rm $DIR2/$tfile || error "Unlink fails"

	local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
	[ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
}
run_test 101a "Discard DoM data on unlink"

test_101b() {
	[ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
		skip "Need MDS version at least 2.10.55"

	$LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
	touch $DIR1/${tfile}_2
	# to get layout
	$CHECKSTAT -t file $DIR1/$tfile

	local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
	sysctl -wq vm.dirty_writeback_centisecs=0

	trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT

	# open + IO lock
	dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 || error "Write fails"
	# must discard pages
	lctl set_param -n mdc.*.stats=clear
	mv $DIR2/${tfile}_2 $DIR2/$tfile || error "Rename fails"

	local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
	[ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
}
run_test 101b "Discard DoM data on rename"

test_101c() {
	[ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
		skip "Need MDS version at least 2.10.55"

	$LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
	# to get layout
	$CHECKSTAT -t file $DIR1/$tfile

	local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
	sysctl -wq vm.dirty_writeback_centisecs=0

	trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT

	# open + IO lock
	dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 || error "Write fails"
	$MULTIOP $DIR1/$tfile O_c &
	MULTIOP_PID=$!
	sleep 1
	lctl set_param -n mdc.*.stats=clear
	rm $DIR2/$tfile > /dev/null || error "Unlink fails for opened file"
	kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"

	local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
	[ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
}
run_test 101c "Discard DoM data on close-unlink"

# test to verify file handle related system calls
# (name_to_handle_at/open_by_handle_at)
# The new system calls are supported in glibc >= 2.14.

# test to verify we can open by handle an unlinked file from > 1 client
# This test opens the file normally on $DIR1, which is on one mount, and then
# opens it by handle on $DIR2, which is on a different mount.
test_102() {
	(( "$MDS1_VERSION" >= $(version_code 2.11.57) )) ||
		skip "Needs MDS version 2.11.57 or later"

	echo "Test file_handle syscalls" > $DIR/$tfile ||
		error "write failed"
	check_fhandle_syscalls $DIR/$tfile $DIR2 ||
		error "check_fhandle_syscalls $tfile failed"

	# test this is working on DNE directories also
	if (( MDSCOUNT > 1 && MDS1_VERSION >= $(version_code 2.14.52) )); then
		$LFS mkdir -i 1 $DIR/$tdir.remote
		cancel_lru_locks mdc
		check_fhandle_syscalls $DIR/$tdir.remote $DIR2 ||
			error "check_fhandle_syscalls $tdir.remote failed"
		$LFS mkdir -c -1 $DIR/$tdir.remote/subdir
		cancel_lru_locks mdc
		check_fhandle_syscalls $DIR/$tdir.remote/subdir $DIR2 ||
			error "check_fhandle_syscalls $tdir.remote/subdir fail"

		$LFS mkdir -c -1 $DIR/$tdir.stripe
		cancel_lru_locks mdc
		check_fhandle_syscalls $DIR/$tdir.stripe $DIR2 ||
			error "check_fhandle_syscalls $tdir.stripe failed"
		$LFS mkdir -c -1 $DIR/$tdir.stripe/subdir
		cancel_lru_locks mdc
		check_fhandle_syscalls $DIR/$tdir.stripe/subdir $DIR2 ||
			error "check_fhandle_syscalls $tdir.stripe/subdir fail"
	fi
}
run_test 102 "Test open by handle of unlinked file"

# Compare file size between first & second mount, ensuring the client correctly
# glimpses even with unused speculative locks - LU-11670
test_103() {
	[ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
		skip "Lockahead needs OST version at least 2.10.50"

	local locktest=23

	test_mkdir -p $DIR/$tdir

	# Force file on to OST0
	$LFS setstripe -i 0 $DIR/$tdir

	# Do not check multiple locks on glimpse
	# OBD_FAIL_OSC_NO_SIZE_DATA 0x415
	$LCTL set_param fail_loc=0x415

	# Delay write commit by 2 seconds to guarantee glimpse wins race
	# The same fail_loc is used on client & server so it can work in the
	# single node sanity setup
	do_facet ost1 $LCTL set_param fail_loc=0x415 fail_val=2

	echo "Incorrect size expected (no glimpse fix):"
	lockahead_test -d $DIR/$tdir -D $DIR2/$tdir -t $locktest -f $tfile
	rc=$?
	if [ $rc -eq 0 ]; then
		echo "This doesn't work 100%, but this is just reproducing the bug, not testing the fix, so OK to not fail test."
	fi

	# guarantee write commit timeout has expired
	sleep 2

	# Clear fail_loc on client
	$LCTL set_param fail_loc=0

	# Delay write commit by 2 seconds to guarantee glimpse wins race
	# OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
	do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2

	# Write commit is still delayed by 2 seconds
	lockahead_test -d $DIR/$tdir -D $DIR2/$tdir -t $locktest -f $tfile
	rc=$?
	[ $rc -eq 0 ] || error "Lockahead test$locktest failed, $rc"

	# guarantee write commit timeout has expired
	sleep 2

	rm -f $DIR/$tfile || error "unable to delete $DIR/$tfile"
}
run_test 103 "Test size correctness with lockahead"

get_stat_xtimes()
{
	local xtimes

	xtimes=$(stat -c "%X %Y %Z" $DIR/$tfile)

	echo ${xtimes[*]}
}

get_mdt_xtimes()
{
	local mdtdev=$1
	local output
	local xtimes

	output=$(do_facet mds1 "$DEBUGFS -c -R 'stat ROOT/$tfile' $mdtdev")
	((xtimes[0]=$(awk -F ':' /atime/'{ print $2 }' <<< "$output")))
	((xtimes[1]=$(awk -F ':' /mtime/'{ print $2 }' <<< "$output")))
	((xtimes[2]=$(awk -F ':' /ctime/'{ print $2 }' <<< "$output")))

	echo ${xtimes[*]}
}

check_mdt_xtimes()
{
	local mdtdev=$1
	local xtimes=($(get_stat_xtimes))
	local mdt_xtimes=($(get_mdt_xtimes $mdtdev))

	echo "STAT a|m|ctime ${xtimes[*]}"
	echo "MDT a|m|ctime ${mdt_xtimes[*]}"
	[[ ${xtimes[0]} == ${mdt_xtimes[0]} ]] ||
		error "$DIR/$tfile atime (${xtimes[0]}:${mdt_xtimes[0]}) diff"
	[[ ${xtimes[1]} == ${mdt_xtimes[1]} ]] ||
		error "$DIR/$tfile mtime (${xtimes[1]}:${mdt_xtimes[1]}) diff"
	[[ ${xtimes[2]} == ${mdt_xtimes[2]} ]] ||
		error "$DIR/$tfile ctime (${xtimes[2]}:${mdt_xtimes[2]}) diff"
}

test_104() {
	[ "$mds1_FSTYPE" == "ldiskfs" ] || skip_env "ldiskfs only test"
	[ $MDS1_VERSION -lt $(version_code 2.12.4) ] &&
		skip "Need MDS version at least 2.12.4"

	local pid
	local mdtdev=$(mdsdevname ${SINGLEMDS//mds/})
	local atime_diff=$(do_facet $SINGLEMDS \
		lctl get_param -n mdd.*MDT0000*.atime_diff)

	do_facet $SINGLEMDS \
		lctl set_param -n mdd.*MDT0000*.atime_diff=0

	stack_trap "do_facet $SINGLEMDS \
		lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff" EXIT

	dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
	check_mdt_xtimes $mdtdev
	sleep 2

	dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
	check_mdt_xtimes $mdtdev
	sleep 2
	$MULTIOP $DIR2/$tfile Oz8192w8192_c &
	pid=$!
	sleep 2
	dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
	sleep 2
	kill -USR1 $pid && wait $pid || error "multiop failure"
	check_mdt_xtimes $mdtdev

	local xtimes
	local mdt_xtimes

	# Verify mtime/ctime is NOT upated on MDS when there is no modification
	# on the client side
	xtimes=($(get_stat_xtimes))
	$MULTIOP $DIR/$tfile O_c &
	pid=$!
	sleep 2
	kill -USR1 $pid && wait $pid || error "multiop failure"
	mdt_xtimes=($(get_mdt_xtimes $mdtdev))
	[[ ${xtimes[1]} == ${mdt_xtimes[1]} ]] ||
		error "$DIR/$tfile mtime (${xtimes[1]}:${mdt_xtimes[1]}) diff"
	[[ ${xtimes[2]} == ${mdt_xtimes[2]} ]] ||
		error "$DIR/$tfile ctime (${xtimes[2]}:${mdt_xtimes[2]}) diff"
	check_mdt_xtimes $mdtdev

	sleep 2
	# Change ctime via chmod
	$MULTIOP $DIR/$tfile o_tc &
	pid=$!
	sleep 2
	kill -USR1 $pid && wait $pid || error "multiop failure"
	check_mdt_xtimes $mdtdev
}
run_test 104 "Verify that MDS stores atime/mtime/ctime during close"

test_105() {
	test_mkdir -p $DIR/$tdir
	echo test > $DIR/$tdir/$tfile
	$LCTL set_param fail_loc=0x416
	cancel_lru_locks osc & sleep 1
	fsize1=$(stat -c %s $DIR2/$tdir/$tfile)
	wait
	[[ $fsize1 = 5 ]] ||  error "Glimpse returned wrong file size $fsize1"
}
run_test 105 "Glimpse and lock cancel race"

test_106a() {
	[ "$mds1_FSTYPE" == "ldiskfs" ] && statx_supported ||
		skip_env "Test only for ldiskfs and statx() supported"

	local btime
	local mdt_btime
	local output
	local mdtdev=$(mdsdevname ${SINGLEMDS//mds/})

	dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
	btime=$($STATX -c %W $DIR/$tfile)
	output=$(do_facet mds1 "$DEBUGFS -c -R 'stat ROOT/$tfile' $mdtdev")
	echo $output
	((mdt_btime=$(awk -F ':' /crtime/'{ print $2 }' <<< "$output")))
	[[ $btime == $mdt_btime ]] ||
		error "$DIR/$tfile btime ($btime:$mdt_btime) diff"

}
run_test 106a "Verify the btime via statx()"

test_106b() {
	statx_supported || skip_env "statx() only test"

	local rpcs_before
	local rpcs_after

	$LFS setstripe -c 1 $DIR/$tfile || error "$DIR/$tfile setstripe failed"
	dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
	cancel_lru_locks $OSC
	rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	$STATX $DIR/$tfile
	rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	[ $rpcs_after -eq $((rpcs_before + 1)) ] ||
		error "$STATX should send 1 glimpse RPC to $OSC"

	cancel_lru_locks $OSC
	rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	# %n: FILENAME; %i: STATX_INO; %A STATX_MODE; %h STATX_NLINK;
	# %u: STATX_UID; %g: STATX_GID; %W STATX_BTIME; %X STATX_ATIME;
	# %Z: STATX_CTIME
	$STATX -c "%n %i %A %h %u %g %W %X %Z" $DIR/$tfile
	rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	[ $rpcs_after -eq $rpcs_before ] ||
		error "$STATX should not send glimpse RPCs to $OSC"

	cancel_lru_locks $OSC
	rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	$STATX --cached=always $DIR/$tfile
	rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	[ $rpcs_after -eq $rpcs_before ] ||
		error "$STATX should not send glimpse RPCs to $OSC"

	cancel_lru_locks $OSC
	rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	$STATX -c %Y $DIR/$tfile
	rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	[ $rpcs_after -eq $((rpcs_before + 1)) ] ||
		error "$STATX -c %Y should send 1 glimpse RPC to $OSC"

	cancel_lru_locks $OSC
	rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	$STATX -c %s $DIR/$tfile
	rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	[ $rpcs_after -eq $((rpcs_before + 1)) ] ||
		error "$STATX -c %s should send 1 glimpse RPC to $OSC"

	cancel_lru_locks $OSC
	rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	$STATX -c %b $DIR/$tfile
	rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
	[ $rpcs_after -eq $((rpcs_before + 1)) ] ||
		error "$STATX -c %b should send 1 glimpse RPC to $OSC"
}
run_test 106b "Glimpse RPCs test for statx"

test_106c() {
	statx_supported || skip_env "statx() only test"

	local mask

	touch $DIR/$tfile
	# Mask supported in stx_attributes by Lustre is
	# STATX_ATTR_IMMUTABLE(0x10) | STATX_ATTR_APPEND(0x20) : (0x30).
	mask=$($STATX -c %p $DIR/$tfile)
	(( (0x$mask & 0x30) == 0x30 )) ||
		error "supported stx_attributes: got '0x$mask', expected '0x30' at least"
	chattr +i $DIR/$tfile || error "chattr +i $DIR/$tfile failed"
	mask=$($STATX -c %r $DIR/$tfile)
	[[ $mask == "10" ]] ||
		error "got immutable flags '$mask', expected '10'"
	chattr -i $DIR/$tfile || error "chattr -i $DIR/$tfile failed"
	mask=$($STATX -c %r $DIR/$tfile)
	[[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
	chattr +a $DIR/$tfile || error "chattr +a $DIR/$tfile failed"
	mask=$($STATX -c %r $DIR/$tfile)
	[[ $mask == "20" ]] || error "got flags '$mask', expected '20'"
	chattr -a $DIR/$tfile || error "chattr -a $DIR/$tfile failed"
	mask=$($STATX -c %r $DIR/$tfile)
	[[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
	chattr +ia $DIR/$tfile || error "chattr +ia $DIR/$tfile failed"
	mask=$($STATX -c %r $DIR/$tfile)
	[[ $mask == "30" ]] || error "got flags '$mask', expected '30'"
	chattr -ia $DIR/$tfile || error "chattr -ia $DIR/$tfile failed"
	mask=$($STATX -c %r $DIR/$tfile)
	[[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
}
run_test 106c "Verify statx attributes mask"

test_107a() { # LU-1031
	dd if=/dev/zero of=$DIR1/$tfile bs=1M count=10
	local gid1=14091995
	local gid2=16022000

	$LFS getstripe $DIR1/$tfile

	multiop_bg_pause $DIR1/$tfile OG${gid1}_g${gid1}c || return 1
	local MULTIPID1=$!
	multiop_bg_pause $DIR2/$tfile O_G${gid2}r10g${gid2}c || return 2
	local MULTIPID2=$!
	kill -USR1 $MULTIPID2
	sleep 2
	if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
		error "First grouplock does not block second one"
	else
		echo "First grouplock blocks second one"
	fi
	kill -USR1 $MULTIPID1
	wait $MULTIPID1
	wait $MULTIPID2
}
run_test 107a "Basic grouplock conflict"

test_107b() {
	dd if=/dev/zero of=$DIR1/$tfile bs=1M count=10
	local gid1=14091995
	local gid2=16022000

	$LFS getstripe $DIR1/$tfile

	multiop_bg_pause $DIR1/$tfile OG${gid1}_g${gid1}c || return 1
	local MULTIPID1=$!
	multiop $DIR2/$tfile Or10c &
	local MULTIPID2=$!
	sleep 2

	if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
		error "Grouplock does not block IO"
	else
		echo "Grouplock blocks IO"
	fi

	multiop $DIR2/$tfile OG${gid2}_g${gid2}c &
	local MULTIPID3=$!
	sleep 2
	if [[ $(ps h -o comm -p $MULTIPID3) == "" ]]; then
		error "First grouplock does not block second one"
	else
		echo "First grouplock blocks second one"
	fi

	kill -USR1 $MULTIPID1
	sleep 2

	if [[ $(ps h -o comm -p $MULTIPID3) == "" ]]; then
		error "Second grouplock thread disappeared"
	fi

	if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
		error "Second grouplock does not block IO"
	else
		echo "Second grouplock blocks IO"
	fi

	kill -USR1 $MULTIPID3
	wait $MULTIPID1
	wait $MULTIPID2
	wait $MULTIPID3
}
run_test 107b "Grouplock is added to the head of waiting list"

test_108a() {
	local offset

	$LFS setstripe -E 1M -c 1 -E -1 $DIR1/$tfile ||
		error "Create $DIR1/$tfile failed"

	dd if=/dev/zero of=$DIR1/$tfile bs=10000 count=1 ||
		error "dd $DIR1/$tfile failed"
	offset=$(lseek_test -d 5000 $DIR2/$tfile)
	[[ $offset == 5000 ]] || error "offset $offset != 5000"

	$TRUNCATE $DIR1/$tfile 2000
	offset=$(lseek_test -l 1000 $DIR2/$tfile)
	[[ $offset == 2000 ]] || error "offset $offset != 2000"

	# The purpose of this test is to confirm for async IO (buffered writes)
	# the write does not need to go to disk for the file size to be updated,
	# but for hybrid/DIO the write does not complete until it goes to disk
	# We disable hybrid here so this definitely tests async/buffered IO
	local hybrid=$($LCTL get_param -n llite.*.hybrid_io | head -n1)
	$LCTL set_param llite.*.hybrid_io=0
	stack_trap "$LCTL set_param -n llite.*.hybrid_io=$hybrid" EXIT
	#define OBD_FAIL_OSC_DELAY_IO 0x414
	$LCTL set_param fail_val=4 fail_loc=0x80000414
	dd if=/dev/zero of=$DIR1/$tfile count=1 bs=8M conv=notrunc oflag=dsync &
	local pid=$!
	sleep 2

	offset=$(lseek_test -l 8000 $DIR2/$tfile)
	wait $pid
	[[ $offset == 8388608 ]] || error "offset $offset != 8388608"
}
run_test 108a "lseek: parallel updates"

# LU-14110
test_109() {
	local pid1 pid2
	local duration=${SANITYN_109_DURATION:-30}
	local i=0

	! local_mode ||
		skip "Clients need to be on different nodes than the servers"

	umount_client $MOUNT
	umount_client $MOUNT2

	[[ "$SLOW" == "no" ]] || ((duration*=10))
	echo "Starting race between client mounts (duration ${duration})"
	start=$SECONDS
	while (( $SECONDS - start < $duration )); do
		log "Iteration $((i++))"

#define CFS_FAIL_ONCE|OBD_FAIL_LLITE_RACE_MOUNT        0x80001417
		$LCTL set_param -n fail_loc=0x80001417

		mount_client $MOUNT  & pid1=$!
		mount_client $MOUNT2 & pid2=$!
		wait $pid1 || error "Mount $MOUNT fails with $?"
		wait $pid2 || error "Mount $MOUNT2 fails with $?"

		umount_client $MOUNT  & pid1=$!
		umount_client $MOUNT2 & pid2=$!
		wait $pid1 || error "Umount $MOUNT fails with $?"
		wait $pid2 || error "Umount $MOUNT2 fails with $?"

		$LUSTRE_RMMOD || error "Fail to remove lustre modules"
		load_modules
		echo
	done

	mount_client $MOUNT
	mount_client $MOUNT2
}

run_test 109 "Race with several mount instances on 1 node"

test_110() {
	local before=$(date +%s)
	local evict

	mkdir -p $DIR/$tdir
	touch $DIR/$tdir/f1
	touch $DIR/$tfile

	#define OBD_FAIL_PTLRPC_RESEND_RACE	 0x525
	do_facet mds1 lctl set_param fail_loc=0x525 fail_val=3

	# disable last_xid logic by dropping link reply
	ln $DIR/$tdir/f1 $DIR/$tdir/f2 &
	sleep 1

	#define OBD_FAIL_PTLRPC_ENQ_RESEND	0x534
	do_facet mds1 lctl set_param fail_loc=0x534

	# RPC will race with its Resend and the Resend will sleep to let
	# the original lock to get granted & cancelled.
	#
	# AST_SENT is set artificially, so an explicit conflict is not needed
	#
	# The woken up Resend gets a new lock, but client does not wait for it
	stat $DIR/$tfile
	sleep $TIMEOUT
	do_facet mds1 lctl set_param fail_loc=0 fail_val=0

	# Take a conflict to wait long enough to see the eviction
	touch $DIR2/$tfile

	# let the client reconnect
	client_reconnect
	evict=$(do_facet client $LCTL get_param mdc.$FSNAME-MDT*.state |
	  awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')

	[ -z "$evict" ] || [[ $evict -le $before ]] ||
		(do_facet client $LCTL get_param mdc.$FSNAME-MDT*.state;
		    error "eviction happened: $evict before:$before")
}
run_test 110 "do not grant another lock on resend"

test_111() {
	[ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
	[[ $(facet_active_host mds1) = $(facet_active_host mds2) ]] ||
		skip "MDT0 and MDT1 should be on the same node"

	mkdir $DIR1/$tdir
	$LFS mkdir -i 0 $DIR1/$tdir/mdt0dir
	$LFS mkdir -i 1 $DIR1/$tdir/mdt1dir

	mkdir $DIR1/$tdir/mdt0dir/foodir
	touch $DIR1/$tdir/mdt0dir/foodir/{file1,file2}

	$MULTIOP $DIR2/$tdir/mdt0dir/foodir/file2 Ow4096_c &
	MULTIOP_PID=$!
	ln $DIR1/$tdir/mdt0dir/foodir/file2 $DIR1/$tdir/mdt1dir/file2

	#define OBD_FAIL_MDS_LINK_RENAME_RACE   0x18a
	do_facet mds1 $LCTL set_param fail_loc=0x8000018a

	ln $DIR1/$tdir/mdt0dir/foodir/file2 $DIR1/$tdir/mdt1dir/file2x &
	sleep 1

	rm $DIR2/$tdir/mdt1dir/file2
	sleep 1

	mv $DIR2/$tdir/mdt0dir/foodir/file1 $DIR2/$tdir/mdt0dir/foodir/file2
	sleep 1

	kill $MULTIOP_PID
	wait
	rm -r $DIR1/$tdir || error "Removing test dir failed"
}
run_test 111 "A racy rename/link an open file should not cause fs corruption"

test_112() {
	(( MDSCOUNT >= 2 )) ||
		skip "We need at least 2 MDTs for this test"

	(( MDS1_VERSION >= $(version_code 2.14.54) )) ||
		skip "Need server version at least 2.14.54"

	local rr
	local count

	rr=$($LCTL get_param -n lmv.*.qos_threshold_rr | head -n1)
	rr=${rr%%%}
	stack_trap "$LCTL set_param lmv.*.qos_threshold_rr=$rr > /dev/null"

	mkdir -p $DIR1/$tdir/s1/s2 || error "mkdir s2 failed"
	$LFS mkdir -i 0 $DIR1/$tdir/s1/s2/s3 || error "mkdir s3 failed"
	$LFS setdirstripe -D -i -1 --max-inherit-rr=0 $DIR1/$tdir/s1/s2/s3 ||
		error "setdirstripe s3 failed"
	$LCTL set_param lmv.*.qos_threshold_rr=90
	mkdir $DIR2/$tdir/s1/s2/s3/d{1..64}
	count=$($LFS getstripe -m $DIR2/$tdir/s1/s2/s3/d* | grep ^0 | wc -l)
	(( count == 64 )) || error "only $count subdirs created on MDT0"

	$LFS setdirstripe -D -i -1 --max-inherit-rr=3 $DIR1/$tdir/s1/s2/s3 ||
		error "setdirstripe s3 failed"
	mkdir $DIR2/$tdir/s1/s2/s3/s{1..64}
	count=$($LFS getstripe -m $DIR2/$tdir/s1/s2/s3/s* | grep ^0 | wc -l)
	(( count == 64 / MDSCOUNT )) || error "$count subdirs created on MDT0"
}
run_test 112 "update max-inherit in default LMV"

test_113 () {
	(( MDS1_VERSION >= $(version_code 2.15.50) )) ||
		skip "Need server version at least 2.15.50"

	local instance
	local nid

	instance=$($LFS getname -i $DIR1) ||
		error "cannot get instance of $DIR1"

	$LFS check osts $DIR1 | grep $instance ||
		error "cannot find OSTs of instance $instance"

	$LFS check osts $DIR1 | grep -v $instance
	if (( $? == 0 )); then
		error "find OSTs other than instance $instance"
	fi

	$LFS check osts | grep $instance ||
		error "cannot find other OSTs"

	nid=$(df $DIR2 | tail -1 | sed 's%:/.*%%') ||
		error "cannot parse nid for $DIR2"

	$LFS check mgts $DIR2 | grep MGC$nid ||
		error "cannot find mgc of $nid"

	$LFS check mgts $DIR2 | grep -v MGC$nid
	if (( $? == 0 )); then
		error "find MGTs other than nid $nid"
	fi
}
run_test 113 "check servers of specified fs"

check_default_lmv() {
	local dir=$1

	local enabled
	local dmv
	local index
	local count
	local inherit
	local inherit_rr
	local raw

	enabled=$(do_facet mds1 \
		$LCTL get_param -n mdt.*-MDT0000*.enable_dmv_implicit_inherit)

	dmv=$($LFS getdirstripe -D $dir)
	echo $dir $dmv
	index=$(echo $dmv | awk '{ print $4 }')
	(( index == $2 )) || error "$dir default stripe index $index != $2"

	count=$(echo $dmv | awk '{ print $2 }')
	(( count == $3 )) || error "$dir default stripe count $count != $3"

	inherit=$(echo $dmv | awk '{ print $8 }')
	(( inherit == $4 )) || error "$dir default max-inherit $inherit != $4"

	if [ $index -eq -1 ]; then
		inherit_rr=$(echo $dmv | awk '{ print $10 }')
		(( inherit_rr == $5 )) ||
			error "$dir default max-inherit-rr $inherit_rr != $5"
	fi

	# with --raw, print default LMV stored in inode, otherwise print nothing
	raw=$($LFS getdirstripe -D --raw $dir)
	if (( enabled == 1 )); then
		[ -z $raw ] ||
			error "implicit inherited DMV is printed with --raw"
	else
		# if disabled, dmv is stored in inode, which will always
		# print max-inherit-rr
		echo $dir $raw
		[[ $raw =~ $dmv.* ]] || error "$dir raw $raw != dmv $dmv"
	fi
}

test_dmv_imp_inherit() {
	local dmv
	local raw
	local index
	local count
	local inherit
	local inherit_rr

	rm -rf $DIR/$tdir || error "rm $tdir failed"
	mkdir -p $DIR/$tdir || error "mkdir $tdir failed"

	# set dir default LMV
	$LFS setdirstripe -D -c1 -X4 --max-inherit-rr 2 $DIR/$tdir ||
		error "setdirstripe -D $tdir failed"
	dmv=$($LFS getdirstripe -D $DIR/$tdir)
	raw=$($LFS getdirstripe -D --raw $DIR/$tdir)
	[ "$dmv" == "$raw" ] || error "$dmv != $raw"

	mkdir -p $DIR/$tdir/l1/l2/l3 || error "mkdir $DIR/$tdir/l1/l2/l3 failed"
	check_default_lmv $DIR/$tdir/l1/l2/l3 -1 1 1 0
	check_default_lmv $DIR2/$tdir/l1/l2/l3 -1 1 1 0

	# below tests are valid only when this feature is enabled
	local enabled=$(do_facet mds1 \
		$LCTL get_param -n mdt.*-MDT0000*.enable_dmv_implicit_inherit)

	(( enabled == 1 )) || return 0

	# set l2 default LMV, dmv of l3 should change immediately
	$LFS setdirstripe -D -i1 -c2 -X4 $DIR/$tdir/l1/l2 ||
		error "setdirstripe -D $tdir/l1/l2 failed"

	check_default_lmv $DIR/$tdir/l1/l2/l3 1 2 3
	check_default_lmv $DIR2/$tdir/l1/l2/l3 1 2 3

	# change tdir default LMV, dmv of l3 should be unchanged because dmv
	# of l2 is explicitly set
	$LFS setdirstripe -D -i2 -c2 -X3 $DIR/$tdir ||
		error "setdirstripe -D $tdir failed"

	check_default_lmv $DIR/$tdir/l1 2 2 2
	check_default_lmv $DIR2/$tdir/l1 2 2 2
	check_default_lmv $DIR/$tdir/l1/l2/l3 1 2 3
	check_default_lmv $DIR2/$tdir/l1/l2/l3 1 2 3
}

test_114() {
	(( MDSCOUNT >= 2 )) ||
		skip "We need at least 2 MDTs for this test"

	(( MDS1_VERSION >= $(version_code 2.15.55.45) )) ||
		skip "Need server version at least 2.15.54.45"

	test_dmv_imp_inherit

	# disable dmv_imp_inherit to simulate old client
	local mdts=$(mdts_nodes)

	do_nodes $mdts \
		$LCTL set_param -n mdt.*MDT*.enable_dmv_implicit_inherit=0
	test_dmv_imp_inherit
	do_nodes $mdts \
		$LCTL set_param -n mdt.*MDT*.enable_dmv_implicit_inherit=1
}
run_test 114 "implicit default LMV inherit"

test_115() {
	local td=$DIR/$tdir

	[ "$mds1_FSTYPE" == "ldiskfs" ] || skip_env "ldiskfs only test"

	mkdir_on_mdt0 $td || error "can't mkdir"
	# turn it htree (don't really needed)
	createmany -m $td/f 3000 || error "can't createmany"

	# here is an example of debugfs output for htree command:
	# Entry #0: Hash 0x00000000, block 27
	# Reading directory block 27, phys 16760
	# 938 0x0016fb58-7f3d21f5 (32) f775   834 0x001db8c8-d31a4e0e (32) f671
	# 1085 0x0040cb70-4498abd4 (32) f922   1850 0x0066a1e6-f6f0dc69 (32) f1687
	# 2005 0x006c1a46-ef466058 (32) f1842   2025 0x007e64d4-8b28b734 (32) f1862
	# 642 0x008b53a0-77adc601 (32) f479   447 0x009ec152-af54eea3 (32) f284
	# 1740 0x00c38f56-ed310e61 (32) f1577   2165 0x00cdfd66-f429a93f (32) f2002
	# 930 0x00d7ada4-b80421c9 (32) f767   1946 0x00da6a7a-e8080600 (32) f1783
	# 273 0x00f8ea00-760bf97c (32) f110   1589 0x0103c4ee-94fad5dd (32) f1426
	# 1383 0x01193516-83120b48 (32) f1220   2379 0x01431e3c-e85b5bd9 (32) f2216
	#
	# find couple names in a same htree block of the same size
	mdt_dev=$(facet_device $SINGLEMDS)
	de=( $(do_facet $SINGLEMDS "debugfs -c -R 'htree /ROOT/$tdir' $mdt_dev" |
		awk '/Reading directory block/ { getline; print $4,$8; exit; }' ))
	local de1=${de[0]}
	local de2=${de[1]}
	[[ $de1 == "" || $de2 == "" ]] && error "de1=$de1 de2=$de2"
	echo "USE: $de1 $de2"
	# release one mkdir will lookup
	rm $DIR/$tdir/$de2
#define OBD_FAIL_MDS_PAUSE_CREATE_AFTER_LOOKUP	0x2401
	do_facet $SINGLEMDS $LCTL set_param fail_loc=0x80002401 fail_val=5
	mkdir $DIR/$tdir/$de2 &
	sleep 0.3
	local PID1=$!
	# recreate $de2
	mkdir $DIR2/$tdir/$de2
	# release space $de1 (should be enough to save $de2)
	rm $DIR2/$tdir/$de1
	# ready to create a dup of $de2
	wait $PID1
	local found=$(ls $DIR/$tdir/|grep "^$de2\$"|wc -l)
	(( $found == 1 )) || error "found $found"
}
run_test 115 "ldiskfs doesn't check direntry for uniqueness"

test_116() {
	(( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
	(( $MDS1_VERSION >= $(version_code v2_15_62-70-g668dfb53de) )) ||
		skip "Need MDS version at least 2.15.62.70 for intent mkdir"

	local mdt_idx
	local save="$TMP/$TESTSUITE-$TESTNAME.parameters"

	save_lustre_params client "llite.*.intent_mkdir" > $save
	stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
	$LCTL set_param llite.*.intent_mkdir=1

	$LFS mkdir -c$MDSCOUNT -i0 $DIR/$tdir ||
		error "$LFS mkdir $DIR/$tdir failed"
	echo "MD layout $DIR/$tdir:"
	$LFS getdirstripe $DIR/$tdir
	echo "mkdir $DIR/$tdir/tdir0"
	mkdir $DIR/$tdir/tdir0 || error "mkdir tdir0 failed"
	echo "setdirstripe -D -i1 $DIR2/$tdir/tdir0"
	$LFS setdirstripe -D -i1 $DIR2/$tdir/tdir0 ||
		error "$LFS setdirstripe $DIR2/$tdir/tdir0 failed"
	echo "mkdir $DIR/$tdir/tdir0/tdir11"
	mkdir $DIR/$tdir/tdir0/tdir11 || error "mkdir tdir0/tdir11 failed"
	$LFS getdirstripe $DIR/$tdir/tdir0
	$LFS getdirstripe $DIR/$tdir/tdir0/tdir11

	mdt_idx=$($LFS getstripe -m $DIR/$tdir/tdir0/tdir11)
	[ $mdt_idx == 1 ] ||
		error "$DIR/$tdir/tdir0/tdir11 on wrong MDT $mdt_idx"
}
run_test 116 "DNE: Set default LMV layout from a remote client"

test_121() {
	rm -f $DIR/$tfile
	$LFS setstripe -E 64K -E 128K -E -1 $DIR/$tfile ||
		error "setstripe $DIR/$tfile failed"

	yes | dd bs=20k count=1 of=$DIR/$tfile conv=notrunc ||
		error "1st dd failed"

#define OBD_FAIL_LLITE_TRUNC_PAUSE		    0x1436
	lctl set_param fail_loc=0x80001436
	truncate $DIR/$tfile 51200 &
	local PID=$!

	sleep 1

	yes | dd bs=20k count=1 of=$DIR2/$tfile conv=notrunc oflag=append ||
		error "2nd dd failed"

	wait $PID || error "trunc failed"

	local size=$(stat -c "%s" $DIR2/$tfile)
	hexdump $DIR/$tfile

	(( $size == 71680 )) || error "wrong size $size"

	return 0
}
run_test 121 "trunc append race"

test_200() {
	remote_ost_nodsh && skip "remote OST with nodsh" && return

	local at_max=10
	local delay=20
	local old_at=$(do_facet ost1 "$LCTL get_param -n at_max")
	local old_uf=$(do_facet ost1 "$LCTL get_param -n at_unhealthy_factor")
	local old_thrds=$(do_facet ost1 \
		"$LCTL get_param -n ost.OSS.ost_io.threads_max")
	local thrds=$(do_facet ost1 \
		"$LCTL get_param -n ost.OSS.ost_io.threads_started")

	if [[ -z "$old_uf" ]]; then
		skip "no at_unhealthy_factor, skipping"
	fi

	stack_trap "wait_osc_import_ready client ost1"
	do_facet ost1 "$LCTL set_param at_max=$at_max at_unhealthy_factor=3 \
		       ost.OSS.ost_io.threads_max=$thrds"
	stack_trap "do_facet ost1 $LCTL set_param at_max=$old_at \
					at_unhealthy_factor=$old_uf \
					ost.OSS.ost_io.threads_max=$old_thrds"

	test_mkdir $DIR/$tdir
	$LFS setstripe -i 0 -c 1 $DIR/$tdir
	# delay for long enough to exceed old UNHEALTHY check, but allow
	# threads to complete RPCs occasionally to keep service healthy
	#define OBD_FAIL_OFD_COMMITRW_DELAY 0x1e1
	do_facet ost1 "$LCTL set_param fail_loc=0x1e1 fail_val=$delay"

	for ((i=0; i < thrds * 2; i++)); do
		dd if=/dev/urandom of=$DIR/$tdir/$tfile.$i bs=4k count=1 \
			oflag=direct &
	done
	sync &
	sleep 5
	# enough time for 2 sets of writes to be blocked for $fail_val
	local end=$((SECONDS + delay))

	sleep $at_max
	while ((SECONDS < end)); do
		local health=$(do_facet ost1 $LCTL get_param -n health_check)

		if [[ "$health" != "healthy" ]]; then
			echo "OST health=$health"
			break
		fi
		sleep 3
	done
	(( SECONDS >= end )) || error "unhealthy state was seen"
	wait
}
run_test 200 "service remains healthy while able to process request"

log "cleanup: ======================================================"

# kill and wait in each test only guarentee script finish, but command in script
# like 'rm' 'chmod' may still be running, wait for all commands to finish
# otherwise umount below will fail
[ "$(mount | grep $MOUNT2)" ] && wait_update $HOSTNAME "fuser -m $MOUNT2" "" ||
	true

complete_test $SECONDS
rm -f $SAMPLE_FILE
check_and_cleanup_lustre
exit_status