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	typeset sum
40	read -r sum _ < <(cksum "$1")
41	echo $sum
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					sync_all_pools
120					log_must sync
121				else
122					#
123					# using 'lockfs -f' to flush the writes
124					# to disk before taking a snapshot.
125					#
126					if [[ $dtst == $VOL ]]; then
127						log_must lockfs -f $TESTDIR1
128					fi
129				fi
130				if is_freebsd && [[ $dtst == $VOL ]]; then
131					# Though sync does start a fs sync on
132					# FreeBSD, it does not wait for it to
133					# finish.  We can force a blocking sync
134					# by updating the fs mount instead.
135					# Otherwise, the snapshot might occur
136					# with the fs in an unmountable state.
137					log_must mount -ur \
138					    $ZVOL_DEVDIR/$VOL $TESTDIR1
139				fi
140				log_must zfs snapshot $snap
141				if is_freebsd && [[ $dtst == $VOL ]]; then
142					log_must mount -uw \
143					    $ZVOL_DEVDIR/$VOL $TESTDIR1
144				fi
145			fi
146			if [[ $createclone == "true" ]]; then
147				if datasetnonexists $clone; then
148					log_must zfs clone $snap $clone
149					block_device_wait
150				fi
151			fi
152			(( ind += 1 ))
153		done
154	done
155}
156
157function setup_clone_env
158{
159	setup_snap_env $1 "true"
160}
161
162#
163# Clean up the test environment
164#
165# $1 number of snapshot Note: Currently only support three snapshots.
166#
167function cleanup_env
168{
169	typeset -i cnt=${1:-3}
170	typeset -i ind=0
171	typeset dtst
172	typeset snap
173
174	pkill -x dd
175
176	if ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
177		log_must umount -f $TESTDIR1
178	fi
179
180	[[ -d $TESTDIR ]] && log_must rm -rf $TESTDIR/*
181	[[ -d $TESTDIR1 ]] && log_must rm -rf $TESTDIR1/*
182
183	for dtst in $FS $VOL; do
184		for snap in $TESTSNAP $TESTSNAP1 $TESTSNAP2; do
185			snapexists $dtst@$snap && destroy_dataset $dtst@$snap -Rf
186		done
187	done
188
189	# Restore original test environment
190	if datasetnonexists $FS ; then
191		log_must zfs create $FS
192	fi
193	if datasetnonexists $VOL ; then
194		if is_global_zone ; then
195			log_must zfs create -V $VOLSIZE $VOL
196		else
197			log_must zfs create $VOL
198		fi
199	fi
200}
201
202#
203# check if the specified files have specified status.
204#
205# $1 expected status
206# $2-n full file name
207# If it is true return 0, else return 1
208#
209function file_status
210{
211	(( $# == 0 )) && \
212		log_fail "The file name is not defined."
213
214	typeset opt
215	case $1 in
216		exist)	opt="-e" ;;
217		nonexist) opt="! -e" ;;
218		*)	log_fail "Unsupported file status." ;;
219	esac
220
221	shift
222	while (( $# > 0 )); do
223		eval [[ $opt $1 ]] || return 1
224		shift
225	done
226
227	return 0
228}
229
230function files_exist
231{
232	file_status "exist" $@
233}
234
235function files_nonexist
236{
237	file_status "nonexist" $@
238}
239
240#
241# According to snapshot check if the file system was recovered to the right
242# point.
243#
244# $1 snapshot. fs@snap or vol@snap
245#
246function check_files
247{
248	typeset dtst=$1
249
250	if [[ $(get_prop type $dtst) != snapshot ]]; then
251		log_fail "Parameter must be a snapshot."
252	fi
253
254	typeset fsvol=${dtst%%@*}
255	typeset snap=${dtst##*@}
256	if [[ $(get_prop type $fsvol) == "filesystem" ]]; then
257		ind=""
258	else
259		ind="1"
260	fi
261
262	eval typeset file0=\$TESTDIR$ind/\$TESTFILE0
263	eval typeset file1=\$TESTDIR$ind/\$TESTFILE1
264	eval typeset file2=\$TESTDIR$ind/\$TESTFILE2
265
266	case $snap in
267		$TESTSNAP2)
268			log_must files_exist $file0 $file1 $file2
269
270			typeset sum0=$(getsum $file0)
271			typeset sum1=$(getsum $file1)
272			typeset sum2=$(getsum $file2)
273			log_must [ $sum0 = $origsum \&\& $sum1 = $origsum \&\& sum2 = $origsum ]
274			;;
275		$TESTSNAP1)
276			log_must files_exist $file0 $file1
277			log_must files_nonexist $file2
278
279			typeset sum0=$(getsum $file0)
280			typeset sum1=$(getsum $file1)
281			log_must [ $sum0 = $origsum \&\& $sum1 = $origsum ]
282			;;
283		$TESTSNAP)
284			log_must files_exist $file0
285			log_must files_nonexist $file1 $file2
286
287			typeset sum0=$(getsum $file0)
288			log_must [ $sum0 = $origsum ]
289			;;
290	esac
291}
292
293# According to dataset type, write file to different directories.
294#
295# $1 dataset
296#
297function write_mountpoint_dir
298{
299	typeset dtst=$1
300	typeset dir
301
302	if [[ $dtst == $FS ]]; then
303		dir=$TESTDIR
304		log_must ismounted $dir
305	else
306		dir=$TESTDIR1
307		log_must ismounted $dir $NEWFS_DEFAULT_FS
308	fi
309	dd if=/dev/urandom of=$dir/$TESTFILE1 &
310	log_must sleep 3
311}
312