1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
3#
4# CDDL HEADER START
5#
6# The contents of this file are subject to the terms of the
7# Common Development and Distribution License (the "License").
8# You may not use this file except in compliance with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or https://opensource.org/licenses/CDDL-1.0.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23
24. $STF_SUITE/include/libtest.shlib
25. $STF_SUITE/tests/functional/block_cloning/block_cloning.kshlib
26
27#
28# DESCRIPTION:
29#	Verify slogs are replayed correctly for encrypted cloned files.
30#	This test is ported from slog_replay tests for block cloning.
31#
32# STRATEGY:
33#	1. Create an encrypted file system (TESTFS)
34#	2. Create regular files and sync
35#	3. Freeze TESTFS
36#	4. Clone the file
37#	5. Unmount filesystem
38#	   <At this stage TESTFS is frozen, the intent log contains a
39#	   complete set of deltas to replay it>
40#	6. Remount encrypted TESTFS <which replays the intent log>
41#	7. Compare clone file with the original file
42#
43
44verify_runnable "global"
45
46export VDIR=$TEST_BASE_DIR/disk-bclone
47export VDEV="$VDIR/a $VDIR/b $VDIR/c"
48export LDEV="$VDIR/e $VDIR/f"
49log_must rm -rf $VDIR
50log_must mkdir -p $VDIR
51log_must truncate -s $MINVDEVSIZE $VDEV $LDEV
52export PASSPHRASE="password"
53
54claim="The slogs are replayed correctly for encrypted cloned files."
55
56log_assert $claim
57
58function cleanup
59{
60	datasetexists $TESTPOOL && destroy_pool $TESTPOOL
61	rm -rf $TESTDIR $VDIR $VDIR2
62}
63
64log_onexit cleanup
65
66#
67# 1. Create an encrypted file system (TESTFS)
68#
69log_must zpool create -o feature@block_cloning=enabled $TESTPOOL $VDEV \
70	log mirror $LDEV
71log_must eval "echo $PASSPHRASE | zfs create -o encryption=on" \
72	"-o keyformat=passphrase -o keylocation=prompt $TESTPOOL/$TESTFS"
73
74#
75# 2. TX_WRITE: Create two files and sync txg
76#
77log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS/file1 \
78    oflag=sync bs=128k count=4
79log_must zfs set recordsize=16K $TESTPOOL/$TESTFS
80log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS/file2 \
81    oflag=sync bs=16K count=2048
82sync_pool $TESTPOOL
83
84#
85# 3. Checkpoint for ZIL Replay
86#
87log_must zpool freeze $TESTPOOL
88
89#
90# 4. TX_CLONE_RANGE: Clone the file
91#
92log_must clonefile -f /$TESTPOOL/$TESTFS/file1 /$TESTPOOL/$TESTFS/clone1
93log_must clonefile -f /$TESTPOOL/$TESTFS/file2 /$TESTPOOL/$TESTFS/clone2
94
95#
96# 5. Unmount filesystem and export the pool
97#
98# At this stage TESTFS is frozen, the intent log contains a complete set
99# of deltas to replay for clone files.
100#
101log_must zfs unmount /$TESTPOOL/$TESTFS
102
103log_note "Verify transactions to replay:"
104log_must zdb -iv $TESTPOOL/$TESTFS
105
106log_must zpool export $TESTPOOL
107
108#
109# 6. Remount TESTFS <which replays the intent log>
110#
111# Import the pool to unfreeze it and claim log blocks.  It has to be
112# `zpool import -f` because we can't write a frozen pool's labels!
113#
114log_must eval "echo $PASSPHRASE | zpool import -l -f -d $VDIR $TESTPOOL"
115
116#
117# 7. Compare clone file with the original file
118#
119log_must have_same_content /$TESTPOOL/$TESTFS/file1 /$TESTPOOL/$TESTFS/clone1
120log_must have_same_content /$TESTPOOL/$TESTFS/file2 /$TESTPOOL/$TESTFS/clone2
121
122typeset blocks=$(get_same_blocks $TESTPOOL/$TESTFS file1 \
123	$TESTPOOL/$TESTFS clone1 $PASSPHRASE)
124log_must [ "$blocks" = "0 1 2 3" ]
125
126typeset blocks=$(get_same_blocks $TESTPOOL/$TESTFS file2 \
127	$TESTPOOL/$TESTFS clone2 $PASSPHRASE)
128# FreeBSD's seq(1) leaves a trailing space, remove it with sed(1).
129log_must [ "$blocks" = "$(seq -s " " 0 2047 | sed 's/ $//')" ]
130
131log_pass $claim
132