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, 2015 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 uint32_t zfs_vdev_async_write_max_active;
65 * Virtual device operations
67 typedef int vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *max_size,
68 uint64_t *logical_ashift, uint64_t *physical_ashift);
69 typedef void vdev_close_func_t(vdev_t *vd);
70 typedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize);
71 typedef void vdev_io_start_func_t(zio_t *zio);
72 typedef void vdev_io_done_func_t(zio_t *zio);
73 typedef void vdev_state_change_func_t(vdev_t *vd, int, int);
74 typedef void vdev_hold_func_t(vdev_t *vd);
75 typedef void vdev_rele_func_t(vdev_t *vd);
77 typedef void vdev_remap_cb_t(uint64_t inner_offset, vdev_t *vd,
78 uint64_t offset, uint64_t size, void *arg);
79 typedef void vdev_remap_func_t(vdev_t *vd, uint64_t offset, uint64_t size,
80 vdev_remap_cb_t callback, void *arg);
82 typedef struct vdev_ops {
83 vdev_open_func_t *vdev_op_open;
84 vdev_close_func_t *vdev_op_close;
85 vdev_asize_func_t *vdev_op_asize;
86 vdev_io_start_func_t *vdev_op_io_start;
87 vdev_io_done_func_t *vdev_op_io_done;
88 vdev_state_change_func_t *vdev_op_state_change;
89 vdev_hold_func_t *vdev_op_hold;
90 vdev_rele_func_t *vdev_op_rele;
91 vdev_remap_func_t *vdev_op_remap;
92 char vdev_op_type[16];
93 boolean_t vdev_op_leaf;
97 * Virtual device properties
99 struct vdev_cache_entry {
102 uint64_t ve_lastused;
103 avl_node_t ve_offset_node;
104 avl_node_t ve_lastused_node;
106 uint16_t ve_missed_update;
111 avl_tree_t vc_offset_tree;
112 avl_tree_t vc_lastused_tree;
116 typedef struct vdev_queue_class {
120 * Sorted by offset or timestamp, depending on if the queue is
121 * LBA-ordered vs FIFO.
123 avl_tree_t vqc_queued_tree;
124 } vdev_queue_class_t;
128 vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
129 avl_tree_t vq_active_tree;
130 avl_tree_t vq_read_offset_tree;
131 avl_tree_t vq_write_offset_tree;
132 uint64_t vq_last_offset;
133 hrtime_t vq_io_complete_ts; /* time last i/o completed */
135 uint64_t vq_lastoffset;
139 * On-disk indirect vdev state.
141 * An indirect vdev is described exclusively in the MOS config of a pool.
142 * The config for an indirect vdev includes several fields, which are
143 * accessed in memory by a vdev_indirect_config_t.
145 typedef struct vdev_indirect_config {
147 * Object (in MOS) which contains the indirect mapping. This object
148 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
149 * vimep_src. The bonus buffer for this object is a
150 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
151 * removal is initiated.
153 * Note that this object can be empty if none of the data on the vdev
154 * has been copied yet.
156 uint64_t vic_mapping_object;
159 * Object (in MOS) which contains the birth times for the mapping
160 * entries. This object contains an array of
161 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
162 * buffer for this object is a vdev_indirect_birth_phys_t. This object
163 * is allocated when a vdev removal is initiated.
165 * Note that this object can be empty if none of the vdev has yet been
168 uint64_t vic_births_object;
171 * This is the vdev ID which was removed previous to this vdev, or
172 * UINT64_MAX if there are no previously removed vdevs.
174 uint64_t vic_prev_indirect_vdev;
175 } vdev_indirect_config_t;
178 * Virtual device descriptor
182 * Common to all vdev types.
184 uint64_t vdev_id; /* child number in vdev parent */
185 uint64_t vdev_guid; /* unique ID for this vdev */
186 uint64_t vdev_guid_sum; /* self guid + all child guids */
187 uint64_t vdev_orig_guid; /* orig. guid prior to remove */
188 uint64_t vdev_asize; /* allocatable device capacity */
189 uint64_t vdev_min_asize; /* min acceptable asize */
190 uint64_t vdev_max_asize; /* max acceptable asize */
191 uint64_t vdev_ashift; /* block alignment shift */
193 * Logical block alignment shift
195 * The smallest sized/aligned I/O supported by the device.
197 uint64_t vdev_logical_ashift;
199 * Physical block alignment shift
201 * The device supports logical I/Os with vdev_logical_ashift
202 * size/alignment, but optimum performance will be achieved by
203 * aligning/sizing requests to vdev_physical_ashift. Smaller
204 * requests may be inflated or incur device level read-modify-write
207 * May be 0 to indicate no preference (i.e. use vdev_logical_ashift).
209 uint64_t vdev_physical_ashift;
210 uint64_t vdev_state; /* see VDEV_STATE_* #defines */
211 uint64_t vdev_prevstate; /* used when reopening a vdev */
212 vdev_ops_t *vdev_ops; /* vdev operations */
213 spa_t *vdev_spa; /* spa for this vdev */
214 void *vdev_tsd; /* type-specific data */
215 vnode_t *vdev_name_vp; /* vnode for pathname */
216 vnode_t *vdev_devid_vp; /* vnode for devid */
217 vdev_t *vdev_top; /* top-level vdev */
218 vdev_t *vdev_parent; /* parent vdev */
219 vdev_t **vdev_child; /* array of children */
220 uint64_t vdev_children; /* number of children */
221 vdev_stat_t vdev_stat; /* virtual device statistics */
222 boolean_t vdev_expanding; /* expand the vdev? */
223 boolean_t vdev_reopening; /* reopen in progress? */
224 int vdev_open_error; /* error on last open */
225 kthread_t *vdev_open_thread; /* thread opening children */
226 uint64_t vdev_crtxg; /* txg when top-level was added */
229 * Top-level vdev state.
231 uint64_t vdev_ms_array; /* metaslab array object */
232 uint64_t vdev_ms_shift; /* metaslab size shift */
233 uint64_t vdev_ms_count; /* number of metaslabs */
234 metaslab_group_t *vdev_mg; /* metaslab group */
235 metaslab_t **vdev_ms; /* metaslab array */
236 txg_list_t vdev_ms_list; /* per-txg dirty metaslab lists */
237 txg_list_t vdev_dtl_list; /* per-txg dirty DTL lists */
238 txg_node_t vdev_txg_node; /* per-txg dirty vdev linkage */
239 boolean_t vdev_remove_wanted; /* async remove wanted? */
240 boolean_t vdev_probe_wanted; /* async probe wanted? */
241 list_node_t vdev_config_dirty_node; /* config dirty list */
242 list_node_t vdev_state_dirty_node; /* state dirty list */
243 uint64_t vdev_deflate_ratio; /* deflation ratio (x512) */
244 uint64_t vdev_islog; /* is an intent log device */
245 uint64_t vdev_removing; /* device is being removed? */
246 boolean_t vdev_ishole; /* is a hole in the namespace */
247 kmutex_t vdev_queue_lock; /* protects vdev_queue_depth */
248 uint64_t vdev_top_zap;
251 * Values stored in the config for an indirect or removing vdev.
253 vdev_indirect_config_t vdev_indirect_config;
256 * The vdev_indirect_rwlock protects the vdev_indirect_mapping
257 * pointer from changing on indirect vdevs (when it is condensed).
258 * Note that removing (not yet indirect) vdevs have different
259 * access patterns (the mapping is not accessed from open context,
260 * e.g. from zio_read) and locking strategy (e.g. svr_lock).
262 krwlock_t vdev_indirect_rwlock;
263 vdev_indirect_mapping_t *vdev_indirect_mapping;
264 vdev_indirect_births_t *vdev_indirect_births;
267 * In memory data structures used to manage the obsolete sm, for
268 * indirect or removing vdevs.
270 * The vdev_obsolete_segments is the in-core record of the segments
271 * that are no longer referenced anywhere in the pool (due to
272 * being freed or remapped and not referenced by any snapshots).
273 * During a sync, segments are added to vdev_obsolete_segments
274 * via vdev_indirect_mark_obsolete(); at the end of each sync
275 * pass, this is appended to vdev_obsolete_sm via
276 * vdev_indirect_sync_obsolete(). The vdev_obsolete_lock
277 * protects against concurrent modifications of vdev_obsolete_segments
278 * from multiple zio threads.
280 kmutex_t vdev_obsolete_lock;
281 range_tree_t *vdev_obsolete_segments;
282 space_map_t *vdev_obsolete_sm;
285 * The queue depth parameters determine how many async writes are
286 * still pending (i.e. allocated by net yet issued to disk) per
287 * top-level (vdev_async_write_queue_depth) and the maximum allowed
288 * (vdev_max_async_write_queue_depth). These values only apply to
291 uint64_t vdev_async_write_queue_depth;
292 uint64_t vdev_max_async_write_queue_depth;
297 range_tree_t *vdev_dtl[DTL_TYPES]; /* dirty time logs */
298 space_map_t *vdev_dtl_sm; /* dirty time log space map */
299 txg_node_t vdev_dtl_node; /* per-txg dirty DTL linkage */
300 uint64_t vdev_dtl_object; /* DTL object */
301 uint64_t vdev_psize; /* physical device capacity */
302 uint64_t vdev_wholedisk; /* true if this is a whole disk */
303 uint64_t vdev_offline; /* persistent offline state */
304 uint64_t vdev_faulted; /* persistent faulted state */
305 uint64_t vdev_degraded; /* persistent degraded state */
306 uint64_t vdev_removed; /* persistent removed state */
307 uint64_t vdev_resilver_txg; /* persistent resilvering state */
308 uint64_t vdev_nparity; /* number of parity devices for raidz */
309 char *vdev_path; /* vdev path (if any) */
310 char *vdev_devid; /* vdev devid (if any) */
311 char *vdev_physpath; /* vdev device path (if any) */
312 char *vdev_fru; /* physical FRU location */
313 uint64_t vdev_not_present; /* not present during import */
314 uint64_t vdev_unspare; /* unspare when resilvering done */
315 boolean_t vdev_nowritecache; /* true if flushwritecache failed */
316 boolean_t vdev_notrim; /* true if trim failed */
317 boolean_t vdev_checkremove; /* temporary online test */
318 boolean_t vdev_forcefault; /* force online fault */
319 boolean_t vdev_splitting; /* split or repair in progress */
320 boolean_t vdev_delayed_close; /* delayed device close? */
321 boolean_t vdev_tmpoffline; /* device taken offline temporarily? */
322 boolean_t vdev_detached; /* device detached? */
323 boolean_t vdev_cant_read; /* vdev is failing all reads */
324 boolean_t vdev_cant_write; /* vdev is failing all writes */
325 boolean_t vdev_isspare; /* was a hot spare */
326 boolean_t vdev_isl2cache; /* was a l2cache device */
327 vdev_queue_t vdev_queue; /* I/O deadline schedule queue */
328 vdev_cache_t vdev_cache; /* physical block cache */
329 spa_aux_vdev_t *vdev_aux; /* for l2cache and spares vdevs */
330 zio_t *vdev_probe_zio; /* root of current probe */
331 vdev_aux_t vdev_label_aux; /* on-disk aux state */
332 struct trim_map *vdev_trimmap; /* map on outstanding trims */
333 uint16_t vdev_rotation_rate; /* rotational rate of the media */
334 #define VDEV_RATE_UNKNOWN 0
335 #define VDEV_RATE_NON_ROTATING 1
336 uint64_t vdev_leaf_zap;
339 * For DTrace to work in userland (libzpool) context, these fields must
340 * remain at the end of the structure. DTrace will use the kernel's
341 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
342 * larger in userland, the offsets for the rest of the fields would be
345 kmutex_t vdev_dtl_lock; /* vdev_dtl_{map,resilver} */
346 kmutex_t vdev_stat_lock; /* vdev_stat */
347 kmutex_t vdev_probe_lock; /* protects vdev_probe_zio */
350 #define VDEV_RAIDZ_MAXPARITY 3
352 #define VDEV_PAD_SIZE (8 << 10)
353 /* 2 padding areas (vl_pad1 and vl_pad2) to skip */
354 #define VDEV_SKIP_SIZE VDEV_PAD_SIZE * 2
355 #define VDEV_PHYS_SIZE (112 << 10)
356 #define VDEV_UBERBLOCK_RING (128 << 10)
358 /* The largest uberblock we support is 8k. */
359 #define MAX_UBERBLOCK_SHIFT (13)
360 #define VDEV_UBERBLOCK_SHIFT(vd) \
361 MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
363 #define VDEV_UBERBLOCK_COUNT(vd) \
364 (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
365 #define VDEV_UBERBLOCK_OFFSET(vd, n) \
366 offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
367 #define VDEV_UBERBLOCK_SIZE(vd) (1ULL << VDEV_UBERBLOCK_SHIFT(vd))
369 typedef struct vdev_phys {
370 char vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
374 typedef struct vdev_label {
375 char vl_pad1[VDEV_PAD_SIZE]; /* 8K */
376 char vl_pad2[VDEV_PAD_SIZE]; /* 8K */
377 vdev_phys_t vl_vdev_phys; /* 112K */
378 char vl_uberblock[VDEV_UBERBLOCK_RING]; /* 128K */
379 } vdev_label_t; /* 256K total */
384 #define VDD_METASLAB 0x01
387 /* Offset of embedded boot loader region on each label */
388 #define VDEV_BOOT_OFFSET (2 * sizeof (vdev_label_t))
390 * Size of embedded boot loader region on each label.
391 * The total size of the first two labels plus the boot area is 4MB.
393 #define VDEV_BOOT_SIZE (7ULL << 19) /* 3.5M */
396 * Size of label regions at the start and end of each leaf device.
398 #define VDEV_LABEL_START_SIZE (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
399 #define VDEV_LABEL_END_SIZE (2 * sizeof (vdev_label_t))
400 #define VDEV_LABELS 4
401 #define VDEV_BEST_LABEL VDEV_LABELS
403 #define VDEV_ALLOC_LOAD 0
404 #define VDEV_ALLOC_ADD 1
405 #define VDEV_ALLOC_SPARE 2
406 #define VDEV_ALLOC_L2CACHE 3
407 #define VDEV_ALLOC_ROOTPOOL 4
408 #define VDEV_ALLOC_SPLIT 5
409 #define VDEV_ALLOC_ATTACH 6
412 * Allocate or free a vdev
414 extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
416 extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
417 vdev_t *parent, uint_t id, int alloctype);
418 extern void vdev_free(vdev_t *vd);
421 * Add or remove children and parents
423 extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
424 extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
425 extern void vdev_compact_children(vdev_t *pvd);
426 extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
427 extern void vdev_remove_parent(vdev_t *cvd);
430 * vdev sync load and sync
432 extern boolean_t vdev_log_state_valid(vdev_t *vd);
433 extern int vdev_load(vdev_t *vd);
434 extern int vdev_dtl_load(vdev_t *vd);
435 extern void vdev_sync(vdev_t *vd, uint64_t txg);
436 extern void vdev_sync_done(vdev_t *vd, uint64_t txg);
437 extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
438 extern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
441 * Available vdev types.
443 extern vdev_ops_t vdev_root_ops;
444 extern vdev_ops_t vdev_mirror_ops;
445 extern vdev_ops_t vdev_replacing_ops;
446 extern vdev_ops_t vdev_raidz_ops;
448 extern vdev_ops_t vdev_geom_ops;
450 extern vdev_ops_t vdev_disk_ops;
452 extern vdev_ops_t vdev_file_ops;
453 extern vdev_ops_t vdev_missing_ops;
454 extern vdev_ops_t vdev_hole_ops;
455 extern vdev_ops_t vdev_spare_ops;
456 extern vdev_ops_t vdev_indirect_ops;
459 * Common size functions
461 extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
462 extern uint64_t vdev_get_min_asize(vdev_t *vd);
463 extern void vdev_set_min_asize(vdev_t *vd);
468 /* zdb uses this tunable, so it must be declared here to make lint happy. */
469 extern int zfs_vdev_cache_size;
470 extern uint_t zfs_geom_probe_vdev_key;
473 * Functions from vdev_indirect.c
475 extern void vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx);
476 extern boolean_t vdev_indirect_should_condense(vdev_t *vd);
477 extern void spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx);
478 extern int vdev_obsolete_sm_object(vdev_t *vd);
479 extern boolean_t vdev_obsolete_counts_are_precise(vdev_t *vd);
483 * The vdev_buf_t is used to translate between zio_t and buf_t, and back again.
485 typedef struct vdev_buf {
486 buf_t vb_buf; /* buffer that describes the io */
487 zio_t *vb_io; /* pointer back to the original zio_t */
495 #endif /* _SYS_VDEV_IMPL_H */