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