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) 2019 by Tim Chase. All rights reserved.
19# Copyright (c) 2019 Lawrence Livermore National Security, LLC.
20#
21
22. $STF_SUITE/include/libtest.shlib
23. $STF_SUITE/tests/functional/trim/trim.kshlib
24. $STF_SUITE/tests/functional/trim/trim.cfg
25
26#
27# DESCRIPTION:
28# 	Check various pool geometries stripe, mirror, raidz
29#
30# STRATEGY:
31#	1. Create a pool on file vdevs to trim.
32#	2. Fill the pool to a known percentage of capacity.
33#	3. Verify the vdevs contain 75% or more allocated blocks.
34#	4. Remove all files making it possible to trim the entire pool.
35#	5. Manually trim the pool.
36#	6. Wait for trim to issue trim IOs for the free blocks.
37#	7. Verify the disks contain 30% or less allocated blocks.
38#	8. Repeat for test for striped, mirrored, and RAIDZ pools.
39
40verify_runnable "global"
41
42log_assert "Run 'zpool trim' verify pool disks were trimmed"
43
44function cleanup
45{
46	if poolexists $TESTPOOL; then
47		destroy_pool $TESTPOOL
48	fi
49
50	log_must rm -f $TRIM_VDEVS
51
52	log_must set_tunable64 TRIM_EXTENT_BYTES_MIN $trim_extent_bytes_min
53	log_must set_tunable64 TRIM_TXG_BATCH $trim_txg_batch
54	log_must set_tunable64 VDEV_MIN_MS_COUNT $vdev_min_ms_count
55}
56log_onexit cleanup
57
58# Minimum trim size is decreased to verify all trim sizes.
59typeset trim_extent_bytes_min=$(get_tunable TRIM_EXTENT_BYTES_MIN)
60log_must set_tunable64 TRIM_EXTENT_BYTES_MIN 512
61
62# Reduced TRIM_TXG_BATCH to make trimming more frequent.
63typeset trim_txg_batch=$(get_tunable TRIM_TXG_BATCH)
64log_must set_tunable64 TRIM_TXG_BATCH 8
65
66# Increased metaslabs to better simulate larger more realistic devices.
67typeset vdev_min_ms_count=$(get_tunable VDEV_MIN_MS_COUNT)
68log_must set_tunable64 VDEV_MIN_MS_COUNT 32
69
70typeset VDEV_MAX_MB=$(( floor(4 * MINVDEVSIZE * 0.75 / 1024 / 1024) ))
71typeset VDEV_MIN_MB=$(( floor(4 * MINVDEVSIZE * 0.30 / 1024 / 1024) ))
72
73for type in "" "mirror" "raidz2" "draid"; do
74
75	if [[ "$type" = "" ]]; then
76		VDEVS="$TRIM_VDEV1"
77	elif [[ "$type" = "mirror" ]]; then
78		VDEVS="$TRIM_VDEV1 $TRIM_VDEV2"
79	elif [[ "$type" = "raidz2" ]]; then
80		VDEVS="$TRIM_VDEV1 $TRIM_VDEV2 $TRIM_VDEV3"
81	elif [[ "$type" = "draid" ]]; then
82		VDEVS="$TRIM_VDEV1 $TRIM_VDEV2 $TRIM_VDEV3 $TRIM_VDEV4"
83
84		# The per-vdev utilization is lower due to the capacity
85		# resilverd for the distributed spare.
86		VDEV_MAX_MB=$(( floor(4 * MINVDEVSIZE * 0.50 / 1024 / 1024) ))
87	fi
88
89	log_must truncate -s $((4 * MINVDEVSIZE)) $VDEVS
90	log_must zpool create -f $TESTPOOL $type $VDEVS
91
92	typeset availspace=$(get_prop available $TESTPOOL)
93	typeset fill_mb=$(( floor(availspace * 0.90 / 1024 / 1024) ))
94
95	# Fill the pool, verify the vdevs are no longer sparse.
96	file_write -o create -f /$TESTPOOL/file -b 1048576 -c $fill_mb -d R
97	sync_pool $TESTPOOL
98	verify_vdevs "-ge" "$VDEV_MAX_MB" $VDEVS
99
100	# Remove the file, issue trim, verify the vdevs are now sparse.
101	log_must rm /$TESTPOOL/file
102	log_must timeout 120 zpool trim -w $TESTPOOL
103	verify_vdevs "-le" "$VDEV_MIN_MB" $VDEVS
104
105	log_must zpool destroy $TESTPOOL
106	log_must rm -f $VDEVS
107done
108
109log_pass "Manual trim successfully shrunk vdevs"
110