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#
25# Copyright (c) 2023, Klara Inc.
26# Copyright (c) 2023, Rob Norris <robn@despairlabs.com>
27#
28
29. $STF_SUITE/include/libtest.shlib
30. $STF_SUITE/tests/functional/block_cloning/block_cloning.kshlib
31
32verify_runnable "global"
33
34claim="copy_file_range will fall back to copy when cloning on same txg"
35
36log_assert $claim
37
38typeset timeout=$(get_tunable TXG_TIMEOUT)
39
40function cleanup
41{
42	datasetexists $TESTPOOL && destroy_pool $TESTPOOL
43	set_tunable64 TXG_TIMEOUT $timeout
44	log_must restore_tunable BCLONE_WAIT_DIRTY
45}
46
47log_onexit cleanup
48
49log_must save_tunable BCLONE_WAIT_DIRTY
50
51log_must set_tunable64 TXG_TIMEOUT 5000
52
53log_must zpool create -o feature@block_cloning=enabled $TESTPOOL $DISKS
54
55log_must sync_pool $TESTPOOL true
56
57# Verify fallback to copy when there are dirty blocks
58log_must set_tunable32 BCLONE_WAIT_DIRTY 0
59
60log_must dd if=/dev/urandom of=/$TESTPOOL/file bs=128K count=4
61log_must clonefile -f /$TESTPOOL/file /$TESTPOOL/clone 0 0 524288
62
63log_must sync_pool $TESTPOOL
64
65log_must have_same_content /$TESTPOOL/file /$TESTPOOL/clone
66
67typeset blocks=$(get_same_blocks $TESTPOOL file $TESTPOOL clone)
68log_must [ "$blocks" = "" ]
69
70log_must rm /$TESTPOOL/file /$TESTPOOL/clone
71
72# Verify blocks are cloned even when there are dirty blocks
73log_must set_tunable32 BCLONE_WAIT_DIRTY 1
74
75log_must dd if=/dev/urandom of=/$TESTPOOL/file bs=128K count=4
76log_must clonefile -f /$TESTPOOL/file /$TESTPOOL/clone 0 0 524288
77
78log_must sync_pool $TESTPOOL
79
80log_must have_same_content /$TESTPOOL/file /$TESTPOOL/clone
81
82typeset blocks=$(get_same_blocks $TESTPOOL file $TESTPOOL clone)
83log_must [ "$blocks" = "0 1 2 3" ]
84
85log_pass $claim
86
87