1227825Stheraven#!/bin/ksh
2227825Stheraven
3227825Stheraven#
4227825Stheraven# This file and its contents are supplied under the terms of the
5227825Stheraven# Common Development and Distribution License ("CDDL"), version 1.0.
6227825Stheraven# You may only use this file in accordance with the terms of version
7227825Stheraven# 1.0 of the CDDL.
8227825Stheraven#
9227825Stheraven# A full copy of the text of the CDDL should have accompanied this
10227825Stheraven# source.  A copy of the CDDL is also available via the Internet at
11227825Stheraven# http://www.illumos.org/license/CDDL.
12227825Stheraven#
13227825Stheraven
14227825Stheraven#
15227825Stheraven# Copyright (c) 2019, Lawrence Livermore National Security, LLC.
16227825Stheraven# Copyright (c) 2021, George Amanakis. All rights reserved.
17227825Stheraven#
18227825Stheraven
19227825Stheraven. $STF_SUITE/include/libtest.shlib
20249998Sdim. $STF_SUITE/include/properties.shlib
21227825Stheraven. $STF_SUITE/tests/functional/rsend/rsend.kshlib
22227825Stheraven
23227825Stheraven#
24227825Stheraven# Description:
25227825Stheraven# Verify encrypted raw sending to pools with greater ashift succeeds.
26246487Stheraven#
27227825Stheraven# Strategy:
28249998Sdim# 1) Create a set of files each containing some file data in an
29227825Stheraven#	encrypted filesystem.
30227825Stheraven# 2) Snapshot and raw send these files to a pool with greater ashift
31227825Stheraven# 3) Verify that all the xattrs (and thus the spill block) were
32227825Stheraven#    preserved when receiving the incremental stream.
33227825Stheraven# 4) Repeat the test for a non-encrypted filesystem using raw send
34227825Stheraven#
35227825Stheraven
36227825Stheravenverify_runnable "both"
37227825Stheraven
38227825Stheravenlog_assert "Verify raw sending to pools with greater ashift succeeds"
39227825Stheraven
40227825Stheravenif is_freebsd; then
41227825Stheraven	log_unsupported "Runs too long on FreeBSD 14 (Issue #14961)"
42227825Stheravenfi
43227825Stheraven
44227825Stheravenfunction cleanup
45227825Stheraven{
46227825Stheraven	rm -f $BACKDIR/fs@*
47227825Stheraven	poolexists pool9 && destroy_pool pool9
48227825Stheraven	poolexists pool12 && destroy_pool pool12
49227825Stheraven	log_must rm -f $TESTDIR/vdev_a $TESTDIR/vdev_b
50227825Stheraven}
51227825Stheraven
52227825Stheravenfunction xattr_test
53227825Stheraven{
54227825Stheraven	log_must zfs set xattr=sa pool9/$1
55227825Stheraven	log_must zfs set dnodesize=legacy pool9/$1
56227825Stheraven	log_must zfs set recordsize=128k pool9/$1
57227825Stheraven	rand_set_prop pool9/$1 compression "${compress_prop_vals[@]}"
58227825Stheraven
59227825Stheraven	# Create 40 files each with a spill block containing xattrs.  Each file
60227825Stheraven	# will be modified in a different way to validate the incremental receive.
61227825Stheraven	for i in {1..40}; do
62227825Stheraven		file="/pool9/$1/file$i"
63227825Stheraven
64227825Stheraven		log_must mkfile 16384 $file
65227825Stheraven		for j in {1..20}; do
66227825Stheraven			log_must set_xattr "testattr$j" "$attrvalue" $file
67227825Stheraven		done
68227825Stheraven	done
69227825Stheraven
70227825Stheraven	# Snapshot the pool and send it to the new dataset.
71227825Stheraven	log_must zfs snapshot pool9/$1@snap1
72227825Stheraven	log_must eval "zfs send -w pool9/$1@snap1 >$BACKDIR/$1@snap1"
73227825Stheraven	log_must eval "zfs recv pool12/$1 < $BACKDIR/$1@snap1"
74227825Stheraven
75227825Stheraven	#
76227825Stheraven	# Modify file[1-6]'s contents but not the spill blocks.
77227825Stheraven	#
78227825Stheraven	# file1 - Increase record size; single block
79227825Stheraven	# file2 - Increase record size; multiple blocks
80227825Stheraven	# file3 - Truncate file to zero size; single block
81227825Stheraven	# file4 - Truncate file to smaller size; single block
82227825Stheraven	# file5 - Truncate file to much larger size; add holes
83227825Stheraven	# file6 - Truncate file to embedded size; embedded data
84227825Stheraven	#
85227825Stheraven	log_must mkfile 32768 /pool9/$1/file1
86227825Stheraven	log_must mkfile 1048576 /pool9/$1/file2
87227825Stheraven	log_must truncate -s 0 /pool9/$1/file3
88227825Stheraven	log_must truncate -s 8192 /pool9/$1/file4
89227825Stheraven	log_must truncate -s 1073741824 /pool9/$1/file5
90227825Stheraven	log_must truncate -s 50 /pool9/$1/file6
91227825Stheraven
92227825Stheraven	#
93227825Stheraven	# Modify file[11-16]'s contents and their spill blocks.
94227825Stheraven	#
95227825Stheraven	# file11 - Increase record size; single block
96227825Stheraven	# file12 - Increase record size; multiple blocks
97227825Stheraven	# file13 - Truncate file to zero size; single block
98227825Stheraven	# file14 - Truncate file to smaller size; single block
99227825Stheraven	# file15 - Truncate file to much larger size; add holes
100227825Stheraven	# file16 - Truncate file to embedded size; embedded data
101227825Stheraven	#
102227825Stheraven	log_must mkfile 32768 /pool9/$1/file11
103227825Stheraven	log_must mkfile 1048576 /pool9/$1/file12
104227825Stheraven	log_must truncate -s 0 /pool9/$1/file13
105227825Stheraven	log_must truncate -s 8192 /pool9/$1/file14
106227825Stheraven	log_must truncate -s 1073741824 /pool9/$1/file15
107227825Stheraven	log_must truncate -s 50 /pool9/$1/file16
108227825Stheraven
109227825Stheraven	for i in {11..20}; do
110227825Stheraven		log_must rm_xattr testattr1 /pool9/$1/file$i
111227825Stheraven	done
112227825Stheraven
113249998Sdim	#
114227825Stheraven	# Modify file[21-26]'s contents and remove their spill blocks.
115227825Stheraven	#
116227825Stheraven	# file21 - Increase record size; single block
117227825Stheraven	# file22 - Increase record size; multiple blocks
118227825Stheraven	# file23 - Truncate file to zero size; single block
119227825Stheraven	# file24 - Truncate file to smaller size; single block
120227825Stheraven	# file25 - Truncate file to much larger size; add holes
121227825Stheraven	# file26 - Truncate file to embedded size; embedded data
122227825Stheraven	#
123227825Stheraven	log_must mkfile 32768 /pool9/$1/file21
124227825Stheraven	log_must mkfile 1048576 /pool9/$1/file22
125227825Stheraven	log_must truncate -s 0 /pool9/$1/file23
126227825Stheraven	log_must truncate -s 8192 /pool9/$1/file24
127232950Stheraven	log_must truncate -s 1073741824 /pool9/$1/file25
128227825Stheraven	log_must truncate -s 50 /pool9/$1/file26
129227825Stheraven
130227825Stheraven	for i in {21..30}; do
131227825Stheraven		for j in {1..20}; do
132227825Stheraven			log_must rm_xattr testattr$j /pool9/$1/file$i
133227825Stheraven		done
134227825Stheraven	done
135227825Stheraven
136227825Stheraven	#
137227825Stheraven	# Modify file[31-40]'s spill blocks but not the file contents.
138227825Stheraven	#
139227825Stheraven	for i in {31..40}; do
140227825Stheraven		file="/pool9/$1/file$i"
141227825Stheraven		log_must rm_xattr testattr$(((RANDOM % 20) + 1)) $file
142227825Stheraven		log_must set_xattr testattr$(((RANDOM % 20) + 1)) "$attrvalue" $file
143232950Stheraven	done
144227825Stheraven
145232950Stheraven	# Snapshot the pool and send the incremental snapshot.
146253159Stheraven	log_must zfs snapshot pool9/$1@snap2
147227825Stheraven	log_must eval "zfs send -w -i pool9/$1@snap1 pool9/$1@snap2 >$BACKDIR/$1@snap2"
148241903Sdim	log_must eval "zfs recv pool12/$1 < $BACKDIR/$1@snap2"
149227825Stheraven}
150241903Sdim
151241903Sdimattrvalue="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
152241903Sdim
153227825Stheravenlog_onexit cleanup
154227825Stheraven
155227825Stheraven# Create pools
156227825Stheraventruncate -s $MINVDEVSIZE $TESTDIR/vdev_a
157227825Stheraventruncate -s $MINVDEVSIZE $TESTDIR/vdev_b
158227825Stheravenlog_must zpool create -f -o ashift=9 pool9 $TESTDIR/vdev_a
159227825Stheravenlog_must zpool create -f -o ashift=12 pool12 $TESTDIR/vdev_b
160227825Stheraven
161227825Stheraven# Create encrypted fs
162227825Stheravenlog_must eval "echo 'password' | zfs create -o encryption=on" \
163227825Stheraven	"-o keyformat=passphrase -o keylocation=prompt " \
164227825Stheraven	"pool9/encfs"
165227825Stheraven
166227825Stheraven# Run xattr tests for encrypted fs
167227825Stheravenxattr_test encfs
168227825Stheraven
169232950Stheraven# Calculate the expected recursive checksum for source encrypted fs
170227825Stheravenexpected_cksum=$(recursive_cksum /pool9/encfs)
171253159Stheraven
172253159Stheraven# Mount target encrypted fs
173227825Stheravenlog_must eval "echo 'password' | zfs load-key pool12/encfs"
174241903Sdimlog_must zfs mount pool12/encfs
175227825Stheraven
176241903Sdim# Validate the received copy using the received recursive checksum
177241903Sdimactual_cksum=$(recursive_cksum /pool12/encfs)
178241903Sdimif [[ "$expected_cksum" != "$actual_cksum" ]]; then
179227825Stheraven	log_fail "Checksums differ ($expected_cksum != $actual_cksum)"
180227825Stheravenfi
181227825Stheraven
182227825Stheraven# Perform the same test but without encryption (send -w)
183227825Stheravenlog_must zfs create pool9/fs
184227825Stheraven
185227825Stheraven# Run xattr tests for non-encrypted fs
186227825Stheravenxattr_test fs
187227825Stheraven
188227825Stheraven# Calculate the expected recursive checksum for source non-encrypted fs
189227825Stheravenexpected_cksum=$(recursive_cksum /pool9/fs)
190227825Stheraven
191232950Stheraven# Validate the received copy using the received recursive checksum
192227825Stheravenactual_cksum=$(recursive_cksum /pool12/fs)
193227825Stheravenif [[ "$expected_cksum" != "$actual_cksum" ]]; then
194227825Stheraven	log_fail "Checksums differ ($expected_cksum != $actual_cksum)"
195227825Stheravenfi
196227825Stheraven
197227825Stheravenlog_pass "Verify raw sending to pools with greater ashift succeeds"
198227825Stheraven