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/cli_root/zpool_initialize/zpool_initialize.kshlib
24. $STF_SUITE/tests/functional/cli_root/zpool_trim/zpool_trim.kshlib
25
26#
27# DESCRIPTION:
28# After trimming, the disk is actually trimmed.
29#
30# STRATEGY:
31# 1. Create a one-disk pool using a sparse file.
32# 2. Initialize the pool and verify the file vdev is no longer sparse.
33# 3. Trim the pool and verify the file vdev is again sparse.
34#
35
36function cleanup
37{
38	if poolexists $TESTPOOL; then
39		destroy_pool $TESTPOOL
40	fi
41
42        if [[ -d "$TESTDIR" ]]; then
43                rm -rf "$TESTDIR"
44        fi
45
46	log_must set_tunable64 TRIM_EXTENT_BYTES_MIN $trim_extent_bytes_min
47}
48log_onexit cleanup
49
50LARGESIZE=$((MINVDEVSIZE * 4))
51LARGEFILE="$TESTDIR/largefile"
52
53# Reduce trim size to allow for tighter tolerance below when checking.
54typeset trim_extent_bytes_min=$(get_tunable TRIM_EXTENT_BYTES_MIN)
55log_must set_tunable64 TRIM_EXTENT_BYTES_MIN 512
56
57log_must mkdir "$TESTDIR"
58log_must truncate -s $LARGESIZE "$LARGEFILE"
59log_must zpool create $TESTPOOL "$LARGEFILE"
60
61original_size=$(du -B1 "$LARGEFILE" | cut -f1)
62
63log_must zpool initialize $TESTPOOL
64
65while [[ "$(initialize_progress $TESTPOOL $LARGEFILE)" -lt "100" ]]; do
66        sleep 0.5
67done
68
69new_size=$(du -B1 "$LARGEFILE" | cut -f1)
70log_must within_tolerance $new_size $LARGESIZE $((128 * 1024 * 1024))
71
72log_must zpool trim $TESTPOOL
73
74while [[ "$(trim_progress $TESTPOOL $LARGEFILE)" -lt "100" ]]; do
75        sleep 0.5
76done
77
78new_size=$(du -B1 "$LARGEFILE" | cut -f1)
79log_must within_tolerance $new_size $original_size $((128 * 1024 * 1024))
80
81log_pass "Trimmed appropriate amount of disk space"
82