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