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