1eda14cbcSMatt Macy#!/bin/ksh -p
2eda14cbcSMatt Macy#
3eda14cbcSMatt Macy# CDDL HEADER START
4eda14cbcSMatt Macy#
5eda14cbcSMatt Macy# The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy# Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy# You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy#
9eda14cbcSMatt Macy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*271171e0SMartin Matuska# or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy# See the License for the specific language governing permissions
12eda14cbcSMatt Macy# and limitations under the License.
13eda14cbcSMatt Macy#
14eda14cbcSMatt Macy# When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy# If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy# fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy# information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy#
20eda14cbcSMatt Macy# CDDL HEADER END
21eda14cbcSMatt Macy#
22eda14cbcSMatt Macy
23eda14cbcSMatt Macy#
24eda14cbcSMatt Macy# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25eda14cbcSMatt Macy# Use is subject to license terms.
26eda14cbcSMatt Macy#
27eda14cbcSMatt Macy
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy#
30eda14cbcSMatt Macy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
31eda14cbcSMatt Macy#
32eda14cbcSMatt Macy
33eda14cbcSMatt Macy. $STF_SUITE/include/libtest.shlib
34eda14cbcSMatt Macy. $STF_SUITE/tests/functional/snapshot/snapshot.cfg
35eda14cbcSMatt Macy. $STF_SUITE/tests/functional/cli_root/zfs_rollback/zfs_rollback_common.kshlib
36eda14cbcSMatt Macy
37eda14cbcSMatt Macy#
38eda14cbcSMatt Macy# DESCRIPTION:
39eda14cbcSMatt Macy#	Verify snapshot can be created or destroy via mkdir or rm
40eda14cbcSMatt Macy#	in .zfs/snapshot.
41eda14cbcSMatt Macy#
42eda14cbcSMatt Macy# STRATEGY:
43eda14cbcSMatt Macy#	1. Verify make directories only successfully in .zfs/snapshot.
44eda14cbcSMatt Macy#	2. Verify snapshot can be created and destroy via mkdir and remove
45eda14cbcSMatt Macy#	directories in .zfs/snapshot.
46eda14cbcSMatt Macy#	3. Verify rollback to previous snapshot can succeed.
47eda14cbcSMatt Macy#	4. Verify remove directory in snapdir can destroy snapshot.
48eda14cbcSMatt Macy#
49eda14cbcSMatt Macy
50eda14cbcSMatt Macyverify_runnable "both"
51eda14cbcSMatt Macy
52eda14cbcSMatt Macyfunction cleanup
53eda14cbcSMatt Macy{
54eda14cbcSMatt Macy	typeset -i i=0
55eda14cbcSMatt Macy	while ((i < snap_cnt)); do
56eda14cbcSMatt Macy		typeset snap=$fs@snap.$i
5781b22a98SMartin Matuska		datasetexists $snap && destroy_dataset $snap -f
58eda14cbcSMatt Macy
59eda14cbcSMatt Macy		((i += 1))
60eda14cbcSMatt Macy	done
61eda14cbcSMatt Macy}
62eda14cbcSMatt Macy
63eda14cbcSMatt Macyzfs 2>&1 | grep "allow" > /dev/null
64eda14cbcSMatt Macy(($? != 0)) && log_unsupported
65eda14cbcSMatt Macy
66eda14cbcSMatt Macylog_assert "Verify snapshot can be created via mkdir in .zfs/snapshot."
67eda14cbcSMatt Macylog_onexit cleanup
68eda14cbcSMatt Macy
69eda14cbcSMatt Macyfs=$TESTPOOL/$TESTFS
70eda14cbcSMatt Macy# Verify all the other directories are readonly.
71eda14cbcSMatt Macymntpnt=$(get_prop mountpoint $fs)
72eda14cbcSMatt Macysnapdir=$mntpnt/.zfs
73eda14cbcSMatt Macyset -A ro_dirs "$snapdir" "$snapdir/snap" "$snapdir/snapshot"
74eda14cbcSMatt Macyfor dir in ${ro_dirs[@]}; do
75eda14cbcSMatt Macy	if [[ -d $dir ]]; then
76eda14cbcSMatt Macy		log_mustnot rm -rf $dir
77eda14cbcSMatt Macy		log_mustnot touch $dir/testfile
78eda14cbcSMatt Macy	else
79eda14cbcSMatt Macy		log_mustnot mkdir $dir
80eda14cbcSMatt Macy	fi
81eda14cbcSMatt Macydone
82eda14cbcSMatt Macy
83eda14cbcSMatt Macy# Verify snapshot can be created via mkdir in .zfs/snapshot
84eda14cbcSMatt Macytypeset -i snap_cnt=5
85eda14cbcSMatt Macytypeset -i cnt=0
86eda14cbcSMatt Macywhile ((cnt < snap_cnt)); do
87eda14cbcSMatt Macy	testfile=$mntpnt/testfile.$cnt
88eda14cbcSMatt Macy	log_must mkfile 1M $testfile
89eda14cbcSMatt Macy	log_must mkdir $snapdir/snapshot/snap.$cnt
90eda14cbcSMatt Macy	if ! datasetexists $fs@snap.$cnt ; then
91eda14cbcSMatt Macy		log_fail "ERROR: $fs@snap.$cnt should exists."
92eda14cbcSMatt Macy	fi
93eda14cbcSMatt Macy
94eda14cbcSMatt Macy	((cnt += 1))
95eda14cbcSMatt Macydone
96eda14cbcSMatt Macy
97eda14cbcSMatt Macy# Verify rollback to previous snapshot succeed.
98eda14cbcSMatt Macy((cnt = RANDOM % snap_cnt))
99eda14cbcSMatt Macylog_must zfs rollback -r $fs@snap.$cnt
100eda14cbcSMatt Macy
101eda14cbcSMatt Macytypeset -i i=0
102eda14cbcSMatt Macywhile ((i < snap_cnt)); do
103eda14cbcSMatt Macy	testfile=$mntpnt/testfile.$i
104eda14cbcSMatt Macy	if ((i <= cnt)); then
105eda14cbcSMatt Macy		if [[ ! -f $testfile ]]; then
106eda14cbcSMatt Macy			log_fail "ERROR: $testfile should exists."
107eda14cbcSMatt Macy		fi
108eda14cbcSMatt Macy	else
109eda14cbcSMatt Macy		if [[ -f $testfile ]]; then
110eda14cbcSMatt Macy			log_fail "ERROR: $testfile should not exists."
111eda14cbcSMatt Macy		fi
112eda14cbcSMatt Macy	fi
113eda14cbcSMatt Macy
114eda14cbcSMatt Macy	((i += 1))
115eda14cbcSMatt Macydone
116eda14cbcSMatt Macy
117eda14cbcSMatt Macy# Verify remove directory in snapdir can destroy snapshot.
118eda14cbcSMatt Macylog_must rmdir $snapdir/snapshot/snap.$cnt
119eda14cbcSMatt Macylog_mustnot datasetexists $fs@snap.$cnt
120eda14cbcSMatt Macy
121eda14cbcSMatt Macylog_pass "Verify snapshot can be created via mkdir in .zfs/snapshot passed."
122