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