xref: /titanic_44/usr/src/uts/common/fs/zfs/vdev.c (revision 734b6a94890be549309b21156f8ed6d4561cac51)
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 		    state != POOL_STATE_DESTROYED))) {
1245 			dprintf("pool state not active (%llu)\n", state);
1246 			nvlist_free(label);
1247 			return (EBADF);
1248 		}
1249 
1250 		nvlist_free(label);
1251 	}
1252 
1253 	/*
1254 	 * If this is a top-level vdev, initialize its metaslabs.
1255 	 */
1256 	if (vd == vd->vdev_top) {
1257 
1258 		if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
1259 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1260 			    VDEV_AUX_CORRUPT_DATA);
1261 			return (0);
1262 		}
1263 
1264 		if ((error = vdev_metaslab_init(vd, 0)) != 0) {
1265 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1266 			    VDEV_AUX_CORRUPT_DATA);
1267 			return (0);
1268 		}
1269 	}
1270 
1271 	/*
1272 	 * If this is a leaf vdev, load its DTL.
1273 	 */
1274 	if (vd->vdev_ops->vdev_op_leaf) {
1275 		error = vdev_dtl_load(vd);
1276 		if (error) {
1277 			dprintf("can't load DTL for %s, error %d\n",
1278 			    vdev_description(vd), error);
1279 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1280 			    VDEV_AUX_CORRUPT_DATA);
1281 			return (0);
1282 		}
1283 	}
1284 
1285 	return (0);
1286 }
1287 
1288 void
1289 vdev_sync_done(vdev_t *vd, uint64_t txg)
1290 {
1291 	metaslab_t *msp;
1292 
1293 	dprintf("%s txg %llu\n", vdev_description(vd), txg);
1294 
1295 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1296 		metaslab_sync_done(msp, txg);
1297 }
1298 
1299 void
1300 vdev_add_sync(vdev_t *vd, uint64_t txg)
1301 {
1302 	spa_t *spa = vd->vdev_spa;
1303 	dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1304 
1305 	ASSERT(vd == vd->vdev_top);
1306 
1307 	if (vd->vdev_ms_array == 0)
1308 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1309 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1310 
1311 	ASSERT(vd->vdev_ms_array != 0);
1312 
1313 	vdev_config_dirty(vd);
1314 
1315 	dmu_tx_commit(tx);
1316 }
1317 
1318 void
1319 vdev_sync(vdev_t *vd, uint64_t txg)
1320 {
1321 	spa_t *spa = vd->vdev_spa;
1322 	vdev_t *lvd;
1323 	metaslab_t *msp;
1324 	uint8_t *dirtyp = &vd->vdev_dirty[txg & TXG_MASK];
1325 	uint8_t dirty = *dirtyp;
1326 
1327 	mutex_enter(&vd->vdev_dirty_lock);
1328 	*dirtyp &= ~(VDD_ALLOC | VDD_FREE | VDD_ADD | VDD_DTL);
1329 	mutex_exit(&vd->vdev_dirty_lock);
1330 
1331 	dprintf("%s txg %llu pass %d\n",
1332 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1333 
1334 	if (dirty & VDD_ADD)
1335 		vdev_add_sync(vd, txg);
1336 
1337 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL)
1338 		metaslab_sync(msp, txg);
1339 
1340 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1341 		vdev_dtl_sync(lvd, txg);
1342 
1343 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1344 }
1345 
1346 uint64_t
1347 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1348 {
1349 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1350 }
1351 
1352 void
1353 vdev_io_start(zio_t *zio)
1354 {
1355 	zio->io_vd->vdev_ops->vdev_op_io_start(zio);
1356 }
1357 
1358 void
1359 vdev_io_done(zio_t *zio)
1360 {
1361 	zio->io_vd->vdev_ops->vdev_op_io_done(zio);
1362 }
1363 
1364 const char *
1365 vdev_description(vdev_t *vd)
1366 {
1367 	if (vd == NULL || vd->vdev_ops == NULL)
1368 		return ("<unknown>");
1369 
1370 	if (vd->vdev_path != NULL)
1371 		return (vd->vdev_path);
1372 
1373 	if (vd->vdev_parent == NULL)
1374 		return (spa_name(vd->vdev_spa));
1375 
1376 	return (vd->vdev_ops->vdev_op_type);
1377 }
1378 
1379 int
1380 vdev_online(spa_t *spa, uint64_t guid)
1381 {
1382 	vdev_t *rvd, *vd;
1383 	uint64_t txg;
1384 
1385 	txg = spa_vdev_enter(spa);
1386 
1387 	rvd = spa->spa_root_vdev;
1388 
1389 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1390 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1391 
1392 	if (!vd->vdev_ops->vdev_op_leaf)
1393 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1394 
1395 	dprintf("ONLINE: %s\n", vdev_description(vd));
1396 
1397 	vd->vdev_offline = B_FALSE;
1398 	vd->vdev_tmpoffline = B_FALSE;
1399 	vdev_reopen(vd->vdev_top);
1400 
1401 	vdev_config_dirty(vd->vdev_top);
1402 
1403 	(void) spa_vdev_exit(spa, NULL, txg, 0);
1404 
1405 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1406 
1407 	return (0);
1408 }
1409 
1410 int
1411 vdev_offline(spa_t *spa, uint64_t guid, int istmp)
1412 {
1413 	vdev_t *rvd, *vd;
1414 	uint64_t txg;
1415 
1416 	txg = spa_vdev_enter(spa);
1417 
1418 	rvd = spa->spa_root_vdev;
1419 
1420 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1421 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1422 
1423 	if (!vd->vdev_ops->vdev_op_leaf)
1424 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1425 
1426 	dprintf("OFFLINE: %s\n", vdev_description(vd));
1427 
1428 	/* vdev is already offlined, do nothing */
1429 	if (vd->vdev_offline)
1430 		return (spa_vdev_exit(spa, NULL, txg, 0));
1431 
1432 	/*
1433 	 * If this device's top-level vdev has a non-empty DTL,
1434 	 * don't allow the device to be offlined.
1435 	 *
1436 	 * XXX -- we should make this more precise by allowing the offline
1437 	 * as long as the remaining devices don't have any DTL holes.
1438 	 */
1439 	if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1440 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1441 
1442 	/*
1443 	 * Set this device to offline state and reopen its top-level vdev.
1444 	 * If this action results in the top-level vdev becoming unusable,
1445 	 * undo it and fail the request.
1446 	 */
1447 	vd->vdev_offline = B_TRUE;
1448 	vdev_reopen(vd->vdev_top);
1449 	if (vdev_is_dead(vd->vdev_top)) {
1450 		vd->vdev_offline = B_FALSE;
1451 		vdev_reopen(vd->vdev_top);
1452 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1453 	}
1454 
1455 	vd->vdev_tmpoffline = istmp;
1456 	if (!istmp)
1457 		vdev_config_dirty(vd->vdev_top);
1458 
1459 	return (spa_vdev_exit(spa, NULL, txg, 0));
1460 }
1461 
1462 /*
1463  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
1464  * vdev_offline(), we assume the spa config is locked.  We also clear all
1465  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
1466  */
1467 void
1468 vdev_clear(spa_t *spa, vdev_t *vd)
1469 {
1470 	int c;
1471 
1472 	if (vd == NULL)
1473 		vd = spa->spa_root_vdev;
1474 
1475 	vd->vdev_stat.vs_read_errors = 0;
1476 	vd->vdev_stat.vs_write_errors = 0;
1477 	vd->vdev_stat.vs_checksum_errors = 0;
1478 
1479 	for (c = 0; c < vd->vdev_children; c++)
1480 		vdev_clear(spa, vd->vdev_child[c]);
1481 }
1482 
1483 int
1484 vdev_is_dead(vdev_t *vd)
1485 {
1486 	return (vd->vdev_state <= VDEV_STATE_CANT_OPEN);
1487 }
1488 
1489 int
1490 vdev_error_inject(vdev_t *vd, zio_t *zio)
1491 {
1492 	int error = 0;
1493 
1494 	if (vd->vdev_fault_mode == VDEV_FAULT_NONE)
1495 		return (0);
1496 
1497 	if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0)
1498 		return (0);
1499 
1500 	switch (vd->vdev_fault_mode) {
1501 	case VDEV_FAULT_RANDOM:
1502 		if (spa_get_random(vd->vdev_fault_arg) == 0)
1503 			error = EIO;
1504 		break;
1505 
1506 	case VDEV_FAULT_COUNT:
1507 		if ((int64_t)--vd->vdev_fault_arg <= 0)
1508 			vd->vdev_fault_mode = VDEV_FAULT_NONE;
1509 		error = EIO;
1510 		break;
1511 	}
1512 
1513 	if (error != 0) {
1514 		dprintf("returning %d for type %d on %s state %d offset %llx\n",
1515 		    error, zio->io_type, vdev_description(vd),
1516 		    vd->vdev_state, zio->io_offset);
1517 	}
1518 
1519 	return (error);
1520 }
1521 
1522 /*
1523  * Get statistics for the given vdev.
1524  */
1525 void
1526 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1527 {
1528 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1529 	int c, t;
1530 
1531 	mutex_enter(&vd->vdev_stat_lock);
1532 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1533 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1534 	vs->vs_state = vd->vdev_state;
1535 	vs->vs_rsize = vdev_get_rsize(vd);
1536 	mutex_exit(&vd->vdev_stat_lock);
1537 
1538 	/*
1539 	 * If we're getting stats on the root vdev, aggregate the I/O counts
1540 	 * over all top-level vdevs (i.e. the direct children of the root).
1541 	 */
1542 	if (vd == rvd) {
1543 		for (c = 0; c < rvd->vdev_children; c++) {
1544 			vdev_t *cvd = rvd->vdev_child[c];
1545 			vdev_stat_t *cvs = &cvd->vdev_stat;
1546 
1547 			mutex_enter(&vd->vdev_stat_lock);
1548 			for (t = 0; t < ZIO_TYPES; t++) {
1549 				vs->vs_ops[t] += cvs->vs_ops[t];
1550 				vs->vs_bytes[t] += cvs->vs_bytes[t];
1551 			}
1552 			vs->vs_read_errors += cvs->vs_read_errors;
1553 			vs->vs_write_errors += cvs->vs_write_errors;
1554 			vs->vs_checksum_errors += cvs->vs_checksum_errors;
1555 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
1556 			vs->vs_scrub_errors += cvs->vs_scrub_errors;
1557 			mutex_exit(&vd->vdev_stat_lock);
1558 		}
1559 	}
1560 }
1561 
1562 void
1563 vdev_stat_update(zio_t *zio)
1564 {
1565 	vdev_t *vd = zio->io_vd;
1566 	vdev_t *pvd;
1567 	uint64_t txg = zio->io_txg;
1568 	vdev_stat_t *vs = &vd->vdev_stat;
1569 	zio_type_t type = zio->io_type;
1570 	int flags = zio->io_flags;
1571 
1572 	if (zio->io_error == 0) {
1573 		if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1574 			mutex_enter(&vd->vdev_stat_lock);
1575 			vs->vs_ops[type]++;
1576 			vs->vs_bytes[type] += zio->io_size;
1577 			mutex_exit(&vd->vdev_stat_lock);
1578 		}
1579 		if ((flags & ZIO_FLAG_IO_REPAIR) &&
1580 		    zio->io_delegate_list == NULL) {
1581 			mutex_enter(&vd->vdev_stat_lock);
1582 			if (flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER))
1583 				vs->vs_scrub_repaired += zio->io_size;
1584 			else
1585 				vs->vs_self_healed += zio->io_size;
1586 			mutex_exit(&vd->vdev_stat_lock);
1587 		}
1588 		return;
1589 	}
1590 
1591 	if (flags & ZIO_FLAG_SPECULATIVE)
1592 		return;
1593 
1594 	if (!vdev_is_dead(vd)) {
1595 		mutex_enter(&vd->vdev_stat_lock);
1596 		if (type == ZIO_TYPE_READ) {
1597 			if (zio->io_error == ECKSUM)
1598 				vs->vs_checksum_errors++;
1599 			else
1600 				vs->vs_read_errors++;
1601 		}
1602 		if (type == ZIO_TYPE_WRITE)
1603 			vs->vs_write_errors++;
1604 		mutex_exit(&vd->vdev_stat_lock);
1605 	}
1606 
1607 	if (type == ZIO_TYPE_WRITE) {
1608 		if (txg == 0 || vd->vdev_children != 0)
1609 			return;
1610 		if (flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER)) {
1611 			ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1612 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1613 				vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1614 		}
1615 		if (!(flags & ZIO_FLAG_IO_REPAIR)) {
1616 			vdev_t *tvd = vd->vdev_top;
1617 			if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
1618 				return;
1619 			vdev_dirty(tvd, VDD_DTL, txg);
1620 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, txg);
1621 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1622 				vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
1623 		}
1624 	}
1625 }
1626 
1627 void
1628 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
1629 {
1630 	int c;
1631 	vdev_stat_t *vs = &vd->vdev_stat;
1632 
1633 	for (c = 0; c < vd->vdev_children; c++)
1634 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
1635 
1636 	mutex_enter(&vd->vdev_stat_lock);
1637 
1638 	if (type == POOL_SCRUB_NONE) {
1639 		/*
1640 		 * Update completion and end time.  Leave everything else alone
1641 		 * so we can report what happened during the previous scrub.
1642 		 */
1643 		vs->vs_scrub_complete = complete;
1644 		vs->vs_scrub_end = gethrestime_sec();
1645 	} else {
1646 		vs->vs_scrub_type = type;
1647 		vs->vs_scrub_complete = 0;
1648 		vs->vs_scrub_examined = 0;
1649 		vs->vs_scrub_repaired = 0;
1650 		vs->vs_scrub_errors = 0;
1651 		vs->vs_scrub_start = gethrestime_sec();
1652 		vs->vs_scrub_end = 0;
1653 	}
1654 
1655 	mutex_exit(&vd->vdev_stat_lock);
1656 }
1657 
1658 /*
1659  * Update the in-core space usage stats for this vdev and the root vdev.
1660  */
1661 void
1662 vdev_space_update(vdev_t *vd, uint64_t space_delta, uint64_t alloc_delta)
1663 {
1664 	ASSERT(vd == vd->vdev_top);
1665 
1666 	do {
1667 		mutex_enter(&vd->vdev_stat_lock);
1668 		vd->vdev_stat.vs_space += space_delta;
1669 		vd->vdev_stat.vs_alloc += alloc_delta;
1670 		mutex_exit(&vd->vdev_stat_lock);
1671 	} while ((vd = vd->vdev_parent) != NULL);
1672 }
1673 
1674 /*
1675  * Various knobs to tune a vdev.
1676  */
1677 static vdev_knob_t vdev_knob[] = {
1678 	{
1679 		"cache_size",
1680 		"size of the read-ahead cache",
1681 		0,
1682 		1ULL << 30,
1683 		10ULL << 20,
1684 		offsetof(struct vdev, vdev_cache.vc_size)
1685 	},
1686 	{
1687 		"cache_bshift",
1688 		"log2 of cache blocksize",
1689 		SPA_MINBLOCKSHIFT,
1690 		SPA_MAXBLOCKSHIFT,
1691 		16,
1692 		offsetof(struct vdev, vdev_cache.vc_bshift)
1693 	},
1694 	{
1695 		"cache_max",
1696 		"largest block size to cache",
1697 		0,
1698 		SPA_MAXBLOCKSIZE,
1699 		1ULL << 14,
1700 		offsetof(struct vdev, vdev_cache.vc_max)
1701 	},
1702 	{
1703 		"min_pending",
1704 		"minimum pending I/Os to the disk",
1705 		1,
1706 		10000,
1707 		2,
1708 		offsetof(struct vdev, vdev_queue.vq_min_pending)
1709 	},
1710 	{
1711 		"max_pending",
1712 		"maximum pending I/Os to the disk",
1713 		1,
1714 		10000,
1715 		35,
1716 		offsetof(struct vdev, vdev_queue.vq_max_pending)
1717 	},
1718 	{
1719 		"scrub_limit",
1720 		"maximum scrub/resilver I/O queue",
1721 		0,
1722 		10000,
1723 		70,
1724 		offsetof(struct vdev, vdev_queue.vq_scrub_limit)
1725 	},
1726 	{
1727 		"agg_limit",
1728 		"maximum size of aggregated I/Os",
1729 		0,
1730 		SPA_MAXBLOCKSIZE,
1731 		SPA_MAXBLOCKSIZE,
1732 		offsetof(struct vdev, vdev_queue.vq_agg_limit)
1733 	},
1734 	{
1735 		"time_shift",
1736 		"deadline = pri + (lbolt >> time_shift)",
1737 		0,
1738 		63,
1739 		4,
1740 		offsetof(struct vdev, vdev_queue.vq_time_shift)
1741 	},
1742 	{
1743 		"ramp_rate",
1744 		"exponential I/O issue ramp-up rate",
1745 		1,
1746 		10000,
1747 		2,
1748 		offsetof(struct vdev, vdev_queue.vq_ramp_rate)
1749 	},
1750 };
1751 
1752 vdev_knob_t *
1753 vdev_knob_next(vdev_knob_t *vk)
1754 {
1755 	if (vk == NULL)
1756 		return (vdev_knob);
1757 
1758 	if (++vk == vdev_knob + sizeof (vdev_knob) / sizeof (vdev_knob_t))
1759 		return (NULL);
1760 
1761 	return (vk);
1762 }
1763 
1764 /*
1765  * Mark a top-level vdev's config as dirty, placing it on the dirty list
1766  * so that it will be written out next time the vdev configuration is synced.
1767  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
1768  */
1769 void
1770 vdev_config_dirty(vdev_t *vd)
1771 {
1772 	spa_t *spa = vd->vdev_spa;
1773 	vdev_t *rvd = spa->spa_root_vdev;
1774 	int c;
1775 
1776 	/*
1777 	 * The dirty list is protected by the config lock.  The caller must
1778 	 * either hold the config lock as writer, or must be the sync thread
1779 	 * (which holds the lock as reader).  There's only one sync thread,
1780 	 * so this is sufficient to ensure mutual exclusion.
1781 	 */
1782 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1783 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1784 
1785 	if (vd == rvd) {
1786 		for (c = 0; c < rvd->vdev_children; c++)
1787 			vdev_config_dirty(rvd->vdev_child[c]);
1788 	} else {
1789 		ASSERT(vd == vd->vdev_top);
1790 
1791 		if (!vd->vdev_is_dirty) {
1792 			list_insert_head(&spa->spa_dirty_list, vd);
1793 			vd->vdev_is_dirty = B_TRUE;
1794 		}
1795 	}
1796 }
1797 
1798 void
1799 vdev_config_clean(vdev_t *vd)
1800 {
1801 	spa_t *spa = vd->vdev_spa;
1802 
1803 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1804 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1805 
1806 	ASSERT(vd->vdev_is_dirty);
1807 
1808 	list_remove(&spa->spa_dirty_list, vd);
1809 	vd->vdev_is_dirty = B_FALSE;
1810 }
1811 
1812 /*
1813  * Set a vdev's state.  If this is during an open, we don't update the parent
1814  * state, because we're in the process of opening children depth-first.
1815  * Otherwise, we propagate the change to the parent.
1816  *
1817  * If this routine places a device in a faulted state, an appropriate ereport is
1818  * generated.
1819  */
1820 void
1821 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
1822 {
1823 	uint64_t prev_state;
1824 
1825 	if (state == vd->vdev_state) {
1826 		vd->vdev_stat.vs_aux = aux;
1827 		return;
1828 	}
1829 
1830 	prev_state = vd->vdev_state;
1831 
1832 	vd->vdev_state = state;
1833 	vd->vdev_stat.vs_aux = aux;
1834 
1835 	if (state == VDEV_STATE_CANT_OPEN) {
1836 		/*
1837 		 * If we fail to open a vdev during an import, we mark it as
1838 		 * "not available", which signifies that it was never there to
1839 		 * begin with.  Failure to open such a device is not considered
1840 		 * an error.
1841 		 */
1842 		if (!vd->vdev_not_present &&
1843 		    vd != vd->vdev_spa->spa_root_vdev) {
1844 			const char *class;
1845 
1846 			switch (aux) {
1847 			case VDEV_AUX_OPEN_FAILED:
1848 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
1849 				break;
1850 			case VDEV_AUX_CORRUPT_DATA:
1851 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
1852 				break;
1853 			case VDEV_AUX_NO_REPLICAS:
1854 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
1855 				break;
1856 			case VDEV_AUX_BAD_GUID_SUM:
1857 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
1858 				break;
1859 			case VDEV_AUX_TOO_SMALL:
1860 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
1861 				break;
1862 			case VDEV_AUX_BAD_LABEL:
1863 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
1864 				break;
1865 			default:
1866 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
1867 			}
1868 
1869 			zfs_ereport_post(class, vd->vdev_spa,
1870 			    vd, NULL, prev_state, 0);
1871 		}
1872 
1873 		if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT &&
1874 		    vd->vdev_ops->vdev_op_leaf)
1875 			vd->vdev_not_present = 1;
1876 	}
1877 
1878 	if (isopen)
1879 		return;
1880 
1881 	if (vd->vdev_parent != NULL) {
1882 		int c;
1883 		int degraded = 0, faulted = 0;
1884 		int corrupted = 0;
1885 		vdev_t *parent, *child;
1886 
1887 		parent = vd->vdev_parent;
1888 		for (c = 0; c < parent->vdev_children; c++) {
1889 			child = parent->vdev_child[c];
1890 			if (child->vdev_state <= VDEV_STATE_CANT_OPEN)
1891 				faulted++;
1892 			else if (child->vdev_state == VDEV_STATE_DEGRADED)
1893 				degraded++;
1894 
1895 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
1896 				corrupted++;
1897 		}
1898 
1899 		vd->vdev_parent->vdev_ops->vdev_op_state_change(
1900 		    vd->vdev_parent, faulted, degraded);
1901 
1902 		/*
1903 		 * Root special: if this is a toplevel vdev that cannot be
1904 		 * opened due to corrupted metadata, then propagate the root
1905 		 * vdev's aux state as 'corrupt' rather than 'insufficient
1906 		 * replicas'.
1907 		 */
1908 		if (corrupted && vd == vd->vdev_top)
1909 			vdev_set_state(vd->vdev_spa->spa_root_vdev,
1910 			    B_FALSE, VDEV_STATE_CANT_OPEN,
1911 			    VDEV_AUX_CORRUPT_DATA);
1912 	}
1913 }
1914