1#!/bin/ksh -p
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 https://opensource.org/licenses/CDDL-1.0.
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#
24# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zfs_destroy/zfs_destroy.cfg
34
35#
36# DESCRIPTION:
37#	Verify 'zfs destroy [-rR]' succeeds as root.
38#
39# STRATEGY:
40#	1. Create two datasets in the storage pool
41#	2. Create fs,vol,ctr,snapshot and clones of snapshot in the two datasets
42#	3. Create clone in the second dataset for the snapshot in the first dataset
43#	4. Verify 'zfs destroy -r' fails to destroy dataset with clone outside it
44#	5. Verify 'zfs destroy -R' succeeds to destroy dataset with clone outside it
45#	6. Verify 'zfs destroy -r' succeeds to destroy dataset without clone outside it.
46#
47
48verify_runnable "both"
49
50function cleanup
51{
52	for obj in $ctr2 $ctr1 $ctr; do
53		datasetexists $obj && destroy_dataset $obj -Rf
54	done
55
56	for mntp in $TESTDIR1 $TESTDIR2; do
57		[[ -d $mntp ]] && \
58			log_must rm -rf $mntp
59	done
60}
61
62log_assert "Verify that 'zfs destroy [-rR]' succeeds as root. "
63
64log_onexit cleanup
65
66#
67# Preparations for testing
68#
69for dir in $TESTDIR1 $TESTDIR2; do
70	[[ ! -d $dir ]] && \
71		log_must mkdir -p $dir
72done
73
74ctr=$TESTPOOL/$TESTCTR
75ctr1=$TESTPOOL/$TESTCTR1
76ctr2=$ctr/$TESTCTR2
77ctr3=$ctr1/$TESTCTR2
78child_fs=$ctr/$TESTFS1
79child_fs1=$ctr1/$TESTFS2
80child_fs_mntp=$TESTDIR1
81child_fs1_mntp=$TESTDIR2
82child_vol=$ctr/$TESTVOL
83child_vol1=$ctr1/$TESTVOL
84child_fs_snap=$child_fs@snap
85child_fs1_snap=$child_fs1@snap
86child_fs_snap_clone=$ctr/$TESTCLONE
87child_fs_snap_clone1=$ctr1/${TESTCLONE}_across_ctr
88child_fs_snap_clone2=$ctr2/$TESTCLONE2
89child_fs1_snap_clone=$ctr1/$TESTCLONE1
90child_fs1_snap_clone1=$ctr/${TESTCLONE1}_across_ctr
91
92#
93# Create two datasets in the storage pool
94#
95log_must zfs create $ctr
96log_must zfs create $ctr1
97
98#
99# Create children datasets fs,vol,snapshot in the datasets, and
100# clones across two datasets
101#
102log_must zfs create $ctr2
103
104for fs in $child_fs $child_fs1; do
105	log_must zfs create $fs
106done
107
108log_must zfs set mountpoint=$child_fs_mntp $child_fs
109log_must zfs set mountpoint=$child_fs1_mntp $child_fs1
110
111for snap in $child_fs_snap $child_fs1_snap; do
112	log_must zfs snapshot $snap
113done
114
115if is_global_zone ; then
116	for vol in $child_vol $child_vol1; do
117		log_must zfs create -V $VOLSIZE $vol
118	done
119fi
120
121for clone in $child_fs_snap_clone $child_fs_snap_clone1; do
122	log_must zfs clone $child_fs_snap $clone
123done
124
125
126for clone in $child_fs1_snap_clone $child_fs1_snap_clone1; do
127	log_must zfs clone $child_fs1_snap $clone
128done
129
130log_note "Verify that 'zfs destroy -r' fails to destroy dataset " \
131	"with dependent clone outside it."
132
133for obj in $child_fs $child_fs1 $ctr $ctr1; do
134	log_mustnot zfs destroy -r $obj
135	datasetexists $obj || \
136		log_fail "'zfs destroy -r' fails to keep dependent " \
137			"clone outside the hierarchy."
138done
139
140
141log_note "Verify that 'zfs destroy -R' succeeds to destroy dataset " \
142	"with dependent clone outside it."
143
144log_must_busy zfs destroy -R $ctr1
145datasetexists $ctr1 && \
146	log_fail "'zfs destroy -R' fails to destroy dataset with clone outside it."
147
148log_note "Verify that 'zfs destroy -r' succeeds to destroy dataset " \
149	"without dependent clone outside it."
150
151log_must_busy zfs destroy -r $ctr
152datasetexists $ctr && \
153	log_fail "'zfs destroy -r' fails to destroy dataset with clone outside it."
154
155log_pass "'zfs destroy [-rR] succeeds as root."
156