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 (c) 2018 by Delphix. All rights reserved.
15#
16
17# DESCRIPTION
18# Verify zfs destroy test for clones with the livelist feature
19# enabled.
20
21# STRATEGY
22# 1. Clone where livelist is condensed
23#	- create clone, write several files, delete those files
24#	- check that the number of livelist entries decreases
25#	  after the delete
26# 2. Clone where livelist is deactivated
27#	- create clone, write files. Delete those files and the
28#	  file in the filesystem when the snapshot was created
29#	  so the clone and snapshot no longer share data
30#	- check that the livelist is destroyed
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zfs_destroy/zfs_destroy_common.kshlib
34
35function cleanup
36{
37	log_must zfs destroy -Rf $TESTPOOL/$TESTFS1
38	# reset the livelist sublist size to the original value
39	set_tunable64 LIVELIST_MAX_ENTRIES $ORIGINAL_MAX
40	# reset the minimum percent shared to 75
41	set_tunable32 LIVELIST_MIN_PERCENT_SHARED $ORIGINAL_MIN
42	log_must zfs inherit compression $TESTPOOL
43}
44
45function check_ll_len
46{
47    string="$(zdb -vvvvv $TESTPOOL | grep "Livelist")"
48    substring="$1"
49    msg=$2
50    if test "${string#*$substring}" != "$string"; then
51        return 0    # $substring is in $string
52    else
53	log_note $string
54        log_fail "$msg" # $substring is not in $string
55    fi
56}
57
58function test_condense
59{
60	# set the max livelist entries to a small value to more easily
61	# trigger a condense
62	set_tunable64 LIVELIST_MAX_ENTRIES 20
63	# set a small percent shared threshold so the livelist is not disabled
64	set_tunable32 LIVELIST_MIN_PERCENT_SHARED 10
65	clone_dataset $TESTFS1 snap $TESTCLONE
66
67	# sync between each write to make sure a new entry is created
68	for i in {0..4}; do
69	    log_must mkfile 5m /$TESTPOOL/$TESTCLONE/testfile$i
70	    sync_pool $TESTPOOL
71	done
72
73	check_ll_len "5 entries" "Unexpected livelist size"
74
75	# sync between each write to allow for a condense of the previous entry
76	for i in {0..4}; do
77	    log_must mkfile 5m /$TESTPOOL/$TESTCLONE/testfile$i
78	    sync_pool $TESTPOOL
79	done
80
81	check_ll_len "6 entries" "Condense did not occur"
82
83	log_must zfs destroy $TESTPOOL/$TESTCLONE
84	check_livelist_gone
85}
86
87function test_deactivated
88{
89	# Threshold set to 50 percent
90	set_tunable32 LIVELIST_MIN_PERCENT_SHARED 50
91	clone_dataset $TESTFS1 snap $TESTCLONE
92
93	log_must mkfile 5m /$TESTPOOL/$TESTCLONE/$TESTFILE0
94	log_must mkfile 5m /$TESTPOOL/$TESTCLONE/$TESTFILE1
95	sync_pool $TESTPOOL
96	# snapshot and clone share 'atestfile', 33 percent
97	check_livelist_gone
98	log_must zfs destroy -R $TESTPOOL/$TESTCLONE
99
100	# Threshold set to 20 percent
101	set_tunable32 LIVELIST_MIN_PERCENT_SHARED 20
102	clone_dataset $TESTFS1 snap $TESTCLONE
103
104	log_must mkfile 5m /$TESTPOOL/$TESTCLONE/$TESTFILE0
105	log_must mkfile 5m /$TESTPOOL/$TESTCLONE/$TESTFILE1
106	log_must mkfile 5m /$TESTPOOL/$TESTCLONE/$TESTFILE2
107	sync_pool $TESTPOOL
108	# snapshot and clone share 'atestfile', 25 percent
109	check_livelist_exists $TESTCLONE
110	log_must rm /$TESTPOOL/$TESTCLONE/atestfile
111	# snapshot and clone share no files
112	check_livelist_gone
113	log_must zfs destroy -R $TESTPOOL/$TESTCLONE
114}
115
116ORIGINAL_MAX=$(get_tunable LIVELIST_MAX_ENTRIES)
117ORIGINAL_MIN=$(get_tunable LIVELIST_MIN_PERCENT_SHARED)
118
119log_onexit cleanup
120# You might think that setting compression=off for $TESTFS1 would be
121# sufficient. You would be mistaken.
122# You need compression=off for whatever the parent of $TESTFS1 is,
123# and $TESTFS1.
124log_must zfs set compression=off $TESTPOOL
125log_must zfs create $TESTPOOL/$TESTFS1
126log_must mkfile 5m /$TESTPOOL/$TESTFS1/atestfile
127log_must zfs snapshot $TESTPOOL/$TESTFS1@snap
128test_condense
129test_deactivated
130
131log_pass "Clone's livelist condenses and disables as expected."
132