vdev_impl.h revision 738f37bc3dcd61e8a893af0f2d466d76690b70ec
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 */
2298d1cbfGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
232a104a5Alex Reece * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24fa9e406ahrens */
26fa9e406ahrens#ifndef _SYS_VDEV_IMPL_H
27fa9e406ahrens#define	_SYS_VDEV_IMPL_H
29fa9e406ahrens#include <sys/avl.h>
30fa9e406ahrens#include <sys/dmu.h>
31fa9e406ahrens#include <sys/metaslab.h>
32fa9e406ahrens#include <sys/nvpair.h>
33fa9e406ahrens#include <sys/space_map.h>
34fa9e406ahrens#include <sys/vdev.h>
35fa9e406ahrens#include <sys/dkio.h>
36fa9e406ahrens#include <sys/uberblock_impl.h>
38fa9e406ahrens#ifdef	__cplusplus
39fa9e406ahrensextern "C" {
43fa9e406ahrens * Virtual device descriptors.
44fa9e406ahrens *
45fa9e406ahrens * All storage pool operations go through the virtual device framework,
46fa9e406ahrens * which provides data replication and I/O scheduling.
47fa9e406ahrens */
50fa9e406ahrens * Forward declarations that lots of things need.
51fa9e406ahrens */
52fa9e406ahrenstypedef struct vdev_queue vdev_queue_t;
53fa9e406ahrenstypedef struct vdev_cache vdev_cache_t;
54fa9e406ahrenstypedef struct vdev_cache_entry vdev_cache_entry_t;
57fa9e406ahrens * Virtual device operations
58fa9e406ahrens */
594263d13George Wilsontypedef int	vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *max_size,
604263d13George Wilson    uint64_t *ashift);
61fa9e406ahrenstypedef void	vdev_close_func_t(vdev_t *vd);
62fa9e406ahrenstypedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize);
63738f37bGeorge Wilsontypedef void	vdev_io_start_func_t(zio_t *zio);
64e14bb32Jeff Bonwicktypedef void	vdev_io_done_func_t(zio_t *zio);
65fa9e406ahrenstypedef void	vdev_state_change_func_t(vdev_t *vd, int, int);
66dcba9f3George Wilsontypedef void	vdev_hold_func_t(vdev_t *vd);
67dcba9f3George Wilsontypedef void	vdev_rele_func_t(vdev_t *vd);
69fa9e406ahrenstypedef struct vdev_ops {
70fa9e406ahrens	vdev_open_func_t		*vdev_op_open;
71fa9e406ahrens	vdev_close_func_t		*vdev_op_close;
72fa9e406ahrens	vdev_asize_func_t		*vdev_op_asize;
73fa9e406ahrens	vdev_io_start_func_t		*vdev_op_io_start;
74fa9e406ahrens	vdev_io_done_func_t		*vdev_op_io_done;
75fa9e406ahrens	vdev_state_change_func_t	*vdev_op_state_change;
76dcba9f3George Wilson	vdev_hold_func_t		*vdev_op_hold;
77dcba9f3George Wilson	vdev_rele_func_t		*vdev_op_rele;
78fa9e406ahrens	char				vdev_op_type[16];
79fa9e406ahrens	boolean_t			vdev_op_leaf;
80fa9e406ahrens} vdev_ops_t;
83fa9e406ahrens * Virtual device properties
84fa9e406ahrens */
85fa9e406ahrensstruct vdev_cache_entry {
86fa9e406ahrens	char		*ve_data;
87fa9e406ahrens	uint64_t	ve_offset;
88fa9e406ahrens	uint64_t	ve_lastused;
89fa9e406ahrens	avl_node_t	ve_offset_node;
90fa9e406ahrens	avl_node_t	ve_lastused_node;
91fa9e406ahrens	uint32_t	ve_hits;
92fa9e406ahrens	uint16_t	ve_missed_update;
93fa9e406ahrens	zio_t		*ve_fill_io;
96fa9e406ahrensstruct vdev_cache {
97fa9e406ahrens	avl_tree_t	vc_offset_tree;
98fa9e406ahrens	avl_tree_t	vc_lastused_tree;
99fa9e406ahrens	kmutex_t	vc_lock;
10269962b5Matthew Ahrenstypedef struct vdev_queue_class {
10369962b5Matthew Ahrens	uint32_t	vqc_active;
10469962b5Matthew Ahrens
10569962b5Matthew Ahrens	/*
10669962b5Matthew Ahrens	 * Sorted by offset or timestamp, depending on if the queue is
10769962b5Matthew Ahrens	 * LBA-ordered vs FIFO.
10869962b5Matthew Ahrens	 */
10969962b5Matthew Ahrens	avl_tree_t	vqc_queued_tree;
11069962b5Matthew Ahrens} vdev_queue_class_t;
11169962b5Matthew Ahrens
112fa9e406ahrensstruct vdev_queue {
11369962b5Matthew Ahrens	vdev_t		*vq_vdev;
11469962b5Matthew Ahrens	vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
11569962b5Matthew Ahrens	avl_tree_t	vq_active_tree;
11669962b5Matthew Ahrens	uint64_t	vq_last_offset;
11769962b5Matthew Ahrens	hrtime_t	vq_io_complete_ts; /* time last i/o completed */
118fa9e406ahrens	kmutex_t	vq_lock;
122fa9e406ahrens * Virtual device descriptor
123fa9e406ahrens */
124fa9e406ahrensstruct vdev {
125fa9e406ahrens	/*
126fa9e406ahrens	 * Common to all vdev types.
127fa9e406ahrens	 */
128fa9e406ahrens	uint64_t	vdev_id;	/* child number in vdev parent	*/
129fa9e406ahrens	uint64_t	vdev_guid;	/* unique ID for this vdev	*/
130fa9e406ahrens	uint64_t	vdev_guid_sum;	/* self guid + all child guids	*/
1311195e68Mark J Musante	uint64_t	vdev_orig_guid;	/* orig. guid prior to remove	*/
132fa9e406ahrens	uint64_t	vdev_asize;	/* allocatable device capacity	*/
133573ca77George Wilson	uint64_t	vdev_min_asize;	/* min acceptable asize		*/
1344263d13George Wilson	uint64_t	vdev_max_asize;	/* max acceptable asize		*/
135fa9e406ahrens	uint64_t	vdev_ashift;	/* block alignment shift	*/
136fa9e406ahrens	uint64_t	vdev_state;	/* see VDEV_STATE_* #defines	*/
137560e6e9eschrock	uint64_t	vdev_prevstate;	/* used when reopening a vdev	*/
138fa9e406ahrens	vdev_ops_t	*vdev_ops;	/* vdev operations		*/
139fa9e406ahrens	spa_t		*vdev_spa;	/* spa for this vdev		*/
140fa9e406ahrens	void		*vdev_tsd;	/* type-specific data		*/
141dcba9f3George Wilson	vnode_t		*vdev_name_vp;	/* vnode for pathname		*/
142dcba9f3George Wilson	vnode_t		*vdev_devid_vp;	/* vnode for devid		*/
143fa9e406ahrens	vdev_t		*vdev_top;	/* top-level vdev		*/
144fa9e406ahrens	vdev_t		*vdev_parent;	/* parent vdev			*/
145fa9e406ahrens	vdev_t		**vdev_child;	/* array of children		*/
146fa9e406ahrens	uint64_t	vdev_children;	/* number of children		*/
147fa9e406ahrens	vdev_stat_t	vdev_stat;	/* virtual device statistics	*/
148573ca77George Wilson	boolean_t	vdev_expanding;	/* expand the vdev?		*/
149095bcd6George Wilson	boolean_t	vdev_reopening;	/* reopen in progress?		*/
150f64c0e3Eric Taylor	int		vdev_open_error; /* error on last open		*/
151f64c0e3Eric Taylor	kthread_t	*vdev_open_thread; /* thread opening children	*/
15288ecc94George Wilson	uint64_t	vdev_crtxg;	/* txg when top-level was added */
154fa9e406ahrens	/*
155fa9e406ahrens	 * Top-level vdev state.
156fa9e406ahrens	 */
157fa9e406ahrens	uint64_t	vdev_ms_array;	/* metaslab array object	*/
158fa9e406ahrens	uint64_t	vdev_ms_shift;	/* metaslab size shift		*/
159fa9e406ahrens	uint64_t	vdev_ms_count;	/* number of metaslabs		*/
160fa9e406ahrens	metaslab_group_t *vdev_mg;	/* metaslab group		*/
161fa9e406ahrens	metaslab_t	**vdev_ms;	/* metaslab array		*/
162fa9e406ahrens	txg_list_t	vdev_ms_list;	/* per-txg dirty metaslab lists	*/
163fa9e406ahrens	txg_list_t	vdev_dtl_list;	/* per-txg dirty DTL lists	*/
164fa9e406ahrens	txg_node_t	vdev_txg_node;	/* per-txg dirty vdev linkage	*/
1653d7072feschrock	boolean_t	vdev_remove_wanted; /* async remove wanted?	*/
166e14bb32Jeff Bonwick	boolean_t	vdev_probe_wanted; /* async probe wanted?	*/
167e14bb32Jeff Bonwick	list_node_t	vdev_config_dirty_node; /* config dirty list	*/
168e14bb32Jeff Bonwick	list_node_t	vdev_state_dirty_node; /* state dirty list	*/
16999653d4eschrock	uint64_t	vdev_deflate_ratio; /* deflation ratio (x512)	*/
1708654d02perrin	uint64_t	vdev_islog;	/* is an intent log device	*/
1710713e23George Wilson	uint64_t	vdev_removing;	/* device is being removed?	*/
1720713e23George Wilson	boolean_t	vdev_ishole;	/* is a hole in the namespace 	*/
174fa9e406ahrens	/*
175fa9e406ahrens	 * Leaf vdev state.
176fa9e406ahrens	 */
1770713e23George Wilson	range_tree_t	*vdev_dtl[DTL_TYPES]; /* dirty time logs	*/
1780713e23George Wilson	space_map_t	*vdev_dtl_sm;	/* dirty time log space map	*/
179fa9e406ahrens	txg_node_t	vdev_dtl_node;	/* per-txg dirty DTL linkage	*/
1800713e23George Wilson	uint64_t	vdev_dtl_object; /* DTL object			*/
1810713e23George Wilson	uint64_t	vdev_psize;	/* physical device capacity	*/
182afefbcdeschrock	uint64_t	vdev_wholedisk;	/* true if this is a whole disk */
1833d7072feschrock	uint64_t	vdev_offline;	/* persistent offline state	*/
1843d7072feschrock	uint64_t	vdev_faulted;	/* persistent faulted state	*/
1853d7072feschrock	uint64_t	vdev_degraded;	/* persistent degraded state	*/
1863d7072feschrock	uint64_t	vdev_removed;	/* persistent removed state	*/
187b4952e1George Wilson	uint64_t	vdev_resilver_txg; /* persistent resilvering state */
18899653d4eschrock	uint64_t	vdev_nparity;	/* number of parity devices for raidz */
189fa9e406ahrens	char		*vdev_path;	/* vdev path (if any)		*/
190fa9e406ahrens	char		*vdev_devid;	/* vdev devid (if any)		*/
1913d7072feschrock	char		*vdev_physpath;	/* vdev device path (if any)	*/
1926809eb4Eric Schrock	char		*vdev_fru;	/* physical FRU location	*/
193e14bb32Jeff Bonwick	uint64_t	vdev_not_present; /* not present during import	*/
194e14bb32Jeff Bonwick	uint64_t	vdev_unspare;	/* unspare when resilvering done */
195e14bb32Jeff Bonwick	boolean_t	vdev_nowritecache; /* true if flushwritecache failed */
196e14bb32Jeff Bonwick	boolean_t	vdev_checkremove; /* temporary online test	*/
197e14bb32Jeff Bonwick	boolean_t	vdev_forcefault; /* force online fault		*/
1981195e68Mark J Musante	boolean_t	vdev_splitting;	/* split or repair in progress  */
19998d1cbfGeorge Wilson	boolean_t	vdev_delayed_close; /* delayed device close?	*/
2000713e23George Wilson	boolean_t	vdev_tmpoffline; /* device taken offline temporarily? */
2010713e23George Wilson	boolean_t	vdev_detached;	/* device detached?		*/
2020713e23George Wilson	boolean_t	vdev_cant_read;	/* vdev is failing all reads	*/
2030713e23George Wilson	boolean_t	vdev_cant_write; /* vdev is failing all writes	*/
2040713e23George Wilson	boolean_t	vdev_isspare;	/* was a hot spare		*/
2050713e23George Wilson	boolean_t	vdev_isl2cache;	/* was a l2cache device		*/
206fa9e406ahrens	vdev_queue_t	vdev_queue;	/* I/O deadline schedule queue	*/
207fa9e406ahrens	vdev_cache_t	vdev_cache;	/* physical block cache		*/
208c5904d1eschrock	spa_aux_vdev_t	*vdev_aux;	/* for l2cache vdevs		*/
209e14bb32Jeff Bonwick	zio_t		*vdev_probe_zio; /* root of current probe	*/
210069f55eEric Schrock	vdev_aux_t	vdev_label_aux;	/* on-disk aux state		*/
212fa9e406ahrens	/*
213fa9e406ahrens	 * For DTrace to work in userland (libzpool) context, these fields must
214fa9e406ahrens	 * remain at the end of the structure.  DTrace will use the kernel's
215fa9e406ahrens	 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
216ad135b5Christopher Siden	 * larger in userland, the offsets for the rest of the fields would be
217fa9e406ahrens	 * incorrect.
218fa9e406ahrens	 */
219fa9e406ahrens	kmutex_t	vdev_dtl_lock;	/* vdev_dtl_{map,resilver}	*/
220fa9e406ahrens	kmutex_t	vdev_stat_lock;	/* vdev_stat			*/
221e14bb32Jeff Bonwick	kmutex_t	vdev_probe_lock; /* protects vdev_probe_zio	*/
224b24ab67Jeff Bonwick#define	VDEV_RAIDZ_MAXPARITY	3
225b24ab67Jeff Bonwick
226f83ffe1Lin Ling#define	VDEV_PAD_SIZE		(8 << 10)
227f83ffe1Lin Ling/* 2 padding areas (vl_pad1 and vl_pad2) to skip */
228f83ffe1Lin Ling#define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
229fa9e406ahrens#define	VDEV_PHYS_SIZE		(112 << 10)
230ecc2d60bonwick#define	VDEV_UBERBLOCK_RING	(128 << 10)
2322a104a5Alex Reece/* The largest uberblock we support is 8k. */
2332a104a5Alex Reece#define	MAX_UBERBLOCK_SHIFT (13)
234ecc2d60bonwick#define	VDEV_UBERBLOCK_SHIFT(vd)	\
2352a104a5Alex Reece	MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
2362a104a5Alex Reece	    MAX_UBERBLOCK_SHIFT)
237ecc2d60bonwick#define	VDEV_UBERBLOCK_COUNT(vd)	\
239ecc2d60bonwick#define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
240ecc2d60bonwick	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
241ecc2d60bonwick#define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
243fa9e406ahrenstypedef struct vdev_phys {
2446e1f5caNeil Perrin	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
2456e1f5caNeil Perrin	zio_eck_t	vp_zbt;
246fa9e406ahrens} vdev_phys_t;
248fa9e406ahrenstypedef struct vdev_label {
249f83ffe1Lin Ling	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K */
250f83ffe1Lin Ling	char		vl_pad2[VDEV_PAD_SIZE];			/*  8K */
251ecc2d60bonwick	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
252ecc2d60bonwick	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
253fa9e406ahrens} vdev_label_t;							/* 256K total */
256ecc2d60bonwick * vdev_dirty() flags
257ecc2d60bonwick */
258ecc2d60bonwick#define	VDD_METASLAB	0x01
259ecc2d60bonwick#define	VDD_DTL		0x02
261f717074Will Andrews/* Offset of embedded boot loader region on each label */
262f717074Will Andrews#define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
264f717074Will Andrews * Size of embedded boot loader region on each label.
265fa9e406ahrens * The total size of the first two labels plus the boot area is 4MB.
266fa9e406ahrens */
267f717074Will Andrews#define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M */
270fa9e406ahrens * Size of label regions at the start and end of each leaf device.
271fa9e406ahrens */
272fa9e406ahrens#define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
273fa9e406ahrens#define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
274fa9e406ahrens#define	VDEV_LABELS		4
275ad135b5Christopher Siden#define	VDEV_BEST_LABEL		VDEV_LABELS
277fa9e406ahrens#define	VDEV_ALLOC_LOAD		0
278fa9e406ahrens#define	VDEV_ALLOC_ADD		1
27999653d4eschrock#define	VDEV_ALLOC_SPARE	2
280fa94a07brendan#define	VDEV_ALLOC_L2CACHE	3
28121ecdf6Lin Ling#define	VDEV_ALLOC_ROOTPOOL	4
2821195e68Mark J Musante#define	VDEV_ALLOC_SPLIT	5
283cd0837cGeorge Wilson#define	VDEV_ALLOC_ATTACH	6
286fa9e406ahrens * Allocate or free a vdev
287fa9e406ahrens */
28888ecc94George Wilsonextern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
28988ecc94George Wilson    vdev_ops_t *ops);
29099653d4eschrockextern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
29199653d4eschrock    vdev_t *parent, uint_t id, int alloctype);
292fa9e406ahrensextern void vdev_free(vdev_t *vd);
295fa9e406ahrens * Add or remove children and parents
296fa9e406ahrens */
297fa9e406ahrensextern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
298fa9e406ahrensextern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
299fa9e406ahrensextern void vdev_compact_children(vdev_t *pvd);
300fa9e406ahrensextern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
301fa9e406ahrensextern void vdev_remove_parent(vdev_t *cvd);
304fa9e406ahrens * vdev sync load and sync
305fa9e406ahrens */
30688ecc94George Wilsonextern void vdev_load_log_state(vdev_t *nvd, vdev_t *ovd);
3074b964adGeorge Wilsonextern boolean_t vdev_log_state_valid(vdev_t *vd);
308560e6e9eschrockextern void vdev_load(vdev_t *vd);
3090713e23George Wilsonextern int vdev_dtl_load(vdev_t *vd);
310fa9e406ahrensextern void vdev_sync(vdev_t *vd, uint64_t txg);
311fa9e406ahrensextern void vdev_sync_done(vdev_t *vd, uint64_t txg);
312ecc2d60bonwickextern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
3130713e23George Wilsonextern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
316fa9e406ahrens * Available vdev types.
317fa9e406ahrens */
318fa9e406ahrensextern vdev_ops_t vdev_root_ops;
319fa9e406ahrensextern vdev_ops_t vdev_mirror_ops;
320fa9e406ahrensextern vdev_ops_t vdev_replacing_ops;
321fa9e406ahrensextern vdev_ops_t vdev_raidz_ops;
322fa9e406ahrensextern vdev_ops_t vdev_disk_ops;
323fa9e406ahrensextern vdev_ops_t vdev_file_ops;
324fa9e406ahrensextern vdev_ops_t vdev_missing_ops;
32588ecc94George Wilsonextern vdev_ops_t vdev_hole_ops;
32699653d4eschrockextern vdev_ops_t vdev_spare_ops;
3292a79c5flling * Common size functions
330fa9e406ahrens */
331fa9e406ahrensextern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
332573ca77George Wilsonextern uint64_t vdev_get_min_asize(vdev_t *vd);
333573ca77George Wilsonextern void vdev_set_min_asize(vdev_t *vd);
336f717074Will Andrews * Global variables
337e81c57bahrens */
338f717074Will Andrews/* zdb uses this tunable, so it must be declared here to make lint happy. */
339e81c57bahrensextern int zfs_vdev_cache_size;
34131d7e8fGeorge Wilson/*
34231d7e8fGeorge Wilson * The vdev_buf_t is used to translate between zio_t and buf_t, and back again.
34331d7e8fGeorge Wilson */
34431d7e8fGeorge Wilsontypedef struct vdev_buf {
34531d7e8fGeorge Wilson	buf_t	vb_buf;		/* buffer that describes the io */
34631d7e8fGeorge Wilson	zio_t	*vb_io;		/* pointer back to the original zio_t */
34731d7e8fGeorge Wilson} vdev_buf_t;
34831d7e8fGeorge Wilson
349fa9e406ahrens#ifdef	__cplusplus
353fa9e406ahrens#endif	/* _SYS_VDEV_IMPL_H */