xref: /titanic_51/usr/src/uts/common/fs/zfs/vdev.c (revision a12e05a04a5d5850f645c79e7e2c74f8d6b7c5ec)
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 2006 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->vdev_guid);
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 	 * Look for the 'is_spare' flag.  If this is the case, then we are a
460 	 * repurposed hot spare.
461 	 */
462 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
463 	    &vd->vdev_isspare);
464 	if (vd->vdev_isspare)
465 		spa_spare_add(vd->vdev_guid);
466 
467 	/*
468 	 * If we're a top-level vdev, try to load the allocation parameters.
469 	 */
470 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
471 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
472 		    &vd->vdev_ms_array);
473 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
474 		    &vd->vdev_ms_shift);
475 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
476 		    &vd->vdev_asize);
477 	}
478 
479 	/*
480 	 * If we're a leaf vdev, try to load the DTL object and offline state.
481 	 */
482 	if (vd->vdev_ops->vdev_op_leaf && alloctype == VDEV_ALLOC_LOAD) {
483 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
484 		    &vd->vdev_dtl.smo_object);
485 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
486 		    &vd->vdev_offline);
487 	}
488 
489 	/*
490 	 * Add ourselves to the parent's list of children.
491 	 */
492 	vdev_add_child(parent, vd);
493 
494 	*vdp = vd;
495 
496 	return (0);
497 }
498 
499 void
500 vdev_free(vdev_t *vd)
501 {
502 	int c;
503 
504 	/*
505 	 * vdev_free() implies closing the vdev first.  This is simpler than
506 	 * trying to ensure complicated semantics for all callers.
507 	 */
508 	vdev_close(vd);
509 
510 	ASSERT(!list_link_active(&vd->vdev_dirty_node));
511 
512 	/*
513 	 * Free all children.
514 	 */
515 	for (c = 0; c < vd->vdev_children; c++)
516 		vdev_free(vd->vdev_child[c]);
517 
518 	ASSERT(vd->vdev_child == NULL);
519 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
520 
521 	/*
522 	 * Discard allocation state.
523 	 */
524 	if (vd == vd->vdev_top)
525 		vdev_metaslab_fini(vd);
526 
527 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
528 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
529 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
530 
531 	/*
532 	 * Remove this vdev from its parent's child list.
533 	 */
534 	vdev_remove_child(vd->vdev_parent, vd);
535 
536 	ASSERT(vd->vdev_parent == NULL);
537 
538 	vdev_free_common(vd);
539 }
540 
541 /*
542  * Transfer top-level vdev state from svd to tvd.
543  */
544 static void
545 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
546 {
547 	spa_t *spa = svd->vdev_spa;
548 	metaslab_t *msp;
549 	vdev_t *vd;
550 	int t;
551 
552 	ASSERT(tvd == tvd->vdev_top);
553 
554 	tvd->vdev_ms_array = svd->vdev_ms_array;
555 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
556 	tvd->vdev_ms_count = svd->vdev_ms_count;
557 
558 	svd->vdev_ms_array = 0;
559 	svd->vdev_ms_shift = 0;
560 	svd->vdev_ms_count = 0;
561 
562 	tvd->vdev_mg = svd->vdev_mg;
563 	tvd->vdev_ms = svd->vdev_ms;
564 
565 	svd->vdev_mg = NULL;
566 	svd->vdev_ms = NULL;
567 
568 	if (tvd->vdev_mg != NULL)
569 		tvd->vdev_mg->mg_vd = tvd;
570 
571 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
572 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
573 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
574 
575 	svd->vdev_stat.vs_alloc = 0;
576 	svd->vdev_stat.vs_space = 0;
577 	svd->vdev_stat.vs_dspace = 0;
578 
579 	for (t = 0; t < TXG_SIZE; t++) {
580 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
581 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
582 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
583 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
584 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
585 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
586 	}
587 
588 	if (list_link_active(&svd->vdev_dirty_node)) {
589 		vdev_config_clean(svd);
590 		vdev_config_dirty(tvd);
591 	}
592 
593 	tvd->vdev_reopen_wanted = svd->vdev_reopen_wanted;
594 	svd->vdev_reopen_wanted = 0;
595 
596 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
597 	svd->vdev_deflate_ratio = 0;
598 }
599 
600 static void
601 vdev_top_update(vdev_t *tvd, vdev_t *vd)
602 {
603 	int c;
604 
605 	if (vd == NULL)
606 		return;
607 
608 	vd->vdev_top = tvd;
609 
610 	for (c = 0; c < vd->vdev_children; c++)
611 		vdev_top_update(tvd, vd->vdev_child[c]);
612 }
613 
614 /*
615  * Add a mirror/replacing vdev above an existing vdev.
616  */
617 vdev_t *
618 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
619 {
620 	spa_t *spa = cvd->vdev_spa;
621 	vdev_t *pvd = cvd->vdev_parent;
622 	vdev_t *mvd;
623 
624 	ASSERT(spa_config_held(spa, RW_WRITER));
625 
626 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
627 
628 	mvd->vdev_asize = cvd->vdev_asize;
629 	mvd->vdev_ashift = cvd->vdev_ashift;
630 	mvd->vdev_state = cvd->vdev_state;
631 
632 	vdev_remove_child(pvd, cvd);
633 	vdev_add_child(pvd, mvd);
634 	cvd->vdev_id = mvd->vdev_children;
635 	vdev_add_child(mvd, cvd);
636 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
637 
638 	if (mvd == mvd->vdev_top)
639 		vdev_top_transfer(cvd, mvd);
640 
641 	return (mvd);
642 }
643 
644 /*
645  * Remove a 1-way mirror/replacing vdev from the tree.
646  */
647 void
648 vdev_remove_parent(vdev_t *cvd)
649 {
650 	vdev_t *mvd = cvd->vdev_parent;
651 	vdev_t *pvd = mvd->vdev_parent;
652 
653 	ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
654 
655 	ASSERT(mvd->vdev_children == 1);
656 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
657 	    mvd->vdev_ops == &vdev_replacing_ops ||
658 	    mvd->vdev_ops == &vdev_spare_ops);
659 	cvd->vdev_ashift = mvd->vdev_ashift;
660 
661 	vdev_remove_child(mvd, cvd);
662 	vdev_remove_child(pvd, mvd);
663 	cvd->vdev_id = mvd->vdev_id;
664 	vdev_add_child(pvd, cvd);
665 	/*
666 	 * If we created a new toplevel vdev, then we need to change the child's
667 	 * vdev GUID to match the old toplevel vdev.  Otherwise, we could have
668 	 * detached an offline device, and when we go to import the pool we'll
669 	 * think we have two toplevel vdevs, instead of a different version of
670 	 * the same toplevel vdev.
671 	 */
672 	if (cvd->vdev_top == cvd) {
673 		pvd->vdev_guid_sum -= cvd->vdev_guid;
674 		cvd->vdev_guid_sum -= cvd->vdev_guid;
675 		cvd->vdev_guid = mvd->vdev_guid;
676 		cvd->vdev_guid_sum += mvd->vdev_guid;
677 		pvd->vdev_guid_sum += cvd->vdev_guid;
678 	}
679 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
680 
681 	if (cvd == cvd->vdev_top)
682 		vdev_top_transfer(mvd, cvd);
683 
684 	ASSERT(mvd->vdev_children == 0);
685 	vdev_free(mvd);
686 }
687 
688 int
689 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
690 {
691 	spa_t *spa = vd->vdev_spa;
692 	objset_t *mos = spa->spa_meta_objset;
693 	metaslab_class_t *mc = spa_metaslab_class_select(spa);
694 	uint64_t m;
695 	uint64_t oldc = vd->vdev_ms_count;
696 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
697 	metaslab_t **mspp;
698 	int error;
699 
700 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
701 		return (0);
702 
703 	dprintf("%s oldc %llu newc %llu\n", vdev_description(vd), oldc, newc);
704 
705 	ASSERT(oldc <= newc);
706 
707 	if (vd->vdev_mg == NULL)
708 		vd->vdev_mg = metaslab_group_create(mc, vd);
709 
710 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
711 
712 	if (oldc != 0) {
713 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
714 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
715 	}
716 
717 	vd->vdev_ms = mspp;
718 	vd->vdev_ms_count = newc;
719 
720 	for (m = oldc; m < newc; m++) {
721 		space_map_obj_t smo = { 0, 0, 0 };
722 		if (txg == 0) {
723 			uint64_t object = 0;
724 			error = dmu_read(mos, vd->vdev_ms_array,
725 			    m * sizeof (uint64_t), sizeof (uint64_t), &object);
726 			if (error)
727 				return (error);
728 			if (object != 0) {
729 				dmu_buf_t *db;
730 				error = dmu_bonus_hold(mos, object, FTAG, &db);
731 				if (error)
732 					return (error);
733 				ASSERT3U(db->db_size, ==, sizeof (smo));
734 				bcopy(db->db_data, &smo, db->db_size);
735 				ASSERT3U(smo.smo_object, ==, object);
736 				dmu_buf_rele(db, FTAG);
737 			}
738 		}
739 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
740 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
741 	}
742 
743 	return (0);
744 }
745 
746 void
747 vdev_metaslab_fini(vdev_t *vd)
748 {
749 	uint64_t m;
750 	uint64_t count = vd->vdev_ms_count;
751 
752 	if (vd->vdev_ms != NULL) {
753 		for (m = 0; m < count; m++)
754 			if (vd->vdev_ms[m] != NULL)
755 				metaslab_fini(vd->vdev_ms[m]);
756 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
757 		vd->vdev_ms = NULL;
758 	}
759 }
760 
761 /*
762  * Prepare a virtual device for access.
763  */
764 int
765 vdev_open(vdev_t *vd)
766 {
767 	int error;
768 	int c;
769 	uint64_t osize = 0;
770 	uint64_t asize, psize;
771 	uint64_t ashift = 0;
772 
773 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
774 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
775 	    vd->vdev_state == VDEV_STATE_OFFLINE);
776 
777 	if (vd->vdev_fault_mode == VDEV_FAULT_COUNT)
778 		vd->vdev_fault_arg >>= 1;
779 	else
780 		vd->vdev_fault_mode = VDEV_FAULT_NONE;
781 
782 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
783 
784 	if (vd->vdev_ops->vdev_op_leaf) {
785 		vdev_cache_init(vd);
786 		vdev_queue_init(vd);
787 		vd->vdev_cache_active = B_TRUE;
788 	}
789 
790 	if (vd->vdev_offline) {
791 		ASSERT(vd->vdev_children == 0);
792 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
793 		return (ENXIO);
794 	}
795 
796 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
797 
798 	if (zio_injection_enabled && error == 0)
799 		error = zio_handle_device_injection(vd, ENXIO);
800 
801 	dprintf("%s = %d, osize %llu, state = %d\n",
802 	    vdev_description(vd), error, osize, vd->vdev_state);
803 
804 	if (error) {
805 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
806 		    vd->vdev_stat.vs_aux);
807 		return (error);
808 	}
809 
810 	vd->vdev_state = VDEV_STATE_HEALTHY;
811 
812 	for (c = 0; c < vd->vdev_children; c++)
813 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
814 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
815 			    VDEV_AUX_NONE);
816 			break;
817 		}
818 
819 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
820 
821 	if (vd->vdev_children == 0) {
822 		if (osize < SPA_MINDEVSIZE) {
823 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
824 			    VDEV_AUX_TOO_SMALL);
825 			return (EOVERFLOW);
826 		}
827 		psize = osize;
828 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
829 	} else {
830 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
831 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
832 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
833 			    VDEV_AUX_TOO_SMALL);
834 			return (EOVERFLOW);
835 		}
836 		psize = 0;
837 		asize = osize;
838 	}
839 
840 	vd->vdev_psize = psize;
841 
842 	if (vd->vdev_asize == 0) {
843 		/*
844 		 * This is the first-ever open, so use the computed values.
845 		 * For testing purposes, a higher ashift can be requested.
846 		 */
847 		vd->vdev_asize = asize;
848 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
849 	} else {
850 		/*
851 		 * Make sure the alignment requirement hasn't increased.
852 		 */
853 		if (ashift > vd->vdev_top->vdev_ashift) {
854 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
855 			    VDEV_AUX_BAD_LABEL);
856 			return (EINVAL);
857 		}
858 
859 		/*
860 		 * Make sure the device hasn't shrunk.
861 		 */
862 		if (asize < vd->vdev_asize) {
863 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
864 			    VDEV_AUX_BAD_LABEL);
865 			return (EINVAL);
866 		}
867 
868 		/*
869 		 * If all children are healthy and the asize has increased,
870 		 * then we've experienced dynamic LUN growth.
871 		 */
872 		if (vd->vdev_state == VDEV_STATE_HEALTHY &&
873 		    asize > vd->vdev_asize) {
874 			vd->vdev_asize = asize;
875 		}
876 	}
877 
878 	/*
879 	 * If this is a top-level vdev, compute the raidz-deflation
880 	 * ratio.  Note, we hard-code in 128k (1<<17) because it is the
881 	 * current "typical" blocksize.  Even if SPA_MAXBLOCKSIZE
882 	 * changes, this algorithm must never change, or we will
883 	 * inconsistently account for existing bp's.
884 	 */
885 	if (vd->vdev_top == vd) {
886 		vd->vdev_deflate_ratio = (1<<17) /
887 		    (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
888 	}
889 
890 	/*
891 	 * This allows the ZFS DE to close cases appropriately.  If a device
892 	 * goes away and later returns, we want to close the associated case.
893 	 * But it's not enough to simply post this only when a device goes from
894 	 * CANT_OPEN -> HEALTHY.  If we reboot the system and the device is
895 	 * back, we also need to close the case (otherwise we will try to replay
896 	 * it).  So we have to post this notifier every time.  Since this only
897 	 * occurs during pool open or error recovery, this should not be an
898 	 * issue.
899 	 */
900 	zfs_post_ok(vd->vdev_spa, vd);
901 
902 	return (0);
903 }
904 
905 /*
906  * Called once the vdevs are all opened, this routine validates the label
907  * contents.  This needs to be done before vdev_load() so that we don't
908  * inadvertently do repair I/Os to the wrong device, and so that vdev_reopen()
909  * won't succeed if the device has been changed underneath.
910  *
911  * This function will only return failure if one of the vdevs indicates that it
912  * has since been destroyed or exported.  This is only possible if
913  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
914  * will be updated but the function will return 0.
915  */
916 int
917 vdev_validate(vdev_t *vd)
918 {
919 	spa_t *spa = vd->vdev_spa;
920 	int c;
921 	nvlist_t *label;
922 	uint64_t guid;
923 	uint64_t state;
924 
925 	for (c = 0; c < vd->vdev_children; c++)
926 		if (vdev_validate(vd->vdev_child[c]) != 0)
927 			return (-1);
928 
929 	/*
930 	 * If the device has already failed, or was marked offline, don't do
931 	 * any further validation.  Otherwise, label I/O will fail and we will
932 	 * overwrite the previous state.
933 	 */
934 	if (vd->vdev_ops->vdev_op_leaf && !vdev_is_dead(vd)) {
935 
936 		if ((label = vdev_label_read_config(vd)) == NULL) {
937 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
938 			    VDEV_AUX_BAD_LABEL);
939 			return (0);
940 		}
941 
942 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
943 		    &guid) != 0 || guid != spa_guid(spa)) {
944 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
945 			    VDEV_AUX_CORRUPT_DATA);
946 			nvlist_free(label);
947 			return (0);
948 		}
949 
950 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
951 		    &guid) != 0 || guid != vd->vdev_guid) {
952 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
953 			    VDEV_AUX_CORRUPT_DATA);
954 			nvlist_free(label);
955 			return (0);
956 		}
957 
958 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
959 		    &state) != 0) {
960 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
961 			    VDEV_AUX_CORRUPT_DATA);
962 			nvlist_free(label);
963 			return (0);
964 		}
965 
966 		nvlist_free(label);
967 
968 		if (spa->spa_load_state == SPA_LOAD_OPEN &&
969 		    state != POOL_STATE_ACTIVE)
970 			return (-1);
971 	}
972 
973 	/*
974 	 * If we were able to open and validate a vdev that was previously
975 	 * marked permanently unavailable, clear that state now.
976 	 */
977 	if (vd->vdev_not_present)
978 		vd->vdev_not_present = 0;
979 
980 	return (0);
981 }
982 
983 /*
984  * Close a virtual device.
985  */
986 void
987 vdev_close(vdev_t *vd)
988 {
989 	vd->vdev_ops->vdev_op_close(vd);
990 
991 	if (vd->vdev_cache_active) {
992 		vdev_cache_fini(vd);
993 		vdev_queue_fini(vd);
994 		vd->vdev_cache_active = B_FALSE;
995 	}
996 
997 	/*
998 	 * We record the previous state before we close it, so  that if we are
999 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1000 	 * it's still faulted.
1001 	 */
1002 	vd->vdev_prevstate = vd->vdev_state;
1003 
1004 	if (vd->vdev_offline)
1005 		vd->vdev_state = VDEV_STATE_OFFLINE;
1006 	else
1007 		vd->vdev_state = VDEV_STATE_CLOSED;
1008 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1009 }
1010 
1011 void
1012 vdev_reopen(vdev_t *vd)
1013 {
1014 	spa_t *spa = vd->vdev_spa;
1015 
1016 	ASSERT(spa_config_held(spa, RW_WRITER));
1017 
1018 	vdev_close(vd);
1019 	(void) vdev_open(vd);
1020 
1021 	/*
1022 	 * Reassess root vdev's health.
1023 	 */
1024 	vdev_propagate_state(spa->spa_root_vdev);
1025 }
1026 
1027 int
1028 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1029 {
1030 	int error;
1031 
1032 	/*
1033 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1034 	 * For a create, however, we want to fail the request if
1035 	 * there are any components we can't open.
1036 	 */
1037 	error = vdev_open(vd);
1038 
1039 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1040 		vdev_close(vd);
1041 		return (error ? error : ENXIO);
1042 	}
1043 
1044 	/*
1045 	 * Recursively initialize all labels.
1046 	 */
1047 	if ((error = vdev_label_init(vd, txg, isreplacing)) != 0) {
1048 		vdev_close(vd);
1049 		return (error);
1050 	}
1051 
1052 	return (0);
1053 }
1054 
1055 /*
1056  * The is the latter half of vdev_create().  It is distinct because it
1057  * involves initiating transactions in order to do metaslab creation.
1058  * For creation, we want to try to create all vdevs at once and then undo it
1059  * if anything fails; this is much harder if we have pending transactions.
1060  */
1061 void
1062 vdev_init(vdev_t *vd, uint64_t txg)
1063 {
1064 	/*
1065 	 * Aim for roughly 200 metaslabs per vdev.
1066 	 */
1067 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1068 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1069 
1070 	/*
1071 	 * Initialize the vdev's metaslabs.  This can't fail because
1072 	 * there's nothing to read when creating all new metaslabs.
1073 	 */
1074 	VERIFY(vdev_metaslab_init(vd, txg) == 0);
1075 }
1076 
1077 void
1078 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1079 {
1080 	ASSERT(vd == vd->vdev_top);
1081 	ASSERT(ISP2(flags));
1082 
1083 	if (flags & VDD_METASLAB)
1084 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1085 
1086 	if (flags & VDD_DTL)
1087 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1088 
1089 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1090 }
1091 
1092 void
1093 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1094 {
1095 	mutex_enter(sm->sm_lock);
1096 	if (!space_map_contains(sm, txg, size))
1097 		space_map_add(sm, txg, size);
1098 	mutex_exit(sm->sm_lock);
1099 }
1100 
1101 int
1102 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1103 {
1104 	int dirty;
1105 
1106 	/*
1107 	 * Quick test without the lock -- covers the common case that
1108 	 * there are no dirty time segments.
1109 	 */
1110 	if (sm->sm_space == 0)
1111 		return (0);
1112 
1113 	mutex_enter(sm->sm_lock);
1114 	dirty = space_map_contains(sm, txg, size);
1115 	mutex_exit(sm->sm_lock);
1116 
1117 	return (dirty);
1118 }
1119 
1120 /*
1121  * Reassess DTLs after a config change or scrub completion.
1122  */
1123 void
1124 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1125 {
1126 	spa_t *spa = vd->vdev_spa;
1127 	int c;
1128 
1129 	ASSERT(spa_config_held(spa, RW_WRITER));
1130 
1131 	if (vd->vdev_children == 0) {
1132 		mutex_enter(&vd->vdev_dtl_lock);
1133 		/*
1134 		 * We're successfully scrubbed everything up to scrub_txg.
1135 		 * Therefore, excise all old DTLs up to that point, then
1136 		 * fold in the DTLs for everything we couldn't scrub.
1137 		 */
1138 		if (scrub_txg != 0) {
1139 			space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1140 			space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1141 		}
1142 		if (scrub_done)
1143 			space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1144 		mutex_exit(&vd->vdev_dtl_lock);
1145 		if (txg != 0)
1146 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1147 		return;
1148 	}
1149 
1150 	/*
1151 	 * Make sure the DTLs are always correct under the scrub lock.
1152 	 */
1153 	if (vd == spa->spa_root_vdev)
1154 		mutex_enter(&spa->spa_scrub_lock);
1155 
1156 	mutex_enter(&vd->vdev_dtl_lock);
1157 	space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1158 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1159 	mutex_exit(&vd->vdev_dtl_lock);
1160 
1161 	for (c = 0; c < vd->vdev_children; c++) {
1162 		vdev_t *cvd = vd->vdev_child[c];
1163 		vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1164 		mutex_enter(&vd->vdev_dtl_lock);
1165 		space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1166 		space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1167 		mutex_exit(&vd->vdev_dtl_lock);
1168 	}
1169 
1170 	if (vd == spa->spa_root_vdev)
1171 		mutex_exit(&spa->spa_scrub_lock);
1172 }
1173 
1174 static int
1175 vdev_dtl_load(vdev_t *vd)
1176 {
1177 	spa_t *spa = vd->vdev_spa;
1178 	space_map_obj_t *smo = &vd->vdev_dtl;
1179 	objset_t *mos = spa->spa_meta_objset;
1180 	dmu_buf_t *db;
1181 	int error;
1182 
1183 	ASSERT(vd->vdev_children == 0);
1184 
1185 	if (smo->smo_object == 0)
1186 		return (0);
1187 
1188 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1189 		return (error);
1190 
1191 	ASSERT3U(db->db_size, ==, sizeof (*smo));
1192 	bcopy(db->db_data, smo, db->db_size);
1193 	dmu_buf_rele(db, FTAG);
1194 
1195 	mutex_enter(&vd->vdev_dtl_lock);
1196 	error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1197 	mutex_exit(&vd->vdev_dtl_lock);
1198 
1199 	return (error);
1200 }
1201 
1202 void
1203 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1204 {
1205 	spa_t *spa = vd->vdev_spa;
1206 	space_map_obj_t *smo = &vd->vdev_dtl;
1207 	space_map_t *sm = &vd->vdev_dtl_map;
1208 	objset_t *mos = spa->spa_meta_objset;
1209 	space_map_t smsync;
1210 	kmutex_t smlock;
1211 	dmu_buf_t *db;
1212 	dmu_tx_t *tx;
1213 
1214 	dprintf("%s in txg %llu pass %d\n",
1215 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1216 
1217 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1218 
1219 	if (vd->vdev_detached) {
1220 		if (smo->smo_object != 0) {
1221 			int err = dmu_object_free(mos, smo->smo_object, tx);
1222 			ASSERT3U(err, ==, 0);
1223 			smo->smo_object = 0;
1224 		}
1225 		dmu_tx_commit(tx);
1226 		dprintf("detach %s committed in txg %llu\n",
1227 		    vdev_description(vd), txg);
1228 		return;
1229 	}
1230 
1231 	if (smo->smo_object == 0) {
1232 		ASSERT(smo->smo_objsize == 0);
1233 		ASSERT(smo->smo_alloc == 0);
1234 		smo->smo_object = dmu_object_alloc(mos,
1235 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1236 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1237 		ASSERT(smo->smo_object != 0);
1238 		vdev_config_dirty(vd->vdev_top);
1239 	}
1240 
1241 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1242 
1243 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1244 	    &smlock);
1245 
1246 	mutex_enter(&smlock);
1247 
1248 	mutex_enter(&vd->vdev_dtl_lock);
1249 	space_map_walk(sm, space_map_add, &smsync);
1250 	mutex_exit(&vd->vdev_dtl_lock);
1251 
1252 	space_map_truncate(smo, mos, tx);
1253 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1254 
1255 	space_map_destroy(&smsync);
1256 
1257 	mutex_exit(&smlock);
1258 	mutex_destroy(&smlock);
1259 
1260 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1261 	dmu_buf_will_dirty(db, tx);
1262 	ASSERT3U(db->db_size, ==, sizeof (*smo));
1263 	bcopy(smo, db->db_data, db->db_size);
1264 	dmu_buf_rele(db, FTAG);
1265 
1266 	dmu_tx_commit(tx);
1267 }
1268 
1269 void
1270 vdev_load(vdev_t *vd)
1271 {
1272 	int c;
1273 
1274 	/*
1275 	 * Recursively load all children.
1276 	 */
1277 	for (c = 0; c < vd->vdev_children; c++)
1278 		vdev_load(vd->vdev_child[c]);
1279 
1280 	/*
1281 	 * If this is a top-level vdev, initialize its metaslabs.
1282 	 */
1283 	if (vd == vd->vdev_top &&
1284 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1285 	    vdev_metaslab_init(vd, 0) != 0))
1286 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1287 		    VDEV_AUX_CORRUPT_DATA);
1288 
1289 	/*
1290 	 * If this is a leaf vdev, load its DTL.
1291 	 */
1292 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1293 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1294 		    VDEV_AUX_CORRUPT_DATA);
1295 }
1296 
1297 /*
1298  * This special case of vdev_spare() is used for hot spares.  It's sole purpose
1299  * it to set the vdev state for the associated vdev.  To do this, we make sure
1300  * that we can open the underlying device, then try to read the label, and make
1301  * sure that the label is sane and that it hasn't been repurposed to another
1302  * pool.
1303  */
1304 int
1305 vdev_validate_spare(vdev_t *vd)
1306 {
1307 	nvlist_t *label;
1308 	uint64_t guid, version;
1309 	uint64_t state;
1310 
1311 	if ((label = vdev_label_read_config(vd)) == NULL) {
1312 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1313 		    VDEV_AUX_CORRUPT_DATA);
1314 		return (-1);
1315 	}
1316 
1317 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1318 	    version > ZFS_VERSION ||
1319 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1320 	    guid != vd->vdev_guid ||
1321 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1322 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1323 		    VDEV_AUX_CORRUPT_DATA);
1324 		nvlist_free(label);
1325 		return (-1);
1326 	}
1327 
1328 	/*
1329 	 * We don't actually check the pool state here.  If it's in fact in
1330 	 * use by another pool, we update this fact on the fly when requested.
1331 	 */
1332 	nvlist_free(label);
1333 	return (0);
1334 }
1335 
1336 void
1337 vdev_sync_done(vdev_t *vd, uint64_t txg)
1338 {
1339 	metaslab_t *msp;
1340 
1341 	dprintf("%s txg %llu\n", vdev_description(vd), txg);
1342 
1343 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1344 		metaslab_sync_done(msp, txg);
1345 }
1346 
1347 void
1348 vdev_sync(vdev_t *vd, uint64_t txg)
1349 {
1350 	spa_t *spa = vd->vdev_spa;
1351 	vdev_t *lvd;
1352 	metaslab_t *msp;
1353 	dmu_tx_t *tx;
1354 
1355 	dprintf("%s txg %llu pass %d\n",
1356 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1357 
1358 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1359 		ASSERT(vd == vd->vdev_top);
1360 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1361 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1362 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1363 		ASSERT(vd->vdev_ms_array != 0);
1364 		vdev_config_dirty(vd);
1365 		dmu_tx_commit(tx);
1366 	}
1367 
1368 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1369 		metaslab_sync(msp, txg);
1370 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1371 	}
1372 
1373 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1374 		vdev_dtl_sync(lvd, txg);
1375 
1376 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1377 }
1378 
1379 uint64_t
1380 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1381 {
1382 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1383 }
1384 
1385 void
1386 vdev_io_start(zio_t *zio)
1387 {
1388 	zio->io_vd->vdev_ops->vdev_op_io_start(zio);
1389 }
1390 
1391 void
1392 vdev_io_done(zio_t *zio)
1393 {
1394 	zio->io_vd->vdev_ops->vdev_op_io_done(zio);
1395 }
1396 
1397 const char *
1398 vdev_description(vdev_t *vd)
1399 {
1400 	if (vd == NULL || vd->vdev_ops == NULL)
1401 		return ("<unknown>");
1402 
1403 	if (vd->vdev_path != NULL)
1404 		return (vd->vdev_path);
1405 
1406 	if (vd->vdev_parent == NULL)
1407 		return (spa_name(vd->vdev_spa));
1408 
1409 	return (vd->vdev_ops->vdev_op_type);
1410 }
1411 
1412 int
1413 vdev_online(spa_t *spa, uint64_t guid)
1414 {
1415 	vdev_t *rvd, *vd;
1416 	uint64_t txg;
1417 
1418 	txg = spa_vdev_enter(spa);
1419 
1420 	rvd = spa->spa_root_vdev;
1421 
1422 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1423 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1424 
1425 	if (!vd->vdev_ops->vdev_op_leaf)
1426 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1427 
1428 	dprintf("ONLINE: %s\n", vdev_description(vd));
1429 
1430 	vd->vdev_offline = B_FALSE;
1431 	vd->vdev_tmpoffline = B_FALSE;
1432 	vdev_reopen(vd->vdev_top);
1433 
1434 	vdev_config_dirty(vd->vdev_top);
1435 
1436 	(void) spa_vdev_exit(spa, NULL, txg, 0);
1437 
1438 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1439 
1440 	return (0);
1441 }
1442 
1443 int
1444 vdev_offline(spa_t *spa, uint64_t guid, int istmp)
1445 {
1446 	vdev_t *rvd, *vd;
1447 	uint64_t txg;
1448 
1449 	txg = spa_vdev_enter(spa);
1450 
1451 	rvd = spa->spa_root_vdev;
1452 
1453 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1454 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1455 
1456 	if (!vd->vdev_ops->vdev_op_leaf)
1457 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1458 
1459 	dprintf("OFFLINE: %s\n", vdev_description(vd));
1460 
1461 	/*
1462 	 * If the device isn't already offline, try to offline it.
1463 	 */
1464 	if (!vd->vdev_offline) {
1465 		/*
1466 		 * If this device's top-level vdev has a non-empty DTL,
1467 		 * don't allow the device to be offlined.
1468 		 *
1469 		 * XXX -- make this more precise by allowing the offline
1470 		 * as long as the remaining devices don't have any DTL holes.
1471 		 */
1472 		if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1473 			return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1474 
1475 		/*
1476 		 * Offline this device and reopen its top-level vdev.
1477 		 * If this action results in the top-level vdev becoming
1478 		 * unusable, undo it and fail the request.
1479 		 */
1480 		vd->vdev_offline = B_TRUE;
1481 		vdev_reopen(vd->vdev_top);
1482 		if (vdev_is_dead(vd->vdev_top)) {
1483 			vd->vdev_offline = B_FALSE;
1484 			vdev_reopen(vd->vdev_top);
1485 			return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1486 		}
1487 	}
1488 
1489 	vd->vdev_tmpoffline = istmp;
1490 
1491 	vdev_config_dirty(vd->vdev_top);
1492 
1493 	return (spa_vdev_exit(spa, NULL, txg, 0));
1494 }
1495 
1496 /*
1497  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
1498  * vdev_offline(), we assume the spa config is locked.  We also clear all
1499  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
1500  */
1501 void
1502 vdev_clear(spa_t *spa, vdev_t *vd)
1503 {
1504 	int c;
1505 
1506 	if (vd == NULL)
1507 		vd = spa->spa_root_vdev;
1508 
1509 	vd->vdev_stat.vs_read_errors = 0;
1510 	vd->vdev_stat.vs_write_errors = 0;
1511 	vd->vdev_stat.vs_checksum_errors = 0;
1512 
1513 	for (c = 0; c < vd->vdev_children; c++)
1514 		vdev_clear(spa, vd->vdev_child[c]);
1515 }
1516 
1517 int
1518 vdev_is_dead(vdev_t *vd)
1519 {
1520 	return (vd->vdev_state <= VDEV_STATE_CANT_OPEN);
1521 }
1522 
1523 int
1524 vdev_error_inject(vdev_t *vd, zio_t *zio)
1525 {
1526 	int error = 0;
1527 
1528 	if (vd->vdev_fault_mode == VDEV_FAULT_NONE)
1529 		return (0);
1530 
1531 	if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0)
1532 		return (0);
1533 
1534 	switch (vd->vdev_fault_mode) {
1535 	case VDEV_FAULT_RANDOM:
1536 		if (spa_get_random(vd->vdev_fault_arg) == 0)
1537 			error = EIO;
1538 		break;
1539 
1540 	case VDEV_FAULT_COUNT:
1541 		if ((int64_t)--vd->vdev_fault_arg <= 0)
1542 			vd->vdev_fault_mode = VDEV_FAULT_NONE;
1543 		error = EIO;
1544 		break;
1545 	}
1546 
1547 	if (error != 0) {
1548 		dprintf("returning %d for type %d on %s state %d offset %llx\n",
1549 		    error, zio->io_type, vdev_description(vd),
1550 		    vd->vdev_state, zio->io_offset);
1551 	}
1552 
1553 	return (error);
1554 }
1555 
1556 /*
1557  * Get statistics for the given vdev.
1558  */
1559 void
1560 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1561 {
1562 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1563 	int c, t;
1564 
1565 	mutex_enter(&vd->vdev_stat_lock);
1566 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1567 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1568 	vs->vs_state = vd->vdev_state;
1569 	vs->vs_rsize = vdev_get_rsize(vd);
1570 	mutex_exit(&vd->vdev_stat_lock);
1571 
1572 	/*
1573 	 * If we're getting stats on the root vdev, aggregate the I/O counts
1574 	 * over all top-level vdevs (i.e. the direct children of the root).
1575 	 */
1576 	if (vd == rvd) {
1577 		for (c = 0; c < rvd->vdev_children; c++) {
1578 			vdev_t *cvd = rvd->vdev_child[c];
1579 			vdev_stat_t *cvs = &cvd->vdev_stat;
1580 
1581 			mutex_enter(&vd->vdev_stat_lock);
1582 			for (t = 0; t < ZIO_TYPES; t++) {
1583 				vs->vs_ops[t] += cvs->vs_ops[t];
1584 				vs->vs_bytes[t] += cvs->vs_bytes[t];
1585 			}
1586 			vs->vs_read_errors += cvs->vs_read_errors;
1587 			vs->vs_write_errors += cvs->vs_write_errors;
1588 			vs->vs_checksum_errors += cvs->vs_checksum_errors;
1589 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
1590 			vs->vs_scrub_errors += cvs->vs_scrub_errors;
1591 			mutex_exit(&vd->vdev_stat_lock);
1592 		}
1593 	}
1594 }
1595 
1596 void
1597 vdev_stat_update(zio_t *zio)
1598 {
1599 	vdev_t *vd = zio->io_vd;
1600 	vdev_t *pvd;
1601 	uint64_t txg = zio->io_txg;
1602 	vdev_stat_t *vs = &vd->vdev_stat;
1603 	zio_type_t type = zio->io_type;
1604 	int flags = zio->io_flags;
1605 
1606 	if (zio->io_error == 0) {
1607 		if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1608 			mutex_enter(&vd->vdev_stat_lock);
1609 			vs->vs_ops[type]++;
1610 			vs->vs_bytes[type] += zio->io_size;
1611 			mutex_exit(&vd->vdev_stat_lock);
1612 		}
1613 		if ((flags & ZIO_FLAG_IO_REPAIR) &&
1614 		    zio->io_delegate_list == NULL) {
1615 			mutex_enter(&vd->vdev_stat_lock);
1616 			if (flags & ZIO_FLAG_SCRUB_THREAD)
1617 				vs->vs_scrub_repaired += zio->io_size;
1618 			else
1619 				vs->vs_self_healed += zio->io_size;
1620 			mutex_exit(&vd->vdev_stat_lock);
1621 		}
1622 		return;
1623 	}
1624 
1625 	if (flags & ZIO_FLAG_SPECULATIVE)
1626 		return;
1627 
1628 	if (!vdev_is_dead(vd)) {
1629 		mutex_enter(&vd->vdev_stat_lock);
1630 		if (type == ZIO_TYPE_READ) {
1631 			if (zio->io_error == ECKSUM)
1632 				vs->vs_checksum_errors++;
1633 			else
1634 				vs->vs_read_errors++;
1635 		}
1636 		if (type == ZIO_TYPE_WRITE)
1637 			vs->vs_write_errors++;
1638 		mutex_exit(&vd->vdev_stat_lock);
1639 	}
1640 
1641 	if (type == ZIO_TYPE_WRITE) {
1642 		if (txg == 0 || vd->vdev_children != 0)
1643 			return;
1644 		if (flags & ZIO_FLAG_SCRUB_THREAD) {
1645 			ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1646 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1647 				vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1648 		}
1649 		if (!(flags & ZIO_FLAG_IO_REPAIR)) {
1650 			if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
1651 				return;
1652 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1653 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1654 				vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
1655 		}
1656 	}
1657 }
1658 
1659 void
1660 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
1661 {
1662 	int c;
1663 	vdev_stat_t *vs = &vd->vdev_stat;
1664 
1665 	for (c = 0; c < vd->vdev_children; c++)
1666 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
1667 
1668 	mutex_enter(&vd->vdev_stat_lock);
1669 
1670 	if (type == POOL_SCRUB_NONE) {
1671 		/*
1672 		 * Update completion and end time.  Leave everything else alone
1673 		 * so we can report what happened during the previous scrub.
1674 		 */
1675 		vs->vs_scrub_complete = complete;
1676 		vs->vs_scrub_end = gethrestime_sec();
1677 	} else {
1678 		vs->vs_scrub_type = type;
1679 		vs->vs_scrub_complete = 0;
1680 		vs->vs_scrub_examined = 0;
1681 		vs->vs_scrub_repaired = 0;
1682 		vs->vs_scrub_errors = 0;
1683 		vs->vs_scrub_start = gethrestime_sec();
1684 		vs->vs_scrub_end = 0;
1685 	}
1686 
1687 	mutex_exit(&vd->vdev_stat_lock);
1688 }
1689 
1690 /*
1691  * Update the in-core space usage stats for this vdev and the root vdev.
1692  */
1693 void
1694 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta)
1695 {
1696 	ASSERT(vd == vd->vdev_top);
1697 	int64_t dspace_delta = space_delta;
1698 
1699 	do {
1700 		if (vd->vdev_ms_count) {
1701 			/*
1702 			 * If this is a top-level vdev, apply the
1703 			 * inverse of its psize-to-asize (ie. RAID-Z)
1704 			 * space-expansion factor.  We must calculate
1705 			 * this here and not at the root vdev because
1706 			 * the root vdev's psize-to-asize is simply the
1707 			 * max of its childrens', thus not accurate
1708 			 * enough for us.
1709 			 */
1710 			ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
1711 			dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
1712 			    vd->vdev_deflate_ratio;
1713 		}
1714 
1715 		mutex_enter(&vd->vdev_stat_lock);
1716 		vd->vdev_stat.vs_space += space_delta;
1717 		vd->vdev_stat.vs_alloc += alloc_delta;
1718 		vd->vdev_stat.vs_dspace += dspace_delta;
1719 		mutex_exit(&vd->vdev_stat_lock);
1720 	} while ((vd = vd->vdev_parent) != NULL);
1721 }
1722 
1723 /*
1724  * Mark a top-level vdev's config as dirty, placing it on the dirty list
1725  * so that it will be written out next time the vdev configuration is synced.
1726  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
1727  */
1728 void
1729 vdev_config_dirty(vdev_t *vd)
1730 {
1731 	spa_t *spa = vd->vdev_spa;
1732 	vdev_t *rvd = spa->spa_root_vdev;
1733 	int c;
1734 
1735 	/*
1736 	 * The dirty list is protected by the config lock.  The caller must
1737 	 * either hold the config lock as writer, or must be the sync thread
1738 	 * (which holds the lock as reader).  There's only one sync thread,
1739 	 * so this is sufficient to ensure mutual exclusion.
1740 	 */
1741 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1742 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1743 
1744 	if (vd == rvd) {
1745 		for (c = 0; c < rvd->vdev_children; c++)
1746 			vdev_config_dirty(rvd->vdev_child[c]);
1747 	} else {
1748 		ASSERT(vd == vd->vdev_top);
1749 
1750 		if (!list_link_active(&vd->vdev_dirty_node))
1751 			list_insert_head(&spa->spa_dirty_list, vd);
1752 	}
1753 }
1754 
1755 void
1756 vdev_config_clean(vdev_t *vd)
1757 {
1758 	spa_t *spa = vd->vdev_spa;
1759 
1760 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1761 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1762 
1763 	ASSERT(list_link_active(&vd->vdev_dirty_node));
1764 	list_remove(&spa->spa_dirty_list, vd);
1765 }
1766 
1767 void
1768 vdev_propagate_state(vdev_t *vd)
1769 {
1770 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1771 	int degraded = 0, faulted = 0;
1772 	int corrupted = 0;
1773 	int c;
1774 	vdev_t *child;
1775 
1776 	for (c = 0; c < vd->vdev_children; c++) {
1777 		child = vd->vdev_child[c];
1778 		if (child->vdev_state <= VDEV_STATE_CANT_OPEN)
1779 			faulted++;
1780 		else if (child->vdev_state == VDEV_STATE_DEGRADED)
1781 			degraded++;
1782 
1783 		if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
1784 			corrupted++;
1785 	}
1786 
1787 	vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
1788 
1789 	/*
1790 	 * Root special: if there is a toplevel vdev that cannot be
1791 	 * opened due to corrupted metadata, then propagate the root
1792 	 * vdev's aux state as 'corrupt' rather than 'insufficient
1793 	 * replicas'.
1794 	 */
1795 	if (corrupted && vd == rvd && rvd->vdev_state == VDEV_STATE_CANT_OPEN)
1796 		vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
1797 		    VDEV_AUX_CORRUPT_DATA);
1798 }
1799 
1800 /*
1801  * Set a vdev's state.  If this is during an open, we don't update the parent
1802  * state, because we're in the process of opening children depth-first.
1803  * Otherwise, we propagate the change to the parent.
1804  *
1805  * If this routine places a device in a faulted state, an appropriate ereport is
1806  * generated.
1807  */
1808 void
1809 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
1810 {
1811 	uint64_t save_state;
1812 
1813 	if (state == vd->vdev_state) {
1814 		vd->vdev_stat.vs_aux = aux;
1815 		return;
1816 	}
1817 
1818 	save_state = vd->vdev_state;
1819 
1820 	vd->vdev_state = state;
1821 	vd->vdev_stat.vs_aux = aux;
1822 
1823 	if (state == VDEV_STATE_CANT_OPEN) {
1824 		/*
1825 		 * If we fail to open a vdev during an import, we mark it as
1826 		 * "not available", which signifies that it was never there to
1827 		 * begin with.  Failure to open such a device is not considered
1828 		 * an error.
1829 		 */
1830 		if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT &&
1831 		    vd->vdev_ops->vdev_op_leaf)
1832 			vd->vdev_not_present = 1;
1833 
1834 		/*
1835 		 * Post the appropriate ereport.  If the 'prevstate' field is
1836 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
1837 		 * that this is part of a vdev_reopen().  In this case, we don't
1838 		 * want to post the ereport if the device was already in the
1839 		 * CANT_OPEN state beforehand.
1840 		 */
1841 		if (vd->vdev_prevstate != state && !vd->vdev_not_present &&
1842 		    vd != vd->vdev_spa->spa_root_vdev) {
1843 			const char *class;
1844 
1845 			switch (aux) {
1846 			case VDEV_AUX_OPEN_FAILED:
1847 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
1848 				break;
1849 			case VDEV_AUX_CORRUPT_DATA:
1850 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
1851 				break;
1852 			case VDEV_AUX_NO_REPLICAS:
1853 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
1854 				break;
1855 			case VDEV_AUX_BAD_GUID_SUM:
1856 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
1857 				break;
1858 			case VDEV_AUX_TOO_SMALL:
1859 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
1860 				break;
1861 			case VDEV_AUX_BAD_LABEL:
1862 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
1863 				break;
1864 			default:
1865 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
1866 			}
1867 
1868 			zfs_ereport_post(class, vd->vdev_spa,
1869 			    vd, NULL, save_state, 0);
1870 		}
1871 	}
1872 
1873 	if (isopen)
1874 		return;
1875 
1876 	if (vd->vdev_parent != NULL)
1877 		vdev_propagate_state(vd->vdev_parent);
1878 }
1879