1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or https://opensource.org/licenses/CDDL-1.0.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2023 Klara, Inc.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.cfg
34
35#
36# DESCRIPTION:
37# 	During a pool import, the 'import_progress' kstat contains details
38# 	on the import progress.
39#
40# STRATEGY:
41#	1. Create test pool with several devices
42#	2. Generate some ZIL records and spacemap logs
43#	3. Export the pool
44#	4. Import the pool in the background and monitor the kstat content
45#	5. Check the zfs debug messages for import progress
46#
47
48verify_runnable "global"
49
50function cleanup
51{
52	log_must set_tunable64 KEEP_LOG_SPACEMAPS_AT_EXPORT 0
53	log_must set_tunable64 METASLAB_DEBUG_LOAD 0
54
55	destroy_pool $TESTPOOL1
56}
57
58log_assert "During a pool import, the 'import_progress' kstat contains " \
59	"notes on the progress"
60
61log_onexit cleanup
62
63log_must zpool create $TESTPOOL1 $VDEV0 $VDEV1 $VDEV2
64typeset guid=$(zpool get -H -o value guid $TESTPOOL1)
65
66log_must zfs create -o recordsize=8k $TESTPOOL1/fs
67#
68# This dd command works around an issue where ZIL records aren't created
69# after freezing the pool unless a ZIL header already exists. Create a file
70# synchronously to force ZFS to write one out.
71#
72log_must dd if=/dev/zero of=/$TESTPOOL1/fs/sync conv=fsync bs=1 count=1
73
74#
75# Overwrite some blocks to populate spacemap logs
76#
77log_must dd if=/dev/urandom of=/$TESTPOOL1/fs/00 bs=1M count=200
78sync_all_pools
79log_must dd if=/dev/urandom of=/$TESTPOOL1/fs/00 bs=1M count=200
80sync_all_pools
81
82#
83# Freeze the pool to retain intent log records
84#
85log_must zpool freeze $TESTPOOL1
86
87# fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
88log_must fill_fs /$TESTPOOL1/fs 1 2000 100 1024 R
89
90log_must zpool list -v $TESTPOOL1
91
92#
93# Unmount filesystem and export the pool
94#
95# At this stage the zfs intent log contains
96# a set of records to replay.
97#
98log_must zfs unmount /$TESTPOOL1/fs
99
100log_must set_tunable64 KEEP_LOG_SPACEMAPS_AT_EXPORT 1
101log_must zpool export $TESTPOOL1
102
103log_must set_tunable64 METASLAB_DEBUG_LOAD 1
104log_note "Starting zpool import in background at" $(date +'%H:%M:%S')
105zpool import -d $DEVICE_DIR -f $guid &
106pid=$!
107
108#
109# capture progress until import is finished
110#
111log_note waiting for pid $pid to exit
112kstat import_progress
113while [[ -d /proc/"$pid" ]]; do
114	line=$(kstat import_progress | grep -v pool_guid)
115	if [[ -n $line ]]; then
116		echo $line
117	fi
118	if [[ -f /$TESTPOOL1/fs/00 ]]; then
119		break;
120	fi
121	sleep 0.0001
122done
123log_note "zpool import completed at" $(date +'%H:%M:%S')
124
125entries=$(kstat dbgmsg | grep "spa_import_progress_set_notes_impl(): 'testpool1'" | wc -l)
126log_note "found $entries progress notes in dbgmsg"
127log_must test $entries -gt 20
128
129log_must zpool status $TESTPOOL1
130
131log_pass "During a pool import, the 'import_progress' kstat contains " \
132	"notes on the progress"
133