1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2018 by Datto Inc. All rights reserved.
19#
20
21. $STF_SUITE/tests/functional/rsend/rsend.kshlib
22
23#
24# DESCRIPTION:
25#
26#
27# STRATEGY:
28# 1. Create a new encrypted filesystem
29# 2. Add a 4 files that are to be truncated later
30# 3. Take a snapshot of the filesystem
31# 4. Truncate one of the files from 32M to 128k
32# 5. Truncate one of the files from 512k to 384k
33# 6. Truncate one of the files from 512k to 0 to 384k via reallocation
34# 7. Truncate one of the files from 1k to 0 to 512b via reallocation
35# 8. Take another snapshot of the filesystem
36# 9. Send and receive both snapshots
37# 10. Mount the filesystem and check the contents
38#
39
40verify_runnable "both"
41
42function cleanup
43{
44	datasetexists $TESTPOOL/$TESTFS2 && \
45		destroy_dataset $TESTPOOL/$TESTFS2 -r
46	datasetexists $TESTPOOL/recv && \
47		destroy_dataset $TESTPOOL/recv -r
48	[[ -f $keyfile ]] && log_must rm $keyfile
49	[[ -f $sendfile ]] && log_must rm $sendfile
50}
51log_onexit cleanup
52
53function recursive_cksum
54{
55	case "$(uname)" in
56	FreeBSD)
57		find $1 -type f -exec sha256 -q {} + | \
58		    sort | sha256digest
59		;;
60	*)
61		find $1 -type f -exec sha256sum {} + | \
62		    sort -k 2 | awk '{ print $1 }' | sha256digest
63		;;
64	esac
65}
66
67log_assert "Verify 'zfs send -w' works with many different file layouts"
68
69typeset keyfile=/$TESTPOOL/pkey
70typeset sendfile=/$TESTPOOL/sendfile
71typeset sendfile2=/$TESTPOOL/sendfile2
72
73# Create an encrypted dataset
74log_must eval "echo 'password' > $keyfile"
75log_must zfs create -o encryption=on -o keyformat=passphrase \
76	-o keylocation=file://$keyfile $TESTPOOL/$TESTFS2
77
78# Explicitly set the recordsize since the truncation sizes below depend on
79# this value being 128k.  This is currently same as the default recordsize.
80log_must zfs set recordsize=128k $TESTPOOL/$TESTFS2
81
82# Create files with varied layouts on disk
83log_must mkfile 32M /$TESTPOOL/$TESTFS2/truncated
84log_must mkfile 524288 /$TESTPOOL/$TESTFS2/truncated2
85log_must mkfile 524288 /$TESTPOOL/$TESTFS2/truncated3
86log_must mkfile 1024 /$TESTPOOL/$TESTFS2/truncated4
87
88log_must zfs snapshot $TESTPOOL/$TESTFS2@snap1
89
90#
91# Truncate files created in the first snapshot. The first tests
92# truncating a large file to a single block. The second tests
93# truncating one block off the end of a file without changing
94# the required nlevels to hold it. The third tests handling
95# of a maxblkid that is dropped and then raised again. The
96# fourth tests an object that is truncated from a single block
97# to a smaller single block.
98#
99log_must truncate -s 131072 /$TESTPOOL/$TESTFS2/truncated
100log_must truncate -s 393216 /$TESTPOOL/$TESTFS2/truncated2
101log_must rm -f /$TESTPOOL/$TESTFS2/truncated3
102log_must rm -f /$TESTPOOL/$TESTFS2/truncated4
103sync_pool $TESTPOOL
104log_must zfs umount $TESTPOOL/$TESTFS2
105log_must zfs mount $TESTPOOL/$TESTFS2
106log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS2/truncated3 \
107	bs=128k count=3 iflag=fullblock
108log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS2/truncated4 \
109	bs=512 count=1 iflag=fullblock
110
111log_must zfs snapshot $TESTPOOL/$TESTFS2@snap2
112expected_cksum=$(recursive_cksum /$TESTPOOL/$TESTFS2)
113
114log_must eval "zfs send -wp $TESTPOOL/$TESTFS2@snap1 > $sendfile"
115log_must eval "zfs send -wp -i @snap1 $TESTPOOL/$TESTFS2@snap2 > $sendfile2"
116
117log_must eval "zfs recv -F $TESTPOOL/recv < $sendfile"
118log_must eval "zfs recv -F $TESTPOOL/recv < $sendfile2"
119log_must zfs load-key $TESTPOOL/recv
120
121log_must zfs mount -a
122actual_cksum=$(recursive_cksum /$TESTPOOL/recv)
123[[ "$expected_cksum" != "$actual_cksum" ]] && \
124	log_fail "Recursive checksums differ ($expected_cksum != $actual_cksum)"
125
126log_pass "Verified 'zfs send -w' works with many different file layouts"
127