xref: /titanic_44/usr/src/uts/common/fs/zfs/vdev.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/zfs_context.h>
28 #include <sys/fm/fs/zfs.h>
29 #include <sys/spa.h>
30 #include <sys/spa_impl.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/uberblock_impl.h>
35 #include <sys/metaslab.h>
36 #include <sys/metaslab_impl.h>
37 #include <sys/space_map.h>
38 #include <sys/zio.h>
39 #include <sys/zap.h>
40 #include <sys/fs/zfs.h>
41 #include <sys/arc.h>
42 
43 /*
44  * Virtual device management.
45  */
46 
47 static vdev_ops_t *vdev_ops_table[] = {
48 	&vdev_root_ops,
49 	&vdev_raidz_ops,
50 	&vdev_mirror_ops,
51 	&vdev_replacing_ops,
52 	&vdev_spare_ops,
53 	&vdev_disk_ops,
54 	&vdev_file_ops,
55 	&vdev_missing_ops,
56 	NULL
57 };
58 
59 /* maximum scrub/resilver I/O queue per leaf vdev */
60 int zfs_scrub_limit = 10;
61 
62 /*
63  * Given a vdev type, return the appropriate ops vector.
64  */
65 static vdev_ops_t *
66 vdev_getops(const char *type)
67 {
68 	vdev_ops_t *ops, **opspp;
69 
70 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
71 		if (strcmp(ops->vdev_op_type, type) == 0)
72 			break;
73 
74 	return (ops);
75 }
76 
77 /*
78  * Default asize function: return the MAX of psize with the asize of
79  * all children.  This is what's used by anything other than RAID-Z.
80  */
81 uint64_t
82 vdev_default_asize(vdev_t *vd, uint64_t psize)
83 {
84 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
85 	uint64_t csize;
86 	uint64_t c;
87 
88 	for (c = 0; c < vd->vdev_children; c++) {
89 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
90 		asize = MAX(asize, csize);
91 	}
92 
93 	return (asize);
94 }
95 
96 /*
97  * Get the replaceable or attachable device size.
98  * If the parent is a mirror or raidz, the replaceable size is the minimum
99  * psize of all its children. For the rest, just return our own psize.
100  *
101  * e.g.
102  *			psize	rsize
103  * root			-	-
104  *	mirror/raidz	-	-
105  *	    disk1	20g	20g
106  *	    disk2 	40g	20g
107  *	disk3 		80g	80g
108  */
109 uint64_t
110 vdev_get_rsize(vdev_t *vd)
111 {
112 	vdev_t *pvd, *cvd;
113 	uint64_t c, rsize;
114 
115 	pvd = vd->vdev_parent;
116 
117 	/*
118 	 * If our parent is NULL or the root, just return our own psize.
119 	 */
120 	if (pvd == NULL || pvd->vdev_parent == NULL)
121 		return (vd->vdev_psize);
122 
123 	rsize = 0;
124 
125 	for (c = 0; c < pvd->vdev_children; c++) {
126 		cvd = pvd->vdev_child[c];
127 		rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
128 	}
129 
130 	return (rsize);
131 }
132 
133 vdev_t *
134 vdev_lookup_top(spa_t *spa, uint64_t vdev)
135 {
136 	vdev_t *rvd = spa->spa_root_vdev;
137 
138 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
139 
140 	if (vdev < rvd->vdev_children) {
141 		ASSERT(rvd->vdev_child[vdev] != NULL);
142 		return (rvd->vdev_child[vdev]);
143 	}
144 
145 	return (NULL);
146 }
147 
148 vdev_t *
149 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
150 {
151 	int c;
152 	vdev_t *mvd;
153 
154 	if (vd->vdev_guid == guid)
155 		return (vd);
156 
157 	for (c = 0; c < vd->vdev_children; c++)
158 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
159 		    NULL)
160 			return (mvd);
161 
162 	return (NULL);
163 }
164 
165 void
166 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
167 {
168 	size_t oldsize, newsize;
169 	uint64_t id = cvd->vdev_id;
170 	vdev_t **newchild;
171 
172 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
173 	ASSERT(cvd->vdev_parent == NULL);
174 
175 	cvd->vdev_parent = pvd;
176 
177 	if (pvd == NULL)
178 		return;
179 
180 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
181 
182 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
183 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
184 	newsize = pvd->vdev_children * sizeof (vdev_t *);
185 
186 	newchild = kmem_zalloc(newsize, KM_SLEEP);
187 	if (pvd->vdev_child != NULL) {
188 		bcopy(pvd->vdev_child, newchild, oldsize);
189 		kmem_free(pvd->vdev_child, oldsize);
190 	}
191 
192 	pvd->vdev_child = newchild;
193 	pvd->vdev_child[id] = cvd;
194 
195 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
196 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
197 
198 	/*
199 	 * Walk up all ancestors to update guid sum.
200 	 */
201 	for (; pvd != NULL; pvd = pvd->vdev_parent)
202 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
203 
204 	if (cvd->vdev_ops->vdev_op_leaf)
205 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
206 }
207 
208 void
209 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
210 {
211 	int c;
212 	uint_t id = cvd->vdev_id;
213 
214 	ASSERT(cvd->vdev_parent == pvd);
215 
216 	if (pvd == NULL)
217 		return;
218 
219 	ASSERT(id < pvd->vdev_children);
220 	ASSERT(pvd->vdev_child[id] == cvd);
221 
222 	pvd->vdev_child[id] = NULL;
223 	cvd->vdev_parent = NULL;
224 
225 	for (c = 0; c < pvd->vdev_children; c++)
226 		if (pvd->vdev_child[c])
227 			break;
228 
229 	if (c == pvd->vdev_children) {
230 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
231 		pvd->vdev_child = NULL;
232 		pvd->vdev_children = 0;
233 	}
234 
235 	/*
236 	 * Walk up all ancestors to update guid sum.
237 	 */
238 	for (; pvd != NULL; pvd = pvd->vdev_parent)
239 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
240 
241 	if (cvd->vdev_ops->vdev_op_leaf)
242 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
243 }
244 
245 /*
246  * Remove any holes in the child array.
247  */
248 void
249 vdev_compact_children(vdev_t *pvd)
250 {
251 	vdev_t **newchild, *cvd;
252 	int oldc = pvd->vdev_children;
253 	int newc, c;
254 
255 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
256 
257 	for (c = newc = 0; c < oldc; c++)
258 		if (pvd->vdev_child[c])
259 			newc++;
260 
261 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
262 
263 	for (c = newc = 0; c < oldc; c++) {
264 		if ((cvd = pvd->vdev_child[c]) != NULL) {
265 			newchild[newc] = cvd;
266 			cvd->vdev_id = newc++;
267 		}
268 	}
269 
270 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
271 	pvd->vdev_child = newchild;
272 	pvd->vdev_children = newc;
273 }
274 
275 /*
276  * Allocate and minimally initialize a vdev_t.
277  */
278 static vdev_t *
279 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
280 {
281 	vdev_t *vd;
282 
283 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
284 
285 	if (spa->spa_root_vdev == NULL) {
286 		ASSERT(ops == &vdev_root_ops);
287 		spa->spa_root_vdev = vd;
288 	}
289 
290 	if (guid == 0) {
291 		if (spa->spa_root_vdev == vd) {
292 			/*
293 			 * The root vdev's guid will also be the pool guid,
294 			 * which must be unique among all pools.
295 			 */
296 			while (guid == 0 || spa_guid_exists(guid, 0))
297 				guid = spa_get_random(-1ULL);
298 		} else {
299 			/*
300 			 * Any other vdev's guid must be unique within the pool.
301 			 */
302 			while (guid == 0 ||
303 			    spa_guid_exists(spa_guid(spa), guid))
304 				guid = spa_get_random(-1ULL);
305 		}
306 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
307 	}
308 
309 	vd->vdev_spa = spa;
310 	vd->vdev_id = id;
311 	vd->vdev_guid = guid;
312 	vd->vdev_guid_sum = guid;
313 	vd->vdev_ops = ops;
314 	vd->vdev_state = VDEV_STATE_CLOSED;
315 
316 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
317 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
318 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
319 	space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
320 	space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
321 	txg_list_create(&vd->vdev_ms_list,
322 	    offsetof(struct metaslab, ms_txg_node));
323 	txg_list_create(&vd->vdev_dtl_list,
324 	    offsetof(struct vdev, vdev_dtl_node));
325 	vd->vdev_stat.vs_timestamp = gethrtime();
326 	vdev_queue_init(vd);
327 	vdev_cache_init(vd);
328 
329 	return (vd);
330 }
331 
332 /*
333  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
334  * creating a new vdev or loading an existing one - the behavior is slightly
335  * different for each case.
336  */
337 int
338 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
339     int alloctype)
340 {
341 	vdev_ops_t *ops;
342 	char *type;
343 	uint64_t guid = 0, islog, nparity;
344 	vdev_t *vd;
345 
346 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
347 
348 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
349 		return (EINVAL);
350 
351 	if ((ops = vdev_getops(type)) == NULL)
352 		return (EINVAL);
353 
354 	/*
355 	 * If this is a load, get the vdev guid from the nvlist.
356 	 * Otherwise, vdev_alloc_common() will generate one for us.
357 	 */
358 	if (alloctype == VDEV_ALLOC_LOAD) {
359 		uint64_t label_id;
360 
361 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
362 		    label_id != id)
363 			return (EINVAL);
364 
365 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
366 			return (EINVAL);
367 	} else if (alloctype == VDEV_ALLOC_SPARE) {
368 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
369 			return (EINVAL);
370 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
371 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
372 			return (EINVAL);
373 	}
374 
375 	/*
376 	 * The first allocated vdev must be of type 'root'.
377 	 */
378 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
379 		return (EINVAL);
380 
381 	/*
382 	 * Determine whether we're a log vdev.
383 	 */
384 	islog = 0;
385 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
386 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
387 		return (ENOTSUP);
388 
389 	/*
390 	 * Set the nparity property for RAID-Z vdevs.
391 	 */
392 	nparity = -1ULL;
393 	if (ops == &vdev_raidz_ops) {
394 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
395 		    &nparity) == 0) {
396 			/*
397 			 * Currently, we can only support 2 parity devices.
398 			 */
399 			if (nparity == 0 || nparity > 2)
400 				return (EINVAL);
401 			/*
402 			 * Older versions can only support 1 parity device.
403 			 */
404 			if (nparity == 2 &&
405 			    spa_version(spa) < SPA_VERSION_RAID6)
406 				return (ENOTSUP);
407 		} else {
408 			/*
409 			 * We require the parity to be specified for SPAs that
410 			 * support multiple parity levels.
411 			 */
412 			if (spa_version(spa) >= SPA_VERSION_RAID6)
413 				return (EINVAL);
414 			/*
415 			 * Otherwise, we default to 1 parity device for RAID-Z.
416 			 */
417 			nparity = 1;
418 		}
419 	} else {
420 		nparity = 0;
421 	}
422 	ASSERT(nparity != -1ULL);
423 
424 	vd = vdev_alloc_common(spa, id, guid, ops);
425 
426 	vd->vdev_islog = islog;
427 	vd->vdev_nparity = nparity;
428 
429 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
430 		vd->vdev_path = spa_strdup(vd->vdev_path);
431 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
432 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
433 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
434 	    &vd->vdev_physpath) == 0)
435 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
436 
437 	/*
438 	 * Set the whole_disk property.  If it's not specified, leave the value
439 	 * as -1.
440 	 */
441 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
442 	    &vd->vdev_wholedisk) != 0)
443 		vd->vdev_wholedisk = -1ULL;
444 
445 	/*
446 	 * Look for the 'not present' flag.  This will only be set if the device
447 	 * was not present at the time of import.
448 	 */
449 	if (!spa->spa_import_faulted)
450 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
451 		    &vd->vdev_not_present);
452 
453 	/*
454 	 * Get the alignment requirement.
455 	 */
456 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
457 
458 	/*
459 	 * If we're a top-level vdev, try to load the allocation parameters.
460 	 */
461 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
462 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
463 		    &vd->vdev_ms_array);
464 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
465 		    &vd->vdev_ms_shift);
466 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
467 		    &vd->vdev_asize);
468 	}
469 
470 	/*
471 	 * If we're a leaf vdev, try to load the DTL object and other state.
472 	 */
473 	if (vd->vdev_ops->vdev_op_leaf &&
474 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) {
475 		if (alloctype == VDEV_ALLOC_LOAD) {
476 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
477 			    &vd->vdev_dtl.smo_object);
478 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
479 			    &vd->vdev_unspare);
480 		}
481 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
482 		    &vd->vdev_offline);
483 
484 		/*
485 		 * When importing a pool, we want to ignore the persistent fault
486 		 * state, as the diagnosis made on another system may not be
487 		 * valid in the current context.
488 		 */
489 		if (spa->spa_load_state == SPA_LOAD_OPEN) {
490 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
491 			    &vd->vdev_faulted);
492 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
493 			    &vd->vdev_degraded);
494 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
495 			    &vd->vdev_removed);
496 		}
497 	}
498 
499 	/*
500 	 * Add ourselves to the parent's list of children.
501 	 */
502 	vdev_add_child(parent, vd);
503 
504 	*vdp = vd;
505 
506 	return (0);
507 }
508 
509 void
510 vdev_free(vdev_t *vd)
511 {
512 	int c;
513 	spa_t *spa = vd->vdev_spa;
514 
515 	/*
516 	 * vdev_free() implies closing the vdev first.  This is simpler than
517 	 * trying to ensure complicated semantics for all callers.
518 	 */
519 	vdev_close(vd);
520 
521 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
522 
523 	/*
524 	 * Free all children.
525 	 */
526 	for (c = 0; c < vd->vdev_children; c++)
527 		vdev_free(vd->vdev_child[c]);
528 
529 	ASSERT(vd->vdev_child == NULL);
530 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
531 
532 	/*
533 	 * Discard allocation state.
534 	 */
535 	if (vd == vd->vdev_top)
536 		vdev_metaslab_fini(vd);
537 
538 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
539 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
540 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
541 
542 	/*
543 	 * Remove this vdev from its parent's child list.
544 	 */
545 	vdev_remove_child(vd->vdev_parent, vd);
546 
547 	ASSERT(vd->vdev_parent == NULL);
548 
549 	/*
550 	 * Clean up vdev structure.
551 	 */
552 	vdev_queue_fini(vd);
553 	vdev_cache_fini(vd);
554 
555 	if (vd->vdev_path)
556 		spa_strfree(vd->vdev_path);
557 	if (vd->vdev_devid)
558 		spa_strfree(vd->vdev_devid);
559 	if (vd->vdev_physpath)
560 		spa_strfree(vd->vdev_physpath);
561 
562 	if (vd->vdev_isspare)
563 		spa_spare_remove(vd);
564 	if (vd->vdev_isl2cache)
565 		spa_l2cache_remove(vd);
566 
567 	txg_list_destroy(&vd->vdev_ms_list);
568 	txg_list_destroy(&vd->vdev_dtl_list);
569 	mutex_enter(&vd->vdev_dtl_lock);
570 	space_map_unload(&vd->vdev_dtl_map);
571 	space_map_destroy(&vd->vdev_dtl_map);
572 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
573 	space_map_destroy(&vd->vdev_dtl_scrub);
574 	mutex_exit(&vd->vdev_dtl_lock);
575 	mutex_destroy(&vd->vdev_dtl_lock);
576 	mutex_destroy(&vd->vdev_stat_lock);
577 	mutex_destroy(&vd->vdev_probe_lock);
578 
579 	if (vd == spa->spa_root_vdev)
580 		spa->spa_root_vdev = NULL;
581 
582 	kmem_free(vd, sizeof (vdev_t));
583 }
584 
585 /*
586  * Transfer top-level vdev state from svd to tvd.
587  */
588 static void
589 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
590 {
591 	spa_t *spa = svd->vdev_spa;
592 	metaslab_t *msp;
593 	vdev_t *vd;
594 	int t;
595 
596 	ASSERT(tvd == tvd->vdev_top);
597 
598 	tvd->vdev_ms_array = svd->vdev_ms_array;
599 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
600 	tvd->vdev_ms_count = svd->vdev_ms_count;
601 
602 	svd->vdev_ms_array = 0;
603 	svd->vdev_ms_shift = 0;
604 	svd->vdev_ms_count = 0;
605 
606 	tvd->vdev_mg = svd->vdev_mg;
607 	tvd->vdev_ms = svd->vdev_ms;
608 
609 	svd->vdev_mg = NULL;
610 	svd->vdev_ms = NULL;
611 
612 	if (tvd->vdev_mg != NULL)
613 		tvd->vdev_mg->mg_vd = tvd;
614 
615 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
616 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
617 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
618 
619 	svd->vdev_stat.vs_alloc = 0;
620 	svd->vdev_stat.vs_space = 0;
621 	svd->vdev_stat.vs_dspace = 0;
622 
623 	for (t = 0; t < TXG_SIZE; t++) {
624 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
625 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
626 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
627 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
628 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
629 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
630 	}
631 
632 	if (list_link_active(&svd->vdev_config_dirty_node)) {
633 		vdev_config_clean(svd);
634 		vdev_config_dirty(tvd);
635 	}
636 
637 	if (list_link_active(&svd->vdev_state_dirty_node)) {
638 		vdev_state_clean(svd);
639 		vdev_state_dirty(tvd);
640 	}
641 
642 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
643 	svd->vdev_deflate_ratio = 0;
644 
645 	tvd->vdev_islog = svd->vdev_islog;
646 	svd->vdev_islog = 0;
647 }
648 
649 static void
650 vdev_top_update(vdev_t *tvd, vdev_t *vd)
651 {
652 	int c;
653 
654 	if (vd == NULL)
655 		return;
656 
657 	vd->vdev_top = tvd;
658 
659 	for (c = 0; c < vd->vdev_children; c++)
660 		vdev_top_update(tvd, vd->vdev_child[c]);
661 }
662 
663 /*
664  * Add a mirror/replacing vdev above an existing vdev.
665  */
666 vdev_t *
667 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
668 {
669 	spa_t *spa = cvd->vdev_spa;
670 	vdev_t *pvd = cvd->vdev_parent;
671 	vdev_t *mvd;
672 
673 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
674 
675 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
676 
677 	mvd->vdev_asize = cvd->vdev_asize;
678 	mvd->vdev_ashift = cvd->vdev_ashift;
679 	mvd->vdev_state = cvd->vdev_state;
680 
681 	vdev_remove_child(pvd, cvd);
682 	vdev_add_child(pvd, mvd);
683 	cvd->vdev_id = mvd->vdev_children;
684 	vdev_add_child(mvd, cvd);
685 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
686 
687 	if (mvd == mvd->vdev_top)
688 		vdev_top_transfer(cvd, mvd);
689 
690 	return (mvd);
691 }
692 
693 /*
694  * Remove a 1-way mirror/replacing vdev from the tree.
695  */
696 void
697 vdev_remove_parent(vdev_t *cvd)
698 {
699 	vdev_t *mvd = cvd->vdev_parent;
700 	vdev_t *pvd = mvd->vdev_parent;
701 
702 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
703 
704 	ASSERT(mvd->vdev_children == 1);
705 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
706 	    mvd->vdev_ops == &vdev_replacing_ops ||
707 	    mvd->vdev_ops == &vdev_spare_ops);
708 	cvd->vdev_ashift = mvd->vdev_ashift;
709 
710 	vdev_remove_child(mvd, cvd);
711 	vdev_remove_child(pvd, mvd);
712 	/*
713 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
714 	 * Otherwise, we could have detached an offline device, and when we
715 	 * go to import the pool we'll think we have two top-level vdevs,
716 	 * instead of a different version of the same top-level vdev.
717 	 */
718 	if (mvd->vdev_top == mvd)
719 		cvd->vdev_guid = cvd->vdev_guid_sum = mvd->vdev_guid;
720 	cvd->vdev_id = mvd->vdev_id;
721 	vdev_add_child(pvd, cvd);
722 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
723 
724 	if (cvd == cvd->vdev_top)
725 		vdev_top_transfer(mvd, cvd);
726 
727 	ASSERT(mvd->vdev_children == 0);
728 	vdev_free(mvd);
729 }
730 
731 int
732 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
733 {
734 	spa_t *spa = vd->vdev_spa;
735 	objset_t *mos = spa->spa_meta_objset;
736 	metaslab_class_t *mc;
737 	uint64_t m;
738 	uint64_t oldc = vd->vdev_ms_count;
739 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
740 	metaslab_t **mspp;
741 	int error;
742 
743 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
744 		return (0);
745 
746 	ASSERT(oldc <= newc);
747 
748 	if (vd->vdev_islog)
749 		mc = spa->spa_log_class;
750 	else
751 		mc = spa->spa_normal_class;
752 
753 	if (vd->vdev_mg == NULL)
754 		vd->vdev_mg = metaslab_group_create(mc, vd);
755 
756 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
757 
758 	if (oldc != 0) {
759 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
760 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
761 	}
762 
763 	vd->vdev_ms = mspp;
764 	vd->vdev_ms_count = newc;
765 
766 	for (m = oldc; m < newc; m++) {
767 		space_map_obj_t smo = { 0, 0, 0 };
768 		if (txg == 0) {
769 			uint64_t object = 0;
770 			error = dmu_read(mos, vd->vdev_ms_array,
771 			    m * sizeof (uint64_t), sizeof (uint64_t), &object);
772 			if (error)
773 				return (error);
774 			if (object != 0) {
775 				dmu_buf_t *db;
776 				error = dmu_bonus_hold(mos, object, FTAG, &db);
777 				if (error)
778 					return (error);
779 				ASSERT3U(db->db_size, >=, sizeof (smo));
780 				bcopy(db->db_data, &smo, sizeof (smo));
781 				ASSERT3U(smo.smo_object, ==, object);
782 				dmu_buf_rele(db, FTAG);
783 			}
784 		}
785 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
786 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
787 	}
788 
789 	return (0);
790 }
791 
792 void
793 vdev_metaslab_fini(vdev_t *vd)
794 {
795 	uint64_t m;
796 	uint64_t count = vd->vdev_ms_count;
797 
798 	if (vd->vdev_ms != NULL) {
799 		for (m = 0; m < count; m++)
800 			if (vd->vdev_ms[m] != NULL)
801 				metaslab_fini(vd->vdev_ms[m]);
802 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
803 		vd->vdev_ms = NULL;
804 	}
805 }
806 
807 typedef struct vdev_probe_stats {
808 	boolean_t	vps_readable;
809 	boolean_t	vps_writeable;
810 	int		vps_flags;
811 	zio_t		*vps_root;
812 	vdev_t		*vps_vd;
813 } vdev_probe_stats_t;
814 
815 static void
816 vdev_probe_done(zio_t *zio)
817 {
818 	vdev_probe_stats_t *vps = zio->io_private;
819 	vdev_t *vd = vps->vps_vd;
820 
821 	if (zio->io_type == ZIO_TYPE_READ) {
822 		ASSERT(zio->io_vd == vd);
823 		if (zio->io_error == 0)
824 			vps->vps_readable = 1;
825 		if (zio->io_error == 0 && (spa_mode & FWRITE)) {
826 			zio_nowait(zio_write_phys(vps->vps_root, vd,
827 			    zio->io_offset, zio->io_size, zio->io_data,
828 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
829 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
830 		} else {
831 			zio_buf_free(zio->io_data, zio->io_size);
832 		}
833 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
834 		ASSERT(zio->io_vd == vd);
835 		if (zio->io_error == 0)
836 			vps->vps_writeable = 1;
837 		zio_buf_free(zio->io_data, zio->io_size);
838 	} else if (zio->io_type == ZIO_TYPE_NULL) {
839 		ASSERT(zio->io_vd == NULL);
840 		ASSERT(zio == vps->vps_root);
841 
842 		vd->vdev_cant_read |= !vps->vps_readable;
843 		vd->vdev_cant_write |= !vps->vps_writeable;
844 
845 		if (vdev_readable(vd) &&
846 		    (vdev_writeable(vd) || !(spa_mode & FWRITE))) {
847 			zio->io_error = 0;
848 		} else {
849 			ASSERT(zio->io_error != 0);
850 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
851 			    zio->io_spa, vd, NULL, 0, 0);
852 			zio->io_error = ENXIO;
853 		}
854 		kmem_free(vps, sizeof (*vps));
855 	}
856 }
857 
858 /*
859  * Determine whether this device is accessible by reading and writing
860  * to several known locations: the pad regions of each vdev label
861  * but the first (which we leave alone in case it contains a VTOC).
862  */
863 zio_t *
864 vdev_probe(vdev_t *vd, zio_t *pio)
865 {
866 	spa_t *spa = vd->vdev_spa;
867 	vdev_probe_stats_t *vps;
868 	zio_t *zio;
869 
870 	vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
871 
872 	vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
873 	    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_DONT_RETRY;
874 
875 	if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
876 		/*
877 		 * vdev_cant_read and vdev_cant_write can only transition
878 		 * from TRUE to FALSE when we have the SCL_ZIO lock as writer;
879 		 * otherwise they can only transition from FALSE to TRUE.
880 		 * This ensures that any zio looking at these values can
881 		 * assume that failures persist for the life of the I/O.
882 		 * That's important because when a device has intermittent
883 		 * connectivity problems, we want to ensure that they're
884 		 * ascribed to the device (ENXIO) and not the zio (EIO).
885 		 *
886 		 * Since we hold SCL_ZIO as writer here, clear both values
887 		 * so the probe can reevaluate from first principles.
888 		 */
889 		vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
890 		vd->vdev_cant_read = B_FALSE;
891 		vd->vdev_cant_write = B_FALSE;
892 	}
893 
894 	ASSERT(vd->vdev_ops->vdev_op_leaf);
895 
896 	zio = zio_null(pio, spa, vdev_probe_done, vps, vps->vps_flags);
897 
898 	vps->vps_root = zio;
899 	vps->vps_vd = vd;
900 
901 	for (int l = 1; l < VDEV_LABELS; l++) {
902 		zio_nowait(zio_read_phys(zio, vd,
903 		    vdev_label_offset(vd->vdev_psize, l,
904 		    offsetof(vdev_label_t, vl_pad)),
905 		    VDEV_SKIP_SIZE, zio_buf_alloc(VDEV_SKIP_SIZE),
906 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
907 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
908 	}
909 
910 	return (zio);
911 }
912 
913 /*
914  * Prepare a virtual device for access.
915  */
916 int
917 vdev_open(vdev_t *vd)
918 {
919 	int error;
920 	int c;
921 	uint64_t osize = 0;
922 	uint64_t asize, psize;
923 	uint64_t ashift = 0;
924 
925 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
926 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
927 	    vd->vdev_state == VDEV_STATE_OFFLINE);
928 
929 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
930 
931 	if (!vd->vdev_removed && vd->vdev_faulted) {
932 		ASSERT(vd->vdev_children == 0);
933 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
934 		    VDEV_AUX_ERR_EXCEEDED);
935 		return (ENXIO);
936 	} else if (vd->vdev_offline) {
937 		ASSERT(vd->vdev_children == 0);
938 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
939 		return (ENXIO);
940 	}
941 
942 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
943 
944 	if (zio_injection_enabled && error == 0)
945 		error = zio_handle_device_injection(vd, ENXIO);
946 
947 	if (error) {
948 		if (vd->vdev_removed &&
949 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
950 			vd->vdev_removed = B_FALSE;
951 
952 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
953 		    vd->vdev_stat.vs_aux);
954 		return (error);
955 	}
956 
957 	vd->vdev_removed = B_FALSE;
958 
959 	if (vd->vdev_degraded) {
960 		ASSERT(vd->vdev_children == 0);
961 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
962 		    VDEV_AUX_ERR_EXCEEDED);
963 	} else {
964 		vd->vdev_state = VDEV_STATE_HEALTHY;
965 	}
966 
967 	for (c = 0; c < vd->vdev_children; c++)
968 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
969 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
970 			    VDEV_AUX_NONE);
971 			break;
972 		}
973 
974 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
975 
976 	if (vd->vdev_children == 0) {
977 		if (osize < SPA_MINDEVSIZE) {
978 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
979 			    VDEV_AUX_TOO_SMALL);
980 			return (EOVERFLOW);
981 		}
982 		psize = osize;
983 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
984 	} else {
985 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
986 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
987 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
988 			    VDEV_AUX_TOO_SMALL);
989 			return (EOVERFLOW);
990 		}
991 		psize = 0;
992 		asize = osize;
993 	}
994 
995 	vd->vdev_psize = psize;
996 
997 	if (vd->vdev_asize == 0) {
998 		/*
999 		 * This is the first-ever open, so use the computed values.
1000 		 * For testing purposes, a higher ashift can be requested.
1001 		 */
1002 		vd->vdev_asize = asize;
1003 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1004 	} else {
1005 		/*
1006 		 * Make sure the alignment requirement hasn't increased.
1007 		 */
1008 		if (ashift > vd->vdev_top->vdev_ashift) {
1009 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1010 			    VDEV_AUX_BAD_LABEL);
1011 			return (EINVAL);
1012 		}
1013 
1014 		/*
1015 		 * Make sure the device hasn't shrunk.
1016 		 */
1017 		if (asize < vd->vdev_asize) {
1018 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1019 			    VDEV_AUX_BAD_LABEL);
1020 			return (EINVAL);
1021 		}
1022 
1023 		/*
1024 		 * If all children are healthy and the asize has increased,
1025 		 * then we've experienced dynamic LUN growth.
1026 		 */
1027 		if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1028 		    asize > vd->vdev_asize) {
1029 			vd->vdev_asize = asize;
1030 		}
1031 	}
1032 
1033 	/*
1034 	 * Ensure we can issue some IO before declaring the
1035 	 * vdev open for business.
1036 	 */
1037 	if (vd->vdev_ops->vdev_op_leaf &&
1038 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1039 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1040 		    VDEV_AUX_IO_FAILURE);
1041 		return (error);
1042 	}
1043 
1044 	/*
1045 	 * If this is a top-level vdev, compute the raidz-deflation
1046 	 * ratio.  Note, we hard-code in 128k (1<<17) because it is the
1047 	 * current "typical" blocksize.  Even if SPA_MAXBLOCKSIZE
1048 	 * changes, this algorithm must never change, or we will
1049 	 * inconsistently account for existing bp's.
1050 	 */
1051 	if (vd->vdev_top == vd) {
1052 		vd->vdev_deflate_ratio = (1<<17) /
1053 		    (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
1054 	}
1055 
1056 	/*
1057 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1058 	 * resilver.  But don't do this if we are doing a reopen for a
1059 	 * scrub, since this would just restart the scrub we are already
1060 	 * doing.
1061 	 */
1062 	if (vd->vdev_children == 0 && !vd->vdev_spa->spa_scrub_reopen) {
1063 		mutex_enter(&vd->vdev_dtl_lock);
1064 		if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd))
1065 			spa_async_request(vd->vdev_spa, SPA_ASYNC_RESILVER);
1066 		mutex_exit(&vd->vdev_dtl_lock);
1067 	}
1068 
1069 	return (0);
1070 }
1071 
1072 /*
1073  * Called once the vdevs are all opened, this routine validates the label
1074  * contents.  This needs to be done before vdev_load() so that we don't
1075  * inadvertently do repair I/Os to the wrong device.
1076  *
1077  * This function will only return failure if one of the vdevs indicates that it
1078  * has since been destroyed or exported.  This is only possible if
1079  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1080  * will be updated but the function will return 0.
1081  */
1082 int
1083 vdev_validate(vdev_t *vd)
1084 {
1085 	spa_t *spa = vd->vdev_spa;
1086 	int c;
1087 	nvlist_t *label;
1088 	uint64_t guid, top_guid;
1089 	uint64_t state;
1090 
1091 	for (c = 0; c < vd->vdev_children; c++)
1092 		if (vdev_validate(vd->vdev_child[c]) != 0)
1093 			return (EBADF);
1094 
1095 	/*
1096 	 * If the device has already failed, or was marked offline, don't do
1097 	 * any further validation.  Otherwise, label I/O will fail and we will
1098 	 * overwrite the previous state.
1099 	 */
1100 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1101 
1102 		if ((label = vdev_label_read_config(vd)) == NULL) {
1103 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1104 			    VDEV_AUX_BAD_LABEL);
1105 			return (0);
1106 		}
1107 
1108 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
1109 		    &guid) != 0 || guid != spa_guid(spa)) {
1110 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1111 			    VDEV_AUX_CORRUPT_DATA);
1112 			nvlist_free(label);
1113 			return (0);
1114 		}
1115 
1116 		/*
1117 		 * If this vdev just became a top-level vdev because its
1118 		 * sibling was detached, it will have adopted the parent's
1119 		 * vdev guid -- but the label may or may not be on disk yet.
1120 		 * Fortunately, either version of the label will have the
1121 		 * same top guid, so if we're a top-level vdev, we can
1122 		 * safely compare to that instead.
1123 		 */
1124 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1125 		    &guid) != 0 ||
1126 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1127 		    &top_guid) != 0 ||
1128 		    (vd->vdev_guid != guid &&
1129 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1130 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1131 			    VDEV_AUX_CORRUPT_DATA);
1132 			nvlist_free(label);
1133 			return (0);
1134 		}
1135 
1136 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1137 		    &state) != 0) {
1138 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1139 			    VDEV_AUX_CORRUPT_DATA);
1140 			nvlist_free(label);
1141 			return (0);
1142 		}
1143 
1144 		nvlist_free(label);
1145 
1146 		if (spa->spa_load_state == SPA_LOAD_OPEN &&
1147 		    state != POOL_STATE_ACTIVE)
1148 			return (EBADF);
1149 
1150 		/*
1151 		 * If we were able to open and validate a vdev that was
1152 		 * previously marked permanently unavailable, clear that state
1153 		 * now.
1154 		 */
1155 		if (vd->vdev_not_present)
1156 			vd->vdev_not_present = 0;
1157 	}
1158 
1159 	return (0);
1160 }
1161 
1162 /*
1163  * Close a virtual device.
1164  */
1165 void
1166 vdev_close(vdev_t *vd)
1167 {
1168 	vd->vdev_ops->vdev_op_close(vd);
1169 
1170 	vdev_cache_purge(vd);
1171 
1172 	/*
1173 	 * We record the previous state before we close it, so  that if we are
1174 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1175 	 * it's still faulted.
1176 	 */
1177 	vd->vdev_prevstate = vd->vdev_state;
1178 
1179 	if (vd->vdev_offline)
1180 		vd->vdev_state = VDEV_STATE_OFFLINE;
1181 	else
1182 		vd->vdev_state = VDEV_STATE_CLOSED;
1183 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1184 }
1185 
1186 void
1187 vdev_reopen(vdev_t *vd)
1188 {
1189 	spa_t *spa = vd->vdev_spa;
1190 
1191 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1192 
1193 	vdev_close(vd);
1194 	(void) vdev_open(vd);
1195 
1196 	/*
1197 	 * Call vdev_validate() here to make sure we have the same device.
1198 	 * Otherwise, a device with an invalid label could be successfully
1199 	 * opened in response to vdev_reopen().
1200 	 */
1201 	if (vd->vdev_aux) {
1202 		(void) vdev_validate_aux(vd);
1203 		if (vdev_readable(vd) && vdev_writeable(vd) &&
1204 		    !l2arc_vdev_present(vd)) {
1205 			uint64_t size = vdev_get_rsize(vd);
1206 			l2arc_add_vdev(spa, vd,
1207 			    VDEV_LABEL_START_SIZE,
1208 			    size - VDEV_LABEL_START_SIZE);
1209 		}
1210 	} else {
1211 		(void) vdev_validate(vd);
1212 	}
1213 
1214 	/*
1215 	 * Reassess parent vdev's health.
1216 	 */
1217 	vdev_propagate_state(vd);
1218 }
1219 
1220 int
1221 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1222 {
1223 	int error;
1224 
1225 	/*
1226 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1227 	 * For a create, however, we want to fail the request if
1228 	 * there are any components we can't open.
1229 	 */
1230 	error = vdev_open(vd);
1231 
1232 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1233 		vdev_close(vd);
1234 		return (error ? error : ENXIO);
1235 	}
1236 
1237 	/*
1238 	 * Recursively initialize all labels.
1239 	 */
1240 	if ((error = vdev_label_init(vd, txg, isreplacing ?
1241 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1242 		vdev_close(vd);
1243 		return (error);
1244 	}
1245 
1246 	return (0);
1247 }
1248 
1249 /*
1250  * The is the latter half of vdev_create().  It is distinct because it
1251  * involves initiating transactions in order to do metaslab creation.
1252  * For creation, we want to try to create all vdevs at once and then undo it
1253  * if anything fails; this is much harder if we have pending transactions.
1254  */
1255 void
1256 vdev_init(vdev_t *vd, uint64_t txg)
1257 {
1258 	/*
1259 	 * Aim for roughly 200 metaslabs per vdev.
1260 	 */
1261 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1262 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1263 
1264 	/*
1265 	 * Initialize the vdev's metaslabs.  This can't fail because
1266 	 * there's nothing to read when creating all new metaslabs.
1267 	 */
1268 	VERIFY(vdev_metaslab_init(vd, txg) == 0);
1269 }
1270 
1271 void
1272 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1273 {
1274 	ASSERT(vd == vd->vdev_top);
1275 	ASSERT(ISP2(flags));
1276 
1277 	if (flags & VDD_METASLAB)
1278 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1279 
1280 	if (flags & VDD_DTL)
1281 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1282 
1283 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1284 }
1285 
1286 void
1287 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1288 {
1289 	mutex_enter(sm->sm_lock);
1290 	if (!space_map_contains(sm, txg, size))
1291 		space_map_add(sm, txg, size);
1292 	mutex_exit(sm->sm_lock);
1293 }
1294 
1295 int
1296 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1297 {
1298 	int dirty;
1299 
1300 	/*
1301 	 * Quick test without the lock -- covers the common case that
1302 	 * there are no dirty time segments.
1303 	 */
1304 	if (sm->sm_space == 0)
1305 		return (0);
1306 
1307 	mutex_enter(sm->sm_lock);
1308 	dirty = space_map_contains(sm, txg, size);
1309 	mutex_exit(sm->sm_lock);
1310 
1311 	return (dirty);
1312 }
1313 
1314 /*
1315  * Reassess DTLs after a config change or scrub completion.
1316  */
1317 void
1318 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1319 {
1320 	spa_t *spa = vd->vdev_spa;
1321 	int c;
1322 
1323 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
1324 
1325 	if (vd->vdev_children == 0) {
1326 		mutex_enter(&vd->vdev_dtl_lock);
1327 		if (scrub_txg != 0 &&
1328 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
1329 			/* XXX should check scrub_done? */
1330 			/*
1331 			 * We completed a scrub up to scrub_txg.  If we
1332 			 * did it without rebooting, then the scrub dtl
1333 			 * will be valid, so excise the old region and
1334 			 * fold in the scrub dtl.  Otherwise, leave the
1335 			 * dtl as-is if there was an error.
1336 			 */
1337 			space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1338 			space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1339 		}
1340 		if (scrub_done)
1341 			space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1342 		mutex_exit(&vd->vdev_dtl_lock);
1343 
1344 		if (txg != 0)
1345 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1346 		return;
1347 	}
1348 
1349 	/*
1350 	 * Make sure the DTLs are always correct under the scrub lock.
1351 	 */
1352 	if (vd == spa->spa_root_vdev)
1353 		mutex_enter(&spa->spa_scrub_lock);
1354 
1355 	mutex_enter(&vd->vdev_dtl_lock);
1356 	space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1357 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1358 	mutex_exit(&vd->vdev_dtl_lock);
1359 
1360 	for (c = 0; c < vd->vdev_children; c++) {
1361 		vdev_t *cvd = vd->vdev_child[c];
1362 		vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1363 		mutex_enter(&vd->vdev_dtl_lock);
1364 		space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1365 		space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1366 		mutex_exit(&vd->vdev_dtl_lock);
1367 	}
1368 
1369 	if (vd == spa->spa_root_vdev)
1370 		mutex_exit(&spa->spa_scrub_lock);
1371 }
1372 
1373 static int
1374 vdev_dtl_load(vdev_t *vd)
1375 {
1376 	spa_t *spa = vd->vdev_spa;
1377 	space_map_obj_t *smo = &vd->vdev_dtl;
1378 	objset_t *mos = spa->spa_meta_objset;
1379 	dmu_buf_t *db;
1380 	int error;
1381 
1382 	ASSERT(vd->vdev_children == 0);
1383 
1384 	if (smo->smo_object == 0)
1385 		return (0);
1386 
1387 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1388 		return (error);
1389 
1390 	ASSERT3U(db->db_size, >=, sizeof (*smo));
1391 	bcopy(db->db_data, smo, sizeof (*smo));
1392 	dmu_buf_rele(db, FTAG);
1393 
1394 	mutex_enter(&vd->vdev_dtl_lock);
1395 	error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1396 	mutex_exit(&vd->vdev_dtl_lock);
1397 
1398 	return (error);
1399 }
1400 
1401 void
1402 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1403 {
1404 	spa_t *spa = vd->vdev_spa;
1405 	space_map_obj_t *smo = &vd->vdev_dtl;
1406 	space_map_t *sm = &vd->vdev_dtl_map;
1407 	objset_t *mos = spa->spa_meta_objset;
1408 	space_map_t smsync;
1409 	kmutex_t smlock;
1410 	dmu_buf_t *db;
1411 	dmu_tx_t *tx;
1412 
1413 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1414 
1415 	if (vd->vdev_detached) {
1416 		if (smo->smo_object != 0) {
1417 			int err = dmu_object_free(mos, smo->smo_object, tx);
1418 			ASSERT3U(err, ==, 0);
1419 			smo->smo_object = 0;
1420 		}
1421 		dmu_tx_commit(tx);
1422 		return;
1423 	}
1424 
1425 	if (smo->smo_object == 0) {
1426 		ASSERT(smo->smo_objsize == 0);
1427 		ASSERT(smo->smo_alloc == 0);
1428 		smo->smo_object = dmu_object_alloc(mos,
1429 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1430 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1431 		ASSERT(smo->smo_object != 0);
1432 		vdev_config_dirty(vd->vdev_top);
1433 	}
1434 
1435 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1436 
1437 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1438 	    &smlock);
1439 
1440 	mutex_enter(&smlock);
1441 
1442 	mutex_enter(&vd->vdev_dtl_lock);
1443 	space_map_walk(sm, space_map_add, &smsync);
1444 	mutex_exit(&vd->vdev_dtl_lock);
1445 
1446 	space_map_truncate(smo, mos, tx);
1447 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1448 
1449 	space_map_destroy(&smsync);
1450 
1451 	mutex_exit(&smlock);
1452 	mutex_destroy(&smlock);
1453 
1454 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1455 	dmu_buf_will_dirty(db, tx);
1456 	ASSERT3U(db->db_size, >=, sizeof (*smo));
1457 	bcopy(smo, db->db_data, sizeof (*smo));
1458 	dmu_buf_rele(db, FTAG);
1459 
1460 	dmu_tx_commit(tx);
1461 }
1462 
1463 /*
1464  * Determine if resilver is needed, and if so the txg range.
1465  */
1466 boolean_t
1467 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
1468 {
1469 	boolean_t needed = B_FALSE;
1470 	uint64_t thismin = UINT64_MAX;
1471 	uint64_t thismax = 0;
1472 
1473 	if (vd->vdev_children == 0) {
1474 		mutex_enter(&vd->vdev_dtl_lock);
1475 		if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd)) {
1476 			space_seg_t *ss;
1477 
1478 			ss = avl_first(&vd->vdev_dtl_map.sm_root);
1479 			thismin = ss->ss_start - 1;
1480 			ss = avl_last(&vd->vdev_dtl_map.sm_root);
1481 			thismax = ss->ss_end;
1482 			needed = B_TRUE;
1483 		}
1484 		mutex_exit(&vd->vdev_dtl_lock);
1485 	} else {
1486 		int c;
1487 		for (c = 0; c < vd->vdev_children; c++) {
1488 			vdev_t *cvd = vd->vdev_child[c];
1489 			uint64_t cmin, cmax;
1490 
1491 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
1492 				thismin = MIN(thismin, cmin);
1493 				thismax = MAX(thismax, cmax);
1494 				needed = B_TRUE;
1495 			}
1496 		}
1497 	}
1498 
1499 	if (needed && minp) {
1500 		*minp = thismin;
1501 		*maxp = thismax;
1502 	}
1503 	return (needed);
1504 }
1505 
1506 void
1507 vdev_load(vdev_t *vd)
1508 {
1509 	int c;
1510 
1511 	/*
1512 	 * Recursively load all children.
1513 	 */
1514 	for (c = 0; c < vd->vdev_children; c++)
1515 		vdev_load(vd->vdev_child[c]);
1516 
1517 	/*
1518 	 * If this is a top-level vdev, initialize its metaslabs.
1519 	 */
1520 	if (vd == vd->vdev_top &&
1521 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1522 	    vdev_metaslab_init(vd, 0) != 0))
1523 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1524 		    VDEV_AUX_CORRUPT_DATA);
1525 
1526 	/*
1527 	 * If this is a leaf vdev, load its DTL.
1528 	 */
1529 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1530 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1531 		    VDEV_AUX_CORRUPT_DATA);
1532 }
1533 
1534 /*
1535  * The special vdev case is used for hot spares and l2cache devices.  Its
1536  * sole purpose it to set the vdev state for the associated vdev.  To do this,
1537  * we make sure that we can open the underlying device, then try to read the
1538  * label, and make sure that the label is sane and that it hasn't been
1539  * repurposed to another pool.
1540  */
1541 int
1542 vdev_validate_aux(vdev_t *vd)
1543 {
1544 	nvlist_t *label;
1545 	uint64_t guid, version;
1546 	uint64_t state;
1547 
1548 	if (!vdev_readable(vd))
1549 		return (0);
1550 
1551 	if ((label = vdev_label_read_config(vd)) == NULL) {
1552 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1553 		    VDEV_AUX_CORRUPT_DATA);
1554 		return (-1);
1555 	}
1556 
1557 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1558 	    version > SPA_VERSION ||
1559 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1560 	    guid != vd->vdev_guid ||
1561 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1562 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1563 		    VDEV_AUX_CORRUPT_DATA);
1564 		nvlist_free(label);
1565 		return (-1);
1566 	}
1567 
1568 	/*
1569 	 * We don't actually check the pool state here.  If it's in fact in
1570 	 * use by another pool, we update this fact on the fly when requested.
1571 	 */
1572 	nvlist_free(label);
1573 	return (0);
1574 }
1575 
1576 void
1577 vdev_sync_done(vdev_t *vd, uint64_t txg)
1578 {
1579 	metaslab_t *msp;
1580 
1581 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1582 		metaslab_sync_done(msp, txg);
1583 }
1584 
1585 void
1586 vdev_sync(vdev_t *vd, uint64_t txg)
1587 {
1588 	spa_t *spa = vd->vdev_spa;
1589 	vdev_t *lvd;
1590 	metaslab_t *msp;
1591 	dmu_tx_t *tx;
1592 
1593 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1594 		ASSERT(vd == vd->vdev_top);
1595 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1596 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1597 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1598 		ASSERT(vd->vdev_ms_array != 0);
1599 		vdev_config_dirty(vd);
1600 		dmu_tx_commit(tx);
1601 	}
1602 
1603 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1604 		metaslab_sync(msp, txg);
1605 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1606 	}
1607 
1608 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1609 		vdev_dtl_sync(lvd, txg);
1610 
1611 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1612 }
1613 
1614 uint64_t
1615 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1616 {
1617 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1618 }
1619 
1620 /*
1621  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
1622  * not be opened, and no I/O is attempted.
1623  */
1624 int
1625 vdev_fault(spa_t *spa, uint64_t guid)
1626 {
1627 	vdev_t *vd;
1628 
1629 	spa_vdev_state_enter(spa);
1630 
1631 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1632 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1633 
1634 	if (!vd->vdev_ops->vdev_op_leaf)
1635 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1636 
1637 	/*
1638 	 * Faulted state takes precedence over degraded.
1639 	 */
1640 	vd->vdev_faulted = 1ULL;
1641 	vd->vdev_degraded = 0ULL;
1642 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
1643 
1644 	/*
1645 	 * If marking the vdev as faulted causes the top-level vdev to become
1646 	 * unavailable, then back off and simply mark the vdev as degraded
1647 	 * instead.
1648 	 */
1649 	if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
1650 		vd->vdev_degraded = 1ULL;
1651 		vd->vdev_faulted = 0ULL;
1652 
1653 		/*
1654 		 * If we reopen the device and it's not dead, only then do we
1655 		 * mark it degraded.
1656 		 */
1657 		vdev_reopen(vd);
1658 
1659 		if (vdev_readable(vd)) {
1660 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1661 			    VDEV_AUX_ERR_EXCEEDED);
1662 		}
1663 	}
1664 
1665 	return (spa_vdev_state_exit(spa, vd, 0));
1666 }
1667 
1668 /*
1669  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
1670  * user that something is wrong.  The vdev continues to operate as normal as far
1671  * as I/O is concerned.
1672  */
1673 int
1674 vdev_degrade(spa_t *spa, uint64_t guid)
1675 {
1676 	vdev_t *vd;
1677 
1678 	spa_vdev_state_enter(spa);
1679 
1680 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1681 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1682 
1683 	if (!vd->vdev_ops->vdev_op_leaf)
1684 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1685 
1686 	/*
1687 	 * If the vdev is already faulted, then don't do anything.
1688 	 */
1689 	if (vd->vdev_faulted || vd->vdev_degraded)
1690 		return (spa_vdev_state_exit(spa, NULL, 0));
1691 
1692 	vd->vdev_degraded = 1ULL;
1693 	if (!vdev_is_dead(vd))
1694 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1695 		    VDEV_AUX_ERR_EXCEEDED);
1696 
1697 	return (spa_vdev_state_exit(spa, vd, 0));
1698 }
1699 
1700 /*
1701  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
1702  * any attached spare device should be detached when the device finishes
1703  * resilvering.  Second, the online should be treated like a 'test' online case,
1704  * so no FMA events are generated if the device fails to open.
1705  */
1706 int
1707 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1708 {
1709 	vdev_t *vd;
1710 
1711 	if (spa_is_root(spa)) {
1712 		/*
1713 		 * if we're trying to online a device that's part of
1714 		 * the root pool, trigger an attach (if any) with only
1715 		 * the SCL_STATE lock held in order to avoid a deadlock
1716 		 * where modload tries to read from the disk
1717 		 */
1718 		spa_config_enter(spa, SCL_STATE, FTAG, RW_WRITER);
1719 		if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
1720 			spa_config_exit(spa, SCL_STATE, FTAG);
1721 			return (ENODEV);
1722 		}
1723 		if (!vd->vdev_ops->vdev_op_leaf) {
1724 			spa_config_exit(spa, SCL_STATE, FTAG);
1725 			return (ENOTSUP);
1726 		}
1727 		vdev_close(vd);
1728 		vdev_open(vd);
1729 		spa_config_exit(spa, SCL_STATE, FTAG);
1730 	}
1731 
1732 	spa_vdev_state_enter(spa);
1733 
1734 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1735 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1736 
1737 	if (!vd->vdev_ops->vdev_op_leaf)
1738 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1739 
1740 	vd->vdev_offline = B_FALSE;
1741 	vd->vdev_tmpoffline = B_FALSE;
1742 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
1743 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
1744 	vdev_reopen(vd->vdev_top);
1745 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
1746 
1747 	if (newstate)
1748 		*newstate = vd->vdev_state;
1749 	if ((flags & ZFS_ONLINE_UNSPARE) &&
1750 	    !vdev_is_dead(vd) && vd->vdev_parent &&
1751 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
1752 	    vd->vdev_parent->vdev_child[0] == vd)
1753 		vd->vdev_unspare = B_TRUE;
1754 
1755 	(void) spa_vdev_state_exit(spa, vd, 0);
1756 
1757 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
1758 
1759 	return (0);
1760 }
1761 
1762 int
1763 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1764 {
1765 	vdev_t *vd;
1766 
1767 	spa_vdev_state_enter(spa);
1768 
1769 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1770 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1771 
1772 	if (!vd->vdev_ops->vdev_op_leaf)
1773 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1774 
1775 	/*
1776 	 * If the device isn't already offline, try to offline it.
1777 	 */
1778 	if (!vd->vdev_offline) {
1779 		/*
1780 		 * If this device's top-level vdev has a non-empty DTL,
1781 		 * don't allow the device to be offlined.
1782 		 *
1783 		 * XXX -- make this more precise by allowing the offline
1784 		 * as long as the remaining devices don't have any DTL holes.
1785 		 */
1786 		if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1787 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
1788 
1789 		/*
1790 		 * Offline this device and reopen its top-level vdev.
1791 		 * If this action results in the top-level vdev becoming
1792 		 * unusable, undo it and fail the request.
1793 		 */
1794 		vd->vdev_offline = B_TRUE;
1795 		vdev_reopen(vd->vdev_top);
1796 		if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
1797 			vd->vdev_offline = B_FALSE;
1798 			vdev_reopen(vd->vdev_top);
1799 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
1800 		}
1801 	}
1802 
1803 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
1804 
1805 	return (spa_vdev_state_exit(spa, vd, 0));
1806 }
1807 
1808 /*
1809  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
1810  * vdev_offline(), we assume the spa config is locked.  We also clear all
1811  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
1812  */
1813 void
1814 vdev_clear(spa_t *spa, vdev_t *vd)
1815 {
1816 	vdev_t *rvd = spa->spa_root_vdev;
1817 
1818 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1819 
1820 	if (vd == NULL)
1821 		vd = rvd;
1822 
1823 	vd->vdev_stat.vs_read_errors = 0;
1824 	vd->vdev_stat.vs_write_errors = 0;
1825 	vd->vdev_stat.vs_checksum_errors = 0;
1826 
1827 	for (int c = 0; c < vd->vdev_children; c++)
1828 		vdev_clear(spa, vd->vdev_child[c]);
1829 
1830 	/*
1831 	 * If we're in the FAULTED state or have experienced failed I/O, then
1832 	 * clear the persistent state and attempt to reopen the device.  We
1833 	 * also mark the vdev config dirty, so that the new faulted state is
1834 	 * written out to disk.
1835 	 */
1836 	if (vd->vdev_faulted || vd->vdev_degraded ||
1837 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
1838 
1839 		vd->vdev_faulted = vd->vdev_degraded = 0;
1840 		vd->vdev_cant_read = B_FALSE;
1841 		vd->vdev_cant_write = B_FALSE;
1842 
1843 		vdev_reopen(vd);
1844 
1845 		if (vd != rvd)
1846 			vdev_state_dirty(vd->vdev_top);
1847 
1848 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
1849 			spa_async_request(spa, SPA_ASYNC_RESILVER);
1850 
1851 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
1852 	}
1853 }
1854 
1855 boolean_t
1856 vdev_is_dead(vdev_t *vd)
1857 {
1858 	return (vd->vdev_state < VDEV_STATE_DEGRADED);
1859 }
1860 
1861 boolean_t
1862 vdev_readable(vdev_t *vd)
1863 {
1864 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
1865 }
1866 
1867 boolean_t
1868 vdev_writeable(vdev_t *vd)
1869 {
1870 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
1871 }
1872 
1873 boolean_t
1874 vdev_allocatable(vdev_t *vd)
1875 {
1876 	/*
1877 	 * We currently allow allocations from vdevs which maybe in the
1878 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
1879 	 * fails to reopen then we'll catch it later when we're holding
1880 	 * the proper locks.
1881 	 */
1882 	return (!(vdev_is_dead(vd) && vd->vdev_state != VDEV_STATE_CLOSED) &&
1883 	    !vd->vdev_cant_write);
1884 }
1885 
1886 boolean_t
1887 vdev_accessible(vdev_t *vd, zio_t *zio)
1888 {
1889 	ASSERT(zio->io_vd == vd);
1890 
1891 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
1892 		return (B_FALSE);
1893 
1894 	if (zio->io_type == ZIO_TYPE_READ)
1895 		return (!vd->vdev_cant_read);
1896 
1897 	if (zio->io_type == ZIO_TYPE_WRITE)
1898 		return (!vd->vdev_cant_write);
1899 
1900 	return (B_TRUE);
1901 }
1902 
1903 /*
1904  * Get statistics for the given vdev.
1905  */
1906 void
1907 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1908 {
1909 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1910 
1911 	mutex_enter(&vd->vdev_stat_lock);
1912 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1913 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
1914 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1915 	vs->vs_state = vd->vdev_state;
1916 	vs->vs_rsize = vdev_get_rsize(vd);
1917 	mutex_exit(&vd->vdev_stat_lock);
1918 
1919 	/*
1920 	 * If we're getting stats on the root vdev, aggregate the I/O counts
1921 	 * over all top-level vdevs (i.e. the direct children of the root).
1922 	 */
1923 	if (vd == rvd) {
1924 		for (int c = 0; c < rvd->vdev_children; c++) {
1925 			vdev_t *cvd = rvd->vdev_child[c];
1926 			vdev_stat_t *cvs = &cvd->vdev_stat;
1927 
1928 			mutex_enter(&vd->vdev_stat_lock);
1929 			for (int t = 0; t < ZIO_TYPES; t++) {
1930 				vs->vs_ops[t] += cvs->vs_ops[t];
1931 				vs->vs_bytes[t] += cvs->vs_bytes[t];
1932 			}
1933 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
1934 			mutex_exit(&vd->vdev_stat_lock);
1935 		}
1936 	}
1937 }
1938 
1939 void
1940 vdev_clear_stats(vdev_t *vd)
1941 {
1942 	mutex_enter(&vd->vdev_stat_lock);
1943 	vd->vdev_stat.vs_space = 0;
1944 	vd->vdev_stat.vs_dspace = 0;
1945 	vd->vdev_stat.vs_alloc = 0;
1946 	mutex_exit(&vd->vdev_stat_lock);
1947 }
1948 
1949 void
1950 vdev_stat_update(zio_t *zio, uint64_t psize)
1951 {
1952 	vdev_t *rvd = zio->io_spa->spa_root_vdev;
1953 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
1954 	vdev_t *pvd;
1955 	uint64_t txg = zio->io_txg;
1956 	vdev_stat_t *vs = &vd->vdev_stat;
1957 	zio_type_t type = zio->io_type;
1958 	int flags = zio->io_flags;
1959 
1960 	/*
1961 	 * If this i/o is a gang leader, it didn't do any actual work.
1962 	 */
1963 	if (zio->io_gang_tree)
1964 		return;
1965 
1966 	if (zio->io_error == 0) {
1967 		/*
1968 		 * If this is a root i/o, don't count it -- we've already
1969 		 * counted the top-level vdevs, and vdev_get_stats() will
1970 		 * aggregate them when asked.  This reduces contention on
1971 		 * the root vdev_stat_lock and implicitly handles blocks
1972 		 * that compress away to holes, for which there is no i/o.
1973 		 * (Holes never create vdev children, so all the counters
1974 		 * remain zero, which is what we want.)
1975 		 *
1976 		 * Note: this only applies to successful i/o (io_error == 0)
1977 		 * because unlike i/o counts, errors are not additive.
1978 		 * When reading a ditto block, for example, failure of
1979 		 * one top-level vdev does not imply a root-level error.
1980 		 */
1981 		if (vd == rvd)
1982 			return;
1983 
1984 		ASSERT(vd == zio->io_vd);
1985 		if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1986 			mutex_enter(&vd->vdev_stat_lock);
1987 			vs->vs_ops[type]++;
1988 			vs->vs_bytes[type] += psize;
1989 			mutex_exit(&vd->vdev_stat_lock);
1990 		}
1991 		if (flags & ZIO_FLAG_IO_REPAIR) {
1992 			ASSERT(zio->io_delegate_list == NULL);
1993 			mutex_enter(&vd->vdev_stat_lock);
1994 			if (flags & ZIO_FLAG_SCRUB_THREAD)
1995 				vs->vs_scrub_repaired += psize;
1996 			else
1997 				vs->vs_self_healed += psize;
1998 			mutex_exit(&vd->vdev_stat_lock);
1999 		}
2000 		return;
2001 	}
2002 
2003 	if (flags & ZIO_FLAG_SPECULATIVE)
2004 		return;
2005 
2006 	mutex_enter(&vd->vdev_stat_lock);
2007 	if (type == ZIO_TYPE_READ) {
2008 		if (zio->io_error == ECKSUM)
2009 			vs->vs_checksum_errors++;
2010 		else
2011 			vs->vs_read_errors++;
2012 	}
2013 	if (type == ZIO_TYPE_WRITE)
2014 		vs->vs_write_errors++;
2015 	mutex_exit(&vd->vdev_stat_lock);
2016 
2017 	if (type == ZIO_TYPE_WRITE && txg != 0 && vd->vdev_children == 0) {
2018 		if (flags & ZIO_FLAG_SCRUB_THREAD) {
2019 			ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2020 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
2021 				vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
2022 		}
2023 		if (!(flags & ZIO_FLAG_IO_REPAIR)) {
2024 			if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
2025 				return;
2026 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2027 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
2028 				vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
2029 		}
2030 	}
2031 }
2032 
2033 void
2034 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2035 {
2036 	int c;
2037 	vdev_stat_t *vs = &vd->vdev_stat;
2038 
2039 	for (c = 0; c < vd->vdev_children; c++)
2040 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2041 
2042 	mutex_enter(&vd->vdev_stat_lock);
2043 
2044 	if (type == POOL_SCRUB_NONE) {
2045 		/*
2046 		 * Update completion and end time.  Leave everything else alone
2047 		 * so we can report what happened during the previous scrub.
2048 		 */
2049 		vs->vs_scrub_complete = complete;
2050 		vs->vs_scrub_end = gethrestime_sec();
2051 	} else {
2052 		vs->vs_scrub_type = type;
2053 		vs->vs_scrub_complete = 0;
2054 		vs->vs_scrub_examined = 0;
2055 		vs->vs_scrub_repaired = 0;
2056 		vs->vs_scrub_start = gethrestime_sec();
2057 		vs->vs_scrub_end = 0;
2058 	}
2059 
2060 	mutex_exit(&vd->vdev_stat_lock);
2061 }
2062 
2063 /*
2064  * Update the in-core space usage stats for this vdev and the root vdev.
2065  */
2066 void
2067 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
2068     boolean_t update_root)
2069 {
2070 	int64_t dspace_delta = space_delta;
2071 	spa_t *spa = vd->vdev_spa;
2072 	vdev_t *rvd = spa->spa_root_vdev;
2073 
2074 	ASSERT(vd == vd->vdev_top);
2075 
2076 	/*
2077 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2078 	 * factor.  We must calculate this here and not at the root vdev
2079 	 * because the root vdev's psize-to-asize is simply the max of its
2080 	 * childrens', thus not accurate enough for us.
2081 	 */
2082 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2083 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2084 	    vd->vdev_deflate_ratio;
2085 
2086 	mutex_enter(&vd->vdev_stat_lock);
2087 	vd->vdev_stat.vs_space += space_delta;
2088 	vd->vdev_stat.vs_alloc += alloc_delta;
2089 	vd->vdev_stat.vs_dspace += dspace_delta;
2090 	mutex_exit(&vd->vdev_stat_lock);
2091 
2092 	if (update_root) {
2093 		ASSERT(rvd == vd->vdev_parent);
2094 		ASSERT(vd->vdev_ms_count != 0);
2095 
2096 		/*
2097 		 * Don't count non-normal (e.g. intent log) space as part of
2098 		 * the pool's capacity.
2099 		 */
2100 		if (vd->vdev_mg->mg_class != spa->spa_normal_class)
2101 			return;
2102 
2103 		mutex_enter(&rvd->vdev_stat_lock);
2104 		rvd->vdev_stat.vs_space += space_delta;
2105 		rvd->vdev_stat.vs_alloc += alloc_delta;
2106 		rvd->vdev_stat.vs_dspace += dspace_delta;
2107 		mutex_exit(&rvd->vdev_stat_lock);
2108 	}
2109 }
2110 
2111 /*
2112  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2113  * so that it will be written out next time the vdev configuration is synced.
2114  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2115  */
2116 void
2117 vdev_config_dirty(vdev_t *vd)
2118 {
2119 	spa_t *spa = vd->vdev_spa;
2120 	vdev_t *rvd = spa->spa_root_vdev;
2121 	int c;
2122 
2123 	/*
2124 	 * If this is an aux vdev (as with l2cache devices), then we update the
2125 	 * vdev config manually and set the sync flag.
2126 	 */
2127 	if (vd->vdev_aux != NULL) {
2128 		spa_aux_vdev_t *sav = vd->vdev_aux;
2129 		nvlist_t **aux;
2130 		uint_t naux;
2131 
2132 		for (c = 0; c < sav->sav_count; c++) {
2133 			if (sav->sav_vdevs[c] == vd)
2134 				break;
2135 		}
2136 
2137 		if (c == sav->sav_count) {
2138 			/*
2139 			 * We're being removed.  There's nothing more to do.
2140 			 */
2141 			ASSERT(sav->sav_sync == B_TRUE);
2142 			return;
2143 		}
2144 
2145 		sav->sav_sync = B_TRUE;
2146 
2147 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
2148 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) == 0);
2149 
2150 		ASSERT(c < naux);
2151 
2152 		/*
2153 		 * Setting the nvlist in the middle if the array is a little
2154 		 * sketchy, but it will work.
2155 		 */
2156 		nvlist_free(aux[c]);
2157 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
2158 
2159 		return;
2160 	}
2161 
2162 	/*
2163 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
2164 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
2165 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
2166 	 * so this is sufficient to ensure mutual exclusion.
2167 	 */
2168 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2169 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2170 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
2171 
2172 	if (vd == rvd) {
2173 		for (c = 0; c < rvd->vdev_children; c++)
2174 			vdev_config_dirty(rvd->vdev_child[c]);
2175 	} else {
2176 		ASSERT(vd == vd->vdev_top);
2177 
2178 		if (!list_link_active(&vd->vdev_config_dirty_node))
2179 			list_insert_head(&spa->spa_config_dirty_list, vd);
2180 	}
2181 }
2182 
2183 void
2184 vdev_config_clean(vdev_t *vd)
2185 {
2186 	spa_t *spa = vd->vdev_spa;
2187 
2188 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2189 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2190 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
2191 
2192 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
2193 	list_remove(&spa->spa_config_dirty_list, vd);
2194 }
2195 
2196 /*
2197  * Mark a top-level vdev's state as dirty, so that the next pass of
2198  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
2199  * the state changes from larger config changes because they require
2200  * much less locking, and are often needed for administrative actions.
2201  */
2202 void
2203 vdev_state_dirty(vdev_t *vd)
2204 {
2205 	spa_t *spa = vd->vdev_spa;
2206 
2207 	ASSERT(vd == vd->vdev_top);
2208 
2209 	/*
2210 	 * The state list is protected by the SCL_STATE lock.  The caller
2211 	 * must either hold SCL_STATE as writer, or must be the sync thread
2212 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
2213 	 * so this is sufficient to ensure mutual exclusion.
2214 	 */
2215 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2216 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2217 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2218 
2219 	if (!list_link_active(&vd->vdev_state_dirty_node))
2220 		list_insert_head(&spa->spa_state_dirty_list, vd);
2221 }
2222 
2223 void
2224 vdev_state_clean(vdev_t *vd)
2225 {
2226 	spa_t *spa = vd->vdev_spa;
2227 
2228 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2229 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2230 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2231 
2232 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
2233 	list_remove(&spa->spa_state_dirty_list, vd);
2234 }
2235 
2236 /*
2237  * Propagate vdev state up from children to parent.
2238  */
2239 void
2240 vdev_propagate_state(vdev_t *vd)
2241 {
2242 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2243 	int degraded = 0, faulted = 0;
2244 	int corrupted = 0;
2245 	int c;
2246 	vdev_t *child;
2247 
2248 	if (vd->vdev_children > 0) {
2249 		for (c = 0; c < vd->vdev_children; c++) {
2250 			child = vd->vdev_child[c];
2251 
2252 			if (!vdev_readable(child) ||
2253 			    (!vdev_writeable(child) && (spa_mode & FWRITE))) {
2254 				/*
2255 				 * Root special: if there is a top-level log
2256 				 * device, treat the root vdev as if it were
2257 				 * degraded.
2258 				 */
2259 				if (child->vdev_islog && vd == rvd)
2260 					degraded++;
2261 				else
2262 					faulted++;
2263 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
2264 				degraded++;
2265 			}
2266 
2267 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
2268 				corrupted++;
2269 		}
2270 
2271 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
2272 
2273 		/*
2274 		 * Root special: if there is a top-level vdev that cannot be
2275 		 * opened due to corrupted metadata, then propagate the root
2276 		 * vdev's aux state as 'corrupt' rather than 'insufficient
2277 		 * replicas'.
2278 		 */
2279 		if (corrupted && vd == rvd &&
2280 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
2281 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
2282 			    VDEV_AUX_CORRUPT_DATA);
2283 	}
2284 
2285 	if (vd->vdev_parent)
2286 		vdev_propagate_state(vd->vdev_parent);
2287 }
2288 
2289 /*
2290  * Set a vdev's state.  If this is during an open, we don't update the parent
2291  * state, because we're in the process of opening children depth-first.
2292  * Otherwise, we propagate the change to the parent.
2293  *
2294  * If this routine places a device in a faulted state, an appropriate ereport is
2295  * generated.
2296  */
2297 void
2298 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2299 {
2300 	uint64_t save_state;
2301 	spa_t *spa = vd->vdev_spa;
2302 
2303 	if (state == vd->vdev_state) {
2304 		vd->vdev_stat.vs_aux = aux;
2305 		return;
2306 	}
2307 
2308 	save_state = vd->vdev_state;
2309 
2310 	vd->vdev_state = state;
2311 	vd->vdev_stat.vs_aux = aux;
2312 
2313 	/*
2314 	 * If we are setting the vdev state to anything but an open state, then
2315 	 * always close the underlying device.  Otherwise, we keep accessible
2316 	 * but invalid devices open forever.  We don't call vdev_close() itself,
2317 	 * because that implies some extra checks (offline, etc) that we don't
2318 	 * want here.  This is limited to leaf devices, because otherwise
2319 	 * closing the device will affect other children.
2320 	 */
2321 	if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
2322 		vd->vdev_ops->vdev_op_close(vd);
2323 
2324 	if (vd->vdev_removed &&
2325 	    state == VDEV_STATE_CANT_OPEN &&
2326 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
2327 		/*
2328 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
2329 		 * device was previously marked removed and someone attempted to
2330 		 * reopen it.  If this failed due to a nonexistent device, then
2331 		 * keep the device in the REMOVED state.  We also let this be if
2332 		 * it is one of our special test online cases, which is only
2333 		 * attempting to online the device and shouldn't generate an FMA
2334 		 * fault.
2335 		 */
2336 		vd->vdev_state = VDEV_STATE_REMOVED;
2337 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2338 	} else if (state == VDEV_STATE_REMOVED) {
2339 		/*
2340 		 * Indicate to the ZFS DE that this device has been removed, and
2341 		 * any recent errors should be ignored.
2342 		 */
2343 		zfs_post_remove(spa, vd);
2344 		vd->vdev_removed = B_TRUE;
2345 	} else if (state == VDEV_STATE_CANT_OPEN) {
2346 		/*
2347 		 * If we fail to open a vdev during an import, we mark it as
2348 		 * "not available", which signifies that it was never there to
2349 		 * begin with.  Failure to open such a device is not considered
2350 		 * an error.
2351 		 */
2352 		if (spa->spa_load_state == SPA_LOAD_IMPORT &&
2353 		    !spa->spa_import_faulted &&
2354 		    vd->vdev_ops->vdev_op_leaf)
2355 			vd->vdev_not_present = 1;
2356 
2357 		/*
2358 		 * Post the appropriate ereport.  If the 'prevstate' field is
2359 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
2360 		 * that this is part of a vdev_reopen().  In this case, we don't
2361 		 * want to post the ereport if the device was already in the
2362 		 * CANT_OPEN state beforehand.
2363 		 *
2364 		 * If the 'checkremove' flag is set, then this is an attempt to
2365 		 * online the device in response to an insertion event.  If we
2366 		 * hit this case, then we have detected an insertion event for a
2367 		 * faulted or offline device that wasn't in the removed state.
2368 		 * In this scenario, we don't post an ereport because we are
2369 		 * about to replace the device, or attempt an online with
2370 		 * vdev_forcefault, which will generate the fault for us.
2371 		 */
2372 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
2373 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
2374 		    vd != spa->spa_root_vdev) {
2375 			const char *class;
2376 
2377 			switch (aux) {
2378 			case VDEV_AUX_OPEN_FAILED:
2379 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
2380 				break;
2381 			case VDEV_AUX_CORRUPT_DATA:
2382 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
2383 				break;
2384 			case VDEV_AUX_NO_REPLICAS:
2385 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
2386 				break;
2387 			case VDEV_AUX_BAD_GUID_SUM:
2388 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
2389 				break;
2390 			case VDEV_AUX_TOO_SMALL:
2391 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
2392 				break;
2393 			case VDEV_AUX_BAD_LABEL:
2394 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
2395 				break;
2396 			case VDEV_AUX_IO_FAILURE:
2397 				class = FM_EREPORT_ZFS_IO_FAILURE;
2398 				break;
2399 			default:
2400 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
2401 			}
2402 
2403 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
2404 		}
2405 
2406 		/* Erase any notion of persistent removed state */
2407 		vd->vdev_removed = B_FALSE;
2408 	} else {
2409 		vd->vdev_removed = B_FALSE;
2410 	}
2411 
2412 	if (!isopen)
2413 		vdev_propagate_state(vd);
2414 }
2415 
2416 /*
2417  * Check the vdev configuration to ensure that it's capable of supporting
2418  * a root pool. Currently, we do not support RAID-Z or partial configuration.
2419  * In addition, only a single top-level vdev is allowed and none of the leaves
2420  * can be wholedisks.
2421  */
2422 boolean_t
2423 vdev_is_bootable(vdev_t *vd)
2424 {
2425 	int c;
2426 
2427 	if (!vd->vdev_ops->vdev_op_leaf) {
2428 		char *vdev_type = vd->vdev_ops->vdev_op_type;
2429 
2430 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
2431 		    vd->vdev_children > 1) {
2432 			return (B_FALSE);
2433 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
2434 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
2435 			return (B_FALSE);
2436 		}
2437 	} else if (vd->vdev_wholedisk == 1) {
2438 		return (B_FALSE);
2439 	}
2440 
2441 	for (c = 0; c < vd->vdev_children; c++) {
2442 		if (!vdev_is_bootable(vd->vdev_child[c]))
2443 			return (B_FALSE);
2444 	}
2445 	return (B_TRUE);
2446 }
2447