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	if pgrep -x dd 2>/dev/null; then
76		pkill -x dd
77	fi
78
79	datasetexists $FS && destroy_dataset $FS -Rf
80	if datasetexists $VOL; then
81		if ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
82			log_must umount -f $TESTDIR1
83			sleep 0.1
84		fi
85
86		destroy_dataset $VOL -Rf
87	fi
88
89	# Create specified test environment
90	case $opt in
91		*r*) setup_snap_env $cnt ;;
92		*R*) setup_clone_env $cnt ;;
93	esac
94
95	all_snap="$TESTSNAP $TESTSNAP1 $TESTSNAP2"
96	all_clone="$TESTCLONE $TESTCLONE1 $TESTCLONE2"
97	typeset snap_point
98	typeset exist_snap
99	typeset exist_clone
100	case $pointcnt in
101		1) snap_point=$TESTSNAP
102		   exist_snap=$TESTSNAP
103		   [[ $opt == *R* ]] && exist_clone=$TESTCLONE
104		   ;;
105		2) snap_point=$TESTSNAP1
106		   exist_snap="$TESTSNAP $TESTSNAP1"
107		   [[ $opt == *R* ]] && exist_clone="$TESTCLONE $TESTCLONE1"
108		   ;;
109	esac
110
111	typeset snap
112	for dtst in $FS $VOL; do
113		# Volume is not available in Local Zone.
114		if [[ $dtst == $VOL ]]; then
115			if ! is_global_zone; then
116				break
117			fi
118		fi
119		if [[ $opt == *f* ]]; then
120			# To write data to the mountpoint directory,
121			write_mountpoint_dir $dtst
122			opt=${opt%f}
123		fi
124
125		if [[ $dtst == $VOL ]]; then
126			if ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
127				log_must umount -f $TESTDIR1
128			fi
129			log_must zfs rollback $opt $dtst@$snap_point
130			log_must mount \
131				$ZVOL_DEVDIR/$TESTPOOL/$TESTVOL $TESTDIR1
132		else
133			log_must zfs rollback $opt $dtst@$snap_point
134		fi
135
136		for snap in $all_snap; do
137			if [[ " $exist_snap " == *" $snap "* ]]; then
138				log_must datasetexists $dtst@$snap
139			else
140				log_must datasetnonexists $dtst@$snap
141			fi
142		done
143		for clone in $all_clone; do
144			if [[ " $exist_clone " == *" $clone "* ]]; then
145				log_must datasetexists $dtst$clone
146			else
147				log_must datasetnonexists $dtst$clone
148			fi
149		done
150
151		check_files $dtst@$snap_point
152	done
153}
154
155typeset opt
156for opt in "-r" "-rf" "-R" "-Rf"; do
157	#
158	# Currently, the test case was limited to create and rollback
159	# in three snapshots
160	#
161	log_note "Create 3 snapshots, rollback to the 2nd snapshot " \
162		"using $opt."
163	test_n_check "$opt" 3 2
164
165	log_note "Create 3 snapshots and rollback to the 1st snapshot " \
166		"using $opt."
167	test_n_check "$opt" 3 1
168
169	log_note "Create 2 snapshots and rollback to the 1st snapshot " \
170		"using $opt."
171	test_n_check "$opt" 2 1
172done
173
174log_pass "'zfs rollback -r|-rf|-R|-Rf' recursively destroy any snapshots more "\
175	"recent than the one specified passed."
176