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# -w flag for 'zpool trim' waits for trimming to complete for all and only those
23# vdevs kicked off by that invocation.
24#
25# STRATEGY:
26# 1. Create a pool with 3 vdevs.
27# 2. Start trimming vdevs 1 and 2 with one invocation of 'zpool trim -w'
28# 3. Start trimming vdev 3 with a second invocation of 'zpool trim -w'
29# 4. Cancel the trim of vdev 1. Check that neither waiting process exits.
30# 5. Cancel the trim of vdev 3. Check that only the second waiting process
31#    exits.
32# 6. Cancel the trim of vdev 2. Check that the first waiting process exits.
33#
34
35function cleanup
36{
37	kill_if_running $trim12_pid
38	kill_if_running $trim3_pid
39	poolexists $TESTPOOL && destroy_pool $TESTPOOL
40	[[ -d "$TESTDIR" ]] && log_must rm -r "$TESTDIR"
41}
42
43if is_freebsd; then
44	log_unsupported "FreeBSD has no hole punching mechanism for the time being."
45fi
46
47typeset trim12_pid trim3_pid
48typeset -r VDEV1="$TESTDIR/file_vdev1"
49typeset -r VDEV2="$TESTDIR/file_vdev2"
50typeset -r VDEV3="$TESTDIR/file_vdev3"
51
52log_onexit cleanup
53
54log_must mkdir "$TESTDIR"
55log_must truncate -s 10G "$VDEV1" "$VDEV2" "$VDEV3"
56log_must zpool create -f $TESTPOOL "$VDEV1" "$VDEV2" "$VDEV3"
57
58log_bkgrnd zpool trim -r 1M -w $TESTPOOL "$VDEV1" "$VDEV2"
59trim12_pid=$!
60log_bkgrnd zpool trim -r 1M -w $TESTPOOL "$VDEV3"
61trim3_pid=$!
62
63# Make sure that we are really waiting
64log_must sleep 3
65proc_must_exist $trim12_pid
66proc_must_exist $trim3_pid
67
68#
69# Cancel trim of one of disks started by trim12, make sure neither
70# process exits
71#
72log_must zpool trim -c $TESTPOOL "$VDEV1"
73proc_must_exist $trim12_pid
74proc_must_exist $trim3_pid
75
76#
77# Cancel trim started by trim3, make sure that process exits, but
78# trim12 doesn't
79#
80log_must zpool trim -c $TESTPOOL "$VDEV3"
81proc_must_exist $trim12_pid
82bkgrnd_proc_succeeded $trim3_pid
83
84# Cancel last trim started by trim12, make sure it returns.
85log_must zpool trim -c $TESTPOOL "$VDEV2"
86bkgrnd_proc_succeeded $trim12_pid
87
88log_pass "'zpool trim -w' works."
89