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		log_must zfs destroy -r $TESTPOOL/$TESTFS2
46	datasetexists $TESTPOOL/recv && \
47		log_must zfs destroy -r $TESTPOOL/recv
48	[[ -f $keyfile ]] && log_must rm $keyfile
49	[[ -f $sendfile ]] && log_must rm $sendfile
50}
51log_onexit cleanup
52
53function recursive_cksum
54{
55	find $1 -type f -exec sha256sum {} \; | \
56		sort -k 2 | awk '{ print $1 }' | sha256sum
57}
58
59log_assert "Verify 'zfs send -w' works with many different file layouts"
60
61typeset keyfile=/$TESTPOOL/pkey
62typeset sendfile=/$TESTPOOL/sendfile
63typeset sendfile2=/$TESTPOOL/sendfile2
64
65# Create an encrypted dataset
66log_must eval "echo 'password' > $keyfile"
67log_must zfs create -o encryption=on -o keyformat=passphrase \
68	-o keylocation=file://$keyfile $TESTPOOL/$TESTFS2
69
70# Explicitly set the recordsize since the truncation sizes below depend on
71# this value being 128k.  This is currently same as the default recordsize.
72log_must zfs set recordsize=128k $TESTPOOL/$TESTFS2
73
74# Create files with varied layouts on disk
75log_must mkfile 32M /$TESTPOOL/$TESTFS2/truncated
76log_must mkfile 524288 /$TESTPOOL/$TESTFS2/truncated2
77log_must mkfile 524288 /$TESTPOOL/$TESTFS2/truncated3
78log_must mkfile 1024 /$TESTPOOL/$TESTFS2/truncated4
79
80log_must zfs snapshot $TESTPOOL/$TESTFS2@snap1
81
82#
83# Truncate files created in the first snapshot. The first tests
84# truncating a large file to a single block. The second tests
85# truncating one block off the end of a file without changing
86# the required nlevels to hold it. The third tests handling
87# of a maxblkid that is dropped and then raised again. The
88# fourth tests an object that is truncated from a single block
89# to a smaller single block.
90#
91log_must truncate -s 131072 /$TESTPOOL/$TESTFS2/truncated
92log_must truncate -s 393216 /$TESTPOOL/$TESTFS2/truncated2
93log_must rm -f /$TESTPOOL/$TESTFS2/truncated3
94log_must rm -f /$TESTPOOL/$TESTFS2/truncated4
95log_must zpool sync $TESTPOOL
96log_must zfs umount $TESTPOOL/$TESTFS2
97log_must zfs mount $TESTPOOL/$TESTFS2
98log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS2/truncated3 \
99	bs=128k count=3 iflag=fullblock
100log_must dd if=/dev/urandom of=/$TESTPOOL/$TESTFS2/truncated4 \
101	bs=512 count=1 iflag=fullblock
102
103log_must zfs snapshot $TESTPOOL/$TESTFS2@snap2
104expected_cksum=$(recursive_cksum /$TESTPOOL/$TESTFS2)
105
106log_must eval "zfs send -wp $TESTPOOL/$TESTFS2@snap1 > $sendfile"
107log_must eval "zfs send -wp -i @snap1 $TESTPOOL/$TESTFS2@snap2 > $sendfile2"
108
109log_must eval "zfs recv -F $TESTPOOL/recv < $sendfile"
110log_must eval "zfs recv -F $TESTPOOL/recv < $sendfile2"
111log_must zfs load-key $TESTPOOL/recv
112
113log_must zfs mount -a
114actual_cksum=$(recursive_cksum /$TESTPOOL/recv)
115[[ "$expected_cksum" != "$actual_cksum" ]] && \
116	log_fail "Recursive checksums differ ($expected_cksum != $actual_cksum)"
117
118log_pass "Verified 'zfs send -w' works with many different file layouts"
119