1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or https://opensource.org/licenses/CDDL-1.0.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 2022 by Lawrence Livermore National Security, LLC.
25#
26
27. $STF_SUITE/include/libtest.shlib
28. $STF_SUITE/include/math.shlib
29. $STF_SUITE/tests/functional/zvol/zvol_common.shlib
30
31#
32# DESCRIPTION:
33#	Verify we can TRIM a zvol
34#
35# STRATEGY:
36# 1. TRIM the entire zvol to remove data from older tests
37# 2. Create a 5MB data file
38# 3. Write the file to the zvol
39# 4. Observe 5MB of used space on the zvol
40# 5. TRIM the first 1MB and last 2MB of the 5MB block of data.
41# 6. Observe 2MB of used space on the zvol
42# 7. Verify the trimmed regions are zero'd on the zvol
43
44verify_runnable "global"
45
46if is_linux ; then
47	if [[ $(linux_version) -gt $(linux_version "6.2") ]]; then
48		log_unsupported "Disabled while issue #14872 is being worked"
49	fi
50
51	# Disabled for the CentOS 9 kernel
52	if [[ $(linux_version) -eq $(linux_version "5.14") ]]; then
53		log_unsupported "Disabled while issue #14872 is being worked"
54	fi
55
56	# We need '--force' here since the prior tests may leave a filesystem
57	# on the zvol, and blkdiscard will see that filesystem and print a
58	# warning unless you force it.
59	#
60	# Only blkdiscard >= v2.36 supports --force, so we need to
61	# check for it.
62	if blkdiscard --help | grep -q '\-\-force' ; then
63		trimcmd='blkdiscard --force'
64	else
65		trimcmd='blkdiscard'
66	fi
67else
68	# By default, FreeBSD 'trim' always does a dry-run.  '-f' makes
69	# it perform the actual operation.
70	trimcmd='trim -f'
71fi
72
73if ! is_physical_device $DISKS; then
74	log_unsupported "This directory cannot be run on raw files."
75fi
76
77typeset datafile1="$(mktemp zvol_misc_flags1.XXXXXX)"
78typeset datafile2="$(mktemp zvol_misc_flags2.XXXXXX)"
79typeset zvolpath=${ZVOL_DEVDIR}/$TESTPOOL/$TESTVOL
80
81function cleanup
82{
83       rm "$datafile1" "$datafile2"
84}
85
86function do_test {
87	# Wait for udev to create symlinks to our zvol
88	block_device_wait $zvolpath
89
90	# Create a data file
91	log_must dd if=/dev/urandom of="$datafile1" bs=1M count=5
92
93	# Write to zvol
94	log_must dd if=$datafile1 of=$zvolpath conv=fsync
95	sync_pool
96
97	# Record how much space we've used (should be 5MB, with 128k
98	# of tolerance).
99	before="$(get_prop refer $TESTPOOL/$TESTVOL)"
100	log_must within_tolerance $before 5242880 131072
101
102	# We currently have 5MB of random data on the zvol.
103	# Trim the first 1MB and also trim 2MB at offset 3MB.
104	log_must $trimcmd -l $((1 * 1048576)) $zvolpath
105	log_must $trimcmd -o $((3 * 1048576)) -l $((2 * 1048576)) $zvolpath
106	sync_pool
107
108	# After trimming 3MB, the zvol should have 2MB of data (with 128k of
109	# tolerance).
110	after="$(get_prop refer $TESTPOOL/$TESTVOL)"
111	log_must within_tolerance $after 2097152 131072
112
113	# Make the same holes in our test data
114	log_must dd if=/dev/zero of="$datafile1" bs=1M count=1 conv=notrunc
115	log_must dd if=/dev/zero of="$datafile1" bs=1M count=2 seek=3 conv=notrunc
116
117	# Extract data from our zvol
118	log_must dd if=$zvolpath of="$datafile2" bs=1M count=5
119
120	# Compare the data we expect with what's on our zvol.  diff will return
121	# non-zero if they differ.
122	log_must diff $datafile1 $datafile2
123
124	log_must rm $datafile1 $datafile2
125}
126
127log_assert "Verify that a ZFS volume can be TRIMed"
128log_onexit cleanup
129
130log_must zfs set compression=off $TESTPOOL/$TESTVOL
131
132# Remove old data from previous tests
133log_must $trimcmd $zvolpath
134
135set_blk_mq 1
136log_must_busy zpool export $TESTPOOL
137log_must zpool import $TESTPOOL
138do_test
139
140set_blk_mq 0
141log_must_busy zpool export $TESTPOOL
142log_must zpool import $TESTPOOL
143do_test
144
145log_pass "ZFS volumes can be trimmed"
146