xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 10817)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
238632SBill.Moore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <sys/zfs_context.h>
281544Seschrock #include <sys/fm/fs/zfs.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/spa_impl.h>
31789Sahrens #include <sys/dmu.h>
32789Sahrens #include <sys/dmu_tx.h>
33789Sahrens #include <sys/vdev_impl.h>
34789Sahrens #include <sys/uberblock_impl.h>
35789Sahrens #include <sys/metaslab.h>
36789Sahrens #include <sys/metaslab_impl.h>
37789Sahrens #include <sys/space_map.h>
38789Sahrens #include <sys/zio.h>
39789Sahrens #include <sys/zap.h>
40789Sahrens #include <sys/fs/zfs.h>
416643Seschrock #include <sys/arc.h>
429701SGeorge.Wilson@Sun.COM #include <sys/zil.h>
43789Sahrens 
44789Sahrens /*
45789Sahrens  * Virtual device management.
46789Sahrens  */
47789Sahrens 
48789Sahrens static vdev_ops_t *vdev_ops_table[] = {
49789Sahrens 	&vdev_root_ops,
50789Sahrens 	&vdev_raidz_ops,
51789Sahrens 	&vdev_mirror_ops,
52789Sahrens 	&vdev_replacing_ops,
532082Seschrock 	&vdev_spare_ops,
54789Sahrens 	&vdev_disk_ops,
55789Sahrens 	&vdev_file_ops,
56789Sahrens 	&vdev_missing_ops,
5710594SGeorge.Wilson@Sun.COM 	&vdev_hole_ops,
58789Sahrens 	NULL
59789Sahrens };
60789Sahrens 
617046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
627046Sahrens int zfs_scrub_limit = 10;
633697Smishra 
64789Sahrens /*
65789Sahrens  * Given a vdev type, return the appropriate ops vector.
66789Sahrens  */
67789Sahrens static vdev_ops_t *
68789Sahrens vdev_getops(const char *type)
69789Sahrens {
70789Sahrens 	vdev_ops_t *ops, **opspp;
71789Sahrens 
72789Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
73789Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
74789Sahrens 			break;
75789Sahrens 
76789Sahrens 	return (ops);
77789Sahrens }
78789Sahrens 
79789Sahrens /*
80789Sahrens  * Default asize function: return the MAX of psize with the asize of
81789Sahrens  * all children.  This is what's used by anything other than RAID-Z.
82789Sahrens  */
83789Sahrens uint64_t
84789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
85789Sahrens {
861732Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
87789Sahrens 	uint64_t csize;
889816SGeorge.Wilson@Sun.COM 
899816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
90789Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
91789Sahrens 		asize = MAX(asize, csize);
92789Sahrens 	}
93789Sahrens 
94789Sahrens 	return (asize);
95789Sahrens }
96789Sahrens 
971175Slling /*
989816SGeorge.Wilson@Sun.COM  * Get the minimum allocatable size. We define the allocatable size as
999816SGeorge.Wilson@Sun.COM  * the vdev's asize rounded to the nearest metaslab. This allows us to
1009816SGeorge.Wilson@Sun.COM  * replace or attach devices which don't have the same physical size but
1019816SGeorge.Wilson@Sun.COM  * can still satisfy the same number of allocations.
1021175Slling  */
1031175Slling uint64_t
1049816SGeorge.Wilson@Sun.COM vdev_get_min_asize(vdev_t *vd)
1051175Slling {
1069816SGeorge.Wilson@Sun.COM 	vdev_t *pvd = vd->vdev_parent;
1079816SGeorge.Wilson@Sun.COM 
1089816SGeorge.Wilson@Sun.COM 	/*
1099816SGeorge.Wilson@Sun.COM 	 * The our parent is NULL (inactive spare or cache) or is the root,
1109816SGeorge.Wilson@Sun.COM 	 * just return our own asize.
1119816SGeorge.Wilson@Sun.COM 	 */
1129816SGeorge.Wilson@Sun.COM 	if (pvd == NULL)
1139816SGeorge.Wilson@Sun.COM 		return (vd->vdev_asize);
1141175Slling 
1151175Slling 	/*
1169816SGeorge.Wilson@Sun.COM 	 * The top-level vdev just returns the allocatable size rounded
1179816SGeorge.Wilson@Sun.COM 	 * to the nearest metaslab.
1189816SGeorge.Wilson@Sun.COM 	 */
1199816SGeorge.Wilson@Sun.COM 	if (vd == vd->vdev_top)
1209816SGeorge.Wilson@Sun.COM 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
1219816SGeorge.Wilson@Sun.COM 
1229816SGeorge.Wilson@Sun.COM 	/*
1239816SGeorge.Wilson@Sun.COM 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
1249816SGeorge.Wilson@Sun.COM 	 * so each child must provide at least 1/Nth of its asize.
1251175Slling 	 */
1269816SGeorge.Wilson@Sun.COM 	if (pvd->vdev_ops == &vdev_raidz_ops)
1279816SGeorge.Wilson@Sun.COM 		return (pvd->vdev_min_asize / pvd->vdev_children);
1289816SGeorge.Wilson@Sun.COM 
1299816SGeorge.Wilson@Sun.COM 	return (pvd->vdev_min_asize);
1309816SGeorge.Wilson@Sun.COM }
1319816SGeorge.Wilson@Sun.COM 
1329816SGeorge.Wilson@Sun.COM void
1339816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vdev_t *vd)
1349816SGeorge.Wilson@Sun.COM {
1359816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1369816SGeorge.Wilson@Sun.COM 
1379816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
1389816SGeorge.Wilson@Sun.COM 		vdev_set_min_asize(vd->vdev_child[c]);
1391175Slling }
1401175Slling 
141789Sahrens vdev_t *
142789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
143789Sahrens {
144789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
145789Sahrens 
1467754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1475530Sbonwick 
1487046Sahrens 	if (vdev < rvd->vdev_children) {
1497046Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
150789Sahrens 		return (rvd->vdev_child[vdev]);
1517046Sahrens 	}
152789Sahrens 
153789Sahrens 	return (NULL);
154789Sahrens }
155789Sahrens 
156789Sahrens vdev_t *
157789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
158789Sahrens {
159789Sahrens 	vdev_t *mvd;
160789Sahrens 
1611585Sbonwick 	if (vd->vdev_guid == guid)
162789Sahrens 		return (vd);
163789Sahrens 
1649816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
165789Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
166789Sahrens 		    NULL)
167789Sahrens 			return (mvd);
168789Sahrens 
169789Sahrens 	return (NULL);
170789Sahrens }
171789Sahrens 
172789Sahrens void
173789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
174789Sahrens {
175789Sahrens 	size_t oldsize, newsize;
176789Sahrens 	uint64_t id = cvd->vdev_id;
177789Sahrens 	vdev_t **newchild;
178789Sahrens 
1797754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
180789Sahrens 	ASSERT(cvd->vdev_parent == NULL);
181789Sahrens 
182789Sahrens 	cvd->vdev_parent = pvd;
183789Sahrens 
184789Sahrens 	if (pvd == NULL)
185789Sahrens 		return;
186789Sahrens 
187789Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
188789Sahrens 
189789Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
190789Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
191789Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
192789Sahrens 
193789Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
194789Sahrens 	if (pvd->vdev_child != NULL) {
195789Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
196789Sahrens 		kmem_free(pvd->vdev_child, oldsize);
197789Sahrens 	}
198789Sahrens 
199789Sahrens 	pvd->vdev_child = newchild;
200789Sahrens 	pvd->vdev_child[id] = cvd;
201789Sahrens 
202789Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
203789Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
204789Sahrens 
205789Sahrens 	/*
206789Sahrens 	 * Walk up all ancestors to update guid sum.
207789Sahrens 	 */
208789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
209789Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
2103697Smishra 
2113697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2123697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
213789Sahrens }
214789Sahrens 
215789Sahrens void
216789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
217789Sahrens {
218789Sahrens 	int c;
219789Sahrens 	uint_t id = cvd->vdev_id;
220789Sahrens 
221789Sahrens 	ASSERT(cvd->vdev_parent == pvd);
222789Sahrens 
223789Sahrens 	if (pvd == NULL)
224789Sahrens 		return;
225789Sahrens 
226789Sahrens 	ASSERT(id < pvd->vdev_children);
227789Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
228789Sahrens 
229789Sahrens 	pvd->vdev_child[id] = NULL;
230789Sahrens 	cvd->vdev_parent = NULL;
231789Sahrens 
232789Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
233789Sahrens 		if (pvd->vdev_child[c])
234789Sahrens 			break;
235789Sahrens 
236789Sahrens 	if (c == pvd->vdev_children) {
237789Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
238789Sahrens 		pvd->vdev_child = NULL;
239789Sahrens 		pvd->vdev_children = 0;
240789Sahrens 	}
241789Sahrens 
242789Sahrens 	/*
243789Sahrens 	 * Walk up all ancestors to update guid sum.
244789Sahrens 	 */
245789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
246789Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
2473697Smishra 
2483697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2493697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
250789Sahrens }
251789Sahrens 
252789Sahrens /*
253789Sahrens  * Remove any holes in the child array.
254789Sahrens  */
255789Sahrens void
256789Sahrens vdev_compact_children(vdev_t *pvd)
257789Sahrens {
258789Sahrens 	vdev_t **newchild, *cvd;
259789Sahrens 	int oldc = pvd->vdev_children;
2609816SGeorge.Wilson@Sun.COM 	int newc;
261789Sahrens 
2627754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
263789Sahrens 
2649816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++)
265789Sahrens 		if (pvd->vdev_child[c])
266789Sahrens 			newc++;
267789Sahrens 
268789Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
269789Sahrens 
2709816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++) {
271789Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
272789Sahrens 			newchild[newc] = cvd;
273789Sahrens 			cvd->vdev_id = newc++;
274789Sahrens 		}
275789Sahrens 	}
276789Sahrens 
277789Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
278789Sahrens 	pvd->vdev_child = newchild;
279789Sahrens 	pvd->vdev_children = newc;
280789Sahrens }
281789Sahrens 
282789Sahrens /*
283789Sahrens  * Allocate and minimally initialize a vdev_t.
284789Sahrens  */
28510594SGeorge.Wilson@Sun.COM vdev_t *
286789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
287789Sahrens {
288789Sahrens 	vdev_t *vd;
289789Sahrens 
2901585Sbonwick 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
2911585Sbonwick 
2921585Sbonwick 	if (spa->spa_root_vdev == NULL) {
2931585Sbonwick 		ASSERT(ops == &vdev_root_ops);
2941585Sbonwick 		spa->spa_root_vdev = vd;
2951585Sbonwick 	}
296789Sahrens 
29710594SGeorge.Wilson@Sun.COM 	if (guid == 0 && ops != &vdev_hole_ops) {
2981585Sbonwick 		if (spa->spa_root_vdev == vd) {
2991585Sbonwick 			/*
3001585Sbonwick 			 * The root vdev's guid will also be the pool guid,
3011585Sbonwick 			 * which must be unique among all pools.
3021585Sbonwick 			 */
3031585Sbonwick 			while (guid == 0 || spa_guid_exists(guid, 0))
3041585Sbonwick 				guid = spa_get_random(-1ULL);
3051585Sbonwick 		} else {
3061585Sbonwick 			/*
3071585Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3081585Sbonwick 			 */
3091585Sbonwick 			while (guid == 0 ||
3101585Sbonwick 			    spa_guid_exists(spa_guid(spa), guid))
3111585Sbonwick 				guid = spa_get_random(-1ULL);
3121585Sbonwick 		}
3131585Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3141585Sbonwick 	}
315789Sahrens 
316789Sahrens 	vd->vdev_spa = spa;
317789Sahrens 	vd->vdev_id = id;
318789Sahrens 	vd->vdev_guid = guid;
319789Sahrens 	vd->vdev_guid_sum = guid;
320789Sahrens 	vd->vdev_ops = ops;
321789Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
32210594SGeorge.Wilson@Sun.COM 	vd->vdev_ishole = (ops == &vdev_hole_ops);
323789Sahrens 
324789Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3252856Snd150628 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
3267754SJeff.Bonwick@Sun.COM 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3278241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
3288241SJeff.Bonwick@Sun.COM 		space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
3298241SJeff.Bonwick@Sun.COM 		    &vd->vdev_dtl_lock);
3308241SJeff.Bonwick@Sun.COM 	}
331789Sahrens 	txg_list_create(&vd->vdev_ms_list,
332789Sahrens 	    offsetof(struct metaslab, ms_txg_node));
333789Sahrens 	txg_list_create(&vd->vdev_dtl_list,
334789Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
335789Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3364451Seschrock 	vdev_queue_init(vd);
3374451Seschrock 	vdev_cache_init(vd);
338789Sahrens 
339789Sahrens 	return (vd);
340789Sahrens }
341789Sahrens 
342789Sahrens /*
343789Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
344789Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
345789Sahrens  * different for each case.
346789Sahrens  */
3472082Seschrock int
3482082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
3492082Seschrock     int alloctype)
350789Sahrens {
351789Sahrens 	vdev_ops_t *ops;
352789Sahrens 	char *type;
3534527Sperrin 	uint64_t guid = 0, islog, nparity;
354789Sahrens 	vdev_t *vd;
355789Sahrens 
3567754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
357789Sahrens 
358789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
3592082Seschrock 		return (EINVAL);
360789Sahrens 
361789Sahrens 	if ((ops = vdev_getops(type)) == NULL)
3622082Seschrock 		return (EINVAL);
363789Sahrens 
364789Sahrens 	/*
365789Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
366789Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
367789Sahrens 	 */
368789Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
369789Sahrens 		uint64_t label_id;
370789Sahrens 
371789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
372789Sahrens 		    label_id != id)
3732082Seschrock 			return (EINVAL);
374789Sahrens 
375789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3762082Seschrock 			return (EINVAL);
3772082Seschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
3782082Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3792082Seschrock 			return (EINVAL);
3805450Sbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
3815450Sbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3825450Sbrendan 			return (EINVAL);
3839790SLin.Ling@Sun.COM 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
3849790SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3859790SLin.Ling@Sun.COM 			return (EINVAL);
386789Sahrens 	}
387789Sahrens 
3882082Seschrock 	/*
3892082Seschrock 	 * The first allocated vdev must be of type 'root'.
3902082Seschrock 	 */
3912082Seschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
3922082Seschrock 		return (EINVAL);
3932082Seschrock 
3944527Sperrin 	/*
3954527Sperrin 	 * Determine whether we're a log vdev.
3964527Sperrin 	 */
3974527Sperrin 	islog = 0;
3984527Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
3995094Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
4004527Sperrin 		return (ENOTSUP);
4014527Sperrin 
40210594SGeorge.Wilson@Sun.COM 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
40310594SGeorge.Wilson@Sun.COM 		return (ENOTSUP);
40410594SGeorge.Wilson@Sun.COM 
4054527Sperrin 	/*
4064527Sperrin 	 * Set the nparity property for RAID-Z vdevs.
4074527Sperrin 	 */
4084527Sperrin 	nparity = -1ULL;
4094527Sperrin 	if (ops == &vdev_raidz_ops) {
4104527Sperrin 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4114527Sperrin 		    &nparity) == 0) {
4124527Sperrin 			/*
41310105Sadam.leventhal@sun.com 			 * Currently, we can only support 3 parity devices.
4144527Sperrin 			 */
41510105Sadam.leventhal@sun.com 			if (nparity == 0 || nparity > 3)
4164527Sperrin 				return (EINVAL);
4174527Sperrin 			/*
41810105Sadam.leventhal@sun.com 			 * Previous versions could only support 1 or 2 parity
41910105Sadam.leventhal@sun.com 			 * device.
4204527Sperrin 			 */
42110105Sadam.leventhal@sun.com 			if (nparity > 1 &&
42210105Sadam.leventhal@sun.com 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
42310105Sadam.leventhal@sun.com 				return (ENOTSUP);
42410105Sadam.leventhal@sun.com 			if (nparity > 2 &&
42510105Sadam.leventhal@sun.com 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
4264527Sperrin 				return (ENOTSUP);
4274527Sperrin 		} else {
4284527Sperrin 			/*
4294527Sperrin 			 * We require the parity to be specified for SPAs that
4304527Sperrin 			 * support multiple parity levels.
4314527Sperrin 			 */
43210105Sadam.leventhal@sun.com 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
4334527Sperrin 				return (EINVAL);
4344527Sperrin 			/*
4354527Sperrin 			 * Otherwise, we default to 1 parity device for RAID-Z.
4364527Sperrin 			 */
4374527Sperrin 			nparity = 1;
4384527Sperrin 		}
4394527Sperrin 	} else {
4404527Sperrin 		nparity = 0;
4414527Sperrin 	}
4424527Sperrin 	ASSERT(nparity != -1ULL);
4434527Sperrin 
444789Sahrens 	vd = vdev_alloc_common(spa, id, guid, ops);
445789Sahrens 
4464527Sperrin 	vd->vdev_islog = islog;
4474527Sperrin 	vd->vdev_nparity = nparity;
4484527Sperrin 
449789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
450789Sahrens 		vd->vdev_path = spa_strdup(vd->vdev_path);
451789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
452789Sahrens 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4534451Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4544451Seschrock 	    &vd->vdev_physpath) == 0)
4554451Seschrock 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4569425SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4579425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
458789Sahrens 
459789Sahrens 	/*
4601171Seschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
4611171Seschrock 	 * as -1.
4621171Seschrock 	 */
4631171Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
4641171Seschrock 	    &vd->vdev_wholedisk) != 0)
4651171Seschrock 		vd->vdev_wholedisk = -1ULL;
4661171Seschrock 
4671171Seschrock 	/*
4681544Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
4691544Seschrock 	 * was not present at the time of import.
4701544Seschrock 	 */
4719425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4729425SEric.Schrock@Sun.COM 	    &vd->vdev_not_present);
4731544Seschrock 
4741544Seschrock 	/*
4751732Sbonwick 	 * Get the alignment requirement.
4761732Sbonwick 	 */
4771732Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
4781732Sbonwick 
4791732Sbonwick 	/*
48010594SGeorge.Wilson@Sun.COM 	 * Retrieve the vdev creation time.
48110594SGeorge.Wilson@Sun.COM 	 */
48210594SGeorge.Wilson@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
48310594SGeorge.Wilson@Sun.COM 	    &vd->vdev_crtxg);
48410594SGeorge.Wilson@Sun.COM 
48510594SGeorge.Wilson@Sun.COM 	/*
486789Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
487789Sahrens 	 */
488789Sahrens 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
489789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
490789Sahrens 		    &vd->vdev_ms_array);
491789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
492789Sahrens 		    &vd->vdev_ms_shift);
493789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
494789Sahrens 		    &vd->vdev_asize);
495789Sahrens 	}
496789Sahrens 
497789Sahrens 	/*
4984451Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
499789Sahrens 	 */
5006643Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
5019790SLin.Ling@Sun.COM 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
5029790SLin.Ling@Sun.COM 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
5036643Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
5046643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
5058241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl_smo.smo_object);
5066643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
5076643Seschrock 			    &vd->vdev_unspare);
5086643Seschrock 		}
5099790SLin.Ling@Sun.COM 
5109790SLin.Ling@Sun.COM 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
5119790SLin.Ling@Sun.COM 			uint64_t spare = 0;
5129790SLin.Ling@Sun.COM 
5139790SLin.Ling@Sun.COM 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
5149790SLin.Ling@Sun.COM 			    &spare) == 0 && spare)
5159790SLin.Ling@Sun.COM 				spa_spare_add(vd);
5169790SLin.Ling@Sun.COM 		}
5179790SLin.Ling@Sun.COM 
5181732Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
5191732Sbonwick 		    &vd->vdev_offline);
5206643Seschrock 
5214451Seschrock 		/*
5224451Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5234451Seschrock 		 * state, as the diagnosis made on another system may not be
524*10817SEric.Schrock@Sun.COM 		 * valid in the current context.  Local vdevs will
525*10817SEric.Schrock@Sun.COM 		 * remain in the faulted state.
5264451Seschrock 		 */
5274451Seschrock 		if (spa->spa_load_state == SPA_LOAD_OPEN) {
5284451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5294451Seschrock 			    &vd->vdev_faulted);
5304451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5314451Seschrock 			    &vd->vdev_degraded);
5324451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5334451Seschrock 			    &vd->vdev_removed);
534*10817SEric.Schrock@Sun.COM 
535*10817SEric.Schrock@Sun.COM 			if (vd->vdev_faulted || vd->vdev_degraded) {
536*10817SEric.Schrock@Sun.COM 				char *aux;
537*10817SEric.Schrock@Sun.COM 
538*10817SEric.Schrock@Sun.COM 				vd->vdev_label_aux =
539*10817SEric.Schrock@Sun.COM 				    VDEV_AUX_ERR_EXCEEDED;
540*10817SEric.Schrock@Sun.COM 				if (nvlist_lookup_string(nv,
541*10817SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
542*10817SEric.Schrock@Sun.COM 				    strcmp(aux, "external") == 0)
543*10817SEric.Schrock@Sun.COM 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
544*10817SEric.Schrock@Sun.COM 			}
5454451Seschrock 		}
546789Sahrens 	}
547789Sahrens 
548789Sahrens 	/*
549789Sahrens 	 * Add ourselves to the parent's list of children.
550789Sahrens 	 */
551789Sahrens 	vdev_add_child(parent, vd);
552789Sahrens 
5532082Seschrock 	*vdp = vd;
5542082Seschrock 
5552082Seschrock 	return (0);
556789Sahrens }
557789Sahrens 
558789Sahrens void
559789Sahrens vdev_free(vdev_t *vd)
560789Sahrens {
5614451Seschrock 	spa_t *spa = vd->vdev_spa;
562789Sahrens 
563789Sahrens 	/*
564789Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
565789Sahrens 	 * trying to ensure complicated semantics for all callers.
566789Sahrens 	 */
567789Sahrens 	vdev_close(vd);
568789Sahrens 
5697754SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
570789Sahrens 
571789Sahrens 	/*
572789Sahrens 	 * Free all children.
573789Sahrens 	 */
5749816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
575789Sahrens 		vdev_free(vd->vdev_child[c]);
576789Sahrens 
577789Sahrens 	ASSERT(vd->vdev_child == NULL);
578789Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
579789Sahrens 
580789Sahrens 	/*
581789Sahrens 	 * Discard allocation state.
582789Sahrens 	 */
583789Sahrens 	if (vd == vd->vdev_top)
584789Sahrens 		vdev_metaslab_fini(vd);
585789Sahrens 
586789Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
5872082Seschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
588789Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
589789Sahrens 
590789Sahrens 	/*
591789Sahrens 	 * Remove this vdev from its parent's child list.
592789Sahrens 	 */
593789Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
594789Sahrens 
595789Sahrens 	ASSERT(vd->vdev_parent == NULL);
596789Sahrens 
5974451Seschrock 	/*
5984451Seschrock 	 * Clean up vdev structure.
5994451Seschrock 	 */
6004451Seschrock 	vdev_queue_fini(vd);
6014451Seschrock 	vdev_cache_fini(vd);
6024451Seschrock 
6034451Seschrock 	if (vd->vdev_path)
6044451Seschrock 		spa_strfree(vd->vdev_path);
6054451Seschrock 	if (vd->vdev_devid)
6064451Seschrock 		spa_strfree(vd->vdev_devid);
6074451Seschrock 	if (vd->vdev_physpath)
6084451Seschrock 		spa_strfree(vd->vdev_physpath);
6099425SEric.Schrock@Sun.COM 	if (vd->vdev_fru)
6109425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_fru);
6114451Seschrock 
6124451Seschrock 	if (vd->vdev_isspare)
6134451Seschrock 		spa_spare_remove(vd);
6145450Sbrendan 	if (vd->vdev_isl2cache)
6155450Sbrendan 		spa_l2cache_remove(vd);
6164451Seschrock 
6174451Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
6184451Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
6198241SJeff.Bonwick@Sun.COM 
6204451Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
6218241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
6228241SJeff.Bonwick@Sun.COM 		space_map_unload(&vd->vdev_dtl[t]);
6238241SJeff.Bonwick@Sun.COM 		space_map_destroy(&vd->vdev_dtl[t]);
6248241SJeff.Bonwick@Sun.COM 	}
6254451Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
6268241SJeff.Bonwick@Sun.COM 
6274451Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6284451Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
6297754SJeff.Bonwick@Sun.COM 	mutex_destroy(&vd->vdev_probe_lock);
6304451Seschrock 
6314451Seschrock 	if (vd == spa->spa_root_vdev)
6324451Seschrock 		spa->spa_root_vdev = NULL;
6334451Seschrock 
6344451Seschrock 	kmem_free(vd, sizeof (vdev_t));
635789Sahrens }
636789Sahrens 
637789Sahrens /*
638789Sahrens  * Transfer top-level vdev state from svd to tvd.
639789Sahrens  */
640789Sahrens static void
641789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
642789Sahrens {
643789Sahrens 	spa_t *spa = svd->vdev_spa;
644789Sahrens 	metaslab_t *msp;
645789Sahrens 	vdev_t *vd;
646789Sahrens 	int t;
647789Sahrens 
648789Sahrens 	ASSERT(tvd == tvd->vdev_top);
649789Sahrens 
650789Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
651789Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
652789Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
653789Sahrens 
654789Sahrens 	svd->vdev_ms_array = 0;
655789Sahrens 	svd->vdev_ms_shift = 0;
656789Sahrens 	svd->vdev_ms_count = 0;
657789Sahrens 
658789Sahrens 	tvd->vdev_mg = svd->vdev_mg;
659789Sahrens 	tvd->vdev_ms = svd->vdev_ms;
660789Sahrens 
661789Sahrens 	svd->vdev_mg = NULL;
662789Sahrens 	svd->vdev_ms = NULL;
6631732Sbonwick 
6641732Sbonwick 	if (tvd->vdev_mg != NULL)
6651732Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
666789Sahrens 
667789Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
668789Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
6692082Seschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
670789Sahrens 
671789Sahrens 	svd->vdev_stat.vs_alloc = 0;
672789Sahrens 	svd->vdev_stat.vs_space = 0;
6732082Seschrock 	svd->vdev_stat.vs_dspace = 0;
674789Sahrens 
675789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
676789Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
677789Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
678789Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
679789Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
680789Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
681789Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
682789Sahrens 	}
683789Sahrens 
6847754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_config_dirty_node)) {
685789Sahrens 		vdev_config_clean(svd);
686789Sahrens 		vdev_config_dirty(tvd);
687789Sahrens 	}
688789Sahrens 
6897754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_state_dirty_node)) {
6907754SJeff.Bonwick@Sun.COM 		vdev_state_clean(svd);
6917754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(tvd);
6927754SJeff.Bonwick@Sun.COM 	}
6937754SJeff.Bonwick@Sun.COM 
6942082Seschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
6952082Seschrock 	svd->vdev_deflate_ratio = 0;
6964527Sperrin 
6974527Sperrin 	tvd->vdev_islog = svd->vdev_islog;
6984527Sperrin 	svd->vdev_islog = 0;
699789Sahrens }
700789Sahrens 
701789Sahrens static void
702789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
703789Sahrens {
704789Sahrens 	if (vd == NULL)
705789Sahrens 		return;
706789Sahrens 
707789Sahrens 	vd->vdev_top = tvd;
708789Sahrens 
7099816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
710789Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
711789Sahrens }
712789Sahrens 
713789Sahrens /*
714789Sahrens  * Add a mirror/replacing vdev above an existing vdev.
715789Sahrens  */
716789Sahrens vdev_t *
717789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
718789Sahrens {
719789Sahrens 	spa_t *spa = cvd->vdev_spa;
720789Sahrens 	vdev_t *pvd = cvd->vdev_parent;
721789Sahrens 	vdev_t *mvd;
722789Sahrens 
7237754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
724789Sahrens 
725789Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
7261732Sbonwick 
7271732Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
7289816SGeorge.Wilson@Sun.COM 	mvd->vdev_min_asize = cvd->vdev_min_asize;
7291732Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
7301732Sbonwick 	mvd->vdev_state = cvd->vdev_state;
73110594SGeorge.Wilson@Sun.COM 	mvd->vdev_crtxg = cvd->vdev_crtxg;
7321732Sbonwick 
733789Sahrens 	vdev_remove_child(pvd, cvd);
734789Sahrens 	vdev_add_child(pvd, mvd);
735789Sahrens 	cvd->vdev_id = mvd->vdev_children;
736789Sahrens 	vdev_add_child(mvd, cvd);
737789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
738789Sahrens 
739789Sahrens 	if (mvd == mvd->vdev_top)
740789Sahrens 		vdev_top_transfer(cvd, mvd);
741789Sahrens 
742789Sahrens 	return (mvd);
743789Sahrens }
744789Sahrens 
745789Sahrens /*
746789Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
747789Sahrens  */
748789Sahrens void
749789Sahrens vdev_remove_parent(vdev_t *cvd)
750789Sahrens {
751789Sahrens 	vdev_t *mvd = cvd->vdev_parent;
752789Sahrens 	vdev_t *pvd = mvd->vdev_parent;
753789Sahrens 
7547754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
755789Sahrens 
756789Sahrens 	ASSERT(mvd->vdev_children == 1);
757789Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
7582082Seschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
7592082Seschrock 	    mvd->vdev_ops == &vdev_spare_ops);
7601732Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
761789Sahrens 
762789Sahrens 	vdev_remove_child(mvd, cvd);
763789Sahrens 	vdev_remove_child(pvd, mvd);
7648241SJeff.Bonwick@Sun.COM 
7657754SJeff.Bonwick@Sun.COM 	/*
7667754SJeff.Bonwick@Sun.COM 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
7677754SJeff.Bonwick@Sun.COM 	 * Otherwise, we could have detached an offline device, and when we
7687754SJeff.Bonwick@Sun.COM 	 * go to import the pool we'll think we have two top-level vdevs,
7697754SJeff.Bonwick@Sun.COM 	 * instead of a different version of the same top-level vdev.
7707754SJeff.Bonwick@Sun.COM 	 */
7718241SJeff.Bonwick@Sun.COM 	if (mvd->vdev_top == mvd) {
7728241SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
7738241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid += guid_delta;
7748241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid_sum += guid_delta;
7758241SJeff.Bonwick@Sun.COM 	}
776789Sahrens 	cvd->vdev_id = mvd->vdev_id;
777789Sahrens 	vdev_add_child(pvd, cvd);
778789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
779789Sahrens 
780789Sahrens 	if (cvd == cvd->vdev_top)
781789Sahrens 		vdev_top_transfer(mvd, cvd);
782789Sahrens 
783789Sahrens 	ASSERT(mvd->vdev_children == 0);
784789Sahrens 	vdev_free(mvd);
785789Sahrens }
786789Sahrens 
7871544Seschrock int
788789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
789789Sahrens {
790789Sahrens 	spa_t *spa = vd->vdev_spa;
7911732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
7924527Sperrin 	metaslab_class_t *mc;
7931732Sbonwick 	uint64_t m;
794789Sahrens 	uint64_t oldc = vd->vdev_ms_count;
795789Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
7961732Sbonwick 	metaslab_t **mspp;
7971732Sbonwick 	int error;
798789Sahrens 
79910594SGeorge.Wilson@Sun.COM 	/*
80010594SGeorge.Wilson@Sun.COM 	 * This vdev is not being allocated from yet or is a hole.
80110594SGeorge.Wilson@Sun.COM 	 */
80210594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms_shift == 0)
8031585Sbonwick 		return (0);
8041585Sbonwick 
80510594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
80610594SGeorge.Wilson@Sun.COM 
8079701SGeorge.Wilson@Sun.COM 	/*
8089701SGeorge.Wilson@Sun.COM 	 * Compute the raidz-deflation ratio.  Note, we hard-code
8099701SGeorge.Wilson@Sun.COM 	 * in 128k (1 << 17) because it is the current "typical" blocksize.
8109701SGeorge.Wilson@Sun.COM 	 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
8119701SGeorge.Wilson@Sun.COM 	 * or we will inconsistently account for existing bp's.
8129701SGeorge.Wilson@Sun.COM 	 */
8139701SGeorge.Wilson@Sun.COM 	vd->vdev_deflate_ratio = (1 << 17) /
8149701SGeorge.Wilson@Sun.COM 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
8159701SGeorge.Wilson@Sun.COM 
816789Sahrens 	ASSERT(oldc <= newc);
817789Sahrens 
8184527Sperrin 	if (vd->vdev_islog)
8194527Sperrin 		mc = spa->spa_log_class;
8204527Sperrin 	else
8214527Sperrin 		mc = spa->spa_normal_class;
8224527Sperrin 
8231732Sbonwick 	if (vd->vdev_mg == NULL)
8241732Sbonwick 		vd->vdev_mg = metaslab_group_create(mc, vd);
8251732Sbonwick 
8261732Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
8271732Sbonwick 
8281732Sbonwick 	if (oldc != 0) {
8291732Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
8301732Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
8311732Sbonwick 	}
8321732Sbonwick 
8331732Sbonwick 	vd->vdev_ms = mspp;
834789Sahrens 	vd->vdev_ms_count = newc;
835789Sahrens 
8361732Sbonwick 	for (m = oldc; m < newc; m++) {
8371732Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
838789Sahrens 		if (txg == 0) {
8391732Sbonwick 			uint64_t object = 0;
8401732Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8419512SNeil.Perrin@Sun.COM 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8429512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
8431732Sbonwick 			if (error)
8441732Sbonwick 				return (error);
8451732Sbonwick 			if (object != 0) {
8461732Sbonwick 				dmu_buf_t *db;
8471732Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
8481732Sbonwick 				if (error)
8491732Sbonwick 					return (error);
8504944Smaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
8514944Smaybee 				bcopy(db->db_data, &smo, sizeof (smo));
8521732Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
8531544Seschrock 				dmu_buf_rele(db, FTAG);
854789Sahrens 			}
855789Sahrens 		}
8561732Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
8571732Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
858789Sahrens 	}
859789Sahrens 
8601544Seschrock 	return (0);
861789Sahrens }
862789Sahrens 
863789Sahrens void
864789Sahrens vdev_metaslab_fini(vdev_t *vd)
865789Sahrens {
866789Sahrens 	uint64_t m;
867789Sahrens 	uint64_t count = vd->vdev_ms_count;
868789Sahrens 
869789Sahrens 	if (vd->vdev_ms != NULL) {
870789Sahrens 		for (m = 0; m < count; m++)
8711732Sbonwick 			if (vd->vdev_ms[m] != NULL)
8721732Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
873789Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
874789Sahrens 		vd->vdev_ms = NULL;
875789Sahrens 	}
876789Sahrens }
877789Sahrens 
8787754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats {
8797754SJeff.Bonwick@Sun.COM 	boolean_t	vps_readable;
8807754SJeff.Bonwick@Sun.COM 	boolean_t	vps_writeable;
8817754SJeff.Bonwick@Sun.COM 	int		vps_flags;
8827754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t;
8837754SJeff.Bonwick@Sun.COM 
8847754SJeff.Bonwick@Sun.COM static void
8857754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio)
8865329Sgw25295 {
8878241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
8888632SBill.Moore@Sun.COM 	vdev_t *vd = zio->io_vd;
8897754SJeff.Bonwick@Sun.COM 	vdev_probe_stats_t *vps = zio->io_private;
8908632SBill.Moore@Sun.COM 
8918632SBill.Moore@Sun.COM 	ASSERT(vd->vdev_probe_zio != NULL);
8927754SJeff.Bonwick@Sun.COM 
8937754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ) {
8947754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8957754SJeff.Bonwick@Sun.COM 			vps->vps_readable = 1;
8968241SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0 && spa_writeable(spa)) {
8978632SBill.Moore@Sun.COM 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
8987754SJeff.Bonwick@Sun.COM 			    zio->io_offset, zio->io_size, zio->io_data,
8997754SJeff.Bonwick@Sun.COM 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
9007754SJeff.Bonwick@Sun.COM 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
9017754SJeff.Bonwick@Sun.COM 		} else {
9027754SJeff.Bonwick@Sun.COM 			zio_buf_free(zio->io_data, zio->io_size);
9037754SJeff.Bonwick@Sun.COM 		}
9047754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
9057754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
9067754SJeff.Bonwick@Sun.COM 			vps->vps_writeable = 1;
9077754SJeff.Bonwick@Sun.COM 		zio_buf_free(zio->io_data, zio->io_size);
9087754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_NULL) {
9098632SBill.Moore@Sun.COM 		zio_t *pio;
9107754SJeff.Bonwick@Sun.COM 
9117754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read |= !vps->vps_readable;
9127754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write |= !vps->vps_writeable;
9137754SJeff.Bonwick@Sun.COM 
9147754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) &&
9158241SJeff.Bonwick@Sun.COM 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
9167754SJeff.Bonwick@Sun.COM 			zio->io_error = 0;
9177754SJeff.Bonwick@Sun.COM 		} else {
9187754SJeff.Bonwick@Sun.COM 			ASSERT(zio->io_error != 0);
9197754SJeff.Bonwick@Sun.COM 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
9208241SJeff.Bonwick@Sun.COM 			    spa, vd, NULL, 0, 0);
9217754SJeff.Bonwick@Sun.COM 			zio->io_error = ENXIO;
9227754SJeff.Bonwick@Sun.COM 		}
9238632SBill.Moore@Sun.COM 
9248632SBill.Moore@Sun.COM 		mutex_enter(&vd->vdev_probe_lock);
9258632SBill.Moore@Sun.COM 		ASSERT(vd->vdev_probe_zio == zio);
9268632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = NULL;
9278632SBill.Moore@Sun.COM 		mutex_exit(&vd->vdev_probe_lock);
9288632SBill.Moore@Sun.COM 
9298632SBill.Moore@Sun.COM 		while ((pio = zio_walk_parents(zio)) != NULL)
9308632SBill.Moore@Sun.COM 			if (!vdev_accessible(vd, pio))
9318632SBill.Moore@Sun.COM 				pio->io_error = ENXIO;
9328632SBill.Moore@Sun.COM 
9337754SJeff.Bonwick@Sun.COM 		kmem_free(vps, sizeof (*vps));
9347754SJeff.Bonwick@Sun.COM 	}
9357754SJeff.Bonwick@Sun.COM }
9365329Sgw25295 
9377754SJeff.Bonwick@Sun.COM /*
9387754SJeff.Bonwick@Sun.COM  * Determine whether this device is accessible by reading and writing
9397754SJeff.Bonwick@Sun.COM  * to several known locations: the pad regions of each vdev label
9407754SJeff.Bonwick@Sun.COM  * but the first (which we leave alone in case it contains a VTOC).
9417754SJeff.Bonwick@Sun.COM  */
9427754SJeff.Bonwick@Sun.COM zio_t *
9438632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio)
9447754SJeff.Bonwick@Sun.COM {
9457754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
9468632SBill.Moore@Sun.COM 	vdev_probe_stats_t *vps = NULL;
9478632SBill.Moore@Sun.COM 	zio_t *pio;
9487754SJeff.Bonwick@Sun.COM 
9497754SJeff.Bonwick@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9507754SJeff.Bonwick@Sun.COM 
9518632SBill.Moore@Sun.COM 	/*
9528632SBill.Moore@Sun.COM 	 * Don't probe the probe.
9538632SBill.Moore@Sun.COM 	 */
9548632SBill.Moore@Sun.COM 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
9558632SBill.Moore@Sun.COM 		return (NULL);
9568632SBill.Moore@Sun.COM 
9578632SBill.Moore@Sun.COM 	/*
9588632SBill.Moore@Sun.COM 	 * To prevent 'probe storms' when a device fails, we create
9598632SBill.Moore@Sun.COM 	 * just one probe i/o at a time.  All zios that want to probe
9608632SBill.Moore@Sun.COM 	 * this vdev will become parents of the probe io.
9618632SBill.Moore@Sun.COM 	 */
9628632SBill.Moore@Sun.COM 	mutex_enter(&vd->vdev_probe_lock);
9638632SBill.Moore@Sun.COM 
9648632SBill.Moore@Sun.COM 	if ((pio = vd->vdev_probe_zio) == NULL) {
9658632SBill.Moore@Sun.COM 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
9668632SBill.Moore@Sun.COM 
9678632SBill.Moore@Sun.COM 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
9688632SBill.Moore@Sun.COM 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9699725SEric.Schrock@Sun.COM 		    ZIO_FLAG_TRYHARD;
9708632SBill.Moore@Sun.COM 
9718632SBill.Moore@Sun.COM 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
9728632SBill.Moore@Sun.COM 			/*
9738632SBill.Moore@Sun.COM 			 * vdev_cant_read and vdev_cant_write can only
9748632SBill.Moore@Sun.COM 			 * transition from TRUE to FALSE when we have the
9758632SBill.Moore@Sun.COM 			 * SCL_ZIO lock as writer; otherwise they can only
9768632SBill.Moore@Sun.COM 			 * transition from FALSE to TRUE.  This ensures that
9778632SBill.Moore@Sun.COM 			 * any zio looking at these values can assume that
9788632SBill.Moore@Sun.COM 			 * failures persist for the life of the I/O.  That's
9798632SBill.Moore@Sun.COM 			 * important because when a device has intermittent
9808632SBill.Moore@Sun.COM 			 * connectivity problems, we want to ensure that
9818632SBill.Moore@Sun.COM 			 * they're ascribed to the device (ENXIO) and not
9828632SBill.Moore@Sun.COM 			 * the zio (EIO).
9838632SBill.Moore@Sun.COM 			 *
9848632SBill.Moore@Sun.COM 			 * Since we hold SCL_ZIO as writer here, clear both
9858632SBill.Moore@Sun.COM 			 * values so the probe can reevaluate from first
9868632SBill.Moore@Sun.COM 			 * principles.
9878632SBill.Moore@Sun.COM 			 */
9888632SBill.Moore@Sun.COM 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
9898632SBill.Moore@Sun.COM 			vd->vdev_cant_read = B_FALSE;
9908632SBill.Moore@Sun.COM 			vd->vdev_cant_write = B_FALSE;
9918632SBill.Moore@Sun.COM 		}
9928632SBill.Moore@Sun.COM 
9938632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
9948632SBill.Moore@Sun.COM 		    vdev_probe_done, vps,
9958632SBill.Moore@Sun.COM 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
9968632SBill.Moore@Sun.COM 
9978632SBill.Moore@Sun.COM 		if (zio != NULL) {
9988632SBill.Moore@Sun.COM 			vd->vdev_probe_wanted = B_TRUE;
9998632SBill.Moore@Sun.COM 			spa_async_request(spa, SPA_ASYNC_PROBE);
10008632SBill.Moore@Sun.COM 		}
10018632SBill.Moore@Sun.COM 	}
10028632SBill.Moore@Sun.COM 
10038632SBill.Moore@Sun.COM 	if (zio != NULL)
10048632SBill.Moore@Sun.COM 		zio_add_child(zio, pio);
10058632SBill.Moore@Sun.COM 
10068632SBill.Moore@Sun.COM 	mutex_exit(&vd->vdev_probe_lock);
10078632SBill.Moore@Sun.COM 
10088632SBill.Moore@Sun.COM 	if (vps == NULL) {
10098632SBill.Moore@Sun.COM 		ASSERT(zio != NULL);
10108632SBill.Moore@Sun.COM 		return (NULL);
10118632SBill.Moore@Sun.COM 	}
10127754SJeff.Bonwick@Sun.COM 
10137754SJeff.Bonwick@Sun.COM 	for (int l = 1; l < VDEV_LABELS; l++) {
10148632SBill.Moore@Sun.COM 		zio_nowait(zio_read_phys(pio, vd,
10157754SJeff.Bonwick@Sun.COM 		    vdev_label_offset(vd->vdev_psize, l,
10169056SLin.Ling@Sun.COM 		    offsetof(vdev_label_t, vl_pad2)),
10179056SLin.Ling@Sun.COM 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
10187754SJeff.Bonwick@Sun.COM 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
10197754SJeff.Bonwick@Sun.COM 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
10207754SJeff.Bonwick@Sun.COM 	}
10217754SJeff.Bonwick@Sun.COM 
10228632SBill.Moore@Sun.COM 	if (zio == NULL)
10238632SBill.Moore@Sun.COM 		return (pio);
10248632SBill.Moore@Sun.COM 
10258632SBill.Moore@Sun.COM 	zio_nowait(pio);
10268632SBill.Moore@Sun.COM 	return (NULL);
10275329Sgw25295 }
10285329Sgw25295 
10299846SEric.Taylor@Sun.COM static void
10309846SEric.Taylor@Sun.COM vdev_open_child(void *arg)
10319846SEric.Taylor@Sun.COM {
10329846SEric.Taylor@Sun.COM 	vdev_t *vd = arg;
10339846SEric.Taylor@Sun.COM 
10349846SEric.Taylor@Sun.COM 	vd->vdev_open_thread = curthread;
10359846SEric.Taylor@Sun.COM 	vd->vdev_open_error = vdev_open(vd);
10369846SEric.Taylor@Sun.COM 	vd->vdev_open_thread = NULL;
10379846SEric.Taylor@Sun.COM }
10389846SEric.Taylor@Sun.COM 
103910588SEric.Taylor@Sun.COM boolean_t
104010588SEric.Taylor@Sun.COM vdev_uses_zvols(vdev_t *vd)
104110588SEric.Taylor@Sun.COM {
104210588SEric.Taylor@Sun.COM 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
104310588SEric.Taylor@Sun.COM 	    strlen(ZVOL_DIR)) == 0)
104410588SEric.Taylor@Sun.COM 		return (B_TRUE);
104510588SEric.Taylor@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
104610588SEric.Taylor@Sun.COM 		if (vdev_uses_zvols(vd->vdev_child[c]))
104710588SEric.Taylor@Sun.COM 			return (B_TRUE);
104810588SEric.Taylor@Sun.COM 	return (B_FALSE);
104910588SEric.Taylor@Sun.COM }
105010588SEric.Taylor@Sun.COM 
10519846SEric.Taylor@Sun.COM void
10529846SEric.Taylor@Sun.COM vdev_open_children(vdev_t *vd)
10539846SEric.Taylor@Sun.COM {
10549846SEric.Taylor@Sun.COM 	taskq_t *tq;
10559846SEric.Taylor@Sun.COM 	int children = vd->vdev_children;
10569846SEric.Taylor@Sun.COM 
105710588SEric.Taylor@Sun.COM 	/*
105810588SEric.Taylor@Sun.COM 	 * in order to handle pools on top of zvols, do the opens
105910588SEric.Taylor@Sun.COM 	 * in a single thread so that the same thread holds the
106010588SEric.Taylor@Sun.COM 	 * spa_namespace_lock
106110588SEric.Taylor@Sun.COM 	 */
106210588SEric.Taylor@Sun.COM 	if (vdev_uses_zvols(vd)) {
106310588SEric.Taylor@Sun.COM 		for (int c = 0; c < children; c++)
106410588SEric.Taylor@Sun.COM 			vd->vdev_child[c]->vdev_open_error =
106510588SEric.Taylor@Sun.COM 			    vdev_open(vd->vdev_child[c]);
106610588SEric.Taylor@Sun.COM 		return;
106710588SEric.Taylor@Sun.COM 	}
10689846SEric.Taylor@Sun.COM 	tq = taskq_create("vdev_open", children, minclsyspri,
10699846SEric.Taylor@Sun.COM 	    children, children, TASKQ_PREPOPULATE);
10709846SEric.Taylor@Sun.COM 
10719846SEric.Taylor@Sun.COM 	for (int c = 0; c < children; c++)
10729846SEric.Taylor@Sun.COM 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
10739846SEric.Taylor@Sun.COM 		    TQ_SLEEP) != NULL);
10749846SEric.Taylor@Sun.COM 
10759846SEric.Taylor@Sun.COM 	taskq_destroy(tq);
10769846SEric.Taylor@Sun.COM }
10779846SEric.Taylor@Sun.COM 
1078789Sahrens /*
1079789Sahrens  * Prepare a virtual device for access.
1080789Sahrens  */
1081789Sahrens int
1082789Sahrens vdev_open(vdev_t *vd)
1083789Sahrens {
10848241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
1085789Sahrens 	int error;
1086789Sahrens 	uint64_t osize = 0;
1087789Sahrens 	uint64_t asize, psize;
10881732Sbonwick 	uint64_t ashift = 0;
1089789Sahrens 
10909846SEric.Taylor@Sun.COM 	ASSERT(vd->vdev_open_thread == curthread ||
10919846SEric.Taylor@Sun.COM 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1092789Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1093789Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1094789Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1095789Sahrens 
1096789Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
10979701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_read = B_FALSE;
10989701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_write = B_FALSE;
10999816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1100789Sahrens 
1101*10817SEric.Schrock@Sun.COM 	/*
1102*10817SEric.Schrock@Sun.COM 	 * If this vdev is not removed, check its fault status.  If it's
1103*10817SEric.Schrock@Sun.COM 	 * faulted, bail out of the open.
1104*10817SEric.Schrock@Sun.COM 	 */
11054451Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
11064451Seschrock 		ASSERT(vd->vdev_children == 0);
1107*10817SEric.Schrock@Sun.COM 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1108*10817SEric.Schrock@Sun.COM 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
11094451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1110*10817SEric.Schrock@Sun.COM 		    vd->vdev_label_aux);
11114451Seschrock 		return (ENXIO);
11124451Seschrock 	} else if (vd->vdev_offline) {
1113789Sahrens 		ASSERT(vd->vdev_children == 0);
11141544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1115789Sahrens 		return (ENXIO);
1116789Sahrens 	}
1117789Sahrens 
1118789Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1119789Sahrens 
11201544Seschrock 	if (zio_injection_enabled && error == 0)
11219725SEric.Schrock@Sun.COM 		error = zio_handle_device_injection(vd, NULL, ENXIO);
11221544Seschrock 
11234451Seschrock 	if (error) {
11244451Seschrock 		if (vd->vdev_removed &&
11254451Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
11264451Seschrock 			vd->vdev_removed = B_FALSE;
1127789Sahrens 
11281544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1129789Sahrens 		    vd->vdev_stat.vs_aux);
1130789Sahrens 		return (error);
1131789Sahrens 	}
1132789Sahrens 
11334451Seschrock 	vd->vdev_removed = B_FALSE;
11344451Seschrock 
11354451Seschrock 	if (vd->vdev_degraded) {
11364451Seschrock 		ASSERT(vd->vdev_children == 0);
11374451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11384451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
11394451Seschrock 	} else {
1140*10817SEric.Schrock@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
11414451Seschrock 	}
1142789Sahrens 
114310594SGeorge.Wilson@Sun.COM 	/*
114410594SGeorge.Wilson@Sun.COM 	 * For hole or missing vdevs we just return success.
114510594SGeorge.Wilson@Sun.COM 	 */
114610594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
114710594SGeorge.Wilson@Sun.COM 		return (0);
114810594SGeorge.Wilson@Sun.COM 
11499816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
11501544Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
11511544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11521544Seschrock 			    VDEV_AUX_NONE);
11531544Seschrock 			break;
11541544Seschrock 		}
11559816SGeorge.Wilson@Sun.COM 	}
1156789Sahrens 
1157789Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1158789Sahrens 
1159789Sahrens 	if (vd->vdev_children == 0) {
1160789Sahrens 		if (osize < SPA_MINDEVSIZE) {
11611544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11621544Seschrock 			    VDEV_AUX_TOO_SMALL);
1163789Sahrens 			return (EOVERFLOW);
1164789Sahrens 		}
1165789Sahrens 		psize = osize;
1166789Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1167789Sahrens 	} else {
11681732Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1169789Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
11701544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11711544Seschrock 			    VDEV_AUX_TOO_SMALL);
1172789Sahrens 			return (EOVERFLOW);
1173789Sahrens 		}
1174789Sahrens 		psize = 0;
1175789Sahrens 		asize = osize;
1176789Sahrens 	}
1177789Sahrens 
1178789Sahrens 	vd->vdev_psize = psize;
1179789Sahrens 
11809816SGeorge.Wilson@Sun.COM 	/*
11819816SGeorge.Wilson@Sun.COM 	 * Make sure the allocatable size hasn't shrunk.
11829816SGeorge.Wilson@Sun.COM 	 */
11839816SGeorge.Wilson@Sun.COM 	if (asize < vd->vdev_min_asize) {
11849816SGeorge.Wilson@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11859816SGeorge.Wilson@Sun.COM 		    VDEV_AUX_BAD_LABEL);
11869816SGeorge.Wilson@Sun.COM 		return (EINVAL);
11879816SGeorge.Wilson@Sun.COM 	}
11889816SGeorge.Wilson@Sun.COM 
1189789Sahrens 	if (vd->vdev_asize == 0) {
1190789Sahrens 		/*
1191789Sahrens 		 * This is the first-ever open, so use the computed values.
11921732Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1193789Sahrens 		 */
1194789Sahrens 		vd->vdev_asize = asize;
11951732Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1196789Sahrens 	} else {
1197789Sahrens 		/*
1198789Sahrens 		 * Make sure the alignment requirement hasn't increased.
1199789Sahrens 		 */
12001732Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
12011544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12021544Seschrock 			    VDEV_AUX_BAD_LABEL);
1203789Sahrens 			return (EINVAL);
1204789Sahrens 		}
1205789Sahrens 	}
1206789Sahrens 
12071544Seschrock 	/*
12089816SGeorge.Wilson@Sun.COM 	 * If all children are healthy and the asize has increased,
12099816SGeorge.Wilson@Sun.COM 	 * then we've experienced dynamic LUN growth.  If automatic
12109816SGeorge.Wilson@Sun.COM 	 * expansion is enabled then use the additional space.
12119816SGeorge.Wilson@Sun.COM 	 */
12129816SGeorge.Wilson@Sun.COM 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
12139816SGeorge.Wilson@Sun.COM 	    (vd->vdev_expanding || spa->spa_autoexpand))
12149816SGeorge.Wilson@Sun.COM 		vd->vdev_asize = asize;
12159816SGeorge.Wilson@Sun.COM 
12169816SGeorge.Wilson@Sun.COM 	vdev_set_min_asize(vd);
12179816SGeorge.Wilson@Sun.COM 
12189816SGeorge.Wilson@Sun.COM 	/*
12195329Sgw25295 	 * Ensure we can issue some IO before declaring the
12205329Sgw25295 	 * vdev open for business.
12215329Sgw25295 	 */
12227754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf &&
12237754SJeff.Bonwick@Sun.COM 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
12245329Sgw25295 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12257754SJeff.Bonwick@Sun.COM 		    VDEV_AUX_IO_FAILURE);
12265329Sgw25295 		return (error);
12275329Sgw25295 	}
12285329Sgw25295 
12295329Sgw25295 	/*
12307046Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
12318241SJeff.Bonwick@Sun.COM 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
12328241SJeff.Bonwick@Sun.COM 	 * since this would just restart the scrub we are already doing.
12337046Sahrens 	 */
12348241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
12358241SJeff.Bonwick@Sun.COM 	    vdev_resilver_needed(vd, NULL, NULL))
12368241SJeff.Bonwick@Sun.COM 		spa_async_request(spa, SPA_ASYNC_RESILVER);
12377046Sahrens 
1238789Sahrens 	return (0);
1239789Sahrens }
1240789Sahrens 
1241789Sahrens /*
12421986Seschrock  * Called once the vdevs are all opened, this routine validates the label
12431986Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
12444451Seschrock  * inadvertently do repair I/Os to the wrong device.
12451986Seschrock  *
12461986Seschrock  * This function will only return failure if one of the vdevs indicates that it
12471986Seschrock  * has since been destroyed or exported.  This is only possible if
12481986Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
12491986Seschrock  * will be updated but the function will return 0.
12501986Seschrock  */
12511986Seschrock int
12521986Seschrock vdev_validate(vdev_t *vd)
12531986Seschrock {
12541986Seschrock 	spa_t *spa = vd->vdev_spa;
12551986Seschrock 	nvlist_t *label;
12567754SJeff.Bonwick@Sun.COM 	uint64_t guid, top_guid;
12571986Seschrock 	uint64_t state;
12581986Seschrock 
12599816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
12601986Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
12614070Smc142369 			return (EBADF);
12621986Seschrock 
12632174Seschrock 	/*
12642174Seschrock 	 * If the device has already failed, or was marked offline, don't do
12652174Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
12662174Seschrock 	 * overwrite the previous state.
12672174Seschrock 	 */
12687754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
12691986Seschrock 
12701986Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
12711986Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12721986Seschrock 			    VDEV_AUX_BAD_LABEL);
12731986Seschrock 			return (0);
12741986Seschrock 		}
12751986Seschrock 
12761986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
12771986Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
12781986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
12791986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12801986Seschrock 			nvlist_free(label);
12811986Seschrock 			return (0);
12821986Seschrock 		}
12831986Seschrock 
12847754SJeff.Bonwick@Sun.COM 		/*
12857754SJeff.Bonwick@Sun.COM 		 * If this vdev just became a top-level vdev because its
12867754SJeff.Bonwick@Sun.COM 		 * sibling was detached, it will have adopted the parent's
12877754SJeff.Bonwick@Sun.COM 		 * vdev guid -- but the label may or may not be on disk yet.
12887754SJeff.Bonwick@Sun.COM 		 * Fortunately, either version of the label will have the
12897754SJeff.Bonwick@Sun.COM 		 * same top guid, so if we're a top-level vdev, we can
12907754SJeff.Bonwick@Sun.COM 		 * safely compare to that instead.
12917754SJeff.Bonwick@Sun.COM 		 */
12921986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
12937754SJeff.Bonwick@Sun.COM 		    &guid) != 0 ||
12947754SJeff.Bonwick@Sun.COM 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
12957754SJeff.Bonwick@Sun.COM 		    &top_guid) != 0 ||
12967754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != guid &&
12977754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
12981986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
12991986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13001986Seschrock 			nvlist_free(label);
13011986Seschrock 			return (0);
13021986Seschrock 		}
13031986Seschrock 
13041986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
13051986Seschrock 		    &state) != 0) {
13061986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13071986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13081986Seschrock 			nvlist_free(label);
13091986Seschrock 			return (0);
13101986Seschrock 		}
13111986Seschrock 
13121986Seschrock 		nvlist_free(label);
13131986Seschrock 
131410100SLin.Ling@Sun.COM 		/*
131510100SLin.Ling@Sun.COM 		 * If spa->spa_load_verbatim is true, no need to check the
131610100SLin.Ling@Sun.COM 		 * state of the pool.
131710100SLin.Ling@Sun.COM 		 */
131810100SLin.Ling@Sun.COM 		if (!spa->spa_load_verbatim &&
131910100SLin.Ling@Sun.COM 		    spa->spa_load_state == SPA_LOAD_OPEN &&
132010100SLin.Ling@Sun.COM 		    state != POOL_STATE_ACTIVE)
13214070Smc142369 			return (EBADF);
13226976Seschrock 
13236976Seschrock 		/*
13246976Seschrock 		 * If we were able to open and validate a vdev that was
13256976Seschrock 		 * previously marked permanently unavailable, clear that state
13266976Seschrock 		 * now.
13276976Seschrock 		 */
13286976Seschrock 		if (vd->vdev_not_present)
13296976Seschrock 			vd->vdev_not_present = 0;
13301986Seschrock 	}
13311986Seschrock 
13321986Seschrock 	return (0);
13331986Seschrock }
13341986Seschrock 
13351986Seschrock /*
1336789Sahrens  * Close a virtual device.
1337789Sahrens  */
1338789Sahrens void
1339789Sahrens vdev_close(vdev_t *vd)
1340789Sahrens {
13418241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
13428241SJeff.Bonwick@Sun.COM 
13438241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
13448241SJeff.Bonwick@Sun.COM 
1345789Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1346789Sahrens 
13474451Seschrock 	vdev_cache_purge(vd);
1348789Sahrens 
13491986Seschrock 	/*
13509816SGeorge.Wilson@Sun.COM 	 * We record the previous state before we close it, so that if we are
13511986Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
13521986Seschrock 	 * it's still faulted.
13531986Seschrock 	 */
13541986Seschrock 	vd->vdev_prevstate = vd->vdev_state;
13551986Seschrock 
1356789Sahrens 	if (vd->vdev_offline)
1357789Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1358789Sahrens 	else
1359789Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
13601544Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1361789Sahrens }
1362789Sahrens 
1363789Sahrens void
13641544Seschrock vdev_reopen(vdev_t *vd)
1365789Sahrens {
13661544Seschrock 	spa_t *spa = vd->vdev_spa;
1367789Sahrens 
13687754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
13691544Seschrock 
1370789Sahrens 	vdev_close(vd);
1371789Sahrens 	(void) vdev_open(vd);
1372789Sahrens 
1373789Sahrens 	/*
13743377Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
13753377Seschrock 	 * Otherwise, a device with an invalid label could be successfully
13763377Seschrock 	 * opened in response to vdev_reopen().
13773377Seschrock 	 */
13786643Seschrock 	if (vd->vdev_aux) {
13796643Seschrock 		(void) vdev_validate_aux(vd);
13807754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) && vdev_writeable(vd) &&
13819425SEric.Schrock@Sun.COM 		    vd->vdev_aux == &spa->spa_l2cache &&
13829816SGeorge.Wilson@Sun.COM 		    !l2arc_vdev_present(vd))
13839816SGeorge.Wilson@Sun.COM 			l2arc_add_vdev(spa, vd);
13846643Seschrock 	} else {
13856643Seschrock 		(void) vdev_validate(vd);
13866643Seschrock 	}
13873377Seschrock 
13883377Seschrock 	/*
13894451Seschrock 	 * Reassess parent vdev's health.
1390789Sahrens 	 */
13914451Seschrock 	vdev_propagate_state(vd);
1392789Sahrens }
1393789Sahrens 
1394789Sahrens int
13952082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1396789Sahrens {
1397789Sahrens 	int error;
1398789Sahrens 
1399789Sahrens 	/*
1400789Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1401789Sahrens 	 * For a create, however, we want to fail the request if
1402789Sahrens 	 * there are any components we can't open.
1403789Sahrens 	 */
1404789Sahrens 	error = vdev_open(vd);
1405789Sahrens 
1406789Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1407789Sahrens 		vdev_close(vd);
1408789Sahrens 		return (error ? error : ENXIO);
1409789Sahrens 	}
1410789Sahrens 
1411789Sahrens 	/*
1412789Sahrens 	 * Recursively initialize all labels.
1413789Sahrens 	 */
14143377Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
14153377Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1416789Sahrens 		vdev_close(vd);
1417789Sahrens 		return (error);
1418789Sahrens 	}
1419789Sahrens 
1420789Sahrens 	return (0);
1421789Sahrens }
1422789Sahrens 
14231585Sbonwick void
14249816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(vdev_t *vd)
1425789Sahrens {
1426789Sahrens 	/*
1427789Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1428789Sahrens 	 */
1429789Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1430789Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1431789Sahrens }
1432789Sahrens 
1433789Sahrens void
14341732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1435789Sahrens {
14361732Sbonwick 	ASSERT(vd == vd->vdev_top);
143710594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
14381732Sbonwick 	ASSERT(ISP2(flags));
1439789Sahrens 
14401732Sbonwick 	if (flags & VDD_METASLAB)
14411732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
14421732Sbonwick 
14431732Sbonwick 	if (flags & VDD_DTL)
14441732Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
14451732Sbonwick 
14461732Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1447789Sahrens }
1448789Sahrens 
14498241SJeff.Bonwick@Sun.COM /*
14508241SJeff.Bonwick@Sun.COM  * DTLs.
14518241SJeff.Bonwick@Sun.COM  *
14528241SJeff.Bonwick@Sun.COM  * A vdev's DTL (dirty time log) is the set of transaction groups for which
14538241SJeff.Bonwick@Sun.COM  * the vdev has less than perfect replication.  There are three kinds of DTL:
14548241SJeff.Bonwick@Sun.COM  *
14558241SJeff.Bonwick@Sun.COM  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
14568241SJeff.Bonwick@Sun.COM  *
14578241SJeff.Bonwick@Sun.COM  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
14588241SJeff.Bonwick@Sun.COM  *
14598241SJeff.Bonwick@Sun.COM  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
14608241SJeff.Bonwick@Sun.COM  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
14618241SJeff.Bonwick@Sun.COM  *	txgs that was scrubbed.
14628241SJeff.Bonwick@Sun.COM  *
14638241SJeff.Bonwick@Sun.COM  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
14648241SJeff.Bonwick@Sun.COM  *	persistent errors or just some device being offline.
14658241SJeff.Bonwick@Sun.COM  *	Unlike the other three, the DTL_OUTAGE map is not generally
14668241SJeff.Bonwick@Sun.COM  *	maintained; it's only computed when needed, typically to
14678241SJeff.Bonwick@Sun.COM  *	determine whether a device can be detached.
14688241SJeff.Bonwick@Sun.COM  *
14698241SJeff.Bonwick@Sun.COM  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
14708241SJeff.Bonwick@Sun.COM  * either has the data or it doesn't.
14718241SJeff.Bonwick@Sun.COM  *
14728241SJeff.Bonwick@Sun.COM  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
14738241SJeff.Bonwick@Sun.COM  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
14748241SJeff.Bonwick@Sun.COM  * if any child is less than fully replicated, then so is its parent.
14758241SJeff.Bonwick@Sun.COM  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
14768241SJeff.Bonwick@Sun.COM  * comprising only those txgs which appear in 'maxfaults' or more children;
14778241SJeff.Bonwick@Sun.COM  * those are the txgs we don't have enough replication to read.  For example,
14788241SJeff.Bonwick@Sun.COM  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
14798241SJeff.Bonwick@Sun.COM  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
14808241SJeff.Bonwick@Sun.COM  * two child DTL_MISSING maps.
14818241SJeff.Bonwick@Sun.COM  *
14828241SJeff.Bonwick@Sun.COM  * It should be clear from the above that to compute the DTLs and outage maps
14838241SJeff.Bonwick@Sun.COM  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
14848241SJeff.Bonwick@Sun.COM  * Therefore, that is all we keep on disk.  When loading the pool, or after
14858241SJeff.Bonwick@Sun.COM  * a configuration change, we generate all other DTLs from first principles.
14868241SJeff.Bonwick@Sun.COM  */
1487789Sahrens void
14888241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1489789Sahrens {
14908241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
14918241SJeff.Bonwick@Sun.COM 
14928241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
14938241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
14948241SJeff.Bonwick@Sun.COM 
1495789Sahrens 	mutex_enter(sm->sm_lock);
1496789Sahrens 	if (!space_map_contains(sm, txg, size))
1497789Sahrens 		space_map_add(sm, txg, size);
1498789Sahrens 	mutex_exit(sm->sm_lock);
1499789Sahrens }
1500789Sahrens 
15018241SJeff.Bonwick@Sun.COM boolean_t
15028241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1503789Sahrens {
15048241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
15058241SJeff.Bonwick@Sun.COM 	boolean_t dirty = B_FALSE;
15068241SJeff.Bonwick@Sun.COM 
15078241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
15088241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1509789Sahrens 
1510789Sahrens 	mutex_enter(sm->sm_lock);
15118241SJeff.Bonwick@Sun.COM 	if (sm->sm_space != 0)
15128241SJeff.Bonwick@Sun.COM 		dirty = space_map_contains(sm, txg, size);
1513789Sahrens 	mutex_exit(sm->sm_lock);
1514789Sahrens 
1515789Sahrens 	return (dirty);
1516789Sahrens }
1517789Sahrens 
15188241SJeff.Bonwick@Sun.COM boolean_t
15198241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
15208241SJeff.Bonwick@Sun.COM {
15218241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
15228241SJeff.Bonwick@Sun.COM 	boolean_t empty;
15238241SJeff.Bonwick@Sun.COM 
15248241SJeff.Bonwick@Sun.COM 	mutex_enter(sm->sm_lock);
15258241SJeff.Bonwick@Sun.COM 	empty = (sm->sm_space == 0);
15268241SJeff.Bonwick@Sun.COM 	mutex_exit(sm->sm_lock);
15278241SJeff.Bonwick@Sun.COM 
15288241SJeff.Bonwick@Sun.COM 	return (empty);
15298241SJeff.Bonwick@Sun.COM }
15308241SJeff.Bonwick@Sun.COM 
1531789Sahrens /*
1532789Sahrens  * Reassess DTLs after a config change or scrub completion.
1533789Sahrens  */
1534789Sahrens void
1535789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1536789Sahrens {
15371544Seschrock 	spa_t *spa = vd->vdev_spa;
15388241SJeff.Bonwick@Sun.COM 	avl_tree_t reftree;
15398241SJeff.Bonwick@Sun.COM 	int minref;
15408241SJeff.Bonwick@Sun.COM 
15418241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
15428241SJeff.Bonwick@Sun.COM 
15438241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
15448241SJeff.Bonwick@Sun.COM 		vdev_dtl_reassess(vd->vdev_child[c], txg,
15458241SJeff.Bonwick@Sun.COM 		    scrub_txg, scrub_done);
15468241SJeff.Bonwick@Sun.COM 
154710594SGeorge.Wilson@Sun.COM 	if (vd == spa->spa_root_vdev || vd->vdev_ishole)
15488241SJeff.Bonwick@Sun.COM 		return;
15498241SJeff.Bonwick@Sun.COM 
15508241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
1551789Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
15527046Sahrens 		if (scrub_txg != 0 &&
15537046Sahrens 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
15547046Sahrens 			/* XXX should check scrub_done? */
15557046Sahrens 			/*
15567046Sahrens 			 * We completed a scrub up to scrub_txg.  If we
15577046Sahrens 			 * did it without rebooting, then the scrub dtl
15587046Sahrens 			 * will be valid, so excise the old region and
15597046Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
15607046Sahrens 			 * dtl as-is if there was an error.
15618241SJeff.Bonwick@Sun.COM 			 *
15628241SJeff.Bonwick@Sun.COM 			 * There's little trick here: to excise the beginning
15638241SJeff.Bonwick@Sun.COM 			 * of the DTL_MISSING map, we put it into a reference
15648241SJeff.Bonwick@Sun.COM 			 * tree and then add a segment with refcnt -1 that
15658241SJeff.Bonwick@Sun.COM 			 * covers the range [0, scrub_txg).  This means
15668241SJeff.Bonwick@Sun.COM 			 * that each txg in that range has refcnt -1 or 0.
15678241SJeff.Bonwick@Sun.COM 			 * We then add DTL_SCRUB with a refcnt of 2, so that
15688241SJeff.Bonwick@Sun.COM 			 * entries in the range [0, scrub_txg) will have a
15698241SJeff.Bonwick@Sun.COM 			 * positive refcnt -- either 1 or 2.  We then convert
15708241SJeff.Bonwick@Sun.COM 			 * the reference tree into the new DTL_MISSING map.
15717046Sahrens 			 */
15728241SJeff.Bonwick@Sun.COM 			space_map_ref_create(&reftree);
15738241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
15748241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
15758241SJeff.Bonwick@Sun.COM 			space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
15768241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
15778241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_SCRUB], 2);
15788241SJeff.Bonwick@Sun.COM 			space_map_ref_generate_map(&reftree,
15798241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
15808241SJeff.Bonwick@Sun.COM 			space_map_ref_destroy(&reftree);
1581789Sahrens 		}
15828241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
15838241SJeff.Bonwick@Sun.COM 		space_map_walk(&vd->vdev_dtl[DTL_MISSING],
15848241SJeff.Bonwick@Sun.COM 		    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1585789Sahrens 		if (scrub_done)
15868241SJeff.Bonwick@Sun.COM 			space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
15878241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
15888241SJeff.Bonwick@Sun.COM 		if (!vdev_readable(vd))
15898241SJeff.Bonwick@Sun.COM 			space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
15908241SJeff.Bonwick@Sun.COM 		else
15918241SJeff.Bonwick@Sun.COM 			space_map_walk(&vd->vdev_dtl[DTL_MISSING],
15928241SJeff.Bonwick@Sun.COM 			    space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1593789Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
15947046Sahrens 
15951732Sbonwick 		if (txg != 0)
15961732Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1597789Sahrens 		return;
1598789Sahrens 	}
1599789Sahrens 
1600789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
16018241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
16028241SJeff.Bonwick@Sun.COM 		if (t == DTL_SCRUB)
16038241SJeff.Bonwick@Sun.COM 			continue;			/* leaf vdevs only */
16048241SJeff.Bonwick@Sun.COM 		if (t == DTL_PARTIAL)
16058241SJeff.Bonwick@Sun.COM 			minref = 1;			/* i.e. non-zero */
16068241SJeff.Bonwick@Sun.COM 		else if (vd->vdev_nparity != 0)
16078241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
16088241SJeff.Bonwick@Sun.COM 		else
16098241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_children;	/* any kind of mirror */
16108241SJeff.Bonwick@Sun.COM 		space_map_ref_create(&reftree);
16118241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
16128241SJeff.Bonwick@Sun.COM 			vdev_t *cvd = vd->vdev_child[c];
16138241SJeff.Bonwick@Sun.COM 			mutex_enter(&cvd->vdev_dtl_lock);
16148241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree, &cvd->vdev_dtl[t], 1);
16158241SJeff.Bonwick@Sun.COM 			mutex_exit(&cvd->vdev_dtl_lock);
16168241SJeff.Bonwick@Sun.COM 		}
16178241SJeff.Bonwick@Sun.COM 		space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
16188241SJeff.Bonwick@Sun.COM 		space_map_ref_destroy(&reftree);
16198241SJeff.Bonwick@Sun.COM 	}
1620789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1621789Sahrens }
1622789Sahrens 
1623789Sahrens static int
1624789Sahrens vdev_dtl_load(vdev_t *vd)
1625789Sahrens {
1626789Sahrens 	spa_t *spa = vd->vdev_spa;
16278241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
16281732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1629789Sahrens 	dmu_buf_t *db;
1630789Sahrens 	int error;
1631789Sahrens 
1632789Sahrens 	ASSERT(vd->vdev_children == 0);
1633789Sahrens 
1634789Sahrens 	if (smo->smo_object == 0)
1635789Sahrens 		return (0);
1636789Sahrens 
163710594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
163810594SGeorge.Wilson@Sun.COM 
16391732Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
16401544Seschrock 		return (error);
16411732Sbonwick 
16424944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
16434944Smaybee 	bcopy(db->db_data, smo, sizeof (*smo));
16441544Seschrock 	dmu_buf_rele(db, FTAG);
1645789Sahrens 
1646789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
16478241SJeff.Bonwick@Sun.COM 	error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
16488241SJeff.Bonwick@Sun.COM 	    NULL, SM_ALLOC, smo, mos);
1649789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1650789Sahrens 
1651789Sahrens 	return (error);
1652789Sahrens }
1653789Sahrens 
1654789Sahrens void
1655789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1656789Sahrens {
1657789Sahrens 	spa_t *spa = vd->vdev_spa;
16588241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
16598241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
16601732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1661789Sahrens 	space_map_t smsync;
1662789Sahrens 	kmutex_t smlock;
1663789Sahrens 	dmu_buf_t *db;
1664789Sahrens 	dmu_tx_t *tx;
1665789Sahrens 
166610594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
166710594SGeorge.Wilson@Sun.COM 
1668789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1669789Sahrens 
1670789Sahrens 	if (vd->vdev_detached) {
1671789Sahrens 		if (smo->smo_object != 0) {
16721732Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1673789Sahrens 			ASSERT3U(err, ==, 0);
1674789Sahrens 			smo->smo_object = 0;
1675789Sahrens 		}
1676789Sahrens 		dmu_tx_commit(tx);
1677789Sahrens 		return;
1678789Sahrens 	}
1679789Sahrens 
1680789Sahrens 	if (smo->smo_object == 0) {
1681789Sahrens 		ASSERT(smo->smo_objsize == 0);
1682789Sahrens 		ASSERT(smo->smo_alloc == 0);
16831732Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1684789Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1685789Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1686789Sahrens 		ASSERT(smo->smo_object != 0);
1687789Sahrens 		vdev_config_dirty(vd->vdev_top);
1688789Sahrens 	}
1689789Sahrens 
1690789Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1691789Sahrens 
1692789Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1693789Sahrens 	    &smlock);
1694789Sahrens 
1695789Sahrens 	mutex_enter(&smlock);
1696789Sahrens 
1697789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
16981732Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1699789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1700789Sahrens 
17011732Sbonwick 	space_map_truncate(smo, mos, tx);
17021732Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1703789Sahrens 
1704789Sahrens 	space_map_destroy(&smsync);
1705789Sahrens 
1706789Sahrens 	mutex_exit(&smlock);
1707789Sahrens 	mutex_destroy(&smlock);
1708789Sahrens 
17091732Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1710789Sahrens 	dmu_buf_will_dirty(db, tx);
17114944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
17124944Smaybee 	bcopy(smo, db->db_data, sizeof (*smo));
17131544Seschrock 	dmu_buf_rele(db, FTAG);
1714789Sahrens 
1715789Sahrens 	dmu_tx_commit(tx);
1716789Sahrens }
1717789Sahrens 
17187046Sahrens /*
17198241SJeff.Bonwick@Sun.COM  * Determine whether the specified vdev can be offlined/detached/removed
17208241SJeff.Bonwick@Sun.COM  * without losing data.
17218241SJeff.Bonwick@Sun.COM  */
17228241SJeff.Bonwick@Sun.COM boolean_t
17238241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd)
17248241SJeff.Bonwick@Sun.COM {
17258241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
17268241SJeff.Bonwick@Sun.COM 	vdev_t *tvd = vd->vdev_top;
17278241SJeff.Bonwick@Sun.COM 	uint8_t cant_read = vd->vdev_cant_read;
17288241SJeff.Bonwick@Sun.COM 	boolean_t required;
17298241SJeff.Bonwick@Sun.COM 
17308241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
17318241SJeff.Bonwick@Sun.COM 
17328241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev || vd == tvd)
17338241SJeff.Bonwick@Sun.COM 		return (B_TRUE);
17348241SJeff.Bonwick@Sun.COM 
17358241SJeff.Bonwick@Sun.COM 	/*
17368241SJeff.Bonwick@Sun.COM 	 * Temporarily mark the device as unreadable, and then determine
17378241SJeff.Bonwick@Sun.COM 	 * whether this results in any DTL outages in the top-level vdev.
17388241SJeff.Bonwick@Sun.COM 	 * If not, we can safely offline/detach/remove the device.
17398241SJeff.Bonwick@Sun.COM 	 */
17408241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = B_TRUE;
17418241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
17428241SJeff.Bonwick@Sun.COM 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
17438241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = cant_read;
17448241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
17458241SJeff.Bonwick@Sun.COM 
17468241SJeff.Bonwick@Sun.COM 	return (required);
17478241SJeff.Bonwick@Sun.COM }
17488241SJeff.Bonwick@Sun.COM 
17498241SJeff.Bonwick@Sun.COM /*
17507046Sahrens  * Determine if resilver is needed, and if so the txg range.
17517046Sahrens  */
17527046Sahrens boolean_t
17537046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
17547046Sahrens {
17557046Sahrens 	boolean_t needed = B_FALSE;
17567046Sahrens 	uint64_t thismin = UINT64_MAX;
17577046Sahrens 	uint64_t thismax = 0;
17587046Sahrens 
17597046Sahrens 	if (vd->vdev_children == 0) {
17607046Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
17618241SJeff.Bonwick@Sun.COM 		if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
17628241SJeff.Bonwick@Sun.COM 		    vdev_writeable(vd)) {
17637046Sahrens 			space_seg_t *ss;
17647046Sahrens 
17658241SJeff.Bonwick@Sun.COM 			ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
17667046Sahrens 			thismin = ss->ss_start - 1;
17678241SJeff.Bonwick@Sun.COM 			ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
17687046Sahrens 			thismax = ss->ss_end;
17697046Sahrens 			needed = B_TRUE;
17707046Sahrens 		}
17717046Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
17727046Sahrens 	} else {
17738241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
17747046Sahrens 			vdev_t *cvd = vd->vdev_child[c];
17757046Sahrens 			uint64_t cmin, cmax;
17767046Sahrens 
17777046Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
17787046Sahrens 				thismin = MIN(thismin, cmin);
17797046Sahrens 				thismax = MAX(thismax, cmax);
17807046Sahrens 				needed = B_TRUE;
17817046Sahrens 			}
17827046Sahrens 		}
17837046Sahrens 	}
17847046Sahrens 
17857046Sahrens 	if (needed && minp) {
17867046Sahrens 		*minp = thismin;
17877046Sahrens 		*maxp = thismax;
17887046Sahrens 	}
17897046Sahrens 	return (needed);
17907046Sahrens }
17917046Sahrens 
17921986Seschrock void
17931544Seschrock vdev_load(vdev_t *vd)
1794789Sahrens {
1795789Sahrens 	/*
1796789Sahrens 	 * Recursively load all children.
1797789Sahrens 	 */
17988241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
17991986Seschrock 		vdev_load(vd->vdev_child[c]);
1800789Sahrens 
1801789Sahrens 	/*
18021585Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1803789Sahrens 	 */
180410594SGeorge.Wilson@Sun.COM 	if (vd == vd->vdev_top && !vd->vdev_ishole &&
18051986Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
18061986Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
18071986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
18081986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1809789Sahrens 
1810789Sahrens 	/*
1811789Sahrens 	 * If this is a leaf vdev, load its DTL.
1812789Sahrens 	 */
18131986Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
18141986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
18151986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1816789Sahrens }
1817789Sahrens 
18182082Seschrock /*
18195450Sbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
18205450Sbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
18215450Sbrendan  * we make sure that we can open the underlying device, then try to read the
18225450Sbrendan  * label, and make sure that the label is sane and that it hasn't been
18235450Sbrendan  * repurposed to another pool.
18242082Seschrock  */
18252082Seschrock int
18265450Sbrendan vdev_validate_aux(vdev_t *vd)
18272082Seschrock {
18282082Seschrock 	nvlist_t *label;
18292082Seschrock 	uint64_t guid, version;
18302082Seschrock 	uint64_t state;
18312082Seschrock 
18327754SJeff.Bonwick@Sun.COM 	if (!vdev_readable(vd))
18336643Seschrock 		return (0);
18346643Seschrock 
18352082Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
18362082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
18372082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
18382082Seschrock 		return (-1);
18392082Seschrock 	}
18402082Seschrock 
18412082Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
18424577Sahrens 	    version > SPA_VERSION ||
18432082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
18442082Seschrock 	    guid != vd->vdev_guid ||
18452082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
18462082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
18472082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
18482082Seschrock 		nvlist_free(label);
18492082Seschrock 		return (-1);
18502082Seschrock 	}
18512082Seschrock 
18522082Seschrock 	/*
18532082Seschrock 	 * We don't actually check the pool state here.  If it's in fact in
18542082Seschrock 	 * use by another pool, we update this fact on the fly when requested.
18552082Seschrock 	 */
18562082Seschrock 	nvlist_free(label);
18572082Seschrock 	return (0);
18582082Seschrock }
18592082Seschrock 
1860789Sahrens void
186110594SGeorge.Wilson@Sun.COM vdev_remove(vdev_t *vd, uint64_t txg)
186210594SGeorge.Wilson@Sun.COM {
186310594SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
186410594SGeorge.Wilson@Sun.COM 	objset_t *mos = spa->spa_meta_objset;
186510594SGeorge.Wilson@Sun.COM 	dmu_tx_t *tx;
186610594SGeorge.Wilson@Sun.COM 
186710594SGeorge.Wilson@Sun.COM 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
186810594SGeorge.Wilson@Sun.COM 
186910594SGeorge.Wilson@Sun.COM 	if (vd->vdev_dtl_smo.smo_object) {
187010594SGeorge.Wilson@Sun.COM 		ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0);
187110594SGeorge.Wilson@Sun.COM 		(void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
187210594SGeorge.Wilson@Sun.COM 		vd->vdev_dtl_smo.smo_object = 0;
187310594SGeorge.Wilson@Sun.COM 	}
187410594SGeorge.Wilson@Sun.COM 
187510594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms != NULL) {
187610594SGeorge.Wilson@Sun.COM 		for (int m = 0; m < vd->vdev_ms_count; m++) {
187710594SGeorge.Wilson@Sun.COM 			metaslab_t *msp = vd->vdev_ms[m];
187810594SGeorge.Wilson@Sun.COM 
187910594SGeorge.Wilson@Sun.COM 			if (msp == NULL || msp->ms_smo.smo_object == 0)
188010594SGeorge.Wilson@Sun.COM 				continue;
188110594SGeorge.Wilson@Sun.COM 
188210594SGeorge.Wilson@Sun.COM 			ASSERT3U(msp->ms_smo.smo_alloc, ==, 0);
188310594SGeorge.Wilson@Sun.COM 			(void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
188410594SGeorge.Wilson@Sun.COM 			msp->ms_smo.smo_object = 0;
188510594SGeorge.Wilson@Sun.COM 		}
188610594SGeorge.Wilson@Sun.COM 	}
188710594SGeorge.Wilson@Sun.COM 
188810594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms_array) {
188910594SGeorge.Wilson@Sun.COM 		(void) dmu_object_free(mos, vd->vdev_ms_array, tx);
189010594SGeorge.Wilson@Sun.COM 		vd->vdev_ms_array = 0;
189110594SGeorge.Wilson@Sun.COM 		vd->vdev_ms_shift = 0;
189210594SGeorge.Wilson@Sun.COM 	}
189310594SGeorge.Wilson@Sun.COM 	dmu_tx_commit(tx);
189410594SGeorge.Wilson@Sun.COM }
189510594SGeorge.Wilson@Sun.COM 
189610594SGeorge.Wilson@Sun.COM void
1897789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
1898789Sahrens {
1899789Sahrens 	metaslab_t *msp;
1900789Sahrens 
190110594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
190210594SGeorge.Wilson@Sun.COM 
1903789Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1904789Sahrens 		metaslab_sync_done(msp, txg);
1905789Sahrens }
1906789Sahrens 
1907789Sahrens void
1908789Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
1909789Sahrens {
1910789Sahrens 	spa_t *spa = vd->vdev_spa;
1911789Sahrens 	vdev_t *lvd;
1912789Sahrens 	metaslab_t *msp;
19131732Sbonwick 	dmu_tx_t *tx;
1914789Sahrens 
191510594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
191610594SGeorge.Wilson@Sun.COM 
19171732Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
19181732Sbonwick 		ASSERT(vd == vd->vdev_top);
19191732Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
19201732Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
19211732Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
19221732Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
19231732Sbonwick 		vdev_config_dirty(vd);
19241732Sbonwick 		dmu_tx_commit(tx);
19251732Sbonwick 	}
1926789Sahrens 
192710594SGeorge.Wilson@Sun.COM 	if (vd->vdev_removing)
192810594SGeorge.Wilson@Sun.COM 		vdev_remove(vd, txg);
192910594SGeorge.Wilson@Sun.COM 
19301732Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1931789Sahrens 		metaslab_sync(msp, txg);
19321732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
19331732Sbonwick 	}
1934789Sahrens 
1935789Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1936789Sahrens 		vdev_dtl_sync(lvd, txg);
1937789Sahrens 
1938789Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1939789Sahrens }
1940789Sahrens 
1941789Sahrens uint64_t
1942789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1943789Sahrens {
1944789Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1945789Sahrens }
1946789Sahrens 
19474451Seschrock /*
19484451Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
19494451Seschrock  * not be opened, and no I/O is attempted.
19504451Seschrock  */
1951789Sahrens int
1952*10817SEric.Schrock@Sun.COM vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
19534451Seschrock {
19546643Seschrock 	vdev_t *vd;
19554451Seschrock 
195610685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
19574451Seschrock 
19586643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
19597754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
19607754SJeff.Bonwick@Sun.COM 
19614451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
19627754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
19634451Seschrock 
19644451Seschrock 	/*
1965*10817SEric.Schrock@Sun.COM 	 * We don't directly use the aux state here, but if we do a
1966*10817SEric.Schrock@Sun.COM 	 * vdev_reopen(), we need this value to be present to remember why we
1967*10817SEric.Schrock@Sun.COM 	 * were faulted.
1968*10817SEric.Schrock@Sun.COM 	 */
1969*10817SEric.Schrock@Sun.COM 	vd->vdev_label_aux = aux;
1970*10817SEric.Schrock@Sun.COM 
1971*10817SEric.Schrock@Sun.COM 	/*
19724451Seschrock 	 * Faulted state takes precedence over degraded.
19734451Seschrock 	 */
19744451Seschrock 	vd->vdev_faulted = 1ULL;
19754451Seschrock 	vd->vdev_degraded = 0ULL;
1976*10817SEric.Schrock@Sun.COM 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
19774451Seschrock 
19784451Seschrock 	/*
19798123SDavid.Marker@sun.com 	 * If marking the vdev as faulted cause the top-level vdev to become
19804451Seschrock 	 * unavailable, then back off and simply mark the vdev as degraded
19814451Seschrock 	 * instead.
19824451Seschrock 	 */
198310685SGeorge.Wilson@Sun.COM 	if (vdev_is_dead(vd->vdev_top) && !vd->vdev_islog &&
198410685SGeorge.Wilson@Sun.COM 	    vd->vdev_aux == NULL) {
19854451Seschrock 		vd->vdev_degraded = 1ULL;
19864451Seschrock 		vd->vdev_faulted = 0ULL;
19874451Seschrock 
19884451Seschrock 		/*
19894451Seschrock 		 * If we reopen the device and it's not dead, only then do we
19904451Seschrock 		 * mark it degraded.
19914451Seschrock 		 */
19924451Seschrock 		vdev_reopen(vd);
19934451Seschrock 
1994*10817SEric.Schrock@Sun.COM 		if (vdev_readable(vd))
1995*10817SEric.Schrock@Sun.COM 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
19964451Seschrock 	}
19974451Seschrock 
19987754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
19994451Seschrock }
20004451Seschrock 
20014451Seschrock /*
20024451Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
20034451Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
20044451Seschrock  * as I/O is concerned.
20054451Seschrock  */
20064451Seschrock int
2007*10817SEric.Schrock@Sun.COM vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
20084451Seschrock {
20096643Seschrock 	vdev_t *vd;
20104451Seschrock 
201110685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
20124451Seschrock 
20136643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
20147754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
20157754SJeff.Bonwick@Sun.COM 
20164451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
20177754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
20184451Seschrock 
20194451Seschrock 	/*
20204451Seschrock 	 * If the vdev is already faulted, then don't do anything.
20214451Seschrock 	 */
20227754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded)
20237754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, 0));
20244451Seschrock 
20254451Seschrock 	vd->vdev_degraded = 1ULL;
20264451Seschrock 	if (!vdev_is_dead(vd))
20274451Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2028*10817SEric.Schrock@Sun.COM 		    aux);
20294451Seschrock 
20307754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
20314451Seschrock }
20324451Seschrock 
20334451Seschrock /*
20344451Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
20354451Seschrock  * any attached spare device should be detached when the device finishes
20364451Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
20374451Seschrock  * so no FMA events are generated if the device fails to open.
20384451Seschrock  */
20394451Seschrock int
20407754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2041789Sahrens {
20429816SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2043789Sahrens 
204410685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
20451485Slling 
20466643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
20477754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2048789Sahrens 
20491585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
20507754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
20511585Sbonwick 
20529816SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
2053789Sahrens 	vd->vdev_offline = B_FALSE;
20541485Slling 	vd->vdev_tmpoffline = B_FALSE;
20557754SJeff.Bonwick@Sun.COM 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
20567754SJeff.Bonwick@Sun.COM 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
20579816SGeorge.Wilson@Sun.COM 
20589816SGeorge.Wilson@Sun.COM 	/* XXX - L2ARC 1.0 does not support expansion */
20599816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
20609816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
20619816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
20629816SGeorge.Wilson@Sun.COM 	}
20639816SGeorge.Wilson@Sun.COM 
20649816SGeorge.Wilson@Sun.COM 	vdev_reopen(tvd);
20654451Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
20664451Seschrock 
20679816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
20689816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
20699816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = B_FALSE;
20709816SGeorge.Wilson@Sun.COM 	}
20719816SGeorge.Wilson@Sun.COM 
20724451Seschrock 	if (newstate)
20734451Seschrock 		*newstate = vd->vdev_state;
20744451Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
20754451Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
20764451Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
20774451Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
20784451Seschrock 		vd->vdev_unspare = B_TRUE;
2079789Sahrens 
20809816SGeorge.Wilson@Sun.COM 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
20819816SGeorge.Wilson@Sun.COM 
20829816SGeorge.Wilson@Sun.COM 		/* XXX - L2ARC 1.0 does not support expansion */
20839816SGeorge.Wilson@Sun.COM 		if (vd->vdev_aux)
20849816SGeorge.Wilson@Sun.COM 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
20859816SGeorge.Wilson@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
20869816SGeorge.Wilson@Sun.COM 	}
20878241SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
2088789Sahrens }
2089789Sahrens 
2090789Sahrens int
209110685SGeorge.Wilson@Sun.COM vdev_offline_log(spa_t *spa)
209210685SGeorge.Wilson@Sun.COM {
209310685SGeorge.Wilson@Sun.COM 	int error = 0;
209410685SGeorge.Wilson@Sun.COM 
209510685SGeorge.Wilson@Sun.COM 	if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
209610685SGeorge.Wilson@Sun.COM 	    NULL, DS_FIND_CHILDREN)) == 0) {
209710685SGeorge.Wilson@Sun.COM 
209810685SGeorge.Wilson@Sun.COM 		/*
209910685SGeorge.Wilson@Sun.COM 		 * We successfully offlined the log device, sync out the
210010685SGeorge.Wilson@Sun.COM 		 * current txg so that the "stubby" block can be removed
210110685SGeorge.Wilson@Sun.COM 		 * by zil_sync().
210210685SGeorge.Wilson@Sun.COM 		 */
210310685SGeorge.Wilson@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, 0);
210410685SGeorge.Wilson@Sun.COM 	}
210510685SGeorge.Wilson@Sun.COM 	return (error);
210610685SGeorge.Wilson@Sun.COM }
210710685SGeorge.Wilson@Sun.COM 
210810685SGeorge.Wilson@Sun.COM int
21094451Seschrock vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2110789Sahrens {
21119701SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd;
211210685SGeorge.Wilson@Sun.COM 	int error = 0;
211310685SGeorge.Wilson@Sun.COM 	uint64_t generation;
211410685SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg;
211510685SGeorge.Wilson@Sun.COM 
211610685SGeorge.Wilson@Sun.COM top:
211710685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_ALLOC);
2118789Sahrens 
21196643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
21207754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2121789Sahrens 
21221585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
21237754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
21241585Sbonwick 
21259701SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
212610685SGeorge.Wilson@Sun.COM 	mg = tvd->vdev_mg;
212710685SGeorge.Wilson@Sun.COM 	generation = spa->spa_config_generation + 1;
21289701SGeorge.Wilson@Sun.COM 
2129789Sahrens 	/*
21301732Sbonwick 	 * If the device isn't already offline, try to offline it.
2131789Sahrens 	 */
21321732Sbonwick 	if (!vd->vdev_offline) {
21331732Sbonwick 		/*
21348241SJeff.Bonwick@Sun.COM 		 * If this device has the only valid copy of some data,
21359701SGeorge.Wilson@Sun.COM 		 * don't allow it to be offlined. Log devices are always
21369701SGeorge.Wilson@Sun.COM 		 * expendable.
21371732Sbonwick 		 */
21389701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
21399701SGeorge.Wilson@Sun.COM 		    vdev_dtl_required(vd))
21407754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2141789Sahrens 
21421732Sbonwick 		/*
214310685SGeorge.Wilson@Sun.COM 		 * If the top-level is a slog and it's had allocations
214410685SGeorge.Wilson@Sun.COM 		 * then proceed. We check that the vdev's metaslab
214510685SGeorge.Wilson@Sun.COM 		 * grop is not NULL since it's possible that we may
214610685SGeorge.Wilson@Sun.COM 		 * have just added this vdev and have not yet initialized
214710685SGeorge.Wilson@Sun.COM 		 * it's metaslabs.
214810685SGeorge.Wilson@Sun.COM 		 */
214910685SGeorge.Wilson@Sun.COM 		if (tvd->vdev_islog && mg != NULL) {
215010685SGeorge.Wilson@Sun.COM 			/*
215110685SGeorge.Wilson@Sun.COM 			 * Prevent any future allocations.
215210685SGeorge.Wilson@Sun.COM 			 */
215310685SGeorge.Wilson@Sun.COM 			metaslab_class_remove(spa->spa_log_class, mg);
215410685SGeorge.Wilson@Sun.COM 			(void) spa_vdev_state_exit(spa, vd, 0);
215510685SGeorge.Wilson@Sun.COM 
215610685SGeorge.Wilson@Sun.COM 			error = vdev_offline_log(spa);
215710685SGeorge.Wilson@Sun.COM 
215810685SGeorge.Wilson@Sun.COM 			spa_vdev_state_enter(spa, SCL_ALLOC);
215910685SGeorge.Wilson@Sun.COM 
216010685SGeorge.Wilson@Sun.COM 			/*
216110685SGeorge.Wilson@Sun.COM 			 * Check to see if the config has changed.
216210685SGeorge.Wilson@Sun.COM 			 */
216310685SGeorge.Wilson@Sun.COM 			if (error || generation != spa->spa_config_generation) {
216410685SGeorge.Wilson@Sun.COM 				metaslab_class_add(spa->spa_log_class, mg);
216510685SGeorge.Wilson@Sun.COM 				if (error)
216610685SGeorge.Wilson@Sun.COM 					return (spa_vdev_state_exit(spa,
216710685SGeorge.Wilson@Sun.COM 					    vd, error));
216810685SGeorge.Wilson@Sun.COM 				(void) spa_vdev_state_exit(spa, vd, 0);
216910685SGeorge.Wilson@Sun.COM 				goto top;
217010685SGeorge.Wilson@Sun.COM 			}
217110685SGeorge.Wilson@Sun.COM 			ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0);
217210685SGeorge.Wilson@Sun.COM 		}
217310685SGeorge.Wilson@Sun.COM 
217410685SGeorge.Wilson@Sun.COM 		/*
21751732Sbonwick 		 * Offline this device and reopen its top-level vdev.
21769701SGeorge.Wilson@Sun.COM 		 * If the top-level vdev is a log device then just offline
21779701SGeorge.Wilson@Sun.COM 		 * it. Otherwise, if this action results in the top-level
21789701SGeorge.Wilson@Sun.COM 		 * vdev becoming unusable, undo it and fail the request.
21791732Sbonwick 		 */
21801732Sbonwick 		vd->vdev_offline = B_TRUE;
21819701SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
21829701SGeorge.Wilson@Sun.COM 
21839701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
21849701SGeorge.Wilson@Sun.COM 		    vdev_is_dead(tvd)) {
21851732Sbonwick 			vd->vdev_offline = B_FALSE;
21869701SGeorge.Wilson@Sun.COM 			vdev_reopen(tvd);
21877754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
21881732Sbonwick 		}
218910685SGeorge.Wilson@Sun.COM 
219010685SGeorge.Wilson@Sun.COM 		/*
219110685SGeorge.Wilson@Sun.COM 		 * Add the device back into the metaslab rotor so that
219210685SGeorge.Wilson@Sun.COM 		 * once we online the device it's open for business.
219310685SGeorge.Wilson@Sun.COM 		 */
219410685SGeorge.Wilson@Sun.COM 		if (tvd->vdev_islog && mg != NULL)
219510685SGeorge.Wilson@Sun.COM 			metaslab_class_add(spa->spa_log_class, mg);
2196789Sahrens 	}
2197789Sahrens 
21987754SJeff.Bonwick@Sun.COM 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
21991732Sbonwick 
220010685SGeorge.Wilson@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
2201789Sahrens }
2202789Sahrens 
22031544Seschrock /*
22041544Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
22051544Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
22061544Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
22071544Seschrock  */
22081544Seschrock void
22097754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd)
2210789Sahrens {
22117754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
22127754SJeff.Bonwick@Sun.COM 
22137754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2214789Sahrens 
22151544Seschrock 	if (vd == NULL)
22167754SJeff.Bonwick@Sun.COM 		vd = rvd;
2217789Sahrens 
22181544Seschrock 	vd->vdev_stat.vs_read_errors = 0;
22191544Seschrock 	vd->vdev_stat.vs_write_errors = 0;
22201544Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2221789Sahrens 
22227754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
22237754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd->vdev_child[c]);
22244451Seschrock 
22254451Seschrock 	/*
22266959Sek110237 	 * If we're in the FAULTED state or have experienced failed I/O, then
22276959Sek110237 	 * clear the persistent state and attempt to reopen the device.  We
22286959Sek110237 	 * also mark the vdev config dirty, so that the new faulted state is
22296959Sek110237 	 * written out to disk.
22304451Seschrock 	 */
22317754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded ||
22327754SJeff.Bonwick@Sun.COM 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
22336959Sek110237 
22344451Seschrock 		vd->vdev_faulted = vd->vdev_degraded = 0;
22357754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read = B_FALSE;
22367754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write = B_FALSE;
22377754SJeff.Bonwick@Sun.COM 
22384451Seschrock 		vdev_reopen(vd);
22394451Seschrock 
22407754SJeff.Bonwick@Sun.COM 		if (vd != rvd)
22417754SJeff.Bonwick@Sun.COM 			vdev_state_dirty(vd->vdev_top);
22427754SJeff.Bonwick@Sun.COM 
22437754SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
22444808Sek110237 			spa_async_request(spa, SPA_ASYNC_RESILVER);
22454451Seschrock 
22464451Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
22474451Seschrock 	}
2248789Sahrens }
2249789Sahrens 
22507754SJeff.Bonwick@Sun.COM boolean_t
22517754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd)
22525329Sgw25295 {
225310594SGeorge.Wilson@Sun.COM 	/*
225410594SGeorge.Wilson@Sun.COM 	 * Holes and missing devices are always considered "dead".
225510594SGeorge.Wilson@Sun.COM 	 * This simplifies the code since we don't have to check for
225610594SGeorge.Wilson@Sun.COM 	 * these types of devices in the various code paths.
225710594SGeorge.Wilson@Sun.COM 	 * Instead we rely on the fact that we skip over dead devices
225810594SGeorge.Wilson@Sun.COM 	 * before issuing I/O to them.
225910594SGeorge.Wilson@Sun.COM 	 */
226010594SGeorge.Wilson@Sun.COM 	return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
226110594SGeorge.Wilson@Sun.COM 	    vd->vdev_ops == &vdev_missing_ops);
22625329Sgw25295 }
22635329Sgw25295 
22647754SJeff.Bonwick@Sun.COM boolean_t
22657754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd)
2266789Sahrens {
22677754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2268789Sahrens }
2269789Sahrens 
22707754SJeff.Bonwick@Sun.COM boolean_t
22717754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd)
2272789Sahrens {
22737754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
22747754SJeff.Bonwick@Sun.COM }
2275789Sahrens 
22767754SJeff.Bonwick@Sun.COM boolean_t
22777980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd)
22787980SGeorge.Wilson@Sun.COM {
22798241SJeff.Bonwick@Sun.COM 	uint64_t state = vd->vdev_state;
22808241SJeff.Bonwick@Sun.COM 
22817980SGeorge.Wilson@Sun.COM 	/*
22828241SJeff.Bonwick@Sun.COM 	 * We currently allow allocations from vdevs which may be in the
22837980SGeorge.Wilson@Sun.COM 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
22847980SGeorge.Wilson@Sun.COM 	 * fails to reopen then we'll catch it later when we're holding
22858241SJeff.Bonwick@Sun.COM 	 * the proper locks.  Note that we have to get the vdev state
22868241SJeff.Bonwick@Sun.COM 	 * in a local variable because although it changes atomically,
22878241SJeff.Bonwick@Sun.COM 	 * we're asking two separate questions about it.
22887980SGeorge.Wilson@Sun.COM 	 */
22898241SJeff.Bonwick@Sun.COM 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
229010594SGeorge.Wilson@Sun.COM 	    !vd->vdev_cant_write && !vd->vdev_ishole && !vd->vdev_removing);
22917980SGeorge.Wilson@Sun.COM }
22927980SGeorge.Wilson@Sun.COM 
22937980SGeorge.Wilson@Sun.COM boolean_t
22947754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio)
22957754SJeff.Bonwick@Sun.COM {
22967754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_vd == vd);
2297789Sahrens 
22987754SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
22997754SJeff.Bonwick@Sun.COM 		return (B_FALSE);
2300789Sahrens 
23017754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ)
23027754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_read);
2303789Sahrens 
23047754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_WRITE)
23057754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_write);
23067754SJeff.Bonwick@Sun.COM 
23077754SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2308789Sahrens }
2309789Sahrens 
2310789Sahrens /*
2311789Sahrens  * Get statistics for the given vdev.
2312789Sahrens  */
2313789Sahrens void
2314789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2315789Sahrens {
2316789Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2317789Sahrens 
2318789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2319789Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
23207046Sahrens 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
2321789Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2322789Sahrens 	vs->vs_state = vd->vdev_state;
23239816SGeorge.Wilson@Sun.COM 	vs->vs_rsize = vdev_get_min_asize(vd);
23249816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
23259816SGeorge.Wilson@Sun.COM 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2326789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2327789Sahrens 
2328789Sahrens 	/*
2329789Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2330789Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2331789Sahrens 	 */
2332789Sahrens 	if (vd == rvd) {
23337754SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
2334789Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2335789Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2336789Sahrens 
2337789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
23387754SJeff.Bonwick@Sun.COM 			for (int t = 0; t < ZIO_TYPES; t++) {
2339789Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2340789Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2341789Sahrens 			}
2342789Sahrens 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
2343789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
2344789Sahrens 		}
2345789Sahrens 	}
2346789Sahrens }
2347789Sahrens 
2348789Sahrens void
23495450Sbrendan vdev_clear_stats(vdev_t *vd)
23505450Sbrendan {
23515450Sbrendan 	mutex_enter(&vd->vdev_stat_lock);
23525450Sbrendan 	vd->vdev_stat.vs_space = 0;
23535450Sbrendan 	vd->vdev_stat.vs_dspace = 0;
23545450Sbrendan 	vd->vdev_stat.vs_alloc = 0;
23555450Sbrendan 	mutex_exit(&vd->vdev_stat_lock);
23565450Sbrendan }
23575450Sbrendan 
23585450Sbrendan void
23597754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize)
2360789Sahrens {
23618241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
23628241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
23637754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2364789Sahrens 	vdev_t *pvd;
2365789Sahrens 	uint64_t txg = zio->io_txg;
2366789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2367789Sahrens 	zio_type_t type = zio->io_type;
2368789Sahrens 	int flags = zio->io_flags;
2369789Sahrens 
23707754SJeff.Bonwick@Sun.COM 	/*
23717754SJeff.Bonwick@Sun.COM 	 * If this i/o is a gang leader, it didn't do any actual work.
23727754SJeff.Bonwick@Sun.COM 	 */
23737754SJeff.Bonwick@Sun.COM 	if (zio->io_gang_tree)
23747754SJeff.Bonwick@Sun.COM 		return;
23757754SJeff.Bonwick@Sun.COM 
2376789Sahrens 	if (zio->io_error == 0) {
23777754SJeff.Bonwick@Sun.COM 		/*
23787754SJeff.Bonwick@Sun.COM 		 * If this is a root i/o, don't count it -- we've already
23797754SJeff.Bonwick@Sun.COM 		 * counted the top-level vdevs, and vdev_get_stats() will
23807754SJeff.Bonwick@Sun.COM 		 * aggregate them when asked.  This reduces contention on
23817754SJeff.Bonwick@Sun.COM 		 * the root vdev_stat_lock and implicitly handles blocks
23827754SJeff.Bonwick@Sun.COM 		 * that compress away to holes, for which there is no i/o.
23837754SJeff.Bonwick@Sun.COM 		 * (Holes never create vdev children, so all the counters
23847754SJeff.Bonwick@Sun.COM 		 * remain zero, which is what we want.)
23857754SJeff.Bonwick@Sun.COM 		 *
23867754SJeff.Bonwick@Sun.COM 		 * Note: this only applies to successful i/o (io_error == 0)
23877754SJeff.Bonwick@Sun.COM 		 * because unlike i/o counts, errors are not additive.
23887754SJeff.Bonwick@Sun.COM 		 * When reading a ditto block, for example, failure of
23897754SJeff.Bonwick@Sun.COM 		 * one top-level vdev does not imply a root-level error.
23907754SJeff.Bonwick@Sun.COM 		 */
23917754SJeff.Bonwick@Sun.COM 		if (vd == rvd)
23927754SJeff.Bonwick@Sun.COM 			return;
23937754SJeff.Bonwick@Sun.COM 
23947754SJeff.Bonwick@Sun.COM 		ASSERT(vd == zio->io_vd);
23958241SJeff.Bonwick@Sun.COM 
23968241SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_BYPASS)
23978241SJeff.Bonwick@Sun.COM 			return;
23988241SJeff.Bonwick@Sun.COM 
23998241SJeff.Bonwick@Sun.COM 		mutex_enter(&vd->vdev_stat_lock);
24008241SJeff.Bonwick@Sun.COM 
24017754SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_REPAIR) {
24021807Sbonwick 			if (flags & ZIO_FLAG_SCRUB_THREAD)
24037754SJeff.Bonwick@Sun.COM 				vs->vs_scrub_repaired += psize;
24048241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SELF_HEAL)
24057754SJeff.Bonwick@Sun.COM 				vs->vs_self_healed += psize;
2406789Sahrens 		}
24078241SJeff.Bonwick@Sun.COM 
24088241SJeff.Bonwick@Sun.COM 		vs->vs_ops[type]++;
24098241SJeff.Bonwick@Sun.COM 		vs->vs_bytes[type] += psize;
24108241SJeff.Bonwick@Sun.COM 
24118241SJeff.Bonwick@Sun.COM 		mutex_exit(&vd->vdev_stat_lock);
2412789Sahrens 		return;
2413789Sahrens 	}
2414789Sahrens 
2415789Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2416789Sahrens 		return;
2417789Sahrens 
24189725SEric.Schrock@Sun.COM 	/*
24199725SEric.Schrock@Sun.COM 	 * If this is an I/O error that is going to be retried, then ignore the
24209725SEric.Schrock@Sun.COM 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
24219725SEric.Schrock@Sun.COM 	 * hard errors, when in reality they can happen for any number of
24229725SEric.Schrock@Sun.COM 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
24239725SEric.Schrock@Sun.COM 	 */
24249725SEric.Schrock@Sun.COM 	if (zio->io_error == EIO &&
24259725SEric.Schrock@Sun.COM 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
24269725SEric.Schrock@Sun.COM 		return;
24279725SEric.Schrock@Sun.COM 
242810685SGeorge.Wilson@Sun.COM 	/*
242910685SGeorge.Wilson@Sun.COM 	 * Intent logs writes won't propagate their error to the root
243010685SGeorge.Wilson@Sun.COM 	 * I/O so don't mark these types of failures as pool-level
243110685SGeorge.Wilson@Sun.COM 	 * errors.
243210685SGeorge.Wilson@Sun.COM 	 */
243310685SGeorge.Wilson@Sun.COM 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
243410685SGeorge.Wilson@Sun.COM 		return;
243510685SGeorge.Wilson@Sun.COM 
24367754SJeff.Bonwick@Sun.COM 	mutex_enter(&vd->vdev_stat_lock);
24379230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
24387754SJeff.Bonwick@Sun.COM 		if (zio->io_error == ECKSUM)
24397754SJeff.Bonwick@Sun.COM 			vs->vs_checksum_errors++;
24407754SJeff.Bonwick@Sun.COM 		else
24417754SJeff.Bonwick@Sun.COM 			vs->vs_read_errors++;
2442789Sahrens 	}
24439230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
24447754SJeff.Bonwick@Sun.COM 		vs->vs_write_errors++;
24457754SJeff.Bonwick@Sun.COM 	mutex_exit(&vd->vdev_stat_lock);
2446789Sahrens 
24478241SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
24488241SJeff.Bonwick@Sun.COM 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
24498241SJeff.Bonwick@Sun.COM 	    (flags & ZIO_FLAG_SCRUB_THREAD))) {
24508241SJeff.Bonwick@Sun.COM 		/*
24518241SJeff.Bonwick@Sun.COM 		 * This is either a normal write (not a repair), or it's a
24528241SJeff.Bonwick@Sun.COM 		 * repair induced by the scrub thread.  In the normal case,
24538241SJeff.Bonwick@Sun.COM 		 * we commit the DTL change in the same txg as the block
24548241SJeff.Bonwick@Sun.COM 		 * was born.  In the scrub-induced repair case, we know that
24558241SJeff.Bonwick@Sun.COM 		 * scrubs run in first-pass syncing context, so we commit
24568241SJeff.Bonwick@Sun.COM 		 * the DTL change in spa->spa_syncing_txg.
24578241SJeff.Bonwick@Sun.COM 		 *
24588241SJeff.Bonwick@Sun.COM 		 * We currently do not make DTL entries for failed spontaneous
24598241SJeff.Bonwick@Sun.COM 		 * self-healing writes triggered by normal (non-scrubbing)
24608241SJeff.Bonwick@Sun.COM 		 * reads, because we have no transactional context in which to
24618241SJeff.Bonwick@Sun.COM 		 * do so -- and it's not clear that it'd be desirable anyway.
24628241SJeff.Bonwick@Sun.COM 		 */
24638241SJeff.Bonwick@Sun.COM 		if (vd->vdev_ops->vdev_op_leaf) {
24648241SJeff.Bonwick@Sun.COM 			uint64_t commit_txg = txg;
24658241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SCRUB_THREAD) {
24668241SJeff.Bonwick@Sun.COM 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
24678241SJeff.Bonwick@Sun.COM 				ASSERT(spa_sync_pass(spa) == 1);
24688241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
24698241SJeff.Bonwick@Sun.COM 				commit_txg = spa->spa_syncing_txg;
24708241SJeff.Bonwick@Sun.COM 			}
24718241SJeff.Bonwick@Sun.COM 			ASSERT(commit_txg >= spa->spa_syncing_txg);
24728241SJeff.Bonwick@Sun.COM 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
24738241SJeff.Bonwick@Sun.COM 				return;
24748241SJeff.Bonwick@Sun.COM 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
24758241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
24768241SJeff.Bonwick@Sun.COM 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2477789Sahrens 		}
24788241SJeff.Bonwick@Sun.COM 		if (vd != rvd)
24798241SJeff.Bonwick@Sun.COM 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2480789Sahrens 	}
2481789Sahrens }
2482789Sahrens 
2483789Sahrens void
2484789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2485789Sahrens {
2486789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2487789Sahrens 
24889816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
2489789Sahrens 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2490789Sahrens 
2491789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2492789Sahrens 
2493789Sahrens 	if (type == POOL_SCRUB_NONE) {
2494789Sahrens 		/*
2495789Sahrens 		 * Update completion and end time.  Leave everything else alone
2496789Sahrens 		 * so we can report what happened during the previous scrub.
2497789Sahrens 		 */
2498789Sahrens 		vs->vs_scrub_complete = complete;
2499789Sahrens 		vs->vs_scrub_end = gethrestime_sec();
2500789Sahrens 	} else {
2501789Sahrens 		vs->vs_scrub_type = type;
2502789Sahrens 		vs->vs_scrub_complete = 0;
2503789Sahrens 		vs->vs_scrub_examined = 0;
2504789Sahrens 		vs->vs_scrub_repaired = 0;
2505789Sahrens 		vs->vs_scrub_start = gethrestime_sec();
2506789Sahrens 		vs->vs_scrub_end = 0;
2507789Sahrens 	}
2508789Sahrens 
2509789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2510789Sahrens }
2511789Sahrens 
2512789Sahrens /*
2513789Sahrens  * Update the in-core space usage stats for this vdev and the root vdev.
2514789Sahrens  */
2515789Sahrens void
25165450Sbrendan vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
25175450Sbrendan     boolean_t update_root)
2518789Sahrens {
25194527Sperrin 	int64_t dspace_delta = space_delta;
25204527Sperrin 	spa_t *spa = vd->vdev_spa;
25214527Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
25224527Sperrin 
2523789Sahrens 	ASSERT(vd == vd->vdev_top);
25244527Sperrin 
25254527Sperrin 	/*
25264527Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
25274527Sperrin 	 * factor.  We must calculate this here and not at the root vdev
25284527Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
25294527Sperrin 	 * childrens', thus not accurate enough for us.
25304527Sperrin 	 */
25314527Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
25329701SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
25334527Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
25344527Sperrin 	    vd->vdev_deflate_ratio;
2535789Sahrens 
25364527Sperrin 	mutex_enter(&vd->vdev_stat_lock);
25374527Sperrin 	vd->vdev_stat.vs_space += space_delta;
25384527Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
25394527Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
25404527Sperrin 	mutex_exit(&vd->vdev_stat_lock);
25412082Seschrock 
25425450Sbrendan 	if (update_root) {
25435450Sbrendan 		ASSERT(rvd == vd->vdev_parent);
25445450Sbrendan 		ASSERT(vd->vdev_ms_count != 0);
25454527Sperrin 
25465450Sbrendan 		/*
25475450Sbrendan 		 * Don't count non-normal (e.g. intent log) space as part of
25485450Sbrendan 		 * the pool's capacity.
25495450Sbrendan 		 */
255010594SGeorge.Wilson@Sun.COM 		if (vd->vdev_islog)
25515450Sbrendan 			return;
25525450Sbrendan 
25535450Sbrendan 		mutex_enter(&rvd->vdev_stat_lock);
25545450Sbrendan 		rvd->vdev_stat.vs_space += space_delta;
25555450Sbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
25565450Sbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
25575450Sbrendan 		mutex_exit(&rvd->vdev_stat_lock);
25585450Sbrendan 	}
2559789Sahrens }
2560789Sahrens 
2561789Sahrens /*
2562789Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2563789Sahrens  * so that it will be written out next time the vdev configuration is synced.
2564789Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2565789Sahrens  */
2566789Sahrens void
2567789Sahrens vdev_config_dirty(vdev_t *vd)
2568789Sahrens {
2569789Sahrens 	spa_t *spa = vd->vdev_spa;
2570789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2571789Sahrens 	int c;
2572789Sahrens 
25731601Sbonwick 	/*
25749425SEric.Schrock@Sun.COM 	 * If this is an aux vdev (as with l2cache and spare devices), then we
25759425SEric.Schrock@Sun.COM 	 * update the vdev config manually and set the sync flag.
25766643Seschrock 	 */
25776643Seschrock 	if (vd->vdev_aux != NULL) {
25786643Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
25796643Seschrock 		nvlist_t **aux;
25806643Seschrock 		uint_t naux;
25816643Seschrock 
25826643Seschrock 		for (c = 0; c < sav->sav_count; c++) {
25836643Seschrock 			if (sav->sav_vdevs[c] == vd)
25846643Seschrock 				break;
25856643Seschrock 		}
25866643Seschrock 
25877754SJeff.Bonwick@Sun.COM 		if (c == sav->sav_count) {
25887754SJeff.Bonwick@Sun.COM 			/*
25897754SJeff.Bonwick@Sun.COM 			 * We're being removed.  There's nothing more to do.
25907754SJeff.Bonwick@Sun.COM 			 */
25917754SJeff.Bonwick@Sun.COM 			ASSERT(sav->sav_sync == B_TRUE);
25927754SJeff.Bonwick@Sun.COM 			return;
25937754SJeff.Bonwick@Sun.COM 		}
25947754SJeff.Bonwick@Sun.COM 
25956643Seschrock 		sav->sav_sync = B_TRUE;
25966643Seschrock 
25979425SEric.Schrock@Sun.COM 		if (nvlist_lookup_nvlist_array(sav->sav_config,
25989425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
25999425SEric.Schrock@Sun.COM 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
26009425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
26019425SEric.Schrock@Sun.COM 		}
26026643Seschrock 
26036643Seschrock 		ASSERT(c < naux);
26046643Seschrock 
26056643Seschrock 		/*
26066643Seschrock 		 * Setting the nvlist in the middle if the array is a little
26076643Seschrock 		 * sketchy, but it will work.
26086643Seschrock 		 */
26096643Seschrock 		nvlist_free(aux[c]);
26106643Seschrock 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
26116643Seschrock 
26126643Seschrock 		return;
26136643Seschrock 	}
26146643Seschrock 
26156643Seschrock 	/*
26167754SJeff.Bonwick@Sun.COM 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
26177754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
26187754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
26191601Sbonwick 	 * so this is sufficient to ensure mutual exclusion.
26201601Sbonwick 	 */
26217754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
26227754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
26237754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
26241601Sbonwick 
2625789Sahrens 	if (vd == rvd) {
2626789Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2627789Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2628789Sahrens 	} else {
2629789Sahrens 		ASSERT(vd == vd->vdev_top);
2630789Sahrens 
263110594SGeorge.Wilson@Sun.COM 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
263210594SGeorge.Wilson@Sun.COM 		    !vd->vdev_ishole)
26337754SJeff.Bonwick@Sun.COM 			list_insert_head(&spa->spa_config_dirty_list, vd);
2634789Sahrens 	}
2635789Sahrens }
2636789Sahrens 
2637789Sahrens void
2638789Sahrens vdev_config_clean(vdev_t *vd)
2639789Sahrens {
26401601Sbonwick 	spa_t *spa = vd->vdev_spa;
26411601Sbonwick 
26427754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
26437754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
26447754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
26457754SJeff.Bonwick@Sun.COM 
26467754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
26477754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_config_dirty_list, vd);
26487754SJeff.Bonwick@Sun.COM }
26497754SJeff.Bonwick@Sun.COM 
26507754SJeff.Bonwick@Sun.COM /*
26517754SJeff.Bonwick@Sun.COM  * Mark a top-level vdev's state as dirty, so that the next pass of
26527754SJeff.Bonwick@Sun.COM  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
26537754SJeff.Bonwick@Sun.COM  * the state changes from larger config changes because they require
26547754SJeff.Bonwick@Sun.COM  * much less locking, and are often needed for administrative actions.
26557754SJeff.Bonwick@Sun.COM  */
26567754SJeff.Bonwick@Sun.COM void
26577754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd)
26587754SJeff.Bonwick@Sun.COM {
26597754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
26607754SJeff.Bonwick@Sun.COM 
26617754SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
26621601Sbonwick 
26637754SJeff.Bonwick@Sun.COM 	/*
26647754SJeff.Bonwick@Sun.COM 	 * The state list is protected by the SCL_STATE lock.  The caller
26657754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_STATE as writer, or must be the sync thread
26667754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
26677754SJeff.Bonwick@Sun.COM 	 * so this is sufficient to ensure mutual exclusion.
26687754SJeff.Bonwick@Sun.COM 	 */
26697754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
26707754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
26717754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
26727754SJeff.Bonwick@Sun.COM 
26737754SJeff.Bonwick@Sun.COM 	if (!list_link_active(&vd->vdev_state_dirty_node))
26747754SJeff.Bonwick@Sun.COM 		list_insert_head(&spa->spa_state_dirty_list, vd);
26757754SJeff.Bonwick@Sun.COM }
26767754SJeff.Bonwick@Sun.COM 
26777754SJeff.Bonwick@Sun.COM void
26787754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd)
26797754SJeff.Bonwick@Sun.COM {
26807754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
26817754SJeff.Bonwick@Sun.COM 
26827754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
26837754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
26847754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
26857754SJeff.Bonwick@Sun.COM 
26867754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
26877754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_state_dirty_list, vd);
2688789Sahrens }
2689789Sahrens 
26906523Sek110237 /*
26916523Sek110237  * Propagate vdev state up from children to parent.
26926523Sek110237  */
26931775Sbillm void
26941775Sbillm vdev_propagate_state(vdev_t *vd)
26951775Sbillm {
26968241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
26978241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
26981775Sbillm 	int degraded = 0, faulted = 0;
26991775Sbillm 	int corrupted = 0;
27001775Sbillm 	vdev_t *child;
27011775Sbillm 
27024451Seschrock 	if (vd->vdev_children > 0) {
27039816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
27044451Seschrock 			child = vd->vdev_child[c];
27056976Seschrock 
270610594SGeorge.Wilson@Sun.COM 			/*
270710594SGeorge.Wilson@Sun.COM 			 * Don't factor holes into the decision.
270810594SGeorge.Wilson@Sun.COM 			 */
270910594SGeorge.Wilson@Sun.COM 			if (child->vdev_ishole)
271010594SGeorge.Wilson@Sun.COM 				continue;
271110594SGeorge.Wilson@Sun.COM 
27127754SJeff.Bonwick@Sun.COM 			if (!vdev_readable(child) ||
27138241SJeff.Bonwick@Sun.COM 			    (!vdev_writeable(child) && spa_writeable(spa))) {
27146976Seschrock 				/*
27156976Seschrock 				 * Root special: if there is a top-level log
27166976Seschrock 				 * device, treat the root vdev as if it were
27176976Seschrock 				 * degraded.
27186976Seschrock 				 */
27196976Seschrock 				if (child->vdev_islog && vd == rvd)
27206976Seschrock 					degraded++;
27216976Seschrock 				else
27226976Seschrock 					faulted++;
27236976Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
27244451Seschrock 				degraded++;
27256976Seschrock 			}
27264451Seschrock 
27274451Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
27284451Seschrock 				corrupted++;
27294451Seschrock 		}
27301775Sbillm 
27314451Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
27324451Seschrock 
27334451Seschrock 		/*
27347754SJeff.Bonwick@Sun.COM 		 * Root special: if there is a top-level vdev that cannot be
27354451Seschrock 		 * opened due to corrupted metadata, then propagate the root
27364451Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
27374451Seschrock 		 * replicas'.
27384451Seschrock 		 */
27394451Seschrock 		if (corrupted && vd == rvd &&
27404451Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
27414451Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
27424451Seschrock 			    VDEV_AUX_CORRUPT_DATA);
27431775Sbillm 	}
27441775Sbillm 
27456976Seschrock 	if (vd->vdev_parent)
27464451Seschrock 		vdev_propagate_state(vd->vdev_parent);
27471775Sbillm }
27481775Sbillm 
2749789Sahrens /*
27501544Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
27511544Seschrock  * state, because we're in the process of opening children depth-first.
27521544Seschrock  * Otherwise, we propagate the change to the parent.
27531544Seschrock  *
27541544Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
27551544Seschrock  * generated.
2756789Sahrens  */
2757789Sahrens void
27581544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2759789Sahrens {
27601986Seschrock 	uint64_t save_state;
27616643Seschrock 	spa_t *spa = vd->vdev_spa;
27621544Seschrock 
27631544Seschrock 	if (state == vd->vdev_state) {
27641544Seschrock 		vd->vdev_stat.vs_aux = aux;
2765789Sahrens 		return;
27661544Seschrock 	}
27671544Seschrock 
27681986Seschrock 	save_state = vd->vdev_state;
2769789Sahrens 
2770789Sahrens 	vd->vdev_state = state;
2771789Sahrens 	vd->vdev_stat.vs_aux = aux;
2772789Sahrens 
27734451Seschrock 	/*
27744451Seschrock 	 * If we are setting the vdev state to anything but an open state, then
27754451Seschrock 	 * always close the underlying device.  Otherwise, we keep accessible
27764451Seschrock 	 * but invalid devices open forever.  We don't call vdev_close() itself,
27774451Seschrock 	 * because that implies some extra checks (offline, etc) that we don't
27784451Seschrock 	 * want here.  This is limited to leaf devices, because otherwise
27794451Seschrock 	 * closing the device will affect other children.
27804451Seschrock 	 */
27817780SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
27824451Seschrock 		vd->vdev_ops->vdev_op_close(vd);
27834451Seschrock 
2784*10817SEric.Schrock@Sun.COM 	/*
2785*10817SEric.Schrock@Sun.COM 	 * If we have brought this vdev back into service, we need
2786*10817SEric.Schrock@Sun.COM 	 * to notify fmd so that it can gracefully repair any outstanding
2787*10817SEric.Schrock@Sun.COM 	 * cases due to a missing device.  We do this in all cases, even those
2788*10817SEric.Schrock@Sun.COM 	 * that probably don't correlate to a repaired fault.  This is sure to
2789*10817SEric.Schrock@Sun.COM 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
2790*10817SEric.Schrock@Sun.COM 	 * this is a transient state it's OK, as the retire agent will
2791*10817SEric.Schrock@Sun.COM 	 * double-check the state of the vdev before repairing it.
2792*10817SEric.Schrock@Sun.COM 	 */
2793*10817SEric.Schrock@Sun.COM 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
2794*10817SEric.Schrock@Sun.COM 	    vd->vdev_prevstate != state)
2795*10817SEric.Schrock@Sun.COM 		zfs_post_state_change(spa, vd);
2796*10817SEric.Schrock@Sun.COM 
27974451Seschrock 	if (vd->vdev_removed &&
27984451Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
27994451Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
28004451Seschrock 		/*
28014451Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
28024451Seschrock 		 * device was previously marked removed and someone attempted to
28034451Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
28044451Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
28054451Seschrock 		 * it is one of our special test online cases, which is only
28064451Seschrock 		 * attempting to online the device and shouldn't generate an FMA
28074451Seschrock 		 * fault.
28084451Seschrock 		 */
28094451Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
28104451Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
28114451Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
28124451Seschrock 		vd->vdev_removed = B_TRUE;
28134451Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
28141544Seschrock 		/*
28151544Seschrock 		 * If we fail to open a vdev during an import, we mark it as
28161544Seschrock 		 * "not available", which signifies that it was never there to
28171544Seschrock 		 * begin with.  Failure to open such a device is not considered
28181544Seschrock 		 * an error.
28191544Seschrock 		 */
28206643Seschrock 		if (spa->spa_load_state == SPA_LOAD_IMPORT &&
28211986Seschrock 		    vd->vdev_ops->vdev_op_leaf)
28221986Seschrock 			vd->vdev_not_present = 1;
28231986Seschrock 
28241986Seschrock 		/*
28251986Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
28261986Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
28271986Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
28281986Seschrock 		 * want to post the ereport if the device was already in the
28291986Seschrock 		 * CANT_OPEN state beforehand.
28304451Seschrock 		 *
28314451Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
28324451Seschrock 		 * online the device in response to an insertion event.  If we
28334451Seschrock 		 * hit this case, then we have detected an insertion event for a
28344451Seschrock 		 * faulted or offline device that wasn't in the removed state.
28354451Seschrock 		 * In this scenario, we don't post an ereport because we are
28364451Seschrock 		 * about to replace the device, or attempt an online with
28374451Seschrock 		 * vdev_forcefault, which will generate the fault for us.
28381986Seschrock 		 */
28394451Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
28404451Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
28416643Seschrock 		    vd != spa->spa_root_vdev) {
28421544Seschrock 			const char *class;
28431544Seschrock 
28441544Seschrock 			switch (aux) {
28451544Seschrock 			case VDEV_AUX_OPEN_FAILED:
28461544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
28471544Seschrock 				break;
28481544Seschrock 			case VDEV_AUX_CORRUPT_DATA:
28491544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
28501544Seschrock 				break;
28511544Seschrock 			case VDEV_AUX_NO_REPLICAS:
28521544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
28531544Seschrock 				break;
28541544Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
28551544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
28561544Seschrock 				break;
28571544Seschrock 			case VDEV_AUX_TOO_SMALL:
28581544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
28591544Seschrock 				break;
28601544Seschrock 			case VDEV_AUX_BAD_LABEL:
28611544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
28621544Seschrock 				break;
28637754SJeff.Bonwick@Sun.COM 			case VDEV_AUX_IO_FAILURE:
28647754SJeff.Bonwick@Sun.COM 				class = FM_EREPORT_ZFS_IO_FAILURE;
28657754SJeff.Bonwick@Sun.COM 				break;
28661544Seschrock 			default:
28671544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
28681544Seschrock 			}
28691544Seschrock 
28706643Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
28711544Seschrock 		}
28724451Seschrock 
28734451Seschrock 		/* Erase any notion of persistent removed state */
28744451Seschrock 		vd->vdev_removed = B_FALSE;
28754451Seschrock 	} else {
28764451Seschrock 		vd->vdev_removed = B_FALSE;
28771544Seschrock 	}
28781544Seschrock 
28799583STim.Haley@Sun.COM 	if (!isopen && vd->vdev_parent)
28809583STim.Haley@Sun.COM 		vdev_propagate_state(vd->vdev_parent);
2881789Sahrens }
28827042Sgw25295 
28837042Sgw25295 /*
28847042Sgw25295  * Check the vdev configuration to ensure that it's capable of supporting
28857042Sgw25295  * a root pool. Currently, we do not support RAID-Z or partial configuration.
28867042Sgw25295  * In addition, only a single top-level vdev is allowed and none of the leaves
28877042Sgw25295  * can be wholedisks.
28887042Sgw25295  */
28897042Sgw25295 boolean_t
28907042Sgw25295 vdev_is_bootable(vdev_t *vd)
28917042Sgw25295 {
28927042Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf) {
28937042Sgw25295 		char *vdev_type = vd->vdev_ops->vdev_op_type;
28947042Sgw25295 
28957042Sgw25295 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
28967042Sgw25295 		    vd->vdev_children > 1) {
28977042Sgw25295 			return (B_FALSE);
28987042Sgw25295 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
28997042Sgw25295 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
29007042Sgw25295 			return (B_FALSE);
29017042Sgw25295 		}
29027042Sgw25295 	} else if (vd->vdev_wholedisk == 1) {
29037042Sgw25295 		return (B_FALSE);
29047042Sgw25295 	}
29057042Sgw25295 
29069816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
29077042Sgw25295 		if (!vdev_is_bootable(vd->vdev_child[c]))
29087042Sgw25295 			return (B_FALSE);
29097042Sgw25295 	}
29107042Sgw25295 	return (B_TRUE);
29117042Sgw25295 }
29129701SGeorge.Wilson@Sun.COM 
291310594SGeorge.Wilson@Sun.COM /*
291410594SGeorge.Wilson@Sun.COM  * Load the state from the original vdev tree (ovd) which
291510594SGeorge.Wilson@Sun.COM  * we've retrieved from the MOS config object. If the original
291610594SGeorge.Wilson@Sun.COM  * vdev was offline then we transfer that state to the device
291710594SGeorge.Wilson@Sun.COM  * in the current vdev tree (nvd).
291810594SGeorge.Wilson@Sun.COM  */
29199701SGeorge.Wilson@Sun.COM void
292010594SGeorge.Wilson@Sun.COM vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
29219701SGeorge.Wilson@Sun.COM {
292210594SGeorge.Wilson@Sun.COM 	spa_t *spa = nvd->vdev_spa;
292310594SGeorge.Wilson@Sun.COM 
292410594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
292510594SGeorge.Wilson@Sun.COM 	ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
292610594SGeorge.Wilson@Sun.COM 
292710594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < nvd->vdev_children; c++)
292810594SGeorge.Wilson@Sun.COM 		vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
292910594SGeorge.Wilson@Sun.COM 
293010594SGeorge.Wilson@Sun.COM 	if (nvd->vdev_ops->vdev_op_leaf && ovd->vdev_offline) {
29319701SGeorge.Wilson@Sun.COM 		/*
29329701SGeorge.Wilson@Sun.COM 		 * It would be nice to call vdev_offline()
29339701SGeorge.Wilson@Sun.COM 		 * directly but the pool isn't fully loaded and
29349701SGeorge.Wilson@Sun.COM 		 * the txg threads have not been started yet.
29359701SGeorge.Wilson@Sun.COM 		 */
293610594SGeorge.Wilson@Sun.COM 		nvd->vdev_offline = ovd->vdev_offline;
293710594SGeorge.Wilson@Sun.COM 		vdev_reopen(nvd->vdev_top);
29389701SGeorge.Wilson@Sun.COM 	}
29399701SGeorge.Wilson@Sun.COM }
29409816SGeorge.Wilson@Sun.COM 
29419816SGeorge.Wilson@Sun.COM /*
29429816SGeorge.Wilson@Sun.COM  * Expand a vdev if possible.
29439816SGeorge.Wilson@Sun.COM  */
29449816SGeorge.Wilson@Sun.COM void
29459816SGeorge.Wilson@Sun.COM vdev_expand(vdev_t *vd, uint64_t txg)
29469816SGeorge.Wilson@Sun.COM {
29479816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_top == vd);
29489816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
29499816SGeorge.Wilson@Sun.COM 
29509816SGeorge.Wilson@Sun.COM 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
29519816SGeorge.Wilson@Sun.COM 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
29529816SGeorge.Wilson@Sun.COM 		vdev_config_dirty(vd);
29539816SGeorge.Wilson@Sun.COM 	}
29549816SGeorge.Wilson@Sun.COM }
2955