1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or https://opensource.org/licenses/CDDL-1.0.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2012, 2018 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/cli_root/zfs_destroy/zfs_destroy.cfg
33
34#
35# Create or recover a set of test environment which include ctr, vol, fs,
36# snap & clone. It looks like the following.
37#
38# pool
39#    |ctr
40#    |  |fs
41#    |  | |fssnap
42#    |  |vol
43#    |     |volsnap
44#    |fsclone
45#    |volclone
46#
47# $1 indicate which dependent dataset need be created. Such as 'snap', 'clone'.
48#
49function setup_testenv #[dtst]
50{
51	typeset dtst=$1
52
53	if ! datasetexists $CTR; then
54		log_must zfs create $CTR
55	fi
56	if ! datasetexists $FS; then
57		log_must zfs create $FS
58	fi
59	# Volume test is only available on global zone
60	if ! datasetexists $VOL && is_global_zone; then
61		log_must zfs create -V $VOLSIZE $VOL
62		block_device_wait
63
64		log_must new_fs $ZVOL_DEVDIR/$VOL
65
66		if [[ ! -d $TESTDIR1 ]]; then
67			log_must mkdir $TESTDIR1
68		fi
69		log_must mount $ZVOL_DEVDIR/$VOL $TESTDIR1
70	fi
71
72	if [[ $dtst == snap || $dtst == clone ]]; then
73		if ! datasetexists $FSSNAP; then
74			log_must zfs snapshot $FSSNAP
75		fi
76		if ! datasetexists $VOLSNAP && is_global_zone; then
77			log_must zfs snapshot $VOLSNAP
78		fi
79	fi
80
81	if [[ $dtst == clone ]]; then
82		if ! datasetexists $FSCLONE; then
83			log_must zfs clone $FSSNAP $FSCLONE
84		fi
85		if ! datasetexists $VOLCLONE && is_global_zone; then
86			log_must zfs clone $VOLSNAP $VOLCLONE
87			block_device_wait
88		fi
89	fi
90}
91
92# Clean up the testing environment
93#
94function cleanup_testenv
95{
96	if is_global_zone && ismounted "$TESTDIR1" "$NEWFS_DEFAULT_FS" ; then
97		log_must umount -f $TESTDIR1
98	fi
99	if [[ -d $TESTDIR1 ]]; then
100		log_must rm -rf $TESTDIR1
101	fi
102
103	pkill mkbusy
104
105	datasetexists $CTR && destroy_dataset $CTR -Rf
106}
107
108#
109# Delete volume and related datasets from list, if the test cases was
110# running in local zone. Then check them are existed or non-exists.
111#
112# $1   function name
113# $2-n datasets name
114#
115function check_dataset
116{
117	typeset funname=$1
118	typeset newlist=""
119	typeset dtst
120	shift
121
122	for dtst in "$@"; do
123		# Volume and related stuff are unavailable in local zone
124		if ! is_global_zone; then
125			if [[ $dtst == $VOL || $dtst == $VOLSNAP || \
126				$dtst == $VOLCLONE ]]
127			then
128				continue
129			fi
130		fi
131		newlist="$newlist $dtst"
132	done
133
134	if (( ${#newlist} != 0 )); then
135		# Run each item in $newlist individually so on failure, the
136		# problematic dataset is listed in the logs.
137		for i in $newlist; do
138			log_must $funname $i
139		done
140	fi
141}
142
143# Use zdb to see if a livelist exists for a given clone
144# $1   clone name
145function check_livelist_exists
146{
147	zdb -vvvvv $TESTPOOL/$1 | grep "Livelist" || \
148		log_fail "zdb could not find Livelist"
149}
150
151# Check that a livelist has been removed, waiting for deferred destroy entries
152# to be cleared from zdb.
153function check_livelist_gone
154{
155	log_must zpool wait -t free $TESTPOOL
156	sync_all_pools
157	zdb -vvvvv $TESTPOOL | grep "Livelist" && \
158		log_fail "zdb found Livelist after the clone is deleted."
159}
160
161# Create a clone in the testpool based on $TESTFS@snap. Verify that the clone
162# was created and that it includes a livelist
163# $1    fs name
164# $2    snap name
165# $3    clone name
166function clone_dataset
167{
168	log_must zfs clone $TESTPOOL/$1@$2 $TESTPOOL/$3
169	datasetexists $TESTPOOL/$3 || \
170		log_fail "zfs clone $TESTPOOL/$3 fail."
171	check_livelist_exists $3
172}
173