1#! /bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2019 by Delphix. All rights reserved.
19# Copyright 2019 Joyent, Inc.
20#
21
22. $STF_SUITE/include/libtest.shlib
23
24#
25# DESCRIPTION:
26# Log spacemaps are generally destroyed at export in order to
27# not induce performance overheads at import time. As a result,
28# the log spacemap codepaths that read the logs in import times
29# are not tested outside of ztest and pools with DEBUG bits doing
30# many imports/exports while running the test suite.
31#
32# This test uses an internal tunable and forces ZFS to keep the
33# log spacemaps at export, and then re-imports the pool, thus
34# providing explicit testing of those codepaths. It also uses
35# another tunable to load all the metaslabs when the pool is
36# re-imported so more assertions and verifications will be hit.
37#
38# STRATEGY:
39#	1. Create pool.
40#	2. Do a couple of writes to generate some data for spacemap logs.
41#	3. Set tunable to keep logs after export.
42#	4. Export pool and verify that there are logs with zdb.
43#	5. Set tunable to load all metaslabs at import.
44#	6. Import pool.
45#	7. Reset tunables.
46#
47
48verify_runnable "global"
49
50function cleanup
51{
52	log_must set_tunable32 zfs_keep_log_spacemaps_at_export 0
53	log_must set_tunable32 metaslab_debug_load 0
54	if poolexists $LOGSM_POOL; then
55		log_must zpool destroy -f $LOGSM_POOL
56	fi
57}
58log_onexit cleanup
59
60LOGSM_POOL="logsm_import"
61TESTDISK="$(echo $DISKS | cut -d' ' -f1)"
62
63log_must zpool create -o cachefile=none -f $LOGSM_POOL $TESTDISK
64log_must zfs create $LOGSM_POOL/fs
65
66log_must dd if=/dev/urandom of=/$LOGSM_POOL/fs/00 bs=128k count=10
67sync_all_pools
68log_must dd if=/dev/urandom of=/$LOGSM_POOL/fs/00 bs=128k count=10
69sync_all_pools
70
71log_must set_tunable32 zfs_keep_log_spacemaps_at_export 1
72log_must zpool export $LOGSM_POOL
73
74LOGSM_COUNT=$(zdb -m -e $LOGSM_POOL | grep "Log Spacemap object" | wc -l)
75if (( LOGSM_COUNT == 0 )); then
76	log_fail "Pool does not have any log spacemaps after being exported"
77fi
78
79log_must set_tunable32 metaslab_debug_load 1
80log_must zpool import $LOGSM_POOL
81
82log_pass "Log spacemaps imported with no errors"
83