xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/vdev_impl.h (revision e4c795beb33bf59dd4ad2e3f88f493111484b890)
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.
23f78cdc34SPaul Dagnelie  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #ifndef _SYS_VDEV_IMPL_H
28fa9e4066Sahrens #define	_SYS_VDEV_IMPL_H
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <sys/avl.h>
315cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h>
32fa9e4066Sahrens #include <sys/dmu.h>
33fa9e4066Sahrens #include <sys/metaslab.h>
34fa9e4066Sahrens #include <sys/nvpair.h>
35fa9e4066Sahrens #include <sys/space_map.h>
36fa9e4066Sahrens #include <sys/vdev.h>
37fa9e4066Sahrens #include <sys/dkio.h>
38fa9e4066Sahrens #include <sys/uberblock_impl.h>
395cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_mapping.h>
405cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_births.h>
415cabbc6bSPrashanth Sreenivasa #include <sys/vdev_removal.h>
42fa9e4066Sahrens 
43fa9e4066Sahrens #ifdef	__cplusplus
44fa9e4066Sahrens extern "C" {
45fa9e4066Sahrens #endif
46fa9e4066Sahrens 
47fa9e4066Sahrens /*
48fa9e4066Sahrens  * Virtual device descriptors.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * All storage pool operations go through the virtual device framework,
51fa9e4066Sahrens  * which provides data replication and I/O scheduling.
52fa9e4066Sahrens  */
53fa9e4066Sahrens 
54fa9e4066Sahrens /*
55fa9e4066Sahrens  * Forward declarations that lots of things need.
56fa9e4066Sahrens  */
57fa9e4066Sahrens typedef struct vdev_queue vdev_queue_t;
58fa9e4066Sahrens typedef struct vdev_cache vdev_cache_t;
59fa9e4066Sahrens typedef struct vdev_cache_entry vdev_cache_entry_t;
60770499e1SDan Kimmel struct abd;
61fa9e4066Sahrens 
620f7643c7SGeorge Wilson extern int zfs_vdev_queue_depth_pct;
63f78cdc34SPaul Dagnelie extern int zfs_vdev_def_queue_depth;
640f7643c7SGeorge Wilson extern uint32_t zfs_vdev_async_write_max_active;
650f7643c7SGeorge Wilson 
66fa9e4066Sahrens /*
67fa9e4066Sahrens  * Virtual device operations
68fa9e4066Sahrens  */
694263d13fSGeorge Wilson typedef int	vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *max_size,
704263d13fSGeorge Wilson     uint64_t *ashift);
71fa9e4066Sahrens typedef void	vdev_close_func_t(vdev_t *vd);
72fa9e4066Sahrens typedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize);
73738f37bcSGeorge Wilson typedef void	vdev_io_start_func_t(zio_t *zio);
74e14bb325SJeff Bonwick typedef void	vdev_io_done_func_t(zio_t *zio);
75fa9e4066Sahrens typedef void	vdev_state_change_func_t(vdev_t *vd, int, int);
76a3874b8bSToomas Soome typedef boolean_t vdev_need_resilver_func_t(vdev_t *vd, uint64_t, size_t);
77dcba9f3fSGeorge Wilson typedef void	vdev_hold_func_t(vdev_t *vd);
78dcba9f3fSGeorge Wilson typedef void	vdev_rele_func_t(vdev_t *vd);
79fa9e4066Sahrens 
805cabbc6bSPrashanth Sreenivasa typedef void	vdev_remap_cb_t(uint64_t inner_offset, vdev_t *vd,
815cabbc6bSPrashanth Sreenivasa     uint64_t offset, uint64_t size, void *arg);
825cabbc6bSPrashanth Sreenivasa typedef void	vdev_remap_func_t(vdev_t *vd, uint64_t offset, uint64_t size,
835cabbc6bSPrashanth Sreenivasa     vdev_remap_cb_t callback, void *arg);
84094e47e9SGeorge Wilson /*
85094e47e9SGeorge Wilson  * Given a target vdev, translates the logical range "in" to the physical
86094e47e9SGeorge Wilson  * range "res"
87094e47e9SGeorge Wilson  */
88094e47e9SGeorge Wilson typedef void vdev_xlation_func_t(vdev_t *cvd, const range_seg_t *in,
89094e47e9SGeorge Wilson     range_seg_t *res);
905cabbc6bSPrashanth Sreenivasa 
91fa9e4066Sahrens typedef struct vdev_ops {
92fa9e4066Sahrens 	vdev_open_func_t		*vdev_op_open;
93fa9e4066Sahrens 	vdev_close_func_t		*vdev_op_close;
94fa9e4066Sahrens 	vdev_asize_func_t		*vdev_op_asize;
95fa9e4066Sahrens 	vdev_io_start_func_t		*vdev_op_io_start;
96fa9e4066Sahrens 	vdev_io_done_func_t		*vdev_op_io_done;
97fa9e4066Sahrens 	vdev_state_change_func_t	*vdev_op_state_change;
98a3874b8bSToomas Soome 	vdev_need_resilver_func_t	*vdev_op_need_resilver;
99dcba9f3fSGeorge Wilson 	vdev_hold_func_t		*vdev_op_hold;
100dcba9f3fSGeorge Wilson 	vdev_rele_func_t		*vdev_op_rele;
1015cabbc6bSPrashanth Sreenivasa 	vdev_remap_func_t		*vdev_op_remap;
102094e47e9SGeorge Wilson 	/*
103094e47e9SGeorge Wilson 	 * For translating ranges from non-leaf vdevs (e.g. raidz) to leaves.
104094e47e9SGeorge Wilson 	 * Used when initializing vdevs. Isn't used by leaf ops.
105094e47e9SGeorge Wilson 	 */
106094e47e9SGeorge Wilson 	vdev_xlation_func_t		*vdev_op_xlate;
107fa9e4066Sahrens 	char				vdev_op_type[16];
108fa9e4066Sahrens 	boolean_t			vdev_op_leaf;
109fa9e4066Sahrens } vdev_ops_t;
110fa9e4066Sahrens 
111fa9e4066Sahrens /*
112fa9e4066Sahrens  * Virtual device properties
113fa9e4066Sahrens  */
114fa9e4066Sahrens struct vdev_cache_entry {
115770499e1SDan Kimmel 	struct abd	*ve_abd;
116fa9e4066Sahrens 	uint64_t	ve_offset;
117fa9e4066Sahrens 	uint64_t	ve_lastused;
118fa9e4066Sahrens 	avl_node_t	ve_offset_node;
119fa9e4066Sahrens 	avl_node_t	ve_lastused_node;
120fa9e4066Sahrens 	uint32_t	ve_hits;
121fa9e4066Sahrens 	uint16_t	ve_missed_update;
122fa9e4066Sahrens 	zio_t		*ve_fill_io;
123fa9e4066Sahrens };
124fa9e4066Sahrens 
125fa9e4066Sahrens struct vdev_cache {
126fa9e4066Sahrens 	avl_tree_t	vc_offset_tree;
127fa9e4066Sahrens 	avl_tree_t	vc_lastused_tree;
128fa9e4066Sahrens 	kmutex_t	vc_lock;
129fa9e4066Sahrens };
130fa9e4066Sahrens 
13169962b56SMatthew Ahrens typedef struct vdev_queue_class {
13269962b56SMatthew Ahrens 	uint32_t	vqc_active;
13369962b56SMatthew Ahrens 
13469962b56SMatthew Ahrens 	/*
13569962b56SMatthew Ahrens 	 * Sorted by offset or timestamp, depending on if the queue is
13669962b56SMatthew Ahrens 	 * LBA-ordered vs FIFO.
13769962b56SMatthew Ahrens 	 */
13869962b56SMatthew Ahrens 	avl_tree_t	vqc_queued_tree;
13969962b56SMatthew Ahrens } vdev_queue_class_t;
14069962b56SMatthew Ahrens 
141fa9e4066Sahrens struct vdev_queue {
14269962b56SMatthew Ahrens 	vdev_t		*vq_vdev;
14369962b56SMatthew Ahrens 	vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
14469962b56SMatthew Ahrens 	avl_tree_t	vq_active_tree;
145fe319232SJustin T. Gibbs 	avl_tree_t	vq_read_offset_tree;
146fe319232SJustin T. Gibbs 	avl_tree_t	vq_write_offset_tree;
14769962b56SMatthew Ahrens 	uint64_t	vq_last_offset;
14869962b56SMatthew Ahrens 	hrtime_t	vq_io_complete_ts; /* time last i/o completed */
149fa9e4066Sahrens 	kmutex_t	vq_lock;
150fa9e4066Sahrens };
151fa9e4066Sahrens 
152663207adSDon Brady typedef enum vdev_alloc_bias {
153663207adSDon Brady 	VDEV_BIAS_NONE,
154663207adSDon Brady 	VDEV_BIAS_LOG,		/* dedicated to ZIL data (SLOG) */
155663207adSDon Brady 	VDEV_BIAS_SPECIAL,	/* dedicated to ddt, metadata, and small blks */
156663207adSDon Brady 	VDEV_BIAS_DEDUP		/* dedicated to dedup metadata */
157663207adSDon Brady } vdev_alloc_bias_t;
158663207adSDon Brady 
159663207adSDon Brady 
1605cabbc6bSPrashanth Sreenivasa /*
1615cabbc6bSPrashanth Sreenivasa  * On-disk indirect vdev state.
1625cabbc6bSPrashanth Sreenivasa  *
1635cabbc6bSPrashanth Sreenivasa  * An indirect vdev is described exclusively in the MOS config of a pool.
1645cabbc6bSPrashanth Sreenivasa  * The config for an indirect vdev includes several fields, which are
1655cabbc6bSPrashanth Sreenivasa  * accessed in memory by a vdev_indirect_config_t.
1665cabbc6bSPrashanth Sreenivasa  */
1675cabbc6bSPrashanth Sreenivasa typedef struct vdev_indirect_config {
1685cabbc6bSPrashanth Sreenivasa 	/*
1695cabbc6bSPrashanth Sreenivasa 	 * Object (in MOS) which contains the indirect mapping. This object
1705cabbc6bSPrashanth Sreenivasa 	 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
1715cabbc6bSPrashanth Sreenivasa 	 * vimep_src. The bonus buffer for this object is a
1725cabbc6bSPrashanth Sreenivasa 	 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
1735cabbc6bSPrashanth Sreenivasa 	 * removal is initiated.
1745cabbc6bSPrashanth Sreenivasa 	 *
1755cabbc6bSPrashanth Sreenivasa 	 * Note that this object can be empty if none of the data on the vdev
1765cabbc6bSPrashanth Sreenivasa 	 * has been copied yet.
1775cabbc6bSPrashanth Sreenivasa 	 */
1785cabbc6bSPrashanth Sreenivasa 	uint64_t	vic_mapping_object;
1795cabbc6bSPrashanth Sreenivasa 
1805cabbc6bSPrashanth Sreenivasa 	/*
1815cabbc6bSPrashanth Sreenivasa 	 * Object (in MOS) which contains the birth times for the mapping
1825cabbc6bSPrashanth Sreenivasa 	 * entries. This object contains an array of
1835cabbc6bSPrashanth Sreenivasa 	 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
1845cabbc6bSPrashanth Sreenivasa 	 * buffer for this object is a vdev_indirect_birth_phys_t. This object
1855cabbc6bSPrashanth Sreenivasa 	 * is allocated when a vdev removal is initiated.
1865cabbc6bSPrashanth Sreenivasa 	 *
1875cabbc6bSPrashanth Sreenivasa 	 * Note that this object can be empty if none of the vdev has yet been
1885cabbc6bSPrashanth Sreenivasa 	 * copied.
1895cabbc6bSPrashanth Sreenivasa 	 */
1905cabbc6bSPrashanth Sreenivasa 	uint64_t	vic_births_object;
1915cabbc6bSPrashanth Sreenivasa 
1925cabbc6bSPrashanth Sreenivasa 	/*
1935cabbc6bSPrashanth Sreenivasa 	 * This is the vdev ID which was removed previous to this vdev, or
1945cabbc6bSPrashanth Sreenivasa 	 * UINT64_MAX if there are no previously removed vdevs.
1955cabbc6bSPrashanth Sreenivasa 	 */
1965cabbc6bSPrashanth Sreenivasa 	uint64_t	vic_prev_indirect_vdev;
1975cabbc6bSPrashanth Sreenivasa } vdev_indirect_config_t;
1985cabbc6bSPrashanth Sreenivasa 
199fa9e4066Sahrens /*
200fa9e4066Sahrens  * Virtual device descriptor
201fa9e4066Sahrens  */
202fa9e4066Sahrens struct vdev {
203fa9e4066Sahrens 	/*
204fa9e4066Sahrens 	 * Common to all vdev types.
205fa9e4066Sahrens 	 */
206fa9e4066Sahrens 	uint64_t	vdev_id;	/* child number in vdev parent	*/
207fa9e4066Sahrens 	uint64_t	vdev_guid;	/* unique ID for this vdev	*/
208fa9e4066Sahrens 	uint64_t	vdev_guid_sum;	/* self guid + all child guids	*/
2091195e687SMark J Musante 	uint64_t	vdev_orig_guid;	/* orig. guid prior to remove	*/
210fa9e4066Sahrens 	uint64_t	vdev_asize;	/* allocatable device capacity	*/
211573ca77eSGeorge Wilson 	uint64_t	vdev_min_asize;	/* min acceptable asize		*/
2124263d13fSGeorge Wilson 	uint64_t	vdev_max_asize;	/* max acceptable asize		*/
213fa9e4066Sahrens 	uint64_t	vdev_ashift;	/* block alignment shift	*/
214fa9e4066Sahrens 	uint64_t	vdev_state;	/* see VDEV_STATE_* #defines	*/
215560e6e96Seschrock 	uint64_t	vdev_prevstate;	/* used when reopening a vdev	*/
216fa9e4066Sahrens 	vdev_ops_t	*vdev_ops;	/* vdev operations		*/
217fa9e4066Sahrens 	spa_t		*vdev_spa;	/* spa for this vdev		*/
218fa9e4066Sahrens 	void		*vdev_tsd;	/* type-specific data		*/
219dcba9f3fSGeorge Wilson 	vnode_t		*vdev_name_vp;	/* vnode for pathname		*/
220dcba9f3fSGeorge Wilson 	vnode_t		*vdev_devid_vp;	/* vnode for devid		*/
221fa9e4066Sahrens 	vdev_t		*vdev_top;	/* top-level vdev		*/
222fa9e4066Sahrens 	vdev_t		*vdev_parent;	/* parent vdev			*/
223fa9e4066Sahrens 	vdev_t		**vdev_child;	/* array of children		*/
224fa9e4066Sahrens 	uint64_t	vdev_children;	/* number of children		*/
225fa9e4066Sahrens 	vdev_stat_t	vdev_stat;	/* virtual device statistics	*/
226573ca77eSGeorge Wilson 	boolean_t	vdev_expanding;	/* expand the vdev?		*/
227095bcd66SGeorge Wilson 	boolean_t	vdev_reopening;	/* reopen in progress?		*/
22812a8814cSTom Caputi 	boolean_t	vdev_nonrot;	/* true if solid state		*/
229f64c0e34SEric Taylor 	int		vdev_open_error; /* error on last open		*/
230f64c0e34SEric Taylor 	kthread_t	*vdev_open_thread; /* thread opening children	*/
23188ecc943SGeorge Wilson 	uint64_t	vdev_crtxg;	/* txg when top-level was added */
232fa9e4066Sahrens 
233fa9e4066Sahrens 	/*
234fa9e4066Sahrens 	 * Top-level vdev state.
235fa9e4066Sahrens 	 */
236fa9e4066Sahrens 	uint64_t	vdev_ms_array;	/* metaslab array object	*/
237fa9e4066Sahrens 	uint64_t	vdev_ms_shift;	/* metaslab size shift		*/
238fa9e4066Sahrens 	uint64_t	vdev_ms_count;	/* number of metaslabs		*/
239fa9e4066Sahrens 	metaslab_group_t *vdev_mg;	/* metaslab group		*/
240fa9e4066Sahrens 	metaslab_t	**vdev_ms;	/* metaslab array		*/
241fa9e4066Sahrens 	txg_list_t	vdev_ms_list;	/* per-txg dirty metaslab lists	*/
242fa9e4066Sahrens 	txg_list_t	vdev_dtl_list;	/* per-txg dirty DTL lists	*/
243fa9e4066Sahrens 	txg_node_t	vdev_txg_node;	/* per-txg dirty vdev linkage	*/
2443d7072f8Seschrock 	boolean_t	vdev_remove_wanted; /* async remove wanted?	*/
245e14bb325SJeff Bonwick 	boolean_t	vdev_probe_wanted; /* async probe wanted?	*/
246e14bb325SJeff Bonwick 	list_node_t	vdev_config_dirty_node; /* config dirty list	*/
247e14bb325SJeff Bonwick 	list_node_t	vdev_state_dirty_node; /* state dirty list	*/
24899653d4eSeschrock 	uint64_t	vdev_deflate_ratio; /* deflation ratio (x512)	*/
2498654d025Sperrin 	uint64_t	vdev_islog;	/* is an intent log device	*/
2500713e232SGeorge Wilson 	uint64_t	vdev_removing;	/* device is being removed?	*/
2510f7643c7SGeorge Wilson 	boolean_t	vdev_ishole;	/* is a hole in the namespace	*/
252215198a6SJoe Stein 	uint64_t	vdev_top_zap;
253663207adSDon Brady 	vdev_alloc_bias_t vdev_alloc_bias; /* metaslab allocation bias	*/
254fa9e4066Sahrens 
25586714001SSerapheim Dimitropoulos 	/* pool checkpoint related */
25686714001SSerapheim Dimitropoulos 	space_map_t	*vdev_checkpoint_sm;	/* contains reserved blocks */
257e0f1c0afSOlaf Faaland 
258094e47e9SGeorge Wilson 	boolean_t	vdev_initialize_exit_wanted;
259094e47e9SGeorge Wilson 	vdev_initializing_state_t	vdev_initialize_state;
260094e47e9SGeorge Wilson 	kthread_t	*vdev_initialize_thread;
261094e47e9SGeorge Wilson 	/* Protects vdev_initialize_thread and vdev_initialize_state. */
262094e47e9SGeorge Wilson 	kmutex_t	vdev_initialize_lock;
263094e47e9SGeorge Wilson 	kcondvar_t	vdev_initialize_cv;
264094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_offset[TXG_SIZE];
265094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_last_offset;
266094e47e9SGeorge Wilson 	range_tree_t	*vdev_initialize_tree;	/* valid while initializing */
267094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_bytes_est;
268094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_bytes_done;
269094e47e9SGeorge Wilson 	time_t		vdev_initialize_action_time;	/* start and end time */
270094e47e9SGeorge Wilson 
271094e47e9SGeorge Wilson 	/* for limiting outstanding I/Os */
272094e47e9SGeorge Wilson 	kmutex_t	vdev_initialize_io_lock;
273094e47e9SGeorge Wilson 	kcondvar_t	vdev_initialize_io_cv;
274094e47e9SGeorge Wilson 	uint64_t	vdev_initialize_inflight;
27586714001SSerapheim Dimitropoulos 
2765cabbc6bSPrashanth Sreenivasa 	/*
2775cabbc6bSPrashanth Sreenivasa 	 * Values stored in the config for an indirect or removing vdev.
2785cabbc6bSPrashanth Sreenivasa 	 */
2795cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t	vdev_indirect_config;
2805cabbc6bSPrashanth Sreenivasa 
2815cabbc6bSPrashanth Sreenivasa 	/*
2825cabbc6bSPrashanth Sreenivasa 	 * The vdev_indirect_rwlock protects the vdev_indirect_mapping
2835cabbc6bSPrashanth Sreenivasa 	 * pointer from changing on indirect vdevs (when it is condensed).
2845cabbc6bSPrashanth Sreenivasa 	 * Note that removing (not yet indirect) vdevs have different
2855cabbc6bSPrashanth Sreenivasa 	 * access patterns (the mapping is not accessed from open context,
2865cabbc6bSPrashanth Sreenivasa 	 * e.g. from zio_read) and locking strategy (e.g. svr_lock).
2875cabbc6bSPrashanth Sreenivasa 	 */
2885cabbc6bSPrashanth Sreenivasa 	krwlock_t vdev_indirect_rwlock;
2895cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vdev_indirect_mapping;
2905cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_t *vdev_indirect_births;
2915cabbc6bSPrashanth Sreenivasa 
2925cabbc6bSPrashanth Sreenivasa 	/*
2935cabbc6bSPrashanth Sreenivasa 	 * In memory data structures used to manage the obsolete sm, for
2945cabbc6bSPrashanth Sreenivasa 	 * indirect or removing vdevs.
2955cabbc6bSPrashanth Sreenivasa 	 *
2965cabbc6bSPrashanth Sreenivasa 	 * The vdev_obsolete_segments is the in-core record of the segments
2975cabbc6bSPrashanth Sreenivasa 	 * that are no longer referenced anywhere in the pool (due to
2985cabbc6bSPrashanth Sreenivasa 	 * being freed or remapped and not referenced by any snapshots).
2995cabbc6bSPrashanth Sreenivasa 	 * During a sync, segments are added to vdev_obsolete_segments
3005cabbc6bSPrashanth Sreenivasa 	 * via vdev_indirect_mark_obsolete(); at the end of each sync
3015cabbc6bSPrashanth Sreenivasa 	 * pass, this is appended to vdev_obsolete_sm via
3025cabbc6bSPrashanth Sreenivasa 	 * vdev_indirect_sync_obsolete().  The vdev_obsolete_lock
3035cabbc6bSPrashanth Sreenivasa 	 * protects against concurrent modifications of vdev_obsolete_segments
3045cabbc6bSPrashanth Sreenivasa 	 * from multiple zio threads.
3055cabbc6bSPrashanth Sreenivasa 	 */
3065cabbc6bSPrashanth Sreenivasa 	kmutex_t	vdev_obsolete_lock;
3075cabbc6bSPrashanth Sreenivasa 	range_tree_t	*vdev_obsolete_segments;
3085cabbc6bSPrashanth Sreenivasa 	space_map_t	*vdev_obsolete_sm;
3095cabbc6bSPrashanth Sreenivasa 
310a3874b8bSToomas Soome 	/*
311a3874b8bSToomas Soome 	 * Protects the vdev_scan_io_queue field itself as well as the
312a3874b8bSToomas Soome 	 * structure's contents (when present).
313a3874b8bSToomas Soome 	 */
314a3874b8bSToomas Soome 	kmutex_t	vdev_scan_io_queue_lock;
315a3874b8bSToomas Soome 	struct dsl_scan_io_queue	*vdev_scan_io_queue;
316a3874b8bSToomas Soome 
317fa9e4066Sahrens 	/*
318fa9e4066Sahrens 	 * Leaf vdev state.
319fa9e4066Sahrens 	 */
3200713e232SGeorge Wilson 	range_tree_t	*vdev_dtl[DTL_TYPES]; /* dirty time logs	*/
3210713e232SGeorge Wilson 	space_map_t	*vdev_dtl_sm;	/* dirty time log space map	*/
322fa9e4066Sahrens 	txg_node_t	vdev_dtl_node;	/* per-txg dirty DTL linkage	*/
3230713e232SGeorge Wilson 	uint64_t	vdev_dtl_object; /* DTL object			*/
3240713e232SGeorge Wilson 	uint64_t	vdev_psize;	/* physical device capacity	*/
325afefbcddSeschrock 	uint64_t	vdev_wholedisk;	/* true if this is a whole disk */
3263d7072f8Seschrock 	uint64_t	vdev_offline;	/* persistent offline state	*/
3273d7072f8Seschrock 	uint64_t	vdev_faulted;	/* persistent faulted state	*/
3283d7072f8Seschrock 	uint64_t	vdev_degraded;	/* persistent degraded state	*/
3293d7072f8Seschrock 	uint64_t	vdev_removed;	/* persistent removed state	*/
330b4952e17SGeorge Wilson 	uint64_t	vdev_resilver_txg; /* persistent resilvering state */
33199653d4eSeschrock 	uint64_t	vdev_nparity;	/* number of parity devices for raidz */
332fa9e4066Sahrens 	char		*vdev_path;	/* vdev path (if any)		*/
333fa9e4066Sahrens 	char		*vdev_devid;	/* vdev devid (if any)		*/
3343d7072f8Seschrock 	char		*vdev_physpath;	/* vdev device path (if any)	*/
3356809eb4eSEric Schrock 	char		*vdev_fru;	/* physical FRU location	*/
336e14bb325SJeff Bonwick 	uint64_t	vdev_not_present; /* not present during import	*/
337e14bb325SJeff Bonwick 	uint64_t	vdev_unspare;	/* unspare when resilvering done */
338e14bb325SJeff Bonwick 	boolean_t	vdev_nowritecache; /* true if flushwritecache failed */
339e14bb325SJeff Bonwick 	boolean_t	vdev_checkremove; /* temporary online test	*/
340e14bb325SJeff Bonwick 	boolean_t	vdev_forcefault; /* force online fault		*/
3411195e687SMark J Musante 	boolean_t	vdev_splitting;	/* split or repair in progress  */
34298d1cbfeSGeorge Wilson 	boolean_t	vdev_delayed_close; /* delayed device close?	*/
3430713e232SGeorge Wilson 	boolean_t	vdev_tmpoffline; /* device taken offline temporarily? */
3440713e232SGeorge Wilson 	boolean_t	vdev_detached;	/* device detached?		*/
3450713e232SGeorge Wilson 	boolean_t	vdev_cant_read;	/* vdev is failing all reads	*/
3460713e232SGeorge Wilson 	boolean_t	vdev_cant_write; /* vdev is failing all writes	*/
3470713e232SGeorge Wilson 	boolean_t	vdev_isspare;	/* was a hot spare		*/
3480713e232SGeorge Wilson 	boolean_t	vdev_isl2cache;	/* was a l2cache device		*/
349*e4c795beSTom Caputi 	boolean_t	vdev_resilver_deferred;  /* resilver deferred */
350fa9e4066Sahrens 	vdev_queue_t	vdev_queue;	/* I/O deadline schedule queue	*/
351fa9e4066Sahrens 	vdev_cache_t	vdev_cache;	/* physical block cache		*/
35281cd5c55SMatthew Ahrens 	spa_aux_vdev_t	*vdev_aux;	/* for l2cache and spares vdevs	*/
353e14bb325SJeff Bonwick 	zio_t		*vdev_probe_zio; /* root of current probe	*/
354069f55e2SEric Schrock 	vdev_aux_t	vdev_label_aux;	/* on-disk aux state		*/
355215198a6SJoe Stein 	uint64_t	vdev_leaf_zap;
356e0f1c0afSOlaf Faaland 	hrtime_t	vdev_mmp_pending; /* 0 if write finished	*/
357e0f1c0afSOlaf Faaland 	uint64_t	vdev_mmp_kstat_id;	/* to find kstat entry */
358e0f1c0afSOlaf Faaland 	list_node_t	vdev_leaf_node;		/* leaf vdev list */
359fa9e4066Sahrens 
360fa9e4066Sahrens 	/*
361fa9e4066Sahrens 	 * For DTrace to work in userland (libzpool) context, these fields must
362fa9e4066Sahrens 	 * remain at the end of the structure.  DTrace will use the kernel's
363fa9e4066Sahrens 	 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
364ad135b5dSChristopher Siden 	 * larger in userland, the offsets for the rest of the fields would be
365fa9e4066Sahrens 	 * incorrect.
366fa9e4066Sahrens 	 */
367fa9e4066Sahrens 	kmutex_t	vdev_dtl_lock;	/* vdev_dtl_{map,resilver}	*/
368fa9e4066Sahrens 	kmutex_t	vdev_stat_lock;	/* vdev_stat			*/
369e14bb325SJeff Bonwick 	kmutex_t	vdev_probe_lock; /* protects vdev_probe_zio	*/
370fa9e4066Sahrens };
371fa9e4066Sahrens 
372b24ab676SJeff Bonwick #define	VDEV_RAIDZ_MAXPARITY	3
373b24ab676SJeff Bonwick 
374f83ffe1aSLin Ling #define	VDEV_PAD_SIZE		(8 << 10)
375f83ffe1aSLin Ling /* 2 padding areas (vl_pad1 and vl_pad2) to skip */
376f83ffe1aSLin Ling #define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
377fa9e4066Sahrens #define	VDEV_PHYS_SIZE		(112 << 10)
378ecc2d604Sbonwick #define	VDEV_UBERBLOCK_RING	(128 << 10)
379ecc2d604Sbonwick 
380e0f1c0afSOlaf Faaland /*
381e0f1c0afSOlaf Faaland  * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
382e0f1c0afSOlaf Faaland  * ring when MMP is enabled.
383e0f1c0afSOlaf Faaland  */
384e0f1c0afSOlaf Faaland #define	MMP_BLOCKS_PER_LABEL	1
385e0f1c0afSOlaf Faaland 
3862a104a52SAlex Reece /* The largest uberblock we support is 8k. */
3872a104a52SAlex Reece #define	MAX_UBERBLOCK_SHIFT (13)
388ecc2d604Sbonwick #define	VDEV_UBERBLOCK_SHIFT(vd)	\
3892a104a52SAlex Reece 	MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
3902a104a52SAlex Reece 	    MAX_UBERBLOCK_SHIFT)
391ecc2d604Sbonwick #define	VDEV_UBERBLOCK_COUNT(vd)	\
392ecc2d604Sbonwick 	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
393ecc2d604Sbonwick #define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
394ecc2d604Sbonwick 	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
395ecc2d604Sbonwick #define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
396fa9e4066Sahrens 
397fa9e4066Sahrens typedef struct vdev_phys {
3986e1f5caaSNeil Perrin 	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
3996e1f5caaSNeil Perrin 	zio_eck_t	vp_zbt;
400fa9e4066Sahrens } vdev_phys_t;
401fa9e4066Sahrens 
402fa9e4066Sahrens typedef struct vdev_label {
403f83ffe1aSLin Ling 	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K */
404f83ffe1aSLin Ling 	char		vl_pad2[VDEV_PAD_SIZE];			/*  8K */
405ecc2d604Sbonwick 	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
406ecc2d604Sbonwick 	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
407fa9e4066Sahrens } vdev_label_t;							/* 256K total */
408fa9e4066Sahrens 
409ecc2d604Sbonwick /*
410ecc2d604Sbonwick  * vdev_dirty() flags
411ecc2d604Sbonwick  */
412ecc2d604Sbonwick #define	VDD_METASLAB	0x01
413ecc2d604Sbonwick #define	VDD_DTL		0x02
414ecc2d604Sbonwick 
415f7170741SWill Andrews /* Offset of embedded boot loader region on each label */
416f7170741SWill Andrews #define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
417fa9e4066Sahrens /*
418f7170741SWill Andrews  * Size of embedded boot loader region on each label.
419fa9e4066Sahrens  * The total size of the first two labels plus the boot area is 4MB.
420fa9e4066Sahrens  */
421f7170741SWill Andrews #define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M */
422fa9e4066Sahrens 
423fa9e4066Sahrens /*
424fa9e4066Sahrens  * Size of label regions at the start and end of each leaf device.
425fa9e4066Sahrens  */
426fa9e4066Sahrens #define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
427fa9e4066Sahrens #define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
428fa9e4066Sahrens #define	VDEV_LABELS		4
429ad135b5dSChristopher Siden #define	VDEV_BEST_LABEL		VDEV_LABELS
430fa9e4066Sahrens 
431fa9e4066Sahrens #define	VDEV_ALLOC_LOAD		0
432fa9e4066Sahrens #define	VDEV_ALLOC_ADD		1
43399653d4eSeschrock #define	VDEV_ALLOC_SPARE	2
434fa94a07fSbrendan #define	VDEV_ALLOC_L2CACHE	3
43521ecdf64SLin Ling #define	VDEV_ALLOC_ROOTPOOL	4
4361195e687SMark J Musante #define	VDEV_ALLOC_SPLIT	5
437cd0837ccSGeorge Wilson #define	VDEV_ALLOC_ATTACH	6
438fa9e4066Sahrens 
439fa9e4066Sahrens /*
440fa9e4066Sahrens  * Allocate or free a vdev
441fa9e4066Sahrens  */
44288ecc943SGeorge Wilson extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
44388ecc943SGeorge Wilson     vdev_ops_t *ops);
44499653d4eSeschrock extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
44599653d4eSeschrock     vdev_t *parent, uint_t id, int alloctype);
446fa9e4066Sahrens extern void vdev_free(vdev_t *vd);
447fa9e4066Sahrens 
448fa9e4066Sahrens /*
449fa9e4066Sahrens  * Add or remove children and parents
450fa9e4066Sahrens  */
451fa9e4066Sahrens extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
452fa9e4066Sahrens extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
453fa9e4066Sahrens extern void vdev_compact_children(vdev_t *pvd);
454fa9e4066Sahrens extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
455fa9e4066Sahrens extern void vdev_remove_parent(vdev_t *cvd);
456fa9e4066Sahrens 
457fa9e4066Sahrens /*
458fa9e4066Sahrens  * vdev sync load and sync
459fa9e4066Sahrens  */
4604b964adaSGeorge Wilson extern boolean_t vdev_log_state_valid(vdev_t *vd);
4615cabbc6bSPrashanth Sreenivasa extern int vdev_load(vdev_t *vd);
4620713e232SGeorge Wilson extern int vdev_dtl_load(vdev_t *vd);
463fa9e4066Sahrens extern void vdev_sync(vdev_t *vd, uint64_t txg);
464fa9e4066Sahrens extern void vdev_sync_done(vdev_t *vd, uint64_t txg);
465ecc2d604Sbonwick extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
4660713e232SGeorge Wilson extern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
467fa9e4066Sahrens 
468fa9e4066Sahrens /*
469fa9e4066Sahrens  * Available vdev types.
470fa9e4066Sahrens  */
471fa9e4066Sahrens extern vdev_ops_t vdev_root_ops;
472fa9e4066Sahrens extern vdev_ops_t vdev_mirror_ops;
473fa9e4066Sahrens extern vdev_ops_t vdev_replacing_ops;
474fa9e4066Sahrens extern vdev_ops_t vdev_raidz_ops;
475fa9e4066Sahrens extern vdev_ops_t vdev_disk_ops;
476fa9e4066Sahrens extern vdev_ops_t vdev_file_ops;
477fa9e4066Sahrens extern vdev_ops_t vdev_missing_ops;
47888ecc943SGeorge Wilson extern vdev_ops_t vdev_hole_ops;
47999653d4eSeschrock extern vdev_ops_t vdev_spare_ops;
4805cabbc6bSPrashanth Sreenivasa extern vdev_ops_t vdev_indirect_ops;
481fa9e4066Sahrens 
482fa9e4066Sahrens /*
4832a79c5feSlling  * Common size functions
484fa9e4066Sahrens  */
485094e47e9SGeorge Wilson extern void vdev_default_xlate(vdev_t *vd, const range_seg_t *in,
486094e47e9SGeorge Wilson     range_seg_t *out);
487fa9e4066Sahrens extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
488573ca77eSGeorge Wilson extern uint64_t vdev_get_min_asize(vdev_t *vd);
489573ca77eSGeorge Wilson extern void vdev_set_min_asize(vdev_t *vd);
490fa9e4066Sahrens 
491e81c57b1Sahrens /*
492f7170741SWill Andrews  * Global variables
493e81c57b1Sahrens  */
49486714001SSerapheim Dimitropoulos extern int vdev_standard_sm_blksz;
495f7170741SWill Andrews /* zdb uses this tunable, so it must be declared here to make lint happy. */
496e81c57b1Sahrens extern int zfs_vdev_cache_size;
497e81c57b1Sahrens 
4985cabbc6bSPrashanth Sreenivasa /*
4995cabbc6bSPrashanth Sreenivasa  * Functions from vdev_indirect.c
5005cabbc6bSPrashanth Sreenivasa  */
5015cabbc6bSPrashanth Sreenivasa extern void vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx);
5025cabbc6bSPrashanth Sreenivasa extern boolean_t vdev_indirect_should_condense(vdev_t *vd);
5035cabbc6bSPrashanth Sreenivasa extern void spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx);
5045cabbc6bSPrashanth Sreenivasa extern int vdev_obsolete_sm_object(vdev_t *vd);
5055cabbc6bSPrashanth Sreenivasa extern boolean_t vdev_obsolete_counts_are_precise(vdev_t *vd);
5065cabbc6bSPrashanth Sreenivasa 
50786714001SSerapheim Dimitropoulos /*
50886714001SSerapheim Dimitropoulos  * Other miscellaneous functions
50986714001SSerapheim Dimitropoulos  */
51086714001SSerapheim Dimitropoulos int vdev_checkpoint_sm_object(vdev_t *vd);
51186714001SSerapheim Dimitropoulos 
51231d7e8faSGeorge Wilson /*
51331d7e8faSGeorge Wilson  * The vdev_buf_t is used to translate between zio_t and buf_t, and back again.
51431d7e8faSGeorge Wilson  */
51531d7e8faSGeorge Wilson typedef struct vdev_buf {
51631d7e8faSGeorge Wilson 	buf_t	vb_buf;		/* buffer that describes the io */
51731d7e8faSGeorge Wilson 	zio_t	*vb_io;		/* pointer back to the original zio_t */
51831d7e8faSGeorge Wilson } vdev_buf_t;
51931d7e8faSGeorge Wilson 
520fa9e4066Sahrens #ifdef	__cplusplus
521fa9e4066Sahrens }
522fa9e4066Sahrens #endif
523fa9e4066Sahrens 
524fa9e4066Sahrens #endif	/* _SYS_VDEV_IMPL_H */
525