xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 10588:dc03f981ea18)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
238632SBill.Moore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <sys/zfs_context.h>
281544Seschrock #include <sys/fm/fs/zfs.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/spa_impl.h>
31789Sahrens #include <sys/dmu.h>
32789Sahrens #include <sys/dmu_tx.h>
33789Sahrens #include <sys/vdev_impl.h>
34789Sahrens #include <sys/uberblock_impl.h>
35789Sahrens #include <sys/metaslab.h>
36789Sahrens #include <sys/metaslab_impl.h>
37789Sahrens #include <sys/space_map.h>
38789Sahrens #include <sys/zio.h>
39789Sahrens #include <sys/zap.h>
40789Sahrens #include <sys/fs/zfs.h>
416643Seschrock #include <sys/arc.h>
429701SGeorge.Wilson@Sun.COM #include <sys/zil.h>
43789Sahrens 
44789Sahrens /*
45789Sahrens  * Virtual device management.
46789Sahrens  */
47789Sahrens 
48789Sahrens static vdev_ops_t *vdev_ops_table[] = {
49789Sahrens 	&vdev_root_ops,
50789Sahrens 	&vdev_raidz_ops,
51789Sahrens 	&vdev_mirror_ops,
52789Sahrens 	&vdev_replacing_ops,
532082Seschrock 	&vdev_spare_ops,
54789Sahrens 	&vdev_disk_ops,
55789Sahrens 	&vdev_file_ops,
56789Sahrens 	&vdev_missing_ops,
57789Sahrens 	NULL
58789Sahrens };
59789Sahrens 
607046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
617046Sahrens int zfs_scrub_limit = 10;
623697Smishra 
63789Sahrens /*
64789Sahrens  * Given a vdev type, return the appropriate ops vector.
65789Sahrens  */
66789Sahrens static vdev_ops_t *
67789Sahrens vdev_getops(const char *type)
68789Sahrens {
69789Sahrens 	vdev_ops_t *ops, **opspp;
70789Sahrens 
71789Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
72789Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
73789Sahrens 			break;
74789Sahrens 
75789Sahrens 	return (ops);
76789Sahrens }
77789Sahrens 
78789Sahrens /*
79789Sahrens  * Default asize function: return the MAX of psize with the asize of
80789Sahrens  * all children.  This is what's used by anything other than RAID-Z.
81789Sahrens  */
82789Sahrens uint64_t
83789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
84789Sahrens {
851732Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
86789Sahrens 	uint64_t csize;
879816SGeorge.Wilson@Sun.COM 
889816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
89789Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
90789Sahrens 		asize = MAX(asize, csize);
91789Sahrens 	}
92789Sahrens 
93789Sahrens 	return (asize);
94789Sahrens }
95789Sahrens 
961175Slling /*
979816SGeorge.Wilson@Sun.COM  * Get the minimum allocatable size. We define the allocatable size as
989816SGeorge.Wilson@Sun.COM  * the vdev's asize rounded to the nearest metaslab. This allows us to
999816SGeorge.Wilson@Sun.COM  * replace or attach devices which don't have the same physical size but
1009816SGeorge.Wilson@Sun.COM  * can still satisfy the same number of allocations.
1011175Slling  */
1021175Slling uint64_t
1039816SGeorge.Wilson@Sun.COM vdev_get_min_asize(vdev_t *vd)
1041175Slling {
1059816SGeorge.Wilson@Sun.COM 	vdev_t *pvd = vd->vdev_parent;
1069816SGeorge.Wilson@Sun.COM 
1079816SGeorge.Wilson@Sun.COM 	/*
1089816SGeorge.Wilson@Sun.COM 	 * The our parent is NULL (inactive spare or cache) or is the root,
1099816SGeorge.Wilson@Sun.COM 	 * just return our own asize.
1109816SGeorge.Wilson@Sun.COM 	 */
1119816SGeorge.Wilson@Sun.COM 	if (pvd == NULL)
1129816SGeorge.Wilson@Sun.COM 		return (vd->vdev_asize);
1131175Slling 
1141175Slling 	/*
1159816SGeorge.Wilson@Sun.COM 	 * The top-level vdev just returns the allocatable size rounded
1169816SGeorge.Wilson@Sun.COM 	 * to the nearest metaslab.
1179816SGeorge.Wilson@Sun.COM 	 */
1189816SGeorge.Wilson@Sun.COM 	if (vd == vd->vdev_top)
1199816SGeorge.Wilson@Sun.COM 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
1209816SGeorge.Wilson@Sun.COM 
1219816SGeorge.Wilson@Sun.COM 	/*
1229816SGeorge.Wilson@Sun.COM 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
1239816SGeorge.Wilson@Sun.COM 	 * so each child must provide at least 1/Nth of its asize.
1241175Slling 	 */
1259816SGeorge.Wilson@Sun.COM 	if (pvd->vdev_ops == &vdev_raidz_ops)
1269816SGeorge.Wilson@Sun.COM 		return (pvd->vdev_min_asize / pvd->vdev_children);
1279816SGeorge.Wilson@Sun.COM 
1289816SGeorge.Wilson@Sun.COM 	return (pvd->vdev_min_asize);
1299816SGeorge.Wilson@Sun.COM }
1309816SGeorge.Wilson@Sun.COM 
1319816SGeorge.Wilson@Sun.COM void
1329816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vdev_t *vd)
1339816SGeorge.Wilson@Sun.COM {
1349816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1359816SGeorge.Wilson@Sun.COM 
1369816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
1379816SGeorge.Wilson@Sun.COM 		vdev_set_min_asize(vd->vdev_child[c]);
1381175Slling }
1391175Slling 
140789Sahrens vdev_t *
141789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
142789Sahrens {
143789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
144789Sahrens 
1457754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1465530Sbonwick 
1477046Sahrens 	if (vdev < rvd->vdev_children) {
1487046Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
149789Sahrens 		return (rvd->vdev_child[vdev]);
1507046Sahrens 	}
151789Sahrens 
152789Sahrens 	return (NULL);
153789Sahrens }
154789Sahrens 
155789Sahrens vdev_t *
156789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
157789Sahrens {
158789Sahrens 	vdev_t *mvd;
159789Sahrens 
1601585Sbonwick 	if (vd->vdev_guid == guid)
161789Sahrens 		return (vd);
162789Sahrens 
1639816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
164789Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
165789Sahrens 		    NULL)
166789Sahrens 			return (mvd);
167789Sahrens 
168789Sahrens 	return (NULL);
169789Sahrens }
170789Sahrens 
171789Sahrens void
172789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
173789Sahrens {
174789Sahrens 	size_t oldsize, newsize;
175789Sahrens 	uint64_t id = cvd->vdev_id;
176789Sahrens 	vdev_t **newchild;
177789Sahrens 
1787754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
179789Sahrens 	ASSERT(cvd->vdev_parent == NULL);
180789Sahrens 
181789Sahrens 	cvd->vdev_parent = pvd;
182789Sahrens 
183789Sahrens 	if (pvd == NULL)
184789Sahrens 		return;
185789Sahrens 
186789Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
187789Sahrens 
188789Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
189789Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
190789Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
191789Sahrens 
192789Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
193789Sahrens 	if (pvd->vdev_child != NULL) {
194789Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
195789Sahrens 		kmem_free(pvd->vdev_child, oldsize);
196789Sahrens 	}
197789Sahrens 
198789Sahrens 	pvd->vdev_child = newchild;
199789Sahrens 	pvd->vdev_child[id] = cvd;
200789Sahrens 
201789Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
202789Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
203789Sahrens 
204789Sahrens 	/*
205789Sahrens 	 * Walk up all ancestors to update guid sum.
206789Sahrens 	 */
207789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
208789Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
2093697Smishra 
2103697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2113697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
212789Sahrens }
213789Sahrens 
214789Sahrens void
215789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
216789Sahrens {
217789Sahrens 	int c;
218789Sahrens 	uint_t id = cvd->vdev_id;
219789Sahrens 
220789Sahrens 	ASSERT(cvd->vdev_parent == pvd);
221789Sahrens 
222789Sahrens 	if (pvd == NULL)
223789Sahrens 		return;
224789Sahrens 
225789Sahrens 	ASSERT(id < pvd->vdev_children);
226789Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
227789Sahrens 
228789Sahrens 	pvd->vdev_child[id] = NULL;
229789Sahrens 	cvd->vdev_parent = NULL;
230789Sahrens 
231789Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
232789Sahrens 		if (pvd->vdev_child[c])
233789Sahrens 			break;
234789Sahrens 
235789Sahrens 	if (c == pvd->vdev_children) {
236789Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
237789Sahrens 		pvd->vdev_child = NULL;
238789Sahrens 		pvd->vdev_children = 0;
239789Sahrens 	}
240789Sahrens 
241789Sahrens 	/*
242789Sahrens 	 * Walk up all ancestors to update guid sum.
243789Sahrens 	 */
244789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
245789Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
2463697Smishra 
2473697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2483697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
249789Sahrens }
250789Sahrens 
251789Sahrens /*
252789Sahrens  * Remove any holes in the child array.
253789Sahrens  */
254789Sahrens void
255789Sahrens vdev_compact_children(vdev_t *pvd)
256789Sahrens {
257789Sahrens 	vdev_t **newchild, *cvd;
258789Sahrens 	int oldc = pvd->vdev_children;
2599816SGeorge.Wilson@Sun.COM 	int newc;
260789Sahrens 
2617754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
262789Sahrens 
2639816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++)
264789Sahrens 		if (pvd->vdev_child[c])
265789Sahrens 			newc++;
266789Sahrens 
267789Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
268789Sahrens 
2699816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++) {
270789Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
271789Sahrens 			newchild[newc] = cvd;
272789Sahrens 			cvd->vdev_id = newc++;
273789Sahrens 		}
274789Sahrens 	}
275789Sahrens 
276789Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
277789Sahrens 	pvd->vdev_child = newchild;
278789Sahrens 	pvd->vdev_children = newc;
279789Sahrens }
280789Sahrens 
281789Sahrens /*
282789Sahrens  * Allocate and minimally initialize a vdev_t.
283789Sahrens  */
284789Sahrens static vdev_t *
285789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
286789Sahrens {
287789Sahrens 	vdev_t *vd;
288789Sahrens 
2891585Sbonwick 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
2901585Sbonwick 
2911585Sbonwick 	if (spa->spa_root_vdev == NULL) {
2921585Sbonwick 		ASSERT(ops == &vdev_root_ops);
2931585Sbonwick 		spa->spa_root_vdev = vd;
2941585Sbonwick 	}
295789Sahrens 
2961585Sbonwick 	if (guid == 0) {
2971585Sbonwick 		if (spa->spa_root_vdev == vd) {
2981585Sbonwick 			/*
2991585Sbonwick 			 * The root vdev's guid will also be the pool guid,
3001585Sbonwick 			 * which must be unique among all pools.
3011585Sbonwick 			 */
3021585Sbonwick 			while (guid == 0 || spa_guid_exists(guid, 0))
3031585Sbonwick 				guid = spa_get_random(-1ULL);
3041585Sbonwick 		} else {
3051585Sbonwick 			/*
3061585Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3071585Sbonwick 			 */
3081585Sbonwick 			while (guid == 0 ||
3091585Sbonwick 			    spa_guid_exists(spa_guid(spa), guid))
3101585Sbonwick 				guid = spa_get_random(-1ULL);
3111585Sbonwick 		}
3121585Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3131585Sbonwick 	}
314789Sahrens 
315789Sahrens 	vd->vdev_spa = spa;
316789Sahrens 	vd->vdev_id = id;
317789Sahrens 	vd->vdev_guid = guid;
318789Sahrens 	vd->vdev_guid_sum = guid;
319789Sahrens 	vd->vdev_ops = ops;
320789Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
321789Sahrens 
322789Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3232856Snd150628 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
3247754SJeff.Bonwick@Sun.COM 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3258241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
3268241SJeff.Bonwick@Sun.COM 		space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
3278241SJeff.Bonwick@Sun.COM 		    &vd->vdev_dtl_lock);
3288241SJeff.Bonwick@Sun.COM 	}
329789Sahrens 	txg_list_create(&vd->vdev_ms_list,
330789Sahrens 	    offsetof(struct metaslab, ms_txg_node));
331789Sahrens 	txg_list_create(&vd->vdev_dtl_list,
332789Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
333789Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3344451Seschrock 	vdev_queue_init(vd);
3354451Seschrock 	vdev_cache_init(vd);
336789Sahrens 
337789Sahrens 	return (vd);
338789Sahrens }
339789Sahrens 
340789Sahrens /*
341789Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
342789Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
343789Sahrens  * different for each case.
344789Sahrens  */
3452082Seschrock int
3462082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
3472082Seschrock     int alloctype)
348789Sahrens {
349789Sahrens 	vdev_ops_t *ops;
350789Sahrens 	char *type;
3514527Sperrin 	uint64_t guid = 0, islog, nparity;
352789Sahrens 	vdev_t *vd;
353789Sahrens 
3547754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
355789Sahrens 
356789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
3572082Seschrock 		return (EINVAL);
358789Sahrens 
359789Sahrens 	if ((ops = vdev_getops(type)) == NULL)
3602082Seschrock 		return (EINVAL);
361789Sahrens 
362789Sahrens 	/*
363789Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
364789Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
365789Sahrens 	 */
366789Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
367789Sahrens 		uint64_t label_id;
368789Sahrens 
369789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
370789Sahrens 		    label_id != id)
3712082Seschrock 			return (EINVAL);
372789Sahrens 
373789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3742082Seschrock 			return (EINVAL);
3752082Seschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
3762082Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3772082Seschrock 			return (EINVAL);
3785450Sbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
3795450Sbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3805450Sbrendan 			return (EINVAL);
3819790SLin.Ling@Sun.COM 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
3829790SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3839790SLin.Ling@Sun.COM 			return (EINVAL);
384789Sahrens 	}
385789Sahrens 
3862082Seschrock 	/*
3872082Seschrock 	 * The first allocated vdev must be of type 'root'.
3882082Seschrock 	 */
3892082Seschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
3902082Seschrock 		return (EINVAL);
3912082Seschrock 
3924527Sperrin 	/*
3934527Sperrin 	 * Determine whether we're a log vdev.
3944527Sperrin 	 */
3954527Sperrin 	islog = 0;
3964527Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
3975094Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
3984527Sperrin 		return (ENOTSUP);
3994527Sperrin 
4004527Sperrin 	/*
4014527Sperrin 	 * Set the nparity property for RAID-Z vdevs.
4024527Sperrin 	 */
4034527Sperrin 	nparity = -1ULL;
4044527Sperrin 	if (ops == &vdev_raidz_ops) {
4054527Sperrin 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4064527Sperrin 		    &nparity) == 0) {
4074527Sperrin 			/*
40810105Sadam.leventhal@sun.com 			 * Currently, we can only support 3 parity devices.
4094527Sperrin 			 */
41010105Sadam.leventhal@sun.com 			if (nparity == 0 || nparity > 3)
4114527Sperrin 				return (EINVAL);
4124527Sperrin 			/*
41310105Sadam.leventhal@sun.com 			 * Previous versions could only support 1 or 2 parity
41410105Sadam.leventhal@sun.com 			 * device.
4154527Sperrin 			 */
41610105Sadam.leventhal@sun.com 			if (nparity > 1 &&
41710105Sadam.leventhal@sun.com 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
41810105Sadam.leventhal@sun.com 				return (ENOTSUP);
41910105Sadam.leventhal@sun.com 			if (nparity > 2 &&
42010105Sadam.leventhal@sun.com 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
4214527Sperrin 				return (ENOTSUP);
4224527Sperrin 		} else {
4234527Sperrin 			/*
4244527Sperrin 			 * We require the parity to be specified for SPAs that
4254527Sperrin 			 * support multiple parity levels.
4264527Sperrin 			 */
42710105Sadam.leventhal@sun.com 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
4284527Sperrin 				return (EINVAL);
4294527Sperrin 			/*
4304527Sperrin 			 * Otherwise, we default to 1 parity device for RAID-Z.
4314527Sperrin 			 */
4324527Sperrin 			nparity = 1;
4334527Sperrin 		}
4344527Sperrin 	} else {
4354527Sperrin 		nparity = 0;
4364527Sperrin 	}
4374527Sperrin 	ASSERT(nparity != -1ULL);
4384527Sperrin 
439789Sahrens 	vd = vdev_alloc_common(spa, id, guid, ops);
440789Sahrens 
4414527Sperrin 	vd->vdev_islog = islog;
4424527Sperrin 	vd->vdev_nparity = nparity;
4434527Sperrin 
444789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
445789Sahrens 		vd->vdev_path = spa_strdup(vd->vdev_path);
446789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
447789Sahrens 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4484451Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4494451Seschrock 	    &vd->vdev_physpath) == 0)
4504451Seschrock 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4519425SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4529425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
453789Sahrens 
454789Sahrens 	/*
4551171Seschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
4561171Seschrock 	 * as -1.
4571171Seschrock 	 */
4581171Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
4591171Seschrock 	    &vd->vdev_wholedisk) != 0)
4601171Seschrock 		vd->vdev_wholedisk = -1ULL;
4611171Seschrock 
4621171Seschrock 	/*
4631544Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
4641544Seschrock 	 * was not present at the time of import.
4651544Seschrock 	 */
4669425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4679425SEric.Schrock@Sun.COM 	    &vd->vdev_not_present);
4681544Seschrock 
4691544Seschrock 	/*
4701732Sbonwick 	 * Get the alignment requirement.
4711732Sbonwick 	 */
4721732Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
4731732Sbonwick 
4741732Sbonwick 	/*
475789Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
476789Sahrens 	 */
477789Sahrens 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
478789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
479789Sahrens 		    &vd->vdev_ms_array);
480789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
481789Sahrens 		    &vd->vdev_ms_shift);
482789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
483789Sahrens 		    &vd->vdev_asize);
484789Sahrens 	}
485789Sahrens 
486789Sahrens 	/*
4874451Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
488789Sahrens 	 */
4896643Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
4909790SLin.Ling@Sun.COM 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
4919790SLin.Ling@Sun.COM 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
4926643Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
4936643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
4948241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl_smo.smo_object);
4956643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
4966643Seschrock 			    &vd->vdev_unspare);
4976643Seschrock 		}
4989790SLin.Ling@Sun.COM 
4999790SLin.Ling@Sun.COM 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
5009790SLin.Ling@Sun.COM 			uint64_t spare = 0;
5019790SLin.Ling@Sun.COM 
5029790SLin.Ling@Sun.COM 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
5039790SLin.Ling@Sun.COM 			    &spare) == 0 && spare)
5049790SLin.Ling@Sun.COM 				spa_spare_add(vd);
5059790SLin.Ling@Sun.COM 		}
5069790SLin.Ling@Sun.COM 
5071732Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
5081732Sbonwick 		    &vd->vdev_offline);
5096643Seschrock 
5104451Seschrock 		/*
5114451Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5124451Seschrock 		 * state, as the diagnosis made on another system may not be
5134451Seschrock 		 * valid in the current context.
5144451Seschrock 		 */
5154451Seschrock 		if (spa->spa_load_state == SPA_LOAD_OPEN) {
5164451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5174451Seschrock 			    &vd->vdev_faulted);
5184451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5194451Seschrock 			    &vd->vdev_degraded);
5204451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5214451Seschrock 			    &vd->vdev_removed);
5224451Seschrock 		}
523789Sahrens 	}
524789Sahrens 
525789Sahrens 	/*
526789Sahrens 	 * Add ourselves to the parent's list of children.
527789Sahrens 	 */
528789Sahrens 	vdev_add_child(parent, vd);
529789Sahrens 
5302082Seschrock 	*vdp = vd;
5312082Seschrock 
5322082Seschrock 	return (0);
533789Sahrens }
534789Sahrens 
535789Sahrens void
536789Sahrens vdev_free(vdev_t *vd)
537789Sahrens {
5384451Seschrock 	spa_t *spa = vd->vdev_spa;
539789Sahrens 
540789Sahrens 	/*
541789Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
542789Sahrens 	 * trying to ensure complicated semantics for all callers.
543789Sahrens 	 */
544789Sahrens 	vdev_close(vd);
545789Sahrens 
5467754SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
547789Sahrens 
548789Sahrens 	/*
549789Sahrens 	 * Free all children.
550789Sahrens 	 */
5519816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
552789Sahrens 		vdev_free(vd->vdev_child[c]);
553789Sahrens 
554789Sahrens 	ASSERT(vd->vdev_child == NULL);
555789Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
556789Sahrens 
557789Sahrens 	/*
558789Sahrens 	 * Discard allocation state.
559789Sahrens 	 */
560789Sahrens 	if (vd == vd->vdev_top)
561789Sahrens 		vdev_metaslab_fini(vd);
562789Sahrens 
563789Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
5642082Seschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
565789Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
566789Sahrens 
567789Sahrens 	/*
568789Sahrens 	 * Remove this vdev from its parent's child list.
569789Sahrens 	 */
570789Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
571789Sahrens 
572789Sahrens 	ASSERT(vd->vdev_parent == NULL);
573789Sahrens 
5744451Seschrock 	/*
5754451Seschrock 	 * Clean up vdev structure.
5764451Seschrock 	 */
5774451Seschrock 	vdev_queue_fini(vd);
5784451Seschrock 	vdev_cache_fini(vd);
5794451Seschrock 
5804451Seschrock 	if (vd->vdev_path)
5814451Seschrock 		spa_strfree(vd->vdev_path);
5824451Seschrock 	if (vd->vdev_devid)
5834451Seschrock 		spa_strfree(vd->vdev_devid);
5844451Seschrock 	if (vd->vdev_physpath)
5854451Seschrock 		spa_strfree(vd->vdev_physpath);
5869425SEric.Schrock@Sun.COM 	if (vd->vdev_fru)
5879425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_fru);
5884451Seschrock 
5894451Seschrock 	if (vd->vdev_isspare)
5904451Seschrock 		spa_spare_remove(vd);
5915450Sbrendan 	if (vd->vdev_isl2cache)
5925450Sbrendan 		spa_l2cache_remove(vd);
5934451Seschrock 
5944451Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
5954451Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
5968241SJeff.Bonwick@Sun.COM 
5974451Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
5988241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
5998241SJeff.Bonwick@Sun.COM 		space_map_unload(&vd->vdev_dtl[t]);
6008241SJeff.Bonwick@Sun.COM 		space_map_destroy(&vd->vdev_dtl[t]);
6018241SJeff.Bonwick@Sun.COM 	}
6024451Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
6038241SJeff.Bonwick@Sun.COM 
6044451Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6054451Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
6067754SJeff.Bonwick@Sun.COM 	mutex_destroy(&vd->vdev_probe_lock);
6074451Seschrock 
6084451Seschrock 	if (vd == spa->spa_root_vdev)
6094451Seschrock 		spa->spa_root_vdev = NULL;
6104451Seschrock 
6114451Seschrock 	kmem_free(vd, sizeof (vdev_t));
612789Sahrens }
613789Sahrens 
614789Sahrens /*
615789Sahrens  * Transfer top-level vdev state from svd to tvd.
616789Sahrens  */
617789Sahrens static void
618789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
619789Sahrens {
620789Sahrens 	spa_t *spa = svd->vdev_spa;
621789Sahrens 	metaslab_t *msp;
622789Sahrens 	vdev_t *vd;
623789Sahrens 	int t;
624789Sahrens 
625789Sahrens 	ASSERT(tvd == tvd->vdev_top);
626789Sahrens 
627789Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
628789Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
629789Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
630789Sahrens 
631789Sahrens 	svd->vdev_ms_array = 0;
632789Sahrens 	svd->vdev_ms_shift = 0;
633789Sahrens 	svd->vdev_ms_count = 0;
634789Sahrens 
635789Sahrens 	tvd->vdev_mg = svd->vdev_mg;
636789Sahrens 	tvd->vdev_ms = svd->vdev_ms;
637789Sahrens 
638789Sahrens 	svd->vdev_mg = NULL;
639789Sahrens 	svd->vdev_ms = NULL;
6401732Sbonwick 
6411732Sbonwick 	if (tvd->vdev_mg != NULL)
6421732Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
643789Sahrens 
644789Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
645789Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
6462082Seschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
647789Sahrens 
648789Sahrens 	svd->vdev_stat.vs_alloc = 0;
649789Sahrens 	svd->vdev_stat.vs_space = 0;
6502082Seschrock 	svd->vdev_stat.vs_dspace = 0;
651789Sahrens 
652789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
653789Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
654789Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
655789Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
656789Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
657789Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
658789Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
659789Sahrens 	}
660789Sahrens 
6617754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_config_dirty_node)) {
662789Sahrens 		vdev_config_clean(svd);
663789Sahrens 		vdev_config_dirty(tvd);
664789Sahrens 	}
665789Sahrens 
6667754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_state_dirty_node)) {
6677754SJeff.Bonwick@Sun.COM 		vdev_state_clean(svd);
6687754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(tvd);
6697754SJeff.Bonwick@Sun.COM 	}
6707754SJeff.Bonwick@Sun.COM 
6712082Seschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
6722082Seschrock 	svd->vdev_deflate_ratio = 0;
6734527Sperrin 
6744527Sperrin 	tvd->vdev_islog = svd->vdev_islog;
6754527Sperrin 	svd->vdev_islog = 0;
676789Sahrens }
677789Sahrens 
678789Sahrens static void
679789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
680789Sahrens {
681789Sahrens 	if (vd == NULL)
682789Sahrens 		return;
683789Sahrens 
684789Sahrens 	vd->vdev_top = tvd;
685789Sahrens 
6869816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
687789Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
688789Sahrens }
689789Sahrens 
690789Sahrens /*
691789Sahrens  * Add a mirror/replacing vdev above an existing vdev.
692789Sahrens  */
693789Sahrens vdev_t *
694789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
695789Sahrens {
696789Sahrens 	spa_t *spa = cvd->vdev_spa;
697789Sahrens 	vdev_t *pvd = cvd->vdev_parent;
698789Sahrens 	vdev_t *mvd;
699789Sahrens 
7007754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
701789Sahrens 
702789Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
7031732Sbonwick 
7041732Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
7059816SGeorge.Wilson@Sun.COM 	mvd->vdev_min_asize = cvd->vdev_min_asize;
7061732Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
7071732Sbonwick 	mvd->vdev_state = cvd->vdev_state;
7081732Sbonwick 
709789Sahrens 	vdev_remove_child(pvd, cvd);
710789Sahrens 	vdev_add_child(pvd, mvd);
711789Sahrens 	cvd->vdev_id = mvd->vdev_children;
712789Sahrens 	vdev_add_child(mvd, cvd);
713789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
714789Sahrens 
715789Sahrens 	if (mvd == mvd->vdev_top)
716789Sahrens 		vdev_top_transfer(cvd, mvd);
717789Sahrens 
718789Sahrens 	return (mvd);
719789Sahrens }
720789Sahrens 
721789Sahrens /*
722789Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
723789Sahrens  */
724789Sahrens void
725789Sahrens vdev_remove_parent(vdev_t *cvd)
726789Sahrens {
727789Sahrens 	vdev_t *mvd = cvd->vdev_parent;
728789Sahrens 	vdev_t *pvd = mvd->vdev_parent;
729789Sahrens 
7307754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
731789Sahrens 
732789Sahrens 	ASSERT(mvd->vdev_children == 1);
733789Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
7342082Seschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
7352082Seschrock 	    mvd->vdev_ops == &vdev_spare_ops);
7361732Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
737789Sahrens 
738789Sahrens 	vdev_remove_child(mvd, cvd);
739789Sahrens 	vdev_remove_child(pvd, mvd);
7408241SJeff.Bonwick@Sun.COM 
7417754SJeff.Bonwick@Sun.COM 	/*
7427754SJeff.Bonwick@Sun.COM 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
7437754SJeff.Bonwick@Sun.COM 	 * Otherwise, we could have detached an offline device, and when we
7447754SJeff.Bonwick@Sun.COM 	 * go to import the pool we'll think we have two top-level vdevs,
7457754SJeff.Bonwick@Sun.COM 	 * instead of a different version of the same top-level vdev.
7467754SJeff.Bonwick@Sun.COM 	 */
7478241SJeff.Bonwick@Sun.COM 	if (mvd->vdev_top == mvd) {
7488241SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
7498241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid += guid_delta;
7508241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid_sum += guid_delta;
7518241SJeff.Bonwick@Sun.COM 	}
752789Sahrens 	cvd->vdev_id = mvd->vdev_id;
753789Sahrens 	vdev_add_child(pvd, cvd);
754789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
755789Sahrens 
756789Sahrens 	if (cvd == cvd->vdev_top)
757789Sahrens 		vdev_top_transfer(mvd, cvd);
758789Sahrens 
759789Sahrens 	ASSERT(mvd->vdev_children == 0);
760789Sahrens 	vdev_free(mvd);
761789Sahrens }
762789Sahrens 
7631544Seschrock int
764789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
765789Sahrens {
766789Sahrens 	spa_t *spa = vd->vdev_spa;
7671732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
7684527Sperrin 	metaslab_class_t *mc;
7691732Sbonwick 	uint64_t m;
770789Sahrens 	uint64_t oldc = vd->vdev_ms_count;
771789Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
7721732Sbonwick 	metaslab_t **mspp;
7731732Sbonwick 	int error;
774789Sahrens 
7751585Sbonwick 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
7761585Sbonwick 		return (0);
7771585Sbonwick 
7789701SGeorge.Wilson@Sun.COM 	/*
7799701SGeorge.Wilson@Sun.COM 	 * Compute the raidz-deflation ratio.  Note, we hard-code
7809701SGeorge.Wilson@Sun.COM 	 * in 128k (1 << 17) because it is the current "typical" blocksize.
7819701SGeorge.Wilson@Sun.COM 	 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
7829701SGeorge.Wilson@Sun.COM 	 * or we will inconsistently account for existing bp's.
7839701SGeorge.Wilson@Sun.COM 	 */
7849701SGeorge.Wilson@Sun.COM 	vd->vdev_deflate_ratio = (1 << 17) /
7859701SGeorge.Wilson@Sun.COM 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
7869701SGeorge.Wilson@Sun.COM 
787789Sahrens 	ASSERT(oldc <= newc);
788789Sahrens 
7894527Sperrin 	if (vd->vdev_islog)
7904527Sperrin 		mc = spa->spa_log_class;
7914527Sperrin 	else
7924527Sperrin 		mc = spa->spa_normal_class;
7934527Sperrin 
7941732Sbonwick 	if (vd->vdev_mg == NULL)
7951732Sbonwick 		vd->vdev_mg = metaslab_group_create(mc, vd);
7961732Sbonwick 
7971732Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
7981732Sbonwick 
7991732Sbonwick 	if (oldc != 0) {
8001732Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
8011732Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
8021732Sbonwick 	}
8031732Sbonwick 
8041732Sbonwick 	vd->vdev_ms = mspp;
805789Sahrens 	vd->vdev_ms_count = newc;
806789Sahrens 
8071732Sbonwick 	for (m = oldc; m < newc; m++) {
8081732Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
809789Sahrens 		if (txg == 0) {
8101732Sbonwick 			uint64_t object = 0;
8111732Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8129512SNeil.Perrin@Sun.COM 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8139512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
8141732Sbonwick 			if (error)
8151732Sbonwick 				return (error);
8161732Sbonwick 			if (object != 0) {
8171732Sbonwick 				dmu_buf_t *db;
8181732Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
8191732Sbonwick 				if (error)
8201732Sbonwick 					return (error);
8214944Smaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
8224944Smaybee 				bcopy(db->db_data, &smo, sizeof (smo));
8231732Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
8241544Seschrock 				dmu_buf_rele(db, FTAG);
825789Sahrens 			}
826789Sahrens 		}
8271732Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
8281732Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
829789Sahrens 	}
830789Sahrens 
8311544Seschrock 	return (0);
832789Sahrens }
833789Sahrens 
834789Sahrens void
835789Sahrens vdev_metaslab_fini(vdev_t *vd)
836789Sahrens {
837789Sahrens 	uint64_t m;
838789Sahrens 	uint64_t count = vd->vdev_ms_count;
839789Sahrens 
840789Sahrens 	if (vd->vdev_ms != NULL) {
841789Sahrens 		for (m = 0; m < count; m++)
8421732Sbonwick 			if (vd->vdev_ms[m] != NULL)
8431732Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
844789Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
845789Sahrens 		vd->vdev_ms = NULL;
846789Sahrens 	}
847789Sahrens }
848789Sahrens 
8497754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats {
8507754SJeff.Bonwick@Sun.COM 	boolean_t	vps_readable;
8517754SJeff.Bonwick@Sun.COM 	boolean_t	vps_writeable;
8527754SJeff.Bonwick@Sun.COM 	int		vps_flags;
8537754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t;
8547754SJeff.Bonwick@Sun.COM 
8557754SJeff.Bonwick@Sun.COM static void
8567754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio)
8575329Sgw25295 {
8588241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
8598632SBill.Moore@Sun.COM 	vdev_t *vd = zio->io_vd;
8607754SJeff.Bonwick@Sun.COM 	vdev_probe_stats_t *vps = zio->io_private;
8618632SBill.Moore@Sun.COM 
8628632SBill.Moore@Sun.COM 	ASSERT(vd->vdev_probe_zio != NULL);
8637754SJeff.Bonwick@Sun.COM 
8647754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ) {
8657754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8667754SJeff.Bonwick@Sun.COM 			vps->vps_readable = 1;
8678241SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0 && spa_writeable(spa)) {
8688632SBill.Moore@Sun.COM 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
8697754SJeff.Bonwick@Sun.COM 			    zio->io_offset, zio->io_size, zio->io_data,
8707754SJeff.Bonwick@Sun.COM 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
8717754SJeff.Bonwick@Sun.COM 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
8727754SJeff.Bonwick@Sun.COM 		} else {
8737754SJeff.Bonwick@Sun.COM 			zio_buf_free(zio->io_data, zio->io_size);
8747754SJeff.Bonwick@Sun.COM 		}
8757754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
8767754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8777754SJeff.Bonwick@Sun.COM 			vps->vps_writeable = 1;
8787754SJeff.Bonwick@Sun.COM 		zio_buf_free(zio->io_data, zio->io_size);
8797754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_NULL) {
8808632SBill.Moore@Sun.COM 		zio_t *pio;
8817754SJeff.Bonwick@Sun.COM 
8827754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read |= !vps->vps_readable;
8837754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write |= !vps->vps_writeable;
8847754SJeff.Bonwick@Sun.COM 
8857754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) &&
8868241SJeff.Bonwick@Sun.COM 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
8877754SJeff.Bonwick@Sun.COM 			zio->io_error = 0;
8887754SJeff.Bonwick@Sun.COM 		} else {
8897754SJeff.Bonwick@Sun.COM 			ASSERT(zio->io_error != 0);
8907754SJeff.Bonwick@Sun.COM 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
8918241SJeff.Bonwick@Sun.COM 			    spa, vd, NULL, 0, 0);
8927754SJeff.Bonwick@Sun.COM 			zio->io_error = ENXIO;
8937754SJeff.Bonwick@Sun.COM 		}
8948632SBill.Moore@Sun.COM 
8958632SBill.Moore@Sun.COM 		mutex_enter(&vd->vdev_probe_lock);
8968632SBill.Moore@Sun.COM 		ASSERT(vd->vdev_probe_zio == zio);
8978632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = NULL;
8988632SBill.Moore@Sun.COM 		mutex_exit(&vd->vdev_probe_lock);
8998632SBill.Moore@Sun.COM 
9008632SBill.Moore@Sun.COM 		while ((pio = zio_walk_parents(zio)) != NULL)
9018632SBill.Moore@Sun.COM 			if (!vdev_accessible(vd, pio))
9028632SBill.Moore@Sun.COM 				pio->io_error = ENXIO;
9038632SBill.Moore@Sun.COM 
9047754SJeff.Bonwick@Sun.COM 		kmem_free(vps, sizeof (*vps));
9057754SJeff.Bonwick@Sun.COM 	}
9067754SJeff.Bonwick@Sun.COM }
9075329Sgw25295 
9087754SJeff.Bonwick@Sun.COM /*
9097754SJeff.Bonwick@Sun.COM  * Determine whether this device is accessible by reading and writing
9107754SJeff.Bonwick@Sun.COM  * to several known locations: the pad regions of each vdev label
9117754SJeff.Bonwick@Sun.COM  * but the first (which we leave alone in case it contains a VTOC).
9127754SJeff.Bonwick@Sun.COM  */
9137754SJeff.Bonwick@Sun.COM zio_t *
9148632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio)
9157754SJeff.Bonwick@Sun.COM {
9167754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
9178632SBill.Moore@Sun.COM 	vdev_probe_stats_t *vps = NULL;
9188632SBill.Moore@Sun.COM 	zio_t *pio;
9197754SJeff.Bonwick@Sun.COM 
9207754SJeff.Bonwick@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9217754SJeff.Bonwick@Sun.COM 
9228632SBill.Moore@Sun.COM 	/*
9238632SBill.Moore@Sun.COM 	 * Don't probe the probe.
9248632SBill.Moore@Sun.COM 	 */
9258632SBill.Moore@Sun.COM 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
9268632SBill.Moore@Sun.COM 		return (NULL);
9278632SBill.Moore@Sun.COM 
9288632SBill.Moore@Sun.COM 	/*
9298632SBill.Moore@Sun.COM 	 * To prevent 'probe storms' when a device fails, we create
9308632SBill.Moore@Sun.COM 	 * just one probe i/o at a time.  All zios that want to probe
9318632SBill.Moore@Sun.COM 	 * this vdev will become parents of the probe io.
9328632SBill.Moore@Sun.COM 	 */
9338632SBill.Moore@Sun.COM 	mutex_enter(&vd->vdev_probe_lock);
9348632SBill.Moore@Sun.COM 
9358632SBill.Moore@Sun.COM 	if ((pio = vd->vdev_probe_zio) == NULL) {
9368632SBill.Moore@Sun.COM 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
9378632SBill.Moore@Sun.COM 
9388632SBill.Moore@Sun.COM 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
9398632SBill.Moore@Sun.COM 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9409725SEric.Schrock@Sun.COM 		    ZIO_FLAG_TRYHARD;
9418632SBill.Moore@Sun.COM 
9428632SBill.Moore@Sun.COM 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
9438632SBill.Moore@Sun.COM 			/*
9448632SBill.Moore@Sun.COM 			 * vdev_cant_read and vdev_cant_write can only
9458632SBill.Moore@Sun.COM 			 * transition from TRUE to FALSE when we have the
9468632SBill.Moore@Sun.COM 			 * SCL_ZIO lock as writer; otherwise they can only
9478632SBill.Moore@Sun.COM 			 * transition from FALSE to TRUE.  This ensures that
9488632SBill.Moore@Sun.COM 			 * any zio looking at these values can assume that
9498632SBill.Moore@Sun.COM 			 * failures persist for the life of the I/O.  That's
9508632SBill.Moore@Sun.COM 			 * important because when a device has intermittent
9518632SBill.Moore@Sun.COM 			 * connectivity problems, we want to ensure that
9528632SBill.Moore@Sun.COM 			 * they're ascribed to the device (ENXIO) and not
9538632SBill.Moore@Sun.COM 			 * the zio (EIO).
9548632SBill.Moore@Sun.COM 			 *
9558632SBill.Moore@Sun.COM 			 * Since we hold SCL_ZIO as writer here, clear both
9568632SBill.Moore@Sun.COM 			 * values so the probe can reevaluate from first
9578632SBill.Moore@Sun.COM 			 * principles.
9588632SBill.Moore@Sun.COM 			 */
9598632SBill.Moore@Sun.COM 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
9608632SBill.Moore@Sun.COM 			vd->vdev_cant_read = B_FALSE;
9618632SBill.Moore@Sun.COM 			vd->vdev_cant_write = B_FALSE;
9628632SBill.Moore@Sun.COM 		}
9638632SBill.Moore@Sun.COM 
9648632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
9658632SBill.Moore@Sun.COM 		    vdev_probe_done, vps,
9668632SBill.Moore@Sun.COM 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
9678632SBill.Moore@Sun.COM 
9688632SBill.Moore@Sun.COM 		if (zio != NULL) {
9698632SBill.Moore@Sun.COM 			vd->vdev_probe_wanted = B_TRUE;
9708632SBill.Moore@Sun.COM 			spa_async_request(spa, SPA_ASYNC_PROBE);
9718632SBill.Moore@Sun.COM 		}
9728632SBill.Moore@Sun.COM 	}
9738632SBill.Moore@Sun.COM 
9748632SBill.Moore@Sun.COM 	if (zio != NULL)
9758632SBill.Moore@Sun.COM 		zio_add_child(zio, pio);
9768632SBill.Moore@Sun.COM 
9778632SBill.Moore@Sun.COM 	mutex_exit(&vd->vdev_probe_lock);
9788632SBill.Moore@Sun.COM 
9798632SBill.Moore@Sun.COM 	if (vps == NULL) {
9808632SBill.Moore@Sun.COM 		ASSERT(zio != NULL);
9818632SBill.Moore@Sun.COM 		return (NULL);
9828632SBill.Moore@Sun.COM 	}
9837754SJeff.Bonwick@Sun.COM 
9847754SJeff.Bonwick@Sun.COM 	for (int l = 1; l < VDEV_LABELS; l++) {
9858632SBill.Moore@Sun.COM 		zio_nowait(zio_read_phys(pio, vd,
9867754SJeff.Bonwick@Sun.COM 		    vdev_label_offset(vd->vdev_psize, l,
9879056SLin.Ling@Sun.COM 		    offsetof(vdev_label_t, vl_pad2)),
9889056SLin.Ling@Sun.COM 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
9897754SJeff.Bonwick@Sun.COM 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
9907754SJeff.Bonwick@Sun.COM 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
9917754SJeff.Bonwick@Sun.COM 	}
9927754SJeff.Bonwick@Sun.COM 
9938632SBill.Moore@Sun.COM 	if (zio == NULL)
9948632SBill.Moore@Sun.COM 		return (pio);
9958632SBill.Moore@Sun.COM 
9968632SBill.Moore@Sun.COM 	zio_nowait(pio);
9978632SBill.Moore@Sun.COM 	return (NULL);
9985329Sgw25295 }
9995329Sgw25295 
10009846SEric.Taylor@Sun.COM static void
10019846SEric.Taylor@Sun.COM vdev_open_child(void *arg)
10029846SEric.Taylor@Sun.COM {
10039846SEric.Taylor@Sun.COM 	vdev_t *vd = arg;
10049846SEric.Taylor@Sun.COM 
10059846SEric.Taylor@Sun.COM 	vd->vdev_open_thread = curthread;
10069846SEric.Taylor@Sun.COM 	vd->vdev_open_error = vdev_open(vd);
10079846SEric.Taylor@Sun.COM 	vd->vdev_open_thread = NULL;
10089846SEric.Taylor@Sun.COM }
10099846SEric.Taylor@Sun.COM 
1010*10588SEric.Taylor@Sun.COM boolean_t
1011*10588SEric.Taylor@Sun.COM vdev_uses_zvols(vdev_t *vd)
1012*10588SEric.Taylor@Sun.COM {
1013*10588SEric.Taylor@Sun.COM 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1014*10588SEric.Taylor@Sun.COM 	    strlen(ZVOL_DIR)) == 0)
1015*10588SEric.Taylor@Sun.COM 		return (B_TRUE);
1016*10588SEric.Taylor@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
1017*10588SEric.Taylor@Sun.COM 		if (vdev_uses_zvols(vd->vdev_child[c]))
1018*10588SEric.Taylor@Sun.COM 			return (B_TRUE);
1019*10588SEric.Taylor@Sun.COM 	return (B_FALSE);
1020*10588SEric.Taylor@Sun.COM }
1021*10588SEric.Taylor@Sun.COM 
10229846SEric.Taylor@Sun.COM void
10239846SEric.Taylor@Sun.COM vdev_open_children(vdev_t *vd)
10249846SEric.Taylor@Sun.COM {
10259846SEric.Taylor@Sun.COM 	taskq_t *tq;
10269846SEric.Taylor@Sun.COM 	int children = vd->vdev_children;
10279846SEric.Taylor@Sun.COM 
1028*10588SEric.Taylor@Sun.COM 	/*
1029*10588SEric.Taylor@Sun.COM 	 * in order to handle pools on top of zvols, do the opens
1030*10588SEric.Taylor@Sun.COM 	 * in a single thread so that the same thread holds the
1031*10588SEric.Taylor@Sun.COM 	 * spa_namespace_lock
1032*10588SEric.Taylor@Sun.COM 	 */
1033*10588SEric.Taylor@Sun.COM 	if (vdev_uses_zvols(vd)) {
1034*10588SEric.Taylor@Sun.COM 		for (int c = 0; c < children; c++)
1035*10588SEric.Taylor@Sun.COM 			vd->vdev_child[c]->vdev_open_error =
1036*10588SEric.Taylor@Sun.COM 			    vdev_open(vd->vdev_child[c]);
1037*10588SEric.Taylor@Sun.COM 		return;
1038*10588SEric.Taylor@Sun.COM 	}
10399846SEric.Taylor@Sun.COM 	tq = taskq_create("vdev_open", children, minclsyspri,
10409846SEric.Taylor@Sun.COM 	    children, children, TASKQ_PREPOPULATE);
10419846SEric.Taylor@Sun.COM 
10429846SEric.Taylor@Sun.COM 	for (int c = 0; c < children; c++)
10439846SEric.Taylor@Sun.COM 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
10449846SEric.Taylor@Sun.COM 		    TQ_SLEEP) != NULL);
10459846SEric.Taylor@Sun.COM 
10469846SEric.Taylor@Sun.COM 	taskq_destroy(tq);
10479846SEric.Taylor@Sun.COM }
10489846SEric.Taylor@Sun.COM 
1049789Sahrens /*
1050789Sahrens  * Prepare a virtual device for access.
1051789Sahrens  */
1052789Sahrens int
1053789Sahrens vdev_open(vdev_t *vd)
1054789Sahrens {
10558241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
1056789Sahrens 	int error;
1057789Sahrens 	uint64_t osize = 0;
1058789Sahrens 	uint64_t asize, psize;
10591732Sbonwick 	uint64_t ashift = 0;
1060789Sahrens 
10619846SEric.Taylor@Sun.COM 	ASSERT(vd->vdev_open_thread == curthread ||
10629846SEric.Taylor@Sun.COM 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1063789Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1064789Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1065789Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1066789Sahrens 
1067789Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
10689701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_read = B_FALSE;
10699701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_write = B_FALSE;
10709816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1071789Sahrens 
10724451Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
10734451Seschrock 		ASSERT(vd->vdev_children == 0);
10744451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
10754451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
10764451Seschrock 		return (ENXIO);
10774451Seschrock 	} else if (vd->vdev_offline) {
1078789Sahrens 		ASSERT(vd->vdev_children == 0);
10791544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1080789Sahrens 		return (ENXIO);
1081789Sahrens 	}
1082789Sahrens 
1083789Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1084789Sahrens 
10851544Seschrock 	if (zio_injection_enabled && error == 0)
10869725SEric.Schrock@Sun.COM 		error = zio_handle_device_injection(vd, NULL, ENXIO);
10871544Seschrock 
10884451Seschrock 	if (error) {
10894451Seschrock 		if (vd->vdev_removed &&
10904451Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
10914451Seschrock 			vd->vdev_removed = B_FALSE;
1092789Sahrens 
10931544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1094789Sahrens 		    vd->vdev_stat.vs_aux);
1095789Sahrens 		return (error);
1096789Sahrens 	}
1097789Sahrens 
10984451Seschrock 	vd->vdev_removed = B_FALSE;
10994451Seschrock 
11004451Seschrock 	if (vd->vdev_degraded) {
11014451Seschrock 		ASSERT(vd->vdev_children == 0);
11024451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11034451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
11044451Seschrock 	} else {
11054451Seschrock 		vd->vdev_state = VDEV_STATE_HEALTHY;
11064451Seschrock 	}
1107789Sahrens 
11089816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
11091544Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
11101544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11111544Seschrock 			    VDEV_AUX_NONE);
11121544Seschrock 			break;
11131544Seschrock 		}
11149816SGeorge.Wilson@Sun.COM 	}
1115789Sahrens 
1116789Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1117789Sahrens 
1118789Sahrens 	if (vd->vdev_children == 0) {
1119789Sahrens 		if (osize < SPA_MINDEVSIZE) {
11201544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11211544Seschrock 			    VDEV_AUX_TOO_SMALL);
1122789Sahrens 			return (EOVERFLOW);
1123789Sahrens 		}
1124789Sahrens 		psize = osize;
1125789Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1126789Sahrens 	} else {
11271732Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1128789Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
11291544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11301544Seschrock 			    VDEV_AUX_TOO_SMALL);
1131789Sahrens 			return (EOVERFLOW);
1132789Sahrens 		}
1133789Sahrens 		psize = 0;
1134789Sahrens 		asize = osize;
1135789Sahrens 	}
1136789Sahrens 
1137789Sahrens 	vd->vdev_psize = psize;
1138789Sahrens 
11399816SGeorge.Wilson@Sun.COM 	/*
11409816SGeorge.Wilson@Sun.COM 	 * Make sure the allocatable size hasn't shrunk.
11419816SGeorge.Wilson@Sun.COM 	 */
11429816SGeorge.Wilson@Sun.COM 	if (asize < vd->vdev_min_asize) {
11439816SGeorge.Wilson@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11449816SGeorge.Wilson@Sun.COM 		    VDEV_AUX_BAD_LABEL);
11459816SGeorge.Wilson@Sun.COM 		return (EINVAL);
11469816SGeorge.Wilson@Sun.COM 	}
11479816SGeorge.Wilson@Sun.COM 
1148789Sahrens 	if (vd->vdev_asize == 0) {
1149789Sahrens 		/*
1150789Sahrens 		 * This is the first-ever open, so use the computed values.
11511732Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1152789Sahrens 		 */
1153789Sahrens 		vd->vdev_asize = asize;
11541732Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1155789Sahrens 	} else {
1156789Sahrens 		/*
1157789Sahrens 		 * Make sure the alignment requirement hasn't increased.
1158789Sahrens 		 */
11591732Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
11601544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11611544Seschrock 			    VDEV_AUX_BAD_LABEL);
1162789Sahrens 			return (EINVAL);
1163789Sahrens 		}
1164789Sahrens 	}
1165789Sahrens 
11661544Seschrock 	/*
11679816SGeorge.Wilson@Sun.COM 	 * If all children are healthy and the asize has increased,
11689816SGeorge.Wilson@Sun.COM 	 * then we've experienced dynamic LUN growth.  If automatic
11699816SGeorge.Wilson@Sun.COM 	 * expansion is enabled then use the additional space.
11709816SGeorge.Wilson@Sun.COM 	 */
11719816SGeorge.Wilson@Sun.COM 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
11729816SGeorge.Wilson@Sun.COM 	    (vd->vdev_expanding || spa->spa_autoexpand))
11739816SGeorge.Wilson@Sun.COM 		vd->vdev_asize = asize;
11749816SGeorge.Wilson@Sun.COM 
11759816SGeorge.Wilson@Sun.COM 	vdev_set_min_asize(vd);
11769816SGeorge.Wilson@Sun.COM 
11779816SGeorge.Wilson@Sun.COM 	/*
11785329Sgw25295 	 * Ensure we can issue some IO before declaring the
11795329Sgw25295 	 * vdev open for business.
11805329Sgw25295 	 */
11817754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf &&
11827754SJeff.Bonwick@Sun.COM 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
11835329Sgw25295 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11847754SJeff.Bonwick@Sun.COM 		    VDEV_AUX_IO_FAILURE);
11855329Sgw25295 		return (error);
11865329Sgw25295 	}
11875329Sgw25295 
11885329Sgw25295 	/*
11897046Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
11908241SJeff.Bonwick@Sun.COM 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
11918241SJeff.Bonwick@Sun.COM 	 * since this would just restart the scrub we are already doing.
11927046Sahrens 	 */
11938241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
11948241SJeff.Bonwick@Sun.COM 	    vdev_resilver_needed(vd, NULL, NULL))
11958241SJeff.Bonwick@Sun.COM 		spa_async_request(spa, SPA_ASYNC_RESILVER);
11967046Sahrens 
1197789Sahrens 	return (0);
1198789Sahrens }
1199789Sahrens 
1200789Sahrens /*
12011986Seschrock  * Called once the vdevs are all opened, this routine validates the label
12021986Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
12034451Seschrock  * inadvertently do repair I/Os to the wrong device.
12041986Seschrock  *
12051986Seschrock  * This function will only return failure if one of the vdevs indicates that it
12061986Seschrock  * has since been destroyed or exported.  This is only possible if
12071986Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
12081986Seschrock  * will be updated but the function will return 0.
12091986Seschrock  */
12101986Seschrock int
12111986Seschrock vdev_validate(vdev_t *vd)
12121986Seschrock {
12131986Seschrock 	spa_t *spa = vd->vdev_spa;
12141986Seschrock 	nvlist_t *label;
12157754SJeff.Bonwick@Sun.COM 	uint64_t guid, top_guid;
12161986Seschrock 	uint64_t state;
12171986Seschrock 
12189816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
12191986Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
12204070Smc142369 			return (EBADF);
12211986Seschrock 
12222174Seschrock 	/*
12232174Seschrock 	 * If the device has already failed, or was marked offline, don't do
12242174Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
12252174Seschrock 	 * overwrite the previous state.
12262174Seschrock 	 */
12277754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
12281986Seschrock 
12291986Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
12301986Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12311986Seschrock 			    VDEV_AUX_BAD_LABEL);
12321986Seschrock 			return (0);
12331986Seschrock 		}
12341986Seschrock 
12351986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
12361986Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
12371986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
12381986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12391986Seschrock 			nvlist_free(label);
12401986Seschrock 			return (0);
12411986Seschrock 		}
12421986Seschrock 
12437754SJeff.Bonwick@Sun.COM 		/*
12447754SJeff.Bonwick@Sun.COM 		 * If this vdev just became a top-level vdev because its
12457754SJeff.Bonwick@Sun.COM 		 * sibling was detached, it will have adopted the parent's
12467754SJeff.Bonwick@Sun.COM 		 * vdev guid -- but the label may or may not be on disk yet.
12477754SJeff.Bonwick@Sun.COM 		 * Fortunately, either version of the label will have the
12487754SJeff.Bonwick@Sun.COM 		 * same top guid, so if we're a top-level vdev, we can
12497754SJeff.Bonwick@Sun.COM 		 * safely compare to that instead.
12507754SJeff.Bonwick@Sun.COM 		 */
12511986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
12527754SJeff.Bonwick@Sun.COM 		    &guid) != 0 ||
12537754SJeff.Bonwick@Sun.COM 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
12547754SJeff.Bonwick@Sun.COM 		    &top_guid) != 0 ||
12557754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != guid &&
12567754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
12571986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
12581986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12591986Seschrock 			nvlist_free(label);
12601986Seschrock 			return (0);
12611986Seschrock 		}
12621986Seschrock 
12631986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
12641986Seschrock 		    &state) != 0) {
12651986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
12661986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12671986Seschrock 			nvlist_free(label);
12681986Seschrock 			return (0);
12691986Seschrock 		}
12701986Seschrock 
12711986Seschrock 		nvlist_free(label);
12721986Seschrock 
127310100SLin.Ling@Sun.COM 		/*
127410100SLin.Ling@Sun.COM 		 * If spa->spa_load_verbatim is true, no need to check the
127510100SLin.Ling@Sun.COM 		 * state of the pool.
127610100SLin.Ling@Sun.COM 		 */
127710100SLin.Ling@Sun.COM 		if (!spa->spa_load_verbatim &&
127810100SLin.Ling@Sun.COM 		    spa->spa_load_state == SPA_LOAD_OPEN &&
127910100SLin.Ling@Sun.COM 		    state != POOL_STATE_ACTIVE)
12804070Smc142369 			return (EBADF);
12816976Seschrock 
12826976Seschrock 		/*
12836976Seschrock 		 * If we were able to open and validate a vdev that was
12846976Seschrock 		 * previously marked permanently unavailable, clear that state
12856976Seschrock 		 * now.
12866976Seschrock 		 */
12876976Seschrock 		if (vd->vdev_not_present)
12886976Seschrock 			vd->vdev_not_present = 0;
12891986Seschrock 	}
12901986Seschrock 
12911986Seschrock 	return (0);
12921986Seschrock }
12931986Seschrock 
12941986Seschrock /*
1295789Sahrens  * Close a virtual device.
1296789Sahrens  */
1297789Sahrens void
1298789Sahrens vdev_close(vdev_t *vd)
1299789Sahrens {
13008241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
13018241SJeff.Bonwick@Sun.COM 
13028241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
13038241SJeff.Bonwick@Sun.COM 
1304789Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1305789Sahrens 
13064451Seschrock 	vdev_cache_purge(vd);
1307789Sahrens 
13081986Seschrock 	/*
13099816SGeorge.Wilson@Sun.COM 	 * We record the previous state before we close it, so that if we are
13101986Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
13111986Seschrock 	 * it's still faulted.
13121986Seschrock 	 */
13131986Seschrock 	vd->vdev_prevstate = vd->vdev_state;
13141986Seschrock 
1315789Sahrens 	if (vd->vdev_offline)
1316789Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1317789Sahrens 	else
1318789Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
13191544Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1320789Sahrens }
1321789Sahrens 
1322789Sahrens void
13231544Seschrock vdev_reopen(vdev_t *vd)
1324789Sahrens {
13251544Seschrock 	spa_t *spa = vd->vdev_spa;
1326789Sahrens 
13277754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
13281544Seschrock 
1329789Sahrens 	vdev_close(vd);
1330789Sahrens 	(void) vdev_open(vd);
1331789Sahrens 
1332789Sahrens 	/*
13333377Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
13343377Seschrock 	 * Otherwise, a device with an invalid label could be successfully
13353377Seschrock 	 * opened in response to vdev_reopen().
13363377Seschrock 	 */
13376643Seschrock 	if (vd->vdev_aux) {
13386643Seschrock 		(void) vdev_validate_aux(vd);
13397754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) && vdev_writeable(vd) &&
13409425SEric.Schrock@Sun.COM 		    vd->vdev_aux == &spa->spa_l2cache &&
13419816SGeorge.Wilson@Sun.COM 		    !l2arc_vdev_present(vd))
13429816SGeorge.Wilson@Sun.COM 			l2arc_add_vdev(spa, vd);
13436643Seschrock 	} else {
13446643Seschrock 		(void) vdev_validate(vd);
13456643Seschrock 	}
13463377Seschrock 
13473377Seschrock 	/*
13484451Seschrock 	 * Reassess parent vdev's health.
1349789Sahrens 	 */
13504451Seschrock 	vdev_propagate_state(vd);
1351789Sahrens }
1352789Sahrens 
1353789Sahrens int
13542082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1355789Sahrens {
1356789Sahrens 	int error;
1357789Sahrens 
1358789Sahrens 	/*
1359789Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1360789Sahrens 	 * For a create, however, we want to fail the request if
1361789Sahrens 	 * there are any components we can't open.
1362789Sahrens 	 */
1363789Sahrens 	error = vdev_open(vd);
1364789Sahrens 
1365789Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1366789Sahrens 		vdev_close(vd);
1367789Sahrens 		return (error ? error : ENXIO);
1368789Sahrens 	}
1369789Sahrens 
1370789Sahrens 	/*
1371789Sahrens 	 * Recursively initialize all labels.
1372789Sahrens 	 */
13733377Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
13743377Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1375789Sahrens 		vdev_close(vd);
1376789Sahrens 		return (error);
1377789Sahrens 	}
1378789Sahrens 
1379789Sahrens 	return (0);
1380789Sahrens }
1381789Sahrens 
13821585Sbonwick void
13839816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(vdev_t *vd)
1384789Sahrens {
1385789Sahrens 	/*
1386789Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1387789Sahrens 	 */
1388789Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1389789Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1390789Sahrens }
1391789Sahrens 
1392789Sahrens void
13931732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1394789Sahrens {
13951732Sbonwick 	ASSERT(vd == vd->vdev_top);
13961732Sbonwick 	ASSERT(ISP2(flags));
1397789Sahrens 
13981732Sbonwick 	if (flags & VDD_METASLAB)
13991732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
14001732Sbonwick 
14011732Sbonwick 	if (flags & VDD_DTL)
14021732Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
14031732Sbonwick 
14041732Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1405789Sahrens }
1406789Sahrens 
14078241SJeff.Bonwick@Sun.COM /*
14088241SJeff.Bonwick@Sun.COM  * DTLs.
14098241SJeff.Bonwick@Sun.COM  *
14108241SJeff.Bonwick@Sun.COM  * A vdev's DTL (dirty time log) is the set of transaction groups for which
14118241SJeff.Bonwick@Sun.COM  * the vdev has less than perfect replication.  There are three kinds of DTL:
14128241SJeff.Bonwick@Sun.COM  *
14138241SJeff.Bonwick@Sun.COM  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
14148241SJeff.Bonwick@Sun.COM  *
14158241SJeff.Bonwick@Sun.COM  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
14168241SJeff.Bonwick@Sun.COM  *
14178241SJeff.Bonwick@Sun.COM  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
14188241SJeff.Bonwick@Sun.COM  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
14198241SJeff.Bonwick@Sun.COM  *	txgs that was scrubbed.
14208241SJeff.Bonwick@Sun.COM  *
14218241SJeff.Bonwick@Sun.COM  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
14228241SJeff.Bonwick@Sun.COM  *	persistent errors or just some device being offline.
14238241SJeff.Bonwick@Sun.COM  *	Unlike the other three, the DTL_OUTAGE map is not generally
14248241SJeff.Bonwick@Sun.COM  *	maintained; it's only computed when needed, typically to
14258241SJeff.Bonwick@Sun.COM  *	determine whether a device can be detached.
14268241SJeff.Bonwick@Sun.COM  *
14278241SJeff.Bonwick@Sun.COM  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
14288241SJeff.Bonwick@Sun.COM  * either has the data or it doesn't.
14298241SJeff.Bonwick@Sun.COM  *
14308241SJeff.Bonwick@Sun.COM  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
14318241SJeff.Bonwick@Sun.COM  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
14328241SJeff.Bonwick@Sun.COM  * if any child is less than fully replicated, then so is its parent.
14338241SJeff.Bonwick@Sun.COM  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
14348241SJeff.Bonwick@Sun.COM  * comprising only those txgs which appear in 'maxfaults' or more children;
14358241SJeff.Bonwick@Sun.COM  * those are the txgs we don't have enough replication to read.  For example,
14368241SJeff.Bonwick@Sun.COM  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
14378241SJeff.Bonwick@Sun.COM  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
14388241SJeff.Bonwick@Sun.COM  * two child DTL_MISSING maps.
14398241SJeff.Bonwick@Sun.COM  *
14408241SJeff.Bonwick@Sun.COM  * It should be clear from the above that to compute the DTLs and outage maps
14418241SJeff.Bonwick@Sun.COM  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
14428241SJeff.Bonwick@Sun.COM  * Therefore, that is all we keep on disk.  When loading the pool, or after
14438241SJeff.Bonwick@Sun.COM  * a configuration change, we generate all other DTLs from first principles.
14448241SJeff.Bonwick@Sun.COM  */
1445789Sahrens void
14468241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1447789Sahrens {
14488241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
14498241SJeff.Bonwick@Sun.COM 
14508241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
14518241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
14528241SJeff.Bonwick@Sun.COM 
1453789Sahrens 	mutex_enter(sm->sm_lock);
1454789Sahrens 	if (!space_map_contains(sm, txg, size))
1455789Sahrens 		space_map_add(sm, txg, size);
1456789Sahrens 	mutex_exit(sm->sm_lock);
1457789Sahrens }
1458789Sahrens 
14598241SJeff.Bonwick@Sun.COM boolean_t
14608241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1461789Sahrens {
14628241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
14638241SJeff.Bonwick@Sun.COM 	boolean_t dirty = B_FALSE;
14648241SJeff.Bonwick@Sun.COM 
14658241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
14668241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1467789Sahrens 
1468789Sahrens 	mutex_enter(sm->sm_lock);
14698241SJeff.Bonwick@Sun.COM 	if (sm->sm_space != 0)
14708241SJeff.Bonwick@Sun.COM 		dirty = space_map_contains(sm, txg, size);
1471789Sahrens 	mutex_exit(sm->sm_lock);
1472789Sahrens 
1473789Sahrens 	return (dirty);
1474789Sahrens }
1475789Sahrens 
14768241SJeff.Bonwick@Sun.COM boolean_t
14778241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
14788241SJeff.Bonwick@Sun.COM {
14798241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
14808241SJeff.Bonwick@Sun.COM 	boolean_t empty;
14818241SJeff.Bonwick@Sun.COM 
14828241SJeff.Bonwick@Sun.COM 	mutex_enter(sm->sm_lock);
14838241SJeff.Bonwick@Sun.COM 	empty = (sm->sm_space == 0);
14848241SJeff.Bonwick@Sun.COM 	mutex_exit(sm->sm_lock);
14858241SJeff.Bonwick@Sun.COM 
14868241SJeff.Bonwick@Sun.COM 	return (empty);
14878241SJeff.Bonwick@Sun.COM }
14888241SJeff.Bonwick@Sun.COM 
1489789Sahrens /*
1490789Sahrens  * Reassess DTLs after a config change or scrub completion.
1491789Sahrens  */
1492789Sahrens void
1493789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1494789Sahrens {
14951544Seschrock 	spa_t *spa = vd->vdev_spa;
14968241SJeff.Bonwick@Sun.COM 	avl_tree_t reftree;
14978241SJeff.Bonwick@Sun.COM 	int minref;
14988241SJeff.Bonwick@Sun.COM 
14998241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
15008241SJeff.Bonwick@Sun.COM 
15018241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
15028241SJeff.Bonwick@Sun.COM 		vdev_dtl_reassess(vd->vdev_child[c], txg,
15038241SJeff.Bonwick@Sun.COM 		    scrub_txg, scrub_done);
15048241SJeff.Bonwick@Sun.COM 
15058241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev)
15068241SJeff.Bonwick@Sun.COM 		return;
15078241SJeff.Bonwick@Sun.COM 
15088241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
1509789Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
15107046Sahrens 		if (scrub_txg != 0 &&
15117046Sahrens 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
15127046Sahrens 			/* XXX should check scrub_done? */
15137046Sahrens 			/*
15147046Sahrens 			 * We completed a scrub up to scrub_txg.  If we
15157046Sahrens 			 * did it without rebooting, then the scrub dtl
15167046Sahrens 			 * will be valid, so excise the old region and
15177046Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
15187046Sahrens 			 * dtl as-is if there was an error.
15198241SJeff.Bonwick@Sun.COM 			 *
15208241SJeff.Bonwick@Sun.COM 			 * There's little trick here: to excise the beginning
15218241SJeff.Bonwick@Sun.COM 			 * of the DTL_MISSING map, we put it into a reference
15228241SJeff.Bonwick@Sun.COM 			 * tree and then add a segment with refcnt -1 that
15238241SJeff.Bonwick@Sun.COM 			 * covers the range [0, scrub_txg).  This means
15248241SJeff.Bonwick@Sun.COM 			 * that each txg in that range has refcnt -1 or 0.
15258241SJeff.Bonwick@Sun.COM 			 * We then add DTL_SCRUB with a refcnt of 2, so that
15268241SJeff.Bonwick@Sun.COM 			 * entries in the range [0, scrub_txg) will have a
15278241SJeff.Bonwick@Sun.COM 			 * positive refcnt -- either 1 or 2.  We then convert
15288241SJeff.Bonwick@Sun.COM 			 * the reference tree into the new DTL_MISSING map.
15297046Sahrens 			 */
15308241SJeff.Bonwick@Sun.COM 			space_map_ref_create(&reftree);
15318241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
15328241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
15338241SJeff.Bonwick@Sun.COM 			space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
15348241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
15358241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_SCRUB], 2);
15368241SJeff.Bonwick@Sun.COM 			space_map_ref_generate_map(&reftree,
15378241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
15388241SJeff.Bonwick@Sun.COM 			space_map_ref_destroy(&reftree);
1539789Sahrens 		}
15408241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
15418241SJeff.Bonwick@Sun.COM 		space_map_walk(&vd->vdev_dtl[DTL_MISSING],
15428241SJeff.Bonwick@Sun.COM 		    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1543789Sahrens 		if (scrub_done)
15448241SJeff.Bonwick@Sun.COM 			space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
15458241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
15468241SJeff.Bonwick@Sun.COM 		if (!vdev_readable(vd))
15478241SJeff.Bonwick@Sun.COM 			space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
15488241SJeff.Bonwick@Sun.COM 		else
15498241SJeff.Bonwick@Sun.COM 			space_map_walk(&vd->vdev_dtl[DTL_MISSING],
15508241SJeff.Bonwick@Sun.COM 			    space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1551789Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
15527046Sahrens 
15531732Sbonwick 		if (txg != 0)
15541732Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1555789Sahrens 		return;
1556789Sahrens 	}
1557789Sahrens 
1558789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
15598241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
15608241SJeff.Bonwick@Sun.COM 		if (t == DTL_SCRUB)
15618241SJeff.Bonwick@Sun.COM 			continue;			/* leaf vdevs only */
15628241SJeff.Bonwick@Sun.COM 		if (t == DTL_PARTIAL)
15638241SJeff.Bonwick@Sun.COM 			minref = 1;			/* i.e. non-zero */
15648241SJeff.Bonwick@Sun.COM 		else if (vd->vdev_nparity != 0)
15658241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
15668241SJeff.Bonwick@Sun.COM 		else
15678241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_children;	/* any kind of mirror */
15688241SJeff.Bonwick@Sun.COM 		space_map_ref_create(&reftree);
15698241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
15708241SJeff.Bonwick@Sun.COM 			vdev_t *cvd = vd->vdev_child[c];
15718241SJeff.Bonwick@Sun.COM 			mutex_enter(&cvd->vdev_dtl_lock);
15728241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree, &cvd->vdev_dtl[t], 1);
15738241SJeff.Bonwick@Sun.COM 			mutex_exit(&cvd->vdev_dtl_lock);
15748241SJeff.Bonwick@Sun.COM 		}
15758241SJeff.Bonwick@Sun.COM 		space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
15768241SJeff.Bonwick@Sun.COM 		space_map_ref_destroy(&reftree);
15778241SJeff.Bonwick@Sun.COM 	}
1578789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1579789Sahrens }
1580789Sahrens 
1581789Sahrens static int
1582789Sahrens vdev_dtl_load(vdev_t *vd)
1583789Sahrens {
1584789Sahrens 	spa_t *spa = vd->vdev_spa;
15858241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
15861732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1587789Sahrens 	dmu_buf_t *db;
1588789Sahrens 	int error;
1589789Sahrens 
1590789Sahrens 	ASSERT(vd->vdev_children == 0);
1591789Sahrens 
1592789Sahrens 	if (smo->smo_object == 0)
1593789Sahrens 		return (0);
1594789Sahrens 
15951732Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
15961544Seschrock 		return (error);
15971732Sbonwick 
15984944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
15994944Smaybee 	bcopy(db->db_data, smo, sizeof (*smo));
16001544Seschrock 	dmu_buf_rele(db, FTAG);
1601789Sahrens 
1602789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
16038241SJeff.Bonwick@Sun.COM 	error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
16048241SJeff.Bonwick@Sun.COM 	    NULL, SM_ALLOC, smo, mos);
1605789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1606789Sahrens 
1607789Sahrens 	return (error);
1608789Sahrens }
1609789Sahrens 
1610789Sahrens void
1611789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1612789Sahrens {
1613789Sahrens 	spa_t *spa = vd->vdev_spa;
16148241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
16158241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
16161732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1617789Sahrens 	space_map_t smsync;
1618789Sahrens 	kmutex_t smlock;
1619789Sahrens 	dmu_buf_t *db;
1620789Sahrens 	dmu_tx_t *tx;
1621789Sahrens 
1622789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1623789Sahrens 
1624789Sahrens 	if (vd->vdev_detached) {
1625789Sahrens 		if (smo->smo_object != 0) {
16261732Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1627789Sahrens 			ASSERT3U(err, ==, 0);
1628789Sahrens 			smo->smo_object = 0;
1629789Sahrens 		}
1630789Sahrens 		dmu_tx_commit(tx);
1631789Sahrens 		return;
1632789Sahrens 	}
1633789Sahrens 
1634789Sahrens 	if (smo->smo_object == 0) {
1635789Sahrens 		ASSERT(smo->smo_objsize == 0);
1636789Sahrens 		ASSERT(smo->smo_alloc == 0);
16371732Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1638789Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1639789Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1640789Sahrens 		ASSERT(smo->smo_object != 0);
1641789Sahrens 		vdev_config_dirty(vd->vdev_top);
1642789Sahrens 	}
1643789Sahrens 
1644789Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1645789Sahrens 
1646789Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1647789Sahrens 	    &smlock);
1648789Sahrens 
1649789Sahrens 	mutex_enter(&smlock);
1650789Sahrens 
1651789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
16521732Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1653789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1654789Sahrens 
16551732Sbonwick 	space_map_truncate(smo, mos, tx);
16561732Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1657789Sahrens 
1658789Sahrens 	space_map_destroy(&smsync);
1659789Sahrens 
1660789Sahrens 	mutex_exit(&smlock);
1661789Sahrens 	mutex_destroy(&smlock);
1662789Sahrens 
16631732Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1664789Sahrens 	dmu_buf_will_dirty(db, tx);
16654944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
16664944Smaybee 	bcopy(smo, db->db_data, sizeof (*smo));
16671544Seschrock 	dmu_buf_rele(db, FTAG);
1668789Sahrens 
1669789Sahrens 	dmu_tx_commit(tx);
1670789Sahrens }
1671789Sahrens 
16727046Sahrens /*
16738241SJeff.Bonwick@Sun.COM  * Determine whether the specified vdev can be offlined/detached/removed
16748241SJeff.Bonwick@Sun.COM  * without losing data.
16758241SJeff.Bonwick@Sun.COM  */
16768241SJeff.Bonwick@Sun.COM boolean_t
16778241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd)
16788241SJeff.Bonwick@Sun.COM {
16798241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
16808241SJeff.Bonwick@Sun.COM 	vdev_t *tvd = vd->vdev_top;
16818241SJeff.Bonwick@Sun.COM 	uint8_t cant_read = vd->vdev_cant_read;
16828241SJeff.Bonwick@Sun.COM 	boolean_t required;
16838241SJeff.Bonwick@Sun.COM 
16848241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
16858241SJeff.Bonwick@Sun.COM 
16868241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev || vd == tvd)
16878241SJeff.Bonwick@Sun.COM 		return (B_TRUE);
16888241SJeff.Bonwick@Sun.COM 
16898241SJeff.Bonwick@Sun.COM 	/*
16908241SJeff.Bonwick@Sun.COM 	 * Temporarily mark the device as unreadable, and then determine
16918241SJeff.Bonwick@Sun.COM 	 * whether this results in any DTL outages in the top-level vdev.
16928241SJeff.Bonwick@Sun.COM 	 * If not, we can safely offline/detach/remove the device.
16938241SJeff.Bonwick@Sun.COM 	 */
16948241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = B_TRUE;
16958241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
16968241SJeff.Bonwick@Sun.COM 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
16978241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = cant_read;
16988241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
16998241SJeff.Bonwick@Sun.COM 
17008241SJeff.Bonwick@Sun.COM 	return (required);
17018241SJeff.Bonwick@Sun.COM }
17028241SJeff.Bonwick@Sun.COM 
17038241SJeff.Bonwick@Sun.COM /*
17047046Sahrens  * Determine if resilver is needed, and if so the txg range.
17057046Sahrens  */
17067046Sahrens boolean_t
17077046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
17087046Sahrens {
17097046Sahrens 	boolean_t needed = B_FALSE;
17107046Sahrens 	uint64_t thismin = UINT64_MAX;
17117046Sahrens 	uint64_t thismax = 0;
17127046Sahrens 
17137046Sahrens 	if (vd->vdev_children == 0) {
17147046Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
17158241SJeff.Bonwick@Sun.COM 		if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
17168241SJeff.Bonwick@Sun.COM 		    vdev_writeable(vd)) {
17177046Sahrens 			space_seg_t *ss;
17187046Sahrens 
17198241SJeff.Bonwick@Sun.COM 			ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
17207046Sahrens 			thismin = ss->ss_start - 1;
17218241SJeff.Bonwick@Sun.COM 			ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
17227046Sahrens 			thismax = ss->ss_end;
17237046Sahrens 			needed = B_TRUE;
17247046Sahrens 		}
17257046Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
17267046Sahrens 	} else {
17278241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
17287046Sahrens 			vdev_t *cvd = vd->vdev_child[c];
17297046Sahrens 			uint64_t cmin, cmax;
17307046Sahrens 
17317046Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
17327046Sahrens 				thismin = MIN(thismin, cmin);
17337046Sahrens 				thismax = MAX(thismax, cmax);
17347046Sahrens 				needed = B_TRUE;
17357046Sahrens 			}
17367046Sahrens 		}
17377046Sahrens 	}
17387046Sahrens 
17397046Sahrens 	if (needed && minp) {
17407046Sahrens 		*minp = thismin;
17417046Sahrens 		*maxp = thismax;
17427046Sahrens 	}
17437046Sahrens 	return (needed);
17447046Sahrens }
17457046Sahrens 
17461986Seschrock void
17471544Seschrock vdev_load(vdev_t *vd)
1748789Sahrens {
1749789Sahrens 	/*
1750789Sahrens 	 * Recursively load all children.
1751789Sahrens 	 */
17528241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
17531986Seschrock 		vdev_load(vd->vdev_child[c]);
1754789Sahrens 
1755789Sahrens 	/*
17561585Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1757789Sahrens 	 */
17581986Seschrock 	if (vd == vd->vdev_top &&
17591986Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
17601986Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
17611986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17621986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1763789Sahrens 
1764789Sahrens 	/*
1765789Sahrens 	 * If this is a leaf vdev, load its DTL.
1766789Sahrens 	 */
17671986Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
17681986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17691986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1770789Sahrens }
1771789Sahrens 
17722082Seschrock /*
17735450Sbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
17745450Sbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
17755450Sbrendan  * we make sure that we can open the underlying device, then try to read the
17765450Sbrendan  * label, and make sure that the label is sane and that it hasn't been
17775450Sbrendan  * repurposed to another pool.
17782082Seschrock  */
17792082Seschrock int
17805450Sbrendan vdev_validate_aux(vdev_t *vd)
17812082Seschrock {
17822082Seschrock 	nvlist_t *label;
17832082Seschrock 	uint64_t guid, version;
17842082Seschrock 	uint64_t state;
17852082Seschrock 
17867754SJeff.Bonwick@Sun.COM 	if (!vdev_readable(vd))
17876643Seschrock 		return (0);
17886643Seschrock 
17892082Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
17902082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
17912082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
17922082Seschrock 		return (-1);
17932082Seschrock 	}
17942082Seschrock 
17952082Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
17964577Sahrens 	    version > SPA_VERSION ||
17972082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
17982082Seschrock 	    guid != vd->vdev_guid ||
17992082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
18002082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
18012082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
18022082Seschrock 		nvlist_free(label);
18032082Seschrock 		return (-1);
18042082Seschrock 	}
18052082Seschrock 
18062082Seschrock 	/*
18072082Seschrock 	 * We don't actually check the pool state here.  If it's in fact in
18082082Seschrock 	 * use by another pool, we update this fact on the fly when requested.
18092082Seschrock 	 */
18102082Seschrock 	nvlist_free(label);
18112082Seschrock 	return (0);
18122082Seschrock }
18132082Seschrock 
1814789Sahrens void
1815789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
1816789Sahrens {
1817789Sahrens 	metaslab_t *msp;
1818789Sahrens 
1819789Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1820789Sahrens 		metaslab_sync_done(msp, txg);
1821789Sahrens }
1822789Sahrens 
1823789Sahrens void
1824789Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
1825789Sahrens {
1826789Sahrens 	spa_t *spa = vd->vdev_spa;
1827789Sahrens 	vdev_t *lvd;
1828789Sahrens 	metaslab_t *msp;
18291732Sbonwick 	dmu_tx_t *tx;
1830789Sahrens 
18311732Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
18321732Sbonwick 		ASSERT(vd == vd->vdev_top);
18331732Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
18341732Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
18351732Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
18361732Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
18371732Sbonwick 		vdev_config_dirty(vd);
18381732Sbonwick 		dmu_tx_commit(tx);
18391732Sbonwick 	}
1840789Sahrens 
18411732Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1842789Sahrens 		metaslab_sync(msp, txg);
18431732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
18441732Sbonwick 	}
1845789Sahrens 
1846789Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1847789Sahrens 		vdev_dtl_sync(lvd, txg);
1848789Sahrens 
1849789Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1850789Sahrens }
1851789Sahrens 
1852789Sahrens uint64_t
1853789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1854789Sahrens {
1855789Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1856789Sahrens }
1857789Sahrens 
18584451Seschrock /*
18594451Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
18604451Seschrock  * not be opened, and no I/O is attempted.
18614451Seschrock  */
1862789Sahrens int
18634451Seschrock vdev_fault(spa_t *spa, uint64_t guid)
18644451Seschrock {
18656643Seschrock 	vdev_t *vd;
18664451Seschrock 
18677754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
18684451Seschrock 
18696643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
18707754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
18717754SJeff.Bonwick@Sun.COM 
18724451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
18737754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
18744451Seschrock 
18754451Seschrock 	/*
18764451Seschrock 	 * Faulted state takes precedence over degraded.
18774451Seschrock 	 */
18784451Seschrock 	vd->vdev_faulted = 1ULL;
18794451Seschrock 	vd->vdev_degraded = 0ULL;
18807754SJeff.Bonwick@Sun.COM 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
18814451Seschrock 
18824451Seschrock 	/*
18838123SDavid.Marker@sun.com 	 * If marking the vdev as faulted cause the top-level vdev to become
18844451Seschrock 	 * unavailable, then back off and simply mark the vdev as degraded
18854451Seschrock 	 * instead.
18864451Seschrock 	 */
18876643Seschrock 	if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
18884451Seschrock 		vd->vdev_degraded = 1ULL;
18894451Seschrock 		vd->vdev_faulted = 0ULL;
18904451Seschrock 
18914451Seschrock 		/*
18924451Seschrock 		 * If we reopen the device and it's not dead, only then do we
18934451Seschrock 		 * mark it degraded.
18944451Seschrock 		 */
18954451Seschrock 		vdev_reopen(vd);
18964451Seschrock 
18975329Sgw25295 		if (vdev_readable(vd)) {
18984451Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
18994451Seschrock 			    VDEV_AUX_ERR_EXCEEDED);
19004451Seschrock 		}
19014451Seschrock 	}
19024451Seschrock 
19037754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
19044451Seschrock }
19054451Seschrock 
19064451Seschrock /*
19074451Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
19084451Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
19094451Seschrock  * as I/O is concerned.
19104451Seschrock  */
19114451Seschrock int
19124451Seschrock vdev_degrade(spa_t *spa, uint64_t guid)
19134451Seschrock {
19146643Seschrock 	vdev_t *vd;
19154451Seschrock 
19167754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
19174451Seschrock 
19186643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
19197754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
19207754SJeff.Bonwick@Sun.COM 
19214451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
19227754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
19234451Seschrock 
19244451Seschrock 	/*
19254451Seschrock 	 * If the vdev is already faulted, then don't do anything.
19264451Seschrock 	 */
19277754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded)
19287754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, 0));
19294451Seschrock 
19304451Seschrock 	vd->vdev_degraded = 1ULL;
19314451Seschrock 	if (!vdev_is_dead(vd))
19324451Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
19334451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
19344451Seschrock 
19357754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
19364451Seschrock }
19374451Seschrock 
19384451Seschrock /*
19394451Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
19404451Seschrock  * any attached spare device should be detached when the device finishes
19414451Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
19424451Seschrock  * so no FMA events are generated if the device fails to open.
19434451Seschrock  */
19444451Seschrock int
19457754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1946789Sahrens {
19479816SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
1948789Sahrens 
19497754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
19501485Slling 
19516643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
19527754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1953789Sahrens 
19541585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
19557754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
19561585Sbonwick 
19579816SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
1958789Sahrens 	vd->vdev_offline = B_FALSE;
19591485Slling 	vd->vdev_tmpoffline = B_FALSE;
19607754SJeff.Bonwick@Sun.COM 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
19617754SJeff.Bonwick@Sun.COM 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
19629816SGeorge.Wilson@Sun.COM 
19639816SGeorge.Wilson@Sun.COM 	/* XXX - L2ARC 1.0 does not support expansion */
19649816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
19659816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
19669816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
19679816SGeorge.Wilson@Sun.COM 	}
19689816SGeorge.Wilson@Sun.COM 
19699816SGeorge.Wilson@Sun.COM 	vdev_reopen(tvd);
19704451Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
19714451Seschrock 
19729816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
19739816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
19749816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = B_FALSE;
19759816SGeorge.Wilson@Sun.COM 	}
19769816SGeorge.Wilson@Sun.COM 
19774451Seschrock 	if (newstate)
19784451Seschrock 		*newstate = vd->vdev_state;
19794451Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
19804451Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
19814451Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
19824451Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
19834451Seschrock 		vd->vdev_unspare = B_TRUE;
1984789Sahrens 
19859816SGeorge.Wilson@Sun.COM 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
19869816SGeorge.Wilson@Sun.COM 
19879816SGeorge.Wilson@Sun.COM 		/* XXX - L2ARC 1.0 does not support expansion */
19889816SGeorge.Wilson@Sun.COM 		if (vd->vdev_aux)
19899816SGeorge.Wilson@Sun.COM 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
19909816SGeorge.Wilson@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
19919816SGeorge.Wilson@Sun.COM 	}
19928241SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
1993789Sahrens }
1994789Sahrens 
1995789Sahrens int
19964451Seschrock vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1997789Sahrens {
19989701SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd;
19999701SGeorge.Wilson@Sun.COM 	int error;
2000789Sahrens 
20017754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
2002789Sahrens 
20036643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
20047754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2005789Sahrens 
20061585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
20077754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
20081585Sbonwick 
20099701SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
20109701SGeorge.Wilson@Sun.COM 
2011789Sahrens 	/*
20121732Sbonwick 	 * If the device isn't already offline, try to offline it.
2013789Sahrens 	 */
20141732Sbonwick 	if (!vd->vdev_offline) {
20151732Sbonwick 		/*
20168241SJeff.Bonwick@Sun.COM 		 * If this device has the only valid copy of some data,
20179701SGeorge.Wilson@Sun.COM 		 * don't allow it to be offlined. Log devices are always
20189701SGeorge.Wilson@Sun.COM 		 * expendable.
20191732Sbonwick 		 */
20209701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
20219701SGeorge.Wilson@Sun.COM 		    vdev_dtl_required(vd))
20227754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2023789Sahrens 
20241732Sbonwick 		/*
20251732Sbonwick 		 * Offline this device and reopen its top-level vdev.
20269701SGeorge.Wilson@Sun.COM 		 * If the top-level vdev is a log device then just offline
20279701SGeorge.Wilson@Sun.COM 		 * it. Otherwise, if this action results in the top-level
20289701SGeorge.Wilson@Sun.COM 		 * vdev becoming unusable, undo it and fail the request.
20291732Sbonwick 		 */
20301732Sbonwick 		vd->vdev_offline = B_TRUE;
20319701SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
20329701SGeorge.Wilson@Sun.COM 
20339701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
20349701SGeorge.Wilson@Sun.COM 		    vdev_is_dead(tvd)) {
20351732Sbonwick 			vd->vdev_offline = B_FALSE;
20369701SGeorge.Wilson@Sun.COM 			vdev_reopen(tvd);
20377754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
20381732Sbonwick 		}
2039789Sahrens 	}
2040789Sahrens 
20417754SJeff.Bonwick@Sun.COM 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
20421732Sbonwick 
20439701SGeorge.Wilson@Sun.COM 	if (!tvd->vdev_islog || !vdev_is_dead(tvd))
20449701SGeorge.Wilson@Sun.COM 		return (spa_vdev_state_exit(spa, vd, 0));
20459701SGeorge.Wilson@Sun.COM 
20469701SGeorge.Wilson@Sun.COM 	(void) spa_vdev_state_exit(spa, vd, 0);
20479701SGeorge.Wilson@Sun.COM 
20489701SGeorge.Wilson@Sun.COM 	error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
20499701SGeorge.Wilson@Sun.COM 	    NULL, DS_FIND_CHILDREN);
20509701SGeorge.Wilson@Sun.COM 	if (error) {
20519701SGeorge.Wilson@Sun.COM 		(void) vdev_online(spa, guid, 0, NULL);
20529701SGeorge.Wilson@Sun.COM 		return (error);
20539701SGeorge.Wilson@Sun.COM 	}
20549701SGeorge.Wilson@Sun.COM 	/*
20559701SGeorge.Wilson@Sun.COM 	 * If we successfully offlined the log device then we need to
20569701SGeorge.Wilson@Sun.COM 	 * sync out the current txg so that the "stubby" block can be
20579701SGeorge.Wilson@Sun.COM 	 * removed by zil_sync().
20589701SGeorge.Wilson@Sun.COM 	 */
20599701SGeorge.Wilson@Sun.COM 	txg_wait_synced(spa->spa_dsl_pool, 0);
20609701SGeorge.Wilson@Sun.COM 	return (0);
2061789Sahrens }
2062789Sahrens 
20631544Seschrock /*
20641544Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
20651544Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
20661544Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
20671544Seschrock  */
20681544Seschrock void
20697754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd)
2070789Sahrens {
20717754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
20727754SJeff.Bonwick@Sun.COM 
20737754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2074789Sahrens 
20751544Seschrock 	if (vd == NULL)
20767754SJeff.Bonwick@Sun.COM 		vd = rvd;
2077789Sahrens 
20781544Seschrock 	vd->vdev_stat.vs_read_errors = 0;
20791544Seschrock 	vd->vdev_stat.vs_write_errors = 0;
20801544Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2081789Sahrens 
20827754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
20837754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd->vdev_child[c]);
20844451Seschrock 
20854451Seschrock 	/*
20866959Sek110237 	 * If we're in the FAULTED state or have experienced failed I/O, then
20876959Sek110237 	 * clear the persistent state and attempt to reopen the device.  We
20886959Sek110237 	 * also mark the vdev config dirty, so that the new faulted state is
20896959Sek110237 	 * written out to disk.
20904451Seschrock 	 */
20917754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded ||
20927754SJeff.Bonwick@Sun.COM 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
20936959Sek110237 
20944451Seschrock 		vd->vdev_faulted = vd->vdev_degraded = 0;
20957754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read = B_FALSE;
20967754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write = B_FALSE;
20977754SJeff.Bonwick@Sun.COM 
20984451Seschrock 		vdev_reopen(vd);
20994451Seschrock 
21007754SJeff.Bonwick@Sun.COM 		if (vd != rvd)
21017754SJeff.Bonwick@Sun.COM 			vdev_state_dirty(vd->vdev_top);
21027754SJeff.Bonwick@Sun.COM 
21037754SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
21044808Sek110237 			spa_async_request(spa, SPA_ASYNC_RESILVER);
21054451Seschrock 
21064451Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
21074451Seschrock 	}
2108789Sahrens }
2109789Sahrens 
21107754SJeff.Bonwick@Sun.COM boolean_t
21117754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd)
21125329Sgw25295 {
21137754SJeff.Bonwick@Sun.COM 	return (vd->vdev_state < VDEV_STATE_DEGRADED);
21145329Sgw25295 }
21155329Sgw25295 
21167754SJeff.Bonwick@Sun.COM boolean_t
21177754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd)
2118789Sahrens {
21197754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2120789Sahrens }
2121789Sahrens 
21227754SJeff.Bonwick@Sun.COM boolean_t
21237754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd)
2124789Sahrens {
21257754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
21267754SJeff.Bonwick@Sun.COM }
2127789Sahrens 
21287754SJeff.Bonwick@Sun.COM boolean_t
21297980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd)
21307980SGeorge.Wilson@Sun.COM {
21318241SJeff.Bonwick@Sun.COM 	uint64_t state = vd->vdev_state;
21328241SJeff.Bonwick@Sun.COM 
21337980SGeorge.Wilson@Sun.COM 	/*
21348241SJeff.Bonwick@Sun.COM 	 * We currently allow allocations from vdevs which may be in the
21357980SGeorge.Wilson@Sun.COM 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
21367980SGeorge.Wilson@Sun.COM 	 * fails to reopen then we'll catch it later when we're holding
21378241SJeff.Bonwick@Sun.COM 	 * the proper locks.  Note that we have to get the vdev state
21388241SJeff.Bonwick@Sun.COM 	 * in a local variable because although it changes atomically,
21398241SJeff.Bonwick@Sun.COM 	 * we're asking two separate questions about it.
21407980SGeorge.Wilson@Sun.COM 	 */
21418241SJeff.Bonwick@Sun.COM 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
21427980SGeorge.Wilson@Sun.COM 	    !vd->vdev_cant_write);
21437980SGeorge.Wilson@Sun.COM }
21447980SGeorge.Wilson@Sun.COM 
21457980SGeorge.Wilson@Sun.COM boolean_t
21467754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio)
21477754SJeff.Bonwick@Sun.COM {
21487754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_vd == vd);
2149789Sahrens 
21507754SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
21517754SJeff.Bonwick@Sun.COM 		return (B_FALSE);
2152789Sahrens 
21537754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ)
21547754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_read);
2155789Sahrens 
21567754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_WRITE)
21577754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_write);
21587754SJeff.Bonwick@Sun.COM 
21597754SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2160789Sahrens }
2161789Sahrens 
2162789Sahrens /*
2163789Sahrens  * Get statistics for the given vdev.
2164789Sahrens  */
2165789Sahrens void
2166789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2167789Sahrens {
2168789Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2169789Sahrens 
2170789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2171789Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
21727046Sahrens 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
2173789Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2174789Sahrens 	vs->vs_state = vd->vdev_state;
21759816SGeorge.Wilson@Sun.COM 	vs->vs_rsize = vdev_get_min_asize(vd);
21769816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
21779816SGeorge.Wilson@Sun.COM 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2178789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2179789Sahrens 
2180789Sahrens 	/*
2181789Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2182789Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2183789Sahrens 	 */
2184789Sahrens 	if (vd == rvd) {
21857754SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
2186789Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2187789Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2188789Sahrens 
2189789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
21907754SJeff.Bonwick@Sun.COM 			for (int t = 0; t < ZIO_TYPES; t++) {
2191789Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2192789Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2193789Sahrens 			}
2194789Sahrens 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
2195789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
2196789Sahrens 		}
2197789Sahrens 	}
2198789Sahrens }
2199789Sahrens 
2200789Sahrens void
22015450Sbrendan vdev_clear_stats(vdev_t *vd)
22025450Sbrendan {
22035450Sbrendan 	mutex_enter(&vd->vdev_stat_lock);
22045450Sbrendan 	vd->vdev_stat.vs_space = 0;
22055450Sbrendan 	vd->vdev_stat.vs_dspace = 0;
22065450Sbrendan 	vd->vdev_stat.vs_alloc = 0;
22075450Sbrendan 	mutex_exit(&vd->vdev_stat_lock);
22085450Sbrendan }
22095450Sbrendan 
22105450Sbrendan void
22117754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize)
2212789Sahrens {
22138241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
22148241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
22157754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2216789Sahrens 	vdev_t *pvd;
2217789Sahrens 	uint64_t txg = zio->io_txg;
2218789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2219789Sahrens 	zio_type_t type = zio->io_type;
2220789Sahrens 	int flags = zio->io_flags;
2221789Sahrens 
22227754SJeff.Bonwick@Sun.COM 	/*
22237754SJeff.Bonwick@Sun.COM 	 * If this i/o is a gang leader, it didn't do any actual work.
22247754SJeff.Bonwick@Sun.COM 	 */
22257754SJeff.Bonwick@Sun.COM 	if (zio->io_gang_tree)
22267754SJeff.Bonwick@Sun.COM 		return;
22277754SJeff.Bonwick@Sun.COM 
2228789Sahrens 	if (zio->io_error == 0) {
22297754SJeff.Bonwick@Sun.COM 		/*
22307754SJeff.Bonwick@Sun.COM 		 * If this is a root i/o, don't count it -- we've already
22317754SJeff.Bonwick@Sun.COM 		 * counted the top-level vdevs, and vdev_get_stats() will
22327754SJeff.Bonwick@Sun.COM 		 * aggregate them when asked.  This reduces contention on
22337754SJeff.Bonwick@Sun.COM 		 * the root vdev_stat_lock and implicitly handles blocks
22347754SJeff.Bonwick@Sun.COM 		 * that compress away to holes, for which there is no i/o.
22357754SJeff.Bonwick@Sun.COM 		 * (Holes never create vdev children, so all the counters
22367754SJeff.Bonwick@Sun.COM 		 * remain zero, which is what we want.)
22377754SJeff.Bonwick@Sun.COM 		 *
22387754SJeff.Bonwick@Sun.COM 		 * Note: this only applies to successful i/o (io_error == 0)
22397754SJeff.Bonwick@Sun.COM 		 * because unlike i/o counts, errors are not additive.
22407754SJeff.Bonwick@Sun.COM 		 * When reading a ditto block, for example, failure of
22417754SJeff.Bonwick@Sun.COM 		 * one top-level vdev does not imply a root-level error.
22427754SJeff.Bonwick@Sun.COM 		 */
22437754SJeff.Bonwick@Sun.COM 		if (vd == rvd)
22447754SJeff.Bonwick@Sun.COM 			return;
22457754SJeff.Bonwick@Sun.COM 
22467754SJeff.Bonwick@Sun.COM 		ASSERT(vd == zio->io_vd);
22478241SJeff.Bonwick@Sun.COM 
22488241SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_BYPASS)
22498241SJeff.Bonwick@Sun.COM 			return;
22508241SJeff.Bonwick@Sun.COM 
22518241SJeff.Bonwick@Sun.COM 		mutex_enter(&vd->vdev_stat_lock);
22528241SJeff.Bonwick@Sun.COM 
22537754SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_REPAIR) {
22541807Sbonwick 			if (flags & ZIO_FLAG_SCRUB_THREAD)
22557754SJeff.Bonwick@Sun.COM 				vs->vs_scrub_repaired += psize;
22568241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SELF_HEAL)
22577754SJeff.Bonwick@Sun.COM 				vs->vs_self_healed += psize;
2258789Sahrens 		}
22598241SJeff.Bonwick@Sun.COM 
22608241SJeff.Bonwick@Sun.COM 		vs->vs_ops[type]++;
22618241SJeff.Bonwick@Sun.COM 		vs->vs_bytes[type] += psize;
22628241SJeff.Bonwick@Sun.COM 
22638241SJeff.Bonwick@Sun.COM 		mutex_exit(&vd->vdev_stat_lock);
2264789Sahrens 		return;
2265789Sahrens 	}
2266789Sahrens 
2267789Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2268789Sahrens 		return;
2269789Sahrens 
22709725SEric.Schrock@Sun.COM 	/*
22719725SEric.Schrock@Sun.COM 	 * If this is an I/O error that is going to be retried, then ignore the
22729725SEric.Schrock@Sun.COM 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
22739725SEric.Schrock@Sun.COM 	 * hard errors, when in reality they can happen for any number of
22749725SEric.Schrock@Sun.COM 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
22759725SEric.Schrock@Sun.COM 	 */
22769725SEric.Schrock@Sun.COM 	if (zio->io_error == EIO &&
22779725SEric.Schrock@Sun.COM 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
22789725SEric.Schrock@Sun.COM 		return;
22799725SEric.Schrock@Sun.COM 
22807754SJeff.Bonwick@Sun.COM 	mutex_enter(&vd->vdev_stat_lock);
22819230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
22827754SJeff.Bonwick@Sun.COM 		if (zio->io_error == ECKSUM)
22837754SJeff.Bonwick@Sun.COM 			vs->vs_checksum_errors++;
22847754SJeff.Bonwick@Sun.COM 		else
22857754SJeff.Bonwick@Sun.COM 			vs->vs_read_errors++;
2286789Sahrens 	}
22879230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
22887754SJeff.Bonwick@Sun.COM 		vs->vs_write_errors++;
22897754SJeff.Bonwick@Sun.COM 	mutex_exit(&vd->vdev_stat_lock);
2290789Sahrens 
22918241SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
22928241SJeff.Bonwick@Sun.COM 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
22938241SJeff.Bonwick@Sun.COM 	    (flags & ZIO_FLAG_SCRUB_THREAD))) {
22948241SJeff.Bonwick@Sun.COM 		/*
22958241SJeff.Bonwick@Sun.COM 		 * This is either a normal write (not a repair), or it's a
22968241SJeff.Bonwick@Sun.COM 		 * repair induced by the scrub thread.  In the normal case,
22978241SJeff.Bonwick@Sun.COM 		 * we commit the DTL change in the same txg as the block
22988241SJeff.Bonwick@Sun.COM 		 * was born.  In the scrub-induced repair case, we know that
22998241SJeff.Bonwick@Sun.COM 		 * scrubs run in first-pass syncing context, so we commit
23008241SJeff.Bonwick@Sun.COM 		 * the DTL change in spa->spa_syncing_txg.
23018241SJeff.Bonwick@Sun.COM 		 *
23028241SJeff.Bonwick@Sun.COM 		 * We currently do not make DTL entries for failed spontaneous
23038241SJeff.Bonwick@Sun.COM 		 * self-healing writes triggered by normal (non-scrubbing)
23048241SJeff.Bonwick@Sun.COM 		 * reads, because we have no transactional context in which to
23058241SJeff.Bonwick@Sun.COM 		 * do so -- and it's not clear that it'd be desirable anyway.
23068241SJeff.Bonwick@Sun.COM 		 */
23078241SJeff.Bonwick@Sun.COM 		if (vd->vdev_ops->vdev_op_leaf) {
23088241SJeff.Bonwick@Sun.COM 			uint64_t commit_txg = txg;
23098241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SCRUB_THREAD) {
23108241SJeff.Bonwick@Sun.COM 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
23118241SJeff.Bonwick@Sun.COM 				ASSERT(spa_sync_pass(spa) == 1);
23128241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
23138241SJeff.Bonwick@Sun.COM 				commit_txg = spa->spa_syncing_txg;
23148241SJeff.Bonwick@Sun.COM 			}
23158241SJeff.Bonwick@Sun.COM 			ASSERT(commit_txg >= spa->spa_syncing_txg);
23168241SJeff.Bonwick@Sun.COM 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
23178241SJeff.Bonwick@Sun.COM 				return;
23188241SJeff.Bonwick@Sun.COM 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
23198241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
23208241SJeff.Bonwick@Sun.COM 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2321789Sahrens 		}
23228241SJeff.Bonwick@Sun.COM 		if (vd != rvd)
23238241SJeff.Bonwick@Sun.COM 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2324789Sahrens 	}
2325789Sahrens }
2326789Sahrens 
2327789Sahrens void
2328789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2329789Sahrens {
2330789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2331789Sahrens 
23329816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
2333789Sahrens 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2334789Sahrens 
2335789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2336789Sahrens 
2337789Sahrens 	if (type == POOL_SCRUB_NONE) {
2338789Sahrens 		/*
2339789Sahrens 		 * Update completion and end time.  Leave everything else alone
2340789Sahrens 		 * so we can report what happened during the previous scrub.
2341789Sahrens 		 */
2342789Sahrens 		vs->vs_scrub_complete = complete;
2343789Sahrens 		vs->vs_scrub_end = gethrestime_sec();
2344789Sahrens 	} else {
2345789Sahrens 		vs->vs_scrub_type = type;
2346789Sahrens 		vs->vs_scrub_complete = 0;
2347789Sahrens 		vs->vs_scrub_examined = 0;
2348789Sahrens 		vs->vs_scrub_repaired = 0;
2349789Sahrens 		vs->vs_scrub_start = gethrestime_sec();
2350789Sahrens 		vs->vs_scrub_end = 0;
2351789Sahrens 	}
2352789Sahrens 
2353789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2354789Sahrens }
2355789Sahrens 
2356789Sahrens /*
2357789Sahrens  * Update the in-core space usage stats for this vdev and the root vdev.
2358789Sahrens  */
2359789Sahrens void
23605450Sbrendan vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
23615450Sbrendan     boolean_t update_root)
2362789Sahrens {
23634527Sperrin 	int64_t dspace_delta = space_delta;
23644527Sperrin 	spa_t *spa = vd->vdev_spa;
23654527Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
23664527Sperrin 
2367789Sahrens 	ASSERT(vd == vd->vdev_top);
23684527Sperrin 
23694527Sperrin 	/*
23704527Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
23714527Sperrin 	 * factor.  We must calculate this here and not at the root vdev
23724527Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
23734527Sperrin 	 * childrens', thus not accurate enough for us.
23744527Sperrin 	 */
23754527Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
23769701SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
23774527Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
23784527Sperrin 	    vd->vdev_deflate_ratio;
2379789Sahrens 
23804527Sperrin 	mutex_enter(&vd->vdev_stat_lock);
23814527Sperrin 	vd->vdev_stat.vs_space += space_delta;
23824527Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
23834527Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
23844527Sperrin 	mutex_exit(&vd->vdev_stat_lock);
23852082Seschrock 
23865450Sbrendan 	if (update_root) {
23875450Sbrendan 		ASSERT(rvd == vd->vdev_parent);
23885450Sbrendan 		ASSERT(vd->vdev_ms_count != 0);
23894527Sperrin 
23905450Sbrendan 		/*
23915450Sbrendan 		 * Don't count non-normal (e.g. intent log) space as part of
23925450Sbrendan 		 * the pool's capacity.
23935450Sbrendan 		 */
23945450Sbrendan 		if (vd->vdev_mg->mg_class != spa->spa_normal_class)
23955450Sbrendan 			return;
23965450Sbrendan 
23975450Sbrendan 		mutex_enter(&rvd->vdev_stat_lock);
23985450Sbrendan 		rvd->vdev_stat.vs_space += space_delta;
23995450Sbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
24005450Sbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
24015450Sbrendan 		mutex_exit(&rvd->vdev_stat_lock);
24025450Sbrendan 	}
2403789Sahrens }
2404789Sahrens 
2405789Sahrens /*
2406789Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2407789Sahrens  * so that it will be written out next time the vdev configuration is synced.
2408789Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2409789Sahrens  */
2410789Sahrens void
2411789Sahrens vdev_config_dirty(vdev_t *vd)
2412789Sahrens {
2413789Sahrens 	spa_t *spa = vd->vdev_spa;
2414789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2415789Sahrens 	int c;
2416789Sahrens 
24171601Sbonwick 	/*
24189425SEric.Schrock@Sun.COM 	 * If this is an aux vdev (as with l2cache and spare devices), then we
24199425SEric.Schrock@Sun.COM 	 * update the vdev config manually and set the sync flag.
24206643Seschrock 	 */
24216643Seschrock 	if (vd->vdev_aux != NULL) {
24226643Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
24236643Seschrock 		nvlist_t **aux;
24246643Seschrock 		uint_t naux;
24256643Seschrock 
24266643Seschrock 		for (c = 0; c < sav->sav_count; c++) {
24276643Seschrock 			if (sav->sav_vdevs[c] == vd)
24286643Seschrock 				break;
24296643Seschrock 		}
24306643Seschrock 
24317754SJeff.Bonwick@Sun.COM 		if (c == sav->sav_count) {
24327754SJeff.Bonwick@Sun.COM 			/*
24337754SJeff.Bonwick@Sun.COM 			 * We're being removed.  There's nothing more to do.
24347754SJeff.Bonwick@Sun.COM 			 */
24357754SJeff.Bonwick@Sun.COM 			ASSERT(sav->sav_sync == B_TRUE);
24367754SJeff.Bonwick@Sun.COM 			return;
24377754SJeff.Bonwick@Sun.COM 		}
24387754SJeff.Bonwick@Sun.COM 
24396643Seschrock 		sav->sav_sync = B_TRUE;
24406643Seschrock 
24419425SEric.Schrock@Sun.COM 		if (nvlist_lookup_nvlist_array(sav->sav_config,
24429425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
24439425SEric.Schrock@Sun.COM 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
24449425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
24459425SEric.Schrock@Sun.COM 		}
24466643Seschrock 
24476643Seschrock 		ASSERT(c < naux);
24486643Seschrock 
24496643Seschrock 		/*
24506643Seschrock 		 * Setting the nvlist in the middle if the array is a little
24516643Seschrock 		 * sketchy, but it will work.
24526643Seschrock 		 */
24536643Seschrock 		nvlist_free(aux[c]);
24546643Seschrock 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
24556643Seschrock 
24566643Seschrock 		return;
24576643Seschrock 	}
24586643Seschrock 
24596643Seschrock 	/*
24607754SJeff.Bonwick@Sun.COM 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
24617754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
24627754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
24631601Sbonwick 	 * so this is sufficient to ensure mutual exclusion.
24641601Sbonwick 	 */
24657754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
24667754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
24677754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
24681601Sbonwick 
2469789Sahrens 	if (vd == rvd) {
2470789Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2471789Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2472789Sahrens 	} else {
2473789Sahrens 		ASSERT(vd == vd->vdev_top);
2474789Sahrens 
24757754SJeff.Bonwick@Sun.COM 		if (!list_link_active(&vd->vdev_config_dirty_node))
24767754SJeff.Bonwick@Sun.COM 			list_insert_head(&spa->spa_config_dirty_list, vd);
2477789Sahrens 	}
2478789Sahrens }
2479789Sahrens 
2480789Sahrens void
2481789Sahrens vdev_config_clean(vdev_t *vd)
2482789Sahrens {
24831601Sbonwick 	spa_t *spa = vd->vdev_spa;
24841601Sbonwick 
24857754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
24867754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
24877754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
24887754SJeff.Bonwick@Sun.COM 
24897754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
24907754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_config_dirty_list, vd);
24917754SJeff.Bonwick@Sun.COM }
24927754SJeff.Bonwick@Sun.COM 
24937754SJeff.Bonwick@Sun.COM /*
24947754SJeff.Bonwick@Sun.COM  * Mark a top-level vdev's state as dirty, so that the next pass of
24957754SJeff.Bonwick@Sun.COM  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
24967754SJeff.Bonwick@Sun.COM  * the state changes from larger config changes because they require
24977754SJeff.Bonwick@Sun.COM  * much less locking, and are often needed for administrative actions.
24987754SJeff.Bonwick@Sun.COM  */
24997754SJeff.Bonwick@Sun.COM void
25007754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd)
25017754SJeff.Bonwick@Sun.COM {
25027754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
25037754SJeff.Bonwick@Sun.COM 
25047754SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
25051601Sbonwick 
25067754SJeff.Bonwick@Sun.COM 	/*
25077754SJeff.Bonwick@Sun.COM 	 * The state list is protected by the SCL_STATE lock.  The caller
25087754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_STATE as writer, or must be the sync thread
25097754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
25107754SJeff.Bonwick@Sun.COM 	 * so this is sufficient to ensure mutual exclusion.
25117754SJeff.Bonwick@Sun.COM 	 */
25127754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
25137754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
25147754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
25157754SJeff.Bonwick@Sun.COM 
25167754SJeff.Bonwick@Sun.COM 	if (!list_link_active(&vd->vdev_state_dirty_node))
25177754SJeff.Bonwick@Sun.COM 		list_insert_head(&spa->spa_state_dirty_list, vd);
25187754SJeff.Bonwick@Sun.COM }
25197754SJeff.Bonwick@Sun.COM 
25207754SJeff.Bonwick@Sun.COM void
25217754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd)
25227754SJeff.Bonwick@Sun.COM {
25237754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
25247754SJeff.Bonwick@Sun.COM 
25257754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
25267754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
25277754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
25287754SJeff.Bonwick@Sun.COM 
25297754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
25307754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_state_dirty_list, vd);
2531789Sahrens }
2532789Sahrens 
25336523Sek110237 /*
25346523Sek110237  * Propagate vdev state up from children to parent.
25356523Sek110237  */
25361775Sbillm void
25371775Sbillm vdev_propagate_state(vdev_t *vd)
25381775Sbillm {
25398241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
25408241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
25411775Sbillm 	int degraded = 0, faulted = 0;
25421775Sbillm 	int corrupted = 0;
25431775Sbillm 	vdev_t *child;
25441775Sbillm 
25454451Seschrock 	if (vd->vdev_children > 0) {
25469816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
25474451Seschrock 			child = vd->vdev_child[c];
25486976Seschrock 
25497754SJeff.Bonwick@Sun.COM 			if (!vdev_readable(child) ||
25508241SJeff.Bonwick@Sun.COM 			    (!vdev_writeable(child) && spa_writeable(spa))) {
25516976Seschrock 				/*
25526976Seschrock 				 * Root special: if there is a top-level log
25536976Seschrock 				 * device, treat the root vdev as if it were
25546976Seschrock 				 * degraded.
25556976Seschrock 				 */
25566976Seschrock 				if (child->vdev_islog && vd == rvd)
25576976Seschrock 					degraded++;
25586976Seschrock 				else
25596976Seschrock 					faulted++;
25606976Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
25614451Seschrock 				degraded++;
25626976Seschrock 			}
25634451Seschrock 
25644451Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
25654451Seschrock 				corrupted++;
25664451Seschrock 		}
25671775Sbillm 
25684451Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
25694451Seschrock 
25704451Seschrock 		/*
25717754SJeff.Bonwick@Sun.COM 		 * Root special: if there is a top-level vdev that cannot be
25724451Seschrock 		 * opened due to corrupted metadata, then propagate the root
25734451Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
25744451Seschrock 		 * replicas'.
25754451Seschrock 		 */
25764451Seschrock 		if (corrupted && vd == rvd &&
25774451Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
25784451Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
25794451Seschrock 			    VDEV_AUX_CORRUPT_DATA);
25801775Sbillm 	}
25811775Sbillm 
25826976Seschrock 	if (vd->vdev_parent)
25834451Seschrock 		vdev_propagate_state(vd->vdev_parent);
25841775Sbillm }
25851775Sbillm 
2586789Sahrens /*
25871544Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
25881544Seschrock  * state, because we're in the process of opening children depth-first.
25891544Seschrock  * Otherwise, we propagate the change to the parent.
25901544Seschrock  *
25911544Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
25921544Seschrock  * generated.
2593789Sahrens  */
2594789Sahrens void
25951544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2596789Sahrens {
25971986Seschrock 	uint64_t save_state;
25986643Seschrock 	spa_t *spa = vd->vdev_spa;
25991544Seschrock 
26001544Seschrock 	if (state == vd->vdev_state) {
26011544Seschrock 		vd->vdev_stat.vs_aux = aux;
2602789Sahrens 		return;
26031544Seschrock 	}
26041544Seschrock 
26051986Seschrock 	save_state = vd->vdev_state;
2606789Sahrens 
2607789Sahrens 	vd->vdev_state = state;
2608789Sahrens 	vd->vdev_stat.vs_aux = aux;
2609789Sahrens 
26104451Seschrock 	/*
26114451Seschrock 	 * If we are setting the vdev state to anything but an open state, then
26124451Seschrock 	 * always close the underlying device.  Otherwise, we keep accessible
26134451Seschrock 	 * but invalid devices open forever.  We don't call vdev_close() itself,
26144451Seschrock 	 * because that implies some extra checks (offline, etc) that we don't
26154451Seschrock 	 * want here.  This is limited to leaf devices, because otherwise
26164451Seschrock 	 * closing the device will affect other children.
26174451Seschrock 	 */
26187780SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
26194451Seschrock 		vd->vdev_ops->vdev_op_close(vd);
26204451Seschrock 
26214451Seschrock 	if (vd->vdev_removed &&
26224451Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
26234451Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
26244451Seschrock 		/*
26254451Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
26264451Seschrock 		 * device was previously marked removed and someone attempted to
26274451Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
26284451Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
26294451Seschrock 		 * it is one of our special test online cases, which is only
26304451Seschrock 		 * attempting to online the device and shouldn't generate an FMA
26314451Seschrock 		 * fault.
26324451Seschrock 		 */
26334451Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
26344451Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
26354451Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
26364451Seschrock 		/*
26374451Seschrock 		 * Indicate to the ZFS DE that this device has been removed, and
26384451Seschrock 		 * any recent errors should be ignored.
26394451Seschrock 		 */
26406643Seschrock 		zfs_post_remove(spa, vd);
26414451Seschrock 		vd->vdev_removed = B_TRUE;
26424451Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
26431544Seschrock 		/*
26441544Seschrock 		 * If we fail to open a vdev during an import, we mark it as
26451544Seschrock 		 * "not available", which signifies that it was never there to
26461544Seschrock 		 * begin with.  Failure to open such a device is not considered
26471544Seschrock 		 * an error.
26481544Seschrock 		 */
26496643Seschrock 		if (spa->spa_load_state == SPA_LOAD_IMPORT &&
26501986Seschrock 		    vd->vdev_ops->vdev_op_leaf)
26511986Seschrock 			vd->vdev_not_present = 1;
26521986Seschrock 
26531986Seschrock 		/*
26541986Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
26551986Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
26561986Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
26571986Seschrock 		 * want to post the ereport if the device was already in the
26581986Seschrock 		 * CANT_OPEN state beforehand.
26594451Seschrock 		 *
26604451Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
26614451Seschrock 		 * online the device in response to an insertion event.  If we
26624451Seschrock 		 * hit this case, then we have detected an insertion event for a
26634451Seschrock 		 * faulted or offline device that wasn't in the removed state.
26644451Seschrock 		 * In this scenario, we don't post an ereport because we are
26654451Seschrock 		 * about to replace the device, or attempt an online with
26664451Seschrock 		 * vdev_forcefault, which will generate the fault for us.
26671986Seschrock 		 */
26684451Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
26694451Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
26706643Seschrock 		    vd != spa->spa_root_vdev) {
26711544Seschrock 			const char *class;
26721544Seschrock 
26731544Seschrock 			switch (aux) {
26741544Seschrock 			case VDEV_AUX_OPEN_FAILED:
26751544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
26761544Seschrock 				break;
26771544Seschrock 			case VDEV_AUX_CORRUPT_DATA:
26781544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
26791544Seschrock 				break;
26801544Seschrock 			case VDEV_AUX_NO_REPLICAS:
26811544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
26821544Seschrock 				break;
26831544Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
26841544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
26851544Seschrock 				break;
26861544Seschrock 			case VDEV_AUX_TOO_SMALL:
26871544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
26881544Seschrock 				break;
26891544Seschrock 			case VDEV_AUX_BAD_LABEL:
26901544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
26911544Seschrock 				break;
26927754SJeff.Bonwick@Sun.COM 			case VDEV_AUX_IO_FAILURE:
26937754SJeff.Bonwick@Sun.COM 				class = FM_EREPORT_ZFS_IO_FAILURE;
26947754SJeff.Bonwick@Sun.COM 				break;
26951544Seschrock 			default:
26961544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
26971544Seschrock 			}
26981544Seschrock 
26996643Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
27001544Seschrock 		}
27014451Seschrock 
27024451Seschrock 		/* Erase any notion of persistent removed state */
27034451Seschrock 		vd->vdev_removed = B_FALSE;
27044451Seschrock 	} else {
27054451Seschrock 		vd->vdev_removed = B_FALSE;
27061544Seschrock 	}
27071544Seschrock 
27089583STim.Haley@Sun.COM 	if (!isopen && vd->vdev_parent)
27099583STim.Haley@Sun.COM 		vdev_propagate_state(vd->vdev_parent);
2710789Sahrens }
27117042Sgw25295 
27127042Sgw25295 /*
27137042Sgw25295  * Check the vdev configuration to ensure that it's capable of supporting
27147042Sgw25295  * a root pool. Currently, we do not support RAID-Z or partial configuration.
27157042Sgw25295  * In addition, only a single top-level vdev is allowed and none of the leaves
27167042Sgw25295  * can be wholedisks.
27177042Sgw25295  */
27187042Sgw25295 boolean_t
27197042Sgw25295 vdev_is_bootable(vdev_t *vd)
27207042Sgw25295 {
27217042Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf) {
27227042Sgw25295 		char *vdev_type = vd->vdev_ops->vdev_op_type;
27237042Sgw25295 
27247042Sgw25295 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
27257042Sgw25295 		    vd->vdev_children > 1) {
27267042Sgw25295 			return (B_FALSE);
27277042Sgw25295 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
27287042Sgw25295 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
27297042Sgw25295 			return (B_FALSE);
27307042Sgw25295 		}
27317042Sgw25295 	} else if (vd->vdev_wholedisk == 1) {
27327042Sgw25295 		return (B_FALSE);
27337042Sgw25295 	}
27347042Sgw25295 
27359816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
27367042Sgw25295 		if (!vdev_is_bootable(vd->vdev_child[c]))
27377042Sgw25295 			return (B_FALSE);
27387042Sgw25295 	}
27397042Sgw25295 	return (B_TRUE);
27407042Sgw25295 }
27419701SGeorge.Wilson@Sun.COM 
27429701SGeorge.Wilson@Sun.COM void
27439701SGeorge.Wilson@Sun.COM vdev_load_log_state(vdev_t *vd, nvlist_t *nv)
27449701SGeorge.Wilson@Sun.COM {
27459816SGeorge.Wilson@Sun.COM 	uint_t children;
27469701SGeorge.Wilson@Sun.COM 	nvlist_t **child;
27479701SGeorge.Wilson@Sun.COM 	uint64_t val;
27489701SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
27499701SGeorge.Wilson@Sun.COM 
27509701SGeorge.Wilson@Sun.COM 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
27519701SGeorge.Wilson@Sun.COM 	    &child, &children) == 0) {
27529816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < children; c++)
27539701SGeorge.Wilson@Sun.COM 			vdev_load_log_state(vd->vdev_child[c], child[c]);
27549701SGeorge.Wilson@Sun.COM 	}
27559701SGeorge.Wilson@Sun.COM 
27569701SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && nvlist_lookup_uint64(nv,
27579701SGeorge.Wilson@Sun.COM 	    ZPOOL_CONFIG_OFFLINE, &val) == 0 && val) {
27589701SGeorge.Wilson@Sun.COM 
27599701SGeorge.Wilson@Sun.COM 		/*
27609701SGeorge.Wilson@Sun.COM 		 * It would be nice to call vdev_offline()
27619701SGeorge.Wilson@Sun.COM 		 * directly but the pool isn't fully loaded and
27629701SGeorge.Wilson@Sun.COM 		 * the txg threads have not been started yet.
27639701SGeorge.Wilson@Sun.COM 		 */
27649701SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_STATE_ALL, FTAG, RW_WRITER);
27659701SGeorge.Wilson@Sun.COM 		vd->vdev_offline = val;
27669701SGeorge.Wilson@Sun.COM 		vdev_reopen(vd->vdev_top);
27679701SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE_ALL, FTAG);
27689701SGeorge.Wilson@Sun.COM 	}
27699701SGeorge.Wilson@Sun.COM }
27709816SGeorge.Wilson@Sun.COM 
27719816SGeorge.Wilson@Sun.COM /*
27729816SGeorge.Wilson@Sun.COM  * Expand a vdev if possible.
27739816SGeorge.Wilson@Sun.COM  */
27749816SGeorge.Wilson@Sun.COM void
27759816SGeorge.Wilson@Sun.COM vdev_expand(vdev_t *vd, uint64_t txg)
27769816SGeorge.Wilson@Sun.COM {
27779816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_top == vd);
27789816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
27799816SGeorge.Wilson@Sun.COM 
27809816SGeorge.Wilson@Sun.COM 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
27819816SGeorge.Wilson@Sun.COM 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
27829816SGeorge.Wilson@Sun.COM 		vdev_config_dirty(vd);
27839816SGeorge.Wilson@Sun.COM 	}
27849816SGeorge.Wilson@Sun.COM }
2785