1#!/bin/ksh
2
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2019, Lawrence Livermore National Security, LLC.
16# Copyright (c) 2021, George Amanakis. All rights reserved.
17#
18
19. $STF_SUITE/include/libtest.shlib
20. $STF_SUITE/include/properties.shlib
21. $STF_SUITE/tests/functional/rsend/rsend.kshlib
22
23#
24# Description:
25# Verify encrypted raw sending to pools with greater ashift succeeds.
26#
27# Strategy:
28# 1) Create a set of files each containing some file data in an
29#	encrypted filesystem.
30# 2) Snapshot and raw send these files to a pool with greater ashift
31# 3) Verify that all the xattrs (and thus the spill block) were
32#    preserved when receiving the incremental stream.
33# 4) Repeat the test for a non-encrypted filesystem using raw send
34#
35
36verify_runnable "both"
37
38log_assert "Verify raw sending to pools with greater ashift succeeds"
39
40function cleanup
41{
42	rm -f $BACKDIR/fs@*
43	poolexists pool9 && destroy_pool pool9
44	poolexists pool12 && destroy_pool pool12
45	log_must rm -f $TESTDIR/vdev_a $TESTDIR/vdev_b
46}
47
48function xattr_test
49{
50	log_must zfs set xattr=sa pool9/$1
51	log_must zfs set dnodesize=legacy pool9/$1
52	log_must zfs set recordsize=128k pool9/$1
53	rand_set_prop pool9/$1 compression "${compress_prop_vals[@]}"
54
55	# Create 40 files each with a spill block containing xattrs.  Each file
56	# will be modified in a different way to validate the incremental receive.
57	for i in {1..40}; do
58		file="/pool9/$1/file$i"
59
60		log_must mkfile 16384 $file
61		for j in {1..20}; do
62			log_must set_xattr "testattr$j" "$attrvalue" $file
63		done
64	done
65
66	# Snapshot the pool and send it to the new dataset.
67	log_must zfs snapshot pool9/$1@snap1
68	log_must eval "zfs send -w pool9/$1@snap1 >$BACKDIR/$1@snap1"
69	log_must eval "zfs recv pool12/$1 < $BACKDIR/$1@snap1"
70
71	#
72	# Modify file[1-6]'s contents but not the spill blocks.
73	#
74	# file1 - Increase record size; single block
75	# file2 - Increase record size; multiple blocks
76	# file3 - Truncate file to zero size; single block
77	# file4 - Truncate file to smaller size; single block
78	# file5 - Truncate file to much larger size; add holes
79	# file6 - Truncate file to embedded size; embedded data
80	#
81	log_must mkfile 32768 /pool9/$1/file1
82	log_must mkfile 1048576 /pool9/$1/file2
83	log_must truncate -s 0 /pool9/$1/file3
84	log_must truncate -s 8192 /pool9/$1/file4
85	log_must truncate -s 1073741824 /pool9/$1/file5
86	log_must truncate -s 50 /pool9/$1/file6
87
88	#
89	# Modify file[11-16]'s contents and their spill blocks.
90	#
91	# file11 - Increase record size; single block
92	# file12 - Increase record size; multiple blocks
93	# file13 - Truncate file to zero size; single block
94	# file14 - Truncate file to smaller size; single block
95	# file15 - Truncate file to much larger size; add holes
96	# file16 - Truncate file to embedded size; embedded data
97	#
98	log_must mkfile 32768 /pool9/$1/file11
99	log_must mkfile 1048576 /pool9/$1/file12
100	log_must truncate -s 0 /pool9/$1/file13
101	log_must truncate -s 8192 /pool9/$1/file14
102	log_must truncate -s 1073741824 /pool9/$1/file15
103	log_must truncate -s 50 /pool9/$1/file16
104
105	for i in {11..20}; do
106		log_must rm_xattr testattr1 /pool9/$1/file$i
107	done
108
109	#
110	# Modify file[21-26]'s contents and remove their spill blocks.
111	#
112	# file21 - Increase record size; single block
113	# file22 - Increase record size; multiple blocks
114	# file23 - Truncate file to zero size; single block
115	# file24 - Truncate file to smaller size; single block
116	# file25 - Truncate file to much larger size; add holes
117	# file26 - Truncate file to embedded size; embedded data
118	#
119	log_must mkfile 32768 /pool9/$1/file21
120	log_must mkfile 1048576 /pool9/$1/file22
121	log_must truncate -s 0 /pool9/$1/file23
122	log_must truncate -s 8192 /pool9/$1/file24
123	log_must truncate -s 1073741824 /pool9/$1/file25
124	log_must truncate -s 50 /pool9/$1/file26
125
126	for i in {21..30}; do
127		for j in {1..20}; do
128			log_must rm_xattr testattr$j /pool9/$1/file$i
129		done
130	done
131
132	#
133	# Modify file[31-40]'s spill blocks but not the file contents.
134	#
135	for i in {31..40}; do
136		file="/pool9/$1/file$i"
137		log_must rm_xattr testattr$(((RANDOM % 20) + 1)) $file
138		log_must set_xattr testattr$(((RANDOM % 20) + 1)) "$attrvalue" $file
139	done
140
141	# Snapshot the pool and send the incremental snapshot.
142	log_must zfs snapshot pool9/$1@snap2
143	log_must eval "zfs send -w -i pool9/$1@snap1 pool9/$1@snap2 >$BACKDIR/$1@snap2"
144	log_must eval "zfs recv pool12/$1 < $BACKDIR/$1@snap2"
145}
146
147attrvalue="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
148
149log_onexit cleanup
150
151# Create pools
152truncate -s $MINVDEVSIZE $TESTDIR/vdev_a
153truncate -s $MINVDEVSIZE $TESTDIR/vdev_b
154log_must zpool create -f -o ashift=9 pool9 $TESTDIR/vdev_a
155log_must zpool create -f -o ashift=12 pool12 $TESTDIR/vdev_b
156
157# Create encrypted fs
158log_must eval "echo 'password' | zfs create -o encryption=on" \
159	"-o keyformat=passphrase -o keylocation=prompt " \
160	"pool9/encfs"
161
162# Run xattr tests for encrypted fs
163xattr_test encfs
164
165# Calculate the expected recursive checksum for source encrypted fs
166expected_cksum=$(recursive_cksum /pool9/encfs)
167
168# Mount target encrypted fs
169log_must eval "echo 'password' | zfs load-key pool12/encfs"
170log_must zfs mount pool12/encfs
171
172# Validate the received copy using the received recursive checksum
173actual_cksum=$(recursive_cksum /pool12/encfs)
174if [[ "$expected_cksum" != "$actual_cksum" ]]; then
175	log_fail "Checksums differ ($expected_cksum != $actual_cksum)"
176fi
177
178# Perform the same test but without encryption (send -w)
179log_must zfs create pool9/fs
180
181# Run xattr tests for non-encrypted fs
182xattr_test fs
183
184# Calculate the expected recursive checksum for source non-encrypted fs
185expected_cksum=$(recursive_cksum /pool9/fs)
186
187# Validate the received copy using the received recursive checksum
188actual_cksum=$(recursive_cksum /pool12/fs)
189if [[ "$expected_cksum" != "$actual_cksum" ]]; then
190	log_fail "Checksums differ ($expected_cksum != $actual_cksum)"
191fi
192
193log_pass "Verify raw sending to pools with greater ashift succeeds"
194