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