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#
24# Copyright (c) 2023, Klara Inc.
25# Copyright (c) 2023, Rob Norris <robn@despairlabs.com>
26#
27
28. $STF_SUITE/include/libtest.shlib
29. $STF_SUITE/tests/functional/block_cloning/block_cloning.kshlib
30
31verify_runnable "global"
32
33if is_linux && [[ $(linux_version) -lt $(linux_version "4.5") ]]; then
34  log_unsupported "copy_file_range not available before Linux 4.5"
35fi
36
37claim="copy_file_range will fall back to copy when cloning not possible."
38
39log_assert $claim
40
41function cleanup
42{
43	datasetexists $TESTPOOL && destroy_pool $TESTPOOL
44}
45
46log_onexit cleanup
47
48log_must zpool create -o feature@block_cloning=enabled $TESTPOOL $DISKS
49
50log_must dd if=/dev/urandom of=/$TESTPOOL/file bs=128K count=4
51log_must sync_pool $TESTPOOL
52
53
54log_note "Copying entire file with copy_file_range"
55
56log_must clonefile -f /$TESTPOOL/file /$TESTPOOL/clone 0 0 524288
57log_must sync_pool $TESTPOOL
58
59log_must have_same_content /$TESTPOOL/file /$TESTPOOL/clone
60
61typeset blocks=$(get_same_blocks $TESTPOOL file $TESTPOOL clone)
62log_must [ "$blocks" = "0 1 2 3" ]
63
64
65log_note "Copying within a block with copy_file_range"
66
67log_must clonefile -f /$TESTPOOL/file /$TESTPOOL/clone 32768 32768 65536
68log_must sync_pool $TESTPOOL
69
70log_must have_same_content /$TESTPOOL/file /$TESTPOOL/clone
71
72typeset blocks=$(get_same_blocks $TESTPOOL file $TESTPOOL clone)
73log_must [ "$blocks" = "1 2 3" ]
74
75
76log_note "Copying across a block with copy_file_range"
77
78log_must clonefile -f /$TESTPOOL/file /$TESTPOOL/clone 327680 327680 131072
79log_must sync_pool $TESTPOOL
80
81log_must have_same_content /$TESTPOOL/file /$TESTPOOL/clone
82
83typeset blocks=$(get_same_blocks $TESTPOOL file $TESTPOOL clone)
84log_must [ "$blocks" = "1" ]
85
86log_pass $claim
87