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