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. $STF_SUITE/include/libtest.shlib
18. $STF_SUITE/tests/functional/cli_root/zpool_wait/zpool_wait.kshlib
19
20#
21# DESCRIPTION:
22# 'zpool wait' works when waiting for background freeing to complete.
23#
24# STRATEGY:
25# 1. Create a pool.
26# 2. Modify tunables to make sure freeing is slow enough to observe.
27# 3. Create a file system with some data.
28# 4. Destroy the file system and call 'zpool wait'.
29# 5. Monitor the waiting process to make sure it returns neither too soon nor
30#    too late.
31# 6. Repeat 3-5, except destroy a snapshot instead of a filesystem.
32# 7. Repeat 3-5, except destroy a clone.
33#
34
35function cleanup
36{
37	log_must set_tunable64 ASYNC_BLOCK_MAX_BLOCKS $default_async_block_max_blocks
38	log_must set_tunable64 LIVELIST_MAX_ENTRIES $default_max_livelist_entries
39	log_must set_tunable64 LIVELIST_MIN_PERCENT_SHARED $default_min_pct_shared
40
41	poolexists $TESTPOOL && destroy_pool $TESTPOOL
42	kill_if_running $pid
43}
44
45function test_wait
46{
47	log_bkgrnd zpool wait -t free $TESTPOOL
48	pid=$!
49	check_while_waiting $pid '[[ $(get_pool_prop freeing $TESTPOOL) != "0" ]]'
50}
51
52typeset -r FS="$TESTPOOL/$TESTFS1"
53typeset -r SNAP="$FS@snap1"
54typeset -r CLONE="$TESTPOOL/clone"
55typeset pid default_max_livelist_entries default_min_pct_shared
56typeset default_async_block_max_blocks
57
58log_onexit cleanup
59
60log_must zpool create $TESTPOOL $DISK1
61
62#
63# Limit the number of blocks that can be freed in a single txg. This slows down
64# freeing so that we actually have something to wait for.
65#
66default_async_block_max_blocks=$(get_tunable ASYNC_BLOCK_MAX_BLOCKS)
67log_must set_tunable64 ASYNC_BLOCK_MAX_BLOCKS 8
68#
69# Space from clones gets freed one livelist per txg instead of being controlled
70# by zfs_async_block_max_blocks. Limit the rate at which space is freed by
71# limiting the size of livelists so that we end up with a number of them.
72#
73default_max_livelist_entries=$(get_tunable LIVELIST_MAX_ENTRIES)
74log_must set_tunable64 LIVELIST_MAX_ENTRIES 16
75# Don't disable livelists, no matter how much clone diverges from snapshot
76default_min_pct_shared=$(get_tunable LIVELIST_MIN_PERCENT_SHARED)
77log_must set_tunable64 LIVELIST_MIN_PERCENT_SHARED -1
78
79#
80# Test waiting for space from destroyed filesystem to be freed
81#
82log_must zfs create "$FS"
83log_must dd if=/dev/zero of="/$FS/testfile" bs=1M count=128
84log_must zfs destroy "$FS"
85test_wait
86
87#
88# Test waiting for space from destroyed snapshot to be freed
89#
90log_must zfs create "$FS"
91log_must dd if=/dev/zero of="/$FS/testfile" bs=1M count=128
92log_must zfs snapshot "$SNAP"
93# Make sure bulk of space is unique to snapshot
94log_must rm "/$FS/testfile"
95log_must zfs destroy "$SNAP"
96test_wait
97
98#
99# Test waiting for space from destroyed clone to be freed
100#
101log_must zfs snapshot "$SNAP"
102log_must zfs clone "$SNAP" "$CLONE"
103# Add some data to the clone
104for i in {1..50}; do
105	log_must dd if=/dev/urandom of="/$CLONE/testfile$i" bs=1k count=512
106	# Force each new file to be tracked by a new livelist
107	sync_pool $TESTPOOL
108done
109log_must zfs destroy "$CLONE"
110test_wait
111
112log_pass "'zpool wait -t freeing' works."
113