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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/snapshot/snapshot.cfg
34
35#
36# DESCRIPTION:
37# Verify that rollbacks are with respect to the latest snapshot.
38#
39# STRATEGY:
40# 1. Empty a file system
41# 2. Populate the file system
42# 3. Take a snapshot of the file system
43# 4. Add new files to the file system
44# 5. Take a snapshot
45# 6. Remove the original files
46# 7. Perform a rollback
47# 8. Verify the latest snapshot and file system agree
48#
49
50verify_runnable "both"
51
52function cleanup
53{
54	snapexists $SNAPFS.1 &&
55		log_must zfs destroy $SNAPFS.1
56
57	snapexists $SNAPFS &&
58		log_must zfs destroy $SNAPFS
59
60	[ -e $TESTDIR ] && log_must rm -rf $TESTDIR/*
61}
62
63log_assert "Verify rollback is with respect to latest snapshot."
64
65log_onexit cleanup
66
67[ -n $TESTDIR ] && log_must rm -rf $TESTDIR/*
68
69typeset -i COUNT=10
70
71log_note "Populate the $TESTDIR directory (prior to first snapshot)"
72typeset -i i=1
73while [[ $i -le $COUNT ]]; do
74	log_must file_write -o create -f $TESTDIR/original_file$i \
75	   -b $BLOCKSZ -c $NUM_WRITES -d $i
76
77	(( i = i + 1 ))
78done
79
80log_must zfs snapshot $SNAPFS
81
82FILE_COUNT=$(ls -A $SNAPDIR | wc -l)
83if [[ $FILE_COUNT -ne $COUNT ]]; then
84        ls -Al $SNAPDIR
85        log_fail "AFTER: $SNAPFS contains $FILE_COUNT files(s)."
86fi
87
88log_note "Populate the $TESTDIR directory (prior to second snapshot)"
89typeset -i i=1
90while [[ $i -le $COUNT ]]; do
91        log_must file_write -o create -f $TESTDIR/afterfirst_file$i \
92           -b $BLOCKSZ -c $NUM_WRITES -d $i
93
94        (( i = i + 1 ))
95done
96
97log_must zfs snapshot $SNAPFS.1
98
99log_note "Populate the $TESTDIR directory (Post second snapshot)"
100typeset -i i=1
101while [[ $i -le $COUNT ]]; do
102        log_must file_write -o create -f $TESTDIR/aftersecond_file$i \
103           -b $BLOCKSZ -c $NUM_WRITES -d $i
104
105        (( i = i + 1 ))
106done
107
108[ -n $TESTDIR ] && log_must rm -f $TESTDIR/original_file*
109
110#
111# Now rollback to latest snapshot
112#
113log_must zfs rollback $SNAPFS.1
114
115FILE_COUNT=$(ls -A $TESTDIR/aftersecond* 2> /dev/null | wc -l)
116if [[ $FILE_COUNT -ne 0 ]]; then
117        ls -Al $TESTDIR
118        log_fail "$TESTDIR contains $FILE_COUNT aftersecond* files(s)."
119fi
120
121FILE_COUNT=$(ls -A $TESTDIR/original* $TESTDIR/afterfirst* | wc -l)
122if [[ $FILE_COUNT -ne 20 ]]; then
123        ls -Al $TESTDIR
124        log_fail "$TESTDIR contains $FILE_COUNT original* files(s)."
125fi
126
127log_pass "The rollback to the latest snapshot succeeded."
128