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) 2020, George Amanakis. All rights reserved.
19#
20
21. $STF_SUITE/include/libtest.shlib
22. $STF_SUITE/tests/functional/l2arc/l2arc.cfg
23. $STF_SUITE/tests/functional/cli_root/zfs_load-key/zfs_load-key_common.kshlib
24
25#
26# DESCRIPTION:
27#	Persistent L2ARC with an encrypted ZFS file system succeeds
28#
29# STRATEGY:
30#	1. Create pool with a cache device.
31#	2. Create a an encrypted ZFS file system.
32#	3. Create a random file in the encrypted file system and random
33#		read for 10 sec.
34#	4. Export pool.
35#	5. Read the amount of log blocks written from the header of the
36#		L2ARC device.
37#	5. Import pool.
38#	6. Mount the encrypted ZFS file system.
39#	7. Read the amount of log blocks rebuilt in arcstats and compare to
40#		(5).
41#	8. Check if the labels of the L2ARC device are intact.
42#
43#	* We can predict the minimum bytes of L2ARC restored if we subtract
44#	from the effective size of the cache device the bytes l2arc_evict()
45#	evicts:
46#	l2: L2ARC device size - VDEV_LABEL_START_SIZE - l2ad_dev_hdr_asize
47#	wr_sz: l2arc_write_max + l2arc_write_boost (worst case)
48#	blk_overhead: wr_sz / SPA_MINBLOCKSIZE / (l2 / SPA_MAXBLOCKSIZE) *
49#		sizeof (l2arc_log_blk_phys_t)
50#	min restored size: l2 - (wr_sz + blk_overhead)
51#
52
53verify_runnable "global"
54
55command -v fio > /dev/null || log_unsupported "fio missing"
56
57log_assert "Persistent L2ARC with an encrypted ZFS file system succeeds."
58
59function cleanup
60{
61	if poolexists $TESTPOOL ; then
62		destroy_pool $TESTPOOL
63	fi
64
65	log_must set_tunable32 L2ARC_NOPREFETCH $noprefetch
66	log_must set_tunable32 L2ARC_REBUILD_BLOCKS_MIN_L2SIZE \
67		$rebuild_blocks_min_l2size
68}
69log_onexit cleanup
70
71# L2ARC_NOPREFETCH is set to 0 to let L2ARC handle prefetches
72typeset noprefetch=$(get_tunable L2ARC_NOPREFETCH)
73typeset rebuild_blocks_min_l2size=$(get_tunable L2ARC_REBUILD_BLOCKS_MIN_L2SIZE)
74log_must set_tunable32 L2ARC_NOPREFETCH 0
75log_must set_tunable32 L2ARC_REBUILD_BLOCKS_MIN_L2SIZE 0
76
77typeset fill_mb=800
78typeset cache_sz=$(( floor($fill_mb / 2) ))
79export FILE_SIZE=$(( floor($fill_mb / $NUMJOBS) ))M
80
81log_must truncate -s ${cache_sz}M $VDEV_CACHE
82
83log_must zpool create -f $TESTPOOL $VDEV cache $VDEV_CACHE
84
85log_must eval "echo $PASSPHRASE | zfs create -o encryption=on" \
86	"-o keyformat=passphrase $TESTPOOL/$TESTFS1"
87
88log_must fio $FIO_SCRIPTS/mkfiles.fio
89log_must fio $FIO_SCRIPTS/random_reads.fio
90
91arcstat_quiescence_noecho l2_size
92log_must zpool export $TESTPOOL
93arcstat_quiescence_noecho l2_feeds
94
95typeset l2_dh_log_blk=$(zdb -l $VDEV_CACHE | awk '/log_blk_count/ {print $2}')
96
97typeset l2_rebuild_log_blk_start=$(get_arcstat l2_rebuild_log_blks)
98
99log_must zpool import -d $VDIR $TESTPOOL
100log_must eval "echo $PASSPHRASE | zfs mount -l $TESTPOOL/$TESTFS1"
101arcstat_quiescence_noecho l2_size
102
103typeset l2_rebuild_log_blk_end=$(arcstat_quiescence_echo l2_rebuild_log_blks)
104
105log_must test $l2_dh_log_blk -eq $(( $l2_rebuild_log_blk_end - \
106	$l2_rebuild_log_blk_start ))
107log_must test $l2_dh_log_blk -gt 0
108
109log_must zpool offline $TESTPOOL $VDEV_CACHE
110arcstat_quiescence_noecho l2_size
111
112log_must zdb -lq $VDEV_CACHE
113
114log_must zpool destroy -f $TESTPOOL
115
116log_pass "Persistent L2ARC with an encrypted ZFS file system succeeds."
117