xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev.c (revision 1f1e2261e341e6ca6862f82261066ef1705f0a7a)
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 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, 2021 by Delphix. All rights reserved.
25  * Copyright 2017 Nexenta Systems, Inc.
26  * Copyright (c) 2014 Integros [integros.com]
27  * Copyright 2016 Toomas Soome <tsoome@me.com>
28  * Copyright 2017 Joyent, Inc.
29  * Copyright (c) 2017, Intel Corporation.
30  * Copyright (c) 2019, Datto Inc. All rights reserved.
31  * Copyright (c) 2021, Klara Inc.
32  * Copyright [2021] Hewlett Packard Enterprise Development LP
33  */
34 
35 #include <sys/zfs_context.h>
36 #include <sys/fm/fs/zfs.h>
37 #include <sys/spa.h>
38 #include <sys/spa_impl.h>
39 #include <sys/bpobj.h>
40 #include <sys/dmu.h>
41 #include <sys/dmu_tx.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/vdev_rebuild.h>
45 #include <sys/vdev_draid.h>
46 #include <sys/uberblock_impl.h>
47 #include <sys/metaslab.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/space_map.h>
50 #include <sys/space_reftree.h>
51 #include <sys/zio.h>
52 #include <sys/zap.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/arc.h>
55 #include <sys/zil.h>
56 #include <sys/dsl_scan.h>
57 #include <sys/vdev_raidz.h>
58 #include <sys/abd.h>
59 #include <sys/vdev_initialize.h>
60 #include <sys/vdev_trim.h>
61 #include <sys/zvol.h>
62 #include <sys/zfs_ratelimit.h>
63 #include "zfs_prop.h"
64 
65 /*
66  * One metaslab from each (normal-class) vdev is used by the ZIL.  These are
67  * called "embedded slog metaslabs", are referenced by vdev_log_mg, and are
68  * part of the spa_embedded_log_class.  The metaslab with the most free space
69  * in each vdev is selected for this purpose when the pool is opened (or a
70  * vdev is added).  See vdev_metaslab_init().
71  *
72  * Log blocks can be allocated from the following locations.  Each one is tried
73  * in order until the allocation succeeds:
74  * 1. dedicated log vdevs, aka "slog" (spa_log_class)
75  * 2. embedded slog metaslabs (spa_embedded_log_class)
76  * 3. other metaslabs in normal vdevs (spa_normal_class)
77  *
78  * zfs_embedded_slog_min_ms disables the embedded slog if there are fewer
79  * than this number of metaslabs in the vdev.  This ensures that we don't set
80  * aside an unreasonable amount of space for the ZIL.  If set to less than
81  * 1 << (spa_slop_shift + 1), on small pools the usable space may be reduced
82  * (by more than 1<<spa_slop_shift) due to the embedded slog metaslab.
83  */
84 static int zfs_embedded_slog_min_ms = 64;
85 
86 /* default target for number of metaslabs per top-level vdev */
87 static int zfs_vdev_default_ms_count = 200;
88 
89 /* minimum number of metaslabs per top-level vdev */
90 static int zfs_vdev_min_ms_count = 16;
91 
92 /* practical upper limit of total metaslabs per top-level vdev */
93 static int zfs_vdev_ms_count_limit = 1ULL << 17;
94 
95 /* lower limit for metaslab size (512M) */
96 static int zfs_vdev_default_ms_shift = 29;
97 
98 /* upper limit for metaslab size (16G) */
99 static const int zfs_vdev_max_ms_shift = 34;
100 
101 int vdev_validate_skip = B_FALSE;
102 
103 /*
104  * Since the DTL space map of a vdev is not expected to have a lot of
105  * entries, we default its block size to 4K.
106  */
107 int zfs_vdev_dtl_sm_blksz = (1 << 12);
108 
109 /*
110  * Rate limit slow IO (delay) events to this many per second.
111  */
112 static unsigned int zfs_slow_io_events_per_second = 20;
113 
114 /*
115  * Rate limit checksum events after this many checksum errors per second.
116  */
117 static unsigned int zfs_checksum_events_per_second = 20;
118 
119 /*
120  * Ignore errors during scrub/resilver.  Allows to work around resilver
121  * upon import when there are pool errors.
122  */
123 static int zfs_scan_ignore_errors = 0;
124 
125 /*
126  * vdev-wide space maps that have lots of entries written to them at
127  * the end of each transaction can benefit from a higher I/O bandwidth
128  * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
129  */
130 int zfs_vdev_standard_sm_blksz = (1 << 17);
131 
132 /*
133  * Tunable parameter for debugging or performance analysis. Setting this
134  * will cause pool corruption on power loss if a volatile out-of-order
135  * write cache is enabled.
136  */
137 int zfs_nocacheflush = 0;
138 
139 uint64_t zfs_vdev_max_auto_ashift = ASHIFT_MAX;
140 uint64_t zfs_vdev_min_auto_ashift = ASHIFT_MIN;
141 
142 void
143 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
144 {
145 	va_list adx;
146 	char buf[256];
147 
148 	va_start(adx, fmt);
149 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
150 	va_end(adx);
151 
152 	if (vd->vdev_path != NULL) {
153 		zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
154 		    vd->vdev_path, buf);
155 	} else {
156 		zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
157 		    vd->vdev_ops->vdev_op_type,
158 		    (u_longlong_t)vd->vdev_id,
159 		    (u_longlong_t)vd->vdev_guid, buf);
160 	}
161 }
162 
163 void
164 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
165 {
166 	char state[20];
167 
168 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
169 		zfs_dbgmsg("%*svdev %llu: %s", indent, "",
170 		    (u_longlong_t)vd->vdev_id,
171 		    vd->vdev_ops->vdev_op_type);
172 		return;
173 	}
174 
175 	switch (vd->vdev_state) {
176 	case VDEV_STATE_UNKNOWN:
177 		(void) snprintf(state, sizeof (state), "unknown");
178 		break;
179 	case VDEV_STATE_CLOSED:
180 		(void) snprintf(state, sizeof (state), "closed");
181 		break;
182 	case VDEV_STATE_OFFLINE:
183 		(void) snprintf(state, sizeof (state), "offline");
184 		break;
185 	case VDEV_STATE_REMOVED:
186 		(void) snprintf(state, sizeof (state), "removed");
187 		break;
188 	case VDEV_STATE_CANT_OPEN:
189 		(void) snprintf(state, sizeof (state), "can't open");
190 		break;
191 	case VDEV_STATE_FAULTED:
192 		(void) snprintf(state, sizeof (state), "faulted");
193 		break;
194 	case VDEV_STATE_DEGRADED:
195 		(void) snprintf(state, sizeof (state), "degraded");
196 		break;
197 	case VDEV_STATE_HEALTHY:
198 		(void) snprintf(state, sizeof (state), "healthy");
199 		break;
200 	default:
201 		(void) snprintf(state, sizeof (state), "<state %u>",
202 		    (uint_t)vd->vdev_state);
203 	}
204 
205 	zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
206 	    "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
207 	    vd->vdev_islog ? " (log)" : "",
208 	    (u_longlong_t)vd->vdev_guid,
209 	    vd->vdev_path ? vd->vdev_path : "N/A", state);
210 
211 	for (uint64_t i = 0; i < vd->vdev_children; i++)
212 		vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
213 }
214 
215 /*
216  * Virtual device management.
217  */
218 
219 static const vdev_ops_t *const vdev_ops_table[] = {
220 	&vdev_root_ops,
221 	&vdev_raidz_ops,
222 	&vdev_draid_ops,
223 	&vdev_draid_spare_ops,
224 	&vdev_mirror_ops,
225 	&vdev_replacing_ops,
226 	&vdev_spare_ops,
227 	&vdev_disk_ops,
228 	&vdev_file_ops,
229 	&vdev_missing_ops,
230 	&vdev_hole_ops,
231 	&vdev_indirect_ops,
232 	NULL
233 };
234 
235 /*
236  * Given a vdev type, return the appropriate ops vector.
237  */
238 static vdev_ops_t *
239 vdev_getops(const char *type)
240 {
241 	const vdev_ops_t *ops, *const *opspp;
242 
243 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
244 		if (strcmp(ops->vdev_op_type, type) == 0)
245 			break;
246 
247 	return (ops);
248 }
249 
250 /*
251  * Given a vdev and a metaslab class, find which metaslab group we're
252  * interested in. All vdevs may belong to two different metaslab classes.
253  * Dedicated slog devices use only the primary metaslab group, rather than a
254  * separate log group. For embedded slogs, the vdev_log_mg will be non-NULL.
255  */
256 metaslab_group_t *
257 vdev_get_mg(vdev_t *vd, metaslab_class_t *mc)
258 {
259 	if (mc == spa_embedded_log_class(vd->vdev_spa) &&
260 	    vd->vdev_log_mg != NULL)
261 		return (vd->vdev_log_mg);
262 	else
263 		return (vd->vdev_mg);
264 }
265 
266 void
267 vdev_default_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
268     range_seg64_t *physical_rs, range_seg64_t *remain_rs)
269 {
270 	(void) vd, (void) remain_rs;
271 
272 	physical_rs->rs_start = logical_rs->rs_start;
273 	physical_rs->rs_end = logical_rs->rs_end;
274 }
275 
276 /*
277  * Derive the enumerated allocation bias from string input.
278  * String origin is either the per-vdev zap or zpool(8).
279  */
280 static vdev_alloc_bias_t
281 vdev_derive_alloc_bias(const char *bias)
282 {
283 	vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
284 
285 	if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0)
286 		alloc_bias = VDEV_BIAS_LOG;
287 	else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
288 		alloc_bias = VDEV_BIAS_SPECIAL;
289 	else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
290 		alloc_bias = VDEV_BIAS_DEDUP;
291 
292 	return (alloc_bias);
293 }
294 
295 /*
296  * Default asize function: return the MAX of psize with the asize of
297  * all children.  This is what's used by anything other than RAID-Z.
298  */
299 uint64_t
300 vdev_default_asize(vdev_t *vd, uint64_t psize)
301 {
302 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
303 	uint64_t csize;
304 
305 	for (int c = 0; c < vd->vdev_children; c++) {
306 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
307 		asize = MAX(asize, csize);
308 	}
309 
310 	return (asize);
311 }
312 
313 uint64_t
314 vdev_default_min_asize(vdev_t *vd)
315 {
316 	return (vd->vdev_min_asize);
317 }
318 
319 /*
320  * Get the minimum allocatable size. We define the allocatable size as
321  * the vdev's asize rounded to the nearest metaslab. This allows us to
322  * replace or attach devices which don't have the same physical size but
323  * can still satisfy the same number of allocations.
324  */
325 uint64_t
326 vdev_get_min_asize(vdev_t *vd)
327 {
328 	vdev_t *pvd = vd->vdev_parent;
329 
330 	/*
331 	 * If our parent is NULL (inactive spare or cache) or is the root,
332 	 * just return our own asize.
333 	 */
334 	if (pvd == NULL)
335 		return (vd->vdev_asize);
336 
337 	/*
338 	 * The top-level vdev just returns the allocatable size rounded
339 	 * to the nearest metaslab.
340 	 */
341 	if (vd == vd->vdev_top)
342 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
343 
344 	return (pvd->vdev_ops->vdev_op_min_asize(pvd));
345 }
346 
347 void
348 vdev_set_min_asize(vdev_t *vd)
349 {
350 	vd->vdev_min_asize = vdev_get_min_asize(vd);
351 
352 	for (int c = 0; c < vd->vdev_children; c++)
353 		vdev_set_min_asize(vd->vdev_child[c]);
354 }
355 
356 /*
357  * Get the minimal allocation size for the top-level vdev.
358  */
359 uint64_t
360 vdev_get_min_alloc(vdev_t *vd)
361 {
362 	uint64_t min_alloc = 1ULL << vd->vdev_ashift;
363 
364 	if (vd->vdev_ops->vdev_op_min_alloc != NULL)
365 		min_alloc = vd->vdev_ops->vdev_op_min_alloc(vd);
366 
367 	return (min_alloc);
368 }
369 
370 /*
371  * Get the parity level for a top-level vdev.
372  */
373 uint64_t
374 vdev_get_nparity(vdev_t *vd)
375 {
376 	uint64_t nparity = 0;
377 
378 	if (vd->vdev_ops->vdev_op_nparity != NULL)
379 		nparity = vd->vdev_ops->vdev_op_nparity(vd);
380 
381 	return (nparity);
382 }
383 
384 /*
385  * Get the number of data disks for a top-level vdev.
386  */
387 uint64_t
388 vdev_get_ndisks(vdev_t *vd)
389 {
390 	uint64_t ndisks = 1;
391 
392 	if (vd->vdev_ops->vdev_op_ndisks != NULL)
393 		ndisks = vd->vdev_ops->vdev_op_ndisks(vd);
394 
395 	return (ndisks);
396 }
397 
398 vdev_t *
399 vdev_lookup_top(spa_t *spa, uint64_t vdev)
400 {
401 	vdev_t *rvd = spa->spa_root_vdev;
402 
403 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
404 
405 	if (vdev < rvd->vdev_children) {
406 		ASSERT(rvd->vdev_child[vdev] != NULL);
407 		return (rvd->vdev_child[vdev]);
408 	}
409 
410 	return (NULL);
411 }
412 
413 vdev_t *
414 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
415 {
416 	vdev_t *mvd;
417 
418 	if (vd->vdev_guid == guid)
419 		return (vd);
420 
421 	for (int c = 0; c < vd->vdev_children; c++)
422 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
423 		    NULL)
424 			return (mvd);
425 
426 	return (NULL);
427 }
428 
429 static int
430 vdev_count_leaves_impl(vdev_t *vd)
431 {
432 	int n = 0;
433 
434 	if (vd->vdev_ops->vdev_op_leaf)
435 		return (1);
436 
437 	for (int c = 0; c < vd->vdev_children; c++)
438 		n += vdev_count_leaves_impl(vd->vdev_child[c]);
439 
440 	return (n);
441 }
442 
443 int
444 vdev_count_leaves(spa_t *spa)
445 {
446 	int rc;
447 
448 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
449 	rc = vdev_count_leaves_impl(spa->spa_root_vdev);
450 	spa_config_exit(spa, SCL_VDEV, FTAG);
451 
452 	return (rc);
453 }
454 
455 void
456 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
457 {
458 	size_t oldsize, newsize;
459 	uint64_t id = cvd->vdev_id;
460 	vdev_t **newchild;
461 
462 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
463 	ASSERT(cvd->vdev_parent == NULL);
464 
465 	cvd->vdev_parent = pvd;
466 
467 	if (pvd == NULL)
468 		return;
469 
470 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
471 
472 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
473 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
474 	newsize = pvd->vdev_children * sizeof (vdev_t *);
475 
476 	newchild = kmem_alloc(newsize, KM_SLEEP);
477 	if (pvd->vdev_child != NULL) {
478 		memcpy(newchild, pvd->vdev_child, oldsize);
479 		kmem_free(pvd->vdev_child, oldsize);
480 	}
481 
482 	pvd->vdev_child = newchild;
483 	pvd->vdev_child[id] = cvd;
484 
485 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
486 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
487 
488 	/*
489 	 * Walk up all ancestors to update guid sum.
490 	 */
491 	for (; pvd != NULL; pvd = pvd->vdev_parent)
492 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
493 
494 	if (cvd->vdev_ops->vdev_op_leaf) {
495 		list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
496 		cvd->vdev_spa->spa_leaf_list_gen++;
497 	}
498 }
499 
500 void
501 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
502 {
503 	int c;
504 	uint_t id = cvd->vdev_id;
505 
506 	ASSERT(cvd->vdev_parent == pvd);
507 
508 	if (pvd == NULL)
509 		return;
510 
511 	ASSERT(id < pvd->vdev_children);
512 	ASSERT(pvd->vdev_child[id] == cvd);
513 
514 	pvd->vdev_child[id] = NULL;
515 	cvd->vdev_parent = NULL;
516 
517 	for (c = 0; c < pvd->vdev_children; c++)
518 		if (pvd->vdev_child[c])
519 			break;
520 
521 	if (c == pvd->vdev_children) {
522 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
523 		pvd->vdev_child = NULL;
524 		pvd->vdev_children = 0;
525 	}
526 
527 	if (cvd->vdev_ops->vdev_op_leaf) {
528 		spa_t *spa = cvd->vdev_spa;
529 		list_remove(&spa->spa_leaf_list, cvd);
530 		spa->spa_leaf_list_gen++;
531 	}
532 
533 	/*
534 	 * Walk up all ancestors to update guid sum.
535 	 */
536 	for (; pvd != NULL; pvd = pvd->vdev_parent)
537 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
538 }
539 
540 /*
541  * Remove any holes in the child array.
542  */
543 void
544 vdev_compact_children(vdev_t *pvd)
545 {
546 	vdev_t **newchild, *cvd;
547 	int oldc = pvd->vdev_children;
548 	int newc;
549 
550 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
551 
552 	if (oldc == 0)
553 		return;
554 
555 	for (int c = newc = 0; c < oldc; c++)
556 		if (pvd->vdev_child[c])
557 			newc++;
558 
559 	if (newc > 0) {
560 		newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
561 
562 		for (int c = newc = 0; c < oldc; c++) {
563 			if ((cvd = pvd->vdev_child[c]) != NULL) {
564 				newchild[newc] = cvd;
565 				cvd->vdev_id = newc++;
566 			}
567 		}
568 	} else {
569 		newchild = NULL;
570 	}
571 
572 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
573 	pvd->vdev_child = newchild;
574 	pvd->vdev_children = newc;
575 }
576 
577 /*
578  * Allocate and minimally initialize a vdev_t.
579  */
580 vdev_t *
581 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
582 {
583 	vdev_t *vd;
584 	vdev_indirect_config_t *vic;
585 
586 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
587 	vic = &vd->vdev_indirect_config;
588 
589 	if (spa->spa_root_vdev == NULL) {
590 		ASSERT(ops == &vdev_root_ops);
591 		spa->spa_root_vdev = vd;
592 		spa->spa_load_guid = spa_generate_guid(NULL);
593 	}
594 
595 	if (guid == 0 && ops != &vdev_hole_ops) {
596 		if (spa->spa_root_vdev == vd) {
597 			/*
598 			 * The root vdev's guid will also be the pool guid,
599 			 * which must be unique among all pools.
600 			 */
601 			guid = spa_generate_guid(NULL);
602 		} else {
603 			/*
604 			 * Any other vdev's guid must be unique within the pool.
605 			 */
606 			guid = spa_generate_guid(spa);
607 		}
608 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
609 	}
610 
611 	vd->vdev_spa = spa;
612 	vd->vdev_id = id;
613 	vd->vdev_guid = guid;
614 	vd->vdev_guid_sum = guid;
615 	vd->vdev_ops = ops;
616 	vd->vdev_state = VDEV_STATE_CLOSED;
617 	vd->vdev_ishole = (ops == &vdev_hole_ops);
618 	vic->vic_prev_indirect_vdev = UINT64_MAX;
619 
620 	rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
621 	mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
622 	vd->vdev_obsolete_segments = range_tree_create(NULL, RANGE_SEG64, NULL,
623 	    0, 0);
624 
625 	/*
626 	 * Initialize rate limit structs for events.  We rate limit ZIO delay
627 	 * and checksum events so that we don't overwhelm ZED with thousands
628 	 * of events when a disk is acting up.
629 	 */
630 	zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second,
631 	    1);
632 	zfs_ratelimit_init(&vd->vdev_deadman_rl, &zfs_slow_io_events_per_second,
633 	    1);
634 	zfs_ratelimit_init(&vd->vdev_checksum_rl,
635 	    &zfs_checksum_events_per_second, 1);
636 
637 	list_link_init(&vd->vdev_config_dirty_node);
638 	list_link_init(&vd->vdev_state_dirty_node);
639 	list_link_init(&vd->vdev_initialize_node);
640 	list_link_init(&vd->vdev_leaf_node);
641 	list_link_init(&vd->vdev_trim_node);
642 
643 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL);
644 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
645 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
646 	mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL);
647 
648 	mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
649 	mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
650 	cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
651 	cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
652 
653 	mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL);
654 	mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL);
655 	mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL);
656 	cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL);
657 	cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL);
658 	cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL);
659 
660 	mutex_init(&vd->vdev_rebuild_lock, NULL, MUTEX_DEFAULT, NULL);
661 	cv_init(&vd->vdev_rebuild_cv, NULL, CV_DEFAULT, NULL);
662 
663 	for (int t = 0; t < DTL_TYPES; t++) {
664 		vd->vdev_dtl[t] = range_tree_create(NULL, RANGE_SEG64, NULL, 0,
665 		    0);
666 	}
667 
668 	txg_list_create(&vd->vdev_ms_list, spa,
669 	    offsetof(struct metaslab, ms_txg_node));
670 	txg_list_create(&vd->vdev_dtl_list, spa,
671 	    offsetof(struct vdev, vdev_dtl_node));
672 	vd->vdev_stat.vs_timestamp = gethrtime();
673 	vdev_queue_init(vd);
674 	vdev_cache_init(vd);
675 
676 	return (vd);
677 }
678 
679 /*
680  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
681  * creating a new vdev or loading an existing one - the behavior is slightly
682  * different for each case.
683  */
684 int
685 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
686     int alloctype)
687 {
688 	vdev_ops_t *ops;
689 	char *type;
690 	uint64_t guid = 0, islog;
691 	vdev_t *vd;
692 	vdev_indirect_config_t *vic;
693 	char *tmp = NULL;
694 	int rc;
695 	vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
696 	boolean_t top_level = (parent && !parent->vdev_parent);
697 
698 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
699 
700 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
701 		return (SET_ERROR(EINVAL));
702 
703 	if ((ops = vdev_getops(type)) == NULL)
704 		return (SET_ERROR(EINVAL));
705 
706 	/*
707 	 * If this is a load, get the vdev guid from the nvlist.
708 	 * Otherwise, vdev_alloc_common() will generate one for us.
709 	 */
710 	if (alloctype == VDEV_ALLOC_LOAD) {
711 		uint64_t label_id;
712 
713 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
714 		    label_id != id)
715 			return (SET_ERROR(EINVAL));
716 
717 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
718 			return (SET_ERROR(EINVAL));
719 	} else if (alloctype == VDEV_ALLOC_SPARE) {
720 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
721 			return (SET_ERROR(EINVAL));
722 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
723 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
724 			return (SET_ERROR(EINVAL));
725 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
726 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
727 			return (SET_ERROR(EINVAL));
728 	}
729 
730 	/*
731 	 * The first allocated vdev must be of type 'root'.
732 	 */
733 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
734 		return (SET_ERROR(EINVAL));
735 
736 	/*
737 	 * Determine whether we're a log vdev.
738 	 */
739 	islog = 0;
740 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
741 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
742 		return (SET_ERROR(ENOTSUP));
743 
744 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
745 		return (SET_ERROR(ENOTSUP));
746 
747 	if (top_level && alloctype == VDEV_ALLOC_ADD) {
748 		char *bias;
749 
750 		/*
751 		 * If creating a top-level vdev, check for allocation
752 		 * classes input.
753 		 */
754 		if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
755 		    &bias) == 0) {
756 			alloc_bias = vdev_derive_alloc_bias(bias);
757 
758 			/* spa_vdev_add() expects feature to be enabled */
759 			if (spa->spa_load_state != SPA_LOAD_CREATE &&
760 			    !spa_feature_is_enabled(spa,
761 			    SPA_FEATURE_ALLOCATION_CLASSES)) {
762 				return (SET_ERROR(ENOTSUP));
763 			}
764 		}
765 
766 		/* spa_vdev_add() expects feature to be enabled */
767 		if (ops == &vdev_draid_ops &&
768 		    spa->spa_load_state != SPA_LOAD_CREATE &&
769 		    !spa_feature_is_enabled(spa, SPA_FEATURE_DRAID)) {
770 			return (SET_ERROR(ENOTSUP));
771 		}
772 	}
773 
774 	/*
775 	 * Initialize the vdev specific data.  This is done before calling
776 	 * vdev_alloc_common() since it may fail and this simplifies the
777 	 * error reporting and cleanup code paths.
778 	 */
779 	void *tsd = NULL;
780 	if (ops->vdev_op_init != NULL) {
781 		rc = ops->vdev_op_init(spa, nv, &tsd);
782 		if (rc != 0) {
783 			return (rc);
784 		}
785 	}
786 
787 	vd = vdev_alloc_common(spa, id, guid, ops);
788 	vd->vdev_tsd = tsd;
789 	vd->vdev_islog = islog;
790 
791 	if (top_level && alloc_bias != VDEV_BIAS_NONE)
792 		vd->vdev_alloc_bias = alloc_bias;
793 
794 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
795 		vd->vdev_path = spa_strdup(vd->vdev_path);
796 
797 	/*
798 	 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
799 	 * fault on a vdev and want it to persist across imports (like with
800 	 * zpool offline -f).
801 	 */
802 	rc = nvlist_lookup_string(nv, ZPOOL_CONFIG_AUX_STATE, &tmp);
803 	if (rc == 0 && tmp != NULL && strcmp(tmp, "external") == 0) {
804 		vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
805 		vd->vdev_faulted = 1;
806 		vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
807 	}
808 
809 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
810 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
811 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
812 	    &vd->vdev_physpath) == 0)
813 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
814 
815 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
816 	    &vd->vdev_enc_sysfs_path) == 0)
817 		vd->vdev_enc_sysfs_path = spa_strdup(vd->vdev_enc_sysfs_path);
818 
819 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
820 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
821 
822 	/*
823 	 * Set the whole_disk property.  If it's not specified, leave the value
824 	 * as -1.
825 	 */
826 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
827 	    &vd->vdev_wholedisk) != 0)
828 		vd->vdev_wholedisk = -1ULL;
829 
830 	vic = &vd->vdev_indirect_config;
831 
832 	ASSERT0(vic->vic_mapping_object);
833 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
834 	    &vic->vic_mapping_object);
835 	ASSERT0(vic->vic_births_object);
836 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
837 	    &vic->vic_births_object);
838 	ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
839 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
840 	    &vic->vic_prev_indirect_vdev);
841 
842 	/*
843 	 * Look for the 'not present' flag.  This will only be set if the device
844 	 * was not present at the time of import.
845 	 */
846 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
847 	    &vd->vdev_not_present);
848 
849 	/*
850 	 * Get the alignment requirement.
851 	 */
852 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
853 
854 	/*
855 	 * Retrieve the vdev creation time.
856 	 */
857 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
858 	    &vd->vdev_crtxg);
859 
860 	/*
861 	 * If we're a top-level vdev, try to load the allocation parameters.
862 	 */
863 	if (top_level &&
864 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
865 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
866 		    &vd->vdev_ms_array);
867 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
868 		    &vd->vdev_ms_shift);
869 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
870 		    &vd->vdev_asize);
871 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NONALLOCATING,
872 		    &vd->vdev_noalloc);
873 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
874 		    &vd->vdev_removing);
875 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
876 		    &vd->vdev_top_zap);
877 	} else {
878 		ASSERT0(vd->vdev_top_zap);
879 	}
880 
881 	if (top_level && alloctype != VDEV_ALLOC_ATTACH) {
882 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
883 		    alloctype == VDEV_ALLOC_ADD ||
884 		    alloctype == VDEV_ALLOC_SPLIT ||
885 		    alloctype == VDEV_ALLOC_ROOTPOOL);
886 		/* Note: metaslab_group_create() is now deferred */
887 	}
888 
889 	if (vd->vdev_ops->vdev_op_leaf &&
890 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
891 		(void) nvlist_lookup_uint64(nv,
892 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
893 	} else {
894 		ASSERT0(vd->vdev_leaf_zap);
895 	}
896 
897 	/*
898 	 * If we're a leaf vdev, try to load the DTL object and other state.
899 	 */
900 
901 	if (vd->vdev_ops->vdev_op_leaf &&
902 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
903 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
904 		if (alloctype == VDEV_ALLOC_LOAD) {
905 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
906 			    &vd->vdev_dtl_object);
907 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
908 			    &vd->vdev_unspare);
909 		}
910 
911 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
912 			uint64_t spare = 0;
913 
914 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
915 			    &spare) == 0 && spare)
916 				spa_spare_add(vd);
917 		}
918 
919 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
920 		    &vd->vdev_offline);
921 
922 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
923 		    &vd->vdev_resilver_txg);
924 
925 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG,
926 		    &vd->vdev_rebuild_txg);
927 
928 		if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER))
929 			vdev_defer_resilver(vd);
930 
931 		/*
932 		 * In general, when importing a pool we want to ignore the
933 		 * persistent fault state, as the diagnosis made on another
934 		 * system may not be valid in the current context.  The only
935 		 * exception is if we forced a vdev to a persistently faulted
936 		 * state with 'zpool offline -f'.  The persistent fault will
937 		 * remain across imports until cleared.
938 		 *
939 		 * Local vdevs will remain in the faulted state.
940 		 */
941 		if (spa_load_state(spa) == SPA_LOAD_OPEN ||
942 		    spa_load_state(spa) == SPA_LOAD_IMPORT) {
943 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
944 			    &vd->vdev_faulted);
945 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
946 			    &vd->vdev_degraded);
947 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
948 			    &vd->vdev_removed);
949 
950 			if (vd->vdev_faulted || vd->vdev_degraded) {
951 				char *aux;
952 
953 				vd->vdev_label_aux =
954 				    VDEV_AUX_ERR_EXCEEDED;
955 				if (nvlist_lookup_string(nv,
956 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
957 				    strcmp(aux, "external") == 0)
958 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
959 				else
960 					vd->vdev_faulted = 0ULL;
961 			}
962 		}
963 	}
964 
965 	/*
966 	 * Add ourselves to the parent's list of children.
967 	 */
968 	vdev_add_child(parent, vd);
969 
970 	*vdp = vd;
971 
972 	return (0);
973 }
974 
975 void
976 vdev_free(vdev_t *vd)
977 {
978 	spa_t *spa = vd->vdev_spa;
979 
980 	ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
981 	ASSERT3P(vd->vdev_trim_thread, ==, NULL);
982 	ASSERT3P(vd->vdev_autotrim_thread, ==, NULL);
983 	ASSERT3P(vd->vdev_rebuild_thread, ==, NULL);
984 
985 	/*
986 	 * Scan queues are normally destroyed at the end of a scan. If the
987 	 * queue exists here, that implies the vdev is being removed while
988 	 * the scan is still running.
989 	 */
990 	if (vd->vdev_scan_io_queue != NULL) {
991 		mutex_enter(&vd->vdev_scan_io_queue_lock);
992 		dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue);
993 		vd->vdev_scan_io_queue = NULL;
994 		mutex_exit(&vd->vdev_scan_io_queue_lock);
995 	}
996 
997 	/*
998 	 * vdev_free() implies closing the vdev first.  This is simpler than
999 	 * trying to ensure complicated semantics for all callers.
1000 	 */
1001 	vdev_close(vd);
1002 
1003 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
1004 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
1005 
1006 	/*
1007 	 * Free all children.
1008 	 */
1009 	for (int c = 0; c < vd->vdev_children; c++)
1010 		vdev_free(vd->vdev_child[c]);
1011 
1012 	ASSERT(vd->vdev_child == NULL);
1013 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
1014 
1015 	if (vd->vdev_ops->vdev_op_fini != NULL)
1016 		vd->vdev_ops->vdev_op_fini(vd);
1017 
1018 	/*
1019 	 * Discard allocation state.
1020 	 */
1021 	if (vd->vdev_mg != NULL) {
1022 		vdev_metaslab_fini(vd);
1023 		metaslab_group_destroy(vd->vdev_mg);
1024 		vd->vdev_mg = NULL;
1025 	}
1026 	if (vd->vdev_log_mg != NULL) {
1027 		ASSERT0(vd->vdev_ms_count);
1028 		metaslab_group_destroy(vd->vdev_log_mg);
1029 		vd->vdev_log_mg = NULL;
1030 	}
1031 
1032 	ASSERT0(vd->vdev_stat.vs_space);
1033 	ASSERT0(vd->vdev_stat.vs_dspace);
1034 	ASSERT0(vd->vdev_stat.vs_alloc);
1035 
1036 	/*
1037 	 * Remove this vdev from its parent's child list.
1038 	 */
1039 	vdev_remove_child(vd->vdev_parent, vd);
1040 
1041 	ASSERT(vd->vdev_parent == NULL);
1042 	ASSERT(!list_link_active(&vd->vdev_leaf_node));
1043 
1044 	/*
1045 	 * Clean up vdev structure.
1046 	 */
1047 	vdev_queue_fini(vd);
1048 	vdev_cache_fini(vd);
1049 
1050 	if (vd->vdev_path)
1051 		spa_strfree(vd->vdev_path);
1052 	if (vd->vdev_devid)
1053 		spa_strfree(vd->vdev_devid);
1054 	if (vd->vdev_physpath)
1055 		spa_strfree(vd->vdev_physpath);
1056 
1057 	if (vd->vdev_enc_sysfs_path)
1058 		spa_strfree(vd->vdev_enc_sysfs_path);
1059 
1060 	if (vd->vdev_fru)
1061 		spa_strfree(vd->vdev_fru);
1062 
1063 	if (vd->vdev_isspare)
1064 		spa_spare_remove(vd);
1065 	if (vd->vdev_isl2cache)
1066 		spa_l2cache_remove(vd);
1067 
1068 	txg_list_destroy(&vd->vdev_ms_list);
1069 	txg_list_destroy(&vd->vdev_dtl_list);
1070 
1071 	mutex_enter(&vd->vdev_dtl_lock);
1072 	space_map_close(vd->vdev_dtl_sm);
1073 	for (int t = 0; t < DTL_TYPES; t++) {
1074 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
1075 		range_tree_destroy(vd->vdev_dtl[t]);
1076 	}
1077 	mutex_exit(&vd->vdev_dtl_lock);
1078 
1079 	EQUIV(vd->vdev_indirect_births != NULL,
1080 	    vd->vdev_indirect_mapping != NULL);
1081 	if (vd->vdev_indirect_births != NULL) {
1082 		vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
1083 		vdev_indirect_births_close(vd->vdev_indirect_births);
1084 	}
1085 
1086 	if (vd->vdev_obsolete_sm != NULL) {
1087 		ASSERT(vd->vdev_removing ||
1088 		    vd->vdev_ops == &vdev_indirect_ops);
1089 		space_map_close(vd->vdev_obsolete_sm);
1090 		vd->vdev_obsolete_sm = NULL;
1091 	}
1092 	range_tree_destroy(vd->vdev_obsolete_segments);
1093 	rw_destroy(&vd->vdev_indirect_rwlock);
1094 	mutex_destroy(&vd->vdev_obsolete_lock);
1095 
1096 	mutex_destroy(&vd->vdev_dtl_lock);
1097 	mutex_destroy(&vd->vdev_stat_lock);
1098 	mutex_destroy(&vd->vdev_probe_lock);
1099 	mutex_destroy(&vd->vdev_scan_io_queue_lock);
1100 
1101 	mutex_destroy(&vd->vdev_initialize_lock);
1102 	mutex_destroy(&vd->vdev_initialize_io_lock);
1103 	cv_destroy(&vd->vdev_initialize_io_cv);
1104 	cv_destroy(&vd->vdev_initialize_cv);
1105 
1106 	mutex_destroy(&vd->vdev_trim_lock);
1107 	mutex_destroy(&vd->vdev_autotrim_lock);
1108 	mutex_destroy(&vd->vdev_trim_io_lock);
1109 	cv_destroy(&vd->vdev_trim_cv);
1110 	cv_destroy(&vd->vdev_autotrim_cv);
1111 	cv_destroy(&vd->vdev_trim_io_cv);
1112 
1113 	mutex_destroy(&vd->vdev_rebuild_lock);
1114 	cv_destroy(&vd->vdev_rebuild_cv);
1115 
1116 	zfs_ratelimit_fini(&vd->vdev_delay_rl);
1117 	zfs_ratelimit_fini(&vd->vdev_deadman_rl);
1118 	zfs_ratelimit_fini(&vd->vdev_checksum_rl);
1119 
1120 	if (vd == spa->spa_root_vdev)
1121 		spa->spa_root_vdev = NULL;
1122 
1123 	kmem_free(vd, sizeof (vdev_t));
1124 }
1125 
1126 /*
1127  * Transfer top-level vdev state from svd to tvd.
1128  */
1129 static void
1130 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
1131 {
1132 	spa_t *spa = svd->vdev_spa;
1133 	metaslab_t *msp;
1134 	vdev_t *vd;
1135 	int t;
1136 
1137 	ASSERT(tvd == tvd->vdev_top);
1138 
1139 	tvd->vdev_pending_fastwrite = svd->vdev_pending_fastwrite;
1140 	tvd->vdev_ms_array = svd->vdev_ms_array;
1141 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
1142 	tvd->vdev_ms_count = svd->vdev_ms_count;
1143 	tvd->vdev_top_zap = svd->vdev_top_zap;
1144 
1145 	svd->vdev_ms_array = 0;
1146 	svd->vdev_ms_shift = 0;
1147 	svd->vdev_ms_count = 0;
1148 	svd->vdev_top_zap = 0;
1149 
1150 	if (tvd->vdev_mg)
1151 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
1152 	if (tvd->vdev_log_mg)
1153 		ASSERT3P(tvd->vdev_log_mg, ==, svd->vdev_log_mg);
1154 	tvd->vdev_mg = svd->vdev_mg;
1155 	tvd->vdev_log_mg = svd->vdev_log_mg;
1156 	tvd->vdev_ms = svd->vdev_ms;
1157 
1158 	svd->vdev_mg = NULL;
1159 	svd->vdev_log_mg = NULL;
1160 	svd->vdev_ms = NULL;
1161 
1162 	if (tvd->vdev_mg != NULL)
1163 		tvd->vdev_mg->mg_vd = tvd;
1164 	if (tvd->vdev_log_mg != NULL)
1165 		tvd->vdev_log_mg->mg_vd = tvd;
1166 
1167 	tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
1168 	svd->vdev_checkpoint_sm = NULL;
1169 
1170 	tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
1171 	svd->vdev_alloc_bias = VDEV_BIAS_NONE;
1172 
1173 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
1174 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
1175 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
1176 
1177 	svd->vdev_stat.vs_alloc = 0;
1178 	svd->vdev_stat.vs_space = 0;
1179 	svd->vdev_stat.vs_dspace = 0;
1180 
1181 	/*
1182 	 * State which may be set on a top-level vdev that's in the
1183 	 * process of being removed.
1184 	 */
1185 	ASSERT0(tvd->vdev_indirect_config.vic_births_object);
1186 	ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
1187 	ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
1188 	ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
1189 	ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
1190 	ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
1191 	ASSERT0(tvd->vdev_noalloc);
1192 	ASSERT0(tvd->vdev_removing);
1193 	ASSERT0(tvd->vdev_rebuilding);
1194 	tvd->vdev_noalloc = svd->vdev_noalloc;
1195 	tvd->vdev_removing = svd->vdev_removing;
1196 	tvd->vdev_rebuilding = svd->vdev_rebuilding;
1197 	tvd->vdev_rebuild_config = svd->vdev_rebuild_config;
1198 	tvd->vdev_indirect_config = svd->vdev_indirect_config;
1199 	tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
1200 	tvd->vdev_indirect_births = svd->vdev_indirect_births;
1201 	range_tree_swap(&svd->vdev_obsolete_segments,
1202 	    &tvd->vdev_obsolete_segments);
1203 	tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
1204 	svd->vdev_indirect_config.vic_mapping_object = 0;
1205 	svd->vdev_indirect_config.vic_births_object = 0;
1206 	svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
1207 	svd->vdev_indirect_mapping = NULL;
1208 	svd->vdev_indirect_births = NULL;
1209 	svd->vdev_obsolete_sm = NULL;
1210 	svd->vdev_noalloc = 0;
1211 	svd->vdev_removing = 0;
1212 	svd->vdev_rebuilding = 0;
1213 
1214 	for (t = 0; t < TXG_SIZE; t++) {
1215 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
1216 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
1217 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
1218 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
1219 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
1220 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
1221 	}
1222 
1223 	if (list_link_active(&svd->vdev_config_dirty_node)) {
1224 		vdev_config_clean(svd);
1225 		vdev_config_dirty(tvd);
1226 	}
1227 
1228 	if (list_link_active(&svd->vdev_state_dirty_node)) {
1229 		vdev_state_clean(svd);
1230 		vdev_state_dirty(tvd);
1231 	}
1232 
1233 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
1234 	svd->vdev_deflate_ratio = 0;
1235 
1236 	tvd->vdev_islog = svd->vdev_islog;
1237 	svd->vdev_islog = 0;
1238 
1239 	dsl_scan_io_queue_vdev_xfer(svd, tvd);
1240 }
1241 
1242 static void
1243 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1244 {
1245 	if (vd == NULL)
1246 		return;
1247 
1248 	vd->vdev_top = tvd;
1249 
1250 	for (int c = 0; c < vd->vdev_children; c++)
1251 		vdev_top_update(tvd, vd->vdev_child[c]);
1252 }
1253 
1254 /*
1255  * Add a mirror/replacing vdev above an existing vdev.  There is no need to
1256  * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1257  */
1258 vdev_t *
1259 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1260 {
1261 	spa_t *spa = cvd->vdev_spa;
1262 	vdev_t *pvd = cvd->vdev_parent;
1263 	vdev_t *mvd;
1264 
1265 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1266 
1267 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1268 
1269 	mvd->vdev_asize = cvd->vdev_asize;
1270 	mvd->vdev_min_asize = cvd->vdev_min_asize;
1271 	mvd->vdev_max_asize = cvd->vdev_max_asize;
1272 	mvd->vdev_psize = cvd->vdev_psize;
1273 	mvd->vdev_ashift = cvd->vdev_ashift;
1274 	mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
1275 	mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
1276 	mvd->vdev_state = cvd->vdev_state;
1277 	mvd->vdev_crtxg = cvd->vdev_crtxg;
1278 
1279 	vdev_remove_child(pvd, cvd);
1280 	vdev_add_child(pvd, mvd);
1281 	cvd->vdev_id = mvd->vdev_children;
1282 	vdev_add_child(mvd, cvd);
1283 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1284 
1285 	if (mvd == mvd->vdev_top)
1286 		vdev_top_transfer(cvd, mvd);
1287 
1288 	return (mvd);
1289 }
1290 
1291 /*
1292  * Remove a 1-way mirror/replacing vdev from the tree.
1293  */
1294 void
1295 vdev_remove_parent(vdev_t *cvd)
1296 {
1297 	vdev_t *mvd = cvd->vdev_parent;
1298 	vdev_t *pvd = mvd->vdev_parent;
1299 
1300 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1301 
1302 	ASSERT(mvd->vdev_children == 1);
1303 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1304 	    mvd->vdev_ops == &vdev_replacing_ops ||
1305 	    mvd->vdev_ops == &vdev_spare_ops);
1306 	cvd->vdev_ashift = mvd->vdev_ashift;
1307 	cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
1308 	cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
1309 	vdev_remove_child(mvd, cvd);
1310 	vdev_remove_child(pvd, mvd);
1311 
1312 	/*
1313 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1314 	 * Otherwise, we could have detached an offline device, and when we
1315 	 * go to import the pool we'll think we have two top-level vdevs,
1316 	 * instead of a different version of the same top-level vdev.
1317 	 */
1318 	if (mvd->vdev_top == mvd) {
1319 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1320 		cvd->vdev_orig_guid = cvd->vdev_guid;
1321 		cvd->vdev_guid += guid_delta;
1322 		cvd->vdev_guid_sum += guid_delta;
1323 
1324 		/*
1325 		 * If pool not set for autoexpand, we need to also preserve
1326 		 * mvd's asize to prevent automatic expansion of cvd.
1327 		 * Otherwise if we are adjusting the mirror by attaching and
1328 		 * detaching children of non-uniform sizes, the mirror could
1329 		 * autoexpand, unexpectedly requiring larger devices to
1330 		 * re-establish the mirror.
1331 		 */
1332 		if (!cvd->vdev_spa->spa_autoexpand)
1333 			cvd->vdev_asize = mvd->vdev_asize;
1334 	}
1335 	cvd->vdev_id = mvd->vdev_id;
1336 	vdev_add_child(pvd, cvd);
1337 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1338 
1339 	if (cvd == cvd->vdev_top)
1340 		vdev_top_transfer(mvd, cvd);
1341 
1342 	ASSERT(mvd->vdev_children == 0);
1343 	vdev_free(mvd);
1344 }
1345 
1346 void
1347 vdev_metaslab_group_create(vdev_t *vd)
1348 {
1349 	spa_t *spa = vd->vdev_spa;
1350 
1351 	/*
1352 	 * metaslab_group_create was delayed until allocation bias was available
1353 	 */
1354 	if (vd->vdev_mg == NULL) {
1355 		metaslab_class_t *mc;
1356 
1357 		if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1358 			vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1359 
1360 		ASSERT3U(vd->vdev_islog, ==,
1361 		    (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1362 
1363 		switch (vd->vdev_alloc_bias) {
1364 		case VDEV_BIAS_LOG:
1365 			mc = spa_log_class(spa);
1366 			break;
1367 		case VDEV_BIAS_SPECIAL:
1368 			mc = spa_special_class(spa);
1369 			break;
1370 		case VDEV_BIAS_DEDUP:
1371 			mc = spa_dedup_class(spa);
1372 			break;
1373 		default:
1374 			mc = spa_normal_class(spa);
1375 		}
1376 
1377 		vd->vdev_mg = metaslab_group_create(mc, vd,
1378 		    spa->spa_alloc_count);
1379 
1380 		if (!vd->vdev_islog) {
1381 			vd->vdev_log_mg = metaslab_group_create(
1382 			    spa_embedded_log_class(spa), vd, 1);
1383 		}
1384 
1385 		/*
1386 		 * The spa ashift min/max only apply for the normal metaslab
1387 		 * class. Class destination is late binding so ashift boundary
1388 		 * setting had to wait until now.
1389 		 */
1390 		if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1391 		    mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1392 			if (vd->vdev_ashift > spa->spa_max_ashift)
1393 				spa->spa_max_ashift = vd->vdev_ashift;
1394 			if (vd->vdev_ashift < spa->spa_min_ashift)
1395 				spa->spa_min_ashift = vd->vdev_ashift;
1396 
1397 			uint64_t min_alloc = vdev_get_min_alloc(vd);
1398 			if (min_alloc < spa->spa_min_alloc)
1399 				spa->spa_min_alloc = min_alloc;
1400 		}
1401 	}
1402 }
1403 
1404 int
1405 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1406 {
1407 	spa_t *spa = vd->vdev_spa;
1408 	uint64_t oldc = vd->vdev_ms_count;
1409 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1410 	metaslab_t **mspp;
1411 	int error;
1412 	boolean_t expanding = (oldc != 0);
1413 
1414 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1415 
1416 	/*
1417 	 * This vdev is not being allocated from yet or is a hole.
1418 	 */
1419 	if (vd->vdev_ms_shift == 0)
1420 		return (0);
1421 
1422 	ASSERT(!vd->vdev_ishole);
1423 
1424 	ASSERT(oldc <= newc);
1425 
1426 	mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1427 
1428 	if (expanding) {
1429 		memcpy(mspp, vd->vdev_ms, oldc * sizeof (*mspp));
1430 		vmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1431 	}
1432 
1433 	vd->vdev_ms = mspp;
1434 	vd->vdev_ms_count = newc;
1435 
1436 	for (uint64_t m = oldc; m < newc; m++) {
1437 		uint64_t object = 0;
1438 		/*
1439 		 * vdev_ms_array may be 0 if we are creating the "fake"
1440 		 * metaslabs for an indirect vdev for zdb's leak detection.
1441 		 * See zdb_leak_init().
1442 		 */
1443 		if (txg == 0 && vd->vdev_ms_array != 0) {
1444 			error = dmu_read(spa->spa_meta_objset,
1445 			    vd->vdev_ms_array,
1446 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
1447 			    DMU_READ_PREFETCH);
1448 			if (error != 0) {
1449 				vdev_dbgmsg(vd, "unable to read the metaslab "
1450 				    "array [error=%d]", error);
1451 				return (error);
1452 			}
1453 		}
1454 
1455 		error = metaslab_init(vd->vdev_mg, m, object, txg,
1456 		    &(vd->vdev_ms[m]));
1457 		if (error != 0) {
1458 			vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1459 			    error);
1460 			return (error);
1461 		}
1462 	}
1463 
1464 	/*
1465 	 * Find the emptiest metaslab on the vdev and mark it for use for
1466 	 * embedded slog by moving it from the regular to the log metaslab
1467 	 * group.
1468 	 */
1469 	if (vd->vdev_mg->mg_class == spa_normal_class(spa) &&
1470 	    vd->vdev_ms_count > zfs_embedded_slog_min_ms &&
1471 	    avl_is_empty(&vd->vdev_log_mg->mg_metaslab_tree)) {
1472 		uint64_t slog_msid = 0;
1473 		uint64_t smallest = UINT64_MAX;
1474 
1475 		/*
1476 		 * Note, we only search the new metaslabs, because the old
1477 		 * (pre-existing) ones may be active (e.g. have non-empty
1478 		 * range_tree's), and we don't move them to the new
1479 		 * metaslab_t.
1480 		 */
1481 		for (uint64_t m = oldc; m < newc; m++) {
1482 			uint64_t alloc =
1483 			    space_map_allocated(vd->vdev_ms[m]->ms_sm);
1484 			if (alloc < smallest) {
1485 				slog_msid = m;
1486 				smallest = alloc;
1487 			}
1488 		}
1489 		metaslab_t *slog_ms = vd->vdev_ms[slog_msid];
1490 		/*
1491 		 * The metaslab was marked as dirty at the end of
1492 		 * metaslab_init(). Remove it from the dirty list so that we
1493 		 * can uninitialize and reinitialize it to the new class.
1494 		 */
1495 		if (txg != 0) {
1496 			(void) txg_list_remove_this(&vd->vdev_ms_list,
1497 			    slog_ms, txg);
1498 		}
1499 		uint64_t sm_obj = space_map_object(slog_ms->ms_sm);
1500 		metaslab_fini(slog_ms);
1501 		VERIFY0(metaslab_init(vd->vdev_log_mg, slog_msid, sm_obj, txg,
1502 		    &vd->vdev_ms[slog_msid]));
1503 	}
1504 
1505 	if (txg == 0)
1506 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1507 
1508 	/*
1509 	 * If the vdev is marked as non-allocating then don't
1510 	 * activate the metaslabs since we want to ensure that
1511 	 * no allocations are performed on this device.
1512 	 */
1513 	if (vd->vdev_noalloc) {
1514 		/* track non-allocating vdev space */
1515 		spa->spa_nonallocating_dspace += spa_deflate(spa) ?
1516 		    vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1517 	} else if (!expanding) {
1518 		metaslab_group_activate(vd->vdev_mg);
1519 		if (vd->vdev_log_mg != NULL)
1520 			metaslab_group_activate(vd->vdev_log_mg);
1521 	}
1522 
1523 	if (txg == 0)
1524 		spa_config_exit(spa, SCL_ALLOC, FTAG);
1525 
1526 	return (0);
1527 }
1528 
1529 void
1530 vdev_metaslab_fini(vdev_t *vd)
1531 {
1532 	if (vd->vdev_checkpoint_sm != NULL) {
1533 		ASSERT(spa_feature_is_active(vd->vdev_spa,
1534 		    SPA_FEATURE_POOL_CHECKPOINT));
1535 		space_map_close(vd->vdev_checkpoint_sm);
1536 		/*
1537 		 * Even though we close the space map, we need to set its
1538 		 * pointer to NULL. The reason is that vdev_metaslab_fini()
1539 		 * may be called multiple times for certain operations
1540 		 * (i.e. when destroying a pool) so we need to ensure that
1541 		 * this clause never executes twice. This logic is similar
1542 		 * to the one used for the vdev_ms clause below.
1543 		 */
1544 		vd->vdev_checkpoint_sm = NULL;
1545 	}
1546 
1547 	if (vd->vdev_ms != NULL) {
1548 		metaslab_group_t *mg = vd->vdev_mg;
1549 
1550 		metaslab_group_passivate(mg);
1551 		if (vd->vdev_log_mg != NULL) {
1552 			ASSERT(!vd->vdev_islog);
1553 			metaslab_group_passivate(vd->vdev_log_mg);
1554 		}
1555 
1556 		uint64_t count = vd->vdev_ms_count;
1557 		for (uint64_t m = 0; m < count; m++) {
1558 			metaslab_t *msp = vd->vdev_ms[m];
1559 			if (msp != NULL)
1560 				metaslab_fini(msp);
1561 		}
1562 		vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1563 		vd->vdev_ms = NULL;
1564 		vd->vdev_ms_count = 0;
1565 
1566 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
1567 			ASSERT0(mg->mg_histogram[i]);
1568 			if (vd->vdev_log_mg != NULL)
1569 				ASSERT0(vd->vdev_log_mg->mg_histogram[i]);
1570 		}
1571 	}
1572 	ASSERT0(vd->vdev_ms_count);
1573 	ASSERT3U(vd->vdev_pending_fastwrite, ==, 0);
1574 }
1575 
1576 typedef struct vdev_probe_stats {
1577 	boolean_t	vps_readable;
1578 	boolean_t	vps_writeable;
1579 	int		vps_flags;
1580 } vdev_probe_stats_t;
1581 
1582 static void
1583 vdev_probe_done(zio_t *zio)
1584 {
1585 	spa_t *spa = zio->io_spa;
1586 	vdev_t *vd = zio->io_vd;
1587 	vdev_probe_stats_t *vps = zio->io_private;
1588 
1589 	ASSERT(vd->vdev_probe_zio != NULL);
1590 
1591 	if (zio->io_type == ZIO_TYPE_READ) {
1592 		if (zio->io_error == 0)
1593 			vps->vps_readable = 1;
1594 		if (zio->io_error == 0 && spa_writeable(spa)) {
1595 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1596 			    zio->io_offset, zio->io_size, zio->io_abd,
1597 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1598 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1599 		} else {
1600 			abd_free(zio->io_abd);
1601 		}
1602 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
1603 		if (zio->io_error == 0)
1604 			vps->vps_writeable = 1;
1605 		abd_free(zio->io_abd);
1606 	} else if (zio->io_type == ZIO_TYPE_NULL) {
1607 		zio_t *pio;
1608 		zio_link_t *zl;
1609 
1610 		vd->vdev_cant_read |= !vps->vps_readable;
1611 		vd->vdev_cant_write |= !vps->vps_writeable;
1612 
1613 		if (vdev_readable(vd) &&
1614 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
1615 			zio->io_error = 0;
1616 		} else {
1617 			ASSERT(zio->io_error != 0);
1618 			vdev_dbgmsg(vd, "failed probe");
1619 			(void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1620 			    spa, vd, NULL, NULL, 0);
1621 			zio->io_error = SET_ERROR(ENXIO);
1622 		}
1623 
1624 		mutex_enter(&vd->vdev_probe_lock);
1625 		ASSERT(vd->vdev_probe_zio == zio);
1626 		vd->vdev_probe_zio = NULL;
1627 		mutex_exit(&vd->vdev_probe_lock);
1628 
1629 		zl = NULL;
1630 		while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1631 			if (!vdev_accessible(vd, pio))
1632 				pio->io_error = SET_ERROR(ENXIO);
1633 
1634 		kmem_free(vps, sizeof (*vps));
1635 	}
1636 }
1637 
1638 /*
1639  * Determine whether this device is accessible.
1640  *
1641  * Read and write to several known locations: the pad regions of each
1642  * vdev label but the first, which we leave alone in case it contains
1643  * a VTOC.
1644  */
1645 zio_t *
1646 vdev_probe(vdev_t *vd, zio_t *zio)
1647 {
1648 	spa_t *spa = vd->vdev_spa;
1649 	vdev_probe_stats_t *vps = NULL;
1650 	zio_t *pio;
1651 
1652 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1653 
1654 	/*
1655 	 * Don't probe the probe.
1656 	 */
1657 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1658 		return (NULL);
1659 
1660 	/*
1661 	 * To prevent 'probe storms' when a device fails, we create
1662 	 * just one probe i/o at a time.  All zios that want to probe
1663 	 * this vdev will become parents of the probe io.
1664 	 */
1665 	mutex_enter(&vd->vdev_probe_lock);
1666 
1667 	if ((pio = vd->vdev_probe_zio) == NULL) {
1668 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1669 
1670 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1671 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1672 		    ZIO_FLAG_TRYHARD;
1673 
1674 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1675 			/*
1676 			 * vdev_cant_read and vdev_cant_write can only
1677 			 * transition from TRUE to FALSE when we have the
1678 			 * SCL_ZIO lock as writer; otherwise they can only
1679 			 * transition from FALSE to TRUE.  This ensures that
1680 			 * any zio looking at these values can assume that
1681 			 * failures persist for the life of the I/O.  That's
1682 			 * important because when a device has intermittent
1683 			 * connectivity problems, we want to ensure that
1684 			 * they're ascribed to the device (ENXIO) and not
1685 			 * the zio (EIO).
1686 			 *
1687 			 * Since we hold SCL_ZIO as writer here, clear both
1688 			 * values so the probe can reevaluate from first
1689 			 * principles.
1690 			 */
1691 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1692 			vd->vdev_cant_read = B_FALSE;
1693 			vd->vdev_cant_write = B_FALSE;
1694 		}
1695 
1696 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1697 		    vdev_probe_done, vps,
1698 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1699 
1700 		/*
1701 		 * We can't change the vdev state in this context, so we
1702 		 * kick off an async task to do it on our behalf.
1703 		 */
1704 		if (zio != NULL) {
1705 			vd->vdev_probe_wanted = B_TRUE;
1706 			spa_async_request(spa, SPA_ASYNC_PROBE);
1707 		}
1708 	}
1709 
1710 	if (zio != NULL)
1711 		zio_add_child(zio, pio);
1712 
1713 	mutex_exit(&vd->vdev_probe_lock);
1714 
1715 	if (vps == NULL) {
1716 		ASSERT(zio != NULL);
1717 		return (NULL);
1718 	}
1719 
1720 	for (int l = 1; l < VDEV_LABELS; l++) {
1721 		zio_nowait(zio_read_phys(pio, vd,
1722 		    vdev_label_offset(vd->vdev_psize, l,
1723 		    offsetof(vdev_label_t, vl_be)), VDEV_PAD_SIZE,
1724 		    abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1725 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1726 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1727 	}
1728 
1729 	if (zio == NULL)
1730 		return (pio);
1731 
1732 	zio_nowait(pio);
1733 	return (NULL);
1734 }
1735 
1736 static void
1737 vdev_load_child(void *arg)
1738 {
1739 	vdev_t *vd = arg;
1740 
1741 	vd->vdev_load_error = vdev_load(vd);
1742 }
1743 
1744 static void
1745 vdev_open_child(void *arg)
1746 {
1747 	vdev_t *vd = arg;
1748 
1749 	vd->vdev_open_thread = curthread;
1750 	vd->vdev_open_error = vdev_open(vd);
1751 	vd->vdev_open_thread = NULL;
1752 }
1753 
1754 static boolean_t
1755 vdev_uses_zvols(vdev_t *vd)
1756 {
1757 #ifdef _KERNEL
1758 	if (zvol_is_zvol(vd->vdev_path))
1759 		return (B_TRUE);
1760 #endif
1761 
1762 	for (int c = 0; c < vd->vdev_children; c++)
1763 		if (vdev_uses_zvols(vd->vdev_child[c]))
1764 			return (B_TRUE);
1765 
1766 	return (B_FALSE);
1767 }
1768 
1769 /*
1770  * Returns B_TRUE if the passed child should be opened.
1771  */
1772 static boolean_t
1773 vdev_default_open_children_func(vdev_t *vd)
1774 {
1775 	(void) vd;
1776 	return (B_TRUE);
1777 }
1778 
1779 /*
1780  * Open the requested child vdevs.  If any of the leaf vdevs are using
1781  * a ZFS volume then do the opens in a single thread.  This avoids a
1782  * deadlock when the current thread is holding the spa_namespace_lock.
1783  */
1784 static void
1785 vdev_open_children_impl(vdev_t *vd, vdev_open_children_func_t *open_func)
1786 {
1787 	int children = vd->vdev_children;
1788 
1789 	taskq_t *tq = taskq_create("vdev_open", children, minclsyspri,
1790 	    children, children, TASKQ_PREPOPULATE);
1791 	vd->vdev_nonrot = B_TRUE;
1792 
1793 	for (int c = 0; c < children; c++) {
1794 		vdev_t *cvd = vd->vdev_child[c];
1795 
1796 		if (open_func(cvd) == B_FALSE)
1797 			continue;
1798 
1799 		if (tq == NULL || vdev_uses_zvols(vd)) {
1800 			cvd->vdev_open_error = vdev_open(cvd);
1801 		} else {
1802 			VERIFY(taskq_dispatch(tq, vdev_open_child,
1803 			    cvd, TQ_SLEEP) != TASKQID_INVALID);
1804 		}
1805 
1806 		vd->vdev_nonrot &= cvd->vdev_nonrot;
1807 	}
1808 
1809 	if (tq != NULL) {
1810 		taskq_wait(tq);
1811 		taskq_destroy(tq);
1812 	}
1813 }
1814 
1815 /*
1816  * Open all child vdevs.
1817  */
1818 void
1819 vdev_open_children(vdev_t *vd)
1820 {
1821 	vdev_open_children_impl(vd, vdev_default_open_children_func);
1822 }
1823 
1824 /*
1825  * Conditionally open a subset of child vdevs.
1826  */
1827 void
1828 vdev_open_children_subset(vdev_t *vd, vdev_open_children_func_t *open_func)
1829 {
1830 	vdev_open_children_impl(vd, open_func);
1831 }
1832 
1833 /*
1834  * Compute the raidz-deflation ratio.  Note, we hard-code
1835  * in 128k (1 << 17) because it is the "typical" blocksize.
1836  * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1837  * otherwise it would inconsistently account for existing bp's.
1838  */
1839 static void
1840 vdev_set_deflate_ratio(vdev_t *vd)
1841 {
1842 	if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1843 		vd->vdev_deflate_ratio = (1 << 17) /
1844 		    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
1845 	}
1846 }
1847 
1848 /*
1849  * Maximize performance by inflating the configured ashift for top level
1850  * vdevs to be as close to the physical ashift as possible while maintaining
1851  * administrator defined limits and ensuring it doesn't go below the
1852  * logical ashift.
1853  */
1854 static void
1855 vdev_ashift_optimize(vdev_t *vd)
1856 {
1857 	ASSERT(vd == vd->vdev_top);
1858 
1859 	if (vd->vdev_ashift < vd->vdev_physical_ashift) {
1860 		vd->vdev_ashift = MIN(
1861 		    MAX(zfs_vdev_max_auto_ashift, vd->vdev_ashift),
1862 		    MAX(zfs_vdev_min_auto_ashift,
1863 		    vd->vdev_physical_ashift));
1864 	} else {
1865 		/*
1866 		 * If the logical and physical ashifts are the same, then
1867 		 * we ensure that the top-level vdev's ashift is not smaller
1868 		 * than our minimum ashift value. For the unusual case
1869 		 * where logical ashift > physical ashift, we can't cap
1870 		 * the calculated ashift based on max ashift as that
1871 		 * would cause failures.
1872 		 * We still check if we need to increase it to match
1873 		 * the min ashift.
1874 		 */
1875 		vd->vdev_ashift = MAX(zfs_vdev_min_auto_ashift,
1876 		    vd->vdev_ashift);
1877 	}
1878 }
1879 
1880 /*
1881  * Prepare a virtual device for access.
1882  */
1883 int
1884 vdev_open(vdev_t *vd)
1885 {
1886 	spa_t *spa = vd->vdev_spa;
1887 	int error;
1888 	uint64_t osize = 0;
1889 	uint64_t max_osize = 0;
1890 	uint64_t asize, max_asize, psize;
1891 	uint64_t logical_ashift = 0;
1892 	uint64_t physical_ashift = 0;
1893 
1894 	ASSERT(vd->vdev_open_thread == curthread ||
1895 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1896 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1897 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1898 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1899 
1900 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1901 	vd->vdev_cant_read = B_FALSE;
1902 	vd->vdev_cant_write = B_FALSE;
1903 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1904 
1905 	/*
1906 	 * If this vdev is not removed, check its fault status.  If it's
1907 	 * faulted, bail out of the open.
1908 	 */
1909 	if (!vd->vdev_removed && vd->vdev_faulted) {
1910 		ASSERT(vd->vdev_children == 0);
1911 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1912 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1913 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1914 		    vd->vdev_label_aux);
1915 		return (SET_ERROR(ENXIO));
1916 	} else if (vd->vdev_offline) {
1917 		ASSERT(vd->vdev_children == 0);
1918 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1919 		return (SET_ERROR(ENXIO));
1920 	}
1921 
1922 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
1923 	    &logical_ashift, &physical_ashift);
1924 	/*
1925 	 * Physical volume size should never be larger than its max size, unless
1926 	 * the disk has shrunk while we were reading it or the device is buggy
1927 	 * or damaged: either way it's not safe for use, bail out of the open.
1928 	 */
1929 	if (osize > max_osize) {
1930 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1931 		    VDEV_AUX_OPEN_FAILED);
1932 		return (SET_ERROR(ENXIO));
1933 	}
1934 
1935 	/*
1936 	 * Reset the vdev_reopening flag so that we actually close
1937 	 * the vdev on error.
1938 	 */
1939 	vd->vdev_reopening = B_FALSE;
1940 	if (zio_injection_enabled && error == 0)
1941 		error = zio_handle_device_injection(vd, NULL, SET_ERROR(ENXIO));
1942 
1943 	if (error) {
1944 		if (vd->vdev_removed &&
1945 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1946 			vd->vdev_removed = B_FALSE;
1947 
1948 		if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
1949 			vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
1950 			    vd->vdev_stat.vs_aux);
1951 		} else {
1952 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1953 			    vd->vdev_stat.vs_aux);
1954 		}
1955 		return (error);
1956 	}
1957 
1958 	vd->vdev_removed = B_FALSE;
1959 
1960 	/*
1961 	 * Recheck the faulted flag now that we have confirmed that
1962 	 * the vdev is accessible.  If we're faulted, bail.
1963 	 */
1964 	if (vd->vdev_faulted) {
1965 		ASSERT(vd->vdev_children == 0);
1966 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1967 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1968 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1969 		    vd->vdev_label_aux);
1970 		return (SET_ERROR(ENXIO));
1971 	}
1972 
1973 	if (vd->vdev_degraded) {
1974 		ASSERT(vd->vdev_children == 0);
1975 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1976 		    VDEV_AUX_ERR_EXCEEDED);
1977 	} else {
1978 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1979 	}
1980 
1981 	/*
1982 	 * For hole or missing vdevs we just return success.
1983 	 */
1984 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1985 		return (0);
1986 
1987 	for (int c = 0; c < vd->vdev_children; c++) {
1988 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1989 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1990 			    VDEV_AUX_NONE);
1991 			break;
1992 		}
1993 	}
1994 
1995 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1996 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1997 
1998 	if (vd->vdev_children == 0) {
1999 		if (osize < SPA_MINDEVSIZE) {
2000 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2001 			    VDEV_AUX_TOO_SMALL);
2002 			return (SET_ERROR(EOVERFLOW));
2003 		}
2004 		psize = osize;
2005 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
2006 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
2007 		    VDEV_LABEL_END_SIZE);
2008 	} else {
2009 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
2010 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
2011 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2012 			    VDEV_AUX_TOO_SMALL);
2013 			return (SET_ERROR(EOVERFLOW));
2014 		}
2015 		psize = 0;
2016 		asize = osize;
2017 		max_asize = max_osize;
2018 	}
2019 
2020 	/*
2021 	 * If the vdev was expanded, record this so that we can re-create the
2022 	 * uberblock rings in labels {2,3}, during the next sync.
2023 	 */
2024 	if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0))
2025 		vd->vdev_copy_uberblocks = B_TRUE;
2026 
2027 	vd->vdev_psize = psize;
2028 
2029 	/*
2030 	 * Make sure the allocatable size hasn't shrunk too much.
2031 	 */
2032 	if (asize < vd->vdev_min_asize) {
2033 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2034 		    VDEV_AUX_BAD_LABEL);
2035 		return (SET_ERROR(EINVAL));
2036 	}
2037 
2038 	/*
2039 	 * We can always set the logical/physical ashift members since
2040 	 * their values are only used to calculate the vdev_ashift when
2041 	 * the device is first added to the config. These values should
2042 	 * not be used for anything else since they may change whenever
2043 	 * the device is reopened and we don't store them in the label.
2044 	 */
2045 	vd->vdev_physical_ashift =
2046 	    MAX(physical_ashift, vd->vdev_physical_ashift);
2047 	vd->vdev_logical_ashift = MAX(logical_ashift,
2048 	    vd->vdev_logical_ashift);
2049 
2050 	if (vd->vdev_asize == 0) {
2051 		/*
2052 		 * This is the first-ever open, so use the computed values.
2053 		 * For compatibility, a different ashift can be requested.
2054 		 */
2055 		vd->vdev_asize = asize;
2056 		vd->vdev_max_asize = max_asize;
2057 
2058 		/*
2059 		 * If the vdev_ashift was not overridden at creation time,
2060 		 * then set it the logical ashift and optimize the ashift.
2061 		 */
2062 		if (vd->vdev_ashift == 0) {
2063 			vd->vdev_ashift = vd->vdev_logical_ashift;
2064 
2065 			if (vd->vdev_logical_ashift > ASHIFT_MAX) {
2066 				vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2067 				    VDEV_AUX_ASHIFT_TOO_BIG);
2068 				return (SET_ERROR(EDOM));
2069 			}
2070 
2071 			if (vd->vdev_top == vd) {
2072 				vdev_ashift_optimize(vd);
2073 			}
2074 		}
2075 		if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
2076 		    vd->vdev_ashift > ASHIFT_MAX)) {
2077 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2078 			    VDEV_AUX_BAD_ASHIFT);
2079 			return (SET_ERROR(EDOM));
2080 		}
2081 	} else {
2082 		/*
2083 		 * Make sure the alignment required hasn't increased.
2084 		 */
2085 		if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
2086 		    vd->vdev_ops->vdev_op_leaf) {
2087 			(void) zfs_ereport_post(
2088 			    FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
2089 			    spa, vd, NULL, NULL, 0);
2090 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2091 			    VDEV_AUX_BAD_LABEL);
2092 			return (SET_ERROR(EDOM));
2093 		}
2094 		vd->vdev_max_asize = max_asize;
2095 	}
2096 
2097 	/*
2098 	 * If all children are healthy we update asize if either:
2099 	 * The asize has increased, due to a device expansion caused by dynamic
2100 	 * LUN growth or vdev replacement, and automatic expansion is enabled;
2101 	 * making the additional space available.
2102 	 *
2103 	 * The asize has decreased, due to a device shrink usually caused by a
2104 	 * vdev replace with a smaller device. This ensures that calculations
2105 	 * based of max_asize and asize e.g. esize are always valid. It's safe
2106 	 * to do this as we've already validated that asize is greater than
2107 	 * vdev_min_asize.
2108 	 */
2109 	if (vd->vdev_state == VDEV_STATE_HEALTHY &&
2110 	    ((asize > vd->vdev_asize &&
2111 	    (vd->vdev_expanding || spa->spa_autoexpand)) ||
2112 	    (asize < vd->vdev_asize)))
2113 		vd->vdev_asize = asize;
2114 
2115 	vdev_set_min_asize(vd);
2116 
2117 	/*
2118 	 * Ensure we can issue some IO before declaring the
2119 	 * vdev open for business.
2120 	 */
2121 	if (vd->vdev_ops->vdev_op_leaf &&
2122 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
2123 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2124 		    VDEV_AUX_ERR_EXCEEDED);
2125 		return (error);
2126 	}
2127 
2128 	/*
2129 	 * Track the minimum allocation size.
2130 	 */
2131 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
2132 	    vd->vdev_islog == 0 && vd->vdev_aux == NULL) {
2133 		uint64_t min_alloc = vdev_get_min_alloc(vd);
2134 		if (min_alloc < spa->spa_min_alloc)
2135 			spa->spa_min_alloc = min_alloc;
2136 	}
2137 
2138 	/*
2139 	 * If this is a leaf vdev, assess whether a resilver is needed.
2140 	 * But don't do this if we are doing a reopen for a scrub, since
2141 	 * this would just restart the scrub we are already doing.
2142 	 */
2143 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen)
2144 		dsl_scan_assess_vdev(spa->spa_dsl_pool, vd);
2145 
2146 	return (0);
2147 }
2148 
2149 static void
2150 vdev_validate_child(void *arg)
2151 {
2152 	vdev_t *vd = arg;
2153 
2154 	vd->vdev_validate_thread = curthread;
2155 	vd->vdev_validate_error = vdev_validate(vd);
2156 	vd->vdev_validate_thread = NULL;
2157 }
2158 
2159 /*
2160  * Called once the vdevs are all opened, this routine validates the label
2161  * contents. This needs to be done before vdev_load() so that we don't
2162  * inadvertently do repair I/Os to the wrong device.
2163  *
2164  * This function will only return failure if one of the vdevs indicates that it
2165  * has since been destroyed or exported.  This is only possible if
2166  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
2167  * will be updated but the function will return 0.
2168  */
2169 int
2170 vdev_validate(vdev_t *vd)
2171 {
2172 	spa_t *spa = vd->vdev_spa;
2173 	taskq_t *tq = NULL;
2174 	nvlist_t *label;
2175 	uint64_t guid = 0, aux_guid = 0, top_guid;
2176 	uint64_t state;
2177 	nvlist_t *nvl;
2178 	uint64_t txg;
2179 	int children = vd->vdev_children;
2180 
2181 	if (vdev_validate_skip)
2182 		return (0);
2183 
2184 	if (children > 0) {
2185 		tq = taskq_create("vdev_validate", children, minclsyspri,
2186 		    children, children, TASKQ_PREPOPULATE);
2187 	}
2188 
2189 	for (uint64_t c = 0; c < children; c++) {
2190 		vdev_t *cvd = vd->vdev_child[c];
2191 
2192 		if (tq == NULL || vdev_uses_zvols(cvd)) {
2193 			vdev_validate_child(cvd);
2194 		} else {
2195 			VERIFY(taskq_dispatch(tq, vdev_validate_child, cvd,
2196 			    TQ_SLEEP) != TASKQID_INVALID);
2197 		}
2198 	}
2199 	if (tq != NULL) {
2200 		taskq_wait(tq);
2201 		taskq_destroy(tq);
2202 	}
2203 	for (int c = 0; c < children; c++) {
2204 		int error = vd->vdev_child[c]->vdev_validate_error;
2205 
2206 		if (error != 0)
2207 			return (SET_ERROR(EBADF));
2208 	}
2209 
2210 
2211 	/*
2212 	 * If the device has already failed, or was marked offline, don't do
2213 	 * any further validation.  Otherwise, label I/O will fail and we will
2214 	 * overwrite the previous state.
2215 	 */
2216 	if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
2217 		return (0);
2218 
2219 	/*
2220 	 * If we are performing an extreme rewind, we allow for a label that
2221 	 * was modified at a point after the current txg.
2222 	 * If config lock is not held do not check for the txg. spa_sync could
2223 	 * be updating the vdev's label before updating spa_last_synced_txg.
2224 	 */
2225 	if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
2226 	    spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
2227 		txg = UINT64_MAX;
2228 	else
2229 		txg = spa_last_synced_txg(spa);
2230 
2231 	if ((label = vdev_label_read_config(vd, txg)) == NULL) {
2232 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2233 		    VDEV_AUX_BAD_LABEL);
2234 		vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
2235 		    "txg %llu", (u_longlong_t)txg);
2236 		return (0);
2237 	}
2238 
2239 	/*
2240 	 * Determine if this vdev has been split off into another
2241 	 * pool.  If so, then refuse to open it.
2242 	 */
2243 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
2244 	    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
2245 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2246 		    VDEV_AUX_SPLIT_POOL);
2247 		nvlist_free(label);
2248 		vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
2249 		return (0);
2250 	}
2251 
2252 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
2253 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2254 		    VDEV_AUX_CORRUPT_DATA);
2255 		nvlist_free(label);
2256 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2257 		    ZPOOL_CONFIG_POOL_GUID);
2258 		return (0);
2259 	}
2260 
2261 	/*
2262 	 * If config is not trusted then ignore the spa guid check. This is
2263 	 * necessary because if the machine crashed during a re-guid the new
2264 	 * guid might have been written to all of the vdev labels, but not the
2265 	 * cached config. The check will be performed again once we have the
2266 	 * trusted config from the MOS.
2267 	 */
2268 	if (spa->spa_trust_config && guid != spa_guid(spa)) {
2269 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2270 		    VDEV_AUX_CORRUPT_DATA);
2271 		nvlist_free(label);
2272 		vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
2273 		    "match config (%llu != %llu)", (u_longlong_t)guid,
2274 		    (u_longlong_t)spa_guid(spa));
2275 		return (0);
2276 	}
2277 
2278 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
2279 	    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
2280 	    &aux_guid) != 0)
2281 		aux_guid = 0;
2282 
2283 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
2284 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2285 		    VDEV_AUX_CORRUPT_DATA);
2286 		nvlist_free(label);
2287 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2288 		    ZPOOL_CONFIG_GUID);
2289 		return (0);
2290 	}
2291 
2292 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
2293 	    != 0) {
2294 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2295 		    VDEV_AUX_CORRUPT_DATA);
2296 		nvlist_free(label);
2297 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2298 		    ZPOOL_CONFIG_TOP_GUID);
2299 		return (0);
2300 	}
2301 
2302 	/*
2303 	 * If this vdev just became a top-level vdev because its sibling was
2304 	 * detached, it will have adopted the parent's vdev guid -- but the
2305 	 * label may or may not be on disk yet. Fortunately, either version
2306 	 * of the label will have the same top guid, so if we're a top-level
2307 	 * vdev, we can safely compare to that instead.
2308 	 * However, if the config comes from a cachefile that failed to update
2309 	 * after the detach, a top-level vdev will appear as a non top-level
2310 	 * vdev in the config. Also relax the constraints if we perform an
2311 	 * extreme rewind.
2312 	 *
2313 	 * If we split this vdev off instead, then we also check the
2314 	 * original pool's guid. We don't want to consider the vdev
2315 	 * corrupt if it is partway through a split operation.
2316 	 */
2317 	if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
2318 		boolean_t mismatch = B_FALSE;
2319 		if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
2320 			if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
2321 				mismatch = B_TRUE;
2322 		} else {
2323 			if (vd->vdev_guid != top_guid &&
2324 			    vd->vdev_top->vdev_guid != guid)
2325 				mismatch = B_TRUE;
2326 		}
2327 
2328 		if (mismatch) {
2329 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2330 			    VDEV_AUX_CORRUPT_DATA);
2331 			nvlist_free(label);
2332 			vdev_dbgmsg(vd, "vdev_validate: config guid "
2333 			    "doesn't match label guid");
2334 			vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
2335 			    (u_longlong_t)vd->vdev_guid,
2336 			    (u_longlong_t)vd->vdev_top->vdev_guid);
2337 			vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
2338 			    "aux_guid %llu", (u_longlong_t)guid,
2339 			    (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
2340 			return (0);
2341 		}
2342 	}
2343 
2344 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
2345 	    &state) != 0) {
2346 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2347 		    VDEV_AUX_CORRUPT_DATA);
2348 		nvlist_free(label);
2349 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2350 		    ZPOOL_CONFIG_POOL_STATE);
2351 		return (0);
2352 	}
2353 
2354 	nvlist_free(label);
2355 
2356 	/*
2357 	 * If this is a verbatim import, no need to check the
2358 	 * state of the pool.
2359 	 */
2360 	if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
2361 	    spa_load_state(spa) == SPA_LOAD_OPEN &&
2362 	    state != POOL_STATE_ACTIVE) {
2363 		vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
2364 		    "for spa %s", (u_longlong_t)state, spa->spa_name);
2365 		return (SET_ERROR(EBADF));
2366 	}
2367 
2368 	/*
2369 	 * If we were able to open and validate a vdev that was
2370 	 * previously marked permanently unavailable, clear that state
2371 	 * now.
2372 	 */
2373 	if (vd->vdev_not_present)
2374 		vd->vdev_not_present = 0;
2375 
2376 	return (0);
2377 }
2378 
2379 static void
2380 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
2381 {
2382 	char *old, *new;
2383 	if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
2384 		if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
2385 			zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2386 			    "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2387 			    dvd->vdev_path, svd->vdev_path);
2388 			spa_strfree(dvd->vdev_path);
2389 			dvd->vdev_path = spa_strdup(svd->vdev_path);
2390 		}
2391 	} else if (svd->vdev_path != NULL) {
2392 		dvd->vdev_path = spa_strdup(svd->vdev_path);
2393 		zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2394 		    (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
2395 	}
2396 
2397 	/*
2398 	 * Our enclosure sysfs path may have changed between imports
2399 	 */
2400 	old = dvd->vdev_enc_sysfs_path;
2401 	new = svd->vdev_enc_sysfs_path;
2402 	if ((old != NULL && new == NULL) ||
2403 	    (old == NULL && new != NULL) ||
2404 	    ((old != NULL && new != NULL) && strcmp(new, old) != 0)) {
2405 		zfs_dbgmsg("vdev_copy_path: vdev %llu: vdev_enc_sysfs_path "
2406 		    "changed from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2407 		    old, new);
2408 
2409 		if (dvd->vdev_enc_sysfs_path)
2410 			spa_strfree(dvd->vdev_enc_sysfs_path);
2411 
2412 		if (svd->vdev_enc_sysfs_path) {
2413 			dvd->vdev_enc_sysfs_path = spa_strdup(
2414 			    svd->vdev_enc_sysfs_path);
2415 		} else {
2416 			dvd->vdev_enc_sysfs_path = NULL;
2417 		}
2418 	}
2419 }
2420 
2421 /*
2422  * Recursively copy vdev paths from one vdev to another. Source and destination
2423  * vdev trees must have same geometry otherwise return error. Intended to copy
2424  * paths from userland config into MOS config.
2425  */
2426 int
2427 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
2428 {
2429 	if ((svd->vdev_ops == &vdev_missing_ops) ||
2430 	    (svd->vdev_ishole && dvd->vdev_ishole) ||
2431 	    (dvd->vdev_ops == &vdev_indirect_ops))
2432 		return (0);
2433 
2434 	if (svd->vdev_ops != dvd->vdev_ops) {
2435 		vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
2436 		    svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
2437 		return (SET_ERROR(EINVAL));
2438 	}
2439 
2440 	if (svd->vdev_guid != dvd->vdev_guid) {
2441 		vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
2442 		    "%llu)", (u_longlong_t)svd->vdev_guid,
2443 		    (u_longlong_t)dvd->vdev_guid);
2444 		return (SET_ERROR(EINVAL));
2445 	}
2446 
2447 	if (svd->vdev_children != dvd->vdev_children) {
2448 		vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
2449 		    "%llu != %llu", (u_longlong_t)svd->vdev_children,
2450 		    (u_longlong_t)dvd->vdev_children);
2451 		return (SET_ERROR(EINVAL));
2452 	}
2453 
2454 	for (uint64_t i = 0; i < svd->vdev_children; i++) {
2455 		int error = vdev_copy_path_strict(svd->vdev_child[i],
2456 		    dvd->vdev_child[i]);
2457 		if (error != 0)
2458 			return (error);
2459 	}
2460 
2461 	if (svd->vdev_ops->vdev_op_leaf)
2462 		vdev_copy_path_impl(svd, dvd);
2463 
2464 	return (0);
2465 }
2466 
2467 static void
2468 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
2469 {
2470 	ASSERT(stvd->vdev_top == stvd);
2471 	ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
2472 
2473 	for (uint64_t i = 0; i < dvd->vdev_children; i++) {
2474 		vdev_copy_path_search(stvd, dvd->vdev_child[i]);
2475 	}
2476 
2477 	if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
2478 		return;
2479 
2480 	/*
2481 	 * The idea here is that while a vdev can shift positions within
2482 	 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2483 	 * step outside of it.
2484 	 */
2485 	vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
2486 
2487 	if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
2488 		return;
2489 
2490 	ASSERT(vd->vdev_ops->vdev_op_leaf);
2491 
2492 	vdev_copy_path_impl(vd, dvd);
2493 }
2494 
2495 /*
2496  * Recursively copy vdev paths from one root vdev to another. Source and
2497  * destination vdev trees may differ in geometry. For each destination leaf
2498  * vdev, search a vdev with the same guid and top vdev id in the source.
2499  * Intended to copy paths from userland config into MOS config.
2500  */
2501 void
2502 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
2503 {
2504 	uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
2505 	ASSERT(srvd->vdev_ops == &vdev_root_ops);
2506 	ASSERT(drvd->vdev_ops == &vdev_root_ops);
2507 
2508 	for (uint64_t i = 0; i < children; i++) {
2509 		vdev_copy_path_search(srvd->vdev_child[i],
2510 		    drvd->vdev_child[i]);
2511 	}
2512 }
2513 
2514 /*
2515  * Close a virtual device.
2516  */
2517 void
2518 vdev_close(vdev_t *vd)
2519 {
2520 	vdev_t *pvd = vd->vdev_parent;
2521 	spa_t *spa __maybe_unused = vd->vdev_spa;
2522 
2523 	ASSERT(vd != NULL);
2524 	ASSERT(vd->vdev_open_thread == curthread ||
2525 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2526 
2527 	/*
2528 	 * If our parent is reopening, then we are as well, unless we are
2529 	 * going offline.
2530 	 */
2531 	if (pvd != NULL && pvd->vdev_reopening)
2532 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2533 
2534 	vd->vdev_ops->vdev_op_close(vd);
2535 
2536 	vdev_cache_purge(vd);
2537 
2538 	/*
2539 	 * We record the previous state before we close it, so that if we are
2540 	 * doing a reopen(), we don't generate FMA ereports if we notice that
2541 	 * it's still faulted.
2542 	 */
2543 	vd->vdev_prevstate = vd->vdev_state;
2544 
2545 	if (vd->vdev_offline)
2546 		vd->vdev_state = VDEV_STATE_OFFLINE;
2547 	else
2548 		vd->vdev_state = VDEV_STATE_CLOSED;
2549 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2550 }
2551 
2552 void
2553 vdev_hold(vdev_t *vd)
2554 {
2555 	spa_t *spa = vd->vdev_spa;
2556 
2557 	ASSERT(spa_is_root(spa));
2558 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2559 		return;
2560 
2561 	for (int c = 0; c < vd->vdev_children; c++)
2562 		vdev_hold(vd->vdev_child[c]);
2563 
2564 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL)
2565 		vd->vdev_ops->vdev_op_hold(vd);
2566 }
2567 
2568 void
2569 vdev_rele(vdev_t *vd)
2570 {
2571 	ASSERT(spa_is_root(vd->vdev_spa));
2572 	for (int c = 0; c < vd->vdev_children; c++)
2573 		vdev_rele(vd->vdev_child[c]);
2574 
2575 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL)
2576 		vd->vdev_ops->vdev_op_rele(vd);
2577 }
2578 
2579 /*
2580  * Reopen all interior vdevs and any unopened leaves.  We don't actually
2581  * reopen leaf vdevs which had previously been opened as they might deadlock
2582  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
2583  * If the leaf has never been opened then open it, as usual.
2584  */
2585 void
2586 vdev_reopen(vdev_t *vd)
2587 {
2588 	spa_t *spa = vd->vdev_spa;
2589 
2590 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2591 
2592 	/* set the reopening flag unless we're taking the vdev offline */
2593 	vd->vdev_reopening = !vd->vdev_offline;
2594 	vdev_close(vd);
2595 	(void) vdev_open(vd);
2596 
2597 	/*
2598 	 * Call vdev_validate() here to make sure we have the same device.
2599 	 * Otherwise, a device with an invalid label could be successfully
2600 	 * opened in response to vdev_reopen().
2601 	 */
2602 	if (vd->vdev_aux) {
2603 		(void) vdev_validate_aux(vd);
2604 		if (vdev_readable(vd) && vdev_writeable(vd) &&
2605 		    vd->vdev_aux == &spa->spa_l2cache) {
2606 			/*
2607 			 * In case the vdev is present we should evict all ARC
2608 			 * buffers and pointers to log blocks and reclaim their
2609 			 * space before restoring its contents to L2ARC.
2610 			 */
2611 			if (l2arc_vdev_present(vd)) {
2612 				l2arc_rebuild_vdev(vd, B_TRUE);
2613 			} else {
2614 				l2arc_add_vdev(spa, vd);
2615 			}
2616 			spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
2617 			spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM);
2618 		}
2619 	} else {
2620 		(void) vdev_validate(vd);
2621 	}
2622 
2623 	/*
2624 	 * Reassess parent vdev's health.
2625 	 */
2626 	vdev_propagate_state(vd);
2627 }
2628 
2629 int
2630 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2631 {
2632 	int error;
2633 
2634 	/*
2635 	 * Normally, partial opens (e.g. of a mirror) are allowed.
2636 	 * For a create, however, we want to fail the request if
2637 	 * there are any components we can't open.
2638 	 */
2639 	error = vdev_open(vd);
2640 
2641 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2642 		vdev_close(vd);
2643 		return (error ? error : SET_ERROR(ENXIO));
2644 	}
2645 
2646 	/*
2647 	 * Recursively load DTLs and initialize all labels.
2648 	 */
2649 	if ((error = vdev_dtl_load(vd)) != 0 ||
2650 	    (error = vdev_label_init(vd, txg, isreplacing ?
2651 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2652 		vdev_close(vd);
2653 		return (error);
2654 	}
2655 
2656 	return (0);
2657 }
2658 
2659 void
2660 vdev_metaslab_set_size(vdev_t *vd)
2661 {
2662 	uint64_t asize = vd->vdev_asize;
2663 	uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2664 	uint64_t ms_shift;
2665 
2666 	/*
2667 	 * There are two dimensions to the metaslab sizing calculation:
2668 	 * the size of the metaslab and the count of metaslabs per vdev.
2669 	 *
2670 	 * The default values used below are a good balance between memory
2671 	 * usage (larger metaslab size means more memory needed for loaded
2672 	 * metaslabs; more metaslabs means more memory needed for the
2673 	 * metaslab_t structs), metaslab load time (larger metaslabs take
2674 	 * longer to load), and metaslab sync time (more metaslabs means
2675 	 * more time spent syncing all of them).
2676 	 *
2677 	 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2678 	 * The range of the dimensions are as follows:
2679 	 *
2680 	 *	2^29 <= ms_size  <= 2^34
2681 	 *	  16 <= ms_count <= 131,072
2682 	 *
2683 	 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2684 	 * at least 512MB (2^29) to minimize fragmentation effects when
2685 	 * testing with smaller devices.  However, the count constraint
2686 	 * of at least 16 metaslabs will override this minimum size goal.
2687 	 *
2688 	 * On the upper end of vdev sizes, we aim for a maximum metaslab
2689 	 * size of 16GB.  However, we will cap the total count to 2^17
2690 	 * metaslabs to keep our memory footprint in check and let the
2691 	 * metaslab size grow from there if that limit is hit.
2692 	 *
2693 	 * The net effect of applying above constrains is summarized below.
2694 	 *
2695 	 *   vdev size       metaslab count
2696 	 *  --------------|-----------------
2697 	 *      < 8GB        ~16
2698 	 *  8GB   - 100GB   one per 512MB
2699 	 *  100GB - 3TB     ~200
2700 	 *  3TB   - 2PB     one per 16GB
2701 	 *      > 2PB       ~131,072
2702 	 *  --------------------------------
2703 	 *
2704 	 *  Finally, note that all of the above calculate the initial
2705 	 *  number of metaslabs. Expanding a top-level vdev will result
2706 	 *  in additional metaslabs being allocated making it possible
2707 	 *  to exceed the zfs_vdev_ms_count_limit.
2708 	 */
2709 
2710 	if (ms_count < zfs_vdev_min_ms_count)
2711 		ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2712 	else if (ms_count > zfs_vdev_default_ms_count)
2713 		ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2714 	else
2715 		ms_shift = zfs_vdev_default_ms_shift;
2716 
2717 	if (ms_shift < SPA_MAXBLOCKSHIFT) {
2718 		ms_shift = SPA_MAXBLOCKSHIFT;
2719 	} else if (ms_shift > zfs_vdev_max_ms_shift) {
2720 		ms_shift = zfs_vdev_max_ms_shift;
2721 		/* cap the total count to constrain memory footprint */
2722 		if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2723 			ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
2724 	}
2725 
2726 	vd->vdev_ms_shift = ms_shift;
2727 	ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2728 }
2729 
2730 void
2731 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2732 {
2733 	ASSERT(vd == vd->vdev_top);
2734 	/* indirect vdevs don't have metaslabs or dtls */
2735 	ASSERT(vdev_is_concrete(vd) || flags == 0);
2736 	ASSERT(ISP2(flags));
2737 	ASSERT(spa_writeable(vd->vdev_spa));
2738 
2739 	if (flags & VDD_METASLAB)
2740 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2741 
2742 	if (flags & VDD_DTL)
2743 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2744 
2745 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2746 }
2747 
2748 void
2749 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2750 {
2751 	for (int c = 0; c < vd->vdev_children; c++)
2752 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2753 
2754 	if (vd->vdev_ops->vdev_op_leaf)
2755 		vdev_dirty(vd->vdev_top, flags, vd, txg);
2756 }
2757 
2758 /*
2759  * DTLs.
2760  *
2761  * A vdev's DTL (dirty time log) is the set of transaction groups for which
2762  * the vdev has less than perfect replication.  There are four kinds of DTL:
2763  *
2764  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2765  *
2766  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2767  *
2768  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2769  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2770  *	txgs that was scrubbed.
2771  *
2772  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2773  *	persistent errors or just some device being offline.
2774  *	Unlike the other three, the DTL_OUTAGE map is not generally
2775  *	maintained; it's only computed when needed, typically to
2776  *	determine whether a device can be detached.
2777  *
2778  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2779  * either has the data or it doesn't.
2780  *
2781  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2782  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2783  * if any child is less than fully replicated, then so is its parent.
2784  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2785  * comprising only those txgs which appear in 'maxfaults' or more children;
2786  * those are the txgs we don't have enough replication to read.  For example,
2787  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2788  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2789  * two child DTL_MISSING maps.
2790  *
2791  * It should be clear from the above that to compute the DTLs and outage maps
2792  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2793  * Therefore, that is all we keep on disk.  When loading the pool, or after
2794  * a configuration change, we generate all other DTLs from first principles.
2795  */
2796 void
2797 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2798 {
2799 	range_tree_t *rt = vd->vdev_dtl[t];
2800 
2801 	ASSERT(t < DTL_TYPES);
2802 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2803 	ASSERT(spa_writeable(vd->vdev_spa));
2804 
2805 	mutex_enter(&vd->vdev_dtl_lock);
2806 	if (!range_tree_contains(rt, txg, size))
2807 		range_tree_add(rt, txg, size);
2808 	mutex_exit(&vd->vdev_dtl_lock);
2809 }
2810 
2811 boolean_t
2812 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2813 {
2814 	range_tree_t *rt = vd->vdev_dtl[t];
2815 	boolean_t dirty = B_FALSE;
2816 
2817 	ASSERT(t < DTL_TYPES);
2818 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2819 
2820 	/*
2821 	 * While we are loading the pool, the DTLs have not been loaded yet.
2822 	 * This isn't a problem but it can result in devices being tried
2823 	 * which are known to not have the data.  In which case, the import
2824 	 * is relying on the checksum to ensure that we get the right data.
2825 	 * Note that while importing we are only reading the MOS, which is
2826 	 * always checksummed.
2827 	 */
2828 	mutex_enter(&vd->vdev_dtl_lock);
2829 	if (!range_tree_is_empty(rt))
2830 		dirty = range_tree_contains(rt, txg, size);
2831 	mutex_exit(&vd->vdev_dtl_lock);
2832 
2833 	return (dirty);
2834 }
2835 
2836 boolean_t
2837 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
2838 {
2839 	range_tree_t *rt = vd->vdev_dtl[t];
2840 	boolean_t empty;
2841 
2842 	mutex_enter(&vd->vdev_dtl_lock);
2843 	empty = range_tree_is_empty(rt);
2844 	mutex_exit(&vd->vdev_dtl_lock);
2845 
2846 	return (empty);
2847 }
2848 
2849 /*
2850  * Check if the txg falls within the range which must be
2851  * resilvered.  DVAs outside this range can always be skipped.
2852  */
2853 boolean_t
2854 vdev_default_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
2855     uint64_t phys_birth)
2856 {
2857 	(void) dva, (void) psize;
2858 
2859 	/* Set by sequential resilver. */
2860 	if (phys_birth == TXG_UNKNOWN)
2861 		return (B_TRUE);
2862 
2863 	return (vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1));
2864 }
2865 
2866 /*
2867  * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
2868  */
2869 boolean_t
2870 vdev_dtl_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
2871     uint64_t phys_birth)
2872 {
2873 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2874 
2875 	if (vd->vdev_ops->vdev_op_need_resilver == NULL ||
2876 	    vd->vdev_ops->vdev_op_leaf)
2877 		return (B_TRUE);
2878 
2879 	return (vd->vdev_ops->vdev_op_need_resilver(vd, dva, psize,
2880 	    phys_birth));
2881 }
2882 
2883 /*
2884  * Returns the lowest txg in the DTL range.
2885  */
2886 static uint64_t
2887 vdev_dtl_min(vdev_t *vd)
2888 {
2889 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2890 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2891 	ASSERT0(vd->vdev_children);
2892 
2893 	return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1);
2894 }
2895 
2896 /*
2897  * Returns the highest txg in the DTL.
2898  */
2899 static uint64_t
2900 vdev_dtl_max(vdev_t *vd)
2901 {
2902 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2903 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2904 	ASSERT0(vd->vdev_children);
2905 
2906 	return (range_tree_max(vd->vdev_dtl[DTL_MISSING]));
2907 }
2908 
2909 /*
2910  * Determine if a resilvering vdev should remove any DTL entries from
2911  * its range. If the vdev was resilvering for the entire duration of the
2912  * scan then it should excise that range from its DTLs. Otherwise, this
2913  * vdev is considered partially resilvered and should leave its DTL
2914  * entries intact. The comment in vdev_dtl_reassess() describes how we
2915  * excise the DTLs.
2916  */
2917 static boolean_t
2918 vdev_dtl_should_excise(vdev_t *vd, boolean_t rebuild_done)
2919 {
2920 	ASSERT0(vd->vdev_children);
2921 
2922 	if (vd->vdev_state < VDEV_STATE_DEGRADED)
2923 		return (B_FALSE);
2924 
2925 	if (vd->vdev_resilver_deferred)
2926 		return (B_FALSE);
2927 
2928 	if (range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2929 		return (B_TRUE);
2930 
2931 	if (rebuild_done) {
2932 		vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
2933 		vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
2934 
2935 		/* Rebuild not initiated by attach */
2936 		if (vd->vdev_rebuild_txg == 0)
2937 			return (B_TRUE);
2938 
2939 		/*
2940 		 * When a rebuild completes without error then all missing data
2941 		 * up to the rebuild max txg has been reconstructed and the DTL
2942 		 * is eligible for excision.
2943 		 */
2944 		if (vrp->vrp_rebuild_state == VDEV_REBUILD_COMPLETE &&
2945 		    vdev_dtl_max(vd) <= vrp->vrp_max_txg) {
2946 			ASSERT3U(vrp->vrp_min_txg, <=, vdev_dtl_min(vd));
2947 			ASSERT3U(vrp->vrp_min_txg, <, vd->vdev_rebuild_txg);
2948 			ASSERT3U(vd->vdev_rebuild_txg, <=, vrp->vrp_max_txg);
2949 			return (B_TRUE);
2950 		}
2951 	} else {
2952 		dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan;
2953 		dsl_scan_phys_t *scnp __maybe_unused = &scn->scn_phys;
2954 
2955 		/* Resilver not initiated by attach */
2956 		if (vd->vdev_resilver_txg == 0)
2957 			return (B_TRUE);
2958 
2959 		/*
2960 		 * When a resilver is initiated the scan will assign the
2961 		 * scn_max_txg value to the highest txg value that exists
2962 		 * in all DTLs. If this device's max DTL is not part of this
2963 		 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
2964 		 * then it is not eligible for excision.
2965 		 */
2966 		if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2967 			ASSERT3U(scnp->scn_min_txg, <=, vdev_dtl_min(vd));
2968 			ASSERT3U(scnp->scn_min_txg, <, vd->vdev_resilver_txg);
2969 			ASSERT3U(vd->vdev_resilver_txg, <=, scnp->scn_max_txg);
2970 			return (B_TRUE);
2971 		}
2972 	}
2973 
2974 	return (B_FALSE);
2975 }
2976 
2977 /*
2978  * Reassess DTLs after a config change or scrub completion. If txg == 0 no
2979  * write operations will be issued to the pool.
2980  */
2981 void
2982 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg,
2983     boolean_t scrub_done, boolean_t rebuild_done)
2984 {
2985 	spa_t *spa = vd->vdev_spa;
2986 	avl_tree_t reftree;
2987 	int minref;
2988 
2989 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2990 
2991 	for (int c = 0; c < vd->vdev_children; c++)
2992 		vdev_dtl_reassess(vd->vdev_child[c], txg,
2993 		    scrub_txg, scrub_done, rebuild_done);
2994 
2995 	if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
2996 		return;
2997 
2998 	if (vd->vdev_ops->vdev_op_leaf) {
2999 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
3000 		vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3001 		boolean_t check_excise = B_FALSE;
3002 		boolean_t wasempty = B_TRUE;
3003 
3004 		mutex_enter(&vd->vdev_dtl_lock);
3005 
3006 		/*
3007 		 * If requested, pretend the scan or rebuild completed cleanly.
3008 		 */
3009 		if (zfs_scan_ignore_errors) {
3010 			if (scn != NULL)
3011 				scn->scn_phys.scn_errors = 0;
3012 			if (vr != NULL)
3013 				vr->vr_rebuild_phys.vrp_errors = 0;
3014 		}
3015 
3016 		if (scrub_txg != 0 &&
3017 		    !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3018 			wasempty = B_FALSE;
3019 			zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
3020 			    "dtl:%llu/%llu errors:%llu",
3021 			    (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg,
3022 			    (u_longlong_t)scrub_txg, spa->spa_scrub_started,
3023 			    (u_longlong_t)vdev_dtl_min(vd),
3024 			    (u_longlong_t)vdev_dtl_max(vd),
3025 			    (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0));
3026 		}
3027 
3028 		/*
3029 		 * If we've completed a scrub/resilver or a rebuild cleanly
3030 		 * then determine if this vdev should remove any DTLs. We
3031 		 * only want to excise regions on vdevs that were available
3032 		 * during the entire duration of this scan.
3033 		 */
3034 		if (rebuild_done &&
3035 		    vr != NULL && vr->vr_rebuild_phys.vrp_errors == 0) {
3036 			check_excise = B_TRUE;
3037 		} else {
3038 			if (spa->spa_scrub_started ||
3039 			    (scn != NULL && scn->scn_phys.scn_errors == 0)) {
3040 				check_excise = B_TRUE;
3041 			}
3042 		}
3043 
3044 		if (scrub_txg && check_excise &&
3045 		    vdev_dtl_should_excise(vd, rebuild_done)) {
3046 			/*
3047 			 * We completed a scrub, resilver or rebuild up to
3048 			 * scrub_txg.  If we did it without rebooting, then
3049 			 * the scrub dtl will be valid, so excise the old
3050 			 * region and fold in the scrub dtl.  Otherwise,
3051 			 * leave the dtl as-is if there was an error.
3052 			 *
3053 			 * There's little trick here: to excise the beginning
3054 			 * of the DTL_MISSING map, we put it into a reference
3055 			 * tree and then add a segment with refcnt -1 that
3056 			 * covers the range [0, scrub_txg).  This means
3057 			 * that each txg in that range has refcnt -1 or 0.
3058 			 * We then add DTL_SCRUB with a refcnt of 2, so that
3059 			 * entries in the range [0, scrub_txg) will have a
3060 			 * positive refcnt -- either 1 or 2.  We then convert
3061 			 * the reference tree into the new DTL_MISSING map.
3062 			 */
3063 			space_reftree_create(&reftree);
3064 			space_reftree_add_map(&reftree,
3065 			    vd->vdev_dtl[DTL_MISSING], 1);
3066 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
3067 			space_reftree_add_map(&reftree,
3068 			    vd->vdev_dtl[DTL_SCRUB], 2);
3069 			space_reftree_generate_map(&reftree,
3070 			    vd->vdev_dtl[DTL_MISSING], 1);
3071 			space_reftree_destroy(&reftree);
3072 
3073 			if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3074 				zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
3075 				    (u_longlong_t)vdev_dtl_min(vd),
3076 				    (u_longlong_t)vdev_dtl_max(vd));
3077 			} else if (!wasempty) {
3078 				zfs_dbgmsg("DTL_MISSING is now empty");
3079 			}
3080 		}
3081 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
3082 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3083 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
3084 		if (scrub_done)
3085 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
3086 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
3087 		if (!vdev_readable(vd))
3088 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
3089 		else
3090 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3091 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
3092 
3093 		/*
3094 		 * If the vdev was resilvering or rebuilding and no longer
3095 		 * has any DTLs then reset the appropriate flag and dirty
3096 		 * the top level so that we persist the change.
3097 		 */
3098 		if (txg != 0 &&
3099 		    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3100 		    range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) {
3101 			if (vd->vdev_rebuild_txg != 0) {
3102 				vd->vdev_rebuild_txg = 0;
3103 				vdev_config_dirty(vd->vdev_top);
3104 			} else if (vd->vdev_resilver_txg != 0) {
3105 				vd->vdev_resilver_txg = 0;
3106 				vdev_config_dirty(vd->vdev_top);
3107 			}
3108 		}
3109 
3110 		mutex_exit(&vd->vdev_dtl_lock);
3111 
3112 		if (txg != 0)
3113 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
3114 		return;
3115 	}
3116 
3117 	mutex_enter(&vd->vdev_dtl_lock);
3118 	for (int t = 0; t < DTL_TYPES; t++) {
3119 		/* account for child's outage in parent's missing map */
3120 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
3121 		if (t == DTL_SCRUB)
3122 			continue;			/* leaf vdevs only */
3123 		if (t == DTL_PARTIAL)
3124 			minref = 1;			/* i.e. non-zero */
3125 		else if (vdev_get_nparity(vd) != 0)
3126 			minref = vdev_get_nparity(vd) + 1; /* RAID-Z, dRAID */
3127 		else
3128 			minref = vd->vdev_children;	/* any kind of mirror */
3129 		space_reftree_create(&reftree);
3130 		for (int c = 0; c < vd->vdev_children; c++) {
3131 			vdev_t *cvd = vd->vdev_child[c];
3132 			mutex_enter(&cvd->vdev_dtl_lock);
3133 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
3134 			mutex_exit(&cvd->vdev_dtl_lock);
3135 		}
3136 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
3137 		space_reftree_destroy(&reftree);
3138 	}
3139 	mutex_exit(&vd->vdev_dtl_lock);
3140 }
3141 
3142 int
3143 vdev_dtl_load(vdev_t *vd)
3144 {
3145 	spa_t *spa = vd->vdev_spa;
3146 	objset_t *mos = spa->spa_meta_objset;
3147 	range_tree_t *rt;
3148 	int error = 0;
3149 
3150 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
3151 		ASSERT(vdev_is_concrete(vd));
3152 
3153 		/*
3154 		 * If the dtl cannot be sync'd there is no need to open it.
3155 		 */
3156 		if (spa->spa_mode == SPA_MODE_READ && !spa->spa_read_spacemaps)
3157 			return (0);
3158 
3159 		error = space_map_open(&vd->vdev_dtl_sm, mos,
3160 		    vd->vdev_dtl_object, 0, -1ULL, 0);
3161 		if (error)
3162 			return (error);
3163 		ASSERT(vd->vdev_dtl_sm != NULL);
3164 
3165 		rt = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3166 		error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC);
3167 		if (error == 0) {
3168 			mutex_enter(&vd->vdev_dtl_lock);
3169 			range_tree_walk(rt, range_tree_add,
3170 			    vd->vdev_dtl[DTL_MISSING]);
3171 			mutex_exit(&vd->vdev_dtl_lock);
3172 		}
3173 
3174 		range_tree_vacate(rt, NULL, NULL);
3175 		range_tree_destroy(rt);
3176 
3177 		return (error);
3178 	}
3179 
3180 	for (int c = 0; c < vd->vdev_children; c++) {
3181 		error = vdev_dtl_load(vd->vdev_child[c]);
3182 		if (error != 0)
3183 			break;
3184 	}
3185 
3186 	return (error);
3187 }
3188 
3189 static void
3190 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
3191 {
3192 	spa_t *spa = vd->vdev_spa;
3193 	objset_t *mos = spa->spa_meta_objset;
3194 	vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
3195 	const char *string;
3196 
3197 	ASSERT(alloc_bias != VDEV_BIAS_NONE);
3198 
3199 	string =
3200 	    (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
3201 	    (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
3202 	    (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
3203 
3204 	ASSERT(string != NULL);
3205 	VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
3206 	    1, strlen(string) + 1, string, tx));
3207 
3208 	if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
3209 		spa_activate_allocation_classes(spa, tx);
3210 	}
3211 }
3212 
3213 void
3214 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
3215 {
3216 	spa_t *spa = vd->vdev_spa;
3217 
3218 	VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
3219 	VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3220 	    zapobj, tx));
3221 }
3222 
3223 uint64_t
3224 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
3225 {
3226 	spa_t *spa = vd->vdev_spa;
3227 	uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
3228 	    DMU_OT_NONE, 0, tx);
3229 
3230 	ASSERT(zap != 0);
3231 	VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3232 	    zap, tx));
3233 
3234 	return (zap);
3235 }
3236 
3237 void
3238 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
3239 {
3240 	if (vd->vdev_ops != &vdev_hole_ops &&
3241 	    vd->vdev_ops != &vdev_missing_ops &&
3242 	    vd->vdev_ops != &vdev_root_ops &&
3243 	    !vd->vdev_top->vdev_removing) {
3244 		if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
3245 			vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
3246 		}
3247 		if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
3248 			vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
3249 			if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
3250 				vdev_zap_allocation_data(vd, tx);
3251 		}
3252 	}
3253 
3254 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
3255 		vdev_construct_zaps(vd->vdev_child[i], tx);
3256 	}
3257 }
3258 
3259 static void
3260 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
3261 {
3262 	spa_t *spa = vd->vdev_spa;
3263 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
3264 	objset_t *mos = spa->spa_meta_objset;
3265 	range_tree_t *rtsync;
3266 	dmu_tx_t *tx;
3267 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
3268 
3269 	ASSERT(vdev_is_concrete(vd));
3270 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3271 
3272 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3273 
3274 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
3275 		mutex_enter(&vd->vdev_dtl_lock);
3276 		space_map_free(vd->vdev_dtl_sm, tx);
3277 		space_map_close(vd->vdev_dtl_sm);
3278 		vd->vdev_dtl_sm = NULL;
3279 		mutex_exit(&vd->vdev_dtl_lock);
3280 
3281 		/*
3282 		 * We only destroy the leaf ZAP for detached leaves or for
3283 		 * removed log devices. Removed data devices handle leaf ZAP
3284 		 * cleanup later, once cancellation is no longer possible.
3285 		 */
3286 		if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
3287 		    vd->vdev_top->vdev_islog)) {
3288 			vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
3289 			vd->vdev_leaf_zap = 0;
3290 		}
3291 
3292 		dmu_tx_commit(tx);
3293 		return;
3294 	}
3295 
3296 	if (vd->vdev_dtl_sm == NULL) {
3297 		uint64_t new_object;
3298 
3299 		new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx);
3300 		VERIFY3U(new_object, !=, 0);
3301 
3302 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
3303 		    0, -1ULL, 0));
3304 		ASSERT(vd->vdev_dtl_sm != NULL);
3305 	}
3306 
3307 	rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3308 
3309 	mutex_enter(&vd->vdev_dtl_lock);
3310 	range_tree_walk(rt, range_tree_add, rtsync);
3311 	mutex_exit(&vd->vdev_dtl_lock);
3312 
3313 	space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx);
3314 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
3315 	range_tree_vacate(rtsync, NULL, NULL);
3316 
3317 	range_tree_destroy(rtsync);
3318 
3319 	/*
3320 	 * If the object for the space map has changed then dirty
3321 	 * the top level so that we update the config.
3322 	 */
3323 	if (object != space_map_object(vd->vdev_dtl_sm)) {
3324 		vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
3325 		    "new object %llu", (u_longlong_t)txg, spa_name(spa),
3326 		    (u_longlong_t)object,
3327 		    (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
3328 		vdev_config_dirty(vd->vdev_top);
3329 	}
3330 
3331 	dmu_tx_commit(tx);
3332 }
3333 
3334 /*
3335  * Determine whether the specified vdev can be offlined/detached/removed
3336  * without losing data.
3337  */
3338 boolean_t
3339 vdev_dtl_required(vdev_t *vd)
3340 {
3341 	spa_t *spa = vd->vdev_spa;
3342 	vdev_t *tvd = vd->vdev_top;
3343 	uint8_t cant_read = vd->vdev_cant_read;
3344 	boolean_t required;
3345 
3346 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3347 
3348 	if (vd == spa->spa_root_vdev || vd == tvd)
3349 		return (B_TRUE);
3350 
3351 	/*
3352 	 * Temporarily mark the device as unreadable, and then determine
3353 	 * whether this results in any DTL outages in the top-level vdev.
3354 	 * If not, we can safely offline/detach/remove the device.
3355 	 */
3356 	vd->vdev_cant_read = B_TRUE;
3357 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE);
3358 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
3359 	vd->vdev_cant_read = cant_read;
3360 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE);
3361 
3362 	if (!required && zio_injection_enabled) {
3363 		required = !!zio_handle_device_injection(vd, NULL,
3364 		    SET_ERROR(ECHILD));
3365 	}
3366 
3367 	return (required);
3368 }
3369 
3370 /*
3371  * Determine if resilver is needed, and if so the txg range.
3372  */
3373 boolean_t
3374 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
3375 {
3376 	boolean_t needed = B_FALSE;
3377 	uint64_t thismin = UINT64_MAX;
3378 	uint64_t thismax = 0;
3379 
3380 	if (vd->vdev_children == 0) {
3381 		mutex_enter(&vd->vdev_dtl_lock);
3382 		if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3383 		    vdev_writeable(vd)) {
3384 
3385 			thismin = vdev_dtl_min(vd);
3386 			thismax = vdev_dtl_max(vd);
3387 			needed = B_TRUE;
3388 		}
3389 		mutex_exit(&vd->vdev_dtl_lock);
3390 	} else {
3391 		for (int c = 0; c < vd->vdev_children; c++) {
3392 			vdev_t *cvd = vd->vdev_child[c];
3393 			uint64_t cmin, cmax;
3394 
3395 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
3396 				thismin = MIN(thismin, cmin);
3397 				thismax = MAX(thismax, cmax);
3398 				needed = B_TRUE;
3399 			}
3400 		}
3401 	}
3402 
3403 	if (needed && minp) {
3404 		*minp = thismin;
3405 		*maxp = thismax;
3406 	}
3407 	return (needed);
3408 }
3409 
3410 /*
3411  * Gets the checkpoint space map object from the vdev's ZAP.  On success sm_obj
3412  * will contain either the checkpoint spacemap object or zero if none exists.
3413  * All other errors are returned to the caller.
3414  */
3415 int
3416 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj)
3417 {
3418 	ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
3419 
3420 	if (vd->vdev_top_zap == 0) {
3421 		*sm_obj = 0;
3422 		return (0);
3423 	}
3424 
3425 	int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
3426 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj);
3427 	if (error == ENOENT) {
3428 		*sm_obj = 0;
3429 		error = 0;
3430 	}
3431 
3432 	return (error);
3433 }
3434 
3435 int
3436 vdev_load(vdev_t *vd)
3437 {
3438 	int children = vd->vdev_children;
3439 	int error = 0;
3440 	taskq_t *tq = NULL;
3441 
3442 	/*
3443 	 * It's only worthwhile to use the taskq for the root vdev, because the
3444 	 * slow part is metaslab_init, and that only happens for top-level
3445 	 * vdevs.
3446 	 */
3447 	if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) {
3448 		tq = taskq_create("vdev_load", children, minclsyspri,
3449 		    children, children, TASKQ_PREPOPULATE);
3450 	}
3451 
3452 	/*
3453 	 * Recursively load all children.
3454 	 */
3455 	for (int c = 0; c < vd->vdev_children; c++) {
3456 		vdev_t *cvd = vd->vdev_child[c];
3457 
3458 		if (tq == NULL || vdev_uses_zvols(cvd)) {
3459 			cvd->vdev_load_error = vdev_load(cvd);
3460 		} else {
3461 			VERIFY(taskq_dispatch(tq, vdev_load_child,
3462 			    cvd, TQ_SLEEP) != TASKQID_INVALID);
3463 		}
3464 	}
3465 
3466 	if (tq != NULL) {
3467 		taskq_wait(tq);
3468 		taskq_destroy(tq);
3469 	}
3470 
3471 	for (int c = 0; c < vd->vdev_children; c++) {
3472 		int error = vd->vdev_child[c]->vdev_load_error;
3473 
3474 		if (error != 0)
3475 			return (error);
3476 	}
3477 
3478 	vdev_set_deflate_ratio(vd);
3479 
3480 	/*
3481 	 * On spa_load path, grab the allocation bias from our zap
3482 	 */
3483 	if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3484 		spa_t *spa = vd->vdev_spa;
3485 		char bias_str[64];
3486 
3487 		error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3488 		    VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
3489 		    bias_str);
3490 		if (error == 0) {
3491 			ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
3492 			vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
3493 		} else if (error != ENOENT) {
3494 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3495 			    VDEV_AUX_CORRUPT_DATA);
3496 			vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) "
3497 			    "failed [error=%d]",
3498 			    (u_longlong_t)vd->vdev_top_zap, error);
3499 			return (error);
3500 		}
3501 	}
3502 
3503 	/*
3504 	 * Load any rebuild state from the top-level vdev zap.
3505 	 */
3506 	if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3507 		error = vdev_rebuild_load(vd);
3508 		if (error && error != ENOTSUP) {
3509 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3510 			    VDEV_AUX_CORRUPT_DATA);
3511 			vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load "
3512 			    "failed [error=%d]", error);
3513 			return (error);
3514 		}
3515 	}
3516 
3517 	/*
3518 	 * If this is a top-level vdev, initialize its metaslabs.
3519 	 */
3520 	if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
3521 		vdev_metaslab_group_create(vd);
3522 
3523 		if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
3524 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3525 			    VDEV_AUX_CORRUPT_DATA);
3526 			vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
3527 			    "asize=%llu", (u_longlong_t)vd->vdev_ashift,
3528 			    (u_longlong_t)vd->vdev_asize);
3529 			return (SET_ERROR(ENXIO));
3530 		}
3531 
3532 		error = vdev_metaslab_init(vd, 0);
3533 		if (error != 0) {
3534 			vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
3535 			    "[error=%d]", error);
3536 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3537 			    VDEV_AUX_CORRUPT_DATA);
3538 			return (error);
3539 		}
3540 
3541 		uint64_t checkpoint_sm_obj;
3542 		error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj);
3543 		if (error == 0 && checkpoint_sm_obj != 0) {
3544 			objset_t *mos = spa_meta_objset(vd->vdev_spa);
3545 			ASSERT(vd->vdev_asize != 0);
3546 			ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
3547 
3548 			error = space_map_open(&vd->vdev_checkpoint_sm,
3549 			    mos, checkpoint_sm_obj, 0, vd->vdev_asize,
3550 			    vd->vdev_ashift);
3551 			if (error != 0) {
3552 				vdev_dbgmsg(vd, "vdev_load: space_map_open "
3553 				    "failed for checkpoint spacemap (obj %llu) "
3554 				    "[error=%d]",
3555 				    (u_longlong_t)checkpoint_sm_obj, error);
3556 				return (error);
3557 			}
3558 			ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
3559 
3560 			/*
3561 			 * Since the checkpoint_sm contains free entries
3562 			 * exclusively we can use space_map_allocated() to
3563 			 * indicate the cumulative checkpointed space that
3564 			 * has been freed.
3565 			 */
3566 			vd->vdev_stat.vs_checkpoint_space =
3567 			    -space_map_allocated(vd->vdev_checkpoint_sm);
3568 			vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
3569 			    vd->vdev_stat.vs_checkpoint_space;
3570 		} else if (error != 0) {
3571 			vdev_dbgmsg(vd, "vdev_load: failed to retrieve "
3572 			    "checkpoint space map object from vdev ZAP "
3573 			    "[error=%d]", error);
3574 			return (error);
3575 		}
3576 	}
3577 
3578 	/*
3579 	 * If this is a leaf vdev, load its DTL.
3580 	 */
3581 	if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
3582 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3583 		    VDEV_AUX_CORRUPT_DATA);
3584 		vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
3585 		    "[error=%d]", error);
3586 		return (error);
3587 	}
3588 
3589 	uint64_t obsolete_sm_object;
3590 	error = vdev_obsolete_sm_object(vd, &obsolete_sm_object);
3591 	if (error == 0 && obsolete_sm_object != 0) {
3592 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
3593 		ASSERT(vd->vdev_asize != 0);
3594 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
3595 
3596 		if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
3597 		    obsolete_sm_object, 0, vd->vdev_asize, 0))) {
3598 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3599 			    VDEV_AUX_CORRUPT_DATA);
3600 			vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
3601 			    "obsolete spacemap (obj %llu) [error=%d]",
3602 			    (u_longlong_t)obsolete_sm_object, error);
3603 			return (error);
3604 		}
3605 	} else if (error != 0) {
3606 		vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete "
3607 		    "space map object from vdev ZAP [error=%d]", error);
3608 		return (error);
3609 	}
3610 
3611 	return (0);
3612 }
3613 
3614 /*
3615  * The special vdev case is used for hot spares and l2cache devices.  Its
3616  * sole purpose it to set the vdev state for the associated vdev.  To do this,
3617  * we make sure that we can open the underlying device, then try to read the
3618  * label, and make sure that the label is sane and that it hasn't been
3619  * repurposed to another pool.
3620  */
3621 int
3622 vdev_validate_aux(vdev_t *vd)
3623 {
3624 	nvlist_t *label;
3625 	uint64_t guid, version;
3626 	uint64_t state;
3627 
3628 	if (!vdev_readable(vd))
3629 		return (0);
3630 
3631 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
3632 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3633 		    VDEV_AUX_CORRUPT_DATA);
3634 		return (-1);
3635 	}
3636 
3637 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
3638 	    !SPA_VERSION_IS_SUPPORTED(version) ||
3639 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
3640 	    guid != vd->vdev_guid ||
3641 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
3642 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3643 		    VDEV_AUX_CORRUPT_DATA);
3644 		nvlist_free(label);
3645 		return (-1);
3646 	}
3647 
3648 	/*
3649 	 * We don't actually check the pool state here.  If it's in fact in
3650 	 * use by another pool, we update this fact on the fly when requested.
3651 	 */
3652 	nvlist_free(label);
3653 	return (0);
3654 }
3655 
3656 static void
3657 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx)
3658 {
3659 	objset_t *mos = spa_meta_objset(vd->vdev_spa);
3660 
3661 	if (vd->vdev_top_zap == 0)
3662 		return;
3663 
3664 	uint64_t object = 0;
3665 	int err = zap_lookup(mos, vd->vdev_top_zap,
3666 	    VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object);
3667 	if (err == ENOENT)
3668 		return;
3669 	VERIFY0(err);
3670 
3671 	VERIFY0(dmu_object_free(mos, object, tx));
3672 	VERIFY0(zap_remove(mos, vd->vdev_top_zap,
3673 	    VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx));
3674 }
3675 
3676 /*
3677  * Free the objects used to store this vdev's spacemaps, and the array
3678  * that points to them.
3679  */
3680 void
3681 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
3682 {
3683 	if (vd->vdev_ms_array == 0)
3684 		return;
3685 
3686 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
3687 	uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
3688 	size_t array_bytes = array_count * sizeof (uint64_t);
3689 	uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
3690 	VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
3691 	    array_bytes, smobj_array, 0));
3692 
3693 	for (uint64_t i = 0; i < array_count; i++) {
3694 		uint64_t smobj = smobj_array[i];
3695 		if (smobj == 0)
3696 			continue;
3697 
3698 		space_map_free_obj(mos, smobj, tx);
3699 	}
3700 
3701 	kmem_free(smobj_array, array_bytes);
3702 	VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
3703 	vdev_destroy_ms_flush_data(vd, tx);
3704 	vd->vdev_ms_array = 0;
3705 }
3706 
3707 static void
3708 vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
3709 {
3710 	spa_t *spa = vd->vdev_spa;
3711 
3712 	ASSERT(vd->vdev_islog);
3713 	ASSERT(vd == vd->vdev_top);
3714 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
3715 
3716 	dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
3717 
3718 	vdev_destroy_spacemaps(vd, tx);
3719 	if (vd->vdev_top_zap != 0) {
3720 		vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
3721 		vd->vdev_top_zap = 0;
3722 	}
3723 
3724 	dmu_tx_commit(tx);
3725 }
3726 
3727 void
3728 vdev_sync_done(vdev_t *vd, uint64_t txg)
3729 {
3730 	metaslab_t *msp;
3731 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
3732 
3733 	ASSERT(vdev_is_concrete(vd));
3734 
3735 	while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
3736 	    != NULL)
3737 		metaslab_sync_done(msp, txg);
3738 
3739 	if (reassess) {
3740 		metaslab_sync_reassess(vd->vdev_mg);
3741 		if (vd->vdev_log_mg != NULL)
3742 			metaslab_sync_reassess(vd->vdev_log_mg);
3743 	}
3744 }
3745 
3746 void
3747 vdev_sync(vdev_t *vd, uint64_t txg)
3748 {
3749 	spa_t *spa = vd->vdev_spa;
3750 	vdev_t *lvd;
3751 	metaslab_t *msp;
3752 
3753 	ASSERT3U(txg, ==, spa->spa_syncing_txg);
3754 	dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3755 	if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
3756 		ASSERT(vd->vdev_removing ||
3757 		    vd->vdev_ops == &vdev_indirect_ops);
3758 
3759 		vdev_indirect_sync_obsolete(vd, tx);
3760 
3761 		/*
3762 		 * If the vdev is indirect, it can't have dirty
3763 		 * metaslabs or DTLs.
3764 		 */
3765 		if (vd->vdev_ops == &vdev_indirect_ops) {
3766 			ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
3767 			ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
3768 			dmu_tx_commit(tx);
3769 			return;
3770 		}
3771 	}
3772 
3773 	ASSERT(vdev_is_concrete(vd));
3774 
3775 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
3776 	    !vd->vdev_removing) {
3777 		ASSERT(vd == vd->vdev_top);
3778 		ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3779 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
3780 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
3781 		ASSERT(vd->vdev_ms_array != 0);
3782 		vdev_config_dirty(vd);
3783 	}
3784 
3785 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
3786 		metaslab_sync(msp, txg);
3787 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
3788 	}
3789 
3790 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
3791 		vdev_dtl_sync(lvd, txg);
3792 
3793 	/*
3794 	 * If this is an empty log device being removed, destroy the
3795 	 * metadata associated with it.
3796 	 */
3797 	if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
3798 		vdev_remove_empty_log(vd, txg);
3799 
3800 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
3801 	dmu_tx_commit(tx);
3802 }
3803 
3804 uint64_t
3805 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
3806 {
3807 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
3808 }
3809 
3810 /*
3811  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
3812  * not be opened, and no I/O is attempted.
3813  */
3814 int
3815 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3816 {
3817 	vdev_t *vd, *tvd;
3818 
3819 	spa_vdev_state_enter(spa, SCL_NONE);
3820 
3821 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3822 		return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
3823 
3824 	if (!vd->vdev_ops->vdev_op_leaf)
3825 		return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
3826 
3827 	tvd = vd->vdev_top;
3828 
3829 	/*
3830 	 * If user did a 'zpool offline -f' then make the fault persist across
3831 	 * reboots.
3832 	 */
3833 	if (aux == VDEV_AUX_EXTERNAL_PERSIST) {
3834 		/*
3835 		 * There are two kinds of forced faults: temporary and
3836 		 * persistent.  Temporary faults go away at pool import, while
3837 		 * persistent faults stay set.  Both types of faults can be
3838 		 * cleared with a zpool clear.
3839 		 *
3840 		 * We tell if a vdev is persistently faulted by looking at the
3841 		 * ZPOOL_CONFIG_AUX_STATE nvpair.  If it's set to "external" at
3842 		 * import then it's a persistent fault.  Otherwise, it's
3843 		 * temporary.  We get ZPOOL_CONFIG_AUX_STATE set to "external"
3844 		 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL.  This
3845 		 * tells vdev_config_generate() (which gets run later) to set
3846 		 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
3847 		 */
3848 		vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
3849 		vd->vdev_tmpoffline = B_FALSE;
3850 		aux = VDEV_AUX_EXTERNAL;
3851 	} else {
3852 		vd->vdev_tmpoffline = B_TRUE;
3853 	}
3854 
3855 	/*
3856 	 * We don't directly use the aux state here, but if we do a
3857 	 * vdev_reopen(), we need this value to be present to remember why we
3858 	 * were faulted.
3859 	 */
3860 	vd->vdev_label_aux = aux;
3861 
3862 	/*
3863 	 * Faulted state takes precedence over degraded.
3864 	 */
3865 	vd->vdev_delayed_close = B_FALSE;
3866 	vd->vdev_faulted = 1ULL;
3867 	vd->vdev_degraded = 0ULL;
3868 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
3869 
3870 	/*
3871 	 * If this device has the only valid copy of the data, then
3872 	 * back off and simply mark the vdev as degraded instead.
3873 	 */
3874 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
3875 		vd->vdev_degraded = 1ULL;
3876 		vd->vdev_faulted = 0ULL;
3877 
3878 		/*
3879 		 * If we reopen the device and it's not dead, only then do we
3880 		 * mark it degraded.
3881 		 */
3882 		vdev_reopen(tvd);
3883 
3884 		if (vdev_readable(vd))
3885 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
3886 	}
3887 
3888 	return (spa_vdev_state_exit(spa, vd, 0));
3889 }
3890 
3891 /*
3892  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
3893  * user that something is wrong.  The vdev continues to operate as normal as far
3894  * as I/O is concerned.
3895  */
3896 int
3897 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3898 {
3899 	vdev_t *vd;
3900 
3901 	spa_vdev_state_enter(spa, SCL_NONE);
3902 
3903 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3904 		return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
3905 
3906 	if (!vd->vdev_ops->vdev_op_leaf)
3907 		return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
3908 
3909 	/*
3910 	 * If the vdev is already faulted, then don't do anything.
3911 	 */
3912 	if (vd->vdev_faulted || vd->vdev_degraded)
3913 		return (spa_vdev_state_exit(spa, NULL, 0));
3914 
3915 	vd->vdev_degraded = 1ULL;
3916 	if (!vdev_is_dead(vd))
3917 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3918 		    aux);
3919 
3920 	return (spa_vdev_state_exit(spa, vd, 0));
3921 }
3922 
3923 /*
3924  * Online the given vdev.
3925  *
3926  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
3927  * spare device should be detached when the device finishes resilvering.
3928  * Second, the online should be treated like a 'test' online case, so no FMA
3929  * events are generated if the device fails to open.
3930  */
3931 int
3932 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
3933 {
3934 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
3935 	boolean_t wasoffline;
3936 	vdev_state_t oldstate;
3937 
3938 	spa_vdev_state_enter(spa, SCL_NONE);
3939 
3940 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3941 		return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
3942 
3943 	if (!vd->vdev_ops->vdev_op_leaf)
3944 		return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
3945 
3946 	wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
3947 	oldstate = vd->vdev_state;
3948 
3949 	tvd = vd->vdev_top;
3950 	vd->vdev_offline = B_FALSE;
3951 	vd->vdev_tmpoffline = B_FALSE;
3952 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3953 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3954 
3955 	/* XXX - L2ARC 1.0 does not support expansion */
3956 	if (!vd->vdev_aux) {
3957 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3958 			pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) ||
3959 			    spa->spa_autoexpand);
3960 		vd->vdev_expansion_time = gethrestime_sec();
3961 	}
3962 
3963 	vdev_reopen(tvd);
3964 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
3965 
3966 	if (!vd->vdev_aux) {
3967 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3968 			pvd->vdev_expanding = B_FALSE;
3969 	}
3970 
3971 	if (newstate)
3972 		*newstate = vd->vdev_state;
3973 	if ((flags & ZFS_ONLINE_UNSPARE) &&
3974 	    !vdev_is_dead(vd) && vd->vdev_parent &&
3975 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3976 	    vd->vdev_parent->vdev_child[0] == vd)
3977 		vd->vdev_unspare = B_TRUE;
3978 
3979 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3980 
3981 		/* XXX - L2ARC 1.0 does not support expansion */
3982 		if (vd->vdev_aux)
3983 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3984 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3985 	}
3986 
3987 	/* Restart initializing if necessary */
3988 	mutex_enter(&vd->vdev_initialize_lock);
3989 	if (vdev_writeable(vd) &&
3990 	    vd->vdev_initialize_thread == NULL &&
3991 	    vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
3992 		(void) vdev_initialize(vd);
3993 	}
3994 	mutex_exit(&vd->vdev_initialize_lock);
3995 
3996 	/*
3997 	 * Restart trimming if necessary. We do not restart trimming for cache
3998 	 * devices here. This is triggered by l2arc_rebuild_vdev()
3999 	 * asynchronously for the whole device or in l2arc_evict() as it evicts
4000 	 * space for upcoming writes.
4001 	 */
4002 	mutex_enter(&vd->vdev_trim_lock);
4003 	if (vdev_writeable(vd) && !vd->vdev_isl2cache &&
4004 	    vd->vdev_trim_thread == NULL &&
4005 	    vd->vdev_trim_state == VDEV_TRIM_ACTIVE) {
4006 		(void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial,
4007 		    vd->vdev_trim_secure);
4008 	}
4009 	mutex_exit(&vd->vdev_trim_lock);
4010 
4011 	if (wasoffline ||
4012 	    (oldstate < VDEV_STATE_DEGRADED &&
4013 	    vd->vdev_state >= VDEV_STATE_DEGRADED))
4014 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
4015 
4016 	return (spa_vdev_state_exit(spa, vd, 0));
4017 }
4018 
4019 static int
4020 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
4021 {
4022 	vdev_t *vd, *tvd;
4023 	int error = 0;
4024 	uint64_t generation;
4025 	metaslab_group_t *mg;
4026 
4027 top:
4028 	spa_vdev_state_enter(spa, SCL_ALLOC);
4029 
4030 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4031 		return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4032 
4033 	if (!vd->vdev_ops->vdev_op_leaf)
4034 		return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4035 
4036 	if (vd->vdev_ops == &vdev_draid_spare_ops)
4037 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4038 
4039 	tvd = vd->vdev_top;
4040 	mg = tvd->vdev_mg;
4041 	generation = spa->spa_config_generation + 1;
4042 
4043 	/*
4044 	 * If the device isn't already offline, try to offline it.
4045 	 */
4046 	if (!vd->vdev_offline) {
4047 		/*
4048 		 * If this device has the only valid copy of some data,
4049 		 * don't allow it to be offlined. Log devices are always
4050 		 * expendable.
4051 		 */
4052 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4053 		    vdev_dtl_required(vd))
4054 			return (spa_vdev_state_exit(spa, NULL,
4055 			    SET_ERROR(EBUSY)));
4056 
4057 		/*
4058 		 * If the top-level is a slog and it has had allocations
4059 		 * then proceed.  We check that the vdev's metaslab group
4060 		 * is not NULL since it's possible that we may have just
4061 		 * added this vdev but not yet initialized its metaslabs.
4062 		 */
4063 		if (tvd->vdev_islog && mg != NULL) {
4064 			/*
4065 			 * Prevent any future allocations.
4066 			 */
4067 			ASSERT3P(tvd->vdev_log_mg, ==, NULL);
4068 			metaslab_group_passivate(mg);
4069 			(void) spa_vdev_state_exit(spa, vd, 0);
4070 
4071 			error = spa_reset_logs(spa);
4072 
4073 			/*
4074 			 * If the log device was successfully reset but has
4075 			 * checkpointed data, do not offline it.
4076 			 */
4077 			if (error == 0 &&
4078 			    tvd->vdev_checkpoint_sm != NULL) {
4079 				ASSERT3U(space_map_allocated(
4080 				    tvd->vdev_checkpoint_sm), !=, 0);
4081 				error = ZFS_ERR_CHECKPOINT_EXISTS;
4082 			}
4083 
4084 			spa_vdev_state_enter(spa, SCL_ALLOC);
4085 
4086 			/*
4087 			 * Check to see if the config has changed.
4088 			 */
4089 			if (error || generation != spa->spa_config_generation) {
4090 				metaslab_group_activate(mg);
4091 				if (error)
4092 					return (spa_vdev_state_exit(spa,
4093 					    vd, error));
4094 				(void) spa_vdev_state_exit(spa, vd, 0);
4095 				goto top;
4096 			}
4097 			ASSERT0(tvd->vdev_stat.vs_alloc);
4098 		}
4099 
4100 		/*
4101 		 * Offline this device and reopen its top-level vdev.
4102 		 * If the top-level vdev is a log device then just offline
4103 		 * it. Otherwise, if this action results in the top-level
4104 		 * vdev becoming unusable, undo it and fail the request.
4105 		 */
4106 		vd->vdev_offline = B_TRUE;
4107 		vdev_reopen(tvd);
4108 
4109 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4110 		    vdev_is_dead(tvd)) {
4111 			vd->vdev_offline = B_FALSE;
4112 			vdev_reopen(tvd);
4113 			return (spa_vdev_state_exit(spa, NULL,
4114 			    SET_ERROR(EBUSY)));
4115 		}
4116 
4117 		/*
4118 		 * Add the device back into the metaslab rotor so that
4119 		 * once we online the device it's open for business.
4120 		 */
4121 		if (tvd->vdev_islog && mg != NULL)
4122 			metaslab_group_activate(mg);
4123 	}
4124 
4125 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
4126 
4127 	return (spa_vdev_state_exit(spa, vd, 0));
4128 }
4129 
4130 int
4131 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
4132 {
4133 	int error;
4134 
4135 	mutex_enter(&spa->spa_vdev_top_lock);
4136 	error = vdev_offline_locked(spa, guid, flags);
4137 	mutex_exit(&spa->spa_vdev_top_lock);
4138 
4139 	return (error);
4140 }
4141 
4142 /*
4143  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
4144  * vdev_offline(), we assume the spa config is locked.  We also clear all
4145  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
4146  */
4147 void
4148 vdev_clear(spa_t *spa, vdev_t *vd)
4149 {
4150 	vdev_t *rvd = spa->spa_root_vdev;
4151 
4152 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
4153 
4154 	if (vd == NULL)
4155 		vd = rvd;
4156 
4157 	vd->vdev_stat.vs_read_errors = 0;
4158 	vd->vdev_stat.vs_write_errors = 0;
4159 	vd->vdev_stat.vs_checksum_errors = 0;
4160 	vd->vdev_stat.vs_slow_ios = 0;
4161 
4162 	for (int c = 0; c < vd->vdev_children; c++)
4163 		vdev_clear(spa, vd->vdev_child[c]);
4164 
4165 	/*
4166 	 * It makes no sense to "clear" an indirect vdev.
4167 	 */
4168 	if (!vdev_is_concrete(vd))
4169 		return;
4170 
4171 	/*
4172 	 * If we're in the FAULTED state or have experienced failed I/O, then
4173 	 * clear the persistent state and attempt to reopen the device.  We
4174 	 * also mark the vdev config dirty, so that the new faulted state is
4175 	 * written out to disk.
4176 	 */
4177 	if (vd->vdev_faulted || vd->vdev_degraded ||
4178 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
4179 		/*
4180 		 * When reopening in response to a clear event, it may be due to
4181 		 * a fmadm repair request.  In this case, if the device is
4182 		 * still broken, we want to still post the ereport again.
4183 		 */
4184 		vd->vdev_forcefault = B_TRUE;
4185 
4186 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
4187 		vd->vdev_cant_read = B_FALSE;
4188 		vd->vdev_cant_write = B_FALSE;
4189 		vd->vdev_stat.vs_aux = 0;
4190 
4191 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
4192 
4193 		vd->vdev_forcefault = B_FALSE;
4194 
4195 		if (vd != rvd && vdev_writeable(vd->vdev_top))
4196 			vdev_state_dirty(vd->vdev_top);
4197 
4198 		/* If a resilver isn't required, check if vdevs can be culled */
4199 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd) &&
4200 		    !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4201 		    !dsl_scan_resilver_scheduled(spa->spa_dsl_pool))
4202 			spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
4203 
4204 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
4205 	}
4206 
4207 	/*
4208 	 * When clearing a FMA-diagnosed fault, we always want to
4209 	 * unspare the device, as we assume that the original spare was
4210 	 * done in response to the FMA fault.
4211 	 */
4212 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
4213 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4214 	    vd->vdev_parent->vdev_child[0] == vd)
4215 		vd->vdev_unspare = B_TRUE;
4216 
4217 	/* Clear recent error events cache (i.e. duplicate events tracking) */
4218 	zfs_ereport_clear(spa, vd);
4219 }
4220 
4221 boolean_t
4222 vdev_is_dead(vdev_t *vd)
4223 {
4224 	/*
4225 	 * Holes and missing devices are always considered "dead".
4226 	 * This simplifies the code since we don't have to check for
4227 	 * these types of devices in the various code paths.
4228 	 * Instead we rely on the fact that we skip over dead devices
4229 	 * before issuing I/O to them.
4230 	 */
4231 	return (vd->vdev_state < VDEV_STATE_DEGRADED ||
4232 	    vd->vdev_ops == &vdev_hole_ops ||
4233 	    vd->vdev_ops == &vdev_missing_ops);
4234 }
4235 
4236 boolean_t
4237 vdev_readable(vdev_t *vd)
4238 {
4239 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
4240 }
4241 
4242 boolean_t
4243 vdev_writeable(vdev_t *vd)
4244 {
4245 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
4246 	    vdev_is_concrete(vd));
4247 }
4248 
4249 boolean_t
4250 vdev_allocatable(vdev_t *vd)
4251 {
4252 	uint64_t state = vd->vdev_state;
4253 
4254 	/*
4255 	 * We currently allow allocations from vdevs which may be in the
4256 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4257 	 * fails to reopen then we'll catch it later when we're holding
4258 	 * the proper locks.  Note that we have to get the vdev state
4259 	 * in a local variable because although it changes atomically,
4260 	 * we're asking two separate questions about it.
4261 	 */
4262 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
4263 	    !vd->vdev_cant_write && vdev_is_concrete(vd) &&
4264 	    vd->vdev_mg->mg_initialized);
4265 }
4266 
4267 boolean_t
4268 vdev_accessible(vdev_t *vd, zio_t *zio)
4269 {
4270 	ASSERT(zio->io_vd == vd);
4271 
4272 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
4273 		return (B_FALSE);
4274 
4275 	if (zio->io_type == ZIO_TYPE_READ)
4276 		return (!vd->vdev_cant_read);
4277 
4278 	if (zio->io_type == ZIO_TYPE_WRITE)
4279 		return (!vd->vdev_cant_write);
4280 
4281 	return (B_TRUE);
4282 }
4283 
4284 static void
4285 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
4286 {
4287 	/*
4288 	 * Exclude the dRAID spare when aggregating to avoid double counting
4289 	 * the ops and bytes.  These IOs are counted by the physical leaves.
4290 	 */
4291 	if (cvd->vdev_ops == &vdev_draid_spare_ops)
4292 		return;
4293 
4294 	for (int t = 0; t < VS_ZIO_TYPES; t++) {
4295 		vs->vs_ops[t] += cvs->vs_ops[t];
4296 		vs->vs_bytes[t] += cvs->vs_bytes[t];
4297 	}
4298 
4299 	cvs->vs_scan_removing = cvd->vdev_removing;
4300 }
4301 
4302 /*
4303  * Get extended stats
4304  */
4305 static void
4306 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
4307 {
4308 	(void) cvd;
4309 
4310 	int t, b;
4311 	for (t = 0; t < ZIO_TYPES; t++) {
4312 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
4313 			vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
4314 
4315 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
4316 			vsx->vsx_total_histo[t][b] +=
4317 			    cvsx->vsx_total_histo[t][b];
4318 		}
4319 	}
4320 
4321 	for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4322 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
4323 			vsx->vsx_queue_histo[t][b] +=
4324 			    cvsx->vsx_queue_histo[t][b];
4325 		}
4326 		vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
4327 		vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
4328 
4329 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
4330 			vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
4331 
4332 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
4333 			vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
4334 	}
4335 
4336 }
4337 
4338 boolean_t
4339 vdev_is_spacemap_addressable(vdev_t *vd)
4340 {
4341 	if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
4342 		return (B_TRUE);
4343 
4344 	/*
4345 	 * If double-word space map entries are not enabled we assume
4346 	 * 47 bits of the space map entry are dedicated to the entry's
4347 	 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4348 	 * to calculate the maximum address that can be described by a
4349 	 * space map entry for the given device.
4350 	 */
4351 	uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
4352 
4353 	if (shift >= 63) /* detect potential overflow */
4354 		return (B_TRUE);
4355 
4356 	return (vd->vdev_asize < (1ULL << shift));
4357 }
4358 
4359 /*
4360  * Get statistics for the given vdev.
4361  */
4362 static void
4363 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4364 {
4365 	int t;
4366 	/*
4367 	 * If we're getting stats on the root vdev, aggregate the I/O counts
4368 	 * over all top-level vdevs (i.e. the direct children of the root).
4369 	 */
4370 	if (!vd->vdev_ops->vdev_op_leaf) {
4371 		if (vs) {
4372 			memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
4373 			memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
4374 		}
4375 		if (vsx)
4376 			memset(vsx, 0, sizeof (*vsx));
4377 
4378 		for (int c = 0; c < vd->vdev_children; c++) {
4379 			vdev_t *cvd = vd->vdev_child[c];
4380 			vdev_stat_t *cvs = &cvd->vdev_stat;
4381 			vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
4382 
4383 			vdev_get_stats_ex_impl(cvd, cvs, cvsx);
4384 			if (vs)
4385 				vdev_get_child_stat(cvd, vs, cvs);
4386 			if (vsx)
4387 				vdev_get_child_stat_ex(cvd, vsx, cvsx);
4388 		}
4389 	} else {
4390 		/*
4391 		 * We're a leaf.  Just copy our ZIO active queue stats in.  The
4392 		 * other leaf stats are updated in vdev_stat_update().
4393 		 */
4394 		if (!vsx)
4395 			return;
4396 
4397 		memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
4398 
4399 		for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) {
4400 			vsx->vsx_active_queue[t] =
4401 			    vd->vdev_queue.vq_class[t].vqc_active;
4402 			vsx->vsx_pend_queue[t] = avl_numnodes(
4403 			    &vd->vdev_queue.vq_class[t].vqc_queued_tree);
4404 		}
4405 	}
4406 }
4407 
4408 void
4409 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4410 {
4411 	vdev_t *tvd = vd->vdev_top;
4412 	mutex_enter(&vd->vdev_stat_lock);
4413 	if (vs) {
4414 		memcpy(vs, &vd->vdev_stat, sizeof (*vs));
4415 		vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
4416 		vs->vs_state = vd->vdev_state;
4417 		vs->vs_rsize = vdev_get_min_asize(vd);
4418 
4419 		if (vd->vdev_ops->vdev_op_leaf) {
4420 			vs->vs_pspace = vd->vdev_psize;
4421 			vs->vs_rsize += VDEV_LABEL_START_SIZE +
4422 			    VDEV_LABEL_END_SIZE;
4423 			/*
4424 			 * Report initializing progress. Since we don't
4425 			 * have the initializing locks held, this is only
4426 			 * an estimate (although a fairly accurate one).
4427 			 */
4428 			vs->vs_initialize_bytes_done =
4429 			    vd->vdev_initialize_bytes_done;
4430 			vs->vs_initialize_bytes_est =
4431 			    vd->vdev_initialize_bytes_est;
4432 			vs->vs_initialize_state = vd->vdev_initialize_state;
4433 			vs->vs_initialize_action_time =
4434 			    vd->vdev_initialize_action_time;
4435 
4436 			/*
4437 			 * Report manual TRIM progress. Since we don't have
4438 			 * the manual TRIM locks held, this is only an
4439 			 * estimate (although fairly accurate one).
4440 			 */
4441 			vs->vs_trim_notsup = !vd->vdev_has_trim;
4442 			vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done;
4443 			vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est;
4444 			vs->vs_trim_state = vd->vdev_trim_state;
4445 			vs->vs_trim_action_time = vd->vdev_trim_action_time;
4446 
4447 			/* Set when there is a deferred resilver. */
4448 			vs->vs_resilver_deferred = vd->vdev_resilver_deferred;
4449 		}
4450 
4451 		/*
4452 		 * Report expandable space on top-level, non-auxiliary devices
4453 		 * only. The expandable space is reported in terms of metaslab
4454 		 * sized units since that determines how much space the pool
4455 		 * can expand.
4456 		 */
4457 		if (vd->vdev_aux == NULL && tvd != NULL) {
4458 			vs->vs_esize = P2ALIGN(
4459 			    vd->vdev_max_asize - vd->vdev_asize,
4460 			    1ULL << tvd->vdev_ms_shift);
4461 		}
4462 
4463 		vs->vs_configured_ashift = vd->vdev_top != NULL
4464 		    ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
4465 		vs->vs_logical_ashift = vd->vdev_logical_ashift;
4466 		vs->vs_physical_ashift = vd->vdev_physical_ashift;
4467 
4468 		/*
4469 		 * Report fragmentation and rebuild progress for top-level,
4470 		 * non-auxiliary, concrete devices.
4471 		 */
4472 		if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
4473 		    vdev_is_concrete(vd)) {
4474 			/*
4475 			 * The vdev fragmentation rating doesn't take into
4476 			 * account the embedded slog metaslab (vdev_log_mg).
4477 			 * Since it's only one metaslab, it would have a tiny
4478 			 * impact on the overall fragmentation.
4479 			 */
4480 			vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
4481 			    vd->vdev_mg->mg_fragmentation : 0;
4482 		}
4483 		vs->vs_noalloc = MAX(vd->vdev_noalloc,
4484 		    tvd ? tvd->vdev_noalloc : 0);
4485 	}
4486 
4487 	vdev_get_stats_ex_impl(vd, vs, vsx);
4488 	mutex_exit(&vd->vdev_stat_lock);
4489 }
4490 
4491 void
4492 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
4493 {
4494 	return (vdev_get_stats_ex(vd, vs, NULL));
4495 }
4496 
4497 void
4498 vdev_clear_stats(vdev_t *vd)
4499 {
4500 	mutex_enter(&vd->vdev_stat_lock);
4501 	vd->vdev_stat.vs_space = 0;
4502 	vd->vdev_stat.vs_dspace = 0;
4503 	vd->vdev_stat.vs_alloc = 0;
4504 	mutex_exit(&vd->vdev_stat_lock);
4505 }
4506 
4507 void
4508 vdev_scan_stat_init(vdev_t *vd)
4509 {
4510 	vdev_stat_t *vs = &vd->vdev_stat;
4511 
4512 	for (int c = 0; c < vd->vdev_children; c++)
4513 		vdev_scan_stat_init(vd->vdev_child[c]);
4514 
4515 	mutex_enter(&vd->vdev_stat_lock);
4516 	vs->vs_scan_processed = 0;
4517 	mutex_exit(&vd->vdev_stat_lock);
4518 }
4519 
4520 void
4521 vdev_stat_update(zio_t *zio, uint64_t psize)
4522 {
4523 	spa_t *spa = zio->io_spa;
4524 	vdev_t *rvd = spa->spa_root_vdev;
4525 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
4526 	vdev_t *pvd;
4527 	uint64_t txg = zio->io_txg;
4528 	vdev_stat_t *vs = vd ? &vd->vdev_stat : NULL;
4529 	vdev_stat_ex_t *vsx = vd ? &vd->vdev_stat_ex : NULL;
4530 	zio_type_t type = zio->io_type;
4531 	int flags = zio->io_flags;
4532 
4533 	/*
4534 	 * If this i/o is a gang leader, it didn't do any actual work.
4535 	 */
4536 	if (zio->io_gang_tree)
4537 		return;
4538 
4539 	if (zio->io_error == 0) {
4540 		/*
4541 		 * If this is a root i/o, don't count it -- we've already
4542 		 * counted the top-level vdevs, and vdev_get_stats() will
4543 		 * aggregate them when asked.  This reduces contention on
4544 		 * the root vdev_stat_lock and implicitly handles blocks
4545 		 * that compress away to holes, for which there is no i/o.
4546 		 * (Holes never create vdev children, so all the counters
4547 		 * remain zero, which is what we want.)
4548 		 *
4549 		 * Note: this only applies to successful i/o (io_error == 0)
4550 		 * because unlike i/o counts, errors are not additive.
4551 		 * When reading a ditto block, for example, failure of
4552 		 * one top-level vdev does not imply a root-level error.
4553 		 */
4554 		if (vd == rvd)
4555 			return;
4556 
4557 		ASSERT(vd == zio->io_vd);
4558 
4559 		if (flags & ZIO_FLAG_IO_BYPASS)
4560 			return;
4561 
4562 		mutex_enter(&vd->vdev_stat_lock);
4563 
4564 		if (flags & ZIO_FLAG_IO_REPAIR) {
4565 			/*
4566 			 * Repair is the result of a resilver issued by the
4567 			 * scan thread (spa_sync).
4568 			 */
4569 			if (flags & ZIO_FLAG_SCAN_THREAD) {
4570 				dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
4571 				dsl_scan_phys_t *scn_phys = &scn->scn_phys;
4572 				uint64_t *processed = &scn_phys->scn_processed;
4573 
4574 				if (vd->vdev_ops->vdev_op_leaf)
4575 					atomic_add_64(processed, psize);
4576 				vs->vs_scan_processed += psize;
4577 			}
4578 
4579 			/*
4580 			 * Repair is the result of a rebuild issued by the
4581 			 * rebuild thread (vdev_rebuild_thread).  To avoid
4582 			 * double counting repaired bytes the virtual dRAID
4583 			 * spare vdev is excluded from the processed bytes.
4584 			 */
4585 			if (zio->io_priority == ZIO_PRIORITY_REBUILD) {
4586 				vdev_t *tvd = vd->vdev_top;
4587 				vdev_rebuild_t *vr = &tvd->vdev_rebuild_config;
4588 				vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
4589 				uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt;
4590 
4591 				if (vd->vdev_ops->vdev_op_leaf &&
4592 				    vd->vdev_ops != &vdev_draid_spare_ops) {
4593 					atomic_add_64(rebuilt, psize);
4594 				}
4595 				vs->vs_rebuild_processed += psize;
4596 			}
4597 
4598 			if (flags & ZIO_FLAG_SELF_HEAL)
4599 				vs->vs_self_healed += psize;
4600 		}
4601 
4602 		/*
4603 		 * The bytes/ops/histograms are recorded at the leaf level and
4604 		 * aggregated into the higher level vdevs in vdev_get_stats().
4605 		 */
4606 		if (vd->vdev_ops->vdev_op_leaf &&
4607 		    (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
4608 			zio_type_t vs_type = type;
4609 			zio_priority_t priority = zio->io_priority;
4610 
4611 			/*
4612 			 * TRIM ops and bytes are reported to user space as
4613 			 * ZIO_TYPE_IOCTL.  This is done to preserve the
4614 			 * vdev_stat_t structure layout for user space.
4615 			 */
4616 			if (type == ZIO_TYPE_TRIM)
4617 				vs_type = ZIO_TYPE_IOCTL;
4618 
4619 			/*
4620 			 * Solely for the purposes of 'zpool iostat -lqrw'
4621 			 * reporting use the priority to categorize the IO.
4622 			 * Only the following are reported to user space:
4623 			 *
4624 			 *   ZIO_PRIORITY_SYNC_READ,
4625 			 *   ZIO_PRIORITY_SYNC_WRITE,
4626 			 *   ZIO_PRIORITY_ASYNC_READ,
4627 			 *   ZIO_PRIORITY_ASYNC_WRITE,
4628 			 *   ZIO_PRIORITY_SCRUB,
4629 			 *   ZIO_PRIORITY_TRIM,
4630 			 *   ZIO_PRIORITY_REBUILD.
4631 			 */
4632 			if (priority == ZIO_PRIORITY_INITIALIZING) {
4633 				ASSERT3U(type, ==, ZIO_TYPE_WRITE);
4634 				priority = ZIO_PRIORITY_ASYNC_WRITE;
4635 			} else if (priority == ZIO_PRIORITY_REMOVAL) {
4636 				priority = ((type == ZIO_TYPE_WRITE) ?
4637 				    ZIO_PRIORITY_ASYNC_WRITE :
4638 				    ZIO_PRIORITY_ASYNC_READ);
4639 			}
4640 
4641 			vs->vs_ops[vs_type]++;
4642 			vs->vs_bytes[vs_type] += psize;
4643 
4644 			if (flags & ZIO_FLAG_DELEGATED) {
4645 				vsx->vsx_agg_histo[priority]
4646 				    [RQ_HISTO(zio->io_size)]++;
4647 			} else {
4648 				vsx->vsx_ind_histo[priority]
4649 				    [RQ_HISTO(zio->io_size)]++;
4650 			}
4651 
4652 			if (zio->io_delta && zio->io_delay) {
4653 				vsx->vsx_queue_histo[priority]
4654 				    [L_HISTO(zio->io_delta - zio->io_delay)]++;
4655 				vsx->vsx_disk_histo[type]
4656 				    [L_HISTO(zio->io_delay)]++;
4657 				vsx->vsx_total_histo[type]
4658 				    [L_HISTO(zio->io_delta)]++;
4659 			}
4660 		}
4661 
4662 		mutex_exit(&vd->vdev_stat_lock);
4663 		return;
4664 	}
4665 
4666 	if (flags & ZIO_FLAG_SPECULATIVE)
4667 		return;
4668 
4669 	/*
4670 	 * If this is an I/O error that is going to be retried, then ignore the
4671 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
4672 	 * hard errors, when in reality they can happen for any number of
4673 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
4674 	 */
4675 	if (zio->io_error == EIO &&
4676 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
4677 		return;
4678 
4679 	/*
4680 	 * Intent logs writes won't propagate their error to the root
4681 	 * I/O so don't mark these types of failures as pool-level
4682 	 * errors.
4683 	 */
4684 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
4685 		return;
4686 
4687 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
4688 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
4689 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
4690 	    spa->spa_claiming)) {
4691 		/*
4692 		 * This is either a normal write (not a repair), or it's
4693 		 * a repair induced by the scrub thread, or it's a repair
4694 		 * made by zil_claim() during spa_load() in the first txg.
4695 		 * In the normal case, we commit the DTL change in the same
4696 		 * txg as the block was born.  In the scrub-induced repair
4697 		 * case, we know that scrubs run in first-pass syncing context,
4698 		 * so we commit the DTL change in spa_syncing_txg(spa).
4699 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
4700 		 *
4701 		 * We currently do not make DTL entries for failed spontaneous
4702 		 * self-healing writes triggered by normal (non-scrubbing)
4703 		 * reads, because we have no transactional context in which to
4704 		 * do so -- and it's not clear that it'd be desirable anyway.
4705 		 */
4706 		if (vd->vdev_ops->vdev_op_leaf) {
4707 			uint64_t commit_txg = txg;
4708 			if (flags & ZIO_FLAG_SCAN_THREAD) {
4709 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4710 				ASSERT(spa_sync_pass(spa) == 1);
4711 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
4712 				commit_txg = spa_syncing_txg(spa);
4713 			} else if (spa->spa_claiming) {
4714 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4715 				commit_txg = spa_first_txg(spa);
4716 			}
4717 			ASSERT(commit_txg >= spa_syncing_txg(spa));
4718 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
4719 				return;
4720 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4721 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
4722 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
4723 		}
4724 		if (vd != rvd)
4725 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
4726 	}
4727 }
4728 
4729 int64_t
4730 vdev_deflated_space(vdev_t *vd, int64_t space)
4731 {
4732 	ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
4733 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
4734 
4735 	return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
4736 }
4737 
4738 /*
4739  * Update the in-core space usage stats for this vdev, its metaslab class,
4740  * and the root vdev.
4741  */
4742 void
4743 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
4744     int64_t space_delta)
4745 {
4746 	(void) defer_delta;
4747 	int64_t dspace_delta;
4748 	spa_t *spa = vd->vdev_spa;
4749 	vdev_t *rvd = spa->spa_root_vdev;
4750 
4751 	ASSERT(vd == vd->vdev_top);
4752 
4753 	/*
4754 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
4755 	 * factor.  We must calculate this here and not at the root vdev
4756 	 * because the root vdev's psize-to-asize is simply the max of its
4757 	 * children's, thus not accurate enough for us.
4758 	 */
4759 	dspace_delta = vdev_deflated_space(vd, space_delta);
4760 
4761 	mutex_enter(&vd->vdev_stat_lock);
4762 	/* ensure we won't underflow */
4763 	if (alloc_delta < 0) {
4764 		ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta);
4765 	}
4766 
4767 	vd->vdev_stat.vs_alloc += alloc_delta;
4768 	vd->vdev_stat.vs_space += space_delta;
4769 	vd->vdev_stat.vs_dspace += dspace_delta;
4770 	mutex_exit(&vd->vdev_stat_lock);
4771 
4772 	/* every class but log contributes to root space stats */
4773 	if (vd->vdev_mg != NULL && !vd->vdev_islog) {
4774 		ASSERT(!vd->vdev_isl2cache);
4775 		mutex_enter(&rvd->vdev_stat_lock);
4776 		rvd->vdev_stat.vs_alloc += alloc_delta;
4777 		rvd->vdev_stat.vs_space += space_delta;
4778 		rvd->vdev_stat.vs_dspace += dspace_delta;
4779 		mutex_exit(&rvd->vdev_stat_lock);
4780 	}
4781 	/* Note: metaslab_class_space_update moved to metaslab_space_update */
4782 }
4783 
4784 /*
4785  * Mark a top-level vdev's config as dirty, placing it on the dirty list
4786  * so that it will be written out next time the vdev configuration is synced.
4787  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
4788  */
4789 void
4790 vdev_config_dirty(vdev_t *vd)
4791 {
4792 	spa_t *spa = vd->vdev_spa;
4793 	vdev_t *rvd = spa->spa_root_vdev;
4794 	int c;
4795 
4796 	ASSERT(spa_writeable(spa));
4797 
4798 	/*
4799 	 * If this is an aux vdev (as with l2cache and spare devices), then we
4800 	 * update the vdev config manually and set the sync flag.
4801 	 */
4802 	if (vd->vdev_aux != NULL) {
4803 		spa_aux_vdev_t *sav = vd->vdev_aux;
4804 		nvlist_t **aux;
4805 		uint_t naux;
4806 
4807 		for (c = 0; c < sav->sav_count; c++) {
4808 			if (sav->sav_vdevs[c] == vd)
4809 				break;
4810 		}
4811 
4812 		if (c == sav->sav_count) {
4813 			/*
4814 			 * We're being removed.  There's nothing more to do.
4815 			 */
4816 			ASSERT(sav->sav_sync == B_TRUE);
4817 			return;
4818 		}
4819 
4820 		sav->sav_sync = B_TRUE;
4821 
4822 		if (nvlist_lookup_nvlist_array(sav->sav_config,
4823 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
4824 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
4825 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
4826 		}
4827 
4828 		ASSERT(c < naux);
4829 
4830 		/*
4831 		 * Setting the nvlist in the middle if the array is a little
4832 		 * sketchy, but it will work.
4833 		 */
4834 		nvlist_free(aux[c]);
4835 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
4836 
4837 		return;
4838 	}
4839 
4840 	/*
4841 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
4842 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
4843 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
4844 	 * so this is sufficient to ensure mutual exclusion.
4845 	 */
4846 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
4847 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4848 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
4849 
4850 	if (vd == rvd) {
4851 		for (c = 0; c < rvd->vdev_children; c++)
4852 			vdev_config_dirty(rvd->vdev_child[c]);
4853 	} else {
4854 		ASSERT(vd == vd->vdev_top);
4855 
4856 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
4857 		    vdev_is_concrete(vd)) {
4858 			list_insert_head(&spa->spa_config_dirty_list, vd);
4859 		}
4860 	}
4861 }
4862 
4863 void
4864 vdev_config_clean(vdev_t *vd)
4865 {
4866 	spa_t *spa = vd->vdev_spa;
4867 
4868 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
4869 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4870 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
4871 
4872 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
4873 	list_remove(&spa->spa_config_dirty_list, vd);
4874 }
4875 
4876 /*
4877  * Mark a top-level vdev's state as dirty, so that the next pass of
4878  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
4879  * the state changes from larger config changes because they require
4880  * much less locking, and are often needed for administrative actions.
4881  */
4882 void
4883 vdev_state_dirty(vdev_t *vd)
4884 {
4885 	spa_t *spa = vd->vdev_spa;
4886 
4887 	ASSERT(spa_writeable(spa));
4888 	ASSERT(vd == vd->vdev_top);
4889 
4890 	/*
4891 	 * The state list is protected by the SCL_STATE lock.  The caller
4892 	 * must either hold SCL_STATE as writer, or must be the sync thread
4893 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
4894 	 * so this is sufficient to ensure mutual exclusion.
4895 	 */
4896 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
4897 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4898 	    spa_config_held(spa, SCL_STATE, RW_READER)));
4899 
4900 	if (!list_link_active(&vd->vdev_state_dirty_node) &&
4901 	    vdev_is_concrete(vd))
4902 		list_insert_head(&spa->spa_state_dirty_list, vd);
4903 }
4904 
4905 void
4906 vdev_state_clean(vdev_t *vd)
4907 {
4908 	spa_t *spa = vd->vdev_spa;
4909 
4910 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
4911 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4912 	    spa_config_held(spa, SCL_STATE, RW_READER)));
4913 
4914 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
4915 	list_remove(&spa->spa_state_dirty_list, vd);
4916 }
4917 
4918 /*
4919  * Propagate vdev state up from children to parent.
4920  */
4921 void
4922 vdev_propagate_state(vdev_t *vd)
4923 {
4924 	spa_t *spa = vd->vdev_spa;
4925 	vdev_t *rvd = spa->spa_root_vdev;
4926 	int degraded = 0, faulted = 0;
4927 	int corrupted = 0;
4928 	vdev_t *child;
4929 
4930 	if (vd->vdev_children > 0) {
4931 		for (int c = 0; c < vd->vdev_children; c++) {
4932 			child = vd->vdev_child[c];
4933 
4934 			/*
4935 			 * Don't factor holes or indirect vdevs into the
4936 			 * decision.
4937 			 */
4938 			if (!vdev_is_concrete(child))
4939 				continue;
4940 
4941 			if (!vdev_readable(child) ||
4942 			    (!vdev_writeable(child) && spa_writeable(spa))) {
4943 				/*
4944 				 * Root special: if there is a top-level log
4945 				 * device, treat the root vdev as if it were
4946 				 * degraded.
4947 				 */
4948 				if (child->vdev_islog && vd == rvd)
4949 					degraded++;
4950 				else
4951 					faulted++;
4952 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
4953 				degraded++;
4954 			}
4955 
4956 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
4957 				corrupted++;
4958 		}
4959 
4960 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
4961 
4962 		/*
4963 		 * Root special: if there is a top-level vdev that cannot be
4964 		 * opened due to corrupted metadata, then propagate the root
4965 		 * vdev's aux state as 'corrupt' rather than 'insufficient
4966 		 * replicas'.
4967 		 */
4968 		if (corrupted && vd == rvd &&
4969 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
4970 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
4971 			    VDEV_AUX_CORRUPT_DATA);
4972 	}
4973 
4974 	if (vd->vdev_parent)
4975 		vdev_propagate_state(vd->vdev_parent);
4976 }
4977 
4978 /*
4979  * Set a vdev's state.  If this is during an open, we don't update the parent
4980  * state, because we're in the process of opening children depth-first.
4981  * Otherwise, we propagate the change to the parent.
4982  *
4983  * If this routine places a device in a faulted state, an appropriate ereport is
4984  * generated.
4985  */
4986 void
4987 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
4988 {
4989 	uint64_t save_state;
4990 	spa_t *spa = vd->vdev_spa;
4991 
4992 	if (state == vd->vdev_state) {
4993 		/*
4994 		 * Since vdev_offline() code path is already in an offline
4995 		 * state we can miss a statechange event to OFFLINE. Check
4996 		 * the previous state to catch this condition.
4997 		 */
4998 		if (vd->vdev_ops->vdev_op_leaf &&
4999 		    (state == VDEV_STATE_OFFLINE) &&
5000 		    (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) {
5001 			/* post an offline state change */
5002 			zfs_post_state_change(spa, vd, vd->vdev_prevstate);
5003 		}
5004 		vd->vdev_stat.vs_aux = aux;
5005 		return;
5006 	}
5007 
5008 	save_state = vd->vdev_state;
5009 
5010 	vd->vdev_state = state;
5011 	vd->vdev_stat.vs_aux = aux;
5012 
5013 	/*
5014 	 * If we are setting the vdev state to anything but an open state, then
5015 	 * always close the underlying device unless the device has requested
5016 	 * a delayed close (i.e. we're about to remove or fault the device).
5017 	 * Otherwise, we keep accessible but invalid devices open forever.
5018 	 * We don't call vdev_close() itself, because that implies some extra
5019 	 * checks (offline, etc) that we don't want here.  This is limited to
5020 	 * leaf devices, because otherwise closing the device will affect other
5021 	 * children.
5022 	 */
5023 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
5024 	    vd->vdev_ops->vdev_op_leaf)
5025 		vd->vdev_ops->vdev_op_close(vd);
5026 
5027 	if (vd->vdev_removed &&
5028 	    state == VDEV_STATE_CANT_OPEN &&
5029 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
5030 		/*
5031 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
5032 		 * device was previously marked removed and someone attempted to
5033 		 * reopen it.  If this failed due to a nonexistent device, then
5034 		 * keep the device in the REMOVED state.  We also let this be if
5035 		 * it is one of our special test online cases, which is only
5036 		 * attempting to online the device and shouldn't generate an FMA
5037 		 * fault.
5038 		 */
5039 		vd->vdev_state = VDEV_STATE_REMOVED;
5040 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
5041 	} else if (state == VDEV_STATE_REMOVED) {
5042 		vd->vdev_removed = B_TRUE;
5043 	} else if (state == VDEV_STATE_CANT_OPEN) {
5044 		/*
5045 		 * If we fail to open a vdev during an import or recovery, we
5046 		 * mark it as "not available", which signifies that it was
5047 		 * never there to begin with.  Failure to open such a device
5048 		 * is not considered an error.
5049 		 */
5050 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
5051 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
5052 		    vd->vdev_ops->vdev_op_leaf)
5053 			vd->vdev_not_present = 1;
5054 
5055 		/*
5056 		 * Post the appropriate ereport.  If the 'prevstate' field is
5057 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
5058 		 * that this is part of a vdev_reopen().  In this case, we don't
5059 		 * want to post the ereport if the device was already in the
5060 		 * CANT_OPEN state beforehand.
5061 		 *
5062 		 * If the 'checkremove' flag is set, then this is an attempt to
5063 		 * online the device in response to an insertion event.  If we
5064 		 * hit this case, then we have detected an insertion event for a
5065 		 * faulted or offline device that wasn't in the removed state.
5066 		 * In this scenario, we don't post an ereport because we are
5067 		 * about to replace the device, or attempt an online with
5068 		 * vdev_forcefault, which will generate the fault for us.
5069 		 */
5070 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
5071 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
5072 		    vd != spa->spa_root_vdev) {
5073 			const char *class;
5074 
5075 			switch (aux) {
5076 			case VDEV_AUX_OPEN_FAILED:
5077 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
5078 				break;
5079 			case VDEV_AUX_CORRUPT_DATA:
5080 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
5081 				break;
5082 			case VDEV_AUX_NO_REPLICAS:
5083 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
5084 				break;
5085 			case VDEV_AUX_BAD_GUID_SUM:
5086 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
5087 				break;
5088 			case VDEV_AUX_TOO_SMALL:
5089 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
5090 				break;
5091 			case VDEV_AUX_BAD_LABEL:
5092 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
5093 				break;
5094 			case VDEV_AUX_BAD_ASHIFT:
5095 				class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
5096 				break;
5097 			default:
5098 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
5099 			}
5100 
5101 			(void) zfs_ereport_post(class, spa, vd, NULL, NULL,
5102 			    save_state);
5103 		}
5104 
5105 		/* Erase any notion of persistent removed state */
5106 		vd->vdev_removed = B_FALSE;
5107 	} else {
5108 		vd->vdev_removed = B_FALSE;
5109 	}
5110 
5111 	/*
5112 	 * Notify ZED of any significant state-change on a leaf vdev.
5113 	 *
5114 	 */
5115 	if (vd->vdev_ops->vdev_op_leaf) {
5116 		/* preserve original state from a vdev_reopen() */
5117 		if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) &&
5118 		    (vd->vdev_prevstate != vd->vdev_state) &&
5119 		    (save_state <= VDEV_STATE_CLOSED))
5120 			save_state = vd->vdev_prevstate;
5121 
5122 		/* filter out state change due to initial vdev_open */
5123 		if (save_state > VDEV_STATE_CLOSED)
5124 			zfs_post_state_change(spa, vd, save_state);
5125 	}
5126 
5127 	if (!isopen && vd->vdev_parent)
5128 		vdev_propagate_state(vd->vdev_parent);
5129 }
5130 
5131 boolean_t
5132 vdev_children_are_offline(vdev_t *vd)
5133 {
5134 	ASSERT(!vd->vdev_ops->vdev_op_leaf);
5135 
5136 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
5137 		if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
5138 			return (B_FALSE);
5139 	}
5140 
5141 	return (B_TRUE);
5142 }
5143 
5144 /*
5145  * Check the vdev configuration to ensure that it's capable of supporting
5146  * a root pool. We do not support partial configuration.
5147  */
5148 boolean_t
5149 vdev_is_bootable(vdev_t *vd)
5150 {
5151 	if (!vd->vdev_ops->vdev_op_leaf) {
5152 		const char *vdev_type = vd->vdev_ops->vdev_op_type;
5153 
5154 		if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0)
5155 			return (B_FALSE);
5156 	}
5157 
5158 	for (int c = 0; c < vd->vdev_children; c++) {
5159 		if (!vdev_is_bootable(vd->vdev_child[c]))
5160 			return (B_FALSE);
5161 	}
5162 	return (B_TRUE);
5163 }
5164 
5165 boolean_t
5166 vdev_is_concrete(vdev_t *vd)
5167 {
5168 	vdev_ops_t *ops = vd->vdev_ops;
5169 	if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
5170 	    ops == &vdev_missing_ops || ops == &vdev_root_ops) {
5171 		return (B_FALSE);
5172 	} else {
5173 		return (B_TRUE);
5174 	}
5175 }
5176 
5177 /*
5178  * Determine if a log device has valid content.  If the vdev was
5179  * removed or faulted in the MOS config then we know that
5180  * the content on the log device has already been written to the pool.
5181  */
5182 boolean_t
5183 vdev_log_state_valid(vdev_t *vd)
5184 {
5185 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
5186 	    !vd->vdev_removed)
5187 		return (B_TRUE);
5188 
5189 	for (int c = 0; c < vd->vdev_children; c++)
5190 		if (vdev_log_state_valid(vd->vdev_child[c]))
5191 			return (B_TRUE);
5192 
5193 	return (B_FALSE);
5194 }
5195 
5196 /*
5197  * Expand a vdev if possible.
5198  */
5199 void
5200 vdev_expand(vdev_t *vd, uint64_t txg)
5201 {
5202 	ASSERT(vd->vdev_top == vd);
5203 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5204 	ASSERT(vdev_is_concrete(vd));
5205 
5206 	vdev_set_deflate_ratio(vd);
5207 
5208 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
5209 	    vdev_is_concrete(vd)) {
5210 		vdev_metaslab_group_create(vd);
5211 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
5212 		vdev_config_dirty(vd);
5213 	}
5214 }
5215 
5216 /*
5217  * Split a vdev.
5218  */
5219 void
5220 vdev_split(vdev_t *vd)
5221 {
5222 	vdev_t *cvd, *pvd = vd->vdev_parent;
5223 
5224 	vdev_remove_child(pvd, vd);
5225 	vdev_compact_children(pvd);
5226 
5227 	cvd = pvd->vdev_child[0];
5228 	if (pvd->vdev_children == 1) {
5229 		vdev_remove_parent(cvd);
5230 		cvd->vdev_splitting = B_TRUE;
5231 	}
5232 	vdev_propagate_state(cvd);
5233 }
5234 
5235 void
5236 vdev_deadman(vdev_t *vd, char *tag)
5237 {
5238 	for (int c = 0; c < vd->vdev_children; c++) {
5239 		vdev_t *cvd = vd->vdev_child[c];
5240 
5241 		vdev_deadman(cvd, tag);
5242 	}
5243 
5244 	if (vd->vdev_ops->vdev_op_leaf) {
5245 		vdev_queue_t *vq = &vd->vdev_queue;
5246 
5247 		mutex_enter(&vq->vq_lock);
5248 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
5249 			spa_t *spa = vd->vdev_spa;
5250 			zio_t *fio;
5251 			uint64_t delta;
5252 
5253 			zfs_dbgmsg("slow vdev: %s has %lu active IOs",
5254 			    vd->vdev_path, avl_numnodes(&vq->vq_active_tree));
5255 
5256 			/*
5257 			 * Look at the head of all the pending queues,
5258 			 * if any I/O has been outstanding for longer than
5259 			 * the spa_deadman_synctime invoke the deadman logic.
5260 			 */
5261 			fio = avl_first(&vq->vq_active_tree);
5262 			delta = gethrtime() - fio->io_timestamp;
5263 			if (delta > spa_deadman_synctime(spa))
5264 				zio_deadman(fio, tag);
5265 		}
5266 		mutex_exit(&vq->vq_lock);
5267 	}
5268 }
5269 
5270 void
5271 vdev_defer_resilver(vdev_t *vd)
5272 {
5273 	ASSERT(vd->vdev_ops->vdev_op_leaf);
5274 
5275 	vd->vdev_resilver_deferred = B_TRUE;
5276 	vd->vdev_spa->spa_resilver_deferred = B_TRUE;
5277 }
5278 
5279 /*
5280  * Clears the resilver deferred flag on all leaf devs under vd. Returns
5281  * B_TRUE if we have devices that need to be resilvered and are available to
5282  * accept resilver I/Os.
5283  */
5284 boolean_t
5285 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx)
5286 {
5287 	boolean_t resilver_needed = B_FALSE;
5288 	spa_t *spa = vd->vdev_spa;
5289 
5290 	for (int c = 0; c < vd->vdev_children; c++) {
5291 		vdev_t *cvd = vd->vdev_child[c];
5292 		resilver_needed |= vdev_clear_resilver_deferred(cvd, tx);
5293 	}
5294 
5295 	if (vd == spa->spa_root_vdev &&
5296 	    spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) {
5297 		spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx);
5298 		vdev_config_dirty(vd);
5299 		spa->spa_resilver_deferred = B_FALSE;
5300 		return (resilver_needed);
5301 	}
5302 
5303 	if (!vdev_is_concrete(vd) || vd->vdev_aux ||
5304 	    !vd->vdev_ops->vdev_op_leaf)
5305 		return (resilver_needed);
5306 
5307 	vd->vdev_resilver_deferred = B_FALSE;
5308 
5309 	return (!vdev_is_dead(vd) && !vd->vdev_offline &&
5310 	    vdev_resilver_needed(vd, NULL, NULL));
5311 }
5312 
5313 boolean_t
5314 vdev_xlate_is_empty(range_seg64_t *rs)
5315 {
5316 	return (rs->rs_start == rs->rs_end);
5317 }
5318 
5319 /*
5320  * Translate a logical range to the first contiguous physical range for the
5321  * specified vdev_t.  This function is initially called with a leaf vdev and
5322  * will walk each parent vdev until it reaches a top-level vdev. Once the
5323  * top-level is reached the physical range is initialized and the recursive
5324  * function begins to unwind. As it unwinds it calls the parent's vdev
5325  * specific translation function to do the real conversion.
5326  */
5327 void
5328 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
5329     range_seg64_t *physical_rs, range_seg64_t *remain_rs)
5330 {
5331 	/*
5332 	 * Walk up the vdev tree
5333 	 */
5334 	if (vd != vd->vdev_top) {
5335 		vdev_xlate(vd->vdev_parent, logical_rs, physical_rs,
5336 		    remain_rs);
5337 	} else {
5338 		/*
5339 		 * We've reached the top-level vdev, initialize the physical
5340 		 * range to the logical range and set an empty remaining
5341 		 * range then start to unwind.
5342 		 */
5343 		physical_rs->rs_start = logical_rs->rs_start;
5344 		physical_rs->rs_end = logical_rs->rs_end;
5345 
5346 		remain_rs->rs_start = logical_rs->rs_start;
5347 		remain_rs->rs_end = logical_rs->rs_start;
5348 
5349 		return;
5350 	}
5351 
5352 	vdev_t *pvd = vd->vdev_parent;
5353 	ASSERT3P(pvd, !=, NULL);
5354 	ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL);
5355 
5356 	/*
5357 	 * As this recursive function unwinds, translate the logical
5358 	 * range into its physical and any remaining components by calling
5359 	 * the vdev specific translate function.
5360 	 */
5361 	range_seg64_t intermediate = { 0 };
5362 	pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs);
5363 
5364 	physical_rs->rs_start = intermediate.rs_start;
5365 	physical_rs->rs_end = intermediate.rs_end;
5366 }
5367 
5368 void
5369 vdev_xlate_walk(vdev_t *vd, const range_seg64_t *logical_rs,
5370     vdev_xlate_func_t *func, void *arg)
5371 {
5372 	range_seg64_t iter_rs = *logical_rs;
5373 	range_seg64_t physical_rs;
5374 	range_seg64_t remain_rs;
5375 
5376 	while (!vdev_xlate_is_empty(&iter_rs)) {
5377 
5378 		vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs);
5379 
5380 		/*
5381 		 * With raidz and dRAID, it's possible that the logical range
5382 		 * does not live on this leaf vdev. Only when there is a non-
5383 		 * zero physical size call the provided function.
5384 		 */
5385 		if (!vdev_xlate_is_empty(&physical_rs))
5386 			func(arg, &physical_rs);
5387 
5388 		iter_rs = remain_rs;
5389 	}
5390 }
5391 
5392 static char *
5393 vdev_name(vdev_t *vd, char *buf, int buflen)
5394 {
5395 	if (vd->vdev_path == NULL) {
5396 		if (strcmp(vd->vdev_ops->vdev_op_type, "root") == 0) {
5397 			strlcpy(buf, vd->vdev_spa->spa_name, buflen);
5398 		} else if (!vd->vdev_ops->vdev_op_leaf) {
5399 			snprintf(buf, buflen, "%s-%llu",
5400 			    vd->vdev_ops->vdev_op_type,
5401 			    (u_longlong_t)vd->vdev_id);
5402 		}
5403 	} else {
5404 		strlcpy(buf, vd->vdev_path, buflen);
5405 	}
5406 	return (buf);
5407 }
5408 
5409 /*
5410  * Look at the vdev tree and determine whether any devices are currently being
5411  * replaced.
5412  */
5413 boolean_t
5414 vdev_replace_in_progress(vdev_t *vdev)
5415 {
5416 	ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0);
5417 
5418 	if (vdev->vdev_ops == &vdev_replacing_ops)
5419 		return (B_TRUE);
5420 
5421 	/*
5422 	 * A 'spare' vdev indicates that we have a replace in progress, unless
5423 	 * it has exactly two children, and the second, the hot spare, has
5424 	 * finished being resilvered.
5425 	 */
5426 	if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 ||
5427 	    !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING)))
5428 		return (B_TRUE);
5429 
5430 	for (int i = 0; i < vdev->vdev_children; i++) {
5431 		if (vdev_replace_in_progress(vdev->vdev_child[i]))
5432 			return (B_TRUE);
5433 	}
5434 
5435 	return (B_FALSE);
5436 }
5437 
5438 /*
5439  * Add a (source=src, propname=propval) list to an nvlist.
5440  */
5441 static void
5442 vdev_prop_add_list(nvlist_t *nvl, const char *propname, char *strval,
5443     uint64_t intval, zprop_source_t src)
5444 {
5445 	nvlist_t *propval;
5446 
5447 	propval = fnvlist_alloc();
5448 	fnvlist_add_uint64(propval, ZPROP_SOURCE, src);
5449 
5450 	if (strval != NULL)
5451 		fnvlist_add_string(propval, ZPROP_VALUE, strval);
5452 	else
5453 		fnvlist_add_uint64(propval, ZPROP_VALUE, intval);
5454 
5455 	fnvlist_add_nvlist(nvl, propname, propval);
5456 	nvlist_free(propval);
5457 }
5458 
5459 static void
5460 vdev_props_set_sync(void *arg, dmu_tx_t *tx)
5461 {
5462 	vdev_t *vd;
5463 	nvlist_t *nvp = arg;
5464 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
5465 	objset_t *mos = spa->spa_meta_objset;
5466 	nvpair_t *elem = NULL;
5467 	uint64_t vdev_guid;
5468 	nvlist_t *nvprops;
5469 
5470 	vdev_guid = fnvlist_lookup_uint64(nvp, ZPOOL_VDEV_PROPS_SET_VDEV);
5471 	nvprops = fnvlist_lookup_nvlist(nvp, ZPOOL_VDEV_PROPS_SET_PROPS);
5472 	vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
5473 
5474 	/* this vdev could get removed while waiting for this sync task */
5475 	if (vd == NULL)
5476 		return;
5477 
5478 	mutex_enter(&spa->spa_props_lock);
5479 
5480 	while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5481 		uint64_t intval, objid = 0;
5482 		char *strval;
5483 		vdev_prop_t prop;
5484 		const char *propname = nvpair_name(elem);
5485 		zprop_type_t proptype;
5486 
5487 		/*
5488 		 * Set vdev property values in the vdev props mos object.
5489 		 */
5490 		if (vd->vdev_top_zap != 0) {
5491 			objid = vd->vdev_top_zap;
5492 		} else if (vd->vdev_leaf_zap != 0) {
5493 			objid = vd->vdev_leaf_zap;
5494 		} else {
5495 			panic("vdev not top or leaf");
5496 		}
5497 
5498 		switch (prop = vdev_name_to_prop(propname)) {
5499 		case VDEV_PROP_USERPROP:
5500 			if (vdev_prop_user(propname)) {
5501 				strval = fnvpair_value_string(elem);
5502 				if (strlen(strval) == 0) {
5503 					/* remove the property if value == "" */
5504 					(void) zap_remove(mos, objid, propname,
5505 					    tx);
5506 				} else {
5507 					VERIFY0(zap_update(mos, objid, propname,
5508 					    1, strlen(strval) + 1, strval, tx));
5509 				}
5510 				spa_history_log_internal(spa, "vdev set", tx,
5511 				    "vdev_guid=%llu: %s=%s",
5512 				    (u_longlong_t)vdev_guid, nvpair_name(elem),
5513 				    strval);
5514 			}
5515 			break;
5516 		default:
5517 			/* normalize the property name */
5518 			propname = vdev_prop_to_name(prop);
5519 			proptype = vdev_prop_get_type(prop);
5520 
5521 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
5522 				ASSERT(proptype == PROP_TYPE_STRING);
5523 				strval = fnvpair_value_string(elem);
5524 				VERIFY0(zap_update(mos, objid, propname,
5525 				    1, strlen(strval) + 1, strval, tx));
5526 				spa_history_log_internal(spa, "vdev set", tx,
5527 				    "vdev_guid=%llu: %s=%s",
5528 				    (u_longlong_t)vdev_guid, nvpair_name(elem),
5529 				    strval);
5530 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5531 				intval = fnvpair_value_uint64(elem);
5532 
5533 				if (proptype == PROP_TYPE_INDEX) {
5534 					const char *unused;
5535 					VERIFY0(vdev_prop_index_to_string(
5536 					    prop, intval, &unused));
5537 				}
5538 				VERIFY0(zap_update(mos, objid, propname,
5539 				    sizeof (uint64_t), 1, &intval, tx));
5540 				spa_history_log_internal(spa, "vdev set", tx,
5541 				    "vdev_guid=%llu: %s=%lld",
5542 				    (u_longlong_t)vdev_guid,
5543 				    nvpair_name(elem), (longlong_t)intval);
5544 			} else {
5545 				panic("invalid vdev property type %u",
5546 				    nvpair_type(elem));
5547 			}
5548 		}
5549 
5550 	}
5551 
5552 	mutex_exit(&spa->spa_props_lock);
5553 }
5554 
5555 int
5556 vdev_prop_set(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
5557 {
5558 	spa_t *spa = vd->vdev_spa;
5559 	nvpair_t *elem = NULL;
5560 	uint64_t vdev_guid;
5561 	nvlist_t *nvprops;
5562 	int error;
5563 
5564 	ASSERT(vd != NULL);
5565 
5566 	if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
5567 	    &vdev_guid) != 0)
5568 		return (SET_ERROR(EINVAL));
5569 
5570 	if (nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_SET_PROPS,
5571 	    &nvprops) != 0)
5572 		return (SET_ERROR(EINVAL));
5573 
5574 	if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL)
5575 		return (SET_ERROR(EINVAL));
5576 
5577 	while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5578 		char *propname = nvpair_name(elem);
5579 		vdev_prop_t prop = vdev_name_to_prop(propname);
5580 		uint64_t intval = 0;
5581 		char *strval = NULL;
5582 
5583 		if (prop == VDEV_PROP_USERPROP && !vdev_prop_user(propname)) {
5584 			error = EINVAL;
5585 			goto end;
5586 		}
5587 
5588 		if (vdev_prop_readonly(prop)) {
5589 			error = EROFS;
5590 			goto end;
5591 		}
5592 
5593 		/* Special Processing */
5594 		switch (prop) {
5595 		case VDEV_PROP_PATH:
5596 			if (vd->vdev_path == NULL) {
5597 				error = EROFS;
5598 				break;
5599 			}
5600 			if (nvpair_value_string(elem, &strval) != 0) {
5601 				error = EINVAL;
5602 				break;
5603 			}
5604 			/* New path must start with /dev/ */
5605 			if (strncmp(strval, "/dev/", 5)) {
5606 				error = EINVAL;
5607 				break;
5608 			}
5609 			error = spa_vdev_setpath(spa, vdev_guid, strval);
5610 			break;
5611 		case VDEV_PROP_ALLOCATING:
5612 			if (nvpair_value_uint64(elem, &intval) != 0) {
5613 				error = EINVAL;
5614 				break;
5615 			}
5616 			if (intval != vd->vdev_noalloc)
5617 				break;
5618 			if (intval == 0)
5619 				error = spa_vdev_noalloc(spa, vdev_guid);
5620 			else
5621 				error = spa_vdev_alloc(spa, vdev_guid);
5622 			break;
5623 		default:
5624 			/* Most processing is done in vdev_props_set_sync */
5625 			break;
5626 		}
5627 end:
5628 		if (error != 0) {
5629 			intval = error;
5630 			vdev_prop_add_list(outnvl, propname, strval, intval, 0);
5631 			return (error);
5632 		}
5633 	}
5634 
5635 	return (dsl_sync_task(spa->spa_name, NULL, vdev_props_set_sync,
5636 	    innvl, 6, ZFS_SPACE_CHECK_EXTRA_RESERVED));
5637 }
5638 
5639 int
5640 vdev_prop_get(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
5641 {
5642 	spa_t *spa = vd->vdev_spa;
5643 	objset_t *mos = spa->spa_meta_objset;
5644 	int err = 0;
5645 	uint64_t objid;
5646 	uint64_t vdev_guid;
5647 	nvpair_t *elem = NULL;
5648 	nvlist_t *nvprops = NULL;
5649 	uint64_t intval = 0;
5650 	char *strval = NULL;
5651 	const char *propname = NULL;
5652 	vdev_prop_t prop;
5653 
5654 	ASSERT(vd != NULL);
5655 	ASSERT(mos != NULL);
5656 
5657 	if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
5658 	    &vdev_guid) != 0)
5659 		return (SET_ERROR(EINVAL));
5660 
5661 	nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_GET_PROPS, &nvprops);
5662 
5663 	if (vd->vdev_top_zap != 0) {
5664 		objid = vd->vdev_top_zap;
5665 	} else if (vd->vdev_leaf_zap != 0) {
5666 		objid = vd->vdev_leaf_zap;
5667 	} else {
5668 		return (SET_ERROR(EINVAL));
5669 	}
5670 	ASSERT(objid != 0);
5671 
5672 	mutex_enter(&spa->spa_props_lock);
5673 
5674 	if (nvprops != NULL) {
5675 		char namebuf[64] = { 0 };
5676 
5677 		while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5678 			intval = 0;
5679 			strval = NULL;
5680 			propname = nvpair_name(elem);
5681 			prop = vdev_name_to_prop(propname);
5682 			zprop_source_t src = ZPROP_SRC_DEFAULT;
5683 			uint64_t integer_size, num_integers;
5684 
5685 			switch (prop) {
5686 			/* Special Read-only Properties */
5687 			case VDEV_PROP_NAME:
5688 				strval = vdev_name(vd, namebuf,
5689 				    sizeof (namebuf));
5690 				if (strval == NULL)
5691 					continue;
5692 				vdev_prop_add_list(outnvl, propname, strval, 0,
5693 				    ZPROP_SRC_NONE);
5694 				continue;
5695 			case VDEV_PROP_CAPACITY:
5696 				/* percent used */
5697 				intval = (vd->vdev_stat.vs_dspace == 0) ? 0 :
5698 				    (vd->vdev_stat.vs_alloc * 100 /
5699 				    vd->vdev_stat.vs_dspace);
5700 				vdev_prop_add_list(outnvl, propname, NULL,
5701 				    intval, ZPROP_SRC_NONE);
5702 				continue;
5703 			case VDEV_PROP_STATE:
5704 				vdev_prop_add_list(outnvl, propname, NULL,
5705 				    vd->vdev_state, ZPROP_SRC_NONE);
5706 				continue;
5707 			case VDEV_PROP_GUID:
5708 				vdev_prop_add_list(outnvl, propname, NULL,
5709 				    vd->vdev_guid, ZPROP_SRC_NONE);
5710 				continue;
5711 			case VDEV_PROP_ASIZE:
5712 				vdev_prop_add_list(outnvl, propname, NULL,
5713 				    vd->vdev_asize, ZPROP_SRC_NONE);
5714 				continue;
5715 			case VDEV_PROP_PSIZE:
5716 				vdev_prop_add_list(outnvl, propname, NULL,
5717 				    vd->vdev_psize, ZPROP_SRC_NONE);
5718 				continue;
5719 			case VDEV_PROP_ASHIFT:
5720 				vdev_prop_add_list(outnvl, propname, NULL,
5721 				    vd->vdev_ashift, ZPROP_SRC_NONE);
5722 				continue;
5723 			case VDEV_PROP_SIZE:
5724 				vdev_prop_add_list(outnvl, propname, NULL,
5725 				    vd->vdev_stat.vs_dspace, ZPROP_SRC_NONE);
5726 				continue;
5727 			case VDEV_PROP_FREE:
5728 				vdev_prop_add_list(outnvl, propname, NULL,
5729 				    vd->vdev_stat.vs_dspace -
5730 				    vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
5731 				continue;
5732 			case VDEV_PROP_ALLOCATED:
5733 				vdev_prop_add_list(outnvl, propname, NULL,
5734 				    vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
5735 				continue;
5736 			case VDEV_PROP_EXPANDSZ:
5737 				vdev_prop_add_list(outnvl, propname, NULL,
5738 				    vd->vdev_stat.vs_esize, ZPROP_SRC_NONE);
5739 				continue;
5740 			case VDEV_PROP_FRAGMENTATION:
5741 				vdev_prop_add_list(outnvl, propname, NULL,
5742 				    vd->vdev_stat.vs_fragmentation,
5743 				    ZPROP_SRC_NONE);
5744 				continue;
5745 			case VDEV_PROP_PARITY:
5746 				vdev_prop_add_list(outnvl, propname, NULL,
5747 				    vdev_get_nparity(vd), ZPROP_SRC_NONE);
5748 				continue;
5749 			case VDEV_PROP_PATH:
5750 				if (vd->vdev_path == NULL)
5751 					continue;
5752 				vdev_prop_add_list(outnvl, propname,
5753 				    vd->vdev_path, 0, ZPROP_SRC_NONE);
5754 				continue;
5755 			case VDEV_PROP_DEVID:
5756 				if (vd->vdev_devid == NULL)
5757 					continue;
5758 				vdev_prop_add_list(outnvl, propname,
5759 				    vd->vdev_devid, 0, ZPROP_SRC_NONE);
5760 				continue;
5761 			case VDEV_PROP_PHYS_PATH:
5762 				if (vd->vdev_physpath == NULL)
5763 					continue;
5764 				vdev_prop_add_list(outnvl, propname,
5765 				    vd->vdev_physpath, 0, ZPROP_SRC_NONE);
5766 				continue;
5767 			case VDEV_PROP_ENC_PATH:
5768 				if (vd->vdev_enc_sysfs_path == NULL)
5769 					continue;
5770 				vdev_prop_add_list(outnvl, propname,
5771 				    vd->vdev_enc_sysfs_path, 0, ZPROP_SRC_NONE);
5772 				continue;
5773 			case VDEV_PROP_FRU:
5774 				if (vd->vdev_fru == NULL)
5775 					continue;
5776 				vdev_prop_add_list(outnvl, propname,
5777 				    vd->vdev_fru, 0, ZPROP_SRC_NONE);
5778 				continue;
5779 			case VDEV_PROP_PARENT:
5780 				if (vd->vdev_parent != NULL) {
5781 					strval = vdev_name(vd->vdev_parent,
5782 					    namebuf, sizeof (namebuf));
5783 					vdev_prop_add_list(outnvl, propname,
5784 					    strval, 0, ZPROP_SRC_NONE);
5785 				}
5786 				continue;
5787 			case VDEV_PROP_CHILDREN:
5788 				if (vd->vdev_children > 0)
5789 					strval = kmem_zalloc(ZAP_MAXVALUELEN,
5790 					    KM_SLEEP);
5791 				for (uint64_t i = 0; i < vd->vdev_children;
5792 				    i++) {
5793 					char *vname;
5794 
5795 					vname = vdev_name(vd->vdev_child[i],
5796 					    namebuf, sizeof (namebuf));
5797 					if (vname == NULL)
5798 						vname = "(unknown)";
5799 					if (strlen(strval) > 0)
5800 						strlcat(strval, ",",
5801 						    ZAP_MAXVALUELEN);
5802 					strlcat(strval, vname, ZAP_MAXVALUELEN);
5803 				}
5804 				if (strval != NULL) {
5805 					vdev_prop_add_list(outnvl, propname,
5806 					    strval, 0, ZPROP_SRC_NONE);
5807 					kmem_free(strval, ZAP_MAXVALUELEN);
5808 				}
5809 				continue;
5810 			case VDEV_PROP_NUMCHILDREN:
5811 				vdev_prop_add_list(outnvl, propname, NULL,
5812 				    vd->vdev_children, ZPROP_SRC_NONE);
5813 				continue;
5814 			case VDEV_PROP_READ_ERRORS:
5815 				vdev_prop_add_list(outnvl, propname, NULL,
5816 				    vd->vdev_stat.vs_read_errors,
5817 				    ZPROP_SRC_NONE);
5818 				continue;
5819 			case VDEV_PROP_WRITE_ERRORS:
5820 				vdev_prop_add_list(outnvl, propname, NULL,
5821 				    vd->vdev_stat.vs_write_errors,
5822 				    ZPROP_SRC_NONE);
5823 				continue;
5824 			case VDEV_PROP_CHECKSUM_ERRORS:
5825 				vdev_prop_add_list(outnvl, propname, NULL,
5826 				    vd->vdev_stat.vs_checksum_errors,
5827 				    ZPROP_SRC_NONE);
5828 				continue;
5829 			case VDEV_PROP_INITIALIZE_ERRORS:
5830 				vdev_prop_add_list(outnvl, propname, NULL,
5831 				    vd->vdev_stat.vs_initialize_errors,
5832 				    ZPROP_SRC_NONE);
5833 				continue;
5834 			case VDEV_PROP_OPS_NULL:
5835 				vdev_prop_add_list(outnvl, propname, NULL,
5836 				    vd->vdev_stat.vs_ops[ZIO_TYPE_NULL],
5837 				    ZPROP_SRC_NONE);
5838 				continue;
5839 			case VDEV_PROP_OPS_READ:
5840 				vdev_prop_add_list(outnvl, propname, NULL,
5841 				    vd->vdev_stat.vs_ops[ZIO_TYPE_READ],
5842 				    ZPROP_SRC_NONE);
5843 				continue;
5844 			case VDEV_PROP_OPS_WRITE:
5845 				vdev_prop_add_list(outnvl, propname, NULL,
5846 				    vd->vdev_stat.vs_ops[ZIO_TYPE_WRITE],
5847 				    ZPROP_SRC_NONE);
5848 				continue;
5849 			case VDEV_PROP_OPS_FREE:
5850 				vdev_prop_add_list(outnvl, propname, NULL,
5851 				    vd->vdev_stat.vs_ops[ZIO_TYPE_FREE],
5852 				    ZPROP_SRC_NONE);
5853 				continue;
5854 			case VDEV_PROP_OPS_CLAIM:
5855 				vdev_prop_add_list(outnvl, propname, NULL,
5856 				    vd->vdev_stat.vs_ops[ZIO_TYPE_CLAIM],
5857 				    ZPROP_SRC_NONE);
5858 				continue;
5859 			case VDEV_PROP_OPS_TRIM:
5860 				/*
5861 				 * TRIM ops and bytes are reported to user
5862 				 * space as ZIO_TYPE_IOCTL.  This is done to
5863 				 * preserve the vdev_stat_t structure layout
5864 				 * for user space.
5865 				 */
5866 				vdev_prop_add_list(outnvl, propname, NULL,
5867 				    vd->vdev_stat.vs_ops[ZIO_TYPE_IOCTL],
5868 				    ZPROP_SRC_NONE);
5869 				continue;
5870 			case VDEV_PROP_BYTES_NULL:
5871 				vdev_prop_add_list(outnvl, propname, NULL,
5872 				    vd->vdev_stat.vs_bytes[ZIO_TYPE_NULL],
5873 				    ZPROP_SRC_NONE);
5874 				continue;
5875 			case VDEV_PROP_BYTES_READ:
5876 				vdev_prop_add_list(outnvl, propname, NULL,
5877 				    vd->vdev_stat.vs_bytes[ZIO_TYPE_READ],
5878 				    ZPROP_SRC_NONE);
5879 				continue;
5880 			case VDEV_PROP_BYTES_WRITE:
5881 				vdev_prop_add_list(outnvl, propname, NULL,
5882 				    vd->vdev_stat.vs_bytes[ZIO_TYPE_WRITE],
5883 				    ZPROP_SRC_NONE);
5884 				continue;
5885 			case VDEV_PROP_BYTES_FREE:
5886 				vdev_prop_add_list(outnvl, propname, NULL,
5887 				    vd->vdev_stat.vs_bytes[ZIO_TYPE_FREE],
5888 				    ZPROP_SRC_NONE);
5889 				continue;
5890 			case VDEV_PROP_BYTES_CLAIM:
5891 				vdev_prop_add_list(outnvl, propname, NULL,
5892 				    vd->vdev_stat.vs_bytes[ZIO_TYPE_CLAIM],
5893 				    ZPROP_SRC_NONE);
5894 				continue;
5895 			case VDEV_PROP_BYTES_TRIM:
5896 				/*
5897 				 * TRIM ops and bytes are reported to user
5898 				 * space as ZIO_TYPE_IOCTL.  This is done to
5899 				 * preserve the vdev_stat_t structure layout
5900 				 * for user space.
5901 				 */
5902 				vdev_prop_add_list(outnvl, propname, NULL,
5903 				    vd->vdev_stat.vs_bytes[ZIO_TYPE_IOCTL],
5904 				    ZPROP_SRC_NONE);
5905 				continue;
5906 			case VDEV_PROP_REMOVING:
5907 				vdev_prop_add_list(outnvl, propname, NULL,
5908 				    vd->vdev_removing, ZPROP_SRC_NONE);
5909 				continue;
5910 			/* Numeric Properites */
5911 			case VDEV_PROP_ALLOCATING:
5912 				src = ZPROP_SRC_LOCAL;
5913 				strval = NULL;
5914 
5915 				err = zap_lookup(mos, objid, nvpair_name(elem),
5916 				    sizeof (uint64_t), 1, &intval);
5917 				if (err == ENOENT) {
5918 					intval =
5919 					    vdev_prop_default_numeric(prop);
5920 					err = 0;
5921 				} else if (err)
5922 					break;
5923 				if (intval == vdev_prop_default_numeric(prop))
5924 					src = ZPROP_SRC_DEFAULT;
5925 
5926 				/* Leaf vdevs cannot have this property */
5927 				if (vd->vdev_mg == NULL &&
5928 				    vd->vdev_top != NULL) {
5929 					src = ZPROP_SRC_NONE;
5930 					intval = ZPROP_BOOLEAN_NA;
5931 				}
5932 
5933 				vdev_prop_add_list(outnvl, propname, strval,
5934 				    intval, src);
5935 				break;
5936 			/* Text Properties */
5937 			case VDEV_PROP_COMMENT:
5938 				/* Exists in the ZAP below */
5939 				/* FALLTHRU */
5940 			case VDEV_PROP_USERPROP:
5941 				/* User Properites */
5942 				src = ZPROP_SRC_LOCAL;
5943 
5944 				err = zap_length(mos, objid, nvpair_name(elem),
5945 				    &integer_size, &num_integers);
5946 				if (err)
5947 					break;
5948 
5949 				switch (integer_size) {
5950 				case 8:
5951 					/* User properties cannot be integers */
5952 					err = EINVAL;
5953 					break;
5954 				case 1:
5955 					/* string property */
5956 					strval = kmem_alloc(num_integers,
5957 					    KM_SLEEP);
5958 					err = zap_lookup(mos, objid,
5959 					    nvpair_name(elem), 1,
5960 					    num_integers, strval);
5961 					if (err) {
5962 						kmem_free(strval,
5963 						    num_integers);
5964 						break;
5965 					}
5966 					vdev_prop_add_list(outnvl, propname,
5967 					    strval, 0, src);
5968 					kmem_free(strval, num_integers);
5969 					break;
5970 				}
5971 				break;
5972 			default:
5973 				err = ENOENT;
5974 				break;
5975 			}
5976 			if (err)
5977 				break;
5978 		}
5979 	} else {
5980 		/*
5981 		 * Get all properties from the MOS vdev property object.
5982 		 */
5983 		zap_cursor_t zc;
5984 		zap_attribute_t za;
5985 		for (zap_cursor_init(&zc, mos, objid);
5986 		    (err = zap_cursor_retrieve(&zc, &za)) == 0;
5987 		    zap_cursor_advance(&zc)) {
5988 			intval = 0;
5989 			strval = NULL;
5990 			zprop_source_t src = ZPROP_SRC_DEFAULT;
5991 			propname = za.za_name;
5992 			prop = vdev_name_to_prop(propname);
5993 
5994 			switch (za.za_integer_length) {
5995 			case 8:
5996 				/* We do not allow integer user properties */
5997 				/* This is likely an internal value */
5998 				break;
5999 			case 1:
6000 				/* string property */
6001 				strval = kmem_alloc(za.za_num_integers,
6002 				    KM_SLEEP);
6003 				err = zap_lookup(mos, objid, za.za_name, 1,
6004 				    za.za_num_integers, strval);
6005 				if (err) {
6006 					kmem_free(strval, za.za_num_integers);
6007 					break;
6008 				}
6009 				vdev_prop_add_list(outnvl, propname, strval, 0,
6010 				    src);
6011 				kmem_free(strval, za.za_num_integers);
6012 				break;
6013 
6014 			default:
6015 				break;
6016 			}
6017 		}
6018 		zap_cursor_fini(&zc);
6019 	}
6020 
6021 	mutex_exit(&spa->spa_props_lock);
6022 	if (err && err != ENOENT) {
6023 		return (err);
6024 	}
6025 
6026 	return (0);
6027 }
6028 
6029 EXPORT_SYMBOL(vdev_fault);
6030 EXPORT_SYMBOL(vdev_degrade);
6031 EXPORT_SYMBOL(vdev_online);
6032 EXPORT_SYMBOL(vdev_offline);
6033 EXPORT_SYMBOL(vdev_clear);
6034 
6035 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, INT, ZMOD_RW,
6036 	"Target number of metaslabs per top-level vdev");
6037 
6038 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, INT, ZMOD_RW,
6039 	"Default limit for metaslab size");
6040 
6041 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, INT, ZMOD_RW,
6042 	"Minimum number of metaslabs per top-level vdev");
6043 
6044 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, INT, ZMOD_RW,
6045 	"Practical upper limit of total metaslabs per top-level vdev");
6046 
6047 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW,
6048 	"Rate limit slow IO (delay) events to this many per second");
6049 
6050 /* BEGIN CSTYLED */
6051 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW,
6052 	"Rate limit checksum events to this many checksum errors per second "
6053 	"(do not set below ZED threshold).");
6054 /* END CSTYLED */
6055 
6056 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW,
6057 	"Ignore errors during resilver/scrub");
6058 
6059 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW,
6060 	"Bypass vdev_validate()");
6061 
6062 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW,
6063 	"Disable cache flushes");
6064 
6065 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, INT, ZMOD_RW,
6066 	"Minimum number of metaslabs required to dedicate one for log blocks");
6067 
6068 /* BEGIN CSTYLED */
6069 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift,
6070 	param_set_min_auto_ashift, param_get_ulong, ZMOD_RW,
6071 	"Minimum ashift used when creating new top-level vdevs");
6072 
6073 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift,
6074 	param_set_max_auto_ashift, param_get_ulong, ZMOD_RW,
6075 	"Maximum ashift used when optimizing for logical -> physical sector "
6076 	"size on new top-level vdevs");
6077 /* END CSTYLED */
6078