1#!/bin/ksh -p
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2021, George Amanakis <gamanakis@gmail.com>. All rights reserved.
15#
16
17. $STF_SUITE/include/libtest.shlib
18. $STF_SUITE/tests/functional/userquota/userquota_common.kshlib
19
20#
21# DESCRIPTION:
22# Sending raw encrypted datasets back to the source dataset succeeds.
23#
24#
25# STRATEGY:
26# 1. Create encrypted source dataset, set userquota and write a file
27# 2. Create base snapshot
28# 3. Write new file, snapshot, get userspace
29# 4. Raw send both snapshots
30# 5. Destroy latest snapshot at source and rollback
31# 6. Unmount, unload key from source
32# 7. Raw send latest snapshot back to source
33# 8. Mount both source and target datasets
34# 9. Verify encrypted datasets support 'zfs userspace' and 'zfs groupspace'
35#	and the accounting is done correctly
36#
37
38function cleanup
39{
40	destroy_pool $POOLNAME
41	rm -f $FILEDEV
42}
43
44log_onexit cleanup
45
46FILEDEV="$TEST_BASE_DIR/userspace_encrypted"
47POOLNAME="testpool$$"
48ENC_SOURCE="$POOLNAME/source"
49ENC_TARGET="$POOLNAME/target"
50
51log_assert "Sending raw encrypted datasets back to the source dataset succeeds."
52
53# Setup pool and create source
54truncate -s 200m $FILEDEV
55log_must zpool create -O compression=off -o feature@encryption=enabled $POOLNAME \
56	$FILEDEV
57log_must eval "echo 'password' | zfs create -o encryption=on" \
58	"-o keyformat=passphrase -o keylocation=prompt " \
59	"$ENC_SOURCE"
60
61# Set user quota and write file
62log_must zfs set userquota@$QUSER1=50m $ENC_SOURCE
63mkmount_writable $ENC_SOURCE
64mntpnt=$(get_prop mountpoint $ENC_SOURCE)
65log_must user_run $QUSER1 mkfile 10m /$mntpnt/file1
66sync
67
68# Snapshot
69log_must zfs snap $ENC_SOURCE@base
70
71# Write new file, snapshot, get userspace
72log_must user_run $QUSER1 mkfile 20m /$mntpnt/file2
73log_must zfs snap $ENC_SOURCE@s1
74
75# Raw send both snapshots
76log_must eval "zfs send -w $ENC_SOURCE@base | zfs recv " \
77	"$ENC_TARGET"
78log_must eval "zfs send -w -i @base $ENC_SOURCE@s1 | zfs recv " \
79	"$ENC_TARGET"
80
81# Destroy latest snapshot at source and rollback
82log_must zfs destroy $ENC_SOURCE@s1
83log_must zfs rollback $ENC_SOURCE@base
84rollback_uspace=$(zfs userspace -Hp $ENC_SOURCE | \
85	awk "/$QUSER1/"' {printf "%d\n", $4 / 1024 / 1024}')
86
87# Unmount, unload key
88log_must zfs umount $ENC_SOURCE
89log_must zfs unload-key -a
90
91# Raw send latest snapshot back to source
92log_must eval "zfs send -w -i @base $ENC_TARGET@s1 | zfs recv " \
93	"$ENC_SOURCE"
94
95#  Mount encrypted datasets and verify they support 'zfs userspace' and
96# 'zfs groupspace' and the accounting is done correctly
97log_must eval "echo 'password' | zfs load-key $ENC_SOURCE"
98log_must eval "echo 'password' | zfs load-key $ENC_TARGET"
99log_must zfs mount $ENC_SOURCE
100log_must zfs mount $ENC_TARGET
101sync
102
103sleep 5
104
105src_uspace=$(zfs userspace -Hp $ENC_SOURCE | \
106	awk "/$QUSER1/"' {printf "%d\n", $4 / 1024 / 1024}')
107tgt_uspace=$(zfs userspace -Hp $ENC_TARGET | \
108	awk "/$QUSER1/"' {printf "%d\n", $4 / 1024 / 1024}')
109log_must test "$src_uspace" -eq "$tgt_uspace"
110log_must test "$rollback_uspace" -ne "$src_uspace"
111
112src_uquota=$(zfs userspace -Hp $ENC_SOURCE | awk "/$QUSER1/"' {print $5}')
113tgt_uquota=$(zfs userspace -Hp $ENC_TARGET | awk "/$QUSER1/"' {print $5}')
114log_must test "$src_uquota" -eq "$tgt_uquota"
115
116# Cleanup
117cleanup
118
119log_pass "Sending raw encrypted datasets back to the source dataset succeeds."
120