13f9d6adLin Ling/*
23f9d6adLin Ling * CDDL HEADER START
33f9d6adLin Ling *
43f9d6adLin Ling * The contents of this file are subject to the terms of the
53f9d6adLin Ling * Common Development and Distribution License (the "License").
63f9d6adLin Ling * You may not use this file except in compliance with the License.
73f9d6adLin Ling *
83f9d6adLin Ling * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93f9d6adLin Ling * or http://www.opensolaris.org/os/licensing.
103f9d6adLin Ling * See the License for the specific language governing permissions
113f9d6adLin Ling * and limitations under the License.
123f9d6adLin Ling *
133f9d6adLin Ling * When distributing Covered Code, include this CDDL HEADER in each
143f9d6adLin Ling * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153f9d6adLin Ling * If applicable, add the following below this CDDL HEADER, with the
163f9d6adLin Ling * fields enclosed by brackets "[]" replaced with your own identifying
173f9d6adLin Ling * information: Portions Copyright [yyyy] [name of copyright owner]
183f9d6adLin Ling *
193f9d6adLin Ling * CDDL HEADER END
203f9d6adLin Ling */
213f9d6adLin Ling/*
223f9d6adLin Ling * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
235cabbc6Prashanth Sreenivasa * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
241702cceAlek Pinchuk * Copyright (c) 2017 Datto Inc.
253f9d6adLin Ling */
263f9d6adLin Ling
273f9d6adLin Ling#ifndef	_SYS_DSL_SCAN_H
283f9d6adLin Ling#define	_SYS_DSL_SCAN_H
293f9d6adLin Ling
303f9d6adLin Ling#include <sys/zfs_context.h>
313f9d6adLin Ling#include <sys/zio.h>
323f9d6adLin Ling#include <sys/ddt.h>
333f9d6adLin Ling#include <sys/bplist.h>
343f9d6adLin Ling
353f9d6adLin Ling#ifdef	__cplusplus
363f9d6adLin Lingextern "C" {
373f9d6adLin Ling#endif
383f9d6adLin Ling
393f9d6adLin Lingstruct objset;
403f9d6adLin Lingstruct dsl_dir;
413f9d6adLin Lingstruct dsl_dataset;
423f9d6adLin Lingstruct dsl_pool;
433f9d6adLin Lingstruct dmu_tx;
443f9d6adLin Ling
453f9d6adLin Ling/*
463f9d6adLin Ling * All members of this structure must be uint64_t, for byteswap
473f9d6adLin Ling * purposes.
483f9d6adLin Ling */
493f9d6adLin Lingtypedef struct dsl_scan_phys {
503f9d6adLin Ling	uint64_t scn_func; /* pool_scan_func_t */
513f9d6adLin Ling	uint64_t scn_state; /* dsl_scan_state_t */
523f9d6adLin Ling	uint64_t scn_queue_obj;
533f9d6adLin Ling	uint64_t scn_min_txg;
543f9d6adLin Ling	uint64_t scn_max_txg;
553f9d6adLin Ling	uint64_t scn_cur_min_txg;
563f9d6adLin Ling	uint64_t scn_cur_max_txg;
573f9d6adLin Ling	uint64_t scn_start_time;
583f9d6adLin Ling	uint64_t scn_end_time;
593f9d6adLin Ling	uint64_t scn_to_examine; /* total bytes to be scanned */
603f9d6adLin Ling	uint64_t scn_examined; /* bytes scanned so far */
613f9d6adLin Ling	uint64_t scn_to_process;
623f9d6adLin Ling	uint64_t scn_processed;
633f9d6adLin Ling	uint64_t scn_errors;	/* scan I/O error count */
643f9d6adLin Ling	uint64_t scn_ddt_class_max;
653f9d6adLin Ling	ddt_bookmark_t scn_ddt_bookmark;
667802d7bMatthew Ahrens	zbookmark_phys_t scn_bookmark;
673f9d6adLin Ling	uint64_t scn_flags; /* dsl_scan_flags_t */
683f9d6adLin Ling} dsl_scan_phys_t;
693f9d6adLin Ling
703f9d6adLin Ling#define	SCAN_PHYS_NUMINTS (sizeof (dsl_scan_phys_t) / sizeof (uint64_t))
713f9d6adLin Ling
723f9d6adLin Lingtypedef enum dsl_scan_flags {
733f9d6adLin Ling	DSF_VISIT_DS_AGAIN = 1<<0,
741702cceAlek Pinchuk	DSF_SCRUB_PAUSED = 1<<1,
753f9d6adLin Ling} dsl_scan_flags_t;
763f9d6adLin Ling
77a3874b8Toomas Soome#define	DSL_SCAN_FLAGS_MASK (DSF_VISIT_DS_AGAIN)
78a3874b8Toomas Soome
79b4952e1George Wilson/*
80b4952e1George Wilson * Every pool will have one dsl_scan_t and this structure will contain
81b4952e1George Wilson * in-memory information about the scan and a pointer to the on-disk
82b4952e1George Wilson * representation (i.e. dsl_scan_phys_t). Most of the state of the scan
83b4952e1George Wilson * is contained on-disk to allow the scan to resume in the event of a reboot
84b4952e1George Wilson * or panic. This structure maintains information about the behavior of a
85b4952e1George Wilson * running scan, some caching information, and how it should traverse the pool.
86b4952e1George Wilson *
87b4952e1George Wilson * The following members of this structure direct the behavior of the scan:
88b4952e1George Wilson *
891702cceAlek Pinchuk * scn_suspending -	a scan that cannot be completed in a single txg or
901702cceAlek Pinchuk *			has exceeded its allotted time will need to suspend.
91b4952e1George Wilson *			When this flag is set the scanner will stop traversing
92b4952e1George Wilson *			the pool and write out the current state to disk.
93b4952e1George Wilson *
94b4952e1George Wilson * scn_restart_txg -	directs the scanner to either restart or start a
95b4952e1George Wilson *			a scan at the specified txg value.
96b4952e1George Wilson *
97b4952e1George Wilson * scn_done_txg -	when a scan completes its traversal it will set
98b4952e1George Wilson *			the completion txg to the next txg. This is necessary
99b4952e1George Wilson *			to ensure that any blocks that were freed during
100b4952e1George Wilson *			the scan but have not yet been processed (i.e deferred
101b4952e1George Wilson *			frees) are accounted for.
102b4952e1George Wilson *
103b4952e1George Wilson * This structure also maintains information about deferred frees which are
104b4952e1George Wilson * a special kind of traversal. Deferred free can exist in either a bptree or
105b4952e1George Wilson * a bpobj structure. The scn_is_bptree flag will indicate the type of
106b4952e1George Wilson * deferred free that is in progress. If the deferred free is part of an
107b4952e1George Wilson * asynchronous destroy then the scn_async_destroying flag will be set.
108b4952e1George Wilson */
1093f9d6adLin Lingtypedef struct dsl_scan {
1103f9d6adLin Ling	struct dsl_pool *scn_dp;
1113f9d6adLin Ling	uint64_t scn_restart_txg;
112b4952e1George Wilson	uint64_t scn_done_txg;
1133f9d6adLin Ling	uint64_t scn_sync_start_time;
114a3874b8Toomas Soome	uint64_t scn_issued_before_pass;
1153f9d6adLin Ling
116ad135b5Christopher Siden	/* for freeing blocks */
117ad135b5Christopher Siden	boolean_t scn_is_bptree;
1184a92375George Wilson	boolean_t scn_async_destroying;
1197fd05acMatthew Ahrens	boolean_t scn_async_stalled;
1205cabbc6Prashanth Sreenivasa	uint64_t  scn_async_block_min_time_ms;
1215cabbc6Prashanth Sreenivasa
122a3874b8Toomas Soome	/* flags and stats for controlling scan state */
123a3874b8Toomas Soome	boolean_t scn_is_sorted;	/* doing sequential scan */
124a3874b8Toomas Soome	boolean_t scn_clearing;		/* scan is issuing sequential extents */
125a3874b8Toomas Soome	boolean_t scn_checkpointing;	/* scan is issuing all queued extents */
126a3874b8Toomas Soome	boolean_t scn_suspending;	/* scan is suspending until next txg */
127a3874b8Toomas Soome	uint64_t scn_last_checkpoint;	/* time of last checkpoint */
128a3874b8Toomas Soome
129a3874b8Toomas Soome	/* members for thread synchronization */
130a3874b8Toomas Soome	zio_t *scn_zio_root;		/* root zio for waiting on IO */
131a3874b8Toomas Soome	taskq_t *scn_taskq;		/* task queue for issuing extents */
1323f9d6adLin Ling
133a3874b8Toomas Soome	/* for controlling scan prefetch, protected by spa_scrub_lock */
134a3874b8Toomas Soome	boolean_t scn_prefetch_stop;	/* prefetch should stop */
135a3874b8Toomas Soome	zbookmark_phys_t scn_prefetch_bookmark;	/* prefetch start bookmark */
136a3874b8Toomas Soome	avl_tree_t scn_prefetch_queue;	/* priority queue of prefetch IOs */
137a3874b8Toomas Soome	uint64_t scn_maxinflight_bytes;	/* max bytes in flight for pool */
138a3874b8Toomas Soome
139a3874b8Toomas Soome	/* per txg statistics */
140a3874b8Toomas Soome	uint64_t scn_visited_this_txg;	/* total bps visited this txg */
141a3874b8Toomas Soome	uint64_t scn_holes_this_txg;
142a3874b8Toomas Soome	uint64_t scn_lt_min_this_txg;
143a3874b8Toomas Soome	uint64_t scn_gt_max_this_txg;
144a3874b8Toomas Soome	uint64_t scn_ddt_contained_this_txg;
145a3874b8Toomas Soome	uint64_t scn_objsets_visited_this_txg;
146a3874b8Toomas Soome	uint64_t scn_avg_seg_size_this_txg;
147a3874b8Toomas Soome	uint64_t scn_segs_this_txg;
148a3874b8Toomas Soome	uint64_t scn_avg_zio_size_this_txg;
149a3874b8Toomas Soome	uint64_t scn_zios_this_txg;
150a3874b8Toomas Soome
151a3874b8Toomas Soome	/* members needed for syncing scan status to disk */
152a3874b8Toomas Soome	dsl_scan_phys_t scn_phys;	/* on disk representation of scan */
153a3874b8Toomas Soome	dsl_scan_phys_t scn_phys_cached;
154a3874b8Toomas Soome	avl_tree_t scn_queue;		/* queue of datasets to scan */
155a3874b8Toomas Soome	uint64_t scn_bytes_pending;	/* outstanding data to issue */
1563f9d6adLin Ling} dsl_scan_t;
1573f9d6adLin Ling
158a3874b8Toomas Soometypedef struct dsl_scan_io_queue dsl_scan_io_queue_t;
159a3874b8Toomas Soome
160a3874b8Toomas Soomevoid scan_init(void);
161a3874b8Toomas Soomevoid scan_fini(void);
1623f9d6adLin Lingint dsl_scan_init(struct dsl_pool *dp, uint64_t txg);
1633f9d6adLin Lingvoid dsl_scan_fini(struct dsl_pool *dp);
1643f9d6adLin Lingvoid dsl_scan_sync(struct dsl_pool *, dmu_tx_t *);
1653f9d6adLin Lingint dsl_scan_cancel(struct dsl_pool *);
1663f9d6adLin Lingint dsl_scan(struct dsl_pool *, pool_scan_func_t);
1671702cceAlek Pinchukboolean_t dsl_scan_scrubbing(const struct dsl_pool *dp);
1681702cceAlek Pinchukint dsl_scrub_set_pause_resume(const struct dsl_pool *dp, pool_scrub_cmd_t cmd);
1693f9d6adLin Lingvoid dsl_resilver_restart(struct dsl_pool *, uint64_t txg);
1703f9d6adLin Lingboolean_t dsl_scan_resilvering(struct dsl_pool *dp);
1713f9d6adLin Lingboolean_t dsl_dataset_unstable(struct dsl_dataset *ds);
1723f9d6adLin Lingvoid dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1733f9d6adLin Ling    ddt_entry_t *dde, dmu_tx_t *tx);
1743f9d6adLin Lingvoid dsl_scan_ds_destroyed(struct dsl_dataset *ds, struct dmu_tx *tx);
1753f9d6adLin Lingvoid dsl_scan_ds_snapshotted(struct dsl_dataset *ds, struct dmu_tx *tx);
1763f9d6adLin Lingvoid dsl_scan_ds_clone_swapped(struct dsl_dataset *ds1, struct dsl_dataset *ds2,
1773f9d6adLin Ling    struct dmu_tx *tx);
178cde58dbMatthew Ahrensboolean_t dsl_scan_active(dsl_scan_t *scn);
1791702cceAlek Pinchukboolean_t dsl_scan_is_paused_scrub(const dsl_scan_t *scn);
180a3874b8Toomas Soomevoid dsl_scan_freed(spa_t *spa, const blkptr_t *bp);
181a3874b8Toomas Soomevoid dsl_scan_io_queue_destroy(dsl_scan_io_queue_t *queue);
182a3874b8Toomas Soomevoid dsl_scan_io_queue_vdev_xfer(vdev_t *svd, vdev_t *tvd);
1833f9d6adLin Ling
1843f9d6adLin Ling#ifdef	__cplusplus
1853f9d6adLin Ling}
1863f9d6adLin Ling#endif
1873f9d6adLin Ling
1883f9d6adLin Ling#endif /* _SYS_DSL_SCAN_H */
189