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