116038816SMartin Matuska#!/bin/ksh -p
216038816SMartin Matuska#
316038816SMartin Matuska# This file and its contents are supplied under the terms of the
416038816SMartin Matuska# Common Development and Distribution License ("CDDL"), version 1.0.
516038816SMartin Matuska# You may only use this file in accordance with the terms of version
616038816SMartin Matuska# 1.0 of the CDDL.
716038816SMartin Matuska#
816038816SMartin Matuska# A full copy of the text of the CDDL should have accompanied this
916038816SMartin Matuska# source.  A copy of the CDDL is also available via the Internet at
1016038816SMartin Matuska# http://www.illumos.org/license/CDDL.
1116038816SMartin Matuska#
1216038816SMartin Matuska
1316038816SMartin Matuska#
1416038816SMartin Matuska# Copyright (c) 2021 by Delphix. All rights reserved.
1516038816SMartin Matuska#
1616038816SMartin Matuska
1716038816SMartin Matuska# DESCRIPTION
1816038816SMartin Matuska# Verify zfs destroy test for clones with livelists that contain
1916038816SMartin Matuska# dedup blocks. This test is a baseline regression test created
2016038816SMartin Matuska# to ensure that past bugs that we've encountered between dedup
2116038816SMartin Matuska# and the livelist logic don't resurface.
2216038816SMartin Matuska
2316038816SMartin Matuska# STRATEGY
2416038816SMartin Matuska# 1. Create a clone from a test filesystem and enable dedup.
2516038816SMartin Matuska# 2. Write some data and create a livelist.
2616038816SMartin Matuska# 3. Copy the data within the clone to create dedup blocks.
2716038816SMartin Matuska# 4. Remove some of the dedup data to create multiple free
2816038816SMartin Matuska#    entries for the same block pointers.
2916038816SMartin Matuska# 5. Process all the livelist entries by destroying the clone.
3016038816SMartin Matuska
3116038816SMartin Matuska. $STF_SUITE/include/libtest.shlib
3216038816SMartin Matuska. $STF_SUITE/tests/functional/cli_root/zfs_destroy/zfs_destroy_common.kshlib
3316038816SMartin Matuska
3416038816SMartin Matuskafunction cleanup
3516038816SMartin Matuska{
3616038816SMartin Matuska	log_must zfs destroy -Rf $TESTPOOL/$TESTFS1
3716038816SMartin Matuska	# Reset the minimum percent shared to 75
3816038816SMartin Matuska	set_tunable32 LIVELIST_MIN_PERCENT_SHARED $ORIGINAL_MIN_SHARED
3916038816SMartin Matuska}
4016038816SMartin Matuska
4116038816SMartin Matuskafunction test_dedup
4216038816SMartin Matuska{
4316038816SMartin Matuska	# Set a small percent shared threshold so the livelist is not disabled
4416038816SMartin Matuska	set_tunable32 LIVELIST_MIN_PERCENT_SHARED 10
4516038816SMartin Matuska	clone_dataset $TESTFS1 snap $TESTCLONE
4616038816SMartin Matuska
4716038816SMartin Matuska	# Enable dedup
4816038816SMartin Matuska	log_must zfs set dedup=on $TESTPOOL/$TESTCLONE
4916038816SMartin Matuska
5016038816SMartin Matuska	# Create some data to be deduped
5116038816SMartin Matuska	log_must dd if=/dev/urandom of="/$TESTPOOL/$TESTCLONE/data" bs=512 count=10k
5216038816SMartin Matuska
5316038816SMartin Matuska	# Create dedup blocks
5416038816SMartin Matuska	# Note: We sync before and after so all dedup blocks belong to the
5516038816SMartin Matuska	#       same TXG, otherwise they won't look identical to the livelist
5616038816SMartin Matuska	#       iterator due to their logical birth TXG being different.
57e92ffd9bSMartin Matuska	sync_pool $TESTPOOL
5816038816SMartin Matuska	log_must cp /$TESTPOOL/$TESTCLONE/data /$TESTPOOL/$TESTCLONE/data-dup-0
5916038816SMartin Matuska	log_must cp /$TESTPOOL/$TESTCLONE/data /$TESTPOOL/$TESTCLONE/data-dup-1
6016038816SMartin Matuska	log_must cp /$TESTPOOL/$TESTCLONE/data /$TESTPOOL/$TESTCLONE/data-dup-2
6116038816SMartin Matuska	log_must cp /$TESTPOOL/$TESTCLONE/data /$TESTPOOL/$TESTCLONE/data-dup-3
62e92ffd9bSMartin Matuska	sync_pool $TESTPOOL
6316038816SMartin Matuska	check_livelist_exists $TESTCLONE
6416038816SMartin Matuska
6516038816SMartin Matuska	# Introduce "double frees"
6616038816SMartin Matuska	#   We want to introduce consecutive FREEs of the same block as this
6716038816SMartin Matuska	#   was what triggered past panics.
6816038816SMartin Matuska	# Note: Similarly to the previouys step we sync before and after our
6916038816SMartin Matuska	#       our deletions so all the entries end up in the same TXG.
70e92ffd9bSMartin Matuska	sync_pool $TESTPOOL
7116038816SMartin Matuska	log_must rm /$TESTPOOL/$TESTCLONE/data-dup-2
7216038816SMartin Matuska	log_must rm /$TESTPOOL/$TESTCLONE/data-dup-3
73e92ffd9bSMartin Matuska	sync_pool $TESTPOOL
7416038816SMartin Matuska	check_livelist_exists $TESTCLONE
7516038816SMartin Matuska
7616038816SMartin Matuska	log_must zfs destroy $TESTPOOL/$TESTCLONE
7716038816SMartin Matuska	check_livelist_gone
7816038816SMartin Matuska}
7916038816SMartin Matuska
8016038816SMartin MatuskaORIGINAL_MIN_SHARED=$(get_tunable LIVELIST_MIN_PERCENT_SHARED)
8116038816SMartin Matuska
8216038816SMartin Matuskalog_onexit cleanup
83*c03c5b1cSMartin Matuska# You might think that setting compression=off for $TESTFS1 would be
84*c03c5b1cSMartin Matuska# sufficient. You would be mistaken.
85*c03c5b1cSMartin Matuska# You need compression=off for whatever the parent of $TESTFS1 is,
86*c03c5b1cSMartin Matuska# and $TESTFS1.
87*c03c5b1cSMartin Matuskalog_must zfs set compression=off $TESTPOOL
8816038816SMartin Matuskalog_must zfs create $TESTPOOL/$TESTFS1
8916038816SMartin Matuskalog_must mkfile 5m /$TESTPOOL/$TESTFS1/atestfile
9016038816SMartin Matuskalog_must zfs snapshot $TESTPOOL/$TESTFS1@snap
9116038816SMartin Matuskatest_dedup
9216038816SMartin Matuska
93*c03c5b1cSMartin Matuskalog_must zfs inherit compression $TESTPOOL
94*c03c5b1cSMartin Matuska
9516038816SMartin Matuskalog_pass "Clone's livelist processes dedup blocks as expected."
96