1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2018 by Delphix. All rights reserved.
16#
17
18# DESCRIPTION
19# Verify that livelists tracking remapped blocks can be
20# properly condensed.
21
22# STRATEGY
23# 1. Create a pool with disk1 and create a filesystem, snapshot
24# and clone. Create two files for the first livelist entry and
25# pause condensing.
26# 2. Add disk2 to the pool and then remove disk1, triggering a
27# remap of the blkptrs tracked in the livelist.
28# 3. Overwrite the first file several times to trigger a condense,
29# overwrite the second file once and resume condensing, now with
30# extra blkptrs added during the remap
31# 4. Check that the test added new ALLOC blkptrs mid-condense using
32# a variable set in that code path
33
34. $STF_SUITE/include/libtest.shlib
35. $STF_SUITE/tests/functional/removal/removal.kshlib
36. $STF_SUITE/tests/functional/cli_root/zfs_destroy/zfs_destroy.cfg
37
38function cleanup
39{
40	poolexists $TESTPOOL2 && zpool destroy $TESTPOOL2
41	# reset livelist max size
42	set_tunable64 LIVELIST_MAX_ENTRIES $ORIGINAL_MAX
43	[[ -f $VIRTUAL_DISK1 ]] && log_must rm $VIRTUAL_DISK1
44	[[ -f $VIRTUAL_DISK2 ]] && log_must rm $VIRTUAL_DISK2
45}
46
47log_onexit cleanup
48
49ORIGINAL_MAX=$(get_tunable LIVELIST_MAX_ENTRIES)
50set_tunable64 LIVELIST_MAX_ENTRIES 20
51
52VIRTUAL_DISK1=$TEST_BASE_DIR/disk1
53VIRTUAL_DISK2=$TEST_BASE_DIR/disk2
54log_must truncate -s $(($MINVDEVSIZE * 8)) $VIRTUAL_DISK1
55log_must truncate -s $(($MINVDEVSIZE * 16)) $VIRTUAL_DISK2
56
57log_must zpool create -O compression=off $TESTPOOL2 $VIRTUAL_DISK1
58log_must poolexists $TESTPOOL2
59
60log_must zfs create $TESTPOOL2/$TESTFS
61log_must mkfile 100m /$TESTPOOL2/$TESTFS/atestfile
62log_must zfs snapshot $TESTPOOL2/$TESTFS@snap
63
64log_must zfs clone $TESTPOOL2/$TESTFS@snap $TESTPOOL2/$TESTCLONE
65
66# Create initial files and pause condense zthr on next execution
67log_must mkfile 10m /$TESTPOOL2/$TESTCLONE/A
68log_must mkfile 1m /$TESTPOOL2/$TESTCLONE/B
69sync_pool $TESTPOOL2
70set_tunable32 LIVELIST_CONDENSE_SYNC_PAUSE 1
71
72# Add a new dev and remove the old one
73log_must zpool add $TESTPOOL2 $VIRTUAL_DISK2
74log_must zpool remove $TESTPOOL2 $VIRTUAL_DISK1
75wait_for_removal $TESTPOOL2
76
77set_tunable32 LIVELIST_CONDENSE_NEW_ALLOC 0
78# Trigger a condense
79log_must mkfile 10m /$TESTPOOL2/$TESTCLONE/A
80sync_pool $TESTPOOL2
81log_must mkfile 10m /$TESTPOOL2/$TESTCLONE/A
82sync_pool $TESTPOOL2
83# Write remapped blkptrs which will modify the livelist mid-condense
84log_must mkfile 1m /$TESTPOOL2/$TESTCLONE/B
85
86# Resume condense thr
87set_tunable32 LIVELIST_CONDENSE_SYNC_PAUSE 0
88sync_pool $TESTPOOL2
89# Check that we've added new ALLOC blkptrs during the condense
90[[ "0" < "$(get_tunable LIVELIST_CONDENSE_NEW_ALLOC)" ]] || \
91    log_fail "removal/condense test failed"
92
93log_must zfs destroy $TESTPOOL2/$TESTCLONE
94log_pass "Clone with the livelist feature and remapped blocks," \
95	"can be condensed."
96