1#! /bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2015, 2016 by Delphix. All rights reserved.
19#
20
21. $STF_SUITE/include/libtest.shlib
22. $STF_SUITE/tests/functional/removal/removal.kshlib
23
24function set_condense_delay # ticks
25{
26	typeset ticks=$1
27	echo "zfs_condense_indirect_commit_entry_delay_ticks/W 0t$ticks" | \
28	    mdb -kw
29}
30
31function reset
32{
33	log_must set_condense_delay 0
34	log_must set_min_bytes 131072
35	default_cleanup_noexit
36}
37
38default_setup_noexit "$DISKS" "true"
39log_onexit reset
40log_must set_condense_delay 500
41log_must set_min_bytes 1
42
43log_must zfs set recordsize=512 $TESTPOOL/$TESTFS
44
45#
46# Create a large file so that we know some of the blocks will be on the
47# removed device, and hence eligible for remapping.
48#
49log_must dd if=/dev/urandom of=$TESTDIR/file bs=1024k count=10
50
51#
52# Create a file in the other filesystem, which will not be remapped.
53#
54log_must dd if=/dev/urandom of=$TESTDIR1/file bs=1024k count=10
55
56#
57# Randomly rewrite some of blocks in the file so that there will be holes and
58# we will not be able to remap the entire file in a few huge chunks.
59#
60for i in {1..4096}; do
61	#
62	# We have to sync periodically so that all the writes don't end up in
63	# the same txg. If they were all in the same txg, only the last write
64	# would go through and we would not have as many allocations to
65	# fragment the file.
66	#
67	((i % 100 > 0 )) || sync || log_fail "Could not sync."
68        random_write $TESTDIR/file 512 || \
69            log_fail "Could not random write."
70done
71
72log_must zpool remove $TESTPOOL $REMOVEDISK
73log_must wait_for_removal $TESTPOOL
74log_mustnot vdevs_in_pool $TESTPOOL $REMOVEDISK
75
76log_must zfs remap $TESTPOOL/$TESTFS
77sync
78sleep 4
79sync
80log_must zpool export $TESTPOOL
81zdb -e $TESTPOOL | grep 'Condensing indirect vdev' || \
82    log_fail "Did not export during a condense."
83log_must zdb -e -cudi $TESTPOOL
84log_must zpool import $TESTPOOL
85
86log_pass "Pool can be exported in the middle of a condense."
87