1# vim: filetype=sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# $FreeBSD$
24
25#
26# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)zfs_destroy_common.kshlib	1.3	07/03/14 SMI"
30#
31
32. $STF_SUITE/include/libtest.kshlib
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 availible on globle zone
60	if ! datasetexists $VOL && is_global_zone; then
61		log_must $ZFS create -V $VOLSIZE $VOL
62		log_must $NEWFS /dev/zvol/$VOL
63
64		if [[ ! -d $TESTDIR1 ]]; then
65			log_must $MKDIR $TESTDIR1
66		fi
67		log_must $MOUNT /dev/zvol/$VOL $TESTDIR1
68	fi
69
70	if [[ $dtst == snap || $dtst == clone ]]; then
71		if ! datasetexists $FSSNAP; then
72			log_must $ZFS snapshot $FSSNAP
73		fi
74		if ! datasetexists $VOLSNAP && is_global_zone; then
75			log_must $ZFS snapshot $VOLSNAP
76		fi
77	fi
78
79	if [[ $dtst == clone ]]; then
80		if ! datasetexists $FSCLONE; then
81			log_must $ZFS clone $FSSNAP $FSCLONE
82		fi
83		if ! datasetexists $VOLCLONE && is_global_zone; then
84			log_must $ZFS clone $VOLSNAP $VOLCLONE
85		fi
86	fi
87}
88
89function make_dir_busy
90{
91	typeset dir=$1
92	typeset dirfiltered=$(echo $dir | sed -Ee 's,[/\.],_,g')
93
94	OLDPWD=$(pwd)
95	cd $dir
96	# Sleep for long enough for the test to have run through.  Note that
97	# even if the test itself changes directory, sleep will still be on it.
98	$SLEEP $STF_TIMEOUT &
99	eval SLEEP_PID_${dirfiltered}=$!
100	pid=$(eval echo \$SLEEP_PID_${dirfiltered})
101	cd ${OLDPWD}
102	log_note "Sleeping while on ${dir} in pid $pid"
103}
104
105function make_dir_unbusy
106{
107	typeset dir=$1
108	typeset dirfiltered=$(echo $dir | sed -Ee 's,[/\.],_,g')
109	typeset pid=$(eval echo \$SLEEP_PID_${dirfiltered})
110
111	# Safeguard in case this is used incorrectly.
112	[[ -z "$pid" ]] && log_fail "make_dir_unbusy called without busy?"
113	$KILL -15 $pid
114	eval SLEEP_PID_${dirfiltered}=""
115	log_note "Unbusied ${dir}"
116}
117
118# Clean up the testing environment
119#
120function cleanup_testenv
121{
122	if [[ $STF_EXITCODE -eq $STF_FAIL ]]; then
123		$ECHO "Testcase failed; dataset listing follows:"
124		$ZFS list -t all -r $TESTPOOL
125	fi
126	if (( ${#init_dir} != 0 )); then
127		cd $init_dir
128		init_dir=""
129	fi
130	if is_global_zone && ismounted "$TESTDIR1" "ufs" ; then
131		log_must $UMOUNT -f $TESTDIR1
132	fi
133	if [[ -d $TESTDIR1 ]]; then
134		log_must $RM -rf $TESTDIR1
135	fi
136
137	[[ -n "$SLEEP_PID" ]] && $KILL -15 $SLEEP_PID
138
139	if datasetexists $CTR; then
140		log_must $ZFS destroy -Rf $CTR
141	fi
142}
143
144#
145# Delete volume and related datasets from list, if the test cases was
146# runing in local zone. Then check them are existed or non-exists.
147#
148# $1   function name
149# $2-n datasets name
150#
151function check_dataset
152{
153	typeset funname=$1
154	typeset newlist=""
155	typeset dtst
156	shift
157
158	for dtst in "$@"; do
159		# Volume and related stuff are unvailable in local zone
160		if ! is_global_zone; then
161			if [[ $dtst == $VOL || $dtst == $VOLSNAP || \
162				$dtst == $VOLCLONE ]]
163			then
164				continue
165			fi
166		fi
167		newlist="$newlist $dtst"
168	done
169
170	if (( ${#newlist} != 0 )); then
171		log_must eval "$funname $newlist"
172	fi
173}
174