--- old/usr/src/test/zfs-tests/include/libtest.shlib	Mon Aug  5 17:25:13 2019
+++ new/usr/src/test/zfs-tests/include/libtest.shlib	Mon Aug  5 17:25:13 2019
@@ -264,41 +264,6 @@
 	log_must zfs bookmark $fs_vol@$snap $fs_vol#$bkmark
 }
 
-#
-# Create a temporary clone result of an interrupted resumable 'zfs receive'
-# $1 Destination filesystem name. Must not exist, will be created as the result
-#    of this function along with its %recv temporary clone
-# $2 Source filesystem name. Must not exist, will be created and destroyed
-#
-function create_recv_clone
-{
-	typeset recvfs="$1"
-	typeset sendfs="${2:-$TESTPOOL/create_recv_clone}"
-	typeset snap="$sendfs@snap1"
-	typeset incr="$sendfs@snap2"
-	typeset mountpoint="$TESTDIR/create_recv_clone"
-	typeset sendfile="$TESTDIR/create_recv_clone.zsnap"
-
-	[[ -z $recvfs ]] && log_fail "Recv filesystem's name is undefined."
-
-	datasetexists $recvfs && log_fail "Recv filesystem must not exist."
-	datasetexists $sendfs && log_fail "Send filesystem must not exist."
-
-	log_must zfs create -o mountpoint="$mountpoint" $sendfs
-	log_must zfs snapshot $snap
-	log_must eval "zfs send $snap | zfs recv -u $recvfs"
-	log_must mkfile 1m "$mountpoint/data"
-	log_must zfs snapshot $incr
-	log_must eval "zfs send -i $snap $incr | dd bs=10k count=1 > $sendfile"
-	log_mustnot eval "zfs recv -su $recvfs < $sendfile"
-	log_must zfs destroy -r $sendfs
-	log_must rm -f "$sendfile"
-
-	if [[ $(get_prop 'inconsistent' "$recvfs/%recv") -ne 1 ]]; then
-		log_fail "Error creating temporary $recvfs/%recv clone"
-	fi
-}
-
 function default_mirror_setup
 {
 	default_mirror_setup_noexit $1 $2 $3
@@ -491,6 +456,8 @@
 
 	[[ -d $TESTDIR ]] && \
 		log_must rm -rf $TESTDIR
+	[[ -d $TESTDIR1 ]] && \
+		log_must rm -rf $TESTDIR1
 }
 
 
@@ -1184,11 +1151,13 @@
 # Note: In local zones, this function should return 0 silently.
 #
 # $1 - pool name
+# $2 - optional force flag
 # Destroy pool with the given parameters.
 
-function destroy_pool #pool
+function do_destroy_pool #pool <-f>
 {
 	typeset pool=${1%%/*}
+	typeset force=$2
 	typeset mtpt
 
 	if [[ -z $pool ]]; then
@@ -1204,14 +1173,14 @@
 			# destroy a pool to fail with EBUSY. We retry a few
 			# times allowing failures before requiring the destroy
 			# to succeed.
-			typeset -i wait_time=10 ret=1 count=0
+			typeset -i wait_time=$DESTROY_SLEEP_TIME ret=1 count=0
 			must=""
 			while [[ $ret -ne 0 ]]; do
-				$must zpool destroy -f $pool
+				$must zpool destroy $force $pool
 				ret=$?
 				[[ $ret -eq 0 ]] && break
 				log_note "zpool destroy failed with $ret"
-				[[ count++ -ge 7 ]] && must=log_must
+				[[ count++ -ge $NUM_RETRIES ]] && must=log_must
 				sleep $wait_time
 			done
 
@@ -1226,7 +1195,43 @@
 	return 0
 }
 
+# Return 0 if destroy successfully or the pool exists; $? otherwise
+# Note: In local zones, this function should return 0 silently.
 #
+# $1 - pool name
+# Destroy pool with the given parameters.
+
+function destroy_pool_no_force #pool
+{
+	typeset pool=${1%%/*}
+
+	do_destroy_pool $pool
+	if (( $? != 0 )); then
+		return 1
+	else
+		return 0
+	fi
+}
+
+# Return 0 if destroy successfully or the pool exists; $? otherwise
+# Note: In local zones, this function should return 0 silently.
+#
+# $1 - pool name
+# Force a destroy pool with the given parameters.
+
+function destroy_pool #pool
+{
+	typeset pool=${1%%/*}
+
+	do_destroy_pool $pool -f
+	if (( $? != 0 )); then
+		return 1
+	else
+		return 0
+	fi
+}
+
+#
 # Firstly, create a pool with 5 datasets. Then, create a single zone and
 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
 # and a zvol device to the zone.
@@ -1506,31 +1511,6 @@
 }
 
 #
-# Wait until a hotspare transitions to a given state or times out.
-#
-# Return 0 when  pool/disk matches expected state, 1 on timeout.
-#
-function wait_hotspare_state # pool disk state timeout
-{
-	typeset pool=$1
-	typeset disk=${2#$/DEV_DSKDIR/}
-	typeset state=$3
-	typeset timeout=${4:-60}
-	typeset -i i=0
-
-	while [[ $i -lt $timeout ]]; do
-		if check_hotspare_state $pool $disk $state; then
-			return 0
-		fi
-
-		i=$((i+1))
-		sleep 1
-	done
-
-	return 1
-}
-
-#
 # Verify a given slog disk is inuse or avail
 #
 # Return 0 is pool/disk matches expected state, 1 otherwise
@@ -1569,31 +1549,6 @@
 }
 
 #
-# Wait until a vdev transitions to a given state or times out.
-#
-# Return 0 when  pool/disk matches expected state, 1 on timeout.
-#
-function wait_vdev_state # pool disk state timeout
-{
-	typeset pool=$1
-	typeset disk=${2#$/DEV_DSKDIR/}
-	typeset state=$3
-	typeset timeout=${4:-60}
-	typeset -i i=0
-
-	while [[ $i -lt $timeout ]]; do
-		if check_vdev_state $pool $disk $state; then
-			return 0
-		fi
-
-		i=$((i+1))
-		sleep 1
-	done
-
-	return 1
-}
-
-#
 # Check the output of 'zpool status -v <pool>',
 # and to see if the content of <token> contain the <keyword> specified.
 #
@@ -1624,8 +1579,6 @@
 #	is_pool_scrubbed - to check if the pool is scrub completed
 #	is_pool_scrub_stopped - to check if the pool is scrub stopped
 #	is_pool_scrub_paused - to check if the pool has scrub paused
-#	is_pool_removing - to check if the pool is removing a vdev
-#	is_pool_removed - to check if the pool is remove completed
 #
 function is_pool_resilvering #pool <verbose>
 {
@@ -1663,18 +1616,6 @@
 	return $?
 }
 
-function is_pool_removing #pool
-{
-	check_pool_status "$1" "remove" "in progress since "
-	return $?
-}
-
-function is_pool_removed #pool
-{
-	check_pool_status "$1" "remove" "completed on"
-	return $?
-}
-
 #
 # Use create_pool()/destroy_pool() to clean up the infomation in
 # in the given disk to avoid slice overlapping.
@@ -2449,7 +2390,49 @@
 	return $?
 }
 
+# Return 0 if the pool is successfully epxorted; $? otherwise
 #
+# $1 - pool name
+# Export pool.
+function export_pool #pool
+{
+	typeset pool=${1%%/*}
+	# Checking to see if the device is busy. If so, we'll
+	# retry the export a few times with a sleep between tries.
+	errmsg='device is busy'
+	retry_num=$NUM_RETRIES
+	TMPFILE=`mktemp`
+	if [ -z "$TMPFILE" ] ; then
+		log_note "Unable to create temporary file $TMPFILE"
+		return 1
+	fi
+	until [ $retry_num == 0 ] ; do
+		log_note "zpool export $pool"
+		zpool export $pool 2>$TMPFILE
+		# If the export failed, see if it's due to a
+		# device is busy issue and retry if it is.
+		if (( $? != 0 )); then
+			# if this is busy then we want to retry
+			if [ "`grep "$errmsg" $TMPFILE`" != "" ]; then
+				retry_num-=1
+				log_note "Device is busy, retry zpool export"
+				sleep $EXPORT_SLEEP_TIME
+			else # return here if error is something else
+				rn $TMPFILE
+				return 1
+			fi
+		else
+			# export succeeded.
+			rm $TMPFILE
+			return 0
+		fi
+	done
+	# We've reached our max retries, try to export one more time
+	# and require it to succeed.
+	log_must zpool export $pool
+}
+
+#
 # Check if the pool contains the specified vdevs
 #
 # $1 pool
@@ -2511,8 +2494,8 @@
 #
 function random
 {
-        typeset max=$1
-        echo $(( ($RANDOM % $max) + 1 ))
+	typeset max=$1
+	echo $(( ($RANDOM % $max) + 1 ))
 }
 
 # Write data that can be compressed into a directory
@@ -2549,47 +2532,4 @@
 	[[ -e $pathname ]] || log_fail "No such file or directory: $pathname"
 	objnum=$(stat -c %i $pathname)
 	echo $objnum
-}
-
-#
-# Prints the current time in seconds since UNIX Epoch.
-#
-function current_epoch
-{
-	printf '%(%s)T'
-}
-
-#
-# Get decimal value of global uint32_t variable using mdb.
-#
-function mdb_get_uint32
-{
-	typeset variable=$1
-	typeset value
-
-	value=$(mdb -k -e "$variable/X | ::eval .=U")
-	if [[ $? -ne 0 ]]; then
-		log_fail "Failed to get value of '$variable' from mdb."
-		return 1
-	fi
-
-	echo $value
-	return 0
-}
-
-#
-# Set global uint32_t variable to a decimal value using mdb.
-#
-function mdb_set_uint32
-{
-	typeset variable=$1
-	typeset value=$2
-
-	mdb -kw -e "$variable/W 0t$value" > /dev/null
-	if [[ $? -ne 0 ]]; then
-		echo "Failed to set '$variable' to '$value' in mdb."
-		return 1
-	fi
-
-	return 0
 }
