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 by iXsystems, Inc. All rights reserved.
25#
26
27. $STF_SUITE/include/libtest.shlib
28. $STF_SUITE/tests/functional/block_cloning/block_cloning.kshlib
29
30#
31# DESCRIPTION:
32#	Test for LWB buffer overflow with multiple VDEVs ZIL when 128KB
33#	block write is split into two 68KB ones, trying to write maximum
34#	sizes 128KB TX_CLONE_RANGE record with 1022 block pointers into
35#	68KB buffer.
36#
37# STRATEGY:
38#	1. Create a pool with multiple VDEVs ZIL
39#	2. Write maximum sizes TX_CLONE_RANGE record with 1022 block
40#	   pointers into 68KB buffer
41#	3. Sync TXG
42#	4. Clone the file
43#	5. Synchronize cached writes
44#
45
46verify_runnable "global"
47
48if is_linux && [[ $(linux_version) -lt $(linux_version "4.5") ]]; then
49  log_unsupported "copy_file_range not available before Linux 4.5"
50fi
51
52VDIR=$TEST_BASE_DIR/disk-bclone
53VDEV="$VDIR/a $VDIR/b $VDIR/c"
54LDEV="$VDIR/e $VDIR/f"
55
56function cleanup
57{
58	datasetexists $TESTPOOL && destroy_pool $TESTPOOL
59	rm -rf $VDIR
60}
61
62log_onexit cleanup
63
64log_assert "Test for LWB buffer overflow with multiple VDEVs ZIL"
65
66log_must rm -rf $VDIR
67log_must mkdir -p $VDIR
68log_must truncate -s $MINVDEVSIZE $VDEV $LDEV
69
70log_must zpool create -o feature@block_cloning=enabled $TESTPOOL $VDEV \
71	log mirror $LDEV
72log_must zfs create -o recordsize=32K $TESTPOOL/$TESTFS
73# Each ZIL log entry can fit 130816 bytes for a block cloning operation,
74# so it can store 1022 block pointers. When LWB optimization is enabled,
75# an assert is hit when 128KB block write is split into two 68KB ones
76# for 2 SLOG devices
77log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS/file1 bs=32K count=1022 \
78	conv=fsync
79sync_pool $TESTPOOL
80log_must clonefile -f /$TESTPOOL/$TESTFS/file1 /$TESTPOOL/$TESTFS/file2
81log_must sync
82
83sync_pool $TESTPOOL
84log_must have_same_content /$TESTPOOL/$TESTFS/file1 /$TESTPOOL/$TESTFS/file2
85typeset blocks=$(get_same_blocks $TESTPOOL/$TESTFS file1 $TESTPOOL/$TESTFS file2)
86# FreeBSD's seq(1) leaves a trailing space, remove it with sed(1).
87log_must [ "$blocks" = "$(seq -s " " 0 1021 | sed 's/ $//')" ]
88
89log_pass "LWB buffer overflow is not triggered with multiple VDEVs ZIL"
90
91