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