1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5441d80alling * Common Development and Distribution License (the "License").
6441d80alling * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
2298d1cbfGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23f78cdc3Paul Dagnelie * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24814dcd4Serapheim Dimitropoulos * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
25663207aDon Brady * Copyright (c) 2017, Intel Corporation.
26084fd14Brian Behlendorf * Copyright 2019 Joyent, Inc.
2730c304dJoshua M. Clulow * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
28fa9e406ahrens */
29fa9e406ahrens
30fa9e406ahrens#ifndef _SYS_VDEV_IMPL_H
31fa9e406ahrens#define	_SYS_VDEV_IMPL_H
32fa9e406ahrens
33fa9e406ahrens#include <sys/avl.h>
345cabbc6Prashanth Sreenivasa#include <sys/bpobj.h>
35fa9e406ahrens#include <sys/dmu.h>
36fa9e406ahrens#include <sys/metaslab.h>
37fa9e406ahrens#include <sys/nvpair.h>
38fa9e406ahrens#include <sys/space_map.h>
39fa9e406ahrens#include <sys/vdev.h>
40fa9e406ahrens#include <sys/dkio.h>
41fa9e406ahrens#include <sys/uberblock_impl.h>
425cabbc6Prashanth Sreenivasa#include <sys/vdev_indirect_mapping.h>
435cabbc6Prashanth Sreenivasa#include <sys/vdev_indirect_births.h>
445cabbc6Prashanth Sreenivasa#include <sys/vdev_removal.h>
45fa9e406ahrens
46fa9e406ahrens#ifdef	__cplusplus
47fa9e406ahrensextern "C" {
48fa9e406ahrens#endif
49fa9e406ahrens
50fa9e406ahrens/*
51fa9e406ahrens * Virtual device descriptors.
52fa9e406ahrens *
53fa9e406ahrens * All storage pool operations go through the virtual device framework,
54fa9e406ahrens * which provides data replication and I/O scheduling.
55fa9e406ahrens */
56fa9e406ahrens
57fa9e406ahrens/*
58fa9e406ahrens * Forward declarations that lots of things need.
59fa9e406ahrens */
60fa9e406ahrenstypedef struct vdev_queue vdev_queue_t;
61fa9e406ahrenstypedef struct vdev_cache vdev_cache_t;
62fa9e406ahrenstypedef struct vdev_cache_entry vdev_cache_entry_t;
63770499eDan Kimmelstruct abd;
64fa9e406ahrens
650f7643cGeorge Wilsonextern int zfs_vdev_queue_depth_pct;
66f78cdc3Paul Dagnelieextern int zfs_vdev_def_queue_depth;
670f7643cGeorge Wilsonextern uint32_t zfs_vdev_async_write_max_active;
680f7643cGeorge Wilson
69fa9e406ahrens/*
70fa9e406ahrens * Virtual device operations
71fa9e406ahrens */
724263d13George Wilsontypedef int	vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *max_size,
734263d13George Wilson    uint64_t *ashift);
74fa9e406ahrenstypedef void	vdev_close_func_t(vdev_t *vd);
75fa9e406ahrenstypedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize);
76738f37bGeorge Wilsontypedef void	vdev_io_start_func_t(zio_t *zio);
77e14bb32Jeff Bonwicktypedef void	vdev_io_done_func_t(zio_t *zio);
78fa9e406ahrenstypedef void	vdev_state_change_func_t(vdev_t *vd, int, int);
79a3874b8Toomas Soometypedef boolean_t vdev_need_resilver_func_t(vdev_t *vd, uint64_t, size_t);
80dcba9f3George Wilsontypedef void	vdev_hold_func_t(vdev_t *vd);
81dcba9f3George Wilsontypedef void	vdev_rele_func_t(vdev_t *vd);
82fa9e406ahrens
835cabbc6Prashanth Sreenivasatypedef void	vdev_remap_cb_t(uint64_t inner_offset, vdev_t *vd,
845cabbc6Prashanth Sreenivasa    uint64_t offset, uint64_t size, void *arg);
855cabbc6Prashanth Sreenivasatypedef void	vdev_remap_func_t(vdev_t *vd, uint64_t offset, uint64_t size,
865cabbc6Prashanth Sreenivasa    vdev_remap_cb_t callback, void *arg);
87ac04831Mike Gerdtstypedef int	vdev_dumpio_func_t(vdev_t *vd, caddr_t data, size_t size,
88ac04831Mike Gerdts    uint64_t offset, uint64_t origoffset, boolean_t doread, boolean_t isdump);
89094e47eGeorge Wilson/*
90094e47eGeorge Wilson * Given a target vdev, translates the logical range "in" to the physical
91094e47eGeorge Wilson * range "res"
92094e47eGeorge Wilson */
934d7988dPaul Dagnelietypedef void vdev_xlation_func_t(vdev_t *cvd, const range_seg64_t *in,
944d7988dPaul Dagnelie    range_seg64_t *res);
955cabbc6Prashanth Sreenivasa
96fa9e406ahrenstypedef struct vdev_ops {
97fa9e406ahrens	vdev_open_func_t		*vdev_op_open;
98fa9e406ahrens	vdev_close_func_t		*vdev_op_close;
99fa9e406ahrens	vdev_asize_func_t		*vdev_op_asize;
100fa9e406ahrens	vdev_io_start_func_t		*vdev_op_io_start;
101fa9e406ahrens	vdev_io_done_func_t		*vdev_op_io_done;
102fa9e406ahrens	vdev_state_change_func_t	*vdev_op_state_change;
103a3874b8Toomas Soome	vdev_need_resilver_func_t	*vdev_op_need_resilver;
104dcba9f3George Wilson	vdev_hold_func_t		*vdev_op_hold;
105dcba9f3George Wilson	vdev_rele_func_t		*vdev_op_rele;
1065cabbc6Prashanth Sreenivasa	vdev_remap_func_t		*vdev_op_remap;
107094e47eGeorge Wilson	/*
108094e47eGeorge Wilson	 * For translating ranges from non-leaf vdevs (e.g. raidz) to leaves.
109094e47eGeorge Wilson	 * Used when initializing vdevs. Isn't used by leaf ops.
110094e47eGeorge Wilson	 */
111094e47eGeorge Wilson	vdev_xlation_func_t		*vdev_op_xlate;
112ac04831Mike Gerdts	vdev_dumpio_func_t		*vdev_op_dumpio;
113fa9e406ahrens	char				vdev_op_type[16];
114fa9e406ahrens	boolean_t			vdev_op_leaf;
115fa9e406ahrens} vdev_ops_t;
116fa9e406ahrens
117fa9e406ahrens/*
118fa9e406ahrens * Virtual device properties
119fa9e406ahrens */
120fa9e406ahrensstruct vdev_cache_entry {
121770499eDan Kimmel	struct abd	*ve_abd;
122fa9e406ahrens	uint64_t	ve_offset;
123fa9e406ahrens	uint64_t	ve_lastused;
124fa9e406ahrens	avl_node_t	ve_offset_node;
125fa9e406ahrens	avl_node_t	ve_lastused_node;
126fa9e406ahrens	uint32_t	ve_hits;
127fa9e406ahrens	uint16_t	ve_missed_update;
128fa9e406ahrens	zio_t		*ve_fill_io;
129fa9e406ahrens};
130fa9e406ahrens
131fa9e406ahrensstruct vdev_cache {
132fa9e406ahrens	avl_tree_t	vc_offset_tree;
133fa9e406ahrens	avl_tree_t	vc_lastused_tree;
134fa9e406ahrens	kmutex_t	vc_lock;
135fa9e406ahrens};
136fa9e406ahrens
13769962b5Matthew Ahrenstypedef struct vdev_queue_class {
13869962b5Matthew Ahrens	uint32_t	vqc_active;
13969962b5Matthew Ahrens
14069962b5Matthew Ahrens	/*
14169962b5Matthew Ahrens	 * Sorted by offset or timestamp, depending on if the queue is
14269962b5Matthew Ahrens	 * LBA-ordered vs FIFO.
14369962b5Matthew Ahrens	 */
14469962b5Matthew Ahrens	avl_tree_t	vqc_queued_tree;
14569962b5Matthew Ahrens} vdev_queue_class_t;
14669962b5Matthew Ahrens
147fa9e406ahrensstruct vdev_queue {
14869962b5Matthew Ahrens	vdev_t		*vq_vdev;
14969962b5Matthew Ahrens	vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
15069962b5Matthew Ahrens	avl_tree_t	vq_active_tree;
151fe31923Justin T. Gibbs	avl_tree_t	vq_read_offset_tree;
152fe31923Justin T. Gibbs	avl_tree_t	vq_write_offset_tree;
153084fd14Brian Behlendorf	avl_tree_t	vq_trim_offset_tree;
15469962b5Matthew Ahrens	uint64_t	vq_last_offset;
15569962b5Matthew Ahrens	hrtime_t	vq_io_complete_ts; /* time last i/o completed */
156fa9e406ahrens	kmutex_t	vq_lock;
157fa9e406ahrens};
158fa9e406ahrens
159663207aDon Bradytypedef enum vdev_alloc_bias {
160663207aDon Brady	VDEV_BIAS_NONE,
161663207aDon Brady	VDEV_BIAS_LOG,		/* dedicated to ZIL data (SLOG) */
162663207aDon Brady	VDEV_BIAS_SPECIAL,	/* dedicated to ddt, metadata, and small blks */
163663207aDon Brady	VDEV_BIAS_DEDUP		/* dedicated to dedup metadata */
164663207aDon Brady} vdev_alloc_bias_t;
165663207aDon Brady
166663207aDon Brady
167fa9e406ahrens/*
1685cabbc6Prashanth Sreenivasa * On-disk indirect vdev state.
1695cabbc6Prashanth Sreenivasa *
1705cabbc6Prashanth Sreenivasa * An indirect vdev is described exclusively in the MOS config of a pool.
1715cabbc6Prashanth Sreenivasa * The config for an indirect vdev includes several fields, which are
1725cabbc6Prashanth Sreenivasa * accessed in memory by a vdev_indirect_config_t.
1735cabbc6Prashanth Sreenivasa */
1745cabbc6Prashanth Sreenivasatypedef struct vdev_indirect_config {
1755cabbc6Prashanth Sreenivasa	/*
1765cabbc6Prashanth Sreenivasa	 * Object (in MOS) which contains the indirect mapping. This object
1775cabbc6Prashanth Sreenivasa	 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
1785cabbc6Prashanth Sreenivasa	 * vimep_src. The bonus buffer for this object is a
1795cabbc6Prashanth Sreenivasa	 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
1805cabbc6Prashanth Sreenivasa	 * removal is initiated.
1815cabbc6Prashanth Sreenivasa	 *
1825cabbc6Prashanth Sreenivasa	 * Note that this object can be empty if none of the data on the vdev
1835cabbc6Prashanth Sreenivasa	 * has been copied yet.
1845cabbc6Prashanth Sreenivasa	 */
1855cabbc6Prashanth Sreenivasa	uint64_t	vic_mapping_object;
1865cabbc6Prashanth Sreenivasa
1875cabbc6Prashanth Sreenivasa	/*
1885cabbc6Prashanth Sreenivasa	 * Object (in MOS) which contains the birth times for the mapping
1895cabbc6Prashanth Sreenivasa	 * entries. This object contains an array of
1905cabbc6Prashanth Sreenivasa	 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
1915cabbc6Prashanth Sreenivasa	 * buffer for this object is a vdev_indirect_birth_phys_t. This object
1925cabbc6Prashanth Sreenivasa	 * is allocated when a vdev removal is initiated.
1935cabbc6Prashanth Sreenivasa	 *
1945cabbc6Prashanth Sreenivasa	 * Note that this object can be empty if none of the vdev has yet been
1955cabbc6Prashanth Sreenivasa	 * copied.
1965cabbc6Prashanth Sreenivasa	 */
1975cabbc6Prashanth Sreenivasa	uint64_t	vic_births_object;
1985cabbc6Prashanth Sreenivasa
1995cabbc6Prashanth Sreenivasa	/*
2005cabbc6Prashanth Sreenivasa	 * This is the vdev ID which was removed previous to this vdev, or
2015cabbc6Prashanth Sreenivasa	 * UINT64_MAX if there are no previously removed vdevs.
2025cabbc6Prashanth Sreenivasa	 */
2035cabbc6Prashanth Sreenivasa	uint64_t	vic_prev_indirect_vdev;
2045cabbc6Prashanth Sreenivasa} vdev_indirect_config_t;
2055cabbc6Prashanth Sreenivasa
2065cabbc6Prashanth Sreenivasa/*
207fa9e406ahrens * Virtual device descriptor
208fa9e406ahrens */
209fa9e406ahrensstruct vdev {
210fa9e406ahrens	/*
211fa9e406ahrens	 * Common to all vdev types.
212fa9e406ahrens	 */
213fa9e406ahrens	uint64_t	vdev_id;	/* child number in vdev parent	*/
214fa9e406ahrens	uint64_t	vdev_guid;	/* unique ID for this vdev	*/
215fa9e406ahrens	uint64_t	vdev_guid_sum;	/* self guid + all child guids	*/
2161195e68Mark J Musante	uint64_t	vdev_orig_guid;	/* orig. guid prior to remove	*/
217fa9e406ahrens	uint64_t	vdev_asize;	/* allocatable device capacity	*/
218573ca77George Wilson	uint64_t	vdev_min_asize;	/* min acceptable asize		*/
2194263d13George Wilson	uint64_t	vdev_max_asize;	/* max acceptable asize		*/
220fa9e406ahrens	uint64_t	vdev_ashift;	/* block alignment shift	*/
221fa9e406ahrens	uint64_t	vdev_state;	/* see VDEV_STATE_* #defines	*/
222560e6e9eschrock	uint64_t	vdev_prevstate;	/* used when reopening a vdev	*/
223fa9e406ahrens	vdev_ops_t	*vdev_ops;	/* vdev operations		*/
224fa9e406ahrens	spa_t		*vdev_spa;	/* spa for this vdev		*/
225fa9e406ahrens	void		*vdev_tsd;	/* type-specific data		*/
226dcba9f3George Wilson	vnode_t		*vdev_name_vp;	/* vnode for pathname		*/
227dcba9f3George Wilson	vnode_t		*vdev_devid_vp;	/* vnode for devid		*/
228fa9e406ahrens	vdev_t		*vdev_top;	/* top-level vdev		*/
229fa9e406ahrens	vdev_t		*vdev_parent;	/* parent vdev			*/
230fa9e406ahrens	vdev_t		**vdev_child;	/* array of children		*/
231fa9e406ahrens	uint64_t	vdev_children;	/* number of children		*/
232fa9e406ahrens	vdev_stat_t	vdev_stat;	/* virtual device statistics	*/
233dd50e0cTony Hutter	vdev_stat_ex_t	vdev_stat_ex;	/* extended statistics		*/
234573ca77George Wilson	boolean_t	vdev_expanding;	/* expand the vdev?		*/
235095bcd6George Wilson	boolean_t	vdev_reopening;	/* reopen in progress?		*/
23612a8814Tom Caputi	boolean_t	vdev_nonrot;	/* true if solid state		*/
237f64c0e3Eric Taylor	int		vdev_open_error; /* error on last open		*/
238f64c0e3Eric Taylor	kthread_t	*vdev_open_thread; /* thread opening children	*/
23988ecc94George Wilson	uint64_t	vdev_crtxg;	/* txg when top-level was added */
240fa9e406ahrens
241fa9e406ahrens	/*
242fa9e406ahrens	 * Top-level vdev state.
243fa9e406ahrens	 */
244fa9e406ahrens	uint64_t	vdev_ms_array;	/* metaslab array object	*/
245fa9e406ahrens	uint64_t	vdev_ms_shift;	/* metaslab size shift		*/
246fa9e406ahrens	uint64_t	vdev_ms_count;	/* number of metaslabs		*/
247fa9e406ahrens	metaslab_group_t *vdev_mg;	/* metaslab group		*/
248fa9e406ahrens	metaslab_t	**vdev_ms;	/* metaslab array		*/
249fa9e406ahrens	txg_list_t	vdev_ms_list;	/* per-txg dirty metaslab lists	*/
250fa9e406ahrens	txg_list_t	vdev_dtl_list;	/* per-txg dirty DTL lists	*/
251fa9e406ahrens	txg_node_t	vdev_txg_node;	/* per-txg dirty vdev linkage	*/
2523d7072feschrock	boolean_t	vdev_remove_wanted; /* async remove wanted?	*/
253e14bb32Jeff Bonwick	boolean_t	vdev_probe_wanted; /* async probe wanted?	*/
254e14bb32Jeff Bonwick	list_node_t	vdev_config_dirty_node; /* config dirty list	*/
255e14bb32Jeff Bonwick	list_node_t	vdev_state_dirty_node; /* state dirty list	*/
25699653d4eschrock	uint64_t	vdev_deflate_ratio; /* deflation ratio (x512)	*/
2578654d02perrin	uint64_t	vdev_islog;	/* is an intent log device	*/
2580713e23George Wilson	uint64_t	vdev_removing;	/* device is being removed?	*/
2590f7643cGeorge Wilson	boolean_t	vdev_ishole;	/* is a hole in the namespace	*/
260215198aJoe Stein	uint64_t	vdev_top_zap;
261663207aDon Brady	vdev_alloc_bias_t vdev_alloc_bias; /* metaslab allocation bias	*/
262fa9e406ahrens
2638671400Serapheim Dimitropoulos	/* pool checkpoint related */
2648671400Serapheim Dimitropoulos	space_map_t	*vdev_checkpoint_sm;	/* contains reserved blocks */
265e0f1c0aOlaf Faaland
266084fd14Brian Behlendorf	/* Initialize related */
267094e47eGeorge Wilson	boolean_t	vdev_initialize_exit_wanted;
268094e47eGeorge Wilson	vdev_initializing_state_t	vdev_initialize_state;
269084fd14Brian Behlendorf	list_node_t	vdev_initialize_node;
270094e47eGeorge Wilson	kthread_t	*vdev_initialize_thread;
271094e47eGeorge Wilson	/* Protects vdev_initialize_thread and vdev_initialize_state. */
272094e47eGeorge Wilson	kmutex_t	vdev_initialize_lock;
273094e47eGeorge Wilson	kcondvar_t	vdev_initialize_cv;
274094e47eGeorge Wilson	uint64_t	vdev_initialize_offset[TXG_SIZE];
275094e47eGeorge Wilson	uint64_t	vdev_initialize_last_offset;
276094e47eGeorge Wilson	range_tree_t	*vdev_initialize_tree;	/* valid while initializing */
277094e47eGeorge Wilson	uint64_t	vdev_initialize_bytes_est;
278094e47eGeorge Wilson	uint64_t	vdev_initialize_bytes_done;
279094e47eGeorge Wilson	time_t		vdev_initialize_action_time;	/* start and end time */
280094e47eGeorge Wilson
281084fd14Brian Behlendorf	/* TRIM related */
282084fd14Brian Behlendorf	boolean_t	vdev_trim_exit_wanted;
283084fd14Brian Behlendorf	boolean_t	vdev_autotrim_exit_wanted;
284084fd14Brian Behlendorf	vdev_trim_state_t	vdev_trim_state;
285084fd14Brian Behlendorf	list_node_t	vdev_trim_node;
286084fd14Brian Behlendorf	kmutex_t	vdev_autotrim_lock;
287084fd14Brian Behlendorf	kcondvar_t	vdev_autotrim_cv;
288084fd14Brian Behlendorf	kthread_t	*vdev_autotrim_thread;
289084fd14Brian Behlendorf	/* Protects vdev_trim_thread and vdev_trim_state. */
290084fd14Brian Behlendorf	kmutex_t	vdev_trim_lock;
291084fd14Brian Behlendorf	kcondvar_t	vdev_trim_cv;
292084fd14Brian Behlendorf	kthread_t	*vdev_trim_thread;
293084fd14Brian Behlendorf	uint64_t	vdev_trim_offset[TXG_SIZE];
294084fd14Brian Behlendorf	uint64_t	vdev_trim_last_offset;
295084fd14Brian Behlendorf	uint64_t	vdev_trim_bytes_est;
296084fd14Brian Behlendorf	uint64_t	vdev_trim_bytes_done;
297084fd14Brian Behlendorf	uint64_t	vdev_trim_rate;		/* requested rate (bytes/sec) */
298084fd14Brian Behlendorf	uint64_t	vdev_trim_partial;	/* requested partial TRIM */
299084fd14Brian Behlendorf	uint64_t	vdev_trim_secure;	/* requested secure TRIM */
300084fd14Brian Behlendorf	time_t		vdev_trim_action_time;	/* start and end time */
301084fd14Brian Behlendorf
302084fd14Brian Behlendorf	/* The following is not in ZoL, but used for auto-trim test progress */
303084fd14Brian Behlendorf	uint64_t	vdev_autotrim_bytes_done;
304084fd14Brian Behlendorf
305084fd14Brian Behlendorf	/* for limiting outstanding I/Os (initialize and TRIM) */
306094e47eGeorge Wilson	kmutex_t	vdev_initialize_io_lock;
307094e47eGeorge Wilson	kcondvar_t	vdev_initialize_io_cv;
308094e47eGeorge Wilson	uint64_t	vdev_initialize_inflight;
309084fd14Brian Behlendorf	kmutex_t	vdev_trim_io_lock;
310084fd14Brian Behlendorf	kcondvar_t	vdev_trim_io_cv;
311084fd14Brian Behlendorf	uint64_t	vdev_trim_inflight[2];
3128671400Serapheim Dimitropoulos
313fa9e406ahrens	/*
3145cabbc6Prashanth Sreenivasa	 * Values stored in the config for an indirect or removing vdev.
3155cabbc6Prashanth Sreenivasa	 */
3165cabbc6Prashanth Sreenivasa	vdev_indirect_config_t	vdev_indirect_config;
3175cabbc6Prashanth Sreenivasa
3185cabbc6Prashanth Sreenivasa	/*
3195cabbc6Prashanth Sreenivasa	 * The vdev_indirect_rwlock protects the vdev_indirect_mapping
3205cabbc6Prashanth Sreenivasa	 * pointer from changing on indirect vdevs (when it is condensed).
3215cabbc6Prashanth Sreenivasa	 * Note that removing (not yet indirect) vdevs have different
3225cabbc6Prashanth Sreenivasa	 * access patterns (the mapping is not accessed from open context,
3235cabbc6Prashanth Sreenivasa	 * e.g. from zio_read) and locking strategy (e.g. svr_lock).
3245cabbc6Prashanth Sreenivasa	 */
3255cabbc6Prashanth Sreenivasa	krwlock_t vdev_indirect_rwlock;
3265cabbc6Prashanth Sreenivasa	vdev_indirect_mapping_t *vdev_indirect_mapping;
3275cabbc6Prashanth Sreenivasa	vdev_indirect_births_t *vdev_indirect_births;
3285cabbc6Prashanth Sreenivasa
3295cabbc6Prashanth Sreenivasa	/*
3305cabbc6Prashanth Sreenivasa	 * In memory data structures used to manage the obsolete sm, for
3315cabbc6Prashanth Sreenivasa	 * indirect or removing vdevs.
3325cabbc6Prashanth Sreenivasa	 *
3335cabbc6Prashanth Sreenivasa	 * The vdev_obsolete_segments is the in-core record of the segments
3345cabbc6Prashanth Sreenivasa	 * that are no longer referenced anywhere in the pool (due to
3355cabbc6Prashanth Sreenivasa	 * being freed or remapped and not referenced by any snapshots).
3365cabbc6Prashanth Sreenivasa	 * During a sync, segments are added to vdev_obsolete_segments
3375cabbc6Prashanth Sreenivasa	 * via vdev_indirect_mark_obsolete(); at the end of each sync
3385cabbc6Prashanth Sreenivasa	 * pass, this is appended to vdev_obsolete_sm via
3395cabbc6Prashanth Sreenivasa	 * vdev_indirect_sync_obsolete().  The vdev_obsolete_lock
3405cabbc6Prashanth Sreenivasa	 * protects against concurrent modifications of vdev_obsolete_segments
3415cabbc6Prashanth Sreenivasa	 * from multiple zio threads.
3425cabbc6Prashanth Sreenivasa	 */
3435cabbc6Prashanth Sreenivasa	kmutex_t	vdev_obsolete_lock;
3445cabbc6Prashanth Sreenivasa	range_tree_t	*vdev_obsolete_segments;
3455cabbc6Prashanth Sreenivasa	space_map_t	*vdev_obsolete_sm;
3465cabbc6Prashanth Sreenivasa
3475cabbc6Prashanth Sreenivasa	/*
348a3874b8Toomas Soome	 * Protects the vdev_scan_io_queue field itself as well as the
349a3874b8Toomas Soome	 * structure's contents (when present).
350a3874b8Toomas Soome	 */
351a3874b8Toomas Soome	kmutex_t	vdev_scan_io_queue_lock;
352a3874b8Toomas Soome	struct dsl_scan_io_queue	*vdev_scan_io_queue;
353a3874b8Toomas Soome
354a3874b8Toomas Soome	/*
355fa9e406ahrens	 * Leaf vdev state.
356fa9e406ahrens	 */
3570713e23George Wilson	range_tree_t	*vdev_dtl[DTL_TYPES]; /* dirty time logs	*/
3580713e23George Wilson	space_map_t	*vdev_dtl_sm;	/* dirty time log space map	*/
359fa9e406ahrens	txg_node_t	vdev_dtl_node;	/* per-txg dirty DTL linkage	*/
3600713e23George Wilson	uint64_t	vdev_dtl_object; /* DTL object			*/
3610713e23George Wilson	uint64_t	vdev_psize;	/* physical device capacity	*/
362afefbcdeschrock	uint64_t	vdev_wholedisk;	/* true if this is a whole disk */
3633d7072feschrock	uint64_t	vdev_offline;	/* persistent offline state	*/
3643d7072feschrock	uint64_t	vdev_faulted;	/* persistent faulted state	*/
3653d7072feschrock	uint64_t	vdev_degraded;	/* persistent degraded state	*/
3663d7072feschrock	uint64_t	vdev_removed;	/* persistent removed state	*/
367b4952e1George Wilson	uint64_t	vdev_resilver_txg; /* persistent resilvering state */
36899653d4eschrock	uint64_t	vdev_nparity;	/* number of parity devices for raidz */
369fa9e406ahrens	char		*vdev_path;	/* vdev path (if any)		*/
370fa9e406ahrens	char		*vdev_devid;	/* vdev devid (if any)		*/
3713d7072feschrock	char		*vdev_physpath;	/* vdev device path (if any)	*/
3726809eb4Eric Schrock	char		*vdev_fru;	/* physical FRU location	*/
373e14bb32Jeff Bonwick	uint64_t	vdev_not_present; /* not present during import	*/
374e14bb32Jeff Bonwick	uint64_t	vdev_unspare;	/* unspare when resilvering done */
375e14bb32Jeff Bonwick	boolean_t	vdev_nowritecache; /* true if flushwritecache failed */
376084fd14Brian Behlendorf	boolean_t	vdev_has_trim;	/* TRIM is supported		*/
377084fd14Brian Behlendorf	boolean_t	vdev_has_securetrim; /* secure TRIM is supported */
378e14bb32Jeff Bonwick	boolean_t	vdev_checkremove; /* temporary online test	*/
379e14bb32Jeff Bonwick	boolean_t	vdev_forcefault; /* force online fault		*/
3801195e68Mark J Musante	boolean_t	vdev_splitting;	/* split or repair in progress  */
38198d1cbfGeorge Wilson	boolean_t	vdev_delayed_close; /* delayed device close?	*/
3820713e23George Wilson	boolean_t	vdev_tmpoffline; /* device taken offline temporarily? */
3830713e23George Wilson	boolean_t	vdev_detached;	/* device detached?		*/
3840713e23George Wilson	boolean_t	vdev_cant_read;	/* vdev is failing all reads	*/
3850713e23George Wilson	boolean_t	vdev_cant_write; /* vdev is failing all writes	*/
3860713e23George Wilson	boolean_t	vdev_isspare;	/* was a hot spare		*/
3870713e23George Wilson	boolean_t	vdev_isl2cache;	/* was a l2cache device		*/
388e4c795bTom Caputi	boolean_t	vdev_resilver_deferred;  /* resilver deferred */
389fa9e406ahrens	vdev_queue_t	vdev_queue;	/* I/O deadline schedule queue	*/
390fa9e406ahrens	vdev_cache_t	vdev_cache;	/* physical block cache		*/
39181cd5c5Matthew Ahrens	spa_aux_vdev_t	*vdev_aux;	/* for l2cache and spares vdevs	*/
392e14bb32Jeff Bonwick	zio_t		*vdev_probe_zio; /* root of current probe	*/
393069f55eEric Schrock	vdev_aux_t	vdev_label_aux;	/* on-disk aux state		*/
394215198aJoe Stein	uint64_t	vdev_leaf_zap;
395e0f1c0aOlaf Faaland	hrtime_t	vdev_mmp_pending; /* 0 if write finished	*/
396e0f1c0aOlaf Faaland	uint64_t	vdev_mmp_kstat_id;	/* to find kstat entry */
397e0f1c0aOlaf Faaland	list_node_t	vdev_leaf_node;		/* leaf vdev list */
398fa9e406ahrens
399fa9e406ahrens	/*
400fa9e406ahrens	 * For DTrace to work in userland (libzpool) context, these fields must
401fa9e406ahrens	 * remain at the end of the structure.  DTrace will use the kernel's
402fa9e406ahrens	 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
403ad135b5Christopher Siden	 * larger in userland, the offsets for the rest of the fields would be
404fa9e406ahrens	 * incorrect.
405fa9e406ahrens	 */
406fa9e406ahrens	kmutex_t	vdev_dtl_lock;	/* vdev_dtl_{map,resilver}	*/
407fa9e406ahrens	kmutex_t	vdev_stat_lock;	/* vdev_stat			*/
408e14bb32Jeff Bonwick	kmutex_t	vdev_probe_lock; /* protects vdev_probe_zio	*/
409fa9e406ahrens};
410fa9e406ahrens
411b24ab67Jeff Bonwick#define	VDEV_RAIDZ_MAXPARITY	3
412b24ab67Jeff Bonwick
413f83ffe1Lin Ling#define	VDEV_PAD_SIZE		(8 << 10)
414f83ffe1Lin Ling/* 2 padding areas (vl_pad1 and vl_pad2) to skip */
415f83ffe1Lin Ling#define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
416fa9e406ahrens#define	VDEV_PHYS_SIZE		(112 << 10)
417ecc2d60bonwick#define	VDEV_UBERBLOCK_RING	(128 << 10)
418ecc2d60bonwick
419e0f1c0aOlaf Faaland/*
420e0f1c0aOlaf Faaland * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
421e0f1c0aOlaf Faaland * ring when MMP is enabled.
422e0f1c0aOlaf Faaland */
423e0f1c0aOlaf Faaland#define	MMP_BLOCKS_PER_LABEL	1
424e0f1c0aOlaf Faaland
4252a104a5Alex Reece/* The largest uberblock we support is 8k. */
4262a104a5Alex Reece#define	MAX_UBERBLOCK_SHIFT (13)
427ecc2d60bonwick#define	VDEV_UBERBLOCK_SHIFT(vd)	\
4282a104a5Alex Reece	MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
4292a104a5Alex Reece	    MAX_UBERBLOCK_SHIFT)
430ecc2d60bonwick#define	VDEV_UBERBLOCK_COUNT(vd)	\
431ecc2d60bonwick	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
432ecc2d60bonwick#define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
433ecc2d60bonwick	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
434ecc2d60bonwick#define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
435fa9e406ahrens
436fa9e406ahrenstypedef struct vdev_phys {
4376e1f5caNeil Perrin	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
4386e1f5caNeil Perrin	zio_eck_t	vp_zbt;
439fa9e406ahrens} vdev_phys_t;
440fa9e406ahrens
441fa9e406ahrenstypedef struct vdev_label {
442f83ffe1Lin Ling	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K */
443f83ffe1Lin Ling	char		vl_pad2[VDEV_PAD_SIZE];			/*  8K */
444ecc2d60bonwick	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
445ecc2d60bonwick	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
446fa9e406ahrens} vdev_label_t;							/* 256K total */
447fa9e406ahrens
448fa9e406ahrens/*
449ecc2d60bonwick * vdev_dirty() flags
450ecc2d60bonwick */
451ecc2d60bonwick#define	VDD_METASLAB	0x01
452ecc2d60bonwick#define	VDD_DTL		0x02
453ecc2d60bonwick
454f717074Will Andrews/* Offset of embedded boot loader region on each label */
455f717074Will Andrews#define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
456ecc2d60bonwick/*
457f717074Will Andrews * Size of embedded boot loader region on each label.
458fa9e406ahrens * The total size of the first two labels plus the boot area is 4MB.
459fa9e406ahrens */
460f717074Will Andrews#define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M */
461fa9e406ahrens
462fa9e406ahrens/*
463fa9e406ahrens * Size of label regions at the start and end of each leaf device.
464fa9e406ahrens */
465fa9e406ahrens#define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
466fa9e406ahrens#define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
467fa9e406ahrens#define	VDEV_LABELS		4
468ad135b5Christopher Siden#define	VDEV_BEST_LABEL		VDEV_LABELS
469fa9e406ahrens
470fa9e406ahrens#define	VDEV_ALLOC_LOAD		0
471fa9e406ahrens#define	VDEV_ALLOC_ADD		1
47299653d4eschrock#define	VDEV_ALLOC_SPARE	2
473fa94a07brendan#define	VDEV_ALLOC_L2CACHE	3
47421ecdf6Lin Ling#define	VDEV_ALLOC_ROOTPOOL	4
4751195e68Mark J Musante#define	VDEV_ALLOC_SPLIT	5
476cd0837cGeorge Wilson#define	VDEV_ALLOC_ATTACH	6
477fa9e406ahrens
478fa9e406ahrens/*
479fa9e406ahrens * Allocate or free a vdev
480fa9e406ahrens */
48188ecc94George Wilsonextern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
48288ecc94George Wilson    vdev_ops_t *ops);
48399653d4eschrockextern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
48499653d4eschrock    vdev_t *parent, uint_t id, int alloctype);
485fa9e406ahrensextern void vdev_free(vdev_t *vd);
486fa9e406ahrens
487fa9e406ahrens/*
488fa9e406ahrens * Add or remove children and parents
489fa9e406ahrens */
490fa9e406ahrensextern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
491fa9e406ahrensextern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
492fa9e406ahrensextern void vdev_compact_children(vdev_t *pvd);
493fa9e406ahrensextern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
494fa9e406ahrensextern void vdev_remove_parent(vdev_t *cvd);
495fa9e406ahrens
496fa9e406ahrens/*
497fa9e406ahrens * vdev sync load and sync
498fa9e406ahrens */
4994b964adGeorge Wilsonextern boolean_t vdev_log_state_valid(vdev_t *vd);
5005cabbc6Prashanth Sreenivasaextern int vdev_load(vdev_t *vd);
5010713e23George Wilsonextern int vdev_dtl_load(vdev_t *vd);
502fa9e406ahrensextern void vdev_sync(vdev_t *vd, uint64_t txg);
503fa9e406ahrensextern void vdev_sync_done(vdev_t *vd, uint64_t txg);
504ecc2d60bonwickextern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
5050713e23George Wilsonextern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
506fa9e406ahrens
507fa9e406ahrens/*
508fa9e406ahrens * Available vdev types.
509fa9e406ahrens */
510fa9e406ahrensextern vdev_ops_t vdev_root_ops;
511fa9e406ahrensextern vdev_ops_t vdev_mirror_ops;
512fa9e406ahrensextern vdev_ops_t vdev_replacing_ops;
513fa9e406ahrensextern vdev_ops_t vdev_raidz_ops;
514fa9e406ahrensextern vdev_ops_t vdev_disk_ops;
515fa9e406ahrensextern vdev_ops_t vdev_file_ops;
516fa9e406ahrensextern vdev_ops_t vdev_missing_ops;
51788ecc94George Wilsonextern vdev_ops_t vdev_hole_ops;
51899653d4eschrockextern vdev_ops_t vdev_spare_ops;
5195cabbc6Prashanth Sreenivasaextern vdev_ops_t vdev_indirect_ops;
520fa9e406ahrens
521fa9e406ahrens/*
5222a79c5flling * Common size functions
523fa9e406ahrens */
5244d7988dPaul Dagnelieextern void vdev_default_xlate(vdev_t *vd, const range_seg64_t *in,
5254d7988dPaul Dagnelie    range_seg64_t *out);
526fa9e406ahrensextern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
527573ca77George Wilsonextern uint64_t vdev_get_min_asize(vdev_t *vd);
528573ca77George Wilsonextern void vdev_set_min_asize(vdev_t *vd);
529fa9e406ahrens
530e81c57bahrens/*
531f717074Will Andrews * Global variables
532e81c57bahrens */
533814dcd4Serapheim Dimitropoulosextern int zfs_vdev_standard_sm_blksz;
534f717074Will Andrews/* zdb uses this tunable, so it must be declared here to make lint happy. */
535e81c57bahrensextern int zfs_vdev_cache_size;
536e81c57bahrens
53731d7e8fGeorge Wilson/*
5385cabbc6Prashanth Sreenivasa * Functions from vdev_indirect.c
5395cabbc6Prashanth Sreenivasa */
5405cabbc6Prashanth Sreenivasaextern void vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx);
5415cabbc6Prashanth Sreenivasaextern boolean_t vdev_indirect_should_condense(vdev_t *vd);
5425cabbc6Prashanth Sreenivasaextern void spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx);
5435cabbc6Prashanth Sreenivasaextern int vdev_obsolete_sm_object(vdev_t *vd);
5445cabbc6Prashanth Sreenivasaextern boolean_t vdev_obsolete_counts_are_precise(vdev_t *vd);
5455cabbc6Prashanth Sreenivasa
5465cabbc6Prashanth Sreenivasa/*
5478671400Serapheim Dimitropoulos * Other miscellaneous functions
5488671400Serapheim Dimitropoulos */
5498671400Serapheim Dimitropoulosint vdev_checkpoint_sm_object(vdev_t *vd);
5508671400Serapheim Dimitropoulos
5518671400Serapheim Dimitropoulos/*
55231d7e8fGeorge Wilson * The vdev_buf_t is used to translate between zio_t and buf_t, and back again.
55331d7e8fGeorge Wilson */
55431d7e8fGeorge Wilsontypedef struct vdev_buf {
55531d7e8fGeorge Wilson	buf_t	vb_buf;		/* buffer that describes the io */
55631d7e8fGeorge Wilson	zio_t	*vb_io;		/* pointer back to the original zio_t */
55731d7e8fGeorge Wilson} vdev_buf_t;
55831d7e8fGeorge Wilson
55930c304dJoshua M. Clulow/*
56030c304dJoshua M. Clulow * Support routines used during boot from a ZFS pool
56130c304dJoshua M. Clulow */
56230c304dJoshua M. Clulowextern int vdev_disk_read_rootlabel(const char *, const char *, nvlist_t **);
56330c304dJoshua M. Clulowextern void vdev_disk_preroot_init(void);
56430c304dJoshua M. Clulowextern void vdev_disk_preroot_fini(void);
56530c304dJoshua M. Clulowextern const char *vdev_disk_preroot_lookup(uint64_t, uint64_t);
56630c304dJoshua M. Clulow
567fa9e406ahrens#ifdef	__cplusplus
568fa9e406ahrens}
569fa9e406ahrens#endif
570fa9e406ahrens
571fa9e406ahrens#endif	/* _SYS_VDEV_IMPL_H */
572