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 https://opensource.org/licenses/CDDL-1.0.
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 (c) 2009, Sun Microsystems Inc. All rights reserved.
25# Copyright (c) 2013, 2016, Delphix. All rights reserved.
26# Use is subject to license terms.
27#
28
29. $STF_SUITE/include/properties.shlib
30. $STF_SUITE/tests/functional/rsend/rsend.kshlib
31
32#
33# DESCRIPTION:
34#	zfs send -R will backup all the filesystem properties correctly.
35#
36# STRATEGY:
37#	1. Setting properties for all the filesystem and volumes randomly
38#	2. Backup all the data from POOL by send -R
39#	3. Restore all the data in POOL2
40#	4. Verify all the properties in the two pools are the same
41#
42
43verify_runnable "global"
44
45function edited_prop
46{
47	typeset behaviour=$1
48	typeset ds=$2
49	typeset backfile=$TESTDIR/edited_prop_$ds
50	typeset te=0
51
52	case $behaviour in
53		"get")
54			is_te_enabled && te=1
55			typeset props=$(zfs inherit 2>&1 | \
56				awk -v te=$te '$2=="YES" && $1 !~ /^vol|\.\.\.$/ && (te || $1 != "mlslabel") {printf("%s,", $1)}')
57			log_must eval "zfs get -Ho property,value ${props%,} $ds >> $backfile"
58			;;
59		"set")
60			if [[ ! -f $backfile ]] ; then
61				log_fail "$ds need backup properties firstly."
62			fi
63
64			log_must zfs set $(tr '\t' '=' < $backfile) "$ds"
65			;;
66		*)
67			log_fail "Unrecognized behaviour: $behaviour"
68	esac
69}
70
71function cleanup
72{
73	log_must cleanup_pool $POOL
74	log_must cleanup_pool $POOL2
75
76	log_must edited_prop "set" $POOL
77	log_must edited_prop "set" $POOL2
78
79	typeset prop
80	for prop in $(fs_inherit_prop) ; do
81		log_must zfs inherit $prop $POOL
82		log_must zfs inherit $prop $POOL2
83	done
84
85	log_must setup_test_model $POOL
86
87	if [[ -d $TESTDIR ]]; then
88		log_must rm -rf $TESTDIR/*
89	fi
90}
91
92log_assert "Verify zfs send -R will backup all the filesystem properties " \
93	"correctly."
94log_onexit cleanup
95
96log_must edited_prop "get" $POOL
97log_must edited_prop "get" $POOL2
98
99for fs in "$POOL" "$POOL/pclone" "$POOL/$FS" "$POOL/$FS/fs1" \
100	"$POOL/$FS/fs1/fs2" "$POOL/$FS/fs1/fclone" ; do
101	rand_set_prop $fs aclinherit "discard" "noallow" "secure" "passthrough"
102	rand_set_prop $fs checksum "on" "off" "fletcher2" "fletcher4" "sha256"
103	rand_set_prop $fs acltype "off" "posix" "nfsv4" "noacl" "posixacl"
104	rand_set_prop $fs atime "on" "off"
105	rand_set_prop $fs checksum "on" "off" "fletcher2" "fletcher4" "sha256"
106	rand_set_prop $fs compression "${compress_prop_vals[@]}"
107	rand_set_prop $fs copies "1" "2" "3"
108	rand_set_prop $fs devices "on" "off"
109	rand_set_prop $fs exec "on" "off"
110	rand_set_prop $fs quota "512M" "1024M"
111	rand_set_prop $fs recordsize "512" "2K" "8K" "32K" "128K"
112	rand_set_prop $fs dnodesize "legacy" "auto" "1k" "2k" "4k" "8k" "16k"
113	rand_set_prop $fs setuid "on" "off"
114	rand_set_prop $fs snapdir "hidden" "visible"
115	if ! is_freebsd; then
116		rand_set_prop $fs xattr "on" "off"
117	fi
118	rand_set_prop $fs user:prop "aaa" "bbb" "23421" "()-+?"
119done
120
121for vol in "$POOL/vol" "$POOL/$FS/vol" ; do
122	rand_set_prop $vol checksum "on" "off" "fletcher2" "fletcher4" "sha256"
123	rand_set_prop $vol compression "${compress_prop_vals[@]}"
124	rand_set_prop $vol readonly "on" "off"
125	rand_set_prop $vol copies "1" "2" "3"
126	rand_set_prop $vol user:prop "aaa" "bbb" "23421" "()-+?"
127done
128
129
130# Verify inherited property can be received
131rand_set_prop $POOL redundant_metadata "all" "most"
132rand_set_prop $POOL sync "standard" "always" "disabled"
133
134#
135# Duplicate POOL2 for testing
136#
137log_must eval "zfs send -R $POOL@final > $BACKDIR/pool-final-R"
138log_must eval "zfs receive -d -F $POOL2 < $BACKDIR/pool-final-R"
139
140#
141# Define all the POOL/POOL2 datasets pair
142#
143set -A pair 	"$POOL" 		"$POOL2" 		\
144		"$POOL/$FS" 		"$POOL2/$FS" 		\
145		"$POOL/$FS/fs1"		"$POOL2/$FS/fs1"	\
146		"$POOL/$FS/fs1/fs2"	"$POOL2/$FS/fs1/fs2"	\
147		"$POOL/pclone"		"$POOL2/pclone"		\
148		"$POOL/$FS/fs1/fclone"	"$POOL2/$FS/fs1/fclone" \
149		"$POOL/vol"		"$POOL2/vol" 		\
150		"$POOL/$FS/vol"		"$POOL2/$FS/vol"
151
152typeset -i i=0
153while ((i < ${#pair[@]})); do
154	log_must cmp_ds_prop ${pair[$i]} ${pair[((i+1))]} nosource
155	((i += 2))
156done
157
158
159i=0
160while ((i < ${#pair[@]})); do
161	log_must cmp_ds_prop ${pair[$i]}@final ${pair[((i+1))]}@final
162	((i += 2))
163done
164
165log_pass "Verify zfs send -R will backup all the filesystem properties" \
166	"correctly."
167