1#!/usr/bin/ksh
2
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2015 by Delphix. All rights reserved.
16#
17
18. $STF_SUITE/tests/functional/rsend/rsend.kshlib
19
20#
21# Description:
22# Verify compressed send works correctly with datasets of varying recsize.
23#
24# Strategy:
25# 1. Check the recv behavior (into pools with features enabled and disabled)
26#    of all combinations of -c -p and -L. Verify the stream is compressed,
27#    and that the recsize property and that of a received file is correct
28#    according to this matrix:
29#
30# +---------+--------+------------+------------+-----------+-----------+
31# | send    | send   | received   | received   | received  | received  |
32# | stream  | stream | file bs    | prop       | file bs   | props     |
33# | recsize | flags  | (disabled) | (disabled) | (enabled) | (enabled) |
34# +---------+--------+------------+------------+-----------+-----------+
35# |    128k |        |       128k |       128k |      128k |      128k |
36# |    128k |     -c |      Fails |      Fails |      128k |      128k |
37# |    128k |     -p |       128k |       128k |      128k |      128k |
38# |    128k |     -L |       128k |       128k |      128k |      128k |
39# |    128k |    -cp |      Fails |      Fails |      128k |      128k |
40# |    128k |    -cL |      Fails |      Fails |      128k |      128k |
41# |    128k |    -pL |       128k |       128k |      128k |      128k |
42# |    128k |   -cpL |      Fails |      Fails |      128k |      128k |
43# |      1m |        |      Fails |      Fails |      128k |      128k |
44# |      1m |     -c |      Fails |      Fails |      128k |      128k |
45# |      1m |     -p |       128k |       128k |      128k |        1m |
46# |      1m |     -L |      Fails |      Fails |        1m |      128k |
47# |      1m |    -cp |      Fails |      Fails |      128k |        1m |
48# |      1m |    -cL |      Fails |      Fails |        1m |      128k |
49# |      1m |    -pL |      Fails |      Fails |        1m |        1m |
50# |      1m |   -cpL |      Fails |      Fails |        1m |        1m |
51# +---------+--------+------------+------------+-----------+-----------+
52#
53
54verify_runnable "both"
55
56function cleanup
57{
58	datasetexists $TESTPOOL/128k && log_must zfs destroy $TESTPOOL/128k
59	datasetexists $TESTPOOL/1m && log_must zfs destroy $TESTPOOL/1m
60	cleanup_pool $POOL2
61	destroy_pool $POOL3
62}
63
64# For a received stream, verify the recsize (prop and file) match expectations.
65function check_recsize
66{
67	typeset recv_ds=$1
68	typeset expected_file_bs=$2
69	typeset expected_recsize=$3
70	typeset file="$(get_prop mountpoint $recv_ds)/testfile"
71
72	[[ -f $file ]] || log_fail "file '$file' doesn't exist"
73
74	typeset read_recsize=$(get_prop recsize $recv_ds)
75	typeset read_file_bs=$(stat $file | sed -n \
76	    's/.*IO Block: \([0-9]*\).*/\1/p')
77
78	[[ $read_recsize = $expected_recsize ]] || log_fail \
79	    "read_recsize: $read_recsize expected_recsize: $expected_recsize"
80	[[ $read_file_bs = $expected_file_bs ]] || log_fail \
81	    "read_file_bs: $read_file_bs expected_file_bs: $expected_file_bs"
82}
83
84#
85# This function does a zfs send and receive according to the parameters
86# below, and verifies the data shown in the strategy section.
87#
88# -[cpL] flags to pass through to 'zfs send'
89# -d Receive into a pool with all features disabled
90#
91# $1 The recordsize of the send dataset
92# $2 Whether or not the recv should work.
93# $3 The blocksize expected in a received file (default 128k)
94# $4 The recordsize property expected in a received dataset (default 128k)
95#
96function check
97{
98	typeset recv_pool=$POOL2
99	typeset flags='-'
100
101	while getopts "cdpL" opt; do
102		case $opt in
103		c)
104			flags+='c'
105			;;
106		d)
107			recv_pool=$POOL3
108			;;
109		p)
110			flags+='p'
111			;;
112		L)
113			flags+='L'
114			;;
115		esac
116	done
117	shift $(($OPTIND - 1))
118	[[ ${#flags} -eq 1 ]] && flags=''
119
120	typeset recsize=$1
121	typeset verify=$2
122	typeset expected_file_bs=${3-131072}
123	typeset expected_recsize=${4-131072}
124	typeset send_ds=$TESTPOOL/$recsize
125	typeset send_snap=$send_ds@snap
126	typeset recv_ds=$recv_pool/$recsize
127	typeset stream=$BACKDIR/stream.out
128
129	datasetexists $send_ds || log_fail "send ds: $send_ds doesn't exist"
130	[[ -f $stream ]] && log_must rm $stream
131	log_must eval "zfs send $flags $send_snap >$stream"
132	$verify eval "zfs recv $recv_ds <$stream"
133	typeset stream_size=$(cat $stream | zstreamdump | sed -n \
134	    's/	Total write size = \(.*\) (0x.*)/\1/p')
135
136	#
137	# Special case: For a send dataset with large blocks, don't try to
138	# verify the stream size is correct if the compress flag is present
139	# but the large blocks flag isn't. In these cases, the user data
140	# isn't compressed in the stream (though metadata is) so the
141	# verification would fail.
142	#
143	typeset do_size_test=true
144	[[ $recsize = $large && $flags =~ 'c' && ! $flags =~ 'L' ]] && \
145	    do_size_test=false
146
147	$do_size_test && verify_stream_size $stream $send_ds
148
149	if [[ $verify = "log_mustnot" ]]; then
150		datasetnonexists $recv_ds || log_fail "$recv_ds shouldn't exist"
151		return
152	fi
153
154	check_recsize $recv_ds $expected_file_bs $expected_recsize
155	$do_size_test && verify_stream_size $stream $recv_ds
156	log_must zfs destroy -r $recv_ds
157}
158
159log_assert "Verify compressed send works with datasets of varying recsize."
160log_onexit cleanup
161typeset recsize opts dir
162typeset small=$((128 * 1024))
163typeset large=$((1024 * 1024))
164
165# Create POOL3 with features disabled and datasets to create test send streams
166log_must zpool create -d $POOL3 $DISK3
167write_compressible $BACKDIR 32m
168for recsize in $small $large; do
169	log_must zfs create -o compress=gzip -o recsize=$recsize \
170	    $TESTPOOL/$recsize
171	dir=$(get_prop mountpoint $TESTPOOL/$recsize)
172	log_must cp $BACKDIR/file.0 $dir/testfile
173	log_must zfs snapshot $TESTPOOL/$recsize@snap
174done
175
176# Run tests for send streams without large blocks
177for opts in '' -d -c -p -dp -L -dL -cp -cL -pL -dpL -cpL; do
178	check $opts $small log_must
179done
180for opts in -dc -dcp -dcL -dcpL; do
181	check $opts $small log_mustnot
182done
183
184# Run tests for send streams with large blocks
185for opts in '' -d -dp -c; do
186	check $opts $large log_must
187done
188for opts in -dc -dL -dcp -dcL -dpL -dcpL; do
189	check $opts $large log_mustnot
190done
191check -p $large log_must $small $large
192check -L $large log_must $large $small
193check -cp $large log_must $small $large
194check -cL $large log_must $large $small
195check -pL $large log_must $large $large
196check -cpL $large log_must $large $large
197
198log_pass "Compressed send works with datasets of varying recsize."
199