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