xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 7980)
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 /*
236523Sek110237  * Copyright 2008 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>
42789Sahrens 
43789Sahrens /*
44789Sahrens  * Virtual device management.
45789Sahrens  */
46789Sahrens 
47789Sahrens static vdev_ops_t *vdev_ops_table[] = {
48789Sahrens 	&vdev_root_ops,
49789Sahrens 	&vdev_raidz_ops,
50789Sahrens 	&vdev_mirror_ops,
51789Sahrens 	&vdev_replacing_ops,
522082Seschrock 	&vdev_spare_ops,
53789Sahrens 	&vdev_disk_ops,
54789Sahrens 	&vdev_file_ops,
55789Sahrens 	&vdev_missing_ops,
56789Sahrens 	NULL
57789Sahrens };
58789Sahrens 
597046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
607046Sahrens int zfs_scrub_limit = 10;
613697Smishra 
62789Sahrens /*
63789Sahrens  * Given a vdev type, return the appropriate ops vector.
64789Sahrens  */
65789Sahrens static vdev_ops_t *
66789Sahrens vdev_getops(const char *type)
67789Sahrens {
68789Sahrens 	vdev_ops_t *ops, **opspp;
69789Sahrens 
70789Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
71789Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
72789Sahrens 			break;
73789Sahrens 
74789Sahrens 	return (ops);
75789Sahrens }
76789Sahrens 
77789Sahrens /*
78789Sahrens  * Default asize function: return the MAX of psize with the asize of
79789Sahrens  * all children.  This is what's used by anything other than RAID-Z.
80789Sahrens  */
81789Sahrens uint64_t
82789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
83789Sahrens {
841732Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
85789Sahrens 	uint64_t csize;
86789Sahrens 	uint64_t c;
87789Sahrens 
88789Sahrens 	for (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 /*
971175Slling  * Get the replaceable or attachable device size.
981175Slling  * If the parent is a mirror or raidz, the replaceable size is the minimum
991175Slling  * psize of all its children. For the rest, just return our own psize.
1001175Slling  *
1011175Slling  * e.g.
1021175Slling  *			psize	rsize
1031175Slling  * root			-	-
1041175Slling  *	mirror/raidz	-	-
1051175Slling  *	    disk1	20g	20g
1061175Slling  *	    disk2 	40g	20g
1071175Slling  *	disk3 		80g	80g
1081175Slling  */
1091175Slling uint64_t
1101175Slling vdev_get_rsize(vdev_t *vd)
1111175Slling {
1121175Slling 	vdev_t *pvd, *cvd;
1131175Slling 	uint64_t c, rsize;
1141175Slling 
1151175Slling 	pvd = vd->vdev_parent;
1161175Slling 
1171175Slling 	/*
1181175Slling 	 * If our parent is NULL or the root, just return our own psize.
1191175Slling 	 */
1201175Slling 	if (pvd == NULL || pvd->vdev_parent == NULL)
1211175Slling 		return (vd->vdev_psize);
1221175Slling 
1231175Slling 	rsize = 0;
1241175Slling 
1251175Slling 	for (c = 0; c < pvd->vdev_children; c++) {
1261175Slling 		cvd = pvd->vdev_child[c];
1271175Slling 		rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
1281175Slling 	}
1291175Slling 
1301175Slling 	return (rsize);
1311175Slling }
1321175Slling 
133789Sahrens vdev_t *
134789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
135789Sahrens {
136789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
137789Sahrens 
1387754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1395530Sbonwick 
1407046Sahrens 	if (vdev < rvd->vdev_children) {
1417046Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
142789Sahrens 		return (rvd->vdev_child[vdev]);
1437046Sahrens 	}
144789Sahrens 
145789Sahrens 	return (NULL);
146789Sahrens }
147789Sahrens 
148789Sahrens vdev_t *
149789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
150789Sahrens {
151789Sahrens 	int c;
152789Sahrens 	vdev_t *mvd;
153789Sahrens 
1541585Sbonwick 	if (vd->vdev_guid == guid)
155789Sahrens 		return (vd);
156789Sahrens 
157789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
158789Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
159789Sahrens 		    NULL)
160789Sahrens 			return (mvd);
161789Sahrens 
162789Sahrens 	return (NULL);
163789Sahrens }
164789Sahrens 
165789Sahrens void
166789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
167789Sahrens {
168789Sahrens 	size_t oldsize, newsize;
169789Sahrens 	uint64_t id = cvd->vdev_id;
170789Sahrens 	vdev_t **newchild;
171789Sahrens 
1727754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
173789Sahrens 	ASSERT(cvd->vdev_parent == NULL);
174789Sahrens 
175789Sahrens 	cvd->vdev_parent = pvd;
176789Sahrens 
177789Sahrens 	if (pvd == NULL)
178789Sahrens 		return;
179789Sahrens 
180789Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
181789Sahrens 
182789Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
183789Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
184789Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
185789Sahrens 
186789Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
187789Sahrens 	if (pvd->vdev_child != NULL) {
188789Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
189789Sahrens 		kmem_free(pvd->vdev_child, oldsize);
190789Sahrens 	}
191789Sahrens 
192789Sahrens 	pvd->vdev_child = newchild;
193789Sahrens 	pvd->vdev_child[id] = cvd;
194789Sahrens 
195789Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
196789Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
197789Sahrens 
198789Sahrens 	/*
199789Sahrens 	 * Walk up all ancestors to update guid sum.
200789Sahrens 	 */
201789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
202789Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
2033697Smishra 
2043697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2053697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
206789Sahrens }
207789Sahrens 
208789Sahrens void
209789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
210789Sahrens {
211789Sahrens 	int c;
212789Sahrens 	uint_t id = cvd->vdev_id;
213789Sahrens 
214789Sahrens 	ASSERT(cvd->vdev_parent == pvd);
215789Sahrens 
216789Sahrens 	if (pvd == NULL)
217789Sahrens 		return;
218789Sahrens 
219789Sahrens 	ASSERT(id < pvd->vdev_children);
220789Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
221789Sahrens 
222789Sahrens 	pvd->vdev_child[id] = NULL;
223789Sahrens 	cvd->vdev_parent = NULL;
224789Sahrens 
225789Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
226789Sahrens 		if (pvd->vdev_child[c])
227789Sahrens 			break;
228789Sahrens 
229789Sahrens 	if (c == pvd->vdev_children) {
230789Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
231789Sahrens 		pvd->vdev_child = NULL;
232789Sahrens 		pvd->vdev_children = 0;
233789Sahrens 	}
234789Sahrens 
235789Sahrens 	/*
236789Sahrens 	 * Walk up all ancestors to update guid sum.
237789Sahrens 	 */
238789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
239789Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
2403697Smishra 
2413697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2423697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
243789Sahrens }
244789Sahrens 
245789Sahrens /*
246789Sahrens  * Remove any holes in the child array.
247789Sahrens  */
248789Sahrens void
249789Sahrens vdev_compact_children(vdev_t *pvd)
250789Sahrens {
251789Sahrens 	vdev_t **newchild, *cvd;
252789Sahrens 	int oldc = pvd->vdev_children;
253789Sahrens 	int newc, c;
254789Sahrens 
2557754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
256789Sahrens 
257789Sahrens 	for (c = newc = 0; c < oldc; c++)
258789Sahrens 		if (pvd->vdev_child[c])
259789Sahrens 			newc++;
260789Sahrens 
261789Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
262789Sahrens 
263789Sahrens 	for (c = newc = 0; c < oldc; c++) {
264789Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
265789Sahrens 			newchild[newc] = cvd;
266789Sahrens 			cvd->vdev_id = newc++;
267789Sahrens 		}
268789Sahrens 	}
269789Sahrens 
270789Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
271789Sahrens 	pvd->vdev_child = newchild;
272789Sahrens 	pvd->vdev_children = newc;
273789Sahrens }
274789Sahrens 
275789Sahrens /*
276789Sahrens  * Allocate and minimally initialize a vdev_t.
277789Sahrens  */
278789Sahrens static vdev_t *
279789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
280789Sahrens {
281789Sahrens 	vdev_t *vd;
282789Sahrens 
2831585Sbonwick 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
2841585Sbonwick 
2851585Sbonwick 	if (spa->spa_root_vdev == NULL) {
2861585Sbonwick 		ASSERT(ops == &vdev_root_ops);
2871585Sbonwick 		spa->spa_root_vdev = vd;
2881585Sbonwick 	}
289789Sahrens 
2901585Sbonwick 	if (guid == 0) {
2911585Sbonwick 		if (spa->spa_root_vdev == vd) {
2921585Sbonwick 			/*
2931585Sbonwick 			 * The root vdev's guid will also be the pool guid,
2941585Sbonwick 			 * which must be unique among all pools.
2951585Sbonwick 			 */
2961585Sbonwick 			while (guid == 0 || spa_guid_exists(guid, 0))
2971585Sbonwick 				guid = spa_get_random(-1ULL);
2981585Sbonwick 		} else {
2991585Sbonwick 			/*
3001585Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3011585Sbonwick 			 */
3021585Sbonwick 			while (guid == 0 ||
3031585Sbonwick 			    spa_guid_exists(spa_guid(spa), guid))
3041585Sbonwick 				guid = spa_get_random(-1ULL);
3051585Sbonwick 		}
3061585Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3071585Sbonwick 	}
308789Sahrens 
309789Sahrens 	vd->vdev_spa = spa;
310789Sahrens 	vd->vdev_id = id;
311789Sahrens 	vd->vdev_guid = guid;
312789Sahrens 	vd->vdev_guid_sum = guid;
313789Sahrens 	vd->vdev_ops = ops;
314789Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
315789Sahrens 
316789Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3172856Snd150628 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
3187754SJeff.Bonwick@Sun.COM 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
319789Sahrens 	space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
320789Sahrens 	space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
321789Sahrens 	txg_list_create(&vd->vdev_ms_list,
322789Sahrens 	    offsetof(struct metaslab, ms_txg_node));
323789Sahrens 	txg_list_create(&vd->vdev_dtl_list,
324789Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
325789Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3264451Seschrock 	vdev_queue_init(vd);
3274451Seschrock 	vdev_cache_init(vd);
328789Sahrens 
329789Sahrens 	return (vd);
330789Sahrens }
331789Sahrens 
332789Sahrens /*
333789Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
334789Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
335789Sahrens  * different for each case.
336789Sahrens  */
3372082Seschrock int
3382082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
3392082Seschrock     int alloctype)
340789Sahrens {
341789Sahrens 	vdev_ops_t *ops;
342789Sahrens 	char *type;
3434527Sperrin 	uint64_t guid = 0, islog, nparity;
344789Sahrens 	vdev_t *vd;
345789Sahrens 
3467754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
347789Sahrens 
348789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
3492082Seschrock 		return (EINVAL);
350789Sahrens 
351789Sahrens 	if ((ops = vdev_getops(type)) == NULL)
3522082Seschrock 		return (EINVAL);
353789Sahrens 
354789Sahrens 	/*
355789Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
356789Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
357789Sahrens 	 */
358789Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
359789Sahrens 		uint64_t label_id;
360789Sahrens 
361789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
362789Sahrens 		    label_id != id)
3632082Seschrock 			return (EINVAL);
364789Sahrens 
365789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3662082Seschrock 			return (EINVAL);
3672082Seschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
3682082Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3692082Seschrock 			return (EINVAL);
3705450Sbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
3715450Sbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3725450Sbrendan 			return (EINVAL);
373789Sahrens 	}
374789Sahrens 
3752082Seschrock 	/*
3762082Seschrock 	 * The first allocated vdev must be of type 'root'.
3772082Seschrock 	 */
3782082Seschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
3792082Seschrock 		return (EINVAL);
3802082Seschrock 
3814527Sperrin 	/*
3824527Sperrin 	 * Determine whether we're a log vdev.
3834527Sperrin 	 */
3844527Sperrin 	islog = 0;
3854527Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
3865094Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
3874527Sperrin 		return (ENOTSUP);
3884527Sperrin 
3894527Sperrin 	/*
3904527Sperrin 	 * Set the nparity property for RAID-Z vdevs.
3914527Sperrin 	 */
3924527Sperrin 	nparity = -1ULL;
3934527Sperrin 	if (ops == &vdev_raidz_ops) {
3944527Sperrin 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3954527Sperrin 		    &nparity) == 0) {
3964527Sperrin 			/*
3974527Sperrin 			 * Currently, we can only support 2 parity devices.
3984527Sperrin 			 */
3994527Sperrin 			if (nparity == 0 || nparity > 2)
4004527Sperrin 				return (EINVAL);
4014527Sperrin 			/*
4024527Sperrin 			 * Older versions can only support 1 parity device.
4034527Sperrin 			 */
4044527Sperrin 			if (nparity == 2 &&
4054577Sahrens 			    spa_version(spa) < SPA_VERSION_RAID6)
4064527Sperrin 				return (ENOTSUP);
4074527Sperrin 		} else {
4084527Sperrin 			/*
4094527Sperrin 			 * We require the parity to be specified for SPAs that
4104527Sperrin 			 * support multiple parity levels.
4114527Sperrin 			 */
4124577Sahrens 			if (spa_version(spa) >= SPA_VERSION_RAID6)
4134527Sperrin 				return (EINVAL);
4144527Sperrin 			/*
4154527Sperrin 			 * Otherwise, we default to 1 parity device for RAID-Z.
4164527Sperrin 			 */
4174527Sperrin 			nparity = 1;
4184527Sperrin 		}
4194527Sperrin 	} else {
4204527Sperrin 		nparity = 0;
4214527Sperrin 	}
4224527Sperrin 	ASSERT(nparity != -1ULL);
4234527Sperrin 
424789Sahrens 	vd = vdev_alloc_common(spa, id, guid, ops);
425789Sahrens 
4264527Sperrin 	vd->vdev_islog = islog;
4274527Sperrin 	vd->vdev_nparity = nparity;
4284527Sperrin 
429789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
430789Sahrens 		vd->vdev_path = spa_strdup(vd->vdev_path);
431789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
432789Sahrens 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4334451Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4344451Seschrock 	    &vd->vdev_physpath) == 0)
4354451Seschrock 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
436789Sahrens 
437789Sahrens 	/*
4381171Seschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
4391171Seschrock 	 * as -1.
4401171Seschrock 	 */
4411171Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
4421171Seschrock 	    &vd->vdev_wholedisk) != 0)
4431171Seschrock 		vd->vdev_wholedisk = -1ULL;
4441171Seschrock 
4451171Seschrock 	/*
4461544Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
4471544Seschrock 	 * was not present at the time of import.
4481544Seschrock 	 */
4496643Seschrock 	if (!spa->spa_import_faulted)
4506643Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4516643Seschrock 		    &vd->vdev_not_present);
4521544Seschrock 
4531544Seschrock 	/*
4541732Sbonwick 	 * Get the alignment requirement.
4551732Sbonwick 	 */
4561732Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
4571732Sbonwick 
4581732Sbonwick 	/*
459789Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
460789Sahrens 	 */
461789Sahrens 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
462789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
463789Sahrens 		    &vd->vdev_ms_array);
464789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
465789Sahrens 		    &vd->vdev_ms_shift);
466789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
467789Sahrens 		    &vd->vdev_asize);
468789Sahrens 	}
469789Sahrens 
470789Sahrens 	/*
4714451Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
472789Sahrens 	 */
4736643Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
4746643Seschrock 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) {
4756643Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
4766643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
4776643Seschrock 			    &vd->vdev_dtl.smo_object);
4786643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
4796643Seschrock 			    &vd->vdev_unspare);
4806643Seschrock 		}
4811732Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
4821732Sbonwick 		    &vd->vdev_offline);
4836643Seschrock 
4844451Seschrock 		/*
4854451Seschrock 		 * When importing a pool, we want to ignore the persistent fault
4864451Seschrock 		 * state, as the diagnosis made on another system may not be
4874451Seschrock 		 * valid in the current context.
4884451Seschrock 		 */
4894451Seschrock 		if (spa->spa_load_state == SPA_LOAD_OPEN) {
4904451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
4914451Seschrock 			    &vd->vdev_faulted);
4924451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
4934451Seschrock 			    &vd->vdev_degraded);
4944451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
4954451Seschrock 			    &vd->vdev_removed);
4964451Seschrock 		}
497789Sahrens 	}
498789Sahrens 
499789Sahrens 	/*
500789Sahrens 	 * Add ourselves to the parent's list of children.
501789Sahrens 	 */
502789Sahrens 	vdev_add_child(parent, vd);
503789Sahrens 
5042082Seschrock 	*vdp = vd;
5052082Seschrock 
5062082Seschrock 	return (0);
507789Sahrens }
508789Sahrens 
509789Sahrens void
510789Sahrens vdev_free(vdev_t *vd)
511789Sahrens {
512789Sahrens 	int c;
5134451Seschrock 	spa_t *spa = vd->vdev_spa;
514789Sahrens 
515789Sahrens 	/*
516789Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
517789Sahrens 	 * trying to ensure complicated semantics for all callers.
518789Sahrens 	 */
519789Sahrens 	vdev_close(vd);
520789Sahrens 
5217754SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
522789Sahrens 
523789Sahrens 	/*
524789Sahrens 	 * Free all children.
525789Sahrens 	 */
526789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
527789Sahrens 		vdev_free(vd->vdev_child[c]);
528789Sahrens 
529789Sahrens 	ASSERT(vd->vdev_child == NULL);
530789Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
531789Sahrens 
532789Sahrens 	/*
533789Sahrens 	 * Discard allocation state.
534789Sahrens 	 */
535789Sahrens 	if (vd == vd->vdev_top)
536789Sahrens 		vdev_metaslab_fini(vd);
537789Sahrens 
538789Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
5392082Seschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
540789Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
541789Sahrens 
542789Sahrens 	/*
543789Sahrens 	 * Remove this vdev from its parent's child list.
544789Sahrens 	 */
545789Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
546789Sahrens 
547789Sahrens 	ASSERT(vd->vdev_parent == NULL);
548789Sahrens 
5494451Seschrock 	/*
5504451Seschrock 	 * Clean up vdev structure.
5514451Seschrock 	 */
5524451Seschrock 	vdev_queue_fini(vd);
5534451Seschrock 	vdev_cache_fini(vd);
5544451Seschrock 
5554451Seschrock 	if (vd->vdev_path)
5564451Seschrock 		spa_strfree(vd->vdev_path);
5574451Seschrock 	if (vd->vdev_devid)
5584451Seschrock 		spa_strfree(vd->vdev_devid);
5594451Seschrock 	if (vd->vdev_physpath)
5604451Seschrock 		spa_strfree(vd->vdev_physpath);
5614451Seschrock 
5624451Seschrock 	if (vd->vdev_isspare)
5634451Seschrock 		spa_spare_remove(vd);
5645450Sbrendan 	if (vd->vdev_isl2cache)
5655450Sbrendan 		spa_l2cache_remove(vd);
5664451Seschrock 
5674451Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
5684451Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
5694451Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
5704451Seschrock 	space_map_unload(&vd->vdev_dtl_map);
5714451Seschrock 	space_map_destroy(&vd->vdev_dtl_map);
5724451Seschrock 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
5734451Seschrock 	space_map_destroy(&vd->vdev_dtl_scrub);
5744451Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
5754451Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
5764451Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
5777754SJeff.Bonwick@Sun.COM 	mutex_destroy(&vd->vdev_probe_lock);
5784451Seschrock 
5794451Seschrock 	if (vd == spa->spa_root_vdev)
5804451Seschrock 		spa->spa_root_vdev = NULL;
5814451Seschrock 
5824451Seschrock 	kmem_free(vd, sizeof (vdev_t));
583789Sahrens }
584789Sahrens 
585789Sahrens /*
586789Sahrens  * Transfer top-level vdev state from svd to tvd.
587789Sahrens  */
588789Sahrens static void
589789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
590789Sahrens {
591789Sahrens 	spa_t *spa = svd->vdev_spa;
592789Sahrens 	metaslab_t *msp;
593789Sahrens 	vdev_t *vd;
594789Sahrens 	int t;
595789Sahrens 
596789Sahrens 	ASSERT(tvd == tvd->vdev_top);
597789Sahrens 
598789Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
599789Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
600789Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
601789Sahrens 
602789Sahrens 	svd->vdev_ms_array = 0;
603789Sahrens 	svd->vdev_ms_shift = 0;
604789Sahrens 	svd->vdev_ms_count = 0;
605789Sahrens 
606789Sahrens 	tvd->vdev_mg = svd->vdev_mg;
607789Sahrens 	tvd->vdev_ms = svd->vdev_ms;
608789Sahrens 
609789Sahrens 	svd->vdev_mg = NULL;
610789Sahrens 	svd->vdev_ms = NULL;
6111732Sbonwick 
6121732Sbonwick 	if (tvd->vdev_mg != NULL)
6131732Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
614789Sahrens 
615789Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
616789Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
6172082Seschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
618789Sahrens 
619789Sahrens 	svd->vdev_stat.vs_alloc = 0;
620789Sahrens 	svd->vdev_stat.vs_space = 0;
6212082Seschrock 	svd->vdev_stat.vs_dspace = 0;
622789Sahrens 
623789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
624789Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
625789Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
626789Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
627789Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
628789Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
629789Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
630789Sahrens 	}
631789Sahrens 
6327754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_config_dirty_node)) {
633789Sahrens 		vdev_config_clean(svd);
634789Sahrens 		vdev_config_dirty(tvd);
635789Sahrens 	}
636789Sahrens 
6377754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_state_dirty_node)) {
6387754SJeff.Bonwick@Sun.COM 		vdev_state_clean(svd);
6397754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(tvd);
6407754SJeff.Bonwick@Sun.COM 	}
6417754SJeff.Bonwick@Sun.COM 
6422082Seschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
6432082Seschrock 	svd->vdev_deflate_ratio = 0;
6444527Sperrin 
6454527Sperrin 	tvd->vdev_islog = svd->vdev_islog;
6464527Sperrin 	svd->vdev_islog = 0;
647789Sahrens }
648789Sahrens 
649789Sahrens static void
650789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
651789Sahrens {
652789Sahrens 	int c;
653789Sahrens 
654789Sahrens 	if (vd == NULL)
655789Sahrens 		return;
656789Sahrens 
657789Sahrens 	vd->vdev_top = tvd;
658789Sahrens 
659789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
660789Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
661789Sahrens }
662789Sahrens 
663789Sahrens /*
664789Sahrens  * Add a mirror/replacing vdev above an existing vdev.
665789Sahrens  */
666789Sahrens vdev_t *
667789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
668789Sahrens {
669789Sahrens 	spa_t *spa = cvd->vdev_spa;
670789Sahrens 	vdev_t *pvd = cvd->vdev_parent;
671789Sahrens 	vdev_t *mvd;
672789Sahrens 
6737754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
674789Sahrens 
675789Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
6761732Sbonwick 
6771732Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
6781732Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
6791732Sbonwick 	mvd->vdev_state = cvd->vdev_state;
6801732Sbonwick 
681789Sahrens 	vdev_remove_child(pvd, cvd);
682789Sahrens 	vdev_add_child(pvd, mvd);
683789Sahrens 	cvd->vdev_id = mvd->vdev_children;
684789Sahrens 	vdev_add_child(mvd, cvd);
685789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
686789Sahrens 
687789Sahrens 	if (mvd == mvd->vdev_top)
688789Sahrens 		vdev_top_transfer(cvd, mvd);
689789Sahrens 
690789Sahrens 	return (mvd);
691789Sahrens }
692789Sahrens 
693789Sahrens /*
694789Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
695789Sahrens  */
696789Sahrens void
697789Sahrens vdev_remove_parent(vdev_t *cvd)
698789Sahrens {
699789Sahrens 	vdev_t *mvd = cvd->vdev_parent;
700789Sahrens 	vdev_t *pvd = mvd->vdev_parent;
701789Sahrens 
7027754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
703789Sahrens 
704789Sahrens 	ASSERT(mvd->vdev_children == 1);
705789Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
7062082Seschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
7072082Seschrock 	    mvd->vdev_ops == &vdev_spare_ops);
7081732Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
709789Sahrens 
710789Sahrens 	vdev_remove_child(mvd, cvd);
711789Sahrens 	vdev_remove_child(pvd, mvd);
7127754SJeff.Bonwick@Sun.COM 	/*
7137754SJeff.Bonwick@Sun.COM 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
7147754SJeff.Bonwick@Sun.COM 	 * Otherwise, we could have detached an offline device, and when we
7157754SJeff.Bonwick@Sun.COM 	 * go to import the pool we'll think we have two top-level vdevs,
7167754SJeff.Bonwick@Sun.COM 	 * instead of a different version of the same top-level vdev.
7177754SJeff.Bonwick@Sun.COM 	 */
7187754SJeff.Bonwick@Sun.COM 	if (mvd->vdev_top == mvd)
7197754SJeff.Bonwick@Sun.COM 		cvd->vdev_guid = cvd->vdev_guid_sum = mvd->vdev_guid;
720789Sahrens 	cvd->vdev_id = mvd->vdev_id;
721789Sahrens 	vdev_add_child(pvd, cvd);
722789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
723789Sahrens 
724789Sahrens 	if (cvd == cvd->vdev_top)
725789Sahrens 		vdev_top_transfer(mvd, cvd);
726789Sahrens 
727789Sahrens 	ASSERT(mvd->vdev_children == 0);
728789Sahrens 	vdev_free(mvd);
729789Sahrens }
730789Sahrens 
7311544Seschrock int
732789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
733789Sahrens {
734789Sahrens 	spa_t *spa = vd->vdev_spa;
7351732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
7364527Sperrin 	metaslab_class_t *mc;
7371732Sbonwick 	uint64_t m;
738789Sahrens 	uint64_t oldc = vd->vdev_ms_count;
739789Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
7401732Sbonwick 	metaslab_t **mspp;
7411732Sbonwick 	int error;
742789Sahrens 
7431585Sbonwick 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
7441585Sbonwick 		return (0);
7451585Sbonwick 
746789Sahrens 	ASSERT(oldc <= newc);
747789Sahrens 
7484527Sperrin 	if (vd->vdev_islog)
7494527Sperrin 		mc = spa->spa_log_class;
7504527Sperrin 	else
7514527Sperrin 		mc = spa->spa_normal_class;
7524527Sperrin 
7531732Sbonwick 	if (vd->vdev_mg == NULL)
7541732Sbonwick 		vd->vdev_mg = metaslab_group_create(mc, vd);
7551732Sbonwick 
7561732Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
7571732Sbonwick 
7581732Sbonwick 	if (oldc != 0) {
7591732Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
7601732Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
7611732Sbonwick 	}
7621732Sbonwick 
7631732Sbonwick 	vd->vdev_ms = mspp;
764789Sahrens 	vd->vdev_ms_count = newc;
765789Sahrens 
7661732Sbonwick 	for (m = oldc; m < newc; m++) {
7671732Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
768789Sahrens 		if (txg == 0) {
7691732Sbonwick 			uint64_t object = 0;
7701732Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
7711732Sbonwick 			    m * sizeof (uint64_t), sizeof (uint64_t), &object);
7721732Sbonwick 			if (error)
7731732Sbonwick 				return (error);
7741732Sbonwick 			if (object != 0) {
7751732Sbonwick 				dmu_buf_t *db;
7761732Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
7771732Sbonwick 				if (error)
7781732Sbonwick 					return (error);
7794944Smaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
7804944Smaybee 				bcopy(db->db_data, &smo, sizeof (smo));
7811732Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
7821544Seschrock 				dmu_buf_rele(db, FTAG);
783789Sahrens 			}
784789Sahrens 		}
7851732Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
7861732Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
787789Sahrens 	}
788789Sahrens 
7891544Seschrock 	return (0);
790789Sahrens }
791789Sahrens 
792789Sahrens void
793789Sahrens vdev_metaslab_fini(vdev_t *vd)
794789Sahrens {
795789Sahrens 	uint64_t m;
796789Sahrens 	uint64_t count = vd->vdev_ms_count;
797789Sahrens 
798789Sahrens 	if (vd->vdev_ms != NULL) {
799789Sahrens 		for (m = 0; m < count; m++)
8001732Sbonwick 			if (vd->vdev_ms[m] != NULL)
8011732Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
802789Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
803789Sahrens 		vd->vdev_ms = NULL;
804789Sahrens 	}
805789Sahrens }
806789Sahrens 
8077754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats {
8087754SJeff.Bonwick@Sun.COM 	boolean_t	vps_readable;
8097754SJeff.Bonwick@Sun.COM 	boolean_t	vps_writeable;
8107754SJeff.Bonwick@Sun.COM 	int		vps_flags;
8117754SJeff.Bonwick@Sun.COM 	zio_t		*vps_root;
8127754SJeff.Bonwick@Sun.COM 	vdev_t		*vps_vd;
8137754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t;
8147754SJeff.Bonwick@Sun.COM 
8157754SJeff.Bonwick@Sun.COM static void
8167754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio)
8175329Sgw25295 {
8187754SJeff.Bonwick@Sun.COM 	vdev_probe_stats_t *vps = zio->io_private;
8197754SJeff.Bonwick@Sun.COM 	vdev_t *vd = vps->vps_vd;
8207754SJeff.Bonwick@Sun.COM 
8217754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ) {
8227754SJeff.Bonwick@Sun.COM 		ASSERT(zio->io_vd == vd);
8237754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8247754SJeff.Bonwick@Sun.COM 			vps->vps_readable = 1;
8257754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0 && (spa_mode & FWRITE)) {
8267754SJeff.Bonwick@Sun.COM 			zio_nowait(zio_write_phys(vps->vps_root, vd,
8277754SJeff.Bonwick@Sun.COM 			    zio->io_offset, zio->io_size, zio->io_data,
8287754SJeff.Bonwick@Sun.COM 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
8297754SJeff.Bonwick@Sun.COM 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
8307754SJeff.Bonwick@Sun.COM 		} else {
8317754SJeff.Bonwick@Sun.COM 			zio_buf_free(zio->io_data, zio->io_size);
8327754SJeff.Bonwick@Sun.COM 		}
8337754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
8347754SJeff.Bonwick@Sun.COM 		ASSERT(zio->io_vd == vd);
8357754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8367754SJeff.Bonwick@Sun.COM 			vps->vps_writeable = 1;
8377754SJeff.Bonwick@Sun.COM 		zio_buf_free(zio->io_data, zio->io_size);
8387754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_NULL) {
8397754SJeff.Bonwick@Sun.COM 		ASSERT(zio->io_vd == NULL);
8407754SJeff.Bonwick@Sun.COM 		ASSERT(zio == vps->vps_root);
8417754SJeff.Bonwick@Sun.COM 
8427754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read |= !vps->vps_readable;
8437754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write |= !vps->vps_writeable;
8447754SJeff.Bonwick@Sun.COM 
8457754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) &&
8467754SJeff.Bonwick@Sun.COM 		    (vdev_writeable(vd) || !(spa_mode & FWRITE))) {
8477754SJeff.Bonwick@Sun.COM 			zio->io_error = 0;
8487754SJeff.Bonwick@Sun.COM 		} else {
8497754SJeff.Bonwick@Sun.COM 			ASSERT(zio->io_error != 0);
8507754SJeff.Bonwick@Sun.COM 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
8517754SJeff.Bonwick@Sun.COM 			    zio->io_spa, vd, NULL, 0, 0);
8527754SJeff.Bonwick@Sun.COM 			zio->io_error = ENXIO;
8537754SJeff.Bonwick@Sun.COM 		}
8547754SJeff.Bonwick@Sun.COM 		kmem_free(vps, sizeof (*vps));
8557754SJeff.Bonwick@Sun.COM 	}
8567754SJeff.Bonwick@Sun.COM }
8575329Sgw25295 
8587754SJeff.Bonwick@Sun.COM /*
8597754SJeff.Bonwick@Sun.COM  * Determine whether this device is accessible by reading and writing
8607754SJeff.Bonwick@Sun.COM  * to several known locations: the pad regions of each vdev label
8617754SJeff.Bonwick@Sun.COM  * but the first (which we leave alone in case it contains a VTOC).
8627754SJeff.Bonwick@Sun.COM  */
8637754SJeff.Bonwick@Sun.COM zio_t *
8647754SJeff.Bonwick@Sun.COM vdev_probe(vdev_t *vd, zio_t *pio)
8657754SJeff.Bonwick@Sun.COM {
8667754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
8677754SJeff.Bonwick@Sun.COM 	vdev_probe_stats_t *vps;
8687754SJeff.Bonwick@Sun.COM 	zio_t *zio;
8697754SJeff.Bonwick@Sun.COM 
8707754SJeff.Bonwick@Sun.COM 	vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
8717754SJeff.Bonwick@Sun.COM 
8727754SJeff.Bonwick@Sun.COM 	vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
8737754SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_DONT_RETRY;
8745329Sgw25295 
8757754SJeff.Bonwick@Sun.COM 	if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
8767754SJeff.Bonwick@Sun.COM 		/*
8777754SJeff.Bonwick@Sun.COM 		 * vdev_cant_read and vdev_cant_write can only transition
8787754SJeff.Bonwick@Sun.COM 		 * from TRUE to FALSE when we have the SCL_ZIO lock as writer;
8797754SJeff.Bonwick@Sun.COM 		 * otherwise they can only transition from FALSE to TRUE.
8807754SJeff.Bonwick@Sun.COM 		 * This ensures that any zio looking at these values can
8817754SJeff.Bonwick@Sun.COM 		 * assume that failures persist for the life of the I/O.
8827754SJeff.Bonwick@Sun.COM 		 * That's important because when a device has intermittent
8837754SJeff.Bonwick@Sun.COM 		 * connectivity problems, we want to ensure that they're
8847754SJeff.Bonwick@Sun.COM 		 * ascribed to the device (ENXIO) and not the zio (EIO).
8857754SJeff.Bonwick@Sun.COM 		 *
8867754SJeff.Bonwick@Sun.COM 		 * Since we hold SCL_ZIO as writer here, clear both values
8877754SJeff.Bonwick@Sun.COM 		 * so the probe can reevaluate from first principles.
8887754SJeff.Bonwick@Sun.COM 		 */
8897754SJeff.Bonwick@Sun.COM 		vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
8907754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read = B_FALSE;
8917754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write = B_FALSE;
8927754SJeff.Bonwick@Sun.COM 	}
8937754SJeff.Bonwick@Sun.COM 
8947754SJeff.Bonwick@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
8957754SJeff.Bonwick@Sun.COM 
8967754SJeff.Bonwick@Sun.COM 	zio = zio_null(pio, spa, vdev_probe_done, vps, vps->vps_flags);
8977754SJeff.Bonwick@Sun.COM 
8987754SJeff.Bonwick@Sun.COM 	vps->vps_root = zio;
8997754SJeff.Bonwick@Sun.COM 	vps->vps_vd = vd;
9007754SJeff.Bonwick@Sun.COM 
9017754SJeff.Bonwick@Sun.COM 	for (int l = 1; l < VDEV_LABELS; l++) {
9027754SJeff.Bonwick@Sun.COM 		zio_nowait(zio_read_phys(zio, vd,
9037754SJeff.Bonwick@Sun.COM 		    vdev_label_offset(vd->vdev_psize, l,
9047754SJeff.Bonwick@Sun.COM 		    offsetof(vdev_label_t, vl_pad)),
9057754SJeff.Bonwick@Sun.COM 		    VDEV_SKIP_SIZE, zio_buf_alloc(VDEV_SKIP_SIZE),
9067754SJeff.Bonwick@Sun.COM 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
9077754SJeff.Bonwick@Sun.COM 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
9087754SJeff.Bonwick@Sun.COM 	}
9097754SJeff.Bonwick@Sun.COM 
9107754SJeff.Bonwick@Sun.COM 	return (zio);
9115329Sgw25295 }
9125329Sgw25295 
913789Sahrens /*
914789Sahrens  * Prepare a virtual device for access.
915789Sahrens  */
916789Sahrens int
917789Sahrens vdev_open(vdev_t *vd)
918789Sahrens {
919789Sahrens 	int error;
920789Sahrens 	int c;
921789Sahrens 	uint64_t osize = 0;
922789Sahrens 	uint64_t asize, psize;
9231732Sbonwick 	uint64_t ashift = 0;
924789Sahrens 
925789Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
926789Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
927789Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
928789Sahrens 
929789Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
930789Sahrens 
9314451Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
9324451Seschrock 		ASSERT(vd->vdev_children == 0);
9334451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
9344451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
9354451Seschrock 		return (ENXIO);
9364451Seschrock 	} else if (vd->vdev_offline) {
937789Sahrens 		ASSERT(vd->vdev_children == 0);
9381544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
939789Sahrens 		return (ENXIO);
940789Sahrens 	}
941789Sahrens 
942789Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
943789Sahrens 
9441544Seschrock 	if (zio_injection_enabled && error == 0)
9451544Seschrock 		error = zio_handle_device_injection(vd, ENXIO);
9461544Seschrock 
9474451Seschrock 	if (error) {
9484451Seschrock 		if (vd->vdev_removed &&
9494451Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
9504451Seschrock 			vd->vdev_removed = B_FALSE;
951789Sahrens 
9521544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
953789Sahrens 		    vd->vdev_stat.vs_aux);
954789Sahrens 		return (error);
955789Sahrens 	}
956789Sahrens 
9574451Seschrock 	vd->vdev_removed = B_FALSE;
9584451Seschrock 
9594451Seschrock 	if (vd->vdev_degraded) {
9604451Seschrock 		ASSERT(vd->vdev_children == 0);
9614451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
9624451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
9634451Seschrock 	} else {
9644451Seschrock 		vd->vdev_state = VDEV_STATE_HEALTHY;
9654451Seschrock 	}
966789Sahrens 
967789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
9681544Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
9691544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
9701544Seschrock 			    VDEV_AUX_NONE);
9711544Seschrock 			break;
9721544Seschrock 		}
973789Sahrens 
974789Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
975789Sahrens 
976789Sahrens 	if (vd->vdev_children == 0) {
977789Sahrens 		if (osize < SPA_MINDEVSIZE) {
9781544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
9791544Seschrock 			    VDEV_AUX_TOO_SMALL);
980789Sahrens 			return (EOVERFLOW);
981789Sahrens 		}
982789Sahrens 		psize = osize;
983789Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
984789Sahrens 	} else {
9851732Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
986789Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
9871544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
9881544Seschrock 			    VDEV_AUX_TOO_SMALL);
989789Sahrens 			return (EOVERFLOW);
990789Sahrens 		}
991789Sahrens 		psize = 0;
992789Sahrens 		asize = osize;
993789Sahrens 	}
994789Sahrens 
995789Sahrens 	vd->vdev_psize = psize;
996789Sahrens 
997789Sahrens 	if (vd->vdev_asize == 0) {
998789Sahrens 		/*
999789Sahrens 		 * This is the first-ever open, so use the computed values.
10001732Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1001789Sahrens 		 */
1002789Sahrens 		vd->vdev_asize = asize;
10031732Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1004789Sahrens 	} else {
1005789Sahrens 		/*
1006789Sahrens 		 * Make sure the alignment requirement hasn't increased.
1007789Sahrens 		 */
10081732Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
10091544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10101544Seschrock 			    VDEV_AUX_BAD_LABEL);
1011789Sahrens 			return (EINVAL);
1012789Sahrens 		}
1013789Sahrens 
1014789Sahrens 		/*
1015789Sahrens 		 * Make sure the device hasn't shrunk.
1016789Sahrens 		 */
1017789Sahrens 		if (asize < vd->vdev_asize) {
10181544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10191544Seschrock 			    VDEV_AUX_BAD_LABEL);
1020789Sahrens 			return (EINVAL);
1021789Sahrens 		}
1022789Sahrens 
1023789Sahrens 		/*
1024789Sahrens 		 * If all children are healthy and the asize has increased,
1025789Sahrens 		 * then we've experienced dynamic LUN growth.
1026789Sahrens 		 */
1027789Sahrens 		if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1028789Sahrens 		    asize > vd->vdev_asize) {
1029789Sahrens 			vd->vdev_asize = asize;
1030789Sahrens 		}
1031789Sahrens 	}
1032789Sahrens 
10331544Seschrock 	/*
10345329Sgw25295 	 * Ensure we can issue some IO before declaring the
10355329Sgw25295 	 * vdev open for business.
10365329Sgw25295 	 */
10377754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf &&
10387754SJeff.Bonwick@Sun.COM 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
10395329Sgw25295 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10407754SJeff.Bonwick@Sun.COM 		    VDEV_AUX_IO_FAILURE);
10415329Sgw25295 		return (error);
10425329Sgw25295 	}
10435329Sgw25295 
10445329Sgw25295 	/*
10452082Seschrock 	 * If this is a top-level vdev, compute the raidz-deflation
10462082Seschrock 	 * ratio.  Note, we hard-code in 128k (1<<17) because it is the
10472082Seschrock 	 * current "typical" blocksize.  Even if SPA_MAXBLOCKSIZE
10482082Seschrock 	 * changes, this algorithm must never change, or we will
10492082Seschrock 	 * inconsistently account for existing bp's.
10502082Seschrock 	 */
10512082Seschrock 	if (vd->vdev_top == vd) {
10522082Seschrock 		vd->vdev_deflate_ratio = (1<<17) /
10532082Seschrock 		    (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
10542082Seschrock 	}
10552082Seschrock 
10567046Sahrens 	/*
10577046Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
10587046Sahrens 	 * resilver.  But don't do this if we are doing a reopen for a
10597046Sahrens 	 * scrub, since this would just restart the scrub we are already
10607046Sahrens 	 * doing.
10617046Sahrens 	 */
10627046Sahrens 	if (vd->vdev_children == 0 && !vd->vdev_spa->spa_scrub_reopen) {
10637046Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
10647046Sahrens 		if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd))
10657046Sahrens 			spa_async_request(vd->vdev_spa, SPA_ASYNC_RESILVER);
10667046Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
10677046Sahrens 	}
10687046Sahrens 
1069789Sahrens 	return (0);
1070789Sahrens }
1071789Sahrens 
1072789Sahrens /*
10731986Seschrock  * Called once the vdevs are all opened, this routine validates the label
10741986Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
10754451Seschrock  * inadvertently do repair I/Os to the wrong device.
10761986Seschrock  *
10771986Seschrock  * This function will only return failure if one of the vdevs indicates that it
10781986Seschrock  * has since been destroyed or exported.  This is only possible if
10791986Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
10801986Seschrock  * will be updated but the function will return 0.
10811986Seschrock  */
10821986Seschrock int
10831986Seschrock vdev_validate(vdev_t *vd)
10841986Seschrock {
10851986Seschrock 	spa_t *spa = vd->vdev_spa;
10861986Seschrock 	int c;
10871986Seschrock 	nvlist_t *label;
10887754SJeff.Bonwick@Sun.COM 	uint64_t guid, top_guid;
10891986Seschrock 	uint64_t state;
10901986Seschrock 
10911986Seschrock 	for (c = 0; c < vd->vdev_children; c++)
10921986Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
10934070Smc142369 			return (EBADF);
10941986Seschrock 
10952174Seschrock 	/*
10962174Seschrock 	 * If the device has already failed, or was marked offline, don't do
10972174Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
10982174Seschrock 	 * overwrite the previous state.
10992174Seschrock 	 */
11007754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
11011986Seschrock 
11021986Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
11031986Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11041986Seschrock 			    VDEV_AUX_BAD_LABEL);
11051986Seschrock 			return (0);
11061986Seschrock 		}
11071986Seschrock 
11081986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
11091986Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
11101986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
11111986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11121986Seschrock 			nvlist_free(label);
11131986Seschrock 			return (0);
11141986Seschrock 		}
11151986Seschrock 
11167754SJeff.Bonwick@Sun.COM 		/*
11177754SJeff.Bonwick@Sun.COM 		 * If this vdev just became a top-level vdev because its
11187754SJeff.Bonwick@Sun.COM 		 * sibling was detached, it will have adopted the parent's
11197754SJeff.Bonwick@Sun.COM 		 * vdev guid -- but the label may or may not be on disk yet.
11207754SJeff.Bonwick@Sun.COM 		 * Fortunately, either version of the label will have the
11217754SJeff.Bonwick@Sun.COM 		 * same top guid, so if we're a top-level vdev, we can
11227754SJeff.Bonwick@Sun.COM 		 * safely compare to that instead.
11237754SJeff.Bonwick@Sun.COM 		 */
11241986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
11257754SJeff.Bonwick@Sun.COM 		    &guid) != 0 ||
11267754SJeff.Bonwick@Sun.COM 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
11277754SJeff.Bonwick@Sun.COM 		    &top_guid) != 0 ||
11287754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != guid &&
11297754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
11301986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
11311986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11321986Seschrock 			nvlist_free(label);
11331986Seschrock 			return (0);
11341986Seschrock 		}
11351986Seschrock 
11361986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
11371986Seschrock 		    &state) != 0) {
11381986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
11391986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11401986Seschrock 			nvlist_free(label);
11411986Seschrock 			return (0);
11421986Seschrock 		}
11431986Seschrock 
11441986Seschrock 		nvlist_free(label);
11451986Seschrock 
11461986Seschrock 		if (spa->spa_load_state == SPA_LOAD_OPEN &&
11471986Seschrock 		    state != POOL_STATE_ACTIVE)
11484070Smc142369 			return (EBADF);
11496976Seschrock 
11506976Seschrock 		/*
11516976Seschrock 		 * If we were able to open and validate a vdev that was
11526976Seschrock 		 * previously marked permanently unavailable, clear that state
11536976Seschrock 		 * now.
11546976Seschrock 		 */
11556976Seschrock 		if (vd->vdev_not_present)
11566976Seschrock 			vd->vdev_not_present = 0;
11571986Seschrock 	}
11581986Seschrock 
11591986Seschrock 	return (0);
11601986Seschrock }
11611986Seschrock 
11621986Seschrock /*
1163789Sahrens  * Close a virtual device.
1164789Sahrens  */
1165789Sahrens void
1166789Sahrens vdev_close(vdev_t *vd)
1167789Sahrens {
1168789Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1169789Sahrens 
11704451Seschrock 	vdev_cache_purge(vd);
1171789Sahrens 
11721986Seschrock 	/*
11731986Seschrock 	 * We record the previous state before we close it, so  that if we are
11741986Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
11751986Seschrock 	 * it's still faulted.
11761986Seschrock 	 */
11771986Seschrock 	vd->vdev_prevstate = vd->vdev_state;
11781986Seschrock 
1179789Sahrens 	if (vd->vdev_offline)
1180789Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1181789Sahrens 	else
1182789Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
11831544Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1184789Sahrens }
1185789Sahrens 
1186789Sahrens void
11871544Seschrock vdev_reopen(vdev_t *vd)
1188789Sahrens {
11891544Seschrock 	spa_t *spa = vd->vdev_spa;
1190789Sahrens 
11917754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
11921544Seschrock 
1193789Sahrens 	vdev_close(vd);
1194789Sahrens 	(void) vdev_open(vd);
1195789Sahrens 
1196789Sahrens 	/*
11973377Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
11983377Seschrock 	 * Otherwise, a device with an invalid label could be successfully
11993377Seschrock 	 * opened in response to vdev_reopen().
12003377Seschrock 	 */
12016643Seschrock 	if (vd->vdev_aux) {
12026643Seschrock 		(void) vdev_validate_aux(vd);
12037754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) && vdev_writeable(vd) &&
12046643Seschrock 		    !l2arc_vdev_present(vd)) {
12056643Seschrock 			uint64_t size = vdev_get_rsize(vd);
12066643Seschrock 			l2arc_add_vdev(spa, vd,
12076643Seschrock 			    VDEV_LABEL_START_SIZE,
12086643Seschrock 			    size - VDEV_LABEL_START_SIZE);
12096643Seschrock 		}
12106643Seschrock 	} else {
12116643Seschrock 		(void) vdev_validate(vd);
12126643Seschrock 	}
12133377Seschrock 
12143377Seschrock 	/*
12154451Seschrock 	 * Reassess parent vdev's health.
1216789Sahrens 	 */
12174451Seschrock 	vdev_propagate_state(vd);
1218789Sahrens }
1219789Sahrens 
1220789Sahrens int
12212082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1222789Sahrens {
1223789Sahrens 	int error;
1224789Sahrens 
1225789Sahrens 	/*
1226789Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1227789Sahrens 	 * For a create, however, we want to fail the request if
1228789Sahrens 	 * there are any components we can't open.
1229789Sahrens 	 */
1230789Sahrens 	error = vdev_open(vd);
1231789Sahrens 
1232789Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1233789Sahrens 		vdev_close(vd);
1234789Sahrens 		return (error ? error : ENXIO);
1235789Sahrens 	}
1236789Sahrens 
1237789Sahrens 	/*
1238789Sahrens 	 * Recursively initialize all labels.
1239789Sahrens 	 */
12403377Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
12413377Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1242789Sahrens 		vdev_close(vd);
1243789Sahrens 		return (error);
1244789Sahrens 	}
1245789Sahrens 
1246789Sahrens 	return (0);
1247789Sahrens }
1248789Sahrens 
1249789Sahrens /*
1250789Sahrens  * The is the latter half of vdev_create().  It is distinct because it
1251789Sahrens  * involves initiating transactions in order to do metaslab creation.
1252789Sahrens  * For creation, we want to try to create all vdevs at once and then undo it
1253789Sahrens  * if anything fails; this is much harder if we have pending transactions.
1254789Sahrens  */
12551585Sbonwick void
1256789Sahrens vdev_init(vdev_t *vd, uint64_t txg)
1257789Sahrens {
1258789Sahrens 	/*
1259789Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1260789Sahrens 	 */
1261789Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1262789Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1263789Sahrens 
1264789Sahrens 	/*
12651585Sbonwick 	 * Initialize the vdev's metaslabs.  This can't fail because
12661585Sbonwick 	 * there's nothing to read when creating all new metaslabs.
1267789Sahrens 	 */
12681585Sbonwick 	VERIFY(vdev_metaslab_init(vd, txg) == 0);
1269789Sahrens }
1270789Sahrens 
1271789Sahrens void
12721732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1273789Sahrens {
12741732Sbonwick 	ASSERT(vd == vd->vdev_top);
12751732Sbonwick 	ASSERT(ISP2(flags));
1276789Sahrens 
12771732Sbonwick 	if (flags & VDD_METASLAB)
12781732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
12791732Sbonwick 
12801732Sbonwick 	if (flags & VDD_DTL)
12811732Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
12821732Sbonwick 
12831732Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1284789Sahrens }
1285789Sahrens 
1286789Sahrens void
1287789Sahrens vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1288789Sahrens {
1289789Sahrens 	mutex_enter(sm->sm_lock);
1290789Sahrens 	if (!space_map_contains(sm, txg, size))
1291789Sahrens 		space_map_add(sm, txg, size);
1292789Sahrens 	mutex_exit(sm->sm_lock);
1293789Sahrens }
1294789Sahrens 
1295789Sahrens int
1296789Sahrens vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1297789Sahrens {
1298789Sahrens 	int dirty;
1299789Sahrens 
1300789Sahrens 	/*
1301789Sahrens 	 * Quick test without the lock -- covers the common case that
1302789Sahrens 	 * there are no dirty time segments.
1303789Sahrens 	 */
1304789Sahrens 	if (sm->sm_space == 0)
1305789Sahrens 		return (0);
1306789Sahrens 
1307789Sahrens 	mutex_enter(sm->sm_lock);
1308789Sahrens 	dirty = space_map_contains(sm, txg, size);
1309789Sahrens 	mutex_exit(sm->sm_lock);
1310789Sahrens 
1311789Sahrens 	return (dirty);
1312789Sahrens }
1313789Sahrens 
1314789Sahrens /*
1315789Sahrens  * Reassess DTLs after a config change or scrub completion.
1316789Sahrens  */
1317789Sahrens void
1318789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1319789Sahrens {
13201544Seschrock 	spa_t *spa = vd->vdev_spa;
1321789Sahrens 	int c;
1322789Sahrens 
13237754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
1324789Sahrens 
1325789Sahrens 	if (vd->vdev_children == 0) {
1326789Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
13277046Sahrens 		if (scrub_txg != 0 &&
13287046Sahrens 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
13297046Sahrens 			/* XXX should check scrub_done? */
13307046Sahrens 			/*
13317046Sahrens 			 * We completed a scrub up to scrub_txg.  If we
13327046Sahrens 			 * did it without rebooting, then the scrub dtl
13337046Sahrens 			 * will be valid, so excise the old region and
13347046Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
13357046Sahrens 			 * dtl as-is if there was an error.
13367046Sahrens 			 */
1337789Sahrens 			space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1338789Sahrens 			space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1339789Sahrens 		}
1340789Sahrens 		if (scrub_done)
1341789Sahrens 			space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1342789Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
13437046Sahrens 
13441732Sbonwick 		if (txg != 0)
13451732Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1346789Sahrens 		return;
1347789Sahrens 	}
1348789Sahrens 
13491544Seschrock 	/*
13501544Seschrock 	 * Make sure the DTLs are always correct under the scrub lock.
13511544Seschrock 	 */
13521544Seschrock 	if (vd == spa->spa_root_vdev)
13531544Seschrock 		mutex_enter(&spa->spa_scrub_lock);
13541544Seschrock 
1355789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
1356789Sahrens 	space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1357789Sahrens 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1358789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1359789Sahrens 
1360789Sahrens 	for (c = 0; c < vd->vdev_children; c++) {
1361789Sahrens 		vdev_t *cvd = vd->vdev_child[c];
1362789Sahrens 		vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1363789Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
1364789Sahrens 		space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1365789Sahrens 		space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1366789Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
1367789Sahrens 	}
13681544Seschrock 
13691544Seschrock 	if (vd == spa->spa_root_vdev)
13701544Seschrock 		mutex_exit(&spa->spa_scrub_lock);
1371789Sahrens }
1372789Sahrens 
1373789Sahrens static int
1374789Sahrens vdev_dtl_load(vdev_t *vd)
1375789Sahrens {
1376789Sahrens 	spa_t *spa = vd->vdev_spa;
1377789Sahrens 	space_map_obj_t *smo = &vd->vdev_dtl;
13781732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1379789Sahrens 	dmu_buf_t *db;
1380789Sahrens 	int error;
1381789Sahrens 
1382789Sahrens 	ASSERT(vd->vdev_children == 0);
1383789Sahrens 
1384789Sahrens 	if (smo->smo_object == 0)
1385789Sahrens 		return (0);
1386789Sahrens 
13871732Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
13881544Seschrock 		return (error);
13891732Sbonwick 
13904944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
13914944Smaybee 	bcopy(db->db_data, smo, sizeof (*smo));
13921544Seschrock 	dmu_buf_rele(db, FTAG);
1393789Sahrens 
1394789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
13951732Sbonwick 	error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1396789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1397789Sahrens 
1398789Sahrens 	return (error);
1399789Sahrens }
1400789Sahrens 
1401789Sahrens void
1402789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1403789Sahrens {
1404789Sahrens 	spa_t *spa = vd->vdev_spa;
1405789Sahrens 	space_map_obj_t *smo = &vd->vdev_dtl;
1406789Sahrens 	space_map_t *sm = &vd->vdev_dtl_map;
14071732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1408789Sahrens 	space_map_t smsync;
1409789Sahrens 	kmutex_t smlock;
1410789Sahrens 	dmu_buf_t *db;
1411789Sahrens 	dmu_tx_t *tx;
1412789Sahrens 
1413789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1414789Sahrens 
1415789Sahrens 	if (vd->vdev_detached) {
1416789Sahrens 		if (smo->smo_object != 0) {
14171732Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1418789Sahrens 			ASSERT3U(err, ==, 0);
1419789Sahrens 			smo->smo_object = 0;
1420789Sahrens 		}
1421789Sahrens 		dmu_tx_commit(tx);
1422789Sahrens 		return;
1423789Sahrens 	}
1424789Sahrens 
1425789Sahrens 	if (smo->smo_object == 0) {
1426789Sahrens 		ASSERT(smo->smo_objsize == 0);
1427789Sahrens 		ASSERT(smo->smo_alloc == 0);
14281732Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1429789Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1430789Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1431789Sahrens 		ASSERT(smo->smo_object != 0);
1432789Sahrens 		vdev_config_dirty(vd->vdev_top);
1433789Sahrens 	}
1434789Sahrens 
1435789Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1436789Sahrens 
1437789Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1438789Sahrens 	    &smlock);
1439789Sahrens 
1440789Sahrens 	mutex_enter(&smlock);
1441789Sahrens 
1442789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
14431732Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1444789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1445789Sahrens 
14461732Sbonwick 	space_map_truncate(smo, mos, tx);
14471732Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1448789Sahrens 
1449789Sahrens 	space_map_destroy(&smsync);
1450789Sahrens 
1451789Sahrens 	mutex_exit(&smlock);
1452789Sahrens 	mutex_destroy(&smlock);
1453789Sahrens 
14541732Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1455789Sahrens 	dmu_buf_will_dirty(db, tx);
14564944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
14574944Smaybee 	bcopy(smo, db->db_data, sizeof (*smo));
14581544Seschrock 	dmu_buf_rele(db, FTAG);
1459789Sahrens 
1460789Sahrens 	dmu_tx_commit(tx);
1461789Sahrens }
1462789Sahrens 
14637046Sahrens /*
14647046Sahrens  * Determine if resilver is needed, and if so the txg range.
14657046Sahrens  */
14667046Sahrens boolean_t
14677046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
14687046Sahrens {
14697046Sahrens 	boolean_t needed = B_FALSE;
14707046Sahrens 	uint64_t thismin = UINT64_MAX;
14717046Sahrens 	uint64_t thismax = 0;
14727046Sahrens 
14737046Sahrens 	if (vd->vdev_children == 0) {
14747046Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
14757046Sahrens 		if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd)) {
14767046Sahrens 			space_seg_t *ss;
14777046Sahrens 
14787046Sahrens 			ss = avl_first(&vd->vdev_dtl_map.sm_root);
14797046Sahrens 			thismin = ss->ss_start - 1;
14807046Sahrens 			ss = avl_last(&vd->vdev_dtl_map.sm_root);
14817046Sahrens 			thismax = ss->ss_end;
14827046Sahrens 			needed = B_TRUE;
14837046Sahrens 		}
14847046Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
14857046Sahrens 	} else {
14867046Sahrens 		int c;
14877046Sahrens 		for (c = 0; c < vd->vdev_children; c++) {
14887046Sahrens 			vdev_t *cvd = vd->vdev_child[c];
14897046Sahrens 			uint64_t cmin, cmax;
14907046Sahrens 
14917046Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
14927046Sahrens 				thismin = MIN(thismin, cmin);
14937046Sahrens 				thismax = MAX(thismax, cmax);
14947046Sahrens 				needed = B_TRUE;
14957046Sahrens 			}
14967046Sahrens 		}
14977046Sahrens 	}
14987046Sahrens 
14997046Sahrens 	if (needed && minp) {
15007046Sahrens 		*minp = thismin;
15017046Sahrens 		*maxp = thismax;
15027046Sahrens 	}
15037046Sahrens 	return (needed);
15047046Sahrens }
15057046Sahrens 
15061986Seschrock void
15071544Seschrock vdev_load(vdev_t *vd)
1508789Sahrens {
15091986Seschrock 	int c;
1510789Sahrens 
1511789Sahrens 	/*
1512789Sahrens 	 * Recursively load all children.
1513789Sahrens 	 */
1514789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
15151986Seschrock 		vdev_load(vd->vdev_child[c]);
1516789Sahrens 
1517789Sahrens 	/*
15181585Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1519789Sahrens 	 */
15201986Seschrock 	if (vd == vd->vdev_top &&
15211986Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
15221986Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
15231986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
15241986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1525789Sahrens 
1526789Sahrens 	/*
1527789Sahrens 	 * If this is a leaf vdev, load its DTL.
1528789Sahrens 	 */
15291986Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
15301986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
15311986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1532789Sahrens }
1533789Sahrens 
15342082Seschrock /*
15355450Sbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
15365450Sbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
15375450Sbrendan  * we make sure that we can open the underlying device, then try to read the
15385450Sbrendan  * label, and make sure that the label is sane and that it hasn't been
15395450Sbrendan  * repurposed to another pool.
15402082Seschrock  */
15412082Seschrock int
15425450Sbrendan vdev_validate_aux(vdev_t *vd)
15432082Seschrock {
15442082Seschrock 	nvlist_t *label;
15452082Seschrock 	uint64_t guid, version;
15462082Seschrock 	uint64_t state;
15472082Seschrock 
15487754SJeff.Bonwick@Sun.COM 	if (!vdev_readable(vd))
15496643Seschrock 		return (0);
15506643Seschrock 
15512082Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
15522082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
15532082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
15542082Seschrock 		return (-1);
15552082Seschrock 	}
15562082Seschrock 
15572082Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
15584577Sahrens 	    version > SPA_VERSION ||
15592082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
15602082Seschrock 	    guid != vd->vdev_guid ||
15612082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
15622082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
15632082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
15642082Seschrock 		nvlist_free(label);
15652082Seschrock 		return (-1);
15662082Seschrock 	}
15672082Seschrock 
15682082Seschrock 	/*
15692082Seschrock 	 * We don't actually check the pool state here.  If it's in fact in
15702082Seschrock 	 * use by another pool, we update this fact on the fly when requested.
15712082Seschrock 	 */
15722082Seschrock 	nvlist_free(label);
15732082Seschrock 	return (0);
15742082Seschrock }
15752082Seschrock 
1576789Sahrens void
1577789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
1578789Sahrens {
1579789Sahrens 	metaslab_t *msp;
1580789Sahrens 
1581789Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1582789Sahrens 		metaslab_sync_done(msp, txg);
1583789Sahrens }
1584789Sahrens 
1585789Sahrens void
1586789Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
1587789Sahrens {
1588789Sahrens 	spa_t *spa = vd->vdev_spa;
1589789Sahrens 	vdev_t *lvd;
1590789Sahrens 	metaslab_t *msp;
15911732Sbonwick 	dmu_tx_t *tx;
1592789Sahrens 
15931732Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
15941732Sbonwick 		ASSERT(vd == vd->vdev_top);
15951732Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
15961732Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
15971732Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
15981732Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
15991732Sbonwick 		vdev_config_dirty(vd);
16001732Sbonwick 		dmu_tx_commit(tx);
16011732Sbonwick 	}
1602789Sahrens 
16031732Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1604789Sahrens 		metaslab_sync(msp, txg);
16051732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
16061732Sbonwick 	}
1607789Sahrens 
1608789Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1609789Sahrens 		vdev_dtl_sync(lvd, txg);
1610789Sahrens 
1611789Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1612789Sahrens }
1613789Sahrens 
1614789Sahrens uint64_t
1615789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1616789Sahrens {
1617789Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1618789Sahrens }
1619789Sahrens 
16204451Seschrock /*
16214451Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
16224451Seschrock  * not be opened, and no I/O is attempted.
16234451Seschrock  */
1624789Sahrens int
16254451Seschrock vdev_fault(spa_t *spa, uint64_t guid)
16264451Seschrock {
16276643Seschrock 	vdev_t *vd;
16284451Seschrock 
16297754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
16304451Seschrock 
16316643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
16327754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
16337754SJeff.Bonwick@Sun.COM 
16344451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
16357754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
16364451Seschrock 
16374451Seschrock 	/*
16384451Seschrock 	 * Faulted state takes precedence over degraded.
16394451Seschrock 	 */
16404451Seschrock 	vd->vdev_faulted = 1ULL;
16414451Seschrock 	vd->vdev_degraded = 0ULL;
16427754SJeff.Bonwick@Sun.COM 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
16434451Seschrock 
16444451Seschrock 	/*
16457881SEric.Taylor@Sun.COM 	 * If marking the vdev as faulted causes the top-level vdev to become
16464451Seschrock 	 * unavailable, then back off and simply mark the vdev as degraded
16474451Seschrock 	 * instead.
16484451Seschrock 	 */
16496643Seschrock 	if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
16504451Seschrock 		vd->vdev_degraded = 1ULL;
16514451Seschrock 		vd->vdev_faulted = 0ULL;
16524451Seschrock 
16534451Seschrock 		/*
16544451Seschrock 		 * If we reopen the device and it's not dead, only then do we
16554451Seschrock 		 * mark it degraded.
16564451Seschrock 		 */
16574451Seschrock 		vdev_reopen(vd);
16584451Seschrock 
16595329Sgw25295 		if (vdev_readable(vd)) {
16604451Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
16614451Seschrock 			    VDEV_AUX_ERR_EXCEEDED);
16624451Seschrock 		}
16634451Seschrock 	}
16644451Seschrock 
16657754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
16664451Seschrock }
16674451Seschrock 
16684451Seschrock /*
16694451Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
16704451Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
16714451Seschrock  * as I/O is concerned.
16724451Seschrock  */
16734451Seschrock int
16744451Seschrock vdev_degrade(spa_t *spa, uint64_t guid)
16754451Seschrock {
16766643Seschrock 	vdev_t *vd;
16774451Seschrock 
16787754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
16794451Seschrock 
16806643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
16817754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
16827754SJeff.Bonwick@Sun.COM 
16834451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
16847754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
16854451Seschrock 
16864451Seschrock 	/*
16874451Seschrock 	 * If the vdev is already faulted, then don't do anything.
16884451Seschrock 	 */
16897754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded)
16907754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, 0));
16914451Seschrock 
16924451Seschrock 	vd->vdev_degraded = 1ULL;
16934451Seschrock 	if (!vdev_is_dead(vd))
16944451Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
16954451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
16964451Seschrock 
16977754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
16984451Seschrock }
16994451Seschrock 
17004451Seschrock /*
17014451Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
17024451Seschrock  * any attached spare device should be detached when the device finishes
17034451Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
17044451Seschrock  * so no FMA events are generated if the device fails to open.
17054451Seschrock  */
17064451Seschrock int
17077754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1708789Sahrens {
17096643Seschrock 	vdev_t *vd;
1710789Sahrens 
17117881SEric.Taylor@Sun.COM 	if (spa_is_root(spa)) {
17127881SEric.Taylor@Sun.COM 		/*
17137881SEric.Taylor@Sun.COM 		 * if we're trying to online a device that's part of
17147881SEric.Taylor@Sun.COM 		 * the root pool, trigger an attach (if any) with only
17157881SEric.Taylor@Sun.COM 		 * the SCL_STATE lock held in order to avoid a deadlock
17167881SEric.Taylor@Sun.COM 		 * where modload tries to read from the disk
17177881SEric.Taylor@Sun.COM 		 */
17187881SEric.Taylor@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_WRITER);
17197881SEric.Taylor@Sun.COM 		if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
17207881SEric.Taylor@Sun.COM 			spa_config_exit(spa, SCL_STATE, FTAG);
17217881SEric.Taylor@Sun.COM 			return (ENODEV);
17227881SEric.Taylor@Sun.COM 		}
17237881SEric.Taylor@Sun.COM 		if (!vd->vdev_ops->vdev_op_leaf) {
17247881SEric.Taylor@Sun.COM 			spa_config_exit(spa, SCL_STATE, FTAG);
17257881SEric.Taylor@Sun.COM 			return (ENOTSUP);
17267881SEric.Taylor@Sun.COM 		}
17277881SEric.Taylor@Sun.COM 		vdev_close(vd);
17287881SEric.Taylor@Sun.COM 		vdev_open(vd);
17297881SEric.Taylor@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
17307881SEric.Taylor@Sun.COM 	}
17317881SEric.Taylor@Sun.COM 
17327754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
17331485Slling 
17346643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
17357754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1736789Sahrens 
17371585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
17387754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
17391585Sbonwick 
1740789Sahrens 	vd->vdev_offline = B_FALSE;
17411485Slling 	vd->vdev_tmpoffline = B_FALSE;
17427754SJeff.Bonwick@Sun.COM 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
17437754SJeff.Bonwick@Sun.COM 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
17441544Seschrock 	vdev_reopen(vd->vdev_top);
17454451Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
17464451Seschrock 
17474451Seschrock 	if (newstate)
17484451Seschrock 		*newstate = vd->vdev_state;
17494451Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
17504451Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
17514451Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
17524451Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
17534451Seschrock 		vd->vdev_unspare = B_TRUE;
1754789Sahrens 
17557754SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, vd, 0);
1756789Sahrens 
17577046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
1758789Sahrens 
1759789Sahrens 	return (0);
1760789Sahrens }
1761789Sahrens 
1762789Sahrens int
17634451Seschrock vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1764789Sahrens {
17656643Seschrock 	vdev_t *vd;
1766789Sahrens 
17677754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
1768789Sahrens 
17696643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
17707754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1771789Sahrens 
17721585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
17737754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
17741585Sbonwick 
1775789Sahrens 	/*
17761732Sbonwick 	 * If the device isn't already offline, try to offline it.
1777789Sahrens 	 */
17781732Sbonwick 	if (!vd->vdev_offline) {
17791732Sbonwick 		/*
17801732Sbonwick 		 * If this device's top-level vdev has a non-empty DTL,
17811732Sbonwick 		 * don't allow the device to be offlined.
17821732Sbonwick 		 *
17831732Sbonwick 		 * XXX -- make this more precise by allowing the offline
17841732Sbonwick 		 * as long as the remaining devices don't have any DTL holes.
17851732Sbonwick 		 */
17861732Sbonwick 		if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
17877754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
1788789Sahrens 
17891732Sbonwick 		/*
17901732Sbonwick 		 * Offline this device and reopen its top-level vdev.
17911732Sbonwick 		 * If this action results in the top-level vdev becoming
17921732Sbonwick 		 * unusable, undo it and fail the request.
17931732Sbonwick 		 */
17941732Sbonwick 		vd->vdev_offline = B_TRUE;
17951544Seschrock 		vdev_reopen(vd->vdev_top);
17966643Seschrock 		if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
17971732Sbonwick 			vd->vdev_offline = B_FALSE;
17981732Sbonwick 			vdev_reopen(vd->vdev_top);
17997754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
18001732Sbonwick 		}
1801789Sahrens 	}
1802789Sahrens 
18037754SJeff.Bonwick@Sun.COM 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
18041732Sbonwick 
18057754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
1806789Sahrens }
1807789Sahrens 
18081544Seschrock /*
18091544Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
18101544Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
18111544Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
18121544Seschrock  */
18131544Seschrock void
18147754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd)
1815789Sahrens {
18167754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
18177754SJeff.Bonwick@Sun.COM 
18187754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1819789Sahrens 
18201544Seschrock 	if (vd == NULL)
18217754SJeff.Bonwick@Sun.COM 		vd = rvd;
1822789Sahrens 
18231544Seschrock 	vd->vdev_stat.vs_read_errors = 0;
18241544Seschrock 	vd->vdev_stat.vs_write_errors = 0;
18251544Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
1826789Sahrens 
18277754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
18287754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd->vdev_child[c]);
18294451Seschrock 
18304451Seschrock 	/*
18316959Sek110237 	 * If we're in the FAULTED state or have experienced failed I/O, then
18326959Sek110237 	 * clear the persistent state and attempt to reopen the device.  We
18336959Sek110237 	 * also mark the vdev config dirty, so that the new faulted state is
18346959Sek110237 	 * written out to disk.
18354451Seschrock 	 */
18367754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded ||
18377754SJeff.Bonwick@Sun.COM 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
18386959Sek110237 
18394451Seschrock 		vd->vdev_faulted = vd->vdev_degraded = 0;
18407754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read = B_FALSE;
18417754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write = B_FALSE;
18427754SJeff.Bonwick@Sun.COM 
18434451Seschrock 		vdev_reopen(vd);
18444451Seschrock 
18457754SJeff.Bonwick@Sun.COM 		if (vd != rvd)
18467754SJeff.Bonwick@Sun.COM 			vdev_state_dirty(vd->vdev_top);
18477754SJeff.Bonwick@Sun.COM 
18487754SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
18494808Sek110237 			spa_async_request(spa, SPA_ASYNC_RESILVER);
18504451Seschrock 
18514451Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
18524451Seschrock 	}
1853789Sahrens }
1854789Sahrens 
18557754SJeff.Bonwick@Sun.COM boolean_t
18567754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd)
18575329Sgw25295 {
18587754SJeff.Bonwick@Sun.COM 	return (vd->vdev_state < VDEV_STATE_DEGRADED);
18595329Sgw25295 }
18605329Sgw25295 
18617754SJeff.Bonwick@Sun.COM boolean_t
18627754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd)
1863789Sahrens {
18647754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
1865789Sahrens }
1866789Sahrens 
18677754SJeff.Bonwick@Sun.COM boolean_t
18687754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd)
1869789Sahrens {
18707754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
18717754SJeff.Bonwick@Sun.COM }
1872789Sahrens 
18737754SJeff.Bonwick@Sun.COM boolean_t
1874*7980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd)
1875*7980SGeorge.Wilson@Sun.COM {
1876*7980SGeorge.Wilson@Sun.COM 	/*
1877*7980SGeorge.Wilson@Sun.COM 	 * We currently allow allocations from vdevs which maybe in the
1878*7980SGeorge.Wilson@Sun.COM 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
1879*7980SGeorge.Wilson@Sun.COM 	 * fails to reopen then we'll catch it later when we're holding
1880*7980SGeorge.Wilson@Sun.COM 	 * the proper locks.
1881*7980SGeorge.Wilson@Sun.COM 	 */
1882*7980SGeorge.Wilson@Sun.COM 	return (!(vdev_is_dead(vd) && vd->vdev_state != VDEV_STATE_CLOSED) &&
1883*7980SGeorge.Wilson@Sun.COM 	    !vd->vdev_cant_write);
1884*7980SGeorge.Wilson@Sun.COM }
1885*7980SGeorge.Wilson@Sun.COM 
1886*7980SGeorge.Wilson@Sun.COM boolean_t
18877754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio)
18887754SJeff.Bonwick@Sun.COM {
18897754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_vd == vd);
1890789Sahrens 
18917754SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
18927754SJeff.Bonwick@Sun.COM 		return (B_FALSE);
1893789Sahrens 
18947754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ)
18957754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_read);
1896789Sahrens 
18977754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_WRITE)
18987754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_write);
18997754SJeff.Bonwick@Sun.COM 
19007754SJeff.Bonwick@Sun.COM 	return (B_TRUE);
1901789Sahrens }
1902789Sahrens 
1903789Sahrens /*
1904789Sahrens  * Get statistics for the given vdev.
1905789Sahrens  */
1906789Sahrens void
1907789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1908789Sahrens {
1909789Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1910789Sahrens 
1911789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
1912789Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
19137046Sahrens 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
1914789Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1915789Sahrens 	vs->vs_state = vd->vdev_state;
19161175Slling 	vs->vs_rsize = vdev_get_rsize(vd);
1917789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
1918789Sahrens 
1919789Sahrens 	/*
1920789Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
1921789Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
1922789Sahrens 	 */
1923789Sahrens 	if (vd == rvd) {
19247754SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
1925789Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
1926789Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
1927789Sahrens 
1928789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
19297754SJeff.Bonwick@Sun.COM 			for (int t = 0; t < ZIO_TYPES; t++) {
1930789Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
1931789Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
1932789Sahrens 			}
1933789Sahrens 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
1934789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
1935789Sahrens 		}
1936789Sahrens 	}
1937789Sahrens }
1938789Sahrens 
1939789Sahrens void
19405450Sbrendan vdev_clear_stats(vdev_t *vd)
19415450Sbrendan {
19425450Sbrendan 	mutex_enter(&vd->vdev_stat_lock);
19435450Sbrendan 	vd->vdev_stat.vs_space = 0;
19445450Sbrendan 	vd->vdev_stat.vs_dspace = 0;
19455450Sbrendan 	vd->vdev_stat.vs_alloc = 0;
19465450Sbrendan 	mutex_exit(&vd->vdev_stat_lock);
19475450Sbrendan }
19485450Sbrendan 
19495450Sbrendan void
19507754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize)
1951789Sahrens {
19527754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = zio->io_spa->spa_root_vdev;
19537754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
1954789Sahrens 	vdev_t *pvd;
1955789Sahrens 	uint64_t txg = zio->io_txg;
1956789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
1957789Sahrens 	zio_type_t type = zio->io_type;
1958789Sahrens 	int flags = zio->io_flags;
1959789Sahrens 
19607754SJeff.Bonwick@Sun.COM 	/*
19617754SJeff.Bonwick@Sun.COM 	 * If this i/o is a gang leader, it didn't do any actual work.
19627754SJeff.Bonwick@Sun.COM 	 */
19637754SJeff.Bonwick@Sun.COM 	if (zio->io_gang_tree)
19647754SJeff.Bonwick@Sun.COM 		return;
19657754SJeff.Bonwick@Sun.COM 
1966789Sahrens 	if (zio->io_error == 0) {
19677754SJeff.Bonwick@Sun.COM 		/*
19687754SJeff.Bonwick@Sun.COM 		 * If this is a root i/o, don't count it -- we've already
19697754SJeff.Bonwick@Sun.COM 		 * counted the top-level vdevs, and vdev_get_stats() will
19707754SJeff.Bonwick@Sun.COM 		 * aggregate them when asked.  This reduces contention on
19717754SJeff.Bonwick@Sun.COM 		 * the root vdev_stat_lock and implicitly handles blocks
19727754SJeff.Bonwick@Sun.COM 		 * that compress away to holes, for which there is no i/o.
19737754SJeff.Bonwick@Sun.COM 		 * (Holes never create vdev children, so all the counters
19747754SJeff.Bonwick@Sun.COM 		 * remain zero, which is what we want.)
19757754SJeff.Bonwick@Sun.COM 		 *
19767754SJeff.Bonwick@Sun.COM 		 * Note: this only applies to successful i/o (io_error == 0)
19777754SJeff.Bonwick@Sun.COM 		 * because unlike i/o counts, errors are not additive.
19787754SJeff.Bonwick@Sun.COM 		 * When reading a ditto block, for example, failure of
19797754SJeff.Bonwick@Sun.COM 		 * one top-level vdev does not imply a root-level error.
19807754SJeff.Bonwick@Sun.COM 		 */
19817754SJeff.Bonwick@Sun.COM 		if (vd == rvd)
19827754SJeff.Bonwick@Sun.COM 			return;
19837754SJeff.Bonwick@Sun.COM 
19847754SJeff.Bonwick@Sun.COM 		ASSERT(vd == zio->io_vd);
1985789Sahrens 		if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1986789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
1987789Sahrens 			vs->vs_ops[type]++;
19887754SJeff.Bonwick@Sun.COM 			vs->vs_bytes[type] += psize;
1989789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
1990789Sahrens 		}
19917754SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_REPAIR) {
19927754SJeff.Bonwick@Sun.COM 			ASSERT(zio->io_delegate_list == NULL);
1993789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
19941807Sbonwick 			if (flags & ZIO_FLAG_SCRUB_THREAD)
19957754SJeff.Bonwick@Sun.COM 				vs->vs_scrub_repaired += psize;
1996789Sahrens 			else
19977754SJeff.Bonwick@Sun.COM 				vs->vs_self_healed += psize;
1998789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
1999789Sahrens 		}
2000789Sahrens 		return;
2001789Sahrens 	}
2002789Sahrens 
2003789Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2004789Sahrens 		return;
2005789Sahrens 
20067754SJeff.Bonwick@Sun.COM 	mutex_enter(&vd->vdev_stat_lock);
20077754SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_READ) {
20087754SJeff.Bonwick@Sun.COM 		if (zio->io_error == ECKSUM)
20097754SJeff.Bonwick@Sun.COM 			vs->vs_checksum_errors++;
20107754SJeff.Bonwick@Sun.COM 		else
20117754SJeff.Bonwick@Sun.COM 			vs->vs_read_errors++;
2012789Sahrens 	}
20137754SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_WRITE)
20147754SJeff.Bonwick@Sun.COM 		vs->vs_write_errors++;
20157754SJeff.Bonwick@Sun.COM 	mutex_exit(&vd->vdev_stat_lock);
2016789Sahrens 
20177754SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_WRITE && txg != 0 && vd->vdev_children == 0) {
20181807Sbonwick 		if (flags & ZIO_FLAG_SCRUB_THREAD) {
2019789Sahrens 			ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2020789Sahrens 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
2021789Sahrens 				vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
2022789Sahrens 		}
2023789Sahrens 		if (!(flags & ZIO_FLAG_IO_REPAIR)) {
2024789Sahrens 			if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
2025789Sahrens 				return;
20261732Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2027789Sahrens 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
2028789Sahrens 				vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
2029789Sahrens 		}
2030789Sahrens 	}
2031789Sahrens }
2032789Sahrens 
2033789Sahrens void
2034789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2035789Sahrens {
2036789Sahrens 	int c;
2037789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2038789Sahrens 
2039789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
2040789Sahrens 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2041789Sahrens 
2042789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2043789Sahrens 
2044789Sahrens 	if (type == POOL_SCRUB_NONE) {
2045789Sahrens 		/*
2046789Sahrens 		 * Update completion and end time.  Leave everything else alone
2047789Sahrens 		 * so we can report what happened during the previous scrub.
2048789Sahrens 		 */
2049789Sahrens 		vs->vs_scrub_complete = complete;
2050789Sahrens 		vs->vs_scrub_end = gethrestime_sec();
2051789Sahrens 	} else {
2052789Sahrens 		vs->vs_scrub_type = type;
2053789Sahrens 		vs->vs_scrub_complete = 0;
2054789Sahrens 		vs->vs_scrub_examined = 0;
2055789Sahrens 		vs->vs_scrub_repaired = 0;
2056789Sahrens 		vs->vs_scrub_start = gethrestime_sec();
2057789Sahrens 		vs->vs_scrub_end = 0;
2058789Sahrens 	}
2059789Sahrens 
2060789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2061789Sahrens }
2062789Sahrens 
2063789Sahrens /*
2064789Sahrens  * Update the in-core space usage stats for this vdev and the root vdev.
2065789Sahrens  */
2066789Sahrens void
20675450Sbrendan vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
20685450Sbrendan     boolean_t update_root)
2069789Sahrens {
20704527Sperrin 	int64_t dspace_delta = space_delta;
20714527Sperrin 	spa_t *spa = vd->vdev_spa;
20724527Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
20734527Sperrin 
2074789Sahrens 	ASSERT(vd == vd->vdev_top);
20754527Sperrin 
20764527Sperrin 	/*
20774527Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
20784527Sperrin 	 * factor.  We must calculate this here and not at the root vdev
20794527Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
20804527Sperrin 	 * childrens', thus not accurate enough for us.
20814527Sperrin 	 */
20824527Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
20834527Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
20844527Sperrin 	    vd->vdev_deflate_ratio;
2085789Sahrens 
20864527Sperrin 	mutex_enter(&vd->vdev_stat_lock);
20874527Sperrin 	vd->vdev_stat.vs_space += space_delta;
20884527Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
20894527Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
20904527Sperrin 	mutex_exit(&vd->vdev_stat_lock);
20912082Seschrock 
20925450Sbrendan 	if (update_root) {
20935450Sbrendan 		ASSERT(rvd == vd->vdev_parent);
20945450Sbrendan 		ASSERT(vd->vdev_ms_count != 0);
20954527Sperrin 
20965450Sbrendan 		/*
20975450Sbrendan 		 * Don't count non-normal (e.g. intent log) space as part of
20985450Sbrendan 		 * the pool's capacity.
20995450Sbrendan 		 */
21005450Sbrendan 		if (vd->vdev_mg->mg_class != spa->spa_normal_class)
21015450Sbrendan 			return;
21025450Sbrendan 
21035450Sbrendan 		mutex_enter(&rvd->vdev_stat_lock);
21045450Sbrendan 		rvd->vdev_stat.vs_space += space_delta;
21055450Sbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
21065450Sbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
21075450Sbrendan 		mutex_exit(&rvd->vdev_stat_lock);
21085450Sbrendan 	}
2109789Sahrens }
2110789Sahrens 
2111789Sahrens /*
2112789Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2113789Sahrens  * so that it will be written out next time the vdev configuration is synced.
2114789Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2115789Sahrens  */
2116789Sahrens void
2117789Sahrens vdev_config_dirty(vdev_t *vd)
2118789Sahrens {
2119789Sahrens 	spa_t *spa = vd->vdev_spa;
2120789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2121789Sahrens 	int c;
2122789Sahrens 
21231601Sbonwick 	/*
21246643Seschrock 	 * If this is an aux vdev (as with l2cache devices), then we update the
21256643Seschrock 	 * vdev config manually and set the sync flag.
21266643Seschrock 	 */
21276643Seschrock 	if (vd->vdev_aux != NULL) {
21286643Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
21296643Seschrock 		nvlist_t **aux;
21306643Seschrock 		uint_t naux;
21316643Seschrock 
21326643Seschrock 		for (c = 0; c < sav->sav_count; c++) {
21336643Seschrock 			if (sav->sav_vdevs[c] == vd)
21346643Seschrock 				break;
21356643Seschrock 		}
21366643Seschrock 
21377754SJeff.Bonwick@Sun.COM 		if (c == sav->sav_count) {
21387754SJeff.Bonwick@Sun.COM 			/*
21397754SJeff.Bonwick@Sun.COM 			 * We're being removed.  There's nothing more to do.
21407754SJeff.Bonwick@Sun.COM 			 */
21417754SJeff.Bonwick@Sun.COM 			ASSERT(sav->sav_sync == B_TRUE);
21427754SJeff.Bonwick@Sun.COM 			return;
21437754SJeff.Bonwick@Sun.COM 		}
21447754SJeff.Bonwick@Sun.COM 
21456643Seschrock 		sav->sav_sync = B_TRUE;
21466643Seschrock 
21476643Seschrock 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
21486643Seschrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) == 0);
21496643Seschrock 
21506643Seschrock 		ASSERT(c < naux);
21516643Seschrock 
21526643Seschrock 		/*
21536643Seschrock 		 * Setting the nvlist in the middle if the array is a little
21546643Seschrock 		 * sketchy, but it will work.
21556643Seschrock 		 */
21566643Seschrock 		nvlist_free(aux[c]);
21576643Seschrock 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
21586643Seschrock 
21596643Seschrock 		return;
21606643Seschrock 	}
21616643Seschrock 
21626643Seschrock 	/*
21637754SJeff.Bonwick@Sun.COM 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
21647754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
21657754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
21661601Sbonwick 	 * so this is sufficient to ensure mutual exclusion.
21671601Sbonwick 	 */
21687754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
21697754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
21707754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
21711601Sbonwick 
2172789Sahrens 	if (vd == rvd) {
2173789Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2174789Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2175789Sahrens 	} else {
2176789Sahrens 		ASSERT(vd == vd->vdev_top);
2177789Sahrens 
21787754SJeff.Bonwick@Sun.COM 		if (!list_link_active(&vd->vdev_config_dirty_node))
21797754SJeff.Bonwick@Sun.COM 			list_insert_head(&spa->spa_config_dirty_list, vd);
2180789Sahrens 	}
2181789Sahrens }
2182789Sahrens 
2183789Sahrens void
2184789Sahrens vdev_config_clean(vdev_t *vd)
2185789Sahrens {
21861601Sbonwick 	spa_t *spa = vd->vdev_spa;
21871601Sbonwick 
21887754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
21897754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
21907754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
21917754SJeff.Bonwick@Sun.COM 
21927754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
21937754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_config_dirty_list, vd);
21947754SJeff.Bonwick@Sun.COM }
21957754SJeff.Bonwick@Sun.COM 
21967754SJeff.Bonwick@Sun.COM /*
21977754SJeff.Bonwick@Sun.COM  * Mark a top-level vdev's state as dirty, so that the next pass of
21987754SJeff.Bonwick@Sun.COM  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
21997754SJeff.Bonwick@Sun.COM  * the state changes from larger config changes because they require
22007754SJeff.Bonwick@Sun.COM  * much less locking, and are often needed for administrative actions.
22017754SJeff.Bonwick@Sun.COM  */
22027754SJeff.Bonwick@Sun.COM void
22037754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd)
22047754SJeff.Bonwick@Sun.COM {
22057754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
22067754SJeff.Bonwick@Sun.COM 
22077754SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
22081601Sbonwick 
22097754SJeff.Bonwick@Sun.COM 	/*
22107754SJeff.Bonwick@Sun.COM 	 * The state list is protected by the SCL_STATE lock.  The caller
22117754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_STATE as writer, or must be the sync thread
22127754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
22137754SJeff.Bonwick@Sun.COM 	 * so this is sufficient to ensure mutual exclusion.
22147754SJeff.Bonwick@Sun.COM 	 */
22157754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
22167754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
22177754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
22187754SJeff.Bonwick@Sun.COM 
22197754SJeff.Bonwick@Sun.COM 	if (!list_link_active(&vd->vdev_state_dirty_node))
22207754SJeff.Bonwick@Sun.COM 		list_insert_head(&spa->spa_state_dirty_list, vd);
22217754SJeff.Bonwick@Sun.COM }
22227754SJeff.Bonwick@Sun.COM 
22237754SJeff.Bonwick@Sun.COM void
22247754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd)
22257754SJeff.Bonwick@Sun.COM {
22267754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
22277754SJeff.Bonwick@Sun.COM 
22287754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
22297754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
22307754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
22317754SJeff.Bonwick@Sun.COM 
22327754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
22337754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_state_dirty_list, vd);
2234789Sahrens }
2235789Sahrens 
22366523Sek110237 /*
22376523Sek110237  * Propagate vdev state up from children to parent.
22386523Sek110237  */
22391775Sbillm void
22401775Sbillm vdev_propagate_state(vdev_t *vd)
22411775Sbillm {
22421775Sbillm 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
22431775Sbillm 	int degraded = 0, faulted = 0;
22441775Sbillm 	int corrupted = 0;
22451775Sbillm 	int c;
22461775Sbillm 	vdev_t *child;
22471775Sbillm 
22484451Seschrock 	if (vd->vdev_children > 0) {
22494451Seschrock 		for (c = 0; c < vd->vdev_children; c++) {
22504451Seschrock 			child = vd->vdev_child[c];
22516976Seschrock 
22527754SJeff.Bonwick@Sun.COM 			if (!vdev_readable(child) ||
22537754SJeff.Bonwick@Sun.COM 			    (!vdev_writeable(child) && (spa_mode & FWRITE))) {
22546976Seschrock 				/*
22556976Seschrock 				 * Root special: if there is a top-level log
22566976Seschrock 				 * device, treat the root vdev as if it were
22576976Seschrock 				 * degraded.
22586976Seschrock 				 */
22596976Seschrock 				if (child->vdev_islog && vd == rvd)
22606976Seschrock 					degraded++;
22616976Seschrock 				else
22626976Seschrock 					faulted++;
22636976Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
22644451Seschrock 				degraded++;
22656976Seschrock 			}
22664451Seschrock 
22674451Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
22684451Seschrock 				corrupted++;
22694451Seschrock 		}
22701775Sbillm 
22714451Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
22724451Seschrock 
22734451Seschrock 		/*
22747754SJeff.Bonwick@Sun.COM 		 * Root special: if there is a top-level vdev that cannot be
22754451Seschrock 		 * opened due to corrupted metadata, then propagate the root
22764451Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
22774451Seschrock 		 * replicas'.
22784451Seschrock 		 */
22794451Seschrock 		if (corrupted && vd == rvd &&
22804451Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
22814451Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
22824451Seschrock 			    VDEV_AUX_CORRUPT_DATA);
22831775Sbillm 	}
22841775Sbillm 
22856976Seschrock 	if (vd->vdev_parent)
22864451Seschrock 		vdev_propagate_state(vd->vdev_parent);
22871775Sbillm }
22881775Sbillm 
2289789Sahrens /*
22901544Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
22911544Seschrock  * state, because we're in the process of opening children depth-first.
22921544Seschrock  * Otherwise, we propagate the change to the parent.
22931544Seschrock  *
22941544Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
22951544Seschrock  * generated.
2296789Sahrens  */
2297789Sahrens void
22981544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2299789Sahrens {
23001986Seschrock 	uint64_t save_state;
23016643Seschrock 	spa_t *spa = vd->vdev_spa;
23021544Seschrock 
23031544Seschrock 	if (state == vd->vdev_state) {
23041544Seschrock 		vd->vdev_stat.vs_aux = aux;
2305789Sahrens 		return;
23061544Seschrock 	}
23071544Seschrock 
23081986Seschrock 	save_state = vd->vdev_state;
2309789Sahrens 
2310789Sahrens 	vd->vdev_state = state;
2311789Sahrens 	vd->vdev_stat.vs_aux = aux;
2312789Sahrens 
23134451Seschrock 	/*
23144451Seschrock 	 * If we are setting the vdev state to anything but an open state, then
23154451Seschrock 	 * always close the underlying device.  Otherwise, we keep accessible
23164451Seschrock 	 * but invalid devices open forever.  We don't call vdev_close() itself,
23174451Seschrock 	 * because that implies some extra checks (offline, etc) that we don't
23184451Seschrock 	 * want here.  This is limited to leaf devices, because otherwise
23194451Seschrock 	 * closing the device will affect other children.
23204451Seschrock 	 */
23217780SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
23224451Seschrock 		vd->vdev_ops->vdev_op_close(vd);
23234451Seschrock 
23244451Seschrock 	if (vd->vdev_removed &&
23254451Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
23264451Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
23274451Seschrock 		/*
23284451Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
23294451Seschrock 		 * device was previously marked removed and someone attempted to
23304451Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
23314451Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
23324451Seschrock 		 * it is one of our special test online cases, which is only
23334451Seschrock 		 * attempting to online the device and shouldn't generate an FMA
23344451Seschrock 		 * fault.
23354451Seschrock 		 */
23364451Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
23374451Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
23384451Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
23394451Seschrock 		/*
23404451Seschrock 		 * Indicate to the ZFS DE that this device has been removed, and
23414451Seschrock 		 * any recent errors should be ignored.
23424451Seschrock 		 */
23436643Seschrock 		zfs_post_remove(spa, vd);
23444451Seschrock 		vd->vdev_removed = B_TRUE;
23454451Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
23461544Seschrock 		/*
23471544Seschrock 		 * If we fail to open a vdev during an import, we mark it as
23481544Seschrock 		 * "not available", which signifies that it was never there to
23491544Seschrock 		 * begin with.  Failure to open such a device is not considered
23501544Seschrock 		 * an error.
23511544Seschrock 		 */
23526643Seschrock 		if (spa->spa_load_state == SPA_LOAD_IMPORT &&
23536643Seschrock 		    !spa->spa_import_faulted &&
23541986Seschrock 		    vd->vdev_ops->vdev_op_leaf)
23551986Seschrock 			vd->vdev_not_present = 1;
23561986Seschrock 
23571986Seschrock 		/*
23581986Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
23591986Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
23601986Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
23611986Seschrock 		 * want to post the ereport if the device was already in the
23621986Seschrock 		 * CANT_OPEN state beforehand.
23634451Seschrock 		 *
23644451Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
23654451Seschrock 		 * online the device in response to an insertion event.  If we
23664451Seschrock 		 * hit this case, then we have detected an insertion event for a
23674451Seschrock 		 * faulted or offline device that wasn't in the removed state.
23684451Seschrock 		 * In this scenario, we don't post an ereport because we are
23694451Seschrock 		 * about to replace the device, or attempt an online with
23704451Seschrock 		 * vdev_forcefault, which will generate the fault for us.
23711986Seschrock 		 */
23724451Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
23734451Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
23746643Seschrock 		    vd != spa->spa_root_vdev) {
23751544Seschrock 			const char *class;
23761544Seschrock 
23771544Seschrock 			switch (aux) {
23781544Seschrock 			case VDEV_AUX_OPEN_FAILED:
23791544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
23801544Seschrock 				break;
23811544Seschrock 			case VDEV_AUX_CORRUPT_DATA:
23821544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
23831544Seschrock 				break;
23841544Seschrock 			case VDEV_AUX_NO_REPLICAS:
23851544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
23861544Seschrock 				break;
23871544Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
23881544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
23891544Seschrock 				break;
23901544Seschrock 			case VDEV_AUX_TOO_SMALL:
23911544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
23921544Seschrock 				break;
23931544Seschrock 			case VDEV_AUX_BAD_LABEL:
23941544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
23951544Seschrock 				break;
23967754SJeff.Bonwick@Sun.COM 			case VDEV_AUX_IO_FAILURE:
23977754SJeff.Bonwick@Sun.COM 				class = FM_EREPORT_ZFS_IO_FAILURE;
23987754SJeff.Bonwick@Sun.COM 				break;
23991544Seschrock 			default:
24001544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
24011544Seschrock 			}
24021544Seschrock 
24036643Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
24041544Seschrock 		}
24054451Seschrock 
24064451Seschrock 		/* Erase any notion of persistent removed state */
24074451Seschrock 		vd->vdev_removed = B_FALSE;
24084451Seschrock 	} else {
24094451Seschrock 		vd->vdev_removed = B_FALSE;
24101544Seschrock 	}
24111544Seschrock 
24124451Seschrock 	if (!isopen)
24134451Seschrock 		vdev_propagate_state(vd);
2414789Sahrens }
24157042Sgw25295 
24167042Sgw25295 /*
24177042Sgw25295  * Check the vdev configuration to ensure that it's capable of supporting
24187042Sgw25295  * a root pool. Currently, we do not support RAID-Z or partial configuration.
24197042Sgw25295  * In addition, only a single top-level vdev is allowed and none of the leaves
24207042Sgw25295  * can be wholedisks.
24217042Sgw25295  */
24227042Sgw25295 boolean_t
24237042Sgw25295 vdev_is_bootable(vdev_t *vd)
24247042Sgw25295 {
24257042Sgw25295 	int c;
24267042Sgw25295 
24277042Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf) {
24287042Sgw25295 		char *vdev_type = vd->vdev_ops->vdev_op_type;
24297042Sgw25295 
24307042Sgw25295 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
24317042Sgw25295 		    vd->vdev_children > 1) {
24327042Sgw25295 			return (B_FALSE);
24337042Sgw25295 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
24347042Sgw25295 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
24357042Sgw25295 			return (B_FALSE);
24367042Sgw25295 		}
24377042Sgw25295 	} else if (vd->vdev_wholedisk == 1) {
24387042Sgw25295 		return (B_FALSE);
24397042Sgw25295 	}
24407042Sgw25295 
24417042Sgw25295 	for (c = 0; c < vd->vdev_children; c++) {
24427042Sgw25295 		if (!vdev_is_bootable(vd->vdev_child[c]))
24437042Sgw25295 			return (B_FALSE);
24447042Sgw25295 	}
24457042Sgw25295 	return (B_TRUE);
24467042Sgw25295 }
2447