xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_scan.c (revision 165c5c6fe7d6c7a95878c8a3aae7da65d1da1d90)
13f9d6ad7SLin Ling /*
23f9d6ad7SLin Ling  * CDDL HEADER START
33f9d6ad7SLin Ling  *
43f9d6ad7SLin Ling  * The contents of this file are subject to the terms of the
53f9d6ad7SLin Ling  * Common Development and Distribution License (the "License").
63f9d6ad7SLin Ling  * You may not use this file except in compliance with the License.
73f9d6ad7SLin Ling  *
83f9d6ad7SLin Ling  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93f9d6ad7SLin Ling  * or http://www.opensolaris.org/os/licensing.
103f9d6ad7SLin Ling  * See the License for the specific language governing permissions
113f9d6ad7SLin Ling  * and limitations under the License.
123f9d6ad7SLin Ling  *
133f9d6ad7SLin Ling  * When distributing Covered Code, include this CDDL HEADER in each
143f9d6ad7SLin Ling  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153f9d6ad7SLin Ling  * If applicable, add the following below this CDDL HEADER, with the
163f9d6ad7SLin Ling  * fields enclosed by brackets "[]" replaced with your own identifying
173f9d6ad7SLin Ling  * information: Portions Copyright [yyyy] [name of copyright owner]
183f9d6ad7SLin Ling  *
193f9d6ad7SLin Ling  * CDDL HEADER END
203f9d6ad7SLin Ling  */
213f9d6ad7SLin Ling /*
223f9d6ad7SLin Ling  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23bb1f4245SMatthew Ahrens  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
248c04a1faSGary Mills  * Copyright 2016 Gary Mills
255cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
26233f6c49SKody Kantor  * Copyright 2019 Joyent, Inc.
270c06d385Sjwpoduska  * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
283f9d6ad7SLin Ling  */
293f9d6ad7SLin Ling 
303f9d6ad7SLin Ling #include <sys/dsl_scan.h>
313f9d6ad7SLin Ling #include <sys/dsl_pool.h>
323f9d6ad7SLin Ling #include <sys/dsl_dataset.h>
333f9d6ad7SLin Ling #include <sys/dsl_prop.h>
343f9d6ad7SLin Ling #include <sys/dsl_dir.h>
353f9d6ad7SLin Ling #include <sys/dsl_synctask.h>
363f9d6ad7SLin Ling #include <sys/dnode.h>
373f9d6ad7SLin Ling #include <sys/dmu_tx.h>
383f9d6ad7SLin Ling #include <sys/dmu_objset.h>
393f9d6ad7SLin Ling #include <sys/arc.h>
403f9d6ad7SLin Ling #include <sys/zap.h>
413f9d6ad7SLin Ling #include <sys/zio.h>
423f9d6ad7SLin Ling #include <sys/zfs_context.h>
433f9d6ad7SLin Ling #include <sys/fs/zfs.h>
443f9d6ad7SLin Ling #include <sys/zfs_znode.h>
453f9d6ad7SLin Ling #include <sys/spa_impl.h>
463f9d6ad7SLin Ling #include <sys/vdev_impl.h>
473f9d6ad7SLin Ling #include <sys/zil_impl.h>
483f9d6ad7SLin Ling #include <sys/zio_checksum.h>
493f9d6ad7SLin Ling #include <sys/ddt.h>
503f9d6ad7SLin Ling #include <sys/sa.h>
513f9d6ad7SLin Ling #include <sys/sa_impl.h>
52ad135b5dSChristopher Siden #include <sys/zfeature.h>
53770499e1SDan Kimmel #include <sys/abd.h>
54a3874b8bSToomas Soome #include <sys/range_tree.h>
553f9d6ad7SLin Ling #ifdef _KERNEL
563f9d6ad7SLin Ling #include <sys/zfs_vfsops.h>
573f9d6ad7SLin Ling #endif
583f9d6ad7SLin Ling 
59a3874b8bSToomas Soome /*
60a3874b8bSToomas Soome  * Grand theory statement on scan queue sorting
61a3874b8bSToomas Soome  *
62a3874b8bSToomas Soome  * Scanning is implemented by recursively traversing all indirection levels
63a3874b8bSToomas Soome  * in an object and reading all blocks referenced from said objects. This
64a3874b8bSToomas Soome  * results in us approximately traversing the object from lowest logical
65a3874b8bSToomas Soome  * offset to the highest. For best performance, we would want the logical
66a3874b8bSToomas Soome  * blocks to be physically contiguous. However, this is frequently not the
67a3874b8bSToomas Soome  * case with pools given the allocation patterns of copy-on-write filesystems.
68a3874b8bSToomas Soome  * So instead, we put the I/Os into a reordering queue and issue them in a
69a3874b8bSToomas Soome  * way that will most benefit physical disks (LBA-order).
70a3874b8bSToomas Soome  *
71a3874b8bSToomas Soome  * Queue management:
72a3874b8bSToomas Soome  *
73a3874b8bSToomas Soome  * Ideally, we would want to scan all metadata and queue up all block I/O
74a3874b8bSToomas Soome  * prior to starting to issue it, because that allows us to do an optimal
75a3874b8bSToomas Soome  * sorting job. This can however consume large amounts of memory. Therefore
76a3874b8bSToomas Soome  * we continuously monitor the size of the queues and constrain them to 5%
77a3874b8bSToomas Soome  * (zfs_scan_mem_lim_fact) of physmem. If the queues grow larger than this
78a3874b8bSToomas Soome  * limit, we clear out a few of the largest extents at the head of the queues
79a3874b8bSToomas Soome  * to make room for more scanning. Hopefully, these extents will be fairly
80a3874b8bSToomas Soome  * large and contiguous, allowing us to approach sequential I/O throughput
81a3874b8bSToomas Soome  * even without a fully sorted tree.
82a3874b8bSToomas Soome  *
83a3874b8bSToomas Soome  * Metadata scanning takes place in dsl_scan_visit(), which is called from
84a3874b8bSToomas Soome  * dsl_scan_sync() every spa_sync(). If we have either fully scanned all
85a3874b8bSToomas Soome  * metadata on the pool, or we need to make room in memory because our
86a3874b8bSToomas Soome  * queues are too large, dsl_scan_visit() is postponed and
87a3874b8bSToomas Soome  * scan_io_queues_run() is called from dsl_scan_sync() instead. This implies
88a3874b8bSToomas Soome  * that metadata scanning and queued I/O issuing are mutually exclusive. This
89a3874b8bSToomas Soome  * allows us to provide maximum sequential I/O throughput for the majority of
90a3874b8bSToomas Soome  * I/O's issued since sequential I/O performance is significantly negatively
91a3874b8bSToomas Soome  * impacted if it is interleaved with random I/O.
92a3874b8bSToomas Soome  *
93a3874b8bSToomas Soome  * Implementation Notes
94a3874b8bSToomas Soome  *
95a3874b8bSToomas Soome  * One side effect of the queued scanning algorithm is that the scanning code
96a3874b8bSToomas Soome  * needs to be notified whenever a block is freed. This is needed to allow
97a3874b8bSToomas Soome  * the scanning code to remove these I/Os from the issuing queue. Additionally,
98a3874b8bSToomas Soome  * we do not attempt to queue gang blocks to be issued sequentially since this
99a3874b8bSToomas Soome  * is very hard to do and would have an extremely limited performance benefit.
100a3874b8bSToomas Soome  * Instead, we simply issue gang I/Os as soon as we find them using the legacy
101a3874b8bSToomas Soome  * algorithm.
102a3874b8bSToomas Soome  *
103a3874b8bSToomas Soome  * Backwards compatibility
104a3874b8bSToomas Soome  *
105a3874b8bSToomas Soome  * This new algorithm is backwards compatible with the legacy on-disk data
106a3874b8bSToomas Soome  * structures (and therefore does not require a new feature flag).
107a3874b8bSToomas Soome  * Periodically during scanning (see zfs_scan_checkpoint_intval), the scan
108a3874b8bSToomas Soome  * will stop scanning metadata (in logical order) and wait for all outstanding
109a3874b8bSToomas Soome  * sorted I/O to complete. Once this is done, we write out a checkpoint
110a3874b8bSToomas Soome  * bookmark, indicating that we have scanned everything logically before it.
111a3874b8bSToomas Soome  * If the pool is imported on a machine without the new sorting algorithm,
112a3874b8bSToomas Soome  * the scan simply resumes from the last checkpoint using the legacy algorithm.
113a3874b8bSToomas Soome  */
114a3874b8bSToomas Soome 
1157802d7bfSMatthew Ahrens typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *,
1167802d7bfSMatthew Ahrens     const zbookmark_phys_t *);
1173f9d6ad7SLin Ling 
1183f9d6ad7SLin Ling static scan_cb_t dsl_scan_scrub_cb;
1193f9d6ad7SLin Ling 
120a3874b8bSToomas Soome static int scan_ds_queue_compare(const void *a, const void *b);
121a3874b8bSToomas Soome static int scan_prefetch_queue_compare(const void *a, const void *b);
122a3874b8bSToomas Soome static void scan_ds_queue_clear(dsl_scan_t *scn);
123a3874b8bSToomas Soome static boolean_t scan_ds_queue_contains(dsl_scan_t *scn, uint64_t dsobj,
124a3874b8bSToomas Soome     uint64_t *txg);
125a3874b8bSToomas Soome static void scan_ds_queue_insert(dsl_scan_t *scn, uint64_t dsobj, uint64_t txg);
126a3874b8bSToomas Soome static void scan_ds_queue_remove(dsl_scan_t *scn, uint64_t dsobj);
127a3874b8bSToomas Soome static void scan_ds_queue_sync(dsl_scan_t *scn, dmu_tx_t *tx);
128a3874b8bSToomas Soome 
129a3874b8bSToomas Soome extern int zfs_vdev_async_write_active_min_dirty_percent;
130a3874b8bSToomas Soome 
131a3874b8bSToomas Soome /*
132a3874b8bSToomas Soome  * By default zfs will check to ensure it is not over the hard memory
133a3874b8bSToomas Soome  * limit before each txg. If finer-grained control of this is needed
134a3874b8bSToomas Soome  * this value can be set to 1 to enable checking before scanning each
135a3874b8bSToomas Soome  * block.
136a3874b8bSToomas Soome  */
137a3874b8bSToomas Soome int zfs_scan_strict_mem_lim = B_FALSE;
138a3874b8bSToomas Soome 
139a3874b8bSToomas Soome /*
140a3874b8bSToomas Soome  * Maximum number of parallelly executing I/Os per top-level vdev.
141a3874b8bSToomas Soome  * Tune with care. Very high settings (hundreds) are known to trigger
142a3874b8bSToomas Soome  * some firmware bugs and resets on certain SSDs.
143a3874b8bSToomas Soome  */
14444ecc532SGeorge Wilson int zfs_top_maxinflight = 32;		/* maximum I/Os per top-level */
145a3874b8bSToomas Soome unsigned int zfs_resilver_delay = 2;	/* number of ticks to delay resilver */
146a3874b8bSToomas Soome unsigned int zfs_scrub_delay = 4;	/* number of ticks to delay scrub */
147a3874b8bSToomas Soome unsigned int zfs_scan_idle = 50;	/* idle window in clock ticks */
148a3874b8bSToomas Soome 
149a3874b8bSToomas Soome /*
150a3874b8bSToomas Soome  * Maximum number of parallelly executed bytes per leaf vdev. We attempt
151a3874b8bSToomas Soome  * to strike a balance here between keeping the vdev queues full of I/Os
152a3874b8bSToomas Soome  * at all times and not overflowing the queues to cause long latency,
153a3874b8bSToomas Soome  * which would cause long txg sync times. No matter what, we will not
154a3874b8bSToomas Soome  * overload the drives with I/O, since that is protected by
155a3874b8bSToomas Soome  * zfs_vdev_scrub_max_active.
156a3874b8bSToomas Soome  */
157a3874b8bSToomas Soome unsigned long zfs_scan_vdev_limit = 4 << 20;
158a3874b8bSToomas Soome 
159a3874b8bSToomas Soome int zfs_scan_issue_strategy = 0;
160a3874b8bSToomas Soome int zfs_scan_legacy = B_FALSE;	/* don't queue & sort zios, go direct */
161a3874b8bSToomas Soome uint64_t zfs_scan_max_ext_gap = 2 << 20;	/* in bytes */
162a3874b8bSToomas Soome 
163a3874b8bSToomas Soome unsigned int zfs_scan_checkpoint_intval = 7200;	/* seconds */
164a3874b8bSToomas Soome #define	ZFS_SCAN_CHECKPOINT_INTVAL	SEC_TO_TICK(zfs_scan_checkpoint_intval)
165a3874b8bSToomas Soome 
166a3874b8bSToomas Soome /*
167a3874b8bSToomas Soome  * fill_weight is non-tunable at runtime, so we copy it at module init from
168a3874b8bSToomas Soome  * zfs_scan_fill_weight. Runtime adjustments to zfs_scan_fill_weight would
169a3874b8bSToomas Soome  * break queue sorting.
170a3874b8bSToomas Soome  */
171a3874b8bSToomas Soome uint64_t zfs_scan_fill_weight = 3;
172a3874b8bSToomas Soome static uint64_t fill_weight;
173a3874b8bSToomas Soome 
174a3874b8bSToomas Soome /* See dsl_scan_should_clear() for details on the memory limit tunables */
175a3874b8bSToomas Soome uint64_t zfs_scan_mem_lim_min = 16 << 20;	/* bytes */
176a3874b8bSToomas Soome uint64_t zfs_scan_mem_lim_soft_max = 128 << 20;	/* bytes */
177a3874b8bSToomas Soome int zfs_scan_mem_lim_fact = 20;		/* fraction of physmem */
178a3874b8bSToomas Soome int zfs_scan_mem_lim_soft_fact = 20;	/* fraction of mem lim above */
179a3874b8bSToomas Soome 
180a3874b8bSToomas Soome unsigned int zfs_scrub_min_time_ms = 1000; /* min millisecs to scrub per txg */
181a3874b8bSToomas Soome unsigned int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
182a3874b8bSToomas Soome /* min millisecs to obsolete per txg */
183a3874b8bSToomas Soome unsigned int zfs_obsolete_min_time_ms = 500;
184a3874b8bSToomas Soome /* min millisecs to resilver per txg */
185a3874b8bSToomas Soome unsigned int zfs_resilver_min_time_ms = 3000;
186e4c795beSTom Caputi int zfs_scan_suspend_progress = 0; /* set to prevent scans from progressing */
1873f9d6ad7SLin Ling boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
1887fd05ac4SMatthew Ahrens boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable scrub prefetch */
1893f9d6ad7SLin Ling enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
190af3465daSMax Grossman /* max number of blocks to free in a single TXG */
1915cabbc6bSPrashanth Sreenivasa uint64_t zfs_async_block_max_blocks = UINT64_MAX;
1923f9d6ad7SLin Ling 
193e4c795beSTom Caputi int zfs_resilver_disable_defer = 0; /* set to disable resilver deferring */
194e4c795beSTom Caputi 
195a3874b8bSToomas Soome /*
196a3874b8bSToomas Soome  * We wait a few txgs after importing a pool to begin scanning so that
197a3874b8bSToomas Soome  * the import / mounting code isn't held up by scrub / resilver IO.
198a3874b8bSToomas Soome  * Unfortunately, it is a bit difficult to determine exactly how long
199a3874b8bSToomas Soome  * this will take since userspace will trigger fs mounts asynchronously
200a3874b8bSToomas Soome  * and the kernel will create zvol minors asynchronously. As a result,
201a3874b8bSToomas Soome  * the value provided here is a bit arbitrary, but represents a
202a3874b8bSToomas Soome  * reasonable estimate of how many txgs it will take to finish fully
203a3874b8bSToomas Soome  * importing a pool
204a3874b8bSToomas Soome  */
205a3874b8bSToomas Soome #define	SCAN_IMPORT_WAIT_TXGS		5
206a3874b8bSToomas Soome 
207a3874b8bSToomas Soome 
2083f9d6ad7SLin Ling #define	DSL_SCAN_IS_SCRUB_RESILVER(scn) \
2093f9d6ad7SLin Ling 	((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
2103f9d6ad7SLin Ling 	(scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
2113f9d6ad7SLin Ling 
2123f9d6ad7SLin Ling extern int zfs_txg_timeout;
2133f9d6ad7SLin Ling 
214139510fbSGeorge Wilson /*
215139510fbSGeorge Wilson  * Enable/disable the processing of the free_bpobj object.
216139510fbSGeorge Wilson  */
217139510fbSGeorge Wilson boolean_t zfs_free_bpobj_enabled = B_TRUE;
218139510fbSGeorge Wilson 
2193f9d6ad7SLin Ling /* the order has to match pool_scan_type */
2203f9d6ad7SLin Ling static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
2213f9d6ad7SLin Ling 	NULL,
2223f9d6ad7SLin Ling 	dsl_scan_scrub_cb,	/* POOL_SCAN_SCRUB */
2233f9d6ad7SLin Ling 	dsl_scan_scrub_cb,	/* POOL_SCAN_RESILVER */
2243f9d6ad7SLin Ling };
2253f9d6ad7SLin Ling 
226a3874b8bSToomas Soome /* In core node for the scn->scn_queue. Represents a dataset to be scanned */
227a3874b8bSToomas Soome typedef struct {
228a3874b8bSToomas Soome 	uint64_t	sds_dsobj;
229a3874b8bSToomas Soome 	uint64_t	sds_txg;
230a3874b8bSToomas Soome 	avl_node_t	sds_node;
231a3874b8bSToomas Soome } scan_ds_t;
232a3874b8bSToomas Soome 
233a3874b8bSToomas Soome /*
234a3874b8bSToomas Soome  * This controls what conditions are placed on dsl_scan_sync_state():
235a3874b8bSToomas Soome  * SYNC_OPTIONAL) write out scn_phys iff scn_bytes_pending == 0
236a3874b8bSToomas Soome  * SYNC_MANDATORY) write out scn_phys always. scn_bytes_pending must be 0.
237a3874b8bSToomas Soome  * SYNC_CACHED) if scn_bytes_pending == 0, write out scn_phys. Otherwise
238a3874b8bSToomas Soome  *	write out the scn_phys_cached version.
239a3874b8bSToomas Soome  * See dsl_scan_sync_state for details.
240a3874b8bSToomas Soome  */
241a3874b8bSToomas Soome typedef enum {
242a3874b8bSToomas Soome 	SYNC_OPTIONAL,
243a3874b8bSToomas Soome 	SYNC_MANDATORY,
244a3874b8bSToomas Soome 	SYNC_CACHED
245a3874b8bSToomas Soome } state_sync_type_t;
246a3874b8bSToomas Soome 
247a3874b8bSToomas Soome /*
248a3874b8bSToomas Soome  * This struct represents the minimum information needed to reconstruct a
249a3874b8bSToomas Soome  * zio for sequential scanning. This is useful because many of these will
250a3874b8bSToomas Soome  * accumulate in the sequential IO queues before being issued, so saving
251a3874b8bSToomas Soome  * memory matters here.
252a3874b8bSToomas Soome  */
253a3874b8bSToomas Soome typedef struct scan_io {
254a3874b8bSToomas Soome 	/* fields from blkptr_t */
255a3874b8bSToomas Soome 	uint64_t		sio_blk_prop;
256a3874b8bSToomas Soome 	uint64_t		sio_phys_birth;
257a3874b8bSToomas Soome 	uint64_t		sio_birth;
258a3874b8bSToomas Soome 	zio_cksum_t		sio_cksum;
25912a8814cSTom Caputi 	uint32_t		sio_nr_dvas;
260a3874b8bSToomas Soome 
261a3874b8bSToomas Soome 	/* fields from zio_t */
26212a8814cSTom Caputi 	uint32_t		sio_flags;
263a3874b8bSToomas Soome 	zbookmark_phys_t	sio_zb;
264a3874b8bSToomas Soome 
265a3874b8bSToomas Soome 	/* members for queue sorting */
266a3874b8bSToomas Soome 	union {
26712a8814cSTom Caputi 		avl_node_t	sio_addr_node; /* link into issuing queue */
268a3874b8bSToomas Soome 		list_node_t	sio_list_node; /* link for issuing to disk */
269a3874b8bSToomas Soome 	} sio_nodes;
27012a8814cSTom Caputi 
27112a8814cSTom Caputi 	/*
27212a8814cSTom Caputi 	 * There may be up to SPA_DVAS_PER_BP DVAs here from the bp,
27312a8814cSTom Caputi 	 * depending on how many were in the original bp. Only the
27412a8814cSTom Caputi 	 * first DVA is really used for sorting and issuing purposes.
27512a8814cSTom Caputi 	 * The other DVAs (if provided) simply exist so that the zio
27612a8814cSTom Caputi 	 * layer can find additional copies to repair from in the
27712a8814cSTom Caputi 	 * event of an error. This array must go at the end of the
27812a8814cSTom Caputi 	 * struct to allow this for the variable number of elements.
27912a8814cSTom Caputi 	 */
28012a8814cSTom Caputi 	dva_t			sio_dva[0];
281a3874b8bSToomas Soome } scan_io_t;
282a3874b8bSToomas Soome 
28312a8814cSTom Caputi #define	SIO_SET_OFFSET(sio, x)		DVA_SET_OFFSET(&(sio)->sio_dva[0], x)
28412a8814cSTom Caputi #define	SIO_SET_ASIZE(sio, x)		DVA_SET_ASIZE(&(sio)->sio_dva[0], x)
28512a8814cSTom Caputi #define	SIO_GET_OFFSET(sio)		DVA_GET_OFFSET(&(sio)->sio_dva[0])
28612a8814cSTom Caputi #define	SIO_GET_ASIZE(sio)		DVA_GET_ASIZE(&(sio)->sio_dva[0])
28712a8814cSTom Caputi #define	SIO_GET_END_OFFSET(sio)		\
28812a8814cSTom Caputi 	(SIO_GET_OFFSET(sio) + SIO_GET_ASIZE(sio))
28912a8814cSTom Caputi #define	SIO_GET_MUSED(sio)		\
29012a8814cSTom Caputi 	(sizeof (scan_io_t) + ((sio)->sio_nr_dvas * sizeof (dva_t)))
29112a8814cSTom Caputi 
292a3874b8bSToomas Soome struct dsl_scan_io_queue {
293a3874b8bSToomas Soome 	dsl_scan_t	*q_scn; /* associated dsl_scan_t */
294a3874b8bSToomas Soome 	vdev_t		*q_vd; /* top-level vdev that this queue represents */
295a3874b8bSToomas Soome 
296a3874b8bSToomas Soome 	/* trees used for sorting I/Os and extents of I/Os */
297a3874b8bSToomas Soome 	range_tree_t	*q_exts_by_addr;
2984d7988d6SPaul Dagnelie 	zfs_btree_t		q_exts_by_size;
299a3874b8bSToomas Soome 	avl_tree_t	q_sios_by_addr;
30012a8814cSTom Caputi 	uint64_t	q_sio_memused;
301a3874b8bSToomas Soome 
302a3874b8bSToomas Soome 	/* members for zio rate limiting */
303a3874b8bSToomas Soome 	uint64_t	q_maxinflight_bytes;
304a3874b8bSToomas Soome 	uint64_t	q_inflight_bytes;
305a3874b8bSToomas Soome 	kcondvar_t	q_zio_cv; /* used under vd->vdev_scan_io_queue_lock */
306a3874b8bSToomas Soome 
307a3874b8bSToomas Soome 	/* per txg statistics */
308a3874b8bSToomas Soome 	uint64_t	q_total_seg_size_this_txg;
309a3874b8bSToomas Soome 	uint64_t	q_segs_this_txg;
310a3874b8bSToomas Soome 	uint64_t	q_total_zio_size_this_txg;
311a3874b8bSToomas Soome 	uint64_t	q_zios_this_txg;
312a3874b8bSToomas Soome };
313a3874b8bSToomas Soome 
314a3874b8bSToomas Soome /* private data for dsl_scan_prefetch_cb() */
315a3874b8bSToomas Soome typedef struct scan_prefetch_ctx {
316a3874b8bSToomas Soome 	zfs_refcount_t spc_refcnt;	/* refcount for memory management */
317a3874b8bSToomas Soome 	dsl_scan_t *spc_scn;		/* dsl_scan_t for the pool */
318a3874b8bSToomas Soome 	boolean_t spc_root;		/* is this prefetch for an objset? */
319a3874b8bSToomas Soome 	uint8_t spc_indblkshift;	/* dn_indblkshift of current dnode */
320a3874b8bSToomas Soome 	uint16_t spc_datablkszsec;	/* dn_idatablkszsec of current dnode */
321a3874b8bSToomas Soome } scan_prefetch_ctx_t;
322a3874b8bSToomas Soome 
323a3874b8bSToomas Soome /* private data for dsl_scan_prefetch() */
324a3874b8bSToomas Soome typedef struct scan_prefetch_issue_ctx {
325a3874b8bSToomas Soome 	avl_node_t spic_avl_node;	/* link into scn->scn_prefetch_queue */
326a3874b8bSToomas Soome 	scan_prefetch_ctx_t *spic_spc;	/* spc for the callback */
327a3874b8bSToomas Soome 	blkptr_t spic_bp;		/* bp to prefetch */
328a3874b8bSToomas Soome 	zbookmark_phys_t spic_zb;	/* bookmark to prefetch */
329a3874b8bSToomas Soome } scan_prefetch_issue_ctx_t;
330a3874b8bSToomas Soome 
331a3874b8bSToomas Soome static void scan_exec_io(dsl_pool_t *dp, const blkptr_t *bp, int zio_flags,
332a3874b8bSToomas Soome     const zbookmark_phys_t *zb, dsl_scan_io_queue_t *queue);
333a3874b8bSToomas Soome static void scan_io_queue_insert_impl(dsl_scan_io_queue_t *queue,
334a3874b8bSToomas Soome     scan_io_t *sio);
335a3874b8bSToomas Soome 
336a3874b8bSToomas Soome static dsl_scan_io_queue_t *scan_io_queue_create(vdev_t *vd);
337a3874b8bSToomas Soome static void scan_io_queues_destroy(dsl_scan_t *scn);
338a3874b8bSToomas Soome 
33912a8814cSTom Caputi static kmem_cache_t *sio_cache[SPA_DVAS_PER_BP];
34012a8814cSTom Caputi 
34112a8814cSTom Caputi /* sio->sio_nr_dvas must be set so we know which cache to free from */
34212a8814cSTom Caputi static void
34312a8814cSTom Caputi sio_free(scan_io_t *sio)
34412a8814cSTom Caputi {
34512a8814cSTom Caputi 	ASSERT3U(sio->sio_nr_dvas, >, 0);
34612a8814cSTom Caputi 	ASSERT3U(sio->sio_nr_dvas, <=, SPA_DVAS_PER_BP);
34712a8814cSTom Caputi 
34812a8814cSTom Caputi 	kmem_cache_free(sio_cache[sio->sio_nr_dvas - 1], sio);
34912a8814cSTom Caputi }
35012a8814cSTom Caputi 
35112a8814cSTom Caputi /* It is up to the caller to set sio->sio_nr_dvas for freeing */
35212a8814cSTom Caputi static scan_io_t *
35312a8814cSTom Caputi sio_alloc(unsigned short nr_dvas)
35412a8814cSTom Caputi {
35512a8814cSTom Caputi 	ASSERT3U(nr_dvas, >, 0);
35612a8814cSTom Caputi 	ASSERT3U(nr_dvas, <=, SPA_DVAS_PER_BP);
35712a8814cSTom Caputi 
35812a8814cSTom Caputi 	return (kmem_cache_alloc(sio_cache[nr_dvas - 1], KM_SLEEP));
35912a8814cSTom Caputi }
360a3874b8bSToomas Soome 
361a3874b8bSToomas Soome void
362a3874b8bSToomas Soome scan_init(void)
363a3874b8bSToomas Soome {
364a3874b8bSToomas Soome 	/*
365a3874b8bSToomas Soome 	 * This is used in ext_size_compare() to weight segments
366a3874b8bSToomas Soome 	 * based on how sparse they are. This cannot be changed
367a3874b8bSToomas Soome 	 * mid-scan and the tree comparison functions don't currently
368a3874b8bSToomas Soome 	 * have a mechansim for passing additional context to the
369a3874b8bSToomas Soome 	 * compare functions. Thus we store this value globally and
370a3874b8bSToomas Soome 	 * we only allow it to be set at module intiailization time
371a3874b8bSToomas Soome 	 */
372a3874b8bSToomas Soome 	fill_weight = zfs_scan_fill_weight;
373a3874b8bSToomas Soome 
37412a8814cSTom Caputi 	for (int i = 0; i < SPA_DVAS_PER_BP; i++) {
37512a8814cSTom Caputi 		char name[36];
37612a8814cSTom Caputi 
37712a8814cSTom Caputi 		(void) sprintf(name, "sio_cache_%d", i);
37812a8814cSTom Caputi 		sio_cache[i] = kmem_cache_create(name,
37912a8814cSTom Caputi 		    (sizeof (scan_io_t) + ((i + 1) * sizeof (dva_t))),
38012a8814cSTom Caputi 		    0, NULL, NULL, NULL, NULL, NULL, 0);
38112a8814cSTom Caputi 	}
382a3874b8bSToomas Soome }
383a3874b8bSToomas Soome 
384a3874b8bSToomas Soome void
385a3874b8bSToomas Soome scan_fini(void)
386a3874b8bSToomas Soome {
38712a8814cSTom Caputi 	for (int i = 0; i < SPA_DVAS_PER_BP; i++) {
38812a8814cSTom Caputi 		kmem_cache_destroy(sio_cache[i]);
38912a8814cSTom Caputi 	}
390a3874b8bSToomas Soome }
391a3874b8bSToomas Soome 
392a3874b8bSToomas Soome static inline boolean_t
393a3874b8bSToomas Soome dsl_scan_is_running(const dsl_scan_t *scn)
394a3874b8bSToomas Soome {
395a3874b8bSToomas Soome 	return (scn->scn_phys.scn_state == DSS_SCANNING);
396a3874b8bSToomas Soome }
397a3874b8bSToomas Soome 
398a3874b8bSToomas Soome boolean_t
399a3874b8bSToomas Soome dsl_scan_resilvering(dsl_pool_t *dp)
400a3874b8bSToomas Soome {
401a3874b8bSToomas Soome 	return (dsl_scan_is_running(dp->dp_scan) &&
402a3874b8bSToomas Soome 	    dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
403a3874b8bSToomas Soome }
404a3874b8bSToomas Soome 
405a3874b8bSToomas Soome static inline void
40612a8814cSTom Caputi sio2bp(const scan_io_t *sio, blkptr_t *bp)
407a3874b8bSToomas Soome {
408a3874b8bSToomas Soome 	bzero(bp, sizeof (*bp));
409a3874b8bSToomas Soome 	bp->blk_prop = sio->sio_blk_prop;
410a3874b8bSToomas Soome 	bp->blk_phys_birth = sio->sio_phys_birth;
411a3874b8bSToomas Soome 	bp->blk_birth = sio->sio_birth;
412a3874b8bSToomas Soome 	bp->blk_fill = 1;	/* we always only work with data pointers */
413a3874b8bSToomas Soome 	bp->blk_cksum = sio->sio_cksum;
41412a8814cSTom Caputi 
41512a8814cSTom Caputi 	ASSERT3U(sio->sio_nr_dvas, >, 0);
41612a8814cSTom Caputi 	ASSERT3U(sio->sio_nr_dvas, <=, SPA_DVAS_PER_BP);
41712a8814cSTom Caputi 
41812a8814cSTom Caputi 	bcopy(sio->sio_dva, bp->blk_dva, sio->sio_nr_dvas * sizeof (dva_t));
419a3874b8bSToomas Soome }
420a3874b8bSToomas Soome 
421a3874b8bSToomas Soome static inline void
422a3874b8bSToomas Soome bp2sio(const blkptr_t *bp, scan_io_t *sio, int dva_i)
423a3874b8bSToomas Soome {
424a3874b8bSToomas Soome 	sio->sio_blk_prop = bp->blk_prop;
425a3874b8bSToomas Soome 	sio->sio_phys_birth = bp->blk_phys_birth;
426a3874b8bSToomas Soome 	sio->sio_birth = bp->blk_birth;
427a3874b8bSToomas Soome 	sio->sio_cksum = bp->blk_cksum;
42812a8814cSTom Caputi 	sio->sio_nr_dvas = BP_GET_NDVAS(bp);
42912a8814cSTom Caputi 
43012a8814cSTom Caputi 	/*
43112a8814cSTom Caputi 	 * Copy the DVAs to the sio. We need all copies of the block so
43212a8814cSTom Caputi 	 * that the self healing code can use the alternate copies if the
43312a8814cSTom Caputi 	 * first is corrupted. We want the DVA at index dva_i to be first
43412a8814cSTom Caputi 	 * in the sio since this is the primary one that we want to issue.
43512a8814cSTom Caputi 	 */
43612a8814cSTom Caputi 	for (int i = 0, j = dva_i; i < sio->sio_nr_dvas; i++, j++) {
43712a8814cSTom Caputi 		sio->sio_dva[i] = bp->blk_dva[j % sio->sio_nr_dvas];
43812a8814cSTom Caputi 	}
439a3874b8bSToomas Soome }
440a3874b8bSToomas Soome 
4413f9d6ad7SLin Ling int
4423f9d6ad7SLin Ling dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
4433f9d6ad7SLin Ling {
4443f9d6ad7SLin Ling 	int err;
4453f9d6ad7SLin Ling 	dsl_scan_t *scn;
4463f9d6ad7SLin Ling 	spa_t *spa = dp->dp_spa;
4473f9d6ad7SLin Ling 	uint64_t f;
4483f9d6ad7SLin Ling 
4493f9d6ad7SLin Ling 	scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
4503f9d6ad7SLin Ling 	scn->scn_dp = dp;
4513f9d6ad7SLin Ling 
4524a923759SGeorge Wilson 	/*
4534a923759SGeorge Wilson 	 * It's possible that we're resuming a scan after a reboot so
4544a923759SGeorge Wilson 	 * make sure that the scan_async_destroying flag is initialized
4554a923759SGeorge Wilson 	 * appropriately.
4564a923759SGeorge Wilson 	 */
4574a923759SGeorge Wilson 	ASSERT(!scn->scn_async_destroying);
4584a923759SGeorge Wilson 	scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa,
4592acef22dSMatthew Ahrens 	    SPA_FEATURE_ASYNC_DESTROY);
4604a923759SGeorge Wilson 
461a3874b8bSToomas Soome 	avl_create(&scn->scn_queue, scan_ds_queue_compare, sizeof (scan_ds_t),
462a3874b8bSToomas Soome 	    offsetof(scan_ds_t, sds_node));
463a3874b8bSToomas Soome 	avl_create(&scn->scn_prefetch_queue, scan_prefetch_queue_compare,
464a3874b8bSToomas Soome 	    sizeof (scan_prefetch_issue_ctx_t),
465a3874b8bSToomas Soome 	    offsetof(scan_prefetch_issue_ctx_t, spic_avl_node));
466a3874b8bSToomas Soome 
4673f9d6ad7SLin Ling 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4683f9d6ad7SLin Ling 	    "scrub_func", sizeof (uint64_t), 1, &f);
4693f9d6ad7SLin Ling 	if (err == 0) {
4703f9d6ad7SLin Ling 		/*
4713f9d6ad7SLin Ling 		 * There was an old-style scrub in progress.  Restart a
4723f9d6ad7SLin Ling 		 * new-style scrub from the beginning.
4733f9d6ad7SLin Ling 		 */
4743f9d6ad7SLin Ling 		scn->scn_restart_txg = txg;
4753f9d6ad7SLin Ling 		zfs_dbgmsg("old-style scrub was in progress; "
4763f9d6ad7SLin Ling 		    "restarting new-style scrub in txg %llu",
477a3874b8bSToomas Soome 		    (longlong_t)scn->scn_restart_txg);
4783f9d6ad7SLin Ling 
4793f9d6ad7SLin Ling 		/*
4803f9d6ad7SLin Ling 		 * Load the queue obj from the old location so that it
4813f9d6ad7SLin Ling 		 * can be freed by dsl_scan_done().
4823f9d6ad7SLin Ling 		 */
4833f9d6ad7SLin Ling 		(void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4843f9d6ad7SLin Ling 		    "scrub_queue", sizeof (uint64_t), 1,
4853f9d6ad7SLin Ling 		    &scn->scn_phys.scn_queue_obj);
4863f9d6ad7SLin Ling 	} else {
4873f9d6ad7SLin Ling 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4883f9d6ad7SLin Ling 		    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
4893f9d6ad7SLin Ling 		    &scn->scn_phys);
490eb633035STom Caputi 
491eb633035STom Caputi 		/*
492eb633035STom Caputi 		 * Detect if the pool contains the signature of #2094.  If it
493eb633035STom Caputi 		 * does properly update the scn->scn_phys structure and notify
494eb633035STom Caputi 		 * the administrator by setting an errata for the pool.
495eb633035STom Caputi 		 */
496eb633035STom Caputi 		if (err == EOVERFLOW) {
497eb633035STom Caputi 			uint64_t zaptmp[SCAN_PHYS_NUMINTS + 1];
498eb633035STom Caputi 			VERIFY3S(SCAN_PHYS_NUMINTS, ==, 24);
499eb633035STom Caputi 			VERIFY3S(offsetof(dsl_scan_phys_t, scn_flags), ==,
500eb633035STom Caputi 			    (23 * sizeof (uint64_t)));
501eb633035STom Caputi 
502eb633035STom Caputi 			err = zap_lookup(dp->dp_meta_objset,
503eb633035STom Caputi 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SCAN,
504eb633035STom Caputi 			    sizeof (uint64_t), SCAN_PHYS_NUMINTS + 1, &zaptmp);
505eb633035STom Caputi 			if (err == 0) {
506eb633035STom Caputi 				uint64_t overflow = zaptmp[SCAN_PHYS_NUMINTS];
507eb633035STom Caputi 
508eb633035STom Caputi 				if (overflow & ~DSF_VISIT_DS_AGAIN ||
509eb633035STom Caputi 				    scn->scn_async_destroying) {
510eb633035STom Caputi 					spa->spa_errata =
511eb633035STom Caputi 					    ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY;
512eb633035STom Caputi 					return (EOVERFLOW);
513eb633035STom Caputi 				}
514eb633035STom Caputi 
515eb633035STom Caputi 				bcopy(zaptmp, &scn->scn_phys,
516eb633035STom Caputi 				    SCAN_PHYS_NUMINTS * sizeof (uint64_t));
517eb633035STom Caputi 				scn->scn_phys.scn_flags = overflow;
518eb633035STom Caputi 
519eb633035STom Caputi 				/* Required scrub already in progress. */
520eb633035STom Caputi 				if (scn->scn_phys.scn_state == DSS_FINISHED ||
521eb633035STom Caputi 				    scn->scn_phys.scn_state == DSS_CANCELED)
522eb633035STom Caputi 					spa->spa_errata =
523eb633035STom Caputi 					    ZPOOL_ERRATA_ZOL_2094_SCRUB;
524eb633035STom Caputi 			}
525eb633035STom Caputi 		}
526eb633035STom Caputi 
5273f9d6ad7SLin Ling 		if (err == ENOENT)
5283f9d6ad7SLin Ling 			return (0);
5293f9d6ad7SLin Ling 		else if (err)
5303f9d6ad7SLin Ling 			return (err);
5313f9d6ad7SLin Ling 
532a3874b8bSToomas Soome 		/*
533a3874b8bSToomas Soome 		 * We might be restarting after a reboot, so jump the issued
534a3874b8bSToomas Soome 		 * counter to how far we've scanned. We know we're consistent
535a3874b8bSToomas Soome 		 * up to here.
536a3874b8bSToomas Soome 		 */
537a3874b8bSToomas Soome 		scn->scn_issued_before_pass = scn->scn_phys.scn_examined;
538a3874b8bSToomas Soome 
539a3874b8bSToomas Soome 		if (dsl_scan_is_running(scn) &&
5403f9d6ad7SLin Ling 		    spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
5413f9d6ad7SLin Ling 			/*
5423f9d6ad7SLin Ling 			 * A new-type scrub was in progress on an old
5433f9d6ad7SLin Ling 			 * pool, and the pool was accessed by old
5443f9d6ad7SLin Ling 			 * software.  Restart from the beginning, since
5453f9d6ad7SLin Ling 			 * the old software may have changed the pool in
5463f9d6ad7SLin Ling 			 * the meantime.
5473f9d6ad7SLin Ling 			 */
5483f9d6ad7SLin Ling 			scn->scn_restart_txg = txg;
5493f9d6ad7SLin Ling 			zfs_dbgmsg("new-style scrub was modified "
5503f9d6ad7SLin Ling 			    "by old software; restarting in txg %llu",
551a3874b8bSToomas Soome 			    (longlong_t)scn->scn_restart_txg);
552*165c5c6fSJohn Poduska 		} else if (dsl_scan_resilvering(dp)) {
553*165c5c6fSJohn Poduska 			/*
554*165c5c6fSJohn Poduska 			 * If a resilver is in progress and there are already
555*165c5c6fSJohn Poduska 			 * errors, restart it instead of finishing this scan and
556*165c5c6fSJohn Poduska 			 * then restarting it. If there haven't been any errors
557*165c5c6fSJohn Poduska 			 * then remember that the incore DTL is valid.
558*165c5c6fSJohn Poduska 			 */
559*165c5c6fSJohn Poduska 			if (scn->scn_phys.scn_errors > 0) {
560*165c5c6fSJohn Poduska 				scn->scn_restart_txg = txg;
561*165c5c6fSJohn Poduska 				zfs_dbgmsg("resilver can't excise DTL_MISSING "
562*165c5c6fSJohn Poduska 				    "when finished; restarting in txg %llu",
563*165c5c6fSJohn Poduska 				    (u_longlong_t)scn->scn_restart_txg);
564*165c5c6fSJohn Poduska 			} else {
565*165c5c6fSJohn Poduska 				/* it's safe to excise DTL when finished */
566*165c5c6fSJohn Poduska 				spa->spa_scrub_started = B_TRUE;
567*165c5c6fSJohn Poduska 			}
568a3874b8bSToomas Soome 		}
569a3874b8bSToomas Soome 	}
570a3874b8bSToomas Soome 
571e4c795beSTom Caputi 	bcopy(&scn->scn_phys, &scn->scn_phys_cached, sizeof (scn->scn_phys));
572e4c795beSTom Caputi 
573a3874b8bSToomas Soome 	/* reload the queue into the in-core state */
574a3874b8bSToomas Soome 	if (scn->scn_phys.scn_queue_obj != 0) {
575a3874b8bSToomas Soome 		zap_cursor_t zc;
576a3874b8bSToomas Soome 		zap_attribute_t za;
577a3874b8bSToomas Soome 
578a3874b8bSToomas Soome 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
579a3874b8bSToomas Soome 		    scn->scn_phys.scn_queue_obj);
580a3874b8bSToomas Soome 		    zap_cursor_retrieve(&zc, &za) == 0;
581a3874b8bSToomas Soome 		    (void) zap_cursor_advance(&zc)) {
582a3874b8bSToomas Soome 			scan_ds_queue_insert(scn,
583a3874b8bSToomas Soome 			    zfs_strtonum(za.za_name, NULL),
584a3874b8bSToomas Soome 			    za.za_first_integer);
5853f9d6ad7SLin Ling 		}
586a3874b8bSToomas Soome 		zap_cursor_fini(&zc);
5873f9d6ad7SLin Ling 	}
5883f9d6ad7SLin Ling 
5893f9d6ad7SLin Ling 	spa_scan_stat_init(spa);
5903f9d6ad7SLin Ling 	return (0);
5913f9d6ad7SLin Ling }
5923f9d6ad7SLin Ling 
5933f9d6ad7SLin Ling void
5943f9d6ad7SLin Ling dsl_scan_fini(dsl_pool_t *dp)
5953f9d6ad7SLin Ling {
596a3874b8bSToomas Soome 	if (dp->dp_scan != NULL) {
597a3874b8bSToomas Soome 		dsl_scan_t *scn = dp->dp_scan;
598a3874b8bSToomas Soome 
599a3874b8bSToomas Soome 		if (scn->scn_taskq != NULL)
600a3874b8bSToomas Soome 			taskq_destroy(scn->scn_taskq);
601a3874b8bSToomas Soome 		scan_ds_queue_clear(scn);
602a3874b8bSToomas Soome 		avl_destroy(&scn->scn_queue);
603a3874b8bSToomas Soome 		avl_destroy(&scn->scn_prefetch_queue);
604a3874b8bSToomas Soome 
6053f9d6ad7SLin Ling 		kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
6063f9d6ad7SLin Ling 		dp->dp_scan = NULL;
6073f9d6ad7SLin Ling 	}
6083f9d6ad7SLin Ling }
6093f9d6ad7SLin Ling 
610a3874b8bSToomas Soome static boolean_t
611a3874b8bSToomas Soome dsl_scan_restarting(dsl_scan_t *scn, dmu_tx_t *tx)
612a3874b8bSToomas Soome {
613a3874b8bSToomas Soome 	return (scn->scn_restart_txg != 0 &&
614a3874b8bSToomas Soome 	    scn->scn_restart_txg <= tx->tx_txg);
615a3874b8bSToomas Soome }
616a3874b8bSToomas Soome 
6170c06d385Sjwpoduska boolean_t
6180c06d385Sjwpoduska dsl_scan_resilver_scheduled(dsl_pool_t *dp)
6190c06d385Sjwpoduska {
6200c06d385Sjwpoduska 	return ((dp->dp_scan && dp->dp_scan->scn_restart_txg != 0) ||
6210c06d385Sjwpoduska 	    (spa_async_tasks(dp->dp_spa) & SPA_ASYNC_RESILVER));
6220c06d385Sjwpoduska }
6230c06d385Sjwpoduska 
624a3874b8bSToomas Soome boolean_t
625a3874b8bSToomas Soome dsl_scan_scrubbing(const dsl_pool_t *dp)
626a3874b8bSToomas Soome {
627a3874b8bSToomas Soome 	dsl_scan_phys_t *scn_phys = &dp->dp_scan->scn_phys;
628a3874b8bSToomas Soome 
629a3874b8bSToomas Soome 	return (scn_phys->scn_state == DSS_SCANNING &&
630a3874b8bSToomas Soome 	    scn_phys->scn_func == POOL_SCAN_SCRUB);
631a3874b8bSToomas Soome }
632a3874b8bSToomas Soome 
633a3874b8bSToomas Soome boolean_t
634a3874b8bSToomas Soome dsl_scan_is_paused_scrub(const dsl_scan_t *scn)
635a3874b8bSToomas Soome {
636a3874b8bSToomas Soome 	return (dsl_scan_scrubbing(scn->scn_dp) &&
637a3874b8bSToomas Soome 	    scn->scn_phys.scn_flags & DSF_SCRUB_PAUSED);
638a3874b8bSToomas Soome }
639a3874b8bSToomas Soome 
640a3874b8bSToomas Soome /*
641a3874b8bSToomas Soome  * Writes out a persistent dsl_scan_phys_t record to the pool directory.
642a3874b8bSToomas Soome  * Because we can be running in the block sorting algorithm, we do not always
643a3874b8bSToomas Soome  * want to write out the record, only when it is "safe" to do so. This safety
644a3874b8bSToomas Soome  * condition is achieved by making sure that the sorting queues are empty
645a3874b8bSToomas Soome  * (scn_bytes_pending == 0). When this condition is not true, the sync'd state
646a3874b8bSToomas Soome  * is inconsistent with how much actual scanning progress has been made. The
647a3874b8bSToomas Soome  * kind of sync to be performed is specified by the sync_type argument. If the
648a3874b8bSToomas Soome  * sync is optional, we only sync if the queues are empty. If the sync is
649a3874b8bSToomas Soome  * mandatory, we do a hard ASSERT to make sure that the queues are empty. The
650a3874b8bSToomas Soome  * third possible state is a "cached" sync. This is done in response to:
651a3874b8bSToomas Soome  * 1) The dataset that was in the last sync'd dsl_scan_phys_t having been
652a3874b8bSToomas Soome  *	destroyed, so we wouldn't be able to restart scanning from it.
653a3874b8bSToomas Soome  * 2) The snapshot that was in the last sync'd dsl_scan_phys_t having been
654a3874b8bSToomas Soome  *	superseded by a newer snapshot.
655a3874b8bSToomas Soome  * 3) The dataset that was in the last sync'd dsl_scan_phys_t having been
656a3874b8bSToomas Soome  *	swapped with its clone.
657a3874b8bSToomas Soome  * In all cases, a cached sync simply rewrites the last record we've written,
658a3874b8bSToomas Soome  * just slightly modified. For the modifications that are performed to the
659a3874b8bSToomas Soome  * last written dsl_scan_phys_t, see dsl_scan_ds_destroyed,
660a3874b8bSToomas Soome  * dsl_scan_ds_snapshotted and dsl_scan_ds_clone_swapped.
661a3874b8bSToomas Soome  */
662a3874b8bSToomas Soome static void
663a3874b8bSToomas Soome dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx, state_sync_type_t sync_type)
664a3874b8bSToomas Soome {
665a3874b8bSToomas Soome 	int i;
666a3874b8bSToomas Soome 	spa_t *spa = scn->scn_dp->dp_spa;
667a3874b8bSToomas Soome 
668a3874b8bSToomas Soome 	ASSERT(sync_type != SYNC_MANDATORY || scn->scn_bytes_pending == 0);
669a3874b8bSToomas Soome 	if (scn->scn_bytes_pending == 0) {
670a3874b8bSToomas Soome 		for (i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
671a3874b8bSToomas Soome 			vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
672a3874b8bSToomas Soome 			dsl_scan_io_queue_t *q = vd->vdev_scan_io_queue;
673a3874b8bSToomas Soome 
674a3874b8bSToomas Soome 			if (q == NULL)
675a3874b8bSToomas Soome 				continue;
676a3874b8bSToomas Soome 
677a3874b8bSToomas Soome 			mutex_enter(&vd->vdev_scan_io_queue_lock);
678a3874b8bSToomas Soome 			ASSERT3P(avl_first(&q->q_sios_by_addr), ==, NULL);
6794d7988d6SPaul Dagnelie 			ASSERT3P(zfs_btree_first(&q->q_exts_by_size, NULL), ==,
6804d7988d6SPaul Dagnelie 			    NULL);
681a3874b8bSToomas Soome 			ASSERT3P(range_tree_first(q->q_exts_by_addr), ==, NULL);
682a3874b8bSToomas Soome 			mutex_exit(&vd->vdev_scan_io_queue_lock);
683a3874b8bSToomas Soome 		}
684a3874b8bSToomas Soome 
685a3874b8bSToomas Soome 		if (scn->scn_phys.scn_queue_obj != 0)
686a3874b8bSToomas Soome 			scan_ds_queue_sync(scn, tx);
687a3874b8bSToomas Soome 		VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
688a3874b8bSToomas Soome 		    DMU_POOL_DIRECTORY_OBJECT,
689a3874b8bSToomas Soome 		    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
690a3874b8bSToomas Soome 		    &scn->scn_phys, tx));
691a3874b8bSToomas Soome 		bcopy(&scn->scn_phys, &scn->scn_phys_cached,
692a3874b8bSToomas Soome 		    sizeof (scn->scn_phys));
693a3874b8bSToomas Soome 
694a3874b8bSToomas Soome 		if (scn->scn_checkpointing)
695a3874b8bSToomas Soome 			zfs_dbgmsg("finish scan checkpoint");
696a3874b8bSToomas Soome 
697a3874b8bSToomas Soome 		scn->scn_checkpointing = B_FALSE;
698a3874b8bSToomas Soome 		scn->scn_last_checkpoint = ddi_get_lbolt();
699a3874b8bSToomas Soome 	} else if (sync_type == SYNC_CACHED) {
700a3874b8bSToomas Soome 		VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
701a3874b8bSToomas Soome 		    DMU_POOL_DIRECTORY_OBJECT,
702a3874b8bSToomas Soome 		    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
703a3874b8bSToomas Soome 		    &scn->scn_phys_cached, tx));
704a3874b8bSToomas Soome 	}
705a3874b8bSToomas Soome }
706a3874b8bSToomas Soome 
7073f9d6ad7SLin Ling /* ARGSUSED */
7083f9d6ad7SLin Ling static int
7093b2aab18SMatthew Ahrens dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
7103f9d6ad7SLin Ling {
7113b2aab18SMatthew Ahrens 	dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
7123f9d6ad7SLin Ling 
713a3874b8bSToomas Soome 	if (dsl_scan_is_running(scn))
714be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
7153f9d6ad7SLin Ling 
7163f9d6ad7SLin Ling 	return (0);
7173f9d6ad7SLin Ling }
7183f9d6ad7SLin Ling 
7193f9d6ad7SLin Ling static void
7203b2aab18SMatthew Ahrens dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
7213f9d6ad7SLin Ling {
7223b2aab18SMatthew Ahrens 	dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
7233b2aab18SMatthew Ahrens 	pool_scan_func_t *funcp = arg;
7243f9d6ad7SLin Ling 	dmu_object_type_t ot = 0;
7253f9d6ad7SLin Ling 	dsl_pool_t *dp = scn->scn_dp;
7263f9d6ad7SLin Ling 	spa_t *spa = dp->dp_spa;
7273f9d6ad7SLin Ling 
728a3874b8bSToomas Soome 	ASSERT(!dsl_scan_is_running(scn));
7293f9d6ad7SLin Ling 	ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
7303f9d6ad7SLin Ling 	bzero(&scn->scn_phys, sizeof (scn->scn_phys));
7313f9d6ad7SLin Ling 	scn->scn_phys.scn_func = *funcp;
7323f9d6ad7SLin Ling 	scn->scn_phys.scn_state = DSS_SCANNING;
7333f9d6ad7SLin Ling 	scn->scn_phys.scn_min_txg = 0;
7343f9d6ad7SLin Ling 	scn->scn_phys.scn_max_txg = tx->tx_txg;
7353f9d6ad7SLin Ling 	scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
7363f9d6ad7SLin Ling 	scn->scn_phys.scn_start_time = gethrestime_sec();
7373f9d6ad7SLin Ling 	scn->scn_phys.scn_errors = 0;
7383f9d6ad7SLin Ling 	scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
739a3874b8bSToomas Soome 	scn->scn_issued_before_pass = 0;
7403f9d6ad7SLin Ling 	scn->scn_restart_txg = 0;
741b4952e17SGeorge Wilson 	scn->scn_done_txg = 0;
742a3874b8bSToomas Soome 	scn->scn_last_checkpoint = 0;
743a3874b8bSToomas Soome 	scn->scn_checkpointing = B_FALSE;
7443f9d6ad7SLin Ling 	spa_scan_stat_init(spa);
7453f9d6ad7SLin Ling 
7463f9d6ad7SLin Ling 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
7473f9d6ad7SLin Ling 		scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
7483f9d6ad7SLin Ling 
7493f9d6ad7SLin Ling 		/* rewrite all disk labels */
7503f9d6ad7SLin Ling 		vdev_config_dirty(spa->spa_root_vdev);
7513f9d6ad7SLin Ling 
7523f9d6ad7SLin Ling 		if (vdev_resilver_needed(spa->spa_root_vdev,
7533f9d6ad7SLin Ling 		    &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
754ce1577b0SDave Eddy 			spa_event_notify(spa, NULL, NULL,
755ce1577b0SDave Eddy 			    ESC_ZFS_RESILVER_START);
7563f9d6ad7SLin Ling 		} else {
757ce1577b0SDave Eddy 			spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_START);
7583f9d6ad7SLin Ling 		}
7593f9d6ad7SLin Ling 
7603f9d6ad7SLin Ling 		spa->spa_scrub_started = B_TRUE;
7613f9d6ad7SLin Ling 		/*
7623f9d6ad7SLin Ling 		 * If this is an incremental scrub, limit the DDT scrub phase
7633f9d6ad7SLin Ling 		 * to just the auto-ditto class (for correctness); the rest
7643f9d6ad7SLin Ling 		 * of the scrub should go faster using top-down pruning.
7653f9d6ad7SLin Ling 		 */
7663f9d6ad7SLin Ling 		if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
7673f9d6ad7SLin Ling 			scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
7683f9d6ad7SLin Ling 
7693f9d6ad7SLin Ling 	}
7703f9d6ad7SLin Ling 
7713f9d6ad7SLin Ling 	/* back to the generic stuff */
7723f9d6ad7SLin Ling 
7733f9d6ad7SLin Ling 	if (dp->dp_blkstats == NULL) {
7743f9d6ad7SLin Ling 		dp->dp_blkstats =
7753f9d6ad7SLin Ling 		    kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
776a3874b8bSToomas Soome 		mutex_init(&dp->dp_blkstats->zab_lock, NULL,
777a3874b8bSToomas Soome 		    MUTEX_DEFAULT, NULL);
7783f9d6ad7SLin Ling 	}
779a3874b8bSToomas Soome 	bzero(&dp->dp_blkstats->zab_type, sizeof (dp->dp_blkstats->zab_type));
7803f9d6ad7SLin Ling 
7813f9d6ad7SLin Ling 	if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
7823f9d6ad7SLin Ling 		ot = DMU_OT_ZAP_OTHER;
7833f9d6ad7SLin Ling 
7843f9d6ad7SLin Ling 	scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
7853f9d6ad7SLin Ling 	    ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
7863f9d6ad7SLin Ling 
787a3874b8bSToomas Soome 	bcopy(&scn->scn_phys, &scn->scn_phys_cached, sizeof (scn->scn_phys));
788a3874b8bSToomas Soome 
789a3874b8bSToomas Soome 	dsl_scan_sync_state(scn, tx, SYNC_MANDATORY);
7903f9d6ad7SLin Ling 
7914445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "scan setup", tx,
7923f9d6ad7SLin Ling 	    "func=%u mintxg=%llu maxtxg=%llu",
7933f9d6ad7SLin Ling 	    *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
7943f9d6ad7SLin Ling }
7953f9d6ad7SLin Ling 
796a3874b8bSToomas Soome /*
797a3874b8bSToomas Soome  * Called by the ZFS_IOC_POOL_SCAN ioctl to start a scrub or resilver.
798a3874b8bSToomas Soome  * Can also be called to resume a paused scrub.
799a3874b8bSToomas Soome  */
800a3874b8bSToomas Soome int
801a3874b8bSToomas Soome dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
802a3874b8bSToomas Soome {
803a3874b8bSToomas Soome 	spa_t *spa = dp->dp_spa;
804a3874b8bSToomas Soome 	dsl_scan_t *scn = dp->dp_scan;
805a3874b8bSToomas Soome 
806a3874b8bSToomas Soome 	/*
807a3874b8bSToomas Soome 	 * Purge all vdev caches and probe all devices.  We do this here
808a3874b8bSToomas Soome 	 * rather than in sync context because this requires a writer lock
809a3874b8bSToomas Soome 	 * on the spa_config lock, which we can't do from sync context.  The
810a3874b8bSToomas Soome 	 * spa_scrub_reopen flag indicates that vdev_open() should not
811a3874b8bSToomas Soome 	 * attempt to start another scrub.
812a3874b8bSToomas Soome 	 */
813a3874b8bSToomas Soome 	spa_vdev_state_enter(spa, SCL_NONE);
814a3874b8bSToomas Soome 	spa->spa_scrub_reopen = B_TRUE;
815a3874b8bSToomas Soome 	vdev_reopen(spa->spa_root_vdev);
816a3874b8bSToomas Soome 	spa->spa_scrub_reopen = B_FALSE;
817a3874b8bSToomas Soome 	(void) spa_vdev_state_exit(spa, NULL, 0);
818a3874b8bSToomas Soome 
819e4c795beSTom Caputi 	if (func == POOL_SCAN_RESILVER) {
8200c06d385Sjwpoduska 		dsl_scan_restart_resilver(spa->spa_dsl_pool, 0);
821e4c795beSTom Caputi 		return (0);
822e4c795beSTom Caputi 	}
823e4c795beSTom Caputi 
824a3874b8bSToomas Soome 	if (func == POOL_SCAN_SCRUB && dsl_scan_is_paused_scrub(scn)) {
825a3874b8bSToomas Soome 		/* got scrub start cmd, resume paused scrub */
826a3874b8bSToomas Soome 		int err = dsl_scrub_set_pause_resume(scn->scn_dp,
827a3874b8bSToomas Soome 		    POOL_SCRUB_NORMAL);
828a3874b8bSToomas Soome 		if (err == 0) {
829a3874b8bSToomas Soome 			spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_RESUME);
830a3874b8bSToomas Soome 			return (ECANCELED);
831a3874b8bSToomas Soome 		}
832a3874b8bSToomas Soome 		return (SET_ERROR(err));
833a3874b8bSToomas Soome 	}
834a3874b8bSToomas Soome 
835a3874b8bSToomas Soome 	return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
836a3874b8bSToomas Soome 	    dsl_scan_setup_sync, &func, 0, ZFS_SPACE_CHECK_EXTRA_RESERVED));
837a3874b8bSToomas Soome }
838a3874b8bSToomas Soome 
8393f9d6ad7SLin Ling /* ARGSUSED */
8403f9d6ad7SLin Ling static void
8413f9d6ad7SLin Ling dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
8423f9d6ad7SLin Ling {
8433f9d6ad7SLin Ling 	static const char *old_names[] = {
8443f9d6ad7SLin Ling 		"scrub_bookmark",
8453f9d6ad7SLin Ling 		"scrub_ddt_bookmark",
8463f9d6ad7SLin Ling 		"scrub_ddt_class_max",
8473f9d6ad7SLin Ling 		"scrub_queue",
8483f9d6ad7SLin Ling 		"scrub_min_txg",
8493f9d6ad7SLin Ling 		"scrub_max_txg",
8503f9d6ad7SLin Ling 		"scrub_func",
8513f9d6ad7SLin Ling 		"scrub_errors",
8523f9d6ad7SLin Ling 		NULL
8533f9d6ad7SLin Ling 	};
8543f9d6ad7SLin Ling 
8553f9d6ad7SLin Ling 	dsl_pool_t *dp = scn->scn_dp;
8563f9d6ad7SLin Ling 	spa_t *spa = dp->dp_spa;
8573f9d6ad7SLin Ling 	int i;
8583f9d6ad7SLin Ling 
8593f9d6ad7SLin Ling 	/* Remove any remnants of an old-style scrub. */
8603f9d6ad7SLin Ling 	for (i = 0; old_names[i]; i++) {
8613f9d6ad7SLin Ling 		(void) zap_remove(dp->dp_meta_objset,
8623f9d6ad7SLin Ling 		    DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
8633f9d6ad7SLin Ling 	}
8643f9d6ad7SLin Ling 
8653f9d6ad7SLin Ling 	if (scn->scn_phys.scn_queue_obj != 0) {
866a3874b8bSToomas Soome 		VERIFY0(dmu_object_free(dp->dp_meta_objset,
8673f9d6ad7SLin Ling 		    scn->scn_phys.scn_queue_obj, tx));
8683f9d6ad7SLin Ling 		scn->scn_phys.scn_queue_obj = 0;
8693f9d6ad7SLin Ling 	}
870a3874b8bSToomas Soome 	scan_ds_queue_clear(scn);
8713f9d6ad7SLin Ling 
8721702cce7SAlek Pinchuk 	scn->scn_phys.scn_flags &= ~DSF_SCRUB_PAUSED;
8731702cce7SAlek Pinchuk 
8743f9d6ad7SLin Ling 	/*
8753f9d6ad7SLin Ling 	 * If we were "restarted" from a stopped state, don't bother
8763f9d6ad7SLin Ling 	 * with anything else.
8773f9d6ad7SLin Ling 	 */
878a3874b8bSToomas Soome 	if (!dsl_scan_is_running(scn)) {
879a3874b8bSToomas Soome 		ASSERT(!scn->scn_is_sorted);
8803f9d6ad7SLin Ling 		return;
881a3874b8bSToomas Soome 	}
8823f9d6ad7SLin Ling 
883a3874b8bSToomas Soome 	if (scn->scn_is_sorted) {
884a3874b8bSToomas Soome 		scan_io_queues_destroy(scn);
885a3874b8bSToomas Soome 		scn->scn_is_sorted = B_FALSE;
886a3874b8bSToomas Soome 
887a3874b8bSToomas Soome 		if (scn->scn_taskq != NULL) {
888a3874b8bSToomas Soome 			taskq_destroy(scn->scn_taskq);
889a3874b8bSToomas Soome 			scn->scn_taskq = NULL;
890a3874b8bSToomas Soome 		}
891a3874b8bSToomas Soome 	}
892a3874b8bSToomas Soome 
893a3874b8bSToomas Soome 	scn->scn_phys.scn_state = complete ? DSS_FINISHED : DSS_CANCELED;
8943f9d6ad7SLin Ling 
8951825bc56SNav Ravindranath 	if (dsl_scan_restarting(scn, tx))
8961825bc56SNav Ravindranath 		spa_history_log_internal(spa, "scan aborted, restarting", tx,
8971825bc56SNav Ravindranath 		    "errors=%llu", spa_get_errlog_size(spa));
8981825bc56SNav Ravindranath 	else if (!complete)
8991825bc56SNav Ravindranath 		spa_history_log_internal(spa, "scan cancelled", tx,
9001825bc56SNav Ravindranath 		    "errors=%llu", spa_get_errlog_size(spa));
9011825bc56SNav Ravindranath 	else
9021825bc56SNav Ravindranath 		spa_history_log_internal(spa, "scan done", tx,
9031825bc56SNav Ravindranath 		    "errors=%llu", spa_get_errlog_size(spa));
9043f9d6ad7SLin Ling 
9053f9d6ad7SLin Ling 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
9063f9d6ad7SLin Ling 		spa->spa_scrub_active = B_FALSE;
9073f9d6ad7SLin Ling 
9083f9d6ad7SLin Ling 		/*
9093f9d6ad7SLin Ling 		 * If the scrub/resilver completed, update all DTLs to
9103f9d6ad7SLin Ling 		 * reflect this.  Whether it succeeded or not, vacate
9113f9d6ad7SLin Ling 		 * all temporary scrub DTLs.
91286714001SSerapheim Dimitropoulos 		 *
91386714001SSerapheim Dimitropoulos 		 * As the scrub does not currently support traversing
91486714001SSerapheim Dimitropoulos 		 * data that have been freed but are part of a checkpoint,
91586714001SSerapheim Dimitropoulos 		 * we don't mark the scrub as done in the DTLs as faults
91686714001SSerapheim Dimitropoulos 		 * may still exist in those vdevs.
9173f9d6ad7SLin Ling 		 */
91886714001SSerapheim Dimitropoulos 		if (complete &&
91986714001SSerapheim Dimitropoulos 		    !spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
92086714001SSerapheim Dimitropoulos 			vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
92186714001SSerapheim Dimitropoulos 			    scn->scn_phys.scn_max_txg, B_TRUE);
92286714001SSerapheim Dimitropoulos 
923ce1577b0SDave Eddy 			spa_event_notify(spa, NULL, NULL,
924ce1577b0SDave Eddy 			    scn->scn_phys.scn_min_txg ?
9253f9d6ad7SLin Ling 			    ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
92686714001SSerapheim Dimitropoulos 		} else {
92786714001SSerapheim Dimitropoulos 			vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
92886714001SSerapheim Dimitropoulos 			    0, B_TRUE);
9293f9d6ad7SLin Ling 		}
9303f9d6ad7SLin Ling 		spa_errlog_rotate(spa);
9313f9d6ad7SLin Ling 
932*165c5c6fSJohn Poduska 		/*
933*165c5c6fSJohn Poduska 		 * Don't clear flag until after vdev_dtl_reassess to ensure that
934*165c5c6fSJohn Poduska 		 * DTL_MISSING will get updated when possible.
935*165c5c6fSJohn Poduska 		 */
936*165c5c6fSJohn Poduska 		spa->spa_scrub_started = B_FALSE;
937*165c5c6fSJohn Poduska 
9383f9d6ad7SLin Ling 		/*
9393f9d6ad7SLin Ling 		 * We may have finished replacing a device.
9403f9d6ad7SLin Ling 		 * Let the async thread assess this and handle the detach.
9413f9d6ad7SLin Ling 		 */
9423f9d6ad7SLin Ling 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
943e4c795beSTom Caputi 
944e4c795beSTom Caputi 		/*
9450c06d385Sjwpoduska 		 * Clear any resilver_deferred flags in the config.
946e4c795beSTom Caputi 		 * If there are drives that need resilvering, kick
947e4c795beSTom Caputi 		 * off an asynchronous request to start resilver.
9480c06d385Sjwpoduska 		 * vdev_clear_resilver_deferred() may update the config
949e4c795beSTom Caputi 		 * before the resilver can restart. In the event of
950e4c795beSTom Caputi 		 * a crash during this period, the spa loading code
951e4c795beSTom Caputi 		 * will find the drives that need to be resilvered
9520c06d385Sjwpoduska 		 * and start the resilver then.
953e4c795beSTom Caputi 		 */
9540c06d385Sjwpoduska 		if (spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER) &&
9550c06d385Sjwpoduska 		    vdev_clear_resilver_deferred(spa->spa_root_vdev, tx)) {
9560c06d385Sjwpoduska 			spa_history_log_internal(spa,
9570c06d385Sjwpoduska 			    "starting deferred resilver", tx, "errors=%llu",
9580c06d385Sjwpoduska 			    (u_longlong_t)spa_get_errlog_size(spa));
9590c06d385Sjwpoduska 			spa_async_request(spa, SPA_ASYNC_RESILVER);
960e4c795beSTom Caputi 		}
9613f9d6ad7SLin Ling 	}
9623f9d6ad7SLin Ling 
9633f9d6ad7SLin Ling 	scn->scn_phys.scn_end_time = gethrestime_sec();
964a3874b8bSToomas Soome 
965a3874b8bSToomas Soome 	ASSERT(!dsl_scan_is_running(scn));
9663f9d6ad7SLin Ling }
9673f9d6ad7SLin Ling 
9683f9d6ad7SLin Ling /* ARGSUSED */
9693f9d6ad7SLin Ling static int
9703b2aab18SMatthew Ahrens dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
9713f9d6ad7SLin Ling {
9723b2aab18SMatthew Ahrens 	dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
9733f9d6ad7SLin Ling 
974a3874b8bSToomas Soome 	if (!dsl_scan_is_running(scn))
975be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
9763f9d6ad7SLin Ling 	return (0);
9773f9d6ad7SLin Ling }
9783f9d6ad7SLin Ling 
9793f9d6ad7SLin Ling /* ARGSUSED */
9803f9d6ad7SLin Ling static void
9813b2aab18SMatthew Ahrens dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
9823f9d6ad7SLin Ling {
9833b2aab18SMatthew Ahrens 	dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
9843f9d6ad7SLin Ling 
9853f9d6ad7SLin Ling 	dsl_scan_done(scn, B_FALSE, tx);
986a3874b8bSToomas Soome 	dsl_scan_sync_state(scn, tx, SYNC_MANDATORY);
987301fd1d6SSean Eric Fagan 	spa_event_notify(scn->scn_dp->dp_spa, NULL, NULL, ESC_ZFS_SCRUB_ABORT);
9883f9d6ad7SLin Ling }
9893f9d6ad7SLin Ling 
9903f9d6ad7SLin Ling int
9913f9d6ad7SLin Ling dsl_scan_cancel(dsl_pool_t *dp)
9923f9d6ad7SLin Ling {
9933b2aab18SMatthew Ahrens 	return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
9947d46dc6cSMatthew Ahrens 	    dsl_scan_cancel_sync, NULL, 3, ZFS_SPACE_CHECK_RESERVED));
9953f9d6ad7SLin Ling }
9963f9d6ad7SLin Ling 
9971702cce7SAlek Pinchuk static int
9981702cce7SAlek Pinchuk dsl_scrub_pause_resume_check(void *arg, dmu_tx_t *tx)
9991702cce7SAlek Pinchuk {
10001702cce7SAlek Pinchuk 	pool_scrub_cmd_t *cmd = arg;
10011702cce7SAlek Pinchuk 	dsl_pool_t *dp = dmu_tx_pool(tx);
10021702cce7SAlek Pinchuk 	dsl_scan_t *scn = dp->dp_scan;
10031702cce7SAlek Pinchuk 
10041702cce7SAlek Pinchuk 	if (*cmd == POOL_SCRUB_PAUSE) {
10051702cce7SAlek Pinchuk 		/* can't pause a scrub when there is no in-progress scrub */
10061702cce7SAlek Pinchuk 		if (!dsl_scan_scrubbing(dp))
10071702cce7SAlek Pinchuk 			return (SET_ERROR(ENOENT));
10081702cce7SAlek Pinchuk 
10091702cce7SAlek Pinchuk 		/* can't pause a paused scrub */
10101702cce7SAlek Pinchuk 		if (dsl_scan_is_paused_scrub(scn))
10111702cce7SAlek Pinchuk 			return (SET_ERROR(EBUSY));
10121702cce7SAlek Pinchuk 	} else if (*cmd != POOL_SCRUB_NORMAL) {
10131702cce7SAlek Pinchuk 		return (SET_ERROR(ENOTSUP));
10141702cce7SAlek Pinchuk 	}
10151702cce7SAlek Pinchuk 
10161702cce7SAlek Pinchuk 	return (0);
10171702cce7SAlek Pinchuk }
10181702cce7SAlek Pinchuk 
10191702cce7SAlek Pinchuk static void
10201702cce7SAlek Pinchuk dsl_scrub_pause_resume_sync(void *arg, dmu_tx_t *tx)
10211702cce7SAlek Pinchuk {
10221702cce7SAlek Pinchuk 	pool_scrub_cmd_t *cmd = arg;
10231702cce7SAlek Pinchuk 	dsl_pool_t *dp = dmu_tx_pool(tx);
10241702cce7SAlek Pinchuk 	spa_t *spa = dp->dp_spa;
10251702cce7SAlek Pinchuk 	dsl_scan_t *scn = dp->dp_scan;
10261702cce7SAlek Pinchuk 
10271702cce7SAlek Pinchuk 	if (*cmd == POOL_SCRUB_PAUSE) {
10281702cce7SAlek Pinchuk 		/* can't pause a scrub when there is no in-progress scrub */
10291702cce7SAlek Pinchuk 		spa->spa_scan_pass_scrub_pause = gethrestime_sec();
10301702cce7SAlek Pinchuk 		scn->scn_phys.scn_flags |= DSF_SCRUB_PAUSED;
1031e4c795beSTom Caputi 		scn->scn_phys_cached.scn_flags |= DSF_SCRUB_PAUSED;
1032a3874b8bSToomas Soome 		dsl_scan_sync_state(scn, tx, SYNC_CACHED);
1033301fd1d6SSean Eric Fagan 		spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_PAUSED);
10341702cce7SAlek Pinchuk 	} else {
10351702cce7SAlek Pinchuk 		ASSERT3U(*cmd, ==, POOL_SCRUB_NORMAL);
10361702cce7SAlek Pinchuk 		if (dsl_scan_is_paused_scrub(scn)) {
10371702cce7SAlek Pinchuk 			/*
10381702cce7SAlek Pinchuk 			 * We need to keep track of how much time we spend
10391702cce7SAlek Pinchuk 			 * paused per pass so that we can adjust the scrub rate
10401702cce7SAlek Pinchuk 			 * shown in the output of 'zpool status'
10411702cce7SAlek Pinchuk 			 */
10421702cce7SAlek Pinchuk 			spa->spa_scan_pass_scrub_spent_paused +=
10431702cce7SAlek Pinchuk 			    gethrestime_sec() - spa->spa_scan_pass_scrub_pause;
10441702cce7SAlek Pinchuk 			spa->spa_scan_pass_scrub_pause = 0;
10451702cce7SAlek Pinchuk 			scn->scn_phys.scn_flags &= ~DSF_SCRUB_PAUSED;
1046e4c795beSTom Caputi 			scn->scn_phys_cached.scn_flags &= ~DSF_SCRUB_PAUSED;
1047a3874b8bSToomas Soome 			dsl_scan_sync_state(scn, tx, SYNC_CACHED);
10481702cce7SAlek Pinchuk 		}
10491702cce7SAlek Pinchuk 	}
10501702cce7SAlek Pinchuk }
10511702cce7SAlek Pinchuk 
10521702cce7SAlek Pinchuk /*
10531702cce7SAlek Pinchuk  * Set scrub pause/resume state if it makes sense to do so
10541702cce7SAlek Pinchuk  */
10551702cce7SAlek Pinchuk int
10561702cce7SAlek Pinchuk dsl_scrub_set_pause_resume(const dsl_pool_t *dp, pool_scrub_cmd_t cmd)
10571702cce7SAlek Pinchuk {
10581702cce7SAlek Pinchuk 	return (dsl_sync_task(spa_name(dp->dp_spa),
10591702cce7SAlek Pinchuk 	    dsl_scrub_pause_resume_check, dsl_scrub_pause_resume_sync, &cmd, 3,
10601702cce7SAlek Pinchuk 	    ZFS_SPACE_CHECK_RESERVED));
10611702cce7SAlek Pinchuk }
10621702cce7SAlek Pinchuk 
10631702cce7SAlek Pinchuk 
1064a3874b8bSToomas Soome /* start a new scan, or restart an existing one. */
1065a3874b8bSToomas Soome void
10660c06d385Sjwpoduska dsl_scan_restart_resilver(dsl_pool_t *dp, uint64_t txg)
1067a3874b8bSToomas Soome {
1068a3874b8bSToomas Soome 	if (txg == 0) {
1069a3874b8bSToomas Soome 		dmu_tx_t *tx;
1070a3874b8bSToomas Soome 		tx = dmu_tx_create_dd(dp->dp_mos_dir);
1071a3874b8bSToomas Soome 		VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
10721702cce7SAlek Pinchuk 
1073a3874b8bSToomas Soome 		txg = dmu_tx_get_txg(tx);
1074a3874b8bSToomas Soome 		dp->dp_scan->scn_restart_txg = txg;
1075a3874b8bSToomas Soome 		dmu_tx_commit(tx);
1076a3874b8bSToomas Soome 	} else {
1077a3874b8bSToomas Soome 		dp->dp_scan->scn_restart_txg = txg;
1078a3874b8bSToomas Soome 	}
1079a3874b8bSToomas Soome 	zfs_dbgmsg("restarting resilver txg=%llu", txg);
10801702cce7SAlek Pinchuk }
10811702cce7SAlek Pinchuk 
10823f9d6ad7SLin Ling void
10833f9d6ad7SLin Ling dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
10843f9d6ad7SLin Ling {
10853f9d6ad7SLin Ling 	zio_free(dp->dp_spa, txg, bp);
10863f9d6ad7SLin Ling }
10873f9d6ad7SLin Ling 
10883f9d6ad7SLin Ling void
10893f9d6ad7SLin Ling dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
10903f9d6ad7SLin Ling {
10913f9d6ad7SLin Ling 	ASSERT(dsl_pool_sync_context(dp));
10923f9d6ad7SLin Ling 	zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
10933f9d6ad7SLin Ling }
10943f9d6ad7SLin Ling 
1095a3874b8bSToomas Soome static int
1096a3874b8bSToomas Soome scan_ds_queue_compare(const void *a, const void *b)
10973f9d6ad7SLin Ling {
1098a3874b8bSToomas Soome 	const scan_ds_t *sds_a = a, *sds_b = b;
1099a3874b8bSToomas Soome 
1100a3874b8bSToomas Soome 	if (sds_a->sds_dsobj < sds_b->sds_dsobj)
1101a3874b8bSToomas Soome 		return (-1);
1102a3874b8bSToomas Soome 	if (sds_a->sds_dsobj == sds_b->sds_dsobj)
1103a3874b8bSToomas Soome 		return (0);
1104a3874b8bSToomas Soome 	return (1);
11053f9d6ad7SLin Ling }
11063f9d6ad7SLin Ling 
11073f9d6ad7SLin Ling static void
1108a3874b8bSToomas Soome scan_ds_queue_clear(dsl_scan_t *scn)
11093f9d6ad7SLin Ling {
1110a3874b8bSToomas Soome 	void *cookie = NULL;
1111a3874b8bSToomas Soome 	scan_ds_t *sds;
1112a3874b8bSToomas Soome 	while ((sds = avl_destroy_nodes(&scn->scn_queue, &cookie)) != NULL) {
1113a3874b8bSToomas Soome 		kmem_free(sds, sizeof (*sds));
1114a3874b8bSToomas Soome 	}
11153f9d6ad7SLin Ling }
11163f9d6ad7SLin Ling 
1117a3874b8bSToomas Soome static boolean_t
1118a3874b8bSToomas Soome scan_ds_queue_contains(dsl_scan_t *scn, uint64_t dsobj, uint64_t *txg)
1119a3874b8bSToomas Soome {
1120a3874b8bSToomas Soome 	scan_ds_t srch, *sds;
1121a3874b8bSToomas Soome 
1122a3874b8bSToomas Soome 	srch.sds_dsobj = dsobj;
1123a3874b8bSToomas Soome 	sds = avl_find(&scn->scn_queue, &srch, NULL);
1124a3874b8bSToomas Soome 	if (sds != NULL && txg != NULL)
1125a3874b8bSToomas Soome 		*txg = sds->sds_txg;
1126a3874b8bSToomas Soome 	return (sds != NULL);
1127a3874b8bSToomas Soome }
1128a3874b8bSToomas Soome 
1129a3874b8bSToomas Soome static void
1130a3874b8bSToomas Soome scan_ds_queue_insert(dsl_scan_t *scn, uint64_t dsobj, uint64_t txg)
1131a3874b8bSToomas Soome {
1132a3874b8bSToomas Soome 	scan_ds_t *sds;
1133a3874b8bSToomas Soome 	avl_index_t where;
1134a3874b8bSToomas Soome 
1135a3874b8bSToomas Soome 	sds = kmem_zalloc(sizeof (*sds), KM_SLEEP);
1136a3874b8bSToomas Soome 	sds->sds_dsobj = dsobj;
1137a3874b8bSToomas Soome 	sds->sds_txg = txg;
1138a3874b8bSToomas Soome 
1139a3874b8bSToomas Soome 	VERIFY3P(avl_find(&scn->scn_queue, sds, &where), ==, NULL);
1140a3874b8bSToomas Soome 	avl_insert(&scn->scn_queue, sds, where);
1141a3874b8bSToomas Soome }
1142a3874b8bSToomas Soome 
1143a3874b8bSToomas Soome static void
1144a3874b8bSToomas Soome scan_ds_queue_remove(dsl_scan_t *scn, uint64_t dsobj)
1145a3874b8bSToomas Soome {
1146a3874b8bSToomas Soome 	scan_ds_t srch, *sds;
1147a3874b8bSToomas Soome 
1148a3874b8bSToomas Soome 	srch.sds_dsobj = dsobj;
1149a3874b8bSToomas Soome 
1150a3874b8bSToomas Soome 	sds = avl_find(&scn->scn_queue, &srch, NULL);
1151a3874b8bSToomas Soome 	VERIFY(sds != NULL);
1152a3874b8bSToomas Soome 	avl_remove(&scn->scn_queue, sds);
1153a3874b8bSToomas Soome 	kmem_free(sds, sizeof (*sds));
1154a3874b8bSToomas Soome }
1155a3874b8bSToomas Soome 
1156a3874b8bSToomas Soome static void
1157a3874b8bSToomas Soome scan_ds_queue_sync(dsl_scan_t *scn, dmu_tx_t *tx)
1158a3874b8bSToomas Soome {
1159a3874b8bSToomas Soome 	dsl_pool_t *dp = scn->scn_dp;
1160a3874b8bSToomas Soome 	spa_t *spa = dp->dp_spa;
1161a3874b8bSToomas Soome 	dmu_object_type_t ot = (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) ?
1162a3874b8bSToomas Soome 	    DMU_OT_SCAN_QUEUE : DMU_OT_ZAP_OTHER;
1163a3874b8bSToomas Soome 
1164a3874b8bSToomas Soome 	ASSERT0(scn->scn_bytes_pending);
1165a3874b8bSToomas Soome 	ASSERT(scn->scn_phys.scn_queue_obj != 0);
1166a3874b8bSToomas Soome 
1167a3874b8bSToomas Soome 	VERIFY0(dmu_object_free(dp->dp_meta_objset,
1168a3874b8bSToomas Soome 	    scn->scn_phys.scn_queue_obj, tx));
1169a3874b8bSToomas Soome 	scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset, ot,
1170a3874b8bSToomas Soome 	    DMU_OT_NONE, 0, tx);
1171a3874b8bSToomas Soome 	for (scan_ds_t *sds = avl_first(&scn->scn_queue);
1172a3874b8bSToomas Soome 	    sds != NULL; sds = AVL_NEXT(&scn->scn_queue, sds)) {
1173a3874b8bSToomas Soome 		VERIFY0(zap_add_int_key(dp->dp_meta_objset,
1174a3874b8bSToomas Soome 		    scn->scn_phys.scn_queue_obj, sds->sds_dsobj,
1175a3874b8bSToomas Soome 		    sds->sds_txg, tx));
1176a3874b8bSToomas Soome 	}
1177a3874b8bSToomas Soome }
1178a3874b8bSToomas Soome 
1179a3874b8bSToomas Soome /*
1180a3874b8bSToomas Soome  * Computes the memory limit state that we're currently in. A sorted scan
1181a3874b8bSToomas Soome  * needs quite a bit of memory to hold the sorting queue, so we need to
1182a3874b8bSToomas Soome  * reasonably constrain the size so it doesn't impact overall system
1183a3874b8bSToomas Soome  * performance. We compute two limits:
1184a3874b8bSToomas Soome  * 1) Hard memory limit: if the amount of memory used by the sorting
1185a3874b8bSToomas Soome  *	queues on a pool gets above this value, we stop the metadata
1186a3874b8bSToomas Soome  *	scanning portion and start issuing the queued up and sorted
1187a3874b8bSToomas Soome  *	I/Os to reduce memory usage.
1188a3874b8bSToomas Soome  *	This limit is calculated as a fraction of physmem (by default 5%).
1189a3874b8bSToomas Soome  *	We constrain the lower bound of the hard limit to an absolute
1190a3874b8bSToomas Soome  *	minimum of zfs_scan_mem_lim_min (default: 16 MiB). We also constrain
1191a3874b8bSToomas Soome  *	the upper bound to 5% of the total pool size - no chance we'll
1192a3874b8bSToomas Soome  *	ever need that much memory, but just to keep the value in check.
1193a3874b8bSToomas Soome  * 2) Soft memory limit: once we hit the hard memory limit, we start
1194a3874b8bSToomas Soome  *	issuing I/O to reduce queue memory usage, but we don't want to
1195a3874b8bSToomas Soome  *	completely empty out the queues, since we might be able to find I/Os
1196a3874b8bSToomas Soome  *	that will fill in the gaps of our non-sequential IOs at some point
1197a3874b8bSToomas Soome  *	in the future. So we stop the issuing of I/Os once the amount of
1198a3874b8bSToomas Soome  *	memory used drops below the soft limit (at which point we stop issuing
1199a3874b8bSToomas Soome  *	I/O and start scanning metadata again).
1200a3874b8bSToomas Soome  *
1201a3874b8bSToomas Soome  *	This limit is calculated by subtracting a fraction of the hard
1202a3874b8bSToomas Soome  *	limit from the hard limit. By default this fraction is 5%, so
1203a3874b8bSToomas Soome  *	the soft limit is 95% of the hard limit. We cap the size of the
1204a3874b8bSToomas Soome  *	difference between the hard and soft limits at an absolute
1205a3874b8bSToomas Soome  *	maximum of zfs_scan_mem_lim_soft_max (default: 128 MiB) - this is
1206a3874b8bSToomas Soome  *	sufficient to not cause too frequent switching between the
1207a3874b8bSToomas Soome  *	metadata scan and I/O issue (even at 2k recordsize, 128 MiB's
1208a3874b8bSToomas Soome  *	worth of queues is about 1.2 GiB of on-pool data, so scanning
1209a3874b8bSToomas Soome  *	that should take at least a decent fraction of a second).
1210a3874b8bSToomas Soome  */
1211a3874b8bSToomas Soome static boolean_t
1212a3874b8bSToomas Soome dsl_scan_should_clear(dsl_scan_t *scn)
1213a3874b8bSToomas Soome {
12140c06d385Sjwpoduska 	spa_t *spa = scn->scn_dp->dp_spa;
1215a3874b8bSToomas Soome 	vdev_t *rvd = scn->scn_dp->dp_spa->spa_root_vdev;
12160c06d385Sjwpoduska 	uint64_t alloc, mlim_hard, mlim_soft, mused;
12170c06d385Sjwpoduska 
12180c06d385Sjwpoduska 	alloc = metaslab_class_get_alloc(spa_normal_class(spa));
12190c06d385Sjwpoduska 	alloc += metaslab_class_get_alloc(spa_special_class(spa));
12200c06d385Sjwpoduska 	alloc += metaslab_class_get_alloc(spa_dedup_class(spa));
1221a3874b8bSToomas Soome 
1222a3874b8bSToomas Soome 	mlim_hard = MAX((physmem / zfs_scan_mem_lim_fact) * PAGESIZE,
1223a3874b8bSToomas Soome 	    zfs_scan_mem_lim_min);
1224a3874b8bSToomas Soome 	mlim_hard = MIN(mlim_hard, alloc / 20);
1225a3874b8bSToomas Soome 	mlim_soft = mlim_hard - MIN(mlim_hard / zfs_scan_mem_lim_soft_fact,
1226a3874b8bSToomas Soome 	    zfs_scan_mem_lim_soft_max);
1227a3874b8bSToomas Soome 	mused = 0;
1228a3874b8bSToomas Soome 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
1229a3874b8bSToomas Soome 		vdev_t *tvd = rvd->vdev_child[i];
1230a3874b8bSToomas Soome 		dsl_scan_io_queue_t *queue;
1231a3874b8bSToomas Soome 
1232a3874b8bSToomas Soome 		mutex_enter(&tvd->vdev_scan_io_queue_lock);
1233a3874b8bSToomas Soome 		queue = tvd->vdev_scan_io_queue;
1234a3874b8bSToomas Soome 		if (queue != NULL) {
123512a8814cSTom Caputi 			/* # extents in exts_by_size = # in exts_by_addr */
12364d7988d6SPaul Dagnelie 			mused += zfs_btree_numnodes(&queue->q_exts_by_size) *
1237028b5df8SPaul Dagnelie 			    sizeof (range_seg_gap_t) + queue->q_sio_memused;
1238a3874b8bSToomas Soome 		}
1239a3874b8bSToomas Soome 		mutex_exit(&tvd->vdev_scan_io_queue_lock);
1240a3874b8bSToomas Soome 	}
1241a3874b8bSToomas Soome 
1242a3874b8bSToomas Soome 	dprintf("current scan memory usage: %llu bytes\n", (longlong_t)mused);
1243a3874b8bSToomas Soome 
1244a3874b8bSToomas Soome 	if (mused == 0)
1245a3874b8bSToomas Soome 		ASSERT0(scn->scn_bytes_pending);
1246a3874b8bSToomas Soome 
1247a3874b8bSToomas Soome 	/*
1248a3874b8bSToomas Soome 	 * If we are above our hard limit, we need to clear out memory.
1249a3874b8bSToomas Soome 	 * If we are below our soft limit, we need to accumulate sequential IOs.
1250a3874b8bSToomas Soome 	 * Otherwise, we should keep doing whatever we are currently doing.
1251a3874b8bSToomas Soome 	 */
1252a3874b8bSToomas Soome 	if (mused >= mlim_hard)
1253a3874b8bSToomas Soome 		return (B_TRUE);
1254a3874b8bSToomas Soome 	else if (mused < mlim_soft)
1255a3874b8bSToomas Soome 		return (B_FALSE);
1256a3874b8bSToomas Soome 	else
1257a3874b8bSToomas Soome 		return (scn->scn_clearing);
1258a3874b8bSToomas Soome }
12596f6a76adSMatthew Ahrens 
12603f9d6ad7SLin Ling static boolean_t
12611702cce7SAlek Pinchuk dsl_scan_check_suspend(dsl_scan_t *scn, const zbookmark_phys_t *zb)
12623f9d6ad7SLin Ling {
12633f9d6ad7SLin Ling 	/* we never skip user/group accounting objects */
12643f9d6ad7SLin Ling 	if (zb && (int64_t)zb->zb_object < 0)
12653f9d6ad7SLin Ling 		return (B_FALSE);
12663f9d6ad7SLin Ling 
12671702cce7SAlek Pinchuk 	if (scn->scn_suspending)
12681702cce7SAlek Pinchuk 		return (B_TRUE); /* we're already suspending */
12693f9d6ad7SLin Ling 
1270ad135b5dSChristopher Siden 	if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
12713f9d6ad7SLin Ling 		return (B_FALSE); /* we're resuming */
12723f9d6ad7SLin Ling 
12733f9d6ad7SLin Ling 	/* We only know how to resume from level-0 blocks. */
12743f9d6ad7SLin Ling 	if (zb && zb->zb_level != 0)
12753f9d6ad7SLin Ling 		return (B_FALSE);
12763f9d6ad7SLin Ling 
12776f6a76adSMatthew Ahrens 	/*
12781702cce7SAlek Pinchuk 	 * We suspend if:
12796f6a76adSMatthew Ahrens 	 *  - we have scanned for at least the minimum time (default 1 sec
12806f6a76adSMatthew Ahrens 	 *    for scrub, 3 sec for resilver), and either we have sufficient
12816f6a76adSMatthew Ahrens 	 *    dirty data that we are starting to write more quickly
12826f6a76adSMatthew Ahrens 	 *    (default 30%), or someone is explicitly waiting for this txg
12836f6a76adSMatthew Ahrens 	 *    to complete.
12846f6a76adSMatthew Ahrens 	 *  or
12856f6a76adSMatthew Ahrens 	 *  - the spa is shutting down because this pool is being exported
12866f6a76adSMatthew Ahrens 	 *    or the machine is rebooting.
1287a3874b8bSToomas Soome 	 *  or
1288a3874b8bSToomas Soome 	 *  - the scan queue has reached its memory use limit
12896f6a76adSMatthew Ahrens 	 */
1290a3874b8bSToomas Soome 	hrtime_t curr_time_ns = gethrtime();
1291a3874b8bSToomas Soome 	uint64_t scan_time_ns = curr_time_ns - scn->scn_sync_start_time;
1292a3874b8bSToomas Soome 	uint64_t sync_time_ns = curr_time_ns -
1293a3874b8bSToomas Soome 	    scn->scn_dp->dp_spa->spa_sync_starttime;
1294a3874b8bSToomas Soome 
12956f6a76adSMatthew Ahrens 	int dirty_pct = scn->scn_dp->dp_dirty_total * 100 / zfs_dirty_data_max;
1296a3874b8bSToomas Soome 	int mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
1297a3874b8bSToomas Soome 	    zfs_resilver_min_time_ms : zfs_scrub_min_time_ms;
1298a3874b8bSToomas Soome 
1299a3874b8bSToomas Soome 	if ((NSEC2MSEC(scan_time_ns) > mintime &&
1300a3874b8bSToomas Soome 	    (dirty_pct >= zfs_vdev_async_write_active_min_dirty_percent ||
1301a3874b8bSToomas Soome 	    txg_sync_waiting(scn->scn_dp) ||
1302a3874b8bSToomas Soome 	    NSEC2SEC(sync_time_ns) >= zfs_txg_timeout)) ||
1303a3874b8bSToomas Soome 	    spa_shutting_down(scn->scn_dp->dp_spa) ||
1304a3874b8bSToomas Soome 	    (zfs_scan_strict_mem_lim && dsl_scan_should_clear(scn))) {
13053f9d6ad7SLin Ling 		if (zb) {
13061702cce7SAlek Pinchuk 			dprintf("suspending at bookmark %llx/%llx/%llx/%llx\n",
13073f9d6ad7SLin Ling 			    (longlong_t)zb->zb_objset,
13083f9d6ad7SLin Ling 			    (longlong_t)zb->zb_object,
13093f9d6ad7SLin Ling 			    (longlong_t)zb->zb_level,
13103f9d6ad7SLin Ling 			    (longlong_t)zb->zb_blkid);
13113f9d6ad7SLin Ling 			scn->scn_phys.scn_bookmark = *zb;
1312a3874b8bSToomas Soome 		} else {
1313a3874b8bSToomas Soome 			dsl_scan_phys_t *scnp = &scn->scn_phys;
1314a3874b8bSToomas Soome 
1315a3874b8bSToomas Soome 			dprintf("suspending at DDT bookmark "
1316a3874b8bSToomas Soome 			    "%llx/%llx/%llx/%llx\n",
1317a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_ddt_bookmark.ddb_class,
1318a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_ddt_bookmark.ddb_type,
1319a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_ddt_bookmark.ddb_checksum,
1320a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_ddt_bookmark.ddb_cursor);
13213f9d6ad7SLin Ling 		}
13221702cce7SAlek Pinchuk 		scn->scn_suspending = B_TRUE;
13233f9d6ad7SLin Ling 		return (B_TRUE);
13243f9d6ad7SLin Ling 	}
13253f9d6ad7SLin Ling 	return (B_FALSE);
13263f9d6ad7SLin Ling }
13273f9d6ad7SLin Ling 
13283f9d6ad7SLin Ling typedef struct zil_scan_arg {
13293f9d6ad7SLin Ling 	dsl_pool_t	*zsa_dp;
13303f9d6ad7SLin Ling 	zil_header_t	*zsa_zh;
13313f9d6ad7SLin Ling } zil_scan_arg_t;
13323f9d6ad7SLin Ling 
13333f9d6ad7SLin Ling /* ARGSUSED */
13343f9d6ad7SLin Ling static int
13353f9d6ad7SLin Ling dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
13363f9d6ad7SLin Ling {
13373f9d6ad7SLin Ling 	zil_scan_arg_t *zsa = arg;
13383f9d6ad7SLin Ling 	dsl_pool_t *dp = zsa->zsa_dp;
13393f9d6ad7SLin Ling 	dsl_scan_t *scn = dp->dp_scan;
13403f9d6ad7SLin Ling 	zil_header_t *zh = zsa->zsa_zh;
13417802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
13423f9d6ad7SLin Ling 
134343466aaeSMax Grossman 	if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
13443f9d6ad7SLin Ling 		return (0);
13453f9d6ad7SLin Ling 
13463f9d6ad7SLin Ling 	/*
13473f9d6ad7SLin Ling 	 * One block ("stubby") can be allocated a long time ago; we
13483f9d6ad7SLin Ling 	 * want to visit that one because it has been allocated
13493f9d6ad7SLin Ling 	 * (on-disk) even if it hasn't been claimed (even though for
13503f9d6ad7SLin Ling 	 * scrub there's nothing to do to it).
13513f9d6ad7SLin Ling 	 */
135286714001SSerapheim Dimitropoulos 	if (claim_txg == 0 && bp->blk_birth >= spa_min_claim_txg(dp->dp_spa))
13533f9d6ad7SLin Ling 		return (0);
13543f9d6ad7SLin Ling 
13553f9d6ad7SLin Ling 	SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
13563f9d6ad7SLin Ling 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
13573f9d6ad7SLin Ling 
13583f9d6ad7SLin Ling 	VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
13593f9d6ad7SLin Ling 	return (0);
13603f9d6ad7SLin Ling }
13613f9d6ad7SLin Ling 
13623f9d6ad7SLin Ling /* ARGSUSED */
13633f9d6ad7SLin Ling static int
13643f9d6ad7SLin Ling dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
13653f9d6ad7SLin Ling {
13663f9d6ad7SLin Ling 	if (lrc->lrc_txtype == TX_WRITE) {
13673f9d6ad7SLin Ling 		zil_scan_arg_t *zsa = arg;
13683f9d6ad7SLin Ling 		dsl_pool_t *dp = zsa->zsa_dp;
13693f9d6ad7SLin Ling 		dsl_scan_t *scn = dp->dp_scan;
13703f9d6ad7SLin Ling 		zil_header_t *zh = zsa->zsa_zh;
13713f9d6ad7SLin Ling 		lr_write_t *lr = (lr_write_t *)lrc;
13723f9d6ad7SLin Ling 		blkptr_t *bp = &lr->lr_blkptr;
13737802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
13743f9d6ad7SLin Ling 
137543466aaeSMax Grossman 		if (BP_IS_HOLE(bp) ||
137643466aaeSMax Grossman 		    bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
13773f9d6ad7SLin Ling 			return (0);
13783f9d6ad7SLin Ling 
13793f9d6ad7SLin Ling 		/*
13803f9d6ad7SLin Ling 		 * birth can be < claim_txg if this record's txg is
13813f9d6ad7SLin Ling 		 * already txg sync'ed (but this log block contains
13823f9d6ad7SLin Ling 		 * other records that are not synced)
13833f9d6ad7SLin Ling 		 */
13843f9d6ad7SLin Ling 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
13853f9d6ad7SLin Ling 			return (0);
13863f9d6ad7SLin Ling 
13873f9d6ad7SLin Ling 		SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
13883f9d6ad7SLin Ling 		    lr->lr_foid, ZB_ZIL_LEVEL,
13893f9d6ad7SLin Ling 		    lr->lr_offset / BP_GET_LSIZE(bp));
13903f9d6ad7SLin Ling 
13913f9d6ad7SLin Ling 		VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
13923f9d6ad7SLin Ling 	}
13933f9d6ad7SLin Ling 	return (0);
13943f9d6ad7SLin Ling }
13953f9d6ad7SLin Ling 
13963f9d6ad7SLin Ling static void
13973f9d6ad7SLin Ling dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
13983f9d6ad7SLin Ling {
13993f9d6ad7SLin Ling 	uint64_t claim_txg = zh->zh_claim_txg;
14003f9d6ad7SLin Ling 	zil_scan_arg_t zsa = { dp, zh };
14013f9d6ad7SLin Ling 	zilog_t *zilog;
14023f9d6ad7SLin Ling 
140386714001SSerapheim Dimitropoulos 	ASSERT(spa_writeable(dp->dp_spa));
140486714001SSerapheim Dimitropoulos 
14053f9d6ad7SLin Ling 	/*
140686714001SSerapheim Dimitropoulos 	 * We only want to visit blocks that have been claimed
140786714001SSerapheim Dimitropoulos 	 * but not yet replayed.
14083f9d6ad7SLin Ling 	 */
140986714001SSerapheim Dimitropoulos 	if (claim_txg == 0)
14103f9d6ad7SLin Ling 		return;
14113f9d6ad7SLin Ling 
14123f9d6ad7SLin Ling 	zilog = zil_alloc(dp->dp_meta_objset, zh);
14133f9d6ad7SLin Ling 
14143f9d6ad7SLin Ling 	(void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
1415eb633035STom Caputi 	    claim_txg, B_FALSE);
14163f9d6ad7SLin Ling 
14173f9d6ad7SLin Ling 	zil_free(zilog);
14183f9d6ad7SLin Ling }
14193f9d6ad7SLin Ling 
1420a3874b8bSToomas Soome /*
1421a3874b8bSToomas Soome  * We compare scan_prefetch_issue_ctx_t's based on their bookmarks. The idea
1422a3874b8bSToomas Soome  * here is to sort the AVL tree by the order each block will be needed.
1423a3874b8bSToomas Soome  */
1424a3874b8bSToomas Soome static int
1425a3874b8bSToomas Soome scan_prefetch_queue_compare(const void *a, const void *b)
1426a3874b8bSToomas Soome {
1427a3874b8bSToomas Soome 	const scan_prefetch_issue_ctx_t *spic_a = a, *spic_b = b;
1428a3874b8bSToomas Soome 	const scan_prefetch_ctx_t *spc_a = spic_a->spic_spc;
1429a3874b8bSToomas Soome 	const scan_prefetch_ctx_t *spc_b = spic_b->spic_spc;
1430a3874b8bSToomas Soome 
1431a3874b8bSToomas Soome 	return (zbookmark_compare(spc_a->spc_datablkszsec,
1432a3874b8bSToomas Soome 	    spc_a->spc_indblkshift, spc_b->spc_datablkszsec,
1433a3874b8bSToomas Soome 	    spc_b->spc_indblkshift, &spic_a->spic_zb, &spic_b->spic_zb));
1434a3874b8bSToomas Soome }
1435a3874b8bSToomas Soome 
1436a3874b8bSToomas Soome static void
1437a3874b8bSToomas Soome scan_prefetch_ctx_rele(scan_prefetch_ctx_t *spc, void *tag)
1438a3874b8bSToomas Soome {
1439a3874b8bSToomas Soome 	if (zfs_refcount_remove(&spc->spc_refcnt, tag) == 0) {
1440a3874b8bSToomas Soome 		zfs_refcount_destroy(&spc->spc_refcnt);
1441a3874b8bSToomas Soome 		kmem_free(spc, sizeof (scan_prefetch_ctx_t));
1442a3874b8bSToomas Soome 	}
1443a3874b8bSToomas Soome }
1444a3874b8bSToomas Soome 
1445a3874b8bSToomas Soome static scan_prefetch_ctx_t *
1446a3874b8bSToomas Soome scan_prefetch_ctx_create(dsl_scan_t *scn, dnode_phys_t *dnp, void *tag)
1447a3874b8bSToomas Soome {
1448a3874b8bSToomas Soome 	scan_prefetch_ctx_t *spc;
1449a3874b8bSToomas Soome 
1450a3874b8bSToomas Soome 	spc = kmem_alloc(sizeof (scan_prefetch_ctx_t), KM_SLEEP);
1451a3874b8bSToomas Soome 	zfs_refcount_create(&spc->spc_refcnt);
1452a3874b8bSToomas Soome 	zfs_refcount_add(&spc->spc_refcnt, tag);
1453a3874b8bSToomas Soome 	spc->spc_scn = scn;
1454a3874b8bSToomas Soome 	if (dnp != NULL) {
1455a3874b8bSToomas Soome 		spc->spc_datablkszsec = dnp->dn_datablkszsec;
1456a3874b8bSToomas Soome 		spc->spc_indblkshift = dnp->dn_indblkshift;
1457a3874b8bSToomas Soome 		spc->spc_root = B_FALSE;
1458a3874b8bSToomas Soome 	} else {
1459a3874b8bSToomas Soome 		spc->spc_datablkszsec = 0;
1460a3874b8bSToomas Soome 		spc->spc_indblkshift = 0;
1461a3874b8bSToomas Soome 		spc->spc_root = B_TRUE;
1462a3874b8bSToomas Soome 	}
1463a3874b8bSToomas Soome 
1464a3874b8bSToomas Soome 	return (spc);
1465a3874b8bSToomas Soome }
1466a3874b8bSToomas Soome 
1467a3874b8bSToomas Soome static void
1468a3874b8bSToomas Soome scan_prefetch_ctx_add_ref(scan_prefetch_ctx_t *spc, void *tag)
1469a3874b8bSToomas Soome {
1470a3874b8bSToomas Soome 	zfs_refcount_add(&spc->spc_refcnt, tag);
1471a3874b8bSToomas Soome }
1472a3874b8bSToomas Soome 
1473a3874b8bSToomas Soome static boolean_t
1474a3874b8bSToomas Soome dsl_scan_check_prefetch_resume(scan_prefetch_ctx_t *spc,
1475a3874b8bSToomas Soome     const zbookmark_phys_t *zb)
1476a3874b8bSToomas Soome {
1477a3874b8bSToomas Soome 	zbookmark_phys_t *last_zb = &spc->spc_scn->scn_prefetch_bookmark;
1478a3874b8bSToomas Soome 	dnode_phys_t tmp_dnp;
1479a3874b8bSToomas Soome 	dnode_phys_t *dnp = (spc->spc_root) ? NULL : &tmp_dnp;
1480a3874b8bSToomas Soome 
1481a3874b8bSToomas Soome 	if (zb->zb_objset != last_zb->zb_objset)
1482a3874b8bSToomas Soome 		return (B_TRUE);
1483a3874b8bSToomas Soome 	if ((int64_t)zb->zb_object < 0)
1484a3874b8bSToomas Soome 		return (B_FALSE);
1485a3874b8bSToomas Soome 
1486a3874b8bSToomas Soome 	tmp_dnp.dn_datablkszsec = spc->spc_datablkszsec;
1487a3874b8bSToomas Soome 	tmp_dnp.dn_indblkshift = spc->spc_indblkshift;
1488a3874b8bSToomas Soome 
1489a3874b8bSToomas Soome 	if (zbookmark_subtree_completed(dnp, zb, last_zb))
1490a3874b8bSToomas Soome 		return (B_TRUE);
1491a3874b8bSToomas Soome 
1492a3874b8bSToomas Soome 	return (B_FALSE);
1493a3874b8bSToomas Soome }
1494a3874b8bSToomas Soome 
1495a3874b8bSToomas Soome static void
1496a3874b8bSToomas Soome dsl_scan_prefetch(scan_prefetch_ctx_t *spc, blkptr_t *bp, zbookmark_phys_t *zb)
14973f9d6ad7SLin Ling {
1498a3874b8bSToomas Soome 	avl_index_t idx;
1499a3874b8bSToomas Soome 	dsl_scan_t *scn = spc->spc_scn;
1500a3874b8bSToomas Soome 	spa_t *spa = scn->scn_dp->dp_spa;
1501a3874b8bSToomas Soome 	scan_prefetch_issue_ctx_t *spic;
15023f9d6ad7SLin Ling 
15033f9d6ad7SLin Ling 	if (zfs_no_scrub_prefetch)
15043f9d6ad7SLin Ling 		return;
15053f9d6ad7SLin Ling 
1506a3874b8bSToomas Soome 	if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_cur_min_txg ||
1507a3874b8bSToomas Soome 	    (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE &&
1508a3874b8bSToomas Soome 	    BP_GET_TYPE(bp) != DMU_OT_OBJSET))
1509a3874b8bSToomas Soome 		return;
1510a3874b8bSToomas Soome 
1511a3874b8bSToomas Soome 	if (dsl_scan_check_prefetch_resume(spc, zb))
1512a3874b8bSToomas Soome 		return;
1513a3874b8bSToomas Soome 
1514a3874b8bSToomas Soome 	scan_prefetch_ctx_add_ref(spc, scn);
1515a3874b8bSToomas Soome 	spic = kmem_alloc(sizeof (scan_prefetch_issue_ctx_t), KM_SLEEP);
1516a3874b8bSToomas Soome 	spic->spic_spc = spc;
1517a3874b8bSToomas Soome 	spic->spic_bp = *bp;
1518a3874b8bSToomas Soome 	spic->spic_zb = *zb;
1519a3874b8bSToomas Soome 
1520a3874b8bSToomas Soome 	/*
1521a3874b8bSToomas Soome 	 * Add the IO to the queue of blocks to prefetch. This allows us to
1522a3874b8bSToomas Soome 	 * prioritize blocks that we will need first for the main traversal
1523a3874b8bSToomas Soome 	 * thread.
1524a3874b8bSToomas Soome 	 */
1525a3874b8bSToomas Soome 	mutex_enter(&spa->spa_scrub_lock);
1526a3874b8bSToomas Soome 	if (avl_find(&scn->scn_prefetch_queue, spic, &idx) != NULL) {
1527a3874b8bSToomas Soome 		/* this block is already queued for prefetch */
1528a3874b8bSToomas Soome 		kmem_free(spic, sizeof (scan_prefetch_issue_ctx_t));
1529a3874b8bSToomas Soome 		scan_prefetch_ctx_rele(spc, scn);
1530a3874b8bSToomas Soome 		mutex_exit(&spa->spa_scrub_lock);
1531a3874b8bSToomas Soome 		return;
1532a3874b8bSToomas Soome 	}
1533a3874b8bSToomas Soome 
1534a3874b8bSToomas Soome 	avl_insert(&scn->scn_prefetch_queue, spic, idx);
1535a3874b8bSToomas Soome 	cv_broadcast(&spa->spa_scrub_io_cv);
1536a3874b8bSToomas Soome 	mutex_exit(&spa->spa_scrub_lock);
1537a3874b8bSToomas Soome }
1538a3874b8bSToomas Soome 
1539a3874b8bSToomas Soome static void
1540a3874b8bSToomas Soome dsl_scan_prefetch_dnode(dsl_scan_t *scn, dnode_phys_t *dnp,
1541a3874b8bSToomas Soome     uint64_t objset, uint64_t object)
1542a3874b8bSToomas Soome {
1543a3874b8bSToomas Soome 	int i;
1544a3874b8bSToomas Soome 	zbookmark_phys_t zb;
1545a3874b8bSToomas Soome 	scan_prefetch_ctx_t *spc;
1546a3874b8bSToomas Soome 
1547a3874b8bSToomas Soome 	if (dnp->dn_nblkptr == 0 && !(dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
15483f9d6ad7SLin Ling 		return;
15493f9d6ad7SLin Ling 
1550a3874b8bSToomas Soome 	SET_BOOKMARK(&zb, objset, object, 0, 0);
1551a3874b8bSToomas Soome 
1552a3874b8bSToomas Soome 	spc = scan_prefetch_ctx_create(scn, dnp, FTAG);
1553a3874b8bSToomas Soome 
1554a3874b8bSToomas Soome 	for (i = 0; i < dnp->dn_nblkptr; i++) {
1555a3874b8bSToomas Soome 		zb.zb_level = BP_GET_LEVEL(&dnp->dn_blkptr[i]);
1556a3874b8bSToomas Soome 		zb.zb_blkid = i;
1557a3874b8bSToomas Soome 		dsl_scan_prefetch(spc, &dnp->dn_blkptr[i], &zb);
1558a3874b8bSToomas Soome 	}
1559a3874b8bSToomas Soome 
1560a3874b8bSToomas Soome 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
1561a3874b8bSToomas Soome 		zb.zb_level = 0;
1562a3874b8bSToomas Soome 		zb.zb_blkid = DMU_SPILL_BLKID;
1563a3874b8bSToomas Soome 		dsl_scan_prefetch(spc, &dnp->dn_spill, &zb);
1564a3874b8bSToomas Soome 	}
1565a3874b8bSToomas Soome 
1566a3874b8bSToomas Soome 	scan_prefetch_ctx_rele(spc, FTAG);
1567a3874b8bSToomas Soome }
1568a3874b8bSToomas Soome 
1569a3874b8bSToomas Soome void
1570a3874b8bSToomas Soome dsl_scan_prefetch_cb(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp,
1571a3874b8bSToomas Soome     arc_buf_t *buf, void *private)
1572a3874b8bSToomas Soome {
1573a3874b8bSToomas Soome 	scan_prefetch_ctx_t *spc = private;
1574a3874b8bSToomas Soome 	dsl_scan_t *scn = spc->spc_scn;
1575a3874b8bSToomas Soome 	spa_t *spa = scn->scn_dp->dp_spa;
1576a3874b8bSToomas Soome 
1577a3874b8bSToomas Soome 	/* broadcast that the IO has completed for rate limitting purposes */
1578a3874b8bSToomas Soome 	mutex_enter(&spa->spa_scrub_lock);
1579a3874b8bSToomas Soome 	ASSERT3U(spa->spa_scrub_inflight, >=, BP_GET_PSIZE(bp));
1580a3874b8bSToomas Soome 	spa->spa_scrub_inflight -= BP_GET_PSIZE(bp);
1581a3874b8bSToomas Soome 	cv_broadcast(&spa->spa_scrub_io_cv);
1582a3874b8bSToomas Soome 	mutex_exit(&spa->spa_scrub_lock);
1583a3874b8bSToomas Soome 
1584a3874b8bSToomas Soome 	/* if there was an error or we are done prefetching, just cleanup */
1585a3874b8bSToomas Soome 	if (buf == NULL || scn->scn_suspending)
1586a3874b8bSToomas Soome 		goto out;
1587a3874b8bSToomas Soome 
1588a3874b8bSToomas Soome 	if (BP_GET_LEVEL(bp) > 0) {
1589a3874b8bSToomas Soome 		int i;
1590a3874b8bSToomas Soome 		blkptr_t *cbp;
1591a3874b8bSToomas Soome 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1592a3874b8bSToomas Soome 		zbookmark_phys_t czb;
1593a3874b8bSToomas Soome 
1594a3874b8bSToomas Soome 		for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) {
1595a3874b8bSToomas Soome 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1596a3874b8bSToomas Soome 			    zb->zb_level - 1, zb->zb_blkid * epb + i);
1597a3874b8bSToomas Soome 			dsl_scan_prefetch(spc, cbp, &czb);
1598a3874b8bSToomas Soome 		}
1599a3874b8bSToomas Soome 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
1600a3874b8bSToomas Soome 		dnode_phys_t *cdnp = buf->b_data;
1601a3874b8bSToomas Soome 		int i;
1602a3874b8bSToomas Soome 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
1603a3874b8bSToomas Soome 
1604a3874b8bSToomas Soome 		for (i = 0, cdnp = buf->b_data; i < epb;
1605a3874b8bSToomas Soome 		    i += cdnp->dn_extra_slots + 1,
1606a3874b8bSToomas Soome 		    cdnp += cdnp->dn_extra_slots + 1) {
1607a3874b8bSToomas Soome 			dsl_scan_prefetch_dnode(scn, cdnp,
1608a3874b8bSToomas Soome 			    zb->zb_objset, zb->zb_blkid * epb + i);
1609a3874b8bSToomas Soome 		}
1610a3874b8bSToomas Soome 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
1611a3874b8bSToomas Soome 		objset_phys_t *osp = buf->b_data;
1612a3874b8bSToomas Soome 
1613a3874b8bSToomas Soome 		dsl_scan_prefetch_dnode(scn, &osp->os_meta_dnode,
1614a3874b8bSToomas Soome 		    zb->zb_objset, DMU_META_DNODE_OBJECT);
16153f9d6ad7SLin Ling 
1616a3874b8bSToomas Soome 		if (OBJSET_BUF_HAS_USERUSED(buf)) {
1617a3874b8bSToomas Soome 			dsl_scan_prefetch_dnode(scn,
1618a3874b8bSToomas Soome 			    &osp->os_groupused_dnode, zb->zb_objset,
1619a3874b8bSToomas Soome 			    DMU_GROUPUSED_OBJECT);
1620a3874b8bSToomas Soome 			dsl_scan_prefetch_dnode(scn,
1621a3874b8bSToomas Soome 			    &osp->os_userused_dnode, zb->zb_objset,
1622a3874b8bSToomas Soome 			    DMU_USERUSED_OBJECT);
1623a3874b8bSToomas Soome 		}
1624a3874b8bSToomas Soome 	}
1625a3874b8bSToomas Soome 
1626a3874b8bSToomas Soome out:
1627a3874b8bSToomas Soome 	if (buf != NULL)
1628a3874b8bSToomas Soome 		arc_buf_destroy(buf, private);
1629a3874b8bSToomas Soome 	scan_prefetch_ctx_rele(spc, scn);
1630a3874b8bSToomas Soome }
1631a3874b8bSToomas Soome 
1632a3874b8bSToomas Soome /* ARGSUSED */
1633a3874b8bSToomas Soome static void
1634a3874b8bSToomas Soome dsl_scan_prefetch_thread(void *arg)
1635a3874b8bSToomas Soome {
1636a3874b8bSToomas Soome 	dsl_scan_t *scn = arg;
1637a3874b8bSToomas Soome 	spa_t *spa = scn->scn_dp->dp_spa;
1638a3874b8bSToomas Soome 	vdev_t *rvd = spa->spa_root_vdev;
1639a3874b8bSToomas Soome 	uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight;
1640a3874b8bSToomas Soome 	scan_prefetch_issue_ctx_t *spic;
1641a3874b8bSToomas Soome 
1642a3874b8bSToomas Soome 	/* loop until we are told to stop */
1643a3874b8bSToomas Soome 	while (!scn->scn_prefetch_stop) {
1644a3874b8bSToomas Soome 		arc_flags_t flags = ARC_FLAG_NOWAIT |
1645a3874b8bSToomas Soome 		    ARC_FLAG_PRESCIENT_PREFETCH | ARC_FLAG_PREFETCH;
1646a3874b8bSToomas Soome 		int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
1647a3874b8bSToomas Soome 
1648a3874b8bSToomas Soome 		mutex_enter(&spa->spa_scrub_lock);
1649a3874b8bSToomas Soome 
1650a3874b8bSToomas Soome 		/*
1651a3874b8bSToomas Soome 		 * Wait until we have an IO to issue and are not above our
1652a3874b8bSToomas Soome 		 * maximum in flight limit.
1653a3874b8bSToomas Soome 		 */
1654a3874b8bSToomas Soome 		while (!scn->scn_prefetch_stop &&
1655a3874b8bSToomas Soome 		    (avl_numnodes(&scn->scn_prefetch_queue) == 0 ||
1656a3874b8bSToomas Soome 		    spa->spa_scrub_inflight >= scn->scn_maxinflight_bytes)) {
1657a3874b8bSToomas Soome 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1658a3874b8bSToomas Soome 		}
1659a3874b8bSToomas Soome 
1660a3874b8bSToomas Soome 		/* recheck if we should stop since we waited for the cv */
1661a3874b8bSToomas Soome 		if (scn->scn_prefetch_stop) {
1662a3874b8bSToomas Soome 			mutex_exit(&spa->spa_scrub_lock);
1663a3874b8bSToomas Soome 			break;
1664a3874b8bSToomas Soome 		}
1665a3874b8bSToomas Soome 
1666a3874b8bSToomas Soome 		/* remove the prefetch IO from the tree */
1667a3874b8bSToomas Soome 		spic = avl_first(&scn->scn_prefetch_queue);
1668a3874b8bSToomas Soome 		spa->spa_scrub_inflight += BP_GET_PSIZE(&spic->spic_bp);
1669a3874b8bSToomas Soome 		avl_remove(&scn->scn_prefetch_queue, spic);
1670a3874b8bSToomas Soome 
1671a3874b8bSToomas Soome 		mutex_exit(&spa->spa_scrub_lock);
1672a3874b8bSToomas Soome 
1673eb633035STom Caputi 		if (BP_IS_PROTECTED(&spic->spic_bp)) {
1674eb633035STom Caputi 			ASSERT(BP_GET_TYPE(&spic->spic_bp) == DMU_OT_DNODE ||
1675eb633035STom Caputi 			    BP_GET_TYPE(&spic->spic_bp) == DMU_OT_OBJSET);
1676eb633035STom Caputi 			ASSERT3U(BP_GET_LEVEL(&spic->spic_bp), ==, 0);
1677eb633035STom Caputi 			zio_flags |= ZIO_FLAG_RAW;
1678eb633035STom Caputi 		}
1679eb633035STom Caputi 
1680a3874b8bSToomas Soome 		/* issue the prefetch asynchronously */
1681a3874b8bSToomas Soome 		(void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa,
1682a3874b8bSToomas Soome 		    &spic->spic_bp, dsl_scan_prefetch_cb, spic->spic_spc,
1683a3874b8bSToomas Soome 		    ZIO_PRIORITY_SCRUB, zio_flags, &flags, &spic->spic_zb);
1684a3874b8bSToomas Soome 
1685a3874b8bSToomas Soome 		kmem_free(spic, sizeof (scan_prefetch_issue_ctx_t));
1686a3874b8bSToomas Soome 	}
1687a3874b8bSToomas Soome 
1688a3874b8bSToomas Soome 	ASSERT(scn->scn_prefetch_stop);
1689a3874b8bSToomas Soome 
1690a3874b8bSToomas Soome 	/* free any prefetches we didn't get to complete */
1691a3874b8bSToomas Soome 	mutex_enter(&spa->spa_scrub_lock);
1692a3874b8bSToomas Soome 	while ((spic = avl_first(&scn->scn_prefetch_queue)) != NULL) {
1693a3874b8bSToomas Soome 		avl_remove(&scn->scn_prefetch_queue, spic);
1694a3874b8bSToomas Soome 		scan_prefetch_ctx_rele(spic->spic_spc, scn);
1695a3874b8bSToomas Soome 		kmem_free(spic, sizeof (scan_prefetch_issue_ctx_t));
1696a3874b8bSToomas Soome 	}
1697a3874b8bSToomas Soome 	ASSERT0(avl_numnodes(&scn->scn_prefetch_queue));
1698a3874b8bSToomas Soome 	mutex_exit(&spa->spa_scrub_lock);
16993f9d6ad7SLin Ling }
17003f9d6ad7SLin Ling 
17013f9d6ad7SLin Ling static boolean_t
17023f9d6ad7SLin Ling dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
17037802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
17043f9d6ad7SLin Ling {
17053f9d6ad7SLin Ling 	/*
17063f9d6ad7SLin Ling 	 * We never skip over user/group accounting objects (obj<0)
17073f9d6ad7SLin Ling 	 */
1708ad135b5dSChristopher Siden 	if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
17093f9d6ad7SLin Ling 	    (int64_t)zb->zb_object >= 0) {
17103f9d6ad7SLin Ling 		/*
17113f9d6ad7SLin Ling 		 * If we already visited this bp & everything below (in
17123f9d6ad7SLin Ling 		 * a prior txg sync), don't bother doing it again.
17133f9d6ad7SLin Ling 		 */
1714a2cdcdd2SPaul Dagnelie 		if (zbookmark_subtree_completed(dnp, zb,
1715a2cdcdd2SPaul Dagnelie 		    &scn->scn_phys.scn_bookmark))
17163f9d6ad7SLin Ling 			return (B_TRUE);
17173f9d6ad7SLin Ling 
17183f9d6ad7SLin Ling 		/*
17193f9d6ad7SLin Ling 		 * If we found the block we're trying to resume from, or
17203f9d6ad7SLin Ling 		 * we went past it to a different object, zero it out to
17211702cce7SAlek Pinchuk 		 * indicate that it's OK to start checking for suspending
17223f9d6ad7SLin Ling 		 * again.
17233f9d6ad7SLin Ling 		 */
17243f9d6ad7SLin Ling 		if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
17253f9d6ad7SLin Ling 		    zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
17263f9d6ad7SLin Ling 			dprintf("resuming at %llx/%llx/%llx/%llx\n",
17273f9d6ad7SLin Ling 			    (longlong_t)zb->zb_objset,
17283f9d6ad7SLin Ling 			    (longlong_t)zb->zb_object,
17293f9d6ad7SLin Ling 			    (longlong_t)zb->zb_level,
17303f9d6ad7SLin Ling 			    (longlong_t)zb->zb_blkid);
17313f9d6ad7SLin Ling 			bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
17323f9d6ad7SLin Ling 		}
17333f9d6ad7SLin Ling 	}
17343f9d6ad7SLin Ling 	return (B_FALSE);
17353f9d6ad7SLin Ling }
17363f9d6ad7SLin Ling 
1737a3874b8bSToomas Soome static void dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
1738a3874b8bSToomas Soome     dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn,
1739a3874b8bSToomas Soome     dmu_objset_type_t ostype, dmu_tx_t *tx);
1740a3874b8bSToomas Soome static void dsl_scan_visitdnode(
1741a3874b8bSToomas Soome     dsl_scan_t *, dsl_dataset_t *ds, dmu_objset_type_t ostype,
1742a3874b8bSToomas Soome     dnode_phys_t *dnp, uint64_t object, dmu_tx_t *tx);
1743a3874b8bSToomas Soome 
17443f9d6ad7SLin Ling /*
17453f9d6ad7SLin Ling  * Return nonzero on i/o error.
17463f9d6ad7SLin Ling  * Return new buf to write out in *bufp.
17473f9d6ad7SLin Ling  */
17483f9d6ad7SLin Ling static int
17493f9d6ad7SLin Ling dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
17503f9d6ad7SLin Ling     dnode_phys_t *dnp, const blkptr_t *bp,
17515f37736aSMatthew Ahrens     const zbookmark_phys_t *zb, dmu_tx_t *tx)
17523f9d6ad7SLin Ling {
17533f9d6ad7SLin Ling 	dsl_pool_t *dp = scn->scn_dp;
175444ecc532SGeorge Wilson 	int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
17553f9d6ad7SLin Ling 	int err;
17563f9d6ad7SLin Ling 
17573f9d6ad7SLin Ling 	if (BP_GET_LEVEL(bp) > 0) {
17587adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
17593f9d6ad7SLin Ling 		int i;
17603f9d6ad7SLin Ling 		blkptr_t *cbp;
17613f9d6ad7SLin Ling 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
17625f37736aSMatthew Ahrens 		arc_buf_t *buf;
17633f9d6ad7SLin Ling 
17645f37736aSMatthew Ahrens 		err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
1765a3874b8bSToomas Soome 		    ZIO_PRIORITY_SCRUB, zio_flags, &flags, zb);
17663f9d6ad7SLin Ling 		if (err) {
17673f9d6ad7SLin Ling 			scn->scn_phys.scn_errors++;
17683f9d6ad7SLin Ling 			return (err);
17693f9d6ad7SLin Ling 		}
17705f37736aSMatthew Ahrens 		for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) {
17717802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
17723f9d6ad7SLin Ling 
17733f9d6ad7SLin Ling 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
17743f9d6ad7SLin Ling 			    zb->zb_level - 1,
17753f9d6ad7SLin Ling 			    zb->zb_blkid * epb + i);
17763f9d6ad7SLin Ling 			dsl_scan_visitbp(cbp, &czb, dnp,
17775f37736aSMatthew Ahrens 			    ds, scn, ostype, tx);
17783f9d6ad7SLin Ling 		}
1779dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
17803f9d6ad7SLin Ling 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
17817adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
17823f9d6ad7SLin Ling 		dnode_phys_t *cdnp;
1783a3874b8bSToomas Soome 		int i;
17843f9d6ad7SLin Ling 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
17855f37736aSMatthew Ahrens 		arc_buf_t *buf;
17863f9d6ad7SLin Ling 
1787eb633035STom Caputi 		if (BP_IS_PROTECTED(bp)) {
1788eb633035STom Caputi 			ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
1789eb633035STom Caputi 			zio_flags |= ZIO_FLAG_RAW;
1790eb633035STom Caputi 		}
1791eb633035STom Caputi 
17925f37736aSMatthew Ahrens 		err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
1793a3874b8bSToomas Soome 		    ZIO_PRIORITY_SCRUB, zio_flags, &flags, zb);
17943f9d6ad7SLin Ling 		if (err) {
17953f9d6ad7SLin Ling 			scn->scn_phys.scn_errors++;
17963f9d6ad7SLin Ling 			return (err);
17973f9d6ad7SLin Ling 		}
179854811da5SToomas Soome 		for (i = 0, cdnp = buf->b_data; i < epb;
179954811da5SToomas Soome 		    i += cdnp->dn_extra_slots + 1,
180054811da5SToomas Soome 		    cdnp += cdnp->dn_extra_slots + 1) {
18013f9d6ad7SLin Ling 			dsl_scan_visitdnode(scn, ds, ostype,
18025f37736aSMatthew Ahrens 			    cdnp, zb->zb_blkid * epb + i, tx);
18033f9d6ad7SLin Ling 		}
18043f9d6ad7SLin Ling 
1805dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
18063f9d6ad7SLin Ling 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
18077adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
18083f9d6ad7SLin Ling 		objset_phys_t *osp;
18095f37736aSMatthew Ahrens 		arc_buf_t *buf;
18103f9d6ad7SLin Ling 
18115f37736aSMatthew Ahrens 		err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
1812a3874b8bSToomas Soome 		    ZIO_PRIORITY_SCRUB, zio_flags, &flags, zb);
18133f9d6ad7SLin Ling 		if (err) {
18143f9d6ad7SLin Ling 			scn->scn_phys.scn_errors++;
18153f9d6ad7SLin Ling 			return (err);
18163f9d6ad7SLin Ling 		}
18173f9d6ad7SLin Ling 
18185f37736aSMatthew Ahrens 		osp = buf->b_data;
18193f9d6ad7SLin Ling 
18203f9d6ad7SLin Ling 		dsl_scan_visitdnode(scn, ds, osp->os_type,
18215f37736aSMatthew Ahrens 		    &osp->os_meta_dnode, DMU_META_DNODE_OBJECT, tx);
18223f9d6ad7SLin Ling 
18235f37736aSMatthew Ahrens 		if (OBJSET_BUF_HAS_USERUSED(buf)) {
18243f9d6ad7SLin Ling 			/*
1825f67950b2SNasf-Fan 			 * We also always visit user/group/project accounting
18263f9d6ad7SLin Ling 			 * objects, and never skip them, even if we are
18271702cce7SAlek Pinchuk 			 * suspending.  This is necessary so that the space
18283f9d6ad7SLin Ling 			 * deltas from this txg get integrated.
18293f9d6ad7SLin Ling 			 */
1830f67950b2SNasf-Fan 			if (OBJSET_BUF_HAS_PROJECTUSED(buf))
1831f67950b2SNasf-Fan 				dsl_scan_visitdnode(scn, ds, osp->os_type,
1832f67950b2SNasf-Fan 				    &osp->os_projectused_dnode,
1833f67950b2SNasf-Fan 				    DMU_PROJECTUSED_OBJECT, tx);
18343f9d6ad7SLin Ling 			dsl_scan_visitdnode(scn, ds, osp->os_type,
18355f37736aSMatthew Ahrens 			    &osp->os_groupused_dnode,
18363f9d6ad7SLin Ling 			    DMU_GROUPUSED_OBJECT, tx);
18373f9d6ad7SLin Ling 			dsl_scan_visitdnode(scn, ds, osp->os_type,
18385f37736aSMatthew Ahrens 			    &osp->os_userused_dnode,
18393f9d6ad7SLin Ling 			    DMU_USERUSED_OBJECT, tx);
18403f9d6ad7SLin Ling 		}
1841dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
18423f9d6ad7SLin Ling 	}
18433f9d6ad7SLin Ling 
18443f9d6ad7SLin Ling 	return (0);
18453f9d6ad7SLin Ling }
18463f9d6ad7SLin Ling 
18473f9d6ad7SLin Ling static void
18483f9d6ad7SLin Ling dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
18495f37736aSMatthew Ahrens     dmu_objset_type_t ostype, dnode_phys_t *dnp,
18503f9d6ad7SLin Ling     uint64_t object, dmu_tx_t *tx)
18513f9d6ad7SLin Ling {
18523f9d6ad7SLin Ling 	int j;
18533f9d6ad7SLin Ling 
18543f9d6ad7SLin Ling 	for (j = 0; j < dnp->dn_nblkptr; j++) {
18557802d7bfSMatthew Ahrens 		zbookmark_phys_t czb;
18563f9d6ad7SLin Ling 
18573f9d6ad7SLin Ling 		SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
18583f9d6ad7SLin Ling 		    dnp->dn_nlevels - 1, j);
18593f9d6ad7SLin Ling 		dsl_scan_visitbp(&dnp->dn_blkptr[j],
18605f37736aSMatthew Ahrens 		    &czb, dnp, ds, scn, ostype, tx);
18613f9d6ad7SLin Ling 	}
18623f9d6ad7SLin Ling 
18633f9d6ad7SLin Ling 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
18647802d7bfSMatthew Ahrens 		zbookmark_phys_t czb;
18653f9d6ad7SLin Ling 		SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
18663f9d6ad7SLin Ling 		    0, DMU_SPILL_BLKID);
186754811da5SToomas Soome 		dsl_scan_visitbp(DN_SPILL_BLKPTR(dnp),
18685f37736aSMatthew Ahrens 		    &czb, dnp, ds, scn, ostype, tx);
18693f9d6ad7SLin Ling 	}
18703f9d6ad7SLin Ling }
18713f9d6ad7SLin Ling 
18723f9d6ad7SLin Ling /*
18733f9d6ad7SLin Ling  * The arguments are in this order because mdb can only print the
18743f9d6ad7SLin Ling  * first 5; we want them to be useful.
18753f9d6ad7SLin Ling  */
18763f9d6ad7SLin Ling static void
18777802d7bfSMatthew Ahrens dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
18785f37736aSMatthew Ahrens     dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn,
18795f37736aSMatthew Ahrens     dmu_objset_type_t ostype, dmu_tx_t *tx)
18803f9d6ad7SLin Ling {
18813f9d6ad7SLin Ling 	dsl_pool_t *dp = scn->scn_dp;
1882a3874b8bSToomas Soome 	blkptr_t *bp_toread = NULL;
18833f9d6ad7SLin Ling 
18841702cce7SAlek Pinchuk 	if (dsl_scan_check_suspend(scn, zb))
18853f9d6ad7SLin Ling 		return;
18863f9d6ad7SLin Ling 
18873f9d6ad7SLin Ling 	if (dsl_scan_check_resume(scn, dnp, zb))
18883f9d6ad7SLin Ling 		return;
18893f9d6ad7SLin Ling 
18903f9d6ad7SLin Ling 	scn->scn_visited_this_txg++;
18913f9d6ad7SLin Ling 
1892a3874b8bSToomas Soome 	/*
1893a3874b8bSToomas Soome 	 * This debugging is commented out to conserve stack space.  This
1894a3874b8bSToomas Soome 	 * function is called recursively and the debugging addes several
1895a3874b8bSToomas Soome 	 * bytes to the stack for each call.  It can be commented back in
1896a3874b8bSToomas Soome 	 * if required to debug an issue in dsl_scan_visitbp().
1897a3874b8bSToomas Soome 	 *
1898a3874b8bSToomas Soome 	 * dprintf_bp(bp,
1899a3874b8bSToomas Soome 	 *	"visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx bp=%p",
1900a3874b8bSToomas Soome 	 *	ds, ds ? ds->ds_object : 0,
1901a3874b8bSToomas Soome 	 *	zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
1902a3874b8bSToomas Soome 	 *	bp);
1903a3874b8bSToomas Soome 	 */
19043f9d6ad7SLin Ling 
1905a3874b8bSToomas Soome 	if (BP_IS_HOLE(bp)) {
1906a3874b8bSToomas Soome 		scn->scn_holes_this_txg++;
19073f9d6ad7SLin Ling 		return;
1908a3874b8bSToomas Soome 	}
19093f9d6ad7SLin Ling 
1910a3874b8bSToomas Soome 	if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg) {
1911a3874b8bSToomas Soome 		scn->scn_lt_min_this_txg++;
19123f9d6ad7SLin Ling 		return;
1913a3874b8bSToomas Soome 	}
1914a3874b8bSToomas Soome 
1915a3874b8bSToomas Soome 	bp_toread = kmem_alloc(sizeof (blkptr_t), KM_SLEEP);
1916a3874b8bSToomas Soome 	*bp_toread = *bp;
1917a3874b8bSToomas Soome 
1918a3874b8bSToomas Soome 	if (dsl_scan_recurse(scn, ds, ostype, dnp, bp_toread, zb, tx) != 0)
1919a3874b8bSToomas Soome 		goto out;
19203f9d6ad7SLin Ling 
19213f9d6ad7SLin Ling 	/*
192240713f2bSAlan Somers 	 * If dsl_scan_ddt() has already visited this block, it will have
19233f9d6ad7SLin Ling 	 * already done any translations or scrubbing, so don't call the
19243f9d6ad7SLin Ling 	 * callback again.
19253f9d6ad7SLin Ling 	 */
19263f9d6ad7SLin Ling 	if (ddt_class_contains(dp->dp_spa,
19273f9d6ad7SLin Ling 	    scn->scn_phys.scn_ddt_class_max, bp)) {
1928a3874b8bSToomas Soome 		scn->scn_ddt_contained_this_txg++;
1929a3874b8bSToomas Soome 		goto out;
19303f9d6ad7SLin Ling 	}
19313f9d6ad7SLin Ling 
19323f9d6ad7SLin Ling 	/*
19333f9d6ad7SLin Ling 	 * If this block is from the future (after cur_max_txg), then we
19343f9d6ad7SLin Ling 	 * are doing this on behalf of a deleted snapshot, and we will
19353f9d6ad7SLin Ling 	 * revisit the future block on the next pass of this dataset.
19363f9d6ad7SLin Ling 	 * Don't scan it now unless we need to because something
19373f9d6ad7SLin Ling 	 * under it was modified.
19383f9d6ad7SLin Ling 	 */
1939a3874b8bSToomas Soome 	if (BP_PHYSICAL_BIRTH(bp) > scn->scn_phys.scn_cur_max_txg) {
1940a3874b8bSToomas Soome 		scn->scn_gt_max_this_txg++;
1941a3874b8bSToomas Soome 		goto out;
19423f9d6ad7SLin Ling 	}
1943a3874b8bSToomas Soome 
1944a3874b8bSToomas Soome 	scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
1945a3874b8bSToomas Soome 
1946a3874b8bSToomas Soome out:
1947a3874b8bSToomas Soome 	kmem_free(bp_toread, sizeof (blkptr_t));
19483f9d6ad7SLin Ling }
19493f9d6ad7SLin Ling 
19503f9d6ad7SLin Ling static void
19513f9d6ad7SLin Ling dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
19523f9d6ad7SLin Ling     dmu_tx_t *tx)
19533f9d6ad7SLin Ling {
19547802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1955a3874b8bSToomas Soome 	scan_prefetch_ctx_t *spc;
19563f9d6ad7SLin Ling 
19573f9d6ad7SLin Ling 	SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
19583f9d6ad7SLin Ling 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1959a3874b8bSToomas Soome 
1960a3874b8bSToomas Soome 	if (ZB_IS_ZERO(&scn->scn_phys.scn_bookmark)) {
1961a3874b8bSToomas Soome 		SET_BOOKMARK(&scn->scn_prefetch_bookmark,
1962a3874b8bSToomas Soome 		    zb.zb_objset, 0, 0, 0);
1963a3874b8bSToomas Soome 	} else {
1964a3874b8bSToomas Soome 		scn->scn_prefetch_bookmark = scn->scn_phys.scn_bookmark;
1965a3874b8bSToomas Soome 	}
1966a3874b8bSToomas Soome 
1967a3874b8bSToomas Soome 	scn->scn_objsets_visited_this_txg++;
1968a3874b8bSToomas Soome 
1969a3874b8bSToomas Soome 	spc = scan_prefetch_ctx_create(scn, NULL, FTAG);
1970a3874b8bSToomas Soome 	dsl_scan_prefetch(spc, bp, &zb);
1971a3874b8bSToomas Soome 	scan_prefetch_ctx_rele(spc, FTAG);
1972a3874b8bSToomas Soome 
1973a3874b8bSToomas Soome 	dsl_scan_visitbp(bp, &zb, NULL, ds, scn, DMU_OST_NONE, tx);
19743f9d6ad7SLin Ling 
19753f9d6ad7SLin Ling 	dprintf_ds(ds, "finished scan%s", "");
19763f9d6ad7SLin Ling }
19773f9d6ad7SLin Ling 
1978a3874b8bSToomas Soome static void
1979a3874b8bSToomas Soome ds_destroyed_scn_phys(dsl_dataset_t *ds, dsl_scan_phys_t *scn_phys)
19803f9d6ad7SLin Ling {
1981a3874b8bSToomas Soome 	if (scn_phys->scn_bookmark.zb_objset == ds->ds_object) {
1982bc9014e6SJustin Gibbs 		if (ds->ds_is_snapshot) {
198338d61036SMatthew Ahrens 			/*
198438d61036SMatthew Ahrens 			 * Note:
198538d61036SMatthew Ahrens 			 *  - scn_cur_{min,max}_txg stays the same.
198638d61036SMatthew Ahrens 			 *  - Setting the flag is not really necessary if
198738d61036SMatthew Ahrens 			 *    scn_cur_max_txg == scn_max_txg, because there
198838d61036SMatthew Ahrens 			 *    is nothing after this snapshot that we care
198938d61036SMatthew Ahrens 			 *    about.  However, we set it anyway and then
199038d61036SMatthew Ahrens 			 *    ignore it when we retraverse it in
199138d61036SMatthew Ahrens 			 *    dsl_scan_visitds().
199238d61036SMatthew Ahrens 			 */
1993a3874b8bSToomas Soome 			scn_phys->scn_bookmark.zb_objset =
1994c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_snap_obj;
19953f9d6ad7SLin Ling 			zfs_dbgmsg("destroying ds %llu; currently traversing; "
19963f9d6ad7SLin Ling 			    "reset zb_objset to %llu",
19973f9d6ad7SLin Ling 			    (u_longlong_t)ds->ds_object,
1998c1379625SJustin T. Gibbs 			    (u_longlong_t)dsl_dataset_phys(ds)->
1999c1379625SJustin T. Gibbs 			    ds_next_snap_obj);
2000a3874b8bSToomas Soome 			scn_phys->scn_flags |= DSF_VISIT_DS_AGAIN;
20013f9d6ad7SLin Ling 		} else {
2002a3874b8bSToomas Soome 			SET_BOOKMARK(&scn_phys->scn_bookmark,
20033f9d6ad7SLin Ling 			    ZB_DESTROYED_OBJSET, 0, 0, 0);
20043f9d6ad7SLin Ling 			zfs_dbgmsg("destroying ds %llu; currently traversing; "
20053f9d6ad7SLin Ling 			    "reset bookmark to -1,0,0,0",
20063f9d6ad7SLin Ling 			    (u_longlong_t)ds->ds_object);
20073f9d6ad7SLin Ling 		}
2008a3874b8bSToomas Soome 	}
2009a3874b8bSToomas Soome }
2010a3874b8bSToomas Soome 
2011a3874b8bSToomas Soome /*
2012a3874b8bSToomas Soome  * Invoked when a dataset is destroyed. We need to make sure that:
2013a3874b8bSToomas Soome  *
2014a3874b8bSToomas Soome  * 1) If it is the dataset that was currently being scanned, we write
2015a3874b8bSToomas Soome  *	a new dsl_scan_phys_t and marking the objset reference in it
2016a3874b8bSToomas Soome  *	as destroyed.
2017a3874b8bSToomas Soome  * 2) Remove it from the work queue, if it was present.
2018a3874b8bSToomas Soome  *
2019a3874b8bSToomas Soome  * If the dataset was actually a snapshot, instead of marking the dataset
2020a3874b8bSToomas Soome  * as destroyed, we instead substitute the next snapshot in line.
2021a3874b8bSToomas Soome  */
2022a3874b8bSToomas Soome void
2023a3874b8bSToomas Soome dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
2024a3874b8bSToomas Soome {
2025a3874b8bSToomas Soome 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2026a3874b8bSToomas Soome 	dsl_scan_t *scn = dp->dp_scan;
2027a3874b8bSToomas Soome 	uint64_t mintxg;
2028a3874b8bSToomas Soome 
2029a3874b8bSToomas Soome 	if (!dsl_scan_is_running(scn))
2030a3874b8bSToomas Soome 		return;
2031a3874b8bSToomas Soome 
2032a3874b8bSToomas Soome 	ds_destroyed_scn_phys(ds, &scn->scn_phys);
2033a3874b8bSToomas Soome 	ds_destroyed_scn_phys(ds, &scn->scn_phys_cached);
2034a3874b8bSToomas Soome 
2035a3874b8bSToomas Soome 	if (scan_ds_queue_contains(scn, ds->ds_object, &mintxg)) {
2036a3874b8bSToomas Soome 		scan_ds_queue_remove(scn, ds->ds_object);
2037a3874b8bSToomas Soome 		if (ds->ds_is_snapshot)
2038a3874b8bSToomas Soome 			scan_ds_queue_insert(scn,
2039a3874b8bSToomas Soome 			    dsl_dataset_phys(ds)->ds_next_snap_obj, mintxg);
2040a3874b8bSToomas Soome 	}
2041a3874b8bSToomas Soome 
2042a3874b8bSToomas Soome 	if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
2043a3874b8bSToomas Soome 	    ds->ds_object, &mintxg) == 0) {
2044c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
2045b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
20463f9d6ad7SLin Ling 		    scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
2047bc9014e6SJustin Gibbs 		if (ds->ds_is_snapshot) {
20483f9d6ad7SLin Ling 			/*
20493f9d6ad7SLin Ling 			 * We keep the same mintxg; it could be >
20503f9d6ad7SLin Ling 			 * ds_creation_txg if the previous snapshot was
20513f9d6ad7SLin Ling 			 * deleted too.
20523f9d6ad7SLin Ling 			 */
20533f9d6ad7SLin Ling 			VERIFY(zap_add_int_key(dp->dp_meta_objset,
20543f9d6ad7SLin Ling 			    scn->scn_phys.scn_queue_obj,
2055c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_snap_obj,
2056c1379625SJustin T. Gibbs 			    mintxg, tx) == 0);
20573f9d6ad7SLin Ling 			zfs_dbgmsg("destroying ds %llu; in queue; "
20583f9d6ad7SLin Ling 			    "replacing with %llu",
20593f9d6ad7SLin Ling 			    (u_longlong_t)ds->ds_object,
2060c1379625SJustin T. Gibbs 			    (u_longlong_t)dsl_dataset_phys(ds)->
2061c1379625SJustin T. Gibbs 			    ds_next_snap_obj);
20623f9d6ad7SLin Ling 		} else {
20633f9d6ad7SLin Ling 			zfs_dbgmsg("destroying ds %llu; in queue; removing",
20643f9d6ad7SLin Ling 			    (u_longlong_t)ds->ds_object);
20653f9d6ad7SLin Ling 		}
20663f9d6ad7SLin Ling 	}
20673f9d6ad7SLin Ling 
20683f9d6ad7SLin Ling 	/*
20693f9d6ad7SLin Ling 	 * dsl_scan_sync() should be called after this, and should sync
20703f9d6ad7SLin Ling 	 * out our changed state, but just to be safe, do it here.
20713f9d6ad7SLin Ling 	 */
2072a3874b8bSToomas Soome 	dsl_scan_sync_state(scn, tx, SYNC_CACHED);
2073a3874b8bSToomas Soome }
2074a3874b8bSToomas Soome 
2075a3874b8bSToomas Soome static void
2076a3874b8bSToomas Soome ds_snapshotted_bookmark(dsl_dataset_t *ds, zbookmark_phys_t *scn_bookmark)
2077a3874b8bSToomas Soome {
2078a3874b8bSToomas Soome 	if (scn_bookmark->zb_objset == ds->ds_object) {
2079a3874b8bSToomas Soome 		scn_bookmark->zb_objset =
2080a3874b8bSToomas Soome 		    dsl_dataset_phys(ds)->ds_prev_snap_obj;
2081a3874b8bSToomas Soome 		zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
2082a3874b8bSToomas Soome 		    "reset zb_objset to %llu",
2083a3874b8bSToomas Soome 		    (u_longlong_t)ds->ds_object,
2084a3874b8bSToomas Soome 		    (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj);
2085a3874b8bSToomas Soome 	}
20863f9d6ad7SLin Ling }
20873f9d6ad7SLin Ling 
2088a3874b8bSToomas Soome /*
2089a3874b8bSToomas Soome  * Called when a dataset is snapshotted. If we were currently traversing
2090a3874b8bSToomas Soome  * this snapshot, we reset our bookmark to point at the newly created
2091a3874b8bSToomas Soome  * snapshot. We also modify our work queue to remove the old snapshot and
2092a3874b8bSToomas Soome  * replace with the new one.
2093a3874b8bSToomas Soome  */
20943f9d6ad7SLin Ling void
20953f9d6ad7SLin Ling dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
20963f9d6ad7SLin Ling {
20973f9d6ad7SLin Ling 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
20983f9d6ad7SLin Ling 	dsl_scan_t *scn = dp->dp_scan;
20993f9d6ad7SLin Ling 	uint64_t mintxg;
21003f9d6ad7SLin Ling 
2101a3874b8bSToomas Soome 	if (!dsl_scan_is_running(scn))
21023f9d6ad7SLin Ling 		return;
21033f9d6ad7SLin Ling 
2104c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
21053f9d6ad7SLin Ling 
2106a3874b8bSToomas Soome 	ds_snapshotted_bookmark(ds, &scn->scn_phys.scn_bookmark);
2107a3874b8bSToomas Soome 	ds_snapshotted_bookmark(ds, &scn->scn_phys_cached.scn_bookmark);
2108a3874b8bSToomas Soome 
2109a3874b8bSToomas Soome 	if (scan_ds_queue_contains(scn, ds->ds_object, &mintxg)) {
2110a3874b8bSToomas Soome 		scan_ds_queue_remove(scn, ds->ds_object);
2111a3874b8bSToomas Soome 		scan_ds_queue_insert(scn,
2112a3874b8bSToomas Soome 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, mintxg);
2113a3874b8bSToomas Soome 	}
2114a3874b8bSToomas Soome 
2115a3874b8bSToomas Soome 	if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
2116a3874b8bSToomas Soome 	    ds->ds_object, &mintxg) == 0) {
2117b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
21183f9d6ad7SLin Ling 		    scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
21193f9d6ad7SLin Ling 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
21203f9d6ad7SLin Ling 		    scn->scn_phys.scn_queue_obj,
2121c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, mintxg, tx) == 0);
21223f9d6ad7SLin Ling 		zfs_dbgmsg("snapshotting ds %llu; in queue; "
21233f9d6ad7SLin Ling 		    "replacing with %llu",
21243f9d6ad7SLin Ling 		    (u_longlong_t)ds->ds_object,
2125c1379625SJustin T. Gibbs 		    (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj);
21263f9d6ad7SLin Ling 	}
2127a3874b8bSToomas Soome 
2128a3874b8bSToomas Soome 	dsl_scan_sync_state(scn, tx, SYNC_CACHED);
21293f9d6ad7SLin Ling }
21303f9d6ad7SLin Ling 
2131a3874b8bSToomas Soome static void
2132a3874b8bSToomas Soome ds_clone_swapped_bookmark(dsl_dataset_t *ds1, dsl_dataset_t *ds2,
2133a3874b8bSToomas Soome     zbookmark_phys_t *scn_bookmark)
21343f9d6ad7SLin Ling {
2135a3874b8bSToomas Soome 	if (scn_bookmark->zb_objset == ds1->ds_object) {
2136a3874b8bSToomas Soome 		scn_bookmark->zb_objset = ds2->ds_object;
21373f9d6ad7SLin Ling 		zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
21383f9d6ad7SLin Ling 		    "reset zb_objset to %llu",
21393f9d6ad7SLin Ling 		    (u_longlong_t)ds1->ds_object,
21403f9d6ad7SLin Ling 		    (u_longlong_t)ds2->ds_object);
2141a3874b8bSToomas Soome 	} else if (scn_bookmark->zb_objset == ds2->ds_object) {
2142a3874b8bSToomas Soome 		scn_bookmark->zb_objset = ds1->ds_object;
21433f9d6ad7SLin Ling 		zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
21443f9d6ad7SLin Ling 		    "reset zb_objset to %llu",
21453f9d6ad7SLin Ling 		    (u_longlong_t)ds2->ds_object,
21463f9d6ad7SLin Ling 		    (u_longlong_t)ds1->ds_object);
21473f9d6ad7SLin Ling 	}
2148a3874b8bSToomas Soome }
2149a3874b8bSToomas Soome 
2150a3874b8bSToomas Soome /*
2151a3874b8bSToomas Soome  * Called when a parent dataset and its clone are swapped. If we were
2152a3874b8bSToomas Soome  * currently traversing the dataset, we need to switch to traversing the
2153a3874b8bSToomas Soome  * newly promoted parent.
2154a3874b8bSToomas Soome  */
2155a3874b8bSToomas Soome void
2156a3874b8bSToomas Soome dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
2157a3874b8bSToomas Soome {
2158a3874b8bSToomas Soome 	dsl_pool_t *dp = ds1->ds_dir->dd_pool;
2159a3874b8bSToomas Soome 	dsl_scan_t *scn = dp->dp_scan;
2160a3874b8bSToomas Soome 	uint64_t mintxg;
2161a3874b8bSToomas Soome 
2162a3874b8bSToomas Soome 	if (!dsl_scan_is_running(scn))
2163a3874b8bSToomas Soome 		return;
2164a3874b8bSToomas Soome 
2165a3874b8bSToomas Soome 	ds_clone_swapped_bookmark(ds1, ds2, &scn->scn_phys.scn_bookmark);
2166a3874b8bSToomas Soome 	ds_clone_swapped_bookmark(ds1, ds2, &scn->scn_phys_cached.scn_bookmark);
2167a3874b8bSToomas Soome 
2168a3874b8bSToomas Soome 	if (scan_ds_queue_contains(scn, ds1->ds_object, &mintxg)) {
2169a3874b8bSToomas Soome 		scan_ds_queue_remove(scn, ds1->ds_object);
2170a3874b8bSToomas Soome 		scan_ds_queue_insert(scn, ds2->ds_object, mintxg);
2171a3874b8bSToomas Soome 	}
2172a3874b8bSToomas Soome 	if (scan_ds_queue_contains(scn, ds2->ds_object, &mintxg)) {
2173a3874b8bSToomas Soome 		scan_ds_queue_remove(scn, ds2->ds_object);
2174a3874b8bSToomas Soome 		scan_ds_queue_insert(scn, ds1->ds_object, mintxg);
2175a3874b8bSToomas Soome 	}
21763f9d6ad7SLin Ling 
21773f9d6ad7SLin Ling 	if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
21783f9d6ad7SLin Ling 	    ds1->ds_object, &mintxg) == 0) {
21793f9d6ad7SLin Ling 		int err;
2180c1379625SJustin T. Gibbs 		ASSERT3U(mintxg, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg);
2181c1379625SJustin T. Gibbs 		ASSERT3U(mintxg, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg);
2182b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
21833f9d6ad7SLin Ling 		    scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
21843f9d6ad7SLin Ling 		err = zap_add_int_key(dp->dp_meta_objset,
21853f9d6ad7SLin Ling 		    scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
21863f9d6ad7SLin Ling 		VERIFY(err == 0 || err == EEXIST);
21873f9d6ad7SLin Ling 		if (err == EEXIST) {
21883f9d6ad7SLin Ling 			/* Both were there to begin with */
21893f9d6ad7SLin Ling 			VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
21903f9d6ad7SLin Ling 			    scn->scn_phys.scn_queue_obj,
21913f9d6ad7SLin Ling 			    ds1->ds_object, mintxg, tx));
21923f9d6ad7SLin Ling 		}
21933f9d6ad7SLin Ling 		zfs_dbgmsg("clone_swap ds %llu; in queue; "
21943f9d6ad7SLin Ling 		    "replacing with %llu",
21953f9d6ad7SLin Ling 		    (u_longlong_t)ds1->ds_object,
21963f9d6ad7SLin Ling 		    (u_longlong_t)ds2->ds_object);
2197a3874b8bSToomas Soome 	}
2198a3874b8bSToomas Soome 	if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
2199a3874b8bSToomas Soome 	    ds2->ds_object, &mintxg) == 0) {
2200c1379625SJustin T. Gibbs 		ASSERT3U(mintxg, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg);
2201c1379625SJustin T. Gibbs 		ASSERT3U(mintxg, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg);
2202b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
22033f9d6ad7SLin Ling 		    scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
22043f9d6ad7SLin Ling 		VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
22053f9d6ad7SLin Ling 		    scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
22063f9d6ad7SLin Ling 		zfs_dbgmsg("clone_swap ds %llu; in queue; "
22073f9d6ad7SLin Ling 		    "replacing with %llu",
22083f9d6ad7SLin Ling 		    (u_longlong_t)ds2->ds_object,
22093f9d6ad7SLin Ling 		    (u_longlong_t)ds1->ds_object);
22103f9d6ad7SLin Ling 	}
22113f9d6ad7SLin Ling 
2212a3874b8bSToomas Soome 	dsl_scan_sync_state(scn, tx, SYNC_CACHED);
22133f9d6ad7SLin Ling }
22143f9d6ad7SLin Ling 
22153f9d6ad7SLin Ling /* ARGSUSED */
22163f9d6ad7SLin Ling static int
22173b2aab18SMatthew Ahrens enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
22183f9d6ad7SLin Ling {
2219a3874b8bSToomas Soome 	uint64_t originobj = *(uint64_t *)arg;
22203f9d6ad7SLin Ling 	dsl_dataset_t *ds;
22213f9d6ad7SLin Ling 	int err;
22223f9d6ad7SLin Ling 	dsl_scan_t *scn = dp->dp_scan;
22233f9d6ad7SLin Ling 
2224a3874b8bSToomas Soome 	if (dsl_dir_phys(hds->ds_dir)->dd_origin_obj != originobj)
22253b2aab18SMatthew Ahrens 		return (0);
22263b2aab18SMatthew Ahrens 
22273b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
22283f9d6ad7SLin Ling 	if (err)
22293f9d6ad7SLin Ling 		return (err);
22303f9d6ad7SLin Ling 
2231a3874b8bSToomas Soome 	while (dsl_dataset_phys(ds)->ds_prev_snap_obj != originobj) {
22323b2aab18SMatthew Ahrens 		dsl_dataset_t *prev;
22333b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp,
2234c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
22353f9d6ad7SLin Ling 
22363b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
22373b2aab18SMatthew Ahrens 		if (err)
22383b2aab18SMatthew Ahrens 			return (err);
22393b2aab18SMatthew Ahrens 		ds = prev;
22403f9d6ad7SLin Ling 	}
2241a3874b8bSToomas Soome 	scan_ds_queue_insert(scn, ds->ds_object,
2242a3874b8bSToomas Soome 	    dsl_dataset_phys(ds)->ds_prev_snap_txg);
22433f9d6ad7SLin Ling 	dsl_dataset_rele(ds, FTAG);
22443f9d6ad7SLin Ling 	return (0);
22453f9d6ad7SLin Ling }
22463f9d6ad7SLin Ling 
22473f9d6ad7SLin Ling static void
22483f9d6ad7SLin Ling dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
22493f9d6ad7SLin Ling {
22503f9d6ad7SLin Ling 	dsl_pool_t *dp = scn->scn_dp;
22513f9d6ad7SLin Ling 	dsl_dataset_t *ds;
22523f9d6ad7SLin Ling 
2253b420f3adSRichard Lowe 	VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
22543f9d6ad7SLin Ling 
225538d61036SMatthew Ahrens 	if (scn->scn_phys.scn_cur_min_txg >=
225638d61036SMatthew Ahrens 	    scn->scn_phys.scn_max_txg) {
225738d61036SMatthew Ahrens 		/*
225838d61036SMatthew Ahrens 		 * This can happen if this snapshot was created after the
225938d61036SMatthew Ahrens 		 * scan started, and we already completed a previous snapshot
226038d61036SMatthew Ahrens 		 * that was created after the scan started.  This snapshot
226138d61036SMatthew Ahrens 		 * only references blocks with:
226238d61036SMatthew Ahrens 		 *
226338d61036SMatthew Ahrens 		 *	birth < our ds_creation_txg
226438d61036SMatthew Ahrens 		 *	cur_min_txg is no less than ds_creation_txg.
226538d61036SMatthew Ahrens 		 *	We have already visited these blocks.
226638d61036SMatthew Ahrens 		 * or
226738d61036SMatthew Ahrens 		 *	birth > scn_max_txg
226838d61036SMatthew Ahrens 		 *	The scan requested not to visit these blocks.
226938d61036SMatthew Ahrens 		 *
227038d61036SMatthew Ahrens 		 * Subsequent snapshots (and clones) can reference our
227138d61036SMatthew Ahrens 		 * blocks, or blocks with even higher birth times.
227238d61036SMatthew Ahrens 		 * Therefore we do not need to visit them either,
227338d61036SMatthew Ahrens 		 * so we do not add them to the work queue.
227438d61036SMatthew Ahrens 		 *
227538d61036SMatthew Ahrens 		 * Note that checking for cur_min_txg >= cur_max_txg
227638d61036SMatthew Ahrens 		 * is not sufficient, because in that case we may need to
227738d61036SMatthew Ahrens 		 * visit subsequent snapshots.  This happens when min_txg > 0,
227838d61036SMatthew Ahrens 		 * which raises cur_min_txg.  In this case we will visit
227938d61036SMatthew Ahrens 		 * this dataset but skip all of its blocks, because the
228038d61036SMatthew Ahrens 		 * rootbp's birth time is < cur_min_txg.  Then we will
228138d61036SMatthew Ahrens 		 * add the next snapshots/clones to the work queue.
228238d61036SMatthew Ahrens 		 */
228338d61036SMatthew Ahrens 		char *dsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
228438d61036SMatthew Ahrens 		dsl_dataset_name(ds, dsname);
228538d61036SMatthew Ahrens 		zfs_dbgmsg("scanning dataset %llu (%s) is unnecessary because "
228638d61036SMatthew Ahrens 		    "cur_min_txg (%llu) >= max_txg (%llu)",
2287a3874b8bSToomas Soome 		    (longlong_t)dsobj, dsname,
2288a3874b8bSToomas Soome 		    (longlong_t)scn->scn_phys.scn_cur_min_txg,
2289a3874b8bSToomas Soome 		    (longlong_t)scn->scn_phys.scn_max_txg);
229038d61036SMatthew Ahrens 		kmem_free(dsname, MAXNAMELEN);
229138d61036SMatthew Ahrens 
229238d61036SMatthew Ahrens 		goto out;
229338d61036SMatthew Ahrens 	}
229438d61036SMatthew Ahrens 
22956e0cbcaaSMatthew Ahrens 	/*
22965cabbc6bSPrashanth Sreenivasa 	 * Only the ZIL in the head (non-snapshot) is valid. Even though
22976e0cbcaaSMatthew Ahrens 	 * snapshots can have ZIL block pointers (which may be the same
22985cabbc6bSPrashanth Sreenivasa 	 * BP as in the head), they must be ignored. In addition, $ORIGIN
22995cabbc6bSPrashanth Sreenivasa 	 * doesn't have a objset (i.e. its ds_bp is a hole) so we don't
23005cabbc6bSPrashanth Sreenivasa 	 * need to look for a ZIL in it either. So we traverse the ZIL here,
23015cabbc6bSPrashanth Sreenivasa 	 * rather than in scan_recurse(), because the regular snapshot
23025cabbc6bSPrashanth Sreenivasa 	 * block-sharing rules don't apply to it.
23036e0cbcaaSMatthew Ahrens 	 */
23045cabbc6bSPrashanth Sreenivasa 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds) &&
2305bb1f4245SMatthew Ahrens 	    (dp->dp_origin_snap == NULL ||
2306bb1f4245SMatthew Ahrens 	    ds->ds_dir != dp->dp_origin_snap->ds_dir)) {
23075cabbc6bSPrashanth Sreenivasa 		objset_t *os;
23085cabbc6bSPrashanth Sreenivasa 		if (dmu_objset_from_ds(ds, &os) != 0) {
23095cabbc6bSPrashanth Sreenivasa 			goto out;
23105cabbc6bSPrashanth Sreenivasa 		}
23116e0cbcaaSMatthew Ahrens 		dsl_scan_zil(dp, &os->os_zil_header);
23125cabbc6bSPrashanth Sreenivasa 	}
23136e0cbcaaSMatthew Ahrens 
23143f9d6ad7SLin Ling 	/*
23153f9d6ad7SLin Ling 	 * Iterate over the bps in this ds.
23163f9d6ad7SLin Ling 	 */
23173f9d6ad7SLin Ling 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
2318c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2319c1379625SJustin T. Gibbs 	dsl_scan_visit_rootbp(scn, ds, &dsl_dataset_phys(ds)->ds_bp, tx);
2320c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
23213f9d6ad7SLin Ling 
23229adfa60dSMatthew Ahrens 	char *dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
23233f9d6ad7SLin Ling 	dsl_dataset_name(ds, dsname);
23243f9d6ad7SLin Ling 	zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
23251702cce7SAlek Pinchuk 	    "suspending=%u",
23263f9d6ad7SLin Ling 	    (longlong_t)dsobj, dsname,
23273f9d6ad7SLin Ling 	    (longlong_t)scn->scn_phys.scn_cur_min_txg,
23283f9d6ad7SLin Ling 	    (longlong_t)scn->scn_phys.scn_cur_max_txg,
23291702cce7SAlek Pinchuk 	    (int)scn->scn_suspending);
23309adfa60dSMatthew Ahrens 	kmem_free(dsname, ZFS_MAX_DATASET_NAME_LEN);
23313f9d6ad7SLin Ling 
23321702cce7SAlek Pinchuk 	if (scn->scn_suspending)
23333f9d6ad7SLin Ling 		goto out;
23343f9d6ad7SLin Ling 
23353f9d6ad7SLin Ling 	/*
23363f9d6ad7SLin Ling 	 * We've finished this pass over this dataset.
23373f9d6ad7SLin Ling 	 */
23383f9d6ad7SLin Ling 
23393f9d6ad7SLin Ling 	/*
23403f9d6ad7SLin Ling 	 * If we did not completely visit this dataset, do another pass.
23413f9d6ad7SLin Ling 	 */
23423f9d6ad7SLin Ling 	if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
23433f9d6ad7SLin Ling 		zfs_dbgmsg("incomplete pass; visiting again");
23443f9d6ad7SLin Ling 		scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
2345a3874b8bSToomas Soome 		scan_ds_queue_insert(scn, ds->ds_object,
2346a3874b8bSToomas Soome 		    scn->scn_phys.scn_cur_max_txg);
23473f9d6ad7SLin Ling 		goto out;
23483f9d6ad7SLin Ling 	}
23493f9d6ad7SLin Ling 
23503f9d6ad7SLin Ling 	/*
23513f9d6ad7SLin Ling 	 * Add descendent datasets to work queue.
23523f9d6ad7SLin Ling 	 */
2353c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
2354a3874b8bSToomas Soome 		scan_ds_queue_insert(scn,
2355c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_next_snap_obj,
2356a3874b8bSToomas Soome 		    dsl_dataset_phys(ds)->ds_creation_txg);
23573f9d6ad7SLin Ling 	}
2358c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_num_children > 1) {
23593f9d6ad7SLin Ling 		boolean_t usenext = B_FALSE;
2360c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
23613f9d6ad7SLin Ling 			uint64_t count;
23623f9d6ad7SLin Ling 			/*
23633f9d6ad7SLin Ling 			 * A bug in a previous version of the code could
23643f9d6ad7SLin Ling 			 * cause upgrade_clones_cb() to not set
23653f9d6ad7SLin Ling 			 * ds_next_snap_obj when it should, leading to a
23663f9d6ad7SLin Ling 			 * missing entry.  Therefore we can only use the
23673f9d6ad7SLin Ling 			 * next_clones_obj when its count is correct.
23683f9d6ad7SLin Ling 			 */
23693f9d6ad7SLin Ling 			int err = zap_count(dp->dp_meta_objset,
2370c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_clones_obj, &count);
23713f9d6ad7SLin Ling 			if (err == 0 &&
2372c1379625SJustin T. Gibbs 			    count == dsl_dataset_phys(ds)->ds_num_children - 1)
23733f9d6ad7SLin Ling 				usenext = B_TRUE;
23743f9d6ad7SLin Ling 		}
23753f9d6ad7SLin Ling 
23763f9d6ad7SLin Ling 		if (usenext) {
2377a3874b8bSToomas Soome 			zap_cursor_t zc;
2378a3874b8bSToomas Soome 			zap_attribute_t za;
2379a3874b8bSToomas Soome 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
2380a3874b8bSToomas Soome 			    dsl_dataset_phys(ds)->ds_next_clones_obj);
2381a3874b8bSToomas Soome 			    zap_cursor_retrieve(&zc, &za) == 0;
2382a3874b8bSToomas Soome 			    (void) zap_cursor_advance(&zc)) {
2383a3874b8bSToomas Soome 				scan_ds_queue_insert(scn,
2384a3874b8bSToomas Soome 				    zfs_strtonum(za.za_name, NULL),
2385a3874b8bSToomas Soome 				    dsl_dataset_phys(ds)->ds_creation_txg);
2386a3874b8bSToomas Soome 			}
2387a3874b8bSToomas Soome 			zap_cursor_fini(&zc);
23883f9d6ad7SLin Ling 		} else {
23893b2aab18SMatthew Ahrens 			VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2390a3874b8bSToomas Soome 			    enqueue_clones_cb, &ds->ds_object,
2391a3874b8bSToomas Soome 			    DS_FIND_CHILDREN));
23923f9d6ad7SLin Ling 		}
23933f9d6ad7SLin Ling 	}
23943f9d6ad7SLin Ling 
23953f9d6ad7SLin Ling out:
23963f9d6ad7SLin Ling 	dsl_dataset_rele(ds, FTAG);
23973f9d6ad7SLin Ling }
23983f9d6ad7SLin Ling 
23993f9d6ad7SLin Ling /* ARGSUSED */
24003f9d6ad7SLin Ling static int
24013b2aab18SMatthew Ahrens enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
24023f9d6ad7SLin Ling {
24033f9d6ad7SLin Ling 	dsl_dataset_t *ds;
24043f9d6ad7SLin Ling 	int err;
24053f9d6ad7SLin Ling 	dsl_scan_t *scn = dp->dp_scan;
24063f9d6ad7SLin Ling 
24073b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
24083f9d6ad7SLin Ling 	if (err)
24093f9d6ad7SLin Ling 		return (err);
24103f9d6ad7SLin Ling 
2411c1379625SJustin T. Gibbs 	while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
24123f9d6ad7SLin Ling 		dsl_dataset_t *prev;
2413c1379625SJustin T. Gibbs 		err = dsl_dataset_hold_obj(dp,
2414c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
24153f9d6ad7SLin Ling 		if (err) {
24163f9d6ad7SLin Ling 			dsl_dataset_rele(ds, FTAG);
24173f9d6ad7SLin Ling 			return (err);
24183f9d6ad7SLin Ling 		}
24193f9d6ad7SLin Ling 
24203f9d6ad7SLin Ling 		/*
24213f9d6ad7SLin Ling 		 * If this is a clone, we don't need to worry about it for now.
24223f9d6ad7SLin Ling 		 */
2423c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) {
24243f9d6ad7SLin Ling 			dsl_dataset_rele(ds, FTAG);
24253f9d6ad7SLin Ling 			dsl_dataset_rele(prev, FTAG);
24263f9d6ad7SLin Ling 			return (0);
24273f9d6ad7SLin Ling 		}
24283f9d6ad7SLin Ling 		dsl_dataset_rele(ds, FTAG);
24293f9d6ad7SLin Ling 		ds = prev;
24303f9d6ad7SLin Ling 	}
24313f9d6ad7SLin Ling 
2432a3874b8bSToomas Soome 	scan_ds_queue_insert(scn, ds->ds_object,
2433a3874b8bSToomas Soome 	    dsl_dataset_phys(ds)->ds_prev_snap_txg);
24343f9d6ad7SLin Ling 	dsl_dataset_rele(ds, FTAG);
24353f9d6ad7SLin Ling 	return (0);
24363f9d6ad7SLin Ling }
24373f9d6ad7SLin Ling 
2438a3874b8bSToomas Soome /* ARGSUSED */
2439a3874b8bSToomas Soome void
2440a3874b8bSToomas Soome dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
2441a3874b8bSToomas Soome     ddt_entry_t *dde, dmu_tx_t *tx)
2442a3874b8bSToomas Soome {
2443a3874b8bSToomas Soome 	const ddt_key_t *ddk = &dde->dde_key;
2444a3874b8bSToomas Soome 	ddt_phys_t *ddp = dde->dde_phys;
2445a3874b8bSToomas Soome 	blkptr_t bp;
2446a3874b8bSToomas Soome 	zbookmark_phys_t zb = { 0 };
2447a3874b8bSToomas Soome 	int p;
2448a3874b8bSToomas Soome 
2449a3874b8bSToomas Soome 	if (scn->scn_phys.scn_state != DSS_SCANNING)
2450a3874b8bSToomas Soome 		return;
2451a3874b8bSToomas Soome 
2452e4c795beSTom Caputi 	/*
2453e4c795beSTom Caputi 	 * This function is special because it is the only thing
2454e4c795beSTom Caputi 	 * that can add scan_io_t's to the vdev scan queues from
2455e4c795beSTom Caputi 	 * outside dsl_scan_sync(). For the most part this is ok
2456e4c795beSTom Caputi 	 * as long as it is called from within syncing context.
2457e4c795beSTom Caputi 	 * However, dsl_scan_sync() expects that no new sio's will
2458e4c795beSTom Caputi 	 * be added between when all the work for a scan is done
2459e4c795beSTom Caputi 	 * and the next txg when the scan is actually marked as
2460e4c795beSTom Caputi 	 * completed. This check ensures we do not issue new sio's
2461e4c795beSTom Caputi 	 * during this period.
2462e4c795beSTom Caputi 	 */
2463e4c795beSTom Caputi 	if (scn->scn_done_txg != 0)
2464e4c795beSTom Caputi 		return;
2465e4c795beSTom Caputi 
2466a3874b8bSToomas Soome 	for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2467a3874b8bSToomas Soome 		if (ddp->ddp_phys_birth == 0 ||
2468a3874b8bSToomas Soome 		    ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg)
2469a3874b8bSToomas Soome 			continue;
2470a3874b8bSToomas Soome 		ddt_bp_create(checksum, ddk, ddp, &bp);
2471a3874b8bSToomas Soome 
2472a3874b8bSToomas Soome 		scn->scn_visited_this_txg++;
2473a3874b8bSToomas Soome 		scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
2474a3874b8bSToomas Soome 	}
2475a3874b8bSToomas Soome }
2476a3874b8bSToomas Soome 
2477a3874b8bSToomas Soome /*
24783f9d6ad7SLin Ling  * Scrub/dedup interaction.
24793f9d6ad7SLin Ling  *
24803f9d6ad7SLin Ling  * If there are N references to a deduped block, we don't want to scrub it
24813f9d6ad7SLin Ling  * N times -- ideally, we should scrub it exactly once.
24823f9d6ad7SLin Ling  *
24833f9d6ad7SLin Ling  * We leverage the fact that the dde's replication class (enum ddt_class)
24843f9d6ad7SLin Ling  * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
24853f9d6ad7SLin Ling  * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
24863f9d6ad7SLin Ling  *
24873f9d6ad7SLin Ling  * To prevent excess scrubbing, the scrub begins by walking the DDT
24883f9d6ad7SLin Ling  * to find all blocks with refcnt > 1, and scrubs each of these once.
24893f9d6ad7SLin Ling  * Since there are two replication classes which contain blocks with
24903f9d6ad7SLin Ling  * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
24913f9d6ad7SLin Ling  * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
24923f9d6ad7SLin Ling  *
24933f9d6ad7SLin Ling  * There would be nothing more to say if a block's refcnt couldn't change
24943f9d6ad7SLin Ling  * during a scrub, but of course it can so we must account for changes
24953f9d6ad7SLin Ling  * in a block's replication class.
24963f9d6ad7SLin Ling  *
24973f9d6ad7SLin Ling  * Here's an example of what can occur:
24983f9d6ad7SLin Ling  *
24993f9d6ad7SLin Ling  * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
25003f9d6ad7SLin Ling  * when visited during the top-down scrub phase, it will be scrubbed twice.
25013f9d6ad7SLin Ling  * This negates our scrub optimization, but is otherwise harmless.
25023f9d6ad7SLin Ling  *
25033f9d6ad7SLin Ling  * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
25043f9d6ad7SLin Ling  * on each visit during the top-down scrub phase, it will never be scrubbed.
25053f9d6ad7SLin Ling  * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
25063f9d6ad7SLin Ling  * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
25073f9d6ad7SLin Ling  * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
25083f9d6ad7SLin Ling  * while a scrub is in progress, it scrubs the block right then.
25093f9d6ad7SLin Ling  */
25103f9d6ad7SLin Ling static void
25113f9d6ad7SLin Ling dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
25123f9d6ad7SLin Ling {
25133f9d6ad7SLin Ling 	ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
25143f9d6ad7SLin Ling 	ddt_entry_t dde = { 0 };
25153f9d6ad7SLin Ling 	int error;
25163f9d6ad7SLin Ling 	uint64_t n = 0;
25173f9d6ad7SLin Ling 
25183f9d6ad7SLin Ling 	while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
25193f9d6ad7SLin Ling 		ddt_t *ddt;
25203f9d6ad7SLin Ling 
25213f9d6ad7SLin Ling 		if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
25223f9d6ad7SLin Ling 			break;
25233f9d6ad7SLin Ling 		dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
25243f9d6ad7SLin Ling 		    (longlong_t)ddb->ddb_class,
25253f9d6ad7SLin Ling 		    (longlong_t)ddb->ddb_type,
25263f9d6ad7SLin Ling 		    (longlong_t)ddb->ddb_checksum,
25273f9d6ad7SLin Ling 		    (longlong_t)ddb->ddb_cursor);
25283f9d6ad7SLin Ling 
25293f9d6ad7SLin Ling 		/* There should be no pending changes to the dedup table */
25303f9d6ad7SLin Ling 		ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
25313f9d6ad7SLin Ling 		ASSERT(avl_first(&ddt->ddt_tree) == NULL);
25323f9d6ad7SLin Ling 
25333f9d6ad7SLin Ling 		dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
25343f9d6ad7SLin Ling 		n++;
25353f9d6ad7SLin Ling 
25361702cce7SAlek Pinchuk 		if (dsl_scan_check_suspend(scn, NULL))
25373f9d6ad7SLin Ling 			break;
25383f9d6ad7SLin Ling 	}
25393f9d6ad7SLin Ling 
25401702cce7SAlek Pinchuk 	zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; "
25411702cce7SAlek Pinchuk 	    "suspending=%u", (longlong_t)n,
25421702cce7SAlek Pinchuk 	    (int)scn->scn_phys.scn_ddt_class_max, (int)scn->scn_suspending);
25433f9d6ad7SLin Ling 
25443f9d6ad7SLin Ling 	ASSERT(error == 0 || error == ENOENT);
25453f9d6ad7SLin Ling 	ASSERT(error != ENOENT ||
25463f9d6ad7SLin Ling 	    ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
25473f9d6ad7SLin Ling }
25483f9d6ad7SLin Ling 
2549a3874b8bSToomas Soome static uint64_t
2550a3874b8bSToomas Soome dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
25513f9d6ad7SLin Ling {
2552a3874b8bSToomas Soome 	uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
2553a3874b8bSToomas Soome 	if (ds->ds_is_snapshot)
2554a3874b8bSToomas Soome 		return (MIN(smt, dsl_dataset_phys(ds)->ds_creation_txg));
2555a3874b8bSToomas Soome 	return (smt);
25563f9d6ad7SLin Ling }
25573f9d6ad7SLin Ling 
25583f9d6ad7SLin Ling static void
25593f9d6ad7SLin Ling dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
25603f9d6ad7SLin Ling {
2561a3874b8bSToomas Soome 	scan_ds_t *sds;
25623f9d6ad7SLin Ling 	dsl_pool_t *dp = scn->scn_dp;
25633f9d6ad7SLin Ling 
25643f9d6ad7SLin Ling 	if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
25653f9d6ad7SLin Ling 	    scn->scn_phys.scn_ddt_class_max) {
25663f9d6ad7SLin Ling 		scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
25673f9d6ad7SLin Ling 		scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
25683f9d6ad7SLin Ling 		dsl_scan_ddt(scn, tx);
25691702cce7SAlek Pinchuk 		if (scn->scn_suspending)
25703f9d6ad7SLin Ling 			return;
25713f9d6ad7SLin Ling 	}
25723f9d6ad7SLin Ling 
25733f9d6ad7SLin Ling 	if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
25743f9d6ad7SLin Ling 		/* First do the MOS & ORIGIN */
25753f9d6ad7SLin Ling 
25763f9d6ad7SLin Ling 		scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
25773f9d6ad7SLin Ling 		scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
25783f9d6ad7SLin Ling 		dsl_scan_visit_rootbp(scn, NULL,
25793f9d6ad7SLin Ling 		    &dp->dp_meta_rootbp, tx);
25803f9d6ad7SLin Ling 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
25811702cce7SAlek Pinchuk 		if (scn->scn_suspending)
25823f9d6ad7SLin Ling 			return;
25833f9d6ad7SLin Ling 
25843f9d6ad7SLin Ling 		if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
25853b2aab18SMatthew Ahrens 			VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2586a3874b8bSToomas Soome 			    enqueue_cb, NULL, DS_FIND_CHILDREN));
25873f9d6ad7SLin Ling 		} else {
25883f9d6ad7SLin Ling 			dsl_scan_visitds(scn,
25893f9d6ad7SLin Ling 			    dp->dp_origin_snap->ds_object, tx);
25903f9d6ad7SLin Ling 		}
25911702cce7SAlek Pinchuk 		ASSERT(!scn->scn_suspending);
25923f9d6ad7SLin Ling 	} else if (scn->scn_phys.scn_bookmark.zb_objset !=
25933f9d6ad7SLin Ling 	    ZB_DESTROYED_OBJSET) {
2594a3874b8bSToomas Soome 		uint64_t dsobj = scn->scn_phys.scn_bookmark.zb_objset;
25953f9d6ad7SLin Ling 		/*
2596a3874b8bSToomas Soome 		 * If we were suspended, continue from here. Note if the
25971702cce7SAlek Pinchuk 		 * ds we were suspended on was deleted, the zb_objset may
25983f9d6ad7SLin Ling 		 * be -1, so we will skip this and find a new objset
25993f9d6ad7SLin Ling 		 * below.
26003f9d6ad7SLin Ling 		 */
2601a3874b8bSToomas Soome 		dsl_scan_visitds(scn, dsobj, tx);
26021702cce7SAlek Pinchuk 		if (scn->scn_suspending)
26033f9d6ad7SLin Ling 			return;
26043f9d6ad7SLin Ling 	}
26053f9d6ad7SLin Ling 
26063f9d6ad7SLin Ling 	/*
2607a3874b8bSToomas Soome 	 * In case we suspended right at the end of the ds, zero the
26083f9d6ad7SLin Ling 	 * bookmark so we don't think that we're still trying to resume.
26093f9d6ad7SLin Ling 	 */
26107802d7bfSMatthew Ahrens 	bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_phys_t));
26113f9d6ad7SLin Ling 
2612a3874b8bSToomas Soome 	/*
2613a3874b8bSToomas Soome 	 * Keep pulling things out of the dataset avl queue. Updates to the
2614a3874b8bSToomas Soome 	 * persistent zap-object-as-queue happen only at checkpoints.
2615a3874b8bSToomas Soome 	 */
2616a3874b8bSToomas Soome 	while ((sds = avl_first(&scn->scn_queue)) != NULL) {
26173f9d6ad7SLin Ling 		dsl_dataset_t *ds;
2618a3874b8bSToomas Soome 		uint64_t dsobj = sds->sds_dsobj;
2619a3874b8bSToomas Soome 		uint64_t txg = sds->sds_txg;
26203f9d6ad7SLin Ling 
2621a3874b8bSToomas Soome 		/* dequeue and free the ds from the queue */
2622a3874b8bSToomas Soome 		scan_ds_queue_remove(scn, dsobj);
2623a3874b8bSToomas Soome 		sds = NULL;	/* must not be touched after removal */
26243f9d6ad7SLin Ling 
2625a3874b8bSToomas Soome 		/* Set up min / max txg */
2626b420f3adSRichard Lowe 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
2627a3874b8bSToomas Soome 		if (txg != 0) {
26283f9d6ad7SLin Ling 			scn->scn_phys.scn_cur_min_txg =
2629a3874b8bSToomas Soome 			    MAX(scn->scn_phys.scn_min_txg, txg);
26303f9d6ad7SLin Ling 		} else {
26313f9d6ad7SLin Ling 			scn->scn_phys.scn_cur_min_txg =
26323f9d6ad7SLin Ling 			    MAX(scn->scn_phys.scn_min_txg,
2633c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_prev_snap_txg);
26343f9d6ad7SLin Ling 		}
26353f9d6ad7SLin Ling 		scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
26363f9d6ad7SLin Ling 		dsl_dataset_rele(ds, FTAG);
26373f9d6ad7SLin Ling 
26383f9d6ad7SLin Ling 		dsl_scan_visitds(scn, dsobj, tx);
26391702cce7SAlek Pinchuk 		if (scn->scn_suspending)
26403f9d6ad7SLin Ling 			return;
26413f9d6ad7SLin Ling 	}
2642a3874b8bSToomas Soome 	/* No more objsets to fetch, we're done */
2643a3874b8bSToomas Soome 	scn->scn_phys.scn_bookmark.zb_objset = ZB_DESTROYED_OBJSET;
2644a3874b8bSToomas Soome 	ASSERT0(scn->scn_suspending);
2645a3874b8bSToomas Soome }
2646a3874b8bSToomas Soome 
2647a3874b8bSToomas Soome static uint64_t
2648a3874b8bSToomas Soome dsl_scan_count_leaves(vdev_t *vd)
2649a3874b8bSToomas Soome {
2650a3874b8bSToomas Soome 	uint64_t i, leaves = 0;
2651a3874b8bSToomas Soome 
2652a3874b8bSToomas Soome 	/* we only count leaves that belong to the main pool and are readable */
2653a3874b8bSToomas Soome 	if (vd->vdev_islog || vd->vdev_isspare ||
2654a3874b8bSToomas Soome 	    vd->vdev_isl2cache || !vdev_readable(vd))
2655a3874b8bSToomas Soome 		return (0);
2656a3874b8bSToomas Soome 
2657a3874b8bSToomas Soome 	if (vd->vdev_ops->vdev_op_leaf)
2658a3874b8bSToomas Soome 		return (1);
2659a3874b8bSToomas Soome 
2660a3874b8bSToomas Soome 	for (i = 0; i < vd->vdev_children; i++) {
2661a3874b8bSToomas Soome 		leaves += dsl_scan_count_leaves(vd->vdev_child[i]);
2662a3874b8bSToomas Soome 	}
2663a3874b8bSToomas Soome 
2664a3874b8bSToomas Soome 	return (leaves);
2665a3874b8bSToomas Soome }
2666a3874b8bSToomas Soome 
2667a3874b8bSToomas Soome 
2668a3874b8bSToomas Soome static void
2669a3874b8bSToomas Soome scan_io_queues_update_zio_stats(dsl_scan_io_queue_t *q, const blkptr_t *bp)
2670a3874b8bSToomas Soome {
2671a3874b8bSToomas Soome 	int i;
2672a3874b8bSToomas Soome 	uint64_t cur_size = 0;
2673a3874b8bSToomas Soome 
2674a3874b8bSToomas Soome 	for (i = 0; i < BP_GET_NDVAS(bp); i++) {
2675a3874b8bSToomas Soome 		cur_size += DVA_GET_ASIZE(&bp->blk_dva[i]);
2676a3874b8bSToomas Soome 	}
2677a3874b8bSToomas Soome 
2678a3874b8bSToomas Soome 	q->q_total_zio_size_this_txg += cur_size;
2679a3874b8bSToomas Soome 	q->q_zios_this_txg++;
2680a3874b8bSToomas Soome }
2681a3874b8bSToomas Soome 
2682a3874b8bSToomas Soome static void
2683a3874b8bSToomas Soome scan_io_queues_update_seg_stats(dsl_scan_io_queue_t *q, uint64_t start,
2684a3874b8bSToomas Soome     uint64_t end)
2685a3874b8bSToomas Soome {
2686a3874b8bSToomas Soome 	q->q_total_seg_size_this_txg += end - start;
2687a3874b8bSToomas Soome 	q->q_segs_this_txg++;
2688a3874b8bSToomas Soome }
2689a3874b8bSToomas Soome 
2690a3874b8bSToomas Soome static boolean_t
2691a3874b8bSToomas Soome scan_io_queue_check_suspend(dsl_scan_t *scn)
2692a3874b8bSToomas Soome {
2693a3874b8bSToomas Soome 	/* See comment in dsl_scan_check_suspend() */
2694a3874b8bSToomas Soome 	uint64_t curr_time_ns = gethrtime();
2695a3874b8bSToomas Soome 	uint64_t scan_time_ns = curr_time_ns - scn->scn_sync_start_time;
2696a3874b8bSToomas Soome 	uint64_t sync_time_ns = curr_time_ns -
2697a3874b8bSToomas Soome 	    scn->scn_dp->dp_spa->spa_sync_starttime;
2698a3874b8bSToomas Soome 	int dirty_pct = scn->scn_dp->dp_dirty_total * 100 / zfs_dirty_data_max;
2699a3874b8bSToomas Soome 	int mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
2700a3874b8bSToomas Soome 	    zfs_resilver_min_time_ms : zfs_scrub_min_time_ms;
2701a3874b8bSToomas Soome 
2702a3874b8bSToomas Soome 	return ((NSEC2MSEC(scan_time_ns) > mintime &&
2703a3874b8bSToomas Soome 	    (dirty_pct >= zfs_vdev_async_write_active_min_dirty_percent ||
2704a3874b8bSToomas Soome 	    txg_sync_waiting(scn->scn_dp) ||
2705a3874b8bSToomas Soome 	    NSEC2SEC(sync_time_ns) >= zfs_txg_timeout)) ||
2706a3874b8bSToomas Soome 	    spa_shutting_down(scn->scn_dp->dp_spa));
2707a3874b8bSToomas Soome }
2708a3874b8bSToomas Soome 
2709a3874b8bSToomas Soome /*
2710a3874b8bSToomas Soome  * Given a list of scan_io_t's in io_list, this issues the io's out to
2711a3874b8bSToomas Soome  * disk. This consumes the io_list and frees the scan_io_t's. This is
2712a3874b8bSToomas Soome  * called when emptying queues, either when we're up against the memory
2713a3874b8bSToomas Soome  * limit or when we have finished scanning. Returns B_TRUE if we stopped
2714a3874b8bSToomas Soome  * processing the list before we finished. Any zios that were not issued
2715a3874b8bSToomas Soome  * will remain in the io_list.
2716a3874b8bSToomas Soome  */
2717a3874b8bSToomas Soome static boolean_t
2718a3874b8bSToomas Soome scan_io_queue_issue(dsl_scan_io_queue_t *queue, list_t *io_list)
2719a3874b8bSToomas Soome {
2720a3874b8bSToomas Soome 	dsl_scan_t *scn = queue->q_scn;
2721a3874b8bSToomas Soome 	scan_io_t *sio;
2722a3874b8bSToomas Soome 	int64_t bytes_issued = 0;
2723a3874b8bSToomas Soome 	boolean_t suspended = B_FALSE;
2724a3874b8bSToomas Soome 
2725a3874b8bSToomas Soome 	while ((sio = list_head(io_list)) != NULL) {
2726a3874b8bSToomas Soome 		blkptr_t bp;
2727a3874b8bSToomas Soome 
2728a3874b8bSToomas Soome 		if (scan_io_queue_check_suspend(scn)) {
2729a3874b8bSToomas Soome 			suspended = B_TRUE;
2730a3874b8bSToomas Soome 			break;
2731a3874b8bSToomas Soome 		}
2732a3874b8bSToomas Soome 
273312a8814cSTom Caputi 		sio2bp(sio, &bp);
273412a8814cSTom Caputi 		bytes_issued += SIO_GET_ASIZE(sio);
2735a3874b8bSToomas Soome 		scan_exec_io(scn->scn_dp, &bp, sio->sio_flags,
2736a3874b8bSToomas Soome 		    &sio->sio_zb, queue);
2737a3874b8bSToomas Soome 		(void) list_remove_head(io_list);
2738a3874b8bSToomas Soome 		scan_io_queues_update_zio_stats(queue, &bp);
273912a8814cSTom Caputi 		sio_free(sio);
2740a3874b8bSToomas Soome 	}
2741a3874b8bSToomas Soome 
2742a3874b8bSToomas Soome 	atomic_add_64(&scn->scn_bytes_pending, -bytes_issued);
2743a3874b8bSToomas Soome 
2744a3874b8bSToomas Soome 	return (suspended);
2745a3874b8bSToomas Soome }
2746a3874b8bSToomas Soome 
2747a3874b8bSToomas Soome /*
2748a3874b8bSToomas Soome  * Given a range_seg_t (extent) and a list, this function passes over a
2749a3874b8bSToomas Soome  * scan queue and gathers up the appropriate ios which fit into that
2750a3874b8bSToomas Soome  * scan seg (starting from lowest LBA). At the end, we remove the segment
2751a3874b8bSToomas Soome  * from the q_exts_by_addr range tree.
2752a3874b8bSToomas Soome  */
2753a3874b8bSToomas Soome static boolean_t
2754a3874b8bSToomas Soome scan_io_queue_gather(dsl_scan_io_queue_t *queue, range_seg_t *rs, list_t *list)
2755a3874b8bSToomas Soome {
275612a8814cSTom Caputi 	scan_io_t *srch_sio, *sio, *next_sio;
2757a3874b8bSToomas Soome 	avl_index_t idx;
2758a3874b8bSToomas Soome 	uint_t num_sios = 0;
2759a3874b8bSToomas Soome 	int64_t bytes_issued = 0;
2760a3874b8bSToomas Soome 
2761a3874b8bSToomas Soome 	ASSERT(rs != NULL);
2762a3874b8bSToomas Soome 	ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock));
2763a3874b8bSToomas Soome 
276412a8814cSTom Caputi 	srch_sio = sio_alloc(1);
276512a8814cSTom Caputi 	srch_sio->sio_nr_dvas = 1;
27664d7988d6SPaul Dagnelie 	SIO_SET_OFFSET(srch_sio, rs_get_start(rs, queue->q_exts_by_addr));
2767a3874b8bSToomas Soome 
2768a3874b8bSToomas Soome 	/*
2769a3874b8bSToomas Soome 	 * The exact start of the extent might not contain any matching zios,
2770a3874b8bSToomas Soome 	 * so if that's the case, examine the next one in the tree.
2771a3874b8bSToomas Soome 	 */
277212a8814cSTom Caputi 	sio = avl_find(&queue->q_sios_by_addr, srch_sio, &idx);
277312a8814cSTom Caputi 	sio_free(srch_sio);
277412a8814cSTom Caputi 
2775a3874b8bSToomas Soome 	if (sio == NULL)
2776a3874b8bSToomas Soome 		sio = avl_nearest(&queue->q_sios_by_addr, idx, AVL_AFTER);
2777a3874b8bSToomas Soome 
27784d7988d6SPaul Dagnelie 	while (sio != NULL && SIO_GET_OFFSET(sio) < rs_get_end(rs,
27794d7988d6SPaul Dagnelie 	    queue->q_exts_by_addr) && num_sios <= 32) {
27804d7988d6SPaul Dagnelie 		ASSERT3U(SIO_GET_OFFSET(sio), >=, rs_get_start(rs,
27814d7988d6SPaul Dagnelie 		    queue->q_exts_by_addr));
27824d7988d6SPaul Dagnelie 		ASSERT3U(SIO_GET_END_OFFSET(sio), <=, rs_get_end(rs,
27834d7988d6SPaul Dagnelie 		    queue->q_exts_by_addr));
2784a3874b8bSToomas Soome 
2785a3874b8bSToomas Soome 		next_sio = AVL_NEXT(&queue->q_sios_by_addr, sio);
2786a3874b8bSToomas Soome 		avl_remove(&queue->q_sios_by_addr, sio);
278712a8814cSTom Caputi 		queue->q_sio_memused -= SIO_GET_MUSED(sio);
2788a3874b8bSToomas Soome 
278912a8814cSTom Caputi 		bytes_issued += SIO_GET_ASIZE(sio);
2790a3874b8bSToomas Soome 		num_sios++;
2791a3874b8bSToomas Soome 		list_insert_tail(list, sio);
2792a3874b8bSToomas Soome 		sio = next_sio;
2793a3874b8bSToomas Soome 	}
2794a3874b8bSToomas Soome 
2795a3874b8bSToomas Soome 	/*
2796a3874b8bSToomas Soome 	 * We limit the number of sios we process at once to 32 to avoid
2797a3874b8bSToomas Soome 	 * biting off more than we can chew. If we didn't take everything
2798a3874b8bSToomas Soome 	 * in the segment we update it to reflect the work we were able to
2799a3874b8bSToomas Soome 	 * complete. Otherwise, we remove it from the range tree entirely.
2800a3874b8bSToomas Soome 	 */
28014d7988d6SPaul Dagnelie 	if (sio != NULL && SIO_GET_OFFSET(sio) < rs_get_end(rs,
28024d7988d6SPaul Dagnelie 	    queue->q_exts_by_addr)) {
2803a3874b8bSToomas Soome 		range_tree_adjust_fill(queue->q_exts_by_addr, rs,
2804a3874b8bSToomas Soome 		    -bytes_issued);
2805a3874b8bSToomas Soome 		range_tree_resize_segment(queue->q_exts_by_addr, rs,
28064d7988d6SPaul Dagnelie 		    SIO_GET_OFFSET(sio), rs_get_end(rs,
28074d7988d6SPaul Dagnelie 		    queue->q_exts_by_addr) - SIO_GET_OFFSET(sio));
2808a3874b8bSToomas Soome 
2809a3874b8bSToomas Soome 		return (B_TRUE);
2810a3874b8bSToomas Soome 	} else {
28114d7988d6SPaul Dagnelie 		uint64_t rstart = rs_get_start(rs, queue->q_exts_by_addr);
28124d7988d6SPaul Dagnelie 		uint64_t rend = rs_get_end(rs, queue->q_exts_by_addr);
28134d7988d6SPaul Dagnelie 		range_tree_remove(queue->q_exts_by_addr, rstart, rend - rstart);
2814a3874b8bSToomas Soome 		return (B_FALSE);
2815a3874b8bSToomas Soome 	}
2816a3874b8bSToomas Soome }
2817a3874b8bSToomas Soome 
2818a3874b8bSToomas Soome 
2819a3874b8bSToomas Soome /*
2820a3874b8bSToomas Soome  * This is called from the queue emptying thread and selects the next
2821a3874b8bSToomas Soome  * extent from which we are to issue io's. The behavior of this function
2822a3874b8bSToomas Soome  * depends on the state of the scan, the current memory consumption and
2823a3874b8bSToomas Soome  * whether or not we are performing a scan shutdown.
2824a3874b8bSToomas Soome  * 1) We select extents in an elevator algorithm (LBA-order) if the scan
2825a3874b8bSToomas Soome  *	needs to perform a checkpoint
2826a3874b8bSToomas Soome  * 2) We select the largest available extent if we are up against the
2827a3874b8bSToomas Soome  *	memory limit.
2828a3874b8bSToomas Soome  * 3) Otherwise we don't select any extents.
2829a3874b8bSToomas Soome  */
2830a3874b8bSToomas Soome static const range_seg_t *
2831a3874b8bSToomas Soome scan_io_queue_fetch_ext(dsl_scan_io_queue_t *queue)
2832a3874b8bSToomas Soome {
2833a3874b8bSToomas Soome 	dsl_scan_t *scn = queue->q_scn;
28344d7988d6SPaul Dagnelie 	range_tree_t *rt = queue->q_exts_by_addr;
2835a3874b8bSToomas Soome 
2836a3874b8bSToomas Soome 	ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock));
2837a3874b8bSToomas Soome 	ASSERT(scn->scn_is_sorted);
2838a3874b8bSToomas Soome 
2839a3874b8bSToomas Soome 	/* handle tunable overrides */
2840a3874b8bSToomas Soome 	if (scn->scn_checkpointing || scn->scn_clearing) {
2841a3874b8bSToomas Soome 		if (zfs_scan_issue_strategy == 1) {
28424d7988d6SPaul Dagnelie 			return (range_tree_first(rt));
2843a3874b8bSToomas Soome 		} else if (zfs_scan_issue_strategy == 2) {
2844bfb9edc9SPaul Dagnelie 			/*
2845bfb9edc9SPaul Dagnelie 			 * We need to get the original entry in the by_addr
2846bfb9edc9SPaul Dagnelie 			 * tree so we can modify it.
2847bfb9edc9SPaul Dagnelie 			 */
28484d7988d6SPaul Dagnelie 			range_seg_t *size_rs =
28494d7988d6SPaul Dagnelie 			    zfs_btree_first(&queue->q_exts_by_size, NULL);
2850bfb9edc9SPaul Dagnelie 			if (size_rs == NULL)
2851bfb9edc9SPaul Dagnelie 				return (NULL);
28524d7988d6SPaul Dagnelie 			uint64_t start = rs_get_start(size_rs, rt);
28534d7988d6SPaul Dagnelie 			uint64_t size = rs_get_end(size_rs, rt) - start;
28544d7988d6SPaul Dagnelie 			range_seg_t *addr_rs = range_tree_find(rt, start,
28554d7988d6SPaul Dagnelie 			    size);
28564d7988d6SPaul Dagnelie 			ASSERT3P(addr_rs, !=, NULL);
2857bfb9edc9SPaul Dagnelie 			ASSERT3U(rs_get_start(size_rs, rt), ==,
2858bfb9edc9SPaul Dagnelie 			    rs_get_start(addr_rs, rt));
2859bfb9edc9SPaul Dagnelie 			ASSERT3U(rs_get_end(size_rs, rt), ==,
2860bfb9edc9SPaul Dagnelie 			    rs_get_end(addr_rs, rt));
28614d7988d6SPaul Dagnelie 			return (addr_rs);
2862a3874b8bSToomas Soome 		}
2863a3874b8bSToomas Soome 	}
2864a3874b8bSToomas Soome 
2865a3874b8bSToomas Soome 	/*
2866a3874b8bSToomas Soome 	 * During normal clearing, we want to issue our largest segments
2867a3874b8bSToomas Soome 	 * first, keeping IO as sequential as possible, and leaving the
2868a3874b8bSToomas Soome 	 * smaller extents for later with the hope that they might eventually
2869a3874b8bSToomas Soome 	 * grow to larger sequential segments. However, when the scan is
2870a3874b8bSToomas Soome 	 * checkpointing, no new extents will be added to the sorting queue,
2871a3874b8bSToomas Soome 	 * so the way we are sorted now is as good as it will ever get.
2872a3874b8bSToomas Soome 	 * In this case, we instead switch to issuing extents in LBA order.
2873a3874b8bSToomas Soome 	 */
2874a3874b8bSToomas Soome 	if (scn->scn_checkpointing) {
28754d7988d6SPaul Dagnelie 		return (range_tree_first(rt));
2876a3874b8bSToomas Soome 	} else if (scn->scn_clearing) {
2877bfb9edc9SPaul Dagnelie 		/*
2878bfb9edc9SPaul Dagnelie 		 * We need to get the original entry in the by_addr
2879bfb9edc9SPaul Dagnelie 		 * tree so we can modify it.
2880bfb9edc9SPaul Dagnelie 		 */
28814d7988d6SPaul Dagnelie 		range_seg_t *size_rs = zfs_btree_first(&queue->q_exts_by_size,
28824d7988d6SPaul Dagnelie 		    NULL);
2883bfb9edc9SPaul Dagnelie 		if (size_rs == NULL)
2884bfb9edc9SPaul Dagnelie 			return (NULL);
28854d7988d6SPaul Dagnelie 		uint64_t start = rs_get_start(size_rs, rt);
28864d7988d6SPaul Dagnelie 		uint64_t size = rs_get_end(size_rs, rt) - start;
28874d7988d6SPaul Dagnelie 		range_seg_t *addr_rs = range_tree_find(rt, start, size);
28884d7988d6SPaul Dagnelie 		ASSERT3P(addr_rs, !=, NULL);
2889bfb9edc9SPaul Dagnelie 		ASSERT3U(rs_get_start(size_rs, rt), ==, rs_get_start(addr_rs,
2890bfb9edc9SPaul Dagnelie 		    rt));
2891bfb9edc9SPaul Dagnelie 		ASSERT3U(rs_get_end(size_rs, rt), ==, rs_get_end(addr_rs, rt));
28924d7988d6SPaul Dagnelie 		return (addr_rs);
2893a3874b8bSToomas Soome 	} else {
2894a3874b8bSToomas Soome 		return (NULL);
2895a3874b8bSToomas Soome 	}
2896a3874b8bSToomas Soome }
2897a3874b8bSToomas Soome 
2898a3874b8bSToomas Soome static void
2899a3874b8bSToomas Soome scan_io_queues_run_one(void *arg)
2900a3874b8bSToomas Soome {
2901a3874b8bSToomas Soome 	dsl_scan_io_queue_t *queue = arg;
2902a3874b8bSToomas Soome 	kmutex_t *q_lock = &queue->q_vd->vdev_scan_io_queue_lock;
2903a3874b8bSToomas Soome 	boolean_t suspended = B_FALSE;
2904a3874b8bSToomas Soome 	range_seg_t *rs = NULL;
2905a3874b8bSToomas Soome 	scan_io_t *sio = NULL;
2906a3874b8bSToomas Soome 	list_t sio_list;
2907a3874b8bSToomas Soome 	uint64_t bytes_per_leaf = zfs_scan_vdev_limit;
2908a3874b8bSToomas Soome 	uint64_t nr_leaves = dsl_scan_count_leaves(queue->q_vd);
2909a3874b8bSToomas Soome 
2910a3874b8bSToomas Soome 	ASSERT(queue->q_scn->scn_is_sorted);
2911a3874b8bSToomas Soome 
2912a3874b8bSToomas Soome 	list_create(&sio_list, sizeof (scan_io_t),
2913a3874b8bSToomas Soome 	    offsetof(scan_io_t, sio_nodes.sio_list_node));
2914a3874b8bSToomas Soome 	mutex_enter(q_lock);
2915a3874b8bSToomas Soome 
2916a3874b8bSToomas Soome 	/* calculate maximum in-flight bytes for this txg (min 1MB) */
2917a3874b8bSToomas Soome 	queue->q_maxinflight_bytes =
2918a3874b8bSToomas Soome 	    MAX(nr_leaves * bytes_per_leaf, 1ULL << 20);
2919a3874b8bSToomas Soome 
2920a3874b8bSToomas Soome 	/* reset per-queue scan statistics for this txg */
2921a3874b8bSToomas Soome 	queue->q_total_seg_size_this_txg = 0;
2922a3874b8bSToomas Soome 	queue->q_segs_this_txg = 0;
2923a3874b8bSToomas Soome 	queue->q_total_zio_size_this_txg = 0;
2924a3874b8bSToomas Soome 	queue->q_zios_this_txg = 0;
2925a3874b8bSToomas Soome 
2926a3874b8bSToomas Soome 	/* loop until we have run out of time or sios */
2927a3874b8bSToomas Soome 	while ((rs = (range_seg_t *)scan_io_queue_fetch_ext(queue)) != NULL) {
2928a3874b8bSToomas Soome 		uint64_t seg_start = 0, seg_end = 0;
2929a3874b8bSToomas Soome 		boolean_t more_left = B_TRUE;
2930a3874b8bSToomas Soome 
2931a3874b8bSToomas Soome 		ASSERT(list_is_empty(&sio_list));
2932a3874b8bSToomas Soome 
2933a3874b8bSToomas Soome 		/* loop while we still have sios left to process in this rs */
2934a3874b8bSToomas Soome 		while (more_left) {
2935a3874b8bSToomas Soome 			scan_io_t *first_sio, *last_sio;
2936a3874b8bSToomas Soome 
2937a3874b8bSToomas Soome 			/*
2938a3874b8bSToomas Soome 			 * We have selected which extent needs to be
2939a3874b8bSToomas Soome 			 * processed next. Gather up the corresponding sios.
2940a3874b8bSToomas Soome 			 */
2941a3874b8bSToomas Soome 			more_left = scan_io_queue_gather(queue, rs, &sio_list);
2942a3874b8bSToomas Soome 			ASSERT(!list_is_empty(&sio_list));
2943a3874b8bSToomas Soome 			first_sio = list_head(&sio_list);
2944a3874b8bSToomas Soome 			last_sio = list_tail(&sio_list);
2945a3874b8bSToomas Soome 
294612a8814cSTom Caputi 			seg_end = SIO_GET_END_OFFSET(last_sio);
2947a3874b8bSToomas Soome 			if (seg_start == 0)
294812a8814cSTom Caputi 				seg_start = SIO_GET_OFFSET(first_sio);
2949a3874b8bSToomas Soome 
2950a3874b8bSToomas Soome 			/*
2951a3874b8bSToomas Soome 			 * Issuing sios can take a long time so drop the
2952a3874b8bSToomas Soome 			 * queue lock. The sio queue won't be updated by
2953a3874b8bSToomas Soome 			 * other threads since we're in syncing context so
2954a3874b8bSToomas Soome 			 * we can be sure that our trees will remain exactly
2955a3874b8bSToomas Soome 			 * as we left them.
2956a3874b8bSToomas Soome 			 */
2957a3874b8bSToomas Soome 			mutex_exit(q_lock);
2958a3874b8bSToomas Soome 			suspended = scan_io_queue_issue(queue, &sio_list);
2959a3874b8bSToomas Soome 			mutex_enter(q_lock);
2960a3874b8bSToomas Soome 
2961a3874b8bSToomas Soome 			if (suspended)
2962a3874b8bSToomas Soome 				break;
2963a3874b8bSToomas Soome 		}
2964a3874b8bSToomas Soome 		/* update statistics for debugging purposes */
2965a3874b8bSToomas Soome 		scan_io_queues_update_seg_stats(queue, seg_start, seg_end);
2966a3874b8bSToomas Soome 
2967a3874b8bSToomas Soome 		if (suspended)
2968a3874b8bSToomas Soome 			break;
2969a3874b8bSToomas Soome 	}
2970a3874b8bSToomas Soome 
2971a3874b8bSToomas Soome 
2972a3874b8bSToomas Soome 	/*
2973a3874b8bSToomas Soome 	 * If we were suspended in the middle of processing,
2974a3874b8bSToomas Soome 	 * requeue any unfinished sios and exit.
2975a3874b8bSToomas Soome 	 */
2976a3874b8bSToomas Soome 	while ((sio = list_head(&sio_list)) != NULL) {
2977a3874b8bSToomas Soome 		list_remove(&sio_list, sio);
2978a3874b8bSToomas Soome 		scan_io_queue_insert_impl(queue, sio);
2979a3874b8bSToomas Soome 	}
2980a3874b8bSToomas Soome 
2981a3874b8bSToomas Soome 	mutex_exit(q_lock);
2982a3874b8bSToomas Soome 	list_destroy(&sio_list);
2983a3874b8bSToomas Soome }
2984a3874b8bSToomas Soome 
2985a3874b8bSToomas Soome /*
2986a3874b8bSToomas Soome  * Performs an emptying run on all scan queues in the pool. This just
2987a3874b8bSToomas Soome  * punches out one thread per top-level vdev, each of which processes
2988a3874b8bSToomas Soome  * only that vdev's scan queue. We can parallelize the I/O here because
2989a3874b8bSToomas Soome  * we know that each queue's io's only affect its own top-level vdev.
2990a3874b8bSToomas Soome  *
2991a3874b8bSToomas Soome  * This function waits for the queue runs to complete, and must be
2992a3874b8bSToomas Soome  * called from dsl_scan_sync (or in general, syncing context).
2993a3874b8bSToomas Soome  */
2994a3874b8bSToomas Soome static void
2995a3874b8bSToomas Soome scan_io_queues_run(dsl_scan_t *scn)
2996a3874b8bSToomas Soome {
2997a3874b8bSToomas Soome 	spa_t *spa = scn->scn_dp->dp_spa;
2998a3874b8bSToomas Soome 
2999a3874b8bSToomas Soome 	ASSERT(scn->scn_is_sorted);
3000a3874b8bSToomas Soome 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3001a3874b8bSToomas Soome 
3002a3874b8bSToomas Soome 	if (scn->scn_bytes_pending == 0)
3003a3874b8bSToomas Soome 		return;
3004a3874b8bSToomas Soome 
3005a3874b8bSToomas Soome 	if (scn->scn_taskq == NULL) {
3006a3874b8bSToomas Soome 		char *tq_name = kmem_zalloc(ZFS_MAX_DATASET_NAME_LEN + 16,
3007a3874b8bSToomas Soome 		    KM_SLEEP);
3008a3874b8bSToomas Soome 		int nthreads = spa->spa_root_vdev->vdev_children;
3009a3874b8bSToomas Soome 
3010a3874b8bSToomas Soome 		/*
3011a3874b8bSToomas Soome 		 * We need to make this taskq *always* execute as many
3012a3874b8bSToomas Soome 		 * threads in parallel as we have top-level vdevs and no
3013a3874b8bSToomas Soome 		 * less, otherwise strange serialization of the calls to
3014a3874b8bSToomas Soome 		 * scan_io_queues_run_one can occur during spa_sync runs
3015a3874b8bSToomas Soome 		 * and that significantly impacts performance.
3016a3874b8bSToomas Soome 		 */
3017a3874b8bSToomas Soome 		(void) snprintf(tq_name, ZFS_MAX_DATASET_NAME_LEN + 16,
3018a3874b8bSToomas Soome 		    "dsl_scan_tq_%s", spa->spa_name);
3019a3874b8bSToomas Soome 		scn->scn_taskq = taskq_create(tq_name, nthreads, minclsyspri,
3020a3874b8bSToomas Soome 		    nthreads, nthreads, TASKQ_PREPOPULATE);
3021a3874b8bSToomas Soome 		kmem_free(tq_name, ZFS_MAX_DATASET_NAME_LEN + 16);
3022a3874b8bSToomas Soome 	}
3023a3874b8bSToomas Soome 
3024a3874b8bSToomas Soome 	for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
3025a3874b8bSToomas Soome 		vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
3026a3874b8bSToomas Soome 
3027a3874b8bSToomas Soome 		mutex_enter(&vd->vdev_scan_io_queue_lock);
3028a3874b8bSToomas Soome 		if (vd->vdev_scan_io_queue != NULL) {
3029a3874b8bSToomas Soome 			VERIFY(taskq_dispatch(scn->scn_taskq,
3030a3874b8bSToomas Soome 			    scan_io_queues_run_one, vd->vdev_scan_io_queue,
3031a3874b8bSToomas Soome 			    TQ_SLEEP) != TASKQID_INVALID);
3032a3874b8bSToomas Soome 		}
3033a3874b8bSToomas Soome 		mutex_exit(&vd->vdev_scan_io_queue_lock);
3034a3874b8bSToomas Soome 	}
3035a3874b8bSToomas Soome 
3036a3874b8bSToomas Soome 	/*
3037a3874b8bSToomas Soome 	 * Wait for the queues to finish issuing thir IOs for this run
3038a3874b8bSToomas Soome 	 * before we return. There may still be IOs in flight at this
3039a3874b8bSToomas Soome 	 * point.
3040a3874b8bSToomas Soome 	 */
3041a3874b8bSToomas Soome 	taskq_wait(scn->scn_taskq);
30423f9d6ad7SLin Ling }
30433f9d6ad7SLin Ling 
3044ad135b5dSChristopher Siden static boolean_t
30455cabbc6bSPrashanth Sreenivasa dsl_scan_async_block_should_pause(dsl_scan_t *scn)
3046cde58dbcSMatthew Ahrens {
3047cde58dbcSMatthew Ahrens 	uint64_t elapsed_nanosecs;
3048cde58dbcSMatthew Ahrens 
30498b36997aSMatthew Ahrens 	if (zfs_recover)
30508b36997aSMatthew Ahrens 		return (B_FALSE);
30518b36997aSMatthew Ahrens 
30525cabbc6bSPrashanth Sreenivasa 	if (scn->scn_visited_this_txg >= zfs_async_block_max_blocks)
3053af3465daSMax Grossman 		return (B_TRUE);
3054af3465daSMax Grossman 
3055cde58dbcSMatthew Ahrens 	elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
3056ad135b5dSChristopher Siden 	return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
30575cabbc6bSPrashanth Sreenivasa 	    (NSEC2MSEC(elapsed_nanosecs) > scn->scn_async_block_min_time_ms &&
3058cde58dbcSMatthew Ahrens 	    txg_sync_waiting(scn->scn_dp)) ||
3059ad135b5dSChristopher Siden 	    spa_shutting_down(scn->scn_dp->dp_spa));
3060ad135b5dSChristopher Siden }
3061ad135b5dSChristopher Siden 
3062ad135b5dSChristopher Siden static int
3063ad135b5dSChristopher Siden dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3064ad135b5dSChristopher Siden {
3065ad135b5dSChristopher Siden 	dsl_scan_t *scn = arg;
3066ad135b5dSChristopher Siden 
3067ad135b5dSChristopher Siden 	if (!scn->scn_is_bptree ||
3068ad135b5dSChristopher Siden 	    (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
30695cabbc6bSPrashanth Sreenivasa 		if (dsl_scan_async_block_should_pause(scn))
3070be6fd75aSMatthew Ahrens 			return (SET_ERROR(ERESTART));
3071ad135b5dSChristopher Siden 	}
3072cde58dbcSMatthew Ahrens 
3073cde58dbcSMatthew Ahrens 	zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
3074cde58dbcSMatthew Ahrens 	    dmu_tx_get_txg(tx), bp, 0));
3075cde58dbcSMatthew Ahrens 	dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
3076cde58dbcSMatthew Ahrens 	    -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
3077cde58dbcSMatthew Ahrens 	    -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
3078cde58dbcSMatthew Ahrens 	scn->scn_visited_this_txg++;
3079cde58dbcSMatthew Ahrens 	return (0);
3080cde58dbcSMatthew Ahrens }
3081cde58dbcSMatthew Ahrens 
3082a3874b8bSToomas Soome static void
3083a3874b8bSToomas Soome dsl_scan_update_stats(dsl_scan_t *scn)
3084a3874b8bSToomas Soome {
3085a3874b8bSToomas Soome 	spa_t *spa = scn->scn_dp->dp_spa;
3086a3874b8bSToomas Soome 	uint64_t i;
3087a3874b8bSToomas Soome 	uint64_t seg_size_total = 0, zio_size_total = 0;
3088a3874b8bSToomas Soome 	uint64_t seg_count_total = 0, zio_count_total = 0;
3089a3874b8bSToomas Soome 
3090a3874b8bSToomas Soome 	for (i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
3091a3874b8bSToomas Soome 		vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
3092a3874b8bSToomas Soome 		dsl_scan_io_queue_t *queue = vd->vdev_scan_io_queue;
3093a3874b8bSToomas Soome 
3094a3874b8bSToomas Soome 		if (queue == NULL)
3095a3874b8bSToomas Soome 			continue;
3096a3874b8bSToomas Soome 
3097a3874b8bSToomas Soome 		seg_size_total += queue->q_total_seg_size_this_txg;
3098a3874b8bSToomas Soome 		zio_size_total += queue->q_total_zio_size_this_txg;
3099a3874b8bSToomas Soome 		seg_count_total += queue->q_segs_this_txg;
3100a3874b8bSToomas Soome 		zio_count_total += queue->q_zios_this_txg;
3101a3874b8bSToomas Soome 	}
3102a3874b8bSToomas Soome 
3103a3874b8bSToomas Soome 	if (seg_count_total == 0 || zio_count_total == 0) {
3104a3874b8bSToomas Soome 		scn->scn_avg_seg_size_this_txg = 0;
3105a3874b8bSToomas Soome 		scn->scn_avg_zio_size_this_txg = 0;
3106a3874b8bSToomas Soome 		scn->scn_segs_this_txg = 0;
3107a3874b8bSToomas Soome 		scn->scn_zios_this_txg = 0;
3108a3874b8bSToomas Soome 		return;
3109a3874b8bSToomas Soome 	}
3110a3874b8bSToomas Soome 
3111a3874b8bSToomas Soome 	scn->scn_avg_seg_size_this_txg = seg_size_total / seg_count_total;
3112a3874b8bSToomas Soome 	scn->scn_avg_zio_size_this_txg = zio_size_total / zio_count_total;
3113a3874b8bSToomas Soome 	scn->scn_segs_this_txg = seg_count_total;
3114a3874b8bSToomas Soome 	scn->scn_zios_this_txg = zio_count_total;
3115a3874b8bSToomas Soome }
3116a3874b8bSToomas Soome 
31175cabbc6bSPrashanth Sreenivasa static int
31185cabbc6bSPrashanth Sreenivasa dsl_scan_obsolete_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
31195cabbc6bSPrashanth Sreenivasa {
31205cabbc6bSPrashanth Sreenivasa 	dsl_scan_t *scn = arg;
31215cabbc6bSPrashanth Sreenivasa 	const dva_t *dva = &bp->blk_dva[0];
31225cabbc6bSPrashanth Sreenivasa 
31235cabbc6bSPrashanth Sreenivasa 	if (dsl_scan_async_block_should_pause(scn))
31245cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ERESTART));
31255cabbc6bSPrashanth Sreenivasa 
31265cabbc6bSPrashanth Sreenivasa 	spa_vdev_indirect_mark_obsolete(scn->scn_dp->dp_spa,
31275cabbc6bSPrashanth Sreenivasa 	    DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva),
31285cabbc6bSPrashanth Sreenivasa 	    DVA_GET_ASIZE(dva), tx);
31295cabbc6bSPrashanth Sreenivasa 	scn->scn_visited_this_txg++;
31305cabbc6bSPrashanth Sreenivasa 	return (0);
31315cabbc6bSPrashanth Sreenivasa }
31325cabbc6bSPrashanth Sreenivasa 
3133cde58dbcSMatthew Ahrens boolean_t
3134cde58dbcSMatthew Ahrens dsl_scan_active(dsl_scan_t *scn)
3135cde58dbcSMatthew Ahrens {
3136cde58dbcSMatthew Ahrens 	spa_t *spa = scn->scn_dp->dp_spa;
3137cde58dbcSMatthew Ahrens 	uint64_t used = 0, comp, uncomp;
3138cde58dbcSMatthew Ahrens 
3139cde58dbcSMatthew Ahrens 	if (spa->spa_load_state != SPA_LOAD_NONE)
3140cde58dbcSMatthew Ahrens 		return (B_FALSE);
3141cde58dbcSMatthew Ahrens 	if (spa_shutting_down(spa))
3142cde58dbcSMatthew Ahrens 		return (B_FALSE);
3143a3874b8bSToomas Soome 	if ((dsl_scan_is_running(scn) && !dsl_scan_is_paused_scrub(scn)) ||
31447fd05ac4SMatthew Ahrens 	    (scn->scn_async_destroying && !scn->scn_async_stalled))
3145cde58dbcSMatthew Ahrens 		return (B_TRUE);
3146cde58dbcSMatthew Ahrens 
3147cde58dbcSMatthew Ahrens 	if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
3148cde58dbcSMatthew Ahrens 		(void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
3149cde58dbcSMatthew Ahrens 		    &used, &comp, &uncomp);
3150cde58dbcSMatthew Ahrens 	}
3151cde58dbcSMatthew Ahrens 	return (used != 0);
3152cde58dbcSMatthew Ahrens }
3153cde58dbcSMatthew Ahrens 
3154e4c795beSTom Caputi static boolean_t
3155e4c795beSTom Caputi dsl_scan_check_deferred(vdev_t *vd)
3156e4c795beSTom Caputi {
3157e4c795beSTom Caputi 	boolean_t need_resilver = B_FALSE;
3158e4c795beSTom Caputi 
3159e4c795beSTom Caputi 	for (int c = 0; c < vd->vdev_children; c++) {
3160e4c795beSTom Caputi 		need_resilver |=
3161e4c795beSTom Caputi 		    dsl_scan_check_deferred(vd->vdev_child[c]);
3162e4c795beSTom Caputi 	}
3163e4c795beSTom Caputi 
3164e4c795beSTom Caputi 	if (!vdev_is_concrete(vd) || vd->vdev_aux ||
3165e4c795beSTom Caputi 	    !vd->vdev_ops->vdev_op_leaf)
3166e4c795beSTom Caputi 		return (need_resilver);
3167e4c795beSTom Caputi 
3168e4c795beSTom Caputi 	if (!vd->vdev_resilver_deferred)
3169e4c795beSTom Caputi 		need_resilver = B_TRUE;
3170e4c795beSTom Caputi 
3171e4c795beSTom Caputi 	return (need_resilver);
3172e4c795beSTom Caputi }
3173e4c795beSTom Caputi 
3174a3874b8bSToomas Soome static boolean_t
3175a3874b8bSToomas Soome dsl_scan_need_resilver(spa_t *spa, const dva_t *dva, size_t psize,
3176a3874b8bSToomas Soome     uint64_t phys_birth)
3177a3874b8bSToomas Soome {
3178a3874b8bSToomas Soome 	vdev_t *vd;
3179a3874b8bSToomas Soome 
3180a3874b8bSToomas Soome 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
3181a3874b8bSToomas Soome 
3182a3874b8bSToomas Soome 	if (vd->vdev_ops == &vdev_indirect_ops) {
3183a3874b8bSToomas Soome 		/*
3184a3874b8bSToomas Soome 		 * The indirect vdev can point to multiple
3185a3874b8bSToomas Soome 		 * vdevs.  For simplicity, always create
3186a3874b8bSToomas Soome 		 * the resilver zio_t. zio_vdev_io_start()
3187a3874b8bSToomas Soome 		 * will bypass the child resilver i/o's if
3188a3874b8bSToomas Soome 		 * they are on vdevs that don't have DTL's.
3189a3874b8bSToomas Soome 		 */
3190a3874b8bSToomas Soome 		return (B_TRUE);
3191a3874b8bSToomas Soome 	}
3192a3874b8bSToomas Soome 
3193a3874b8bSToomas Soome 	if (DVA_GET_GANG(dva)) {
3194a3874b8bSToomas Soome 		/*
3195a3874b8bSToomas Soome 		 * Gang members may be spread across multiple
3196a3874b8bSToomas Soome 		 * vdevs, so the best estimate we have is the
3197a3874b8bSToomas Soome 		 * scrub range, which has already been checked.
3198a3874b8bSToomas Soome 		 * XXX -- it would be better to change our
3199a3874b8bSToomas Soome 		 * allocation policy to ensure that all
3200a3874b8bSToomas Soome 		 * gang members reside on the same vdev.
3201a3874b8bSToomas Soome 		 */
3202a3874b8bSToomas Soome 		return (B_TRUE);
3203a3874b8bSToomas Soome 	}
3204a3874b8bSToomas Soome 
3205a3874b8bSToomas Soome 	/*
3206a3874b8bSToomas Soome 	 * Check if the txg falls within the range which must be
3207a3874b8bSToomas Soome 	 * resilvered.  DVAs outside this range can always be skipped.
3208a3874b8bSToomas Soome 	 */
3209a3874b8bSToomas Soome 	if (!vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1))
3210a3874b8bSToomas Soome 		return (B_FALSE);
3211a3874b8bSToomas Soome 
3212a3874b8bSToomas Soome 	/*
3213a3874b8bSToomas Soome 	 * Check if the top-level vdev must resilver this offset.
3214a3874b8bSToomas Soome 	 * When the offset does not intersect with a dirty leaf DTL
3215a3874b8bSToomas Soome 	 * then it may be possible to skip the resilver IO.  The psize
3216a3874b8bSToomas Soome 	 * is provided instead of asize to simplify the check for RAIDZ.
3217a3874b8bSToomas Soome 	 */
3218a3874b8bSToomas Soome 	if (!vdev_dtl_need_resilver(vd, DVA_GET_OFFSET(dva), psize))
3219a3874b8bSToomas Soome 		return (B_FALSE);
3220a3874b8bSToomas Soome 
3221e4c795beSTom Caputi 	/*
3222e4c795beSTom Caputi 	 * Check that this top-level vdev has a device under it which
3223e4c795beSTom Caputi 	 * is resilvering and is not deferred.
3224e4c795beSTom Caputi 	 */
3225e4c795beSTom Caputi 	if (!dsl_scan_check_deferred(vd))
3226e4c795beSTom Caputi 		return (B_FALSE);
3227e4c795beSTom Caputi 
3228a3874b8bSToomas Soome 	return (B_TRUE);
3229a3874b8bSToomas Soome }
3230a3874b8bSToomas Soome 
323186714001SSerapheim Dimitropoulos static int
323286714001SSerapheim Dimitropoulos dsl_process_async_destroys(dsl_pool_t *dp, dmu_tx_t *tx)
32333f9d6ad7SLin Ling {
3234a3874b8bSToomas Soome 	int err = 0;
32353f9d6ad7SLin Ling 	dsl_scan_t *scn = dp->dp_scan;
32363f9d6ad7SLin Ling 	spa_t *spa = dp->dp_spa;
32373f9d6ad7SLin Ling 
323886714001SSerapheim Dimitropoulos 	if (spa_suspend_async_destroy(spa))
323986714001SSerapheim Dimitropoulos 		return (0);
32403f9d6ad7SLin Ling 
3241139510fbSGeorge Wilson 	if (zfs_free_bpobj_enabled &&
3242a3874b8bSToomas Soome 	    spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3243ad135b5dSChristopher Siden 		scn->scn_is_bptree = B_FALSE;
32445cabbc6bSPrashanth Sreenivasa 		scn->scn_async_block_min_time_ms = zfs_free_min_time_ms;
3245a3874b8bSToomas Soome 		scn->scn_zio_root = zio_root(spa, NULL,
3246cde58dbcSMatthew Ahrens 		    NULL, ZIO_FLAG_MUSTSUCCEED);
3247cde58dbcSMatthew Ahrens 		err = bpobj_iterate(&dp->dp_free_bpobj,
3248ad135b5dSChristopher Siden 		    dsl_scan_free_block_cb, scn, tx);
3249a3874b8bSToomas Soome 		VERIFY0(zio_wait(scn->scn_zio_root));
3250a3874b8bSToomas Soome 		scn->scn_zio_root = NULL;
3251ad135b5dSChristopher Siden 
32527fd05ac4SMatthew Ahrens 		if (err != 0 && err != ERESTART)
32537fd05ac4SMatthew Ahrens 			zfs_panic_recover("error %u from bpobj_iterate()", err);
32547fd05ac4SMatthew Ahrens 	}
32557fd05ac4SMatthew Ahrens 
32567fd05ac4SMatthew Ahrens 	if (err == 0 && spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
32577fd05ac4SMatthew Ahrens 		ASSERT(scn->scn_async_destroying);
32587fd05ac4SMatthew Ahrens 		scn->scn_is_bptree = B_TRUE;
3259a3874b8bSToomas Soome 		scn->scn_zio_root = zio_root(spa, NULL,
32607fd05ac4SMatthew Ahrens 		    NULL, ZIO_FLAG_MUSTSUCCEED);
32617fd05ac4SMatthew Ahrens 		err = bptree_iterate(dp->dp_meta_objset,
32627fd05ac4SMatthew Ahrens 		    dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb, scn, tx);
32637fd05ac4SMatthew Ahrens 		VERIFY0(zio_wait(scn->scn_zio_root));
3264a3874b8bSToomas Soome 		scn->scn_zio_root = NULL;
32657fd05ac4SMatthew Ahrens 
32667fd05ac4SMatthew Ahrens 		if (err == EIO || err == ECKSUM) {
32677fd05ac4SMatthew Ahrens 			err = 0;
32687fd05ac4SMatthew Ahrens 		} else if (err != 0 && err != ERESTART) {
32697fd05ac4SMatthew Ahrens 			zfs_panic_recover("error %u from "
32707fd05ac4SMatthew Ahrens 			    "traverse_dataset_destroyed()", err);
3271ad135b5dSChristopher Siden 		}
32727fd05ac4SMatthew Ahrens 
32737fd05ac4SMatthew Ahrens 		if (bptree_is_empty(dp->dp_meta_objset, dp->dp_bptree_obj)) {
32747fd05ac4SMatthew Ahrens 			/* finished; deactivate async destroy feature */
32757fd05ac4SMatthew Ahrens 			spa_feature_decr(spa, SPA_FEATURE_ASYNC_DESTROY, tx);
32767fd05ac4SMatthew Ahrens 			ASSERT(!spa_feature_is_active(spa,
32777fd05ac4SMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY));
32787fd05ac4SMatthew Ahrens 			VERIFY0(zap_remove(dp->dp_meta_objset,
32797fd05ac4SMatthew Ahrens 			    DMU_POOL_DIRECTORY_OBJECT,
32807fd05ac4SMatthew Ahrens 			    DMU_POOL_BPTREE_OBJ, tx));
32817fd05ac4SMatthew Ahrens 			VERIFY0(bptree_free(dp->dp_meta_objset,
32827fd05ac4SMatthew Ahrens 			    dp->dp_bptree_obj, tx));
32837fd05ac4SMatthew Ahrens 			dp->dp_bptree_obj = 0;
32847fd05ac4SMatthew Ahrens 			scn->scn_async_destroying = B_FALSE;
3285231aab85SMatthew Ahrens 			scn->scn_async_stalled = B_FALSE;
3286231aab85SMatthew Ahrens 		} else {
3287231aab85SMatthew Ahrens 			/*
3288231aab85SMatthew Ahrens 			 * If we didn't make progress, mark the async
3289231aab85SMatthew Ahrens 			 * destroy as stalled, so that we will not initiate
3290231aab85SMatthew Ahrens 			 * a spa_sync() on its behalf.  Note that we only
3291231aab85SMatthew Ahrens 			 * check this if we are not finished, because if the
3292231aab85SMatthew Ahrens 			 * bptree had no blocks for us to visit, we can
3293231aab85SMatthew Ahrens 			 * finish without "making progress".
3294231aab85SMatthew Ahrens 			 */
3295231aab85SMatthew Ahrens 			scn->scn_async_stalled =
3296231aab85SMatthew Ahrens 			    (scn->scn_visited_this_txg == 0);
3297cde58dbcSMatthew Ahrens 		}
32987fd05ac4SMatthew Ahrens 	}
32997fd05ac4SMatthew Ahrens 	if (scn->scn_visited_this_txg) {
33007fd05ac4SMatthew Ahrens 		zfs_dbgmsg("freed %llu blocks in %llums from "
3301a3874b8bSToomas Soome 		    "free_bpobj/bptree txg %llu; err=%d",
33027fd05ac4SMatthew Ahrens 		    (longlong_t)scn->scn_visited_this_txg,
33037fd05ac4SMatthew Ahrens 		    (longlong_t)
33047fd05ac4SMatthew Ahrens 		    NSEC2MSEC(gethrtime() - scn->scn_sync_start_time),
33057fd05ac4SMatthew Ahrens 		    (longlong_t)tx->tx_txg, err);
33067fd05ac4SMatthew Ahrens 		scn->scn_visited_this_txg = 0;
33077fd05ac4SMatthew Ahrens 
33087fd05ac4SMatthew Ahrens 		/*
33097fd05ac4SMatthew Ahrens 		 * Write out changes to the DDT that may be required as a
33107fd05ac4SMatthew Ahrens 		 * result of the blocks freed.  This ensures that the DDT
33117fd05ac4SMatthew Ahrens 		 * is clean when a scrub/resilver runs.
33127fd05ac4SMatthew Ahrens 		 */
33137fd05ac4SMatthew Ahrens 		ddt_sync(spa, tx->tx_txg);
33147fd05ac4SMatthew Ahrens 	}
33157fd05ac4SMatthew Ahrens 	if (err != 0)
331686714001SSerapheim Dimitropoulos 		return (err);
33178c04a1faSGary Mills 	if (dp->dp_free_dir != NULL && !scn->scn_async_destroying &&
33188c04a1faSGary Mills 	    zfs_free_leak_on_eio &&
3319c1379625SJustin T. Gibbs 	    (dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes != 0 ||
3320c1379625SJustin T. Gibbs 	    dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes != 0 ||
3321c1379625SJustin T. Gibbs 	    dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes != 0)) {
33227fd05ac4SMatthew Ahrens 		/*
33237fd05ac4SMatthew Ahrens 		 * We have finished background destroying, but there is still
33247fd05ac4SMatthew Ahrens 		 * some space left in the dp_free_dir. Transfer this leaked
33257fd05ac4SMatthew Ahrens 		 * space to the dp_leak_dir.
33267fd05ac4SMatthew Ahrens 		 */
33277fd05ac4SMatthew Ahrens 		if (dp->dp_leak_dir == NULL) {
33287fd05ac4SMatthew Ahrens 			rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
33297fd05ac4SMatthew Ahrens 			(void) dsl_dir_create_sync(dp, dp->dp_root_dir,
33307fd05ac4SMatthew Ahrens 			    LEAK_DIR_NAME, tx);
33317fd05ac4SMatthew Ahrens 			VERIFY0(dsl_pool_open_special_dir(dp,
33327fd05ac4SMatthew Ahrens 			    LEAK_DIR_NAME, &dp->dp_leak_dir));
33337fd05ac4SMatthew Ahrens 			rrw_exit(&dp->dp_config_rwlock, FTAG);
33347fd05ac4SMatthew Ahrens 		}
33357fd05ac4SMatthew Ahrens 		dsl_dir_diduse_space(dp->dp_leak_dir, DD_USED_HEAD,
3336c1379625SJustin T. Gibbs 		    dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes,
3337c1379625SJustin T. Gibbs 		    dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes,
3338c1379625SJustin T. Gibbs 		    dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx);
33397fd05ac4SMatthew Ahrens 		dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
3340c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes,
3341c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes,
3342c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx);
33437fd05ac4SMatthew Ahrens 	}
33445cabbc6bSPrashanth Sreenivasa 
33458c04a1faSGary Mills 	if (dp->dp_free_dir != NULL && !scn->scn_async_destroying) {
33465d7b4d43SMatthew Ahrens 		/* finished; verify that space accounting went to zero */
3347c1379625SJustin T. Gibbs 		ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes);
3348c1379625SJustin T. Gibbs 		ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes);
3349c1379625SJustin T. Gibbs 		ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes);
3350cde58dbcSMatthew Ahrens 	}
3351cde58dbcSMatthew Ahrens 
33525cabbc6bSPrashanth Sreenivasa 	EQUIV(bpobj_is_open(&dp->dp_obsolete_bpobj),
33535cabbc6bSPrashanth Sreenivasa 	    0 == zap_contains(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
33545cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_OBSOLETE_BPOBJ));
33555cabbc6bSPrashanth Sreenivasa 	if (err == 0 && bpobj_is_open(&dp->dp_obsolete_bpobj)) {
33565cabbc6bSPrashanth Sreenivasa 		ASSERT(spa_feature_is_active(dp->dp_spa,
33575cabbc6bSPrashanth Sreenivasa 		    SPA_FEATURE_OBSOLETE_COUNTS));
33585cabbc6bSPrashanth Sreenivasa 
33595cabbc6bSPrashanth Sreenivasa 		scn->scn_is_bptree = B_FALSE;
33605cabbc6bSPrashanth Sreenivasa 		scn->scn_async_block_min_time_ms = zfs_obsolete_min_time_ms;
33615cabbc6bSPrashanth Sreenivasa 		err = bpobj_iterate(&dp->dp_obsolete_bpobj,
33625cabbc6bSPrashanth Sreenivasa 		    dsl_scan_obsolete_block_cb, scn, tx);
33635cabbc6bSPrashanth Sreenivasa 		if (err != 0 && err != ERESTART)
33645cabbc6bSPrashanth Sreenivasa 			zfs_panic_recover("error %u from bpobj_iterate()", err);
33655cabbc6bSPrashanth Sreenivasa 
33665cabbc6bSPrashanth Sreenivasa 		if (bpobj_is_empty(&dp->dp_obsolete_bpobj))
33675cabbc6bSPrashanth Sreenivasa 			dsl_pool_destroy_obsolete_bpobj(dp, tx);
33685cabbc6bSPrashanth Sreenivasa 	}
33695cabbc6bSPrashanth Sreenivasa 
337086714001SSerapheim Dimitropoulos 	return (0);
337186714001SSerapheim Dimitropoulos }
337286714001SSerapheim Dimitropoulos 
3373a3874b8bSToomas Soome /*
3374a3874b8bSToomas Soome  * This is the primary entry point for scans that is called from syncing
3375a3874b8bSToomas Soome  * context. Scans must happen entirely during syncing context so that we
3376a3874b8bSToomas Soome  * cna guarantee that blocks we are currently scanning will not change out
3377a3874b8bSToomas Soome  * from under us. While a scan is active, this funciton controls how quickly
3378a3874b8bSToomas Soome  * transaction groups proceed, instead of the normal handling provided by
3379a3874b8bSToomas Soome  * txg_sync_thread().
3380a3874b8bSToomas Soome  */
338186714001SSerapheim Dimitropoulos void
338286714001SSerapheim Dimitropoulos dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
338386714001SSerapheim Dimitropoulos {
338486714001SSerapheim Dimitropoulos 	dsl_scan_t *scn = dp->dp_scan;
338586714001SSerapheim Dimitropoulos 	spa_t *spa = dp->dp_spa;
338686714001SSerapheim Dimitropoulos 	int err = 0;
3387a3874b8bSToomas Soome 	state_sync_type_t sync_type = SYNC_OPTIONAL;
338886714001SSerapheim Dimitropoulos 
3389e4c795beSTom Caputi 	if (spa->spa_resilver_deferred &&
3390e4c795beSTom Caputi 	    !spa_feature_is_active(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))
3391e4c795beSTom Caputi 		spa_feature_incr(spa, SPA_FEATURE_RESILVER_DEFER, tx);
3392e4c795beSTom Caputi 
339386714001SSerapheim Dimitropoulos 	/*
339486714001SSerapheim Dimitropoulos 	 * Check for scn_restart_txg before checking spa_load_state, so
339586714001SSerapheim Dimitropoulos 	 * that we can restart an old-style scan while the pool is being
3396e4c795beSTom Caputi 	 * imported (see dsl_scan_init). We also restart scans if there
3397e4c795beSTom Caputi 	 * is a deferred resilver and the user has manually disabled
3398e4c795beSTom Caputi 	 * deferred resilvers via the tunable.
339986714001SSerapheim Dimitropoulos 	 */
3400e4c795beSTom Caputi 	if (dsl_scan_restarting(scn, tx) ||
3401e4c795beSTom Caputi 	    (spa->spa_resilver_deferred && zfs_resilver_disable_defer)) {
340286714001SSerapheim Dimitropoulos 		pool_scan_func_t func = POOL_SCAN_SCRUB;
340386714001SSerapheim Dimitropoulos 		dsl_scan_done(scn, B_FALSE, tx);
340486714001SSerapheim Dimitropoulos 		if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
340586714001SSerapheim Dimitropoulos 			func = POOL_SCAN_RESILVER;
340686714001SSerapheim Dimitropoulos 		zfs_dbgmsg("restarting scan func=%u txg=%llu",
3407a3874b8bSToomas Soome 		    func, (longlong_t)tx->tx_txg);
340886714001SSerapheim Dimitropoulos 		dsl_scan_setup_sync(&func, tx);
340986714001SSerapheim Dimitropoulos 	}
341086714001SSerapheim Dimitropoulos 
341186714001SSerapheim Dimitropoulos 	/*
341286714001SSerapheim Dimitropoulos 	 * Only process scans in sync pass 1.
341386714001SSerapheim Dimitropoulos 	 */
341486714001SSerapheim Dimitropoulos 	if (spa_sync_pass(dp->dp_spa) > 1)
341586714001SSerapheim Dimitropoulos 		return;
341686714001SSerapheim Dimitropoulos 
341786714001SSerapheim Dimitropoulos 	/*
341886714001SSerapheim Dimitropoulos 	 * If the spa is shutting down, then stop scanning. This will
341986714001SSerapheim Dimitropoulos 	 * ensure that the scan does not dirty any new data during the
342086714001SSerapheim Dimitropoulos 	 * shutdown phase.
342186714001SSerapheim Dimitropoulos 	 */
342286714001SSerapheim Dimitropoulos 	if (spa_shutting_down(spa))
342386714001SSerapheim Dimitropoulos 		return;
342486714001SSerapheim Dimitropoulos 
342586714001SSerapheim Dimitropoulos 	/*
342686714001SSerapheim Dimitropoulos 	 * If the scan is inactive due to a stalled async destroy, try again.
342786714001SSerapheim Dimitropoulos 	 */
342886714001SSerapheim Dimitropoulos 	if (!scn->scn_async_stalled && !dsl_scan_active(scn))
342986714001SSerapheim Dimitropoulos 		return;
343086714001SSerapheim Dimitropoulos 
3431a3874b8bSToomas Soome 	/* reset scan statistics */
343286714001SSerapheim Dimitropoulos 	scn->scn_visited_this_txg = 0;
3433a3874b8bSToomas Soome 	scn->scn_holes_this_txg = 0;
3434a3874b8bSToomas Soome 	scn->scn_lt_min_this_txg = 0;
3435a3874b8bSToomas Soome 	scn->scn_gt_max_this_txg = 0;
3436a3874b8bSToomas Soome 	scn->scn_ddt_contained_this_txg = 0;
3437a3874b8bSToomas Soome 	scn->scn_objsets_visited_this_txg = 0;
3438a3874b8bSToomas Soome 	scn->scn_avg_seg_size_this_txg = 0;
3439a3874b8bSToomas Soome 	scn->scn_segs_this_txg = 0;
3440a3874b8bSToomas Soome 	scn->scn_avg_zio_size_this_txg = 0;
3441a3874b8bSToomas Soome 	scn->scn_zios_this_txg = 0;
344286714001SSerapheim Dimitropoulos 	scn->scn_suspending = B_FALSE;
344386714001SSerapheim Dimitropoulos 	scn->scn_sync_start_time = gethrtime();
344486714001SSerapheim Dimitropoulos 	spa->spa_scrub_active = B_TRUE;
344586714001SSerapheim Dimitropoulos 
344686714001SSerapheim Dimitropoulos 	/*
344786714001SSerapheim Dimitropoulos 	 * First process the async destroys.  If we pause, don't do
344886714001SSerapheim Dimitropoulos 	 * any scrubbing or resilvering.  This ensures that there are no
344986714001SSerapheim Dimitropoulos 	 * async destroys while we are scanning, so the scan code doesn't
345086714001SSerapheim Dimitropoulos 	 * have to worry about traversing it.  It is also faster to free the
345186714001SSerapheim Dimitropoulos 	 * blocks than to scrub them.
345286714001SSerapheim Dimitropoulos 	 */
345386714001SSerapheim Dimitropoulos 	err = dsl_process_async_destroys(dp, tx);
345486714001SSerapheim Dimitropoulos 	if (err != 0)
345586714001SSerapheim Dimitropoulos 		return;
345686714001SSerapheim Dimitropoulos 
3457a3874b8bSToomas Soome 	if (!dsl_scan_is_running(scn) || dsl_scan_is_paused_scrub(scn))
3458cde58dbcSMatthew Ahrens 		return;
3459cde58dbcSMatthew Ahrens 
3460a3874b8bSToomas Soome 	/*
3461a3874b8bSToomas Soome 	 * Wait a few txgs after importing to begin scanning so that
3462a3874b8bSToomas Soome 	 * we can get the pool imported quickly.
3463a3874b8bSToomas Soome 	 */
3464a3874b8bSToomas Soome 	if (spa->spa_syncing_txg < spa->spa_first_txg + SCAN_IMPORT_WAIT_TXGS)
3465b4952e17SGeorge Wilson 		return;
3466b4952e17SGeorge Wilson 
3467e4c795beSTom Caputi 	/*
3468e4c795beSTom Caputi 	 * zfs_scan_suspend_progress can be set to disable scan progress.
3469e4c795beSTom Caputi 	 * We don't want to spin the txg_sync thread, so we add a delay
3470e4c795beSTom Caputi 	 * here to simulate the time spent doing a scan. This is mostly
3471e4c795beSTom Caputi 	 * useful for testing and debugging.
3472e4c795beSTom Caputi 	 */
3473e4c795beSTom Caputi 	if (zfs_scan_suspend_progress) {
3474e4c795beSTom Caputi 		uint64_t scan_time_ns = gethrtime() - scn->scn_sync_start_time;
3475e4c795beSTom Caputi 		int mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
3476e4c795beSTom Caputi 		    zfs_resilver_min_time_ms : zfs_scrub_min_time_ms;
3477e4c795beSTom Caputi 
3478e4c795beSTom Caputi 		while (zfs_scan_suspend_progress &&
3479e4c795beSTom Caputi 		    !txg_sync_waiting(scn->scn_dp) &&
3480e4c795beSTom Caputi 		    !spa_shutting_down(scn->scn_dp->dp_spa) &&
3481e4c795beSTom Caputi 		    NSEC2MSEC(scan_time_ns) < mintime) {
3482e4c795beSTom Caputi 			delay(hz);
3483e4c795beSTom Caputi 			scan_time_ns = gethrtime() - scn->scn_sync_start_time;
3484e4c795beSTom Caputi 		}
3485e4c795beSTom Caputi 		return;
3486e4c795beSTom Caputi 	}
3487e4c795beSTom Caputi 
3488a3874b8bSToomas Soome 	/*
3489a3874b8bSToomas Soome 	 * It is possible to switch from unsorted to sorted at any time,
3490a3874b8bSToomas Soome 	 * but afterwards the scan will remain sorted unless reloaded from
3491a3874b8bSToomas Soome 	 * a checkpoint after a reboot.
3492a3874b8bSToomas Soome 	 */
3493a3874b8bSToomas Soome 	if (!zfs_scan_legacy) {
3494a3874b8bSToomas Soome 		scn->scn_is_sorted = B_TRUE;
3495a3874b8bSToomas Soome 		if (scn->scn_last_checkpoint == 0)
3496a3874b8bSToomas Soome 			scn->scn_last_checkpoint = ddi_get_lbolt();
3497a3874b8bSToomas Soome 	}
34981702cce7SAlek Pinchuk 
3499a3874b8bSToomas Soome 	/*
3500a3874b8bSToomas Soome 	 * For sorted scans, determine what kind of work we will be doing
3501a3874b8bSToomas Soome 	 * this txg based on our memory limitations and whether or not we
3502a3874b8bSToomas Soome 	 * need to perform a checkpoint.
3503a3874b8bSToomas Soome 	 */
3504a3874b8bSToomas Soome 	if (scn->scn_is_sorted) {
3505a3874b8bSToomas Soome 		/*
3506a3874b8bSToomas Soome 		 * If we are over our checkpoint interval, set scn_clearing
3507a3874b8bSToomas Soome 		 * so that we can begin checkpointing immediately. The
3508a3874b8bSToomas Soome 		 * checkpoint allows us to save a consisent bookmark
3509a3874b8bSToomas Soome 		 * representing how much data we have scrubbed so far.
3510a3874b8bSToomas Soome 		 * Otherwise, use the memory limit to determine if we should
3511a3874b8bSToomas Soome 		 * scan for metadata or start issue scrub IOs. We accumulate
3512a3874b8bSToomas Soome 		 * metadata until we hit our hard memory limit at which point
3513a3874b8bSToomas Soome 		 * we issue scrub IOs until we are at our soft memory limit.
3514a3874b8bSToomas Soome 		 */
3515a3874b8bSToomas Soome 		if (scn->scn_checkpointing ||
3516a3874b8bSToomas Soome 		    ddi_get_lbolt() - scn->scn_last_checkpoint >
3517a3874b8bSToomas Soome 		    SEC_TO_TICK(zfs_scan_checkpoint_intval)) {
3518a3874b8bSToomas Soome 			if (!scn->scn_checkpointing)
3519a3874b8bSToomas Soome 				zfs_dbgmsg("begin scan checkpoint");
3520a3874b8bSToomas Soome 
3521a3874b8bSToomas Soome 			scn->scn_checkpointing = B_TRUE;
3522a3874b8bSToomas Soome 			scn->scn_clearing = B_TRUE;
3523a3874b8bSToomas Soome 		} else {
3524a3874b8bSToomas Soome 			boolean_t should_clear = dsl_scan_should_clear(scn);
3525a3874b8bSToomas Soome 			if (should_clear && !scn->scn_clearing) {
3526a3874b8bSToomas Soome 				zfs_dbgmsg("begin scan clearing");
3527a3874b8bSToomas Soome 				scn->scn_clearing = B_TRUE;
3528a3874b8bSToomas Soome 			} else if (!should_clear && scn->scn_clearing) {
3529a3874b8bSToomas Soome 				zfs_dbgmsg("finish scan clearing");
3530a3874b8bSToomas Soome 				scn->scn_clearing = B_FALSE;
3531a3874b8bSToomas Soome 			}
3532a3874b8bSToomas Soome 		}
35333f9d6ad7SLin Ling 	} else {
3534a3874b8bSToomas Soome 		ASSERT0(scn->scn_checkpointing);
3535a3874b8bSToomas Soome 		ASSERT0(scn->scn_clearing);
35363f9d6ad7SLin Ling 	}
35373f9d6ad7SLin Ling 
3538a3874b8bSToomas Soome 	if (!scn->scn_clearing && scn->scn_done_txg == 0) {
3539a3874b8bSToomas Soome 		/* Need to scan metadata for more blocks to scrub */
3540a3874b8bSToomas Soome 		dsl_scan_phys_t *scnp = &scn->scn_phys;
3541a3874b8bSToomas Soome 		taskqid_t prefetch_tqid;
3542a3874b8bSToomas Soome 		uint64_t bytes_per_leaf = zfs_scan_vdev_limit;
3543a3874b8bSToomas Soome 		uint64_t nr_leaves = dsl_scan_count_leaves(spa->spa_root_vdev);
35443f9d6ad7SLin Ling 
3545a3874b8bSToomas Soome 		/*
3546a3874b8bSToomas Soome 		 * Calculate the max number of in-flight bytes for pool-wide
3547a3874b8bSToomas Soome 		 * scanning operations (minimum 1MB). Limits for the issuing
3548a3874b8bSToomas Soome 		 * phase are done per top-level vdev and are handled separately.
3549a3874b8bSToomas Soome 		 */
3550a3874b8bSToomas Soome 		scn->scn_maxinflight_bytes =
3551a3874b8bSToomas Soome 		    MAX(nr_leaves * bytes_per_leaf, 1ULL << 20);
3552a3874b8bSToomas Soome 
3553a3874b8bSToomas Soome 		if (scnp->scn_ddt_bookmark.ddb_class <=
3554a3874b8bSToomas Soome 		    scnp->scn_ddt_class_max) {
3555a3874b8bSToomas Soome 			ASSERT(ZB_IS_ZERO(&scnp->scn_bookmark));
3556a3874b8bSToomas Soome 			zfs_dbgmsg("doing scan sync txg %llu; "
3557a3874b8bSToomas Soome 			    "ddt bm=%llu/%llu/%llu/%llx",
3558a3874b8bSToomas Soome 			    (longlong_t)tx->tx_txg,
3559a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_ddt_bookmark.ddb_class,
3560a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_ddt_bookmark.ddb_type,
3561a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_ddt_bookmark.ddb_checksum,
3562a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_ddt_bookmark.ddb_cursor);
3563a3874b8bSToomas Soome 		} else {
3564a3874b8bSToomas Soome 			zfs_dbgmsg("doing scan sync txg %llu; "
3565a3874b8bSToomas Soome 			    "bm=%llu/%llu/%llu/%llu",
3566a3874b8bSToomas Soome 			    (longlong_t)tx->tx_txg,
3567a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_bookmark.zb_objset,
3568a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_bookmark.zb_object,
3569a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_bookmark.zb_level,
3570a3874b8bSToomas Soome 			    (longlong_t)scnp->scn_bookmark.zb_blkid);
3571a3874b8bSToomas Soome 		}
35723f9d6ad7SLin Ling 
3573a3874b8bSToomas Soome 		scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
3574a3874b8bSToomas Soome 		    NULL, ZIO_FLAG_CANFAIL);
35753f9d6ad7SLin Ling 
3576a3874b8bSToomas Soome 		scn->scn_prefetch_stop = B_FALSE;
3577a3874b8bSToomas Soome 		prefetch_tqid = taskq_dispatch(dp->dp_sync_taskq,
3578a3874b8bSToomas Soome 		    dsl_scan_prefetch_thread, scn, TQ_SLEEP);
3579a3874b8bSToomas Soome 		ASSERT(prefetch_tqid != TASKQID_INVALID);
35803f9d6ad7SLin Ling 
3581a3874b8bSToomas Soome 		dsl_pool_config_enter(dp, FTAG);
3582a3874b8bSToomas Soome 		dsl_scan_visit(scn, tx);
3583a3874b8bSToomas Soome 		dsl_pool_config_exit(dp, FTAG);
35843f9d6ad7SLin Ling 
3585a3874b8bSToomas Soome 		mutex_enter(&dp->dp_spa->spa_scrub_lock);
3586a3874b8bSToomas Soome 		scn->scn_prefetch_stop = B_TRUE;
3587a3874b8bSToomas Soome 		cv_broadcast(&spa->spa_scrub_io_cv);
3588a3874b8bSToomas Soome 		mutex_exit(&dp->dp_spa->spa_scrub_lock);
35893f9d6ad7SLin Ling 
3590a3874b8bSToomas Soome 		taskq_wait_id(dp->dp_sync_taskq, prefetch_tqid);
3591a3874b8bSToomas Soome 		(void) zio_wait(scn->scn_zio_root);
3592a3874b8bSToomas Soome 		scn->scn_zio_root = NULL;
3593a3874b8bSToomas Soome 
3594a3874b8bSToomas Soome 		zfs_dbgmsg("scan visited %llu blocks in %llums "
3595a3874b8bSToomas Soome 		    "(%llu os's, %llu holes, %llu < mintxg, "
3596a3874b8bSToomas Soome 		    "%llu in ddt, %llu > maxtxg)",
3597a3874b8bSToomas Soome 		    (longlong_t)scn->scn_visited_this_txg,
3598a3874b8bSToomas Soome 		    (longlong_t)NSEC2MSEC(gethrtime() -
3599a3874b8bSToomas Soome 		    scn->scn_sync_start_time),
3600a3874b8bSToomas Soome 		    (longlong_t)scn->scn_objsets_visited_this_txg,
3601a3874b8bSToomas Soome 		    (longlong_t)scn->scn_holes_this_txg,
3602a3874b8bSToomas Soome 		    (longlong_t)scn->scn_lt_min_this_txg,
3603a3874b8bSToomas Soome 		    (longlong_t)scn->scn_ddt_contained_this_txg,
3604a3874b8bSToomas Soome 		    (longlong_t)scn->scn_gt_max_this_txg);
3605a3874b8bSToomas Soome 
3606a3874b8bSToomas Soome 		if (!scn->scn_suspending) {
3607a3874b8bSToomas Soome 			ASSERT0(avl_numnodes(&scn->scn_queue));
3608a3874b8bSToomas Soome 			scn->scn_done_txg = tx->tx_txg + 1;
3609a3874b8bSToomas Soome 			if (scn->scn_is_sorted) {
3610a3874b8bSToomas Soome 				scn->scn_checkpointing = B_TRUE;
3611a3874b8bSToomas Soome 				scn->scn_clearing = B_TRUE;
3612a3874b8bSToomas Soome 			}
3613a3874b8bSToomas Soome 			zfs_dbgmsg("scan complete txg %llu",
3614a3874b8bSToomas Soome 			    (longlong_t)tx->tx_txg);
3615a3874b8bSToomas Soome 		}
3616a3874b8bSToomas Soome 	} else if (scn->scn_is_sorted && scn->scn_bytes_pending != 0) {
3617e4c795beSTom Caputi 		ASSERT(scn->scn_clearing);
3618e4c795beSTom Caputi 
3619a3874b8bSToomas Soome 		/* need to issue scrubbing IOs from per-vdev queues */
3620a3874b8bSToomas Soome 		scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
3621a3874b8bSToomas Soome 		    NULL, ZIO_FLAG_CANFAIL);
3622a3874b8bSToomas Soome 		scan_io_queues_run(scn);
3623a3874b8bSToomas Soome 		(void) zio_wait(scn->scn_zio_root);
3624a3874b8bSToomas Soome 		scn->scn_zio_root = NULL;
3625a3874b8bSToomas Soome 
3626a3874b8bSToomas Soome 		/* calculate and dprintf the current memory usage */
3627a3874b8bSToomas Soome 		(void) dsl_scan_should_clear(scn);
3628a3874b8bSToomas Soome 		dsl_scan_update_stats(scn);
3629a3874b8bSToomas Soome 
3630a3874b8bSToomas Soome 		zfs_dbgmsg("scrubbed %llu blocks (%llu segs) in %llums "
3631a3874b8bSToomas Soome 		    "(avg_block_size = %llu, avg_seg_size = %llu)",
3632a3874b8bSToomas Soome 		    (longlong_t)scn->scn_zios_this_txg,
3633a3874b8bSToomas Soome 		    (longlong_t)scn->scn_segs_this_txg,
3634a3874b8bSToomas Soome 		    (longlong_t)NSEC2MSEC(gethrtime() -
3635a3874b8bSToomas Soome 		    scn->scn_sync_start_time),
3636a3874b8bSToomas Soome 		    (longlong_t)scn->scn_avg_zio_size_this_txg,
3637a3874b8bSToomas Soome 		    (longlong_t)scn->scn_avg_seg_size_this_txg);
3638a3874b8bSToomas Soome 	} else if (scn->scn_done_txg != 0 && scn->scn_done_txg <= tx->tx_txg) {
3639a3874b8bSToomas Soome 		/* Finished with everything. Mark the scrub as complete */
3640a3874b8bSToomas Soome 		zfs_dbgmsg("scan issuing complete txg %llu",
3641a3874b8bSToomas Soome 		    (longlong_t)tx->tx_txg);
3642a3874b8bSToomas Soome 		ASSERT3U(scn->scn_done_txg, !=, 0);
3643a3874b8bSToomas Soome 		ASSERT0(spa->spa_scrub_inflight);
3644a3874b8bSToomas Soome 		ASSERT0(scn->scn_bytes_pending);
3645a3874b8bSToomas Soome 		dsl_scan_done(scn, B_TRUE, tx);
3646a3874b8bSToomas Soome 		sync_type = SYNC_MANDATORY;
36473f9d6ad7SLin Ling 	}
36483f9d6ad7SLin Ling 
3649a3874b8bSToomas Soome 	dsl_scan_sync_state(scn, tx, sync_type);
36503f9d6ad7SLin Ling }
36513f9d6ad7SLin Ling 
36523f9d6ad7SLin Ling static void
3653a3874b8bSToomas Soome count_block(dsl_scan_t *scn, zfs_all_blkstats_t *zab, const blkptr_t *bp)
36543f9d6ad7SLin Ling {
36553f9d6ad7SLin Ling 	int i;
36563f9d6ad7SLin Ling 
3657ee2f9ca4SBill Sommerfeld 	/*
3658ee2f9ca4SBill Sommerfeld 	 * Don't count embedded bp's, since we already did the work of
3659ee2f9ca4SBill Sommerfeld 	 * scanning these when we scanned the containing block.
3660ee2f9ca4SBill Sommerfeld 	 */
3661ee2f9ca4SBill Sommerfeld 	if (BP_IS_EMBEDDED(bp))
3662ee2f9ca4SBill Sommerfeld 		return;
3663ee2f9ca4SBill Sommerfeld 
366412a8814cSTom Caputi 	/*
366512a8814cSTom Caputi 	 * Update the spa's stats on how many bytes we have issued.
366612a8814cSTom Caputi 	 * Sequential scrubs create a zio for each DVA of the bp. Each
366712a8814cSTom Caputi 	 * of these will include all DVAs for repair purposes, but the
366812a8814cSTom Caputi 	 * zio code will only try the first one unless there is an issue.
366912a8814cSTom Caputi 	 * Therefore, we should only count the first DVA for these IOs.
367012a8814cSTom Caputi 	 */
367112a8814cSTom Caputi 	if (scn->scn_is_sorted) {
3672a3874b8bSToomas Soome 		atomic_add_64(&scn->scn_dp->dp_spa->spa_scan_pass_issued,
367312a8814cSTom Caputi 		    DVA_GET_ASIZE(&bp->blk_dva[0]));
367412a8814cSTom Caputi 	} else {
367512a8814cSTom Caputi 		spa_t *spa = scn->scn_dp->dp_spa;
367612a8814cSTom Caputi 
367712a8814cSTom Caputi 		for (i = 0; i < BP_GET_NDVAS(bp); i++) {
367812a8814cSTom Caputi 			atomic_add_64(&spa->spa_scan_pass_issued,
367912a8814cSTom Caputi 			    DVA_GET_ASIZE(&bp->blk_dva[i]));
368012a8814cSTom Caputi 		}
3681a3874b8bSToomas Soome 	}
3682a3874b8bSToomas Soome 
36833f9d6ad7SLin Ling 	/*
36843f9d6ad7SLin Ling 	 * If we resume after a reboot, zab will be NULL; don't record
36853f9d6ad7SLin Ling 	 * incomplete stats in that case.
36863f9d6ad7SLin Ling 	 */
36873f9d6ad7SLin Ling 	if (zab == NULL)
36883f9d6ad7SLin Ling 		return;
36893f9d6ad7SLin Ling 
3690a3874b8bSToomas Soome 	mutex_enter(&zab->zab_lock);
3691a3874b8bSToomas Soome 
36923f9d6ad7SLin Ling 	for (i = 0; i < 4; i++) {
36933f9d6ad7SLin Ling 		int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
36943f9d6ad7SLin Ling 		int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
3695ad135b5dSChristopher Siden 		if (t & DMU_OT_NEWTYPE)
3696ad135b5dSChristopher Siden 			t = DMU_OT_OTHER;
36973f9d6ad7SLin Ling 		zfs_blkstat_t *zb = &zab->zab_type[l][t];
36983f9d6ad7SLin Ling 		int equal;
36993f9d6ad7SLin Ling 
37003f9d6ad7SLin Ling 		zb->zb_count++;
37013f9d6ad7SLin Ling 		zb->zb_asize += BP_GET_ASIZE(bp);
37023f9d6ad7SLin Ling 		zb->zb_lsize += BP_GET_LSIZE(bp);
37033f9d6ad7SLin Ling 		zb->zb_psize += BP_GET_PSIZE(bp);
37043f9d6ad7SLin Ling 		zb->zb_gangs += BP_COUNT_GANG(bp);
37053f9d6ad7SLin Ling 
37063f9d6ad7SLin Ling 		switch (BP_GET_NDVAS(bp)) {
37073f9d6ad7SLin Ling 		case 2:
37083f9d6ad7SLin Ling 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
37093f9d6ad7SLin Ling 			    DVA_GET_VDEV(&bp->blk_dva[1]))
37103f9d6ad7SLin Ling 				zb->zb_ditto_2_of_2_samevdev++;
37113f9d6ad7SLin Ling 			break;
37123f9d6ad7SLin Ling 		case 3:
37133f9d6ad7SLin Ling 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
37143f9d6ad7SLin Ling 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
37153f9d6ad7SLin Ling 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
37163f9d6ad7SLin Ling 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
37173f9d6ad7SLin Ling 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
37183f9d6ad7SLin Ling 			    DVA_GET_VDEV(&bp->blk_dva[2]));
37193f9d6ad7SLin Ling 			if (equal == 1)
37203f9d6ad7SLin Ling 				zb->zb_ditto_2_of_3_samevdev++;
37213f9d6ad7SLin Ling 			else if (equal == 3)
37223f9d6ad7SLin Ling 				zb->zb_ditto_3_of_3_samevdev++;
37233f9d6ad7SLin Ling 			break;
37243f9d6ad7SLin Ling 		}
37253f9d6ad7SLin Ling 	}
3726a3874b8bSToomas Soome 
3727a3874b8bSToomas Soome 	mutex_exit(&zab->zab_lock);
37283f9d6ad7SLin Ling }
37293f9d6ad7SLin Ling 
37303f9d6ad7SLin Ling static void
3731a3874b8bSToomas Soome scan_io_queue_insert_impl(dsl_scan_io_queue_t *queue, scan_io_t *sio)
37323f9d6ad7SLin Ling {
3733a3874b8bSToomas Soome 	avl_index_t idx;
373412a8814cSTom Caputi 	int64_t asize = SIO_GET_ASIZE(sio);
3735a3874b8bSToomas Soome 	dsl_scan_t *scn = queue->q_scn;
37363f9d6ad7SLin Ling 
3737a3874b8bSToomas Soome 	ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock));
37383f9d6ad7SLin Ling 
3739a3874b8bSToomas Soome 	if (avl_find(&queue->q_sios_by_addr, sio, &idx) != NULL) {
3740a3874b8bSToomas Soome 		/* block is already scheduled for reading */
3741a3874b8bSToomas Soome 		atomic_add_64(&scn->scn_bytes_pending, -asize);
374212a8814cSTom Caputi 		sio_free(sio);
3743a3874b8bSToomas Soome 		return;
3744a3874b8bSToomas Soome 	}
3745a3874b8bSToomas Soome 	avl_insert(&queue->q_sios_by_addr, sio, idx);
374612a8814cSTom Caputi 	queue->q_sio_memused += SIO_GET_MUSED(sio);
374712a8814cSTom Caputi 	range_tree_add(queue->q_exts_by_addr, SIO_GET_OFFSET(sio), asize);
3748a3874b8bSToomas Soome }
37493f9d6ad7SLin Ling 
3750a3874b8bSToomas Soome /*
3751a3874b8bSToomas Soome  * Given all the info we got from our metadata scanning process, we
3752a3874b8bSToomas Soome  * construct a scan_io_t and insert it into the scan sorting queue. The
3753a3874b8bSToomas Soome  * I/O must already be suitable for us to process. This is controlled
3754a3874b8bSToomas Soome  * by dsl_scan_enqueue().
3755a3874b8bSToomas Soome  */
3756a3874b8bSToomas Soome static void
3757a3874b8bSToomas Soome scan_io_queue_insert(dsl_scan_io_queue_t *queue, const blkptr_t *bp, int dva_i,
3758a3874b8bSToomas Soome     int zio_flags, const zbookmark_phys_t *zb)
3759a3874b8bSToomas Soome {
3760a3874b8bSToomas Soome 	dsl_scan_t *scn = queue->q_scn;
376112a8814cSTom Caputi 	scan_io_t *sio = sio_alloc(BP_GET_NDVAS(bp));
3762a3874b8bSToomas Soome 
3763a3874b8bSToomas Soome 	ASSERT0(BP_IS_GANG(bp));
3764a3874b8bSToomas Soome 	ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock));
3765a3874b8bSToomas Soome 
3766a3874b8bSToomas Soome 	bp2sio(bp, sio, dva_i);
3767a3874b8bSToomas Soome 	sio->sio_flags = zio_flags;
3768a3874b8bSToomas Soome 	sio->sio_zb = *zb;
3769a3874b8bSToomas Soome 
3770a3874b8bSToomas Soome 	/*
3771a3874b8bSToomas Soome 	 * Increment the bytes pending counter now so that we can't
3772a3874b8bSToomas Soome 	 * get an integer underflow in case the worker processes the
3773a3874b8bSToomas Soome 	 * zio before we get to incrementing this counter.
3774a3874b8bSToomas Soome 	 */
377512a8814cSTom Caputi 	atomic_add_64(&scn->scn_bytes_pending, SIO_GET_ASIZE(sio));
3776a3874b8bSToomas Soome 
3777a3874b8bSToomas Soome 	scan_io_queue_insert_impl(queue, sio);
3778a3874b8bSToomas Soome }
3779a3874b8bSToomas Soome 
3780a3874b8bSToomas Soome /*
3781a3874b8bSToomas Soome  * Given a set of I/O parameters as discovered by the metadata traversal
3782a3874b8bSToomas Soome  * process, attempts to place the I/O into the sorted queues (if allowed),
3783a3874b8bSToomas Soome  * or immediately executes the I/O.
3784a3874b8bSToomas Soome  */
3785a3874b8bSToomas Soome static void
3786a3874b8bSToomas Soome dsl_scan_enqueue(dsl_pool_t *dp, const blkptr_t *bp, int zio_flags,
3787a3874b8bSToomas Soome     const zbookmark_phys_t *zb)
3788a3874b8bSToomas Soome {
3789a3874b8bSToomas Soome 	spa_t *spa = dp->dp_spa;
3790a3874b8bSToomas Soome 
3791a3874b8bSToomas Soome 	ASSERT(!BP_IS_EMBEDDED(bp));
3792a3874b8bSToomas Soome 
3793a3874b8bSToomas Soome 	/*
3794a3874b8bSToomas Soome 	 * Gang blocks are hard to issue sequentially, so we just issue them
3795a3874b8bSToomas Soome 	 * here immediately instead of queuing them.
3796a3874b8bSToomas Soome 	 */
3797a3874b8bSToomas Soome 	if (!dp->dp_scan->scn_is_sorted || BP_IS_GANG(bp)) {
3798a3874b8bSToomas Soome 		scan_exec_io(dp, bp, zio_flags, zb, NULL);
3799a3874b8bSToomas Soome 		return;
3800a3874b8bSToomas Soome 	}
3801a3874b8bSToomas Soome 	for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
3802a3874b8bSToomas Soome 		dva_t dva;
3803a3874b8bSToomas Soome 		vdev_t *vdev;
3804a3874b8bSToomas Soome 
3805a3874b8bSToomas Soome 		dva = bp->blk_dva[i];
3806a3874b8bSToomas Soome 		vdev = vdev_lookup_top(spa, DVA_GET_VDEV(&dva));
3807a3874b8bSToomas Soome 		ASSERT(vdev != NULL);
3808a3874b8bSToomas Soome 
3809a3874b8bSToomas Soome 		mutex_enter(&vdev->vdev_scan_io_queue_lock);
3810a3874b8bSToomas Soome 		if (vdev->vdev_scan_io_queue == NULL)
3811a3874b8bSToomas Soome 			vdev->vdev_scan_io_queue = scan_io_queue_create(vdev);
3812a3874b8bSToomas Soome 		ASSERT(dp->dp_scan != NULL);
3813a3874b8bSToomas Soome 		scan_io_queue_insert(vdev->vdev_scan_io_queue, bp,
3814a3874b8bSToomas Soome 		    i, zio_flags, zb);
3815a3874b8bSToomas Soome 		mutex_exit(&vdev->vdev_scan_io_queue_lock);
38163f9d6ad7SLin Ling 	}
38173f9d6ad7SLin Ling }
38183f9d6ad7SLin Ling 
38193f9d6ad7SLin Ling static int
38203f9d6ad7SLin Ling dsl_scan_scrub_cb(dsl_pool_t *dp,
38217802d7bfSMatthew Ahrens     const blkptr_t *bp, const zbookmark_phys_t *zb)
38223f9d6ad7SLin Ling {
38233f9d6ad7SLin Ling 	dsl_scan_t *scn = dp->dp_scan;
38243f9d6ad7SLin Ling 	spa_t *spa = dp->dp_spa;
38253f9d6ad7SLin Ling 	uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
3826a3874b8bSToomas Soome 	size_t psize = BP_GET_PSIZE(bp);
38273f9d6ad7SLin Ling 	boolean_t needs_io;
382844ecc532SGeorge Wilson 	int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
3829a3874b8bSToomas Soome 	int d;
3830dec267e7SMatthew Ahrens 
38313f9d6ad7SLin Ling 	if (phys_birth <= scn->scn_phys.scn_min_txg ||
3832a3874b8bSToomas Soome 	    phys_birth >= scn->scn_phys.scn_max_txg) {
3833a3874b8bSToomas Soome 		count_block(scn, dp->dp_blkstats, bp);
38343f9d6ad7SLin Ling 		return (0);
3835a3874b8bSToomas Soome 	}
38363f9d6ad7SLin Ling 
3837dec267e7SMatthew Ahrens 	/* Embedded BP's have phys_birth==0, so we reject them above. */
3838dec267e7SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
38395d7b4d43SMatthew Ahrens 
38403f9d6ad7SLin Ling 	ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
38413f9d6ad7SLin Ling 	if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
38423f9d6ad7SLin Ling 		zio_flags |= ZIO_FLAG_SCRUB;
38433f9d6ad7SLin Ling 		needs_io = B_TRUE;
3844d5285caeSGeorge Wilson 	} else {
3845d5285caeSGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
38463f9d6ad7SLin Ling 		zio_flags |= ZIO_FLAG_RESILVER;
38473f9d6ad7SLin Ling 		needs_io = B_FALSE;
38483f9d6ad7SLin Ling 	}
38493f9d6ad7SLin Ling 
38503f9d6ad7SLin Ling 	/* If it's an intent log block, failure is expected. */
38513f9d6ad7SLin Ling 	if (zb->zb_level == ZB_ZIL_LEVEL)
38523f9d6ad7SLin Ling 		zio_flags |= ZIO_FLAG_SPECULATIVE;
38533f9d6ad7SLin Ling 
3854a3874b8bSToomas Soome 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
3855a3874b8bSToomas Soome 		const dva_t *dva = &bp->blk_dva[d];
38563f9d6ad7SLin Ling 
38573f9d6ad7SLin Ling 		/*
38583f9d6ad7SLin Ling 		 * Keep track of how much data we've examined so that
38593f9d6ad7SLin Ling 		 * zpool(1M) status can make useful progress reports.
38603f9d6ad7SLin Ling 		 */
3861a3874b8bSToomas Soome 		scn->scn_phys.scn_examined += DVA_GET_ASIZE(dva);
3862a3874b8bSToomas Soome 		spa->spa_scan_pass_exam += DVA_GET_ASIZE(dva);
38633f9d6ad7SLin Ling 
38643f9d6ad7SLin Ling 		/* if it's a resilver, this may not be in the target range */
3865a3874b8bSToomas Soome 		if (!needs_io)
3866a3874b8bSToomas Soome 			needs_io = dsl_scan_need_resilver(spa, dva, psize,
3867a3874b8bSToomas Soome 			    phys_birth);
38683f9d6ad7SLin Ling 	}
38693f9d6ad7SLin Ling 
38703f9d6ad7SLin Ling 	if (needs_io && !zfs_no_scrub_io) {
3871a3874b8bSToomas Soome 		dsl_scan_enqueue(dp, bp, zio_flags, zb);
3872a3874b8bSToomas Soome 	} else {
3873a3874b8bSToomas Soome 		count_block(scn, dp->dp_blkstats, bp);
3874a3874b8bSToomas Soome 	}
3875a3874b8bSToomas Soome 
3876a3874b8bSToomas Soome 	/* do not relocate this block */
3877a3874b8bSToomas Soome 	return (0);
3878a3874b8bSToomas Soome }
38793f9d6ad7SLin Ling 
3880a3874b8bSToomas Soome static void
3881a3874b8bSToomas Soome dsl_scan_scrub_done(zio_t *zio)
3882a3874b8bSToomas Soome {
3883a3874b8bSToomas Soome 	spa_t *spa = zio->io_spa;
3884a3874b8bSToomas Soome 	blkptr_t *bp = zio->io_bp;
3885a3874b8bSToomas Soome 	dsl_scan_io_queue_t *queue = zio->io_private;
3886a3874b8bSToomas Soome 
3887a3874b8bSToomas Soome 	abd_free(zio->io_abd);
3888a3874b8bSToomas Soome 
3889a3874b8bSToomas Soome 	if (queue == NULL) {
38903f9d6ad7SLin Ling 		mutex_enter(&spa->spa_scrub_lock);
3891a3874b8bSToomas Soome 		ASSERT3U(spa->spa_scrub_inflight, >=, BP_GET_PSIZE(bp));
3892a3874b8bSToomas Soome 		spa->spa_scrub_inflight -= BP_GET_PSIZE(bp);
3893a3874b8bSToomas Soome 		cv_broadcast(&spa->spa_scrub_io_cv);
3894a3874b8bSToomas Soome 		mutex_exit(&spa->spa_scrub_lock);
3895a3874b8bSToomas Soome 	} else {
3896a3874b8bSToomas Soome 		mutex_enter(&queue->q_vd->vdev_scan_io_queue_lock);
3897a3874b8bSToomas Soome 		ASSERT3U(queue->q_inflight_bytes, >=, BP_GET_PSIZE(bp));
3898a3874b8bSToomas Soome 		queue->q_inflight_bytes -= BP_GET_PSIZE(bp);
3899a3874b8bSToomas Soome 		cv_broadcast(&queue->q_zio_cv);
3900a3874b8bSToomas Soome 		mutex_exit(&queue->q_vd->vdev_scan_io_queue_lock);
3901a3874b8bSToomas Soome 	}
3902a3874b8bSToomas Soome 
3903a3874b8bSToomas Soome 	if (zio->io_error && (zio->io_error != ECKSUM ||
3904a3874b8bSToomas Soome 	    !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
3905a3874b8bSToomas Soome 		atomic_inc_64(&spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors);
3906a3874b8bSToomas Soome 	}
3907a3874b8bSToomas Soome }
3908a3874b8bSToomas Soome 
3909a3874b8bSToomas Soome /*
3910a3874b8bSToomas Soome  * Given a scanning zio's information, executes the zio. The zio need
3911a3874b8bSToomas Soome  * not necessarily be only sortable, this function simply executes the
3912a3874b8bSToomas Soome  * zio, no matter what it is. The optional queue argument allows the
3913a3874b8bSToomas Soome  * caller to specify that they want per top level vdev IO rate limiting
3914a3874b8bSToomas Soome  * instead of the legacy global limiting.
3915a3874b8bSToomas Soome  */
3916a3874b8bSToomas Soome static void
3917a3874b8bSToomas Soome scan_exec_io(dsl_pool_t *dp, const blkptr_t *bp, int zio_flags,
3918a3874b8bSToomas Soome     const zbookmark_phys_t *zb, dsl_scan_io_queue_t *queue)
3919a3874b8bSToomas Soome {
3920a3874b8bSToomas Soome 	spa_t *spa = dp->dp_spa;
3921a3874b8bSToomas Soome 	dsl_scan_t *scn = dp->dp_scan;
3922a3874b8bSToomas Soome 	size_t size = BP_GET_PSIZE(bp);
3923a3874b8bSToomas Soome 	abd_t *data = abd_alloc_for_io(size, B_FALSE);
3924a3874b8bSToomas Soome 
3925a3874b8bSToomas Soome 	if (queue == NULL) {
3926a3874b8bSToomas Soome 		mutex_enter(&spa->spa_scrub_lock);
3927a3874b8bSToomas Soome 		while (spa->spa_scrub_inflight >= scn->scn_maxinflight_bytes)
39283f9d6ad7SLin Ling 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3929a3874b8bSToomas Soome 		spa->spa_scrub_inflight += BP_GET_PSIZE(bp);
39303f9d6ad7SLin Ling 		mutex_exit(&spa->spa_scrub_lock);
3931a3874b8bSToomas Soome 	} else {
3932a3874b8bSToomas Soome 		kmutex_t *q_lock = &queue->q_vd->vdev_scan_io_queue_lock;
39333f9d6ad7SLin Ling 
3934a3874b8bSToomas Soome 		mutex_enter(q_lock);
3935a3874b8bSToomas Soome 		while (queue->q_inflight_bytes >= queue->q_maxinflight_bytes)
3936a3874b8bSToomas Soome 			cv_wait(&queue->q_zio_cv, q_lock);
3937a3874b8bSToomas Soome 		queue->q_inflight_bytes += BP_GET_PSIZE(bp);
3938a3874b8bSToomas Soome 		mutex_exit(q_lock);
3939a3874b8bSToomas Soome 	}
3940a3874b8bSToomas Soome 
3941a3874b8bSToomas Soome 	count_block(dp->dp_scan, dp->dp_blkstats, bp);
3942a3874b8bSToomas Soome 	zio_nowait(zio_read(dp->dp_scan->scn_zio_root, spa, bp, data, size,
3943a3874b8bSToomas Soome 	    dsl_scan_scrub_done, queue, ZIO_PRIORITY_SCRUB, zio_flags, zb));
3944a3874b8bSToomas Soome }
394544ecc532SGeorge Wilson 
3946a3874b8bSToomas Soome /*
3947a3874b8bSToomas Soome  * This is the primary extent sorting algorithm. We balance two parameters:
3948a3874b8bSToomas Soome  * 1) how many bytes of I/O are in an extent
3949a3874b8bSToomas Soome  * 2) how well the extent is filled with I/O (as a fraction of its total size)
3950a3874b8bSToomas Soome  * Since we allow extents to have gaps between their constituent I/Os, it's
3951a3874b8bSToomas Soome  * possible to have a fairly large extent that contains the same amount of
3952a3874b8bSToomas Soome  * I/O bytes than a much smaller extent, which just packs the I/O more tightly.
3953a3874b8bSToomas Soome  * The algorithm sorts based on a score calculated from the extent's size,
3954a3874b8bSToomas Soome  * the relative fill volume (in %) and a "fill weight" parameter that controls
3955a3874b8bSToomas Soome  * the split between whether we prefer larger extents or more well populated
3956a3874b8bSToomas Soome  * extents:
3957a3874b8bSToomas Soome  *
3958a3874b8bSToomas Soome  * SCORE = FILL_IN_BYTES + (FILL_IN_PERCENT * FILL_IN_BYTES * FILL_WEIGHT)
3959a3874b8bSToomas Soome  *
3960a3874b8bSToomas Soome  * Example:
3961a3874b8bSToomas Soome  * 1) assume extsz = 64 MiB
3962a3874b8bSToomas Soome  * 2) assume fill = 32 MiB (extent is half full)
3963a3874b8bSToomas Soome  * 3) assume fill_weight = 3
3964a3874b8bSToomas Soome  * 4)	SCORE = 32M + (((32M * 100) / 64M) * 3 * 32M) / 100
3965a3874b8bSToomas Soome  *	SCORE = 32M + (50 * 3 * 32M) / 100
3966a3874b8bSToomas Soome  *	SCORE = 32M + (4800M / 100)
3967a3874b8bSToomas Soome  *	SCORE = 32M + 48M
3968a3874b8bSToomas Soome  *		^	^
3969a3874b8bSToomas Soome  *		|	+--- final total relative fill-based score
3970a3874b8bSToomas Soome  *		+--------- final total fill-based score
3971a3874b8bSToomas Soome  *	SCORE = 80M
3972a3874b8bSToomas Soome  *
3973a3874b8bSToomas Soome  * As can be seen, at fill_ratio=3, the algorithm is slightly biased towards
3974a3874b8bSToomas Soome  * extents that are more completely filled (in a 3:2 ratio) vs just larger.
3975a3874b8bSToomas Soome  * Note that as an optimization, we replace multiplication and division by
3976a3874b8bSToomas Soome  * 100 with bitshifting by 7 (which effecitvely multiplies and divides by 128).
3977a3874b8bSToomas Soome  */
3978a3874b8bSToomas Soome static int
3979a3874b8bSToomas Soome ext_size_compare(const void *x, const void *y)
3980a3874b8bSToomas Soome {
39814d7988d6SPaul Dagnelie 	const range_seg_gap_t *rsa = x, *rsb = y;
39824d7988d6SPaul Dagnelie 
39834d7988d6SPaul Dagnelie 	uint64_t sa = rsa->rs_end - rsa->rs_start;
39844d7988d6SPaul Dagnelie 	uint64_t sb = rsb->rs_end - rsb->rs_start;
3985a3874b8bSToomas Soome 	uint64_t score_a, score_b;
3986a3874b8bSToomas Soome 
3987a3874b8bSToomas Soome 	score_a = rsa->rs_fill + ((((rsa->rs_fill << 7) / sa) *
3988a3874b8bSToomas Soome 	    fill_weight * rsa->rs_fill) >> 7);
3989a3874b8bSToomas Soome 	score_b = rsb->rs_fill + ((((rsb->rs_fill << 7) / sb) *
3990a3874b8bSToomas Soome 	    fill_weight * rsb->rs_fill) >> 7);
3991a3874b8bSToomas Soome 
3992a3874b8bSToomas Soome 	if (score_a > score_b)
3993a3874b8bSToomas Soome 		return (-1);
3994a3874b8bSToomas Soome 	if (score_a == score_b) {
3995a3874b8bSToomas Soome 		if (rsa->rs_start < rsb->rs_start)
3996a3874b8bSToomas Soome 			return (-1);
3997a3874b8bSToomas Soome 		if (rsa->rs_start == rsb->rs_start)
3998a3874b8bSToomas Soome 			return (0);
3999a3874b8bSToomas Soome 		return (1);
40003f9d6ad7SLin Ling 	}
4001a3874b8bSToomas Soome 	return (1);
4002a3874b8bSToomas Soome }
40033f9d6ad7SLin Ling 
4004a3874b8bSToomas Soome /*
4005a3874b8bSToomas Soome  * Comparator for the q_sios_by_addr tree. Sorting is simply performed
4006a3874b8bSToomas Soome  * based on LBA-order (from lowest to highest).
4007a3874b8bSToomas Soome  */
4008a3874b8bSToomas Soome static int
400912a8814cSTom Caputi sio_addr_compare(const void *x, const void *y)
4010a3874b8bSToomas Soome {
4011a3874b8bSToomas Soome 	const scan_io_t *a = x, *b = y;
4012a3874b8bSToomas Soome 
40134d7988d6SPaul Dagnelie 	return (TREE_CMP(SIO_GET_OFFSET(a), SIO_GET_OFFSET(b)));
4014a3874b8bSToomas Soome }
4015a3874b8bSToomas Soome 
4016a3874b8bSToomas Soome /* IO queues are created on demand when they are needed. */
4017a3874b8bSToomas Soome static dsl_scan_io_queue_t *
4018a3874b8bSToomas Soome scan_io_queue_create(vdev_t *vd)
4019a3874b8bSToomas Soome {
4020a3874b8bSToomas Soome 	dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan;
4021a3874b8bSToomas Soome 	dsl_scan_io_queue_t *q = kmem_zalloc(sizeof (*q), KM_SLEEP);
4022a3874b8bSToomas Soome 
4023a3874b8bSToomas Soome 	q->q_scn = scn;
4024a3874b8bSToomas Soome 	q->q_vd = vd;
402512a8814cSTom Caputi 	q->q_sio_memused = 0;
4026a3874b8bSToomas Soome 	cv_init(&q->q_zio_cv, NULL, CV_DEFAULT, NULL);
40274d7988d6SPaul Dagnelie 	q->q_exts_by_addr = range_tree_create_impl(&rt_btree_ops, RANGE_SEG_GAP,
40284d7988d6SPaul Dagnelie 	    &q->q_exts_by_size, 0, 0, ext_size_compare, zfs_scan_max_ext_gap);
402912a8814cSTom Caputi 	avl_create(&q->q_sios_by_addr, sio_addr_compare,
4030a3874b8bSToomas Soome 	    sizeof (scan_io_t), offsetof(scan_io_t, sio_nodes.sio_addr_node));
4031a3874b8bSToomas Soome 
4032a3874b8bSToomas Soome 	return (q);
40333f9d6ad7SLin Ling }
40343f9d6ad7SLin Ling 
40351702cce7SAlek Pinchuk /*
4036a3874b8bSToomas Soome  * Destroys a scan queue and all segments and scan_io_t's contained in it.
4037a3874b8bSToomas Soome  * No further execution of I/O occurs, anything pending in the queue is
4038a3874b8bSToomas Soome  * simply freed without being executed.
40391702cce7SAlek Pinchuk  */
4040a3874b8bSToomas Soome void
4041a3874b8bSToomas Soome dsl_scan_io_queue_destroy(dsl_scan_io_queue_t *queue)
40423f9d6ad7SLin Ling {
4043a3874b8bSToomas Soome 	dsl_scan_t *scn = queue->q_scn;
4044a3874b8bSToomas Soome 	scan_io_t *sio;
4045a3874b8bSToomas Soome 	void *cookie = NULL;
4046a3874b8bSToomas Soome 	int64_t bytes_dequeued = 0;
4047a3874b8bSToomas Soome 
4048a3874b8bSToomas Soome 	ASSERT(MUTEX_HELD(&queue->q_vd->vdev_scan_io_queue_lock));
4049a3874b8bSToomas Soome 
4050a3874b8bSToomas Soome 	while ((sio = avl_destroy_nodes(&queue->q_sios_by_addr, &cookie)) !=
4051a3874b8bSToomas Soome 	    NULL) {
4052a3874b8bSToomas Soome 		ASSERT(range_tree_contains(queue->q_exts_by_addr,
405312a8814cSTom Caputi 		    SIO_GET_OFFSET(sio), SIO_GET_ASIZE(sio)));
405412a8814cSTom Caputi 		bytes_dequeued += SIO_GET_ASIZE(sio);
405512a8814cSTom Caputi 		queue->q_sio_memused -= SIO_GET_MUSED(sio);
405612a8814cSTom Caputi 		sio_free(sio);
4057a3874b8bSToomas Soome 	}
4058a3874b8bSToomas Soome 
405912a8814cSTom Caputi 	ASSERT0(queue->q_sio_memused);
4060a3874b8bSToomas Soome 	atomic_add_64(&scn->scn_bytes_pending, -bytes_dequeued);
4061a3874b8bSToomas Soome 	range_tree_vacate(queue->q_exts_by_addr, NULL, queue);
4062a3874b8bSToomas Soome 	range_tree_destroy(queue->q_exts_by_addr);
4063a3874b8bSToomas Soome 	avl_destroy(&queue->q_sios_by_addr);
4064a3874b8bSToomas Soome 	cv_destroy(&queue->q_zio_cv);
4065a3874b8bSToomas Soome 
4066a3874b8bSToomas Soome 	kmem_free(queue, sizeof (*queue));
4067a3874b8bSToomas Soome }
4068a3874b8bSToomas Soome 
4069a3874b8bSToomas Soome /*
4070a3874b8bSToomas Soome  * Properly transfers a dsl_scan_queue_t from `svd' to `tvd'. This is
4071a3874b8bSToomas Soome  * called on behalf of vdev_top_transfer when creating or destroying
4072a3874b8bSToomas Soome  * a mirror vdev due to zpool attach/detach.
4073a3874b8bSToomas Soome  */
4074a3874b8bSToomas Soome void
4075a3874b8bSToomas Soome dsl_scan_io_queue_vdev_xfer(vdev_t *svd, vdev_t *tvd)
4076a3874b8bSToomas Soome {
4077a3874b8bSToomas Soome 	mutex_enter(&svd->vdev_scan_io_queue_lock);
4078a3874b8bSToomas Soome 	mutex_enter(&tvd->vdev_scan_io_queue_lock);
4079a3874b8bSToomas Soome 
4080a3874b8bSToomas Soome 	VERIFY3P(tvd->vdev_scan_io_queue, ==, NULL);
4081a3874b8bSToomas Soome 	tvd->vdev_scan_io_queue = svd->vdev_scan_io_queue;
4082a3874b8bSToomas Soome 	svd->vdev_scan_io_queue = NULL;
4083a3874b8bSToomas Soome 	if (tvd->vdev_scan_io_queue != NULL)
4084a3874b8bSToomas Soome 		tvd->vdev_scan_io_queue->q_vd = tvd;
4085a3874b8bSToomas Soome 
4086a3874b8bSToomas Soome 	mutex_exit(&tvd->vdev_scan_io_queue_lock);
4087a3874b8bSToomas Soome 	mutex_exit(&svd->vdev_scan_io_queue_lock);
4088a3874b8bSToomas Soome }
4089a3874b8bSToomas Soome 
4090a3874b8bSToomas Soome static void
4091a3874b8bSToomas Soome scan_io_queues_destroy(dsl_scan_t *scn)
4092a3874b8bSToomas Soome {
4093a3874b8bSToomas Soome 	vdev_t *rvd = scn->scn_dp->dp_spa->spa_root_vdev;
4094a3874b8bSToomas Soome 
4095a3874b8bSToomas Soome 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
4096a3874b8bSToomas Soome 		vdev_t *tvd = rvd->vdev_child[i];
4097a3874b8bSToomas Soome 
4098a3874b8bSToomas Soome 		mutex_enter(&tvd->vdev_scan_io_queue_lock);
4099a3874b8bSToomas Soome 		if (tvd->vdev_scan_io_queue != NULL)
4100a3874b8bSToomas Soome 			dsl_scan_io_queue_destroy(tvd->vdev_scan_io_queue);
4101a3874b8bSToomas Soome 		tvd->vdev_scan_io_queue = NULL;
4102a3874b8bSToomas Soome 		mutex_exit(&tvd->vdev_scan_io_queue_lock);
4103a3874b8bSToomas Soome 	}
4104a3874b8bSToomas Soome }
4105a3874b8bSToomas Soome 
4106a3874b8bSToomas Soome static void
4107a3874b8bSToomas Soome dsl_scan_freed_dva(spa_t *spa, const blkptr_t *bp, int dva_i)
4108a3874b8bSToomas Soome {
4109a3874b8bSToomas Soome 	dsl_pool_t *dp = spa->spa_dsl_pool;
41101702cce7SAlek Pinchuk 	dsl_scan_t *scn = dp->dp_scan;
4111a3874b8bSToomas Soome 	vdev_t *vdev;
4112a3874b8bSToomas Soome 	kmutex_t *q_lock;
4113a3874b8bSToomas Soome 	dsl_scan_io_queue_t *queue;
411412a8814cSTom Caputi 	scan_io_t *srch_sio, *sio;
4115a3874b8bSToomas Soome 	avl_index_t idx;
4116a3874b8bSToomas Soome 	uint64_t start, size;
4117a3874b8bSToomas Soome 
4118a3874b8bSToomas Soome 	vdev = vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[dva_i]));
4119a3874b8bSToomas Soome 	ASSERT(vdev != NULL);
4120a3874b8bSToomas Soome 	q_lock = &vdev->vdev_scan_io_queue_lock;
4121a3874b8bSToomas Soome 	queue = vdev->vdev_scan_io_queue;
4122a3874b8bSToomas Soome 
4123a3874b8bSToomas Soome 	mutex_enter(q_lock);
4124a3874b8bSToomas Soome 	if (queue == NULL) {
4125a3874b8bSToomas Soome 		mutex_exit(q_lock);
4126a3874b8bSToomas Soome 		return;
4127a3874b8bSToomas Soome 	}
4128a3874b8bSToomas Soome 
412912a8814cSTom Caputi 	srch_sio = sio_alloc(BP_GET_NDVAS(bp));
413012a8814cSTom Caputi 	bp2sio(bp, srch_sio, dva_i);
413112a8814cSTom Caputi 	start = SIO_GET_OFFSET(srch_sio);
413212a8814cSTom Caputi 	size = SIO_GET_ASIZE(srch_sio);
41333f9d6ad7SLin Ling 
41343f9d6ad7SLin Ling 	/*
4135a3874b8bSToomas Soome 	 * We can find the zio in two states:
4136a3874b8bSToomas Soome 	 * 1) Cold, just sitting in the queue of zio's to be issued at
4137a3874b8bSToomas Soome 	 *	some point in the future. In this case, all we do is
4138a3874b8bSToomas Soome 	 *	remove the zio from the q_sios_by_addr tree, decrement
4139a3874b8bSToomas Soome 	 *	its data volume from the containing range_seg_t and
4140a3874b8bSToomas Soome 	 *	resort the q_exts_by_size tree to reflect that the
4141a3874b8bSToomas Soome 	 *	range_seg_t has lost some of its 'fill'. We don't shorten
4142a3874b8bSToomas Soome 	 *	the range_seg_t - this is usually rare enough not to be
4143a3874b8bSToomas Soome 	 *	worth the extra hassle of trying keep track of precise
4144a3874b8bSToomas Soome 	 *	extent boundaries.
4145a3874b8bSToomas Soome 	 * 2) Hot, where the zio is currently in-flight in
4146a3874b8bSToomas Soome 	 *	dsl_scan_issue_ios. In this case, we can't simply
4147a3874b8bSToomas Soome 	 *	reach in and stop the in-flight zio's, so we instead
4148a3874b8bSToomas Soome 	 *	block the caller. Eventually, dsl_scan_issue_ios will
4149a3874b8bSToomas Soome 	 *	be done with issuing the zio's it gathered and will
4150a3874b8bSToomas Soome 	 *	signal us.
41513f9d6ad7SLin Ling 	 */
415212a8814cSTom Caputi 	sio = avl_find(&queue->q_sios_by_addr, srch_sio, &idx);
415312a8814cSTom Caputi 	sio_free(srch_sio);
415412a8814cSTom Caputi 
4155a3874b8bSToomas Soome 	if (sio != NULL) {
415612a8814cSTom Caputi 		int64_t asize = SIO_GET_ASIZE(sio);
4157a3874b8bSToomas Soome 		blkptr_t tmpbp;
41583f9d6ad7SLin Ling 
4159a3874b8bSToomas Soome 		/* Got it while it was cold in the queue */
416012a8814cSTom Caputi 		ASSERT3U(start, ==, SIO_GET_OFFSET(sio));
4161a3874b8bSToomas Soome 		ASSERT3U(size, ==, asize);
4162a3874b8bSToomas Soome 		avl_remove(&queue->q_sios_by_addr, sio);
416312a8814cSTom Caputi 		queue->q_sio_memused -= SIO_GET_MUSED(sio);
41641702cce7SAlek Pinchuk 
4165a3874b8bSToomas Soome 		ASSERT(range_tree_contains(queue->q_exts_by_addr, start, size));
4166a3874b8bSToomas Soome 		range_tree_remove_fill(queue->q_exts_by_addr, start, size);
41671702cce7SAlek Pinchuk 
4168a3874b8bSToomas Soome 		/*
4169a3874b8bSToomas Soome 		 * We only update scn_bytes_pending in the cold path,
4170a3874b8bSToomas Soome 		 * otherwise it will already have been accounted for as
4171a3874b8bSToomas Soome 		 * part of the zio's execution.
4172a3874b8bSToomas Soome 		 */
4173a3874b8bSToomas Soome 		atomic_add_64(&scn->scn_bytes_pending, -asize);
4174a3874b8bSToomas Soome 
4175a3874b8bSToomas Soome 		/* count the block as though we issued it */
417612a8814cSTom Caputi 		sio2bp(sio, &tmpbp);
4177a3874b8bSToomas Soome 		count_block(scn, dp->dp_blkstats, &tmpbp);
4178a3874b8bSToomas Soome 
417912a8814cSTom Caputi 		sio_free(sio);
4180a3874b8bSToomas Soome 	}
4181a3874b8bSToomas Soome 	mutex_exit(q_lock);
41823f9d6ad7SLin Ling }
41831825bc56SNav Ravindranath 
4184a3874b8bSToomas Soome /*
4185a3874b8bSToomas Soome  * Callback invoked when a zio_free() zio is executing. This needs to be
4186a3874b8bSToomas Soome  * intercepted to prevent the zio from deallocating a particular portion
4187a3874b8bSToomas Soome  * of disk space and it then getting reallocated and written to, while we
4188a3874b8bSToomas Soome  * still have it queued up for processing.
4189a3874b8bSToomas Soome  */
4190a3874b8bSToomas Soome void
4191a3874b8bSToomas Soome dsl_scan_freed(spa_t *spa, const blkptr_t *bp)
41921825bc56SNav Ravindranath {
4193a3874b8bSToomas Soome 	dsl_pool_t *dp = spa->spa_dsl_pool;
4194a3874b8bSToomas Soome 	dsl_scan_t *scn = dp->dp_scan;
4195a3874b8bSToomas Soome 
4196a3874b8bSToomas Soome 	ASSERT(!BP_IS_EMBEDDED(bp));
4197a3874b8bSToomas Soome 	ASSERT(scn != NULL);
4198a3874b8bSToomas Soome 	if (!dsl_scan_is_running(scn))
4199a3874b8bSToomas Soome 		return;
4200a3874b8bSToomas Soome 
4201a3874b8bSToomas Soome 	for (int i = 0; i < BP_GET_NDVAS(bp); i++)
4202a3874b8bSToomas Soome 		dsl_scan_freed_dva(spa, bp, i);
42031825bc56SNav Ravindranath }
42040c06d385Sjwpoduska 
42050c06d385Sjwpoduska /*
42060c06d385Sjwpoduska  * Check if a vdev needs resilvering (non-empty DTL), if so, and resilver has
42070c06d385Sjwpoduska  * not started, start it. Otherwise, only restart if max txg in DTL range is
42080c06d385Sjwpoduska  * greater than the max txg in the current scan. If the DTL max is less than
42090c06d385Sjwpoduska  * the scan max, then the vdev has not missed any new data since the resilver
42100c06d385Sjwpoduska  * started, so a restart is not needed.
42110c06d385Sjwpoduska  */
42120c06d385Sjwpoduska void
42130c06d385Sjwpoduska dsl_scan_assess_vdev(dsl_pool_t *dp, vdev_t *vd)
42140c06d385Sjwpoduska {
42150c06d385Sjwpoduska 	uint64_t min, max;
42160c06d385Sjwpoduska 
42170c06d385Sjwpoduska 	if (!vdev_resilver_needed(vd, &min, &max))
42180c06d385Sjwpoduska 		return;
42190c06d385Sjwpoduska 
42200c06d385Sjwpoduska 	if (!dsl_scan_resilvering(dp)) {
42210c06d385Sjwpoduska 		spa_async_request(dp->dp_spa, SPA_ASYNC_RESILVER);
42220c06d385Sjwpoduska 		return;
42230c06d385Sjwpoduska 	}
42240c06d385Sjwpoduska 
42250c06d385Sjwpoduska 	if (max <= dp->dp_scan->scn_phys.scn_max_txg)
42260c06d385Sjwpoduska 		return;
42270c06d385Sjwpoduska 
42280c06d385Sjwpoduska 	/* restart is needed, check if it can be deferred */
42290c06d385Sjwpoduska 	if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))
42300c06d385Sjwpoduska 		vdev_defer_resilver(vd);
42310c06d385Sjwpoduska 	else
42320c06d385Sjwpoduska 		spa_async_request(dp->dp_spa, SPA_ASYNC_RESILVER);
42330c06d385Sjwpoduska }
4234