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# Populate a file system and take a snapshot. Add some more files to the
38# file system and rollback to the last snapshot. Verify no post snapshot
39# file exist.
40#
41# STRATEGY:
42# 1. Empty a file system
43# 2. Populate the file system
44# 3. Take a snapshot of the file system
45# 4. Add new files to the file system
46# 5. Perform a rollback
47# 6. Verify the snapshot and file system agree
48#
49
50verify_runnable "both"
51
52function cleanup
53{
54	snapexists $SNAPFS
55	[[ $? -eq 0 ]] && \
56		log_must zfs destroy $SNAPFS
57
58	[[ -e $TESTDIR ]] && \
59		log_must rm -rf $TESTDIR/* > /dev/null 2>&1
60}
61
62log_assert "Verify that a rollback to a previous snapshot succeeds."
63
64log_onexit cleanup
65
66[[ -n $TESTDIR ]] && \
67    log_must rm -rf $TESTDIR/* > /dev/null 2>&1
68
69typeset -i COUNT=10
70
71log_note "Populate the $TESTDIR directory (prior to snapshot)"
72typeset -i i=1
73while [[ $i -le $COUNT ]]; do
74	log_must file_write -o create -f $TESTDIR/before_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 -Al $SNAPDIR | grep -v "total" | 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 (post snapshot)"
89typeset -i i=1
90while [[ $i -le $COUNT ]]; do
91        log_must file_write -o create -f $TESTDIR/after_file$i \
92           -b $BLOCKSZ -c $NUM_WRITES -d $i
93
94        (( i = i + 1 ))
95done
96sync_pool $TESTPOOL
97
98#
99# Now rollback to latest snapshot
100#
101log_must zfs rollback $SNAPFS
102
103FILE_COUNT=`ls -Al $TESTDIR/after* 2> /dev/null | grep -v "total" | wc -l`
104if [[ $FILE_COUNT -ne 0 ]]; then
105        ls -Al $TESTDIR
106        log_fail "$TESTDIR contains $FILE_COUNT after* files(s)."
107fi
108
109FILE_COUNT=`ls -Al $TESTDIR/before* 2> /dev/null \
110    | grep -v "total" | wc -l`
111if [[ $FILE_COUNT -ne $COUNT ]]; then
112	ls -Al $TESTDIR
113	log_fail "$TESTDIR contains $FILE_COUNT before* files(s)."
114fi
115
116log_pass "The rollback operation succeeded."
117