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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
26 #ifndef _SYS_VDEV_IMPL_H
27 #define _SYS_VDEV_IMPL_H
30 #include <sys/bpobj.h>
32 #include <sys/metaslab.h>
33 #include <sys/nvpair.h>
34 #include <sys/space_map.h>
37 #include <sys/uberblock_impl.h>
38 #include <sys/vdev_indirect_mapping.h>
39 #include <sys/vdev_indirect_births.h>
40 #include <sys/vdev_removal.h>
47 * Virtual device descriptors.
49 * All storage pool operations go through the virtual device framework,
50 * which provides data replication and I/O scheduling.
54 * Forward declarations that lots of things need.
56 typedef struct vdev_queue vdev_queue_t;
57 typedef struct vdev_cache vdev_cache_t;
58 typedef struct vdev_cache_entry vdev_cache_entry_t;
61 extern int zfs_vdev_queue_depth_pct;
62 extern int zfs_vdev_def_queue_depth;
63 extern uint32_t zfs_vdev_async_write_max_active;
66 * Virtual device operations
68 typedef int vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *max_size,
69 uint64_t *logical_ashift, uint64_t *physical_ashift);
70 typedef void vdev_close_func_t(vdev_t *vd);
71 typedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize);
72 typedef void vdev_io_start_func_t(zio_t *zio);
73 typedef void vdev_io_done_func_t(zio_t *zio);
74 typedef void vdev_state_change_func_t(vdev_t *vd, int, int);
75 typedef boolean_t vdev_need_resilver_func_t(vdev_t *vd, uint64_t, size_t);
76 typedef void vdev_hold_func_t(vdev_t *vd);
77 typedef void vdev_rele_func_t(vdev_t *vd);
79 typedef void vdev_remap_cb_t(uint64_t inner_offset, vdev_t *vd,
80 uint64_t offset, uint64_t size, void *arg);
81 typedef void vdev_remap_func_t(vdev_t *vd, uint64_t offset, uint64_t size,
82 vdev_remap_cb_t callback, void *arg);
84 * Given a target vdev, translates the logical range "in" to the physical
87 typedef void vdev_xlation_func_t(vdev_t *cvd, const range_seg_t *in,
90 typedef struct vdev_ops {
91 vdev_open_func_t *vdev_op_open;
92 vdev_close_func_t *vdev_op_close;
93 vdev_asize_func_t *vdev_op_asize;
94 vdev_io_start_func_t *vdev_op_io_start;
95 vdev_io_done_func_t *vdev_op_io_done;
96 vdev_state_change_func_t *vdev_op_state_change;
97 vdev_need_resilver_func_t *vdev_op_need_resilver;
98 vdev_hold_func_t *vdev_op_hold;
99 vdev_rele_func_t *vdev_op_rele;
100 vdev_remap_func_t *vdev_op_remap;
102 * For translating ranges from non-leaf vdevs (e.g. raidz) to leaves.
103 * Used when initializing vdevs. Isn't used by leaf ops.
105 vdev_xlation_func_t *vdev_op_xlate;
106 char vdev_op_type[16];
107 boolean_t vdev_op_leaf;
111 * Virtual device properties
113 struct vdev_cache_entry {
116 uint64_t ve_lastused;
117 avl_node_t ve_offset_node;
118 avl_node_t ve_lastused_node;
120 uint16_t ve_missed_update;
125 avl_tree_t vc_offset_tree;
126 avl_tree_t vc_lastused_tree;
130 typedef struct vdev_queue_class {
134 * Sorted by offset or timestamp, depending on if the queue is
135 * LBA-ordered vs FIFO.
137 avl_tree_t vqc_queued_tree;
138 } vdev_queue_class_t;
142 vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
143 avl_tree_t vq_active_tree;
144 avl_tree_t vq_read_offset_tree;
145 avl_tree_t vq_write_offset_tree;
146 uint64_t vq_last_offset;
147 hrtime_t vq_io_complete_ts; /* time last i/o completed */
149 uint64_t vq_lastoffset;
153 * On-disk indirect vdev state.
155 * An indirect vdev is described exclusively in the MOS config of a pool.
156 * The config for an indirect vdev includes several fields, which are
157 * accessed in memory by a vdev_indirect_config_t.
159 typedef struct vdev_indirect_config {
161 * Object (in MOS) which contains the indirect mapping. This object
162 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
163 * vimep_src. The bonus buffer for this object is a
164 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
165 * removal is initiated.
167 * Note that this object can be empty if none of the data on the vdev
168 * has been copied yet.
170 uint64_t vic_mapping_object;
173 * Object (in MOS) which contains the birth times for the mapping
174 * entries. This object contains an array of
175 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
176 * buffer for this object is a vdev_indirect_birth_phys_t. This object
177 * is allocated when a vdev removal is initiated.
179 * Note that this object can be empty if none of the vdev has yet been
182 uint64_t vic_births_object;
185 * This is the vdev ID which was removed previous to this vdev, or
186 * UINT64_MAX if there are no previously removed vdevs.
188 uint64_t vic_prev_indirect_vdev;
189 } vdev_indirect_config_t;
192 * Virtual device descriptor
196 * Common to all vdev types.
198 uint64_t vdev_id; /* child number in vdev parent */
199 uint64_t vdev_guid; /* unique ID for this vdev */
200 uint64_t vdev_guid_sum; /* self guid + all child guids */
201 uint64_t vdev_orig_guid; /* orig. guid prior to remove */
202 uint64_t vdev_asize; /* allocatable device capacity */
203 uint64_t vdev_min_asize; /* min acceptable asize */
204 uint64_t vdev_max_asize; /* max acceptable asize */
205 uint64_t vdev_ashift; /* block alignment shift */
207 * Logical block alignment shift
209 * The smallest sized/aligned I/O supported by the device.
211 uint64_t vdev_logical_ashift;
213 * Physical block alignment shift
215 * The device supports logical I/Os with vdev_logical_ashift
216 * size/alignment, but optimum performance will be achieved by
217 * aligning/sizing requests to vdev_physical_ashift. Smaller
218 * requests may be inflated or incur device level read-modify-write
221 * May be 0 to indicate no preference (i.e. use vdev_logical_ashift).
223 uint64_t vdev_physical_ashift;
224 uint64_t vdev_state; /* see VDEV_STATE_* #defines */
225 uint64_t vdev_prevstate; /* used when reopening a vdev */
226 vdev_ops_t *vdev_ops; /* vdev operations */
227 spa_t *vdev_spa; /* spa for this vdev */
228 void *vdev_tsd; /* type-specific data */
229 vnode_t *vdev_name_vp; /* vnode for pathname */
230 vnode_t *vdev_devid_vp; /* vnode for devid */
231 vdev_t *vdev_top; /* top-level vdev */
232 vdev_t *vdev_parent; /* parent vdev */
233 vdev_t **vdev_child; /* array of children */
234 uint64_t vdev_children; /* number of children */
235 vdev_stat_t vdev_stat; /* virtual device statistics */
236 boolean_t vdev_expanding; /* expand the vdev? */
237 boolean_t vdev_reopening; /* reopen in progress? */
238 int vdev_open_error; /* error on last open */
239 kthread_t *vdev_open_thread; /* thread opening children */
240 uint64_t vdev_crtxg; /* txg when top-level was added */
243 * Top-level vdev state.
245 uint64_t vdev_ms_array; /* metaslab array object */
246 uint64_t vdev_ms_shift; /* metaslab size shift */
247 uint64_t vdev_ms_count; /* number of metaslabs */
248 metaslab_group_t *vdev_mg; /* metaslab group */
249 metaslab_t **vdev_ms; /* metaslab array */
250 txg_list_t vdev_ms_list; /* per-txg dirty metaslab lists */
251 txg_list_t vdev_dtl_list; /* per-txg dirty DTL lists */
252 txg_node_t vdev_txg_node; /* per-txg dirty vdev linkage */
253 boolean_t vdev_remove_wanted; /* async remove wanted? */
254 boolean_t vdev_probe_wanted; /* async probe wanted? */
255 list_node_t vdev_config_dirty_node; /* config dirty list */
256 list_node_t vdev_state_dirty_node; /* state dirty list */
257 uint64_t vdev_deflate_ratio; /* deflation ratio (x512) */
258 uint64_t vdev_islog; /* is an intent log device */
259 uint64_t vdev_removing; /* device is being removed? */
260 boolean_t vdev_ishole; /* is a hole in the namespace */
261 kmutex_t vdev_queue_lock; /* protects vdev_queue_depth */
262 uint64_t vdev_top_zap;
264 /* pool checkpoint related */
265 space_map_t *vdev_checkpoint_sm; /* contains reserved blocks */
267 boolean_t vdev_initialize_exit_wanted;
268 vdev_initializing_state_t vdev_initialize_state;
269 kthread_t *vdev_initialize_thread;
270 /* Protects vdev_initialize_thread and vdev_initialize_state. */
271 kmutex_t vdev_initialize_lock;
272 kcondvar_t vdev_initialize_cv;
273 uint64_t vdev_initialize_offset[TXG_SIZE];
274 uint64_t vdev_initialize_last_offset;
275 range_tree_t *vdev_initialize_tree; /* valid while initializing */
276 uint64_t vdev_initialize_bytes_est;
277 uint64_t vdev_initialize_bytes_done;
278 time_t vdev_initialize_action_time; /* start and end time */
280 /* for limiting outstanding I/Os */
281 kmutex_t vdev_initialize_io_lock;
282 kcondvar_t vdev_initialize_io_cv;
283 uint64_t vdev_initialize_inflight;
286 * Values stored in the config for an indirect or removing vdev.
288 vdev_indirect_config_t vdev_indirect_config;
291 * The vdev_indirect_rwlock protects the vdev_indirect_mapping
292 * pointer from changing on indirect vdevs (when it is condensed).
293 * Note that removing (not yet indirect) vdevs have different
294 * access patterns (the mapping is not accessed from open context,
295 * e.g. from zio_read) and locking strategy (e.g. svr_lock).
297 krwlock_t vdev_indirect_rwlock;
298 vdev_indirect_mapping_t *vdev_indirect_mapping;
299 vdev_indirect_births_t *vdev_indirect_births;
302 * In memory data structures used to manage the obsolete sm, for
303 * indirect or removing vdevs.
305 * The vdev_obsolete_segments is the in-core record of the segments
306 * that are no longer referenced anywhere in the pool (due to
307 * being freed or remapped and not referenced by any snapshots).
308 * During a sync, segments are added to vdev_obsolete_segments
309 * via vdev_indirect_mark_obsolete(); at the end of each sync
310 * pass, this is appended to vdev_obsolete_sm via
311 * vdev_indirect_sync_obsolete(). The vdev_obsolete_lock
312 * protects against concurrent modifications of vdev_obsolete_segments
313 * from multiple zio threads.
315 kmutex_t vdev_obsolete_lock;
316 range_tree_t *vdev_obsolete_segments;
317 space_map_t *vdev_obsolete_sm;
320 * The queue depth parameters determine how many async writes are
321 * still pending (i.e. allocated by net yet issued to disk) per
322 * top-level (vdev_async_write_queue_depth) and the maximum allowed
323 * (vdev_max_async_write_queue_depth). These values only apply to
326 uint64_t vdev_async_write_queue_depth;
327 uint64_t vdev_max_async_write_queue_depth;
330 * Protects the vdev_scan_io_queue field itself as well as the
331 * structure's contents (when present).
333 kmutex_t vdev_scan_io_queue_lock;
334 struct dsl_scan_io_queue *vdev_scan_io_queue;
339 range_tree_t *vdev_dtl[DTL_TYPES]; /* dirty time logs */
340 space_map_t *vdev_dtl_sm; /* dirty time log space map */
341 txg_node_t vdev_dtl_node; /* per-txg dirty DTL linkage */
342 uint64_t vdev_dtl_object; /* DTL object */
343 uint64_t vdev_psize; /* physical device capacity */
344 uint64_t vdev_wholedisk; /* true if this is a whole disk */
345 uint64_t vdev_offline; /* persistent offline state */
346 uint64_t vdev_faulted; /* persistent faulted state */
347 uint64_t vdev_degraded; /* persistent degraded state */
348 uint64_t vdev_removed; /* persistent removed state */
349 uint64_t vdev_resilver_txg; /* persistent resilvering state */
350 uint64_t vdev_nparity; /* number of parity devices for raidz */
351 char *vdev_path; /* vdev path (if any) */
352 char *vdev_devid; /* vdev devid (if any) */
353 char *vdev_physpath; /* vdev device path (if any) */
354 char *vdev_fru; /* physical FRU location */
355 uint64_t vdev_not_present; /* not present during import */
356 uint64_t vdev_unspare; /* unspare when resilvering done */
357 boolean_t vdev_nowritecache; /* true if flushwritecache failed */
358 boolean_t vdev_notrim; /* true if trim failed */
359 boolean_t vdev_checkremove; /* temporary online test */
360 boolean_t vdev_forcefault; /* force online fault */
361 boolean_t vdev_splitting; /* split or repair in progress */
362 boolean_t vdev_delayed_close; /* delayed device close? */
363 boolean_t vdev_tmpoffline; /* device taken offline temporarily? */
364 boolean_t vdev_detached; /* device detached? */
365 boolean_t vdev_cant_read; /* vdev is failing all reads */
366 boolean_t vdev_cant_write; /* vdev is failing all writes */
367 boolean_t vdev_isspare; /* was a hot spare */
368 boolean_t vdev_isl2cache; /* was a l2cache device */
369 vdev_queue_t vdev_queue; /* I/O deadline schedule queue */
370 vdev_cache_t vdev_cache; /* physical block cache */
371 spa_aux_vdev_t *vdev_aux; /* for l2cache and spares vdevs */
372 zio_t *vdev_probe_zio; /* root of current probe */
373 vdev_aux_t vdev_label_aux; /* on-disk aux state */
374 struct trim_map *vdev_trimmap; /* map on outstanding trims */
375 uint16_t vdev_rotation_rate; /* rotational rate of the media */
376 #define VDEV_RATE_UNKNOWN 0
377 #define VDEV_RATE_NON_ROTATING 1
378 uint64_t vdev_leaf_zap;
381 * For DTrace to work in userland (libzpool) context, these fields must
382 * remain at the end of the structure. DTrace will use the kernel's
383 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
384 * larger in userland, the offsets for the rest of the fields would be
387 kmutex_t vdev_dtl_lock; /* vdev_dtl_{map,resilver} */
388 kmutex_t vdev_stat_lock; /* vdev_stat */
389 kmutex_t vdev_probe_lock; /* protects vdev_probe_zio */
392 #define VDEV_RAIDZ_MAXPARITY 3
394 #define VDEV_PAD_SIZE (8 << 10)
395 /* 2 padding areas (vl_pad1 and vl_pad2) to skip */
396 #define VDEV_SKIP_SIZE VDEV_PAD_SIZE * 2
397 #define VDEV_PHYS_SIZE (112 << 10)
398 #define VDEV_UBERBLOCK_RING (128 << 10)
400 /* The largest uberblock we support is 8k. */
401 #define MAX_UBERBLOCK_SHIFT (13)
402 #define VDEV_UBERBLOCK_SHIFT(vd) \
403 MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
405 #define VDEV_UBERBLOCK_COUNT(vd) \
406 (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
407 #define VDEV_UBERBLOCK_OFFSET(vd, n) \
408 offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
409 #define VDEV_UBERBLOCK_SIZE(vd) (1ULL << VDEV_UBERBLOCK_SHIFT(vd))
411 typedef struct vdev_phys {
412 char vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
416 typedef struct vdev_label {
417 char vl_pad1[VDEV_PAD_SIZE]; /* 8K */
418 char vl_pad2[VDEV_PAD_SIZE]; /* 8K */
419 vdev_phys_t vl_vdev_phys; /* 112K */
420 char vl_uberblock[VDEV_UBERBLOCK_RING]; /* 128K */
421 } vdev_label_t; /* 256K total */
426 #define VDD_METASLAB 0x01
429 /* Offset of embedded boot loader region on each label */
430 #define VDEV_BOOT_OFFSET (2 * sizeof (vdev_label_t))
432 * Size of embedded boot loader region on each label.
433 * The total size of the first two labels plus the boot area is 4MB.
435 #define VDEV_BOOT_SIZE (7ULL << 19) /* 3.5M */
438 * Size of label regions at the start and end of each leaf device.
440 #define VDEV_LABEL_START_SIZE (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
441 #define VDEV_LABEL_END_SIZE (2 * sizeof (vdev_label_t))
442 #define VDEV_LABELS 4
443 #define VDEV_BEST_LABEL VDEV_LABELS
445 #define VDEV_ALLOC_LOAD 0
446 #define VDEV_ALLOC_ADD 1
447 #define VDEV_ALLOC_SPARE 2
448 #define VDEV_ALLOC_L2CACHE 3
449 #define VDEV_ALLOC_ROOTPOOL 4
450 #define VDEV_ALLOC_SPLIT 5
451 #define VDEV_ALLOC_ATTACH 6
454 * Allocate or free a vdev
456 extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
458 extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
459 vdev_t *parent, uint_t id, int alloctype);
460 extern void vdev_free(vdev_t *vd);
463 * Add or remove children and parents
465 extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
466 extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
467 extern void vdev_compact_children(vdev_t *pvd);
468 extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
469 extern void vdev_remove_parent(vdev_t *cvd);
472 * vdev sync load and sync
474 extern boolean_t vdev_log_state_valid(vdev_t *vd);
475 extern int vdev_load(vdev_t *vd);
476 extern int vdev_dtl_load(vdev_t *vd);
477 extern void vdev_sync(vdev_t *vd, uint64_t txg);
478 extern void vdev_sync_done(vdev_t *vd, uint64_t txg);
479 extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
480 extern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
483 * Available vdev types.
485 extern vdev_ops_t vdev_root_ops;
486 extern vdev_ops_t vdev_mirror_ops;
487 extern vdev_ops_t vdev_replacing_ops;
488 extern vdev_ops_t vdev_raidz_ops;
490 extern vdev_ops_t vdev_geom_ops;
492 extern vdev_ops_t vdev_disk_ops;
494 extern vdev_ops_t vdev_file_ops;
495 extern vdev_ops_t vdev_missing_ops;
496 extern vdev_ops_t vdev_hole_ops;
497 extern vdev_ops_t vdev_spare_ops;
498 extern vdev_ops_t vdev_indirect_ops;
501 * Common size functions
503 extern void vdev_default_xlate(vdev_t *vd, const range_seg_t *in,
505 extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
506 extern uint64_t vdev_get_min_asize(vdev_t *vd);
507 extern void vdev_set_min_asize(vdev_t *vd);
512 extern int vdev_standard_sm_blksz;
513 /* zdb uses this tunable, so it must be declared here to make lint happy. */
514 extern int zfs_vdev_cache_size;
515 extern uint_t zfs_geom_probe_vdev_key;
518 * Functions from vdev_indirect.c
520 extern void vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx);
521 extern boolean_t vdev_indirect_should_condense(vdev_t *vd);
522 extern void spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx);
523 extern int vdev_obsolete_sm_object(vdev_t *vd);
524 extern boolean_t vdev_obsolete_counts_are_precise(vdev_t *vd);
528 * Other miscellaneous functions
530 int vdev_checkpoint_sm_object(vdev_t *vd);
533 * The vdev_buf_t is used to translate between zio_t and buf_t, and back again.
535 typedef struct vdev_buf {
536 buf_t vb_buf; /* buffer that describes the io */
537 zio_t *vb_io; /* pointer back to the original zio_t */
545 #endif /* _SYS_VDEV_IMPL_H */