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 # We need '--force' here since the prior tests may leave a filesystem 48 # on the zvol, and blkdiscard will see that filesystem and print a 49 # warning unless you force it. 50 # 51 # Only blkdiscard >= v2.36 supports --force, so we need to 52 # check for it. 53 if blkdiscard --help | grep -q '\-\-force' ; then 54 trimcmd='blkdiscard --force' 55 else 56 trimcmd='blkdiscard' 57 fi 58else 59 # By default, FreeBSD 'trim' always does a dry-run. '-f' makes 60 # it perform the actual operation. 61 trimcmd='trim -f' 62fi 63 64if ! is_physical_device $DISKS; then 65 log_unsupported "This directory cannot be run on raw files." 66fi 67 68typeset datafile1="$(mktemp zvol_misc_flags1.XXXXXX)" 69typeset datafile2="$(mktemp zvol_misc_flags2.XXXXXX)" 70typeset zvolpath=${ZVOL_DEVDIR}/$TESTPOOL/$TESTVOL 71 72function cleanup 73{ 74 rm "$datafile1" "$datafile2" 75} 76 77function do_test { 78 # Wait for udev to create symlinks to our zvol 79 block_device_wait $zvolpath 80 81 # Create a data file 82 log_must dd if=/dev/urandom of="$datafile1" bs=1M count=5 83 84 # Write to zvol 85 log_must dd if=$datafile1 of=$zvolpath conv=fsync 86 sync_pool 87 88 # Record how much space we've used (should be 5MB, with 128k 89 # of tolerance). 90 before="$(get_prop refer $TESTPOOL/$TESTVOL)" 91 log_must within_tolerance $before 5242880 131072 92 93 # We currently have 5MB of random data on the zvol. 94 # Trim the first 1MB and also trim 2MB at offset 3MB. 95 log_must $trimcmd -l $((1 * 1048576)) $zvolpath 96 log_must $trimcmd -o $((3 * 1048576)) -l $((2 * 1048576)) $zvolpath 97 sync_pool 98 99 # After trimming 3MB, the zvol should have 2MB of data (with 128k of 100 # tolerance). 101 after="$(get_prop refer $TESTPOOL/$TESTVOL)" 102 log_must within_tolerance $after 2097152 131072 103 104 # Make the same holes in our test data 105 log_must dd if=/dev/zero of="$datafile1" bs=1M count=1 conv=notrunc 106 log_must dd if=/dev/zero of="$datafile1" bs=1M count=2 seek=3 conv=notrunc 107 108 # Extract data from our zvol 109 log_must dd if=$zvolpath of="$datafile2" bs=1M count=5 110 111 # Compare the data we expect with what's on our zvol. diff will return 112 # non-zero if they differ. 113 log_must diff $datafile1 $datafile2 114 115 log_must rm $datafile1 $datafile2 116} 117 118log_assert "Verify that a ZFS volume can be TRIMed" 119log_onexit cleanup 120 121log_must zfs set compression=off $TESTPOOL/$TESTVOL 122 123# Remove old data from previous tests 124log_must $trimcmd $zvolpath 125 126set_blk_mq 1 127log_must_busy zpool export $TESTPOOL 128log_must zpool import $TESTPOOL 129do_test 130 131set_blk_mq 0 132log_must_busy zpool export $TESTPOOL 133log_must zpool import $TESTPOOL 134do_test 135 136log_pass "ZFS volumes can be trimmed" 137