xref: /freebsd/sys/contrib/openzfs/include/sys/vdev_impl.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
15  * Copyright (c) 2017, Intel Corporation.
16  * Copyright (c) 2023, Klara Inc.
17  */
18 
19 #ifndef _SYS_VDEV_IMPL_H
20 #define	_SYS_VDEV_IMPL_H
21 
22 #include <sys/avl.h>
23 #include <sys/bpobj.h>
24 #include <sys/dmu.h>
25 #include <sys/metaslab.h>
26 #include <sys/nvpair.h>
27 #include <sys/space_map.h>
28 #include <sys/vdev.h>
29 #include <sys/uberblock_impl.h>
30 #include <sys/vdev_indirect_mapping.h>
31 #include <sys/vdev_indirect_births.h>
32 #include <sys/vdev_rebuild.h>
33 #include <sys/vdev_removal.h>
34 #include <sys/zfs_ratelimit.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * Virtual device descriptors.
42  *
43  * All storage pool operations go through the virtual device framework,
44  * which provides data replication and I/O scheduling.
45  */
46 
47 /*
48  * Forward declarations that lots of things need.
49  */
50 typedef struct vdev_queue vdev_queue_t;
51 struct abd;
52 
53 /*
54  * Virtual device operations
55  */
56 typedef int	vdev_init_func_t(spa_t *spa, nvlist_t *nv, void **tsd);
57 typedef void	vdev_kobj_post_evt_func_t(vdev_t *vd);
58 typedef void	vdev_fini_func_t(vdev_t *vd);
59 typedef int	vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *max_size,
60     uint64_t *ashift, uint64_t *pshift, cred_t *cr);
61 typedef void	vdev_close_func_t(vdev_t *vd);
62 typedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize, uint64_t txg);
63 typedef uint64_t vdev_min_asize_func_t(vdev_t *vd);
64 typedef uint64_t vdev_min_alloc_func_t(vdev_t *vd);
65 typedef void	vdev_io_start_func_t(zio_t *zio);
66 typedef void	vdev_io_done_func_t(zio_t *zio);
67 typedef void	vdev_state_change_func_t(vdev_t *vd, int, int);
68 typedef boolean_t vdev_need_resilver_func_t(vdev_t *vd, const dva_t *dva,
69     size_t psize, uint64_t phys_birth);
70 typedef void	vdev_hold_func_t(vdev_t *vd);
71 typedef void	vdev_rele_func_t(vdev_t *vd);
72 
73 typedef void	vdev_remap_cb_t(uint64_t inner_offset, vdev_t *vd,
74     uint64_t offset, uint64_t size, void *arg);
75 typedef void	vdev_remap_func_t(vdev_t *vd, uint64_t offset, uint64_t size,
76     vdev_remap_cb_t callback, void *arg);
77 /*
78  * Given a target vdev, translates the logical range "in" to the physical
79  * range "res"
80  */
81 typedef void vdev_xlation_func_t(vdev_t *cvd, const zfs_range_seg64_t *logical,
82     zfs_range_seg64_t *physical, zfs_range_seg64_t *remain);
83 typedef uint64_t vdev_rebuild_asize_func_t(vdev_t *vd, uint64_t start,
84     uint64_t size, uint64_t max_segment);
85 typedef void vdev_metaslab_init_func_t(vdev_t *vd, uint64_t *startp,
86     uint64_t *sizep);
87 typedef void vdev_config_generate_func_t(vdev_t *vd, nvlist_t *nv);
88 typedef uint64_t vdev_nparity_func_t(vdev_t *vd);
89 typedef uint64_t vdev_ndisks_func_t(vdev_t *vd);
90 
91 typedef const struct vdev_ops {
92 	vdev_init_func_t		*vdev_op_init;
93 	vdev_fini_func_t		*vdev_op_fini;
94 	vdev_open_func_t		*vdev_op_open;
95 	vdev_close_func_t		*vdev_op_close;
96 	vdev_asize_func_t		*vdev_op_psize_to_asize;
97 	vdev_asize_func_t		*vdev_op_asize_to_psize;
98 	vdev_min_asize_func_t		*vdev_op_min_asize;
99 	vdev_min_alloc_func_t		*vdev_op_min_alloc;
100 	vdev_io_start_func_t		*vdev_op_io_start;
101 	vdev_io_done_func_t		*vdev_op_io_done;
102 	vdev_state_change_func_t	*vdev_op_state_change;
103 	vdev_need_resilver_func_t	*vdev_op_need_resilver;
104 	vdev_hold_func_t		*vdev_op_hold;
105 	vdev_rele_func_t		*vdev_op_rele;
106 	vdev_remap_func_t		*vdev_op_remap;
107 	vdev_xlation_func_t		*vdev_op_xlate;
108 	vdev_rebuild_asize_func_t	*vdev_op_rebuild_asize;
109 	vdev_metaslab_init_func_t	*vdev_op_metaslab_init;
110 	vdev_config_generate_func_t	*vdev_op_config_generate;
111 	vdev_nparity_func_t		*vdev_op_nparity;
112 	vdev_ndisks_func_t		*vdev_op_ndisks;
113 	vdev_kobj_post_evt_func_t	*vdev_op_kobj_evt_post;
114 	char				vdev_op_type[16];
115 	boolean_t			vdev_op_leaf;
116 } vdev_ops_t;
117 
118 /*
119  * Virtual device properties
120  */
121 typedef union vdev_queue_class {
122 	struct {
123 		ulong_t 	vqc_list_numnodes;
124 		list_t		vqc_list;
125 	};
126 	avl_tree_t	vqc_tree;
127 } vdev_queue_class_t;
128 
129 struct vdev_queue {
130 	vdev_t		*vq_vdev;
131 	vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
132 	avl_tree_t	vq_read_offset_tree;
133 	avl_tree_t	vq_write_offset_tree;
134 	uint64_t	vq_last_offset;
135 	zio_priority_t	vq_last_prio;	/* Last sent I/O priority. */
136 	uint32_t	vq_cqueued;	/* Classes with queued I/Os. */
137 	uint32_t	vq_cactive[ZIO_PRIORITY_NUM_QUEUEABLE];
138 	uint32_t	vq_active;	/* Number of active I/Os. */
139 	uint32_t	vq_ia_active;	/* Active interactive I/Os. */
140 	uint32_t	vq_nia_credit;	/* Non-interactive I/Os credit. */
141 	list_t		vq_active_list;	/* List of active I/Os. */
142 	hrtime_t	vq_io_complete_ts; /* time last i/o completed */
143 	hrtime_t	vq_io_delta_ts;
144 	zio_t		vq_io_search; /* used as local for stack reduction */
145 	kmutex_t	vq_lock;
146 };
147 
148 /*
149  * On-disk indirect vdev state.
150  *
151  * An indirect vdev is described exclusively in the MOS config of a pool.
152  * The config for an indirect vdev includes several fields, which are
153  * accessed in memory by a vdev_indirect_config_t.
154  */
155 typedef struct vdev_indirect_config {
156 	/*
157 	 * Object (in MOS) which contains the indirect mapping. This object
158 	 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
159 	 * vimep_src. The bonus buffer for this object is a
160 	 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
161 	 * removal is initiated.
162 	 *
163 	 * Note that this object can be empty if none of the data on the vdev
164 	 * has been copied yet.
165 	 */
166 	uint64_t	vic_mapping_object;
167 
168 	/*
169 	 * Object (in MOS) which contains the birth times for the mapping
170 	 * entries. This object contains an array of
171 	 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
172 	 * buffer for this object is a vdev_indirect_birth_phys_t. This object
173 	 * is allocated when a vdev removal is initiated.
174 	 *
175 	 * Note that this object can be empty if none of the vdev has yet been
176 	 * copied.
177 	 */
178 	uint64_t	vic_births_object;
179 
180 	/*
181 	 * This is the vdev ID which was removed previous to this vdev, or
182 	 * UINT64_MAX if there are no previously removed vdevs.
183 	 */
184 	uint64_t	vic_prev_indirect_vdev;
185 } vdev_indirect_config_t;
186 
187 /*
188  * Virtual device descriptor
189  */
190 struct vdev {
191 	/*
192 	 * Common to all vdev types.
193 	 */
194 	uint64_t	vdev_id;	/* child number in vdev parent	*/
195 	uint64_t	vdev_guid;	/* unique ID for this vdev	*/
196 	uint64_t	vdev_guid_sum;	/* self guid + all child guids	*/
197 	uint64_t	vdev_orig_guid;	/* orig. guid prior to remove	*/
198 	uint64_t	vdev_asize;	/* allocatable device capacity	*/
199 	uint64_t	vdev_min_asize;	/* min acceptable asize		*/
200 	uint64_t	vdev_max_asize;	/* max acceptable asize		*/
201 	uint64_t	vdev_ashift;	/* block alignment shift	*/
202 
203 	/*
204 	 * Logical block alignment shift
205 	 *
206 	 * The smallest sized/aligned I/O supported by the device.
207 	 */
208 	uint64_t	vdev_logical_ashift;
209 	/*
210 	 * Physical block alignment shift
211 	 *
212 	 * The device supports logical I/Os with vdev_logical_ashift
213 	 * size/alignment, but optimum performance will be achieved by
214 	 * aligning/sizing requests to vdev_physical_ashift.  Smaller
215 	 * requests may be inflated or incur device level read-modify-write
216 	 * operations.
217 	 *
218 	 * May be 0 to indicate no preference (i.e. use vdev_logical_ashift).
219 	 */
220 	uint64_t	vdev_physical_ashift;
221 	uint64_t	vdev_state;	/* see VDEV_STATE_* #defines	*/
222 	uint64_t	vdev_prevstate;	/* used when reopening a vdev	*/
223 	vdev_ops_t	*vdev_ops;	/* vdev operations		*/
224 	spa_t		*vdev_spa;	/* spa for this vdev		*/
225 	void		*vdev_tsd;	/* type-specific data		*/
226 	vdev_t		*vdev_top;	/* top-level vdev		*/
227 	vdev_t		*vdev_parent;	/* parent vdev			*/
228 	vdev_t		**vdev_child;	/* array of children		*/
229 	uint64_t	vdev_children;	/* number of children		*/
230 	vdev_stat_t	vdev_stat;	/* virtual device statistics	*/
231 	vdev_stat_ex_t	vdev_stat_ex;	/* extended statistics		*/
232 	boolean_t	vdev_expanding;	/* expand the vdev?		*/
233 	boolean_t	vdev_reopening;	/* reopen in progress?		*/
234 	boolean_t	vdev_nonrot;	/* true if solid state		*/
235 	int		vdev_load_error; /* error on last load		*/
236 	int		vdev_open_error; /* error on last open		*/
237 	int		vdev_validate_error; /* error on last validate	*/
238 	kthread_t	*vdev_open_thread; /* thread opening children	*/
239 	kthread_t	*vdev_validate_thread; /* thread validating children */
240 	uint64_t	vdev_crtxg;	/* txg when top-level was added */
241 	uint64_t	vdev_root_zap;
242 
243 	/*
244 	 * Top-level vdev state.
245 	 */
246 	uint64_t	vdev_ms_array;	/* metaslab array object	*/
247 	uint64_t	vdev_ms_shift;	/* metaslab size shift		*/
248 	uint64_t	vdev_ms_count;	/* number of metaslabs		*/
249 	metaslab_group_t *vdev_mg;	/* metaslab group		*/
250 	metaslab_group_t *vdev_log_mg;	/* embedded slog metaslab group	*/
251 	metaslab_t	**vdev_ms;	/* metaslab array		*/
252 	txg_list_t	vdev_ms_list;	/* per-txg dirty metaslab lists	*/
253 	txg_list_t	vdev_dtl_list;	/* per-txg dirty DTL lists	*/
254 	txg_node_t	vdev_txg_node;	/* per-txg dirty vdev linkage	*/
255 	boolean_t	vdev_remove_wanted; /* async remove wanted?	*/
256 	boolean_t	vdev_fault_wanted; /* async faulted wanted?	*/
257 	list_node_t	vdev_config_dirty_node; /* config dirty list	*/
258 	list_node_t	vdev_state_dirty_node; /* state dirty list	*/
259 	uint64_t	vdev_deflate_ratio; /* deflation ratio (x512)	*/
260 	uint64_t	vdev_islog;	/* is an intent log device	*/
261 	uint64_t	vdev_noalloc;	/* device is passivated?	*/
262 	uint64_t	vdev_removing;	/* device is being removed?	*/
263 	uint64_t	vdev_failfast;	/* device failfast setting	*/
264 	boolean_t	vdev_autosit;	/* automatic sitout management	*/
265 	boolean_t	vdev_rz_expanding; /* raidz is being expanded?	*/
266 	boolean_t	vdev_ishole;	/* is a hole in the namespace	*/
267 	uint64_t	vdev_top_zap;
268 	vdev_alloc_bias_t vdev_alloc_bias; /* metaslab allocation bias	*/
269 	uint64_t	vdev_last_latency_check;
270 
271 	/* pool checkpoint related */
272 	space_map_t	*vdev_checkpoint_sm;	/* contains reserved blocks */
273 
274 	/* Initialize related */
275 	boolean_t	vdev_initialize_exit_wanted;
276 	vdev_initializing_state_t	vdev_initialize_state;
277 	list_node_t	vdev_initialize_node;
278 	kthread_t	*vdev_initialize_thread;
279 	/* Protects vdev_initialize_thread and vdev_initialize_state. */
280 	kmutex_t	vdev_initialize_lock;
281 	kcondvar_t	vdev_initialize_cv;
282 	uint64_t	vdev_initialize_offset[TXG_SIZE];
283 	uint64_t	vdev_initialize_last_offset;
284 	/* value written to free space by the initializing thread */
285 	uint64_t	vdev_initialize_value;
286 	/* valid while initializing */
287 	zfs_range_tree_t	*vdev_initialize_tree;
288 	uint64_t	vdev_initialize_bytes_est;
289 	uint64_t	vdev_initialize_bytes_done;
290 	uint64_t	vdev_initialize_action_time;	/* start and end time */
291 
292 	/* TRIM related */
293 	boolean_t	vdev_trim_exit_wanted;
294 	boolean_t	vdev_autotrim_exit_wanted;
295 	vdev_trim_state_t	vdev_trim_state;
296 	list_node_t	vdev_trim_node;
297 	kmutex_t	vdev_autotrim_lock;
298 	kcondvar_t	vdev_autotrim_cv;
299 	kcondvar_t	vdev_autotrim_kick_cv;
300 	kthread_t	*vdev_autotrim_thread;
301 	/* Protects vdev_trim_thread and vdev_trim_state. */
302 	kmutex_t	vdev_trim_lock;
303 	kcondvar_t	vdev_trim_cv;
304 	kthread_t	*vdev_trim_thread;
305 	uint64_t	vdev_trim_offset[TXG_SIZE];
306 	uint64_t	vdev_trim_last_offset;
307 	uint64_t	vdev_trim_bytes_est;
308 	uint64_t	vdev_trim_bytes_done;
309 	uint64_t	vdev_trim_rate;		/* requested rate (bytes/sec) */
310 	uint64_t	vdev_trim_partial;	/* requested partial TRIM */
311 	uint64_t	vdev_trim_secure;	/* requested secure TRIM */
312 	uint64_t	vdev_trim_action_time;	/* start and end time */
313 
314 	/* Rebuild related */
315 	boolean_t	vdev_rebuilding;
316 	boolean_t	vdev_rebuild_exit_wanted;
317 	boolean_t	vdev_rebuild_cancel_wanted;
318 	boolean_t	vdev_rebuild_reset_wanted;
319 	kmutex_t	vdev_rebuild_lock;
320 	kcondvar_t	vdev_rebuild_cv;
321 	kthread_t	*vdev_rebuild_thread;
322 	vdev_rebuild_t	vdev_rebuild_config;
323 
324 	/* For limiting outstanding I/Os (initialize, TRIM) */
325 	kmutex_t	vdev_initialize_io_lock;
326 	kcondvar_t	vdev_initialize_io_cv;
327 	uint64_t	vdev_initialize_inflight;
328 	kmutex_t	vdev_trim_io_lock;
329 	kcondvar_t	vdev_trim_io_cv;
330 	uint64_t	vdev_trim_inflight[3];
331 
332 	/*
333 	 * Values stored in the config for an indirect or removing vdev.
334 	 */
335 	vdev_indirect_config_t	vdev_indirect_config;
336 
337 	/*
338 	 * The vdev_indirect_rwlock protects the vdev_indirect_mapping
339 	 * pointer from changing on indirect vdevs (when it is condensed).
340 	 * Note that removing (not yet indirect) vdevs have different
341 	 * access patterns (the mapping is not accessed from open context,
342 	 * e.g. from zio_read) and locking strategy (e.g. svr_lock).
343 	 */
344 	krwlock_t vdev_indirect_rwlock;
345 	vdev_indirect_mapping_t *vdev_indirect_mapping;
346 	vdev_indirect_births_t *vdev_indirect_births;
347 
348 	/*
349 	 * In memory data structures used to manage the obsolete sm, for
350 	 * indirect or removing vdevs.
351 	 *
352 	 * The vdev_obsolete_segments is the in-core record of the segments
353 	 * that are no longer referenced anywhere in the pool (due to
354 	 * being freed or remapped and not referenced by any snapshots).
355 	 * During a sync, segments are added to vdev_obsolete_segments
356 	 * via vdev_indirect_mark_obsolete(); at the end of each sync
357 	 * pass, this is appended to vdev_obsolete_sm via
358 	 * vdev_indirect_sync_obsolete().  The vdev_obsolete_lock
359 	 * protects against concurrent modifications of vdev_obsolete_segments
360 	 * from multiple zio threads.
361 	 */
362 	kmutex_t	vdev_obsolete_lock;
363 	zfs_range_tree_t	*vdev_obsolete_segments;
364 	space_map_t	*vdev_obsolete_sm;
365 
366 	/*
367 	 * Protects the vdev_scan_io_queue field itself as well as the
368 	 * structure's contents (when present).
369 	 */
370 	kmutex_t			vdev_scan_io_queue_lock;
371 	struct dsl_scan_io_queue	*vdev_scan_io_queue;
372 
373 	/*
374 	 * Leaf vdev state.
375 	 */
376 	zfs_range_tree_t	*vdev_dtl[DTL_TYPES]; /* dirty time logs */
377 	space_map_t	*vdev_dtl_sm;	/* dirty time log space map	*/
378 	txg_node_t	vdev_dtl_node;	/* per-txg dirty DTL linkage	*/
379 	uint64_t	vdev_dtl_object; /* DTL object			*/
380 	uint64_t	vdev_psize;	/* physical device capacity	*/
381 	uint64_t	vdev_wholedisk;	/* true if this is a whole disk */
382 	uint64_t	vdev_offline;	/* persistent offline state	*/
383 	uint64_t	vdev_faulted;	/* persistent faulted state	*/
384 	uint64_t	vdev_degraded;	/* persistent degraded state	*/
385 	uint64_t	vdev_removed;	/* persistent removed state	*/
386 	uint64_t	vdev_resilver_txg; /* persistent resilvering state */
387 	uint64_t	vdev_rebuild_txg; /* persistent rebuilding state */
388 	char		*vdev_path;	/* vdev path (if any)		*/
389 	char		*vdev_devid;	/* vdev devid (if any)		*/
390 	char		*vdev_physpath;	/* vdev device path (if any)	*/
391 	char		*vdev_enc_sysfs_path;	/* enclosure sysfs path */
392 	char		*vdev_fru;	/* physical FRU location	*/
393 	uint64_t	vdev_not_present; /* not present during import	*/
394 	uint64_t	vdev_unspare;	/* unspare when resilvering done */
395 	boolean_t	vdev_nowritecache; /* true if flushwritecache failed */
396 	boolean_t	vdev_has_trim;	/* TRIM is supported		*/
397 	boolean_t	vdev_has_securetrim; /* secure TRIM is supported */
398 	boolean_t	vdev_checkremove; /* temporary online test	*/
399 	boolean_t	vdev_forcefault; /* force online fault		*/
400 	boolean_t	vdev_splitting;	/* split or repair in progress  */
401 	boolean_t	vdev_delayed_close; /* delayed device close?	*/
402 	boolean_t	vdev_tmpoffline; /* device taken offline temporarily? */
403 	boolean_t	vdev_detached;	/* device detached?		*/
404 	boolean_t	vdev_cant_read;	/* vdev is failing all reads	*/
405 	boolean_t	vdev_cant_write; /* vdev is failing all writes	*/
406 	boolean_t	vdev_isspare;	/* was a hot spare		*/
407 	boolean_t	vdev_isl2cache;	/* was a l2cache device		*/
408 	boolean_t	vdev_copy_uberblocks;  /* post expand copy uberblocks */
409 	boolean_t	vdev_resilver_deferred;  /* resilver deferred */
410 	boolean_t	vdev_kobj_flag; /* kobj event record */
411 	boolean_t	vdev_attaching; /* vdev attach ashift handling */
412 	boolean_t	vdev_is_blkdev; /* vdev is backed by block device */
413 	vdev_queue_t	vdev_queue;	/* I/O deadline schedule queue	*/
414 	spa_aux_vdev_t	*vdev_aux;	/* for l2cache and spares vdevs	*/
415 	zio_t		*vdev_probe_zio; /* root of current probe	*/
416 	vdev_aux_t	vdev_label_aux;	/* on-disk aux state		*/
417 	uint64_t	vdev_leaf_zap;
418 	hrtime_t	vdev_mmp_pending; /* 0 if write finished	*/
419 	uint64_t	vdev_mmp_kstat_id;	/* to find kstat entry */
420 	uint64_t	vdev_expansion_time;	/* vdev's last expansion time */
421 	/* used to calculate average read latency */
422 	uint64_t	*vdev_prev_histo;
423 	int64_t		vdev_outlier_count;	/* read outlier amongst peers */
424 	hrtime_t	vdev_read_sit_out_expire; /* end of sit out period    */
425 	list_node_t	vdev_leaf_node;		/* leaf vdev list */
426 
427 	/*
428 	 * For DTrace to work in userland (libzpool) context, these fields must
429 	 * remain at the end of the structure.  DTrace will use the kernel's
430 	 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
431 	 * larger in userland, the offsets for the rest of the fields would be
432 	 * incorrect.
433 	 */
434 	kmutex_t	vdev_dtl_lock;	/* vdev_dtl_{map,resilver}	*/
435 	kmutex_t	vdev_stat_lock;	/* vdev_stat			*/
436 	kmutex_t	vdev_probe_lock; /* protects vdev_probe_zio	*/
437 
438 	/*
439 	 * We rate limit ZIO delay, deadman, and checksum events, since they
440 	 * can flood ZED with tons of events when a drive is acting up.
441 	 *
442 	 * We also rate limit Direct I/O write verify errors, since a user might
443 	 * be continually manipulating a buffer that can flood ZED with tons of
444 	 * events.
445 	 */
446 	zfs_ratelimit_t vdev_delay_rl;
447 	zfs_ratelimit_t vdev_deadman_rl;
448 	zfs_ratelimit_t vdev_dio_verify_rl;
449 	zfs_ratelimit_t vdev_checksum_rl;
450 
451 	/*
452 	 * Vdev properties for tuning ZED or zfsd
453 	 */
454 	uint64_t	vdev_checksum_n;
455 	uint64_t	vdev_checksum_t;
456 	uint64_t	vdev_io_n;
457 	uint64_t	vdev_io_t;
458 	boolean_t	vdev_slow_io_events;
459 	uint64_t	vdev_slow_io_n;
460 	uint64_t	vdev_slow_io_t;
461 	uint64_t	vdev_scheduler; /* control how I/Os are submitted */
462 };
463 
464 #define	VDEV_PAD_SIZE		(8 << 10)
465 /* 2 padding areas (vl_pad1 and vl_be) to skip */
466 #define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
467 #define	VDEV_PHYS_SIZE		(112 << 10)
468 #define	VDEV_UBERBLOCK_RING	(128 << 10)
469 
470 /*
471  * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
472  * ring when MMP is enabled.
473  */
474 #define	MMP_BLOCKS_PER_LABEL	1
475 
476 /* The largest uberblock we support is 8k. */
477 #define	MAX_UBERBLOCK_SHIFT (13)
478 #define	VDEV_UBERBLOCK_SHIFT(vd)	\
479 	MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
480 	    MAX_UBERBLOCK_SHIFT)
481 #define	VDEV_UBERBLOCK_COUNT(vd)	\
482 	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
483 #define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
484 	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
485 #define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
486 
487 typedef struct vdev_phys {
488 	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
489 	zio_eck_t	vp_zbt;
490 } vdev_phys_t;
491 
492 typedef enum vbe_vers {
493 	/*
494 	 * The bootenv file is stored as ascii text in the envblock.
495 	 * It is used by the GRUB bootloader used on Linux to store the
496 	 * contents of the grubenv file. The file is stored as raw ASCII,
497 	 * and is protected by an embedded checksum. By default, GRUB will
498 	 * check if the boot filesystem supports storing the environment data
499 	 * in a special location, and if so, will invoke filesystem specific
500 	 * logic to retrieve it. This can be overridden by a variable, should
501 	 * the user so desire.
502 	 */
503 	VB_RAW = 0,
504 
505 	/*
506 	 * The bootenv file is converted to an nvlist and then packed into the
507 	 * envblock.
508 	 */
509 	VB_NVLIST = 1
510 } vbe_vers_t;
511 
512 typedef struct vdev_boot_envblock {
513 	uint64_t	vbe_version;
514 	char		vbe_bootenv[VDEV_PAD_SIZE - sizeof (uint64_t) -
515 			sizeof (zio_eck_t)];
516 	zio_eck_t	vbe_zbt;
517 } vdev_boot_envblock_t;
518 _Static_assert(sizeof (vdev_boot_envblock_t) == VDEV_PAD_SIZE,
519 	"vdev_boot_envblock_t wrong size");
520 
521 typedef struct vdev_label {
522 	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K */
523 	vdev_boot_envblock_t	vl_be;				/*  8K */
524 	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
525 	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
526 } vdev_label_t;						/* 256K total */
527 
528 /*
529  * vdev_dirty() flags
530  */
531 #define	VDD_METASLAB	0x01
532 #define	VDD_DTL		0x02
533 
534 /* Offset of embedded boot loader region on each label */
535 #define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
536 /*
537  * Size of embedded boot loader region on each label.
538  * The total size of the first two labels plus the boot area is 4MB.
539  * On RAIDZ, this space is overwritten during RAIDZ expansion.
540  */
541 #define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M */
542 
543 /*
544  * Size of label regions at the start and end of each leaf device.
545  */
546 #define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
547 #define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
548 #define	VDEV_LABELS		4
549 #define	VDEV_BEST_LABEL		VDEV_LABELS
550 #define	VDEV_OFFSET_IS_LABEL(vd, off)                           \
551 	(((off) < VDEV_LABEL_START_SIZE) ||                     \
552 	((off) >= ((vd)->vdev_psize - VDEV_LABEL_END_SIZE)))
553 
554 #define	VDEV_ALLOC_LOAD		0
555 #define	VDEV_ALLOC_ADD		1
556 #define	VDEV_ALLOC_SPARE	2
557 #define	VDEV_ALLOC_L2CACHE	3
558 #define	VDEV_ALLOC_ROOTPOOL	4
559 #define	VDEV_ALLOC_SPLIT	5
560 #define	VDEV_ALLOC_ATTACH	6
561 
562 /*
563  * Allocate or free a vdev
564  */
565 extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
566     vdev_ops_t *ops);
567 extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
568     vdev_t *parent, uint_t id, int alloctype);
569 extern void vdev_free(vdev_t *vd);
570 
571 /*
572  * Add or remove children and parents
573  */
574 extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
575 extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
576 extern void vdev_compact_children(vdev_t *pvd);
577 extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
578 extern void vdev_remove_parent(vdev_t *cvd);
579 
580 /*
581  * vdev sync load and sync
582  */
583 extern boolean_t vdev_log_state_valid(vdev_t *vd);
584 extern int vdev_load(vdev_t *vd);
585 extern int vdev_dtl_load(vdev_t *vd);
586 extern void vdev_sync(vdev_t *vd, uint64_t txg);
587 extern void vdev_sync_dispatch(vdev_t *vd, uint64_t txg);
588 extern void vdev_sync_done(vdev_t *vd, uint64_t txg);
589 extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
590 extern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
591 
592 /*
593  * Available vdev types.
594  */
595 extern vdev_ops_t vdev_root_ops;
596 extern vdev_ops_t vdev_mirror_ops;
597 extern vdev_ops_t vdev_replacing_ops;
598 extern vdev_ops_t vdev_raidz_ops;
599 extern vdev_ops_t vdev_draid_ops;
600 extern vdev_ops_t vdev_draid_spare_ops;
601 extern vdev_ops_t vdev_disk_ops;
602 extern vdev_ops_t vdev_file_ops;
603 extern vdev_ops_t vdev_missing_ops;
604 extern vdev_ops_t vdev_hole_ops;
605 extern vdev_ops_t vdev_spare_ops;
606 extern vdev_ops_t vdev_indirect_ops;
607 
608 /*
609  * Common size functions
610  */
611 extern void vdev_default_xlate(vdev_t *vd, const zfs_range_seg64_t *logical_rs,
612     zfs_range_seg64_t *physical_rs, zfs_range_seg64_t *remain_rs);
613 extern uint64_t vdev_default_psize(vdev_t *vd, uint64_t asize, uint64_t txg);
614 extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize, uint64_t txg);
615 extern uint64_t vdev_default_min_asize(vdev_t *vd);
616 extern uint64_t vdev_get_min_asize(vdev_t *vd);
617 extern void vdev_set_min_asize(vdev_t *vd);
618 extern uint64_t vdev_get_nparity(vdev_t *vd);
619 extern uint64_t vdev_get_ndisks(vdev_t *vd);
620 
621 /*
622  * Global variables
623  */
624 extern int zfs_vdev_standard_sm_blksz;
625 
626 /*
627  * Functions from vdev_indirect.c
628  */
629 extern void vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx);
630 extern boolean_t vdev_indirect_should_condense(vdev_t *vd);
631 extern void spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx);
632 extern int vdev_obsolete_sm_object(vdev_t *vd, uint64_t *sm_obj);
633 extern int vdev_obsolete_counts_are_precise(vdev_t *vd, boolean_t *are_precise);
634 
635 /*
636  * Other miscellaneous functions
637  */
638 int vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj);
639 void vdev_metaslab_group_create(vdev_t *vd);
640 uint64_t vdev_best_ashift(uint64_t logical, uint64_t a, uint64_t b);
641 #if defined(__linux__) && defined(_KERNEL)
642 int param_get_raidz_impl(char *buf, zfs_kernel_param_t *kp);
643 #endif
644 int param_set_raidz_impl(ZFS_MODULE_PARAM_ARGS);
645 char *vdev_rt_name(vdev_t *vd, const char *name);
646 
647 /*
648  * Vdev ashift optimization tunables
649  */
650 extern uint_t zfs_vdev_min_auto_ashift;
651 extern uint_t zfs_vdev_max_auto_ashift;
652 int param_set_min_auto_ashift(ZFS_MODULE_PARAM_ARGS);
653 int param_set_max_auto_ashift(ZFS_MODULE_PARAM_ARGS);
654 
655 /*
656  * VDEV checksum verification for Direct I/O writes
657  */
658 extern uint_t zfs_vdev_direct_write_verify;
659 
660 #ifdef	__cplusplus
661 }
662 #endif
663 
664 #endif	/* _SYS_VDEV_IMPL_H */
665