]> git.proxmox.com Git - mirror_zfs.git/blob - include/sys/vdev_impl.h
OpenZFS 7614, 9064 - zfs device evacuation/removal
[mirror_zfs.git] / include / sys / vdev_impl.h
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, 2015 by Delphix. All rights reserved.
24 */
25
26 #ifndef _SYS_VDEV_IMPL_H
27 #define _SYS_VDEV_IMPL_H
28
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>
41 #include <sys/zfs_ratelimit.h>
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /*
48 * Virtual device descriptors.
49 *
50 * All storage pool operations go through the virtual device framework,
51 * which provides data replication and I/O scheduling.
52 */
53
54 /*
55 * Forward declarations that lots of things need.
56 */
57 typedef struct vdev_queue vdev_queue_t;
58 typedef struct vdev_cache vdev_cache_t;
59 typedef struct vdev_cache_entry vdev_cache_entry_t;
60 struct abd;
61
62 extern int zfs_vdev_queue_depth_pct;
63 extern uint32_t zfs_vdev_async_write_max_active;
64
65 /*
66 * Virtual device operations
67 */
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 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);
78
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);
83
84 typedef const struct vdev_ops {
85 vdev_open_func_t *vdev_op_open;
86 vdev_close_func_t *vdev_op_close;
87 vdev_asize_func_t *vdev_op_asize;
88 vdev_io_start_func_t *vdev_op_io_start;
89 vdev_io_done_func_t *vdev_op_io_done;
90 vdev_state_change_func_t *vdev_op_state_change;
91 vdev_need_resilver_func_t *vdev_op_need_resilver;
92 vdev_hold_func_t *vdev_op_hold;
93 vdev_rele_func_t *vdev_op_rele;
94 vdev_remap_func_t *vdev_op_remap;
95 char vdev_op_type[16];
96 boolean_t vdev_op_leaf;
97 } vdev_ops_t;
98
99 /*
100 * Virtual device properties
101 */
102 struct vdev_cache_entry {
103 struct abd *ve_abd;
104 uint64_t ve_offset;
105 clock_t ve_lastused;
106 avl_node_t ve_offset_node;
107 avl_node_t ve_lastused_node;
108 uint32_t ve_hits;
109 uint16_t ve_missed_update;
110 zio_t *ve_fill_io;
111 };
112
113 struct vdev_cache {
114 avl_tree_t vc_offset_tree;
115 avl_tree_t vc_lastused_tree;
116 kmutex_t vc_lock;
117 };
118
119 typedef struct vdev_queue_class {
120 uint32_t vqc_active;
121
122 /*
123 * Sorted by offset or timestamp, depending on if the queue is
124 * LBA-ordered vs FIFO.
125 */
126 avl_tree_t vqc_queued_tree;
127 } vdev_queue_class_t;
128
129 struct vdev_queue {
130 vdev_t *vq_vdev;
131 vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
132 avl_tree_t vq_active_tree;
133 avl_tree_t vq_read_offset_tree;
134 avl_tree_t vq_write_offset_tree;
135 uint64_t vq_last_offset;
136 hrtime_t vq_io_complete_ts; /* time last i/o completed */
137 hrtime_t vq_io_delta_ts;
138 zio_t vq_io_search; /* used as local for stack reduction */
139 kmutex_t vq_lock;
140 };
141
142 /*
143 * On-disk indirect vdev state.
144 *
145 * An indirect vdev is described exclusively in the MOS config of a pool.
146 * The config for an indirect vdev includes several fields, which are
147 * accessed in memory by a vdev_indirect_config_t.
148 */
149 typedef struct vdev_indirect_config {
150 /*
151 * Object (in MOS) which contains the indirect mapping. This object
152 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
153 * vimep_src. The bonus buffer for this object is a
154 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
155 * removal is initiated.
156 *
157 * Note that this object can be empty if none of the data on the vdev
158 * has been copied yet.
159 */
160 uint64_t vic_mapping_object;
161
162 /*
163 * Object (in MOS) which contains the birth times for the mapping
164 * entries. This object contains an array of
165 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
166 * buffer for this object is a vdev_indirect_birth_phys_t. This object
167 * is allocated when a vdev removal is initiated.
168 *
169 * Note that this object can be empty if none of the vdev has yet been
170 * copied.
171 */
172 uint64_t vic_births_object;
173
174 /*
175 * This is the vdev ID which was removed previous to this vdev, or
176 * UINT64_MAX if there are no previously removed vdevs.
177 */
178 uint64_t vic_prev_indirect_vdev;
179 } vdev_indirect_config_t;
180
181 /*
182 * Virtual device descriptor
183 */
184 struct vdev {
185 /*
186 * Common to all vdev types.
187 */
188 uint64_t vdev_id; /* child number in vdev parent */
189 uint64_t vdev_guid; /* unique ID for this vdev */
190 uint64_t vdev_guid_sum; /* self guid + all child guids */
191 uint64_t vdev_orig_guid; /* orig. guid prior to remove */
192 uint64_t vdev_asize; /* allocatable device capacity */
193 uint64_t vdev_min_asize; /* min acceptable asize */
194 uint64_t vdev_max_asize; /* max acceptable asize */
195 uint64_t vdev_ashift; /* block alignment shift */
196 uint64_t vdev_state; /* see VDEV_STATE_* #defines */
197 uint64_t vdev_prevstate; /* used when reopening a vdev */
198 vdev_ops_t *vdev_ops; /* vdev operations */
199 spa_t *vdev_spa; /* spa for this vdev */
200 void *vdev_tsd; /* type-specific data */
201 vnode_t *vdev_name_vp; /* vnode for pathname */
202 vnode_t *vdev_devid_vp; /* vnode for devid */
203 vdev_t *vdev_top; /* top-level vdev */
204 vdev_t *vdev_parent; /* parent vdev */
205 vdev_t **vdev_child; /* array of children */
206 uint64_t vdev_children; /* number of children */
207 vdev_stat_t vdev_stat; /* virtual device statistics */
208 vdev_stat_ex_t vdev_stat_ex; /* extended statistics */
209 boolean_t vdev_expanding; /* expand the vdev? */
210 boolean_t vdev_reopening; /* reopen in progress? */
211 boolean_t vdev_nonrot; /* true if solid state */
212 int vdev_open_error; /* error on last open */
213 kthread_t *vdev_open_thread; /* thread opening children */
214 uint64_t vdev_crtxg; /* txg when top-level was added */
215
216 /*
217 * Top-level vdev state.
218 */
219 uint64_t vdev_ms_array; /* metaslab array object */
220 uint64_t vdev_ms_shift; /* metaslab size shift */
221 uint64_t vdev_ms_count; /* number of metaslabs */
222 metaslab_group_t *vdev_mg; /* metaslab group */
223 metaslab_t **vdev_ms; /* metaslab array */
224 uint64_t vdev_pending_fastwrite; /* allocated fastwrites */
225 txg_list_t vdev_ms_list; /* per-txg dirty metaslab lists */
226 txg_list_t vdev_dtl_list; /* per-txg dirty DTL lists */
227 txg_node_t vdev_txg_node; /* per-txg dirty vdev linkage */
228 boolean_t vdev_remove_wanted; /* async remove wanted? */
229 boolean_t vdev_probe_wanted; /* async probe wanted? */
230 list_node_t vdev_config_dirty_node; /* config dirty list */
231 list_node_t vdev_state_dirty_node; /* state dirty list */
232 uint64_t vdev_deflate_ratio; /* deflation ratio (x512) */
233 uint64_t vdev_islog; /* is an intent log device */
234 uint64_t vdev_removing; /* device is being removed? */
235 boolean_t vdev_ishole; /* is a hole in the namespace */
236 kmutex_t vdev_queue_lock; /* protects vdev_queue_depth */
237 uint64_t vdev_top_zap;
238
239 /*
240 * Values stored in the config for an indirect or removing vdev.
241 */
242 vdev_indirect_config_t vdev_indirect_config;
243
244 /*
245 * The vdev_indirect_rwlock protects the vdev_indirect_mapping
246 * pointer from changing on indirect vdevs (when it is condensed).
247 * Note that removing (not yet indirect) vdevs have different
248 * access patterns (the mapping is not accessed from open context,
249 * e.g. from zio_read) and locking strategy (e.g. svr_lock).
250 */
251 krwlock_t vdev_indirect_rwlock;
252 vdev_indirect_mapping_t *vdev_indirect_mapping;
253 vdev_indirect_births_t *vdev_indirect_births;
254
255 /*
256 * In memory data structures used to manage the obsolete sm, for
257 * indirect or removing vdevs.
258 *
259 * The vdev_obsolete_segments is the in-core record of the segments
260 * that are no longer referenced anywhere in the pool (due to
261 * being freed or remapped and not referenced by any snapshots).
262 * During a sync, segments are added to vdev_obsolete_segments
263 * via vdev_indirect_mark_obsolete(); at the end of each sync
264 * pass, this is appended to vdev_obsolete_sm via
265 * vdev_indirect_sync_obsolete(). The vdev_obsolete_lock
266 * protects against concurrent modifications of vdev_obsolete_segments
267 * from multiple zio threads.
268 */
269 kmutex_t vdev_obsolete_lock;
270 range_tree_t *vdev_obsolete_segments;
271 space_map_t *vdev_obsolete_sm;
272
273 /*
274 * The queue depth parameters determine how many async writes are
275 * still pending (i.e. allocated by net yet issued to disk) per
276 * top-level (vdev_async_write_queue_depth) and the maximum allowed
277 * (vdev_max_async_write_queue_depth). These values only apply to
278 * top-level vdevs.
279 */
280 uint64_t vdev_async_write_queue_depth;
281 uint64_t vdev_max_async_write_queue_depth;
282
283 /*
284 * Protects the vdev_scan_io_queue field itself as well as the
285 * structure's contents (when present).
286 */
287 kmutex_t vdev_scan_io_queue_lock;
288 struct dsl_scan_io_queue *vdev_scan_io_queue;
289
290 /*
291 * Leaf vdev state.
292 */
293 range_tree_t *vdev_dtl[DTL_TYPES]; /* dirty time logs */
294 space_map_t *vdev_dtl_sm; /* dirty time log space map */
295 txg_node_t vdev_dtl_node; /* per-txg dirty DTL linkage */
296 uint64_t vdev_dtl_object; /* DTL object */
297 uint64_t vdev_psize; /* physical device capacity */
298 uint64_t vdev_wholedisk; /* true if this is a whole disk */
299 uint64_t vdev_offline; /* persistent offline state */
300 uint64_t vdev_faulted; /* persistent faulted state */
301 uint64_t vdev_degraded; /* persistent degraded state */
302 uint64_t vdev_removed; /* persistent removed state */
303 uint64_t vdev_resilver_txg; /* persistent resilvering state */
304 uint64_t vdev_nparity; /* number of parity devices for raidz */
305 char *vdev_path; /* vdev path (if any) */
306 char *vdev_devid; /* vdev devid (if any) */
307 char *vdev_physpath; /* vdev device path (if any) */
308 char *vdev_enc_sysfs_path; /* enclosure sysfs path */
309 char *vdev_fru; /* physical FRU location */
310 uint64_t vdev_not_present; /* not present during import */
311 uint64_t vdev_unspare; /* unspare when resilvering done */
312 boolean_t vdev_nowritecache; /* true if flushwritecache failed */
313 boolean_t vdev_checkremove; /* temporary online test */
314 boolean_t vdev_forcefault; /* force online fault */
315 boolean_t vdev_splitting; /* split or repair in progress */
316 boolean_t vdev_delayed_close; /* delayed device close? */
317 boolean_t vdev_tmpoffline; /* device taken offline temporarily? */
318 boolean_t vdev_detached; /* device detached? */
319 boolean_t vdev_cant_read; /* vdev is failing all reads */
320 boolean_t vdev_cant_write; /* vdev is failing all writes */
321 boolean_t vdev_isspare; /* was a hot spare */
322 boolean_t vdev_isl2cache; /* was a l2cache device */
323 boolean_t vdev_copy_uberblocks; /* post expand copy uberblocks */
324 vdev_queue_t vdev_queue; /* I/O deadline schedule queue */
325 vdev_cache_t vdev_cache; /* physical block cache */
326 spa_aux_vdev_t *vdev_aux; /* for l2cache and spares vdevs */
327 zio_t *vdev_probe_zio; /* root of current probe */
328 vdev_aux_t vdev_label_aux; /* on-disk aux state */
329 uint64_t vdev_leaf_zap;
330 hrtime_t vdev_mmp_pending; /* 0 if write finished */
331 uint64_t vdev_mmp_kstat_id; /* to find kstat entry */
332
333 /*
334 * For DTrace to work in userland (libzpool) context, these fields must
335 * remain at the end of the structure. DTrace will use the kernel's
336 * CTF definition for 'struct vdev', and since the size of a kmutex_t is
337 * larger in userland, the offsets for the rest of the fields would be
338 * incorrect.
339 */
340 kmutex_t vdev_dtl_lock; /* vdev_dtl_{map,resilver} */
341 kmutex_t vdev_stat_lock; /* vdev_stat */
342 kmutex_t vdev_probe_lock; /* protects vdev_probe_zio */
343
344 /*
345 * We rate limit ZIO delay and ZIO checksum events, since they
346 * can flood ZED with tons of events when a drive is acting up.
347 */
348 zfs_ratelimit_t vdev_delay_rl;
349 zfs_ratelimit_t vdev_checksum_rl;
350 };
351
352 #define VDEV_RAIDZ_MAXPARITY 3
353
354 #define VDEV_PAD_SIZE (8 << 10)
355 /* 2 padding areas (vl_pad1 and vl_pad2) to skip */
356 #define VDEV_SKIP_SIZE VDEV_PAD_SIZE * 2
357 #define VDEV_PHYS_SIZE (112 << 10)
358 #define VDEV_UBERBLOCK_RING (128 << 10)
359
360 /*
361 * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
362 * ring when MMP is enabled.
363 */
364 #define MMP_BLOCKS_PER_LABEL 1
365
366 /* The largest uberblock we support is 8k. */
367 #define MAX_UBERBLOCK_SHIFT (13)
368 #define VDEV_UBERBLOCK_SHIFT(vd) \
369 MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
370 MAX_UBERBLOCK_SHIFT)
371 #define VDEV_UBERBLOCK_COUNT(vd) \
372 (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
373 #define VDEV_UBERBLOCK_OFFSET(vd, n) \
374 offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
375 #define VDEV_UBERBLOCK_SIZE(vd) (1ULL << VDEV_UBERBLOCK_SHIFT(vd))
376
377 typedef struct vdev_phys {
378 char vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
379 zio_eck_t vp_zbt;
380 } vdev_phys_t;
381
382 typedef struct vdev_label {
383 char vl_pad1[VDEV_PAD_SIZE]; /* 8K */
384 char vl_pad2[VDEV_PAD_SIZE]; /* 8K */
385 vdev_phys_t vl_vdev_phys; /* 112K */
386 char vl_uberblock[VDEV_UBERBLOCK_RING]; /* 128K */
387 } vdev_label_t; /* 256K total */
388
389 /*
390 * vdev_dirty() flags
391 */
392 #define VDD_METASLAB 0x01
393 #define VDD_DTL 0x02
394
395 /* Offset of embedded boot loader region on each label */
396 #define VDEV_BOOT_OFFSET (2 * sizeof (vdev_label_t))
397 /*
398 * Size of embedded boot loader region on each label.
399 * The total size of the first two labels plus the boot area is 4MB.
400 */
401 #define VDEV_BOOT_SIZE (7ULL << 19) /* 3.5M */
402
403 /*
404 * Size of label regions at the start and end of each leaf device.
405 */
406 #define VDEV_LABEL_START_SIZE (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
407 #define VDEV_LABEL_END_SIZE (2 * sizeof (vdev_label_t))
408 #define VDEV_LABELS 4
409 #define VDEV_BEST_LABEL VDEV_LABELS
410
411 #define VDEV_ALLOC_LOAD 0
412 #define VDEV_ALLOC_ADD 1
413 #define VDEV_ALLOC_SPARE 2
414 #define VDEV_ALLOC_L2CACHE 3
415 #define VDEV_ALLOC_ROOTPOOL 4
416 #define VDEV_ALLOC_SPLIT 5
417 #define VDEV_ALLOC_ATTACH 6
418
419 /*
420 * Allocate or free a vdev
421 */
422 extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
423 vdev_ops_t *ops);
424 extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
425 vdev_t *parent, uint_t id, int alloctype);
426 extern void vdev_free(vdev_t *vd);
427
428 /*
429 * Add or remove children and parents
430 */
431 extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
432 extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
433 extern void vdev_compact_children(vdev_t *pvd);
434 extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
435 extern void vdev_remove_parent(vdev_t *cvd);
436
437 /*
438 * vdev sync load and sync
439 */
440 extern void vdev_load_log_state(vdev_t *nvd, vdev_t *ovd);
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);
448
449 /*
450 * Available vdev types.
451 */
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;
462
463 /*
464 * Common size functions
465 */
466 extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
467 extern uint64_t vdev_get_min_asize(vdev_t *vd);
468 extern void vdev_set_min_asize(vdev_t *vd);
469
470 /*
471 * Global variables
472 */
473 /* zdb uses this tunable, so it must be declared here to make lint happy. */
474 extern int zfs_vdev_cache_size;
475
476 /*
477 * Functions from vdev_indirect.c
478 */
479 extern void vdev_indirect_sync_obsolete(vdev_t *vd, dmu_tx_t *tx);
480 extern boolean_t vdev_indirect_should_condense(vdev_t *vd);
481 extern void spa_condense_indirect_start_sync(vdev_t *vd, dmu_tx_t *tx);
482 extern int vdev_obsolete_sm_object(vdev_t *vd);
483 extern boolean_t vdev_obsolete_counts_are_precise(vdev_t *vd);
484
485 #ifdef __cplusplus
486 }
487 #endif
488
489 #endif /* _SYS_VDEV_IMPL_H */