1#!/bin/ksh -p
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright (c) 2020 by Delphix. All rights reserved.
15#
16
17. $STF_SUITE/include/libtest.shlib
18. $STF_SUITE/tests/functional/cli_root/zpool_wait/zpool_wait.kshlib
19
20#
21# DESCRIPTION:
22# 'zpool wait' works when a trim operation is canceled.
23#
24# STRATEGY:
25# 1. Create a pool.
26# 2. Start trimming the vdev in the pool, setting the rate low enough that the
27#    operation won't complete before the test finishes.
28# 3. Start 'zpool wait'.
29# 4. Wait a few seconds and then check that the wait process is actually
30#    waiting.
31# 5. Cancel the trim.
32# 6. Check that the wait process returns reasonably promptly.
33# 7. Repeat 3-7, except pause the trim instead of canceling it.
34#
35
36function cleanup
37{
38	kill_if_running $pid
39	poolexists $TESTPOOL && destroy_pool $TESTPOOL
40	[[ -d "$TESTDIR" ]] && log_must rm -r "$TESTDIR"
41}
42
43function do_test
44{
45	typeset stop_cmd=$1
46
47	log_must zpool trim -r 1M $TESTPOOL "$FILE_VDEV"
48
49	log_bkgrnd zpool wait -t trim $TESTPOOL
50	pid=$!
51
52	# Make sure that we are really waiting
53	log_must sleep 3
54	proc_must_exist $pid
55
56	# Stop trimming and make sure process returns
57	log_must eval "$stop_cmd"
58	bkgrnd_proc_succeeded $pid
59}
60
61if is_freebsd; then
62	log_unsupported "FreeBSD has no hole punching mechanism for the time being."
63fi
64
65typeset pid
66typeset -r FILE_VDEV="$TESTDIR/file_vdev1"
67
68log_onexit cleanup
69
70log_must mkdir "$TESTDIR"
71log_must truncate -s 10G "$FILE_VDEV"
72log_must zpool create -f $TESTPOOL "$FILE_VDEV"
73
74do_test "zpool trim -c $TESTPOOL $FILE_VDEV"
75do_test "zpool trim -s $TESTPOOL $FILE_VDEV"
76
77log_pass "'zpool wait' works when trim is stopped before completion."
78