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