Merge commit '7e3488dc6cdcb0c04e1ce167a1a3bfef83b5f2e0'
[unleashed.git] / kernel / fs / zfs / sys / vdev_impl.h
blobd4ba5669a07f9116e0d98826e7322154d72d15a9
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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
29 #include <sys/avl.h>
30 #include <sys/bpobj.h>
31 #include <sys/dmu.h>
32 #include <sys/metaslab.h>
33 #include <sys/nvpair.h>
34 #include <sys/space_map.h>
35 #include <sys/vdev.h>
36 #include <sys/dkio.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>
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
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;
59 struct abd;
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 *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 void vdev_hold_func_t(vdev_t *vd);
76 typedef void vdev_rele_func_t(vdev_t *vd);
78 typedef void vdev_remap_cb_t(uint64_t inner_offset, vdev_t *vd,
79 uint64_t offset, uint64_t size, void *arg);
80 typedef void vdev_remap_func_t(vdev_t *vd, uint64_t offset, uint64_t size,
81 vdev_remap_cb_t callback, void *arg);
83 * Given a target vdev, translates the logical range "in" to the physical
84 * range "res"
86 typedef void vdev_xlation_func_t(vdev_t *cvd, const range_seg_t *in,
87 range_seg_t *res);
89 typedef struct vdev_ops {
90 vdev_open_func_t *vdev_op_open;
91 vdev_close_func_t *vdev_op_close;
92 vdev_asize_func_t *vdev_op_asize;
93 vdev_io_start_func_t *vdev_op_io_start;
94 vdev_io_done_func_t *vdev_op_io_done;
95 vdev_state_change_func_t *vdev_op_state_change;
96 vdev_hold_func_t *vdev_op_hold;
97 vdev_rele_func_t *vdev_op_rele;
98 vdev_remap_func_t *vdev_op_remap;
100 * For translating ranges from non-leaf vdevs (e.g. raidz) to leaves.
101 * Used when initializing vdevs. Isn't used by leaf ops.
103 vdev_xlation_func_t *vdev_op_xlate;
104 char vdev_op_type[16];
105 boolean_t vdev_op_leaf;
106 } vdev_ops_t;
109 * Virtual device properties
111 struct vdev_cache_entry {
112 struct abd *ve_abd;
113 uint64_t ve_offset;
114 uint64_t ve_lastused;
115 avl_node_t ve_offset_node;
116 avl_node_t ve_lastused_node;
117 uint32_t ve_hits;
118 uint16_t ve_missed_update;
119 zio_t *ve_fill_io;
122 struct vdev_cache {
123 avl_tree_t vc_offset_tree;
124 avl_tree_t vc_lastused_tree;
125 kmutex_t vc_lock;
128 typedef struct vdev_queue_class {
129 uint32_t vqc_active;
132 * Sorted by offset or timestamp, depending on if the queue is
133 * LBA-ordered vs FIFO.
135 avl_tree_t vqc_queued_tree;
136 } vdev_queue_class_t;
138 struct vdev_queue {
139 vdev_t *vq_vdev;
140 vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
141 avl_tree_t vq_active_tree;
142 avl_tree_t vq_read_offset_tree;
143 avl_tree_t vq_write_offset_tree;
144 uint64_t vq_last_offset;
145 hrtime_t vq_io_complete_ts; /* time last i/o completed */
146 kmutex_t vq_lock;
150 * On-disk indirect vdev state.
152 * An indirect vdev is described exclusively in the MOS config of a pool.
153 * The config for an indirect vdev includes several fields, which are
154 * accessed in memory by a vdev_indirect_config_t.
156 typedef struct vdev_indirect_config {
158 * Object (in MOS) which contains the indirect mapping. This object
159 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
160 * vimep_src. The bonus buffer for this object is a
161 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
162 * removal is initiated.
164 * Note that this object can be empty if none of the data on the vdev
165 * has been copied yet.
167 uint64_t vic_mapping_object;
170 * Object (in MOS) which contains the birth times for the mapping
171 * entries. This object contains an array of
172 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
173 * buffer for this object is a vdev_indirect_birth_phys_t. This object
174 * is allocated when a vdev removal is initiated.
176 * Note that this object can be empty if none of the vdev has yet been
177 * copied.
179 uint64_t vic_births_object;
182 * This is the vdev ID which was removed previous to this vdev, or
183 * UINT64_MAX if there are no previously removed vdevs.
185 uint64_t vic_prev_indirect_vdev;
186 } vdev_indirect_config_t;
189 * Virtual device descriptor
191 struct vdev {
193 * Common to all vdev types.
195 uint64_t vdev_id; /* child number in vdev parent */
196 uint64_t vdev_guid; /* unique ID for this vdev */
197 uint64_t vdev_guid_sum; /* self guid + all child guids */
198 uint64_t vdev_orig_guid; /* orig. guid prior to remove */
199 uint64_t vdev_asize; /* allocatable device capacity */
200 uint64_t vdev_min_asize; /* min acceptable asize */
201 uint64_t vdev_max_asize; /* max acceptable asize */
202 uint64_t vdev_ashift; /* block alignment shift */
203 uint64_t vdev_state; /* see VDEV_STATE_* #defines */
204 uint64_t vdev_prevstate; /* used when reopening a vdev */
205 vdev_ops_t *vdev_ops; /* vdev operations */
206 spa_t *vdev_spa; /* spa for this vdev */
207 void *vdev_tsd; /* type-specific data */
208 vnode_t *vdev_name_vp; /* vnode for pathname */
209 vnode_t *vdev_devid_vp; /* vnode for devid */
210 vdev_t *vdev_top; /* top-level vdev */
211 vdev_t *vdev_parent; /* parent vdev */
212 vdev_t **vdev_child; /* array of children */
213 uint64_t vdev_children; /* number of children */
214 vdev_stat_t vdev_stat; /* virtual device statistics */
215 boolean_t vdev_expanding; /* expand the vdev? */
216 boolean_t vdev_reopening; /* reopen in progress? */
217 int vdev_open_error; /* error on last open */
218 kthread_t *vdev_open_thread; /* thread opening children */
219 uint64_t vdev_crtxg; /* txg when top-level was added */
222 * Top-level vdev state.
224 uint64_t vdev_ms_array; /* metaslab array object */
225 uint64_t vdev_ms_shift; /* metaslab size shift */
226 uint64_t vdev_ms_count; /* number of metaslabs */
227 metaslab_group_t *vdev_mg; /* metaslab group */
228 metaslab_t **vdev_ms; /* metaslab array */
229 txg_list_t vdev_ms_list; /* per-txg dirty metaslab lists */
230 txg_list_t vdev_dtl_list; /* per-txg dirty DTL lists */
231 txg_node_t vdev_txg_node; /* per-txg dirty vdev linkage */
232 boolean_t vdev_remove_wanted; /* async remove wanted? */
233 boolean_t vdev_probe_wanted; /* async probe wanted? */
234 list_node_t vdev_config_dirty_node; /* config dirty list */
235 list_node_t vdev_state_dirty_node; /* state dirty list */
236 uint64_t vdev_deflate_ratio; /* deflation ratio (x512) */
237 uint64_t vdev_islog; /* is an intent log device */
238 uint64_t vdev_removing; /* device is being removed? */
239 boolean_t vdev_ishole; /* is a hole in the namespace */
240 kmutex_t vdev_queue_lock; /* protects vdev_queue_depth */
241 uint64_t vdev_top_zap;
243 /* pool checkpoint related */
244 space_map_t *vdev_checkpoint_sm; /* contains reserved blocks */
246 boolean_t vdev_initialize_exit_wanted;
247 vdev_initializing_state_t vdev_initialize_state;
248 kthread_t *vdev_initialize_thread;
249 /* Protects vdev_initialize_thread and vdev_initialize_state. */
250 kmutex_t vdev_initialize_lock;
251 kcondvar_t vdev_initialize_cv;
252 uint64_t vdev_initialize_offset[TXG_SIZE];
253 uint64_t vdev_initialize_last_offset;
254 range_tree_t *vdev_initialize_tree; /* valid while initializing */
255 uint64_t vdev_initialize_bytes_est;
256 uint64_t vdev_initialize_bytes_done;
257 time_t vdev_initialize_action_time; /* start and end time */
259 /* for limiting outstanding I/Os */
260 kmutex_t vdev_initialize_io_lock;
261 kcondvar_t vdev_initialize_io_cv;
262 uint64_t vdev_initialize_inflight;
265 * Values stored in the config for an indirect or removing vdev.
267 vdev_indirect_config_t vdev_indirect_config;
270 * The vdev_indirect_rwlock protects the vdev_indirect_mapping
271 * pointer from changing on indirect vdevs (when it is condensed).
272 * Note that removing (not yet indirect) vdevs have different
273 * access patterns (the mapping is not accessed from open context,
274 * e.g. from zio_read) and locking strategy (e.g. svr_lock).
276 krwlock_t vdev_indirect_rwlock;
277 vdev_indirect_mapping_t *vdev_indirect_mapping;
278 vdev_indirect_births_t *vdev_indirect_births;
281 * In memory data structures used to manage the obsolete sm, for
282 * indirect or removing vdevs.
284 * The vdev_obsolete_segments is the in-core record of the segments
285 * that are no longer referenced anywhere in the pool (due to
286 * being freed or remapped and not referenced by any snapshots).
287 * During a sync, segments are added to vdev_obsolete_segments
288 * via vdev_indirect_mark_obsolete(); at the end of each sync
289 * pass, this is appended to vdev_obsolete_sm via
290 * vdev_indirect_sync_obsolete(). The vdev_obsolete_lock
291 * protects against concurrent modifications of vdev_obsolete_segments
292 * from multiple zio threads.
294 kmutex_t vdev_obsolete_lock;
295 range_tree_t *vdev_obsolete_segments;
296 space_map_t *vdev_obsolete_sm;
299 * The queue depth parameters determine how many async writes are
300 * still pending (i.e. allocated by net yet issued to disk) per
301 * top-level (vdev_async_write_queue_depth) and the maximum allowed
302 * (vdev_max_async_write_queue_depth). These values only apply to
303 * top-level vdevs.
305 uint64_t vdev_async_write_queue_depth;
306 uint64_t vdev_max_async_write_queue_depth;
309 * Leaf vdev state.
311 range_tree_t *vdev_dtl[DTL_TYPES]; /* dirty time logs */
312 space_map_t *vdev_dtl_sm; /* dirty time log space map */
313 txg_node_t vdev_dtl_node; /* per-txg dirty DTL linkage */
314 uint64_t vdev_dtl_object; /* DTL object */
315 uint64_t vdev_psize; /* physical device capacity */
316 uint64_t vdev_wholedisk; /* true if this is a whole disk */
317 uint64_t vdev_offline; /* persistent offline state */
318 uint64_t vdev_faulted; /* persistent faulted state */
319 uint64_t vdev_degraded; /* persistent degraded state */
320 uint64_t vdev_removed; /* persistent removed state */
321 uint64_t vdev_resilver_txg; /* persistent resilvering state */
322 uint64_t vdev_nparity; /* number of parity devices for raidz */
323 char *vdev_path; /* vdev path (if any) */
324 char *vdev_devid; /* vdev devid (if any) */
325 char *vdev_physpath; /* vdev device path (if any) */
326 char *vdev_fru; /* physical FRU location */
327 uint64_t vdev_not_present; /* not present during import */
328 uint64_t vdev_unspare; /* unspare when resilvering done */
329 boolean_t vdev_nowritecache; /* true if flushwritecache failed */
330 boolean_t vdev_checkremove; /* temporary online test */
331 boolean_t vdev_forcefault; /* force online fault */
332 boolean_t vdev_splitting; /* split or repair in progress */
333 boolean_t vdev_delayed_close; /* delayed device close? */
334 boolean_t vdev_tmpoffline; /* device taken offline temporarily? */
335 boolean_t vdev_detached; /* device detached? */
336 boolean_t vdev_cant_read; /* vdev is failing all reads */
337 boolean_t vdev_cant_write; /* vdev is failing all writes */
338 boolean_t vdev_isspare; /* was a hot spare */
339 boolean_t vdev_isl2cache; /* was a l2cache device */
340 vdev_queue_t vdev_queue; /* I/O deadline schedule queue */
341 vdev_cache_t vdev_cache; /* physical block cache */
342 spa_aux_vdev_t *vdev_aux; /* for l2cache and spares vdevs */
343 zio_t *vdev_probe_zio; /* root of current probe */
344 vdev_aux_t vdev_label_aux; /* on-disk aux state */
345 uint64_t vdev_leaf_zap;
348 * For DTrace to work in userland (libzpool) context, these fields must
349 * remain at the end of the structure. DTrace will use the kernel's
350 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
351 * larger in userland, the offsets for the rest of the fields would be
352 * incorrect.
354 kmutex_t vdev_dtl_lock; /* vdev_dtl_{map,resilver} */
355 kmutex_t vdev_stat_lock; /* vdev_stat */
356 kmutex_t vdev_probe_lock; /* protects vdev_probe_zio */
359 #define VDEV_RAIDZ_MAXPARITY 3
361 #define VDEV_PAD_SIZE (8 << 10)
362 /* 2 padding areas (vl_pad1 and vl_pad2) to skip */
363 #define VDEV_SKIP_SIZE VDEV_PAD_SIZE * 2
364 #define VDEV_PHYS_SIZE (112 << 10)
365 #define VDEV_UBERBLOCK_RING (128 << 10)
367 /* The largest uberblock we support is 8k. */
368 #define MAX_UBERBLOCK_SHIFT (13)
369 #define VDEV_UBERBLOCK_SHIFT(vd) \
370 MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
371 MAX_UBERBLOCK_SHIFT)
372 #define VDEV_UBERBLOCK_COUNT(vd) \
373 (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
374 #define VDEV_UBERBLOCK_OFFSET(vd, n) \
375 offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
376 #define VDEV_UBERBLOCK_SIZE(vd) (1ULL << VDEV_UBERBLOCK_SHIFT(vd))
378 typedef struct vdev_phys {
379 char vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
380 zio_eck_t vp_zbt;
381 } vdev_phys_t;
383 typedef struct vdev_label {
384 char vl_pad1[VDEV_PAD_SIZE]; /* 8K */
385 char vl_pad2[VDEV_PAD_SIZE]; /* 8K */
386 vdev_phys_t vl_vdev_phys; /* 112K */
387 char vl_uberblock[VDEV_UBERBLOCK_RING]; /* 128K */
388 } vdev_label_t; /* 256K total */
391 * vdev_dirty() flags
393 #define VDD_METASLAB 0x01
394 #define VDD_DTL 0x02
396 /* Offset of embedded boot loader region on each label */
397 #define VDEV_BOOT_OFFSET (2 * sizeof (vdev_label_t))
399 * Size of embedded boot loader region on each label.
400 * The total size of the first two labels plus the boot area is 4MB.
402 #define VDEV_BOOT_SIZE (7ULL << 19) /* 3.5M */
405 * Size of label regions at the start and end of each leaf device.
407 #define VDEV_LABEL_START_SIZE (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
408 #define VDEV_LABEL_END_SIZE (2 * sizeof (vdev_label_t))
409 #define VDEV_LABELS 4
410 #define VDEV_BEST_LABEL VDEV_LABELS
412 #define VDEV_ALLOC_LOAD 0
413 #define VDEV_ALLOC_ADD 1
414 #define VDEV_ALLOC_SPARE 2
415 #define VDEV_ALLOC_L2CACHE 3
416 #define VDEV_ALLOC_ROOTPOOL 4
417 #define VDEV_ALLOC_SPLIT 5
418 #define VDEV_ALLOC_ATTACH 6
421 * Allocate or free a vdev
423 extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
424 vdev_ops_t *ops);
425 extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
426 vdev_t *parent, uint_t id, int alloctype);
427 extern void vdev_free(vdev_t *vd);
430 * Add or remove children and parents
432 extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
433 extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
434 extern void vdev_compact_children(vdev_t *pvd);
435 extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
436 extern void vdev_remove_parent(vdev_t *cvd);
439 * vdev sync load and sync
441 extern boolean_t vdev_log_state_valid(vdev_t *vd);
442 extern int vdev_load(vdev_t *vd);
443 extern int vdev_dtl_load(vdev_t *vd);
444 extern void vdev_sync(vdev_t *vd, uint64_t txg);
445 extern void vdev_sync_done(vdev_t *vd, uint64_t txg);
446 extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
447 extern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
450 * Available vdev types.
452 extern vdev_ops_t vdev_root_ops;
453 extern vdev_ops_t vdev_mirror_ops;
454 extern vdev_ops_t vdev_replacing_ops;
455 extern vdev_ops_t vdev_raidz_ops;
456 extern vdev_ops_t vdev_disk_ops;
457 extern vdev_ops_t vdev_file_ops;
458 extern vdev_ops_t vdev_missing_ops;
459 extern vdev_ops_t vdev_hole_ops;
460 extern vdev_ops_t vdev_spare_ops;
461 extern vdev_ops_t vdev_indirect_ops;
464 * Common size functions
466 extern void vdev_default_xlate(vdev_t *vd, const range_seg_t *in,
467 range_seg_t *out);
468 extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
469 extern uint64_t vdev_get_min_asize(vdev_t *vd);
470 extern void vdev_set_min_asize(vdev_t *vd);
473 * Global variables
475 extern int vdev_standard_sm_blksz;
476 /* zdb uses this tunable, so it must be declared here to make lint happy. */
477 extern int zfs_vdev_cache_size;
480 * Functions from vdev_indirect.c
482 extern void vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx);
483 extern boolean_t vdev_indirect_should_condense(vdev_t *vd);
484 extern void spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx);
485 extern int vdev_obsolete_sm_object(vdev_t *vd);
486 extern boolean_t vdev_obsolete_counts_are_precise(vdev_t *vd);
489 * Other miscellaneous functions
491 int vdev_checkpoint_sm_object(vdev_t *vd);
494 * The vdev_buf_t is used to translate between zio_t and buf_t, and back again.
496 typedef struct vdev_buf {
497 buf_t vb_buf; /* buffer that describes the io */
498 zio_t *vb_io; /* pointer back to the original zio_t */
499 } vdev_buf_t;
501 #ifdef __cplusplus
503 #endif
505 #endif /* _SYS_VDEV_IMPL_H */