1eda14cbcSMatt Macy#!/bin/ksh -p
2eda14cbcSMatt Macy#
3eda14cbcSMatt Macy# CDDL HEADER START
4eda14cbcSMatt Macy#
5eda14cbcSMatt Macy# This file and its contents are supplied under the terms of the
6eda14cbcSMatt Macy# Common Development and Distribution License ("CDDL"), version 1.0.
7eda14cbcSMatt Macy# You may only use this file in accordance with the terms of version
8eda14cbcSMatt Macy# 1.0 of the CDDL.
9eda14cbcSMatt Macy#
10eda14cbcSMatt Macy# A full copy of the text of the CDDL should have accompanied this
11eda14cbcSMatt Macy# source.  A copy of the CDDL is also available via the Internet at
12eda14cbcSMatt Macy# http://www.illumos.org/license/CDDL.
13eda14cbcSMatt Macy#
14eda14cbcSMatt Macy# CDDL HEADER END
15eda14cbcSMatt Macy#
16eda14cbcSMatt Macy
17eda14cbcSMatt Macy#
18eda14cbcSMatt Macy# Copyright (c) 2019 by Tim Chase. All rights reserved.
19eda14cbcSMatt Macy# Copyright (c) 2019 Lawrence Livermore National Security, LLC.
20eda14cbcSMatt Macy#
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy. $STF_SUITE/include/libtest.shlib
23eda14cbcSMatt Macy. $STF_SUITE/tests/functional/trim/trim.kshlib
24eda14cbcSMatt Macy. $STF_SUITE/tests/functional/trim/trim.cfg
25eda14cbcSMatt Macy
26eda14cbcSMatt Macy#
27eda14cbcSMatt Macy# DESCRIPTION:
28eda14cbcSMatt Macy#	Verify automatic trim and manual trim coexist correctly.
29eda14cbcSMatt Macy#
30eda14cbcSMatt Macy# STRATEGY:
31eda14cbcSMatt Macy#	1. Create a pool on sparse file vdevs to trim.
32eda14cbcSMatt Macy#	2. Set autotrim=on to enable asynchronous pool trimming.
33eda14cbcSMatt Macy#	3. Generate some interesting pool data which can be trimmed.
34eda14cbcSMatt Macy#	4. While generating data issue manual trims.
35eda14cbcSMatt Macy#	4. Verify trim IOs of the expected type were issued for the pool.
36eda14cbcSMatt Macy#	5. Verify data integrity of the pool after trim.
37eda14cbcSMatt Macy#	6. Repeat test for striped, mirrored, and RAIDZ pools.
38eda14cbcSMatt Macy
39eda14cbcSMatt Macyverify_runnable "global"
40eda14cbcSMatt Macy
41eda14cbcSMatt Macylog_assert "Set 'autotrim=on', run 'zpool trim' and verify pool data integrity"
42eda14cbcSMatt Macy
43eda14cbcSMatt Macyfunction cleanup
44eda14cbcSMatt Macy{
45eda14cbcSMatt Macy	if poolexists $TESTPOOL; then
46eda14cbcSMatt Macy		destroy_pool $TESTPOOL
47eda14cbcSMatt Macy	fi
48eda14cbcSMatt Macy
49eda14cbcSMatt Macy	log_must rm -f $TRIM_VDEVS
50eda14cbcSMatt Macy
51eda14cbcSMatt Macy	log_must set_tunable64 TRIM_EXTENT_BYTES_MIN $trim_extent_bytes_min
52eda14cbcSMatt Macy	log_must set_tunable64 TRIM_TXG_BATCH $trim_txg_batch
53eda14cbcSMatt Macy}
54eda14cbcSMatt Macylog_onexit cleanup
55eda14cbcSMatt Macy
56eda14cbcSMatt Macy# Minimum trim size is decreased to verify all trim sizes.
57eda14cbcSMatt Macytypeset trim_extent_bytes_min=$(get_tunable TRIM_EXTENT_BYTES_MIN)
58f7a5903dSMartin Matuskalog_must set_tunable64 TRIM_EXTENT_BYTES_MIN 512
59eda14cbcSMatt Macy
60eda14cbcSMatt Macy# Reduced TRIM_TXG_BATCH to make trimming more frequent.
61eda14cbcSMatt Macytypeset trim_txg_batch=$(get_tunable TRIM_TXG_BATCH)
62eda14cbcSMatt Macylog_must set_tunable64 TRIM_TXG_BATCH 8
63eda14cbcSMatt Macy
647877fdebSMatt Macyfor type in "" "mirror" "raidz" "raidz2" "draid" "draid2"; do
65eda14cbcSMatt Macy	log_must truncate -s 1G $TRIM_VDEVS
66eda14cbcSMatt Macy
67eda14cbcSMatt Macy	log_must zpool create -f $TESTPOOL $type $TRIM_VDEVS
68eda14cbcSMatt Macy	log_must zpool set autotrim=on $TESTPOOL
69eda14cbcSMatt Macy
70eda14cbcSMatt Macy	# Add and remove data from the pool in a random fashion in order
71eda14cbcSMatt Macy	# to generate a variety of interesting ranges to be auto trimmed.
72eda14cbcSMatt Macy	for n in {0..10}; do
73eda14cbcSMatt Macy		dir="/$TESTPOOL/autotrim-$((RANDOM % 5))"
74eda14cbcSMatt Macy		filesize=$((4096 + ((RANDOM * 691) % 131072) ))
75eda14cbcSMatt Macy		log_must rm -rf $dir
76eda14cbcSMatt Macy		log_must fill_fs $dir 10 10 $filesize 1 R
77e92ffd9bSMartin Matuska		sync_all_pools
78eda14cbcSMatt Macy
79eda14cbcSMatt Macy		if [[ $((n % 4)) -eq 0 ]]; then
80eda14cbcSMatt Macy			log_must timeout 120 zpool trim -w $TESTPOOL
81eda14cbcSMatt Macy		fi
82eda14cbcSMatt Macy	done
83eda14cbcSMatt Macy	log_must du -hs /$TESTPOOL
84eda14cbcSMatt Macy
85eda14cbcSMatt Macy	verify_trim_io $TESTPOOL "ind" 10
86eda14cbcSMatt Macy	verify_pool $TESTPOOL
87eda14cbcSMatt Macy
88eda14cbcSMatt Macy	log_must zpool destroy $TESTPOOL
89eda14cbcSMatt Macy	log_must rm -f $TRIM_VDEVS
90eda14cbcSMatt Macydone
91eda14cbcSMatt Macy
92eda14cbcSMatt Macylog_pass "Automatic trim and manual trim coexistence successfully validated"
93