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# Use is subject to license terms.
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 incremental receives handle dnode reallocation.
26
27# Strategy:
28# 1. Create a pool containing an encrypted filesystem.
29# 2. Use 'zfs send -wp' to perform a raw send of the initial filesystem.
30# 3. Repeat the following steps N times to verify raw incremental receives.
31#   a) Randomly change several key dataset properties.
32#   b) Modify the contents of the filesystem such that dnode reallocation
33#      is likely during the 'zfs receive', and receive_object() exercises
34#      as much of its functionality as possible.
35#   c) Create a new snapshot and generate an raw incremental stream.
36#   d) Receive the raw incremental stream and verify the received contents.
37#   e) Destroy the incremental stream and old snapshot.
38#
39
40verify_runnable "both"
41
42log_assert "Verify encrypted raw incremental receive handles reallocation"
43
44function cleanup
45{
46	rm -f $BACKDIR/fs@*
47	rm -f $keyfile
48	destroy_dataset $POOL/fs "-rR"
49	destroy_dataset $POOL/newfs "-rR"
50}
51
52log_onexit cleanup
53
54typeset keyfile=/$TESTPOOL/pkey
55
56# Create an encrypted dataset
57log_must eval "echo 'password' > $keyfile"
58log_must zfs create -o encryption=on -o keyformat=passphrase \
59    -o keylocation=file://$keyfile $POOL/fs
60
61last_snap=1
62log_must zfs snapshot $POOL/fs@snap${last_snap}
63log_must eval "zfs send -wp $POOL/fs@snap${last_snap} \
64    >$BACKDIR/fs@snap${last_snap}"
65log_must eval "zfs recv $POOL/newfs < $BACKDIR/fs@snap${last_snap}"
66
67# Set atime=off to prevent the recursive_cksum from modifying newfs.
68log_must zfs set atime=off $POOL/newfs
69
70if is_kmemleak; then
71	# Use fewer files and passes on debug kernels
72	# to avoid timeout due to reduced performance.
73	nr_files=100
74	passes=2
75elif is_freebsd; then
76	# Use fewer files and passes on FreeBSD to avoid timeout.
77	nr_files=500
78	passes=2
79else
80	nr_files=1000
81	passes=3
82fi
83
84for i in {1..$passes}; do
85	# Randomly modify several dataset properties in order to generate
86	# more interesting incremental send streams.
87	rand_set_prop $POOL/fs checksum "off" "fletcher4" "sha256"
88	rand_set_prop $POOL/fs compression "${compress_prop_vals[@]}"
89	rand_set_prop $POOL/fs recordsize "32K" "128K"
90	rand_set_prop $POOL/fs dnodesize "legacy" "auto" "4k"
91	rand_set_prop $POOL/fs xattr "on" "sa"
92
93	# Churn the filesystem in such a way that we're likely to be both
94	# allocating and reallocating objects in the incremental stream.
95	log_must churn_files $nr_files 524288 $POOL/fs
96	expected_cksum=$(recursive_cksum /$POOL/fs)
97
98	# Create a snapshot and use it to send an incremental stream.
99	this_snap=$((last_snap + 1))
100	log_must zfs snapshot $POOL/fs@snap${this_snap}
101	log_must eval "zfs send -wp -i $POOL/fs@snap${last_snap} \
102	    $POOL/fs@snap${this_snap} > $BACKDIR/fs@snap${this_snap}"
103
104	# Receive the incremental stream and verify the received contents.
105	log_must eval "zfs recv -Fu $POOL/newfs < $BACKDIR/fs@snap${this_snap}"
106
107	log_must zfs load-key $POOL/newfs
108	log_must zfs mount $POOL/newfs
109	actual_cksum=$(recursive_cksum /$POOL/newfs)
110	log_must zfs umount $POOL/newfs
111	log_must zfs unload-key $POOL/newfs
112
113	if [[ "$expected_cksum" != "$actual_cksum" ]]; then
114		log_fail "Checksums differ ($expected_cksum != $actual_cksum)"
115	fi
116
117	# Destroy the incremental stream and old snapshot.
118	rm -f $BACKDIR/fs@snap${last_snap}
119	log_must zfs destroy $POOL/fs@snap${last_snap}
120	log_must zfs destroy $POOL/newfs@snap${last_snap}
121	last_snap=$this_snap
122done
123
124log_pass "Verify encrypted raw incremental receive handles reallocation"
125