xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 48bbca816818409505a6e214d0911fda44e622e3)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
26  * Copyright (c) 2014 Integros [integros.com]
27  * Copyright 2016 Toomas Soome <tsoome@me.com>
28  */
29 
30 #include <sys/zfs_context.h>
31 #include <sys/fm/fs/zfs.h>
32 #include <sys/spa.h>
33 #include <sys/spa_impl.h>
34 #include <sys/dmu.h>
35 #include <sys/dmu_tx.h>
36 #include <sys/vdev_impl.h>
37 #include <sys/uberblock_impl.h>
38 #include <sys/metaslab.h>
39 #include <sys/metaslab_impl.h>
40 #include <sys/space_map.h>
41 #include <sys/space_reftree.h>
42 #include <sys/zio.h>
43 #include <sys/zap.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/arc.h>
46 #include <sys/zil.h>
47 #include <sys/dsl_scan.h>
48 
49 /*
50  * Virtual device management.
51  */
52 
53 static vdev_ops_t *vdev_ops_table[] = {
54 	&vdev_root_ops,
55 	&vdev_raidz_ops,
56 	&vdev_mirror_ops,
57 	&vdev_replacing_ops,
58 	&vdev_spare_ops,
59 	&vdev_disk_ops,
60 	&vdev_file_ops,
61 	&vdev_missing_ops,
62 	&vdev_hole_ops,
63 	NULL
64 };
65 
66 /* maximum scrub/resilver I/O queue per leaf vdev */
67 int zfs_scrub_limit = 10;
68 
69 /*
70  * When a vdev is added, it will be divided into approximately (but no
71  * more than) this number of metaslabs.
72  */
73 int metaslabs_per_vdev = 200;
74 
75 /*
76  * Given a vdev type, return the appropriate ops vector.
77  */
78 static vdev_ops_t *
79 vdev_getops(const char *type)
80 {
81 	vdev_ops_t *ops, **opspp;
82 
83 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
84 		if (strcmp(ops->vdev_op_type, type) == 0)
85 			break;
86 
87 	return (ops);
88 }
89 
90 /*
91  * Default asize function: return the MAX of psize with the asize of
92  * all children.  This is what's used by anything other than RAID-Z.
93  */
94 uint64_t
95 vdev_default_asize(vdev_t *vd, uint64_t psize)
96 {
97 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
98 	uint64_t csize;
99 
100 	for (int c = 0; c < vd->vdev_children; c++) {
101 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
102 		asize = MAX(asize, csize);
103 	}
104 
105 	return (asize);
106 }
107 
108 /*
109  * Get the minimum allocatable size. We define the allocatable size as
110  * the vdev's asize rounded to the nearest metaslab. This allows us to
111  * replace or attach devices which don't have the same physical size but
112  * can still satisfy the same number of allocations.
113  */
114 uint64_t
115 vdev_get_min_asize(vdev_t *vd)
116 {
117 	vdev_t *pvd = vd->vdev_parent;
118 
119 	/*
120 	 * If our parent is NULL (inactive spare or cache) or is the root,
121 	 * just return our own asize.
122 	 */
123 	if (pvd == NULL)
124 		return (vd->vdev_asize);
125 
126 	/*
127 	 * The top-level vdev just returns the allocatable size rounded
128 	 * to the nearest metaslab.
129 	 */
130 	if (vd == vd->vdev_top)
131 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
132 
133 	/*
134 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
135 	 * so each child must provide at least 1/Nth of its asize.
136 	 */
137 	if (pvd->vdev_ops == &vdev_raidz_ops)
138 		return (pvd->vdev_min_asize / pvd->vdev_children);
139 
140 	return (pvd->vdev_min_asize);
141 }
142 
143 void
144 vdev_set_min_asize(vdev_t *vd)
145 {
146 	vd->vdev_min_asize = vdev_get_min_asize(vd);
147 
148 	for (int c = 0; c < vd->vdev_children; c++)
149 		vdev_set_min_asize(vd->vdev_child[c]);
150 }
151 
152 vdev_t *
153 vdev_lookup_top(spa_t *spa, uint64_t vdev)
154 {
155 	vdev_t *rvd = spa->spa_root_vdev;
156 
157 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
158 
159 	if (vdev < rvd->vdev_children) {
160 		ASSERT(rvd->vdev_child[vdev] != NULL);
161 		return (rvd->vdev_child[vdev]);
162 	}
163 
164 	return (NULL);
165 }
166 
167 vdev_t *
168 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
169 {
170 	vdev_t *mvd;
171 
172 	if (vd->vdev_guid == guid)
173 		return (vd);
174 
175 	for (int c = 0; c < vd->vdev_children; c++)
176 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
177 		    NULL)
178 			return (mvd);
179 
180 	return (NULL);
181 }
182 
183 static int
184 vdev_count_leaves_impl(vdev_t *vd)
185 {
186 	int n = 0;
187 
188 	if (vd->vdev_ops->vdev_op_leaf)
189 		return (1);
190 
191 	for (int c = 0; c < vd->vdev_children; c++)
192 		n += vdev_count_leaves_impl(vd->vdev_child[c]);
193 
194 	return (n);
195 }
196 
197 int
198 vdev_count_leaves(spa_t *spa)
199 {
200 	return (vdev_count_leaves_impl(spa->spa_root_vdev));
201 }
202 
203 void
204 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
205 {
206 	size_t oldsize, newsize;
207 	uint64_t id = cvd->vdev_id;
208 	vdev_t **newchild;
209 	spa_t *spa = cvd->vdev_spa;
210 
211 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
212 	ASSERT(cvd->vdev_parent == NULL);
213 
214 	cvd->vdev_parent = pvd;
215 
216 	if (pvd == NULL)
217 		return;
218 
219 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
220 
221 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
222 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
223 	newsize = pvd->vdev_children * sizeof (vdev_t *);
224 
225 	newchild = kmem_zalloc(newsize, KM_SLEEP);
226 	if (pvd->vdev_child != NULL) {
227 		bcopy(pvd->vdev_child, newchild, oldsize);
228 		kmem_free(pvd->vdev_child, oldsize);
229 	}
230 
231 	pvd->vdev_child = newchild;
232 	pvd->vdev_child[id] = cvd;
233 
234 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
235 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
236 
237 	/*
238 	 * Walk up all ancestors to update guid sum.
239 	 */
240 	for (; pvd != NULL; pvd = pvd->vdev_parent)
241 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
242 }
243 
244 void
245 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
246 {
247 	int c;
248 	uint_t id = cvd->vdev_id;
249 
250 	ASSERT(cvd->vdev_parent == pvd);
251 
252 	if (pvd == NULL)
253 		return;
254 
255 	ASSERT(id < pvd->vdev_children);
256 	ASSERT(pvd->vdev_child[id] == cvd);
257 
258 	pvd->vdev_child[id] = NULL;
259 	cvd->vdev_parent = NULL;
260 
261 	for (c = 0; c < pvd->vdev_children; c++)
262 		if (pvd->vdev_child[c])
263 			break;
264 
265 	if (c == pvd->vdev_children) {
266 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
267 		pvd->vdev_child = NULL;
268 		pvd->vdev_children = 0;
269 	}
270 
271 	/*
272 	 * Walk up all ancestors to update guid sum.
273 	 */
274 	for (; pvd != NULL; pvd = pvd->vdev_parent)
275 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
276 }
277 
278 /*
279  * Remove any holes in the child array.
280  */
281 void
282 vdev_compact_children(vdev_t *pvd)
283 {
284 	vdev_t **newchild, *cvd;
285 	int oldc = pvd->vdev_children;
286 	int newc;
287 
288 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
289 
290 	for (int c = newc = 0; c < oldc; c++)
291 		if (pvd->vdev_child[c])
292 			newc++;
293 
294 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
295 
296 	for (int c = newc = 0; c < oldc; c++) {
297 		if ((cvd = pvd->vdev_child[c]) != NULL) {
298 			newchild[newc] = cvd;
299 			cvd->vdev_id = newc++;
300 		}
301 	}
302 
303 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
304 	pvd->vdev_child = newchild;
305 	pvd->vdev_children = newc;
306 }
307 
308 /*
309  * Allocate and minimally initialize a vdev_t.
310  */
311 vdev_t *
312 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
313 {
314 	vdev_t *vd;
315 
316 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
317 
318 	if (spa->spa_root_vdev == NULL) {
319 		ASSERT(ops == &vdev_root_ops);
320 		spa->spa_root_vdev = vd;
321 		spa->spa_load_guid = spa_generate_guid(NULL);
322 	}
323 
324 	if (guid == 0 && ops != &vdev_hole_ops) {
325 		if (spa->spa_root_vdev == vd) {
326 			/*
327 			 * The root vdev's guid will also be the pool guid,
328 			 * which must be unique among all pools.
329 			 */
330 			guid = spa_generate_guid(NULL);
331 		} else {
332 			/*
333 			 * Any other vdev's guid must be unique within the pool.
334 			 */
335 			guid = spa_generate_guid(spa);
336 		}
337 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
338 	}
339 
340 	vd->vdev_spa = spa;
341 	vd->vdev_id = id;
342 	vd->vdev_guid = guid;
343 	vd->vdev_guid_sum = guid;
344 	vd->vdev_ops = ops;
345 	vd->vdev_state = VDEV_STATE_CLOSED;
346 	vd->vdev_ishole = (ops == &vdev_hole_ops);
347 
348 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
349 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
350 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
351 	mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
352 	for (int t = 0; t < DTL_TYPES; t++) {
353 		vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
354 		    &vd->vdev_dtl_lock);
355 	}
356 	txg_list_create(&vd->vdev_ms_list,
357 	    offsetof(struct metaslab, ms_txg_node));
358 	txg_list_create(&vd->vdev_dtl_list,
359 	    offsetof(struct vdev, vdev_dtl_node));
360 	vd->vdev_stat.vs_timestamp = gethrtime();
361 	vdev_queue_init(vd);
362 	vdev_cache_init(vd);
363 
364 	return (vd);
365 }
366 
367 /*
368  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
369  * creating a new vdev or loading an existing one - the behavior is slightly
370  * different for each case.
371  */
372 int
373 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
374     int alloctype)
375 {
376 	vdev_ops_t *ops;
377 	char *type;
378 	uint64_t guid = 0, islog, nparity;
379 	vdev_t *vd;
380 
381 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
382 
383 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
384 		return (SET_ERROR(EINVAL));
385 
386 	if ((ops = vdev_getops(type)) == NULL)
387 		return (SET_ERROR(EINVAL));
388 
389 	/*
390 	 * If this is a load, get the vdev guid from the nvlist.
391 	 * Otherwise, vdev_alloc_common() will generate one for us.
392 	 */
393 	if (alloctype == VDEV_ALLOC_LOAD) {
394 		uint64_t label_id;
395 
396 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
397 		    label_id != id)
398 			return (SET_ERROR(EINVAL));
399 
400 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
401 			return (SET_ERROR(EINVAL));
402 	} else if (alloctype == VDEV_ALLOC_SPARE) {
403 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
404 			return (SET_ERROR(EINVAL));
405 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
406 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
407 			return (SET_ERROR(EINVAL));
408 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
409 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
410 			return (SET_ERROR(EINVAL));
411 	}
412 
413 	/*
414 	 * The first allocated vdev must be of type 'root'.
415 	 */
416 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
417 		return (SET_ERROR(EINVAL));
418 
419 	/*
420 	 * Determine whether we're a log vdev.
421 	 */
422 	islog = 0;
423 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
424 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
425 		return (SET_ERROR(ENOTSUP));
426 
427 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
428 		return (SET_ERROR(ENOTSUP));
429 
430 	/*
431 	 * Set the nparity property for RAID-Z vdevs.
432 	 */
433 	nparity = -1ULL;
434 	if (ops == &vdev_raidz_ops) {
435 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
436 		    &nparity) == 0) {
437 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
438 				return (SET_ERROR(EINVAL));
439 			/*
440 			 * Previous versions could only support 1 or 2 parity
441 			 * device.
442 			 */
443 			if (nparity > 1 &&
444 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
445 				return (SET_ERROR(ENOTSUP));
446 			if (nparity > 2 &&
447 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
448 				return (SET_ERROR(ENOTSUP));
449 		} else {
450 			/*
451 			 * We require the parity to be specified for SPAs that
452 			 * support multiple parity levels.
453 			 */
454 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
455 				return (SET_ERROR(EINVAL));
456 			/*
457 			 * Otherwise, we default to 1 parity device for RAID-Z.
458 			 */
459 			nparity = 1;
460 		}
461 	} else {
462 		nparity = 0;
463 	}
464 	ASSERT(nparity != -1ULL);
465 
466 	vd = vdev_alloc_common(spa, id, guid, ops);
467 
468 	vd->vdev_islog = islog;
469 	vd->vdev_nparity = nparity;
470 
471 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
472 		vd->vdev_path = spa_strdup(vd->vdev_path);
473 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
474 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
475 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
476 	    &vd->vdev_physpath) == 0)
477 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
478 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
479 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
480 
481 	/*
482 	 * Set the whole_disk property.  If it's not specified, leave the value
483 	 * as -1.
484 	 */
485 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
486 	    &vd->vdev_wholedisk) != 0)
487 		vd->vdev_wholedisk = -1ULL;
488 
489 	/*
490 	 * Look for the 'not present' flag.  This will only be set if the device
491 	 * was not present at the time of import.
492 	 */
493 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
494 	    &vd->vdev_not_present);
495 
496 	/*
497 	 * Get the alignment requirement.
498 	 */
499 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
500 
501 	/*
502 	 * Retrieve the vdev creation time.
503 	 */
504 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
505 	    &vd->vdev_crtxg);
506 
507 	/*
508 	 * If we're a top-level vdev, try to load the allocation parameters.
509 	 */
510 	if (parent && !parent->vdev_parent &&
511 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
512 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
513 		    &vd->vdev_ms_array);
514 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
515 		    &vd->vdev_ms_shift);
516 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
517 		    &vd->vdev_asize);
518 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
519 		    &vd->vdev_removing);
520 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
521 		    &vd->vdev_top_zap);
522 	} else {
523 		ASSERT0(vd->vdev_top_zap);
524 	}
525 
526 	if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
527 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
528 		    alloctype == VDEV_ALLOC_ADD ||
529 		    alloctype == VDEV_ALLOC_SPLIT ||
530 		    alloctype == VDEV_ALLOC_ROOTPOOL);
531 		vd->vdev_mg = metaslab_group_create(islog ?
532 		    spa_log_class(spa) : spa_normal_class(spa), vd);
533 	}
534 
535 	if (vd->vdev_ops->vdev_op_leaf &&
536 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
537 		(void) nvlist_lookup_uint64(nv,
538 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
539 	} else {
540 		ASSERT0(vd->vdev_leaf_zap);
541 	}
542 
543 	/*
544 	 * If we're a leaf vdev, try to load the DTL object and other state.
545 	 */
546 
547 	if (vd->vdev_ops->vdev_op_leaf &&
548 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
549 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
550 		if (alloctype == VDEV_ALLOC_LOAD) {
551 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
552 			    &vd->vdev_dtl_object);
553 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
554 			    &vd->vdev_unspare);
555 		}
556 
557 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
558 			uint64_t spare = 0;
559 
560 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
561 			    &spare) == 0 && spare)
562 				spa_spare_add(vd);
563 		}
564 
565 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
566 		    &vd->vdev_offline);
567 
568 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
569 		    &vd->vdev_resilver_txg);
570 
571 		/*
572 		 * When importing a pool, we want to ignore the persistent fault
573 		 * state, as the diagnosis made on another system may not be
574 		 * valid in the current context.  Local vdevs will
575 		 * remain in the faulted state.
576 		 */
577 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
578 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
579 			    &vd->vdev_faulted);
580 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
581 			    &vd->vdev_degraded);
582 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
583 			    &vd->vdev_removed);
584 
585 			if (vd->vdev_faulted || vd->vdev_degraded) {
586 				char *aux;
587 
588 				vd->vdev_label_aux =
589 				    VDEV_AUX_ERR_EXCEEDED;
590 				if (nvlist_lookup_string(nv,
591 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
592 				    strcmp(aux, "external") == 0)
593 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
594 			}
595 		}
596 	}
597 
598 	/*
599 	 * Add ourselves to the parent's list of children.
600 	 */
601 	vdev_add_child(parent, vd);
602 
603 	*vdp = vd;
604 
605 	return (0);
606 }
607 
608 void
609 vdev_free(vdev_t *vd)
610 {
611 	spa_t *spa = vd->vdev_spa;
612 
613 	/*
614 	 * vdev_free() implies closing the vdev first.  This is simpler than
615 	 * trying to ensure complicated semantics for all callers.
616 	 */
617 	vdev_close(vd);
618 
619 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
620 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
621 
622 	/*
623 	 * Free all children.
624 	 */
625 	for (int c = 0; c < vd->vdev_children; c++)
626 		vdev_free(vd->vdev_child[c]);
627 
628 	ASSERT(vd->vdev_child == NULL);
629 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
630 
631 	/*
632 	 * Discard allocation state.
633 	 */
634 	if (vd->vdev_mg != NULL) {
635 		vdev_metaslab_fini(vd);
636 		metaslab_group_destroy(vd->vdev_mg);
637 	}
638 
639 	ASSERT0(vd->vdev_stat.vs_space);
640 	ASSERT0(vd->vdev_stat.vs_dspace);
641 	ASSERT0(vd->vdev_stat.vs_alloc);
642 
643 	/*
644 	 * Remove this vdev from its parent's child list.
645 	 */
646 	vdev_remove_child(vd->vdev_parent, vd);
647 
648 	ASSERT(vd->vdev_parent == NULL);
649 
650 	/*
651 	 * Clean up vdev structure.
652 	 */
653 	vdev_queue_fini(vd);
654 	vdev_cache_fini(vd);
655 
656 	if (vd->vdev_path)
657 		spa_strfree(vd->vdev_path);
658 	if (vd->vdev_devid)
659 		spa_strfree(vd->vdev_devid);
660 	if (vd->vdev_physpath)
661 		spa_strfree(vd->vdev_physpath);
662 	if (vd->vdev_fru)
663 		spa_strfree(vd->vdev_fru);
664 
665 	if (vd->vdev_isspare)
666 		spa_spare_remove(vd);
667 	if (vd->vdev_isl2cache)
668 		spa_l2cache_remove(vd);
669 
670 	txg_list_destroy(&vd->vdev_ms_list);
671 	txg_list_destroy(&vd->vdev_dtl_list);
672 
673 	mutex_enter(&vd->vdev_dtl_lock);
674 	space_map_close(vd->vdev_dtl_sm);
675 	for (int t = 0; t < DTL_TYPES; t++) {
676 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
677 		range_tree_destroy(vd->vdev_dtl[t]);
678 	}
679 	mutex_exit(&vd->vdev_dtl_lock);
680 
681 	mutex_destroy(&vd->vdev_queue_lock);
682 	mutex_destroy(&vd->vdev_dtl_lock);
683 	mutex_destroy(&vd->vdev_stat_lock);
684 	mutex_destroy(&vd->vdev_probe_lock);
685 
686 	if (vd == spa->spa_root_vdev)
687 		spa->spa_root_vdev = NULL;
688 
689 	kmem_free(vd, sizeof (vdev_t));
690 }
691 
692 /*
693  * Transfer top-level vdev state from svd to tvd.
694  */
695 static void
696 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
697 {
698 	spa_t *spa = svd->vdev_spa;
699 	metaslab_t *msp;
700 	vdev_t *vd;
701 	int t;
702 
703 	ASSERT(tvd == tvd->vdev_top);
704 
705 	tvd->vdev_ms_array = svd->vdev_ms_array;
706 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
707 	tvd->vdev_ms_count = svd->vdev_ms_count;
708 	tvd->vdev_top_zap = svd->vdev_top_zap;
709 
710 	svd->vdev_ms_array = 0;
711 	svd->vdev_ms_shift = 0;
712 	svd->vdev_ms_count = 0;
713 	svd->vdev_top_zap = 0;
714 
715 	if (tvd->vdev_mg)
716 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
717 	tvd->vdev_mg = svd->vdev_mg;
718 	tvd->vdev_ms = svd->vdev_ms;
719 
720 	svd->vdev_mg = NULL;
721 	svd->vdev_ms = NULL;
722 
723 	if (tvd->vdev_mg != NULL)
724 		tvd->vdev_mg->mg_vd = tvd;
725 
726 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
727 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
728 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
729 
730 	svd->vdev_stat.vs_alloc = 0;
731 	svd->vdev_stat.vs_space = 0;
732 	svd->vdev_stat.vs_dspace = 0;
733 
734 	for (t = 0; t < TXG_SIZE; t++) {
735 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
736 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
737 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
738 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
739 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
740 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
741 	}
742 
743 	if (list_link_active(&svd->vdev_config_dirty_node)) {
744 		vdev_config_clean(svd);
745 		vdev_config_dirty(tvd);
746 	}
747 
748 	if (list_link_active(&svd->vdev_state_dirty_node)) {
749 		vdev_state_clean(svd);
750 		vdev_state_dirty(tvd);
751 	}
752 
753 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
754 	svd->vdev_deflate_ratio = 0;
755 
756 	tvd->vdev_islog = svd->vdev_islog;
757 	svd->vdev_islog = 0;
758 }
759 
760 static void
761 vdev_top_update(vdev_t *tvd, vdev_t *vd)
762 {
763 	if (vd == NULL)
764 		return;
765 
766 	vd->vdev_top = tvd;
767 
768 	for (int c = 0; c < vd->vdev_children; c++)
769 		vdev_top_update(tvd, vd->vdev_child[c]);
770 }
771 
772 /*
773  * Add a mirror/replacing vdev above an existing vdev.
774  */
775 vdev_t *
776 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
777 {
778 	spa_t *spa = cvd->vdev_spa;
779 	vdev_t *pvd = cvd->vdev_parent;
780 	vdev_t *mvd;
781 
782 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
783 
784 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
785 
786 	mvd->vdev_asize = cvd->vdev_asize;
787 	mvd->vdev_min_asize = cvd->vdev_min_asize;
788 	mvd->vdev_max_asize = cvd->vdev_max_asize;
789 	mvd->vdev_ashift = cvd->vdev_ashift;
790 	mvd->vdev_state = cvd->vdev_state;
791 	mvd->vdev_crtxg = cvd->vdev_crtxg;
792 
793 	vdev_remove_child(pvd, cvd);
794 	vdev_add_child(pvd, mvd);
795 	cvd->vdev_id = mvd->vdev_children;
796 	vdev_add_child(mvd, cvd);
797 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
798 
799 	if (mvd == mvd->vdev_top)
800 		vdev_top_transfer(cvd, mvd);
801 
802 	return (mvd);
803 }
804 
805 /*
806  * Remove a 1-way mirror/replacing vdev from the tree.
807  */
808 void
809 vdev_remove_parent(vdev_t *cvd)
810 {
811 	vdev_t *mvd = cvd->vdev_parent;
812 	vdev_t *pvd = mvd->vdev_parent;
813 
814 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
815 
816 	ASSERT(mvd->vdev_children == 1);
817 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
818 	    mvd->vdev_ops == &vdev_replacing_ops ||
819 	    mvd->vdev_ops == &vdev_spare_ops);
820 	cvd->vdev_ashift = mvd->vdev_ashift;
821 
822 	vdev_remove_child(mvd, cvd);
823 	vdev_remove_child(pvd, mvd);
824 
825 	/*
826 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
827 	 * Otherwise, we could have detached an offline device, and when we
828 	 * go to import the pool we'll think we have two top-level vdevs,
829 	 * instead of a different version of the same top-level vdev.
830 	 */
831 	if (mvd->vdev_top == mvd) {
832 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
833 		cvd->vdev_orig_guid = cvd->vdev_guid;
834 		cvd->vdev_guid += guid_delta;
835 		cvd->vdev_guid_sum += guid_delta;
836 	}
837 	cvd->vdev_id = mvd->vdev_id;
838 	vdev_add_child(pvd, cvd);
839 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
840 
841 	if (cvd == cvd->vdev_top)
842 		vdev_top_transfer(mvd, cvd);
843 
844 	ASSERT(mvd->vdev_children == 0);
845 	vdev_free(mvd);
846 }
847 
848 int
849 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
850 {
851 	spa_t *spa = vd->vdev_spa;
852 	objset_t *mos = spa->spa_meta_objset;
853 	uint64_t m;
854 	uint64_t oldc = vd->vdev_ms_count;
855 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
856 	metaslab_t **mspp;
857 	int error;
858 
859 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
860 
861 	/*
862 	 * This vdev is not being allocated from yet or is a hole.
863 	 */
864 	if (vd->vdev_ms_shift == 0)
865 		return (0);
866 
867 	ASSERT(!vd->vdev_ishole);
868 
869 	/*
870 	 * Compute the raidz-deflation ratio.  Note, we hard-code
871 	 * in 128k (1 << 17) because it is the "typical" blocksize.
872 	 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
873 	 * otherwise it would inconsistently account for existing bp's.
874 	 */
875 	vd->vdev_deflate_ratio = (1 << 17) /
876 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
877 
878 	ASSERT(oldc <= newc);
879 
880 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
881 
882 	if (oldc != 0) {
883 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
884 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
885 	}
886 
887 	vd->vdev_ms = mspp;
888 	vd->vdev_ms_count = newc;
889 
890 	for (m = oldc; m < newc; m++) {
891 		uint64_t object = 0;
892 
893 		if (txg == 0) {
894 			error = dmu_read(mos, vd->vdev_ms_array,
895 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
896 			    DMU_READ_PREFETCH);
897 			if (error)
898 				return (error);
899 		}
900 
901 		error = metaslab_init(vd->vdev_mg, m, object, txg,
902 		    &(vd->vdev_ms[m]));
903 		if (error)
904 			return (error);
905 	}
906 
907 	if (txg == 0)
908 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
909 
910 	/*
911 	 * If the vdev is being removed we don't activate
912 	 * the metaslabs since we want to ensure that no new
913 	 * allocations are performed on this device.
914 	 */
915 	if (oldc == 0 && !vd->vdev_removing)
916 		metaslab_group_activate(vd->vdev_mg);
917 
918 	if (txg == 0)
919 		spa_config_exit(spa, SCL_ALLOC, FTAG);
920 
921 	return (0);
922 }
923 
924 void
925 vdev_metaslab_fini(vdev_t *vd)
926 {
927 	uint64_t m;
928 	uint64_t count = vd->vdev_ms_count;
929 
930 	if (vd->vdev_ms != NULL) {
931 		metaslab_group_passivate(vd->vdev_mg);
932 		for (m = 0; m < count; m++) {
933 			metaslab_t *msp = vd->vdev_ms[m];
934 
935 			if (msp != NULL)
936 				metaslab_fini(msp);
937 		}
938 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
939 		vd->vdev_ms = NULL;
940 	}
941 }
942 
943 typedef struct vdev_probe_stats {
944 	boolean_t	vps_readable;
945 	boolean_t	vps_writeable;
946 	int		vps_flags;
947 } vdev_probe_stats_t;
948 
949 static void
950 vdev_probe_done(zio_t *zio)
951 {
952 	spa_t *spa = zio->io_spa;
953 	vdev_t *vd = zio->io_vd;
954 	vdev_probe_stats_t *vps = zio->io_private;
955 
956 	ASSERT(vd->vdev_probe_zio != NULL);
957 
958 	if (zio->io_type == ZIO_TYPE_READ) {
959 		if (zio->io_error == 0)
960 			vps->vps_readable = 1;
961 		if (zio->io_error == 0 && spa_writeable(spa)) {
962 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
963 			    zio->io_offset, zio->io_size, zio->io_data,
964 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
965 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
966 		} else {
967 			zio_buf_free(zio->io_data, zio->io_size);
968 		}
969 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
970 		if (zio->io_error == 0)
971 			vps->vps_writeable = 1;
972 		zio_buf_free(zio->io_data, zio->io_size);
973 	} else if (zio->io_type == ZIO_TYPE_NULL) {
974 		zio_t *pio;
975 
976 		vd->vdev_cant_read |= !vps->vps_readable;
977 		vd->vdev_cant_write |= !vps->vps_writeable;
978 
979 		if (vdev_readable(vd) &&
980 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
981 			zio->io_error = 0;
982 		} else {
983 			ASSERT(zio->io_error != 0);
984 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
985 			    spa, vd, NULL, 0, 0);
986 			zio->io_error = SET_ERROR(ENXIO);
987 		}
988 
989 		mutex_enter(&vd->vdev_probe_lock);
990 		ASSERT(vd->vdev_probe_zio == zio);
991 		vd->vdev_probe_zio = NULL;
992 		mutex_exit(&vd->vdev_probe_lock);
993 
994 		zio_link_t *zl = NULL;
995 		while ((pio = zio_walk_parents(zio, &zl)) != NULL)
996 			if (!vdev_accessible(vd, pio))
997 				pio->io_error = SET_ERROR(ENXIO);
998 
999 		kmem_free(vps, sizeof (*vps));
1000 	}
1001 }
1002 
1003 /*
1004  * Determine whether this device is accessible.
1005  *
1006  * Read and write to several known locations: the pad regions of each
1007  * vdev label but the first, which we leave alone in case it contains
1008  * a VTOC.
1009  */
1010 zio_t *
1011 vdev_probe(vdev_t *vd, zio_t *zio)
1012 {
1013 	spa_t *spa = vd->vdev_spa;
1014 	vdev_probe_stats_t *vps = NULL;
1015 	zio_t *pio;
1016 
1017 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1018 
1019 	/*
1020 	 * Don't probe the probe.
1021 	 */
1022 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1023 		return (NULL);
1024 
1025 	/*
1026 	 * To prevent 'probe storms' when a device fails, we create
1027 	 * just one probe i/o at a time.  All zios that want to probe
1028 	 * this vdev will become parents of the probe io.
1029 	 */
1030 	mutex_enter(&vd->vdev_probe_lock);
1031 
1032 	if ((pio = vd->vdev_probe_zio) == NULL) {
1033 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1034 
1035 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1036 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1037 		    ZIO_FLAG_TRYHARD;
1038 
1039 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1040 			/*
1041 			 * vdev_cant_read and vdev_cant_write can only
1042 			 * transition from TRUE to FALSE when we have the
1043 			 * SCL_ZIO lock as writer; otherwise they can only
1044 			 * transition from FALSE to TRUE.  This ensures that
1045 			 * any zio looking at these values can assume that
1046 			 * failures persist for the life of the I/O.  That's
1047 			 * important because when a device has intermittent
1048 			 * connectivity problems, we want to ensure that
1049 			 * they're ascribed to the device (ENXIO) and not
1050 			 * the zio (EIO).
1051 			 *
1052 			 * Since we hold SCL_ZIO as writer here, clear both
1053 			 * values so the probe can reevaluate from first
1054 			 * principles.
1055 			 */
1056 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1057 			vd->vdev_cant_read = B_FALSE;
1058 			vd->vdev_cant_write = B_FALSE;
1059 		}
1060 
1061 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1062 		    vdev_probe_done, vps,
1063 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1064 
1065 		/*
1066 		 * We can't change the vdev state in this context, so we
1067 		 * kick off an async task to do it on our behalf.
1068 		 */
1069 		if (zio != NULL) {
1070 			vd->vdev_probe_wanted = B_TRUE;
1071 			spa_async_request(spa, SPA_ASYNC_PROBE);
1072 		}
1073 	}
1074 
1075 	if (zio != NULL)
1076 		zio_add_child(zio, pio);
1077 
1078 	mutex_exit(&vd->vdev_probe_lock);
1079 
1080 	if (vps == NULL) {
1081 		ASSERT(zio != NULL);
1082 		return (NULL);
1083 	}
1084 
1085 	for (int l = 1; l < VDEV_LABELS; l++) {
1086 		zio_nowait(zio_read_phys(pio, vd,
1087 		    vdev_label_offset(vd->vdev_psize, l,
1088 		    offsetof(vdev_label_t, vl_pad2)),
1089 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1090 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1091 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1092 	}
1093 
1094 	if (zio == NULL)
1095 		return (pio);
1096 
1097 	zio_nowait(pio);
1098 	return (NULL);
1099 }
1100 
1101 static void
1102 vdev_open_child(void *arg)
1103 {
1104 	vdev_t *vd = arg;
1105 
1106 	vd->vdev_open_thread = curthread;
1107 	vd->vdev_open_error = vdev_open(vd);
1108 	vd->vdev_open_thread = NULL;
1109 }
1110 
1111 boolean_t
1112 vdev_uses_zvols(vdev_t *vd)
1113 {
1114 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1115 	    strlen(ZVOL_DIR)) == 0)
1116 		return (B_TRUE);
1117 	for (int c = 0; c < vd->vdev_children; c++)
1118 		if (vdev_uses_zvols(vd->vdev_child[c]))
1119 			return (B_TRUE);
1120 	return (B_FALSE);
1121 }
1122 
1123 void
1124 vdev_open_children(vdev_t *vd)
1125 {
1126 	taskq_t *tq;
1127 	int children = vd->vdev_children;
1128 
1129 	/*
1130 	 * in order to handle pools on top of zvols, do the opens
1131 	 * in a single thread so that the same thread holds the
1132 	 * spa_namespace_lock
1133 	 */
1134 	if (vdev_uses_zvols(vd)) {
1135 		for (int c = 0; c < children; c++)
1136 			vd->vdev_child[c]->vdev_open_error =
1137 			    vdev_open(vd->vdev_child[c]);
1138 		return;
1139 	}
1140 	tq = taskq_create("vdev_open", children, minclsyspri,
1141 	    children, children, TASKQ_PREPOPULATE);
1142 
1143 	for (int c = 0; c < children; c++)
1144 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1145 		    TQ_SLEEP) != NULL);
1146 
1147 	taskq_destroy(tq);
1148 }
1149 
1150 /*
1151  * Prepare a virtual device for access.
1152  */
1153 int
1154 vdev_open(vdev_t *vd)
1155 {
1156 	spa_t *spa = vd->vdev_spa;
1157 	int error;
1158 	uint64_t osize = 0;
1159 	uint64_t max_osize = 0;
1160 	uint64_t asize, max_asize, psize;
1161 	uint64_t ashift = 0;
1162 
1163 	ASSERT(vd->vdev_open_thread == curthread ||
1164 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1165 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1166 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1167 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1168 
1169 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1170 	vd->vdev_cant_read = B_FALSE;
1171 	vd->vdev_cant_write = B_FALSE;
1172 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1173 
1174 	/*
1175 	 * If this vdev is not removed, check its fault status.  If it's
1176 	 * faulted, bail out of the open.
1177 	 */
1178 	if (!vd->vdev_removed && vd->vdev_faulted) {
1179 		ASSERT(vd->vdev_children == 0);
1180 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1181 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1182 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1183 		    vd->vdev_label_aux);
1184 		return (SET_ERROR(ENXIO));
1185 	} else if (vd->vdev_offline) {
1186 		ASSERT(vd->vdev_children == 0);
1187 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1188 		return (SET_ERROR(ENXIO));
1189 	}
1190 
1191 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1192 
1193 	/*
1194 	 * Reset the vdev_reopening flag so that we actually close
1195 	 * the vdev on error.
1196 	 */
1197 	vd->vdev_reopening = B_FALSE;
1198 	if (zio_injection_enabled && error == 0)
1199 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1200 
1201 	if (error) {
1202 		if (vd->vdev_removed &&
1203 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1204 			vd->vdev_removed = B_FALSE;
1205 
1206 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1207 		    vd->vdev_stat.vs_aux);
1208 		return (error);
1209 	}
1210 
1211 	vd->vdev_removed = B_FALSE;
1212 
1213 	/*
1214 	 * Recheck the faulted flag now that we have confirmed that
1215 	 * the vdev is accessible.  If we're faulted, bail.
1216 	 */
1217 	if (vd->vdev_faulted) {
1218 		ASSERT(vd->vdev_children == 0);
1219 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1220 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1221 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1222 		    vd->vdev_label_aux);
1223 		return (SET_ERROR(ENXIO));
1224 	}
1225 
1226 	if (vd->vdev_degraded) {
1227 		ASSERT(vd->vdev_children == 0);
1228 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1229 		    VDEV_AUX_ERR_EXCEEDED);
1230 	} else {
1231 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1232 	}
1233 
1234 	/*
1235 	 * For hole or missing vdevs we just return success.
1236 	 */
1237 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1238 		return (0);
1239 
1240 	for (int c = 0; c < vd->vdev_children; c++) {
1241 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1242 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1243 			    VDEV_AUX_NONE);
1244 			break;
1245 		}
1246 	}
1247 
1248 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1249 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1250 
1251 	if (vd->vdev_children == 0) {
1252 		if (osize < SPA_MINDEVSIZE) {
1253 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1254 			    VDEV_AUX_TOO_SMALL);
1255 			return (SET_ERROR(EOVERFLOW));
1256 		}
1257 		psize = osize;
1258 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1259 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1260 		    VDEV_LABEL_END_SIZE);
1261 	} else {
1262 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1263 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1264 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1265 			    VDEV_AUX_TOO_SMALL);
1266 			return (SET_ERROR(EOVERFLOW));
1267 		}
1268 		psize = 0;
1269 		asize = osize;
1270 		max_asize = max_osize;
1271 	}
1272 
1273 	vd->vdev_psize = psize;
1274 
1275 	/*
1276 	 * Make sure the allocatable size hasn't shrunk.
1277 	 */
1278 	if (asize < vd->vdev_min_asize) {
1279 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1280 		    VDEV_AUX_BAD_LABEL);
1281 		return (SET_ERROR(EINVAL));
1282 	}
1283 
1284 	if (vd->vdev_asize == 0) {
1285 		/*
1286 		 * This is the first-ever open, so use the computed values.
1287 		 * For testing purposes, a higher ashift can be requested.
1288 		 */
1289 		vd->vdev_asize = asize;
1290 		vd->vdev_max_asize = max_asize;
1291 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1292 	} else {
1293 		/*
1294 		 * Detect if the alignment requirement has increased.
1295 		 * We don't want to make the pool unavailable, just
1296 		 * issue a warning instead.
1297 		 */
1298 		if (ashift > vd->vdev_top->vdev_ashift &&
1299 		    vd->vdev_ops->vdev_op_leaf) {
1300 			cmn_err(CE_WARN,
1301 			    "Disk, '%s', has a block alignment that is "
1302 			    "larger than the pool's alignment\n",
1303 			    vd->vdev_path);
1304 		}
1305 		vd->vdev_max_asize = max_asize;
1306 	}
1307 
1308 	/*
1309 	 * If all children are healthy and the asize has increased,
1310 	 * then we've experienced dynamic LUN growth.  If automatic
1311 	 * expansion is enabled then use the additional space.
1312 	 */
1313 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1314 	    (vd->vdev_expanding || spa->spa_autoexpand))
1315 		vd->vdev_asize = asize;
1316 
1317 	vdev_set_min_asize(vd);
1318 
1319 	/*
1320 	 * Ensure we can issue some IO before declaring the
1321 	 * vdev open for business.
1322 	 */
1323 	if (vd->vdev_ops->vdev_op_leaf &&
1324 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1325 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1326 		    VDEV_AUX_ERR_EXCEEDED);
1327 		return (error);
1328 	}
1329 
1330 	/*
1331 	 * Track the min and max ashift values for normal data devices.
1332 	 */
1333 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1334 	    !vd->vdev_islog && vd->vdev_aux == NULL) {
1335 		if (vd->vdev_ashift > spa->spa_max_ashift)
1336 			spa->spa_max_ashift = vd->vdev_ashift;
1337 		if (vd->vdev_ashift < spa->spa_min_ashift)
1338 			spa->spa_min_ashift = vd->vdev_ashift;
1339 	}
1340 
1341 	/*
1342 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1343 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
1344 	 * since this would just restart the scrub we are already doing.
1345 	 */
1346 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1347 	    vdev_resilver_needed(vd, NULL, NULL))
1348 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1349 
1350 	return (0);
1351 }
1352 
1353 /*
1354  * Called once the vdevs are all opened, this routine validates the label
1355  * contents.  This needs to be done before vdev_load() so that we don't
1356  * inadvertently do repair I/Os to the wrong device.
1357  *
1358  * If 'strict' is false ignore the spa guid check. This is necessary because
1359  * if the machine crashed during a re-guid the new guid might have been written
1360  * to all of the vdev labels, but not the cached config. The strict check
1361  * will be performed when the pool is opened again using the mos config.
1362  *
1363  * This function will only return failure if one of the vdevs indicates that it
1364  * has since been destroyed or exported.  This is only possible if
1365  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1366  * will be updated but the function will return 0.
1367  */
1368 int
1369 vdev_validate(vdev_t *vd, boolean_t strict)
1370 {
1371 	spa_t *spa = vd->vdev_spa;
1372 	nvlist_t *label;
1373 	uint64_t guid = 0, top_guid;
1374 	uint64_t state;
1375 
1376 	for (int c = 0; c < vd->vdev_children; c++)
1377 		if (vdev_validate(vd->vdev_child[c], strict) != 0)
1378 			return (SET_ERROR(EBADF));
1379 
1380 	/*
1381 	 * If the device has already failed, or was marked offline, don't do
1382 	 * any further validation.  Otherwise, label I/O will fail and we will
1383 	 * overwrite the previous state.
1384 	 */
1385 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1386 		uint64_t aux_guid = 0;
1387 		nvlist_t *nvl;
1388 		uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1389 		    spa_last_synced_txg(spa) : -1ULL;
1390 
1391 		if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1392 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1393 			    VDEV_AUX_BAD_LABEL);
1394 			return (0);
1395 		}
1396 
1397 		/*
1398 		 * Determine if this vdev has been split off into another
1399 		 * pool.  If so, then refuse to open it.
1400 		 */
1401 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1402 		    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1403 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1404 			    VDEV_AUX_SPLIT_POOL);
1405 			nvlist_free(label);
1406 			return (0);
1407 		}
1408 
1409 		if (strict && (nvlist_lookup_uint64(label,
1410 		    ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1411 		    guid != spa_guid(spa))) {
1412 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1413 			    VDEV_AUX_CORRUPT_DATA);
1414 			nvlist_free(label);
1415 			return (0);
1416 		}
1417 
1418 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1419 		    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1420 		    &aux_guid) != 0)
1421 			aux_guid = 0;
1422 
1423 		/*
1424 		 * If this vdev just became a top-level vdev because its
1425 		 * sibling was detached, it will have adopted the parent's
1426 		 * vdev guid -- but the label may or may not be on disk yet.
1427 		 * Fortunately, either version of the label will have the
1428 		 * same top guid, so if we're a top-level vdev, we can
1429 		 * safely compare to that instead.
1430 		 *
1431 		 * If we split this vdev off instead, then we also check the
1432 		 * original pool's guid.  We don't want to consider the vdev
1433 		 * corrupt if it is partway through a split operation.
1434 		 */
1435 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1436 		    &guid) != 0 ||
1437 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1438 		    &top_guid) != 0 ||
1439 		    ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1440 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1441 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1442 			    VDEV_AUX_CORRUPT_DATA);
1443 			nvlist_free(label);
1444 			return (0);
1445 		}
1446 
1447 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1448 		    &state) != 0) {
1449 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1450 			    VDEV_AUX_CORRUPT_DATA);
1451 			nvlist_free(label);
1452 			return (0);
1453 		}
1454 
1455 		nvlist_free(label);
1456 
1457 		/*
1458 		 * If this is a verbatim import, no need to check the
1459 		 * state of the pool.
1460 		 */
1461 		if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1462 		    spa_load_state(spa) == SPA_LOAD_OPEN &&
1463 		    state != POOL_STATE_ACTIVE)
1464 			return (SET_ERROR(EBADF));
1465 
1466 		/*
1467 		 * If we were able to open and validate a vdev that was
1468 		 * previously marked permanently unavailable, clear that state
1469 		 * now.
1470 		 */
1471 		if (vd->vdev_not_present)
1472 			vd->vdev_not_present = 0;
1473 	}
1474 
1475 	return (0);
1476 }
1477 
1478 /*
1479  * Close a virtual device.
1480  */
1481 void
1482 vdev_close(vdev_t *vd)
1483 {
1484 	spa_t *spa = vd->vdev_spa;
1485 	vdev_t *pvd = vd->vdev_parent;
1486 
1487 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1488 
1489 	/*
1490 	 * If our parent is reopening, then we are as well, unless we are
1491 	 * going offline.
1492 	 */
1493 	if (pvd != NULL && pvd->vdev_reopening)
1494 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1495 
1496 	vd->vdev_ops->vdev_op_close(vd);
1497 
1498 	vdev_cache_purge(vd);
1499 
1500 	/*
1501 	 * We record the previous state before we close it, so that if we are
1502 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1503 	 * it's still faulted.
1504 	 */
1505 	vd->vdev_prevstate = vd->vdev_state;
1506 
1507 	if (vd->vdev_offline)
1508 		vd->vdev_state = VDEV_STATE_OFFLINE;
1509 	else
1510 		vd->vdev_state = VDEV_STATE_CLOSED;
1511 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1512 }
1513 
1514 void
1515 vdev_hold(vdev_t *vd)
1516 {
1517 	spa_t *spa = vd->vdev_spa;
1518 
1519 	ASSERT(spa_is_root(spa));
1520 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1521 		return;
1522 
1523 	for (int c = 0; c < vd->vdev_children; c++)
1524 		vdev_hold(vd->vdev_child[c]);
1525 
1526 	if (vd->vdev_ops->vdev_op_leaf)
1527 		vd->vdev_ops->vdev_op_hold(vd);
1528 }
1529 
1530 void
1531 vdev_rele(vdev_t *vd)
1532 {
1533 	spa_t *spa = vd->vdev_spa;
1534 
1535 	ASSERT(spa_is_root(spa));
1536 	for (int c = 0; c < vd->vdev_children; c++)
1537 		vdev_rele(vd->vdev_child[c]);
1538 
1539 	if (vd->vdev_ops->vdev_op_leaf)
1540 		vd->vdev_ops->vdev_op_rele(vd);
1541 }
1542 
1543 /*
1544  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1545  * reopen leaf vdevs which had previously been opened as they might deadlock
1546  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1547  * If the leaf has never been opened then open it, as usual.
1548  */
1549 void
1550 vdev_reopen(vdev_t *vd)
1551 {
1552 	spa_t *spa = vd->vdev_spa;
1553 
1554 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1555 
1556 	/* set the reopening flag unless we're taking the vdev offline */
1557 	vd->vdev_reopening = !vd->vdev_offline;
1558 	vdev_close(vd);
1559 	(void) vdev_open(vd);
1560 
1561 	/*
1562 	 * Call vdev_validate() here to make sure we have the same device.
1563 	 * Otherwise, a device with an invalid label could be successfully
1564 	 * opened in response to vdev_reopen().
1565 	 */
1566 	if (vd->vdev_aux) {
1567 		(void) vdev_validate_aux(vd);
1568 		if (vdev_readable(vd) && vdev_writeable(vd) &&
1569 		    vd->vdev_aux == &spa->spa_l2cache &&
1570 		    !l2arc_vdev_present(vd))
1571 			l2arc_add_vdev(spa, vd);
1572 	} else {
1573 		(void) vdev_validate(vd, B_TRUE);
1574 	}
1575 
1576 	/*
1577 	 * Reassess parent vdev's health.
1578 	 */
1579 	vdev_propagate_state(vd);
1580 }
1581 
1582 int
1583 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1584 {
1585 	int error;
1586 
1587 	/*
1588 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1589 	 * For a create, however, we want to fail the request if
1590 	 * there are any components we can't open.
1591 	 */
1592 	error = vdev_open(vd);
1593 
1594 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1595 		vdev_close(vd);
1596 		return (error ? error : ENXIO);
1597 	}
1598 
1599 	/*
1600 	 * Recursively load DTLs and initialize all labels.
1601 	 */
1602 	if ((error = vdev_dtl_load(vd)) != 0 ||
1603 	    (error = vdev_label_init(vd, txg, isreplacing ?
1604 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1605 		vdev_close(vd);
1606 		return (error);
1607 	}
1608 
1609 	return (0);
1610 }
1611 
1612 void
1613 vdev_metaslab_set_size(vdev_t *vd)
1614 {
1615 	/*
1616 	 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1617 	 */
1618 	vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
1619 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1620 }
1621 
1622 void
1623 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1624 {
1625 	ASSERT(vd == vd->vdev_top);
1626 	ASSERT(!vd->vdev_ishole);
1627 	ASSERT(ISP2(flags));
1628 	ASSERT(spa_writeable(vd->vdev_spa));
1629 
1630 	if (flags & VDD_METASLAB)
1631 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1632 
1633 	if (flags & VDD_DTL)
1634 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1635 
1636 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1637 }
1638 
1639 void
1640 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
1641 {
1642 	for (int c = 0; c < vd->vdev_children; c++)
1643 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
1644 
1645 	if (vd->vdev_ops->vdev_op_leaf)
1646 		vdev_dirty(vd->vdev_top, flags, vd, txg);
1647 }
1648 
1649 /*
1650  * DTLs.
1651  *
1652  * A vdev's DTL (dirty time log) is the set of transaction groups for which
1653  * the vdev has less than perfect replication.  There are four kinds of DTL:
1654  *
1655  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1656  *
1657  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1658  *
1659  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1660  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1661  *	txgs that was scrubbed.
1662  *
1663  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1664  *	persistent errors or just some device being offline.
1665  *	Unlike the other three, the DTL_OUTAGE map is not generally
1666  *	maintained; it's only computed when needed, typically to
1667  *	determine whether a device can be detached.
1668  *
1669  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1670  * either has the data or it doesn't.
1671  *
1672  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1673  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1674  * if any child is less than fully replicated, then so is its parent.
1675  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1676  * comprising only those txgs which appear in 'maxfaults' or more children;
1677  * those are the txgs we don't have enough replication to read.  For example,
1678  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1679  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1680  * two child DTL_MISSING maps.
1681  *
1682  * It should be clear from the above that to compute the DTLs and outage maps
1683  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1684  * Therefore, that is all we keep on disk.  When loading the pool, or after
1685  * a configuration change, we generate all other DTLs from first principles.
1686  */
1687 void
1688 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1689 {
1690 	range_tree_t *rt = vd->vdev_dtl[t];
1691 
1692 	ASSERT(t < DTL_TYPES);
1693 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1694 	ASSERT(spa_writeable(vd->vdev_spa));
1695 
1696 	mutex_enter(rt->rt_lock);
1697 	if (!range_tree_contains(rt, txg, size))
1698 		range_tree_add(rt, txg, size);
1699 	mutex_exit(rt->rt_lock);
1700 }
1701 
1702 boolean_t
1703 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1704 {
1705 	range_tree_t *rt = vd->vdev_dtl[t];
1706 	boolean_t dirty = B_FALSE;
1707 
1708 	ASSERT(t < DTL_TYPES);
1709 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1710 
1711 	mutex_enter(rt->rt_lock);
1712 	if (range_tree_space(rt) != 0)
1713 		dirty = range_tree_contains(rt, txg, size);
1714 	mutex_exit(rt->rt_lock);
1715 
1716 	return (dirty);
1717 }
1718 
1719 boolean_t
1720 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
1721 {
1722 	range_tree_t *rt = vd->vdev_dtl[t];
1723 	boolean_t empty;
1724 
1725 	mutex_enter(rt->rt_lock);
1726 	empty = (range_tree_space(rt) == 0);
1727 	mutex_exit(rt->rt_lock);
1728 
1729 	return (empty);
1730 }
1731 
1732 /*
1733  * Returns the lowest txg in the DTL range.
1734  */
1735 static uint64_t
1736 vdev_dtl_min(vdev_t *vd)
1737 {
1738 	range_seg_t *rs;
1739 
1740 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1741 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1742 	ASSERT0(vd->vdev_children);
1743 
1744 	rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1745 	return (rs->rs_start - 1);
1746 }
1747 
1748 /*
1749  * Returns the highest txg in the DTL.
1750  */
1751 static uint64_t
1752 vdev_dtl_max(vdev_t *vd)
1753 {
1754 	range_seg_t *rs;
1755 
1756 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1757 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1758 	ASSERT0(vd->vdev_children);
1759 
1760 	rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1761 	return (rs->rs_end);
1762 }
1763 
1764 /*
1765  * Determine if a resilvering vdev should remove any DTL entries from
1766  * its range. If the vdev was resilvering for the entire duration of the
1767  * scan then it should excise that range from its DTLs. Otherwise, this
1768  * vdev is considered partially resilvered and should leave its DTL
1769  * entries intact. The comment in vdev_dtl_reassess() describes how we
1770  * excise the DTLs.
1771  */
1772 static boolean_t
1773 vdev_dtl_should_excise(vdev_t *vd)
1774 {
1775 	spa_t *spa = vd->vdev_spa;
1776 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1777 
1778 	ASSERT0(scn->scn_phys.scn_errors);
1779 	ASSERT0(vd->vdev_children);
1780 
1781 	if (vd->vdev_resilver_txg == 0 ||
1782 	    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1783 		return (B_TRUE);
1784 
1785 	/*
1786 	 * When a resilver is initiated the scan will assign the scn_max_txg
1787 	 * value to the highest txg value that exists in all DTLs. If this
1788 	 * device's max DTL is not part of this scan (i.e. it is not in
1789 	 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1790 	 * for excision.
1791 	 */
1792 	if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1793 		ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1794 		ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1795 		ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1796 		return (B_TRUE);
1797 	}
1798 	return (B_FALSE);
1799 }
1800 
1801 /*
1802  * Reassess DTLs after a config change or scrub completion.
1803  */
1804 void
1805 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1806 {
1807 	spa_t *spa = vd->vdev_spa;
1808 	avl_tree_t reftree;
1809 	int minref;
1810 
1811 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1812 
1813 	for (int c = 0; c < vd->vdev_children; c++)
1814 		vdev_dtl_reassess(vd->vdev_child[c], txg,
1815 		    scrub_txg, scrub_done);
1816 
1817 	if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
1818 		return;
1819 
1820 	if (vd->vdev_ops->vdev_op_leaf) {
1821 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1822 
1823 		mutex_enter(&vd->vdev_dtl_lock);
1824 
1825 		/*
1826 		 * If we've completed a scan cleanly then determine
1827 		 * if this vdev should remove any DTLs. We only want to
1828 		 * excise regions on vdevs that were available during
1829 		 * the entire duration of this scan.
1830 		 */
1831 		if (scrub_txg != 0 &&
1832 		    (spa->spa_scrub_started ||
1833 		    (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1834 		    vdev_dtl_should_excise(vd)) {
1835 			/*
1836 			 * We completed a scrub up to scrub_txg.  If we
1837 			 * did it without rebooting, then the scrub dtl
1838 			 * will be valid, so excise the old region and
1839 			 * fold in the scrub dtl.  Otherwise, leave the
1840 			 * dtl as-is if there was an error.
1841 			 *
1842 			 * There's little trick here: to excise the beginning
1843 			 * of the DTL_MISSING map, we put it into a reference
1844 			 * tree and then add a segment with refcnt -1 that
1845 			 * covers the range [0, scrub_txg).  This means
1846 			 * that each txg in that range has refcnt -1 or 0.
1847 			 * We then add DTL_SCRUB with a refcnt of 2, so that
1848 			 * entries in the range [0, scrub_txg) will have a
1849 			 * positive refcnt -- either 1 or 2.  We then convert
1850 			 * the reference tree into the new DTL_MISSING map.
1851 			 */
1852 			space_reftree_create(&reftree);
1853 			space_reftree_add_map(&reftree,
1854 			    vd->vdev_dtl[DTL_MISSING], 1);
1855 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
1856 			space_reftree_add_map(&reftree,
1857 			    vd->vdev_dtl[DTL_SCRUB], 2);
1858 			space_reftree_generate_map(&reftree,
1859 			    vd->vdev_dtl[DTL_MISSING], 1);
1860 			space_reftree_destroy(&reftree);
1861 		}
1862 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
1863 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1864 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
1865 		if (scrub_done)
1866 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
1867 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
1868 		if (!vdev_readable(vd))
1869 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
1870 		else
1871 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1872 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
1873 
1874 		/*
1875 		 * If the vdev was resilvering and no longer has any
1876 		 * DTLs then reset its resilvering flag.
1877 		 */
1878 		if (vd->vdev_resilver_txg != 0 &&
1879 		    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
1880 		    range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
1881 			vd->vdev_resilver_txg = 0;
1882 
1883 		mutex_exit(&vd->vdev_dtl_lock);
1884 
1885 		if (txg != 0)
1886 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1887 		return;
1888 	}
1889 
1890 	mutex_enter(&vd->vdev_dtl_lock);
1891 	for (int t = 0; t < DTL_TYPES; t++) {
1892 		/* account for child's outage in parent's missing map */
1893 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
1894 		if (t == DTL_SCRUB)
1895 			continue;			/* leaf vdevs only */
1896 		if (t == DTL_PARTIAL)
1897 			minref = 1;			/* i.e. non-zero */
1898 		else if (vd->vdev_nparity != 0)
1899 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
1900 		else
1901 			minref = vd->vdev_children;	/* any kind of mirror */
1902 		space_reftree_create(&reftree);
1903 		for (int c = 0; c < vd->vdev_children; c++) {
1904 			vdev_t *cvd = vd->vdev_child[c];
1905 			mutex_enter(&cvd->vdev_dtl_lock);
1906 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
1907 			mutex_exit(&cvd->vdev_dtl_lock);
1908 		}
1909 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
1910 		space_reftree_destroy(&reftree);
1911 	}
1912 	mutex_exit(&vd->vdev_dtl_lock);
1913 }
1914 
1915 int
1916 vdev_dtl_load(vdev_t *vd)
1917 {
1918 	spa_t *spa = vd->vdev_spa;
1919 	objset_t *mos = spa->spa_meta_objset;
1920 	int error = 0;
1921 
1922 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
1923 		ASSERT(!vd->vdev_ishole);
1924 
1925 		error = space_map_open(&vd->vdev_dtl_sm, mos,
1926 		    vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
1927 		if (error)
1928 			return (error);
1929 		ASSERT(vd->vdev_dtl_sm != NULL);
1930 
1931 		mutex_enter(&vd->vdev_dtl_lock);
1932 
1933 		/*
1934 		 * Now that we've opened the space_map we need to update
1935 		 * the in-core DTL.
1936 		 */
1937 		space_map_update(vd->vdev_dtl_sm);
1938 
1939 		error = space_map_load(vd->vdev_dtl_sm,
1940 		    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
1941 		mutex_exit(&vd->vdev_dtl_lock);
1942 
1943 		return (error);
1944 	}
1945 
1946 	for (int c = 0; c < vd->vdev_children; c++) {
1947 		error = vdev_dtl_load(vd->vdev_child[c]);
1948 		if (error != 0)
1949 			break;
1950 	}
1951 
1952 	return (error);
1953 }
1954 
1955 void
1956 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
1957 {
1958 	spa_t *spa = vd->vdev_spa;
1959 
1960 	VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
1961 	VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
1962 	    zapobj, tx));
1963 }
1964 
1965 uint64_t
1966 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
1967 {
1968 	spa_t *spa = vd->vdev_spa;
1969 	uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
1970 	    DMU_OT_NONE, 0, tx);
1971 
1972 	ASSERT(zap != 0);
1973 	VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
1974 	    zap, tx));
1975 
1976 	return (zap);
1977 }
1978 
1979 void
1980 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
1981 {
1982 	if (vd->vdev_ops != &vdev_hole_ops &&
1983 	    vd->vdev_ops != &vdev_missing_ops &&
1984 	    vd->vdev_ops != &vdev_root_ops &&
1985 	    !vd->vdev_top->vdev_removing) {
1986 		if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
1987 			vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
1988 		}
1989 		if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
1990 			vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
1991 		}
1992 	}
1993 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
1994 		vdev_construct_zaps(vd->vdev_child[i], tx);
1995 	}
1996 }
1997 
1998 void
1999 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2000 {
2001 	spa_t *spa = vd->vdev_spa;
2002 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2003 	objset_t *mos = spa->spa_meta_objset;
2004 	range_tree_t *rtsync;
2005 	kmutex_t rtlock;
2006 	dmu_tx_t *tx;
2007 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
2008 
2009 	ASSERT(!vd->vdev_ishole);
2010 	ASSERT(vd->vdev_ops->vdev_op_leaf);
2011 
2012 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2013 
2014 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2015 		mutex_enter(&vd->vdev_dtl_lock);
2016 		space_map_free(vd->vdev_dtl_sm, tx);
2017 		space_map_close(vd->vdev_dtl_sm);
2018 		vd->vdev_dtl_sm = NULL;
2019 		mutex_exit(&vd->vdev_dtl_lock);
2020 
2021 		/*
2022 		 * We only destroy the leaf ZAP for detached leaves or for
2023 		 * removed log devices. Removed data devices handle leaf ZAP
2024 		 * cleanup later, once cancellation is no longer possible.
2025 		 */
2026 		if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2027 		    vd->vdev_top->vdev_islog)) {
2028 			vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2029 			vd->vdev_leaf_zap = 0;
2030 		}
2031 
2032 		dmu_tx_commit(tx);
2033 		return;
2034 	}
2035 
2036 	if (vd->vdev_dtl_sm == NULL) {
2037 		uint64_t new_object;
2038 
2039 		new_object = space_map_alloc(mos, tx);
2040 		VERIFY3U(new_object, !=, 0);
2041 
2042 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2043 		    0, -1ULL, 0, &vd->vdev_dtl_lock));
2044 		ASSERT(vd->vdev_dtl_sm != NULL);
2045 	}
2046 
2047 	mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
2048 
2049 	rtsync = range_tree_create(NULL, NULL, &rtlock);
2050 
2051 	mutex_enter(&rtlock);
2052 
2053 	mutex_enter(&vd->vdev_dtl_lock);
2054 	range_tree_walk(rt, range_tree_add, rtsync);
2055 	mutex_exit(&vd->vdev_dtl_lock);
2056 
2057 	space_map_truncate(vd->vdev_dtl_sm, tx);
2058 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
2059 	range_tree_vacate(rtsync, NULL, NULL);
2060 
2061 	range_tree_destroy(rtsync);
2062 
2063 	mutex_exit(&rtlock);
2064 	mutex_destroy(&rtlock);
2065 
2066 	/*
2067 	 * If the object for the space map has changed then dirty
2068 	 * the top level so that we update the config.
2069 	 */
2070 	if (object != space_map_object(vd->vdev_dtl_sm)) {
2071 		zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2072 		    "new object %llu", txg, spa_name(spa), object,
2073 		    space_map_object(vd->vdev_dtl_sm));
2074 		vdev_config_dirty(vd->vdev_top);
2075 	}
2076 
2077 	dmu_tx_commit(tx);
2078 
2079 	mutex_enter(&vd->vdev_dtl_lock);
2080 	space_map_update(vd->vdev_dtl_sm);
2081 	mutex_exit(&vd->vdev_dtl_lock);
2082 }
2083 
2084 /*
2085  * Determine whether the specified vdev can be offlined/detached/removed
2086  * without losing data.
2087  */
2088 boolean_t
2089 vdev_dtl_required(vdev_t *vd)
2090 {
2091 	spa_t *spa = vd->vdev_spa;
2092 	vdev_t *tvd = vd->vdev_top;
2093 	uint8_t cant_read = vd->vdev_cant_read;
2094 	boolean_t required;
2095 
2096 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2097 
2098 	if (vd == spa->spa_root_vdev || vd == tvd)
2099 		return (B_TRUE);
2100 
2101 	/*
2102 	 * Temporarily mark the device as unreadable, and then determine
2103 	 * whether this results in any DTL outages in the top-level vdev.
2104 	 * If not, we can safely offline/detach/remove the device.
2105 	 */
2106 	vd->vdev_cant_read = B_TRUE;
2107 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2108 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2109 	vd->vdev_cant_read = cant_read;
2110 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2111 
2112 	if (!required && zio_injection_enabled)
2113 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2114 
2115 	return (required);
2116 }
2117 
2118 /*
2119  * Determine if resilver is needed, and if so the txg range.
2120  */
2121 boolean_t
2122 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2123 {
2124 	boolean_t needed = B_FALSE;
2125 	uint64_t thismin = UINT64_MAX;
2126 	uint64_t thismax = 0;
2127 
2128 	if (vd->vdev_children == 0) {
2129 		mutex_enter(&vd->vdev_dtl_lock);
2130 		if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
2131 		    vdev_writeable(vd)) {
2132 
2133 			thismin = vdev_dtl_min(vd);
2134 			thismax = vdev_dtl_max(vd);
2135 			needed = B_TRUE;
2136 		}
2137 		mutex_exit(&vd->vdev_dtl_lock);
2138 	} else {
2139 		for (int c = 0; c < vd->vdev_children; c++) {
2140 			vdev_t *cvd = vd->vdev_child[c];
2141 			uint64_t cmin, cmax;
2142 
2143 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2144 				thismin = MIN(thismin, cmin);
2145 				thismax = MAX(thismax, cmax);
2146 				needed = B_TRUE;
2147 			}
2148 		}
2149 	}
2150 
2151 	if (needed && minp) {
2152 		*minp = thismin;
2153 		*maxp = thismax;
2154 	}
2155 	return (needed);
2156 }
2157 
2158 void
2159 vdev_load(vdev_t *vd)
2160 {
2161 	/*
2162 	 * Recursively load all children.
2163 	 */
2164 	for (int c = 0; c < vd->vdev_children; c++)
2165 		vdev_load(vd->vdev_child[c]);
2166 
2167 	/*
2168 	 * If this is a top-level vdev, initialize its metaslabs.
2169 	 */
2170 	if (vd == vd->vdev_top && !vd->vdev_ishole &&
2171 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2172 	    vdev_metaslab_init(vd, 0) != 0))
2173 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2174 		    VDEV_AUX_CORRUPT_DATA);
2175 
2176 	/*
2177 	 * If this is a leaf vdev, load its DTL.
2178 	 */
2179 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2180 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2181 		    VDEV_AUX_CORRUPT_DATA);
2182 }
2183 
2184 /*
2185  * The special vdev case is used for hot spares and l2cache devices.  Its
2186  * sole purpose it to set the vdev state for the associated vdev.  To do this,
2187  * we make sure that we can open the underlying device, then try to read the
2188  * label, and make sure that the label is sane and that it hasn't been
2189  * repurposed to another pool.
2190  */
2191 int
2192 vdev_validate_aux(vdev_t *vd)
2193 {
2194 	nvlist_t *label;
2195 	uint64_t guid, version;
2196 	uint64_t state;
2197 
2198 	if (!vdev_readable(vd))
2199 		return (0);
2200 
2201 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2202 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2203 		    VDEV_AUX_CORRUPT_DATA);
2204 		return (-1);
2205 	}
2206 
2207 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2208 	    !SPA_VERSION_IS_SUPPORTED(version) ||
2209 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2210 	    guid != vd->vdev_guid ||
2211 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2212 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2213 		    VDEV_AUX_CORRUPT_DATA);
2214 		nvlist_free(label);
2215 		return (-1);
2216 	}
2217 
2218 	/*
2219 	 * We don't actually check the pool state here.  If it's in fact in
2220 	 * use by another pool, we update this fact on the fly when requested.
2221 	 */
2222 	nvlist_free(label);
2223 	return (0);
2224 }
2225 
2226 void
2227 vdev_remove(vdev_t *vd, uint64_t txg)
2228 {
2229 	spa_t *spa = vd->vdev_spa;
2230 	objset_t *mos = spa->spa_meta_objset;
2231 	dmu_tx_t *tx;
2232 
2233 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2234 	ASSERT(vd == vd->vdev_top);
2235 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
2236 
2237 	if (vd->vdev_ms != NULL) {
2238 		metaslab_group_t *mg = vd->vdev_mg;
2239 
2240 		metaslab_group_histogram_verify(mg);
2241 		metaslab_class_histogram_verify(mg->mg_class);
2242 
2243 		for (int m = 0; m < vd->vdev_ms_count; m++) {
2244 			metaslab_t *msp = vd->vdev_ms[m];
2245 
2246 			if (msp == NULL || msp->ms_sm == NULL)
2247 				continue;
2248 
2249 			mutex_enter(&msp->ms_lock);
2250 			/*
2251 			 * If the metaslab was not loaded when the vdev
2252 			 * was removed then the histogram accounting may
2253 			 * not be accurate. Update the histogram information
2254 			 * here so that we ensure that the metaslab group
2255 			 * and metaslab class are up-to-date.
2256 			 */
2257 			metaslab_group_histogram_remove(mg, msp);
2258 
2259 			VERIFY0(space_map_allocated(msp->ms_sm));
2260 			space_map_free(msp->ms_sm, tx);
2261 			space_map_close(msp->ms_sm);
2262 			msp->ms_sm = NULL;
2263 			mutex_exit(&msp->ms_lock);
2264 		}
2265 
2266 		metaslab_group_histogram_verify(mg);
2267 		metaslab_class_histogram_verify(mg->mg_class);
2268 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2269 			ASSERT0(mg->mg_histogram[i]);
2270 
2271 	}
2272 
2273 	if (vd->vdev_ms_array) {
2274 		(void) dmu_object_free(mos, vd->vdev_ms_array, tx);
2275 		vd->vdev_ms_array = 0;
2276 	}
2277 
2278 	if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2279 		vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2280 		vd->vdev_top_zap = 0;
2281 	}
2282 	dmu_tx_commit(tx);
2283 }
2284 
2285 void
2286 vdev_sync_done(vdev_t *vd, uint64_t txg)
2287 {
2288 	metaslab_t *msp;
2289 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2290 
2291 	ASSERT(!vd->vdev_ishole);
2292 
2293 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2294 		metaslab_sync_done(msp, txg);
2295 
2296 	if (reassess)
2297 		metaslab_sync_reassess(vd->vdev_mg);
2298 }
2299 
2300 void
2301 vdev_sync(vdev_t *vd, uint64_t txg)
2302 {
2303 	spa_t *spa = vd->vdev_spa;
2304 	vdev_t *lvd;
2305 	metaslab_t *msp;
2306 	dmu_tx_t *tx;
2307 
2308 	ASSERT(!vd->vdev_ishole);
2309 
2310 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2311 		ASSERT(vd == vd->vdev_top);
2312 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2313 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2314 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2315 		ASSERT(vd->vdev_ms_array != 0);
2316 		vdev_config_dirty(vd);
2317 		dmu_tx_commit(tx);
2318 	}
2319 
2320 	/*
2321 	 * Remove the metadata associated with this vdev once it's empty.
2322 	 */
2323 	if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
2324 		vdev_remove(vd, txg);
2325 
2326 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2327 		metaslab_sync(msp, txg);
2328 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2329 	}
2330 
2331 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2332 		vdev_dtl_sync(lvd, txg);
2333 
2334 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2335 }
2336 
2337 uint64_t
2338 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2339 {
2340 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2341 }
2342 
2343 /*
2344  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
2345  * not be opened, and no I/O is attempted.
2346  */
2347 int
2348 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2349 {
2350 	vdev_t *vd, *tvd;
2351 
2352 	spa_vdev_state_enter(spa, SCL_NONE);
2353 
2354 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2355 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2356 
2357 	if (!vd->vdev_ops->vdev_op_leaf)
2358 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2359 
2360 	tvd = vd->vdev_top;
2361 
2362 	/*
2363 	 * We don't directly use the aux state here, but if we do a
2364 	 * vdev_reopen(), we need this value to be present to remember why we
2365 	 * were faulted.
2366 	 */
2367 	vd->vdev_label_aux = aux;
2368 
2369 	/*
2370 	 * Faulted state takes precedence over degraded.
2371 	 */
2372 	vd->vdev_delayed_close = B_FALSE;
2373 	vd->vdev_faulted = 1ULL;
2374 	vd->vdev_degraded = 0ULL;
2375 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
2376 
2377 	/*
2378 	 * If this device has the only valid copy of the data, then
2379 	 * back off and simply mark the vdev as degraded instead.
2380 	 */
2381 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
2382 		vd->vdev_degraded = 1ULL;
2383 		vd->vdev_faulted = 0ULL;
2384 
2385 		/*
2386 		 * If we reopen the device and it's not dead, only then do we
2387 		 * mark it degraded.
2388 		 */
2389 		vdev_reopen(tvd);
2390 
2391 		if (vdev_readable(vd))
2392 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
2393 	}
2394 
2395 	return (spa_vdev_state_exit(spa, vd, 0));
2396 }
2397 
2398 /*
2399  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
2400  * user that something is wrong.  The vdev continues to operate as normal as far
2401  * as I/O is concerned.
2402  */
2403 int
2404 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2405 {
2406 	vdev_t *vd;
2407 
2408 	spa_vdev_state_enter(spa, SCL_NONE);
2409 
2410 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2411 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2412 
2413 	if (!vd->vdev_ops->vdev_op_leaf)
2414 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2415 
2416 	/*
2417 	 * If the vdev is already faulted, then don't do anything.
2418 	 */
2419 	if (vd->vdev_faulted || vd->vdev_degraded)
2420 		return (spa_vdev_state_exit(spa, NULL, 0));
2421 
2422 	vd->vdev_degraded = 1ULL;
2423 	if (!vdev_is_dead(vd))
2424 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2425 		    aux);
2426 
2427 	return (spa_vdev_state_exit(spa, vd, 0));
2428 }
2429 
2430 /*
2431  * Online the given vdev.
2432  *
2433  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
2434  * spare device should be detached when the device finishes resilvering.
2435  * Second, the online should be treated like a 'test' online case, so no FMA
2436  * events are generated if the device fails to open.
2437  */
2438 int
2439 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2440 {
2441 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2442 	boolean_t postevent = B_FALSE;
2443 
2444 	spa_vdev_state_enter(spa, SCL_NONE);
2445 
2446 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2447 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2448 
2449 	if (!vd->vdev_ops->vdev_op_leaf)
2450 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2451 
2452 	postevent =
2453 	    (vd->vdev_offline == B_TRUE || vd->vdev_tmpoffline == B_TRUE) ?
2454 	    B_TRUE : B_FALSE;
2455 
2456 	tvd = vd->vdev_top;
2457 	vd->vdev_offline = B_FALSE;
2458 	vd->vdev_tmpoffline = B_FALSE;
2459 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2460 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2461 
2462 	/* XXX - L2ARC 1.0 does not support expansion */
2463 	if (!vd->vdev_aux) {
2464 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2465 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2466 	}
2467 
2468 	vdev_reopen(tvd);
2469 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
2470 
2471 	if (!vd->vdev_aux) {
2472 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2473 			pvd->vdev_expanding = B_FALSE;
2474 	}
2475 
2476 	if (newstate)
2477 		*newstate = vd->vdev_state;
2478 	if ((flags & ZFS_ONLINE_UNSPARE) &&
2479 	    !vdev_is_dead(vd) && vd->vdev_parent &&
2480 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2481 	    vd->vdev_parent->vdev_child[0] == vd)
2482 		vd->vdev_unspare = B_TRUE;
2483 
2484 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2485 
2486 		/* XXX - L2ARC 1.0 does not support expansion */
2487 		if (vd->vdev_aux)
2488 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2489 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2490 	}
2491 
2492 	if (postevent)
2493 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_ONLINE);
2494 
2495 	return (spa_vdev_state_exit(spa, vd, 0));
2496 }
2497 
2498 static int
2499 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2500 {
2501 	vdev_t *vd, *tvd;
2502 	int error = 0;
2503 	uint64_t generation;
2504 	metaslab_group_t *mg;
2505 
2506 top:
2507 	spa_vdev_state_enter(spa, SCL_ALLOC);
2508 
2509 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2510 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2511 
2512 	if (!vd->vdev_ops->vdev_op_leaf)
2513 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2514 
2515 	tvd = vd->vdev_top;
2516 	mg = tvd->vdev_mg;
2517 	generation = spa->spa_config_generation + 1;
2518 
2519 	/*
2520 	 * If the device isn't already offline, try to offline it.
2521 	 */
2522 	if (!vd->vdev_offline) {
2523 		/*
2524 		 * If this device has the only valid copy of some data,
2525 		 * don't allow it to be offlined. Log devices are always
2526 		 * expendable.
2527 		 */
2528 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2529 		    vdev_dtl_required(vd))
2530 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2531 
2532 		/*
2533 		 * If the top-level is a slog and it has had allocations
2534 		 * then proceed.  We check that the vdev's metaslab group
2535 		 * is not NULL since it's possible that we may have just
2536 		 * added this vdev but not yet initialized its metaslabs.
2537 		 */
2538 		if (tvd->vdev_islog && mg != NULL) {
2539 			/*
2540 			 * Prevent any future allocations.
2541 			 */
2542 			metaslab_group_passivate(mg);
2543 			(void) spa_vdev_state_exit(spa, vd, 0);
2544 
2545 			error = spa_offline_log(spa);
2546 
2547 			spa_vdev_state_enter(spa, SCL_ALLOC);
2548 
2549 			/*
2550 			 * Check to see if the config has changed.
2551 			 */
2552 			if (error || generation != spa->spa_config_generation) {
2553 				metaslab_group_activate(mg);
2554 				if (error)
2555 					return (spa_vdev_state_exit(spa,
2556 					    vd, error));
2557 				(void) spa_vdev_state_exit(spa, vd, 0);
2558 				goto top;
2559 			}
2560 			ASSERT0(tvd->vdev_stat.vs_alloc);
2561 		}
2562 
2563 		/*
2564 		 * Offline this device and reopen its top-level vdev.
2565 		 * If the top-level vdev is a log device then just offline
2566 		 * it. Otherwise, if this action results in the top-level
2567 		 * vdev becoming unusable, undo it and fail the request.
2568 		 */
2569 		vd->vdev_offline = B_TRUE;
2570 		vdev_reopen(tvd);
2571 
2572 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2573 		    vdev_is_dead(tvd)) {
2574 			vd->vdev_offline = B_FALSE;
2575 			vdev_reopen(tvd);
2576 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2577 		}
2578 
2579 		/*
2580 		 * Add the device back into the metaslab rotor so that
2581 		 * once we online the device it's open for business.
2582 		 */
2583 		if (tvd->vdev_islog && mg != NULL)
2584 			metaslab_group_activate(mg);
2585 	}
2586 
2587 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2588 
2589 	return (spa_vdev_state_exit(spa, vd, 0));
2590 }
2591 
2592 int
2593 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2594 {
2595 	int error;
2596 
2597 	mutex_enter(&spa->spa_vdev_top_lock);
2598 	error = vdev_offline_locked(spa, guid, flags);
2599 	mutex_exit(&spa->spa_vdev_top_lock);
2600 
2601 	return (error);
2602 }
2603 
2604 /*
2605  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
2606  * vdev_offline(), we assume the spa config is locked.  We also clear all
2607  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
2608  */
2609 void
2610 vdev_clear(spa_t *spa, vdev_t *vd)
2611 {
2612 	vdev_t *rvd = spa->spa_root_vdev;
2613 
2614 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2615 
2616 	if (vd == NULL)
2617 		vd = rvd;
2618 
2619 	vd->vdev_stat.vs_read_errors = 0;
2620 	vd->vdev_stat.vs_write_errors = 0;
2621 	vd->vdev_stat.vs_checksum_errors = 0;
2622 
2623 	for (int c = 0; c < vd->vdev_children; c++)
2624 		vdev_clear(spa, vd->vdev_child[c]);
2625 
2626 	/*
2627 	 * If we're in the FAULTED state or have experienced failed I/O, then
2628 	 * clear the persistent state and attempt to reopen the device.  We
2629 	 * also mark the vdev config dirty, so that the new faulted state is
2630 	 * written out to disk.
2631 	 */
2632 	if (vd->vdev_faulted || vd->vdev_degraded ||
2633 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
2634 
2635 		/*
2636 		 * When reopening in reponse to a clear event, it may be due to
2637 		 * a fmadm repair request.  In this case, if the device is
2638 		 * still broken, we want to still post the ereport again.
2639 		 */
2640 		vd->vdev_forcefault = B_TRUE;
2641 
2642 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2643 		vd->vdev_cant_read = B_FALSE;
2644 		vd->vdev_cant_write = B_FALSE;
2645 
2646 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
2647 
2648 		vd->vdev_forcefault = B_FALSE;
2649 
2650 		if (vd != rvd && vdev_writeable(vd->vdev_top))
2651 			vdev_state_dirty(vd->vdev_top);
2652 
2653 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2654 			spa_async_request(spa, SPA_ASYNC_RESILVER);
2655 
2656 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
2657 	}
2658 
2659 	/*
2660 	 * When clearing a FMA-diagnosed fault, we always want to
2661 	 * unspare the device, as we assume that the original spare was
2662 	 * done in response to the FMA fault.
2663 	 */
2664 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2665 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2666 	    vd->vdev_parent->vdev_child[0] == vd)
2667 		vd->vdev_unspare = B_TRUE;
2668 }
2669 
2670 boolean_t
2671 vdev_is_dead(vdev_t *vd)
2672 {
2673 	/*
2674 	 * Holes and missing devices are always considered "dead".
2675 	 * This simplifies the code since we don't have to check for
2676 	 * these types of devices in the various code paths.
2677 	 * Instead we rely on the fact that we skip over dead devices
2678 	 * before issuing I/O to them.
2679 	 */
2680 	return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
2681 	    vd->vdev_ops == &vdev_missing_ops);
2682 }
2683 
2684 boolean_t
2685 vdev_readable(vdev_t *vd)
2686 {
2687 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2688 }
2689 
2690 boolean_t
2691 vdev_writeable(vdev_t *vd)
2692 {
2693 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2694 }
2695 
2696 boolean_t
2697 vdev_allocatable(vdev_t *vd)
2698 {
2699 	uint64_t state = vd->vdev_state;
2700 
2701 	/*
2702 	 * We currently allow allocations from vdevs which may be in the
2703 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2704 	 * fails to reopen then we'll catch it later when we're holding
2705 	 * the proper locks.  Note that we have to get the vdev state
2706 	 * in a local variable because although it changes atomically,
2707 	 * we're asking two separate questions about it.
2708 	 */
2709 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
2710 	    !vd->vdev_cant_write && !vd->vdev_ishole &&
2711 	    vd->vdev_mg->mg_initialized);
2712 }
2713 
2714 boolean_t
2715 vdev_accessible(vdev_t *vd, zio_t *zio)
2716 {
2717 	ASSERT(zio->io_vd == vd);
2718 
2719 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2720 		return (B_FALSE);
2721 
2722 	if (zio->io_type == ZIO_TYPE_READ)
2723 		return (!vd->vdev_cant_read);
2724 
2725 	if (zio->io_type == ZIO_TYPE_WRITE)
2726 		return (!vd->vdev_cant_write);
2727 
2728 	return (B_TRUE);
2729 }
2730 
2731 /*
2732  * Get statistics for the given vdev.
2733  */
2734 void
2735 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2736 {
2737 	spa_t *spa = vd->vdev_spa;
2738 	vdev_t *rvd = spa->spa_root_vdev;
2739 	vdev_t *tvd = vd->vdev_top;
2740 
2741 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2742 
2743 	mutex_enter(&vd->vdev_stat_lock);
2744 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2745 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2746 	vs->vs_state = vd->vdev_state;
2747 	vs->vs_rsize = vdev_get_min_asize(vd);
2748 	if (vd->vdev_ops->vdev_op_leaf)
2749 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2750 	/*
2751 	 * Report expandable space on top-level, non-auxillary devices only.
2752 	 * The expandable space is reported in terms of metaslab sized units
2753 	 * since that determines how much space the pool can expand.
2754 	 */
2755 	if (vd->vdev_aux == NULL && tvd != NULL) {
2756 		vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize,
2757 		    1ULL << tvd->vdev_ms_shift);
2758 	}
2759 	if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) {
2760 		vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
2761 	}
2762 
2763 	/*
2764 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2765 	 * over all top-level vdevs (i.e. the direct children of the root).
2766 	 */
2767 	if (vd == rvd) {
2768 		for (int c = 0; c < rvd->vdev_children; c++) {
2769 			vdev_t *cvd = rvd->vdev_child[c];
2770 			vdev_stat_t *cvs = &cvd->vdev_stat;
2771 
2772 			for (int t = 0; t < ZIO_TYPES; t++) {
2773 				vs->vs_ops[t] += cvs->vs_ops[t];
2774 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2775 			}
2776 			cvs->vs_scan_removing = cvd->vdev_removing;
2777 		}
2778 	}
2779 	mutex_exit(&vd->vdev_stat_lock);
2780 }
2781 
2782 void
2783 vdev_clear_stats(vdev_t *vd)
2784 {
2785 	mutex_enter(&vd->vdev_stat_lock);
2786 	vd->vdev_stat.vs_space = 0;
2787 	vd->vdev_stat.vs_dspace = 0;
2788 	vd->vdev_stat.vs_alloc = 0;
2789 	mutex_exit(&vd->vdev_stat_lock);
2790 }
2791 
2792 void
2793 vdev_scan_stat_init(vdev_t *vd)
2794 {
2795 	vdev_stat_t *vs = &vd->vdev_stat;
2796 
2797 	for (int c = 0; c < vd->vdev_children; c++)
2798 		vdev_scan_stat_init(vd->vdev_child[c]);
2799 
2800 	mutex_enter(&vd->vdev_stat_lock);
2801 	vs->vs_scan_processed = 0;
2802 	mutex_exit(&vd->vdev_stat_lock);
2803 }
2804 
2805 void
2806 vdev_stat_update(zio_t *zio, uint64_t psize)
2807 {
2808 	spa_t *spa = zio->io_spa;
2809 	vdev_t *rvd = spa->spa_root_vdev;
2810 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2811 	vdev_t *pvd;
2812 	uint64_t txg = zio->io_txg;
2813 	vdev_stat_t *vs = &vd->vdev_stat;
2814 	zio_type_t type = zio->io_type;
2815 	int flags = zio->io_flags;
2816 
2817 	/*
2818 	 * If this i/o is a gang leader, it didn't do any actual work.
2819 	 */
2820 	if (zio->io_gang_tree)
2821 		return;
2822 
2823 	if (zio->io_error == 0) {
2824 		/*
2825 		 * If this is a root i/o, don't count it -- we've already
2826 		 * counted the top-level vdevs, and vdev_get_stats() will
2827 		 * aggregate them when asked.  This reduces contention on
2828 		 * the root vdev_stat_lock and implicitly handles blocks
2829 		 * that compress away to holes, for which there is no i/o.
2830 		 * (Holes never create vdev children, so all the counters
2831 		 * remain zero, which is what we want.)
2832 		 *
2833 		 * Note: this only applies to successful i/o (io_error == 0)
2834 		 * because unlike i/o counts, errors are not additive.
2835 		 * When reading a ditto block, for example, failure of
2836 		 * one top-level vdev does not imply a root-level error.
2837 		 */
2838 		if (vd == rvd)
2839 			return;
2840 
2841 		ASSERT(vd == zio->io_vd);
2842 
2843 		if (flags & ZIO_FLAG_IO_BYPASS)
2844 			return;
2845 
2846 		mutex_enter(&vd->vdev_stat_lock);
2847 
2848 		if (flags & ZIO_FLAG_IO_REPAIR) {
2849 			if (flags & ZIO_FLAG_SCAN_THREAD) {
2850 				dsl_scan_phys_t *scn_phys =
2851 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
2852 				uint64_t *processed = &scn_phys->scn_processed;
2853 
2854 				/* XXX cleanup? */
2855 				if (vd->vdev_ops->vdev_op_leaf)
2856 					atomic_add_64(processed, psize);
2857 				vs->vs_scan_processed += psize;
2858 			}
2859 
2860 			if (flags & ZIO_FLAG_SELF_HEAL)
2861 				vs->vs_self_healed += psize;
2862 		}
2863 
2864 		vs->vs_ops[type]++;
2865 		vs->vs_bytes[type] += psize;
2866 
2867 		mutex_exit(&vd->vdev_stat_lock);
2868 		return;
2869 	}
2870 
2871 	if (flags & ZIO_FLAG_SPECULATIVE)
2872 		return;
2873 
2874 	/*
2875 	 * If this is an I/O error that is going to be retried, then ignore the
2876 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
2877 	 * hard errors, when in reality they can happen for any number of
2878 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
2879 	 */
2880 	if (zio->io_error == EIO &&
2881 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
2882 		return;
2883 
2884 	/*
2885 	 * Intent logs writes won't propagate their error to the root
2886 	 * I/O so don't mark these types of failures as pool-level
2887 	 * errors.
2888 	 */
2889 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
2890 		return;
2891 
2892 	mutex_enter(&vd->vdev_stat_lock);
2893 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2894 		if (zio->io_error == ECKSUM)
2895 			vs->vs_checksum_errors++;
2896 		else
2897 			vs->vs_read_errors++;
2898 	}
2899 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2900 		vs->vs_write_errors++;
2901 	mutex_exit(&vd->vdev_stat_lock);
2902 
2903 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
2904 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
2905 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
2906 	    spa->spa_claiming)) {
2907 		/*
2908 		 * This is either a normal write (not a repair), or it's
2909 		 * a repair induced by the scrub thread, or it's a repair
2910 		 * made by zil_claim() during spa_load() in the first txg.
2911 		 * In the normal case, we commit the DTL change in the same
2912 		 * txg as the block was born.  In the scrub-induced repair
2913 		 * case, we know that scrubs run in first-pass syncing context,
2914 		 * so we commit the DTL change in spa_syncing_txg(spa).
2915 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
2916 		 *
2917 		 * We currently do not make DTL entries for failed spontaneous
2918 		 * self-healing writes triggered by normal (non-scrubbing)
2919 		 * reads, because we have no transactional context in which to
2920 		 * do so -- and it's not clear that it'd be desirable anyway.
2921 		 */
2922 		if (vd->vdev_ops->vdev_op_leaf) {
2923 			uint64_t commit_txg = txg;
2924 			if (flags & ZIO_FLAG_SCAN_THREAD) {
2925 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2926 				ASSERT(spa_sync_pass(spa) == 1);
2927 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2928 				commit_txg = spa_syncing_txg(spa);
2929 			} else if (spa->spa_claiming) {
2930 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2931 				commit_txg = spa_first_txg(spa);
2932 			}
2933 			ASSERT(commit_txg >= spa_syncing_txg(spa));
2934 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2935 				return;
2936 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2937 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
2938 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2939 		}
2940 		if (vd != rvd)
2941 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2942 	}
2943 }
2944 
2945 /*
2946  * Update the in-core space usage stats for this vdev, its metaslab class,
2947  * and the root vdev.
2948  */
2949 void
2950 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2951     int64_t space_delta)
2952 {
2953 	int64_t dspace_delta = space_delta;
2954 	spa_t *spa = vd->vdev_spa;
2955 	vdev_t *rvd = spa->spa_root_vdev;
2956 	metaslab_group_t *mg = vd->vdev_mg;
2957 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2958 
2959 	ASSERT(vd == vd->vdev_top);
2960 
2961 	/*
2962 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2963 	 * factor.  We must calculate this here and not at the root vdev
2964 	 * because the root vdev's psize-to-asize is simply the max of its
2965 	 * childrens', thus not accurate enough for us.
2966 	 */
2967 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2968 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
2969 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2970 	    vd->vdev_deflate_ratio;
2971 
2972 	mutex_enter(&vd->vdev_stat_lock);
2973 	vd->vdev_stat.vs_alloc += alloc_delta;
2974 	vd->vdev_stat.vs_space += space_delta;
2975 	vd->vdev_stat.vs_dspace += dspace_delta;
2976 	mutex_exit(&vd->vdev_stat_lock);
2977 
2978 	if (mc == spa_normal_class(spa)) {
2979 		mutex_enter(&rvd->vdev_stat_lock);
2980 		rvd->vdev_stat.vs_alloc += alloc_delta;
2981 		rvd->vdev_stat.vs_space += space_delta;
2982 		rvd->vdev_stat.vs_dspace += dspace_delta;
2983 		mutex_exit(&rvd->vdev_stat_lock);
2984 	}
2985 
2986 	if (mc != NULL) {
2987 		ASSERT(rvd == vd->vdev_parent);
2988 		ASSERT(vd->vdev_ms_count != 0);
2989 
2990 		metaslab_class_space_update(mc,
2991 		    alloc_delta, defer_delta, space_delta, dspace_delta);
2992 	}
2993 }
2994 
2995 /*
2996  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2997  * so that it will be written out next time the vdev configuration is synced.
2998  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2999  */
3000 void
3001 vdev_config_dirty(vdev_t *vd)
3002 {
3003 	spa_t *spa = vd->vdev_spa;
3004 	vdev_t *rvd = spa->spa_root_vdev;
3005 	int c;
3006 
3007 	ASSERT(spa_writeable(spa));
3008 
3009 	/*
3010 	 * If this is an aux vdev (as with l2cache and spare devices), then we
3011 	 * update the vdev config manually and set the sync flag.
3012 	 */
3013 	if (vd->vdev_aux != NULL) {
3014 		spa_aux_vdev_t *sav = vd->vdev_aux;
3015 		nvlist_t **aux;
3016 		uint_t naux;
3017 
3018 		for (c = 0; c < sav->sav_count; c++) {
3019 			if (sav->sav_vdevs[c] == vd)
3020 				break;
3021 		}
3022 
3023 		if (c == sav->sav_count) {
3024 			/*
3025 			 * We're being removed.  There's nothing more to do.
3026 			 */
3027 			ASSERT(sav->sav_sync == B_TRUE);
3028 			return;
3029 		}
3030 
3031 		sav->sav_sync = B_TRUE;
3032 
3033 		if (nvlist_lookup_nvlist_array(sav->sav_config,
3034 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
3035 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
3036 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
3037 		}
3038 
3039 		ASSERT(c < naux);
3040 
3041 		/*
3042 		 * Setting the nvlist in the middle if the array is a little
3043 		 * sketchy, but it will work.
3044 		 */
3045 		nvlist_free(aux[c]);
3046 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3047 
3048 		return;
3049 	}
3050 
3051 	/*
3052 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
3053 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
3054 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
3055 	 * so this is sufficient to ensure mutual exclusion.
3056 	 */
3057 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3058 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3059 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
3060 
3061 	if (vd == rvd) {
3062 		for (c = 0; c < rvd->vdev_children; c++)
3063 			vdev_config_dirty(rvd->vdev_child[c]);
3064 	} else {
3065 		ASSERT(vd == vd->vdev_top);
3066 
3067 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
3068 		    !vd->vdev_ishole)
3069 			list_insert_head(&spa->spa_config_dirty_list, vd);
3070 	}
3071 }
3072 
3073 void
3074 vdev_config_clean(vdev_t *vd)
3075 {
3076 	spa_t *spa = vd->vdev_spa;
3077 
3078 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3079 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3080 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
3081 
3082 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3083 	list_remove(&spa->spa_config_dirty_list, vd);
3084 }
3085 
3086 /*
3087  * Mark a top-level vdev's state as dirty, so that the next pass of
3088  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
3089  * the state changes from larger config changes because they require
3090  * much less locking, and are often needed for administrative actions.
3091  */
3092 void
3093 vdev_state_dirty(vdev_t *vd)
3094 {
3095 	spa_t *spa = vd->vdev_spa;
3096 
3097 	ASSERT(spa_writeable(spa));
3098 	ASSERT(vd == vd->vdev_top);
3099 
3100 	/*
3101 	 * The state list is protected by the SCL_STATE lock.  The caller
3102 	 * must either hold SCL_STATE as writer, or must be the sync thread
3103 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
3104 	 * so this is sufficient to ensure mutual exclusion.
3105 	 */
3106 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3107 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3108 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3109 
3110 	if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
3111 		list_insert_head(&spa->spa_state_dirty_list, vd);
3112 }
3113 
3114 void
3115 vdev_state_clean(vdev_t *vd)
3116 {
3117 	spa_t *spa = vd->vdev_spa;
3118 
3119 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3120 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3121 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3122 
3123 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3124 	list_remove(&spa->spa_state_dirty_list, vd);
3125 }
3126 
3127 /*
3128  * Propagate vdev state up from children to parent.
3129  */
3130 void
3131 vdev_propagate_state(vdev_t *vd)
3132 {
3133 	spa_t *spa = vd->vdev_spa;
3134 	vdev_t *rvd = spa->spa_root_vdev;
3135 	int degraded = 0, faulted = 0;
3136 	int corrupted = 0;
3137 	vdev_t *child;
3138 
3139 	if (vd->vdev_children > 0) {
3140 		for (int c = 0; c < vd->vdev_children; c++) {
3141 			child = vd->vdev_child[c];
3142 
3143 			/*
3144 			 * Don't factor holes into the decision.
3145 			 */
3146 			if (child->vdev_ishole)
3147 				continue;
3148 
3149 			if (!vdev_readable(child) ||
3150 			    (!vdev_writeable(child) && spa_writeable(spa))) {
3151 				/*
3152 				 * Root special: if there is a top-level log
3153 				 * device, treat the root vdev as if it were
3154 				 * degraded.
3155 				 */
3156 				if (child->vdev_islog && vd == rvd)
3157 					degraded++;
3158 				else
3159 					faulted++;
3160 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3161 				degraded++;
3162 			}
3163 
3164 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3165 				corrupted++;
3166 		}
3167 
3168 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3169 
3170 		/*
3171 		 * Root special: if there is a top-level vdev that cannot be
3172 		 * opened due to corrupted metadata, then propagate the root
3173 		 * vdev's aux state as 'corrupt' rather than 'insufficient
3174 		 * replicas'.
3175 		 */
3176 		if (corrupted && vd == rvd &&
3177 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3178 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3179 			    VDEV_AUX_CORRUPT_DATA);
3180 	}
3181 
3182 	if (vd->vdev_parent)
3183 		vdev_propagate_state(vd->vdev_parent);
3184 }
3185 
3186 /*
3187  * Set a vdev's state.  If this is during an open, we don't update the parent
3188  * state, because we're in the process of opening children depth-first.
3189  * Otherwise, we propagate the change to the parent.
3190  *
3191  * If this routine places a device in a faulted state, an appropriate ereport is
3192  * generated.
3193  */
3194 void
3195 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3196 {
3197 	uint64_t save_state;
3198 	spa_t *spa = vd->vdev_spa;
3199 
3200 	if (state == vd->vdev_state) {
3201 		vd->vdev_stat.vs_aux = aux;
3202 		return;
3203 	}
3204 
3205 	save_state = vd->vdev_state;
3206 
3207 	vd->vdev_state = state;
3208 	vd->vdev_stat.vs_aux = aux;
3209 
3210 	/*
3211 	 * If we are setting the vdev state to anything but an open state, then
3212 	 * always close the underlying device unless the device has requested
3213 	 * a delayed close (i.e. we're about to remove or fault the device).
3214 	 * Otherwise, we keep accessible but invalid devices open forever.
3215 	 * We don't call vdev_close() itself, because that implies some extra
3216 	 * checks (offline, etc) that we don't want here.  This is limited to
3217 	 * leaf devices, because otherwise closing the device will affect other
3218 	 * children.
3219 	 */
3220 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3221 	    vd->vdev_ops->vdev_op_leaf)
3222 		vd->vdev_ops->vdev_op_close(vd);
3223 
3224 	/*
3225 	 * If we have brought this vdev back into service, we need
3226 	 * to notify fmd so that it can gracefully repair any outstanding
3227 	 * cases due to a missing device.  We do this in all cases, even those
3228 	 * that probably don't correlate to a repaired fault.  This is sure to
3229 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
3230 	 * this is a transient state it's OK, as the retire agent will
3231 	 * double-check the state of the vdev before repairing it.
3232 	 */
3233 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3234 	    vd->vdev_prevstate != state)
3235 		zfs_post_state_change(spa, vd);
3236 
3237 	if (vd->vdev_removed &&
3238 	    state == VDEV_STATE_CANT_OPEN &&
3239 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3240 		/*
3241 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
3242 		 * device was previously marked removed and someone attempted to
3243 		 * reopen it.  If this failed due to a nonexistent device, then
3244 		 * keep the device in the REMOVED state.  We also let this be if
3245 		 * it is one of our special test online cases, which is only
3246 		 * attempting to online the device and shouldn't generate an FMA
3247 		 * fault.
3248 		 */
3249 		vd->vdev_state = VDEV_STATE_REMOVED;
3250 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
3251 	} else if (state == VDEV_STATE_REMOVED) {
3252 		vd->vdev_removed = B_TRUE;
3253 	} else if (state == VDEV_STATE_CANT_OPEN) {
3254 		/*
3255 		 * If we fail to open a vdev during an import or recovery, we
3256 		 * mark it as "not available", which signifies that it was
3257 		 * never there to begin with.  Failure to open such a device
3258 		 * is not considered an error.
3259 		 */
3260 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3261 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3262 		    vd->vdev_ops->vdev_op_leaf)
3263 			vd->vdev_not_present = 1;
3264 
3265 		/*
3266 		 * Post the appropriate ereport.  If the 'prevstate' field is
3267 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3268 		 * that this is part of a vdev_reopen().  In this case, we don't
3269 		 * want to post the ereport if the device was already in the
3270 		 * CANT_OPEN state beforehand.
3271 		 *
3272 		 * If the 'checkremove' flag is set, then this is an attempt to
3273 		 * online the device in response to an insertion event.  If we
3274 		 * hit this case, then we have detected an insertion event for a
3275 		 * faulted or offline device that wasn't in the removed state.
3276 		 * In this scenario, we don't post an ereport because we are
3277 		 * about to replace the device, or attempt an online with
3278 		 * vdev_forcefault, which will generate the fault for us.
3279 		 */
3280 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
3281 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
3282 		    vd != spa->spa_root_vdev) {
3283 			const char *class;
3284 
3285 			switch (aux) {
3286 			case VDEV_AUX_OPEN_FAILED:
3287 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3288 				break;
3289 			case VDEV_AUX_CORRUPT_DATA:
3290 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3291 				break;
3292 			case VDEV_AUX_NO_REPLICAS:
3293 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3294 				break;
3295 			case VDEV_AUX_BAD_GUID_SUM:
3296 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3297 				break;
3298 			case VDEV_AUX_TOO_SMALL:
3299 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3300 				break;
3301 			case VDEV_AUX_BAD_LABEL:
3302 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3303 				break;
3304 			default:
3305 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3306 			}
3307 
3308 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3309 		}
3310 
3311 		/* Erase any notion of persistent removed state */
3312 		vd->vdev_removed = B_FALSE;
3313 	} else {
3314 		vd->vdev_removed = B_FALSE;
3315 	}
3316 
3317 	if (!isopen && vd->vdev_parent)
3318 		vdev_propagate_state(vd->vdev_parent);
3319 }
3320 
3321 /*
3322  * Check the vdev configuration to ensure that it's capable of supporting
3323  * a root pool. We do not support partial configuration.
3324  * In addition, only a single top-level vdev is allowed.
3325  */
3326 boolean_t
3327 vdev_is_bootable(vdev_t *vd)
3328 {
3329 	if (!vd->vdev_ops->vdev_op_leaf) {
3330 		char *vdev_type = vd->vdev_ops->vdev_op_type;
3331 
3332 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
3333 		    vd->vdev_children > 1) {
3334 			return (B_FALSE);
3335 		} else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
3336 			return (B_FALSE);
3337 		}
3338 	}
3339 
3340 	for (int c = 0; c < vd->vdev_children; c++) {
3341 		if (!vdev_is_bootable(vd->vdev_child[c]))
3342 			return (B_FALSE);
3343 	}
3344 	return (B_TRUE);
3345 }
3346 
3347 /*
3348  * Load the state from the original vdev tree (ovd) which
3349  * we've retrieved from the MOS config object. If the original
3350  * vdev was offline or faulted then we transfer that state to the
3351  * device in the current vdev tree (nvd).
3352  */
3353 void
3354 vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3355 {
3356 	spa_t *spa = nvd->vdev_spa;
3357 
3358 	ASSERT(nvd->vdev_top->vdev_islog);
3359 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3360 	ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3361 
3362 	for (int c = 0; c < nvd->vdev_children; c++)
3363 		vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3364 
3365 	if (nvd->vdev_ops->vdev_op_leaf) {
3366 		/*
3367 		 * Restore the persistent vdev state
3368 		 */
3369 		nvd->vdev_offline = ovd->vdev_offline;
3370 		nvd->vdev_faulted = ovd->vdev_faulted;
3371 		nvd->vdev_degraded = ovd->vdev_degraded;
3372 		nvd->vdev_removed = ovd->vdev_removed;
3373 	}
3374 }
3375 
3376 /*
3377  * Determine if a log device has valid content.  If the vdev was
3378  * removed or faulted in the MOS config then we know that
3379  * the content on the log device has already been written to the pool.
3380  */
3381 boolean_t
3382 vdev_log_state_valid(vdev_t *vd)
3383 {
3384 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
3385 	    !vd->vdev_removed)
3386 		return (B_TRUE);
3387 
3388 	for (int c = 0; c < vd->vdev_children; c++)
3389 		if (vdev_log_state_valid(vd->vdev_child[c]))
3390 			return (B_TRUE);
3391 
3392 	return (B_FALSE);
3393 }
3394 
3395 /*
3396  * Expand a vdev if possible.
3397  */
3398 void
3399 vdev_expand(vdev_t *vd, uint64_t txg)
3400 {
3401 	ASSERT(vd->vdev_top == vd);
3402 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3403 
3404 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3405 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
3406 		vdev_config_dirty(vd);
3407 	}
3408 }
3409 
3410 /*
3411  * Split a vdev.
3412  */
3413 void
3414 vdev_split(vdev_t *vd)
3415 {
3416 	vdev_t *cvd, *pvd = vd->vdev_parent;
3417 
3418 	vdev_remove_child(pvd, vd);
3419 	vdev_compact_children(pvd);
3420 
3421 	cvd = pvd->vdev_child[0];
3422 	if (pvd->vdev_children == 1) {
3423 		vdev_remove_parent(cvd);
3424 		cvd->vdev_splitting = B_TRUE;
3425 	}
3426 	vdev_propagate_state(cvd);
3427 }
3428 
3429 void
3430 vdev_deadman(vdev_t *vd)
3431 {
3432 	for (int c = 0; c < vd->vdev_children; c++) {
3433 		vdev_t *cvd = vd->vdev_child[c];
3434 
3435 		vdev_deadman(cvd);
3436 	}
3437 
3438 	if (vd->vdev_ops->vdev_op_leaf) {
3439 		vdev_queue_t *vq = &vd->vdev_queue;
3440 
3441 		mutex_enter(&vq->vq_lock);
3442 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
3443 			spa_t *spa = vd->vdev_spa;
3444 			zio_t *fio;
3445 			uint64_t delta;
3446 
3447 			/*
3448 			 * Look at the head of all the pending queues,
3449 			 * if any I/O has been outstanding for longer than
3450 			 * the spa_deadman_synctime we panic the system.
3451 			 */
3452 			fio = avl_first(&vq->vq_active_tree);
3453 			delta = gethrtime() - fio->io_timestamp;
3454 			if (delta > spa_deadman_synctime(spa)) {
3455 				zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3456 				    "delta %lluns, last io %lluns",
3457 				    fio->io_timestamp, delta,
3458 				    vq->vq_io_complete_ts);
3459 				fm_panic("I/O to pool '%s' appears to be "
3460 				    "hung.", spa_name(spa));
3461 			}
3462 		}
3463 		mutex_exit(&vq->vq_lock);
3464 	}
3465 }
3466