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