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# 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#	Create a snapshot from regular filesystem, volume,
38#	or filesystem upon volume, Build a clone file system
39#	from the snapshot and verify new files can be written.
40#
41# STRATEGY:
42# 	1. Create snapshot use 3 combination:
43#		- Regular filesystem
44#		- Regular volume
45#		- Filesystem upon volume
46# 	2. Clone a new file system from the snapshot
47# 	3. Verify the cloned file system is writable
48#
49
50verify_runnable "both"
51
52# Setup array, 4 elements as a group, refer to:
53# i+0: name of a snapshot
54# i+1: mountpoint of the snapshot
55# i+2: clone created from the snapshot
56# i+3: mountpoint of the clone
57
58set -A args "$SNAPFS" "$SNAPDIR" "$TESTPOOL/$TESTCLONE" "$TESTDIR.0" \
59	"$SNAPFS1" "$SNAPDIR3" "$TESTPOOL/$TESTCLONE1" "" \
60	"$SNAPFS2" "$SNAPDIR2" "$TESTPOOL1/$TESTCLONE2" "$TESTDIR.2"
61
62function setup_all
63{
64	create_pool $TESTPOOL1 /dev/zvol/dsk/$TESTPOOL/$TESTVOL
65	log_must zfs create $TESTPOOL1/$TESTFS
66	log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL1/$TESTFS
67
68	return 0
69}
70
71function cleanup_all
72{
73	typeset -i i=0
74
75	i=0
76	while (( i < ${#args[*]} )); do
77		snapexists ${args[i]} && \
78			log_must zfs destroy -Rf ${args[i]}
79
80		[[ -d ${args[i+3]} ]] && \
81			log_must rm -rf ${args[i+3]}
82
83		[[ -d ${args[i+1]} ]] && \
84			log_must rm -rf ${args[i+1]}
85
86		(( i = i + 4 ))
87	done
88
89	datasetexists $TESTPOOL1/$TESTFS  && \
90		log_must zfs destroy -f $TESTPOOL1/$TESTFS
91
92	destroy_pool $TESTPOOL1
93
94	[[ -d $TESTDIR2 ]] && \
95		log_must rm -rf $TESTDIR2
96
97	return 0
98}
99
100log_assert "Verify a cloned file system is writable."
101
102log_onexit cleanup_all
103
104setup_all
105
106[[ -n $TESTDIR ]] && \
107    log_must rm -rf $TESTDIR/* > /dev/null 2>&1
108
109typeset -i COUNT=10
110typeset -i i=0
111
112for mtpt in $TESTDIR $TESTDIR2 ; do
113	log_note "Populate the $mtpt directory (prior to snapshot)"
114	typeset -i j=1
115	while [[ $j -le $COUNT ]]; do
116		log_must file_write -o create -f $mtpt/before_file$j \
117			-b $BLOCKSZ -c $NUM_WRITES -d $j
118
119		(( j = j + 1 ))
120	done
121done
122
123while (( i < ${#args[*]} )); do
124	#
125	# Take a snapshot of the test file system.
126	#
127	log_must zfs snapshot ${args[i]}
128
129	#
130	# Clone a new file system from the snapshot
131	#
132	log_must zfs clone ${args[i]} ${args[i+2]}
133	if [[ -n ${args[i+3]} ]] ; then
134		log_must zfs set mountpoint=${args[i+3]} ${args[i+2]}
135
136		FILE_COUNT=`ls -Al ${args[i+3]} | grep -v "total" \
137		    | grep -v "\.zfs" | wc -l`
138		if [[ $FILE_COUNT -ne $COUNT ]]; then
139			ls -Al ${args[i+3]}
140			log_fail "AFTER: ${args[i+3]} contains $FILE_COUNT files(s)."
141		fi
142
143		log_note "Verify the ${args[i+3]} directory is writable"
144		j=1
145		while [[ $j -le $COUNT ]]; do
146			log_must file_write -o create -f ${args[i+3]}/after_file$j \
147			-b $BLOCKSZ -c $NUM_WRITES -d $j
148			(( j = j + 1 ))
149		done
150
151		FILE_COUNT=`ls -Al ${args[i+3]}/after* | grep -v "total" | wc -l`
152		if [[ $FILE_COUNT -ne $COUNT ]]; then
153			ls -Al ${args[i+3]}
154			log_fail "${args[i+3]} contains $FILE_COUNT after* files(s)."
155		fi
156	fi
157
158	(( i = i + 4 ))
159done
160
161log_pass "The clone file system is writable."
162