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 http://www.opensolaris.org/os/licensing.
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 2008 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/cli_root/zfs_rollback/zfs_rollback.cfg
33
34# Get file sum
35#
36# $1 full file name
37function getsum #fname
38{
39	(( ${#1} == 0 )) && \
40		log_fail "Need give file name."
41	return $(sum $1 | awk '{print $1}')
42}
43
44# Define global variable checksum, get the original file sum.
45#
46origsum=$(getsum /etc/passwd)
47
48#
49# Setup or recover the test environment. Firstly, copy /etc/passwd to ZFS file
50# system or volume, then make a snapshot or clone. Repeat up to three times.
51#
52# $1 number of snapshot. Note: Currently only support three snapshots.
53# $2 indicate if it is necessary to create clone
54#
55function setup_snap_env
56{
57	typeset -i cnt=${1:-3}
58	typeset createclone=${2:-"false"}
59
60	if datasetnonexists $FS; then
61		log_must zfs create $FS
62		log_must zfs set mountpoint=$TESTDIR $FS
63	fi
64	# Volume can't be created in Local Zone.
65	if datasetnonexists $VOL && is_global_zone; then
66		log_must zfs create -V $VOLSIZE $VOL
67		block_device_wait
68	fi
69
70	# Make sure $VOL is volume
71	typeset type=$(get_prop type $VOL)
72	if datasetexists $VOL && \
73		[[ $type == 'volume' ]]; then
74		#
75		# At the first time, Make a UFS file system in volume and
76		# mount it. Otherwise, only check if this ufs|ext file system
77		# was mounted.
78		#
79		log_must new_fs $ZVOL_DEVDIR/$VOL
80
81		[[ ! -d $TESTDIR1 ]] && log_must mkdir $TESTDIR1
82
83		# Make sure the ufs|ext filesystem hasn't been mounted,
84		# then mount the new ufs|ext filesystem.
85		if ! ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
86			log_must mount $ZVOL_DEVDIR/$VOL $TESTDIR1
87		fi
88	fi
89
90	# Separately Create three snapshots for file system & volume
91	typeset -i ind=0
92	typeset dtst
93	for dtst in $FS $VOL; do
94		# Volume can be created in Local Zone.
95		if [[ $dtst == $VOL ]]; then
96			if ! is_global_zone; then
97				break
98			fi
99		fi
100
101		ind=0
102		while (( ind < cnt )); do
103			case $dtst in
104			$FS)
105				eval typeset snap=\$FSSNAP$ind
106				eval typeset clone=\$FSCLONE$ind
107				eval typeset fname=\$TESTDIR/\$TESTFILE$ind
108				;;
109			$VOL)
110				eval typeset snap=\$VOLSNAP$ind
111				eval typeset clone=\$VOLCLONE$ind
112				eval typeset fname=\$TESTDIR1/\$TESTFILE$ind
113				;;
114			esac
115
116			if datasetnonexists $snap; then
117				log_must cp /etc/passwd $fname
118				if is_linux || is_freebsd; then
119					log_must sync
120				else
121					#
122					# using 'lockfs -f' to flush the writes
123					# to disk before taking a snapshot.
124					#
125					if [[ $dtst == $VOL ]]; then
126						log_must lockfs -f $TESTDIR1
127					fi
128				fi
129				if is_freebsd && [[ $dtst == $VOL ]]; then
130					# Though sync does start a fs sync on
131					# FreeBSD, it does not wait for it to
132					# finish.  We can force a blocking sync
133					# by updating the fs mount instead.
134					# Otherwise, the snapshot might occur
135					# with the fs in an unmountable state.
136					log_must mount -ur \
137					    $ZVOL_DEVDIR/$VOL $TESTDIR1
138				fi
139				log_must zfs snapshot $snap
140				if is_freebsd && [[ $dtst == $VOL ]]; then
141					log_must mount -uw \
142					    $ZVOL_DEVDIR/$VOL $TESTDIR1
143				fi
144			fi
145			if [[ $createclone == "true" ]]; then
146				if datasetnonexists $clone; then
147					log_must zfs clone $snap $clone
148					block_device_wait
149				fi
150			fi
151			(( ind += 1 ))
152		done
153	done
154}
155
156function setup_clone_env
157{
158	setup_snap_env $1 "true"
159}
160
161#
162# Clean up the test environment
163#
164# $1 number of snapshot Note: Currently only support three snapshots.
165#
166function cleanup_env
167{
168	typeset -i cnt=${1:-3}
169	typeset -i ind=0
170	typeset dtst
171	typeset snap
172
173	pkill -x dd
174
175	if ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
176		log_must umount -f $TESTDIR1
177	fi
178
179	[[ -d $TESTDIR ]] && log_must rm -rf $TESTDIR/*
180	[[ -d $TESTDIR1 ]] && log_must rm -rf $TESTDIR1/*
181
182	for dtst in $FS $VOL; do
183		for snap in $TESTSNAP $TESTSNAP1 $TESTSNAP2; do
184			snapexists $dtst@$snap && destroy_dataset $dtst@$snap -Rf
185		done
186	done
187
188	# Restore original test environment
189	if datasetnonexists $FS ; then
190		log_must zfs create $FS
191	fi
192	if datasetnonexists $VOL ; then
193		if is_global_zone ; then
194			log_must zfs create -V $VOLSIZE $VOL
195		else
196			log_must zfs create $VOL
197		fi
198	fi
199}
200
201#
202# check if the specified files have specified status.
203#
204# $1 expected status
205# $2-n full file name
206# If it is true return 0, else return 1
207#
208function file_status
209{
210	(( $# == 0 )) && \
211		log_fail "The file name is not defined."
212
213	typeset opt
214	case $1 in
215		exist)	opt="-e" ;;
216		nonexist) opt="! -e" ;;
217		*)	log_fail "Unsupported file status." ;;
218	esac
219
220	shift
221	while (( $# > 0 )); do
222		eval [[ $opt $1 ]] || return 1
223		shift
224	done
225
226	return 0
227}
228
229function files_exist
230{
231	file_status "exist" $@
232}
233
234function files_nonexist
235{
236	file_status "nonexist" $@
237}
238
239#
240# According to snapshot check if the file system was recovered to the right
241# point.
242#
243# $1 snapshot. fs@snap or vol@snap
244#
245function check_files
246{
247	typeset dtst=$1
248
249	if [[ $(get_prop type $dtst) != snapshot ]]; then
250		log_fail "Parameter must be a snapshot."
251	fi
252
253	typeset fsvol=${dtst%%@*}
254	typeset snap=${dtst##*@}
255	if [[ $(get_prop type $fsvol) == "filesystem" ]]; then
256		ind=""
257	else
258		ind="1"
259	fi
260
261	eval typeset file0=\$TESTDIR$ind/\$TESTFILE0
262	eval typeset file1=\$TESTDIR$ind/\$TESTFILE1
263	eval typeset file2=\$TESTDIR$ind/\$TESTFILE2
264
265	case $snap in
266		$TESTSNAP2)
267			log_must files_exist $file0 $file1 $file2
268
269			typeset sum0=$(getsum $file0)
270			typeset sum1=$(getsum $file1)
271			typeset sum2=$(getsum $file2)
272			if [[ $sum0 != $origsum || \
273				$sum1 != $origsum || sum2 != $origsum ]]
274			then
275				log_fail "After rollback, file sum is changed."
276			fi
277			;;
278		$TESTSNAP1)
279			log_must files_exist $file0 $file1
280			log_must files_nonexist $file2
281
282			typeset sum0=$(getsum $file0)
283			typeset sum1=$(getsum $file1)
284			if [[ $sum0 != $origsum || $sum1 != $origsum ]]
285			then
286				log_fail "After rollback, file sum is changed."
287			fi
288			;;
289		$TESTSNAP)
290			log_must files_exist $file0
291			log_must files_nonexist $file1 $file2
292
293			typeset sum0=$(getsum $file0)
294			if [[ $sum0 != $origsum ]]; then
295				log_fail "After rollback, file sum is changed."
296			fi
297			;;
298	esac
299}
300
301# According to dataset type, write file to different directories.
302#
303# $1 dataset
304#
305function write_mountpoint_dir
306{
307	typeset dtst=$1
308	typeset dir
309
310	if [[ $dtst == $FS ]]; then
311		dir=$TESTDIR
312		log_must ismounted $dir
313	else
314		dir=$TESTDIR1
315		log_must ismounted $dir $NEWFS_DEFAULT_FS
316	fi
317	dd if=/dev/urandom of=$dir/$TESTFILE1 &
318	log_must sleep 3
319}
320