1f38cb554SJohn Wren Kennedy#!/bin/ksh -p
2f38cb554SJohn Wren Kennedy#
3f38cb554SJohn Wren Kennedy# CDDL HEADER START
4f38cb554SJohn Wren Kennedy#
5f38cb554SJohn Wren Kennedy# The contents of this file are subject to the terms of the
6f38cb554SJohn Wren Kennedy# Common Development and Distribution License (the "License").
7f38cb554SJohn Wren Kennedy# You may not use this file except in compliance with the License.
8f38cb554SJohn Wren Kennedy#
9f38cb554SJohn Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10f38cb554SJohn Wren Kennedy# or http://www.opensolaris.org/os/licensing.
11f38cb554SJohn Wren Kennedy# See the License for the specific language governing permissions
12f38cb554SJohn Wren Kennedy# and limitations under the License.
13f38cb554SJohn Wren Kennedy#
14f38cb554SJohn Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
15f38cb554SJohn Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16f38cb554SJohn Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
17f38cb554SJohn Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
18f38cb554SJohn Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
19f38cb554SJohn Wren Kennedy#
20f38cb554SJohn Wren Kennedy# CDDL HEADER END
21f38cb554SJohn Wren Kennedy#
22f38cb554SJohn Wren Kennedy
23f38cb554SJohn Wren Kennedy#
241d32ba66SJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
25f38cb554SJohn Wren Kennedy#
26f38cb554SJohn Wren Kennedy
27f38cb554SJohn Wren Kennedy. $STF_SUITE/include/libtest.shlib
28f38cb554SJohn Wren Kennedy
29f38cb554SJohn Wren Kennedy#
30f38cb554SJohn Wren Kennedy# DESCRIPTION:
31f38cb554SJohn Wren Kennedy# Exercise the traversal suspend/resume code in async_destroy by
32f38cb554SJohn Wren Kennedy# destroying a file system that has more blocks than we can free
33f38cb554SJohn Wren Kennedy# in a single txg.
34f38cb554SJohn Wren Kennedy#
35f38cb554SJohn Wren Kennedy# STRATEGY:
36f38cb554SJohn Wren Kennedy# 1. Create a file system
37f38cb554SJohn Wren Kennedy# 2. Set recordsize to 512 to create the maximum number of blocks
38f38cb554SJohn Wren Kennedy# 3. Set compression to off to force zero-ed blocks to be written
39f38cb554SJohn Wren Kennedy# 4. dd a lot of data from /dev/zero to the file system
40f38cb554SJohn Wren Kennedy# 5. Destroy the file system
41f38cb554SJohn Wren Kennedy# 6. Wait for the freeing property to go to 0
42f38cb554SJohn Wren Kennedy# 7. Use zdb to check for leaked blocks
43f38cb554SJohn Wren Kennedy#
44f38cb554SJohn Wren Kennedy
45f38cb554SJohn Wren KennedyTEST_FS=$TESTPOOL/async_destroy
46f38cb554SJohn Wren Kennedy
47f38cb554SJohn Wren Kennedyverify_runnable "both"
48f38cb554SJohn Wren Kennedy
495cabbc6bSPrashanth Sreenivasafunction set_max_blocks
505cabbc6bSPrashanth Sreenivasa{
515cabbc6bSPrashanth Sreenivasa	echo "zfs_async_block_max_blocks/Z$1" | mdb -kw
525cabbc6bSPrashanth Sreenivasa}
535cabbc6bSPrashanth Sreenivasa
54f38cb554SJohn Wren Kennedyfunction cleanup
55f38cb554SJohn Wren Kennedy{
561d32ba66SJohn Wren Kennedy	datasetexists $TEST_FS && log_must zfs destroy $TEST_FS
575cabbc6bSPrashanth Sreenivasa	log_must set_max_blocks ffffffffffffffff
58f38cb554SJohn Wren Kennedy}
59f38cb554SJohn Wren Kennedy
60f38cb554SJohn Wren Kennedylog_onexit cleanup
61f38cb554SJohn Wren Kennedylog_assert "async_destroy can suspend and resume traversal"
62f38cb554SJohn Wren Kennedy
635cabbc6bSPrashanth Sreenivasalog_must zfs create -o recordsize=1k -o compression=off $TEST_FS
64f38cb554SJohn Wren Kennedy
655cabbc6bSPrashanth Sreenivasa# Fill with 128,000 blocks.
665cabbc6bSPrashanth Sreenivasalog_must dd bs=1024k count=128 if=/dev/zero of=/$TEST_FS/file
675cabbc6bSPrashanth Sreenivasa
685cabbc6bSPrashanth Sreenivasa#
695cabbc6bSPrashanth Sreenivasa# Decrease the max blocks to free each txg, so that freeing takes
705cabbc6bSPrashanth Sreenivasa# long enough that we can observe it.
715cabbc6bSPrashanth Sreenivasa#
725cabbc6bSPrashanth Sreenivasalog_must set_max_blocks 0t100
73f38cb554SJohn Wren Kennedy
74*09fbbb7dSAllan Judesync_all_pools
751d32ba66SJohn Wren Kennedylog_must zfs destroy $TEST_FS
76f38cb554SJohn Wren Kennedy
77db241752SJohn Wren Kennedy#
78db241752SJohn Wren Kennedy# We monitor the freeing property, to verify we can see blocks being
795cabbc6bSPrashanth Sreenivasa# freed while the suspend/resume code is exercised.
80db241752SJohn Wren Kennedy#
81db241752SJohn Wren Kennedyt0=$SECONDS
82f38cb554SJohn Wren Kennedycount=0
83db241752SJohn Wren Kennedywhile [[ $((SECONDS - t0)) -lt 10 ]]; do
841d32ba66SJohn Wren Kennedy	[[ "0" != "$(zpool list -Ho freeing $TESTPOOL)" ]] && ((count++))
85db241752SJohn Wren Kennedy	[[ $count -gt 1 ]] && break
861d32ba66SJohn Wren Kennedy	sleep 1
87f38cb554SJohn Wren Kennedydone
88f38cb554SJohn Wren Kennedy
89db241752SJohn Wren Kennedy[[ $count -eq 0 ]] && log_fail "Freeing property remained empty"
90db241752SJohn Wren Kennedy
915cabbc6bSPrashanth Sreenivasa#
925cabbc6bSPrashanth Sreenivasa# After a bit, go back to allowing an unlimited amount of freeing
935cabbc6bSPrashanth Sreenivasa# per txg.
945cabbc6bSPrashanth Sreenivasa#
955cabbc6bSPrashanth Sreenivasasleep 10
965cabbc6bSPrashanth Sreenivasalog_must set_max_blocks ffffffffffffffff
975cabbc6bSPrashanth Sreenivasa
98db241752SJohn Wren Kennedy# Wait for everything to be freed.
991d32ba66SJohn Wren Kennedywhile [[ "0" != "$(zpool list -Ho freeing $TESTPOOL)" ]]; do
100db241752SJohn Wren Kennedy	[[ $((SECONDS - t0)) -gt 180 ]] && \
101db241752SJohn Wren Kennedy	    log_fail "Timed out waiting for freeing to drop to zero"
102db241752SJohn Wren Kennedydone
103f38cb554SJohn Wren Kennedy
104f38cb554SJohn Wren Kennedy# Check for leaked blocks.
1051d32ba66SJohn Wren Kennedylog_must zdb -b $TESTPOOL
106f38cb554SJohn Wren Kennedy
107f38cb554SJohn Wren Kennedylog_pass "async_destroy can suspend and resume traversal"
108