xref: /titanic_44/usr/src/uts/common/fs/zfs/vdev.c (revision a1e4879402b1a0a7c410577cff81f1ce31ace7af)
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 /* maximum scrub/resilver I/O queue */
61 int zfs_scrub_limit = 70;
62 
63 /*
64  * Given a vdev type, return the appropriate ops vector.
65  */
66 static vdev_ops_t *
67 vdev_getops(const char *type)
68 {
69 	vdev_ops_t *ops, **opspp;
70 
71 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
72 		if (strcmp(ops->vdev_op_type, type) == 0)
73 			break;
74 
75 	return (ops);
76 }
77 
78 /*
79  * Default asize function: return the MAX of psize with the asize of
80  * all children.  This is what's used by anything other than RAID-Z.
81  */
82 uint64_t
83 vdev_default_asize(vdev_t *vd, uint64_t psize)
84 {
85 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
86 	uint64_t csize;
87 	uint64_t c;
88 
89 	for (c = 0; c < vd->vdev_children; c++) {
90 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
91 		asize = MAX(asize, csize);
92 	}
93 
94 	return (asize);
95 }
96 
97 /*
98  * Get the replaceable or attachable device size.
99  * If the parent is a mirror or raidz, the replaceable size is the minimum
100  * psize of all its children. For the rest, just return our own psize.
101  *
102  * e.g.
103  *			psize	rsize
104  * root			-	-
105  *	mirror/raidz	-	-
106  *	    disk1	20g	20g
107  *	    disk2 	40g	20g
108  *	disk3 		80g	80g
109  */
110 uint64_t
111 vdev_get_rsize(vdev_t *vd)
112 {
113 	vdev_t *pvd, *cvd;
114 	uint64_t c, rsize;
115 
116 	pvd = vd->vdev_parent;
117 
118 	/*
119 	 * If our parent is NULL or the root, just return our own psize.
120 	 */
121 	if (pvd == NULL || pvd->vdev_parent == NULL)
122 		return (vd->vdev_psize);
123 
124 	rsize = 0;
125 
126 	for (c = 0; c < pvd->vdev_children; c++) {
127 		cvd = pvd->vdev_child[c];
128 		rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
129 	}
130 
131 	return (rsize);
132 }
133 
134 vdev_t *
135 vdev_lookup_top(spa_t *spa, uint64_t vdev)
136 {
137 	vdev_t *rvd = spa->spa_root_vdev;
138 
139 	if (vdev < rvd->vdev_children)
140 		return (rvd->vdev_child[vdev]);
141 
142 	return (NULL);
143 }
144 
145 vdev_t *
146 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
147 {
148 	int c;
149 	vdev_t *mvd;
150 
151 	if (vd->vdev_guid == guid)
152 		return (vd);
153 
154 	for (c = 0; c < vd->vdev_children; c++)
155 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
156 		    NULL)
157 			return (mvd);
158 
159 	return (NULL);
160 }
161 
162 void
163 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
164 {
165 	size_t oldsize, newsize;
166 	uint64_t id = cvd->vdev_id;
167 	vdev_t **newchild;
168 
169 	ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
170 	ASSERT(cvd->vdev_parent == NULL);
171 
172 	cvd->vdev_parent = pvd;
173 
174 	if (pvd == NULL)
175 		return;
176 
177 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
178 
179 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
180 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
181 	newsize = pvd->vdev_children * sizeof (vdev_t *);
182 
183 	newchild = kmem_zalloc(newsize, KM_SLEEP);
184 	if (pvd->vdev_child != NULL) {
185 		bcopy(pvd->vdev_child, newchild, oldsize);
186 		kmem_free(pvd->vdev_child, oldsize);
187 	}
188 
189 	pvd->vdev_child = newchild;
190 	pvd->vdev_child[id] = cvd;
191 
192 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
193 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
194 
195 	/*
196 	 * Walk up all ancestors to update guid sum.
197 	 */
198 	for (; pvd != NULL; pvd = pvd->vdev_parent)
199 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
200 
201 	if (cvd->vdev_ops->vdev_op_leaf)
202 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
203 }
204 
205 void
206 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
207 {
208 	int c;
209 	uint_t id = cvd->vdev_id;
210 
211 	ASSERT(cvd->vdev_parent == pvd);
212 
213 	if (pvd == NULL)
214 		return;
215 
216 	ASSERT(id < pvd->vdev_children);
217 	ASSERT(pvd->vdev_child[id] == cvd);
218 
219 	pvd->vdev_child[id] = NULL;
220 	cvd->vdev_parent = NULL;
221 
222 	for (c = 0; c < pvd->vdev_children; c++)
223 		if (pvd->vdev_child[c])
224 			break;
225 
226 	if (c == pvd->vdev_children) {
227 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
228 		pvd->vdev_child = NULL;
229 		pvd->vdev_children = 0;
230 	}
231 
232 	/*
233 	 * Walk up all ancestors to update guid sum.
234 	 */
235 	for (; pvd != NULL; pvd = pvd->vdev_parent)
236 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
237 
238 	if (cvd->vdev_ops->vdev_op_leaf)
239 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
240 }
241 
242 /*
243  * Remove any holes in the child array.
244  */
245 void
246 vdev_compact_children(vdev_t *pvd)
247 {
248 	vdev_t **newchild, *cvd;
249 	int oldc = pvd->vdev_children;
250 	int newc, c;
251 
252 	ASSERT(spa_config_held(pvd->vdev_spa, RW_WRITER));
253 
254 	for (c = newc = 0; c < oldc; c++)
255 		if (pvd->vdev_child[c])
256 			newc++;
257 
258 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
259 
260 	for (c = newc = 0; c < oldc; c++) {
261 		if ((cvd = pvd->vdev_child[c]) != NULL) {
262 			newchild[newc] = cvd;
263 			cvd->vdev_id = newc++;
264 		}
265 	}
266 
267 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
268 	pvd->vdev_child = newchild;
269 	pvd->vdev_children = newc;
270 }
271 
272 /*
273  * Allocate and minimally initialize a vdev_t.
274  */
275 static vdev_t *
276 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
277 {
278 	vdev_t *vd;
279 
280 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
281 
282 	if (spa->spa_root_vdev == NULL) {
283 		ASSERT(ops == &vdev_root_ops);
284 		spa->spa_root_vdev = vd;
285 	}
286 
287 	if (guid == 0) {
288 		if (spa->spa_root_vdev == vd) {
289 			/*
290 			 * The root vdev's guid will also be the pool guid,
291 			 * which must be unique among all pools.
292 			 */
293 			while (guid == 0 || spa_guid_exists(guid, 0))
294 				guid = spa_get_random(-1ULL);
295 		} else {
296 			/*
297 			 * Any other vdev's guid must be unique within the pool.
298 			 */
299 			while (guid == 0 ||
300 			    spa_guid_exists(spa_guid(spa), guid))
301 				guid = spa_get_random(-1ULL);
302 		}
303 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
304 	}
305 
306 	vd->vdev_spa = spa;
307 	vd->vdev_id = id;
308 	vd->vdev_guid = guid;
309 	vd->vdev_guid_sum = guid;
310 	vd->vdev_ops = ops;
311 	vd->vdev_state = VDEV_STATE_CLOSED;
312 
313 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
314 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
315 	space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
316 	space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
317 	txg_list_create(&vd->vdev_ms_list,
318 	    offsetof(struct metaslab, ms_txg_node));
319 	txg_list_create(&vd->vdev_dtl_list,
320 	    offsetof(struct vdev, vdev_dtl_node));
321 	vd->vdev_stat.vs_timestamp = gethrtime();
322 
323 	return (vd);
324 }
325 
326 /*
327  * Free a vdev_t that has been removed from service.
328  */
329 static void
330 vdev_free_common(vdev_t *vd)
331 {
332 	spa_t *spa = vd->vdev_spa;
333 
334 	if (vd->vdev_path)
335 		spa_strfree(vd->vdev_path);
336 	if (vd->vdev_devid)
337 		spa_strfree(vd->vdev_devid);
338 
339 	if (vd->vdev_isspare)
340 		spa_spare_remove(vd);
341 
342 	txg_list_destroy(&vd->vdev_ms_list);
343 	txg_list_destroy(&vd->vdev_dtl_list);
344 	mutex_enter(&vd->vdev_dtl_lock);
345 	space_map_unload(&vd->vdev_dtl_map);
346 	space_map_destroy(&vd->vdev_dtl_map);
347 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
348 	space_map_destroy(&vd->vdev_dtl_scrub);
349 	mutex_exit(&vd->vdev_dtl_lock);
350 	mutex_destroy(&vd->vdev_dtl_lock);
351 	mutex_destroy(&vd->vdev_stat_lock);
352 
353 	if (vd == spa->spa_root_vdev)
354 		spa->spa_root_vdev = NULL;
355 
356 	kmem_free(vd, sizeof (vdev_t));
357 }
358 
359 /*
360  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
361  * creating a new vdev or loading an existing one - the behavior is slightly
362  * different for each case.
363  */
364 int
365 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
366     int alloctype)
367 {
368 	vdev_ops_t *ops;
369 	char *type;
370 	uint64_t guid = 0;
371 	vdev_t *vd;
372 
373 	ASSERT(spa_config_held(spa, RW_WRITER));
374 
375 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
376 		return (EINVAL);
377 
378 	if ((ops = vdev_getops(type)) == NULL)
379 		return (EINVAL);
380 
381 	/*
382 	 * If this is a load, get the vdev guid from the nvlist.
383 	 * Otherwise, vdev_alloc_common() will generate one for us.
384 	 */
385 	if (alloctype == VDEV_ALLOC_LOAD) {
386 		uint64_t label_id;
387 
388 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
389 		    label_id != id)
390 			return (EINVAL);
391 
392 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
393 			return (EINVAL);
394 	} else if (alloctype == VDEV_ALLOC_SPARE) {
395 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
396 			return (EINVAL);
397 	}
398 
399 	/*
400 	 * The first allocated vdev must be of type 'root'.
401 	 */
402 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
403 		return (EINVAL);
404 
405 	vd = vdev_alloc_common(spa, id, guid, ops);
406 
407 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
408 		vd->vdev_path = spa_strdup(vd->vdev_path);
409 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
410 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
411 
412 	/*
413 	 * Set the nparity propery for RAID-Z vdevs.
414 	 */
415 	if (ops == &vdev_raidz_ops) {
416 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
417 		    &vd->vdev_nparity) == 0) {
418 			/*
419 			 * Currently, we can only support 2 parity devices.
420 			 */
421 			if (vd->vdev_nparity > 2)
422 				return (EINVAL);
423 			/*
424 			 * Older versions can only support 1 parity device.
425 			 */
426 			if (vd->vdev_nparity == 2 &&
427 			    spa_version(spa) < ZFS_VERSION_RAID6)
428 				return (ENOTSUP);
429 
430 		} else {
431 			/*
432 			 * We require the parity to be specified for SPAs that
433 			 * support multiple parity levels.
434 			 */
435 			if (spa_version(spa) >= ZFS_VERSION_RAID6)
436 				return (EINVAL);
437 
438 			/*
439 			 * Otherwise, we default to 1 parity device for RAID-Z.
440 			 */
441 			vd->vdev_nparity = 1;
442 		}
443 	} else {
444 		vd->vdev_nparity = 0;
445 	}
446 
447 	/*
448 	 * Set the whole_disk property.  If it's not specified, leave the value
449 	 * as -1.
450 	 */
451 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
452 	    &vd->vdev_wholedisk) != 0)
453 		vd->vdev_wholedisk = -1ULL;
454 
455 	/*
456 	 * Look for the 'not present' flag.  This will only be set if the device
457 	 * was not present at the time of import.
458 	 */
459 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
460 	    &vd->vdev_not_present);
461 
462 	/*
463 	 * Get the alignment requirement.
464 	 */
465 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
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 	 * Call vdev_validate() here to make sure we have the same device.
1023 	 * Otherwise, a device with an invalid label could be successfully
1024 	 * opened in response to vdev_reopen().
1025 	 *
1026 	 * The downside to this is that if the user is simply experimenting by
1027 	 * overwriting an entire disk, we'll fault the device rather than
1028 	 * demonstrate self-healing capabilities.  On the other hand, with
1029 	 * proper FMA integration, the series of errors we'd see from the device
1030 	 * would result in a faulted device anyway.  Given that this doesn't
1031 	 * model any real-world corruption, it's better to catch this here and
1032 	 * correctly identify that the device has either changed beneath us, or
1033 	 * is corrupted beyond recognition.
1034 	 */
1035 	(void) vdev_validate(vd);
1036 
1037 	/*
1038 	 * Reassess root vdev's health.
1039 	 */
1040 	vdev_propagate_state(spa->spa_root_vdev);
1041 }
1042 
1043 int
1044 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1045 {
1046 	int error;
1047 
1048 	/*
1049 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1050 	 * For a create, however, we want to fail the request if
1051 	 * there are any components we can't open.
1052 	 */
1053 	error = vdev_open(vd);
1054 
1055 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1056 		vdev_close(vd);
1057 		return (error ? error : ENXIO);
1058 	}
1059 
1060 	/*
1061 	 * Recursively initialize all labels.
1062 	 */
1063 	if ((error = vdev_label_init(vd, txg, isreplacing ?
1064 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1065 		vdev_close(vd);
1066 		return (error);
1067 	}
1068 
1069 	return (0);
1070 }
1071 
1072 /*
1073  * The is the latter half of vdev_create().  It is distinct because it
1074  * involves initiating transactions in order to do metaslab creation.
1075  * For creation, we want to try to create all vdevs at once and then undo it
1076  * if anything fails; this is much harder if we have pending transactions.
1077  */
1078 void
1079 vdev_init(vdev_t *vd, uint64_t txg)
1080 {
1081 	/*
1082 	 * Aim for roughly 200 metaslabs per vdev.
1083 	 */
1084 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1085 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1086 
1087 	/*
1088 	 * Initialize the vdev's metaslabs.  This can't fail because
1089 	 * there's nothing to read when creating all new metaslabs.
1090 	 */
1091 	VERIFY(vdev_metaslab_init(vd, txg) == 0);
1092 }
1093 
1094 void
1095 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1096 {
1097 	ASSERT(vd == vd->vdev_top);
1098 	ASSERT(ISP2(flags));
1099 
1100 	if (flags & VDD_METASLAB)
1101 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1102 
1103 	if (flags & VDD_DTL)
1104 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1105 
1106 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1107 }
1108 
1109 void
1110 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1111 {
1112 	mutex_enter(sm->sm_lock);
1113 	if (!space_map_contains(sm, txg, size))
1114 		space_map_add(sm, txg, size);
1115 	mutex_exit(sm->sm_lock);
1116 }
1117 
1118 int
1119 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1120 {
1121 	int dirty;
1122 
1123 	/*
1124 	 * Quick test without the lock -- covers the common case that
1125 	 * there are no dirty time segments.
1126 	 */
1127 	if (sm->sm_space == 0)
1128 		return (0);
1129 
1130 	mutex_enter(sm->sm_lock);
1131 	dirty = space_map_contains(sm, txg, size);
1132 	mutex_exit(sm->sm_lock);
1133 
1134 	return (dirty);
1135 }
1136 
1137 /*
1138  * Reassess DTLs after a config change or scrub completion.
1139  */
1140 void
1141 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1142 {
1143 	spa_t *spa = vd->vdev_spa;
1144 	int c;
1145 
1146 	ASSERT(spa_config_held(spa, RW_WRITER));
1147 
1148 	if (vd->vdev_children == 0) {
1149 		mutex_enter(&vd->vdev_dtl_lock);
1150 		/*
1151 		 * We're successfully scrubbed everything up to scrub_txg.
1152 		 * Therefore, excise all old DTLs up to that point, then
1153 		 * fold in the DTLs for everything we couldn't scrub.
1154 		 */
1155 		if (scrub_txg != 0) {
1156 			space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1157 			space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1158 		}
1159 		if (scrub_done)
1160 			space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1161 		mutex_exit(&vd->vdev_dtl_lock);
1162 		if (txg != 0)
1163 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1164 		return;
1165 	}
1166 
1167 	/*
1168 	 * Make sure the DTLs are always correct under the scrub lock.
1169 	 */
1170 	if (vd == spa->spa_root_vdev)
1171 		mutex_enter(&spa->spa_scrub_lock);
1172 
1173 	mutex_enter(&vd->vdev_dtl_lock);
1174 	space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1175 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1176 	mutex_exit(&vd->vdev_dtl_lock);
1177 
1178 	for (c = 0; c < vd->vdev_children; c++) {
1179 		vdev_t *cvd = vd->vdev_child[c];
1180 		vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1181 		mutex_enter(&vd->vdev_dtl_lock);
1182 		space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1183 		space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1184 		mutex_exit(&vd->vdev_dtl_lock);
1185 	}
1186 
1187 	if (vd == spa->spa_root_vdev)
1188 		mutex_exit(&spa->spa_scrub_lock);
1189 }
1190 
1191 static int
1192 vdev_dtl_load(vdev_t *vd)
1193 {
1194 	spa_t *spa = vd->vdev_spa;
1195 	space_map_obj_t *smo = &vd->vdev_dtl;
1196 	objset_t *mos = spa->spa_meta_objset;
1197 	dmu_buf_t *db;
1198 	int error;
1199 
1200 	ASSERT(vd->vdev_children == 0);
1201 
1202 	if (smo->smo_object == 0)
1203 		return (0);
1204 
1205 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1206 		return (error);
1207 
1208 	ASSERT3U(db->db_size, ==, sizeof (*smo));
1209 	bcopy(db->db_data, smo, db->db_size);
1210 	dmu_buf_rele(db, FTAG);
1211 
1212 	mutex_enter(&vd->vdev_dtl_lock);
1213 	error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1214 	mutex_exit(&vd->vdev_dtl_lock);
1215 
1216 	return (error);
1217 }
1218 
1219 void
1220 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1221 {
1222 	spa_t *spa = vd->vdev_spa;
1223 	space_map_obj_t *smo = &vd->vdev_dtl;
1224 	space_map_t *sm = &vd->vdev_dtl_map;
1225 	objset_t *mos = spa->spa_meta_objset;
1226 	space_map_t smsync;
1227 	kmutex_t smlock;
1228 	dmu_buf_t *db;
1229 	dmu_tx_t *tx;
1230 
1231 	dprintf("%s in txg %llu pass %d\n",
1232 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1233 
1234 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1235 
1236 	if (vd->vdev_detached) {
1237 		if (smo->smo_object != 0) {
1238 			int err = dmu_object_free(mos, smo->smo_object, tx);
1239 			ASSERT3U(err, ==, 0);
1240 			smo->smo_object = 0;
1241 		}
1242 		dmu_tx_commit(tx);
1243 		dprintf("detach %s committed in txg %llu\n",
1244 		    vdev_description(vd), txg);
1245 		return;
1246 	}
1247 
1248 	if (smo->smo_object == 0) {
1249 		ASSERT(smo->smo_objsize == 0);
1250 		ASSERT(smo->smo_alloc == 0);
1251 		smo->smo_object = dmu_object_alloc(mos,
1252 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1253 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1254 		ASSERT(smo->smo_object != 0);
1255 		vdev_config_dirty(vd->vdev_top);
1256 	}
1257 
1258 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1259 
1260 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1261 	    &smlock);
1262 
1263 	mutex_enter(&smlock);
1264 
1265 	mutex_enter(&vd->vdev_dtl_lock);
1266 	space_map_walk(sm, space_map_add, &smsync);
1267 	mutex_exit(&vd->vdev_dtl_lock);
1268 
1269 	space_map_truncate(smo, mos, tx);
1270 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1271 
1272 	space_map_destroy(&smsync);
1273 
1274 	mutex_exit(&smlock);
1275 	mutex_destroy(&smlock);
1276 
1277 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1278 	dmu_buf_will_dirty(db, tx);
1279 	ASSERT3U(db->db_size, ==, sizeof (*smo));
1280 	bcopy(smo, db->db_data, db->db_size);
1281 	dmu_buf_rele(db, FTAG);
1282 
1283 	dmu_tx_commit(tx);
1284 }
1285 
1286 void
1287 vdev_load(vdev_t *vd)
1288 {
1289 	int c;
1290 
1291 	/*
1292 	 * Recursively load all children.
1293 	 */
1294 	for (c = 0; c < vd->vdev_children; c++)
1295 		vdev_load(vd->vdev_child[c]);
1296 
1297 	/*
1298 	 * If this is a top-level vdev, initialize its metaslabs.
1299 	 */
1300 	if (vd == vd->vdev_top &&
1301 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1302 	    vdev_metaslab_init(vd, 0) != 0))
1303 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1304 		    VDEV_AUX_CORRUPT_DATA);
1305 
1306 	/*
1307 	 * If this is a leaf vdev, load its DTL.
1308 	 */
1309 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1310 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1311 		    VDEV_AUX_CORRUPT_DATA);
1312 }
1313 
1314 /*
1315  * This special case of vdev_spare() is used for hot spares.  It's sole purpose
1316  * it to set the vdev state for the associated vdev.  To do this, we make sure
1317  * that we can open the underlying device, then try to read the label, and make
1318  * sure that the label is sane and that it hasn't been repurposed to another
1319  * pool.
1320  */
1321 int
1322 vdev_validate_spare(vdev_t *vd)
1323 {
1324 	nvlist_t *label;
1325 	uint64_t guid, version;
1326 	uint64_t state;
1327 
1328 	if ((label = vdev_label_read_config(vd)) == NULL) {
1329 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1330 		    VDEV_AUX_CORRUPT_DATA);
1331 		return (-1);
1332 	}
1333 
1334 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1335 	    version > ZFS_VERSION ||
1336 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1337 	    guid != vd->vdev_guid ||
1338 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1339 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1340 		    VDEV_AUX_CORRUPT_DATA);
1341 		nvlist_free(label);
1342 		return (-1);
1343 	}
1344 
1345 	spa_spare_add(vd);
1346 
1347 	/*
1348 	 * We don't actually check the pool state here.  If it's in fact in
1349 	 * use by another pool, we update this fact on the fly when requested.
1350 	 */
1351 	nvlist_free(label);
1352 	return (0);
1353 }
1354 
1355 void
1356 vdev_sync_done(vdev_t *vd, uint64_t txg)
1357 {
1358 	metaslab_t *msp;
1359 
1360 	dprintf("%s txg %llu\n", vdev_description(vd), txg);
1361 
1362 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1363 		metaslab_sync_done(msp, txg);
1364 }
1365 
1366 void
1367 vdev_sync(vdev_t *vd, uint64_t txg)
1368 {
1369 	spa_t *spa = vd->vdev_spa;
1370 	vdev_t *lvd;
1371 	metaslab_t *msp;
1372 	dmu_tx_t *tx;
1373 
1374 	dprintf("%s txg %llu pass %d\n",
1375 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1376 
1377 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1378 		ASSERT(vd == vd->vdev_top);
1379 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1380 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1381 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1382 		ASSERT(vd->vdev_ms_array != 0);
1383 		vdev_config_dirty(vd);
1384 		dmu_tx_commit(tx);
1385 	}
1386 
1387 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1388 		metaslab_sync(msp, txg);
1389 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1390 	}
1391 
1392 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1393 		vdev_dtl_sync(lvd, txg);
1394 
1395 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1396 }
1397 
1398 uint64_t
1399 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1400 {
1401 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1402 }
1403 
1404 void
1405 vdev_io_start(zio_t *zio)
1406 {
1407 	zio->io_vd->vdev_ops->vdev_op_io_start(zio);
1408 }
1409 
1410 void
1411 vdev_io_done(zio_t *zio)
1412 {
1413 	zio->io_vd->vdev_ops->vdev_op_io_done(zio);
1414 }
1415 
1416 const char *
1417 vdev_description(vdev_t *vd)
1418 {
1419 	if (vd == NULL || vd->vdev_ops == NULL)
1420 		return ("<unknown>");
1421 
1422 	if (vd->vdev_path != NULL)
1423 		return (vd->vdev_path);
1424 
1425 	if (vd->vdev_parent == NULL)
1426 		return (spa_name(vd->vdev_spa));
1427 
1428 	return (vd->vdev_ops->vdev_op_type);
1429 }
1430 
1431 int
1432 vdev_online(spa_t *spa, uint64_t guid)
1433 {
1434 	vdev_t *rvd, *vd;
1435 	uint64_t txg;
1436 
1437 	txg = spa_vdev_enter(spa);
1438 
1439 	rvd = spa->spa_root_vdev;
1440 
1441 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1442 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1443 
1444 	if (!vd->vdev_ops->vdev_op_leaf)
1445 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1446 
1447 	dprintf("ONLINE: %s\n", vdev_description(vd));
1448 
1449 	vd->vdev_offline = B_FALSE;
1450 	vd->vdev_tmpoffline = B_FALSE;
1451 	vdev_reopen(vd->vdev_top);
1452 
1453 	vdev_config_dirty(vd->vdev_top);
1454 
1455 	(void) spa_vdev_exit(spa, NULL, txg, 0);
1456 
1457 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1458 
1459 	return (0);
1460 }
1461 
1462 int
1463 vdev_offline(spa_t *spa, uint64_t guid, int istmp)
1464 {
1465 	vdev_t *rvd, *vd;
1466 	uint64_t txg;
1467 
1468 	txg = spa_vdev_enter(spa);
1469 
1470 	rvd = spa->spa_root_vdev;
1471 
1472 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1473 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1474 
1475 	if (!vd->vdev_ops->vdev_op_leaf)
1476 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1477 
1478 	dprintf("OFFLINE: %s\n", vdev_description(vd));
1479 
1480 	/*
1481 	 * If the device isn't already offline, try to offline it.
1482 	 */
1483 	if (!vd->vdev_offline) {
1484 		/*
1485 		 * If this device's top-level vdev has a non-empty DTL,
1486 		 * don't allow the device to be offlined.
1487 		 *
1488 		 * XXX -- make this more precise by allowing the offline
1489 		 * as long as the remaining devices don't have any DTL holes.
1490 		 */
1491 		if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1492 			return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1493 
1494 		/*
1495 		 * Offline this device and reopen its top-level vdev.
1496 		 * If this action results in the top-level vdev becoming
1497 		 * unusable, undo it and fail the request.
1498 		 */
1499 		vd->vdev_offline = B_TRUE;
1500 		vdev_reopen(vd->vdev_top);
1501 		if (vdev_is_dead(vd->vdev_top)) {
1502 			vd->vdev_offline = B_FALSE;
1503 			vdev_reopen(vd->vdev_top);
1504 			return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1505 		}
1506 	}
1507 
1508 	vd->vdev_tmpoffline = istmp;
1509 
1510 	vdev_config_dirty(vd->vdev_top);
1511 
1512 	return (spa_vdev_exit(spa, NULL, txg, 0));
1513 }
1514 
1515 /*
1516  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
1517  * vdev_offline(), we assume the spa config is locked.  We also clear all
1518  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
1519  */
1520 void
1521 vdev_clear(spa_t *spa, vdev_t *vd)
1522 {
1523 	int c;
1524 
1525 	if (vd == NULL)
1526 		vd = spa->spa_root_vdev;
1527 
1528 	vd->vdev_stat.vs_read_errors = 0;
1529 	vd->vdev_stat.vs_write_errors = 0;
1530 	vd->vdev_stat.vs_checksum_errors = 0;
1531 
1532 	for (c = 0; c < vd->vdev_children; c++)
1533 		vdev_clear(spa, vd->vdev_child[c]);
1534 }
1535 
1536 int
1537 vdev_is_dead(vdev_t *vd)
1538 {
1539 	return (vd->vdev_state <= VDEV_STATE_CANT_OPEN);
1540 }
1541 
1542 int
1543 vdev_error_inject(vdev_t *vd, zio_t *zio)
1544 {
1545 	int error = 0;
1546 
1547 	if (vd->vdev_fault_mode == VDEV_FAULT_NONE)
1548 		return (0);
1549 
1550 	if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0)
1551 		return (0);
1552 
1553 	switch (vd->vdev_fault_mode) {
1554 	case VDEV_FAULT_RANDOM:
1555 		if (spa_get_random(vd->vdev_fault_arg) == 0)
1556 			error = EIO;
1557 		break;
1558 
1559 	case VDEV_FAULT_COUNT:
1560 		if ((int64_t)--vd->vdev_fault_arg <= 0)
1561 			vd->vdev_fault_mode = VDEV_FAULT_NONE;
1562 		error = EIO;
1563 		break;
1564 	}
1565 
1566 	if (error != 0) {
1567 		dprintf("returning %d for type %d on %s state %d offset %llx\n",
1568 		    error, zio->io_type, vdev_description(vd),
1569 		    vd->vdev_state, zio->io_offset);
1570 	}
1571 
1572 	return (error);
1573 }
1574 
1575 /*
1576  * Get statistics for the given vdev.
1577  */
1578 void
1579 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1580 {
1581 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1582 	int c, t;
1583 
1584 	mutex_enter(&vd->vdev_stat_lock);
1585 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1586 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1587 	vs->vs_state = vd->vdev_state;
1588 	vs->vs_rsize = vdev_get_rsize(vd);
1589 	mutex_exit(&vd->vdev_stat_lock);
1590 
1591 	/*
1592 	 * If we're getting stats on the root vdev, aggregate the I/O counts
1593 	 * over all top-level vdevs (i.e. the direct children of the root).
1594 	 */
1595 	if (vd == rvd) {
1596 		for (c = 0; c < rvd->vdev_children; c++) {
1597 			vdev_t *cvd = rvd->vdev_child[c];
1598 			vdev_stat_t *cvs = &cvd->vdev_stat;
1599 
1600 			mutex_enter(&vd->vdev_stat_lock);
1601 			for (t = 0; t < ZIO_TYPES; t++) {
1602 				vs->vs_ops[t] += cvs->vs_ops[t];
1603 				vs->vs_bytes[t] += cvs->vs_bytes[t];
1604 			}
1605 			vs->vs_read_errors += cvs->vs_read_errors;
1606 			vs->vs_write_errors += cvs->vs_write_errors;
1607 			vs->vs_checksum_errors += cvs->vs_checksum_errors;
1608 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
1609 			vs->vs_scrub_errors += cvs->vs_scrub_errors;
1610 			mutex_exit(&vd->vdev_stat_lock);
1611 		}
1612 	}
1613 }
1614 
1615 void
1616 vdev_stat_update(zio_t *zio)
1617 {
1618 	vdev_t *vd = zio->io_vd;
1619 	vdev_t *pvd;
1620 	uint64_t txg = zio->io_txg;
1621 	vdev_stat_t *vs = &vd->vdev_stat;
1622 	zio_type_t type = zio->io_type;
1623 	int flags = zio->io_flags;
1624 
1625 	if (zio->io_error == 0) {
1626 		if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1627 			mutex_enter(&vd->vdev_stat_lock);
1628 			vs->vs_ops[type]++;
1629 			vs->vs_bytes[type] += zio->io_size;
1630 			mutex_exit(&vd->vdev_stat_lock);
1631 		}
1632 		if ((flags & ZIO_FLAG_IO_REPAIR) &&
1633 		    zio->io_delegate_list == NULL) {
1634 			mutex_enter(&vd->vdev_stat_lock);
1635 			if (flags & ZIO_FLAG_SCRUB_THREAD)
1636 				vs->vs_scrub_repaired += zio->io_size;
1637 			else
1638 				vs->vs_self_healed += zio->io_size;
1639 			mutex_exit(&vd->vdev_stat_lock);
1640 		}
1641 		return;
1642 	}
1643 
1644 	if (flags & ZIO_FLAG_SPECULATIVE)
1645 		return;
1646 
1647 	if (!vdev_is_dead(vd)) {
1648 		mutex_enter(&vd->vdev_stat_lock);
1649 		if (type == ZIO_TYPE_READ) {
1650 			if (zio->io_error == ECKSUM)
1651 				vs->vs_checksum_errors++;
1652 			else
1653 				vs->vs_read_errors++;
1654 		}
1655 		if (type == ZIO_TYPE_WRITE)
1656 			vs->vs_write_errors++;
1657 		mutex_exit(&vd->vdev_stat_lock);
1658 	}
1659 
1660 	if (type == ZIO_TYPE_WRITE) {
1661 		if (txg == 0 || vd->vdev_children != 0)
1662 			return;
1663 		if (flags & ZIO_FLAG_SCRUB_THREAD) {
1664 			ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1665 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1666 				vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1667 		}
1668 		if (!(flags & ZIO_FLAG_IO_REPAIR)) {
1669 			if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
1670 				return;
1671 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1672 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1673 				vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
1674 		}
1675 	}
1676 }
1677 
1678 void
1679 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
1680 {
1681 	int c;
1682 	vdev_stat_t *vs = &vd->vdev_stat;
1683 
1684 	for (c = 0; c < vd->vdev_children; c++)
1685 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
1686 
1687 	mutex_enter(&vd->vdev_stat_lock);
1688 
1689 	if (type == POOL_SCRUB_NONE) {
1690 		/*
1691 		 * Update completion and end time.  Leave everything else alone
1692 		 * so we can report what happened during the previous scrub.
1693 		 */
1694 		vs->vs_scrub_complete = complete;
1695 		vs->vs_scrub_end = gethrestime_sec();
1696 	} else {
1697 		vs->vs_scrub_type = type;
1698 		vs->vs_scrub_complete = 0;
1699 		vs->vs_scrub_examined = 0;
1700 		vs->vs_scrub_repaired = 0;
1701 		vs->vs_scrub_errors = 0;
1702 		vs->vs_scrub_start = gethrestime_sec();
1703 		vs->vs_scrub_end = 0;
1704 	}
1705 
1706 	mutex_exit(&vd->vdev_stat_lock);
1707 }
1708 
1709 /*
1710  * Update the in-core space usage stats for this vdev and the root vdev.
1711  */
1712 void
1713 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta)
1714 {
1715 	ASSERT(vd == vd->vdev_top);
1716 	int64_t dspace_delta = space_delta;
1717 
1718 	do {
1719 		if (vd->vdev_ms_count) {
1720 			/*
1721 			 * If this is a top-level vdev, apply the
1722 			 * inverse of its psize-to-asize (ie. RAID-Z)
1723 			 * space-expansion factor.  We must calculate
1724 			 * this here and not at the root vdev because
1725 			 * the root vdev's psize-to-asize is simply the
1726 			 * max of its childrens', thus not accurate
1727 			 * enough for us.
1728 			 */
1729 			ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
1730 			dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
1731 			    vd->vdev_deflate_ratio;
1732 		}
1733 
1734 		mutex_enter(&vd->vdev_stat_lock);
1735 		vd->vdev_stat.vs_space += space_delta;
1736 		vd->vdev_stat.vs_alloc += alloc_delta;
1737 		vd->vdev_stat.vs_dspace += dspace_delta;
1738 		mutex_exit(&vd->vdev_stat_lock);
1739 	} while ((vd = vd->vdev_parent) != NULL);
1740 }
1741 
1742 /*
1743  * Mark a top-level vdev's config as dirty, placing it on the dirty list
1744  * so that it will be written out next time the vdev configuration is synced.
1745  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
1746  */
1747 void
1748 vdev_config_dirty(vdev_t *vd)
1749 {
1750 	spa_t *spa = vd->vdev_spa;
1751 	vdev_t *rvd = spa->spa_root_vdev;
1752 	int c;
1753 
1754 	/*
1755 	 * The dirty list is protected by the config lock.  The caller must
1756 	 * either hold the config lock as writer, or must be the sync thread
1757 	 * (which holds the lock as reader).  There's only one sync thread,
1758 	 * so this is sufficient to ensure mutual exclusion.
1759 	 */
1760 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1761 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1762 
1763 	if (vd == rvd) {
1764 		for (c = 0; c < rvd->vdev_children; c++)
1765 			vdev_config_dirty(rvd->vdev_child[c]);
1766 	} else {
1767 		ASSERT(vd == vd->vdev_top);
1768 
1769 		if (!list_link_active(&vd->vdev_dirty_node))
1770 			list_insert_head(&spa->spa_dirty_list, vd);
1771 	}
1772 }
1773 
1774 void
1775 vdev_config_clean(vdev_t *vd)
1776 {
1777 	spa_t *spa = vd->vdev_spa;
1778 
1779 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1780 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1781 
1782 	ASSERT(list_link_active(&vd->vdev_dirty_node));
1783 	list_remove(&spa->spa_dirty_list, vd);
1784 }
1785 
1786 void
1787 vdev_propagate_state(vdev_t *vd)
1788 {
1789 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1790 	int degraded = 0, faulted = 0;
1791 	int corrupted = 0;
1792 	int c;
1793 	vdev_t *child;
1794 
1795 	for (c = 0; c < vd->vdev_children; c++) {
1796 		child = vd->vdev_child[c];
1797 		if (child->vdev_state <= VDEV_STATE_CANT_OPEN)
1798 			faulted++;
1799 		else if (child->vdev_state == VDEV_STATE_DEGRADED)
1800 			degraded++;
1801 
1802 		if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
1803 			corrupted++;
1804 	}
1805 
1806 	vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
1807 
1808 	/*
1809 	 * Root special: if there is a toplevel vdev that cannot be
1810 	 * opened due to corrupted metadata, then propagate the root
1811 	 * vdev's aux state as 'corrupt' rather than 'insufficient
1812 	 * replicas'.
1813 	 */
1814 	if (corrupted && vd == rvd && rvd->vdev_state == VDEV_STATE_CANT_OPEN)
1815 		vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
1816 		    VDEV_AUX_CORRUPT_DATA);
1817 }
1818 
1819 /*
1820  * Set a vdev's state.  If this is during an open, we don't update the parent
1821  * state, because we're in the process of opening children depth-first.
1822  * Otherwise, we propagate the change to the parent.
1823  *
1824  * If this routine places a device in a faulted state, an appropriate ereport is
1825  * generated.
1826  */
1827 void
1828 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
1829 {
1830 	uint64_t save_state;
1831 
1832 	if (state == vd->vdev_state) {
1833 		vd->vdev_stat.vs_aux = aux;
1834 		return;
1835 	}
1836 
1837 	save_state = vd->vdev_state;
1838 
1839 	vd->vdev_state = state;
1840 	vd->vdev_stat.vs_aux = aux;
1841 
1842 	if (state == VDEV_STATE_CANT_OPEN) {
1843 		/*
1844 		 * If we fail to open a vdev during an import, we mark it as
1845 		 * "not available", which signifies that it was never there to
1846 		 * begin with.  Failure to open such a device is not considered
1847 		 * an error.
1848 		 */
1849 		if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT &&
1850 		    vd->vdev_ops->vdev_op_leaf)
1851 			vd->vdev_not_present = 1;
1852 
1853 		/*
1854 		 * Post the appropriate ereport.  If the 'prevstate' field is
1855 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
1856 		 * that this is part of a vdev_reopen().  In this case, we don't
1857 		 * want to post the ereport if the device was already in the
1858 		 * CANT_OPEN state beforehand.
1859 		 */
1860 		if (vd->vdev_prevstate != state && !vd->vdev_not_present &&
1861 		    vd != vd->vdev_spa->spa_root_vdev) {
1862 			const char *class;
1863 
1864 			switch (aux) {
1865 			case VDEV_AUX_OPEN_FAILED:
1866 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
1867 				break;
1868 			case VDEV_AUX_CORRUPT_DATA:
1869 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
1870 				break;
1871 			case VDEV_AUX_NO_REPLICAS:
1872 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
1873 				break;
1874 			case VDEV_AUX_BAD_GUID_SUM:
1875 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
1876 				break;
1877 			case VDEV_AUX_TOO_SMALL:
1878 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
1879 				break;
1880 			case VDEV_AUX_BAD_LABEL:
1881 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
1882 				break;
1883 			default:
1884 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
1885 			}
1886 
1887 			zfs_ereport_post(class, vd->vdev_spa,
1888 			    vd, NULL, save_state, 0);
1889 		}
1890 	}
1891 
1892 	if (isopen)
1893 		return;
1894 
1895 	if (vd->vdev_parent != NULL)
1896 		vdev_propagate_state(vd->vdev_parent);
1897 }
1898