xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 6f7938128a2c5e23f4b970ea101137eadd1470a1)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
2398d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
255f368aefSYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
26c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
27c8811bd3SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
28ce1577b0SDave Eddy  * Copyright 2017 Joyent, Inc.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/zfs_context.h>
32ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
355cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h>
36fa9e4066Sahrens #include <sys/dmu.h>
37fa9e4066Sahrens #include <sys/dmu_tx.h>
385cabbc6bSPrashanth Sreenivasa #include <sys/dsl_dir.h>
39fa9e4066Sahrens #include <sys/vdev_impl.h>
40fa9e4066Sahrens #include <sys/uberblock_impl.h>
41fa9e4066Sahrens #include <sys/metaslab.h>
42fa9e4066Sahrens #include <sys/metaslab_impl.h>
43fa9e4066Sahrens #include <sys/space_map.h>
440713e232SGeorge Wilson #include <sys/space_reftree.h>
45fa9e4066Sahrens #include <sys/zio.h>
46fa9e4066Sahrens #include <sys/zap.h>
47fa9e4066Sahrens #include <sys/fs/zfs.h>
48c5904d13Seschrock #include <sys/arc.h>
49e6ca193dSGeorge Wilson #include <sys/zil.h>
503f9d6ad7SLin Ling #include <sys/dsl_scan.h>
51770499e1SDan Kimmel #include <sys/abd.h>
52fa9e4066Sahrens 
53fa9e4066Sahrens /*
54fa9e4066Sahrens  * Virtual device management.
55fa9e4066Sahrens  */
56fa9e4066Sahrens 
57fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = {
58fa9e4066Sahrens 	&vdev_root_ops,
59fa9e4066Sahrens 	&vdev_raidz_ops,
60fa9e4066Sahrens 	&vdev_mirror_ops,
61fa9e4066Sahrens 	&vdev_replacing_ops,
6299653d4eSeschrock 	&vdev_spare_ops,
63fa9e4066Sahrens 	&vdev_disk_ops,
64fa9e4066Sahrens 	&vdev_file_ops,
65fa9e4066Sahrens 	&vdev_missing_ops,
6688ecc943SGeorge Wilson 	&vdev_hole_ops,
675cabbc6bSPrashanth Sreenivasa 	&vdev_indirect_ops,
68fa9e4066Sahrens 	NULL
69fa9e4066Sahrens };
70fa9e4066Sahrens 
71088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
72088f3894Sahrens int zfs_scrub_limit = 10;
7305b2b3b8Smishra 
74bf3e216cSMatthew Ahrens /*
75bf3e216cSMatthew Ahrens  * When a vdev is added, it will be divided into approximately (but no
76bf3e216cSMatthew Ahrens  * more than) this number of metaslabs.
77bf3e216cSMatthew Ahrens  */
78bf3e216cSMatthew Ahrens int metaslabs_per_vdev = 200;
79bf3e216cSMatthew Ahrens 
80*6f793812SPavel Zakharov boolean_t vdev_validate_skip = B_FALSE;
81*6f793812SPavel Zakharov 
823ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
833ee8c80cSPavel Zakharov void
843ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
853ee8c80cSPavel Zakharov {
863ee8c80cSPavel Zakharov 	va_list adx;
873ee8c80cSPavel Zakharov 	char buf[256];
883ee8c80cSPavel Zakharov 
893ee8c80cSPavel Zakharov 	va_start(adx, fmt);
903ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
913ee8c80cSPavel Zakharov 	va_end(adx);
923ee8c80cSPavel Zakharov 
933ee8c80cSPavel Zakharov 	if (vd->vdev_path != NULL) {
943ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
953ee8c80cSPavel Zakharov 		    vd->vdev_path, buf);
963ee8c80cSPavel Zakharov 	} else {
973ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
983ee8c80cSPavel Zakharov 		    vd->vdev_ops->vdev_op_type,
993ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_id,
1003ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_guid, buf);
1013ee8c80cSPavel Zakharov 	}
1023ee8c80cSPavel Zakharov }
1033ee8c80cSPavel Zakharov 
104*6f793812SPavel Zakharov void
105*6f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
106*6f793812SPavel Zakharov {
107*6f793812SPavel Zakharov 	char state[20];
108*6f793812SPavel Zakharov 
109*6f793812SPavel Zakharov 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
110*6f793812SPavel Zakharov 		zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
111*6f793812SPavel Zakharov 		    vd->vdev_ops->vdev_op_type);
112*6f793812SPavel Zakharov 		return;
113*6f793812SPavel Zakharov 	}
114*6f793812SPavel Zakharov 
115*6f793812SPavel Zakharov 	switch (vd->vdev_state) {
116*6f793812SPavel Zakharov 	case VDEV_STATE_UNKNOWN:
117*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "unknown");
118*6f793812SPavel Zakharov 		break;
119*6f793812SPavel Zakharov 	case VDEV_STATE_CLOSED:
120*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "closed");
121*6f793812SPavel Zakharov 		break;
122*6f793812SPavel Zakharov 	case VDEV_STATE_OFFLINE:
123*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "offline");
124*6f793812SPavel Zakharov 		break;
125*6f793812SPavel Zakharov 	case VDEV_STATE_REMOVED:
126*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "removed");
127*6f793812SPavel Zakharov 		break;
128*6f793812SPavel Zakharov 	case VDEV_STATE_CANT_OPEN:
129*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "can't open");
130*6f793812SPavel Zakharov 		break;
131*6f793812SPavel Zakharov 	case VDEV_STATE_FAULTED:
132*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "faulted");
133*6f793812SPavel Zakharov 		break;
134*6f793812SPavel Zakharov 	case VDEV_STATE_DEGRADED:
135*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "degraded");
136*6f793812SPavel Zakharov 		break;
137*6f793812SPavel Zakharov 	case VDEV_STATE_HEALTHY:
138*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "healthy");
139*6f793812SPavel Zakharov 		break;
140*6f793812SPavel Zakharov 	default:
141*6f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "<state %u>",
142*6f793812SPavel Zakharov 		    (uint_t)vd->vdev_state);
143*6f793812SPavel Zakharov 	}
144*6f793812SPavel Zakharov 
145*6f793812SPavel Zakharov 	zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
146*6f793812SPavel Zakharov 	    "", vd->vdev_id, vd->vdev_ops->vdev_op_type,
147*6f793812SPavel Zakharov 	    vd->vdev_islog ? " (log)" : "",
148*6f793812SPavel Zakharov 	    (u_longlong_t)vd->vdev_guid,
149*6f793812SPavel Zakharov 	    vd->vdev_path ? vd->vdev_path : "N/A", state);
150*6f793812SPavel Zakharov 
151*6f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++)
152*6f793812SPavel Zakharov 		vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
153*6f793812SPavel Zakharov }
154*6f793812SPavel Zakharov 
155fa9e4066Sahrens /*
156fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
157fa9e4066Sahrens  */
158fa9e4066Sahrens static vdev_ops_t *
159fa9e4066Sahrens vdev_getops(const char *type)
160fa9e4066Sahrens {
161fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
162fa9e4066Sahrens 
163fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
164fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
165fa9e4066Sahrens 			break;
166fa9e4066Sahrens 
167fa9e4066Sahrens 	return (ops);
168fa9e4066Sahrens }
169fa9e4066Sahrens 
170fa9e4066Sahrens /*
171fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
172fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
173fa9e4066Sahrens  */
174fa9e4066Sahrens uint64_t
175fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
176fa9e4066Sahrens {
177ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
178fa9e4066Sahrens 	uint64_t csize;
179fa9e4066Sahrens 
180573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
181fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
182fa9e4066Sahrens 		asize = MAX(asize, csize);
183fa9e4066Sahrens 	}
184fa9e4066Sahrens 
185fa9e4066Sahrens 	return (asize);
186fa9e4066Sahrens }
187fa9e4066Sahrens 
1882a79c5feSlling /*
189573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
190573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
191573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
192573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
1932a79c5feSlling  */
1942a79c5feSlling uint64_t
195573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
1962a79c5feSlling {
197573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
1982a79c5feSlling 
199573ca77eSGeorge Wilson 	/*
2004263d13fSGeorge Wilson 	 * If our parent is NULL (inactive spare or cache) or is the root,
201573ca77eSGeorge Wilson 	 * just return our own asize.
202573ca77eSGeorge Wilson 	 */
203573ca77eSGeorge Wilson 	if (pvd == NULL)
204573ca77eSGeorge Wilson 		return (vd->vdev_asize);
2052a79c5feSlling 
2062a79c5feSlling 	/*
207573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
208573ca77eSGeorge Wilson 	 * to the nearest metaslab.
2092a79c5feSlling 	 */
210573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
211573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
2122a79c5feSlling 
213573ca77eSGeorge Wilson 	/*
214573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
215573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
216573ca77eSGeorge Wilson 	 */
217573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
218c040c10cSSteven Hartland 		return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
219c040c10cSSteven Hartland 		    pvd->vdev_children);
2202a79c5feSlling 
221573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
222573ca77eSGeorge Wilson }
2232a79c5feSlling 
224573ca77eSGeorge Wilson void
225573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
226573ca77eSGeorge Wilson {
227573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
228573ca77eSGeorge Wilson 
229573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
230573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
2312a79c5feSlling }
2322a79c5feSlling 
233fa9e4066Sahrens vdev_t *
234fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
235fa9e4066Sahrens {
236fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
237fa9e4066Sahrens 
238e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
239e05725b1Sbonwick 
240088f3894Sahrens 	if (vdev < rvd->vdev_children) {
241088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
242fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
243088f3894Sahrens 	}
244fa9e4066Sahrens 
245fa9e4066Sahrens 	return (NULL);
246fa9e4066Sahrens }
247fa9e4066Sahrens 
248fa9e4066Sahrens vdev_t *
249fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
250fa9e4066Sahrens {
251fa9e4066Sahrens 	vdev_t *mvd;
252fa9e4066Sahrens 
2530e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
254fa9e4066Sahrens 		return (vd);
255fa9e4066Sahrens 
256573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
257fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
258fa9e4066Sahrens 		    NULL)
259fa9e4066Sahrens 			return (mvd);
260fa9e4066Sahrens 
261fa9e4066Sahrens 	return (NULL);
262fa9e4066Sahrens }
263fa9e4066Sahrens 
26412380e1eSArne Jansen static int
26512380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd)
26612380e1eSArne Jansen {
26712380e1eSArne Jansen 	int n = 0;
26812380e1eSArne Jansen 
26912380e1eSArne Jansen 	if (vd->vdev_ops->vdev_op_leaf)
27012380e1eSArne Jansen 		return (1);
27112380e1eSArne Jansen 
27212380e1eSArne Jansen 	for (int c = 0; c < vd->vdev_children; c++)
27312380e1eSArne Jansen 		n += vdev_count_leaves_impl(vd->vdev_child[c]);
27412380e1eSArne Jansen 
27512380e1eSArne Jansen 	return (n);
27612380e1eSArne Jansen }
27712380e1eSArne Jansen 
27812380e1eSArne Jansen int
27912380e1eSArne Jansen vdev_count_leaves(spa_t *spa)
28012380e1eSArne Jansen {
28112380e1eSArne Jansen 	return (vdev_count_leaves_impl(spa->spa_root_vdev));
28212380e1eSArne Jansen }
28312380e1eSArne Jansen 
284fa9e4066Sahrens void
285fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
286fa9e4066Sahrens {
287fa9e4066Sahrens 	size_t oldsize, newsize;
288fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
289fa9e4066Sahrens 	vdev_t **newchild;
29081cd5c55SMatthew Ahrens 	spa_t *spa = cvd->vdev_spa;
291fa9e4066Sahrens 
29281cd5c55SMatthew Ahrens 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
293fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
294fa9e4066Sahrens 
295fa9e4066Sahrens 	cvd->vdev_parent = pvd;
296fa9e4066Sahrens 
297fa9e4066Sahrens 	if (pvd == NULL)
298fa9e4066Sahrens 		return;
299fa9e4066Sahrens 
300fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
301fa9e4066Sahrens 
302fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
303fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
304fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
305fa9e4066Sahrens 
306fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
307fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
308fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
309fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
310fa9e4066Sahrens 	}
311fa9e4066Sahrens 
312fa9e4066Sahrens 	pvd->vdev_child = newchild;
313fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
314fa9e4066Sahrens 
315fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
316fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
317fa9e4066Sahrens 
318fa9e4066Sahrens 	/*
319fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
320fa9e4066Sahrens 	 */
321fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
322fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
323fa9e4066Sahrens }
324fa9e4066Sahrens 
325fa9e4066Sahrens void
326fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
327fa9e4066Sahrens {
328fa9e4066Sahrens 	int c;
329fa9e4066Sahrens 	uint_t id = cvd->vdev_id;
330fa9e4066Sahrens 
331fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == pvd);
332fa9e4066Sahrens 
333fa9e4066Sahrens 	if (pvd == NULL)
334fa9e4066Sahrens 		return;
335fa9e4066Sahrens 
336fa9e4066Sahrens 	ASSERT(id < pvd->vdev_children);
337fa9e4066Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
338fa9e4066Sahrens 
339fa9e4066Sahrens 	pvd->vdev_child[id] = NULL;
340fa9e4066Sahrens 	cvd->vdev_parent = NULL;
341fa9e4066Sahrens 
342fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
343fa9e4066Sahrens 		if (pvd->vdev_child[c])
344fa9e4066Sahrens 			break;
345fa9e4066Sahrens 
346fa9e4066Sahrens 	if (c == pvd->vdev_children) {
347fa9e4066Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
348fa9e4066Sahrens 		pvd->vdev_child = NULL;
349fa9e4066Sahrens 		pvd->vdev_children = 0;
350fa9e4066Sahrens 	}
351fa9e4066Sahrens 
352fa9e4066Sahrens 	/*
353fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
354fa9e4066Sahrens 	 */
355fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
356fa9e4066Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
357fa9e4066Sahrens }
358fa9e4066Sahrens 
359fa9e4066Sahrens /*
360fa9e4066Sahrens  * Remove any holes in the child array.
361fa9e4066Sahrens  */
362fa9e4066Sahrens void
363fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
364fa9e4066Sahrens {
365fa9e4066Sahrens 	vdev_t **newchild, *cvd;
366fa9e4066Sahrens 	int oldc = pvd->vdev_children;
367573ca77eSGeorge Wilson 	int newc;
368fa9e4066Sahrens 
369e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
370fa9e4066Sahrens 
371573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
372fa9e4066Sahrens 		if (pvd->vdev_child[c])
373fa9e4066Sahrens 			newc++;
374fa9e4066Sahrens 
375fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
376fa9e4066Sahrens 
377573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
378fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
379fa9e4066Sahrens 			newchild[newc] = cvd;
380fa9e4066Sahrens 			cvd->vdev_id = newc++;
381fa9e4066Sahrens 		}
382fa9e4066Sahrens 	}
383fa9e4066Sahrens 
384fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
385fa9e4066Sahrens 	pvd->vdev_child = newchild;
386fa9e4066Sahrens 	pvd->vdev_children = newc;
387fa9e4066Sahrens }
388fa9e4066Sahrens 
389fa9e4066Sahrens /*
390fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
391fa9e4066Sahrens  */
39288ecc943SGeorge Wilson vdev_t *
393fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
394fa9e4066Sahrens {
395fa9e4066Sahrens 	vdev_t *vd;
3965cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
397fa9e4066Sahrens 
398fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
3995cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
400fa9e4066Sahrens 
4010e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
4020e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
4030e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
404e9103aaeSGarrett D'Amore 		spa->spa_load_guid = spa_generate_guid(NULL);
4050e34b6a7Sbonwick 	}
4060e34b6a7Sbonwick 
40788ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
4080e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
4090e34b6a7Sbonwick 			/*
4100e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
4110e34b6a7Sbonwick 			 * which must be unique among all pools.
4120e34b6a7Sbonwick 			 */
4131195e687SMark J Musante 			guid = spa_generate_guid(NULL);
4140e34b6a7Sbonwick 		} else {
4150e34b6a7Sbonwick 			/*
4160e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
4170e34b6a7Sbonwick 			 */
4181195e687SMark J Musante 			guid = spa_generate_guid(spa);
4190e34b6a7Sbonwick 		}
4200e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
4210e34b6a7Sbonwick 	}
4220e34b6a7Sbonwick 
423fa9e4066Sahrens 	vd->vdev_spa = spa;
424fa9e4066Sahrens 	vd->vdev_id = id;
425fa9e4066Sahrens 	vd->vdev_guid = guid;
426fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
427fa9e4066Sahrens 	vd->vdev_ops = ops;
428fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
42988ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
4305cabbc6bSPrashanth Sreenivasa 	vic->vic_prev_indirect_vdev = UINT64_MAX;
4315cabbc6bSPrashanth Sreenivasa 
4325cabbc6bSPrashanth Sreenivasa 	rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
4335cabbc6bSPrashanth Sreenivasa 	mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
4345cabbc6bSPrashanth Sreenivasa 	vd->vdev_obsolete_segments = range_tree_create(NULL, NULL);
435fa9e4066Sahrens 
436fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
4375ad82045Snd 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
438e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
4390f7643c7SGeorge Wilson 	mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
4408ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
4415cabbc6bSPrashanth Sreenivasa 		vd->vdev_dtl[t] = range_tree_create(NULL, NULL);
4428ad4d6ddSJeff Bonwick 	}
443b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_ms_list, spa,
444fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
445b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_dtl_list, spa,
446fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
447fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
4483d7072f8Seschrock 	vdev_queue_init(vd);
4493d7072f8Seschrock 	vdev_cache_init(vd);
450fa9e4066Sahrens 
451fa9e4066Sahrens 	return (vd);
452fa9e4066Sahrens }
453fa9e4066Sahrens 
454fa9e4066Sahrens /*
455fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
456fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
457fa9e4066Sahrens  * different for each case.
458fa9e4066Sahrens  */
45999653d4eSeschrock int
46099653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
46199653d4eSeschrock     int alloctype)
462fa9e4066Sahrens {
463fa9e4066Sahrens 	vdev_ops_t *ops;
464fa9e4066Sahrens 	char *type;
4658654d025Sperrin 	uint64_t guid = 0, islog, nparity;
466fa9e4066Sahrens 	vdev_t *vd;
4675cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
468fa9e4066Sahrens 
469e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
470fa9e4066Sahrens 
471fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
472be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
473fa9e4066Sahrens 
474fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
475be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
476fa9e4066Sahrens 
477fa9e4066Sahrens 	/*
478fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
479fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
480fa9e4066Sahrens 	 */
481fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
482fa9e4066Sahrens 		uint64_t label_id;
483fa9e4066Sahrens 
484fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
485fa9e4066Sahrens 		    label_id != id)
486be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
487fa9e4066Sahrens 
488fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
489be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
49099653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
49199653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
492be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
493fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
494fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
495be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
49621ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
49721ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
498be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
499fa9e4066Sahrens 	}
500fa9e4066Sahrens 
50199653d4eSeschrock 	/*
50299653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
50399653d4eSeschrock 	 */
50499653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
505be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
50699653d4eSeschrock 
5078654d025Sperrin 	/*
5088654d025Sperrin 	 * Determine whether we're a log vdev.
5098654d025Sperrin 	 */
5108654d025Sperrin 	islog = 0;
5118654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
512990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
513be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
514fa9e4066Sahrens 
51588ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
516be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
51788ecc943SGeorge Wilson 
51899653d4eSeschrock 	/*
5198654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
52099653d4eSeschrock 	 */
5218654d025Sperrin 	nparity = -1ULL;
52299653d4eSeschrock 	if (ops == &vdev_raidz_ops) {
52399653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
5248654d025Sperrin 		    &nparity) == 0) {
525b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
526be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
52799653d4eSeschrock 			/*
528f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
529f94275ceSAdam Leventhal 			 * device.
53099653d4eSeschrock 			 */
531f94275ceSAdam Leventhal 			if (nparity > 1 &&
532f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
533be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
534f94275ceSAdam Leventhal 			if (nparity > 2 &&
535f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
536be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
53799653d4eSeschrock 		} else {
53899653d4eSeschrock 			/*
53999653d4eSeschrock 			 * We require the parity to be specified for SPAs that
54099653d4eSeschrock 			 * support multiple parity levels.
54199653d4eSeschrock 			 */
542f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
543be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
54499653d4eSeschrock 			/*
54599653d4eSeschrock 			 * Otherwise, we default to 1 parity device for RAID-Z.
54699653d4eSeschrock 			 */
5478654d025Sperrin 			nparity = 1;
54899653d4eSeschrock 		}
54999653d4eSeschrock 	} else {
5508654d025Sperrin 		nparity = 0;
55199653d4eSeschrock 	}
5528654d025Sperrin 	ASSERT(nparity != -1ULL);
5538654d025Sperrin 
5548654d025Sperrin 	vd = vdev_alloc_common(spa, id, guid, ops);
5555cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
5568654d025Sperrin 
5578654d025Sperrin 	vd->vdev_islog = islog;
5588654d025Sperrin 	vd->vdev_nparity = nparity;
5598654d025Sperrin 
5608654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
5618654d025Sperrin 		vd->vdev_path = spa_strdup(vd->vdev_path);
5628654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
5638654d025Sperrin 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
5648654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
5658654d025Sperrin 	    &vd->vdev_physpath) == 0)
5668654d025Sperrin 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
5676809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
5686809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
56999653d4eSeschrock 
570afefbcddSeschrock 	/*
571afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
572afefbcddSeschrock 	 * as -1.
573afefbcddSeschrock 	 */
574afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
575afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
576afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
577afefbcddSeschrock 
5785cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_mapping_object);
5795cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
5805cabbc6bSPrashanth Sreenivasa 	    &vic->vic_mapping_object);
5815cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_births_object);
5825cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
5835cabbc6bSPrashanth Sreenivasa 	    &vic->vic_births_object);
5845cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
5855cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
5865cabbc6bSPrashanth Sreenivasa 	    &vic->vic_prev_indirect_vdev);
5875cabbc6bSPrashanth Sreenivasa 
588ea8dc4b6Seschrock 	/*
589ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
590ea8dc4b6Seschrock 	 * was not present at the time of import.
591ea8dc4b6Seschrock 	 */
5926809eb4eSEric Schrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
5936809eb4eSEric Schrock 	    &vd->vdev_not_present);
594ea8dc4b6Seschrock 
595ecc2d604Sbonwick 	/*
596ecc2d604Sbonwick 	 * Get the alignment requirement.
597ecc2d604Sbonwick 	 */
598ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
599ecc2d604Sbonwick 
60088ecc943SGeorge Wilson 	/*
60188ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
60288ecc943SGeorge Wilson 	 */
60388ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
60488ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
60588ecc943SGeorge Wilson 
606fa9e4066Sahrens 	/*
607fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
608fa9e4066Sahrens 	 */
6091195e687SMark J Musante 	if (parent && !parent->vdev_parent &&
6101195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
611fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
612fa9e4066Sahrens 		    &vd->vdev_ms_array);
613fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
614fa9e4066Sahrens 		    &vd->vdev_ms_shift);
615fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
616fa9e4066Sahrens 		    &vd->vdev_asize);
6173f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
6183f9d6ad7SLin Ling 		    &vd->vdev_removing);
619215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
620215198a6SJoe Stein 		    &vd->vdev_top_zap);
621215198a6SJoe Stein 	} else {
622215198a6SJoe Stein 		ASSERT0(vd->vdev_top_zap);
623fa9e4066Sahrens 	}
624fa9e4066Sahrens 
625cd0837ccSGeorge Wilson 	if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
626a1521560SJeff Bonwick 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
6279f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ADD ||
6281195e687SMark J Musante 		    alloctype == VDEV_ALLOC_SPLIT ||
6299f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ROOTPOOL);
630a1521560SJeff Bonwick 		vd->vdev_mg = metaslab_group_create(islog ?
631a1521560SJeff Bonwick 		    spa_log_class(spa) : spa_normal_class(spa), vd);
632a1521560SJeff Bonwick 	}
633a1521560SJeff Bonwick 
634215198a6SJoe Stein 	if (vd->vdev_ops->vdev_op_leaf &&
635215198a6SJoe Stein 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
636215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv,
637215198a6SJoe Stein 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
638215198a6SJoe Stein 	} else {
639215198a6SJoe Stein 		ASSERT0(vd->vdev_leaf_zap);
640215198a6SJoe Stein 	}
641215198a6SJoe Stein 
642fa9e4066Sahrens 	/*
6433d7072f8Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
644fa9e4066Sahrens 	 */
645215198a6SJoe Stein 
646c5904d13Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
64721ecdf64SLin Ling 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
64821ecdf64SLin Ling 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
649c5904d13Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
650c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
6510713e232SGeorge Wilson 			    &vd->vdev_dtl_object);
652c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
653c5904d13Seschrock 			    &vd->vdev_unspare);
654c5904d13Seschrock 		}
65521ecdf64SLin Ling 
65621ecdf64SLin Ling 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
65721ecdf64SLin Ling 			uint64_t spare = 0;
65821ecdf64SLin Ling 
65921ecdf64SLin Ling 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
66021ecdf64SLin Ling 			    &spare) == 0 && spare)
66121ecdf64SLin Ling 				spa_spare_add(vd);
66221ecdf64SLin Ling 		}
66321ecdf64SLin Ling 
664ecc2d604Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
665ecc2d604Sbonwick 		    &vd->vdev_offline);
666c5904d13Seschrock 
667b4952e17SGeorge Wilson 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
668b4952e17SGeorge Wilson 		    &vd->vdev_resilver_txg);
669cb04b873SMark J Musante 
6703d7072f8Seschrock 		/*
6713d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
6723d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
673069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
674069f55e2SEric Schrock 		 * remain in the faulted state.
6753d7072f8Seschrock 		 */
676b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
6773d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
6783d7072f8Seschrock 			    &vd->vdev_faulted);
6793d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
6803d7072f8Seschrock 			    &vd->vdev_degraded);
6813d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
6823d7072f8Seschrock 			    &vd->vdev_removed);
683069f55e2SEric Schrock 
684069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
685069f55e2SEric Schrock 				char *aux;
686069f55e2SEric Schrock 
687069f55e2SEric Schrock 				vd->vdev_label_aux =
688069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
689069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
690069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
691069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
692069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
693069f55e2SEric Schrock 			}
6943d7072f8Seschrock 		}
695fa9e4066Sahrens 	}
696fa9e4066Sahrens 
697fa9e4066Sahrens 	/*
698fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
699fa9e4066Sahrens 	 */
700fa9e4066Sahrens 	vdev_add_child(parent, vd);
701fa9e4066Sahrens 
70299653d4eSeschrock 	*vdp = vd;
70399653d4eSeschrock 
70499653d4eSeschrock 	return (0);
705fa9e4066Sahrens }
706fa9e4066Sahrens 
707fa9e4066Sahrens void
708fa9e4066Sahrens vdev_free(vdev_t *vd)
709fa9e4066Sahrens {
7103d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
711fa9e4066Sahrens 
712fa9e4066Sahrens 	/*
713fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
714fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
715fa9e4066Sahrens 	 */
716fa9e4066Sahrens 	vdev_close(vd);
717fa9e4066Sahrens 
718e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
719b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
720fa9e4066Sahrens 
721fa9e4066Sahrens 	/*
722fa9e4066Sahrens 	 * Free all children.
723fa9e4066Sahrens 	 */
724573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
725fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
726fa9e4066Sahrens 
727fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
728fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
729fa9e4066Sahrens 
730fa9e4066Sahrens 	/*
731fa9e4066Sahrens 	 * Discard allocation state.
732fa9e4066Sahrens 	 */
733a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
734fa9e4066Sahrens 		vdev_metaslab_fini(vd);
735a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
736a1521560SJeff Bonwick 	}
737fa9e4066Sahrens 
738fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_space);
739fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_dspace);
740fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_alloc);
741fa9e4066Sahrens 
742fa9e4066Sahrens 	/*
743fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
744fa9e4066Sahrens 	 */
745fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
746fa9e4066Sahrens 
747fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
748fa9e4066Sahrens 
7493d7072f8Seschrock 	/*
7503d7072f8Seschrock 	 * Clean up vdev structure.
7513d7072f8Seschrock 	 */
7523d7072f8Seschrock 	vdev_queue_fini(vd);
7533d7072f8Seschrock 	vdev_cache_fini(vd);
7543d7072f8Seschrock 
7553d7072f8Seschrock 	if (vd->vdev_path)
7563d7072f8Seschrock 		spa_strfree(vd->vdev_path);
7573d7072f8Seschrock 	if (vd->vdev_devid)
7583d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
7593d7072f8Seschrock 	if (vd->vdev_physpath)
7603d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
7616809eb4eSEric Schrock 	if (vd->vdev_fru)
7626809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
7633d7072f8Seschrock 
7643d7072f8Seschrock 	if (vd->vdev_isspare)
7653d7072f8Seschrock 		spa_spare_remove(vd);
766fa94a07fSbrendan 	if (vd->vdev_isl2cache)
767fa94a07fSbrendan 		spa_l2cache_remove(vd);
7683d7072f8Seschrock 
7693d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
7703d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
7718ad4d6ddSJeff Bonwick 
7723d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
7730713e232SGeorge Wilson 	space_map_close(vd->vdev_dtl_sm);
7748ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
7750713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
7760713e232SGeorge Wilson 		range_tree_destroy(vd->vdev_dtl[t]);
7778ad4d6ddSJeff Bonwick 	}
7783d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
7798ad4d6ddSJeff Bonwick 
7805cabbc6bSPrashanth Sreenivasa 	EQUIV(vd->vdev_indirect_births != NULL,
7815cabbc6bSPrashanth Sreenivasa 	    vd->vdev_indirect_mapping != NULL);
7825cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_indirect_births != NULL) {
7835cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
7845cabbc6bSPrashanth Sreenivasa 		vdev_indirect_births_close(vd->vdev_indirect_births);
7855cabbc6bSPrashanth Sreenivasa 	}
7865cabbc6bSPrashanth Sreenivasa 
7875cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_obsolete_sm != NULL) {
7885cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
7895cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
7905cabbc6bSPrashanth Sreenivasa 		space_map_close(vd->vdev_obsolete_sm);
7915cabbc6bSPrashanth Sreenivasa 		vd->vdev_obsolete_sm = NULL;
7925cabbc6bSPrashanth Sreenivasa 	}
7935cabbc6bSPrashanth Sreenivasa 	range_tree_destroy(vd->vdev_obsolete_segments);
7945cabbc6bSPrashanth Sreenivasa 	rw_destroy(&vd->vdev_indirect_rwlock);
7955cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&vd->vdev_obsolete_lock);
7965cabbc6bSPrashanth Sreenivasa 
7970f7643c7SGeorge Wilson 	mutex_destroy(&vd->vdev_queue_lock);
7983d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
7993d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
800e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
8013d7072f8Seschrock 
8023d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
8033d7072f8Seschrock 		spa->spa_root_vdev = NULL;
8043d7072f8Seschrock 
8053d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
806fa9e4066Sahrens }
807fa9e4066Sahrens 
808fa9e4066Sahrens /*
809fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
810fa9e4066Sahrens  */
811fa9e4066Sahrens static void
812fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
813fa9e4066Sahrens {
814fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
815fa9e4066Sahrens 	metaslab_t *msp;
816fa9e4066Sahrens 	vdev_t *vd;
817fa9e4066Sahrens 	int t;
818fa9e4066Sahrens 
819fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
820fa9e4066Sahrens 
821fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
822fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
823fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
824215198a6SJoe Stein 	tvd->vdev_top_zap = svd->vdev_top_zap;
825fa9e4066Sahrens 
826fa9e4066Sahrens 	svd->vdev_ms_array = 0;
827fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
828fa9e4066Sahrens 	svd->vdev_ms_count = 0;
829215198a6SJoe Stein 	svd->vdev_top_zap = 0;
830fa9e4066Sahrens 
831cd0837ccSGeorge Wilson 	if (tvd->vdev_mg)
832cd0837ccSGeorge Wilson 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
833fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
834fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
835fa9e4066Sahrens 
836fa9e4066Sahrens 	svd->vdev_mg = NULL;
837fa9e4066Sahrens 	svd->vdev_ms = NULL;
838ecc2d604Sbonwick 
839ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
840ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
841fa9e4066Sahrens 
842fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
843fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
84499653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
845fa9e4066Sahrens 
846fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
847fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
84899653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
849fa9e4066Sahrens 
850fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
851fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
852fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
853fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
854fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
855fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
856fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
857fa9e4066Sahrens 	}
858fa9e4066Sahrens 
859e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
860fa9e4066Sahrens 		vdev_config_clean(svd);
861fa9e4066Sahrens 		vdev_config_dirty(tvd);
862fa9e4066Sahrens 	}
863fa9e4066Sahrens 
864e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
865e14bb325SJeff Bonwick 		vdev_state_clean(svd);
866e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
867e14bb325SJeff Bonwick 	}
868e14bb325SJeff Bonwick 
86999653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
87099653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
8718654d025Sperrin 
8728654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
8738654d025Sperrin 	svd->vdev_islog = 0;
874fa9e4066Sahrens }
875fa9e4066Sahrens 
876fa9e4066Sahrens static void
877fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
878fa9e4066Sahrens {
879fa9e4066Sahrens 	if (vd == NULL)
880fa9e4066Sahrens 		return;
881fa9e4066Sahrens 
882fa9e4066Sahrens 	vd->vdev_top = tvd;
883fa9e4066Sahrens 
884573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
885fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
886fa9e4066Sahrens }
887fa9e4066Sahrens 
888fa9e4066Sahrens /*
889fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
890fa9e4066Sahrens  */
891fa9e4066Sahrens vdev_t *
892fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
893fa9e4066Sahrens {
894fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
895fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
896fa9e4066Sahrens 	vdev_t *mvd;
897fa9e4066Sahrens 
898e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
899fa9e4066Sahrens 
900fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
901ecc2d604Sbonwick 
902ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
903573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
9044263d13fSGeorge Wilson 	mvd->vdev_max_asize = cvd->vdev_max_asize;
9055cabbc6bSPrashanth Sreenivasa 	mvd->vdev_psize = cvd->vdev_psize;
906ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
907ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
90888ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
909ecc2d604Sbonwick 
910fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
911fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
912fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
913fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
914fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
915fa9e4066Sahrens 
916fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
917fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
918fa9e4066Sahrens 
919fa9e4066Sahrens 	return (mvd);
920fa9e4066Sahrens }
921fa9e4066Sahrens 
922fa9e4066Sahrens /*
923fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
924fa9e4066Sahrens  */
925fa9e4066Sahrens void
926fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
927fa9e4066Sahrens {
928fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
929fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
930fa9e4066Sahrens 
931e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
932fa9e4066Sahrens 
933fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
934fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
93599653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
93699653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
937ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
938fa9e4066Sahrens 
939fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
940fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
9418ad4d6ddSJeff Bonwick 
94299653d4eSeschrock 	/*
943e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
944e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
945e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
946e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
94799653d4eSeschrock 	 */
9488ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
9498ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
9501195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
9518ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
9528ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
9538ad4d6ddSJeff Bonwick 	}
954e14bb325SJeff Bonwick 	cvd->vdev_id = mvd->vdev_id;
955e14bb325SJeff Bonwick 	vdev_add_child(pvd, cvd);
956fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
957fa9e4066Sahrens 
958fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
959fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
960fa9e4066Sahrens 
961fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
962fa9e4066Sahrens 	vdev_free(mvd);
963fa9e4066Sahrens }
964fa9e4066Sahrens 
965ea8dc4b6Seschrock int
966fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
967fa9e4066Sahrens {
968fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
969ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
970ecc2d604Sbonwick 	uint64_t m;
971fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
972fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
973ecc2d604Sbonwick 	metaslab_t **mspp;
974ecc2d604Sbonwick 	int error;
975fa9e4066Sahrens 
976a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
977a1521560SJeff Bonwick 
97888ecc943SGeorge Wilson 	/*
97988ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
98088ecc943SGeorge Wilson 	 */
98188ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
9820e34b6a7Sbonwick 		return (0);
9830e34b6a7Sbonwick 
98488ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
98588ecc943SGeorge Wilson 
986fa9e4066Sahrens 	ASSERT(oldc <= newc);
987fa9e4066Sahrens 
988ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
989fa9e4066Sahrens 
990ecc2d604Sbonwick 	if (oldc != 0) {
991ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
992ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
993ecc2d604Sbonwick 	}
994fa9e4066Sahrens 
995ecc2d604Sbonwick 	vd->vdev_ms = mspp;
996ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
997fa9e4066Sahrens 
998ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
9990713e232SGeorge Wilson 		uint64_t object = 0;
10000713e232SGeorge Wilson 
10015cabbc6bSPrashanth Sreenivasa 		/*
10025cabbc6bSPrashanth Sreenivasa 		 * vdev_ms_array may be 0 if we are creating the "fake"
10035cabbc6bSPrashanth Sreenivasa 		 * metaslabs for an indirect vdev for zdb's leak detection.
10045cabbc6bSPrashanth Sreenivasa 		 * See zdb_leak_init().
10055cabbc6bSPrashanth Sreenivasa 		 */
10065cabbc6bSPrashanth Sreenivasa 		if (txg == 0 && vd->vdev_ms_array != 0) {
1007ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
10087bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
10097bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
10103ee8c80cSPavel Zakharov 			if (error != 0) {
10113ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "unable to read the metaslab "
10123ee8c80cSPavel Zakharov 				    "array [error=%d]", error);
1013ecc2d604Sbonwick 				return (error);
10143ee8c80cSPavel Zakharov 			}
1015fa9e4066Sahrens 		}
10161e9bd7ecSPrakash Surya 
10171e9bd7ecSPrakash Surya 		error = metaslab_init(vd->vdev_mg, m, object, txg,
10181e9bd7ecSPrakash Surya 		    &(vd->vdev_ms[m]));
10193ee8c80cSPavel Zakharov 		if (error != 0) {
10203ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
10213ee8c80cSPavel Zakharov 			    error);
10221e9bd7ecSPrakash Surya 			return (error);
10233ee8c80cSPavel Zakharov 		}
1024fa9e4066Sahrens 	}
1025fa9e4066Sahrens 
1026a1521560SJeff Bonwick 	if (txg == 0)
1027a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1028a1521560SJeff Bonwick 
10293f9d6ad7SLin Ling 	/*
10303f9d6ad7SLin Ling 	 * If the vdev is being removed we don't activate
10313f9d6ad7SLin Ling 	 * the metaslabs since we want to ensure that no new
10323f9d6ad7SLin Ling 	 * allocations are performed on this device.
10333f9d6ad7SLin Ling 	 */
10343f9d6ad7SLin Ling 	if (oldc == 0 && !vd->vdev_removing)
1035a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
1036a1521560SJeff Bonwick 
1037a1521560SJeff Bonwick 	if (txg == 0)
1038a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
1039a1521560SJeff Bonwick 
1040ea8dc4b6Seschrock 	return (0);
1041fa9e4066Sahrens }
1042fa9e4066Sahrens 
1043fa9e4066Sahrens void
1044fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
1045fa9e4066Sahrens {
1046fa9e4066Sahrens 	if (vd->vdev_ms != NULL) {
10475cabbc6bSPrashanth Sreenivasa 		uint64_t count = vd->vdev_ms_count;
10485cabbc6bSPrashanth Sreenivasa 
1049a1521560SJeff Bonwick 		metaslab_group_passivate(vd->vdev_mg);
10505cabbc6bSPrashanth Sreenivasa 		for (uint64_t m = 0; m < count; m++) {
10510713e232SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
10520713e232SGeorge Wilson 
10530713e232SGeorge Wilson 			if (msp != NULL)
10540713e232SGeorge Wilson 				metaslab_fini(msp);
10550713e232SGeorge Wilson 		}
1056fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1057fa9e4066Sahrens 		vd->vdev_ms = NULL;
10585cabbc6bSPrashanth Sreenivasa 
10595cabbc6bSPrashanth Sreenivasa 		vd->vdev_ms_count = 0;
1060fa9e4066Sahrens 	}
10615cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_ms_count);
1062fa9e4066Sahrens }
1063fa9e4066Sahrens 
1064e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
1065e14bb325SJeff Bonwick 	boolean_t	vps_readable;
1066e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
1067e14bb325SJeff Bonwick 	int		vps_flags;
1068e14bb325SJeff Bonwick } vdev_probe_stats_t;
1069e14bb325SJeff Bonwick 
1070e14bb325SJeff Bonwick static void
1071e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
10720a4e9518Sgw {
10738ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
1074a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
1075e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
1076a3f829aeSBill Moore 
1077a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
1078e14bb325SJeff Bonwick 
1079e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
1080e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1081e14bb325SJeff Bonwick 			vps->vps_readable = 1;
10828ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
1083a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1084770499e1SDan Kimmel 			    zio->io_offset, zio->io_size, zio->io_abd,
1085e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1086e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1087e14bb325SJeff Bonwick 		} else {
1088770499e1SDan Kimmel 			abd_free(zio->io_abd);
1089e14bb325SJeff Bonwick 		}
1090e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
1091e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1092e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
1093770499e1SDan Kimmel 		abd_free(zio->io_abd);
1094e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
1095a3f829aeSBill Moore 		zio_t *pio;
1096e14bb325SJeff Bonwick 
1097e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
1098e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
1099e14bb325SJeff Bonwick 
1100e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
11018ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
1102e14bb325SJeff Bonwick 			zio->io_error = 0;
1103e14bb325SJeff Bonwick 		} else {
1104e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
11053ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "failed probe");
1106e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
11078ad4d6ddSJeff Bonwick 			    spa, vd, NULL, 0, 0);
1108be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
1109e14bb325SJeff Bonwick 		}
1110a3f829aeSBill Moore 
1111a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
1112a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
1113a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
1114a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
1115a3f829aeSBill Moore 
11160f7643c7SGeorge Wilson 		zio_link_t *zl = NULL;
11170f7643c7SGeorge Wilson 		while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1118a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
1119be6fd75aSMatthew Ahrens 				pio->io_error = SET_ERROR(ENXIO);
1120a3f829aeSBill Moore 
1121e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
1122e14bb325SJeff Bonwick 	}
1123e14bb325SJeff Bonwick }
11240a4e9518Sgw 
1125e14bb325SJeff Bonwick /*
1126f7170741SWill Andrews  * Determine whether this device is accessible.
1127f7170741SWill Andrews  *
1128f7170741SWill Andrews  * Read and write to several known locations: the pad regions of each
1129f7170741SWill Andrews  * vdev label but the first, which we leave alone in case it contains
1130f7170741SWill Andrews  * a VTOC.
1131e14bb325SJeff Bonwick  */
1132e14bb325SJeff Bonwick zio_t *
1133a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
1134e14bb325SJeff Bonwick {
1135e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1136a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
1137a3f829aeSBill Moore 	zio_t *pio;
1138a3f829aeSBill Moore 
1139a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
11400a4e9518Sgw 
1141a3f829aeSBill Moore 	/*
1142a3f829aeSBill Moore 	 * Don't probe the probe.
1143a3f829aeSBill Moore 	 */
1144a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1145a3f829aeSBill Moore 		return (NULL);
1146e14bb325SJeff Bonwick 
1147a3f829aeSBill Moore 	/*
1148a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
1149a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
1150a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
1151a3f829aeSBill Moore 	 */
1152a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
1153e14bb325SJeff Bonwick 
1154a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
1155a3f829aeSBill Moore 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1156a3f829aeSBill Moore 
1157a3f829aeSBill Moore 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1158a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
11598956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
1160a3f829aeSBill Moore 
1161a3f829aeSBill Moore 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1162a3f829aeSBill Moore 			/*
1163a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
1164a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
1165a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
1166a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
1167a3f829aeSBill Moore 			 * any zio looking at these values can assume that
1168a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
1169a3f829aeSBill Moore 			 * important because when a device has intermittent
1170a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
1171a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
1172a3f829aeSBill Moore 			 * the zio (EIO).
1173a3f829aeSBill Moore 			 *
1174a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
1175a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
1176a3f829aeSBill Moore 			 * principles.
1177a3f829aeSBill Moore 			 */
1178a3f829aeSBill Moore 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1179a3f829aeSBill Moore 			vd->vdev_cant_read = B_FALSE;
1180a3f829aeSBill Moore 			vd->vdev_cant_write = B_FALSE;
1181a3f829aeSBill Moore 		}
1182a3f829aeSBill Moore 
1183a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1184a3f829aeSBill Moore 		    vdev_probe_done, vps,
1185a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1186a3f829aeSBill Moore 
118798d1cbfeSGeorge Wilson 		/*
118898d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
118998d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
119098d1cbfeSGeorge Wilson 		 */
1191a3f829aeSBill Moore 		if (zio != NULL) {
1192a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1193a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1194a3f829aeSBill Moore 		}
1195e14bb325SJeff Bonwick 	}
1196e14bb325SJeff Bonwick 
1197a3f829aeSBill Moore 	if (zio != NULL)
1198a3f829aeSBill Moore 		zio_add_child(zio, pio);
1199e14bb325SJeff Bonwick 
1200a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1201e14bb325SJeff Bonwick 
1202a3f829aeSBill Moore 	if (vps == NULL) {
1203a3f829aeSBill Moore 		ASSERT(zio != NULL);
1204a3f829aeSBill Moore 		return (NULL);
1205a3f829aeSBill Moore 	}
1206e14bb325SJeff Bonwick 
1207e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1208a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1209e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1210770499e1SDan Kimmel 		    offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1211770499e1SDan Kimmel 		    abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1212e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1213e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1214e14bb325SJeff Bonwick 	}
1215e14bb325SJeff Bonwick 
1216a3f829aeSBill Moore 	if (zio == NULL)
1217a3f829aeSBill Moore 		return (pio);
1218a3f829aeSBill Moore 
1219a3f829aeSBill Moore 	zio_nowait(pio);
1220a3f829aeSBill Moore 	return (NULL);
12210a4e9518Sgw }
12220a4e9518Sgw 
1223f64c0e34SEric Taylor static void
1224f64c0e34SEric Taylor vdev_open_child(void *arg)
1225f64c0e34SEric Taylor {
1226f64c0e34SEric Taylor 	vdev_t *vd = arg;
1227f64c0e34SEric Taylor 
1228f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1229f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1230f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1231f64c0e34SEric Taylor }
1232f64c0e34SEric Taylor 
1233681d9761SEric Taylor boolean_t
1234681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1235681d9761SEric Taylor {
1236681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1237681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1238681d9761SEric Taylor 		return (B_TRUE);
1239681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1240681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1241681d9761SEric Taylor 			return (B_TRUE);
1242681d9761SEric Taylor 	return (B_FALSE);
1243681d9761SEric Taylor }
1244681d9761SEric Taylor 
1245f64c0e34SEric Taylor void
1246f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1247f64c0e34SEric Taylor {
1248f64c0e34SEric Taylor 	taskq_t *tq;
1249f64c0e34SEric Taylor 	int children = vd->vdev_children;
1250f64c0e34SEric Taylor 
1251681d9761SEric Taylor 	/*
1252681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1253681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1254681d9761SEric Taylor 	 * spa_namespace_lock
1255681d9761SEric Taylor 	 */
1256681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
1257681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1258681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1259681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
1260681d9761SEric Taylor 		return;
1261681d9761SEric Taylor 	}
1262f64c0e34SEric Taylor 	tq = taskq_create("vdev_open", children, minclsyspri,
1263f64c0e34SEric Taylor 	    children, children, TASKQ_PREPOPULATE);
1264f64c0e34SEric Taylor 
1265f64c0e34SEric Taylor 	for (int c = 0; c < children; c++)
1266f64c0e34SEric Taylor 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1267f64c0e34SEric Taylor 		    TQ_SLEEP) != NULL);
1268f64c0e34SEric Taylor 
1269f64c0e34SEric Taylor 	taskq_destroy(tq);
1270f64c0e34SEric Taylor }
1271f64c0e34SEric Taylor 
12725cabbc6bSPrashanth Sreenivasa /*
12735cabbc6bSPrashanth Sreenivasa  * Compute the raidz-deflation ratio.  Note, we hard-code
12745cabbc6bSPrashanth Sreenivasa  * in 128k (1 << 17) because it is the "typical" blocksize.
12755cabbc6bSPrashanth Sreenivasa  * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
12765cabbc6bSPrashanth Sreenivasa  * otherwise it would inconsistently account for existing bp's.
12775cabbc6bSPrashanth Sreenivasa  */
12785cabbc6bSPrashanth Sreenivasa static void
12795cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd)
12805cabbc6bSPrashanth Sreenivasa {
12815cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
12825cabbc6bSPrashanth Sreenivasa 		vd->vdev_deflate_ratio = (1 << 17) /
12835cabbc6bSPrashanth Sreenivasa 		    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
12845cabbc6bSPrashanth Sreenivasa 	}
12855cabbc6bSPrashanth Sreenivasa }
12865cabbc6bSPrashanth Sreenivasa 
1287fa9e4066Sahrens /*
1288fa9e4066Sahrens  * Prepare a virtual device for access.
1289fa9e4066Sahrens  */
1290fa9e4066Sahrens int
1291fa9e4066Sahrens vdev_open(vdev_t *vd)
1292fa9e4066Sahrens {
12938ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1294fa9e4066Sahrens 	int error;
1295fa9e4066Sahrens 	uint64_t osize = 0;
12964263d13fSGeorge Wilson 	uint64_t max_osize = 0;
12974263d13fSGeorge Wilson 	uint64_t asize, max_asize, psize;
1298ecc2d604Sbonwick 	uint64_t ashift = 0;
1299fa9e4066Sahrens 
1300f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1301f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1302fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1303fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1304fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1305fa9e4066Sahrens 
1306fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1307e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1308e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1309573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1310fa9e4066Sahrens 
1311069f55e2SEric Schrock 	/*
1312069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1313069f55e2SEric Schrock 	 * faulted, bail out of the open.
1314069f55e2SEric Schrock 	 */
13153d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
13163d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1317069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1318069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
13193d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1320069f55e2SEric Schrock 		    vd->vdev_label_aux);
1321be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
13223d7072f8Seschrock 	} else if (vd->vdev_offline) {
1323fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1324ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1325be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1326fa9e4066Sahrens 	}
1327fa9e4066Sahrens 
13284263d13fSGeorge Wilson 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1329fa9e4066Sahrens 
1330095bcd66SGeorge Wilson 	/*
1331095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1332095bcd66SGeorge Wilson 	 * the vdev on error.
1333095bcd66SGeorge Wilson 	 */
1334095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1335ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
13368956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1337ea8dc4b6Seschrock 
1338fa9e4066Sahrens 	if (error) {
13393d7072f8Seschrock 		if (vd->vdev_removed &&
13403d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
13413d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
13423d7072f8Seschrock 
1343*6f793812SPavel Zakharov 		if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
1344*6f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
1345*6f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
1346*6f793812SPavel Zakharov 		} else {
1347*6f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1348*6f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
1349*6f793812SPavel Zakharov 		}
1350fa9e4066Sahrens 		return (error);
1351fa9e4066Sahrens 	}
1352fa9e4066Sahrens 
13533d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
13543d7072f8Seschrock 
1355096d22d4SEric Schrock 	/*
1356096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1357096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1358096d22d4SEric Schrock 	 */
1359096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1360096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1361096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1362096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1363096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1364096d22d4SEric Schrock 		    vd->vdev_label_aux);
1365be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1366096d22d4SEric Schrock 	}
1367096d22d4SEric Schrock 
13683d7072f8Seschrock 	if (vd->vdev_degraded) {
13693d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
13703d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
13713d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
13723d7072f8Seschrock 	} else {
1373069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
13743d7072f8Seschrock 	}
1375fa9e4066Sahrens 
137688ecc943SGeorge Wilson 	/*
137788ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
137888ecc943SGeorge Wilson 	 */
137988ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
138088ecc943SGeorge Wilson 		return (0);
138188ecc943SGeorge Wilson 
1382573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1383ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1384ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1385ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1386ea8dc4b6Seschrock 			break;
1387ea8dc4b6Seschrock 		}
1388573ca77eSGeorge Wilson 	}
1389fa9e4066Sahrens 
1390fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
13914263d13fSGeorge Wilson 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1392fa9e4066Sahrens 
1393fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1394fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1395ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1396ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1397be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1398fa9e4066Sahrens 		}
1399fa9e4066Sahrens 		psize = osize;
1400fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
14014263d13fSGeorge Wilson 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
14024263d13fSGeorge Wilson 		    VDEV_LABEL_END_SIZE);
1403fa9e4066Sahrens 	} else {
1404ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1405fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1406ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1407ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1408be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1409fa9e4066Sahrens 		}
1410fa9e4066Sahrens 		psize = 0;
1411fa9e4066Sahrens 		asize = osize;
14124263d13fSGeorge Wilson 		max_asize = max_osize;
1413fa9e4066Sahrens 	}
1414fa9e4066Sahrens 
1415fa9e4066Sahrens 	vd->vdev_psize = psize;
1416fa9e4066Sahrens 
1417573ca77eSGeorge Wilson 	/*
1418c040c10cSSteven Hartland 	 * Make sure the allocatable size hasn't shrunk too much.
1419573ca77eSGeorge Wilson 	 */
1420573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1421573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1422573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1423be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1424573ca77eSGeorge Wilson 	}
1425573ca77eSGeorge Wilson 
1426fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1427fa9e4066Sahrens 		/*
1428fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
1429ecc2d604Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1430fa9e4066Sahrens 		 */
1431fa9e4066Sahrens 		vd->vdev_asize = asize;
14324263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1433ecc2d604Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1434fa9e4066Sahrens 	} else {
1435fa9e4066Sahrens 		/*
14362384d9f8SGeorge Wilson 		 * Detect if the alignment requirement has increased.
14372384d9f8SGeorge Wilson 		 * We don't want to make the pool unavailable, just
14382384d9f8SGeorge Wilson 		 * issue a warning instead.
1439fa9e4066Sahrens 		 */
14402384d9f8SGeorge Wilson 		if (ashift > vd->vdev_top->vdev_ashift &&
14412384d9f8SGeorge Wilson 		    vd->vdev_ops->vdev_op_leaf) {
14422384d9f8SGeorge Wilson 			cmn_err(CE_WARN,
14432384d9f8SGeorge Wilson 			    "Disk, '%s', has a block alignment that is "
14442384d9f8SGeorge Wilson 			    "larger than the pool's alignment\n",
14452384d9f8SGeorge Wilson 			    vd->vdev_path);
1446fa9e4066Sahrens 		}
14474263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1448573ca77eSGeorge Wilson 	}
1449fa9e4066Sahrens 
1450573ca77eSGeorge Wilson 	/*
1451c040c10cSSteven Hartland 	 * If all children are healthy we update asize if either:
1452c040c10cSSteven Hartland 	 * The asize has increased, due to a device expansion caused by dynamic
1453c040c10cSSteven Hartland 	 * LUN growth or vdev replacement, and automatic expansion is enabled;
1454c040c10cSSteven Hartland 	 * making the additional space available.
1455c040c10cSSteven Hartland 	 *
1456c040c10cSSteven Hartland 	 * The asize has decreased, due to a device shrink usually caused by a
1457c040c10cSSteven Hartland 	 * vdev replace with a smaller device. This ensures that calculations
1458c040c10cSSteven Hartland 	 * based of max_asize and asize e.g. esize are always valid. It's safe
1459c040c10cSSteven Hartland 	 * to do this as we've already validated that asize is greater than
1460c040c10cSSteven Hartland 	 * vdev_min_asize.
1461573ca77eSGeorge Wilson 	 */
1462c040c10cSSteven Hartland 	if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1463c040c10cSSteven Hartland 	    ((asize > vd->vdev_asize &&
1464c040c10cSSteven Hartland 	    (vd->vdev_expanding || spa->spa_autoexpand)) ||
1465c040c10cSSteven Hartland 	    (asize < vd->vdev_asize)))
1466573ca77eSGeorge Wilson 		vd->vdev_asize = asize;
1467fa9e4066Sahrens 
1468573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1469fa9e4066Sahrens 
14700a4e9518Sgw 	/*
14710a4e9518Sgw 	 * Ensure we can issue some IO before declaring the
14720a4e9518Sgw 	 * vdev open for business.
14730a4e9518Sgw 	 */
1474e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1475e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
147698d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
147798d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
14780a4e9518Sgw 		return (error);
14790a4e9518Sgw 	}
14800a4e9518Sgw 
14815cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
14825cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_isl2cache && !vd->vdev_islog) {
14835cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ashift > spa->spa_max_ashift)
14845cabbc6bSPrashanth Sreenivasa 			spa->spa_max_ashift = vd->vdev_ashift;
14855cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ashift < spa->spa_min_ashift)
14865cabbc6bSPrashanth Sreenivasa 			spa->spa_min_ashift = vd->vdev_ashift;
14875cabbc6bSPrashanth Sreenivasa 	}
14885cabbc6bSPrashanth Sreenivasa 
148981cd5c55SMatthew Ahrens 	/*
149081cd5c55SMatthew Ahrens 	 * Track the min and max ashift values for normal data devices.
149181cd5c55SMatthew Ahrens 	 */
149281cd5c55SMatthew Ahrens 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
149381cd5c55SMatthew Ahrens 	    !vd->vdev_islog && vd->vdev_aux == NULL) {
149481cd5c55SMatthew Ahrens 		if (vd->vdev_ashift > spa->spa_max_ashift)
149581cd5c55SMatthew Ahrens 			spa->spa_max_ashift = vd->vdev_ashift;
149681cd5c55SMatthew Ahrens 		if (vd->vdev_ashift < spa->spa_min_ashift)
149781cd5c55SMatthew Ahrens 			spa->spa_min_ashift = vd->vdev_ashift;
149881cd5c55SMatthew Ahrens 	}
149981cd5c55SMatthew Ahrens 
1500088f3894Sahrens 	/*
1501088f3894Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
15028ad4d6ddSJeff Bonwick 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
15038ad4d6ddSJeff Bonwick 	 * since this would just restart the scrub we are already doing.
1504088f3894Sahrens 	 */
15058ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
15068ad4d6ddSJeff Bonwick 	    vdev_resilver_needed(vd, NULL, NULL))
15078ad4d6ddSJeff Bonwick 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1508088f3894Sahrens 
1509fa9e4066Sahrens 	return (0);
1510fa9e4066Sahrens }
1511fa9e4066Sahrens 
1512560e6e96Seschrock /*
1513560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
1514*6f793812SPavel Zakharov  * contents. This needs to be done before vdev_load() so that we don't
15153d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1516560e6e96Seschrock  *
1517560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1518560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1519560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1520560e6e96Seschrock  * will be updated but the function will return 0.
1521560e6e96Seschrock  */
1522560e6e96Seschrock int
1523*6f793812SPavel Zakharov vdev_validate(vdev_t *vd)
1524560e6e96Seschrock {
1525560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1526560e6e96Seschrock 	nvlist_t *label;
1527*6f793812SPavel Zakharov 	uint64_t guid = 0, aux_guid = 0, top_guid;
1528560e6e96Seschrock 	uint64_t state;
1529*6f793812SPavel Zakharov 	nvlist_t *nvl;
1530*6f793812SPavel Zakharov 	uint64_t txg;
1531560e6e96Seschrock 
1532*6f793812SPavel Zakharov 	if (vdev_validate_skip)
1533*6f793812SPavel Zakharov 		return (0);
1534*6f793812SPavel Zakharov 
1535*6f793812SPavel Zakharov 	for (uint64_t c = 0; c < vd->vdev_children; c++)
1536*6f793812SPavel Zakharov 		if (vdev_validate(vd->vdev_child[c]) != 0)
1537be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1538560e6e96Seschrock 
1539b5989ec7Seschrock 	/*
1540b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1541b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1542b5989ec7Seschrock 	 * overwrite the previous state.
1543b5989ec7Seschrock 	 */
1544*6f793812SPavel Zakharov 	if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
1545*6f793812SPavel Zakharov 		return (0);
1546560e6e96Seschrock 
1547*6f793812SPavel Zakharov 	/*
1548*6f793812SPavel Zakharov 	 * If we are performing an extreme rewind, we allow for a label that
1549*6f793812SPavel Zakharov 	 * was modified at a point after the current txg.
1550*6f793812SPavel Zakharov 	 */
1551*6f793812SPavel Zakharov 	if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0)
1552*6f793812SPavel Zakharov 		txg = UINT64_MAX;
1553*6f793812SPavel Zakharov 	else
1554*6f793812SPavel Zakharov 		txg = spa_last_synced_txg(spa);
1555560e6e96Seschrock 
1556*6f793812SPavel Zakharov 	if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1557*6f793812SPavel Zakharov 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1558*6f793812SPavel Zakharov 		    VDEV_AUX_BAD_LABEL);
1559*6f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: failed reading config");
1560*6f793812SPavel Zakharov 		return (0);
1561*6f793812SPavel Zakharov 	}
15621195e687SMark J Musante 
1563*6f793812SPavel Zakharov 	/*
1564*6f793812SPavel Zakharov 	 * Determine if this vdev has been split off into another
1565*6f793812SPavel Zakharov 	 * pool.  If so, then refuse to open it.
1566*6f793812SPavel Zakharov 	 */
1567*6f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1568*6f793812SPavel Zakharov 	    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1569*6f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1570*6f793812SPavel Zakharov 		    VDEV_AUX_SPLIT_POOL);
1571*6f793812SPavel Zakharov 		nvlist_free(label);
1572*6f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
1573*6f793812SPavel Zakharov 		return (0);
1574*6f793812SPavel Zakharov 	}
1575560e6e96Seschrock 
1576*6f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
1577*6f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1578*6f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
1579*6f793812SPavel Zakharov 		nvlist_free(label);
1580*6f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1581*6f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_GUID);
1582*6f793812SPavel Zakharov 		return (0);
1583*6f793812SPavel Zakharov 	}
15841195e687SMark J Musante 
1585*6f793812SPavel Zakharov 	/*
1586*6f793812SPavel Zakharov 	 * If config is not trusted then ignore the spa guid check. This is
1587*6f793812SPavel Zakharov 	 * necessary because if the machine crashed during a re-guid the new
1588*6f793812SPavel Zakharov 	 * guid might have been written to all of the vdev labels, but not the
1589*6f793812SPavel Zakharov 	 * cached config. The check will be performed again once we have the
1590*6f793812SPavel Zakharov 	 * trusted config from the MOS.
1591*6f793812SPavel Zakharov 	 */
1592*6f793812SPavel Zakharov 	if (spa->spa_trust_config && guid != spa_guid(spa)) {
1593*6f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1594*6f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
1595*6f793812SPavel Zakharov 		nvlist_free(label);
1596*6f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
1597*6f793812SPavel Zakharov 		    "match config (%llu != %llu)", (u_longlong_t)guid,
1598*6f793812SPavel Zakharov 		    (u_longlong_t)spa_guid(spa));
1599*6f793812SPavel Zakharov 		return (0);
1600*6f793812SPavel Zakharov 	}
1601*6f793812SPavel Zakharov 
1602*6f793812SPavel Zakharov 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1603*6f793812SPavel Zakharov 	    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1604*6f793812SPavel Zakharov 	    &aux_guid) != 0)
1605*6f793812SPavel Zakharov 		aux_guid = 0;
1606*6f793812SPavel Zakharov 
1607*6f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
1608*6f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1609*6f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
1610*6f793812SPavel Zakharov 		nvlist_free(label);
1611*6f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1612*6f793812SPavel Zakharov 		    ZPOOL_CONFIG_GUID);
1613*6f793812SPavel Zakharov 		return (0);
1614*6f793812SPavel Zakharov 	}
1615*6f793812SPavel Zakharov 
1616*6f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
1617*6f793812SPavel Zakharov 	    != 0) {
1618*6f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1619*6f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
1620*6f793812SPavel Zakharov 		nvlist_free(label);
1621*6f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1622*6f793812SPavel Zakharov 		    ZPOOL_CONFIG_TOP_GUID);
1623*6f793812SPavel Zakharov 		return (0);
1624*6f793812SPavel Zakharov 	}
1625*6f793812SPavel Zakharov 
1626*6f793812SPavel Zakharov 	/*
1627*6f793812SPavel Zakharov 	 * If this vdev just became a top-level vdev because its sibling was
1628*6f793812SPavel Zakharov 	 * detached, it will have adopted the parent's vdev guid -- but the
1629*6f793812SPavel Zakharov 	 * label may or may not be on disk yet. Fortunately, either version
1630*6f793812SPavel Zakharov 	 * of the label will have the same top guid, so if we're a top-level
1631*6f793812SPavel Zakharov 	 * vdev, we can safely compare to that instead.
1632*6f793812SPavel Zakharov 	 * However, if the config comes from a cachefile that failed to update
1633*6f793812SPavel Zakharov 	 * after the detach, a top-level vdev will appear as a non top-level
1634*6f793812SPavel Zakharov 	 * vdev in the config. Also relax the constraints if we perform an
1635*6f793812SPavel Zakharov 	 * extreme rewind.
1636*6f793812SPavel Zakharov 	 *
1637*6f793812SPavel Zakharov 	 * If we split this vdev off instead, then we also check the
1638*6f793812SPavel Zakharov 	 * original pool's guid. We don't want to consider the vdev
1639*6f793812SPavel Zakharov 	 * corrupt if it is partway through a split operation.
1640*6f793812SPavel Zakharov 	 */
1641*6f793812SPavel Zakharov 	if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
1642*6f793812SPavel Zakharov 		boolean_t mismatch = B_FALSE;
1643*6f793812SPavel Zakharov 		if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
1644*6f793812SPavel Zakharov 			if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
1645*6f793812SPavel Zakharov 				mismatch = B_TRUE;
1646*6f793812SPavel Zakharov 		} else {
1647*6f793812SPavel Zakharov 			if (vd->vdev_guid != top_guid &&
1648*6f793812SPavel Zakharov 			    vd->vdev_top->vdev_guid != guid)
1649*6f793812SPavel Zakharov 				mismatch = B_TRUE;
1650560e6e96Seschrock 		}
1651560e6e96Seschrock 
1652*6f793812SPavel Zakharov 		if (mismatch) {
1653560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1654560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1655560e6e96Seschrock 			nvlist_free(label);
1656*6f793812SPavel Zakharov 			vdev_dbgmsg(vd, "vdev_validate: config guid "
1657*6f793812SPavel Zakharov 			    "doesn't match label guid");
1658*6f793812SPavel Zakharov 			vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
1659*6f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_guid,
1660*6f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_top->vdev_guid);
1661*6f793812SPavel Zakharov 			vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
1662*6f793812SPavel Zakharov 			    "aux_guid %llu", (u_longlong_t)guid,
1663*6f793812SPavel Zakharov 			    (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
1664560e6e96Seschrock 			return (0);
1665560e6e96Seschrock 		}
1666*6f793812SPavel Zakharov 	}
1667560e6e96Seschrock 
1668*6f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1669*6f793812SPavel Zakharov 	    &state) != 0) {
1670*6f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1671*6f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
1672560e6e96Seschrock 		nvlist_free(label);
1673*6f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1674*6f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_STATE);
1675*6f793812SPavel Zakharov 		return (0);
1676*6f793812SPavel Zakharov 	}
1677560e6e96Seschrock 
1678*6f793812SPavel Zakharov 	nvlist_free(label);
1679*6f793812SPavel Zakharov 
1680*6f793812SPavel Zakharov 	/*
1681*6f793812SPavel Zakharov 	 * If this is a verbatim import, no need to check the
1682*6f793812SPavel Zakharov 	 * state of the pool.
1683*6f793812SPavel Zakharov 	 */
1684*6f793812SPavel Zakharov 	if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1685*6f793812SPavel Zakharov 	    spa_load_state(spa) == SPA_LOAD_OPEN &&
1686*6f793812SPavel Zakharov 	    state != POOL_STATE_ACTIVE) {
1687*6f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
1688*6f793812SPavel Zakharov 		    "for spa %s", (u_longlong_t)state, spa->spa_name);
1689*6f793812SPavel Zakharov 		return (SET_ERROR(EBADF));
1690*6f793812SPavel Zakharov 	}
1691*6f793812SPavel Zakharov 
1692*6f793812SPavel Zakharov 	/*
1693*6f793812SPavel Zakharov 	 * If we were able to open and validate a vdev that was
1694*6f793812SPavel Zakharov 	 * previously marked permanently unavailable, clear that state
1695*6f793812SPavel Zakharov 	 * now.
1696*6f793812SPavel Zakharov 	 */
1697*6f793812SPavel Zakharov 	if (vd->vdev_not_present)
1698*6f793812SPavel Zakharov 		vd->vdev_not_present = 0;
1699*6f793812SPavel Zakharov 
1700*6f793812SPavel Zakharov 	return (0);
1701*6f793812SPavel Zakharov }
1702*6f793812SPavel Zakharov 
1703*6f793812SPavel Zakharov static void
1704*6f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
1705*6f793812SPavel Zakharov {
1706*6f793812SPavel Zakharov 	if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
1707*6f793812SPavel Zakharov 		if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
1708*6f793812SPavel Zakharov 			zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
1709*6f793812SPavel Zakharov 			    "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
1710*6f793812SPavel Zakharov 			    dvd->vdev_path, svd->vdev_path);
1711*6f793812SPavel Zakharov 			spa_strfree(dvd->vdev_path);
1712*6f793812SPavel Zakharov 			dvd->vdev_path = spa_strdup(svd->vdev_path);
17133ee8c80cSPavel Zakharov 		}
1714*6f793812SPavel Zakharov 	} else if (svd->vdev_path != NULL) {
1715*6f793812SPavel Zakharov 		dvd->vdev_path = spa_strdup(svd->vdev_path);
1716*6f793812SPavel Zakharov 		zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
1717*6f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
1718*6f793812SPavel Zakharov 	}
1719*6f793812SPavel Zakharov }
1720560e6e96Seschrock 
1721*6f793812SPavel Zakharov /*
1722*6f793812SPavel Zakharov  * Recursively copy vdev paths from one vdev to another. Source and destination
1723*6f793812SPavel Zakharov  * vdev trees must have same geometry otherwise return error. Intended to copy
1724*6f793812SPavel Zakharov  * paths from userland config into MOS config.
1725*6f793812SPavel Zakharov  */
1726*6f793812SPavel Zakharov int
1727*6f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
1728*6f793812SPavel Zakharov {
1729*6f793812SPavel Zakharov 	if ((svd->vdev_ops == &vdev_missing_ops) ||
1730*6f793812SPavel Zakharov 	    (svd->vdev_ishole && dvd->vdev_ishole) ||
1731*6f793812SPavel Zakharov 	    (dvd->vdev_ops == &vdev_indirect_ops))
1732*6f793812SPavel Zakharov 		return (0);
1733*6f793812SPavel Zakharov 
1734*6f793812SPavel Zakharov 	if (svd->vdev_ops != dvd->vdev_ops) {
1735*6f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
1736*6f793812SPavel Zakharov 		    svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
1737*6f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
1738*6f793812SPavel Zakharov 	}
1739*6f793812SPavel Zakharov 
1740*6f793812SPavel Zakharov 	if (svd->vdev_guid != dvd->vdev_guid) {
1741*6f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
1742*6f793812SPavel Zakharov 		    "%llu)", (u_longlong_t)svd->vdev_guid,
1743*6f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid);
1744*6f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
1745*6f793812SPavel Zakharov 	}
1746*6f793812SPavel Zakharov 
1747*6f793812SPavel Zakharov 	if (svd->vdev_children != dvd->vdev_children) {
1748*6f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
1749*6f793812SPavel Zakharov 		    "%llu != %llu", (u_longlong_t)svd->vdev_children,
1750*6f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_children);
1751*6f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
175251ece835Seschrock 	}
1753560e6e96Seschrock 
1754*6f793812SPavel Zakharov 	for (uint64_t i = 0; i < svd->vdev_children; i++) {
1755*6f793812SPavel Zakharov 		int error = vdev_copy_path_strict(svd->vdev_child[i],
1756*6f793812SPavel Zakharov 		    dvd->vdev_child[i]);
1757*6f793812SPavel Zakharov 		if (error != 0)
1758*6f793812SPavel Zakharov 			return (error);
1759*6f793812SPavel Zakharov 	}
1760*6f793812SPavel Zakharov 
1761*6f793812SPavel Zakharov 	if (svd->vdev_ops->vdev_op_leaf)
1762*6f793812SPavel Zakharov 		vdev_copy_path_impl(svd, dvd);
1763*6f793812SPavel Zakharov 
1764560e6e96Seschrock 	return (0);
1765560e6e96Seschrock }
1766560e6e96Seschrock 
1767*6f793812SPavel Zakharov static void
1768*6f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
1769*6f793812SPavel Zakharov {
1770*6f793812SPavel Zakharov 	ASSERT(stvd->vdev_top == stvd);
1771*6f793812SPavel Zakharov 	ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
1772*6f793812SPavel Zakharov 
1773*6f793812SPavel Zakharov 	for (uint64_t i = 0; i < dvd->vdev_children; i++) {
1774*6f793812SPavel Zakharov 		vdev_copy_path_search(stvd, dvd->vdev_child[i]);
1775*6f793812SPavel Zakharov 	}
1776*6f793812SPavel Zakharov 
1777*6f793812SPavel Zakharov 	if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
1778*6f793812SPavel Zakharov 		return;
1779*6f793812SPavel Zakharov 
1780*6f793812SPavel Zakharov 	/*
1781*6f793812SPavel Zakharov 	 * The idea here is that while a vdev can shift positions within
1782*6f793812SPavel Zakharov 	 * a top vdev (when replacing, attaching mirror, etc.) it cannot
1783*6f793812SPavel Zakharov 	 * step outside of it.
1784*6f793812SPavel Zakharov 	 */
1785*6f793812SPavel Zakharov 	vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
1786*6f793812SPavel Zakharov 
1787*6f793812SPavel Zakharov 	if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
1788*6f793812SPavel Zakharov 		return;
1789*6f793812SPavel Zakharov 
1790*6f793812SPavel Zakharov 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1791*6f793812SPavel Zakharov 
1792*6f793812SPavel Zakharov 	vdev_copy_path_impl(vd, dvd);
1793*6f793812SPavel Zakharov }
1794*6f793812SPavel Zakharov 
1795*6f793812SPavel Zakharov /*
1796*6f793812SPavel Zakharov  * Recursively copy vdev paths from one root vdev to another. Source and
1797*6f793812SPavel Zakharov  * destination vdev trees may differ in geometry. For each destination leaf
1798*6f793812SPavel Zakharov  * vdev, search a vdev with the same guid and top vdev id in the source.
1799*6f793812SPavel Zakharov  * Intended to copy paths from userland config into MOS config.
1800*6f793812SPavel Zakharov  */
1801*6f793812SPavel Zakharov void
1802*6f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
1803*6f793812SPavel Zakharov {
1804*6f793812SPavel Zakharov 	uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
1805*6f793812SPavel Zakharov 	ASSERT(srvd->vdev_ops == &vdev_root_ops);
1806*6f793812SPavel Zakharov 	ASSERT(drvd->vdev_ops == &vdev_root_ops);
1807*6f793812SPavel Zakharov 
1808*6f793812SPavel Zakharov 	for (uint64_t i = 0; i < children; i++) {
1809*6f793812SPavel Zakharov 		vdev_copy_path_search(srvd->vdev_child[i],
1810*6f793812SPavel Zakharov 		    drvd->vdev_child[i]);
1811*6f793812SPavel Zakharov 	}
1812*6f793812SPavel Zakharov }
1813*6f793812SPavel Zakharov 
1814fa9e4066Sahrens /*
1815fa9e4066Sahrens  * Close a virtual device.
1816fa9e4066Sahrens  */
1817fa9e4066Sahrens void
1818fa9e4066Sahrens vdev_close(vdev_t *vd)
1819fa9e4066Sahrens {
18208ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1821095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
18228ad4d6ddSJeff Bonwick 
18238ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
18248ad4d6ddSJeff Bonwick 
18251195e687SMark J Musante 	/*
18261195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
18271195e687SMark J Musante 	 * going offline.
18281195e687SMark J Musante 	 */
1829095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
18301195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1831095bcd66SGeorge Wilson 
1832fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1833fa9e4066Sahrens 
18343d7072f8Seschrock 	vdev_cache_purge(vd);
1835fa9e4066Sahrens 
1836560e6e96Seschrock 	/*
1837573ca77eSGeorge Wilson 	 * We record the previous state before we close it, so that if we are
1838560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1839560e6e96Seschrock 	 * it's still faulted.
1840560e6e96Seschrock 	 */
1841560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
1842560e6e96Seschrock 
1843fa9e4066Sahrens 	if (vd->vdev_offline)
1844fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1845fa9e4066Sahrens 	else
1846fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
1847ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1848fa9e4066Sahrens }
1849fa9e4066Sahrens 
1850dcba9f3fSGeorge Wilson void
1851dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
1852dcba9f3fSGeorge Wilson {
1853dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1854dcba9f3fSGeorge Wilson 
1855dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1856dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1857dcba9f3fSGeorge Wilson 		return;
1858dcba9f3fSGeorge Wilson 
1859dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1860dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
1861dcba9f3fSGeorge Wilson 
1862dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1863dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
1864dcba9f3fSGeorge Wilson }
1865dcba9f3fSGeorge Wilson 
1866dcba9f3fSGeorge Wilson void
1867dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
1868dcba9f3fSGeorge Wilson {
1869dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1870dcba9f3fSGeorge Wilson 
1871dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1872dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1873dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
1874dcba9f3fSGeorge Wilson 
1875dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1876dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
1877dcba9f3fSGeorge Wilson }
1878dcba9f3fSGeorge Wilson 
1879095bcd66SGeorge Wilson /*
1880095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1881095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
1882095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1883095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
1884095bcd66SGeorge Wilson  */
1885fa9e4066Sahrens void
1886ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
1887fa9e4066Sahrens {
1888ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
1889fa9e4066Sahrens 
1890e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1891ea8dc4b6Seschrock 
18921195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
18931195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
1894fa9e4066Sahrens 	vdev_close(vd);
1895fa9e4066Sahrens 	(void) vdev_open(vd);
1896fa9e4066Sahrens 
189739c23413Seschrock 	/*
189839c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
189939c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
190039c23413Seschrock 	 * opened in response to vdev_reopen().
190139c23413Seschrock 	 */
1902c5904d13Seschrock 	if (vd->vdev_aux) {
1903c5904d13Seschrock 		(void) vdev_validate_aux(vd);
1904e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
19056809eb4eSEric Schrock 		    vd->vdev_aux == &spa->spa_l2cache &&
1906573ca77eSGeorge Wilson 		    !l2arc_vdev_present(vd))
1907573ca77eSGeorge Wilson 			l2arc_add_vdev(spa, vd);
1908c5904d13Seschrock 	} else {
1909*6f793812SPavel Zakharov 		(void) vdev_validate(vd);
1910c5904d13Seschrock 	}
191139c23413Seschrock 
1912fa9e4066Sahrens 	/*
19133d7072f8Seschrock 	 * Reassess parent vdev's health.
1914fa9e4066Sahrens 	 */
19153d7072f8Seschrock 	vdev_propagate_state(vd);
1916fa9e4066Sahrens }
1917fa9e4066Sahrens 
1918fa9e4066Sahrens int
191999653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1920fa9e4066Sahrens {
1921fa9e4066Sahrens 	int error;
1922fa9e4066Sahrens 
1923fa9e4066Sahrens 	/*
1924fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1925fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
1926fa9e4066Sahrens 	 * there are any components we can't open.
1927fa9e4066Sahrens 	 */
1928fa9e4066Sahrens 	error = vdev_open(vd);
1929fa9e4066Sahrens 
1930fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1931fa9e4066Sahrens 		vdev_close(vd);
1932fa9e4066Sahrens 		return (error ? error : ENXIO);
1933fa9e4066Sahrens 	}
1934fa9e4066Sahrens 
1935fa9e4066Sahrens 	/*
19360713e232SGeorge Wilson 	 * Recursively load DTLs and initialize all labels.
1937fa9e4066Sahrens 	 */
19380713e232SGeorge Wilson 	if ((error = vdev_dtl_load(vd)) != 0 ||
19390713e232SGeorge Wilson 	    (error = vdev_label_init(vd, txg, isreplacing ?
194039c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1941fa9e4066Sahrens 		vdev_close(vd);
1942fa9e4066Sahrens 		return (error);
1943fa9e4066Sahrens 	}
1944fa9e4066Sahrens 
1945fa9e4066Sahrens 	return (0);
1946fa9e4066Sahrens }
1947fa9e4066Sahrens 
19480e34b6a7Sbonwick void
1949573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
1950fa9e4066Sahrens {
1951fa9e4066Sahrens 	/*
1952bf3e216cSMatthew Ahrens 	 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1953fa9e4066Sahrens 	 */
1954bf3e216cSMatthew Ahrens 	vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
1955fa9e4066Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1956fa9e4066Sahrens }
1957fa9e4066Sahrens 
1958fa9e4066Sahrens void
1959ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1960fa9e4066Sahrens {
1961ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
19625cabbc6bSPrashanth Sreenivasa 	/* indirect vdevs don't have metaslabs or dtls */
19635cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd) || flags == 0);
1964ecc2d604Sbonwick 	ASSERT(ISP2(flags));
1965f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
1966fa9e4066Sahrens 
1967ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
1968ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1969ecc2d604Sbonwick 
1970ecc2d604Sbonwick 	if (flags & VDD_DTL)
1971ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1972ecc2d604Sbonwick 
1973ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1974fa9e4066Sahrens }
1975fa9e4066Sahrens 
19760713e232SGeorge Wilson void
19770713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
19780713e232SGeorge Wilson {
19790713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
19800713e232SGeorge Wilson 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
19810713e232SGeorge Wilson 
19820713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
19830713e232SGeorge Wilson 		vdev_dirty(vd->vdev_top, flags, vd, txg);
19840713e232SGeorge Wilson }
19850713e232SGeorge Wilson 
19868ad4d6ddSJeff Bonwick /*
19878ad4d6ddSJeff Bonwick  * DTLs.
19888ad4d6ddSJeff Bonwick  *
19898ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
19909fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
19918ad4d6ddSJeff Bonwick  *
19928ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
19938ad4d6ddSJeff Bonwick  *
19948ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
19958ad4d6ddSJeff Bonwick  *
19968ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
19978ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
19988ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
19998ad4d6ddSJeff Bonwick  *
20008ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
20018ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
20028ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
20038ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
20048ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
20058ad4d6ddSJeff Bonwick  *
20068ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
20078ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
20088ad4d6ddSJeff Bonwick  *
20098ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
20108ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
20118ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
20128ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
20138ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
20148ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
20158ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
20168ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
20178ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
20188ad4d6ddSJeff Bonwick  *
20198ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
20208ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
20218ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
20228ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
20238ad4d6ddSJeff Bonwick  */
2024fa9e4066Sahrens void
20258ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2026fa9e4066Sahrens {
20270713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
20288ad4d6ddSJeff Bonwick 
20298ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
20308ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2031f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
20328ad4d6ddSJeff Bonwick 
20335cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
20340713e232SGeorge Wilson 	if (!range_tree_contains(rt, txg, size))
20350713e232SGeorge Wilson 		range_tree_add(rt, txg, size);
20365cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2037fa9e4066Sahrens }
2038fa9e4066Sahrens 
20398ad4d6ddSJeff Bonwick boolean_t
20408ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2041fa9e4066Sahrens {
20420713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
20438ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
2044fa9e4066Sahrens 
20458ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
20468ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2047fa9e4066Sahrens 
20485cabbc6bSPrashanth Sreenivasa 	/*
20495cabbc6bSPrashanth Sreenivasa 	 * While we are loading the pool, the DTLs have not been loaded yet.
20505cabbc6bSPrashanth Sreenivasa 	 * Ignore the DTLs and try all devices.  This avoids a recursive
20515cabbc6bSPrashanth Sreenivasa 	 * mutex enter on the vdev_dtl_lock, and also makes us try hard
20525cabbc6bSPrashanth Sreenivasa 	 * when loading the pool (relying on the checksum to ensure that
20535cabbc6bSPrashanth Sreenivasa 	 * we get the right data -- note that we while loading, we are
20545cabbc6bSPrashanth Sreenivasa 	 * only reading the MOS, which is always checksummed).
20555cabbc6bSPrashanth Sreenivasa 	 */
20565cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
20575cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
20585cabbc6bSPrashanth Sreenivasa 
20595cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
20600713e232SGeorge Wilson 	if (range_tree_space(rt) != 0)
20610713e232SGeorge Wilson 		dirty = range_tree_contains(rt, txg, size);
20625cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2063fa9e4066Sahrens 
2064fa9e4066Sahrens 	return (dirty);
2065fa9e4066Sahrens }
2066fa9e4066Sahrens 
20678ad4d6ddSJeff Bonwick boolean_t
20688ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
20698ad4d6ddSJeff Bonwick {
20700713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
20718ad4d6ddSJeff Bonwick 	boolean_t empty;
20728ad4d6ddSJeff Bonwick 
20735cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
20740713e232SGeorge Wilson 	empty = (range_tree_space(rt) == 0);
20755cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
20768ad4d6ddSJeff Bonwick 
20778ad4d6ddSJeff Bonwick 	return (empty);
20788ad4d6ddSJeff Bonwick }
20798ad4d6ddSJeff Bonwick 
2080b4952e17SGeorge Wilson /*
2081b4952e17SGeorge Wilson  * Returns the lowest txg in the DTL range.
2082b4952e17SGeorge Wilson  */
2083b4952e17SGeorge Wilson static uint64_t
2084b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd)
2085b4952e17SGeorge Wilson {
20860713e232SGeorge Wilson 	range_seg_t *rs;
2087b4952e17SGeorge Wilson 
2088b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
20890713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2090b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2091b4952e17SGeorge Wilson 
20920713e232SGeorge Wilson 	rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
20930713e232SGeorge Wilson 	return (rs->rs_start - 1);
2094b4952e17SGeorge Wilson }
2095b4952e17SGeorge Wilson 
2096b4952e17SGeorge Wilson /*
2097b4952e17SGeorge Wilson  * Returns the highest txg in the DTL.
2098b4952e17SGeorge Wilson  */
2099b4952e17SGeorge Wilson static uint64_t
2100b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd)
2101b4952e17SGeorge Wilson {
21020713e232SGeorge Wilson 	range_seg_t *rs;
2103b4952e17SGeorge Wilson 
2104b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
21050713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2106b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2107b4952e17SGeorge Wilson 
21080713e232SGeorge Wilson 	rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
21090713e232SGeorge Wilson 	return (rs->rs_end);
2110b4952e17SGeorge Wilson }
2111b4952e17SGeorge Wilson 
2112b4952e17SGeorge Wilson /*
2113b4952e17SGeorge Wilson  * Determine if a resilvering vdev should remove any DTL entries from
2114b4952e17SGeorge Wilson  * its range. If the vdev was resilvering for the entire duration of the
2115b4952e17SGeorge Wilson  * scan then it should excise that range from its DTLs. Otherwise, this
2116b4952e17SGeorge Wilson  * vdev is considered partially resilvered and should leave its DTL
2117b4952e17SGeorge Wilson  * entries intact. The comment in vdev_dtl_reassess() describes how we
2118b4952e17SGeorge Wilson  * excise the DTLs.
2119b4952e17SGeorge Wilson  */
2120b4952e17SGeorge Wilson static boolean_t
2121b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd)
2122b4952e17SGeorge Wilson {
2123b4952e17SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2124b4952e17SGeorge Wilson 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2125b4952e17SGeorge Wilson 
2126b4952e17SGeorge Wilson 	ASSERT0(scn->scn_phys.scn_errors);
2127b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2128b4952e17SGeorge Wilson 
21292d2f193aSMatthew Ahrens 	if (vd->vdev_state < VDEV_STATE_DEGRADED)
21302d2f193aSMatthew Ahrens 		return (B_FALSE);
21312d2f193aSMatthew Ahrens 
2132b4952e17SGeorge Wilson 	if (vd->vdev_resilver_txg == 0 ||
21330713e232SGeorge Wilson 	    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
2134b4952e17SGeorge Wilson 		return (B_TRUE);
2135b4952e17SGeorge Wilson 
2136b4952e17SGeorge Wilson 	/*
2137b4952e17SGeorge Wilson 	 * When a resilver is initiated the scan will assign the scn_max_txg
2138b4952e17SGeorge Wilson 	 * value to the highest txg value that exists in all DTLs. If this
2139b4952e17SGeorge Wilson 	 * device's max DTL is not part of this scan (i.e. it is not in
2140b4952e17SGeorge Wilson 	 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2141b4952e17SGeorge Wilson 	 * for excision.
2142b4952e17SGeorge Wilson 	 */
2143b4952e17SGeorge Wilson 	if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2144b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2145b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2146b4952e17SGeorge Wilson 		ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2147b4952e17SGeorge Wilson 		return (B_TRUE);
2148b4952e17SGeorge Wilson 	}
2149b4952e17SGeorge Wilson 	return (B_FALSE);
2150b4952e17SGeorge Wilson }
2151b4952e17SGeorge Wilson 
2152fa9e4066Sahrens /*
2153fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
2154fa9e4066Sahrens  */
2155fa9e4066Sahrens void
2156fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2157fa9e4066Sahrens {
2158ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
21598ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
21608ad4d6ddSJeff Bonwick 	int minref;
2161fa9e4066Sahrens 
21628ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2163fa9e4066Sahrens 
21648ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
21658ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
21668ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
21678ad4d6ddSJeff Bonwick 
21685cabbc6bSPrashanth Sreenivasa 	if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
21698ad4d6ddSJeff Bonwick 		return;
21708ad4d6ddSJeff Bonwick 
21718ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
21723f9d6ad7SLin Ling 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
21733f9d6ad7SLin Ling 
2174fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
2175b4952e17SGeorge Wilson 
2176b4952e17SGeorge Wilson 		/*
2177b4952e17SGeorge Wilson 		 * If we've completed a scan cleanly then determine
2178b4952e17SGeorge Wilson 		 * if this vdev should remove any DTLs. We only want to
2179b4952e17SGeorge Wilson 		 * excise regions on vdevs that were available during
2180b4952e17SGeorge Wilson 		 * the entire duration of this scan.
2181b4952e17SGeorge Wilson 		 */
2182088f3894Sahrens 		if (scrub_txg != 0 &&
21833f9d6ad7SLin Ling 		    (spa->spa_scrub_started ||
2184b4952e17SGeorge Wilson 		    (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2185b4952e17SGeorge Wilson 		    vdev_dtl_should_excise(vd)) {
2186088f3894Sahrens 			/*
2187088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
2188088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
2189088f3894Sahrens 			 * will be valid, so excise the old region and
2190088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
2191088f3894Sahrens 			 * dtl as-is if there was an error.
21928ad4d6ddSJeff Bonwick 			 *
21938ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
21948ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
21958ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
21968ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
21978ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
21988ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
21998ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
22008ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
22018ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
2202088f3894Sahrens 			 */
22030713e232SGeorge Wilson 			space_reftree_create(&reftree);
22040713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
22050713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
22060713e232SGeorge Wilson 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
22070713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
22080713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_SCRUB], 2);
22090713e232SGeorge Wilson 			space_reftree_generate_map(&reftree,
22100713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
22110713e232SGeorge Wilson 			space_reftree_destroy(&reftree);
2212fa9e4066Sahrens 		}
22130713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
22140713e232SGeorge Wilson 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
22150713e232SGeorge Wilson 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2216fa9e4066Sahrens 		if (scrub_done)
22170713e232SGeorge Wilson 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
22180713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
22198ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
22200713e232SGeorge Wilson 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
22218ad4d6ddSJeff Bonwick 		else
22220713e232SGeorge Wilson 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
22230713e232SGeorge Wilson 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2224b4952e17SGeorge Wilson 
2225b4952e17SGeorge Wilson 		/*
2226b4952e17SGeorge Wilson 		 * If the vdev was resilvering and no longer has any
2227b4952e17SGeorge Wilson 		 * DTLs then reset its resilvering flag.
2228b4952e17SGeorge Wilson 		 */
2229b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0 &&
22300713e232SGeorge Wilson 		    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
22310713e232SGeorge Wilson 		    range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
2232b4952e17SGeorge Wilson 			vd->vdev_resilver_txg = 0;
2233b4952e17SGeorge Wilson 
2234fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2235088f3894Sahrens 
2236ecc2d604Sbonwick 		if (txg != 0)
2237ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2238fa9e4066Sahrens 		return;
2239fa9e4066Sahrens 	}
2240fa9e4066Sahrens 
2241fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
22428ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
224399bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
224499bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
22458ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
22468ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
22478ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
22488ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
22498ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
22508ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
22518ad4d6ddSJeff Bonwick 		else
22528ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
22530713e232SGeorge Wilson 		space_reftree_create(&reftree);
22548ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
22558ad4d6ddSJeff Bonwick 			vdev_t *cvd = vd->vdev_child[c];
22568ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
22570713e232SGeorge Wilson 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
22588ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
22598ad4d6ddSJeff Bonwick 		}
22600713e232SGeorge Wilson 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
22610713e232SGeorge Wilson 		space_reftree_destroy(&reftree);
2262fa9e4066Sahrens 	}
22638ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
2264fa9e4066Sahrens }
2265fa9e4066Sahrens 
22660713e232SGeorge Wilson int
2267fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
2268fa9e4066Sahrens {
2269fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2270ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
22710713e232SGeorge Wilson 	int error = 0;
2272fa9e4066Sahrens 
22730713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
22745cabbc6bSPrashanth Sreenivasa 		ASSERT(vdev_is_concrete(vd));
2275fa9e4066Sahrens 
22760713e232SGeorge Wilson 		error = space_map_open(&vd->vdev_dtl_sm, mos,
22775cabbc6bSPrashanth Sreenivasa 		    vd->vdev_dtl_object, 0, -1ULL, 0);
22780713e232SGeorge Wilson 		if (error)
22790713e232SGeorge Wilson 			return (error);
22800713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2281fa9e4066Sahrens 
22820713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
228388ecc943SGeorge Wilson 
22840713e232SGeorge Wilson 		/*
22850713e232SGeorge Wilson 		 * Now that we've opened the space_map we need to update
22860713e232SGeorge Wilson 		 * the in-core DTL.
22870713e232SGeorge Wilson 		 */
22880713e232SGeorge Wilson 		space_map_update(vd->vdev_dtl_sm);
2289ecc2d604Sbonwick 
22900713e232SGeorge Wilson 		error = space_map_load(vd->vdev_dtl_sm,
22910713e232SGeorge Wilson 		    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
22920713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2293fa9e4066Sahrens 
22940713e232SGeorge Wilson 		return (error);
22950713e232SGeorge Wilson 	}
22960713e232SGeorge Wilson 
22970713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
22980713e232SGeorge Wilson 		error = vdev_dtl_load(vd->vdev_child[c]);
22990713e232SGeorge Wilson 		if (error != 0)
23000713e232SGeorge Wilson 			break;
23010713e232SGeorge Wilson 	}
2302fa9e4066Sahrens 
2303fa9e4066Sahrens 	return (error);
2304fa9e4066Sahrens }
2305fa9e4066Sahrens 
2306215198a6SJoe Stein void
2307215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2308215198a6SJoe Stein {
2309215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2310215198a6SJoe Stein 
2311215198a6SJoe Stein 	VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2312215198a6SJoe Stein 	VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2313215198a6SJoe Stein 	    zapobj, tx));
2314215198a6SJoe Stein }
2315215198a6SJoe Stein 
2316215198a6SJoe Stein uint64_t
2317215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2318215198a6SJoe Stein {
2319215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2320215198a6SJoe Stein 	uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2321215198a6SJoe Stein 	    DMU_OT_NONE, 0, tx);
2322215198a6SJoe Stein 
2323215198a6SJoe Stein 	ASSERT(zap != 0);
2324215198a6SJoe Stein 	VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2325215198a6SJoe Stein 	    zap, tx));
2326215198a6SJoe Stein 
2327215198a6SJoe Stein 	return (zap);
2328215198a6SJoe Stein }
2329215198a6SJoe Stein 
2330215198a6SJoe Stein void
2331215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2332215198a6SJoe Stein {
2333215198a6SJoe Stein 	if (vd->vdev_ops != &vdev_hole_ops &&
2334215198a6SJoe Stein 	    vd->vdev_ops != &vdev_missing_ops &&
2335215198a6SJoe Stein 	    vd->vdev_ops != &vdev_root_ops &&
2336215198a6SJoe Stein 	    !vd->vdev_top->vdev_removing) {
2337215198a6SJoe Stein 		if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2338215198a6SJoe Stein 			vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2339215198a6SJoe Stein 		}
2340215198a6SJoe Stein 		if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2341215198a6SJoe Stein 			vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2342215198a6SJoe Stein 		}
2343215198a6SJoe Stein 	}
2344215198a6SJoe Stein 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
2345215198a6SJoe Stein 		vdev_construct_zaps(vd->vdev_child[i], tx);
2346215198a6SJoe Stein 	}
2347215198a6SJoe Stein }
2348215198a6SJoe Stein 
2349fa9e4066Sahrens void
2350fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2351fa9e4066Sahrens {
2352fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
23530713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2354ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
23550713e232SGeorge Wilson 	range_tree_t *rtsync;
2356fa9e4066Sahrens 	dmu_tx_t *tx;
23570713e232SGeorge Wilson 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
2358fa9e4066Sahrens 
23595cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
23600713e232SGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
236188ecc943SGeorge Wilson 
2362fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2363fa9e4066Sahrens 
23640713e232SGeorge Wilson 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
23650713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
23660713e232SGeorge Wilson 		space_map_free(vd->vdev_dtl_sm, tx);
23670713e232SGeorge Wilson 		space_map_close(vd->vdev_dtl_sm);
23680713e232SGeorge Wilson 		vd->vdev_dtl_sm = NULL;
23690713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2370215198a6SJoe Stein 
2371215198a6SJoe Stein 		/*
2372215198a6SJoe Stein 		 * We only destroy the leaf ZAP for detached leaves or for
2373215198a6SJoe Stein 		 * removed log devices. Removed data devices handle leaf ZAP
2374215198a6SJoe Stein 		 * cleanup later, once cancellation is no longer possible.
2375215198a6SJoe Stein 		 */
2376215198a6SJoe Stein 		if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2377215198a6SJoe Stein 		    vd->vdev_top->vdev_islog)) {
2378215198a6SJoe Stein 			vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2379215198a6SJoe Stein 			vd->vdev_leaf_zap = 0;
2380215198a6SJoe Stein 		}
2381215198a6SJoe Stein 
2382fa9e4066Sahrens 		dmu_tx_commit(tx);
2383fa9e4066Sahrens 		return;
2384fa9e4066Sahrens 	}
2385fa9e4066Sahrens 
23860713e232SGeorge Wilson 	if (vd->vdev_dtl_sm == NULL) {
23870713e232SGeorge Wilson 		uint64_t new_object;
23880713e232SGeorge Wilson 
23890713e232SGeorge Wilson 		new_object = space_map_alloc(mos, tx);
23900713e232SGeorge Wilson 		VERIFY3U(new_object, !=, 0);
23910713e232SGeorge Wilson 
23920713e232SGeorge Wilson 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
23935cabbc6bSPrashanth Sreenivasa 		    0, -1ULL, 0));
23940713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2395fa9e4066Sahrens 	}
2396fa9e4066Sahrens 
23975cabbc6bSPrashanth Sreenivasa 	rtsync = range_tree_create(NULL, NULL);
2398fa9e4066Sahrens 
2399fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
24000713e232SGeorge Wilson 	range_tree_walk(rt, range_tree_add, rtsync);
2401fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
2402fa9e4066Sahrens 
24030713e232SGeorge Wilson 	space_map_truncate(vd->vdev_dtl_sm, tx);
24040713e232SGeorge Wilson 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
24050713e232SGeorge Wilson 	range_tree_vacate(rtsync, NULL, NULL);
2406fa9e4066Sahrens 
24070713e232SGeorge Wilson 	range_tree_destroy(rtsync);
2408fa9e4066Sahrens 
24090713e232SGeorge Wilson 	/*
24100713e232SGeorge Wilson 	 * If the object for the space map has changed then dirty
24110713e232SGeorge Wilson 	 * the top level so that we update the config.
24120713e232SGeorge Wilson 	 */
24130713e232SGeorge Wilson 	if (object != space_map_object(vd->vdev_dtl_sm)) {
24143ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
24153ee8c80cSPavel Zakharov 		    "new object %llu", (u_longlong_t)txg, spa_name(spa),
24163ee8c80cSPavel Zakharov 		    (u_longlong_t)object,
24173ee8c80cSPavel Zakharov 		    (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
24180713e232SGeorge Wilson 		vdev_config_dirty(vd->vdev_top);
24190713e232SGeorge Wilson 	}
2420fa9e4066Sahrens 
2421fa9e4066Sahrens 	dmu_tx_commit(tx);
24220713e232SGeorge Wilson 
24230713e232SGeorge Wilson 	mutex_enter(&vd->vdev_dtl_lock);
24240713e232SGeorge Wilson 	space_map_update(vd->vdev_dtl_sm);
24250713e232SGeorge Wilson 	mutex_exit(&vd->vdev_dtl_lock);
2426fa9e4066Sahrens }
2427fa9e4066Sahrens 
24288ad4d6ddSJeff Bonwick /*
24298ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
24308ad4d6ddSJeff Bonwick  * without losing data.
24318ad4d6ddSJeff Bonwick  */
24328ad4d6ddSJeff Bonwick boolean_t
24338ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
24348ad4d6ddSJeff Bonwick {
24358ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
24368ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
24378ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
24388ad4d6ddSJeff Bonwick 	boolean_t required;
24398ad4d6ddSJeff Bonwick 
24408ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
24418ad4d6ddSJeff Bonwick 
24428ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
24438ad4d6ddSJeff Bonwick 		return (B_TRUE);
24448ad4d6ddSJeff Bonwick 
24458ad4d6ddSJeff Bonwick 	/*
24468ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
24478ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
24488ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
24498ad4d6ddSJeff Bonwick 	 */
24508ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
24518ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
24528ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
24538ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
24548ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
24558ad4d6ddSJeff Bonwick 
2456cb04b873SMark J Musante 	if (!required && zio_injection_enabled)
2457cb04b873SMark J Musante 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2458cb04b873SMark J Musante 
24598ad4d6ddSJeff Bonwick 	return (required);
24608ad4d6ddSJeff Bonwick }
24618ad4d6ddSJeff Bonwick 
2462088f3894Sahrens /*
2463088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
2464088f3894Sahrens  */
2465088f3894Sahrens boolean_t
2466088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2467088f3894Sahrens {
2468088f3894Sahrens 	boolean_t needed = B_FALSE;
2469088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
2470088f3894Sahrens 	uint64_t thismax = 0;
2471088f3894Sahrens 
2472088f3894Sahrens 	if (vd->vdev_children == 0) {
2473088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
24740713e232SGeorge Wilson 		if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
24758ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
2476088f3894Sahrens 
2477b4952e17SGeorge Wilson 			thismin = vdev_dtl_min(vd);
2478b4952e17SGeorge Wilson 			thismax = vdev_dtl_max(vd);
2479088f3894Sahrens 			needed = B_TRUE;
2480088f3894Sahrens 		}
2481088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2482088f3894Sahrens 	} else {
24838ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
2484088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
2485088f3894Sahrens 			uint64_t cmin, cmax;
2486088f3894Sahrens 
2487088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2488088f3894Sahrens 				thismin = MIN(thismin, cmin);
2489088f3894Sahrens 				thismax = MAX(thismax, cmax);
2490088f3894Sahrens 				needed = B_TRUE;
2491088f3894Sahrens 			}
2492088f3894Sahrens 		}
2493088f3894Sahrens 	}
2494088f3894Sahrens 
2495088f3894Sahrens 	if (needed && minp) {
2496088f3894Sahrens 		*minp = thismin;
2497088f3894Sahrens 		*maxp = thismax;
2498088f3894Sahrens 	}
2499088f3894Sahrens 	return (needed);
2500088f3894Sahrens }
2501088f3894Sahrens 
25025cabbc6bSPrashanth Sreenivasa int
2503ea8dc4b6Seschrock vdev_load(vdev_t *vd)
2504fa9e4066Sahrens {
25055cabbc6bSPrashanth Sreenivasa 	int error = 0;
2506fa9e4066Sahrens 	/*
2507fa9e4066Sahrens 	 * Recursively load all children.
2508fa9e4066Sahrens 	 */
25095cabbc6bSPrashanth Sreenivasa 	for (int c = 0; c < vd->vdev_children; c++) {
25105cabbc6bSPrashanth Sreenivasa 		error = vdev_load(vd->vdev_child[c]);
25115cabbc6bSPrashanth Sreenivasa 		if (error != 0) {
25125cabbc6bSPrashanth Sreenivasa 			return (error);
25135cabbc6bSPrashanth Sreenivasa 		}
25145cabbc6bSPrashanth Sreenivasa 	}
25155cabbc6bSPrashanth Sreenivasa 
25165cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
2517fa9e4066Sahrens 
2518fa9e4066Sahrens 	/*
25190e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
2520fa9e4066Sahrens 	 */
25215cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
25225cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
25235cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
25245cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
25253ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
25263ee8c80cSPavel Zakharov 			    "asize=%llu", (u_longlong_t)vd->vdev_ashift,
25273ee8c80cSPavel Zakharov 			    (u_longlong_t)vd->vdev_asize);
25285cabbc6bSPrashanth Sreenivasa 			return (SET_ERROR(ENXIO));
25295cabbc6bSPrashanth Sreenivasa 		} else if ((error = vdev_metaslab_init(vd, 0)) != 0) {
25303ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
25313ee8c80cSPavel Zakharov 			    "[error=%d]", error);
25325cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
25335cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
25345cabbc6bSPrashanth Sreenivasa 			return (error);
25355cabbc6bSPrashanth Sreenivasa 		}
25365cabbc6bSPrashanth Sreenivasa 	}
2537fa9e4066Sahrens 
2538fa9e4066Sahrens 	/*
2539fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
2540fa9e4066Sahrens 	 */
25415cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
2542560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2543560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
25443ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
25453ee8c80cSPavel Zakharov 		    "[error=%d]", error);
25465cabbc6bSPrashanth Sreenivasa 		return (error);
25475cabbc6bSPrashanth Sreenivasa 	}
25485cabbc6bSPrashanth Sreenivasa 
25495cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
25505cabbc6bSPrashanth Sreenivasa 	if (obsolete_sm_object != 0) {
25515cabbc6bSPrashanth Sreenivasa 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
25525cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_asize != 0);
25535cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_obsolete_sm == NULL);
25545cabbc6bSPrashanth Sreenivasa 
25555cabbc6bSPrashanth Sreenivasa 		if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
25565cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_object, 0, vd->vdev_asize, 0))) {
25575cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
25585cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
25593ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
25603ee8c80cSPavel Zakharov 			    "obsolete spacemap (obj %llu) [error=%d]",
25613ee8c80cSPavel Zakharov 			    (u_longlong_t)obsolete_sm_object, error);
25625cabbc6bSPrashanth Sreenivasa 			return (error);
25635cabbc6bSPrashanth Sreenivasa 		}
25645cabbc6bSPrashanth Sreenivasa 		space_map_update(vd->vdev_obsolete_sm);
25655cabbc6bSPrashanth Sreenivasa 	}
25665cabbc6bSPrashanth Sreenivasa 
25675cabbc6bSPrashanth Sreenivasa 	return (0);
2568fa9e4066Sahrens }
2569fa9e4066Sahrens 
257099653d4eSeschrock /*
2571fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
2572fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
2573fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
2574fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
2575fa94a07fSbrendan  * repurposed to another pool.
257699653d4eSeschrock  */
257799653d4eSeschrock int
2578fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
257999653d4eSeschrock {
258099653d4eSeschrock 	nvlist_t *label;
258199653d4eSeschrock 	uint64_t guid, version;
258299653d4eSeschrock 	uint64_t state;
258399653d4eSeschrock 
2584e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
2585c5904d13Seschrock 		return (0);
2586c5904d13Seschrock 
2587dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
258899653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
258999653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
259099653d4eSeschrock 		return (-1);
259199653d4eSeschrock 	}
259299653d4eSeschrock 
259399653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2594ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(version) ||
259599653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
259699653d4eSeschrock 	    guid != vd->vdev_guid ||
259799653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
259899653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
259999653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
260099653d4eSeschrock 		nvlist_free(label);
260199653d4eSeschrock 		return (-1);
260299653d4eSeschrock 	}
260399653d4eSeschrock 
260499653d4eSeschrock 	/*
260599653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
260699653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
260799653d4eSeschrock 	 */
260899653d4eSeschrock 	nvlist_free(label);
260999653d4eSeschrock 	return (0);
261099653d4eSeschrock }
261199653d4eSeschrock 
26125cabbc6bSPrashanth Sreenivasa /*
26135cabbc6bSPrashanth Sreenivasa  * Free the objects used to store this vdev's spacemaps, and the array
26145cabbc6bSPrashanth Sreenivasa  * that points to them.
26155cabbc6bSPrashanth Sreenivasa  */
261688ecc943SGeorge Wilson void
26175cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
26185cabbc6bSPrashanth Sreenivasa {
26195cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0)
26205cabbc6bSPrashanth Sreenivasa 		return;
26215cabbc6bSPrashanth Sreenivasa 
26225cabbc6bSPrashanth Sreenivasa 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
26235cabbc6bSPrashanth Sreenivasa 	uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
26245cabbc6bSPrashanth Sreenivasa 	size_t array_bytes = array_count * sizeof (uint64_t);
26255cabbc6bSPrashanth Sreenivasa 	uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
26265cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
26275cabbc6bSPrashanth Sreenivasa 	    array_bytes, smobj_array, 0));
26285cabbc6bSPrashanth Sreenivasa 
26295cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < array_count; i++) {
26305cabbc6bSPrashanth Sreenivasa 		uint64_t smobj = smobj_array[i];
26315cabbc6bSPrashanth Sreenivasa 		if (smobj == 0)
26325cabbc6bSPrashanth Sreenivasa 			continue;
26335cabbc6bSPrashanth Sreenivasa 
26345cabbc6bSPrashanth Sreenivasa 		space_map_free_obj(mos, smobj, tx);
26355cabbc6bSPrashanth Sreenivasa 	}
26365cabbc6bSPrashanth Sreenivasa 
26375cabbc6bSPrashanth Sreenivasa 	kmem_free(smobj_array, array_bytes);
26385cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
26395cabbc6bSPrashanth Sreenivasa 	vd->vdev_ms_array = 0;
26405cabbc6bSPrashanth Sreenivasa }
26415cabbc6bSPrashanth Sreenivasa 
26425cabbc6bSPrashanth Sreenivasa static void
26435cabbc6bSPrashanth Sreenivasa vdev_remove_empty(vdev_t *vd, uint64_t txg)
264488ecc943SGeorge Wilson {
264588ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
264688ecc943SGeorge Wilson 	dmu_tx_t *tx;
264788ecc943SGeorge Wilson 
2648215198a6SJoe Stein 	ASSERT(vd == vd->vdev_top);
2649215198a6SJoe Stein 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
265088ecc943SGeorge Wilson 
265188ecc943SGeorge Wilson 	if (vd->vdev_ms != NULL) {
26522e4c9986SGeorge Wilson 		metaslab_group_t *mg = vd->vdev_mg;
26532e4c9986SGeorge Wilson 
26542e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
26552e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
26562e4c9986SGeorge Wilson 
265788ecc943SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
265888ecc943SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
265988ecc943SGeorge Wilson 
26600713e232SGeorge Wilson 			if (msp == NULL || msp->ms_sm == NULL)
266188ecc943SGeorge Wilson 				continue;
266288ecc943SGeorge Wilson 
26630713e232SGeorge Wilson 			mutex_enter(&msp->ms_lock);
26642e4c9986SGeorge Wilson 			/*
26652e4c9986SGeorge Wilson 			 * If the metaslab was not loaded when the vdev
26662e4c9986SGeorge Wilson 			 * was removed then the histogram accounting may
26672e4c9986SGeorge Wilson 			 * not be accurate. Update the histogram information
26682e4c9986SGeorge Wilson 			 * here so that we ensure that the metaslab group
26692e4c9986SGeorge Wilson 			 * and metaslab class are up-to-date.
26702e4c9986SGeorge Wilson 			 */
26712e4c9986SGeorge Wilson 			metaslab_group_histogram_remove(mg, msp);
26722e4c9986SGeorge Wilson 
26730713e232SGeorge Wilson 			VERIFY0(space_map_allocated(msp->ms_sm));
26740713e232SGeorge Wilson 			space_map_close(msp->ms_sm);
26750713e232SGeorge Wilson 			msp->ms_sm = NULL;
26760713e232SGeorge Wilson 			mutex_exit(&msp->ms_lock);
267788ecc943SGeorge Wilson 		}
26782e4c9986SGeorge Wilson 
26792e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
26802e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
26812e4c9986SGeorge Wilson 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
26822e4c9986SGeorge Wilson 			ASSERT0(mg->mg_histogram[i]);
268388ecc943SGeorge Wilson 	}
268488ecc943SGeorge Wilson 
26855cabbc6bSPrashanth Sreenivasa 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
26865cabbc6bSPrashanth Sreenivasa 	vdev_destroy_spacemaps(vd, tx);
2687215198a6SJoe Stein 
2688215198a6SJoe Stein 	if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2689215198a6SJoe Stein 		vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2690215198a6SJoe Stein 		vd->vdev_top_zap = 0;
2691215198a6SJoe Stein 	}
269288ecc943SGeorge Wilson 	dmu_tx_commit(tx);
269388ecc943SGeorge Wilson }
269488ecc943SGeorge Wilson 
2695fa9e4066Sahrens void
2696fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
2697fa9e4066Sahrens {
2698fa9e4066Sahrens 	metaslab_t *msp;
269980eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2700fa9e4066Sahrens 
27015cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
270288ecc943SGeorge Wilson 
2703fa9e4066Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2704fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
270580eb36f2SGeorge Wilson 
270680eb36f2SGeorge Wilson 	if (reassess)
270780eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
2708fa9e4066Sahrens }
2709fa9e4066Sahrens 
2710fa9e4066Sahrens void
2711fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2712fa9e4066Sahrens {
2713fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2714fa9e4066Sahrens 	vdev_t *lvd;
2715fa9e4066Sahrens 	metaslab_t *msp;
2716ecc2d604Sbonwick 	dmu_tx_t *tx;
2717fa9e4066Sahrens 
27185cabbc6bSPrashanth Sreenivasa 	if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
27195cabbc6bSPrashanth Sreenivasa 		dmu_tx_t *tx;
27205cabbc6bSPrashanth Sreenivasa 
27215cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
27225cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
272388ecc943SGeorge Wilson 
27245cabbc6bSPrashanth Sreenivasa 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
27255cabbc6bSPrashanth Sreenivasa 		vdev_indirect_sync_obsolete(vd, tx);
27265cabbc6bSPrashanth Sreenivasa 		dmu_tx_commit(tx);
27275cabbc6bSPrashanth Sreenivasa 
27285cabbc6bSPrashanth Sreenivasa 		/*
27295cabbc6bSPrashanth Sreenivasa 		 * If the vdev is indirect, it can't have dirty
27305cabbc6bSPrashanth Sreenivasa 		 * metaslabs or DTLs.
27315cabbc6bSPrashanth Sreenivasa 		 */
27325cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ops == &vdev_indirect_ops) {
27335cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
27345cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
27355cabbc6bSPrashanth Sreenivasa 			return;
27365cabbc6bSPrashanth Sreenivasa 		}
27375cabbc6bSPrashanth Sreenivasa 	}
27385cabbc6bSPrashanth Sreenivasa 
27395cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
27405cabbc6bSPrashanth Sreenivasa 
27415cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
27425cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_removing) {
2743ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
27445cabbc6bSPrashanth Sreenivasa 		ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
2745ecc2d604Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2746ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2747ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2748ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
2749ecc2d604Sbonwick 		vdev_config_dirty(vd);
2750ecc2d604Sbonwick 		dmu_tx_commit(tx);
2751ecc2d604Sbonwick 	}
2752fa9e4066Sahrens 
2753ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2754fa9e4066Sahrens 		metaslab_sync(msp, txg);
2755ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2756ecc2d604Sbonwick 	}
2757fa9e4066Sahrens 
2758fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2759fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
2760fa9e4066Sahrens 
27615cabbc6bSPrashanth Sreenivasa 	/*
27625cabbc6bSPrashanth Sreenivasa 	 * Remove the metadata associated with this vdev once it's empty.
27635cabbc6bSPrashanth Sreenivasa 	 * Note that this is typically used for log/cache device removal;
27645cabbc6bSPrashanth Sreenivasa 	 * we don't empty toplevel vdevs when removing them.  But if
27655cabbc6bSPrashanth Sreenivasa 	 * a toplevel happens to be emptied, this is not harmful.
27665cabbc6bSPrashanth Sreenivasa 	 */
27675cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) {
27685cabbc6bSPrashanth Sreenivasa 		vdev_remove_empty(vd, txg);
27695cabbc6bSPrashanth Sreenivasa 	}
27705cabbc6bSPrashanth Sreenivasa 
2771fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2772fa9e4066Sahrens }
2773fa9e4066Sahrens 
2774fa9e4066Sahrens uint64_t
2775fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2776fa9e4066Sahrens {
2777fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2778fa9e4066Sahrens }
2779fa9e4066Sahrens 
27803d7072f8Seschrock /*
27813d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
27823d7072f8Seschrock  * not be opened, and no I/O is attempted.
27833d7072f8Seschrock  */
2784fa9e4066Sahrens int
2785069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2786fa9e4066Sahrens {
27874b964adaSGeorge Wilson 	vdev_t *vd, *tvd;
2788fa9e4066Sahrens 
27898f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
2790fa9e4066Sahrens 
2791c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2792e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2793e14bb325SJeff Bonwick 
27943d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2795e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2796fa9e4066Sahrens 
27974b964adaSGeorge Wilson 	tvd = vd->vdev_top;
27984b964adaSGeorge Wilson 
2799069f55e2SEric Schrock 	/*
2800069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
2801069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
2802069f55e2SEric Schrock 	 * were faulted.
2803069f55e2SEric Schrock 	 */
2804069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
2805069f55e2SEric Schrock 
28063d7072f8Seschrock 	/*
28073d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
28083d7072f8Seschrock 	 */
280998d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
28103d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
28113d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
2812069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
28133d7072f8Seschrock 
28143d7072f8Seschrock 	/*
2815c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
2816c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
28173d7072f8Seschrock 	 */
28184b964adaSGeorge Wilson 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
28193d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
28203d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
28213d7072f8Seschrock 
28223d7072f8Seschrock 		/*
28233d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
28243d7072f8Seschrock 		 * mark it degraded.
28253d7072f8Seschrock 		 */
28264b964adaSGeorge Wilson 		vdev_reopen(tvd);
28273d7072f8Seschrock 
2828069f55e2SEric Schrock 		if (vdev_readable(vd))
2829069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
28303d7072f8Seschrock 	}
28313d7072f8Seschrock 
2832e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
28333d7072f8Seschrock }
28343d7072f8Seschrock 
28353d7072f8Seschrock /*
28363d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
28373d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
28383d7072f8Seschrock  * as I/O is concerned.
28393d7072f8Seschrock  */
28403d7072f8Seschrock int
2841069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
28423d7072f8Seschrock {
2843c5904d13Seschrock 	vdev_t *vd;
28440a4e9518Sgw 
28458f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
28463d7072f8Seschrock 
2847c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2848e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2849e14bb325SJeff Bonwick 
28500e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2851e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
28520e34b6a7Sbonwick 
28533d7072f8Seschrock 	/*
28543d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
28553d7072f8Seschrock 	 */
2856e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
2857e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
28583d7072f8Seschrock 
28593d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
28603d7072f8Seschrock 	if (!vdev_is_dead(vd))
28613d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2862069f55e2SEric Schrock 		    aux);
28633d7072f8Seschrock 
2864e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
28653d7072f8Seschrock }
28663d7072f8Seschrock 
28673d7072f8Seschrock /*
2868f7170741SWill Andrews  * Online the given vdev.
2869f7170741SWill Andrews  *
2870f7170741SWill Andrews  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
2871f7170741SWill Andrews  * spare device should be detached when the device finishes resilvering.
2872f7170741SWill Andrews  * Second, the online should be treated like a 'test' online case, so no FMA
2873f7170741SWill Andrews  * events are generated if the device fails to open.
28743d7072f8Seschrock  */
28753d7072f8Seschrock int
2876e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
28773d7072f8Seschrock {
2878573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
28795f368aefSYuri Pankov 	boolean_t wasoffline;
28805f368aefSYuri Pankov 	vdev_state_t oldstate;
28813d7072f8Seschrock 
28828f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
28833d7072f8Seschrock 
2884c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2885e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
28863d7072f8Seschrock 
28873d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2888e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2889fa9e4066Sahrens 
28905f368aefSYuri Pankov 	wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
28915f368aefSYuri Pankov 	oldstate = vd->vdev_state;
289214372834SHans Rosenfeld 
2893573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
2894fa9e4066Sahrens 	vd->vdev_offline = B_FALSE;
2895441d80aaSlling 	vd->vdev_tmpoffline = B_FALSE;
2896e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2897e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2898573ca77eSGeorge Wilson 
2899573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
2900573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2901573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2902573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2903573ca77eSGeorge Wilson 	}
2904573ca77eSGeorge Wilson 
2905573ca77eSGeorge Wilson 	vdev_reopen(tvd);
29063d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
29073d7072f8Seschrock 
2908573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2909573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2910573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
2911573ca77eSGeorge Wilson 	}
2912573ca77eSGeorge Wilson 
29133d7072f8Seschrock 	if (newstate)
29143d7072f8Seschrock 		*newstate = vd->vdev_state;
29153d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
29163d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
29173d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
29183d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
29193d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
2920fa9e4066Sahrens 
2921573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2922573ca77eSGeorge Wilson 
2923573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
2924573ca77eSGeorge Wilson 		if (vd->vdev_aux)
2925573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2926573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2927573ca77eSGeorge Wilson 	}
292814372834SHans Rosenfeld 
29295f368aefSYuri Pankov 	if (wasoffline ||
29305f368aefSYuri Pankov 	    (oldstate < VDEV_STATE_DEGRADED &&
29315f368aefSYuri Pankov 	    vd->vdev_state >= VDEV_STATE_DEGRADED))
2932ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
293314372834SHans Rosenfeld 
29348ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
2935fa9e4066Sahrens }
2936fa9e4066Sahrens 
2937a1521560SJeff Bonwick static int
2938a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2939fa9e4066Sahrens {
2940e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
29418f18d1faSGeorge Wilson 	int error = 0;
29428f18d1faSGeorge Wilson 	uint64_t generation;
29438f18d1faSGeorge Wilson 	metaslab_group_t *mg;
29440a4e9518Sgw 
29458f18d1faSGeorge Wilson top:
29468f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
2947fa9e4066Sahrens 
2948c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2949e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2950fa9e4066Sahrens 
29510e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2952e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
29530e34b6a7Sbonwick 
2954e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
29558f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
29568f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
2957e6ca193dSGeorge Wilson 
2958fa9e4066Sahrens 	/*
2959ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
2960fa9e4066Sahrens 	 */
2961ecc2d604Sbonwick 	if (!vd->vdev_offline) {
2962ecc2d604Sbonwick 		/*
29638ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
2964e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
2965e6ca193dSGeorge Wilson 		 * expendable.
2966ecc2d604Sbonwick 		 */
2967e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2968e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
2969e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2970fa9e4066Sahrens 
29718f18d1faSGeorge Wilson 		/*
2972b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
2973b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
2974b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
2975b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
29768f18d1faSGeorge Wilson 		 */
29778f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
29788f18d1faSGeorge Wilson 			/*
29798f18d1faSGeorge Wilson 			 * Prevent any future allocations.
29808f18d1faSGeorge Wilson 			 */
2981a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
29828f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
29838f18d1faSGeorge Wilson 
29845cabbc6bSPrashanth Sreenivasa 			error = spa_reset_logs(spa);
29858f18d1faSGeorge Wilson 
29868f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
29878f18d1faSGeorge Wilson 
29888f18d1faSGeorge Wilson 			/*
29898f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
29908f18d1faSGeorge Wilson 			 */
29918f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
2992a1521560SJeff Bonwick 				metaslab_group_activate(mg);
29938f18d1faSGeorge Wilson 				if (error)
29948f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
29958f18d1faSGeorge Wilson 					    vd, error));
29968f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
29978f18d1faSGeorge Wilson 				goto top;
29988f18d1faSGeorge Wilson 			}
2999fb09f5aaSMadhav Suresh 			ASSERT0(tvd->vdev_stat.vs_alloc);
30008f18d1faSGeorge Wilson 		}
30018f18d1faSGeorge Wilson 
3002ecc2d604Sbonwick 		/*
3003ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
3004e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
3005e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
3006e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
3007ecc2d604Sbonwick 		 */
3008ecc2d604Sbonwick 		vd->vdev_offline = B_TRUE;
3009e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
3010e6ca193dSGeorge Wilson 
3011e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3012e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
3013ecc2d604Sbonwick 			vd->vdev_offline = B_FALSE;
3014e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
3015e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3016ecc2d604Sbonwick 		}
30178f18d1faSGeorge Wilson 
30188f18d1faSGeorge Wilson 		/*
30198f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
30208f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
30218f18d1faSGeorge Wilson 		 */
30228f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
3023a1521560SJeff Bonwick 			metaslab_group_activate(mg);
3024fa9e4066Sahrens 	}
3025fa9e4066Sahrens 
3026e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3027ecc2d604Sbonwick 
30288f18d1faSGeorge Wilson 	return (spa_vdev_state_exit(spa, vd, 0));
3029fa9e4066Sahrens }
3030fa9e4066Sahrens 
3031a1521560SJeff Bonwick int
3032a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3033a1521560SJeff Bonwick {
3034a1521560SJeff Bonwick 	int error;
3035a1521560SJeff Bonwick 
3036a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
3037a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
3038a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
3039a1521560SJeff Bonwick 
3040a1521560SJeff Bonwick 	return (error);
3041a1521560SJeff Bonwick }
3042a1521560SJeff Bonwick 
3043ea8dc4b6Seschrock /*
3044ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
3045ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
3046ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
3047ea8dc4b6Seschrock  */
3048ea8dc4b6Seschrock void
3049e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
3050fa9e4066Sahrens {
3051e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3052e14bb325SJeff Bonwick 
3053e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3054fa9e4066Sahrens 
3055ea8dc4b6Seschrock 	if (vd == NULL)
3056e14bb325SJeff Bonwick 		vd = rvd;
3057fa9e4066Sahrens 
3058ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
3059ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
3060ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
3061fa9e4066Sahrens 
3062e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
3063e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
30643d7072f8Seschrock 
30655cabbc6bSPrashanth Sreenivasa 	/*
30665cabbc6bSPrashanth Sreenivasa 	 * It makes no sense to "clear" an indirect vdev.
30675cabbc6bSPrashanth Sreenivasa 	 */
30685cabbc6bSPrashanth Sreenivasa 	if (!vdev_is_concrete(vd))
30695cabbc6bSPrashanth Sreenivasa 		return;
30705cabbc6bSPrashanth Sreenivasa 
30713d7072f8Seschrock 	/*
30728a79c1b5Sek 	 * If we're in the FAULTED state or have experienced failed I/O, then
30738a79c1b5Sek 	 * clear the persistent state and attempt to reopen the device.  We
30748a79c1b5Sek 	 * also mark the vdev config dirty, so that the new faulted state is
30758a79c1b5Sek 	 * written out to disk.
30763d7072f8Seschrock 	 */
3077e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
3078e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
30798a79c1b5Sek 
3080096d22d4SEric Schrock 		/*
3081096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
3082096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
3083096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
3084096d22d4SEric Schrock 		 */
3085096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
3086096d22d4SEric Schrock 
30874b964adaSGeorge Wilson 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3088e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
3089e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
3090e14bb325SJeff Bonwick 
3091f9af39baSGeorge Wilson 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
30923d7072f8Seschrock 
3093096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
3094096d22d4SEric Schrock 
3095f9af39baSGeorge Wilson 		if (vd != rvd && vdev_writeable(vd->vdev_top))
3096e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
3097e14bb325SJeff Bonwick 
3098e14bb325SJeff Bonwick 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
3099bb8b5132Sek 			spa_async_request(spa, SPA_ASYNC_RESILVER);
31003d7072f8Seschrock 
3101ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
31023d7072f8Seschrock 	}
3103096d22d4SEric Schrock 
3104096d22d4SEric Schrock 	/*
3105096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
3106096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
3107096d22d4SEric Schrock 	 * done in response to the FMA fault.
3108096d22d4SEric Schrock 	 */
3109096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3110096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3111096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
3112096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
3113fa9e4066Sahrens }
3114fa9e4066Sahrens 
3115e14bb325SJeff Bonwick boolean_t
3116e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd)
31170a4e9518Sgw {
311888ecc943SGeorge Wilson 	/*
311988ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
312088ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
312188ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
312288ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
312388ecc943SGeorge Wilson 	 * before issuing I/O to them.
312488ecc943SGeorge Wilson 	 */
31255cabbc6bSPrashanth Sreenivasa 	return (vd->vdev_state < VDEV_STATE_DEGRADED ||
31265cabbc6bSPrashanth Sreenivasa 	    vd->vdev_ops == &vdev_hole_ops ||
312788ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
31280a4e9518Sgw }
31290a4e9518Sgw 
3130e14bb325SJeff Bonwick boolean_t
3131e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
31320a4e9518Sgw {
3133e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
31340a4e9518Sgw }
31350a4e9518Sgw 
3136e14bb325SJeff Bonwick boolean_t
3137e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
3138fa9e4066Sahrens {
31395cabbc6bSPrashanth Sreenivasa 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
31405cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd));
3141fa9e4066Sahrens }
3142fa9e4066Sahrens 
3143a31e6787SGeorge Wilson boolean_t
3144a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
3145a31e6787SGeorge Wilson {
31468ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
31478ad4d6ddSJeff Bonwick 
3148a31e6787SGeorge Wilson 	/*
31498ad4d6ddSJeff Bonwick 	 * We currently allow allocations from vdevs which may be in the
3150a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3151a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
31528ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
31538ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
31548ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
3155a31e6787SGeorge Wilson 	 */
31568ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
31575cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_cant_write && vdev_is_concrete(vd) &&
31580f7643c7SGeorge Wilson 	    vd->vdev_mg->mg_initialized);
3159a31e6787SGeorge Wilson }
3160a31e6787SGeorge Wilson 
3161e14bb325SJeff Bonwick boolean_t
3162e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
3163fa9e4066Sahrens {
3164e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
3165fa9e4066Sahrens 
3166e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3167e14bb325SJeff Bonwick 		return (B_FALSE);
3168fa9e4066Sahrens 
3169e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
3170e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
3171fa9e4066Sahrens 
3172e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
3173e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
3174fa9e4066Sahrens 
3175e14bb325SJeff Bonwick 	return (B_TRUE);
3176fa9e4066Sahrens }
3177fa9e4066Sahrens 
3178fa9e4066Sahrens /*
3179fa9e4066Sahrens  * Get statistics for the given vdev.
3180fa9e4066Sahrens  */
3181fa9e4066Sahrens void
3182fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3183fa9e4066Sahrens {
31842e4c9986SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
31852e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
3186c39a2aaeSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
31872e4c9986SGeorge Wilson 
31882e4c9986SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3189fa9e4066Sahrens 
3190fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
3191fa9e4066Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3192fa9e4066Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3193fa9e4066Sahrens 	vs->vs_state = vd->vdev_state;
3194573ca77eSGeorge Wilson 	vs->vs_rsize = vdev_get_min_asize(vd);
3195573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
3196573ca77eSGeorge Wilson 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
3197c39a2aaeSGeorge Wilson 	/*
3198c39a2aaeSGeorge Wilson 	 * Report expandable space on top-level, non-auxillary devices only.
3199c39a2aaeSGeorge Wilson 	 * The expandable space is reported in terms of metaslab sized units
3200c39a2aaeSGeorge Wilson 	 * since that determines how much space the pool can expand.
3201c39a2aaeSGeorge Wilson 	 */
3202c39a2aaeSGeorge Wilson 	if (vd->vdev_aux == NULL && tvd != NULL) {
32037855d95bSToomas Soome 		vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize -
32047855d95bSToomas Soome 		    spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift);
3205c39a2aaeSGeorge Wilson 	}
32065cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
32075cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd)) {
32082e4c9986SGeorge Wilson 		vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
32092986efa8SAlex Reece 	}
3210fa9e4066Sahrens 
3211fa9e4066Sahrens 	/*
3212fa9e4066Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
3213fa9e4066Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
3214fa9e4066Sahrens 	 */
3215fa9e4066Sahrens 	if (vd == rvd) {
3216e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
3217fa9e4066Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
3218fa9e4066Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
3219fa9e4066Sahrens 
3220e14bb325SJeff Bonwick 			for (int t = 0; t < ZIO_TYPES; t++) {
3221fa9e4066Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
3222fa9e4066Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
3223fa9e4066Sahrens 			}
32243f9d6ad7SLin Ling 			cvs->vs_scan_removing = cvd->vdev_removing;
3225fa9e4066Sahrens 		}
3226fa9e4066Sahrens 	}
32272e4c9986SGeorge Wilson 	mutex_exit(&vd->vdev_stat_lock);
3228fa9e4066Sahrens }
3229fa9e4066Sahrens 
3230fa94a07fSbrendan void
3231fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
3232fa94a07fSbrendan {
3233fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
3234fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
3235fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
3236fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
3237fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
3238fa94a07fSbrendan }
3239fa94a07fSbrendan 
32403f9d6ad7SLin Ling void
32413f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd)
32423f9d6ad7SLin Ling {
32433f9d6ad7SLin Ling 	vdev_stat_t *vs = &vd->vdev_stat;
32443f9d6ad7SLin Ling 
32453f9d6ad7SLin Ling 	for (int c = 0; c < vd->vdev_children; c++)
32463f9d6ad7SLin Ling 		vdev_scan_stat_init(vd->vdev_child[c]);
32473f9d6ad7SLin Ling 
32483f9d6ad7SLin Ling 	mutex_enter(&vd->vdev_stat_lock);
32493f9d6ad7SLin Ling 	vs->vs_scan_processed = 0;
32503f9d6ad7SLin Ling 	mutex_exit(&vd->vdev_stat_lock);
32513f9d6ad7SLin Ling }
32523f9d6ad7SLin Ling 
3253fa9e4066Sahrens void
3254e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
3255fa9e4066Sahrens {
32568ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
32578ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3258e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3259fa9e4066Sahrens 	vdev_t *pvd;
3260fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
3261fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
3262fa9e4066Sahrens 	zio_type_t type = zio->io_type;
3263fa9e4066Sahrens 	int flags = zio->io_flags;
3264fa9e4066Sahrens 
3265e14bb325SJeff Bonwick 	/*
3266e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
3267e14bb325SJeff Bonwick 	 */
3268e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
3269e14bb325SJeff Bonwick 		return;
3270e14bb325SJeff Bonwick 
3271fa9e4066Sahrens 	if (zio->io_error == 0) {
3272e14bb325SJeff Bonwick 		/*
3273e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
3274e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
3275e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
3276e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
3277e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
3278e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
3279e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
3280e14bb325SJeff Bonwick 		 *
3281e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
3282e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
3283e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
3284e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
3285e14bb325SJeff Bonwick 		 */
3286e14bb325SJeff Bonwick 		if (vd == rvd)
3287e14bb325SJeff Bonwick 			return;
3288e14bb325SJeff Bonwick 
3289e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
32908ad4d6ddSJeff Bonwick 
32918ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
32928ad4d6ddSJeff Bonwick 			return;
32938ad4d6ddSJeff Bonwick 
32948ad4d6ddSJeff Bonwick 		mutex_enter(&vd->vdev_stat_lock);
32958ad4d6ddSJeff Bonwick 
3296e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
329744ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
32983f9d6ad7SLin Ling 				dsl_scan_phys_t *scn_phys =
32993f9d6ad7SLin Ling 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
33003f9d6ad7SLin Ling 				uint64_t *processed = &scn_phys->scn_processed;
33013f9d6ad7SLin Ling 
33023f9d6ad7SLin Ling 				/* XXX cleanup? */
33033f9d6ad7SLin Ling 				if (vd->vdev_ops->vdev_op_leaf)
33043f9d6ad7SLin Ling 					atomic_add_64(processed, psize);
33053f9d6ad7SLin Ling 				vs->vs_scan_processed += psize;
33063f9d6ad7SLin Ling 			}
33073f9d6ad7SLin Ling 
33088ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
3309e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
3310fa9e4066Sahrens 		}
33118ad4d6ddSJeff Bonwick 
33128ad4d6ddSJeff Bonwick 		vs->vs_ops[type]++;
33138ad4d6ddSJeff Bonwick 		vs->vs_bytes[type] += psize;
33148ad4d6ddSJeff Bonwick 
33158ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
3316fa9e4066Sahrens 		return;
3317fa9e4066Sahrens 	}
3318fa9e4066Sahrens 
3319fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
3320fa9e4066Sahrens 		return;
3321fa9e4066Sahrens 
33228956713aSEric Schrock 	/*
33238956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
33248956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
33258956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
33268956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
33278956713aSEric Schrock 	 */
33288956713aSEric Schrock 	if (zio->io_error == EIO &&
33298956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
33308956713aSEric Schrock 		return;
33318956713aSEric Schrock 
33328f18d1faSGeorge Wilson 	/*
33338f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
33348f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
33358f18d1faSGeorge Wilson 	 * errors.
33368f18d1faSGeorge Wilson 	 */
33378f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
33388f18d1faSGeorge Wilson 		return;
33398f18d1faSGeorge Wilson 
3340e14bb325SJeff Bonwick 	mutex_enter(&vd->vdev_stat_lock);
3341b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3342e14bb325SJeff Bonwick 		if (zio->io_error == ECKSUM)
3343e14bb325SJeff Bonwick 			vs->vs_checksum_errors++;
3344e14bb325SJeff Bonwick 		else
3345e14bb325SJeff Bonwick 			vs->vs_read_errors++;
3346fa9e4066Sahrens 	}
3347b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3348e14bb325SJeff Bonwick 		vs->vs_write_errors++;
3349e14bb325SJeff Bonwick 	mutex_exit(&vd->vdev_stat_lock);
3350fa9e4066Sahrens 
33515cabbc6bSPrashanth Sreenivasa 	if (spa->spa_load_state == SPA_LOAD_NONE &&
33525cabbc6bSPrashanth Sreenivasa 	    type == ZIO_TYPE_WRITE && txg != 0 &&
33538ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
335444ecc532SGeorge Wilson 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
3355b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
33568ad4d6ddSJeff Bonwick 		/*
3357b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
3358b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
3359b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
3360b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
3361b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
3362b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
3363b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
3364b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
33658ad4d6ddSJeff Bonwick 		 *
33668ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
33678ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
33688ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
33698ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
33708ad4d6ddSJeff Bonwick 		 */
33718ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
33728ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
337344ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
33748ad4d6ddSJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
33758ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
33768ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3377b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
3378b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
3379b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3380b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
33818ad4d6ddSJeff Bonwick 			}
3382b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
33838ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3384fa9e4066Sahrens 				return;
33858ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
33868ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
33878ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3388fa9e4066Sahrens 		}
33898ad4d6ddSJeff Bonwick 		if (vd != rvd)
33908ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3391fa9e4066Sahrens 	}
3392fa9e4066Sahrens }
3393fa9e4066Sahrens 
3394fa9e4066Sahrens /*
3395b24ab676SJeff Bonwick  * Update the in-core space usage stats for this vdev, its metaslab class,
3396b24ab676SJeff Bonwick  * and the root vdev.
3397fa9e4066Sahrens  */
3398fa9e4066Sahrens void
3399b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3400b24ab676SJeff Bonwick     int64_t space_delta)
3401fa9e4066Sahrens {
340299653d4eSeschrock 	int64_t dspace_delta = space_delta;
34038654d025Sperrin 	spa_t *spa = vd->vdev_spa;
34048654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
3405b24ab676SJeff Bonwick 	metaslab_group_t *mg = vd->vdev_mg;
3406b24ab676SJeff Bonwick 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
3407fa9e4066Sahrens 
34088654d025Sperrin 	ASSERT(vd == vd->vdev_top);
340999653d4eSeschrock 
34108654d025Sperrin 	/*
34118654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
34128654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
34138654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
34148654d025Sperrin 	 * childrens', thus not accurate enough for us.
34158654d025Sperrin 	 */
34168654d025Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
3417e6ca193dSGeorge Wilson 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
34188654d025Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
34198654d025Sperrin 	    vd->vdev_deflate_ratio;
34208654d025Sperrin 
34218654d025Sperrin 	mutex_enter(&vd->vdev_stat_lock);
34228654d025Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
3423b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
34248654d025Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
34258654d025Sperrin 	mutex_exit(&vd->vdev_stat_lock);
34268654d025Sperrin 
3427b24ab676SJeff Bonwick 	if (mc == spa_normal_class(spa)) {
3428fa94a07fSbrendan 		mutex_enter(&rvd->vdev_stat_lock);
3429fa94a07fSbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
3430b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
3431fa94a07fSbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
3432fa94a07fSbrendan 		mutex_exit(&rvd->vdev_stat_lock);
3433fa94a07fSbrendan 	}
3434b24ab676SJeff Bonwick 
3435b24ab676SJeff Bonwick 	if (mc != NULL) {
3436b24ab676SJeff Bonwick 		ASSERT(rvd == vd->vdev_parent);
3437b24ab676SJeff Bonwick 		ASSERT(vd->vdev_ms_count != 0);
3438b24ab676SJeff Bonwick 
3439b24ab676SJeff Bonwick 		metaslab_class_space_update(mc,
3440b24ab676SJeff Bonwick 		    alloc_delta, defer_delta, space_delta, dspace_delta);
3441b24ab676SJeff Bonwick 	}
3442fa9e4066Sahrens }
3443fa9e4066Sahrens 
3444fa9e4066Sahrens /*
3445fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
3446fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
3447fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3448fa9e4066Sahrens  */
3449fa9e4066Sahrens void
3450fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
3451fa9e4066Sahrens {
3452fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
3453fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3454fa9e4066Sahrens 	int c;
3455fa9e4066Sahrens 
3456f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3457f9af39baSGeorge Wilson 
3458c5904d13Seschrock 	/*
34596809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
34606809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
3461c5904d13Seschrock 	 */
3462c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
3463c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
3464c5904d13Seschrock 		nvlist_t **aux;
3465c5904d13Seschrock 		uint_t naux;
3466c5904d13Seschrock 
3467c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
3468c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
3469c5904d13Seschrock 				break;
3470c5904d13Seschrock 		}
3471c5904d13Seschrock 
3472e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
3473e14bb325SJeff Bonwick 			/*
3474e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
3475e14bb325SJeff Bonwick 			 */
3476e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
3477e14bb325SJeff Bonwick 			return;
3478e14bb325SJeff Bonwick 		}
3479e14bb325SJeff Bonwick 
3480c5904d13Seschrock 		sav->sav_sync = B_TRUE;
3481c5904d13Seschrock 
34826809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
34836809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
34846809eb4eSEric Schrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
34856809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
34866809eb4eSEric Schrock 		}
3487c5904d13Seschrock 
3488c5904d13Seschrock 		ASSERT(c < naux);
3489c5904d13Seschrock 
3490c5904d13Seschrock 		/*
3491c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
3492c5904d13Seschrock 		 * sketchy, but it will work.
3493c5904d13Seschrock 		 */
3494c5904d13Seschrock 		nvlist_free(aux[c]);
34953f9d6ad7SLin Ling 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3496c5904d13Seschrock 
3497c5904d13Seschrock 		return;
3498c5904d13Seschrock 	}
3499c5904d13Seschrock 
35005dabedeeSbonwick 	/*
3501e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
3502e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
3503e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
35045dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
35055dabedeeSbonwick 	 */
3506e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3507e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3508e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
35095dabedeeSbonwick 
3510fa9e4066Sahrens 	if (vd == rvd) {
3511fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
3512fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
3513fa9e4066Sahrens 	} else {
3514fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
3515fa9e4066Sahrens 
351688ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
35175cabbc6bSPrashanth Sreenivasa 		    vdev_is_concrete(vd)) {
3518e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
35195cabbc6bSPrashanth Sreenivasa 		}
3520fa9e4066Sahrens 	}
3521fa9e4066Sahrens }
3522fa9e4066Sahrens 
3523fa9e4066Sahrens void
3524fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
3525fa9e4066Sahrens {
35265dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
35275dabedeeSbonwick 
3528e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3529e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3530e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
35315dabedeeSbonwick 
3532e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3533e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
3534e14bb325SJeff Bonwick }
3535e14bb325SJeff Bonwick 
3536e14bb325SJeff Bonwick /*
3537e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
3538e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
3539e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
3540e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
3541e14bb325SJeff Bonwick  */
3542e14bb325SJeff Bonwick void
3543e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
3544e14bb325SJeff Bonwick {
3545e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3546e14bb325SJeff Bonwick 
3547f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3548e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
3549e14bb325SJeff Bonwick 
3550e14bb325SJeff Bonwick 	/*
3551e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
3552e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
3553e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
3554e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
3555e14bb325SJeff Bonwick 	 */
3556e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3557e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3558e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3559e14bb325SJeff Bonwick 
35605cabbc6bSPrashanth Sreenivasa 	if (!list_link_active(&vd->vdev_state_dirty_node) &&
35615cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd))
3562e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
3563e14bb325SJeff Bonwick }
3564e14bb325SJeff Bonwick 
3565e14bb325SJeff Bonwick void
3566e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
3567e14bb325SJeff Bonwick {
3568e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3569e14bb325SJeff Bonwick 
3570e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3571e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3572e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3573e14bb325SJeff Bonwick 
3574e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3575e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
3576fa9e4066Sahrens }
3577fa9e4066Sahrens 
357832b87932Sek /*
357932b87932Sek  * Propagate vdev state up from children to parent.
358032b87932Sek  */
358144cd46caSbillm void
358244cd46caSbillm vdev_propagate_state(vdev_t *vd)
358344cd46caSbillm {
35848ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
35858ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
358644cd46caSbillm 	int degraded = 0, faulted = 0;
358744cd46caSbillm 	int corrupted = 0;
358844cd46caSbillm 	vdev_t *child;
358944cd46caSbillm 
35903d7072f8Seschrock 	if (vd->vdev_children > 0) {
3591573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
35923d7072f8Seschrock 			child = vd->vdev_child[c];
359351ece835Seschrock 
359488ecc943SGeorge Wilson 			/*
35955cabbc6bSPrashanth Sreenivasa 			 * Don't factor holes or indirect vdevs into the
35965cabbc6bSPrashanth Sreenivasa 			 * decision.
359788ecc943SGeorge Wilson 			 */
35985cabbc6bSPrashanth Sreenivasa 			if (!vdev_is_concrete(child))
359988ecc943SGeorge Wilson 				continue;
360088ecc943SGeorge Wilson 
3601e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
36028ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
360351ece835Seschrock 				/*
360451ece835Seschrock 				 * Root special: if there is a top-level log
360551ece835Seschrock 				 * device, treat the root vdev as if it were
360651ece835Seschrock 				 * degraded.
360751ece835Seschrock 				 */
360851ece835Seschrock 				if (child->vdev_islog && vd == rvd)
360951ece835Seschrock 					degraded++;
361051ece835Seschrock 				else
361151ece835Seschrock 					faulted++;
361251ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
36133d7072f8Seschrock 				degraded++;
361451ece835Seschrock 			}
361544cd46caSbillm 
36163d7072f8Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
36173d7072f8Seschrock 				corrupted++;
36183d7072f8Seschrock 		}
361944cd46caSbillm 
36203d7072f8Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
36213d7072f8Seschrock 
36223d7072f8Seschrock 		/*
3623e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
36243d7072f8Seschrock 		 * opened due to corrupted metadata, then propagate the root
36253d7072f8Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
36263d7072f8Seschrock 		 * replicas'.
36273d7072f8Seschrock 		 */
36283d7072f8Seschrock 		if (corrupted && vd == rvd &&
36293d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
36303d7072f8Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
36313d7072f8Seschrock 			    VDEV_AUX_CORRUPT_DATA);
36323d7072f8Seschrock 	}
36333d7072f8Seschrock 
363451ece835Seschrock 	if (vd->vdev_parent)
36353d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
363644cd46caSbillm }
363744cd46caSbillm 
3638fa9e4066Sahrens /*
3639ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
3640ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
3641ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
3642ea8dc4b6Seschrock  *
3643ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
3644ea8dc4b6Seschrock  * generated.
3645fa9e4066Sahrens  */
3646fa9e4066Sahrens void
3647ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3648fa9e4066Sahrens {
3649560e6e96Seschrock 	uint64_t save_state;
3650c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
3651ea8dc4b6Seschrock 
3652ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
3653ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
3654fa9e4066Sahrens 		return;
3655ea8dc4b6Seschrock 	}
3656ea8dc4b6Seschrock 
3657560e6e96Seschrock 	save_state = vd->vdev_state;
3658fa9e4066Sahrens 
3659fa9e4066Sahrens 	vd->vdev_state = state;
3660fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
3661fa9e4066Sahrens 
36623d7072f8Seschrock 	/*
36633d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
366498d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
366598d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
366698d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
366798d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
366898d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
366998d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
367098d1cbfeSGeorge Wilson 	 * children.
367198d1cbfeSGeorge Wilson 	 */
367298d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
367398d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
36743d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
36753d7072f8Seschrock 
3676069f55e2SEric Schrock 	/*
3677069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
3678069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
3679069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
3680069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
3681069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
3682069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
3683069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
3684069f55e2SEric Schrock 	 */
3685069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3686069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
3687069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
3688069f55e2SEric Schrock 
36893d7072f8Seschrock 	if (vd->vdev_removed &&
36903d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
36913d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
36923d7072f8Seschrock 		/*
36933d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
36943d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
36953d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
36963d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
36973d7072f8Seschrock 		 * it is one of our special test online cases, which is only
36983d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
36993d7072f8Seschrock 		 * fault.
37003d7072f8Seschrock 		 */
37013d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
37023d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
37033d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
37043d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
37053d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
3706ea8dc4b6Seschrock 		/*
3707cb04b873SMark J Musante 		 * If we fail to open a vdev during an import or recovery, we
3708cb04b873SMark J Musante 		 * mark it as "not available", which signifies that it was
3709cb04b873SMark J Musante 		 * never there to begin with.  Failure to open such a device
3710cb04b873SMark J Musante 		 * is not considered an error.
3711ea8dc4b6Seschrock 		 */
3712cb04b873SMark J Musante 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3713cb04b873SMark J Musante 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3714560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
3715560e6e96Seschrock 			vd->vdev_not_present = 1;
3716560e6e96Seschrock 
3717560e6e96Seschrock 		/*
3718560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
3719560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3720560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
3721560e6e96Seschrock 		 * want to post the ereport if the device was already in the
3722560e6e96Seschrock 		 * CANT_OPEN state beforehand.
37233d7072f8Seschrock 		 *
37243d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
37253d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
37263d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
37273d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
37283d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
37293d7072f8Seschrock 		 * about to replace the device, or attempt an online with
37303d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
3731560e6e96Seschrock 		 */
37323d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
37333d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
3734c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
3735ea8dc4b6Seschrock 			const char *class;
3736ea8dc4b6Seschrock 
3737ea8dc4b6Seschrock 			switch (aux) {
3738ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
3739ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3740ea8dc4b6Seschrock 				break;
3741ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
3742ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3743ea8dc4b6Seschrock 				break;
3744ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
3745ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3746ea8dc4b6Seschrock 				break;
3747ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
3748ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3749ea8dc4b6Seschrock 				break;
3750ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
3751ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3752ea8dc4b6Seschrock 				break;
3753ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
3754ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3755ea8dc4b6Seschrock 				break;
3756ea8dc4b6Seschrock 			default:
3757ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3758ea8dc4b6Seschrock 			}
3759ea8dc4b6Seschrock 
3760c5904d13Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3761ea8dc4b6Seschrock 		}
3762ea8dc4b6Seschrock 
37633d7072f8Seschrock 		/* Erase any notion of persistent removed state */
37643d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
37653d7072f8Seschrock 	} else {
37663d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
37673d7072f8Seschrock 	}
3768ea8dc4b6Seschrock 
37698b33d774STim Haley 	if (!isopen && vd->vdev_parent)
37708b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
3771fa9e4066Sahrens }
377215e6edf1Sgw 
3773*6f793812SPavel Zakharov boolean_t
3774*6f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd)
3775*6f793812SPavel Zakharov {
3776*6f793812SPavel Zakharov 	ASSERT(!vd->vdev_ops->vdev_op_leaf);
3777*6f793812SPavel Zakharov 
3778*6f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
3779*6f793812SPavel Zakharov 		if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
3780*6f793812SPavel Zakharov 			return (B_FALSE);
3781*6f793812SPavel Zakharov 	}
3782*6f793812SPavel Zakharov 
3783*6f793812SPavel Zakharov 	return (B_TRUE);
3784*6f793812SPavel Zakharov }
3785*6f793812SPavel Zakharov 
378615e6edf1Sgw /*
378715e6edf1Sgw  * Check the vdev configuration to ensure that it's capable of supporting
3788c8811bd3SToomas Soome  * a root pool. We do not support partial configuration.
3789c8811bd3SToomas Soome  * In addition, only a single top-level vdev is allowed.
379015e6edf1Sgw  */
379115e6edf1Sgw boolean_t
379215e6edf1Sgw vdev_is_bootable(vdev_t *vd)
379315e6edf1Sgw {
379415e6edf1Sgw 	if (!vd->vdev_ops->vdev_op_leaf) {
379515e6edf1Sgw 		char *vdev_type = vd->vdev_ops->vdev_op_type;
379615e6edf1Sgw 
379715e6edf1Sgw 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
379815e6edf1Sgw 		    vd->vdev_children > 1) {
379915e6edf1Sgw 			return (B_FALSE);
38005cabbc6bSPrashanth Sreenivasa 		} else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
38015cabbc6bSPrashanth Sreenivasa 		    strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
380215e6edf1Sgw 			return (B_FALSE);
380315e6edf1Sgw 		}
380415e6edf1Sgw 	}
380515e6edf1Sgw 
3806573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
380715e6edf1Sgw 		if (!vdev_is_bootable(vd->vdev_child[c]))
380815e6edf1Sgw 			return (B_FALSE);
380915e6edf1Sgw 	}
381015e6edf1Sgw 	return (B_TRUE);
381115e6edf1Sgw }
3812e6ca193dSGeorge Wilson 
38135cabbc6bSPrashanth Sreenivasa boolean_t
38145cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd)
38155cabbc6bSPrashanth Sreenivasa {
38165cabbc6bSPrashanth Sreenivasa 	vdev_ops_t *ops = vd->vdev_ops;
38175cabbc6bSPrashanth Sreenivasa 	if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
38185cabbc6bSPrashanth Sreenivasa 	    ops == &vdev_missing_ops || ops == &vdev_root_ops) {
38195cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
38205cabbc6bSPrashanth Sreenivasa 	} else {
38215cabbc6bSPrashanth Sreenivasa 		return (B_TRUE);
38225cabbc6bSPrashanth Sreenivasa 	}
38235cabbc6bSPrashanth Sreenivasa }
38245cabbc6bSPrashanth Sreenivasa 
38254b964adaSGeorge Wilson /*
38264b964adaSGeorge Wilson  * Determine if a log device has valid content.  If the vdev was
38274b964adaSGeorge Wilson  * removed or faulted in the MOS config then we know that
38284b964adaSGeorge Wilson  * the content on the log device has already been written to the pool.
38294b964adaSGeorge Wilson  */
38304b964adaSGeorge Wilson boolean_t
38314b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd)
38324b964adaSGeorge Wilson {
38334b964adaSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
38344b964adaSGeorge Wilson 	    !vd->vdev_removed)
38354b964adaSGeorge Wilson 		return (B_TRUE);
38364b964adaSGeorge Wilson 
38374b964adaSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
38384b964adaSGeorge Wilson 		if (vdev_log_state_valid(vd->vdev_child[c]))
38394b964adaSGeorge Wilson 			return (B_TRUE);
38404b964adaSGeorge Wilson 
38414b964adaSGeorge Wilson 	return (B_FALSE);
38424b964adaSGeorge Wilson }
38434b964adaSGeorge Wilson 
3844573ca77eSGeorge Wilson /*
3845573ca77eSGeorge Wilson  * Expand a vdev if possible.
3846573ca77eSGeorge Wilson  */
3847573ca77eSGeorge Wilson void
3848573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
3849573ca77eSGeorge Wilson {
3850573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
3851573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3852573ca77eSGeorge Wilson 
38535cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
38545cabbc6bSPrashanth Sreenivasa 
38555cabbc6bSPrashanth Sreenivasa 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
38565cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd)) {
3857573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
3858573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
3859573ca77eSGeorge Wilson 	}
3860573ca77eSGeorge Wilson }
38611195e687SMark J Musante 
38621195e687SMark J Musante /*
38631195e687SMark J Musante  * Split a vdev.
38641195e687SMark J Musante  */
38651195e687SMark J Musante void
38661195e687SMark J Musante vdev_split(vdev_t *vd)
38671195e687SMark J Musante {
38681195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
38691195e687SMark J Musante 
38701195e687SMark J Musante 	vdev_remove_child(pvd, vd);
38711195e687SMark J Musante 	vdev_compact_children(pvd);
38721195e687SMark J Musante 
38731195e687SMark J Musante 	cvd = pvd->vdev_child[0];
38741195e687SMark J Musante 	if (pvd->vdev_children == 1) {
38751195e687SMark J Musante 		vdev_remove_parent(cvd);
38761195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
38771195e687SMark J Musante 	}
38781195e687SMark J Musante 	vdev_propagate_state(cvd);
38791195e687SMark J Musante }
3880283b8460SGeorge.Wilson 
3881283b8460SGeorge.Wilson void
3882283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd)
3883283b8460SGeorge.Wilson {
3884283b8460SGeorge.Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
3885283b8460SGeorge.Wilson 		vdev_t *cvd = vd->vdev_child[c];
3886283b8460SGeorge.Wilson 
3887283b8460SGeorge.Wilson 		vdev_deadman(cvd);
3888283b8460SGeorge.Wilson 	}
3889283b8460SGeorge.Wilson 
3890283b8460SGeorge.Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3891283b8460SGeorge.Wilson 		vdev_queue_t *vq = &vd->vdev_queue;
3892283b8460SGeorge.Wilson 
3893283b8460SGeorge.Wilson 		mutex_enter(&vq->vq_lock);
389469962b56SMatthew Ahrens 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
3895283b8460SGeorge.Wilson 			spa_t *spa = vd->vdev_spa;
3896283b8460SGeorge.Wilson 			zio_t *fio;
3897283b8460SGeorge.Wilson 			uint64_t delta;
3898283b8460SGeorge.Wilson 
3899283b8460SGeorge.Wilson 			/*
3900283b8460SGeorge.Wilson 			 * Look at the head of all the pending queues,
3901283b8460SGeorge.Wilson 			 * if any I/O has been outstanding for longer than
3902283b8460SGeorge.Wilson 			 * the spa_deadman_synctime we panic the system.
3903283b8460SGeorge.Wilson 			 */
390469962b56SMatthew Ahrens 			fio = avl_first(&vq->vq_active_tree);
3905c55e05cbSMatthew Ahrens 			delta = gethrtime() - fio->io_timestamp;
3906c55e05cbSMatthew Ahrens 			if (delta > spa_deadman_synctime(spa)) {
39073ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "SLOW IO: zio timestamp "
39083ee8c80cSPavel Zakharov 				    "%lluns, delta %lluns, last io %lluns",
39093ee8c80cSPavel Zakharov 				    fio->io_timestamp, (u_longlong_t)delta,
3910283b8460SGeorge.Wilson 				    vq->vq_io_complete_ts);
3911283b8460SGeorge.Wilson 				fm_panic("I/O to pool '%s' appears to be "
3912283b8460SGeorge.Wilson 				    "hung.", spa_name(spa));
3913283b8460SGeorge.Wilson 			}
3914283b8460SGeorge.Wilson 		}
3915283b8460SGeorge.Wilson 		mutex_exit(&vq->vq_lock);
3916283b8460SGeorge.Wilson 	}
3917283b8460SGeorge.Wilson }
3918