xref: /titanic_51/usr/src/uts/common/fs/zfs/vdev.c (revision f06271be56df67ca3faa4ca4bc51457dad15c3b5)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/zfs_context.h>
28 #include <sys/fm/fs/zfs.h>
29 #include <sys/spa.h>
30 #include <sys/spa_impl.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/uberblock_impl.h>
35 #include <sys/metaslab.h>
36 #include <sys/metaslab_impl.h>
37 #include <sys/space_map.h>
38 #include <sys/zio.h>
39 #include <sys/zap.h>
40 #include <sys/fs/zfs.h>
41 #include <sys/arc.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_spare_ops,
53 	&vdev_disk_ops,
54 	&vdev_file_ops,
55 	&vdev_missing_ops,
56 	NULL
57 };
58 
59 /* maximum scrub/resilver I/O queue per leaf vdev */
60 int zfs_scrub_limit = 10;
61 
62 /*
63  * Given a vdev type, return the appropriate ops vector.
64  */
65 static vdev_ops_t *
66 vdev_getops(const char *type)
67 {
68 	vdev_ops_t *ops, **opspp;
69 
70 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
71 		if (strcmp(ops->vdev_op_type, type) == 0)
72 			break;
73 
74 	return (ops);
75 }
76 
77 /*
78  * Default asize function: return the MAX of psize with the asize of
79  * all children.  This is what's used by anything other than RAID-Z.
80  */
81 uint64_t
82 vdev_default_asize(vdev_t *vd, uint64_t psize)
83 {
84 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
85 	uint64_t csize;
86 	uint64_t c;
87 
88 	for (c = 0; c < vd->vdev_children; c++) {
89 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
90 		asize = MAX(asize, csize);
91 	}
92 
93 	return (asize);
94 }
95 
96 /*
97  * Get the replaceable or attachable device size.
98  * If the parent is a mirror or raidz, the replaceable size is the minimum
99  * psize of all its children. For the rest, just return our own psize.
100  *
101  * e.g.
102  *			psize	rsize
103  * root			-	-
104  *	mirror/raidz	-	-
105  *	    disk1	20g	20g
106  *	    disk2 	40g	20g
107  *	disk3 		80g	80g
108  */
109 uint64_t
110 vdev_get_rsize(vdev_t *vd)
111 {
112 	vdev_t *pvd, *cvd;
113 	uint64_t c, rsize;
114 
115 	pvd = vd->vdev_parent;
116 
117 	/*
118 	 * If our parent is NULL or the root, just return our own psize.
119 	 */
120 	if (pvd == NULL || pvd->vdev_parent == NULL)
121 		return (vd->vdev_psize);
122 
123 	rsize = 0;
124 
125 	for (c = 0; c < pvd->vdev_children; c++) {
126 		cvd = pvd->vdev_child[c];
127 		rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
128 	}
129 
130 	return (rsize);
131 }
132 
133 vdev_t *
134 vdev_lookup_top(spa_t *spa, uint64_t vdev)
135 {
136 	vdev_t *rvd = spa->spa_root_vdev;
137 
138 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
139 
140 	if (vdev < rvd->vdev_children) {
141 		ASSERT(rvd->vdev_child[vdev] != NULL);
142 		return (rvd->vdev_child[vdev]);
143 	}
144 
145 	return (NULL);
146 }
147 
148 vdev_t *
149 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
150 {
151 	int c;
152 	vdev_t *mvd;
153 
154 	if (vd->vdev_guid == guid)
155 		return (vd);
156 
157 	for (c = 0; c < vd->vdev_children; c++)
158 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
159 		    NULL)
160 			return (mvd);
161 
162 	return (NULL);
163 }
164 
165 void
166 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
167 {
168 	size_t oldsize, newsize;
169 	uint64_t id = cvd->vdev_id;
170 	vdev_t **newchild;
171 
172 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
173 	ASSERT(cvd->vdev_parent == NULL);
174 
175 	cvd->vdev_parent = pvd;
176 
177 	if (pvd == NULL)
178 		return;
179 
180 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
181 
182 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
183 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
184 	newsize = pvd->vdev_children * sizeof (vdev_t *);
185 
186 	newchild = kmem_zalloc(newsize, KM_SLEEP);
187 	if (pvd->vdev_child != NULL) {
188 		bcopy(pvd->vdev_child, newchild, oldsize);
189 		kmem_free(pvd->vdev_child, oldsize);
190 	}
191 
192 	pvd->vdev_child = newchild;
193 	pvd->vdev_child[id] = cvd;
194 
195 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
196 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
197 
198 	/*
199 	 * Walk up all ancestors to update guid sum.
200 	 */
201 	for (; pvd != NULL; pvd = pvd->vdev_parent)
202 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
203 
204 	if (cvd->vdev_ops->vdev_op_leaf)
205 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
206 }
207 
208 void
209 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
210 {
211 	int c;
212 	uint_t id = cvd->vdev_id;
213 
214 	ASSERT(cvd->vdev_parent == pvd);
215 
216 	if (pvd == NULL)
217 		return;
218 
219 	ASSERT(id < pvd->vdev_children);
220 	ASSERT(pvd->vdev_child[id] == cvd);
221 
222 	pvd->vdev_child[id] = NULL;
223 	cvd->vdev_parent = NULL;
224 
225 	for (c = 0; c < pvd->vdev_children; c++)
226 		if (pvd->vdev_child[c])
227 			break;
228 
229 	if (c == pvd->vdev_children) {
230 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
231 		pvd->vdev_child = NULL;
232 		pvd->vdev_children = 0;
233 	}
234 
235 	/*
236 	 * Walk up all ancestors to update guid sum.
237 	 */
238 	for (; pvd != NULL; pvd = pvd->vdev_parent)
239 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
240 
241 	if (cvd->vdev_ops->vdev_op_leaf)
242 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
243 }
244 
245 /*
246  * Remove any holes in the child array.
247  */
248 void
249 vdev_compact_children(vdev_t *pvd)
250 {
251 	vdev_t **newchild, *cvd;
252 	int oldc = pvd->vdev_children;
253 	int newc, c;
254 
255 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
256 
257 	for (c = newc = 0; c < oldc; c++)
258 		if (pvd->vdev_child[c])
259 			newc++;
260 
261 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
262 
263 	for (c = newc = 0; c < oldc; c++) {
264 		if ((cvd = pvd->vdev_child[c]) != NULL) {
265 			newchild[newc] = cvd;
266 			cvd->vdev_id = newc++;
267 		}
268 	}
269 
270 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
271 	pvd->vdev_child = newchild;
272 	pvd->vdev_children = newc;
273 }
274 
275 /*
276  * Allocate and minimally initialize a vdev_t.
277  */
278 static vdev_t *
279 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
280 {
281 	vdev_t *vd;
282 
283 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
284 
285 	if (spa->spa_root_vdev == NULL) {
286 		ASSERT(ops == &vdev_root_ops);
287 		spa->spa_root_vdev = vd;
288 	}
289 
290 	if (guid == 0) {
291 		if (spa->spa_root_vdev == vd) {
292 			/*
293 			 * The root vdev's guid will also be the pool guid,
294 			 * which must be unique among all pools.
295 			 */
296 			while (guid == 0 || spa_guid_exists(guid, 0))
297 				guid = spa_get_random(-1ULL);
298 		} else {
299 			/*
300 			 * Any other vdev's guid must be unique within the pool.
301 			 */
302 			while (guid == 0 ||
303 			    spa_guid_exists(spa_guid(spa), guid))
304 				guid = spa_get_random(-1ULL);
305 		}
306 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
307 	}
308 
309 	vd->vdev_spa = spa;
310 	vd->vdev_id = id;
311 	vd->vdev_guid = guid;
312 	vd->vdev_guid_sum = guid;
313 	vd->vdev_ops = ops;
314 	vd->vdev_state = VDEV_STATE_CLOSED;
315 
316 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
317 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
318 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
319 	space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
320 	space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
321 	txg_list_create(&vd->vdev_ms_list,
322 	    offsetof(struct metaslab, ms_txg_node));
323 	txg_list_create(&vd->vdev_dtl_list,
324 	    offsetof(struct vdev, vdev_dtl_node));
325 	vd->vdev_stat.vs_timestamp = gethrtime();
326 	vdev_queue_init(vd);
327 	vdev_cache_init(vd);
328 
329 	return (vd);
330 }
331 
332 /*
333  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
334  * creating a new vdev or loading an existing one - the behavior is slightly
335  * different for each case.
336  */
337 int
338 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
339     int alloctype)
340 {
341 	vdev_ops_t *ops;
342 	char *type;
343 	uint64_t guid = 0, islog, nparity;
344 	vdev_t *vd;
345 
346 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
347 
348 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
349 		return (EINVAL);
350 
351 	if ((ops = vdev_getops(type)) == NULL)
352 		return (EINVAL);
353 
354 	/*
355 	 * If this is a load, get the vdev guid from the nvlist.
356 	 * Otherwise, vdev_alloc_common() will generate one for us.
357 	 */
358 	if (alloctype == VDEV_ALLOC_LOAD) {
359 		uint64_t label_id;
360 
361 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
362 		    label_id != id)
363 			return (EINVAL);
364 
365 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
366 			return (EINVAL);
367 	} else if (alloctype == VDEV_ALLOC_SPARE) {
368 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
369 			return (EINVAL);
370 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
371 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
372 			return (EINVAL);
373 	}
374 
375 	/*
376 	 * The first allocated vdev must be of type 'root'.
377 	 */
378 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
379 		return (EINVAL);
380 
381 	/*
382 	 * Determine whether we're a log vdev.
383 	 */
384 	islog = 0;
385 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
386 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
387 		return (ENOTSUP);
388 
389 	/*
390 	 * Set the nparity property for RAID-Z vdevs.
391 	 */
392 	nparity = -1ULL;
393 	if (ops == &vdev_raidz_ops) {
394 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
395 		    &nparity) == 0) {
396 			/*
397 			 * Currently, we can only support 2 parity devices.
398 			 */
399 			if (nparity == 0 || nparity > 2)
400 				return (EINVAL);
401 			/*
402 			 * Older versions can only support 1 parity device.
403 			 */
404 			if (nparity == 2 &&
405 			    spa_version(spa) < SPA_VERSION_RAID6)
406 				return (ENOTSUP);
407 		} else {
408 			/*
409 			 * We require the parity to be specified for SPAs that
410 			 * support multiple parity levels.
411 			 */
412 			if (spa_version(spa) >= SPA_VERSION_RAID6)
413 				return (EINVAL);
414 			/*
415 			 * Otherwise, we default to 1 parity device for RAID-Z.
416 			 */
417 			nparity = 1;
418 		}
419 	} else {
420 		nparity = 0;
421 	}
422 	ASSERT(nparity != -1ULL);
423 
424 	vd = vdev_alloc_common(spa, id, guid, ops);
425 
426 	vd->vdev_islog = islog;
427 	vd->vdev_nparity = nparity;
428 
429 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
430 		vd->vdev_path = spa_strdup(vd->vdev_path);
431 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
432 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
433 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
434 	    &vd->vdev_physpath) == 0)
435 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
436 
437 	/*
438 	 * Set the whole_disk property.  If it's not specified, leave the value
439 	 * as -1.
440 	 */
441 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
442 	    &vd->vdev_wholedisk) != 0)
443 		vd->vdev_wholedisk = -1ULL;
444 
445 	/*
446 	 * Look for the 'not present' flag.  This will only be set if the device
447 	 * was not present at the time of import.
448 	 */
449 	if (!spa->spa_import_faulted)
450 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
451 		    &vd->vdev_not_present);
452 
453 	/*
454 	 * Get the alignment requirement.
455 	 */
456 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
457 
458 	/*
459 	 * If we're a top-level vdev, try to load the allocation parameters.
460 	 */
461 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
462 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
463 		    &vd->vdev_ms_array);
464 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
465 		    &vd->vdev_ms_shift);
466 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
467 		    &vd->vdev_asize);
468 	}
469 
470 	/*
471 	 * If we're a leaf vdev, try to load the DTL object and other state.
472 	 */
473 	if (vd->vdev_ops->vdev_op_leaf &&
474 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) {
475 		if (alloctype == VDEV_ALLOC_LOAD) {
476 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
477 			    &vd->vdev_dtl.smo_object);
478 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
479 			    &vd->vdev_unspare);
480 		}
481 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
482 		    &vd->vdev_offline);
483 
484 		/*
485 		 * When importing a pool, we want to ignore the persistent fault
486 		 * state, as the diagnosis made on another system may not be
487 		 * valid in the current context.
488 		 */
489 		if (spa->spa_load_state == SPA_LOAD_OPEN) {
490 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
491 			    &vd->vdev_faulted);
492 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
493 			    &vd->vdev_degraded);
494 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
495 			    &vd->vdev_removed);
496 		}
497 	}
498 
499 	/*
500 	 * Add ourselves to the parent's list of children.
501 	 */
502 	vdev_add_child(parent, vd);
503 
504 	*vdp = vd;
505 
506 	return (0);
507 }
508 
509 void
510 vdev_free(vdev_t *vd)
511 {
512 	int c;
513 	spa_t *spa = vd->vdev_spa;
514 
515 	/*
516 	 * vdev_free() implies closing the vdev first.  This is simpler than
517 	 * trying to ensure complicated semantics for all callers.
518 	 */
519 	vdev_close(vd);
520 
521 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
522 
523 	/*
524 	 * Free all children.
525 	 */
526 	for (c = 0; c < vd->vdev_children; c++)
527 		vdev_free(vd->vdev_child[c]);
528 
529 	ASSERT(vd->vdev_child == NULL);
530 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
531 
532 	/*
533 	 * Discard allocation state.
534 	 */
535 	if (vd == vd->vdev_top)
536 		vdev_metaslab_fini(vd);
537 
538 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
539 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
540 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
541 
542 	/*
543 	 * Remove this vdev from its parent's child list.
544 	 */
545 	vdev_remove_child(vd->vdev_parent, vd);
546 
547 	ASSERT(vd->vdev_parent == NULL);
548 
549 	/*
550 	 * Clean up vdev structure.
551 	 */
552 	vdev_queue_fini(vd);
553 	vdev_cache_fini(vd);
554 
555 	if (vd->vdev_path)
556 		spa_strfree(vd->vdev_path);
557 	if (vd->vdev_devid)
558 		spa_strfree(vd->vdev_devid);
559 	if (vd->vdev_physpath)
560 		spa_strfree(vd->vdev_physpath);
561 
562 	if (vd->vdev_isspare)
563 		spa_spare_remove(vd);
564 	if (vd->vdev_isl2cache)
565 		spa_l2cache_remove(vd);
566 
567 	txg_list_destroy(&vd->vdev_ms_list);
568 	txg_list_destroy(&vd->vdev_dtl_list);
569 	mutex_enter(&vd->vdev_dtl_lock);
570 	space_map_unload(&vd->vdev_dtl_map);
571 	space_map_destroy(&vd->vdev_dtl_map);
572 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
573 	space_map_destroy(&vd->vdev_dtl_scrub);
574 	mutex_exit(&vd->vdev_dtl_lock);
575 	mutex_destroy(&vd->vdev_dtl_lock);
576 	mutex_destroy(&vd->vdev_stat_lock);
577 	mutex_destroy(&vd->vdev_probe_lock);
578 
579 	if (vd == spa->spa_root_vdev)
580 		spa->spa_root_vdev = NULL;
581 
582 	kmem_free(vd, sizeof (vdev_t));
583 }
584 
585 /*
586  * Transfer top-level vdev state from svd to tvd.
587  */
588 static void
589 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
590 {
591 	spa_t *spa = svd->vdev_spa;
592 	metaslab_t *msp;
593 	vdev_t *vd;
594 	int t;
595 
596 	ASSERT(tvd == tvd->vdev_top);
597 
598 	tvd->vdev_ms_array = svd->vdev_ms_array;
599 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
600 	tvd->vdev_ms_count = svd->vdev_ms_count;
601 
602 	svd->vdev_ms_array = 0;
603 	svd->vdev_ms_shift = 0;
604 	svd->vdev_ms_count = 0;
605 
606 	tvd->vdev_mg = svd->vdev_mg;
607 	tvd->vdev_ms = svd->vdev_ms;
608 
609 	svd->vdev_mg = NULL;
610 	svd->vdev_ms = NULL;
611 
612 	if (tvd->vdev_mg != NULL)
613 		tvd->vdev_mg->mg_vd = tvd;
614 
615 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
616 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
617 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
618 
619 	svd->vdev_stat.vs_alloc = 0;
620 	svd->vdev_stat.vs_space = 0;
621 	svd->vdev_stat.vs_dspace = 0;
622 
623 	for (t = 0; t < TXG_SIZE; t++) {
624 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
625 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
626 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
627 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
628 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
629 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
630 	}
631 
632 	if (list_link_active(&svd->vdev_config_dirty_node)) {
633 		vdev_config_clean(svd);
634 		vdev_config_dirty(tvd);
635 	}
636 
637 	if (list_link_active(&svd->vdev_state_dirty_node)) {
638 		vdev_state_clean(svd);
639 		vdev_state_dirty(tvd);
640 	}
641 
642 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
643 	svd->vdev_deflate_ratio = 0;
644 
645 	tvd->vdev_islog = svd->vdev_islog;
646 	svd->vdev_islog = 0;
647 }
648 
649 static void
650 vdev_top_update(vdev_t *tvd, vdev_t *vd)
651 {
652 	int c;
653 
654 	if (vd == NULL)
655 		return;
656 
657 	vd->vdev_top = tvd;
658 
659 	for (c = 0; c < vd->vdev_children; c++)
660 		vdev_top_update(tvd, vd->vdev_child[c]);
661 }
662 
663 /*
664  * Add a mirror/replacing vdev above an existing vdev.
665  */
666 vdev_t *
667 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
668 {
669 	spa_t *spa = cvd->vdev_spa;
670 	vdev_t *pvd = cvd->vdev_parent;
671 	vdev_t *mvd;
672 
673 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
674 
675 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
676 
677 	mvd->vdev_asize = cvd->vdev_asize;
678 	mvd->vdev_ashift = cvd->vdev_ashift;
679 	mvd->vdev_state = cvd->vdev_state;
680 
681 	vdev_remove_child(pvd, cvd);
682 	vdev_add_child(pvd, mvd);
683 	cvd->vdev_id = mvd->vdev_children;
684 	vdev_add_child(mvd, cvd);
685 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
686 
687 	if (mvd == mvd->vdev_top)
688 		vdev_top_transfer(cvd, mvd);
689 
690 	return (mvd);
691 }
692 
693 /*
694  * Remove a 1-way mirror/replacing vdev from the tree.
695  */
696 void
697 vdev_remove_parent(vdev_t *cvd)
698 {
699 	vdev_t *mvd = cvd->vdev_parent;
700 	vdev_t *pvd = mvd->vdev_parent;
701 
702 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
703 
704 	ASSERT(mvd->vdev_children == 1);
705 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
706 	    mvd->vdev_ops == &vdev_replacing_ops ||
707 	    mvd->vdev_ops == &vdev_spare_ops);
708 	cvd->vdev_ashift = mvd->vdev_ashift;
709 
710 	vdev_remove_child(mvd, cvd);
711 	vdev_remove_child(pvd, mvd);
712 	/*
713 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
714 	 * Otherwise, we could have detached an offline device, and when we
715 	 * go to import the pool we'll think we have two top-level vdevs,
716 	 * instead of a different version of the same top-level vdev.
717 	 */
718 	if (mvd->vdev_top == mvd)
719 		cvd->vdev_guid = cvd->vdev_guid_sum = mvd->vdev_guid;
720 	cvd->vdev_id = mvd->vdev_id;
721 	vdev_add_child(pvd, cvd);
722 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
723 
724 	if (cvd == cvd->vdev_top)
725 		vdev_top_transfer(mvd, cvd);
726 
727 	ASSERT(mvd->vdev_children == 0);
728 	vdev_free(mvd);
729 }
730 
731 int
732 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
733 {
734 	spa_t *spa = vd->vdev_spa;
735 	objset_t *mos = spa->spa_meta_objset;
736 	metaslab_class_t *mc;
737 	uint64_t m;
738 	uint64_t oldc = vd->vdev_ms_count;
739 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
740 	metaslab_t **mspp;
741 	int error;
742 
743 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
744 		return (0);
745 
746 	ASSERT(oldc <= newc);
747 
748 	if (vd->vdev_islog)
749 		mc = spa->spa_log_class;
750 	else
751 		mc = spa->spa_normal_class;
752 
753 	if (vd->vdev_mg == NULL)
754 		vd->vdev_mg = metaslab_group_create(mc, vd);
755 
756 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
757 
758 	if (oldc != 0) {
759 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
760 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
761 	}
762 
763 	vd->vdev_ms = mspp;
764 	vd->vdev_ms_count = newc;
765 
766 	for (m = oldc; m < newc; m++) {
767 		space_map_obj_t smo = { 0, 0, 0 };
768 		if (txg == 0) {
769 			uint64_t object = 0;
770 			error = dmu_read(mos, vd->vdev_ms_array,
771 			    m * sizeof (uint64_t), sizeof (uint64_t), &object);
772 			if (error)
773 				return (error);
774 			if (object != 0) {
775 				dmu_buf_t *db;
776 				error = dmu_bonus_hold(mos, object, FTAG, &db);
777 				if (error)
778 					return (error);
779 				ASSERT3U(db->db_size, >=, sizeof (smo));
780 				bcopy(db->db_data, &smo, sizeof (smo));
781 				ASSERT3U(smo.smo_object, ==, object);
782 				dmu_buf_rele(db, FTAG);
783 			}
784 		}
785 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
786 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
787 	}
788 
789 	return (0);
790 }
791 
792 void
793 vdev_metaslab_fini(vdev_t *vd)
794 {
795 	uint64_t m;
796 	uint64_t count = vd->vdev_ms_count;
797 
798 	if (vd->vdev_ms != NULL) {
799 		for (m = 0; m < count; m++)
800 			if (vd->vdev_ms[m] != NULL)
801 				metaslab_fini(vd->vdev_ms[m]);
802 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
803 		vd->vdev_ms = NULL;
804 	}
805 }
806 
807 typedef struct vdev_probe_stats {
808 	boolean_t	vps_readable;
809 	boolean_t	vps_writeable;
810 	int		vps_flags;
811 	zio_t		*vps_root;
812 	vdev_t		*vps_vd;
813 } vdev_probe_stats_t;
814 
815 static void
816 vdev_probe_done(zio_t *zio)
817 {
818 	vdev_probe_stats_t *vps = zio->io_private;
819 	vdev_t *vd = vps->vps_vd;
820 
821 	if (zio->io_type == ZIO_TYPE_READ) {
822 		ASSERT(zio->io_vd == vd);
823 		if (zio->io_error == 0)
824 			vps->vps_readable = 1;
825 		if (zio->io_error == 0 && (spa_mode & FWRITE)) {
826 			zio_nowait(zio_write_phys(vps->vps_root, vd,
827 			    zio->io_offset, zio->io_size, zio->io_data,
828 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
829 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
830 		} else {
831 			zio_buf_free(zio->io_data, zio->io_size);
832 		}
833 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
834 		ASSERT(zio->io_vd == vd);
835 		if (zio->io_error == 0)
836 			vps->vps_writeable = 1;
837 		zio_buf_free(zio->io_data, zio->io_size);
838 	} else if (zio->io_type == ZIO_TYPE_NULL) {
839 		ASSERT(zio->io_vd == NULL);
840 		ASSERT(zio == vps->vps_root);
841 
842 		vd->vdev_cant_read |= !vps->vps_readable;
843 		vd->vdev_cant_write |= !vps->vps_writeable;
844 
845 		if (vdev_readable(vd) &&
846 		    (vdev_writeable(vd) || !(spa_mode & FWRITE))) {
847 			zio->io_error = 0;
848 		} else {
849 			ASSERT(zio->io_error != 0);
850 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
851 			    zio->io_spa, vd, NULL, 0, 0);
852 			zio->io_error = ENXIO;
853 		}
854 		kmem_free(vps, sizeof (*vps));
855 	}
856 }
857 
858 /*
859  * Determine whether this device is accessible by reading and writing
860  * to several known locations: the pad regions of each vdev label
861  * but the first (which we leave alone in case it contains a VTOC).
862  */
863 zio_t *
864 vdev_probe(vdev_t *vd, zio_t *pio)
865 {
866 	spa_t *spa = vd->vdev_spa;
867 	vdev_probe_stats_t *vps;
868 	zio_t *zio;
869 
870 	vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
871 
872 	vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
873 	    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_DONT_RETRY;
874 
875 	if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
876 		/*
877 		 * vdev_cant_read and vdev_cant_write can only transition
878 		 * from TRUE to FALSE when we have the SCL_ZIO lock as writer;
879 		 * otherwise they can only transition from FALSE to TRUE.
880 		 * This ensures that any zio looking at these values can
881 		 * assume that failures persist for the life of the I/O.
882 		 * That's important because when a device has intermittent
883 		 * connectivity problems, we want to ensure that they're
884 		 * ascribed to the device (ENXIO) and not the zio (EIO).
885 		 *
886 		 * Since we hold SCL_ZIO as writer here, clear both values
887 		 * so the probe can reevaluate from first principles.
888 		 */
889 		vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
890 		vd->vdev_cant_read = B_FALSE;
891 		vd->vdev_cant_write = B_FALSE;
892 	}
893 
894 	ASSERT(vd->vdev_ops->vdev_op_leaf);
895 
896 	zio = zio_null(pio, spa, vdev_probe_done, vps, vps->vps_flags);
897 
898 	vps->vps_root = zio;
899 	vps->vps_vd = vd;
900 
901 	for (int l = 1; l < VDEV_LABELS; l++) {
902 		zio_nowait(zio_read_phys(zio, vd,
903 		    vdev_label_offset(vd->vdev_psize, l,
904 		    offsetof(vdev_label_t, vl_pad)),
905 		    VDEV_SKIP_SIZE, zio_buf_alloc(VDEV_SKIP_SIZE),
906 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
907 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
908 	}
909 
910 	return (zio);
911 }
912 
913 /*
914  * Prepare a virtual device for access.
915  */
916 int
917 vdev_open(vdev_t *vd)
918 {
919 	int error;
920 	int c;
921 	uint64_t osize = 0;
922 	uint64_t asize, psize;
923 	uint64_t ashift = 0;
924 
925 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
926 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
927 	    vd->vdev_state == VDEV_STATE_OFFLINE);
928 
929 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
930 
931 	if (!vd->vdev_removed && vd->vdev_faulted) {
932 		ASSERT(vd->vdev_children == 0);
933 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
934 		    VDEV_AUX_ERR_EXCEEDED);
935 		return (ENXIO);
936 	} else if (vd->vdev_offline) {
937 		ASSERT(vd->vdev_children == 0);
938 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
939 		return (ENXIO);
940 	}
941 
942 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
943 
944 	if (zio_injection_enabled && error == 0)
945 		error = zio_handle_device_injection(vd, ENXIO);
946 
947 	if (error) {
948 		if (vd->vdev_removed &&
949 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
950 			vd->vdev_removed = B_FALSE;
951 
952 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
953 		    vd->vdev_stat.vs_aux);
954 		return (error);
955 	}
956 
957 	vd->vdev_removed = B_FALSE;
958 
959 	if (vd->vdev_degraded) {
960 		ASSERT(vd->vdev_children == 0);
961 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
962 		    VDEV_AUX_ERR_EXCEEDED);
963 	} else {
964 		vd->vdev_state = VDEV_STATE_HEALTHY;
965 	}
966 
967 	for (c = 0; c < vd->vdev_children; c++)
968 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
969 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
970 			    VDEV_AUX_NONE);
971 			break;
972 		}
973 
974 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
975 
976 	if (vd->vdev_children == 0) {
977 		if (osize < SPA_MINDEVSIZE) {
978 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
979 			    VDEV_AUX_TOO_SMALL);
980 			return (EOVERFLOW);
981 		}
982 		psize = osize;
983 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
984 	} else {
985 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
986 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
987 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
988 			    VDEV_AUX_TOO_SMALL);
989 			return (EOVERFLOW);
990 		}
991 		psize = 0;
992 		asize = osize;
993 	}
994 
995 	vd->vdev_psize = psize;
996 
997 	if (vd->vdev_asize == 0) {
998 		/*
999 		 * This is the first-ever open, so use the computed values.
1000 		 * For testing purposes, a higher ashift can be requested.
1001 		 */
1002 		vd->vdev_asize = asize;
1003 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1004 	} else {
1005 		/*
1006 		 * Make sure the alignment requirement hasn't increased.
1007 		 */
1008 		if (ashift > vd->vdev_top->vdev_ashift) {
1009 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1010 			    VDEV_AUX_BAD_LABEL);
1011 			return (EINVAL);
1012 		}
1013 
1014 		/*
1015 		 * Make sure the device hasn't shrunk.
1016 		 */
1017 		if (asize < vd->vdev_asize) {
1018 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1019 			    VDEV_AUX_BAD_LABEL);
1020 			return (EINVAL);
1021 		}
1022 
1023 		/*
1024 		 * If all children are healthy and the asize has increased,
1025 		 * then we've experienced dynamic LUN growth.
1026 		 */
1027 		if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1028 		    asize > vd->vdev_asize) {
1029 			vd->vdev_asize = asize;
1030 		}
1031 	}
1032 
1033 	/*
1034 	 * Ensure we can issue some IO before declaring the
1035 	 * vdev open for business.
1036 	 */
1037 	if (vd->vdev_ops->vdev_op_leaf &&
1038 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1039 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1040 		    VDEV_AUX_IO_FAILURE);
1041 		return (error);
1042 	}
1043 
1044 	/*
1045 	 * If this is a top-level vdev, compute the raidz-deflation
1046 	 * ratio.  Note, we hard-code in 128k (1<<17) because it is the
1047 	 * current "typical" blocksize.  Even if SPA_MAXBLOCKSIZE
1048 	 * changes, this algorithm must never change, or we will
1049 	 * inconsistently account for existing bp's.
1050 	 */
1051 	if (vd->vdev_top == vd) {
1052 		vd->vdev_deflate_ratio = (1<<17) /
1053 		    (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
1054 	}
1055 
1056 	/*
1057 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1058 	 * resilver.  But don't do this if we are doing a reopen for a
1059 	 * scrub, since this would just restart the scrub we are already
1060 	 * doing.
1061 	 */
1062 	if (vd->vdev_children == 0 && !vd->vdev_spa->spa_scrub_reopen) {
1063 		mutex_enter(&vd->vdev_dtl_lock);
1064 		if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd))
1065 			spa_async_request(vd->vdev_spa, SPA_ASYNC_RESILVER);
1066 		mutex_exit(&vd->vdev_dtl_lock);
1067 	}
1068 
1069 	return (0);
1070 }
1071 
1072 /*
1073  * Called once the vdevs are all opened, this routine validates the label
1074  * contents.  This needs to be done before vdev_load() so that we don't
1075  * inadvertently do repair I/Os to the wrong device.
1076  *
1077  * This function will only return failure if one of the vdevs indicates that it
1078  * has since been destroyed or exported.  This is only possible if
1079  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1080  * will be updated but the function will return 0.
1081  */
1082 int
1083 vdev_validate(vdev_t *vd)
1084 {
1085 	spa_t *spa = vd->vdev_spa;
1086 	int c;
1087 	nvlist_t *label;
1088 	uint64_t guid, top_guid;
1089 	uint64_t state;
1090 
1091 	for (c = 0; c < vd->vdev_children; c++)
1092 		if (vdev_validate(vd->vdev_child[c]) != 0)
1093 			return (EBADF);
1094 
1095 	/*
1096 	 * If the device has already failed, or was marked offline, don't do
1097 	 * any further validation.  Otherwise, label I/O will fail and we will
1098 	 * overwrite the previous state.
1099 	 */
1100 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1101 
1102 		if ((label = vdev_label_read_config(vd)) == NULL) {
1103 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1104 			    VDEV_AUX_BAD_LABEL);
1105 			return (0);
1106 		}
1107 
1108 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
1109 		    &guid) != 0 || guid != spa_guid(spa)) {
1110 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1111 			    VDEV_AUX_CORRUPT_DATA);
1112 			nvlist_free(label);
1113 			return (0);
1114 		}
1115 
1116 		/*
1117 		 * If this vdev just became a top-level vdev because its
1118 		 * sibling was detached, it will have adopted the parent's
1119 		 * vdev guid -- but the label may or may not be on disk yet.
1120 		 * Fortunately, either version of the label will have the
1121 		 * same top guid, so if we're a top-level vdev, we can
1122 		 * safely compare to that instead.
1123 		 */
1124 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1125 		    &guid) != 0 ||
1126 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1127 		    &top_guid) != 0 ||
1128 		    (vd->vdev_guid != guid &&
1129 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1130 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1131 			    VDEV_AUX_CORRUPT_DATA);
1132 			nvlist_free(label);
1133 			return (0);
1134 		}
1135 
1136 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1137 		    &state) != 0) {
1138 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1139 			    VDEV_AUX_CORRUPT_DATA);
1140 			nvlist_free(label);
1141 			return (0);
1142 		}
1143 
1144 		nvlist_free(label);
1145 
1146 		if (spa->spa_load_state == SPA_LOAD_OPEN &&
1147 		    state != POOL_STATE_ACTIVE)
1148 			return (EBADF);
1149 
1150 		/*
1151 		 * If we were able to open and validate a vdev that was
1152 		 * previously marked permanently unavailable, clear that state
1153 		 * now.
1154 		 */
1155 		if (vd->vdev_not_present)
1156 			vd->vdev_not_present = 0;
1157 	}
1158 
1159 	return (0);
1160 }
1161 
1162 /*
1163  * Close a virtual device.
1164  */
1165 void
1166 vdev_close(vdev_t *vd)
1167 {
1168 	vd->vdev_ops->vdev_op_close(vd);
1169 
1170 	vdev_cache_purge(vd);
1171 
1172 	/*
1173 	 * We record the previous state before we close it, so  that if we are
1174 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1175 	 * it's still faulted.
1176 	 */
1177 	vd->vdev_prevstate = vd->vdev_state;
1178 
1179 	if (vd->vdev_offline)
1180 		vd->vdev_state = VDEV_STATE_OFFLINE;
1181 	else
1182 		vd->vdev_state = VDEV_STATE_CLOSED;
1183 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1184 }
1185 
1186 void
1187 vdev_reopen(vdev_t *vd)
1188 {
1189 	spa_t *spa = vd->vdev_spa;
1190 
1191 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1192 
1193 	vdev_close(vd);
1194 	(void) vdev_open(vd);
1195 
1196 	/*
1197 	 * Call vdev_validate() here to make sure we have the same device.
1198 	 * Otherwise, a device with an invalid label could be successfully
1199 	 * opened in response to vdev_reopen().
1200 	 */
1201 	if (vd->vdev_aux) {
1202 		(void) vdev_validate_aux(vd);
1203 		if (vdev_readable(vd) && vdev_writeable(vd) &&
1204 		    !l2arc_vdev_present(vd)) {
1205 			uint64_t size = vdev_get_rsize(vd);
1206 			l2arc_add_vdev(spa, vd,
1207 			    VDEV_LABEL_START_SIZE,
1208 			    size - VDEV_LABEL_START_SIZE);
1209 		}
1210 	} else {
1211 		(void) vdev_validate(vd);
1212 	}
1213 
1214 	/*
1215 	 * Reassess parent vdev's health.
1216 	 */
1217 	vdev_propagate_state(vd);
1218 }
1219 
1220 int
1221 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1222 {
1223 	int error;
1224 
1225 	/*
1226 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1227 	 * For a create, however, we want to fail the request if
1228 	 * there are any components we can't open.
1229 	 */
1230 	error = vdev_open(vd);
1231 
1232 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1233 		vdev_close(vd);
1234 		return (error ? error : ENXIO);
1235 	}
1236 
1237 	/*
1238 	 * Recursively initialize all labels.
1239 	 */
1240 	if ((error = vdev_label_init(vd, txg, isreplacing ?
1241 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1242 		vdev_close(vd);
1243 		return (error);
1244 	}
1245 
1246 	return (0);
1247 }
1248 
1249 /*
1250  * The is the latter half of vdev_create().  It is distinct because it
1251  * involves initiating transactions in order to do metaslab creation.
1252  * For creation, we want to try to create all vdevs at once and then undo it
1253  * if anything fails; this is much harder if we have pending transactions.
1254  */
1255 void
1256 vdev_init(vdev_t *vd, uint64_t txg)
1257 {
1258 	/*
1259 	 * Aim for roughly 200 metaslabs per vdev.
1260 	 */
1261 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1262 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1263 
1264 	/*
1265 	 * Initialize the vdev's metaslabs.  This can't fail because
1266 	 * there's nothing to read when creating all new metaslabs.
1267 	 */
1268 	VERIFY(vdev_metaslab_init(vd, txg) == 0);
1269 }
1270 
1271 void
1272 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1273 {
1274 	ASSERT(vd == vd->vdev_top);
1275 	ASSERT(ISP2(flags));
1276 
1277 	if (flags & VDD_METASLAB)
1278 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1279 
1280 	if (flags & VDD_DTL)
1281 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1282 
1283 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1284 }
1285 
1286 void
1287 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1288 {
1289 	mutex_enter(sm->sm_lock);
1290 	if (!space_map_contains(sm, txg, size))
1291 		space_map_add(sm, txg, size);
1292 	mutex_exit(sm->sm_lock);
1293 }
1294 
1295 int
1296 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1297 {
1298 	int dirty;
1299 
1300 	/*
1301 	 * Quick test without the lock -- covers the common case that
1302 	 * there are no dirty time segments.
1303 	 */
1304 	if (sm->sm_space == 0)
1305 		return (0);
1306 
1307 	mutex_enter(sm->sm_lock);
1308 	dirty = space_map_contains(sm, txg, size);
1309 	mutex_exit(sm->sm_lock);
1310 
1311 	return (dirty);
1312 }
1313 
1314 /*
1315  * Reassess DTLs after a config change or scrub completion.
1316  */
1317 void
1318 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1319 {
1320 	spa_t *spa = vd->vdev_spa;
1321 	int c;
1322 
1323 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
1324 
1325 	if (vd->vdev_children == 0) {
1326 		mutex_enter(&vd->vdev_dtl_lock);
1327 		if (scrub_txg != 0 &&
1328 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
1329 			/* XXX should check scrub_done? */
1330 			/*
1331 			 * We completed a scrub up to scrub_txg.  If we
1332 			 * did it without rebooting, then the scrub dtl
1333 			 * will be valid, so excise the old region and
1334 			 * fold in the scrub dtl.  Otherwise, leave the
1335 			 * dtl as-is if there was an error.
1336 			 */
1337 			space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1338 			space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1339 		}
1340 		if (scrub_done)
1341 			space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1342 		mutex_exit(&vd->vdev_dtl_lock);
1343 
1344 		if (txg != 0)
1345 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1346 		return;
1347 	}
1348 
1349 	/*
1350 	 * Make sure the DTLs are always correct under the scrub lock.
1351 	 */
1352 	if (vd == spa->spa_root_vdev)
1353 		mutex_enter(&spa->spa_scrub_lock);
1354 
1355 	mutex_enter(&vd->vdev_dtl_lock);
1356 	space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1357 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1358 	mutex_exit(&vd->vdev_dtl_lock);
1359 
1360 	for (c = 0; c < vd->vdev_children; c++) {
1361 		vdev_t *cvd = vd->vdev_child[c];
1362 		vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1363 		mutex_enter(&vd->vdev_dtl_lock);
1364 		space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1365 		space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1366 		mutex_exit(&vd->vdev_dtl_lock);
1367 	}
1368 
1369 	if (vd == spa->spa_root_vdev)
1370 		mutex_exit(&spa->spa_scrub_lock);
1371 }
1372 
1373 static int
1374 vdev_dtl_load(vdev_t *vd)
1375 {
1376 	spa_t *spa = vd->vdev_spa;
1377 	space_map_obj_t *smo = &vd->vdev_dtl;
1378 	objset_t *mos = spa->spa_meta_objset;
1379 	dmu_buf_t *db;
1380 	int error;
1381 
1382 	ASSERT(vd->vdev_children == 0);
1383 
1384 	if (smo->smo_object == 0)
1385 		return (0);
1386 
1387 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1388 		return (error);
1389 
1390 	ASSERT3U(db->db_size, >=, sizeof (*smo));
1391 	bcopy(db->db_data, smo, sizeof (*smo));
1392 	dmu_buf_rele(db, FTAG);
1393 
1394 	mutex_enter(&vd->vdev_dtl_lock);
1395 	error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1396 	mutex_exit(&vd->vdev_dtl_lock);
1397 
1398 	return (error);
1399 }
1400 
1401 void
1402 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1403 {
1404 	spa_t *spa = vd->vdev_spa;
1405 	space_map_obj_t *smo = &vd->vdev_dtl;
1406 	space_map_t *sm = &vd->vdev_dtl_map;
1407 	objset_t *mos = spa->spa_meta_objset;
1408 	space_map_t smsync;
1409 	kmutex_t smlock;
1410 	dmu_buf_t *db;
1411 	dmu_tx_t *tx;
1412 
1413 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1414 
1415 	if (vd->vdev_detached) {
1416 		if (smo->smo_object != 0) {
1417 			int err = dmu_object_free(mos, smo->smo_object, tx);
1418 			ASSERT3U(err, ==, 0);
1419 			smo->smo_object = 0;
1420 		}
1421 		dmu_tx_commit(tx);
1422 		return;
1423 	}
1424 
1425 	if (smo->smo_object == 0) {
1426 		ASSERT(smo->smo_objsize == 0);
1427 		ASSERT(smo->smo_alloc == 0);
1428 		smo->smo_object = dmu_object_alloc(mos,
1429 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1430 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1431 		ASSERT(smo->smo_object != 0);
1432 		vdev_config_dirty(vd->vdev_top);
1433 	}
1434 
1435 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1436 
1437 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1438 	    &smlock);
1439 
1440 	mutex_enter(&smlock);
1441 
1442 	mutex_enter(&vd->vdev_dtl_lock);
1443 	space_map_walk(sm, space_map_add, &smsync);
1444 	mutex_exit(&vd->vdev_dtl_lock);
1445 
1446 	space_map_truncate(smo, mos, tx);
1447 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1448 
1449 	space_map_destroy(&smsync);
1450 
1451 	mutex_exit(&smlock);
1452 	mutex_destroy(&smlock);
1453 
1454 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1455 	dmu_buf_will_dirty(db, tx);
1456 	ASSERT3U(db->db_size, >=, sizeof (*smo));
1457 	bcopy(smo, db->db_data, sizeof (*smo));
1458 	dmu_buf_rele(db, FTAG);
1459 
1460 	dmu_tx_commit(tx);
1461 }
1462 
1463 /*
1464  * Determine if resilver is needed, and if so the txg range.
1465  */
1466 boolean_t
1467 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
1468 {
1469 	boolean_t needed = B_FALSE;
1470 	uint64_t thismin = UINT64_MAX;
1471 	uint64_t thismax = 0;
1472 
1473 	if (vd->vdev_children == 0) {
1474 		mutex_enter(&vd->vdev_dtl_lock);
1475 		if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd)) {
1476 			space_seg_t *ss;
1477 
1478 			ss = avl_first(&vd->vdev_dtl_map.sm_root);
1479 			thismin = ss->ss_start - 1;
1480 			ss = avl_last(&vd->vdev_dtl_map.sm_root);
1481 			thismax = ss->ss_end;
1482 			needed = B_TRUE;
1483 		}
1484 		mutex_exit(&vd->vdev_dtl_lock);
1485 	} else {
1486 		int c;
1487 		for (c = 0; c < vd->vdev_children; c++) {
1488 			vdev_t *cvd = vd->vdev_child[c];
1489 			uint64_t cmin, cmax;
1490 
1491 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
1492 				thismin = MIN(thismin, cmin);
1493 				thismax = MAX(thismax, cmax);
1494 				needed = B_TRUE;
1495 			}
1496 		}
1497 	}
1498 
1499 	if (needed && minp) {
1500 		*minp = thismin;
1501 		*maxp = thismax;
1502 	}
1503 	return (needed);
1504 }
1505 
1506 void
1507 vdev_load(vdev_t *vd)
1508 {
1509 	int c;
1510 
1511 	/*
1512 	 * Recursively load all children.
1513 	 */
1514 	for (c = 0; c < vd->vdev_children; c++)
1515 		vdev_load(vd->vdev_child[c]);
1516 
1517 	/*
1518 	 * If this is a top-level vdev, initialize its metaslabs.
1519 	 */
1520 	if (vd == vd->vdev_top &&
1521 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1522 	    vdev_metaslab_init(vd, 0) != 0))
1523 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1524 		    VDEV_AUX_CORRUPT_DATA);
1525 
1526 	/*
1527 	 * If this is a leaf vdev, load its DTL.
1528 	 */
1529 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1530 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1531 		    VDEV_AUX_CORRUPT_DATA);
1532 }
1533 
1534 /*
1535  * The special vdev case is used for hot spares and l2cache devices.  Its
1536  * sole purpose it to set the vdev state for the associated vdev.  To do this,
1537  * we make sure that we can open the underlying device, then try to read the
1538  * label, and make sure that the label is sane and that it hasn't been
1539  * repurposed to another pool.
1540  */
1541 int
1542 vdev_validate_aux(vdev_t *vd)
1543 {
1544 	nvlist_t *label;
1545 	uint64_t guid, version;
1546 	uint64_t state;
1547 
1548 	if (!vdev_readable(vd))
1549 		return (0);
1550 
1551 	if ((label = vdev_label_read_config(vd)) == NULL) {
1552 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1553 		    VDEV_AUX_CORRUPT_DATA);
1554 		return (-1);
1555 	}
1556 
1557 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1558 	    version > SPA_VERSION ||
1559 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1560 	    guid != vd->vdev_guid ||
1561 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1562 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1563 		    VDEV_AUX_CORRUPT_DATA);
1564 		nvlist_free(label);
1565 		return (-1);
1566 	}
1567 
1568 	/*
1569 	 * We don't actually check the pool state here.  If it's in fact in
1570 	 * use by another pool, we update this fact on the fly when requested.
1571 	 */
1572 	nvlist_free(label);
1573 	return (0);
1574 }
1575 
1576 void
1577 vdev_sync_done(vdev_t *vd, uint64_t txg)
1578 {
1579 	metaslab_t *msp;
1580 
1581 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1582 		metaslab_sync_done(msp, txg);
1583 }
1584 
1585 void
1586 vdev_sync(vdev_t *vd, uint64_t txg)
1587 {
1588 	spa_t *spa = vd->vdev_spa;
1589 	vdev_t *lvd;
1590 	metaslab_t *msp;
1591 	dmu_tx_t *tx;
1592 
1593 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1594 		ASSERT(vd == vd->vdev_top);
1595 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1596 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1597 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1598 		ASSERT(vd->vdev_ms_array != 0);
1599 		vdev_config_dirty(vd);
1600 		dmu_tx_commit(tx);
1601 	}
1602 
1603 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1604 		metaslab_sync(msp, txg);
1605 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1606 	}
1607 
1608 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1609 		vdev_dtl_sync(lvd, txg);
1610 
1611 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1612 }
1613 
1614 uint64_t
1615 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1616 {
1617 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1618 }
1619 
1620 /*
1621  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
1622  * not be opened, and no I/O is attempted.
1623  */
1624 int
1625 vdev_fault(spa_t *spa, uint64_t guid)
1626 {
1627 	vdev_t *vd;
1628 
1629 	spa_vdev_state_enter(spa);
1630 
1631 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1632 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1633 
1634 	if (!vd->vdev_ops->vdev_op_leaf)
1635 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1636 
1637 	/*
1638 	 * Faulted state takes precedence over degraded.
1639 	 */
1640 	vd->vdev_faulted = 1ULL;
1641 	vd->vdev_degraded = 0ULL;
1642 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
1643 
1644 	/*
1645 	 * If marking the vdev as faulted cause the top-level vdev to become
1646 	 * unavailable, then back off and simply mark the vdev as degraded
1647 	 * instead.
1648 	 */
1649 	if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
1650 		vd->vdev_degraded = 1ULL;
1651 		vd->vdev_faulted = 0ULL;
1652 
1653 		/*
1654 		 * If we reopen the device and it's not dead, only then do we
1655 		 * mark it degraded.
1656 		 */
1657 		vdev_reopen(vd);
1658 
1659 		if (vdev_readable(vd)) {
1660 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1661 			    VDEV_AUX_ERR_EXCEEDED);
1662 		}
1663 	}
1664 
1665 	return (spa_vdev_state_exit(spa, vd, 0));
1666 }
1667 
1668 /*
1669  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
1670  * user that something is wrong.  The vdev continues to operate as normal as far
1671  * as I/O is concerned.
1672  */
1673 int
1674 vdev_degrade(spa_t *spa, uint64_t guid)
1675 {
1676 	vdev_t *vd;
1677 
1678 	spa_vdev_state_enter(spa);
1679 
1680 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1681 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1682 
1683 	if (!vd->vdev_ops->vdev_op_leaf)
1684 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1685 
1686 	/*
1687 	 * If the vdev is already faulted, then don't do anything.
1688 	 */
1689 	if (vd->vdev_faulted || vd->vdev_degraded)
1690 		return (spa_vdev_state_exit(spa, NULL, 0));
1691 
1692 	vd->vdev_degraded = 1ULL;
1693 	if (!vdev_is_dead(vd))
1694 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1695 		    VDEV_AUX_ERR_EXCEEDED);
1696 
1697 	return (spa_vdev_state_exit(spa, vd, 0));
1698 }
1699 
1700 /*
1701  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
1702  * any attached spare device should be detached when the device finishes
1703  * resilvering.  Second, the online should be treated like a 'test' online case,
1704  * so no FMA events are generated if the device fails to open.
1705  */
1706 int
1707 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1708 {
1709 	vdev_t *vd;
1710 
1711 	spa_vdev_state_enter(spa);
1712 
1713 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1714 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1715 
1716 	if (!vd->vdev_ops->vdev_op_leaf)
1717 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1718 
1719 	vd->vdev_offline = B_FALSE;
1720 	vd->vdev_tmpoffline = B_FALSE;
1721 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
1722 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
1723 	vdev_reopen(vd->vdev_top);
1724 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
1725 
1726 	if (newstate)
1727 		*newstate = vd->vdev_state;
1728 	if ((flags & ZFS_ONLINE_UNSPARE) &&
1729 	    !vdev_is_dead(vd) && vd->vdev_parent &&
1730 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
1731 	    vd->vdev_parent->vdev_child[0] == vd)
1732 		vd->vdev_unspare = B_TRUE;
1733 
1734 	(void) spa_vdev_state_exit(spa, vd, 0);
1735 
1736 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
1737 
1738 	return (0);
1739 }
1740 
1741 int
1742 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1743 {
1744 	vdev_t *vd;
1745 
1746 	spa_vdev_state_enter(spa);
1747 
1748 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1749 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1750 
1751 	if (!vd->vdev_ops->vdev_op_leaf)
1752 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1753 
1754 	/*
1755 	 * If the device isn't already offline, try to offline it.
1756 	 */
1757 	if (!vd->vdev_offline) {
1758 		/*
1759 		 * If this device's top-level vdev has a non-empty DTL,
1760 		 * don't allow the device to be offlined.
1761 		 *
1762 		 * XXX -- make this more precise by allowing the offline
1763 		 * as long as the remaining devices don't have any DTL holes.
1764 		 */
1765 		if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1766 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
1767 
1768 		/*
1769 		 * Offline this device and reopen its top-level vdev.
1770 		 * If this action results in the top-level vdev becoming
1771 		 * unusable, undo it and fail the request.
1772 		 */
1773 		vd->vdev_offline = B_TRUE;
1774 		vdev_reopen(vd->vdev_top);
1775 		if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
1776 			vd->vdev_offline = B_FALSE;
1777 			vdev_reopen(vd->vdev_top);
1778 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
1779 		}
1780 	}
1781 
1782 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
1783 
1784 	return (spa_vdev_state_exit(spa, vd, 0));
1785 }
1786 
1787 /*
1788  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
1789  * vdev_offline(), we assume the spa config is locked.  We also clear all
1790  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
1791  */
1792 void
1793 vdev_clear(spa_t *spa, vdev_t *vd)
1794 {
1795 	vdev_t *rvd = spa->spa_root_vdev;
1796 
1797 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1798 
1799 	if (vd == NULL)
1800 		vd = rvd;
1801 
1802 	vd->vdev_stat.vs_read_errors = 0;
1803 	vd->vdev_stat.vs_write_errors = 0;
1804 	vd->vdev_stat.vs_checksum_errors = 0;
1805 
1806 	for (int c = 0; c < vd->vdev_children; c++)
1807 		vdev_clear(spa, vd->vdev_child[c]);
1808 
1809 	/*
1810 	 * If we're in the FAULTED state or have experienced failed I/O, then
1811 	 * clear the persistent state and attempt to reopen the device.  We
1812 	 * also mark the vdev config dirty, so that the new faulted state is
1813 	 * written out to disk.
1814 	 */
1815 	if (vd->vdev_faulted || vd->vdev_degraded ||
1816 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
1817 
1818 		vd->vdev_faulted = vd->vdev_degraded = 0;
1819 		vd->vdev_cant_read = B_FALSE;
1820 		vd->vdev_cant_write = B_FALSE;
1821 
1822 		vdev_reopen(vd);
1823 
1824 		if (vd != rvd)
1825 			vdev_state_dirty(vd->vdev_top);
1826 
1827 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
1828 			spa_async_request(spa, SPA_ASYNC_RESILVER);
1829 
1830 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
1831 	}
1832 }
1833 
1834 boolean_t
1835 vdev_is_dead(vdev_t *vd)
1836 {
1837 	return (vd->vdev_state < VDEV_STATE_DEGRADED);
1838 }
1839 
1840 boolean_t
1841 vdev_readable(vdev_t *vd)
1842 {
1843 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
1844 }
1845 
1846 boolean_t
1847 vdev_writeable(vdev_t *vd)
1848 {
1849 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
1850 }
1851 
1852 boolean_t
1853 vdev_allocatable(vdev_t *vd)
1854 {
1855 	/*
1856 	 * We currently allow allocations from vdevs which maybe in the
1857 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
1858 	 * fails to reopen then we'll catch it later when we're holding
1859 	 * the proper locks.
1860 	 */
1861 	return (!(vdev_is_dead(vd) && vd->vdev_state != VDEV_STATE_CLOSED) &&
1862 	    !vd->vdev_cant_write);
1863 }
1864 
1865 boolean_t
1866 vdev_accessible(vdev_t *vd, zio_t *zio)
1867 {
1868 	ASSERT(zio->io_vd == vd);
1869 
1870 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
1871 		return (B_FALSE);
1872 
1873 	if (zio->io_type == ZIO_TYPE_READ)
1874 		return (!vd->vdev_cant_read);
1875 
1876 	if (zio->io_type == ZIO_TYPE_WRITE)
1877 		return (!vd->vdev_cant_write);
1878 
1879 	return (B_TRUE);
1880 }
1881 
1882 /*
1883  * Get statistics for the given vdev.
1884  */
1885 void
1886 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1887 {
1888 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1889 
1890 	mutex_enter(&vd->vdev_stat_lock);
1891 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1892 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
1893 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1894 	vs->vs_state = vd->vdev_state;
1895 	vs->vs_rsize = vdev_get_rsize(vd);
1896 	mutex_exit(&vd->vdev_stat_lock);
1897 
1898 	/*
1899 	 * If we're getting stats on the root vdev, aggregate the I/O counts
1900 	 * over all top-level vdevs (i.e. the direct children of the root).
1901 	 */
1902 	if (vd == rvd) {
1903 		for (int c = 0; c < rvd->vdev_children; c++) {
1904 			vdev_t *cvd = rvd->vdev_child[c];
1905 			vdev_stat_t *cvs = &cvd->vdev_stat;
1906 
1907 			mutex_enter(&vd->vdev_stat_lock);
1908 			for (int t = 0; t < ZIO_TYPES; t++) {
1909 				vs->vs_ops[t] += cvs->vs_ops[t];
1910 				vs->vs_bytes[t] += cvs->vs_bytes[t];
1911 			}
1912 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
1913 			mutex_exit(&vd->vdev_stat_lock);
1914 		}
1915 	}
1916 }
1917 
1918 void
1919 vdev_clear_stats(vdev_t *vd)
1920 {
1921 	mutex_enter(&vd->vdev_stat_lock);
1922 	vd->vdev_stat.vs_space = 0;
1923 	vd->vdev_stat.vs_dspace = 0;
1924 	vd->vdev_stat.vs_alloc = 0;
1925 	mutex_exit(&vd->vdev_stat_lock);
1926 }
1927 
1928 void
1929 vdev_stat_update(zio_t *zio, uint64_t psize)
1930 {
1931 	vdev_t *rvd = zio->io_spa->spa_root_vdev;
1932 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
1933 	vdev_t *pvd;
1934 	uint64_t txg = zio->io_txg;
1935 	vdev_stat_t *vs = &vd->vdev_stat;
1936 	zio_type_t type = zio->io_type;
1937 	int flags = zio->io_flags;
1938 
1939 	/*
1940 	 * If this i/o is a gang leader, it didn't do any actual work.
1941 	 */
1942 	if (zio->io_gang_tree)
1943 		return;
1944 
1945 	if (zio->io_error == 0) {
1946 		/*
1947 		 * If this is a root i/o, don't count it -- we've already
1948 		 * counted the top-level vdevs, and vdev_get_stats() will
1949 		 * aggregate them when asked.  This reduces contention on
1950 		 * the root vdev_stat_lock and implicitly handles blocks
1951 		 * that compress away to holes, for which there is no i/o.
1952 		 * (Holes never create vdev children, so all the counters
1953 		 * remain zero, which is what we want.)
1954 		 *
1955 		 * Note: this only applies to successful i/o (io_error == 0)
1956 		 * because unlike i/o counts, errors are not additive.
1957 		 * When reading a ditto block, for example, failure of
1958 		 * one top-level vdev does not imply a root-level error.
1959 		 */
1960 		if (vd == rvd)
1961 			return;
1962 
1963 		ASSERT(vd == zio->io_vd);
1964 		if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1965 			mutex_enter(&vd->vdev_stat_lock);
1966 			vs->vs_ops[type]++;
1967 			vs->vs_bytes[type] += psize;
1968 			mutex_exit(&vd->vdev_stat_lock);
1969 		}
1970 		if (flags & ZIO_FLAG_IO_REPAIR) {
1971 			ASSERT(zio->io_delegate_list == NULL);
1972 			mutex_enter(&vd->vdev_stat_lock);
1973 			if (flags & ZIO_FLAG_SCRUB_THREAD)
1974 				vs->vs_scrub_repaired += psize;
1975 			else
1976 				vs->vs_self_healed += psize;
1977 			mutex_exit(&vd->vdev_stat_lock);
1978 		}
1979 		return;
1980 	}
1981 
1982 	if (flags & ZIO_FLAG_SPECULATIVE)
1983 		return;
1984 
1985 	mutex_enter(&vd->vdev_stat_lock);
1986 	if (type == ZIO_TYPE_READ) {
1987 		if (zio->io_error == ECKSUM)
1988 			vs->vs_checksum_errors++;
1989 		else
1990 			vs->vs_read_errors++;
1991 	}
1992 	if (type == ZIO_TYPE_WRITE)
1993 		vs->vs_write_errors++;
1994 	mutex_exit(&vd->vdev_stat_lock);
1995 
1996 	if (type == ZIO_TYPE_WRITE && txg != 0 && vd->vdev_children == 0) {
1997 		if (flags & ZIO_FLAG_SCRUB_THREAD) {
1998 			ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1999 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
2000 				vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
2001 		}
2002 		if (!(flags & ZIO_FLAG_IO_REPAIR)) {
2003 			if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
2004 				return;
2005 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2006 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
2007 				vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
2008 		}
2009 	}
2010 }
2011 
2012 void
2013 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2014 {
2015 	int c;
2016 	vdev_stat_t *vs = &vd->vdev_stat;
2017 
2018 	for (c = 0; c < vd->vdev_children; c++)
2019 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2020 
2021 	mutex_enter(&vd->vdev_stat_lock);
2022 
2023 	if (type == POOL_SCRUB_NONE) {
2024 		/*
2025 		 * Update completion and end time.  Leave everything else alone
2026 		 * so we can report what happened during the previous scrub.
2027 		 */
2028 		vs->vs_scrub_complete = complete;
2029 		vs->vs_scrub_end = gethrestime_sec();
2030 	} else {
2031 		vs->vs_scrub_type = type;
2032 		vs->vs_scrub_complete = 0;
2033 		vs->vs_scrub_examined = 0;
2034 		vs->vs_scrub_repaired = 0;
2035 		vs->vs_scrub_start = gethrestime_sec();
2036 		vs->vs_scrub_end = 0;
2037 	}
2038 
2039 	mutex_exit(&vd->vdev_stat_lock);
2040 }
2041 
2042 /*
2043  * Update the in-core space usage stats for this vdev and the root vdev.
2044  */
2045 void
2046 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
2047     boolean_t update_root)
2048 {
2049 	int64_t dspace_delta = space_delta;
2050 	spa_t *spa = vd->vdev_spa;
2051 	vdev_t *rvd = spa->spa_root_vdev;
2052 
2053 	ASSERT(vd == vd->vdev_top);
2054 
2055 	/*
2056 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2057 	 * factor.  We must calculate this here and not at the root vdev
2058 	 * because the root vdev's psize-to-asize is simply the max of its
2059 	 * childrens', thus not accurate enough for us.
2060 	 */
2061 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2062 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2063 	    vd->vdev_deflate_ratio;
2064 
2065 	mutex_enter(&vd->vdev_stat_lock);
2066 	vd->vdev_stat.vs_space += space_delta;
2067 	vd->vdev_stat.vs_alloc += alloc_delta;
2068 	vd->vdev_stat.vs_dspace += dspace_delta;
2069 	mutex_exit(&vd->vdev_stat_lock);
2070 
2071 	if (update_root) {
2072 		ASSERT(rvd == vd->vdev_parent);
2073 		ASSERT(vd->vdev_ms_count != 0);
2074 
2075 		/*
2076 		 * Don't count non-normal (e.g. intent log) space as part of
2077 		 * the pool's capacity.
2078 		 */
2079 		if (vd->vdev_mg->mg_class != spa->spa_normal_class)
2080 			return;
2081 
2082 		mutex_enter(&rvd->vdev_stat_lock);
2083 		rvd->vdev_stat.vs_space += space_delta;
2084 		rvd->vdev_stat.vs_alloc += alloc_delta;
2085 		rvd->vdev_stat.vs_dspace += dspace_delta;
2086 		mutex_exit(&rvd->vdev_stat_lock);
2087 	}
2088 }
2089 
2090 /*
2091  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2092  * so that it will be written out next time the vdev configuration is synced.
2093  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2094  */
2095 void
2096 vdev_config_dirty(vdev_t *vd)
2097 {
2098 	spa_t *spa = vd->vdev_spa;
2099 	vdev_t *rvd = spa->spa_root_vdev;
2100 	int c;
2101 
2102 	/*
2103 	 * If this is an aux vdev (as with l2cache devices), then we update the
2104 	 * vdev config manually and set the sync flag.
2105 	 */
2106 	if (vd->vdev_aux != NULL) {
2107 		spa_aux_vdev_t *sav = vd->vdev_aux;
2108 		nvlist_t **aux;
2109 		uint_t naux;
2110 
2111 		for (c = 0; c < sav->sav_count; c++) {
2112 			if (sav->sav_vdevs[c] == vd)
2113 				break;
2114 		}
2115 
2116 		if (c == sav->sav_count) {
2117 			/*
2118 			 * We're being removed.  There's nothing more to do.
2119 			 */
2120 			ASSERT(sav->sav_sync == B_TRUE);
2121 			return;
2122 		}
2123 
2124 		sav->sav_sync = B_TRUE;
2125 
2126 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
2127 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) == 0);
2128 
2129 		ASSERT(c < naux);
2130 
2131 		/*
2132 		 * Setting the nvlist in the middle if the array is a little
2133 		 * sketchy, but it will work.
2134 		 */
2135 		nvlist_free(aux[c]);
2136 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
2137 
2138 		return;
2139 	}
2140 
2141 	/*
2142 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
2143 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
2144 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
2145 	 * so this is sufficient to ensure mutual exclusion.
2146 	 */
2147 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2148 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2149 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
2150 
2151 	if (vd == rvd) {
2152 		for (c = 0; c < rvd->vdev_children; c++)
2153 			vdev_config_dirty(rvd->vdev_child[c]);
2154 	} else {
2155 		ASSERT(vd == vd->vdev_top);
2156 
2157 		if (!list_link_active(&vd->vdev_config_dirty_node))
2158 			list_insert_head(&spa->spa_config_dirty_list, vd);
2159 	}
2160 }
2161 
2162 void
2163 vdev_config_clean(vdev_t *vd)
2164 {
2165 	spa_t *spa = vd->vdev_spa;
2166 
2167 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2168 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2169 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
2170 
2171 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
2172 	list_remove(&spa->spa_config_dirty_list, vd);
2173 }
2174 
2175 /*
2176  * Mark a top-level vdev's state as dirty, so that the next pass of
2177  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
2178  * the state changes from larger config changes because they require
2179  * much less locking, and are often needed for administrative actions.
2180  */
2181 void
2182 vdev_state_dirty(vdev_t *vd)
2183 {
2184 	spa_t *spa = vd->vdev_spa;
2185 
2186 	ASSERT(vd == vd->vdev_top);
2187 
2188 	/*
2189 	 * The state list is protected by the SCL_STATE lock.  The caller
2190 	 * must either hold SCL_STATE as writer, or must be the sync thread
2191 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
2192 	 * so this is sufficient to ensure mutual exclusion.
2193 	 */
2194 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2195 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2196 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2197 
2198 	if (!list_link_active(&vd->vdev_state_dirty_node))
2199 		list_insert_head(&spa->spa_state_dirty_list, vd);
2200 }
2201 
2202 void
2203 vdev_state_clean(vdev_t *vd)
2204 {
2205 	spa_t *spa = vd->vdev_spa;
2206 
2207 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2208 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2209 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2210 
2211 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
2212 	list_remove(&spa->spa_state_dirty_list, vd);
2213 }
2214 
2215 /*
2216  * Propagate vdev state up from children to parent.
2217  */
2218 void
2219 vdev_propagate_state(vdev_t *vd)
2220 {
2221 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2222 	int degraded = 0, faulted = 0;
2223 	int corrupted = 0;
2224 	int c;
2225 	vdev_t *child;
2226 
2227 	if (vd->vdev_children > 0) {
2228 		for (c = 0; c < vd->vdev_children; c++) {
2229 			child = vd->vdev_child[c];
2230 
2231 			if (!vdev_readable(child) ||
2232 			    (!vdev_writeable(child) && (spa_mode & FWRITE))) {
2233 				/*
2234 				 * Root special: if there is a top-level log
2235 				 * device, treat the root vdev as if it were
2236 				 * degraded.
2237 				 */
2238 				if (child->vdev_islog && vd == rvd)
2239 					degraded++;
2240 				else
2241 					faulted++;
2242 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
2243 				degraded++;
2244 			}
2245 
2246 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
2247 				corrupted++;
2248 		}
2249 
2250 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
2251 
2252 		/*
2253 		 * Root special: if there is a top-level vdev that cannot be
2254 		 * opened due to corrupted metadata, then propagate the root
2255 		 * vdev's aux state as 'corrupt' rather than 'insufficient
2256 		 * replicas'.
2257 		 */
2258 		if (corrupted && vd == rvd &&
2259 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
2260 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
2261 			    VDEV_AUX_CORRUPT_DATA);
2262 	}
2263 
2264 	if (vd->vdev_parent)
2265 		vdev_propagate_state(vd->vdev_parent);
2266 }
2267 
2268 /*
2269  * Set a vdev's state.  If this is during an open, we don't update the parent
2270  * state, because we're in the process of opening children depth-first.
2271  * Otherwise, we propagate the change to the parent.
2272  *
2273  * If this routine places a device in a faulted state, an appropriate ereport is
2274  * generated.
2275  */
2276 void
2277 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2278 {
2279 	uint64_t save_state;
2280 	spa_t *spa = vd->vdev_spa;
2281 
2282 	if (state == vd->vdev_state) {
2283 		vd->vdev_stat.vs_aux = aux;
2284 		return;
2285 	}
2286 
2287 	save_state = vd->vdev_state;
2288 
2289 	vd->vdev_state = state;
2290 	vd->vdev_stat.vs_aux = aux;
2291 
2292 	/*
2293 	 * If we are setting the vdev state to anything but an open state, then
2294 	 * always close the underlying device.  Otherwise, we keep accessible
2295 	 * but invalid devices open forever.  We don't call vdev_close() itself,
2296 	 * because that implies some extra checks (offline, etc) that we don't
2297 	 * want here.  This is limited to leaf devices, because otherwise
2298 	 * closing the device will affect other children.
2299 	 */
2300 	if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
2301 		vd->vdev_ops->vdev_op_close(vd);
2302 
2303 	if (vd->vdev_removed &&
2304 	    state == VDEV_STATE_CANT_OPEN &&
2305 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
2306 		/*
2307 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
2308 		 * device was previously marked removed and someone attempted to
2309 		 * reopen it.  If this failed due to a nonexistent device, then
2310 		 * keep the device in the REMOVED state.  We also let this be if
2311 		 * it is one of our special test online cases, which is only
2312 		 * attempting to online the device and shouldn't generate an FMA
2313 		 * fault.
2314 		 */
2315 		vd->vdev_state = VDEV_STATE_REMOVED;
2316 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2317 	} else if (state == VDEV_STATE_REMOVED) {
2318 		/*
2319 		 * Indicate to the ZFS DE that this device has been removed, and
2320 		 * any recent errors should be ignored.
2321 		 */
2322 		zfs_post_remove(spa, vd);
2323 		vd->vdev_removed = B_TRUE;
2324 	} else if (state == VDEV_STATE_CANT_OPEN) {
2325 		/*
2326 		 * If we fail to open a vdev during an import, we mark it as
2327 		 * "not available", which signifies that it was never there to
2328 		 * begin with.  Failure to open such a device is not considered
2329 		 * an error.
2330 		 */
2331 		if (spa->spa_load_state == SPA_LOAD_IMPORT &&
2332 		    !spa->spa_import_faulted &&
2333 		    vd->vdev_ops->vdev_op_leaf)
2334 			vd->vdev_not_present = 1;
2335 
2336 		/*
2337 		 * Post the appropriate ereport.  If the 'prevstate' field is
2338 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
2339 		 * that this is part of a vdev_reopen().  In this case, we don't
2340 		 * want to post the ereport if the device was already in the
2341 		 * CANT_OPEN state beforehand.
2342 		 *
2343 		 * If the 'checkremove' flag is set, then this is an attempt to
2344 		 * online the device in response to an insertion event.  If we
2345 		 * hit this case, then we have detected an insertion event for a
2346 		 * faulted or offline device that wasn't in the removed state.
2347 		 * In this scenario, we don't post an ereport because we are
2348 		 * about to replace the device, or attempt an online with
2349 		 * vdev_forcefault, which will generate the fault for us.
2350 		 */
2351 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
2352 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
2353 		    vd != spa->spa_root_vdev) {
2354 			const char *class;
2355 
2356 			switch (aux) {
2357 			case VDEV_AUX_OPEN_FAILED:
2358 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
2359 				break;
2360 			case VDEV_AUX_CORRUPT_DATA:
2361 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
2362 				break;
2363 			case VDEV_AUX_NO_REPLICAS:
2364 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
2365 				break;
2366 			case VDEV_AUX_BAD_GUID_SUM:
2367 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
2368 				break;
2369 			case VDEV_AUX_TOO_SMALL:
2370 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
2371 				break;
2372 			case VDEV_AUX_BAD_LABEL:
2373 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
2374 				break;
2375 			case VDEV_AUX_IO_FAILURE:
2376 				class = FM_EREPORT_ZFS_IO_FAILURE;
2377 				break;
2378 			default:
2379 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
2380 			}
2381 
2382 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
2383 		}
2384 
2385 		/* Erase any notion of persistent removed state */
2386 		vd->vdev_removed = B_FALSE;
2387 	} else {
2388 		vd->vdev_removed = B_FALSE;
2389 	}
2390 
2391 	if (!isopen)
2392 		vdev_propagate_state(vd);
2393 }
2394 
2395 /*
2396  * Check the vdev configuration to ensure that it's capable of supporting
2397  * a root pool. Currently, we do not support RAID-Z or partial configuration.
2398  * In addition, only a single top-level vdev is allowed and none of the leaves
2399  * can be wholedisks.
2400  */
2401 boolean_t
2402 vdev_is_bootable(vdev_t *vd)
2403 {
2404 	int c;
2405 
2406 	if (!vd->vdev_ops->vdev_op_leaf) {
2407 		char *vdev_type = vd->vdev_ops->vdev_op_type;
2408 
2409 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
2410 		    vd->vdev_children > 1) {
2411 			return (B_FALSE);
2412 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
2413 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
2414 			return (B_FALSE);
2415 		}
2416 	} else if (vd->vdev_wholedisk == 1) {
2417 		return (B_FALSE);
2418 	}
2419 
2420 	for (c = 0; c < vd->vdev_children; c++) {
2421 		if (!vdev_is_bootable(vd->vdev_child[c]))
2422 			return (B_FALSE);
2423 	}
2424 	return (B_TRUE);
2425 }
2426