xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 150d2c5288c645a1c1a7d2bee61199a3729406c7)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/zfs_context.h>
30 #include <sys/fm/fs/zfs.h>
31 #include <sys/spa.h>
32 #include <sys/spa_impl.h>
33 #include <sys/dmu.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/uberblock_impl.h>
37 #include <sys/metaslab.h>
38 #include <sys/metaslab_impl.h>
39 #include <sys/space_map.h>
40 #include <sys/zio.h>
41 #include <sys/zap.h>
42 #include <sys/fs/zfs.h>
43 
44 /*
45  * Virtual device management.
46  */
47 
48 static vdev_ops_t *vdev_ops_table[] = {
49 	&vdev_root_ops,
50 	&vdev_raidz_ops,
51 	&vdev_mirror_ops,
52 	&vdev_replacing_ops,
53 	&vdev_spare_ops,
54 	&vdev_disk_ops,
55 	&vdev_file_ops,
56 	&vdev_missing_ops,
57 	NULL
58 };
59 
60 /*
61  * Given a vdev type, return the appropriate ops vector.
62  */
63 static vdev_ops_t *
64 vdev_getops(const char *type)
65 {
66 	vdev_ops_t *ops, **opspp;
67 
68 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
69 		if (strcmp(ops->vdev_op_type, type) == 0)
70 			break;
71 
72 	return (ops);
73 }
74 
75 /*
76  * Default asize function: return the MAX of psize with the asize of
77  * all children.  This is what's used by anything other than RAID-Z.
78  */
79 uint64_t
80 vdev_default_asize(vdev_t *vd, uint64_t psize)
81 {
82 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
83 	uint64_t csize;
84 	uint64_t c;
85 
86 	for (c = 0; c < vd->vdev_children; c++) {
87 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
88 		asize = MAX(asize, csize);
89 	}
90 
91 	return (asize);
92 }
93 
94 /*
95  * Get the replaceable or attachable device size.
96  * If the parent is a mirror or raidz, the replaceable size is the minimum
97  * psize of all its children. For the rest, just return our own psize.
98  *
99  * e.g.
100  *			psize	rsize
101  * root			-	-
102  *	mirror/raidz	-	-
103  *	    disk1	20g	20g
104  *	    disk2 	40g	20g
105  *	disk3 		80g	80g
106  */
107 uint64_t
108 vdev_get_rsize(vdev_t *vd)
109 {
110 	vdev_t *pvd, *cvd;
111 	uint64_t c, rsize;
112 
113 	pvd = vd->vdev_parent;
114 
115 	/*
116 	 * If our parent is NULL or the root, just return our own psize.
117 	 */
118 	if (pvd == NULL || pvd->vdev_parent == NULL)
119 		return (vd->vdev_psize);
120 
121 	rsize = 0;
122 
123 	for (c = 0; c < pvd->vdev_children; c++) {
124 		cvd = pvd->vdev_child[c];
125 		rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
126 	}
127 
128 	return (rsize);
129 }
130 
131 vdev_t *
132 vdev_lookup_top(spa_t *spa, uint64_t vdev)
133 {
134 	vdev_t *rvd = spa->spa_root_vdev;
135 
136 	if (vdev < rvd->vdev_children)
137 		return (rvd->vdev_child[vdev]);
138 
139 	return (NULL);
140 }
141 
142 vdev_t *
143 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
144 {
145 	int c;
146 	vdev_t *mvd;
147 
148 	if (vd->vdev_guid == guid)
149 		return (vd);
150 
151 	for (c = 0; c < vd->vdev_children; c++)
152 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
153 		    NULL)
154 			return (mvd);
155 
156 	return (NULL);
157 }
158 
159 void
160 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
161 {
162 	size_t oldsize, newsize;
163 	uint64_t id = cvd->vdev_id;
164 	vdev_t **newchild;
165 
166 	ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
167 	ASSERT(cvd->vdev_parent == NULL);
168 
169 	cvd->vdev_parent = pvd;
170 
171 	if (pvd == NULL)
172 		return;
173 
174 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
175 
176 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
177 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
178 	newsize = pvd->vdev_children * sizeof (vdev_t *);
179 
180 	newchild = kmem_zalloc(newsize, KM_SLEEP);
181 	if (pvd->vdev_child != NULL) {
182 		bcopy(pvd->vdev_child, newchild, oldsize);
183 		kmem_free(pvd->vdev_child, oldsize);
184 	}
185 
186 	pvd->vdev_child = newchild;
187 	pvd->vdev_child[id] = cvd;
188 
189 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
190 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
191 
192 	/*
193 	 * Walk up all ancestors to update guid sum.
194 	 */
195 	for (; pvd != NULL; pvd = pvd->vdev_parent)
196 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
197 }
198 
199 void
200 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
201 {
202 	int c;
203 	uint_t id = cvd->vdev_id;
204 
205 	ASSERT(cvd->vdev_parent == pvd);
206 
207 	if (pvd == NULL)
208 		return;
209 
210 	ASSERT(id < pvd->vdev_children);
211 	ASSERT(pvd->vdev_child[id] == cvd);
212 
213 	pvd->vdev_child[id] = NULL;
214 	cvd->vdev_parent = NULL;
215 
216 	for (c = 0; c < pvd->vdev_children; c++)
217 		if (pvd->vdev_child[c])
218 			break;
219 
220 	if (c == pvd->vdev_children) {
221 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
222 		pvd->vdev_child = NULL;
223 		pvd->vdev_children = 0;
224 	}
225 
226 	/*
227 	 * Walk up all ancestors to update guid sum.
228 	 */
229 	for (; pvd != NULL; pvd = pvd->vdev_parent)
230 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
231 }
232 
233 /*
234  * Remove any holes in the child array.
235  */
236 void
237 vdev_compact_children(vdev_t *pvd)
238 {
239 	vdev_t **newchild, *cvd;
240 	int oldc = pvd->vdev_children;
241 	int newc, c;
242 
243 	ASSERT(spa_config_held(pvd->vdev_spa, RW_WRITER));
244 
245 	for (c = newc = 0; c < oldc; c++)
246 		if (pvd->vdev_child[c])
247 			newc++;
248 
249 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
250 
251 	for (c = newc = 0; c < oldc; c++) {
252 		if ((cvd = pvd->vdev_child[c]) != NULL) {
253 			newchild[newc] = cvd;
254 			cvd->vdev_id = newc++;
255 		}
256 	}
257 
258 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
259 	pvd->vdev_child = newchild;
260 	pvd->vdev_children = newc;
261 }
262 
263 /*
264  * Allocate and minimally initialize a vdev_t.
265  */
266 static vdev_t *
267 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
268 {
269 	vdev_t *vd;
270 
271 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
272 
273 	if (spa->spa_root_vdev == NULL) {
274 		ASSERT(ops == &vdev_root_ops);
275 		spa->spa_root_vdev = vd;
276 	}
277 
278 	if (guid == 0) {
279 		if (spa->spa_root_vdev == vd) {
280 			/*
281 			 * The root vdev's guid will also be the pool guid,
282 			 * which must be unique among all pools.
283 			 */
284 			while (guid == 0 || spa_guid_exists(guid, 0))
285 				guid = spa_get_random(-1ULL);
286 		} else {
287 			/*
288 			 * Any other vdev's guid must be unique within the pool.
289 			 */
290 			while (guid == 0 ||
291 			    spa_guid_exists(spa_guid(spa), guid))
292 				guid = spa_get_random(-1ULL);
293 		}
294 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
295 	}
296 
297 	vd->vdev_spa = spa;
298 	vd->vdev_id = id;
299 	vd->vdev_guid = guid;
300 	vd->vdev_guid_sum = guid;
301 	vd->vdev_ops = ops;
302 	vd->vdev_state = VDEV_STATE_CLOSED;
303 
304 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
305 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
306 	space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
307 	space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
308 	txg_list_create(&vd->vdev_ms_list,
309 	    offsetof(struct metaslab, ms_txg_node));
310 	txg_list_create(&vd->vdev_dtl_list,
311 	    offsetof(struct vdev, vdev_dtl_node));
312 	vd->vdev_stat.vs_timestamp = gethrtime();
313 
314 	return (vd);
315 }
316 
317 /*
318  * Free a vdev_t that has been removed from service.
319  */
320 static void
321 vdev_free_common(vdev_t *vd)
322 {
323 	spa_t *spa = vd->vdev_spa;
324 
325 	if (vd->vdev_path)
326 		spa_strfree(vd->vdev_path);
327 	if (vd->vdev_devid)
328 		spa_strfree(vd->vdev_devid);
329 
330 	if (vd->vdev_isspare)
331 		spa_spare_remove(vd);
332 
333 	txg_list_destroy(&vd->vdev_ms_list);
334 	txg_list_destroy(&vd->vdev_dtl_list);
335 	mutex_enter(&vd->vdev_dtl_lock);
336 	space_map_unload(&vd->vdev_dtl_map);
337 	space_map_destroy(&vd->vdev_dtl_map);
338 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
339 	space_map_destroy(&vd->vdev_dtl_scrub);
340 	mutex_exit(&vd->vdev_dtl_lock);
341 	mutex_destroy(&vd->vdev_dtl_lock);
342 	mutex_destroy(&vd->vdev_stat_lock);
343 
344 	if (vd == spa->spa_root_vdev)
345 		spa->spa_root_vdev = NULL;
346 
347 	kmem_free(vd, sizeof (vdev_t));
348 }
349 
350 /*
351  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
352  * creating a new vdev or loading an existing one - the behavior is slightly
353  * different for each case.
354  */
355 int
356 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
357     int alloctype)
358 {
359 	vdev_ops_t *ops;
360 	char *type;
361 	uint64_t guid = 0;
362 	vdev_t *vd;
363 
364 	ASSERT(spa_config_held(spa, RW_WRITER));
365 
366 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
367 		return (EINVAL);
368 
369 	if ((ops = vdev_getops(type)) == NULL)
370 		return (EINVAL);
371 
372 	/*
373 	 * If this is a load, get the vdev guid from the nvlist.
374 	 * Otherwise, vdev_alloc_common() will generate one for us.
375 	 */
376 	if (alloctype == VDEV_ALLOC_LOAD) {
377 		uint64_t label_id;
378 
379 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
380 		    label_id != id)
381 			return (EINVAL);
382 
383 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
384 			return (EINVAL);
385 	} else if (alloctype == VDEV_ALLOC_SPARE) {
386 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
387 			return (EINVAL);
388 	}
389 
390 	/*
391 	 * The first allocated vdev must be of type 'root'.
392 	 */
393 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
394 		return (EINVAL);
395 
396 	vd = vdev_alloc_common(spa, id, guid, ops);
397 
398 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
399 		vd->vdev_path = spa_strdup(vd->vdev_path);
400 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
401 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
402 
403 	/*
404 	 * Set the nparity propery for RAID-Z vdevs.
405 	 */
406 	if (ops == &vdev_raidz_ops) {
407 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
408 		    &vd->vdev_nparity) == 0) {
409 			/*
410 			 * Currently, we can only support 2 parity devices.
411 			 */
412 			if (vd->vdev_nparity > 2)
413 				return (EINVAL);
414 			/*
415 			 * Older versions can only support 1 parity device.
416 			 */
417 			if (vd->vdev_nparity == 2 &&
418 			    spa_version(spa) < ZFS_VERSION_RAID6)
419 				return (ENOTSUP);
420 
421 		} else {
422 			/*
423 			 * We require the parity to be specified for SPAs that
424 			 * support multiple parity levels.
425 			 */
426 			if (spa_version(spa) >= ZFS_VERSION_RAID6)
427 				return (EINVAL);
428 
429 			/*
430 			 * Otherwise, we default to 1 parity device for RAID-Z.
431 			 */
432 			vd->vdev_nparity = 1;
433 		}
434 	} else {
435 		vd->vdev_nparity = 0;
436 	}
437 
438 	/*
439 	 * Set the whole_disk property.  If it's not specified, leave the value
440 	 * as -1.
441 	 */
442 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
443 	    &vd->vdev_wholedisk) != 0)
444 		vd->vdev_wholedisk = -1ULL;
445 
446 	/*
447 	 * Look for the 'not present' flag.  This will only be set if the device
448 	 * was not present at the time of import.
449 	 */
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 offline state.
472 	 */
473 	if (vd->vdev_ops->vdev_op_leaf && alloctype == VDEV_ALLOC_LOAD) {
474 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
475 		    &vd->vdev_dtl.smo_object);
476 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
477 		    &vd->vdev_offline);
478 	}
479 
480 	/*
481 	 * Add ourselves to the parent's list of children.
482 	 */
483 	vdev_add_child(parent, vd);
484 
485 	*vdp = vd;
486 
487 	return (0);
488 }
489 
490 void
491 vdev_free(vdev_t *vd)
492 {
493 	int c;
494 
495 	/*
496 	 * vdev_free() implies closing the vdev first.  This is simpler than
497 	 * trying to ensure complicated semantics for all callers.
498 	 */
499 	vdev_close(vd);
500 
501 	ASSERT(!list_link_active(&vd->vdev_dirty_node));
502 
503 	/*
504 	 * Free all children.
505 	 */
506 	for (c = 0; c < vd->vdev_children; c++)
507 		vdev_free(vd->vdev_child[c]);
508 
509 	ASSERT(vd->vdev_child == NULL);
510 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
511 
512 	/*
513 	 * Discard allocation state.
514 	 */
515 	if (vd == vd->vdev_top)
516 		vdev_metaslab_fini(vd);
517 
518 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
519 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
520 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
521 
522 	/*
523 	 * Remove this vdev from its parent's child list.
524 	 */
525 	vdev_remove_child(vd->vdev_parent, vd);
526 
527 	ASSERT(vd->vdev_parent == NULL);
528 
529 	vdev_free_common(vd);
530 }
531 
532 /*
533  * Transfer top-level vdev state from svd to tvd.
534  */
535 static void
536 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
537 {
538 	spa_t *spa = svd->vdev_spa;
539 	metaslab_t *msp;
540 	vdev_t *vd;
541 	int t;
542 
543 	ASSERT(tvd == tvd->vdev_top);
544 
545 	tvd->vdev_ms_array = svd->vdev_ms_array;
546 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
547 	tvd->vdev_ms_count = svd->vdev_ms_count;
548 
549 	svd->vdev_ms_array = 0;
550 	svd->vdev_ms_shift = 0;
551 	svd->vdev_ms_count = 0;
552 
553 	tvd->vdev_mg = svd->vdev_mg;
554 	tvd->vdev_ms = svd->vdev_ms;
555 
556 	svd->vdev_mg = NULL;
557 	svd->vdev_ms = NULL;
558 
559 	if (tvd->vdev_mg != NULL)
560 		tvd->vdev_mg->mg_vd = tvd;
561 
562 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
563 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
564 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
565 
566 	svd->vdev_stat.vs_alloc = 0;
567 	svd->vdev_stat.vs_space = 0;
568 	svd->vdev_stat.vs_dspace = 0;
569 
570 	for (t = 0; t < TXG_SIZE; t++) {
571 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
572 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
573 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
574 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
575 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
576 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
577 	}
578 
579 	if (list_link_active(&svd->vdev_dirty_node)) {
580 		vdev_config_clean(svd);
581 		vdev_config_dirty(tvd);
582 	}
583 
584 	tvd->vdev_reopen_wanted = svd->vdev_reopen_wanted;
585 	svd->vdev_reopen_wanted = 0;
586 
587 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
588 	svd->vdev_deflate_ratio = 0;
589 }
590 
591 static void
592 vdev_top_update(vdev_t *tvd, vdev_t *vd)
593 {
594 	int c;
595 
596 	if (vd == NULL)
597 		return;
598 
599 	vd->vdev_top = tvd;
600 
601 	for (c = 0; c < vd->vdev_children; c++)
602 		vdev_top_update(tvd, vd->vdev_child[c]);
603 }
604 
605 /*
606  * Add a mirror/replacing vdev above an existing vdev.
607  */
608 vdev_t *
609 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
610 {
611 	spa_t *spa = cvd->vdev_spa;
612 	vdev_t *pvd = cvd->vdev_parent;
613 	vdev_t *mvd;
614 
615 	ASSERT(spa_config_held(spa, RW_WRITER));
616 
617 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
618 
619 	mvd->vdev_asize = cvd->vdev_asize;
620 	mvd->vdev_ashift = cvd->vdev_ashift;
621 	mvd->vdev_state = cvd->vdev_state;
622 
623 	vdev_remove_child(pvd, cvd);
624 	vdev_add_child(pvd, mvd);
625 	cvd->vdev_id = mvd->vdev_children;
626 	vdev_add_child(mvd, cvd);
627 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
628 
629 	if (mvd == mvd->vdev_top)
630 		vdev_top_transfer(cvd, mvd);
631 
632 	return (mvd);
633 }
634 
635 /*
636  * Remove a 1-way mirror/replacing vdev from the tree.
637  */
638 void
639 vdev_remove_parent(vdev_t *cvd)
640 {
641 	vdev_t *mvd = cvd->vdev_parent;
642 	vdev_t *pvd = mvd->vdev_parent;
643 
644 	ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
645 
646 	ASSERT(mvd->vdev_children == 1);
647 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
648 	    mvd->vdev_ops == &vdev_replacing_ops ||
649 	    mvd->vdev_ops == &vdev_spare_ops);
650 	cvd->vdev_ashift = mvd->vdev_ashift;
651 
652 	vdev_remove_child(mvd, cvd);
653 	vdev_remove_child(pvd, mvd);
654 	cvd->vdev_id = mvd->vdev_id;
655 	vdev_add_child(pvd, cvd);
656 	/*
657 	 * If we created a new toplevel vdev, then we need to change the child's
658 	 * vdev GUID to match the old toplevel vdev.  Otherwise, we could have
659 	 * detached an offline device, and when we go to import the pool we'll
660 	 * think we have two toplevel vdevs, instead of a different version of
661 	 * the same toplevel vdev.
662 	 */
663 	if (cvd->vdev_top == cvd) {
664 		pvd->vdev_guid_sum -= cvd->vdev_guid;
665 		cvd->vdev_guid_sum -= cvd->vdev_guid;
666 		cvd->vdev_guid = mvd->vdev_guid;
667 		cvd->vdev_guid_sum += mvd->vdev_guid;
668 		pvd->vdev_guid_sum += cvd->vdev_guid;
669 	}
670 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
671 
672 	if (cvd == cvd->vdev_top)
673 		vdev_top_transfer(mvd, cvd);
674 
675 	ASSERT(mvd->vdev_children == 0);
676 	vdev_free(mvd);
677 }
678 
679 int
680 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
681 {
682 	spa_t *spa = vd->vdev_spa;
683 	objset_t *mos = spa->spa_meta_objset;
684 	metaslab_class_t *mc = spa_metaslab_class_select(spa);
685 	uint64_t m;
686 	uint64_t oldc = vd->vdev_ms_count;
687 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
688 	metaslab_t **mspp;
689 	int error;
690 
691 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
692 		return (0);
693 
694 	dprintf("%s oldc %llu newc %llu\n", vdev_description(vd), oldc, newc);
695 
696 	ASSERT(oldc <= newc);
697 
698 	if (vd->vdev_mg == NULL)
699 		vd->vdev_mg = metaslab_group_create(mc, vd);
700 
701 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
702 
703 	if (oldc != 0) {
704 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
705 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
706 	}
707 
708 	vd->vdev_ms = mspp;
709 	vd->vdev_ms_count = newc;
710 
711 	for (m = oldc; m < newc; m++) {
712 		space_map_obj_t smo = { 0, 0, 0 };
713 		if (txg == 0) {
714 			uint64_t object = 0;
715 			error = dmu_read(mos, vd->vdev_ms_array,
716 			    m * sizeof (uint64_t), sizeof (uint64_t), &object);
717 			if (error)
718 				return (error);
719 			if (object != 0) {
720 				dmu_buf_t *db;
721 				error = dmu_bonus_hold(mos, object, FTAG, &db);
722 				if (error)
723 					return (error);
724 				ASSERT3U(db->db_size, ==, sizeof (smo));
725 				bcopy(db->db_data, &smo, db->db_size);
726 				ASSERT3U(smo.smo_object, ==, object);
727 				dmu_buf_rele(db, FTAG);
728 			}
729 		}
730 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
731 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
732 	}
733 
734 	return (0);
735 }
736 
737 void
738 vdev_metaslab_fini(vdev_t *vd)
739 {
740 	uint64_t m;
741 	uint64_t count = vd->vdev_ms_count;
742 
743 	if (vd->vdev_ms != NULL) {
744 		for (m = 0; m < count; m++)
745 			if (vd->vdev_ms[m] != NULL)
746 				metaslab_fini(vd->vdev_ms[m]);
747 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
748 		vd->vdev_ms = NULL;
749 	}
750 }
751 
752 /*
753  * Prepare a virtual device for access.
754  */
755 int
756 vdev_open(vdev_t *vd)
757 {
758 	int error;
759 	int c;
760 	uint64_t osize = 0;
761 	uint64_t asize, psize;
762 	uint64_t ashift = 0;
763 
764 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
765 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
766 	    vd->vdev_state == VDEV_STATE_OFFLINE);
767 
768 	if (vd->vdev_fault_mode == VDEV_FAULT_COUNT)
769 		vd->vdev_fault_arg >>= 1;
770 	else
771 		vd->vdev_fault_mode = VDEV_FAULT_NONE;
772 
773 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
774 
775 	if (vd->vdev_ops->vdev_op_leaf) {
776 		vdev_cache_init(vd);
777 		vdev_queue_init(vd);
778 		vd->vdev_cache_active = B_TRUE;
779 	}
780 
781 	if (vd->vdev_offline) {
782 		ASSERT(vd->vdev_children == 0);
783 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
784 		return (ENXIO);
785 	}
786 
787 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
788 
789 	if (zio_injection_enabled && error == 0)
790 		error = zio_handle_device_injection(vd, ENXIO);
791 
792 	dprintf("%s = %d, osize %llu, state = %d\n",
793 	    vdev_description(vd), error, osize, vd->vdev_state);
794 
795 	if (error) {
796 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
797 		    vd->vdev_stat.vs_aux);
798 		return (error);
799 	}
800 
801 	vd->vdev_state = VDEV_STATE_HEALTHY;
802 
803 	for (c = 0; c < vd->vdev_children; c++)
804 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
805 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
806 			    VDEV_AUX_NONE);
807 			break;
808 		}
809 
810 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
811 
812 	if (vd->vdev_children == 0) {
813 		if (osize < SPA_MINDEVSIZE) {
814 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
815 			    VDEV_AUX_TOO_SMALL);
816 			return (EOVERFLOW);
817 		}
818 		psize = osize;
819 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
820 	} else {
821 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
822 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
823 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
824 			    VDEV_AUX_TOO_SMALL);
825 			return (EOVERFLOW);
826 		}
827 		psize = 0;
828 		asize = osize;
829 	}
830 
831 	vd->vdev_psize = psize;
832 
833 	if (vd->vdev_asize == 0) {
834 		/*
835 		 * This is the first-ever open, so use the computed values.
836 		 * For testing purposes, a higher ashift can be requested.
837 		 */
838 		vd->vdev_asize = asize;
839 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
840 	} else {
841 		/*
842 		 * Make sure the alignment requirement hasn't increased.
843 		 */
844 		if (ashift > vd->vdev_top->vdev_ashift) {
845 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
846 			    VDEV_AUX_BAD_LABEL);
847 			return (EINVAL);
848 		}
849 
850 		/*
851 		 * Make sure the device hasn't shrunk.
852 		 */
853 		if (asize < vd->vdev_asize) {
854 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
855 			    VDEV_AUX_BAD_LABEL);
856 			return (EINVAL);
857 		}
858 
859 		/*
860 		 * If all children are healthy and the asize has increased,
861 		 * then we've experienced dynamic LUN growth.
862 		 */
863 		if (vd->vdev_state == VDEV_STATE_HEALTHY &&
864 		    asize > vd->vdev_asize) {
865 			vd->vdev_asize = asize;
866 		}
867 	}
868 
869 	/*
870 	 * If this is a top-level vdev, compute the raidz-deflation
871 	 * ratio.  Note, we hard-code in 128k (1<<17) because it is the
872 	 * current "typical" blocksize.  Even if SPA_MAXBLOCKSIZE
873 	 * changes, this algorithm must never change, or we will
874 	 * inconsistently account for existing bp's.
875 	 */
876 	if (vd->vdev_top == vd) {
877 		vd->vdev_deflate_ratio = (1<<17) /
878 		    (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
879 	}
880 
881 	/*
882 	 * This allows the ZFS DE to close cases appropriately.  If a device
883 	 * goes away and later returns, we want to close the associated case.
884 	 * But it's not enough to simply post this only when a device goes from
885 	 * CANT_OPEN -> HEALTHY.  If we reboot the system and the device is
886 	 * back, we also need to close the case (otherwise we will try to replay
887 	 * it).  So we have to post this notifier every time.  Since this only
888 	 * occurs during pool open or error recovery, this should not be an
889 	 * issue.
890 	 */
891 	zfs_post_ok(vd->vdev_spa, vd);
892 
893 	return (0);
894 }
895 
896 /*
897  * Called once the vdevs are all opened, this routine validates the label
898  * contents.  This needs to be done before vdev_load() so that we don't
899  * inadvertently do repair I/Os to the wrong device, and so that vdev_reopen()
900  * won't succeed if the device has been changed underneath.
901  *
902  * This function will only return failure if one of the vdevs indicates that it
903  * has since been destroyed or exported.  This is only possible if
904  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
905  * will be updated but the function will return 0.
906  */
907 int
908 vdev_validate(vdev_t *vd)
909 {
910 	spa_t *spa = vd->vdev_spa;
911 	int c;
912 	nvlist_t *label;
913 	uint64_t guid;
914 	uint64_t state;
915 
916 	for (c = 0; c < vd->vdev_children; c++)
917 		if (vdev_validate(vd->vdev_child[c]) != 0)
918 			return (-1);
919 
920 	/*
921 	 * If the device has already failed, or was marked offline, don't do
922 	 * any further validation.  Otherwise, label I/O will fail and we will
923 	 * overwrite the previous state.
924 	 */
925 	if (vd->vdev_ops->vdev_op_leaf && !vdev_is_dead(vd)) {
926 
927 		if ((label = vdev_label_read_config(vd)) == NULL) {
928 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
929 			    VDEV_AUX_BAD_LABEL);
930 			return (0);
931 		}
932 
933 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
934 		    &guid) != 0 || guid != spa_guid(spa)) {
935 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
936 			    VDEV_AUX_CORRUPT_DATA);
937 			nvlist_free(label);
938 			return (0);
939 		}
940 
941 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
942 		    &guid) != 0 || guid != vd->vdev_guid) {
943 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
944 			    VDEV_AUX_CORRUPT_DATA);
945 			nvlist_free(label);
946 			return (0);
947 		}
948 
949 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
950 		    &state) != 0) {
951 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
952 			    VDEV_AUX_CORRUPT_DATA);
953 			nvlist_free(label);
954 			return (0);
955 		}
956 
957 		nvlist_free(label);
958 
959 		if (spa->spa_load_state == SPA_LOAD_OPEN &&
960 		    state != POOL_STATE_ACTIVE)
961 			return (-1);
962 	}
963 
964 	/*
965 	 * If we were able to open and validate a vdev that was previously
966 	 * marked permanently unavailable, clear that state now.
967 	 */
968 	if (vd->vdev_not_present)
969 		vd->vdev_not_present = 0;
970 
971 	return (0);
972 }
973 
974 /*
975  * Close a virtual device.
976  */
977 void
978 vdev_close(vdev_t *vd)
979 {
980 	vd->vdev_ops->vdev_op_close(vd);
981 
982 	if (vd->vdev_cache_active) {
983 		vdev_cache_fini(vd);
984 		vdev_queue_fini(vd);
985 		vd->vdev_cache_active = B_FALSE;
986 	}
987 
988 	/*
989 	 * We record the previous state before we close it, so  that if we are
990 	 * doing a reopen(), we don't generate FMA ereports if we notice that
991 	 * it's still faulted.
992 	 */
993 	vd->vdev_prevstate = vd->vdev_state;
994 
995 	if (vd->vdev_offline)
996 		vd->vdev_state = VDEV_STATE_OFFLINE;
997 	else
998 		vd->vdev_state = VDEV_STATE_CLOSED;
999 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1000 }
1001 
1002 void
1003 vdev_reopen(vdev_t *vd)
1004 {
1005 	spa_t *spa = vd->vdev_spa;
1006 
1007 	ASSERT(spa_config_held(spa, RW_WRITER));
1008 
1009 	vdev_close(vd);
1010 	(void) vdev_open(vd);
1011 
1012 	/*
1013 	 * Call vdev_validate() here to make sure we have the same device.
1014 	 * Otherwise, a device with an invalid label could be successfully
1015 	 * opened in response to vdev_reopen().
1016 	 *
1017 	 * The downside to this is that if the user is simply experimenting by
1018 	 * overwriting an entire disk, we'll fault the device rather than
1019 	 * demonstrate self-healing capabilities.  On the other hand, with
1020 	 * proper FMA integration, the series of errors we'd see from the device
1021 	 * would result in a faulted device anyway.  Given that this doesn't
1022 	 * model any real-world corruption, it's better to catch this here and
1023 	 * correctly identify that the device has either changed beneath us, or
1024 	 * is corrupted beyond recognition.
1025 	 */
1026 	(void) vdev_validate(vd);
1027 
1028 	/*
1029 	 * Reassess root vdev's health.
1030 	 */
1031 	vdev_propagate_state(spa->spa_root_vdev);
1032 }
1033 
1034 int
1035 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1036 {
1037 	int error;
1038 
1039 	/*
1040 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1041 	 * For a create, however, we want to fail the request if
1042 	 * there are any components we can't open.
1043 	 */
1044 	error = vdev_open(vd);
1045 
1046 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1047 		vdev_close(vd);
1048 		return (error ? error : ENXIO);
1049 	}
1050 
1051 	/*
1052 	 * Recursively initialize all labels.
1053 	 */
1054 	if ((error = vdev_label_init(vd, txg, isreplacing ?
1055 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1056 		vdev_close(vd);
1057 		return (error);
1058 	}
1059 
1060 	return (0);
1061 }
1062 
1063 /*
1064  * The is the latter half of vdev_create().  It is distinct because it
1065  * involves initiating transactions in order to do metaslab creation.
1066  * For creation, we want to try to create all vdevs at once and then undo it
1067  * if anything fails; this is much harder if we have pending transactions.
1068  */
1069 void
1070 vdev_init(vdev_t *vd, uint64_t txg)
1071 {
1072 	/*
1073 	 * Aim for roughly 200 metaslabs per vdev.
1074 	 */
1075 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1076 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1077 
1078 	/*
1079 	 * Initialize the vdev's metaslabs.  This can't fail because
1080 	 * there's nothing to read when creating all new metaslabs.
1081 	 */
1082 	VERIFY(vdev_metaslab_init(vd, txg) == 0);
1083 }
1084 
1085 void
1086 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1087 {
1088 	ASSERT(vd == vd->vdev_top);
1089 	ASSERT(ISP2(flags));
1090 
1091 	if (flags & VDD_METASLAB)
1092 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1093 
1094 	if (flags & VDD_DTL)
1095 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1096 
1097 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1098 }
1099 
1100 void
1101 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1102 {
1103 	mutex_enter(sm->sm_lock);
1104 	if (!space_map_contains(sm, txg, size))
1105 		space_map_add(sm, txg, size);
1106 	mutex_exit(sm->sm_lock);
1107 }
1108 
1109 int
1110 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1111 {
1112 	int dirty;
1113 
1114 	/*
1115 	 * Quick test without the lock -- covers the common case that
1116 	 * there are no dirty time segments.
1117 	 */
1118 	if (sm->sm_space == 0)
1119 		return (0);
1120 
1121 	mutex_enter(sm->sm_lock);
1122 	dirty = space_map_contains(sm, txg, size);
1123 	mutex_exit(sm->sm_lock);
1124 
1125 	return (dirty);
1126 }
1127 
1128 /*
1129  * Reassess DTLs after a config change or scrub completion.
1130  */
1131 void
1132 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1133 {
1134 	spa_t *spa = vd->vdev_spa;
1135 	int c;
1136 
1137 	ASSERT(spa_config_held(spa, RW_WRITER));
1138 
1139 	if (vd->vdev_children == 0) {
1140 		mutex_enter(&vd->vdev_dtl_lock);
1141 		/*
1142 		 * We're successfully scrubbed everything up to scrub_txg.
1143 		 * Therefore, excise all old DTLs up to that point, then
1144 		 * fold in the DTLs for everything we couldn't scrub.
1145 		 */
1146 		if (scrub_txg != 0) {
1147 			space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1148 			space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1149 		}
1150 		if (scrub_done)
1151 			space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1152 		mutex_exit(&vd->vdev_dtl_lock);
1153 		if (txg != 0)
1154 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1155 		return;
1156 	}
1157 
1158 	/*
1159 	 * Make sure the DTLs are always correct under the scrub lock.
1160 	 */
1161 	if (vd == spa->spa_root_vdev)
1162 		mutex_enter(&spa->spa_scrub_lock);
1163 
1164 	mutex_enter(&vd->vdev_dtl_lock);
1165 	space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1166 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1167 	mutex_exit(&vd->vdev_dtl_lock);
1168 
1169 	for (c = 0; c < vd->vdev_children; c++) {
1170 		vdev_t *cvd = vd->vdev_child[c];
1171 		vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1172 		mutex_enter(&vd->vdev_dtl_lock);
1173 		space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1174 		space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1175 		mutex_exit(&vd->vdev_dtl_lock);
1176 	}
1177 
1178 	if (vd == spa->spa_root_vdev)
1179 		mutex_exit(&spa->spa_scrub_lock);
1180 }
1181 
1182 static int
1183 vdev_dtl_load(vdev_t *vd)
1184 {
1185 	spa_t *spa = vd->vdev_spa;
1186 	space_map_obj_t *smo = &vd->vdev_dtl;
1187 	objset_t *mos = spa->spa_meta_objset;
1188 	dmu_buf_t *db;
1189 	int error;
1190 
1191 	ASSERT(vd->vdev_children == 0);
1192 
1193 	if (smo->smo_object == 0)
1194 		return (0);
1195 
1196 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1197 		return (error);
1198 
1199 	ASSERT3U(db->db_size, ==, sizeof (*smo));
1200 	bcopy(db->db_data, smo, db->db_size);
1201 	dmu_buf_rele(db, FTAG);
1202 
1203 	mutex_enter(&vd->vdev_dtl_lock);
1204 	error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1205 	mutex_exit(&vd->vdev_dtl_lock);
1206 
1207 	return (error);
1208 }
1209 
1210 void
1211 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1212 {
1213 	spa_t *spa = vd->vdev_spa;
1214 	space_map_obj_t *smo = &vd->vdev_dtl;
1215 	space_map_t *sm = &vd->vdev_dtl_map;
1216 	objset_t *mos = spa->spa_meta_objset;
1217 	space_map_t smsync;
1218 	kmutex_t smlock;
1219 	dmu_buf_t *db;
1220 	dmu_tx_t *tx;
1221 
1222 	dprintf("%s in txg %llu pass %d\n",
1223 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1224 
1225 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1226 
1227 	if (vd->vdev_detached) {
1228 		if (smo->smo_object != 0) {
1229 			int err = dmu_object_free(mos, smo->smo_object, tx);
1230 			ASSERT3U(err, ==, 0);
1231 			smo->smo_object = 0;
1232 		}
1233 		dmu_tx_commit(tx);
1234 		dprintf("detach %s committed in txg %llu\n",
1235 		    vdev_description(vd), txg);
1236 		return;
1237 	}
1238 
1239 	if (smo->smo_object == 0) {
1240 		ASSERT(smo->smo_objsize == 0);
1241 		ASSERT(smo->smo_alloc == 0);
1242 		smo->smo_object = dmu_object_alloc(mos,
1243 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1244 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1245 		ASSERT(smo->smo_object != 0);
1246 		vdev_config_dirty(vd->vdev_top);
1247 	}
1248 
1249 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1250 
1251 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1252 	    &smlock);
1253 
1254 	mutex_enter(&smlock);
1255 
1256 	mutex_enter(&vd->vdev_dtl_lock);
1257 	space_map_walk(sm, space_map_add, &smsync);
1258 	mutex_exit(&vd->vdev_dtl_lock);
1259 
1260 	space_map_truncate(smo, mos, tx);
1261 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1262 
1263 	space_map_destroy(&smsync);
1264 
1265 	mutex_exit(&smlock);
1266 	mutex_destroy(&smlock);
1267 
1268 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1269 	dmu_buf_will_dirty(db, tx);
1270 	ASSERT3U(db->db_size, ==, sizeof (*smo));
1271 	bcopy(smo, db->db_data, db->db_size);
1272 	dmu_buf_rele(db, FTAG);
1273 
1274 	dmu_tx_commit(tx);
1275 }
1276 
1277 void
1278 vdev_load(vdev_t *vd)
1279 {
1280 	int c;
1281 
1282 	/*
1283 	 * Recursively load all children.
1284 	 */
1285 	for (c = 0; c < vd->vdev_children; c++)
1286 		vdev_load(vd->vdev_child[c]);
1287 
1288 	/*
1289 	 * If this is a top-level vdev, initialize its metaslabs.
1290 	 */
1291 	if (vd == vd->vdev_top &&
1292 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1293 	    vdev_metaslab_init(vd, 0) != 0))
1294 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1295 		    VDEV_AUX_CORRUPT_DATA);
1296 
1297 	/*
1298 	 * If this is a leaf vdev, load its DTL.
1299 	 */
1300 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1301 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1302 		    VDEV_AUX_CORRUPT_DATA);
1303 }
1304 
1305 /*
1306  * This special case of vdev_spare() is used for hot spares.  It's sole purpose
1307  * it to set the vdev state for the associated vdev.  To do this, we make sure
1308  * that we can open the underlying device, then try to read the label, and make
1309  * sure that the label is sane and that it hasn't been repurposed to another
1310  * pool.
1311  */
1312 int
1313 vdev_validate_spare(vdev_t *vd)
1314 {
1315 	nvlist_t *label;
1316 	uint64_t guid, version;
1317 	uint64_t state;
1318 
1319 	if ((label = vdev_label_read_config(vd)) == NULL) {
1320 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1321 		    VDEV_AUX_CORRUPT_DATA);
1322 		return (-1);
1323 	}
1324 
1325 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1326 	    version > ZFS_VERSION ||
1327 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1328 	    guid != vd->vdev_guid ||
1329 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1330 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1331 		    VDEV_AUX_CORRUPT_DATA);
1332 		nvlist_free(label);
1333 		return (-1);
1334 	}
1335 
1336 	spa_spare_add(vd);
1337 
1338 	/*
1339 	 * We don't actually check the pool state here.  If it's in fact in
1340 	 * use by another pool, we update this fact on the fly when requested.
1341 	 */
1342 	nvlist_free(label);
1343 	return (0);
1344 }
1345 
1346 void
1347 vdev_sync_done(vdev_t *vd, uint64_t txg)
1348 {
1349 	metaslab_t *msp;
1350 
1351 	dprintf("%s txg %llu\n", vdev_description(vd), txg);
1352 
1353 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1354 		metaslab_sync_done(msp, txg);
1355 }
1356 
1357 void
1358 vdev_sync(vdev_t *vd, uint64_t txg)
1359 {
1360 	spa_t *spa = vd->vdev_spa;
1361 	vdev_t *lvd;
1362 	metaslab_t *msp;
1363 	dmu_tx_t *tx;
1364 
1365 	dprintf("%s txg %llu pass %d\n",
1366 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1367 
1368 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1369 		ASSERT(vd == vd->vdev_top);
1370 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1371 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1372 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1373 		ASSERT(vd->vdev_ms_array != 0);
1374 		vdev_config_dirty(vd);
1375 		dmu_tx_commit(tx);
1376 	}
1377 
1378 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1379 		metaslab_sync(msp, txg);
1380 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1381 	}
1382 
1383 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1384 		vdev_dtl_sync(lvd, txg);
1385 
1386 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1387 }
1388 
1389 uint64_t
1390 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1391 {
1392 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1393 }
1394 
1395 void
1396 vdev_io_start(zio_t *zio)
1397 {
1398 	zio->io_vd->vdev_ops->vdev_op_io_start(zio);
1399 }
1400 
1401 void
1402 vdev_io_done(zio_t *zio)
1403 {
1404 	zio->io_vd->vdev_ops->vdev_op_io_done(zio);
1405 }
1406 
1407 const char *
1408 vdev_description(vdev_t *vd)
1409 {
1410 	if (vd == NULL || vd->vdev_ops == NULL)
1411 		return ("<unknown>");
1412 
1413 	if (vd->vdev_path != NULL)
1414 		return (vd->vdev_path);
1415 
1416 	if (vd->vdev_parent == NULL)
1417 		return (spa_name(vd->vdev_spa));
1418 
1419 	return (vd->vdev_ops->vdev_op_type);
1420 }
1421 
1422 int
1423 vdev_online(spa_t *spa, uint64_t guid)
1424 {
1425 	vdev_t *rvd, *vd;
1426 	uint64_t txg;
1427 
1428 	txg = spa_vdev_enter(spa);
1429 
1430 	rvd = spa->spa_root_vdev;
1431 
1432 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1433 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1434 
1435 	if (!vd->vdev_ops->vdev_op_leaf)
1436 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1437 
1438 	dprintf("ONLINE: %s\n", vdev_description(vd));
1439 
1440 	vd->vdev_offline = B_FALSE;
1441 	vd->vdev_tmpoffline = B_FALSE;
1442 	vdev_reopen(vd->vdev_top);
1443 
1444 	vdev_config_dirty(vd->vdev_top);
1445 
1446 	(void) spa_vdev_exit(spa, NULL, txg, 0);
1447 
1448 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1449 
1450 	return (0);
1451 }
1452 
1453 int
1454 vdev_offline(spa_t *spa, uint64_t guid, int istmp)
1455 {
1456 	vdev_t *rvd, *vd;
1457 	uint64_t txg;
1458 
1459 	txg = spa_vdev_enter(spa);
1460 
1461 	rvd = spa->spa_root_vdev;
1462 
1463 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1464 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1465 
1466 	if (!vd->vdev_ops->vdev_op_leaf)
1467 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1468 
1469 	dprintf("OFFLINE: %s\n", vdev_description(vd));
1470 
1471 	/*
1472 	 * If the device isn't already offline, try to offline it.
1473 	 */
1474 	if (!vd->vdev_offline) {
1475 		/*
1476 		 * If this device's top-level vdev has a non-empty DTL,
1477 		 * don't allow the device to be offlined.
1478 		 *
1479 		 * XXX -- make this more precise by allowing the offline
1480 		 * as long as the remaining devices don't have any DTL holes.
1481 		 */
1482 		if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1483 			return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1484 
1485 		/*
1486 		 * Offline this device and reopen its top-level vdev.
1487 		 * If this action results in the top-level vdev becoming
1488 		 * unusable, undo it and fail the request.
1489 		 */
1490 		vd->vdev_offline = B_TRUE;
1491 		vdev_reopen(vd->vdev_top);
1492 		if (vdev_is_dead(vd->vdev_top)) {
1493 			vd->vdev_offline = B_FALSE;
1494 			vdev_reopen(vd->vdev_top);
1495 			return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1496 		}
1497 	}
1498 
1499 	vd->vdev_tmpoffline = istmp;
1500 
1501 	vdev_config_dirty(vd->vdev_top);
1502 
1503 	return (spa_vdev_exit(spa, NULL, txg, 0));
1504 }
1505 
1506 /*
1507  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
1508  * vdev_offline(), we assume the spa config is locked.  We also clear all
1509  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
1510  */
1511 void
1512 vdev_clear(spa_t *spa, vdev_t *vd)
1513 {
1514 	int c;
1515 
1516 	if (vd == NULL)
1517 		vd = spa->spa_root_vdev;
1518 
1519 	vd->vdev_stat.vs_read_errors = 0;
1520 	vd->vdev_stat.vs_write_errors = 0;
1521 	vd->vdev_stat.vs_checksum_errors = 0;
1522 
1523 	for (c = 0; c < vd->vdev_children; c++)
1524 		vdev_clear(spa, vd->vdev_child[c]);
1525 }
1526 
1527 int
1528 vdev_is_dead(vdev_t *vd)
1529 {
1530 	return (vd->vdev_state <= VDEV_STATE_CANT_OPEN);
1531 }
1532 
1533 int
1534 vdev_error_inject(vdev_t *vd, zio_t *zio)
1535 {
1536 	int error = 0;
1537 
1538 	if (vd->vdev_fault_mode == VDEV_FAULT_NONE)
1539 		return (0);
1540 
1541 	if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0)
1542 		return (0);
1543 
1544 	switch (vd->vdev_fault_mode) {
1545 	case VDEV_FAULT_RANDOM:
1546 		if (spa_get_random(vd->vdev_fault_arg) == 0)
1547 			error = EIO;
1548 		break;
1549 
1550 	case VDEV_FAULT_COUNT:
1551 		if ((int64_t)--vd->vdev_fault_arg <= 0)
1552 			vd->vdev_fault_mode = VDEV_FAULT_NONE;
1553 		error = EIO;
1554 		break;
1555 	}
1556 
1557 	if (error != 0) {
1558 		dprintf("returning %d for type %d on %s state %d offset %llx\n",
1559 		    error, zio->io_type, vdev_description(vd),
1560 		    vd->vdev_state, zio->io_offset);
1561 	}
1562 
1563 	return (error);
1564 }
1565 
1566 /*
1567  * Get statistics for the given vdev.
1568  */
1569 void
1570 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1571 {
1572 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1573 	int c, t;
1574 
1575 	mutex_enter(&vd->vdev_stat_lock);
1576 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1577 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1578 	vs->vs_state = vd->vdev_state;
1579 	vs->vs_rsize = vdev_get_rsize(vd);
1580 	mutex_exit(&vd->vdev_stat_lock);
1581 
1582 	/*
1583 	 * If we're getting stats on the root vdev, aggregate the I/O counts
1584 	 * over all top-level vdevs (i.e. the direct children of the root).
1585 	 */
1586 	if (vd == rvd) {
1587 		for (c = 0; c < rvd->vdev_children; c++) {
1588 			vdev_t *cvd = rvd->vdev_child[c];
1589 			vdev_stat_t *cvs = &cvd->vdev_stat;
1590 
1591 			mutex_enter(&vd->vdev_stat_lock);
1592 			for (t = 0; t < ZIO_TYPES; t++) {
1593 				vs->vs_ops[t] += cvs->vs_ops[t];
1594 				vs->vs_bytes[t] += cvs->vs_bytes[t];
1595 			}
1596 			vs->vs_read_errors += cvs->vs_read_errors;
1597 			vs->vs_write_errors += cvs->vs_write_errors;
1598 			vs->vs_checksum_errors += cvs->vs_checksum_errors;
1599 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
1600 			vs->vs_scrub_errors += cvs->vs_scrub_errors;
1601 			mutex_exit(&vd->vdev_stat_lock);
1602 		}
1603 	}
1604 }
1605 
1606 void
1607 vdev_stat_update(zio_t *zio)
1608 {
1609 	vdev_t *vd = zio->io_vd;
1610 	vdev_t *pvd;
1611 	uint64_t txg = zio->io_txg;
1612 	vdev_stat_t *vs = &vd->vdev_stat;
1613 	zio_type_t type = zio->io_type;
1614 	int flags = zio->io_flags;
1615 
1616 	if (zio->io_error == 0) {
1617 		if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1618 			mutex_enter(&vd->vdev_stat_lock);
1619 			vs->vs_ops[type]++;
1620 			vs->vs_bytes[type] += zio->io_size;
1621 			mutex_exit(&vd->vdev_stat_lock);
1622 		}
1623 		if ((flags & ZIO_FLAG_IO_REPAIR) &&
1624 		    zio->io_delegate_list == NULL) {
1625 			mutex_enter(&vd->vdev_stat_lock);
1626 			if (flags & ZIO_FLAG_SCRUB_THREAD)
1627 				vs->vs_scrub_repaired += zio->io_size;
1628 			else
1629 				vs->vs_self_healed += zio->io_size;
1630 			mutex_exit(&vd->vdev_stat_lock);
1631 		}
1632 		return;
1633 	}
1634 
1635 	if (flags & ZIO_FLAG_SPECULATIVE)
1636 		return;
1637 
1638 	if (!vdev_is_dead(vd)) {
1639 		mutex_enter(&vd->vdev_stat_lock);
1640 		if (type == ZIO_TYPE_READ) {
1641 			if (zio->io_error == ECKSUM)
1642 				vs->vs_checksum_errors++;
1643 			else
1644 				vs->vs_read_errors++;
1645 		}
1646 		if (type == ZIO_TYPE_WRITE)
1647 			vs->vs_write_errors++;
1648 		mutex_exit(&vd->vdev_stat_lock);
1649 	}
1650 
1651 	if (type == ZIO_TYPE_WRITE) {
1652 		if (txg == 0 || vd->vdev_children != 0)
1653 			return;
1654 		if (flags & ZIO_FLAG_SCRUB_THREAD) {
1655 			ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1656 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1657 				vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1658 		}
1659 		if (!(flags & ZIO_FLAG_IO_REPAIR)) {
1660 			if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
1661 				return;
1662 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1663 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1664 				vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
1665 		}
1666 	}
1667 }
1668 
1669 void
1670 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
1671 {
1672 	int c;
1673 	vdev_stat_t *vs = &vd->vdev_stat;
1674 
1675 	for (c = 0; c < vd->vdev_children; c++)
1676 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
1677 
1678 	mutex_enter(&vd->vdev_stat_lock);
1679 
1680 	if (type == POOL_SCRUB_NONE) {
1681 		/*
1682 		 * Update completion and end time.  Leave everything else alone
1683 		 * so we can report what happened during the previous scrub.
1684 		 */
1685 		vs->vs_scrub_complete = complete;
1686 		vs->vs_scrub_end = gethrestime_sec();
1687 	} else {
1688 		vs->vs_scrub_type = type;
1689 		vs->vs_scrub_complete = 0;
1690 		vs->vs_scrub_examined = 0;
1691 		vs->vs_scrub_repaired = 0;
1692 		vs->vs_scrub_errors = 0;
1693 		vs->vs_scrub_start = gethrestime_sec();
1694 		vs->vs_scrub_end = 0;
1695 	}
1696 
1697 	mutex_exit(&vd->vdev_stat_lock);
1698 }
1699 
1700 /*
1701  * Update the in-core space usage stats for this vdev and the root vdev.
1702  */
1703 void
1704 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta)
1705 {
1706 	ASSERT(vd == vd->vdev_top);
1707 	int64_t dspace_delta = space_delta;
1708 
1709 	do {
1710 		if (vd->vdev_ms_count) {
1711 			/*
1712 			 * If this is a top-level vdev, apply the
1713 			 * inverse of its psize-to-asize (ie. RAID-Z)
1714 			 * space-expansion factor.  We must calculate
1715 			 * this here and not at the root vdev because
1716 			 * the root vdev's psize-to-asize is simply the
1717 			 * max of its childrens', thus not accurate
1718 			 * enough for us.
1719 			 */
1720 			ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
1721 			dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
1722 			    vd->vdev_deflate_ratio;
1723 		}
1724 
1725 		mutex_enter(&vd->vdev_stat_lock);
1726 		vd->vdev_stat.vs_space += space_delta;
1727 		vd->vdev_stat.vs_alloc += alloc_delta;
1728 		vd->vdev_stat.vs_dspace += dspace_delta;
1729 		mutex_exit(&vd->vdev_stat_lock);
1730 	} while ((vd = vd->vdev_parent) != NULL);
1731 }
1732 
1733 /*
1734  * Mark a top-level vdev's config as dirty, placing it on the dirty list
1735  * so that it will be written out next time the vdev configuration is synced.
1736  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
1737  */
1738 void
1739 vdev_config_dirty(vdev_t *vd)
1740 {
1741 	spa_t *spa = vd->vdev_spa;
1742 	vdev_t *rvd = spa->spa_root_vdev;
1743 	int c;
1744 
1745 	/*
1746 	 * The dirty list is protected by the config lock.  The caller must
1747 	 * either hold the config lock as writer, or must be the sync thread
1748 	 * (which holds the lock as reader).  There's only one sync thread,
1749 	 * so this is sufficient to ensure mutual exclusion.
1750 	 */
1751 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1752 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1753 
1754 	if (vd == rvd) {
1755 		for (c = 0; c < rvd->vdev_children; c++)
1756 			vdev_config_dirty(rvd->vdev_child[c]);
1757 	} else {
1758 		ASSERT(vd == vd->vdev_top);
1759 
1760 		if (!list_link_active(&vd->vdev_dirty_node))
1761 			list_insert_head(&spa->spa_dirty_list, vd);
1762 	}
1763 }
1764 
1765 void
1766 vdev_config_clean(vdev_t *vd)
1767 {
1768 	spa_t *spa = vd->vdev_spa;
1769 
1770 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1771 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1772 
1773 	ASSERT(list_link_active(&vd->vdev_dirty_node));
1774 	list_remove(&spa->spa_dirty_list, vd);
1775 }
1776 
1777 void
1778 vdev_propagate_state(vdev_t *vd)
1779 {
1780 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1781 	int degraded = 0, faulted = 0;
1782 	int corrupted = 0;
1783 	int c;
1784 	vdev_t *child;
1785 
1786 	for (c = 0; c < vd->vdev_children; c++) {
1787 		child = vd->vdev_child[c];
1788 		if (child->vdev_state <= VDEV_STATE_CANT_OPEN)
1789 			faulted++;
1790 		else if (child->vdev_state == VDEV_STATE_DEGRADED)
1791 			degraded++;
1792 
1793 		if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
1794 			corrupted++;
1795 	}
1796 
1797 	vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
1798 
1799 	/*
1800 	 * Root special: if there is a toplevel vdev that cannot be
1801 	 * opened due to corrupted metadata, then propagate the root
1802 	 * vdev's aux state as 'corrupt' rather than 'insufficient
1803 	 * replicas'.
1804 	 */
1805 	if (corrupted && vd == rvd && rvd->vdev_state == VDEV_STATE_CANT_OPEN)
1806 		vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
1807 		    VDEV_AUX_CORRUPT_DATA);
1808 }
1809 
1810 /*
1811  * Set a vdev's state.  If this is during an open, we don't update the parent
1812  * state, because we're in the process of opening children depth-first.
1813  * Otherwise, we propagate the change to the parent.
1814  *
1815  * If this routine places a device in a faulted state, an appropriate ereport is
1816  * generated.
1817  */
1818 void
1819 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
1820 {
1821 	uint64_t save_state;
1822 
1823 	if (state == vd->vdev_state) {
1824 		vd->vdev_stat.vs_aux = aux;
1825 		return;
1826 	}
1827 
1828 	save_state = vd->vdev_state;
1829 
1830 	vd->vdev_state = state;
1831 	vd->vdev_stat.vs_aux = aux;
1832 
1833 	if (state == VDEV_STATE_CANT_OPEN) {
1834 		/*
1835 		 * If we fail to open a vdev during an import, we mark it as
1836 		 * "not available", which signifies that it was never there to
1837 		 * begin with.  Failure to open such a device is not considered
1838 		 * an error.
1839 		 */
1840 		if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT &&
1841 		    vd->vdev_ops->vdev_op_leaf)
1842 			vd->vdev_not_present = 1;
1843 
1844 		/*
1845 		 * Post the appropriate ereport.  If the 'prevstate' field is
1846 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
1847 		 * that this is part of a vdev_reopen().  In this case, we don't
1848 		 * want to post the ereport if the device was already in the
1849 		 * CANT_OPEN state beforehand.
1850 		 */
1851 		if (vd->vdev_prevstate != state && !vd->vdev_not_present &&
1852 		    vd != vd->vdev_spa->spa_root_vdev) {
1853 			const char *class;
1854 
1855 			switch (aux) {
1856 			case VDEV_AUX_OPEN_FAILED:
1857 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
1858 				break;
1859 			case VDEV_AUX_CORRUPT_DATA:
1860 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
1861 				break;
1862 			case VDEV_AUX_NO_REPLICAS:
1863 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
1864 				break;
1865 			case VDEV_AUX_BAD_GUID_SUM:
1866 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
1867 				break;
1868 			case VDEV_AUX_TOO_SMALL:
1869 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
1870 				break;
1871 			case VDEV_AUX_BAD_LABEL:
1872 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
1873 				break;
1874 			default:
1875 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
1876 			}
1877 
1878 			zfs_ereport_post(class, vd->vdev_spa,
1879 			    vd, NULL, save_state, 0);
1880 		}
1881 	}
1882 
1883 	if (isopen)
1884 		return;
1885 
1886 	if (vd->vdev_parent != NULL)
1887 		vdev_propagate_state(vd->vdev_parent);
1888 }
1889