1219089Spjd/*
2219089Spjd * CDDL HEADER START
3219089Spjd *
4219089Spjd * The contents of this file are subject to the terms of the
5219089Spjd * Common Development and Distribution License (the "License").
6219089Spjd * You may not use this file except in compliance with the License.
7219089Spjd *
8219089Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9219089Spjd * or http://www.opensolaris.org/os/licensing.
10219089Spjd * See the License for the specific language governing permissions
11219089Spjd * and limitations under the License.
12219089Spjd *
13219089Spjd * When distributing Covered Code, include this CDDL HEADER in each
14219089Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15219089Spjd * If applicable, add the following below this CDDL HEADER, with the
16219089Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17219089Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18219089Spjd *
19219089Spjd * CDDL HEADER END
20219089Spjd */
21219089Spjd/*
22219089Spjd * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23332525Smav * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24324010Savg * Copyright (c) 2017 Datto Inc.
25219089Spjd */
26219089Spjd
27219089Spjd#ifndef	_SYS_DSL_SCAN_H
28219089Spjd#define	_SYS_DSL_SCAN_H
29219089Spjd
30219089Spjd#include <sys/zfs_context.h>
31219089Spjd#include <sys/zio.h>
32219089Spjd#include <sys/ddt.h>
33219089Spjd#include <sys/bplist.h>
34219089Spjd
35219089Spjd#ifdef	__cplusplus
36219089Spjdextern "C" {
37219089Spjd#endif
38219089Spjd
39219089Spjdstruct objset;
40219089Spjdstruct dsl_dir;
41219089Spjdstruct dsl_dataset;
42219089Spjdstruct dsl_pool;
43219089Spjdstruct dmu_tx;
44219089Spjd
45219089Spjd/*
46219089Spjd * All members of this structure must be uint64_t, for byteswap
47219089Spjd * purposes.
48219089Spjd */
49219089Spjdtypedef struct dsl_scan_phys {
50219089Spjd	uint64_t scn_func; /* pool_scan_func_t */
51219089Spjd	uint64_t scn_state; /* dsl_scan_state_t */
52219089Spjd	uint64_t scn_queue_obj;
53219089Spjd	uint64_t scn_min_txg;
54219089Spjd	uint64_t scn_max_txg;
55219089Spjd	uint64_t scn_cur_min_txg;
56219089Spjd	uint64_t scn_cur_max_txg;
57219089Spjd	uint64_t scn_start_time;
58219089Spjd	uint64_t scn_end_time;
59219089Spjd	uint64_t scn_to_examine; /* total bytes to be scanned */
60219089Spjd	uint64_t scn_examined; /* bytes scanned so far */
61219089Spjd	uint64_t scn_to_process;
62219089Spjd	uint64_t scn_processed;
63219089Spjd	uint64_t scn_errors;	/* scan I/O error count */
64219089Spjd	uint64_t scn_ddt_class_max;
65219089Spjd	ddt_bookmark_t scn_ddt_bookmark;
66268123Sdelphij	zbookmark_phys_t scn_bookmark;
67219089Spjd	uint64_t scn_flags; /* dsl_scan_flags_t */
68219089Spjd} dsl_scan_phys_t;
69219089Spjd
70219089Spjd#define	SCAN_PHYS_NUMINTS (sizeof (dsl_scan_phys_t) / sizeof (uint64_t))
71219089Spjd
72219089Spjdtypedef enum dsl_scan_flags {
73219089Spjd	DSF_VISIT_DS_AGAIN = 1<<0,
74324010Savg	DSF_SCRUB_PAUSED = 1<<1,
75219089Spjd} dsl_scan_flags_t;
76219089Spjd
77254112Sdelphij/*
78254112Sdelphij * Every pool will have one dsl_scan_t and this structure will contain
79254112Sdelphij * in-memory information about the scan and a pointer to the on-disk
80254112Sdelphij * representation (i.e. dsl_scan_phys_t). Most of the state of the scan
81254112Sdelphij * is contained on-disk to allow the scan to resume in the event of a reboot
82254112Sdelphij * or panic. This structure maintains information about the behavior of a
83254112Sdelphij * running scan, some caching information, and how it should traverse the pool.
84254112Sdelphij *
85254112Sdelphij * The following members of this structure direct the behavior of the scan:
86254112Sdelphij *
87324010Savg * scn_suspending -	a scan that cannot be completed in a single txg or
88324010Savg *			has exceeded its allotted time will need to suspend.
89254112Sdelphij *			When this flag is set the scanner will stop traversing
90254112Sdelphij *			the pool and write out the current state to disk.
91254112Sdelphij *
92254112Sdelphij * scn_restart_txg -	directs the scanner to either restart or start a
93254112Sdelphij *			a scan at the specified txg value.
94254112Sdelphij *
95254112Sdelphij * scn_done_txg -	when a scan completes its traversal it will set
96254112Sdelphij *			the completion txg to the next txg. This is necessary
97254112Sdelphij *			to ensure that any blocks that were freed during
98254112Sdelphij *			the scan but have not yet been processed (i.e deferred
99254112Sdelphij *			frees) are accounted for.
100254112Sdelphij *
101254112Sdelphij * This structure also maintains information about deferred frees which are
102254112Sdelphij * a special kind of traversal. Deferred free can exist in either a bptree or
103254112Sdelphij * a bpobj structure. The scn_is_bptree flag will indicate the type of
104254112Sdelphij * deferred free that is in progress. If the deferred free is part of an
105254112Sdelphij * asynchronous destroy then the scn_async_destroying flag will be set.
106254112Sdelphij */
107219089Spjdtypedef struct dsl_scan {
108219089Spjd	struct dsl_pool *scn_dp;
109219089Spjd
110219089Spjd	uint64_t scn_restart_txg;
111254112Sdelphij	uint64_t scn_done_txg;
112219089Spjd	uint64_t scn_sync_start_time;
113339034Ssef	uint64_t scn_issued_before_pass;
114219089Spjd
115236884Smm	/* for freeing blocks */
116236884Smm	boolean_t scn_is_bptree;
117249858Smm	boolean_t scn_async_destroying;
118268079Sdelphij	boolean_t scn_async_stalled;
119332525Smav	uint64_t  scn_async_block_min_time_ms;
120339034Ssef	/* flags and stats for controlling scan state */
121339034Ssef	boolean_t scn_is_sorted;	/* doing sequential scan */
122339034Ssef	boolean_t scn_clearing;		/* scan is issuing sequential extents */
123339034Ssef	boolean_t scn_checkpointing;	/* scan is issuing all queued extents */
124339034Ssef	boolean_t scn_suspending;	/* scan is suspending until next txg */
125339034Ssef	uint64_t scn_last_checkpoint;	/* time of last checkpoint */
126332525Smav
127339034Ssef	/* members for thread synchronization */
128339034Ssef	zio_t *scn_zio_root;		/* root zio for waiting on IO */
129339034Ssef	taskq_t *scn_taskq;		/* task queue for issuing extents */
130219089Spjd
131339034Ssef	/* for controlling scan prefetch, protected by spa_scrub_lock */
132339034Ssef	boolean_t scn_prefetch_stop;	/* prefetch should stop */
133339034Ssef	zbookmark_phys_t scn_prefetch_bookmark;	/* prefetch start bookmark */
134339034Ssef	avl_tree_t scn_prefetch_queue;	/* priority queue of prefetch IOs */
135339034Ssef	uint64_t scn_maxinflight_bytes;	/* max bytes in flight for poool */
136339034Ssef
137339034Ssef	/* per txg statistics */
138339034Ssef	uint64_t scn_visited_this_txg;	/* total bps visited this txg */
139339034Ssef	uint64_t scn_holes_this_txg;
140339034Ssef	uint64_t scn_lt_min_this_txg;
141339034Ssef	uint64_t scn_gt_max_this_txg;
142339034Ssef	uint64_t scn_ddt_contained_this_txg;
143339034Ssef	uint64_t scn_objsets_visited_this_txg;
144339034Ssef	uint64_t scn_avg_seg_size_this_txg;
145339034Ssef	uint64_t scn_segs_this_txg;
146339034Ssef	uint64_t scn_avg_zio_size_this_txg;
147339034Ssef	uint64_t scn_zios_this_txg;
148339034Ssef
149339034Ssef	/* members needed for syncing scan status to disk */
150339034Ssef	dsl_scan_phys_t scn_phys;	/* on disk representation of scan */
151339034Ssef	dsl_scan_phys_t scn_phys_cached;
152339034Ssef	avl_tree_t scn_queue;		/* queue of datasets to scan */
153339034Ssef	uint64_t scn_bytes_pending;	/* outstanding data to issue */
154219089Spjd} dsl_scan_t;
155219089Spjd
156339034Sseftypedef struct dsl_scan_io_queue dsl_scan_io_queue_t;
157339034Ssef
158339034Ssefvoid dsl_scan_global_init(void);
159339034Ssef
160339034Ssefvoid scan_init(void);
161339034Ssefvoid scan_fini(void);
162219089Spjdint dsl_scan_init(struct dsl_pool *dp, uint64_t txg);
163219089Spjdvoid dsl_scan_fini(struct dsl_pool *dp);
164219089Spjdvoid dsl_scan_sync(struct dsl_pool *, dmu_tx_t *);
165219089Spjdint dsl_scan_cancel(struct dsl_pool *);
166219089Spjdint dsl_scan(struct dsl_pool *, pool_scan_func_t);
167324010Savgboolean_t dsl_scan_scrubbing(const struct dsl_pool *dp);
168324010Savgint dsl_scrub_set_pause_resume(const struct dsl_pool *dp, pool_scrub_cmd_t cmd);
169219089Spjdvoid dsl_resilver_restart(struct dsl_pool *, uint64_t txg);
170219089Spjdboolean_t dsl_scan_resilvering(struct dsl_pool *dp);
171219089Spjdboolean_t dsl_dataset_unstable(struct dsl_dataset *ds);
172219089Spjdvoid dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
173219089Spjd    ddt_entry_t *dde, dmu_tx_t *tx);
174219089Spjdvoid dsl_scan_ds_destroyed(struct dsl_dataset *ds, struct dmu_tx *tx);
175219089Spjdvoid dsl_scan_ds_snapshotted(struct dsl_dataset *ds, struct dmu_tx *tx);
176219089Spjdvoid dsl_scan_ds_clone_swapped(struct dsl_dataset *ds1, struct dsl_dataset *ds2,
177219089Spjd    struct dmu_tx *tx);
178219089Spjdboolean_t dsl_scan_active(dsl_scan_t *scn);
179324010Savgboolean_t dsl_scan_is_paused_scrub(const dsl_scan_t *scn);
180339034Ssefvoid dsl_scan_freed(spa_t *spa, const blkptr_t *bp);
181339034Ssefvoid dsl_scan_io_queue_destroy(dsl_scan_io_queue_t *queue);
182339034Ssefvoid dsl_scan_io_queue_vdev_xfer(vdev_t *svd, vdev_t *tvd);
183219089Spjd
184219089Spjd#ifdef	__cplusplus
185219089Spjd}
186219089Spjd#endif
187219089Spjd
188219089Spjd#endif /* _SYS_DSL_SCAN_H */
189