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 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#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zfs_rollback/zfs_rollback_common.kshlib
34
35#
36# DESCRIPTION:
37#	'zfs rollback -r|-rf|-R|-Rf' will recursively destroy any snapshots
38#	more recent than the one specified.
39#
40# STRATEGY:
41#	1. Create pool, fs & volume.
42#	2. Separately create three snapshots or clones for fs & volume
43#	3. Roll back to the second snapshot and check the results.
44#	4. Create the third snapshot or clones for fs & volume again.
45#	5. Roll back to the first snapshot and check the results.
46#	6. Separately create two snapshots for fs & volume.
47#	7. Roll back to the first snapshot and check the results.
48#
49
50verify_runnable "both"
51
52log_assert "'zfs rollback -r|-rf|-R|-Rf' will recursively destroy any " \
53	"snapshots more recent than the one specified."
54log_onexit cleanup_env
55
56#
57# Create suitable test environment and run 'zfs rollback', then compare with
58# expected value to check the system status.
59#
60# $1 option.
61# $2 the number of snapshots or clones.
62# $3 the number of snapshot point which we want to rollback.
63#
64function test_n_check #opt num_snap_clone num_rollback
65{
66	typeset opt=$1
67	typeset -i cnt=$2
68	typeset -i pointcnt=$3
69	typeset dtst
70
71	(( cnt > 3 || pointcnt > cnt )) && \
72		log_fail "Unsupported testing condition."
73
74	# Clean up the test environment
75	datasetexists $FS && log_must zfs destroy -Rf $FS
76	if datasetexists $VOL; then
77		df -lhF ufs "/dev/zvol/dsk/$VOL" > /dev/null 2>&1
78		(( $? == 0 )) && log_must umount -f $TESTDIR1
79
80		log_must zfs destroy -Rf $VOL
81	fi
82
83	# Create specified test environment
84	case $opt in
85		*r*) setup_snap_env $cnt ;;
86		*R*) setup_clone_env $cnt ;;
87	esac
88
89	all_snap="$TESTSNAP $TESTSNAP1 $TESTSNAP2"
90	all_clone="$TESTCLONE $TESTCLONE1 $TESTCLONE2"
91	typeset snap_point
92	typeset exist_snap
93	typeset exist_clone
94	case $pointcnt in
95		1) snap_point=$TESTSNAP
96		   exist_snap=$TESTSNAP
97		   [[ $opt == *R* ]] && exist_clone=$TESTCLONE
98		   ;;
99		2) snap_point=$TESTSNAP1
100		   exist_snap="$TESTSNAP $TESTSNAP1"
101		   [[ $opt == *R* ]] && exist_clone="$TESTCLONE $TESTCLONE1"
102		   ;;
103	esac
104
105	typeset snap
106	for dtst in $FS $VOL; do
107		# Volume is not available in Local Zone.
108		if [[ $dtst == $VOL ]]; then
109			if ! is_global_zone; then
110				break
111			fi
112		fi
113		if [[ $opt == *f* ]]; then
114			# To write data to the mountpoint directory,
115			write_mountpoint_dir $dtst
116			opt=${opt%f}
117		fi
118
119		if [[ $dtst == $VOL ]]; then
120			log_must umount -f $TESTDIR1
121			log_must zfs rollback $opt $dtst@$snap_point
122			log_must mount \
123				/dev/zvol/dsk/$TESTPOOL/$TESTVOL $TESTDIR1
124		else
125			log_must zfs rollback $opt $dtst@$snap_point
126		fi
127
128		for snap in $all_snap; do
129			if [[ " $exist_snap " == *" $snap "* ]]; then
130				log_must datasetexists $dtst@$snap
131			else
132				log_must datasetnonexists $dtst@$snap
133			fi
134		done
135		for clone in $all_clone; do
136			if [[ " $exist_clone " == *" $clone "* ]]; then
137				log_must datasetexists $dtst$clone
138			else
139				log_must datasetnonexists $dtst$clone
140			fi
141		done
142
143		check_files $dtst@$snap_point
144	done
145}
146
147typeset opt
148for opt in "-r" "-rf" "-R" "-Rf"; do
149	#
150	# Currently, the test case was limited to create and rollback
151	# in three snapshots
152	#
153	log_note "Create 3 snapshots, rollback to the 2nd snapshot " \
154		"using $opt."
155	test_n_check "$opt" 3 2
156
157	log_note "Create 3 snapshots and rollback to the 1st snapshot " \
158		"using $opt."
159	test_n_check "$opt" 3 1
160
161	log_note "Create 2 snapshots and rollback to the 1st snapshot " \
162		"using $opt."
163	test_n_check "$opt" 2 1
164done
165
166log_pass "'zfs rollback -r|-rf|-R|-Rf' recursively destroy any snapshots more "\
167	"recent than the one specified passed."
168