xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/block_cloning/block_cloning_replay.ksh (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
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 cloned files. This
30#	test is ported from slog_replay tests for block cloning.
31#
32# STRATEGY:
33#	1. Create an empty 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 TESTFS <which replays the intent log>
41#	7. Compare clone file with the original file
42#
43
44verify_runnable "global"
45
46if is_linux && [[ $(linux_version) -lt $(linux_version "4.5") ]]; then
47  log_unsupported "copy_file_range not available before Linux 4.5"
48fi
49
50export VDIR=$TEST_BASE_DIR/disk-bclone
51export VDEV="$VDIR/a $VDIR/b $VDIR/c"
52export LDEV="$VDIR/e $VDIR/f"
53log_must rm -rf $VDIR
54log_must mkdir -p $VDIR
55log_must truncate -s $MINVDEVSIZE $VDEV $LDEV
56
57claim="The slogs are replayed correctly for cloned files."
58
59log_assert $claim
60
61function cleanup
62{
63	datasetexists $TESTPOOL && destroy_pool $TESTPOOL
64	rm -rf $TESTDIR $VDIR $VDIR2
65}
66
67log_onexit cleanup
68
69#
70# 1. Create an empty file system (TESTFS)
71#
72log_must zpool create -o feature@block_cloning=enabled $TESTPOOL $VDEV \
73	log mirror $LDEV
74log_must zfs create $TESTPOOL/$TESTFS
75
76#
77# 2. TX_WRITE: Create two files and sync txg
78#
79log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS/file1 \
80    oflag=sync bs=128k count=4
81log_must zfs set recordsize=16K $TESTPOOL/$TESTFS
82log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS/file2 \
83    oflag=sync bs=16K count=2048
84sync_pool $TESTPOOL
85
86#
87# 3. Checkpoint for ZIL Replay
88#
89log_must zpool freeze $TESTPOOL
90
91#
92# 4. TX_CLONE_RANGE: Clone the file
93#
94log_must clonefile -f /$TESTPOOL/$TESTFS/file1 /$TESTPOOL/$TESTFS/clone1
95log_must clonefile -f /$TESTPOOL/$TESTFS/file2 /$TESTPOOL/$TESTFS/clone2
96
97#
98# 5. Unmount filesystem and export the pool
99#
100# At this stage TESTFS is frozen, the intent log contains a complete set
101# of deltas to replay for clone files.
102#
103log_must zfs unmount /$TESTPOOL/$TESTFS
104
105log_note "Verify transactions to replay:"
106log_must zdb -iv $TESTPOOL/$TESTFS
107
108log_must zpool export $TESTPOOL
109
110#
111# 6. Remount TESTFS <which replays the intent log>
112#
113# Import the pool to unfreeze it and claim log blocks.  It has to be
114# `zpool import -f` because we can't write a frozen pool's labels!
115#
116log_must zpool import -f -d $VDIR $TESTPOOL
117
118#
119# 7. Compare clone file with the original file
120#
121log_must have_same_content /$TESTPOOL/$TESTFS/file1 /$TESTPOOL/$TESTFS/clone1
122log_must have_same_content /$TESTPOOL/$TESTFS/file2 /$TESTPOOL/$TESTFS/clone2
123
124typeset blocks=$(get_same_blocks $TESTPOOL/$TESTFS file1 \
125	$TESTPOOL/$TESTFS clone1)
126log_must [ "$blocks" = "0 1 2 3" ]
127
128typeset blocks=$(get_same_blocks $TESTPOOL/$TESTFS file2 \
129	$TESTPOOL/$TESTFS clone2)
130# FreeBSD's seq(1) leaves a trailing space, remove it with sed(1).
131log_must [ "$blocks" = "$(seq -s " " 0 2047 | sed 's/ $//')" ]
132
133log_pass $claim
134