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#	When the destination file is mmaped and is already cached we need to
29#	update mmaped pages after successful clone.
30#
31# STRATEGY:
32#	1. Create a pool.
33#	2. Create a two test files with random content.
34#	3. mmap the files, read them and clone from one to the other using
35#	   clone_mmap_cached.
36#	4. clone_mmap_cached also verifies if the content of the destination
37#	   file was updated while reading it from mmaped memory.
38#
39
40verify_runnable "global"
41
42if is_linux && [[ $(linux_version) -lt $(linux_version "4.5") ]]; then
43  log_unsupported "copy_file_range not available before Linux 4.5"
44fi
45
46VDIR=$TEST_BASE_DIR/disk-bclone
47VDEV="$VDIR/a"
48
49function cleanup
50{
51	datasetexists $TESTPOOL && destroy_pool $TESTPOOL
52	rm -rf $VDIR
53}
54
55log_onexit cleanup
56
57log_assert "Test for clone into mmaped and cached file"
58
59log_must rm -rf $VDIR
60log_must mkdir -p $VDIR
61log_must truncate -s 1G $VDEV
62
63log_must zpool create -o feature@block_cloning=enabled $TESTPOOL $VDEV
64log_must zfs create $TESTPOOL/$TESTFS
65
66for opts in "--" "-i" "-o" "-io"
67do
68	log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS/src bs=1M count=1
69	log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS/dst bs=1M count=1
70
71	# Clear cache.
72	log_must zpool export $TESTPOOL
73	log_must zpool import -d $VDIR $TESTPOOL
74
75	log_must clone_mmap_cached $opts /$TESTPOOL/$TESTFS/src /$TESTPOOL/$TESTFS/dst
76
77	sync_pool $TESTPOOL
78	log_must sync
79
80	log_must have_same_content /$TESTPOOL/$TESTFS/src /$TESTPOOL/$TESTFS/dst
81	blocks=$(get_same_blocks $TESTPOOL/$TESTFS src $TESTPOOL/$TESTFS dst)
82	# FreeBSD's seq(1) leaves a trailing space, remove it with sed(1).
83	log_must [ "$blocks" = "$(seq -s " " 0 7 | sed 's/ $//')" ]
84done
85
86log_pass "Clone properly updates mmapped and cached pages"
87