1#!/bin/ksh -p
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. $STF_SUITE/include/properties.shlib
20
21#
22# Description:
23# Verify that compressed streams can contain embedded blocks.
24#
25# Strategy:
26# 1. Create a filesystem with compressible data and embedded blocks.
27# 2. Verify the created streams can be received correctly.
28# 3. Verify the presence / absence of embedded blocks in the compressed stream,
29#    as well as the receiving file system.
30#
31
32verify_runnable "both"
33
34log_assert "Verify that compressed streams can contain embedded blocks."
35log_onexit cleanup_pool $POOL2
36
37typeset objs obj recsize
38typeset sendfs=$POOL2/sendfs
39typeset recvfs=$POOL2/recvfs
40typeset stream=$BACKDIR/stream
41typeset dump=$BACKDIR/dump
42typeset recvfs2=$POOL2/recvfs2
43typeset stream2=$BACKDIR/stream2
44typeset dump2=$BACKDIR/dump2
45log_must zfs create -o compress=lz4 $sendfs
46log_must zfs create -o compress=lz4 $recvfs
47log_must zfs create -o compress=lz4 $recvfs2
48typeset dir=$(get_prop mountpoint $sendfs)
49
50# Populate the send dataset with compressible data and embedded block files.
51write_compressible $dir 16m
52for recsize in "${recsize_prop_vals[@]}"; do
53	# For lz4, this method works for blocks up to 16k, but not larger
54	[[ $recsize -eq $((32 * 1024)) ]] && break
55
56	if is_linux || is_freebsd; then
57		log_must truncate -s $recsize $dir/$recsize
58		log_must dd if=/dev/urandom of=$dir/$recsize \
59		    seek=$((recsize - 8)) bs=1 count=8 conv=notrunc
60	else
61		log_must mkholes -h 0:$((recsize - 8)) -d $((recsize - 8)):8 \
62		    $dir/$recsize
63	fi
64done
65
66# Generate the streams and zstream dump output.
67log_must zfs snapshot $sendfs@now
68log_must eval "zfs send -c $sendfs@now >$stream"
69log_must eval "zstream dump -v <$stream >$dump"
70log_must eval "zfs recv -d $recvfs <$stream"
71cmp_ds_cont $sendfs $recvfs
72verify_stream_size $stream $sendfs
73log_mustnot stream_has_features $stream embed_data
74
75log_must eval "zfs send -c -e $sendfs@now >$stream2"
76log_must eval "zstream dump -v <$stream2 >$dump2"
77log_must eval "zfs recv -d $recvfs2 <$stream2"
78cmp_ds_cont $sendfs $recvfs2
79verify_stream_size $stream2 $sendfs
80log_must stream_has_features $stream2 embed_data
81
82# Verify embedded blocks are present only when expected.
83for recsize in "${recsize_prop_vals[@]}"; do
84	[[ $recsize -eq $((32 * 1024)) ]] && break
85
86	typeset send_obj=$(get_objnum $(get_prop mountpoint $sendfs)/$recsize)
87	typeset recv_obj=$(get_objnum \
88	    $(get_prop mountpoint $recvfs/sendfs)/$recsize)
89	typeset recv2_obj=$(get_objnum \
90	    $(get_prop mountpoint $recvfs2/sendfs)/$recsize)
91
92	log_must eval "zdb -ddddd $sendfs $send_obj >$BACKDIR/sendfs.zdb"
93	log_must eval "zdb -ddddd $recvfs/sendfs $recv_obj >$BACKDIR/recvfs.zdb"
94	log_must eval "zdb -ddddd $recvfs2/sendfs $recv2_obj >$BACKDIR/recvfs2.zdb"
95
96	grep -q "EMBEDDED" $BACKDIR/sendfs.zdb || \
97	    log_fail "Obj $send_obj not embedded in $sendfs"
98	grep -q "EMBEDDED" $BACKDIR/recvfs.zdb || \
99	    log_fail "Obj $recv_obj not embedded in $recvfs"
100	grep -q "EMBEDDED" $BACKDIR/recvfs2.zdb || \
101	    log_fail "Obj $recv2_obj not embedded in $recvfs2"
102
103	grep -q "WRITE_EMBEDDED object = $send_obj offset = 0" $dump && \
104	    log_fail "Obj $obj embedded in zstream dump output"
105	grep -q "WRITE_EMBEDDED object = $send_obj offset = 0" $dump2 || \
106	    log_fail "Obj $obj not embedded in zstream dump output"
107done
108
109log_pass "Compressed streams can contain embedded blocks."
110