1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2298d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23c4ecba8aSPaul Dagnelie  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
24663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
25084fd14fSBrian Behlendorf  * Copyright 2019 Joyent, Inc.
2630c304d9SJoshua M. Clulow  * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #ifndef _SYS_VDEV_IMPL_H
30fa9e4066Sahrens #define	_SYS_VDEV_IMPL_H
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <sys/avl.h>
335cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h>
34fa9e4066Sahrens #include <sys/dmu.h>
35fa9e4066Sahrens #include <sys/metaslab.h>
36fa9e4066Sahrens #include <sys/nvpair.h>
37fa9e4066Sahrens #include <sys/space_map.h>
38fa9e4066Sahrens #include <sys/vdev.h>
39fa9e4066Sahrens #include <sys/dkio.h>
40fa9e4066Sahrens #include <sys/uberblock_impl.h>
415cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_mapping.h>
425cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_births.h>
435cabbc6bSPrashanth Sreenivasa #include <sys/vdev_removal.h>
44fa9e4066Sahrens 
45fa9e4066Sahrens #ifdef	__cplusplus
46fa9e4066Sahrens extern "C" {
47fa9e4066Sahrens #endif
48fa9e4066Sahrens 
49fa9e4066Sahrens /*
50fa9e4066Sahrens  * Virtual device descriptors.
51fa9e4066Sahrens  *
52fa9e4066Sahrens  * All storage pool operations go through the virtual device framework,
53fa9e4066Sahrens  * which provides data replication and I/O scheduling.
54fa9e4066Sahrens  */
55fa9e4066Sahrens 
56fa9e4066Sahrens /*
57fa9e4066Sahrens  * Forward declarations that lots of things need.
58fa9e4066Sahrens  */
59fa9e4066Sahrens typedef struct vdev_queue vdev_queue_t;
60fa9e4066Sahrens typedef struct vdev_cache vdev_cache_t;
61fa9e4066Sahrens typedef struct vdev_cache_entry vdev_cache_entry_t;
62770499e1SDan Kimmel struct abd;
63fa9e4066Sahrens 
640f7643c7SGeorge Wilson extern int zfs_vdev_queue_depth_pct;
65f78cdc34SPaul Dagnelie extern int zfs_vdev_def_queue_depth;
660f7643c7SGeorge Wilson extern uint32_t zfs_vdev_async_write_max_active;
670f7643c7SGeorge Wilson 
68fa9e4066Sahrens /*
69fa9e4066Sahrens  * Virtual device operations
70fa9e4066Sahrens  */
714263d13fSGeorge Wilson typedef int	vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *max_size,
724263d13fSGeorge Wilson     uint64_t *ashift);
73fa9e4066Sahrens typedef void	vdev_close_func_t(vdev_t *vd);
74fa9e4066Sahrens typedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize);
75738f37bcSGeorge Wilson typedef void	vdev_io_start_func_t(zio_t *zio);
76e14bb325SJeff Bonwick typedef void	vdev_io_done_func_t(zio_t *zio);
77fa9e4066Sahrens typedef void	vdev_state_change_func_t(vdev_t *vd, int, int);
78a3874b8bSToomas Soome typedef boolean_t vdev_need_resilver_func_t(vdev_t *vd, uint64_t, size_t);
79dcba9f3fSGeorge Wilson typedef void	vdev_hold_func_t(vdev_t *vd);
80dcba9f3fSGeorge Wilson typedef void	vdev_rele_func_t(vdev_t *vd);
81fa9e4066Sahrens 
825cabbc6bSPrashanth Sreenivasa typedef void	vdev_remap_cb_t(uint64_t inner_offset, vdev_t *vd,
835cabbc6bSPrashanth Sreenivasa     uint64_t offset, uint64_t size, void *arg);
845cabbc6bSPrashanth Sreenivasa typedef void	vdev_remap_func_t(vdev_t *vd, uint64_t offset, uint64_t size,
855cabbc6bSPrashanth Sreenivasa     vdev_remap_cb_t callback, void *arg);
86ac04831dSMike Gerdts typedef int	vdev_dumpio_func_t(vdev_t *vd, caddr_t data, size_t size,
87ac04831dSMike Gerdts     uint64_t offset, uint64_t origoffset, boolean_t doread, boolean_t isdump);
88094e47e9SGeorge Wilson /*
89094e47e9SGeorge Wilson  * Given a target vdev, translates the logical range "in" to the physical
90094e47e9SGeorge Wilson  * range "res"
91094e47e9SGeorge Wilson  */
924d7988d6SPaul Dagnelie typedef void vdev_xlation_func_t(vdev_t *cvd, const range_seg64_t *in,
934d7988d6SPaul Dagnelie     range_seg64_t *res);
945cabbc6bSPrashanth Sreenivasa 
95fa9e4066Sahrens typedef struct vdev_ops {
96fa9e4066Sahrens 	vdev_open_func_t		*vdev_op_open;
97fa9e4066Sahrens 	vdev_close_func_t		*vdev_op_close;
98fa9e4066Sahrens 	vdev_asize_func_t		*vdev_op_asize;
99fa9e4066Sahrens 	vdev_io_start_func_t		*vdev_op_io_start;
100fa9e4066Sahrens 	vdev_io_done_func_t		*vdev_op_io_done;
101fa9e4066Sahrens 	vdev_state_change_func_t	*vdev_op_state_change;
102a3874b8bSToomas Soome 	vdev_need_resilver_func_t	*vdev_op_need_resilver;
103dcba9f3fSGeorge Wilson 	vdev_hold_func_t		*vdev_op_hold;
104dcba9f3fSGeorge Wilson 	vdev_rele_func_t		*vdev_op_rele;
1055cabbc6bSPrashanth Sreenivasa 	vdev_remap_func_t		*vdev_op_remap;
106094e47e9SGeorge Wilson 	/*
107094e47e9SGeorge Wilson 	 * For translating ranges from non-leaf vdevs (e.g. raidz) to leaves.
108094e47e9SGeorge Wilson 	 * Used when initializing vdevs. Isn't used by leaf ops.
109094e47e9SGeorge Wilson 	 */
110094e47e9SGeorge Wilson 	vdev_xlation_func_t		*vdev_op_xlate;
111ac04831dSMike Gerdts 	vdev_dumpio_func_t		*vdev_op_dumpio;
112fa9e4066Sahrens 	char				vdev_op_type[16];
113fa9e4066Sahrens 	boolean_t			vdev_op_leaf;
114fa9e4066Sahrens } vdev_ops_t;
115fa9e4066Sahrens 
116fa9e4066Sahrens /*
117fa9e4066Sahrens  * Virtual device properties
118fa9e4066Sahrens  */
119fa9e4066Sahrens struct vdev_cache_entry {
120770499e1SDan Kimmel 	struct abd	*ve_abd;
121fa9e4066Sahrens 	uint64_t	ve_offset;
122fa9e4066Sahrens 	uint64_t	ve_lastused;
123fa9e4066Sahrens 	avl_node_t	ve_offset_node;
124fa9e4066Sahrens 	avl_node_t	ve_lastused_node;
125fa9e4066Sahrens 	uint32_t	ve_hits;
126fa9e4066Sahrens 	uint16_t	ve_missed_update;
127fa9e4066Sahrens 	zio_t		*ve_fill_io;
128fa9e4066Sahrens };
129fa9e4066Sahrens 
130fa9e4066Sahrens struct vdev_cache {
131fa9e4066Sahrens 	avl_tree_t	vc_offset_tree;
132fa9e4066Sahrens 	avl_tree_t	vc_lastused_tree;
133fa9e4066Sahrens 	kmutex_t	vc_lock;
134fa9e4066Sahrens };
135fa9e4066Sahrens 
13669962b56SMatthew Ahrens typedef struct vdev_queue_class {
13769962b56SMatthew Ahrens 	uint32_t	vqc_active;
13869962b56SMatthew Ahrens 
13969962b56SMatthew Ahrens 	/*
14069962b56SMatthew Ahrens 	 * Sorted by offset or timestamp, depending on if the queue is
14169962b56SMatthew Ahrens 	 * LBA-ordered vs FIFO.
14269962b56SMatthew Ahrens 	 */
14369962b56SMatthew Ahrens 	avl_tree_t	vqc_queued_tree;
14469962b56SMatthew Ahrens } vdev_queue_class_t;
14569962b56SMatthew Ahrens 
146fa9e4066Sahrens struct vdev_queue {
14769962b56SMatthew Ahrens 	vdev_t		*vq_vdev;
14869962b56SMatthew Ahrens 	vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
14969962b56SMatthew Ahrens 	avl_tree_t	vq_active_tree;
150fe319232SJustin T. Gibbs 	avl_tree_t	vq_read_offset_tree;
151fe319232SJustin T. Gibbs 	avl_tree_t	vq_write_offset_tree;
152084fd14fSBrian Behlendorf 	avl_tree_t	vq_trim_offset_tree;
15369962b56SMatthew Ahrens 	uint64_t	vq_last_offset;
15469962b56SMatthew Ahrens 	hrtime_t	vq_io_complete_ts; /* time last i/o completed */
155fa9e4066Sahrens 	kmutex_t	vq_lock;
156fa9e4066Sahrens };
157fa9e4066Sahrens 
158663207adSDon Brady typedef enum vdev_alloc_bias {
159663207adSDon Brady 	VDEV_BIAS_NONE,
160663207adSDon Brady 	VDEV_BIAS_LOG,		/* dedicated to ZIL data (SLOG) */
161663207adSDon Brady 	VDEV_BIAS_SPECIAL,	/* dedicated to ddt, metadata, and small blks */
162663207adSDon Brady 	VDEV_BIAS_DEDUP		/* dedicated to dedup metadata */
163663207adSDon Brady } vdev_alloc_bias_t;
164663207adSDon Brady 
165663207adSDon Brady 
1665cabbc6bSPrashanth Sreenivasa /*
1675cabbc6bSPrashanth Sreenivasa  * On-disk indirect vdev state.
1685cabbc6bSPrashanth Sreenivasa  *
1695cabbc6bSPrashanth Sreenivasa  * An indirect vdev is described exclusively in the MOS config of a pool.
1705cabbc6bSPrashanth Sreenivasa  * The config for an indirect vdev includes several fields, which are
1715cabbc6bSPrashanth Sreenivasa  * accessed in memory by a vdev_indirect_config_t.
1725cabbc6bSPrashanth Sreenivasa  */
1735cabbc6bSPrashanth Sreenivasa typedef struct vdev_indirect_config {
1745cabbc6bSPrashanth Sreenivasa 	/*
1755cabbc6bSPrashanth Sreenivasa 	 * Object (in MOS) which contains the indirect mapping. This object
1765cabbc6bSPrashanth Sreenivasa 	 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
1775cabbc6bSPrashanth Sreenivasa 	 * vimep_src. The bonus buffer for this object is a
1785cabbc6bSPrashanth Sreenivasa 	 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
1795cabbc6bSPrashanth Sreenivasa 	 * removal is initiated.
1805cabbc6bSPrashanth Sreenivasa 	 *
1815cabbc6bSPrashanth Sreenivasa 	 * Note that this object can be empty if none of the data on the vdev
1825cabbc6bSPrashanth Sreenivasa 	 * has been copied yet.
1835cabbc6bSPrashanth Sreenivasa 	 */
1845cabbc6bSPrashanth Sreenivasa 	uint64_t	vic_mapping_object;
1855cabbc6bSPrashanth Sreenivasa 
1865cabbc6bSPrashanth Sreenivasa 	/*
1875cabbc6bSPrashanth Sreenivasa 	 * Object (in MOS) which contains the birth times for the mapping
1885cabbc6bSPrashanth Sreenivasa 	 * entries. This object contains an array of
1895cabbc6bSPrashanth Sreenivasa 	 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
1905cabbc6bSPrashanth Sreenivasa 	 * buffer for this object is a vdev_indirect_birth_phys_t. This object
1915cabbc6bSPrashanth Sreenivasa 	 * is allocated when a vdev removal is initiated.
1925cabbc6bSPrashanth Sreenivasa 	 *
1935cabbc6bSPrashanth Sreenivasa 	 * Note that this object can be empty if none of the vdev has yet been
1945cabbc6bSPrashanth Sreenivasa 	 * copied.
1955cabbc6bSPrashanth Sreenivasa 	 */
1965cabbc6bSPrashanth Sreenivasa 	uint64_t	vic_births_object;
1975cabbc6bSPrashanth Sreenivasa 
1985cabbc6bSPrashanth Sreenivasa 	/*
1995cabbc6bSPrashanth Sreenivasa 	 * This is the vdev ID which was removed previous to this vdev, or
2005cabbc6bSPrashanth Sreenivasa 	 * UINT64_MAX if there are no previously removed vdevs.
2015cabbc6bSPrashanth Sreenivasa 	 */
2025cabbc6bSPrashanth Sreenivasa 	uint64_t	vic_prev_indirect_vdev;
2035cabbc6bSPrashanth Sreenivasa } vdev_indirect_config_t;
2045cabbc6bSPrashanth Sreenivasa 
205fa9e4066Sahrens /*
206fa9e4066Sahrens  * Virtual device descriptor
207fa9e4066Sahrens  */
208fa9e4066Sahrens struct vdev {
209fa9e4066Sahrens 	/*
210fa9e4066Sahrens 	 * Common to all vdev types.
211fa9e4066Sahrens 	 */
212fa9e4066Sahrens 	uint64_t	vdev_id;	/* child number in vdev parent	*/
213fa9e4066Sahrens 	uint64_t	vdev_guid;	/* unique ID for this vdev	*/
214fa9e4066Sahrens 	uint64_t	vdev_guid_sum;	/* self guid + all child guids	*/
2151195e687SMark J Musante 	uint64_t	vdev_orig_guid;	/* orig. guid prior to remove	*/
216fa9e4066Sahrens 	uint64_t	vdev_asize;	/* allocatable device capacity	*/
217573ca77eSGeorge Wilson 	uint64_t	vdev_min_asize;	/* min acceptable asize		*/
2184263d13fSGeorge Wilson 	uint64_t	vdev_max_asize;	/* max acceptable asize		*/
219fa9e4066Sahrens 	uint64_t	vdev_ashift;	/* block alignment shift	*/
220fa9e4066Sahrens 	uint64_t	vdev_state;	/* see VDEV_STATE_* #defines	*/
221560e6e96Seschrock 	uint64_t	vdev_prevstate;	/* used when reopening a vdev	*/
222fa9e4066Sahrens 	vdev_ops_t	*vdev_ops;	/* vdev operations		*/
223fa9e4066Sahrens 	spa_t		*vdev_spa;	/* spa for this vdev		*/
224fa9e4066Sahrens 	void		*vdev_tsd;	/* type-specific data		*/
225dcba9f3fSGeorge Wilson 	vnode_t		*vdev_name_vp;	/* vnode for pathname		*/
226dcba9f3fSGeorge Wilson 	vnode_t		*vdev_devid_vp;	/* vnode for devid		*/
227fa9e4066Sahrens 	vdev_t		*vdev_top;	/* top-level vdev		*/
228fa9e4066Sahrens 	vdev_t		*vdev_parent;	/* parent vdev			*/
229fa9e4066Sahrens 	vdev_t		**vdev_child;	/* array of children		*/
230fa9e4066Sahrens 	uint64_t	vdev_children;	/* number of children		*/
231fa9e4066Sahrens 	vdev_stat_t	vdev_stat;	/* virtual device statistics	*/
232dd50e0ccSTony Hutter 	vdev_stat_ex_t	vdev_stat_ex;	/* extended statistics		*/
233573ca77eSGeorge Wilson 	boolean_t	vdev_expanding;	/* expand the vdev?		*/
234095bcd66SGeorge Wilson 	boolean_t	vdev_reopening;	/* reopen in progress?		*/
23512a8814cSTom Caputi 	boolean_t	vdev_nonrot;	/* true if solid state		*/
236f64c0e34SEric Taylor 	int		vdev_open_error; /* error on last open		*/
237f64c0e34SEric Taylor 	kthread_t	*vdev_open_thread; /* thread opening children	*/
23888ecc943SGeorge Wilson 	uint64_t	vdev_crtxg;	/* txg when top-level was added */
239fa9e4066Sahrens 
240fa9e4066Sahrens 	/*
241fa9e4066Sahrens 	 * Top-level vdev state.
242fa9e4066Sahrens 	 */
243fa9e4066Sahrens 	uint64_t	vdev_ms_array;	/* metaslab array object	*/
244fa9e4066Sahrens 	uint64_t	vdev_ms_shift;	/* metaslab size shift		*/
245fa9e4066Sahrens 	uint64_t	vdev_ms_count;	/* number of metaslabs		*/
246fa9e4066Sahrens 	metaslab_group_t *vdev_mg;	/* metaslab group		*/
247fa9e4066Sahrens 	metaslab_t	**vdev_ms;	/* metaslab array		*/
248fa9e4066Sahrens 	txg_list_t	vdev_ms_list;	/* per-txg dirty metaslab lists	*/
249fa9e4066Sahrens 	txg_list_t	vdev_dtl_list;	/* per-txg dirty DTL lists	*/
250fa9e4066Sahrens 	txg_node_t	vdev_txg_node;	/* per-txg dirty vdev linkage	*/
2513d7072f8Seschrock 	boolean_t	vdev_remove_wanted; /* async remove wanted?	*/
252e14bb325SJeff Bonwick 	boolean_t	vdev_probe_wanted; /* async probe wanted?	*/
253e14bb325SJeff Bonwick 	list_node_t	vdev_config_dirty_node; /* config dirty list	*/
254e14bb325SJeff Bonwick 	list_node_t	vdev_state_dirty_node; /* state dirty list	*/
25599653d4eSeschrock 	uint64_t	vdev_deflate_ratio; /* deflation ratio (x512)	*/
2568654d025Sperrin 	uint64_t	vdev_islog;	/* is an intent log device	*/
2570713e232SGeorge Wilson 	uint64_t	vdev_removing;	/* device is being removed?	*/
2580f7643c7SGeorge Wilson 	boolean_t	vdev_ishole;	/* is a hole in the namespace	*/
259215198a6SJoe Stein 	uint64_t	vdev_top_zap;
260663207adSDon Brady 	vdev_alloc_bias_t vdev_alloc_bias; /* metaslab allocation bias	*/
261fa9e4066Sahrens 
26286714001SSerapheim Dimitropoulos 	/* pool checkpoint related */
26386714001SSerapheim Dimitropoulos 	space_map_t	*vdev_checkpoint_sm;	/* contains reserved blocks */
264e0f1c0afSOlaf Faaland 
265084fd14fSBrian Behlendorf 	/* Initialize related */
266094e47e9SGeorge Wilson 	boolean_t	vdev_initialize_exit_wanted;
267094e47e9SGeorge Wilson 	vdev_initializing_state_t	vdev_initialize_state;
268084fd14fSBrian Behlendorf 	list_node_t	vdev_initialize_node;
269094e47e9SGeorge Wilson 	kthread_t	*vdev_initialize_thread;
270094e47e9SGeorge Wilson 	/* Protects vdev_initialize_thread and vdev_initialize_state. */
271094e47e9SGeorge Wilson 	kmutex_t	vdev_initialize_lock;
272094e47e9SGeorge Wilson 	kcondvar_t	vdev_initialize_cv;
273094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_offset[TXG_SIZE];
274094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_last_offset;
275094e47e9SGeorge Wilson 	range_tree_t	*vdev_initialize_tree;	/* valid while initializing */
276094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_bytes_est;
277094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_bytes_done;
278094e47e9SGeorge Wilson 	time_t		vdev_initialize_action_time;	/* start and end time */
279094e47e9SGeorge Wilson 
280084fd14fSBrian Behlendorf 	/* TRIM related */
281084fd14fSBrian Behlendorf 	boolean_t	vdev_trim_exit_wanted;
282084fd14fSBrian Behlendorf 	boolean_t	vdev_autotrim_exit_wanted;
283084fd14fSBrian Behlendorf 	vdev_trim_state_t	vdev_trim_state;
284084fd14fSBrian Behlendorf 	list_node_t	vdev_trim_node;
285084fd14fSBrian Behlendorf 	kmutex_t	vdev_autotrim_lock;
286084fd14fSBrian Behlendorf 	kcondvar_t	vdev_autotrim_cv;
287084fd14fSBrian Behlendorf 	kthread_t	*vdev_autotrim_thread;
288084fd14fSBrian Behlendorf 	/* Protects vdev_trim_thread and vdev_trim_state. */
289084fd14fSBrian Behlendorf 	kmutex_t	vdev_trim_lock;
290084fd14fSBrian Behlendorf 	kcondvar_t	vdev_trim_cv;
291084fd14fSBrian Behlendorf 	kthread_t	*vdev_trim_thread;
292084fd14fSBrian Behlendorf 	uint64_t	vdev_trim_offset[TXG_SIZE];
293084fd14fSBrian Behlendorf 	uint64_t	vdev_trim_last_offset;
294084fd14fSBrian Behlendorf 	uint64_t	vdev_trim_bytes_est;
295084fd14fSBrian Behlendorf 	uint64_t	vdev_trim_bytes_done;
296084fd14fSBrian Behlendorf 	uint64_t	vdev_trim_rate;		/* requested rate (bytes/sec) */
297084fd14fSBrian Behlendorf 	uint64_t	vdev_trim_partial;	/* requested partial TRIM */
298084fd14fSBrian Behlendorf 	uint64_t	vdev_trim_secure;	/* requested secure TRIM */
299084fd14fSBrian Behlendorf 	time_t		vdev_trim_action_time;	/* start and end time */
300084fd14fSBrian Behlendorf 
301084fd14fSBrian Behlendorf 	/* The following is not in ZoL, but used for auto-trim test progress */
302084fd14fSBrian Behlendorf 	uint64_t	vdev_autotrim_bytes_done;
303084fd14fSBrian Behlendorf 
304084fd14fSBrian Behlendorf 	/* for limiting outstanding I/Os (initialize and TRIM) */
305094e47e9SGeorge Wilson 	kmutex_t	vdev_initialize_io_lock;
306094e47e9SGeorge Wilson 	kcondvar_t	vdev_initialize_io_cv;
307094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_inflight;
308084fd14fSBrian Behlendorf 	kmutex_t	vdev_trim_io_lock;
309084fd14fSBrian Behlendorf 	kcondvar_t	vdev_trim_io_cv;
310084fd14fSBrian Behlendorf 	uint64_t	vdev_trim_inflight[2];
31186714001SSerapheim Dimitropoulos 
3125cabbc6bSPrashanth Sreenivasa 	/*
3135cabbc6bSPrashanth Sreenivasa 	 * Values stored in the config for an indirect or removing vdev.
3145cabbc6bSPrashanth Sreenivasa 	 */
3155cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t	vdev_indirect_config;
3165cabbc6bSPrashanth Sreenivasa 
3175cabbc6bSPrashanth Sreenivasa 	/*
3185cabbc6bSPrashanth Sreenivasa 	 * The vdev_indirect_rwlock protects the vdev_indirect_mapping
3195cabbc6bSPrashanth Sreenivasa 	 * pointer from changing on indirect vdevs (when it is condensed).
3205cabbc6bSPrashanth Sreenivasa 	 * Note that removing (not yet indirect) vdevs have different
3215cabbc6bSPrashanth Sreenivasa 	 * access patterns (the mapping is not accessed from open context,
3225cabbc6bSPrashanth Sreenivasa 	 * e.g. from zio_read) and locking strategy (e.g. svr_lock).
3235cabbc6bSPrashanth Sreenivasa 	 */
3245cabbc6bSPrashanth Sreenivasa 	krwlock_t vdev_indirect_rwlock;
3255cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vdev_indirect_mapping;
3265cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_t *vdev_indirect_births;
3275cabbc6bSPrashanth Sreenivasa 
3285cabbc6bSPrashanth Sreenivasa 	/*
3295cabbc6bSPrashanth Sreenivasa 	 * In memory data structures used to manage the obsolete sm, for
3305cabbc6bSPrashanth Sreenivasa 	 * indirect or removing vdevs.
3315cabbc6bSPrashanth Sreenivasa 	 *
3325cabbc6bSPrashanth Sreenivasa 	 * The vdev_obsolete_segments is the in-core record of the segments
3335cabbc6bSPrashanth Sreenivasa 	 * that are no longer referenced anywhere in the pool (due to
3345cabbc6bSPrashanth Sreenivasa 	 * being freed or remapped and not referenced by any snapshots).
3355cabbc6bSPrashanth Sreenivasa 	 * During a sync, segments are added to vdev_obsolete_segments
3365cabbc6bSPrashanth Sreenivasa 	 * via vdev_indirect_mark_obsolete(); at the end of each sync
3375cabbc6bSPrashanth Sreenivasa 	 * pass, this is appended to vdev_obsolete_sm via
3385cabbc6bSPrashanth Sreenivasa 	 * vdev_indirect_sync_obsolete().  The vdev_obsolete_lock
3395cabbc6bSPrashanth Sreenivasa 	 * protects against concurrent modifications of vdev_obsolete_segments
3405cabbc6bSPrashanth Sreenivasa 	 * from multiple zio threads.
3415cabbc6bSPrashanth Sreenivasa 	 */
3425cabbc6bSPrashanth Sreenivasa 	kmutex_t	vdev_obsolete_lock;
3435cabbc6bSPrashanth Sreenivasa 	range_tree_t	*vdev_obsolete_segments;
3445cabbc6bSPrashanth Sreenivasa 	space_map_t	*vdev_obsolete_sm;
3455cabbc6bSPrashanth Sreenivasa 
346a3874b8bSToomas Soome 	/*
347a3874b8bSToomas Soome 	 * Protects the vdev_scan_io_queue field itself as well as the
348a3874b8bSToomas Soome 	 * structure's contents (when present).
349a3874b8bSToomas Soome 	 */
350a3874b8bSToomas Soome 	kmutex_t	vdev_scan_io_queue_lock;
351a3874b8bSToomas Soome 	struct dsl_scan_io_queue	*vdev_scan_io_queue;
352a3874b8bSToomas Soome 
353fa9e4066Sahrens 	/*
354fa9e4066Sahrens 	 * Leaf vdev state.
355fa9e4066Sahrens 	 */
3560713e232SGeorge Wilson 	range_tree_t	*vdev_dtl[DTL_TYPES]; /* dirty time logs	*/
3570713e232SGeorge Wilson 	space_map_t	*vdev_dtl_sm;	/* dirty time log space map	*/
358fa9e4066Sahrens 	txg_node_t	vdev_dtl_node;	/* per-txg dirty DTL linkage	*/
3590713e232SGeorge Wilson 	uint64_t	vdev_dtl_object; /* DTL object			*/
3600713e232SGeorge Wilson 	uint64_t	vdev_psize;	/* physical device capacity	*/
361afefbcddSeschrock 	uint64_t	vdev_wholedisk;	/* true if this is a whole disk */
3623d7072f8Seschrock 	uint64_t	vdev_offline;	/* persistent offline state	*/
3633d7072f8Seschrock 	uint64_t	vdev_faulted;	/* persistent faulted state	*/
3643d7072f8Seschrock 	uint64_t	vdev_degraded;	/* persistent degraded state	*/
3653d7072f8Seschrock 	uint64_t	vdev_removed;	/* persistent removed state	*/
366b4952e17SGeorge Wilson 	uint64_t	vdev_resilver_txg; /* persistent resilvering state */
36799653d4eSeschrock 	uint64_t	vdev_nparity;	/* number of parity devices for raidz */
368fa9e4066Sahrens 	char		*vdev_path;	/* vdev path (if any)		*/
369fa9e4066Sahrens 	char		*vdev_devid;	/* vdev devid (if any)		*/
3703d7072f8Seschrock 	char		*vdev_physpath;	/* vdev device path (if any)	*/
3716809eb4eSEric Schrock 	char		*vdev_fru;	/* physical FRU location	*/
372e14bb325SJeff Bonwick 	uint64_t	vdev_not_present; /* not present during import	*/
373e14bb325SJeff Bonwick 	uint64_t	vdev_unspare;	/* unspare when resilvering done */
374e14bb325SJeff Bonwick 	boolean_t	vdev_nowritecache; /* true if flushwritecache failed */
375084fd14fSBrian Behlendorf 	boolean_t	vdev_has_trim;	/* TRIM is supported		*/
376084fd14fSBrian Behlendorf 	boolean_t	vdev_has_securetrim; /* secure TRIM is supported */
377e14bb325SJeff Bonwick 	boolean_t	vdev_checkremove; /* temporary online test	*/
378e14bb325SJeff Bonwick 	boolean_t	vdev_forcefault; /* force online fault		*/
3791195e687SMark J Musante 	boolean_t	vdev_splitting;	/* split or repair in progress  */
38098d1cbfeSGeorge Wilson 	boolean_t	vdev_delayed_close; /* delayed device close?	*/
3810713e232SGeorge Wilson 	boolean_t	vdev_tmpoffline; /* device taken offline temporarily? */
3820713e232SGeorge Wilson 	boolean_t	vdev_detached;	/* device detached?		*/
3830713e232SGeorge Wilson 	boolean_t	vdev_cant_read;	/* vdev is failing all reads	*/
3840713e232SGeorge Wilson 	boolean_t	vdev_cant_write; /* vdev is failing all writes	*/
3850713e232SGeorge Wilson 	boolean_t	vdev_isspare;	/* was a hot spare		*/
3860713e232SGeorge Wilson 	boolean_t	vdev_isl2cache;	/* was a l2cache device		*/
387e4c795beSTom Caputi 	boolean_t	vdev_resilver_deferred;  /* resilver deferred */
388fa9e4066Sahrens 	vdev_queue_t	vdev_queue;	/* I/O deadline schedule queue	*/
389fa9e4066Sahrens 	vdev_cache_t	vdev_cache;	/* physical block cache		*/
39081cd5c55SMatthew Ahrens 	spa_aux_vdev_t	*vdev_aux;	/* for l2cache and spares vdevs	*/
391e14bb325SJeff Bonwick 	zio_t		*vdev_probe_zio; /* root of current probe	*/
392069f55e2SEric Schrock 	vdev_aux_t	vdev_label_aux;	/* on-disk aux state		*/
393215198a6SJoe Stein 	uint64_t	vdev_leaf_zap;
394e0f1c0afSOlaf Faaland 	hrtime_t	vdev_mmp_pending; /* 0 if write finished	*/
395e0f1c0afSOlaf Faaland 	uint64_t	vdev_mmp_kstat_id;	/* to find kstat entry */
396e0f1c0afSOlaf Faaland 	list_node_t	vdev_leaf_node;		/* leaf vdev list */
397fa9e4066Sahrens 
398fa9e4066Sahrens 	/*
399fa9e4066Sahrens 	 * For DTrace to work in userland (libzpool) context, these fields must
400fa9e4066Sahrens 	 * remain at the end of the structure.  DTrace will use the kernel's
401fa9e4066Sahrens 	 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
402ad135b5dSChristopher Siden 	 * larger in userland, the offsets for the rest of the fields would be
403fa9e4066Sahrens 	 * incorrect.
404fa9e4066Sahrens 	 */
405fa9e4066Sahrens 	kmutex_t	vdev_dtl_lock;	/* vdev_dtl_{map,resilver}	*/
406fa9e4066Sahrens 	kmutex_t	vdev_stat_lock;	/* vdev_stat			*/
407e14bb325SJeff Bonwick 	kmutex_t	vdev_probe_lock; /* protects vdev_probe_zio	*/
408fa9e4066Sahrens };
409fa9e4066Sahrens 
410b24ab676SJeff Bonwick #define	VDEV_RAIDZ_MAXPARITY	3
411b24ab676SJeff Bonwick 
412f83ffe1aSLin Ling #define	VDEV_PAD_SIZE		(8 << 10)
413c4ecba8aSPaul Dagnelie /* 2 padding areas (vl_pad1 and vl_be) to skip */
414f83ffe1aSLin Ling #define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
415fa9e4066Sahrens #define	VDEV_PHYS_SIZE		(112 << 10)
416ecc2d604Sbonwick #define	VDEV_UBERBLOCK_RING	(128 << 10)
417ecc2d604Sbonwick 
418e0f1c0afSOlaf Faaland /*
419e0f1c0afSOlaf Faaland  * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
420e0f1c0afSOlaf Faaland  * ring when MMP is enabled.
421e0f1c0afSOlaf Faaland  */
422e0f1c0afSOlaf Faaland #define	MMP_BLOCKS_PER_LABEL	1
423e0f1c0afSOlaf Faaland 
4242a104a52SAlex Reece /* The largest uberblock we support is 8k. */
4252a104a52SAlex Reece #define	MAX_UBERBLOCK_SHIFT (13)
426ecc2d604Sbonwick #define	VDEV_UBERBLOCK_SHIFT(vd)	\
4272a104a52SAlex Reece 	MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
4282a104a52SAlex Reece 	    MAX_UBERBLOCK_SHIFT)
429ecc2d604Sbonwick #define	VDEV_UBERBLOCK_COUNT(vd)	\
430ecc2d604Sbonwick 	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
431ecc2d604Sbonwick #define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
432ecc2d604Sbonwick 	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
433ecc2d604Sbonwick #define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
434fa9e4066Sahrens 
435fa9e4066Sahrens typedef struct vdev_phys {
4366e1f5caaSNeil Perrin 	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
4376e1f5caaSNeil Perrin 	zio_eck_t	vp_zbt;
438fa9e4066Sahrens } vdev_phys_t;
439fa9e4066Sahrens 
440c4ecba8aSPaul Dagnelie typedef enum vbe_vers {
44109fcda9fSToomas Soome 	/*
44209fcda9fSToomas Soome 	 * The bootenv file is stored as ascii text in the envblock.
44309fcda9fSToomas Soome 	 * It is used by the GRUB bootloader used on Linux to store the
44409fcda9fSToomas Soome 	 * contents of the grubenv file. The file is stored as raw ASCII,
44509fcda9fSToomas Soome 	 * and is protected by an embedded checksum. By default, GRUB will
44609fcda9fSToomas Soome 	 * check if the boot filesystem supports storing the environment data
44709fcda9fSToomas Soome 	 * in a special location, and if so, will invoke filesystem specific
44809fcda9fSToomas Soome 	 * logic to retrieve it. This can be overriden by a variable, should
44909fcda9fSToomas Soome 	 * the user so desire.
45009fcda9fSToomas Soome 	 */
451c4ecba8aSPaul Dagnelie 	VB_RAW = 0,
452c4ecba8aSPaul Dagnelie 
453c4ecba8aSPaul Dagnelie 	/*
454c4ecba8aSPaul Dagnelie 	 * The bootenv file is converted to an nvlist and then packed into the
455c4ecba8aSPaul Dagnelie 	 * envblock.
456c4ecba8aSPaul Dagnelie 	 */
457c4ecba8aSPaul Dagnelie 	VB_NVLIST = 1
458c4ecba8aSPaul Dagnelie } vbe_vers_t;
459c4ecba8aSPaul Dagnelie 
460c4ecba8aSPaul Dagnelie typedef struct vdev_boot_envblock {
461c4ecba8aSPaul Dagnelie 	uint64_t	vbe_version;
462c4ecba8aSPaul Dagnelie 	char		vbe_bootenv[VDEV_PAD_SIZE - sizeof (uint64_t) -
463c4ecba8aSPaul Dagnelie 			sizeof (zio_eck_t)];
464c4ecba8aSPaul Dagnelie 	zio_eck_t	vbe_zbt;
465c4ecba8aSPaul Dagnelie } vdev_boot_envblock_t;
466c4ecba8aSPaul Dagnelie 
467c4ecba8aSPaul Dagnelie CTASSERT(sizeof (vdev_boot_envblock_t) == VDEV_PAD_SIZE);
468c4ecba8aSPaul Dagnelie 
469fa9e4066Sahrens typedef struct vdev_label {
470f83ffe1aSLin Ling 	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K */
471c4ecba8aSPaul Dagnelie 	vdev_boot_envblock_t	vl_be;				/*  8K */
472ecc2d604Sbonwick 	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
473ecc2d604Sbonwick 	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
474fa9e4066Sahrens } vdev_label_t;							/* 256K total */
475fa9e4066Sahrens 
476ecc2d604Sbonwick /*
477ecc2d604Sbonwick  * vdev_dirty() flags
478ecc2d604Sbonwick  */
479ecc2d604Sbonwick #define	VDD_METASLAB	0x01
480ecc2d604Sbonwick #define	VDD_DTL		0x02
481ecc2d604Sbonwick 
482f7170741SWill Andrews /* Offset of embedded boot loader region on each label */
483f7170741SWill Andrews #define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
484fa9e4066Sahrens /*
485f7170741SWill Andrews  * Size of embedded boot loader region on each label.
486fa9e4066Sahrens  * The total size of the first two labels plus the boot area is 4MB.
487fa9e4066Sahrens  */
488f7170741SWill Andrews #define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M */
489fa9e4066Sahrens 
490fa9e4066Sahrens /*
491fa9e4066Sahrens  * Size of label regions at the start and end of each leaf device.
492fa9e4066Sahrens  */
493fa9e4066Sahrens #define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
494fa9e4066Sahrens #define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
495fa9e4066Sahrens #define	VDEV_LABELS		4
496ad135b5dSChristopher Siden #define	VDEV_BEST_LABEL		VDEV_LABELS
497fa9e4066Sahrens 
498fa9e4066Sahrens #define	VDEV_ALLOC_LOAD		0
499fa9e4066Sahrens #define	VDEV_ALLOC_ADD		1
50099653d4eSeschrock #define	VDEV_ALLOC_SPARE	2
501fa94a07fSbrendan #define	VDEV_ALLOC_L2CACHE	3
50221ecdf64SLin Ling #define	VDEV_ALLOC_ROOTPOOL	4
5031195e687SMark J Musante #define	VDEV_ALLOC_SPLIT	5
504cd0837ccSGeorge Wilson #define	VDEV_ALLOC_ATTACH	6
505fa9e4066Sahrens 
506fa9e4066Sahrens /*
507fa9e4066Sahrens  * Allocate or free a vdev
508fa9e4066Sahrens  */
50988ecc943SGeorge Wilson extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
51088ecc943SGeorge Wilson     vdev_ops_t *ops);
51199653d4eSeschrock extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
51299653d4eSeschrock     vdev_t *parent, uint_t id, int alloctype);
513fa9e4066Sahrens extern void vdev_free(vdev_t *vd);
514fa9e4066Sahrens 
515fa9e4066Sahrens /*
516fa9e4066Sahrens  * Add or remove children and parents
517fa9e4066Sahrens  */
518fa9e4066Sahrens extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
519fa9e4066Sahrens extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
520fa9e4066Sahrens extern void vdev_compact_children(vdev_t *pvd);
521fa9e4066Sahrens extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
522fa9e4066Sahrens extern void vdev_remove_parent(vdev_t *cvd);
523fa9e4066Sahrens 
524fa9e4066Sahrens /*
525fa9e4066Sahrens  * vdev sync load and sync
526fa9e4066Sahrens  */
5274b964adaSGeorge Wilson extern boolean_t vdev_log_state_valid(vdev_t *vd);
5285cabbc6bSPrashanth Sreenivasa extern int vdev_load(vdev_t *vd);
5290713e232SGeorge Wilson extern int vdev_dtl_load(vdev_t *vd);
530fa9e4066Sahrens extern void vdev_sync(vdev_t *vd, uint64_t txg);
531fa9e4066Sahrens extern void vdev_sync_done(vdev_t *vd, uint64_t txg);
532ecc2d604Sbonwick extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
5330713e232SGeorge Wilson extern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
534fa9e4066Sahrens 
535fa9e4066Sahrens /*
536fa9e4066Sahrens  * Available vdev types.
537fa9e4066Sahrens  */
538fa9e4066Sahrens extern vdev_ops_t vdev_root_ops;
539fa9e4066Sahrens extern vdev_ops_t vdev_mirror_ops;
540fa9e4066Sahrens extern vdev_ops_t vdev_replacing_ops;
541fa9e4066Sahrens extern vdev_ops_t vdev_raidz_ops;
542fa9e4066Sahrens extern vdev_ops_t vdev_disk_ops;
543fa9e4066Sahrens extern vdev_ops_t vdev_file_ops;
544fa9e4066Sahrens extern vdev_ops_t vdev_missing_ops;
54588ecc943SGeorge Wilson extern vdev_ops_t vdev_hole_ops;
54699653d4eSeschrock extern vdev_ops_t vdev_spare_ops;
5475cabbc6bSPrashanth Sreenivasa extern vdev_ops_t vdev_indirect_ops;
548fa9e4066Sahrens 
549fa9e4066Sahrens /*
5502a79c5feSlling  * Common size functions
551fa9e4066Sahrens  */
5524d7988d6SPaul Dagnelie extern void vdev_default_xlate(vdev_t *vd, const range_seg64_t *in,
5534d7988d6SPaul Dagnelie     range_seg64_t *out);
554fa9e4066Sahrens extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
555573ca77eSGeorge Wilson extern uint64_t vdev_get_min_asize(vdev_t *vd);
556573ca77eSGeorge Wilson extern void vdev_set_min_asize(vdev_t *vd);
557fa9e4066Sahrens 
558e81c57b1Sahrens /*
559f7170741SWill Andrews  * Global variables
560e81c57b1Sahrens  */
561814dcd43SSerapheim Dimitropoulos extern int zfs_vdev_standard_sm_blksz;
562f7170741SWill Andrews /* zdb uses this tunable, so it must be declared here to make lint happy. */
563e81c57b1Sahrens extern int zfs_vdev_cache_size;
564e81c57b1Sahrens 
5655cabbc6bSPrashanth Sreenivasa /*
5665cabbc6bSPrashanth Sreenivasa  * Functions from vdev_indirect.c
5675cabbc6bSPrashanth Sreenivasa  */
5685cabbc6bSPrashanth Sreenivasa extern void vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx);
5695cabbc6bSPrashanth Sreenivasa extern boolean_t vdev_indirect_should_condense(vdev_t *vd);
5705cabbc6bSPrashanth Sreenivasa extern void spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx);
5715cabbc6bSPrashanth Sreenivasa extern int vdev_obsolete_sm_object(vdev_t *vd);
5725cabbc6bSPrashanth Sreenivasa extern boolean_t vdev_obsolete_counts_are_precise(vdev_t *vd);
5735cabbc6bSPrashanth Sreenivasa 
57486714001SSerapheim Dimitropoulos /*
57586714001SSerapheim Dimitropoulos  * Other miscellaneous functions
57686714001SSerapheim Dimitropoulos  */
57786714001SSerapheim Dimitropoulos int vdev_checkpoint_sm_object(vdev_t *vd);
57886714001SSerapheim Dimitropoulos 
57931d7e8faSGeorge Wilson /*
58031d7e8faSGeorge Wilson  * The vdev_buf_t is used to translate between zio_t and buf_t, and back again.
58131d7e8faSGeorge Wilson  */
58231d7e8faSGeorge Wilson typedef struct vdev_buf {
58331d7e8faSGeorge Wilson 	buf_t	vb_buf;		/* buffer that describes the io */
58431d7e8faSGeorge Wilson 	zio_t	*vb_io;		/* pointer back to the original zio_t */
58531d7e8faSGeorge Wilson } vdev_buf_t;
58631d7e8faSGeorge Wilson 
58730c304d9SJoshua M. Clulow /*
58830c304d9SJoshua M. Clulow  * Support routines used during boot from a ZFS pool
58930c304d9SJoshua M. Clulow  */
59030c304d9SJoshua M. Clulow extern int vdev_disk_read_rootlabel(const char *, const char *, nvlist_t **);
591*8b26092dSJoshua M. Clulow extern void vdev_disk_preroot_init(const char *);
59230c304d9SJoshua M. Clulow extern void vdev_disk_preroot_fini(void);
59330c304d9SJoshua M. Clulow extern const char *vdev_disk_preroot_lookup(uint64_t, uint64_t);
594*8b26092dSJoshua M. Clulow extern const char *vdev_disk_preroot_force_path(void);
59530c304d9SJoshua M. Clulow 
596fa9e4066Sahrens #ifdef	__cplusplus
597fa9e4066Sahrens }
598fa9e4066Sahrens #endif
599fa9e4066Sahrens 
600fa9e4066Sahrens #endif	/* _SYS_VDEV_IMPL_H */
601