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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28
29#
30# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
31#
32
33. $STF_SUITE/include/libtest.shlib
34. $STF_SUITE/tests/functional/snapshot/snapshot.cfg
35. $STF_SUITE/tests/functional/cli_root/zfs_rollback/zfs_rollback_common.kshlib
36
37#
38# DESCRIPTION:
39#	Verify snapshot can be created or destroy via mkdir or rm
40#	in .zfs/snapshot.
41#
42# STRATEGY:
43#	1. Verify make directories only successfully in .zfs/snapshot.
44#	2. Verify snapshot can be created and destroy via mkdir and remove
45#	directories in .zfs/snapshot.
46#	3. Verify rollback to previous snapshot can succeed.
47#	4. Verify remove directory in snapdir can destroy snapshot.
48#
49
50verify_runnable "both"
51
52function cleanup
53{
54	typeset -i i=0
55	while ((i < snap_cnt)); do
56		typeset snap=$fs@snap.$i
57		datasetexists $snap && log_must zfs destroy -f $snap
58
59		((i += 1))
60	done
61}
62
63zfs 2>&1 | grep "allow" > /dev/null
64(($? != 0)) && log_unsupported
65
66log_assert "Verify snapshot can be created via mkdir in .zfs/snapshot."
67log_onexit cleanup
68
69fs=$TESTPOOL/$TESTFS
70# Verify all the other directories are readonly.
71mntpnt=$(get_prop mountpoint $fs)
72snapdir=$mntpnt/.zfs
73set -A ro_dirs "$snapdir" "$snapdir/snap" "$snapdir/snapshot"
74for dir in ${ro_dirs[@]}; do
75	if [[ -d $dir ]]; then
76		log_mustnot rm -rf $dir
77		log_mustnot touch $dir/testfile
78	else
79		log_mustnot mkdir $dir
80	fi
81done
82
83# Verify snapshot can be created via mkdir in .zfs/snapshot
84typeset -i snap_cnt=5
85typeset -i cnt=0
86while ((cnt < snap_cnt)); do
87	testfile=$mntpnt/testfile.$cnt
88	log_must mkfile 1M $testfile
89	log_must mkdir $snapdir/snapshot/snap.$cnt
90	if ! datasetexists $fs@snap.$cnt ; then
91		log_fail "ERROR: $fs@snap.$cnt should exists."
92	fi
93
94	((cnt += 1))
95done
96
97# Verify rollback to previous snapshot succeed.
98((cnt = RANDOM % snap_cnt))
99log_must zfs rollback -r $fs@snap.$cnt
100
101typeset -i i=0
102while ((i < snap_cnt)); do
103	testfile=$mntpnt/testfile.$i
104	if ((i <= cnt)); then
105		if [[ ! -f $testfile ]]; then
106			log_fail "ERROR: $testfile should exists."
107		fi
108	else
109		if [[ -f $testfile ]]; then
110			log_fail "ERROR: $testfile should not exists."
111		fi
112	fi
113
114	((i += 1))
115done
116
117# Verify remove directory in snapdir can destroy snapshot.
118log_must rmdir $snapdir/snapshot/snap.$cnt
119log_mustnot datasetexists $fs@snap.$cnt
120
121log_pass "Verify snapshot can be created via mkdir in .zfs/snapshot passed."
122