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
24default_setup_noexit "$DISKS"
25
26
27function cleanup
28{
29	log_must set_min_bytes 131072
30	default_cleanup_noexit
31}
32
33log_onexit cleanup
34
35log_must set_min_bytes 1
36
37log_must zfs set recordsize=512 $TESTPOOL/$TESTFS
38
39#
40# Create a large file so that we know some of the blocks will be on the
41# removed device, and hence eligible for remapping.
42#
43log_must dd if=/dev/urandom of=$TESTDIR/file bs=$((2**12)) count=$((2**9))
44
45#
46# Randomly rewrite some of blocks in the file so that there will be holes and
47# we will not be able to remap the entire file in a few huge chunks.
48#
49for i in $(seq $((2**12))); do
50	#
51	# We have to sync periodically so that all the writes don't end up in
52	# the same txg. If they were all in the same txg, only the last write
53	# would go through and we would not have as many allocations to
54	# fragment the file.
55	#
56	((i % 100 > 0 )) || sync || log_fail "Could not sync."
57        random_write $TESTDIR/file $((2**9)) || \
58            log_fail "Could not random write."
59done
60
61#
62# Remap should quietly succeed as a noop before a removal.
63#
64log_must zfs remap $TESTPOOL/$TESTFS
65remaptxg_before=$(zfs get -H -o value remaptxg $TESTPOOL/$TESTFS)
66(( $? == 0 )) || log_fail "Could not get remaptxg."
67[[ $remaptxg_before == "-" ]] || \
68    log_fail "remaptxg ($remaptxg_before) had value before a removal"
69
70log_must zpool remove $TESTPOOL $REMOVEDISK
71log_must wait_for_removal $TESTPOOL
72log_mustnot vdevs_in_pool $TESTPOOL $REMOVEDISK
73
74#
75# remaptxg should not be set if we haven't done a remap.
76#
77remaptxg_before=$(zfs get -H -o value remaptxg $TESTPOOL/$TESTFS)
78(( $? == 0 )) || log_fail "Could not get remaptxg."
79[[ $remaptxg_before == "-" ]] || \
80    log_fail "remaptxg ($remaptxg_before) had value before a removal"
81
82mapping_size_before=$(indirect_vdev_mapping_size $TESTPOOL)
83log_must zfs remap $TESTPOOL/$TESTFS
84
85# Try to wait for a condense to finish.
86for i in {1..5}; do
87	sleep 5
88	sync
89done
90mapping_size_after=$(indirect_vdev_mapping_size $TESTPOOL)
91
92#
93# After the remap, there should not be very many blocks referenced. The reason
94# why our threshold is as high as 512 is because our ratio of metadata to
95# user data is relatively high, with only 64M of user data on the file system.
96#
97(( mapping_size_after < mapping_size_before )) || \
98    log_fail "Mapping size did not decrease after remap: " \
99    "$mapping_size_before before to $mapping_size_after after."
100(( mapping_size_after < 512 )) || \
101    log_fail "Mapping size not small enough after remap: " \
102    "$mapping_size_before before to $mapping_size_after after."
103
104#
105# After a remap, the remaptxg should be set to a non-zero value.
106#
107remaptxg_after=$(zfs get -H -o value remaptxg $TESTPOOL/$TESTFS)
108(( $? == 0 )) || log_fail "Could not get remaptxg."
109log_note "remap txg after remap is $remaptxg_after"
110(( remaptxg_after > 0 )) || log_fail "remaptxg not increased"
111
112#
113# Remap should quietly succeed as a noop if there have been no removals since
114# the last remap.
115#
116log_must zfs remap $TESTPOOL/$TESTFS
117remaptxg_again=$(zfs get -H -o value remaptxg $TESTPOOL/$TESTFS)
118(( $? == 0 )) || log_fail "Could not get remaptxg."
119log_note "remap txg after second remap is $remaptxg_again"
120(( remaptxg_again == remaptxg_after )) || \
121    log_fail "remap not noop if there has been no removal"
122
123log_pass "Remapping a fs caused mapping size to decrease."
124