xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 9816)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
238632SBill.Moore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <sys/zfs_context.h>
281544Seschrock #include <sys/fm/fs/zfs.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/spa_impl.h>
31789Sahrens #include <sys/dmu.h>
32789Sahrens #include <sys/dmu_tx.h>
33789Sahrens #include <sys/vdev_impl.h>
34789Sahrens #include <sys/uberblock_impl.h>
35789Sahrens #include <sys/metaslab.h>
36789Sahrens #include <sys/metaslab_impl.h>
37789Sahrens #include <sys/space_map.h>
38789Sahrens #include <sys/zio.h>
39789Sahrens #include <sys/zap.h>
40789Sahrens #include <sys/fs/zfs.h>
416643Seschrock #include <sys/arc.h>
429701SGeorge.Wilson@Sun.COM #include <sys/zil.h>
43789Sahrens 
44789Sahrens /*
45789Sahrens  * Virtual device management.
46789Sahrens  */
47789Sahrens 
48789Sahrens static vdev_ops_t *vdev_ops_table[] = {
49789Sahrens 	&vdev_root_ops,
50789Sahrens 	&vdev_raidz_ops,
51789Sahrens 	&vdev_mirror_ops,
52789Sahrens 	&vdev_replacing_ops,
532082Seschrock 	&vdev_spare_ops,
54789Sahrens 	&vdev_disk_ops,
55789Sahrens 	&vdev_file_ops,
56789Sahrens 	&vdev_missing_ops,
57789Sahrens 	NULL
58789Sahrens };
59789Sahrens 
607046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
617046Sahrens int zfs_scrub_limit = 10;
623697Smishra 
63789Sahrens /*
64789Sahrens  * Given a vdev type, return the appropriate ops vector.
65789Sahrens  */
66789Sahrens static vdev_ops_t *
67789Sahrens vdev_getops(const char *type)
68789Sahrens {
69789Sahrens 	vdev_ops_t *ops, **opspp;
70789Sahrens 
71789Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
72789Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
73789Sahrens 			break;
74789Sahrens 
75789Sahrens 	return (ops);
76789Sahrens }
77789Sahrens 
78789Sahrens /*
79789Sahrens  * Default asize function: return the MAX of psize with the asize of
80789Sahrens  * all children.  This is what's used by anything other than RAID-Z.
81789Sahrens  */
82789Sahrens uint64_t
83789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
84789Sahrens {
851732Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
86789Sahrens 	uint64_t csize;
87*9816SGeorge.Wilson@Sun.COM 
88*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
89789Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
90789Sahrens 		asize = MAX(asize, csize);
91789Sahrens 	}
92789Sahrens 
93789Sahrens 	return (asize);
94789Sahrens }
95789Sahrens 
961175Slling /*
97*9816SGeorge.Wilson@Sun.COM  * Get the minimum allocatable size. We define the allocatable size as
98*9816SGeorge.Wilson@Sun.COM  * the vdev's asize rounded to the nearest metaslab. This allows us to
99*9816SGeorge.Wilson@Sun.COM  * replace or attach devices which don't have the same physical size but
100*9816SGeorge.Wilson@Sun.COM  * can still satisfy the same number of allocations.
1011175Slling  */
1021175Slling uint64_t
103*9816SGeorge.Wilson@Sun.COM vdev_get_min_asize(vdev_t *vd)
1041175Slling {
105*9816SGeorge.Wilson@Sun.COM 	vdev_t *pvd = vd->vdev_parent;
106*9816SGeorge.Wilson@Sun.COM 
107*9816SGeorge.Wilson@Sun.COM 	/*
108*9816SGeorge.Wilson@Sun.COM 	 * The our parent is NULL (inactive spare or cache) or is the root,
109*9816SGeorge.Wilson@Sun.COM 	 * just return our own asize.
110*9816SGeorge.Wilson@Sun.COM 	 */
111*9816SGeorge.Wilson@Sun.COM 	if (pvd == NULL)
112*9816SGeorge.Wilson@Sun.COM 		return (vd->vdev_asize);
1131175Slling 
1141175Slling 	/*
115*9816SGeorge.Wilson@Sun.COM 	 * The top-level vdev just returns the allocatable size rounded
116*9816SGeorge.Wilson@Sun.COM 	 * to the nearest metaslab.
117*9816SGeorge.Wilson@Sun.COM 	 */
118*9816SGeorge.Wilson@Sun.COM 	if (vd == vd->vdev_top)
119*9816SGeorge.Wilson@Sun.COM 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
120*9816SGeorge.Wilson@Sun.COM 
121*9816SGeorge.Wilson@Sun.COM 	/*
122*9816SGeorge.Wilson@Sun.COM 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
123*9816SGeorge.Wilson@Sun.COM 	 * so each child must provide at least 1/Nth of its asize.
1241175Slling 	 */
125*9816SGeorge.Wilson@Sun.COM 	if (pvd->vdev_ops == &vdev_raidz_ops)
126*9816SGeorge.Wilson@Sun.COM 		return (pvd->vdev_min_asize / pvd->vdev_children);
127*9816SGeorge.Wilson@Sun.COM 
128*9816SGeorge.Wilson@Sun.COM 	return (pvd->vdev_min_asize);
129*9816SGeorge.Wilson@Sun.COM }
130*9816SGeorge.Wilson@Sun.COM 
131*9816SGeorge.Wilson@Sun.COM void
132*9816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vdev_t *vd)
133*9816SGeorge.Wilson@Sun.COM {
134*9816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
135*9816SGeorge.Wilson@Sun.COM 
136*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
137*9816SGeorge.Wilson@Sun.COM 		vdev_set_min_asize(vd->vdev_child[c]);
1381175Slling }
1391175Slling 
140789Sahrens vdev_t *
141789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
142789Sahrens {
143789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
144789Sahrens 
1457754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1465530Sbonwick 
1477046Sahrens 	if (vdev < rvd->vdev_children) {
1487046Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
149789Sahrens 		return (rvd->vdev_child[vdev]);
1507046Sahrens 	}
151789Sahrens 
152789Sahrens 	return (NULL);
153789Sahrens }
154789Sahrens 
155789Sahrens vdev_t *
156789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
157789Sahrens {
158789Sahrens 	vdev_t *mvd;
159789Sahrens 
1601585Sbonwick 	if (vd->vdev_guid == guid)
161789Sahrens 		return (vd);
162789Sahrens 
163*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
164789Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
165789Sahrens 		    NULL)
166789Sahrens 			return (mvd);
167789Sahrens 
168789Sahrens 	return (NULL);
169789Sahrens }
170789Sahrens 
171789Sahrens void
172789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
173789Sahrens {
174789Sahrens 	size_t oldsize, newsize;
175789Sahrens 	uint64_t id = cvd->vdev_id;
176789Sahrens 	vdev_t **newchild;
177789Sahrens 
1787754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
179789Sahrens 	ASSERT(cvd->vdev_parent == NULL);
180789Sahrens 
181789Sahrens 	cvd->vdev_parent = pvd;
182789Sahrens 
183789Sahrens 	if (pvd == NULL)
184789Sahrens 		return;
185789Sahrens 
186789Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
187789Sahrens 
188789Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
189789Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
190789Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
191789Sahrens 
192789Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
193789Sahrens 	if (pvd->vdev_child != NULL) {
194789Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
195789Sahrens 		kmem_free(pvd->vdev_child, oldsize);
196789Sahrens 	}
197789Sahrens 
198789Sahrens 	pvd->vdev_child = newchild;
199789Sahrens 	pvd->vdev_child[id] = cvd;
200789Sahrens 
201789Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
202789Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
203789Sahrens 
204789Sahrens 	/*
205789Sahrens 	 * Walk up all ancestors to update guid sum.
206789Sahrens 	 */
207789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
208789Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
2093697Smishra 
2103697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2113697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
212789Sahrens }
213789Sahrens 
214789Sahrens void
215789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
216789Sahrens {
217789Sahrens 	int c;
218789Sahrens 	uint_t id = cvd->vdev_id;
219789Sahrens 
220789Sahrens 	ASSERT(cvd->vdev_parent == pvd);
221789Sahrens 
222789Sahrens 	if (pvd == NULL)
223789Sahrens 		return;
224789Sahrens 
225789Sahrens 	ASSERT(id < pvd->vdev_children);
226789Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
227789Sahrens 
228789Sahrens 	pvd->vdev_child[id] = NULL;
229789Sahrens 	cvd->vdev_parent = NULL;
230789Sahrens 
231789Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
232789Sahrens 		if (pvd->vdev_child[c])
233789Sahrens 			break;
234789Sahrens 
235789Sahrens 	if (c == pvd->vdev_children) {
236789Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
237789Sahrens 		pvd->vdev_child = NULL;
238789Sahrens 		pvd->vdev_children = 0;
239789Sahrens 	}
240789Sahrens 
241789Sahrens 	/*
242789Sahrens 	 * Walk up all ancestors to update guid sum.
243789Sahrens 	 */
244789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
245789Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
2463697Smishra 
2473697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2483697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
249789Sahrens }
250789Sahrens 
251789Sahrens /*
252789Sahrens  * Remove any holes in the child array.
253789Sahrens  */
254789Sahrens void
255789Sahrens vdev_compact_children(vdev_t *pvd)
256789Sahrens {
257789Sahrens 	vdev_t **newchild, *cvd;
258789Sahrens 	int oldc = pvd->vdev_children;
259*9816SGeorge.Wilson@Sun.COM 	int newc;
260789Sahrens 
2617754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
262789Sahrens 
263*9816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++)
264789Sahrens 		if (pvd->vdev_child[c])
265789Sahrens 			newc++;
266789Sahrens 
267789Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
268789Sahrens 
269*9816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++) {
270789Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
271789Sahrens 			newchild[newc] = cvd;
272789Sahrens 			cvd->vdev_id = newc++;
273789Sahrens 		}
274789Sahrens 	}
275789Sahrens 
276789Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
277789Sahrens 	pvd->vdev_child = newchild;
278789Sahrens 	pvd->vdev_children = newc;
279789Sahrens }
280789Sahrens 
281789Sahrens /*
282789Sahrens  * Allocate and minimally initialize a vdev_t.
283789Sahrens  */
284789Sahrens static vdev_t *
285789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
286789Sahrens {
287789Sahrens 	vdev_t *vd;
288789Sahrens 
2891585Sbonwick 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
2901585Sbonwick 
2911585Sbonwick 	if (spa->spa_root_vdev == NULL) {
2921585Sbonwick 		ASSERT(ops == &vdev_root_ops);
2931585Sbonwick 		spa->spa_root_vdev = vd;
2941585Sbonwick 	}
295789Sahrens 
2961585Sbonwick 	if (guid == 0) {
2971585Sbonwick 		if (spa->spa_root_vdev == vd) {
2981585Sbonwick 			/*
2991585Sbonwick 			 * The root vdev's guid will also be the pool guid,
3001585Sbonwick 			 * which must be unique among all pools.
3011585Sbonwick 			 */
3021585Sbonwick 			while (guid == 0 || spa_guid_exists(guid, 0))
3031585Sbonwick 				guid = spa_get_random(-1ULL);
3041585Sbonwick 		} else {
3051585Sbonwick 			/*
3061585Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3071585Sbonwick 			 */
3081585Sbonwick 			while (guid == 0 ||
3091585Sbonwick 			    spa_guid_exists(spa_guid(spa), guid))
3101585Sbonwick 				guid = spa_get_random(-1ULL);
3111585Sbonwick 		}
3121585Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3131585Sbonwick 	}
314789Sahrens 
315789Sahrens 	vd->vdev_spa = spa;
316789Sahrens 	vd->vdev_id = id;
317789Sahrens 	vd->vdev_guid = guid;
318789Sahrens 	vd->vdev_guid_sum = guid;
319789Sahrens 	vd->vdev_ops = ops;
320789Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
321789Sahrens 
322789Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3232856Snd150628 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
3247754SJeff.Bonwick@Sun.COM 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3258241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
3268241SJeff.Bonwick@Sun.COM 		space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
3278241SJeff.Bonwick@Sun.COM 		    &vd->vdev_dtl_lock);
3288241SJeff.Bonwick@Sun.COM 	}
329789Sahrens 	txg_list_create(&vd->vdev_ms_list,
330789Sahrens 	    offsetof(struct metaslab, ms_txg_node));
331789Sahrens 	txg_list_create(&vd->vdev_dtl_list,
332789Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
333789Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3344451Seschrock 	vdev_queue_init(vd);
3354451Seschrock 	vdev_cache_init(vd);
336789Sahrens 
337789Sahrens 	return (vd);
338789Sahrens }
339789Sahrens 
340789Sahrens /*
341789Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
342789Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
343789Sahrens  * different for each case.
344789Sahrens  */
3452082Seschrock int
3462082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
3472082Seschrock     int alloctype)
348789Sahrens {
349789Sahrens 	vdev_ops_t *ops;
350789Sahrens 	char *type;
3514527Sperrin 	uint64_t guid = 0, islog, nparity;
352789Sahrens 	vdev_t *vd;
353789Sahrens 
3547754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
355789Sahrens 
356789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
3572082Seschrock 		return (EINVAL);
358789Sahrens 
359789Sahrens 	if ((ops = vdev_getops(type)) == NULL)
3602082Seschrock 		return (EINVAL);
361789Sahrens 
362789Sahrens 	/*
363789Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
364789Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
365789Sahrens 	 */
366789Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
367789Sahrens 		uint64_t label_id;
368789Sahrens 
369789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
370789Sahrens 		    label_id != id)
3712082Seschrock 			return (EINVAL);
372789Sahrens 
373789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3742082Seschrock 			return (EINVAL);
3752082Seschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
3762082Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3772082Seschrock 			return (EINVAL);
3785450Sbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
3795450Sbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3805450Sbrendan 			return (EINVAL);
3819790SLin.Ling@Sun.COM 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
3829790SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3839790SLin.Ling@Sun.COM 			return (EINVAL);
384789Sahrens 	}
385789Sahrens 
3862082Seschrock 	/*
3872082Seschrock 	 * The first allocated vdev must be of type 'root'.
3882082Seschrock 	 */
3892082Seschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
3902082Seschrock 		return (EINVAL);
3912082Seschrock 
3924527Sperrin 	/*
3934527Sperrin 	 * Determine whether we're a log vdev.
3944527Sperrin 	 */
3954527Sperrin 	islog = 0;
3964527Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
3975094Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
3984527Sperrin 		return (ENOTSUP);
3994527Sperrin 
4004527Sperrin 	/*
4014527Sperrin 	 * Set the nparity property for RAID-Z vdevs.
4024527Sperrin 	 */
4034527Sperrin 	nparity = -1ULL;
4044527Sperrin 	if (ops == &vdev_raidz_ops) {
4054527Sperrin 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4064527Sperrin 		    &nparity) == 0) {
4074527Sperrin 			/*
4084527Sperrin 			 * Currently, we can only support 2 parity devices.
4094527Sperrin 			 */
4104527Sperrin 			if (nparity == 0 || nparity > 2)
4114527Sperrin 				return (EINVAL);
4124527Sperrin 			/*
4134527Sperrin 			 * Older versions can only support 1 parity device.
4144527Sperrin 			 */
4154527Sperrin 			if (nparity == 2 &&
4164577Sahrens 			    spa_version(spa) < SPA_VERSION_RAID6)
4174527Sperrin 				return (ENOTSUP);
4184527Sperrin 		} else {
4194527Sperrin 			/*
4204527Sperrin 			 * We require the parity to be specified for SPAs that
4214527Sperrin 			 * support multiple parity levels.
4224527Sperrin 			 */
4234577Sahrens 			if (spa_version(spa) >= SPA_VERSION_RAID6)
4244527Sperrin 				return (EINVAL);
4254527Sperrin 			/*
4264527Sperrin 			 * Otherwise, we default to 1 parity device for RAID-Z.
4274527Sperrin 			 */
4284527Sperrin 			nparity = 1;
4294527Sperrin 		}
4304527Sperrin 	} else {
4314527Sperrin 		nparity = 0;
4324527Sperrin 	}
4334527Sperrin 	ASSERT(nparity != -1ULL);
4344527Sperrin 
435789Sahrens 	vd = vdev_alloc_common(spa, id, guid, ops);
436789Sahrens 
4374527Sperrin 	vd->vdev_islog = islog;
4384527Sperrin 	vd->vdev_nparity = nparity;
4394527Sperrin 
440789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
441789Sahrens 		vd->vdev_path = spa_strdup(vd->vdev_path);
442789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
443789Sahrens 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4444451Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4454451Seschrock 	    &vd->vdev_physpath) == 0)
4464451Seschrock 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4479425SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4489425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
449789Sahrens 
450789Sahrens 	/*
4511171Seschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
4521171Seschrock 	 * as -1.
4531171Seschrock 	 */
4541171Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
4551171Seschrock 	    &vd->vdev_wholedisk) != 0)
4561171Seschrock 		vd->vdev_wholedisk = -1ULL;
4571171Seschrock 
4581171Seschrock 	/*
4591544Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
4601544Seschrock 	 * was not present at the time of import.
4611544Seschrock 	 */
4629425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4639425SEric.Schrock@Sun.COM 	    &vd->vdev_not_present);
4641544Seschrock 
4651544Seschrock 	/*
4661732Sbonwick 	 * Get the alignment requirement.
4671732Sbonwick 	 */
4681732Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
4691732Sbonwick 
4701732Sbonwick 	/*
471789Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
472789Sahrens 	 */
473789Sahrens 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
474789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
475789Sahrens 		    &vd->vdev_ms_array);
476789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
477789Sahrens 		    &vd->vdev_ms_shift);
478789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
479789Sahrens 		    &vd->vdev_asize);
480789Sahrens 	}
481789Sahrens 
482789Sahrens 	/*
4834451Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
484789Sahrens 	 */
4856643Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
4869790SLin.Ling@Sun.COM 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
4879790SLin.Ling@Sun.COM 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
4886643Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
4896643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
4908241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl_smo.smo_object);
4916643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
4926643Seschrock 			    &vd->vdev_unspare);
4936643Seschrock 		}
4949790SLin.Ling@Sun.COM 
4959790SLin.Ling@Sun.COM 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
4969790SLin.Ling@Sun.COM 			uint64_t spare = 0;
4979790SLin.Ling@Sun.COM 
4989790SLin.Ling@Sun.COM 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
4999790SLin.Ling@Sun.COM 			    &spare) == 0 && spare)
5009790SLin.Ling@Sun.COM 				spa_spare_add(vd);
5019790SLin.Ling@Sun.COM 		}
5029790SLin.Ling@Sun.COM 
5031732Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
5041732Sbonwick 		    &vd->vdev_offline);
5056643Seschrock 
5064451Seschrock 		/*
5074451Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5084451Seschrock 		 * state, as the diagnosis made on another system may not be
5094451Seschrock 		 * valid in the current context.
5104451Seschrock 		 */
5114451Seschrock 		if (spa->spa_load_state == SPA_LOAD_OPEN) {
5124451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5134451Seschrock 			    &vd->vdev_faulted);
5144451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5154451Seschrock 			    &vd->vdev_degraded);
5164451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5174451Seschrock 			    &vd->vdev_removed);
5184451Seschrock 		}
519789Sahrens 	}
520789Sahrens 
521789Sahrens 	/*
522789Sahrens 	 * Add ourselves to the parent's list of children.
523789Sahrens 	 */
524789Sahrens 	vdev_add_child(parent, vd);
525789Sahrens 
5262082Seschrock 	*vdp = vd;
5272082Seschrock 
5282082Seschrock 	return (0);
529789Sahrens }
530789Sahrens 
531789Sahrens void
532789Sahrens vdev_free(vdev_t *vd)
533789Sahrens {
5344451Seschrock 	spa_t *spa = vd->vdev_spa;
535789Sahrens 
536789Sahrens 	/*
537789Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
538789Sahrens 	 * trying to ensure complicated semantics for all callers.
539789Sahrens 	 */
540789Sahrens 	vdev_close(vd);
541789Sahrens 
5427754SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
543789Sahrens 
544789Sahrens 	/*
545789Sahrens 	 * Free all children.
546789Sahrens 	 */
547*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
548789Sahrens 		vdev_free(vd->vdev_child[c]);
549789Sahrens 
550789Sahrens 	ASSERT(vd->vdev_child == NULL);
551789Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
552789Sahrens 
553789Sahrens 	/*
554789Sahrens 	 * Discard allocation state.
555789Sahrens 	 */
556789Sahrens 	if (vd == vd->vdev_top)
557789Sahrens 		vdev_metaslab_fini(vd);
558789Sahrens 
559789Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
5602082Seschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
561789Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
562789Sahrens 
563789Sahrens 	/*
564789Sahrens 	 * Remove this vdev from its parent's child list.
565789Sahrens 	 */
566789Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
567789Sahrens 
568789Sahrens 	ASSERT(vd->vdev_parent == NULL);
569789Sahrens 
5704451Seschrock 	/*
5714451Seschrock 	 * Clean up vdev structure.
5724451Seschrock 	 */
5734451Seschrock 	vdev_queue_fini(vd);
5744451Seschrock 	vdev_cache_fini(vd);
5754451Seschrock 
5764451Seschrock 	if (vd->vdev_path)
5774451Seschrock 		spa_strfree(vd->vdev_path);
5784451Seschrock 	if (vd->vdev_devid)
5794451Seschrock 		spa_strfree(vd->vdev_devid);
5804451Seschrock 	if (vd->vdev_physpath)
5814451Seschrock 		spa_strfree(vd->vdev_physpath);
5829425SEric.Schrock@Sun.COM 	if (vd->vdev_fru)
5839425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_fru);
5844451Seschrock 
5854451Seschrock 	if (vd->vdev_isspare)
5864451Seschrock 		spa_spare_remove(vd);
5875450Sbrendan 	if (vd->vdev_isl2cache)
5885450Sbrendan 		spa_l2cache_remove(vd);
5894451Seschrock 
5904451Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
5914451Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
5928241SJeff.Bonwick@Sun.COM 
5934451Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
5948241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
5958241SJeff.Bonwick@Sun.COM 		space_map_unload(&vd->vdev_dtl[t]);
5968241SJeff.Bonwick@Sun.COM 		space_map_destroy(&vd->vdev_dtl[t]);
5978241SJeff.Bonwick@Sun.COM 	}
5984451Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
5998241SJeff.Bonwick@Sun.COM 
6004451Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6014451Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
6027754SJeff.Bonwick@Sun.COM 	mutex_destroy(&vd->vdev_probe_lock);
6034451Seschrock 
6044451Seschrock 	if (vd == spa->spa_root_vdev)
6054451Seschrock 		spa->spa_root_vdev = NULL;
6064451Seschrock 
6074451Seschrock 	kmem_free(vd, sizeof (vdev_t));
608789Sahrens }
609789Sahrens 
610789Sahrens /*
611789Sahrens  * Transfer top-level vdev state from svd to tvd.
612789Sahrens  */
613789Sahrens static void
614789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
615789Sahrens {
616789Sahrens 	spa_t *spa = svd->vdev_spa;
617789Sahrens 	metaslab_t *msp;
618789Sahrens 	vdev_t *vd;
619789Sahrens 	int t;
620789Sahrens 
621789Sahrens 	ASSERT(tvd == tvd->vdev_top);
622789Sahrens 
623789Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
624789Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
625789Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
626789Sahrens 
627789Sahrens 	svd->vdev_ms_array = 0;
628789Sahrens 	svd->vdev_ms_shift = 0;
629789Sahrens 	svd->vdev_ms_count = 0;
630789Sahrens 
631789Sahrens 	tvd->vdev_mg = svd->vdev_mg;
632789Sahrens 	tvd->vdev_ms = svd->vdev_ms;
633789Sahrens 
634789Sahrens 	svd->vdev_mg = NULL;
635789Sahrens 	svd->vdev_ms = NULL;
6361732Sbonwick 
6371732Sbonwick 	if (tvd->vdev_mg != NULL)
6381732Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
639789Sahrens 
640789Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
641789Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
6422082Seschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
643789Sahrens 
644789Sahrens 	svd->vdev_stat.vs_alloc = 0;
645789Sahrens 	svd->vdev_stat.vs_space = 0;
6462082Seschrock 	svd->vdev_stat.vs_dspace = 0;
647789Sahrens 
648789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
649789Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
650789Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
651789Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
652789Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
653789Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
654789Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
655789Sahrens 	}
656789Sahrens 
6577754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_config_dirty_node)) {
658789Sahrens 		vdev_config_clean(svd);
659789Sahrens 		vdev_config_dirty(tvd);
660789Sahrens 	}
661789Sahrens 
6627754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_state_dirty_node)) {
6637754SJeff.Bonwick@Sun.COM 		vdev_state_clean(svd);
6647754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(tvd);
6657754SJeff.Bonwick@Sun.COM 	}
6667754SJeff.Bonwick@Sun.COM 
6672082Seschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
6682082Seschrock 	svd->vdev_deflate_ratio = 0;
6694527Sperrin 
6704527Sperrin 	tvd->vdev_islog = svd->vdev_islog;
6714527Sperrin 	svd->vdev_islog = 0;
672789Sahrens }
673789Sahrens 
674789Sahrens static void
675789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
676789Sahrens {
677789Sahrens 	if (vd == NULL)
678789Sahrens 		return;
679789Sahrens 
680789Sahrens 	vd->vdev_top = tvd;
681789Sahrens 
682*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
683789Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
684789Sahrens }
685789Sahrens 
686789Sahrens /*
687789Sahrens  * Add a mirror/replacing vdev above an existing vdev.
688789Sahrens  */
689789Sahrens vdev_t *
690789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
691789Sahrens {
692789Sahrens 	spa_t *spa = cvd->vdev_spa;
693789Sahrens 	vdev_t *pvd = cvd->vdev_parent;
694789Sahrens 	vdev_t *mvd;
695789Sahrens 
6967754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
697789Sahrens 
698789Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
6991732Sbonwick 
7001732Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
701*9816SGeorge.Wilson@Sun.COM 	mvd->vdev_min_asize = cvd->vdev_min_asize;
7021732Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
7031732Sbonwick 	mvd->vdev_state = cvd->vdev_state;
7041732Sbonwick 
705789Sahrens 	vdev_remove_child(pvd, cvd);
706789Sahrens 	vdev_add_child(pvd, mvd);
707789Sahrens 	cvd->vdev_id = mvd->vdev_children;
708789Sahrens 	vdev_add_child(mvd, cvd);
709789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
710789Sahrens 
711789Sahrens 	if (mvd == mvd->vdev_top)
712789Sahrens 		vdev_top_transfer(cvd, mvd);
713789Sahrens 
714789Sahrens 	return (mvd);
715789Sahrens }
716789Sahrens 
717789Sahrens /*
718789Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
719789Sahrens  */
720789Sahrens void
721789Sahrens vdev_remove_parent(vdev_t *cvd)
722789Sahrens {
723789Sahrens 	vdev_t *mvd = cvd->vdev_parent;
724789Sahrens 	vdev_t *pvd = mvd->vdev_parent;
725789Sahrens 
7267754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
727789Sahrens 
728789Sahrens 	ASSERT(mvd->vdev_children == 1);
729789Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
7302082Seschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
7312082Seschrock 	    mvd->vdev_ops == &vdev_spare_ops);
7321732Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
733789Sahrens 
734789Sahrens 	vdev_remove_child(mvd, cvd);
735789Sahrens 	vdev_remove_child(pvd, mvd);
7368241SJeff.Bonwick@Sun.COM 
7377754SJeff.Bonwick@Sun.COM 	/*
7387754SJeff.Bonwick@Sun.COM 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
7397754SJeff.Bonwick@Sun.COM 	 * Otherwise, we could have detached an offline device, and when we
7407754SJeff.Bonwick@Sun.COM 	 * go to import the pool we'll think we have two top-level vdevs,
7417754SJeff.Bonwick@Sun.COM 	 * instead of a different version of the same top-level vdev.
7427754SJeff.Bonwick@Sun.COM 	 */
7438241SJeff.Bonwick@Sun.COM 	if (mvd->vdev_top == mvd) {
7448241SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
7458241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid += guid_delta;
7468241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid_sum += guid_delta;
7478241SJeff.Bonwick@Sun.COM 	}
748789Sahrens 	cvd->vdev_id = mvd->vdev_id;
749789Sahrens 	vdev_add_child(pvd, cvd);
750789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
751789Sahrens 
752789Sahrens 	if (cvd == cvd->vdev_top)
753789Sahrens 		vdev_top_transfer(mvd, cvd);
754789Sahrens 
755789Sahrens 	ASSERT(mvd->vdev_children == 0);
756789Sahrens 	vdev_free(mvd);
757789Sahrens }
758789Sahrens 
7591544Seschrock int
760789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
761789Sahrens {
762789Sahrens 	spa_t *spa = vd->vdev_spa;
7631732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
7644527Sperrin 	metaslab_class_t *mc;
7651732Sbonwick 	uint64_t m;
766789Sahrens 	uint64_t oldc = vd->vdev_ms_count;
767789Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
7681732Sbonwick 	metaslab_t **mspp;
7691732Sbonwick 	int error;
770789Sahrens 
7711585Sbonwick 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
7721585Sbonwick 		return (0);
7731585Sbonwick 
7749701SGeorge.Wilson@Sun.COM 	/*
7759701SGeorge.Wilson@Sun.COM 	 * Compute the raidz-deflation ratio.  Note, we hard-code
7769701SGeorge.Wilson@Sun.COM 	 * in 128k (1 << 17) because it is the current "typical" blocksize.
7779701SGeorge.Wilson@Sun.COM 	 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
7789701SGeorge.Wilson@Sun.COM 	 * or we will inconsistently account for existing bp's.
7799701SGeorge.Wilson@Sun.COM 	 */
7809701SGeorge.Wilson@Sun.COM 	vd->vdev_deflate_ratio = (1 << 17) /
7819701SGeorge.Wilson@Sun.COM 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
7829701SGeorge.Wilson@Sun.COM 
783789Sahrens 	ASSERT(oldc <= newc);
784789Sahrens 
7854527Sperrin 	if (vd->vdev_islog)
7864527Sperrin 		mc = spa->spa_log_class;
7874527Sperrin 	else
7884527Sperrin 		mc = spa->spa_normal_class;
7894527Sperrin 
7901732Sbonwick 	if (vd->vdev_mg == NULL)
7911732Sbonwick 		vd->vdev_mg = metaslab_group_create(mc, vd);
7921732Sbonwick 
7931732Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
7941732Sbonwick 
7951732Sbonwick 	if (oldc != 0) {
7961732Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
7971732Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
7981732Sbonwick 	}
7991732Sbonwick 
8001732Sbonwick 	vd->vdev_ms = mspp;
801789Sahrens 	vd->vdev_ms_count = newc;
802789Sahrens 
8031732Sbonwick 	for (m = oldc; m < newc; m++) {
8041732Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
805789Sahrens 		if (txg == 0) {
8061732Sbonwick 			uint64_t object = 0;
8071732Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8089512SNeil.Perrin@Sun.COM 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8099512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
8101732Sbonwick 			if (error)
8111732Sbonwick 				return (error);
8121732Sbonwick 			if (object != 0) {
8131732Sbonwick 				dmu_buf_t *db;
8141732Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
8151732Sbonwick 				if (error)
8161732Sbonwick 					return (error);
8174944Smaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
8184944Smaybee 				bcopy(db->db_data, &smo, sizeof (smo));
8191732Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
8201544Seschrock 				dmu_buf_rele(db, FTAG);
821789Sahrens 			}
822789Sahrens 		}
8231732Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
8241732Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
825789Sahrens 	}
826789Sahrens 
8271544Seschrock 	return (0);
828789Sahrens }
829789Sahrens 
830789Sahrens void
831789Sahrens vdev_metaslab_fini(vdev_t *vd)
832789Sahrens {
833789Sahrens 	uint64_t m;
834789Sahrens 	uint64_t count = vd->vdev_ms_count;
835789Sahrens 
836789Sahrens 	if (vd->vdev_ms != NULL) {
837789Sahrens 		for (m = 0; m < count; m++)
8381732Sbonwick 			if (vd->vdev_ms[m] != NULL)
8391732Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
840789Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
841789Sahrens 		vd->vdev_ms = NULL;
842789Sahrens 	}
843789Sahrens }
844789Sahrens 
8457754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats {
8467754SJeff.Bonwick@Sun.COM 	boolean_t	vps_readable;
8477754SJeff.Bonwick@Sun.COM 	boolean_t	vps_writeable;
8487754SJeff.Bonwick@Sun.COM 	int		vps_flags;
8497754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t;
8507754SJeff.Bonwick@Sun.COM 
8517754SJeff.Bonwick@Sun.COM static void
8527754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio)
8535329Sgw25295 {
8548241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
8558632SBill.Moore@Sun.COM 	vdev_t *vd = zio->io_vd;
8567754SJeff.Bonwick@Sun.COM 	vdev_probe_stats_t *vps = zio->io_private;
8578632SBill.Moore@Sun.COM 
8588632SBill.Moore@Sun.COM 	ASSERT(vd->vdev_probe_zio != NULL);
8597754SJeff.Bonwick@Sun.COM 
8607754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ) {
8617754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8627754SJeff.Bonwick@Sun.COM 			vps->vps_readable = 1;
8638241SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0 && spa_writeable(spa)) {
8648632SBill.Moore@Sun.COM 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
8657754SJeff.Bonwick@Sun.COM 			    zio->io_offset, zio->io_size, zio->io_data,
8667754SJeff.Bonwick@Sun.COM 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
8677754SJeff.Bonwick@Sun.COM 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
8687754SJeff.Bonwick@Sun.COM 		} else {
8697754SJeff.Bonwick@Sun.COM 			zio_buf_free(zio->io_data, zio->io_size);
8707754SJeff.Bonwick@Sun.COM 		}
8717754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
8727754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8737754SJeff.Bonwick@Sun.COM 			vps->vps_writeable = 1;
8747754SJeff.Bonwick@Sun.COM 		zio_buf_free(zio->io_data, zio->io_size);
8757754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_NULL) {
8768632SBill.Moore@Sun.COM 		zio_t *pio;
8777754SJeff.Bonwick@Sun.COM 
8787754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read |= !vps->vps_readable;
8797754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write |= !vps->vps_writeable;
8807754SJeff.Bonwick@Sun.COM 
8817754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) &&
8828241SJeff.Bonwick@Sun.COM 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
8837754SJeff.Bonwick@Sun.COM 			zio->io_error = 0;
8847754SJeff.Bonwick@Sun.COM 		} else {
8857754SJeff.Bonwick@Sun.COM 			ASSERT(zio->io_error != 0);
8867754SJeff.Bonwick@Sun.COM 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
8878241SJeff.Bonwick@Sun.COM 			    spa, vd, NULL, 0, 0);
8887754SJeff.Bonwick@Sun.COM 			zio->io_error = ENXIO;
8897754SJeff.Bonwick@Sun.COM 		}
8908632SBill.Moore@Sun.COM 
8918632SBill.Moore@Sun.COM 		mutex_enter(&vd->vdev_probe_lock);
8928632SBill.Moore@Sun.COM 		ASSERT(vd->vdev_probe_zio == zio);
8938632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = NULL;
8948632SBill.Moore@Sun.COM 		mutex_exit(&vd->vdev_probe_lock);
8958632SBill.Moore@Sun.COM 
8968632SBill.Moore@Sun.COM 		while ((pio = zio_walk_parents(zio)) != NULL)
8978632SBill.Moore@Sun.COM 			if (!vdev_accessible(vd, pio))
8988632SBill.Moore@Sun.COM 				pio->io_error = ENXIO;
8998632SBill.Moore@Sun.COM 
9007754SJeff.Bonwick@Sun.COM 		kmem_free(vps, sizeof (*vps));
9017754SJeff.Bonwick@Sun.COM 	}
9027754SJeff.Bonwick@Sun.COM }
9035329Sgw25295 
9047754SJeff.Bonwick@Sun.COM /*
9057754SJeff.Bonwick@Sun.COM  * Determine whether this device is accessible by reading and writing
9067754SJeff.Bonwick@Sun.COM  * to several known locations: the pad regions of each vdev label
9077754SJeff.Bonwick@Sun.COM  * but the first (which we leave alone in case it contains a VTOC).
9087754SJeff.Bonwick@Sun.COM  */
9097754SJeff.Bonwick@Sun.COM zio_t *
9108632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio)
9117754SJeff.Bonwick@Sun.COM {
9127754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
9138632SBill.Moore@Sun.COM 	vdev_probe_stats_t *vps = NULL;
9148632SBill.Moore@Sun.COM 	zio_t *pio;
9157754SJeff.Bonwick@Sun.COM 
9167754SJeff.Bonwick@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9177754SJeff.Bonwick@Sun.COM 
9188632SBill.Moore@Sun.COM 	/*
9198632SBill.Moore@Sun.COM 	 * Don't probe the probe.
9208632SBill.Moore@Sun.COM 	 */
9218632SBill.Moore@Sun.COM 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
9228632SBill.Moore@Sun.COM 		return (NULL);
9238632SBill.Moore@Sun.COM 
9248632SBill.Moore@Sun.COM 	/*
9258632SBill.Moore@Sun.COM 	 * To prevent 'probe storms' when a device fails, we create
9268632SBill.Moore@Sun.COM 	 * just one probe i/o at a time.  All zios that want to probe
9278632SBill.Moore@Sun.COM 	 * this vdev will become parents of the probe io.
9288632SBill.Moore@Sun.COM 	 */
9298632SBill.Moore@Sun.COM 	mutex_enter(&vd->vdev_probe_lock);
9308632SBill.Moore@Sun.COM 
9318632SBill.Moore@Sun.COM 	if ((pio = vd->vdev_probe_zio) == NULL) {
9328632SBill.Moore@Sun.COM 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
9338632SBill.Moore@Sun.COM 
9348632SBill.Moore@Sun.COM 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
9358632SBill.Moore@Sun.COM 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9369725SEric.Schrock@Sun.COM 		    ZIO_FLAG_TRYHARD;
9378632SBill.Moore@Sun.COM 
9388632SBill.Moore@Sun.COM 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
9398632SBill.Moore@Sun.COM 			/*
9408632SBill.Moore@Sun.COM 			 * vdev_cant_read and vdev_cant_write can only
9418632SBill.Moore@Sun.COM 			 * transition from TRUE to FALSE when we have the
9428632SBill.Moore@Sun.COM 			 * SCL_ZIO lock as writer; otherwise they can only
9438632SBill.Moore@Sun.COM 			 * transition from FALSE to TRUE.  This ensures that
9448632SBill.Moore@Sun.COM 			 * any zio looking at these values can assume that
9458632SBill.Moore@Sun.COM 			 * failures persist for the life of the I/O.  That's
9468632SBill.Moore@Sun.COM 			 * important because when a device has intermittent
9478632SBill.Moore@Sun.COM 			 * connectivity problems, we want to ensure that
9488632SBill.Moore@Sun.COM 			 * they're ascribed to the device (ENXIO) and not
9498632SBill.Moore@Sun.COM 			 * the zio (EIO).
9508632SBill.Moore@Sun.COM 			 *
9518632SBill.Moore@Sun.COM 			 * Since we hold SCL_ZIO as writer here, clear both
9528632SBill.Moore@Sun.COM 			 * values so the probe can reevaluate from first
9538632SBill.Moore@Sun.COM 			 * principles.
9548632SBill.Moore@Sun.COM 			 */
9558632SBill.Moore@Sun.COM 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
9568632SBill.Moore@Sun.COM 			vd->vdev_cant_read = B_FALSE;
9578632SBill.Moore@Sun.COM 			vd->vdev_cant_write = B_FALSE;
9588632SBill.Moore@Sun.COM 		}
9598632SBill.Moore@Sun.COM 
9608632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
9618632SBill.Moore@Sun.COM 		    vdev_probe_done, vps,
9628632SBill.Moore@Sun.COM 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
9638632SBill.Moore@Sun.COM 
9648632SBill.Moore@Sun.COM 		if (zio != NULL) {
9658632SBill.Moore@Sun.COM 			vd->vdev_probe_wanted = B_TRUE;
9668632SBill.Moore@Sun.COM 			spa_async_request(spa, SPA_ASYNC_PROBE);
9678632SBill.Moore@Sun.COM 		}
9688632SBill.Moore@Sun.COM 	}
9698632SBill.Moore@Sun.COM 
9708632SBill.Moore@Sun.COM 	if (zio != NULL)
9718632SBill.Moore@Sun.COM 		zio_add_child(zio, pio);
9728632SBill.Moore@Sun.COM 
9738632SBill.Moore@Sun.COM 	mutex_exit(&vd->vdev_probe_lock);
9748632SBill.Moore@Sun.COM 
9758632SBill.Moore@Sun.COM 	if (vps == NULL) {
9768632SBill.Moore@Sun.COM 		ASSERT(zio != NULL);
9778632SBill.Moore@Sun.COM 		return (NULL);
9788632SBill.Moore@Sun.COM 	}
9797754SJeff.Bonwick@Sun.COM 
9807754SJeff.Bonwick@Sun.COM 	for (int l = 1; l < VDEV_LABELS; l++) {
9818632SBill.Moore@Sun.COM 		zio_nowait(zio_read_phys(pio, vd,
9827754SJeff.Bonwick@Sun.COM 		    vdev_label_offset(vd->vdev_psize, l,
9839056SLin.Ling@Sun.COM 		    offsetof(vdev_label_t, vl_pad2)),
9849056SLin.Ling@Sun.COM 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
9857754SJeff.Bonwick@Sun.COM 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
9867754SJeff.Bonwick@Sun.COM 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
9877754SJeff.Bonwick@Sun.COM 	}
9887754SJeff.Bonwick@Sun.COM 
9898632SBill.Moore@Sun.COM 	if (zio == NULL)
9908632SBill.Moore@Sun.COM 		return (pio);
9918632SBill.Moore@Sun.COM 
9928632SBill.Moore@Sun.COM 	zio_nowait(pio);
9938632SBill.Moore@Sun.COM 	return (NULL);
9945329Sgw25295 }
9955329Sgw25295 
996789Sahrens /*
997789Sahrens  * Prepare a virtual device for access.
998789Sahrens  */
999789Sahrens int
1000789Sahrens vdev_open(vdev_t *vd)
1001789Sahrens {
10028241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
1003789Sahrens 	int error;
1004789Sahrens 	uint64_t osize = 0;
1005789Sahrens 	uint64_t asize, psize;
10061732Sbonwick 	uint64_t ashift = 0;
1007789Sahrens 
10088241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
10098241SJeff.Bonwick@Sun.COM 
1010789Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1011789Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1012789Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1013789Sahrens 
1014789Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
10159701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_read = B_FALSE;
10169701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_write = B_FALSE;
1017*9816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1018789Sahrens 
10194451Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
10204451Seschrock 		ASSERT(vd->vdev_children == 0);
10214451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
10224451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
10234451Seschrock 		return (ENXIO);
10244451Seschrock 	} else if (vd->vdev_offline) {
1025789Sahrens 		ASSERT(vd->vdev_children == 0);
10261544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1027789Sahrens 		return (ENXIO);
1028789Sahrens 	}
1029789Sahrens 
1030789Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1031789Sahrens 
10321544Seschrock 	if (zio_injection_enabled && error == 0)
10339725SEric.Schrock@Sun.COM 		error = zio_handle_device_injection(vd, NULL, ENXIO);
10341544Seschrock 
10354451Seschrock 	if (error) {
10364451Seschrock 		if (vd->vdev_removed &&
10374451Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
10384451Seschrock 			vd->vdev_removed = B_FALSE;
1039789Sahrens 
10401544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1041789Sahrens 		    vd->vdev_stat.vs_aux);
1042789Sahrens 		return (error);
1043789Sahrens 	}
1044789Sahrens 
10454451Seschrock 	vd->vdev_removed = B_FALSE;
10464451Seschrock 
10474451Seschrock 	if (vd->vdev_degraded) {
10484451Seschrock 		ASSERT(vd->vdev_children == 0);
10494451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
10504451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
10514451Seschrock 	} else {
10524451Seschrock 		vd->vdev_state = VDEV_STATE_HEALTHY;
10534451Seschrock 	}
1054789Sahrens 
1055*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
10561544Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
10571544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
10581544Seschrock 			    VDEV_AUX_NONE);
10591544Seschrock 			break;
10601544Seschrock 		}
1061*9816SGeorge.Wilson@Sun.COM 	}
1062789Sahrens 
1063789Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1064789Sahrens 
1065789Sahrens 	if (vd->vdev_children == 0) {
1066789Sahrens 		if (osize < SPA_MINDEVSIZE) {
10671544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10681544Seschrock 			    VDEV_AUX_TOO_SMALL);
1069789Sahrens 			return (EOVERFLOW);
1070789Sahrens 		}
1071789Sahrens 		psize = osize;
1072789Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1073789Sahrens 	} else {
10741732Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1075789Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
10761544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10771544Seschrock 			    VDEV_AUX_TOO_SMALL);
1078789Sahrens 			return (EOVERFLOW);
1079789Sahrens 		}
1080789Sahrens 		psize = 0;
1081789Sahrens 		asize = osize;
1082789Sahrens 	}
1083789Sahrens 
1084789Sahrens 	vd->vdev_psize = psize;
1085789Sahrens 
1086*9816SGeorge.Wilson@Sun.COM 	/*
1087*9816SGeorge.Wilson@Sun.COM 	 * Make sure the allocatable size hasn't shrunk.
1088*9816SGeorge.Wilson@Sun.COM 	 */
1089*9816SGeorge.Wilson@Sun.COM 	if (asize < vd->vdev_min_asize) {
1090*9816SGeorge.Wilson@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1091*9816SGeorge.Wilson@Sun.COM 		    VDEV_AUX_BAD_LABEL);
1092*9816SGeorge.Wilson@Sun.COM 		return (EINVAL);
1093*9816SGeorge.Wilson@Sun.COM 	}
1094*9816SGeorge.Wilson@Sun.COM 
1095789Sahrens 	if (vd->vdev_asize == 0) {
1096789Sahrens 		/*
1097789Sahrens 		 * This is the first-ever open, so use the computed values.
10981732Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1099789Sahrens 		 */
1100789Sahrens 		vd->vdev_asize = asize;
11011732Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1102789Sahrens 	} else {
1103789Sahrens 		/*
1104789Sahrens 		 * Make sure the alignment requirement hasn't increased.
1105789Sahrens 		 */
11061732Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
11071544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11081544Seschrock 			    VDEV_AUX_BAD_LABEL);
1109789Sahrens 			return (EINVAL);
1110789Sahrens 		}
1111789Sahrens 	}
1112789Sahrens 
11131544Seschrock 	/*
1114*9816SGeorge.Wilson@Sun.COM 	 * If all children are healthy and the asize has increased,
1115*9816SGeorge.Wilson@Sun.COM 	 * then we've experienced dynamic LUN growth.  If automatic
1116*9816SGeorge.Wilson@Sun.COM 	 * expansion is enabled then use the additional space.
1117*9816SGeorge.Wilson@Sun.COM 	 */
1118*9816SGeorge.Wilson@Sun.COM 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1119*9816SGeorge.Wilson@Sun.COM 	    (vd->vdev_expanding || spa->spa_autoexpand))
1120*9816SGeorge.Wilson@Sun.COM 		vd->vdev_asize = asize;
1121*9816SGeorge.Wilson@Sun.COM 
1122*9816SGeorge.Wilson@Sun.COM 	vdev_set_min_asize(vd);
1123*9816SGeorge.Wilson@Sun.COM 
1124*9816SGeorge.Wilson@Sun.COM 	/*
11255329Sgw25295 	 * Ensure we can issue some IO before declaring the
11265329Sgw25295 	 * vdev open for business.
11275329Sgw25295 	 */
11287754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf &&
11297754SJeff.Bonwick@Sun.COM 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
11305329Sgw25295 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11317754SJeff.Bonwick@Sun.COM 		    VDEV_AUX_IO_FAILURE);
11325329Sgw25295 		return (error);
11335329Sgw25295 	}
11345329Sgw25295 
11355329Sgw25295 	/*
11367046Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
11378241SJeff.Bonwick@Sun.COM 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
11388241SJeff.Bonwick@Sun.COM 	 * since this would just restart the scrub we are already doing.
11397046Sahrens 	 */
11408241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
11418241SJeff.Bonwick@Sun.COM 	    vdev_resilver_needed(vd, NULL, NULL))
11428241SJeff.Bonwick@Sun.COM 		spa_async_request(spa, SPA_ASYNC_RESILVER);
11437046Sahrens 
1144789Sahrens 	return (0);
1145789Sahrens }
1146789Sahrens 
1147789Sahrens /*
11481986Seschrock  * Called once the vdevs are all opened, this routine validates the label
11491986Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
11504451Seschrock  * inadvertently do repair I/Os to the wrong device.
11511986Seschrock  *
11521986Seschrock  * This function will only return failure if one of the vdevs indicates that it
11531986Seschrock  * has since been destroyed or exported.  This is only possible if
11541986Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
11551986Seschrock  * will be updated but the function will return 0.
11561986Seschrock  */
11571986Seschrock int
11581986Seschrock vdev_validate(vdev_t *vd)
11591986Seschrock {
11601986Seschrock 	spa_t *spa = vd->vdev_spa;
11611986Seschrock 	nvlist_t *label;
11627754SJeff.Bonwick@Sun.COM 	uint64_t guid, top_guid;
11631986Seschrock 	uint64_t state;
11641986Seschrock 
1165*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
11661986Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
11674070Smc142369 			return (EBADF);
11681986Seschrock 
11692174Seschrock 	/*
11702174Seschrock 	 * If the device has already failed, or was marked offline, don't do
11712174Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
11722174Seschrock 	 * overwrite the previous state.
11732174Seschrock 	 */
11747754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
11751986Seschrock 
11761986Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
11771986Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11781986Seschrock 			    VDEV_AUX_BAD_LABEL);
11791986Seschrock 			return (0);
11801986Seschrock 		}
11811986Seschrock 
11821986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
11831986Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
11841986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
11851986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11861986Seschrock 			nvlist_free(label);
11871986Seschrock 			return (0);
11881986Seschrock 		}
11891986Seschrock 
11907754SJeff.Bonwick@Sun.COM 		/*
11917754SJeff.Bonwick@Sun.COM 		 * If this vdev just became a top-level vdev because its
11927754SJeff.Bonwick@Sun.COM 		 * sibling was detached, it will have adopted the parent's
11937754SJeff.Bonwick@Sun.COM 		 * vdev guid -- but the label may or may not be on disk yet.
11947754SJeff.Bonwick@Sun.COM 		 * Fortunately, either version of the label will have the
11957754SJeff.Bonwick@Sun.COM 		 * same top guid, so if we're a top-level vdev, we can
11967754SJeff.Bonwick@Sun.COM 		 * safely compare to that instead.
11977754SJeff.Bonwick@Sun.COM 		 */
11981986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
11997754SJeff.Bonwick@Sun.COM 		    &guid) != 0 ||
12007754SJeff.Bonwick@Sun.COM 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
12017754SJeff.Bonwick@Sun.COM 		    &top_guid) != 0 ||
12027754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != guid &&
12037754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
12041986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
12051986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12061986Seschrock 			nvlist_free(label);
12071986Seschrock 			return (0);
12081986Seschrock 		}
12091986Seschrock 
12101986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
12111986Seschrock 		    &state) != 0) {
12121986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
12131986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12141986Seschrock 			nvlist_free(label);
12151986Seschrock 			return (0);
12161986Seschrock 		}
12171986Seschrock 
12181986Seschrock 		nvlist_free(label);
12191986Seschrock 
12201986Seschrock 		if (spa->spa_load_state == SPA_LOAD_OPEN &&
12211986Seschrock 		    state != POOL_STATE_ACTIVE)
12224070Smc142369 			return (EBADF);
12236976Seschrock 
12246976Seschrock 		/*
12256976Seschrock 		 * If we were able to open and validate a vdev that was
12266976Seschrock 		 * previously marked permanently unavailable, clear that state
12276976Seschrock 		 * now.
12286976Seschrock 		 */
12296976Seschrock 		if (vd->vdev_not_present)
12306976Seschrock 			vd->vdev_not_present = 0;
12311986Seschrock 	}
12321986Seschrock 
12331986Seschrock 	return (0);
12341986Seschrock }
12351986Seschrock 
12361986Seschrock /*
1237789Sahrens  * Close a virtual device.
1238789Sahrens  */
1239789Sahrens void
1240789Sahrens vdev_close(vdev_t *vd)
1241789Sahrens {
12428241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
12438241SJeff.Bonwick@Sun.COM 
12448241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
12458241SJeff.Bonwick@Sun.COM 
1246789Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1247789Sahrens 
12484451Seschrock 	vdev_cache_purge(vd);
1249789Sahrens 
12501986Seschrock 	/*
1251*9816SGeorge.Wilson@Sun.COM 	 * We record the previous state before we close it, so that if we are
12521986Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
12531986Seschrock 	 * it's still faulted.
12541986Seschrock 	 */
12551986Seschrock 	vd->vdev_prevstate = vd->vdev_state;
12561986Seschrock 
1257789Sahrens 	if (vd->vdev_offline)
1258789Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1259789Sahrens 	else
1260789Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
12611544Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1262789Sahrens }
1263789Sahrens 
1264789Sahrens void
12651544Seschrock vdev_reopen(vdev_t *vd)
1266789Sahrens {
12671544Seschrock 	spa_t *spa = vd->vdev_spa;
1268789Sahrens 
12697754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
12701544Seschrock 
1271789Sahrens 	vdev_close(vd);
1272789Sahrens 	(void) vdev_open(vd);
1273789Sahrens 
1274789Sahrens 	/*
12753377Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
12763377Seschrock 	 * Otherwise, a device with an invalid label could be successfully
12773377Seschrock 	 * opened in response to vdev_reopen().
12783377Seschrock 	 */
12796643Seschrock 	if (vd->vdev_aux) {
12806643Seschrock 		(void) vdev_validate_aux(vd);
12817754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) && vdev_writeable(vd) &&
12829425SEric.Schrock@Sun.COM 		    vd->vdev_aux == &spa->spa_l2cache &&
1283*9816SGeorge.Wilson@Sun.COM 		    !l2arc_vdev_present(vd))
1284*9816SGeorge.Wilson@Sun.COM 			l2arc_add_vdev(spa, vd);
12856643Seschrock 	} else {
12866643Seschrock 		(void) vdev_validate(vd);
12876643Seschrock 	}
12883377Seschrock 
12893377Seschrock 	/*
12904451Seschrock 	 * Reassess parent vdev's health.
1291789Sahrens 	 */
12924451Seschrock 	vdev_propagate_state(vd);
1293789Sahrens }
1294789Sahrens 
1295789Sahrens int
12962082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1297789Sahrens {
1298789Sahrens 	int error;
1299789Sahrens 
1300789Sahrens 	/*
1301789Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1302789Sahrens 	 * For a create, however, we want to fail the request if
1303789Sahrens 	 * there are any components we can't open.
1304789Sahrens 	 */
1305789Sahrens 	error = vdev_open(vd);
1306789Sahrens 
1307789Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1308789Sahrens 		vdev_close(vd);
1309789Sahrens 		return (error ? error : ENXIO);
1310789Sahrens 	}
1311789Sahrens 
1312789Sahrens 	/*
1313789Sahrens 	 * Recursively initialize all labels.
1314789Sahrens 	 */
13153377Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
13163377Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1317789Sahrens 		vdev_close(vd);
1318789Sahrens 		return (error);
1319789Sahrens 	}
1320789Sahrens 
1321789Sahrens 	return (0);
1322789Sahrens }
1323789Sahrens 
13241585Sbonwick void
1325*9816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(vdev_t *vd)
1326789Sahrens {
1327789Sahrens 	/*
1328789Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1329789Sahrens 	 */
1330789Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1331789Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1332789Sahrens }
1333789Sahrens 
1334789Sahrens void
13351732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1336789Sahrens {
13371732Sbonwick 	ASSERT(vd == vd->vdev_top);
13381732Sbonwick 	ASSERT(ISP2(flags));
1339789Sahrens 
13401732Sbonwick 	if (flags & VDD_METASLAB)
13411732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
13421732Sbonwick 
13431732Sbonwick 	if (flags & VDD_DTL)
13441732Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
13451732Sbonwick 
13461732Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1347789Sahrens }
1348789Sahrens 
13498241SJeff.Bonwick@Sun.COM /*
13508241SJeff.Bonwick@Sun.COM  * DTLs.
13518241SJeff.Bonwick@Sun.COM  *
13528241SJeff.Bonwick@Sun.COM  * A vdev's DTL (dirty time log) is the set of transaction groups for which
13538241SJeff.Bonwick@Sun.COM  * the vdev has less than perfect replication.  There are three kinds of DTL:
13548241SJeff.Bonwick@Sun.COM  *
13558241SJeff.Bonwick@Sun.COM  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
13568241SJeff.Bonwick@Sun.COM  *
13578241SJeff.Bonwick@Sun.COM  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
13588241SJeff.Bonwick@Sun.COM  *
13598241SJeff.Bonwick@Sun.COM  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
13608241SJeff.Bonwick@Sun.COM  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
13618241SJeff.Bonwick@Sun.COM  *	txgs that was scrubbed.
13628241SJeff.Bonwick@Sun.COM  *
13638241SJeff.Bonwick@Sun.COM  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
13648241SJeff.Bonwick@Sun.COM  *	persistent errors or just some device being offline.
13658241SJeff.Bonwick@Sun.COM  *	Unlike the other three, the DTL_OUTAGE map is not generally
13668241SJeff.Bonwick@Sun.COM  *	maintained; it's only computed when needed, typically to
13678241SJeff.Bonwick@Sun.COM  *	determine whether a device can be detached.
13688241SJeff.Bonwick@Sun.COM  *
13698241SJeff.Bonwick@Sun.COM  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
13708241SJeff.Bonwick@Sun.COM  * either has the data or it doesn't.
13718241SJeff.Bonwick@Sun.COM  *
13728241SJeff.Bonwick@Sun.COM  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
13738241SJeff.Bonwick@Sun.COM  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
13748241SJeff.Bonwick@Sun.COM  * if any child is less than fully replicated, then so is its parent.
13758241SJeff.Bonwick@Sun.COM  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
13768241SJeff.Bonwick@Sun.COM  * comprising only those txgs which appear in 'maxfaults' or more children;
13778241SJeff.Bonwick@Sun.COM  * those are the txgs we don't have enough replication to read.  For example,
13788241SJeff.Bonwick@Sun.COM  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
13798241SJeff.Bonwick@Sun.COM  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
13808241SJeff.Bonwick@Sun.COM  * two child DTL_MISSING maps.
13818241SJeff.Bonwick@Sun.COM  *
13828241SJeff.Bonwick@Sun.COM  * It should be clear from the above that to compute the DTLs and outage maps
13838241SJeff.Bonwick@Sun.COM  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
13848241SJeff.Bonwick@Sun.COM  * Therefore, that is all we keep on disk.  When loading the pool, or after
13858241SJeff.Bonwick@Sun.COM  * a configuration change, we generate all other DTLs from first principles.
13868241SJeff.Bonwick@Sun.COM  */
1387789Sahrens void
13888241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1389789Sahrens {
13908241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
13918241SJeff.Bonwick@Sun.COM 
13928241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
13938241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
13948241SJeff.Bonwick@Sun.COM 
1395789Sahrens 	mutex_enter(sm->sm_lock);
1396789Sahrens 	if (!space_map_contains(sm, txg, size))
1397789Sahrens 		space_map_add(sm, txg, size);
1398789Sahrens 	mutex_exit(sm->sm_lock);
1399789Sahrens }
1400789Sahrens 
14018241SJeff.Bonwick@Sun.COM boolean_t
14028241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1403789Sahrens {
14048241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
14058241SJeff.Bonwick@Sun.COM 	boolean_t dirty = B_FALSE;
14068241SJeff.Bonwick@Sun.COM 
14078241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
14088241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1409789Sahrens 
1410789Sahrens 	mutex_enter(sm->sm_lock);
14118241SJeff.Bonwick@Sun.COM 	if (sm->sm_space != 0)
14128241SJeff.Bonwick@Sun.COM 		dirty = space_map_contains(sm, txg, size);
1413789Sahrens 	mutex_exit(sm->sm_lock);
1414789Sahrens 
1415789Sahrens 	return (dirty);
1416789Sahrens }
1417789Sahrens 
14188241SJeff.Bonwick@Sun.COM boolean_t
14198241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
14208241SJeff.Bonwick@Sun.COM {
14218241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
14228241SJeff.Bonwick@Sun.COM 	boolean_t empty;
14238241SJeff.Bonwick@Sun.COM 
14248241SJeff.Bonwick@Sun.COM 	mutex_enter(sm->sm_lock);
14258241SJeff.Bonwick@Sun.COM 	empty = (sm->sm_space == 0);
14268241SJeff.Bonwick@Sun.COM 	mutex_exit(sm->sm_lock);
14278241SJeff.Bonwick@Sun.COM 
14288241SJeff.Bonwick@Sun.COM 	return (empty);
14298241SJeff.Bonwick@Sun.COM }
14308241SJeff.Bonwick@Sun.COM 
1431789Sahrens /*
1432789Sahrens  * Reassess DTLs after a config change or scrub completion.
1433789Sahrens  */
1434789Sahrens void
1435789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1436789Sahrens {
14371544Seschrock 	spa_t *spa = vd->vdev_spa;
14388241SJeff.Bonwick@Sun.COM 	avl_tree_t reftree;
14398241SJeff.Bonwick@Sun.COM 	int minref;
14408241SJeff.Bonwick@Sun.COM 
14418241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
14428241SJeff.Bonwick@Sun.COM 
14438241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
14448241SJeff.Bonwick@Sun.COM 		vdev_dtl_reassess(vd->vdev_child[c], txg,
14458241SJeff.Bonwick@Sun.COM 		    scrub_txg, scrub_done);
14468241SJeff.Bonwick@Sun.COM 
14478241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev)
14488241SJeff.Bonwick@Sun.COM 		return;
14498241SJeff.Bonwick@Sun.COM 
14508241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
1451789Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
14527046Sahrens 		if (scrub_txg != 0 &&
14537046Sahrens 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
14547046Sahrens 			/* XXX should check scrub_done? */
14557046Sahrens 			/*
14567046Sahrens 			 * We completed a scrub up to scrub_txg.  If we
14577046Sahrens 			 * did it without rebooting, then the scrub dtl
14587046Sahrens 			 * will be valid, so excise the old region and
14597046Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
14607046Sahrens 			 * dtl as-is if there was an error.
14618241SJeff.Bonwick@Sun.COM 			 *
14628241SJeff.Bonwick@Sun.COM 			 * There's little trick here: to excise the beginning
14638241SJeff.Bonwick@Sun.COM 			 * of the DTL_MISSING map, we put it into a reference
14648241SJeff.Bonwick@Sun.COM 			 * tree and then add a segment with refcnt -1 that
14658241SJeff.Bonwick@Sun.COM 			 * covers the range [0, scrub_txg).  This means
14668241SJeff.Bonwick@Sun.COM 			 * that each txg in that range has refcnt -1 or 0.
14678241SJeff.Bonwick@Sun.COM 			 * We then add DTL_SCRUB with a refcnt of 2, so that
14688241SJeff.Bonwick@Sun.COM 			 * entries in the range [0, scrub_txg) will have a
14698241SJeff.Bonwick@Sun.COM 			 * positive refcnt -- either 1 or 2.  We then convert
14708241SJeff.Bonwick@Sun.COM 			 * the reference tree into the new DTL_MISSING map.
14717046Sahrens 			 */
14728241SJeff.Bonwick@Sun.COM 			space_map_ref_create(&reftree);
14738241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
14748241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
14758241SJeff.Bonwick@Sun.COM 			space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
14768241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
14778241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_SCRUB], 2);
14788241SJeff.Bonwick@Sun.COM 			space_map_ref_generate_map(&reftree,
14798241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
14808241SJeff.Bonwick@Sun.COM 			space_map_ref_destroy(&reftree);
1481789Sahrens 		}
14828241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
14838241SJeff.Bonwick@Sun.COM 		space_map_walk(&vd->vdev_dtl[DTL_MISSING],
14848241SJeff.Bonwick@Sun.COM 		    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1485789Sahrens 		if (scrub_done)
14868241SJeff.Bonwick@Sun.COM 			space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
14878241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
14888241SJeff.Bonwick@Sun.COM 		if (!vdev_readable(vd))
14898241SJeff.Bonwick@Sun.COM 			space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
14908241SJeff.Bonwick@Sun.COM 		else
14918241SJeff.Bonwick@Sun.COM 			space_map_walk(&vd->vdev_dtl[DTL_MISSING],
14928241SJeff.Bonwick@Sun.COM 			    space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1493789Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
14947046Sahrens 
14951732Sbonwick 		if (txg != 0)
14961732Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1497789Sahrens 		return;
1498789Sahrens 	}
1499789Sahrens 
1500789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
15018241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
15028241SJeff.Bonwick@Sun.COM 		if (t == DTL_SCRUB)
15038241SJeff.Bonwick@Sun.COM 			continue;			/* leaf vdevs only */
15048241SJeff.Bonwick@Sun.COM 		if (t == DTL_PARTIAL)
15058241SJeff.Bonwick@Sun.COM 			minref = 1;			/* i.e. non-zero */
15068241SJeff.Bonwick@Sun.COM 		else if (vd->vdev_nparity != 0)
15078241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
15088241SJeff.Bonwick@Sun.COM 		else
15098241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_children;	/* any kind of mirror */
15108241SJeff.Bonwick@Sun.COM 		space_map_ref_create(&reftree);
15118241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
15128241SJeff.Bonwick@Sun.COM 			vdev_t *cvd = vd->vdev_child[c];
15138241SJeff.Bonwick@Sun.COM 			mutex_enter(&cvd->vdev_dtl_lock);
15148241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree, &cvd->vdev_dtl[t], 1);
15158241SJeff.Bonwick@Sun.COM 			mutex_exit(&cvd->vdev_dtl_lock);
15168241SJeff.Bonwick@Sun.COM 		}
15178241SJeff.Bonwick@Sun.COM 		space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
15188241SJeff.Bonwick@Sun.COM 		space_map_ref_destroy(&reftree);
15198241SJeff.Bonwick@Sun.COM 	}
1520789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1521789Sahrens }
1522789Sahrens 
1523789Sahrens static int
1524789Sahrens vdev_dtl_load(vdev_t *vd)
1525789Sahrens {
1526789Sahrens 	spa_t *spa = vd->vdev_spa;
15278241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
15281732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1529789Sahrens 	dmu_buf_t *db;
1530789Sahrens 	int error;
1531789Sahrens 
1532789Sahrens 	ASSERT(vd->vdev_children == 0);
1533789Sahrens 
1534789Sahrens 	if (smo->smo_object == 0)
1535789Sahrens 		return (0);
1536789Sahrens 
15371732Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
15381544Seschrock 		return (error);
15391732Sbonwick 
15404944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
15414944Smaybee 	bcopy(db->db_data, smo, sizeof (*smo));
15421544Seschrock 	dmu_buf_rele(db, FTAG);
1543789Sahrens 
1544789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
15458241SJeff.Bonwick@Sun.COM 	error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
15468241SJeff.Bonwick@Sun.COM 	    NULL, SM_ALLOC, smo, mos);
1547789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1548789Sahrens 
1549789Sahrens 	return (error);
1550789Sahrens }
1551789Sahrens 
1552789Sahrens void
1553789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1554789Sahrens {
1555789Sahrens 	spa_t *spa = vd->vdev_spa;
15568241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
15578241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
15581732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1559789Sahrens 	space_map_t smsync;
1560789Sahrens 	kmutex_t smlock;
1561789Sahrens 	dmu_buf_t *db;
1562789Sahrens 	dmu_tx_t *tx;
1563789Sahrens 
1564789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1565789Sahrens 
1566789Sahrens 	if (vd->vdev_detached) {
1567789Sahrens 		if (smo->smo_object != 0) {
15681732Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1569789Sahrens 			ASSERT3U(err, ==, 0);
1570789Sahrens 			smo->smo_object = 0;
1571789Sahrens 		}
1572789Sahrens 		dmu_tx_commit(tx);
1573789Sahrens 		return;
1574789Sahrens 	}
1575789Sahrens 
1576789Sahrens 	if (smo->smo_object == 0) {
1577789Sahrens 		ASSERT(smo->smo_objsize == 0);
1578789Sahrens 		ASSERT(smo->smo_alloc == 0);
15791732Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1580789Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1581789Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1582789Sahrens 		ASSERT(smo->smo_object != 0);
1583789Sahrens 		vdev_config_dirty(vd->vdev_top);
1584789Sahrens 	}
1585789Sahrens 
1586789Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1587789Sahrens 
1588789Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1589789Sahrens 	    &smlock);
1590789Sahrens 
1591789Sahrens 	mutex_enter(&smlock);
1592789Sahrens 
1593789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
15941732Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1595789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1596789Sahrens 
15971732Sbonwick 	space_map_truncate(smo, mos, tx);
15981732Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1599789Sahrens 
1600789Sahrens 	space_map_destroy(&smsync);
1601789Sahrens 
1602789Sahrens 	mutex_exit(&smlock);
1603789Sahrens 	mutex_destroy(&smlock);
1604789Sahrens 
16051732Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1606789Sahrens 	dmu_buf_will_dirty(db, tx);
16074944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
16084944Smaybee 	bcopy(smo, db->db_data, sizeof (*smo));
16091544Seschrock 	dmu_buf_rele(db, FTAG);
1610789Sahrens 
1611789Sahrens 	dmu_tx_commit(tx);
1612789Sahrens }
1613789Sahrens 
16147046Sahrens /*
16158241SJeff.Bonwick@Sun.COM  * Determine whether the specified vdev can be offlined/detached/removed
16168241SJeff.Bonwick@Sun.COM  * without losing data.
16178241SJeff.Bonwick@Sun.COM  */
16188241SJeff.Bonwick@Sun.COM boolean_t
16198241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd)
16208241SJeff.Bonwick@Sun.COM {
16218241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
16228241SJeff.Bonwick@Sun.COM 	vdev_t *tvd = vd->vdev_top;
16238241SJeff.Bonwick@Sun.COM 	uint8_t cant_read = vd->vdev_cant_read;
16248241SJeff.Bonwick@Sun.COM 	boolean_t required;
16258241SJeff.Bonwick@Sun.COM 
16268241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
16278241SJeff.Bonwick@Sun.COM 
16288241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev || vd == tvd)
16298241SJeff.Bonwick@Sun.COM 		return (B_TRUE);
16308241SJeff.Bonwick@Sun.COM 
16318241SJeff.Bonwick@Sun.COM 	/*
16328241SJeff.Bonwick@Sun.COM 	 * Temporarily mark the device as unreadable, and then determine
16338241SJeff.Bonwick@Sun.COM 	 * whether this results in any DTL outages in the top-level vdev.
16348241SJeff.Bonwick@Sun.COM 	 * If not, we can safely offline/detach/remove the device.
16358241SJeff.Bonwick@Sun.COM 	 */
16368241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = B_TRUE;
16378241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
16388241SJeff.Bonwick@Sun.COM 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
16398241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = cant_read;
16408241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
16418241SJeff.Bonwick@Sun.COM 
16428241SJeff.Bonwick@Sun.COM 	return (required);
16438241SJeff.Bonwick@Sun.COM }
16448241SJeff.Bonwick@Sun.COM 
16458241SJeff.Bonwick@Sun.COM /*
16467046Sahrens  * Determine if resilver is needed, and if so the txg range.
16477046Sahrens  */
16487046Sahrens boolean_t
16497046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
16507046Sahrens {
16517046Sahrens 	boolean_t needed = B_FALSE;
16527046Sahrens 	uint64_t thismin = UINT64_MAX;
16537046Sahrens 	uint64_t thismax = 0;
16547046Sahrens 
16557046Sahrens 	if (vd->vdev_children == 0) {
16567046Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
16578241SJeff.Bonwick@Sun.COM 		if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
16588241SJeff.Bonwick@Sun.COM 		    vdev_writeable(vd)) {
16597046Sahrens 			space_seg_t *ss;
16607046Sahrens 
16618241SJeff.Bonwick@Sun.COM 			ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
16627046Sahrens 			thismin = ss->ss_start - 1;
16638241SJeff.Bonwick@Sun.COM 			ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
16647046Sahrens 			thismax = ss->ss_end;
16657046Sahrens 			needed = B_TRUE;
16667046Sahrens 		}
16677046Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
16687046Sahrens 	} else {
16698241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
16707046Sahrens 			vdev_t *cvd = vd->vdev_child[c];
16717046Sahrens 			uint64_t cmin, cmax;
16727046Sahrens 
16737046Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
16747046Sahrens 				thismin = MIN(thismin, cmin);
16757046Sahrens 				thismax = MAX(thismax, cmax);
16767046Sahrens 				needed = B_TRUE;
16777046Sahrens 			}
16787046Sahrens 		}
16797046Sahrens 	}
16807046Sahrens 
16817046Sahrens 	if (needed && minp) {
16827046Sahrens 		*minp = thismin;
16837046Sahrens 		*maxp = thismax;
16847046Sahrens 	}
16857046Sahrens 	return (needed);
16867046Sahrens }
16877046Sahrens 
16881986Seschrock void
16891544Seschrock vdev_load(vdev_t *vd)
1690789Sahrens {
1691789Sahrens 	/*
1692789Sahrens 	 * Recursively load all children.
1693789Sahrens 	 */
16948241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
16951986Seschrock 		vdev_load(vd->vdev_child[c]);
1696789Sahrens 
1697789Sahrens 	/*
16981585Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1699789Sahrens 	 */
17001986Seschrock 	if (vd == vd->vdev_top &&
17011986Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
17021986Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
17031986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17041986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1705789Sahrens 
1706789Sahrens 	/*
1707789Sahrens 	 * If this is a leaf vdev, load its DTL.
1708789Sahrens 	 */
17091986Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
17101986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17111986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1712789Sahrens }
1713789Sahrens 
17142082Seschrock /*
17155450Sbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
17165450Sbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
17175450Sbrendan  * we make sure that we can open the underlying device, then try to read the
17185450Sbrendan  * label, and make sure that the label is sane and that it hasn't been
17195450Sbrendan  * repurposed to another pool.
17202082Seschrock  */
17212082Seschrock int
17225450Sbrendan vdev_validate_aux(vdev_t *vd)
17232082Seschrock {
17242082Seschrock 	nvlist_t *label;
17252082Seschrock 	uint64_t guid, version;
17262082Seschrock 	uint64_t state;
17272082Seschrock 
17287754SJeff.Bonwick@Sun.COM 	if (!vdev_readable(vd))
17296643Seschrock 		return (0);
17306643Seschrock 
17312082Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
17322082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
17332082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
17342082Seschrock 		return (-1);
17352082Seschrock 	}
17362082Seschrock 
17372082Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
17384577Sahrens 	    version > SPA_VERSION ||
17392082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
17402082Seschrock 	    guid != vd->vdev_guid ||
17412082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
17422082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
17432082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
17442082Seschrock 		nvlist_free(label);
17452082Seschrock 		return (-1);
17462082Seschrock 	}
17472082Seschrock 
17482082Seschrock 	/*
17492082Seschrock 	 * We don't actually check the pool state here.  If it's in fact in
17502082Seschrock 	 * use by another pool, we update this fact on the fly when requested.
17512082Seschrock 	 */
17522082Seschrock 	nvlist_free(label);
17532082Seschrock 	return (0);
17542082Seschrock }
17552082Seschrock 
1756789Sahrens void
1757789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
1758789Sahrens {
1759789Sahrens 	metaslab_t *msp;
1760789Sahrens 
1761789Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1762789Sahrens 		metaslab_sync_done(msp, txg);
1763789Sahrens }
1764789Sahrens 
1765789Sahrens void
1766789Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
1767789Sahrens {
1768789Sahrens 	spa_t *spa = vd->vdev_spa;
1769789Sahrens 	vdev_t *lvd;
1770789Sahrens 	metaslab_t *msp;
17711732Sbonwick 	dmu_tx_t *tx;
1772789Sahrens 
17731732Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
17741732Sbonwick 		ASSERT(vd == vd->vdev_top);
17751732Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
17761732Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
17771732Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
17781732Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
17791732Sbonwick 		vdev_config_dirty(vd);
17801732Sbonwick 		dmu_tx_commit(tx);
17811732Sbonwick 	}
1782789Sahrens 
17831732Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1784789Sahrens 		metaslab_sync(msp, txg);
17851732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
17861732Sbonwick 	}
1787789Sahrens 
1788789Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1789789Sahrens 		vdev_dtl_sync(lvd, txg);
1790789Sahrens 
1791789Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1792789Sahrens }
1793789Sahrens 
1794789Sahrens uint64_t
1795789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1796789Sahrens {
1797789Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1798789Sahrens }
1799789Sahrens 
18004451Seschrock /*
18014451Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
18024451Seschrock  * not be opened, and no I/O is attempted.
18034451Seschrock  */
1804789Sahrens int
18054451Seschrock vdev_fault(spa_t *spa, uint64_t guid)
18064451Seschrock {
18076643Seschrock 	vdev_t *vd;
18084451Seschrock 
18097754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
18104451Seschrock 
18116643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
18127754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
18137754SJeff.Bonwick@Sun.COM 
18144451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
18157754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
18164451Seschrock 
18174451Seschrock 	/*
18184451Seschrock 	 * Faulted state takes precedence over degraded.
18194451Seschrock 	 */
18204451Seschrock 	vd->vdev_faulted = 1ULL;
18214451Seschrock 	vd->vdev_degraded = 0ULL;
18227754SJeff.Bonwick@Sun.COM 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
18234451Seschrock 
18244451Seschrock 	/*
18258123SDavid.Marker@sun.com 	 * If marking the vdev as faulted cause the top-level vdev to become
18264451Seschrock 	 * unavailable, then back off and simply mark the vdev as degraded
18274451Seschrock 	 * instead.
18284451Seschrock 	 */
18296643Seschrock 	if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
18304451Seschrock 		vd->vdev_degraded = 1ULL;
18314451Seschrock 		vd->vdev_faulted = 0ULL;
18324451Seschrock 
18334451Seschrock 		/*
18344451Seschrock 		 * If we reopen the device and it's not dead, only then do we
18354451Seschrock 		 * mark it degraded.
18364451Seschrock 		 */
18374451Seschrock 		vdev_reopen(vd);
18384451Seschrock 
18395329Sgw25295 		if (vdev_readable(vd)) {
18404451Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
18414451Seschrock 			    VDEV_AUX_ERR_EXCEEDED);
18424451Seschrock 		}
18434451Seschrock 	}
18444451Seschrock 
18457754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
18464451Seschrock }
18474451Seschrock 
18484451Seschrock /*
18494451Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
18504451Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
18514451Seschrock  * as I/O is concerned.
18524451Seschrock  */
18534451Seschrock int
18544451Seschrock vdev_degrade(spa_t *spa, uint64_t guid)
18554451Seschrock {
18566643Seschrock 	vdev_t *vd;
18574451Seschrock 
18587754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
18594451Seschrock 
18606643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
18617754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
18627754SJeff.Bonwick@Sun.COM 
18634451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
18647754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
18654451Seschrock 
18664451Seschrock 	/*
18674451Seschrock 	 * If the vdev is already faulted, then don't do anything.
18684451Seschrock 	 */
18697754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded)
18707754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, 0));
18714451Seschrock 
18724451Seschrock 	vd->vdev_degraded = 1ULL;
18734451Seschrock 	if (!vdev_is_dead(vd))
18744451Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
18754451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
18764451Seschrock 
18777754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
18784451Seschrock }
18794451Seschrock 
18804451Seschrock /*
18814451Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
18824451Seschrock  * any attached spare device should be detached when the device finishes
18834451Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
18844451Seschrock  * so no FMA events are generated if the device fails to open.
18854451Seschrock  */
18864451Seschrock int
18877754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1888789Sahrens {
1889*9816SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
1890789Sahrens 
18917754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
18921485Slling 
18936643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
18947754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1895789Sahrens 
18961585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
18977754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
18981585Sbonwick 
1899*9816SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
1900789Sahrens 	vd->vdev_offline = B_FALSE;
19011485Slling 	vd->vdev_tmpoffline = B_FALSE;
19027754SJeff.Bonwick@Sun.COM 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
19037754SJeff.Bonwick@Sun.COM 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
1904*9816SGeorge.Wilson@Sun.COM 
1905*9816SGeorge.Wilson@Sun.COM 	/* XXX - L2ARC 1.0 does not support expansion */
1906*9816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
1907*9816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
1908*9816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
1909*9816SGeorge.Wilson@Sun.COM 	}
1910*9816SGeorge.Wilson@Sun.COM 
1911*9816SGeorge.Wilson@Sun.COM 	vdev_reopen(tvd);
19124451Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
19134451Seschrock 
1914*9816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
1915*9816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
1916*9816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = B_FALSE;
1917*9816SGeorge.Wilson@Sun.COM 	}
1918*9816SGeorge.Wilson@Sun.COM 
19194451Seschrock 	if (newstate)
19204451Seschrock 		*newstate = vd->vdev_state;
19214451Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
19224451Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
19234451Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
19244451Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
19254451Seschrock 		vd->vdev_unspare = B_TRUE;
1926789Sahrens 
1927*9816SGeorge.Wilson@Sun.COM 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
1928*9816SGeorge.Wilson@Sun.COM 
1929*9816SGeorge.Wilson@Sun.COM 		/* XXX - L2ARC 1.0 does not support expansion */
1930*9816SGeorge.Wilson@Sun.COM 		if (vd->vdev_aux)
1931*9816SGeorge.Wilson@Sun.COM 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
1932*9816SGeorge.Wilson@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1933*9816SGeorge.Wilson@Sun.COM 	}
19348241SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
1935789Sahrens }
1936789Sahrens 
1937789Sahrens int
19384451Seschrock vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1939789Sahrens {
19409701SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd;
19419701SGeorge.Wilson@Sun.COM 	int error;
1942789Sahrens 
19437754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
1944789Sahrens 
19456643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
19467754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1947789Sahrens 
19481585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
19497754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
19501585Sbonwick 
19519701SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
19529701SGeorge.Wilson@Sun.COM 
1953789Sahrens 	/*
19541732Sbonwick 	 * If the device isn't already offline, try to offline it.
1955789Sahrens 	 */
19561732Sbonwick 	if (!vd->vdev_offline) {
19571732Sbonwick 		/*
19588241SJeff.Bonwick@Sun.COM 		 * If this device has the only valid copy of some data,
19599701SGeorge.Wilson@Sun.COM 		 * don't allow it to be offlined. Log devices are always
19609701SGeorge.Wilson@Sun.COM 		 * expendable.
19611732Sbonwick 		 */
19629701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
19639701SGeorge.Wilson@Sun.COM 		    vdev_dtl_required(vd))
19647754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
1965789Sahrens 
19661732Sbonwick 		/*
19671732Sbonwick 		 * Offline this device and reopen its top-level vdev.
19689701SGeorge.Wilson@Sun.COM 		 * If the top-level vdev is a log device then just offline
19699701SGeorge.Wilson@Sun.COM 		 * it. Otherwise, if this action results in the top-level
19709701SGeorge.Wilson@Sun.COM 		 * vdev becoming unusable, undo it and fail the request.
19711732Sbonwick 		 */
19721732Sbonwick 		vd->vdev_offline = B_TRUE;
19739701SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
19749701SGeorge.Wilson@Sun.COM 
19759701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
19769701SGeorge.Wilson@Sun.COM 		    vdev_is_dead(tvd)) {
19771732Sbonwick 			vd->vdev_offline = B_FALSE;
19789701SGeorge.Wilson@Sun.COM 			vdev_reopen(tvd);
19797754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
19801732Sbonwick 		}
1981789Sahrens 	}
1982789Sahrens 
19837754SJeff.Bonwick@Sun.COM 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
19841732Sbonwick 
19859701SGeorge.Wilson@Sun.COM 	if (!tvd->vdev_islog || !vdev_is_dead(tvd))
19869701SGeorge.Wilson@Sun.COM 		return (spa_vdev_state_exit(spa, vd, 0));
19879701SGeorge.Wilson@Sun.COM 
19889701SGeorge.Wilson@Sun.COM 	(void) spa_vdev_state_exit(spa, vd, 0);
19899701SGeorge.Wilson@Sun.COM 
19909701SGeorge.Wilson@Sun.COM 	error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
19919701SGeorge.Wilson@Sun.COM 	    NULL, DS_FIND_CHILDREN);
19929701SGeorge.Wilson@Sun.COM 	if (error) {
19939701SGeorge.Wilson@Sun.COM 		(void) vdev_online(spa, guid, 0, NULL);
19949701SGeorge.Wilson@Sun.COM 		return (error);
19959701SGeorge.Wilson@Sun.COM 	}
19969701SGeorge.Wilson@Sun.COM 	/*
19979701SGeorge.Wilson@Sun.COM 	 * If we successfully offlined the log device then we need to
19989701SGeorge.Wilson@Sun.COM 	 * sync out the current txg so that the "stubby" block can be
19999701SGeorge.Wilson@Sun.COM 	 * removed by zil_sync().
20009701SGeorge.Wilson@Sun.COM 	 */
20019701SGeorge.Wilson@Sun.COM 	txg_wait_synced(spa->spa_dsl_pool, 0);
20029701SGeorge.Wilson@Sun.COM 	return (0);
2003789Sahrens }
2004789Sahrens 
20051544Seschrock /*
20061544Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
20071544Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
20081544Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
20091544Seschrock  */
20101544Seschrock void
20117754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd)
2012789Sahrens {
20137754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
20147754SJeff.Bonwick@Sun.COM 
20157754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2016789Sahrens 
20171544Seschrock 	if (vd == NULL)
20187754SJeff.Bonwick@Sun.COM 		vd = rvd;
2019789Sahrens 
20201544Seschrock 	vd->vdev_stat.vs_read_errors = 0;
20211544Seschrock 	vd->vdev_stat.vs_write_errors = 0;
20221544Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2023789Sahrens 
20247754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
20257754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd->vdev_child[c]);
20264451Seschrock 
20274451Seschrock 	/*
20286959Sek110237 	 * If we're in the FAULTED state or have experienced failed I/O, then
20296959Sek110237 	 * clear the persistent state and attempt to reopen the device.  We
20306959Sek110237 	 * also mark the vdev config dirty, so that the new faulted state is
20316959Sek110237 	 * written out to disk.
20324451Seschrock 	 */
20337754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded ||
20347754SJeff.Bonwick@Sun.COM 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
20356959Sek110237 
20364451Seschrock 		vd->vdev_faulted = vd->vdev_degraded = 0;
20377754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read = B_FALSE;
20387754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write = B_FALSE;
20397754SJeff.Bonwick@Sun.COM 
20404451Seschrock 		vdev_reopen(vd);
20414451Seschrock 
20427754SJeff.Bonwick@Sun.COM 		if (vd != rvd)
20437754SJeff.Bonwick@Sun.COM 			vdev_state_dirty(vd->vdev_top);
20447754SJeff.Bonwick@Sun.COM 
20457754SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
20464808Sek110237 			spa_async_request(spa, SPA_ASYNC_RESILVER);
20474451Seschrock 
20484451Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
20494451Seschrock 	}
2050789Sahrens }
2051789Sahrens 
20527754SJeff.Bonwick@Sun.COM boolean_t
20537754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd)
20545329Sgw25295 {
20557754SJeff.Bonwick@Sun.COM 	return (vd->vdev_state < VDEV_STATE_DEGRADED);
20565329Sgw25295 }
20575329Sgw25295 
20587754SJeff.Bonwick@Sun.COM boolean_t
20597754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd)
2060789Sahrens {
20617754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2062789Sahrens }
2063789Sahrens 
20647754SJeff.Bonwick@Sun.COM boolean_t
20657754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd)
2066789Sahrens {
20677754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
20687754SJeff.Bonwick@Sun.COM }
2069789Sahrens 
20707754SJeff.Bonwick@Sun.COM boolean_t
20717980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd)
20727980SGeorge.Wilson@Sun.COM {
20738241SJeff.Bonwick@Sun.COM 	uint64_t state = vd->vdev_state;
20748241SJeff.Bonwick@Sun.COM 
20757980SGeorge.Wilson@Sun.COM 	/*
20768241SJeff.Bonwick@Sun.COM 	 * We currently allow allocations from vdevs which may be in the
20777980SGeorge.Wilson@Sun.COM 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
20787980SGeorge.Wilson@Sun.COM 	 * fails to reopen then we'll catch it later when we're holding
20798241SJeff.Bonwick@Sun.COM 	 * the proper locks.  Note that we have to get the vdev state
20808241SJeff.Bonwick@Sun.COM 	 * in a local variable because although it changes atomically,
20818241SJeff.Bonwick@Sun.COM 	 * we're asking two separate questions about it.
20827980SGeorge.Wilson@Sun.COM 	 */
20838241SJeff.Bonwick@Sun.COM 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
20847980SGeorge.Wilson@Sun.COM 	    !vd->vdev_cant_write);
20857980SGeorge.Wilson@Sun.COM }
20867980SGeorge.Wilson@Sun.COM 
20877980SGeorge.Wilson@Sun.COM boolean_t
20887754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio)
20897754SJeff.Bonwick@Sun.COM {
20907754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_vd == vd);
2091789Sahrens 
20927754SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
20937754SJeff.Bonwick@Sun.COM 		return (B_FALSE);
2094789Sahrens 
20957754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ)
20967754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_read);
2097789Sahrens 
20987754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_WRITE)
20997754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_write);
21007754SJeff.Bonwick@Sun.COM 
21017754SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2102789Sahrens }
2103789Sahrens 
2104789Sahrens /*
2105789Sahrens  * Get statistics for the given vdev.
2106789Sahrens  */
2107789Sahrens void
2108789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2109789Sahrens {
2110789Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2111789Sahrens 
2112789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2113789Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
21147046Sahrens 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
2115789Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2116789Sahrens 	vs->vs_state = vd->vdev_state;
2117*9816SGeorge.Wilson@Sun.COM 	vs->vs_rsize = vdev_get_min_asize(vd);
2118*9816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
2119*9816SGeorge.Wilson@Sun.COM 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2120789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2121789Sahrens 
2122789Sahrens 	/*
2123789Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2124789Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2125789Sahrens 	 */
2126789Sahrens 	if (vd == rvd) {
21277754SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
2128789Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2129789Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2130789Sahrens 
2131789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
21327754SJeff.Bonwick@Sun.COM 			for (int t = 0; t < ZIO_TYPES; t++) {
2133789Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2134789Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2135789Sahrens 			}
2136789Sahrens 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
2137789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
2138789Sahrens 		}
2139789Sahrens 	}
2140789Sahrens }
2141789Sahrens 
2142789Sahrens void
21435450Sbrendan vdev_clear_stats(vdev_t *vd)
21445450Sbrendan {
21455450Sbrendan 	mutex_enter(&vd->vdev_stat_lock);
21465450Sbrendan 	vd->vdev_stat.vs_space = 0;
21475450Sbrendan 	vd->vdev_stat.vs_dspace = 0;
21485450Sbrendan 	vd->vdev_stat.vs_alloc = 0;
21495450Sbrendan 	mutex_exit(&vd->vdev_stat_lock);
21505450Sbrendan }
21515450Sbrendan 
21525450Sbrendan void
21537754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize)
2154789Sahrens {
21558241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
21568241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
21577754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2158789Sahrens 	vdev_t *pvd;
2159789Sahrens 	uint64_t txg = zio->io_txg;
2160789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2161789Sahrens 	zio_type_t type = zio->io_type;
2162789Sahrens 	int flags = zio->io_flags;
2163789Sahrens 
21647754SJeff.Bonwick@Sun.COM 	/*
21657754SJeff.Bonwick@Sun.COM 	 * If this i/o is a gang leader, it didn't do any actual work.
21667754SJeff.Bonwick@Sun.COM 	 */
21677754SJeff.Bonwick@Sun.COM 	if (zio->io_gang_tree)
21687754SJeff.Bonwick@Sun.COM 		return;
21697754SJeff.Bonwick@Sun.COM 
2170789Sahrens 	if (zio->io_error == 0) {
21717754SJeff.Bonwick@Sun.COM 		/*
21727754SJeff.Bonwick@Sun.COM 		 * If this is a root i/o, don't count it -- we've already
21737754SJeff.Bonwick@Sun.COM 		 * counted the top-level vdevs, and vdev_get_stats() will
21747754SJeff.Bonwick@Sun.COM 		 * aggregate them when asked.  This reduces contention on
21757754SJeff.Bonwick@Sun.COM 		 * the root vdev_stat_lock and implicitly handles blocks
21767754SJeff.Bonwick@Sun.COM 		 * that compress away to holes, for which there is no i/o.
21777754SJeff.Bonwick@Sun.COM 		 * (Holes never create vdev children, so all the counters
21787754SJeff.Bonwick@Sun.COM 		 * remain zero, which is what we want.)
21797754SJeff.Bonwick@Sun.COM 		 *
21807754SJeff.Bonwick@Sun.COM 		 * Note: this only applies to successful i/o (io_error == 0)
21817754SJeff.Bonwick@Sun.COM 		 * because unlike i/o counts, errors are not additive.
21827754SJeff.Bonwick@Sun.COM 		 * When reading a ditto block, for example, failure of
21837754SJeff.Bonwick@Sun.COM 		 * one top-level vdev does not imply a root-level error.
21847754SJeff.Bonwick@Sun.COM 		 */
21857754SJeff.Bonwick@Sun.COM 		if (vd == rvd)
21867754SJeff.Bonwick@Sun.COM 			return;
21877754SJeff.Bonwick@Sun.COM 
21887754SJeff.Bonwick@Sun.COM 		ASSERT(vd == zio->io_vd);
21898241SJeff.Bonwick@Sun.COM 
21908241SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_BYPASS)
21918241SJeff.Bonwick@Sun.COM 			return;
21928241SJeff.Bonwick@Sun.COM 
21938241SJeff.Bonwick@Sun.COM 		mutex_enter(&vd->vdev_stat_lock);
21948241SJeff.Bonwick@Sun.COM 
21957754SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_REPAIR) {
21961807Sbonwick 			if (flags & ZIO_FLAG_SCRUB_THREAD)
21977754SJeff.Bonwick@Sun.COM 				vs->vs_scrub_repaired += psize;
21988241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SELF_HEAL)
21997754SJeff.Bonwick@Sun.COM 				vs->vs_self_healed += psize;
2200789Sahrens 		}
22018241SJeff.Bonwick@Sun.COM 
22028241SJeff.Bonwick@Sun.COM 		vs->vs_ops[type]++;
22038241SJeff.Bonwick@Sun.COM 		vs->vs_bytes[type] += psize;
22048241SJeff.Bonwick@Sun.COM 
22058241SJeff.Bonwick@Sun.COM 		mutex_exit(&vd->vdev_stat_lock);
2206789Sahrens 		return;
2207789Sahrens 	}
2208789Sahrens 
2209789Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2210789Sahrens 		return;
2211789Sahrens 
22129725SEric.Schrock@Sun.COM 	/*
22139725SEric.Schrock@Sun.COM 	 * If this is an I/O error that is going to be retried, then ignore the
22149725SEric.Schrock@Sun.COM 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
22159725SEric.Schrock@Sun.COM 	 * hard errors, when in reality they can happen for any number of
22169725SEric.Schrock@Sun.COM 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
22179725SEric.Schrock@Sun.COM 	 */
22189725SEric.Schrock@Sun.COM 	if (zio->io_error == EIO &&
22199725SEric.Schrock@Sun.COM 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
22209725SEric.Schrock@Sun.COM 		return;
22219725SEric.Schrock@Sun.COM 
22227754SJeff.Bonwick@Sun.COM 	mutex_enter(&vd->vdev_stat_lock);
22239230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
22247754SJeff.Bonwick@Sun.COM 		if (zio->io_error == ECKSUM)
22257754SJeff.Bonwick@Sun.COM 			vs->vs_checksum_errors++;
22267754SJeff.Bonwick@Sun.COM 		else
22277754SJeff.Bonwick@Sun.COM 			vs->vs_read_errors++;
2228789Sahrens 	}
22299230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
22307754SJeff.Bonwick@Sun.COM 		vs->vs_write_errors++;
22317754SJeff.Bonwick@Sun.COM 	mutex_exit(&vd->vdev_stat_lock);
2232789Sahrens 
22338241SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
22348241SJeff.Bonwick@Sun.COM 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
22358241SJeff.Bonwick@Sun.COM 	    (flags & ZIO_FLAG_SCRUB_THREAD))) {
22368241SJeff.Bonwick@Sun.COM 		/*
22378241SJeff.Bonwick@Sun.COM 		 * This is either a normal write (not a repair), or it's a
22388241SJeff.Bonwick@Sun.COM 		 * repair induced by the scrub thread.  In the normal case,
22398241SJeff.Bonwick@Sun.COM 		 * we commit the DTL change in the same txg as the block
22408241SJeff.Bonwick@Sun.COM 		 * was born.  In the scrub-induced repair case, we know that
22418241SJeff.Bonwick@Sun.COM 		 * scrubs run in first-pass syncing context, so we commit
22428241SJeff.Bonwick@Sun.COM 		 * the DTL change in spa->spa_syncing_txg.
22438241SJeff.Bonwick@Sun.COM 		 *
22448241SJeff.Bonwick@Sun.COM 		 * We currently do not make DTL entries for failed spontaneous
22458241SJeff.Bonwick@Sun.COM 		 * self-healing writes triggered by normal (non-scrubbing)
22468241SJeff.Bonwick@Sun.COM 		 * reads, because we have no transactional context in which to
22478241SJeff.Bonwick@Sun.COM 		 * do so -- and it's not clear that it'd be desirable anyway.
22488241SJeff.Bonwick@Sun.COM 		 */
22498241SJeff.Bonwick@Sun.COM 		if (vd->vdev_ops->vdev_op_leaf) {
22508241SJeff.Bonwick@Sun.COM 			uint64_t commit_txg = txg;
22518241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SCRUB_THREAD) {
22528241SJeff.Bonwick@Sun.COM 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
22538241SJeff.Bonwick@Sun.COM 				ASSERT(spa_sync_pass(spa) == 1);
22548241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
22558241SJeff.Bonwick@Sun.COM 				commit_txg = spa->spa_syncing_txg;
22568241SJeff.Bonwick@Sun.COM 			}
22578241SJeff.Bonwick@Sun.COM 			ASSERT(commit_txg >= spa->spa_syncing_txg);
22588241SJeff.Bonwick@Sun.COM 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
22598241SJeff.Bonwick@Sun.COM 				return;
22608241SJeff.Bonwick@Sun.COM 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
22618241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
22628241SJeff.Bonwick@Sun.COM 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2263789Sahrens 		}
22648241SJeff.Bonwick@Sun.COM 		if (vd != rvd)
22658241SJeff.Bonwick@Sun.COM 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2266789Sahrens 	}
2267789Sahrens }
2268789Sahrens 
2269789Sahrens void
2270789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2271789Sahrens {
2272789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2273789Sahrens 
2274*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
2275789Sahrens 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2276789Sahrens 
2277789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2278789Sahrens 
2279789Sahrens 	if (type == POOL_SCRUB_NONE) {
2280789Sahrens 		/*
2281789Sahrens 		 * Update completion and end time.  Leave everything else alone
2282789Sahrens 		 * so we can report what happened during the previous scrub.
2283789Sahrens 		 */
2284789Sahrens 		vs->vs_scrub_complete = complete;
2285789Sahrens 		vs->vs_scrub_end = gethrestime_sec();
2286789Sahrens 	} else {
2287789Sahrens 		vs->vs_scrub_type = type;
2288789Sahrens 		vs->vs_scrub_complete = 0;
2289789Sahrens 		vs->vs_scrub_examined = 0;
2290789Sahrens 		vs->vs_scrub_repaired = 0;
2291789Sahrens 		vs->vs_scrub_start = gethrestime_sec();
2292789Sahrens 		vs->vs_scrub_end = 0;
2293789Sahrens 	}
2294789Sahrens 
2295789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2296789Sahrens }
2297789Sahrens 
2298789Sahrens /*
2299789Sahrens  * Update the in-core space usage stats for this vdev and the root vdev.
2300789Sahrens  */
2301789Sahrens void
23025450Sbrendan vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
23035450Sbrendan     boolean_t update_root)
2304789Sahrens {
23054527Sperrin 	int64_t dspace_delta = space_delta;
23064527Sperrin 	spa_t *spa = vd->vdev_spa;
23074527Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
23084527Sperrin 
2309789Sahrens 	ASSERT(vd == vd->vdev_top);
23104527Sperrin 
23114527Sperrin 	/*
23124527Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
23134527Sperrin 	 * factor.  We must calculate this here and not at the root vdev
23144527Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
23154527Sperrin 	 * childrens', thus not accurate enough for us.
23164527Sperrin 	 */
23174527Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
23189701SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
23194527Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
23204527Sperrin 	    vd->vdev_deflate_ratio;
2321789Sahrens 
23224527Sperrin 	mutex_enter(&vd->vdev_stat_lock);
23234527Sperrin 	vd->vdev_stat.vs_space += space_delta;
23244527Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
23254527Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
23264527Sperrin 	mutex_exit(&vd->vdev_stat_lock);
23272082Seschrock 
23285450Sbrendan 	if (update_root) {
23295450Sbrendan 		ASSERT(rvd == vd->vdev_parent);
23305450Sbrendan 		ASSERT(vd->vdev_ms_count != 0);
23314527Sperrin 
23325450Sbrendan 		/*
23335450Sbrendan 		 * Don't count non-normal (e.g. intent log) space as part of
23345450Sbrendan 		 * the pool's capacity.
23355450Sbrendan 		 */
23365450Sbrendan 		if (vd->vdev_mg->mg_class != spa->spa_normal_class)
23375450Sbrendan 			return;
23385450Sbrendan 
23395450Sbrendan 		mutex_enter(&rvd->vdev_stat_lock);
23405450Sbrendan 		rvd->vdev_stat.vs_space += space_delta;
23415450Sbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
23425450Sbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
23435450Sbrendan 		mutex_exit(&rvd->vdev_stat_lock);
23445450Sbrendan 	}
2345789Sahrens }
2346789Sahrens 
2347789Sahrens /*
2348789Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2349789Sahrens  * so that it will be written out next time the vdev configuration is synced.
2350789Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2351789Sahrens  */
2352789Sahrens void
2353789Sahrens vdev_config_dirty(vdev_t *vd)
2354789Sahrens {
2355789Sahrens 	spa_t *spa = vd->vdev_spa;
2356789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2357789Sahrens 	int c;
2358789Sahrens 
23591601Sbonwick 	/*
23609425SEric.Schrock@Sun.COM 	 * If this is an aux vdev (as with l2cache and spare devices), then we
23619425SEric.Schrock@Sun.COM 	 * update the vdev config manually and set the sync flag.
23626643Seschrock 	 */
23636643Seschrock 	if (vd->vdev_aux != NULL) {
23646643Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
23656643Seschrock 		nvlist_t **aux;
23666643Seschrock 		uint_t naux;
23676643Seschrock 
23686643Seschrock 		for (c = 0; c < sav->sav_count; c++) {
23696643Seschrock 			if (sav->sav_vdevs[c] == vd)
23706643Seschrock 				break;
23716643Seschrock 		}
23726643Seschrock 
23737754SJeff.Bonwick@Sun.COM 		if (c == sav->sav_count) {
23747754SJeff.Bonwick@Sun.COM 			/*
23757754SJeff.Bonwick@Sun.COM 			 * We're being removed.  There's nothing more to do.
23767754SJeff.Bonwick@Sun.COM 			 */
23777754SJeff.Bonwick@Sun.COM 			ASSERT(sav->sav_sync == B_TRUE);
23787754SJeff.Bonwick@Sun.COM 			return;
23797754SJeff.Bonwick@Sun.COM 		}
23807754SJeff.Bonwick@Sun.COM 
23816643Seschrock 		sav->sav_sync = B_TRUE;
23826643Seschrock 
23839425SEric.Schrock@Sun.COM 		if (nvlist_lookup_nvlist_array(sav->sav_config,
23849425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
23859425SEric.Schrock@Sun.COM 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
23869425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
23879425SEric.Schrock@Sun.COM 		}
23886643Seschrock 
23896643Seschrock 		ASSERT(c < naux);
23906643Seschrock 
23916643Seschrock 		/*
23926643Seschrock 		 * Setting the nvlist in the middle if the array is a little
23936643Seschrock 		 * sketchy, but it will work.
23946643Seschrock 		 */
23956643Seschrock 		nvlist_free(aux[c]);
23966643Seschrock 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
23976643Seschrock 
23986643Seschrock 		return;
23996643Seschrock 	}
24006643Seschrock 
24016643Seschrock 	/*
24027754SJeff.Bonwick@Sun.COM 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
24037754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
24047754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
24051601Sbonwick 	 * so this is sufficient to ensure mutual exclusion.
24061601Sbonwick 	 */
24077754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
24087754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
24097754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
24101601Sbonwick 
2411789Sahrens 	if (vd == rvd) {
2412789Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2413789Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2414789Sahrens 	} else {
2415789Sahrens 		ASSERT(vd == vd->vdev_top);
2416789Sahrens 
24177754SJeff.Bonwick@Sun.COM 		if (!list_link_active(&vd->vdev_config_dirty_node))
24187754SJeff.Bonwick@Sun.COM 			list_insert_head(&spa->spa_config_dirty_list, vd);
2419789Sahrens 	}
2420789Sahrens }
2421789Sahrens 
2422789Sahrens void
2423789Sahrens vdev_config_clean(vdev_t *vd)
2424789Sahrens {
24251601Sbonwick 	spa_t *spa = vd->vdev_spa;
24261601Sbonwick 
24277754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
24287754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
24297754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
24307754SJeff.Bonwick@Sun.COM 
24317754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
24327754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_config_dirty_list, vd);
24337754SJeff.Bonwick@Sun.COM }
24347754SJeff.Bonwick@Sun.COM 
24357754SJeff.Bonwick@Sun.COM /*
24367754SJeff.Bonwick@Sun.COM  * Mark a top-level vdev's state as dirty, so that the next pass of
24377754SJeff.Bonwick@Sun.COM  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
24387754SJeff.Bonwick@Sun.COM  * the state changes from larger config changes because they require
24397754SJeff.Bonwick@Sun.COM  * much less locking, and are often needed for administrative actions.
24407754SJeff.Bonwick@Sun.COM  */
24417754SJeff.Bonwick@Sun.COM void
24427754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd)
24437754SJeff.Bonwick@Sun.COM {
24447754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
24457754SJeff.Bonwick@Sun.COM 
24467754SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
24471601Sbonwick 
24487754SJeff.Bonwick@Sun.COM 	/*
24497754SJeff.Bonwick@Sun.COM 	 * The state list is protected by the SCL_STATE lock.  The caller
24507754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_STATE as writer, or must be the sync thread
24517754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
24527754SJeff.Bonwick@Sun.COM 	 * so this is sufficient to ensure mutual exclusion.
24537754SJeff.Bonwick@Sun.COM 	 */
24547754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
24557754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
24567754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
24577754SJeff.Bonwick@Sun.COM 
24587754SJeff.Bonwick@Sun.COM 	if (!list_link_active(&vd->vdev_state_dirty_node))
24597754SJeff.Bonwick@Sun.COM 		list_insert_head(&spa->spa_state_dirty_list, vd);
24607754SJeff.Bonwick@Sun.COM }
24617754SJeff.Bonwick@Sun.COM 
24627754SJeff.Bonwick@Sun.COM void
24637754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd)
24647754SJeff.Bonwick@Sun.COM {
24657754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
24667754SJeff.Bonwick@Sun.COM 
24677754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
24687754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
24697754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
24707754SJeff.Bonwick@Sun.COM 
24717754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
24727754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_state_dirty_list, vd);
2473789Sahrens }
2474789Sahrens 
24756523Sek110237 /*
24766523Sek110237  * Propagate vdev state up from children to parent.
24776523Sek110237  */
24781775Sbillm void
24791775Sbillm vdev_propagate_state(vdev_t *vd)
24801775Sbillm {
24818241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
24828241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
24831775Sbillm 	int degraded = 0, faulted = 0;
24841775Sbillm 	int corrupted = 0;
24851775Sbillm 	vdev_t *child;
24861775Sbillm 
24874451Seschrock 	if (vd->vdev_children > 0) {
2488*9816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
24894451Seschrock 			child = vd->vdev_child[c];
24906976Seschrock 
24917754SJeff.Bonwick@Sun.COM 			if (!vdev_readable(child) ||
24928241SJeff.Bonwick@Sun.COM 			    (!vdev_writeable(child) && spa_writeable(spa))) {
24936976Seschrock 				/*
24946976Seschrock 				 * Root special: if there is a top-level log
24956976Seschrock 				 * device, treat the root vdev as if it were
24966976Seschrock 				 * degraded.
24976976Seschrock 				 */
24986976Seschrock 				if (child->vdev_islog && vd == rvd)
24996976Seschrock 					degraded++;
25006976Seschrock 				else
25016976Seschrock 					faulted++;
25026976Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
25034451Seschrock 				degraded++;
25046976Seschrock 			}
25054451Seschrock 
25064451Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
25074451Seschrock 				corrupted++;
25084451Seschrock 		}
25091775Sbillm 
25104451Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
25114451Seschrock 
25124451Seschrock 		/*
25137754SJeff.Bonwick@Sun.COM 		 * Root special: if there is a top-level vdev that cannot be
25144451Seschrock 		 * opened due to corrupted metadata, then propagate the root
25154451Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
25164451Seschrock 		 * replicas'.
25174451Seschrock 		 */
25184451Seschrock 		if (corrupted && vd == rvd &&
25194451Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
25204451Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
25214451Seschrock 			    VDEV_AUX_CORRUPT_DATA);
25221775Sbillm 	}
25231775Sbillm 
25246976Seschrock 	if (vd->vdev_parent)
25254451Seschrock 		vdev_propagate_state(vd->vdev_parent);
25261775Sbillm }
25271775Sbillm 
2528789Sahrens /*
25291544Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
25301544Seschrock  * state, because we're in the process of opening children depth-first.
25311544Seschrock  * Otherwise, we propagate the change to the parent.
25321544Seschrock  *
25331544Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
25341544Seschrock  * generated.
2535789Sahrens  */
2536789Sahrens void
25371544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2538789Sahrens {
25391986Seschrock 	uint64_t save_state;
25406643Seschrock 	spa_t *spa = vd->vdev_spa;
25411544Seschrock 
25421544Seschrock 	if (state == vd->vdev_state) {
25431544Seschrock 		vd->vdev_stat.vs_aux = aux;
2544789Sahrens 		return;
25451544Seschrock 	}
25461544Seschrock 
25471986Seschrock 	save_state = vd->vdev_state;
2548789Sahrens 
2549789Sahrens 	vd->vdev_state = state;
2550789Sahrens 	vd->vdev_stat.vs_aux = aux;
2551789Sahrens 
25524451Seschrock 	/*
25534451Seschrock 	 * If we are setting the vdev state to anything but an open state, then
25544451Seschrock 	 * always close the underlying device.  Otherwise, we keep accessible
25554451Seschrock 	 * but invalid devices open forever.  We don't call vdev_close() itself,
25564451Seschrock 	 * because that implies some extra checks (offline, etc) that we don't
25574451Seschrock 	 * want here.  This is limited to leaf devices, because otherwise
25584451Seschrock 	 * closing the device will affect other children.
25594451Seschrock 	 */
25607780SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
25614451Seschrock 		vd->vdev_ops->vdev_op_close(vd);
25624451Seschrock 
25634451Seschrock 	if (vd->vdev_removed &&
25644451Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
25654451Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
25664451Seschrock 		/*
25674451Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
25684451Seschrock 		 * device was previously marked removed and someone attempted to
25694451Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
25704451Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
25714451Seschrock 		 * it is one of our special test online cases, which is only
25724451Seschrock 		 * attempting to online the device and shouldn't generate an FMA
25734451Seschrock 		 * fault.
25744451Seschrock 		 */
25754451Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
25764451Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
25774451Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
25784451Seschrock 		/*
25794451Seschrock 		 * Indicate to the ZFS DE that this device has been removed, and
25804451Seschrock 		 * any recent errors should be ignored.
25814451Seschrock 		 */
25826643Seschrock 		zfs_post_remove(spa, vd);
25834451Seschrock 		vd->vdev_removed = B_TRUE;
25844451Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
25851544Seschrock 		/*
25861544Seschrock 		 * If we fail to open a vdev during an import, we mark it as
25871544Seschrock 		 * "not available", which signifies that it was never there to
25881544Seschrock 		 * begin with.  Failure to open such a device is not considered
25891544Seschrock 		 * an error.
25901544Seschrock 		 */
25916643Seschrock 		if (spa->spa_load_state == SPA_LOAD_IMPORT &&
25921986Seschrock 		    vd->vdev_ops->vdev_op_leaf)
25931986Seschrock 			vd->vdev_not_present = 1;
25941986Seschrock 
25951986Seschrock 		/*
25961986Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
25971986Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
25981986Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
25991986Seschrock 		 * want to post the ereport if the device was already in the
26001986Seschrock 		 * CANT_OPEN state beforehand.
26014451Seschrock 		 *
26024451Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
26034451Seschrock 		 * online the device in response to an insertion event.  If we
26044451Seschrock 		 * hit this case, then we have detected an insertion event for a
26054451Seschrock 		 * faulted or offline device that wasn't in the removed state.
26064451Seschrock 		 * In this scenario, we don't post an ereport because we are
26074451Seschrock 		 * about to replace the device, or attempt an online with
26084451Seschrock 		 * vdev_forcefault, which will generate the fault for us.
26091986Seschrock 		 */
26104451Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
26114451Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
26126643Seschrock 		    vd != spa->spa_root_vdev) {
26131544Seschrock 			const char *class;
26141544Seschrock 
26151544Seschrock 			switch (aux) {
26161544Seschrock 			case VDEV_AUX_OPEN_FAILED:
26171544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
26181544Seschrock 				break;
26191544Seschrock 			case VDEV_AUX_CORRUPT_DATA:
26201544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
26211544Seschrock 				break;
26221544Seschrock 			case VDEV_AUX_NO_REPLICAS:
26231544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
26241544Seschrock 				break;
26251544Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
26261544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
26271544Seschrock 				break;
26281544Seschrock 			case VDEV_AUX_TOO_SMALL:
26291544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
26301544Seschrock 				break;
26311544Seschrock 			case VDEV_AUX_BAD_LABEL:
26321544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
26331544Seschrock 				break;
26347754SJeff.Bonwick@Sun.COM 			case VDEV_AUX_IO_FAILURE:
26357754SJeff.Bonwick@Sun.COM 				class = FM_EREPORT_ZFS_IO_FAILURE;
26367754SJeff.Bonwick@Sun.COM 				break;
26371544Seschrock 			default:
26381544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
26391544Seschrock 			}
26401544Seschrock 
26416643Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
26421544Seschrock 		}
26434451Seschrock 
26444451Seschrock 		/* Erase any notion of persistent removed state */
26454451Seschrock 		vd->vdev_removed = B_FALSE;
26464451Seschrock 	} else {
26474451Seschrock 		vd->vdev_removed = B_FALSE;
26481544Seschrock 	}
26491544Seschrock 
26509583STim.Haley@Sun.COM 	if (!isopen && vd->vdev_parent)
26519583STim.Haley@Sun.COM 		vdev_propagate_state(vd->vdev_parent);
2652789Sahrens }
26537042Sgw25295 
26547042Sgw25295 /*
26557042Sgw25295  * Check the vdev configuration to ensure that it's capable of supporting
26567042Sgw25295  * a root pool. Currently, we do not support RAID-Z or partial configuration.
26577042Sgw25295  * In addition, only a single top-level vdev is allowed and none of the leaves
26587042Sgw25295  * can be wholedisks.
26597042Sgw25295  */
26607042Sgw25295 boolean_t
26617042Sgw25295 vdev_is_bootable(vdev_t *vd)
26627042Sgw25295 {
26637042Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf) {
26647042Sgw25295 		char *vdev_type = vd->vdev_ops->vdev_op_type;
26657042Sgw25295 
26667042Sgw25295 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
26677042Sgw25295 		    vd->vdev_children > 1) {
26687042Sgw25295 			return (B_FALSE);
26697042Sgw25295 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
26707042Sgw25295 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
26717042Sgw25295 			return (B_FALSE);
26727042Sgw25295 		}
26737042Sgw25295 	} else if (vd->vdev_wholedisk == 1) {
26747042Sgw25295 		return (B_FALSE);
26757042Sgw25295 	}
26767042Sgw25295 
2677*9816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
26787042Sgw25295 		if (!vdev_is_bootable(vd->vdev_child[c]))
26797042Sgw25295 			return (B_FALSE);
26807042Sgw25295 	}
26817042Sgw25295 	return (B_TRUE);
26827042Sgw25295 }
26839701SGeorge.Wilson@Sun.COM 
26849701SGeorge.Wilson@Sun.COM void
26859701SGeorge.Wilson@Sun.COM vdev_load_log_state(vdev_t *vd, nvlist_t *nv)
26869701SGeorge.Wilson@Sun.COM {
2687*9816SGeorge.Wilson@Sun.COM 	uint_t children;
26889701SGeorge.Wilson@Sun.COM 	nvlist_t **child;
26899701SGeorge.Wilson@Sun.COM 	uint64_t val;
26909701SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
26919701SGeorge.Wilson@Sun.COM 
26929701SGeorge.Wilson@Sun.COM 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
26939701SGeorge.Wilson@Sun.COM 	    &child, &children) == 0) {
2694*9816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < children; c++)
26959701SGeorge.Wilson@Sun.COM 			vdev_load_log_state(vd->vdev_child[c], child[c]);
26969701SGeorge.Wilson@Sun.COM 	}
26979701SGeorge.Wilson@Sun.COM 
26989701SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && nvlist_lookup_uint64(nv,
26999701SGeorge.Wilson@Sun.COM 	    ZPOOL_CONFIG_OFFLINE, &val) == 0 && val) {
27009701SGeorge.Wilson@Sun.COM 
27019701SGeorge.Wilson@Sun.COM 		/*
27029701SGeorge.Wilson@Sun.COM 		 * It would be nice to call vdev_offline()
27039701SGeorge.Wilson@Sun.COM 		 * directly but the pool isn't fully loaded and
27049701SGeorge.Wilson@Sun.COM 		 * the txg threads have not been started yet.
27059701SGeorge.Wilson@Sun.COM 		 */
27069701SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_STATE_ALL, FTAG, RW_WRITER);
27079701SGeorge.Wilson@Sun.COM 		vd->vdev_offline = val;
27089701SGeorge.Wilson@Sun.COM 		vdev_reopen(vd->vdev_top);
27099701SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE_ALL, FTAG);
27109701SGeorge.Wilson@Sun.COM 	}
27119701SGeorge.Wilson@Sun.COM }
2712*9816SGeorge.Wilson@Sun.COM 
2713*9816SGeorge.Wilson@Sun.COM /*
2714*9816SGeorge.Wilson@Sun.COM  * Expand a vdev if possible.
2715*9816SGeorge.Wilson@Sun.COM  */
2716*9816SGeorge.Wilson@Sun.COM void
2717*9816SGeorge.Wilson@Sun.COM vdev_expand(vdev_t *vd, uint64_t txg)
2718*9816SGeorge.Wilson@Sun.COM {
2719*9816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_top == vd);
2720*9816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2721*9816SGeorge.Wilson@Sun.COM 
2722*9816SGeorge.Wilson@Sun.COM 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
2723*9816SGeorge.Wilson@Sun.COM 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
2724*9816SGeorge.Wilson@Sun.COM 		vdev_config_dirty(vd);
2725*9816SGeorge.Wilson@Sun.COM 	}
2726*9816SGeorge.Wilson@Sun.COM }
2727