xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 13037)
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 /*
2312247SGeorge.Wilson@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/zfs_context.h>
271544Seschrock #include <sys/fm/fs/zfs.h>
28789Sahrens #include <sys/spa.h>
29789Sahrens #include <sys/spa_impl.h>
30789Sahrens #include <sys/dmu.h>
31789Sahrens #include <sys/dmu_tx.h>
32789Sahrens #include <sys/vdev_impl.h>
33789Sahrens #include <sys/uberblock_impl.h>
34789Sahrens #include <sys/metaslab.h>
35789Sahrens #include <sys/metaslab_impl.h>
36789Sahrens #include <sys/space_map.h>
37789Sahrens #include <sys/zio.h>
38789Sahrens #include <sys/zap.h>
39789Sahrens #include <sys/fs/zfs.h>
406643Seschrock #include <sys/arc.h>
419701SGeorge.Wilson@Sun.COM #include <sys/zil.h>
4212296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
43789Sahrens 
44789Sahrens /*
45789Sahrens  * Virtual device management.
46789Sahrens  */
47789Sahrens 
48789Sahrens static vdev_ops_t *vdev_ops_table[] = {
49789Sahrens 	&vdev_root_ops,
50789Sahrens 	&vdev_raidz_ops,
51789Sahrens 	&vdev_mirror_ops,
52789Sahrens 	&vdev_replacing_ops,
532082Seschrock 	&vdev_spare_ops,
54789Sahrens 	&vdev_disk_ops,
55789Sahrens 	&vdev_file_ops,
56789Sahrens 	&vdev_missing_ops,
5710594SGeorge.Wilson@Sun.COM 	&vdev_hole_ops,
58789Sahrens 	NULL
59789Sahrens };
60789Sahrens 
617046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
627046Sahrens int zfs_scrub_limit = 10;
633697Smishra 
64789Sahrens /*
65789Sahrens  * Given a vdev type, return the appropriate ops vector.
66789Sahrens  */
67789Sahrens static vdev_ops_t *
68789Sahrens vdev_getops(const char *type)
69789Sahrens {
70789Sahrens 	vdev_ops_t *ops, **opspp;
71789Sahrens 
72789Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
73789Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
74789Sahrens 			break;
75789Sahrens 
76789Sahrens 	return (ops);
77789Sahrens }
78789Sahrens 
79789Sahrens /*
80789Sahrens  * Default asize function: return the MAX of psize with the asize of
81789Sahrens  * all children.  This is what's used by anything other than RAID-Z.
82789Sahrens  */
83789Sahrens uint64_t
84789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
85789Sahrens {
861732Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
87789Sahrens 	uint64_t csize;
889816SGeorge.Wilson@Sun.COM 
899816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
90789Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
91789Sahrens 		asize = MAX(asize, csize);
92789Sahrens 	}
93789Sahrens 
94789Sahrens 	return (asize);
95789Sahrens }
96789Sahrens 
971175Slling /*
989816SGeorge.Wilson@Sun.COM  * Get the minimum allocatable size. We define the allocatable size as
999816SGeorge.Wilson@Sun.COM  * the vdev's asize rounded to the nearest metaslab. This allows us to
1009816SGeorge.Wilson@Sun.COM  * replace or attach devices which don't have the same physical size but
1019816SGeorge.Wilson@Sun.COM  * can still satisfy the same number of allocations.
1021175Slling  */
1031175Slling uint64_t
1049816SGeorge.Wilson@Sun.COM vdev_get_min_asize(vdev_t *vd)
1051175Slling {
1069816SGeorge.Wilson@Sun.COM 	vdev_t *pvd = vd->vdev_parent;
1079816SGeorge.Wilson@Sun.COM 
1089816SGeorge.Wilson@Sun.COM 	/*
1099816SGeorge.Wilson@Sun.COM 	 * The our parent is NULL (inactive spare or cache) or is the root,
1109816SGeorge.Wilson@Sun.COM 	 * just return our own asize.
1119816SGeorge.Wilson@Sun.COM 	 */
1129816SGeorge.Wilson@Sun.COM 	if (pvd == NULL)
1139816SGeorge.Wilson@Sun.COM 		return (vd->vdev_asize);
1141175Slling 
1151175Slling 	/*
1169816SGeorge.Wilson@Sun.COM 	 * The top-level vdev just returns the allocatable size rounded
1179816SGeorge.Wilson@Sun.COM 	 * to the nearest metaslab.
1189816SGeorge.Wilson@Sun.COM 	 */
1199816SGeorge.Wilson@Sun.COM 	if (vd == vd->vdev_top)
1209816SGeorge.Wilson@Sun.COM 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
1219816SGeorge.Wilson@Sun.COM 
1229816SGeorge.Wilson@Sun.COM 	/*
1239816SGeorge.Wilson@Sun.COM 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
1249816SGeorge.Wilson@Sun.COM 	 * so each child must provide at least 1/Nth of its asize.
1251175Slling 	 */
1269816SGeorge.Wilson@Sun.COM 	if (pvd->vdev_ops == &vdev_raidz_ops)
1279816SGeorge.Wilson@Sun.COM 		return (pvd->vdev_min_asize / pvd->vdev_children);
1289816SGeorge.Wilson@Sun.COM 
1299816SGeorge.Wilson@Sun.COM 	return (pvd->vdev_min_asize);
1309816SGeorge.Wilson@Sun.COM }
1319816SGeorge.Wilson@Sun.COM 
1329816SGeorge.Wilson@Sun.COM void
1339816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vdev_t *vd)
1349816SGeorge.Wilson@Sun.COM {
1359816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1369816SGeorge.Wilson@Sun.COM 
1379816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
1389816SGeorge.Wilson@Sun.COM 		vdev_set_min_asize(vd->vdev_child[c]);
1391175Slling }
1401175Slling 
141789Sahrens vdev_t *
142789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
143789Sahrens {
144789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
145789Sahrens 
1467754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1475530Sbonwick 
1487046Sahrens 	if (vdev < rvd->vdev_children) {
1497046Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
150789Sahrens 		return (rvd->vdev_child[vdev]);
1517046Sahrens 	}
152789Sahrens 
153789Sahrens 	return (NULL);
154789Sahrens }
155789Sahrens 
156789Sahrens vdev_t *
157789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
158789Sahrens {
159789Sahrens 	vdev_t *mvd;
160789Sahrens 
1611585Sbonwick 	if (vd->vdev_guid == guid)
162789Sahrens 		return (vd);
163789Sahrens 
1649816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
165789Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
166789Sahrens 		    NULL)
167789Sahrens 			return (mvd);
168789Sahrens 
169789Sahrens 	return (NULL);
170789Sahrens }
171789Sahrens 
172789Sahrens void
173789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
174789Sahrens {
175789Sahrens 	size_t oldsize, newsize;
176789Sahrens 	uint64_t id = cvd->vdev_id;
177789Sahrens 	vdev_t **newchild;
178789Sahrens 
1797754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
180789Sahrens 	ASSERT(cvd->vdev_parent == NULL);
181789Sahrens 
182789Sahrens 	cvd->vdev_parent = pvd;
183789Sahrens 
184789Sahrens 	if (pvd == NULL)
185789Sahrens 		return;
186789Sahrens 
187789Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
188789Sahrens 
189789Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
190789Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
191789Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
192789Sahrens 
193789Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
194789Sahrens 	if (pvd->vdev_child != NULL) {
195789Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
196789Sahrens 		kmem_free(pvd->vdev_child, oldsize);
197789Sahrens 	}
198789Sahrens 
199789Sahrens 	pvd->vdev_child = newchild;
200789Sahrens 	pvd->vdev_child[id] = cvd;
201789Sahrens 
202789Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
203789Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
204789Sahrens 
205789Sahrens 	/*
206789Sahrens 	 * Walk up all ancestors to update guid sum.
207789Sahrens 	 */
208789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
209789Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
210789Sahrens }
211789Sahrens 
212789Sahrens void
213789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
214789Sahrens {
215789Sahrens 	int c;
216789Sahrens 	uint_t id = cvd->vdev_id;
217789Sahrens 
218789Sahrens 	ASSERT(cvd->vdev_parent == pvd);
219789Sahrens 
220789Sahrens 	if (pvd == NULL)
221789Sahrens 		return;
222789Sahrens 
223789Sahrens 	ASSERT(id < pvd->vdev_children);
224789Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
225789Sahrens 
226789Sahrens 	pvd->vdev_child[id] = NULL;
227789Sahrens 	cvd->vdev_parent = NULL;
228789Sahrens 
229789Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
230789Sahrens 		if (pvd->vdev_child[c])
231789Sahrens 			break;
232789Sahrens 
233789Sahrens 	if (c == pvd->vdev_children) {
234789Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
235789Sahrens 		pvd->vdev_child = NULL;
236789Sahrens 		pvd->vdev_children = 0;
237789Sahrens 	}
238789Sahrens 
239789Sahrens 	/*
240789Sahrens 	 * Walk up all ancestors to update guid sum.
241789Sahrens 	 */
242789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
243789Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
244789Sahrens }
245789Sahrens 
246789Sahrens /*
247789Sahrens  * Remove any holes in the child array.
248789Sahrens  */
249789Sahrens void
250789Sahrens vdev_compact_children(vdev_t *pvd)
251789Sahrens {
252789Sahrens 	vdev_t **newchild, *cvd;
253789Sahrens 	int oldc = pvd->vdev_children;
2549816SGeorge.Wilson@Sun.COM 	int newc;
255789Sahrens 
2567754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
257789Sahrens 
2589816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++)
259789Sahrens 		if (pvd->vdev_child[c])
260789Sahrens 			newc++;
261789Sahrens 
262789Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
263789Sahrens 
2649816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++) {
265789Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
266789Sahrens 			newchild[newc] = cvd;
267789Sahrens 			cvd->vdev_id = newc++;
268789Sahrens 		}
269789Sahrens 	}
270789Sahrens 
271789Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
272789Sahrens 	pvd->vdev_child = newchild;
273789Sahrens 	pvd->vdev_children = newc;
274789Sahrens }
275789Sahrens 
276789Sahrens /*
277789Sahrens  * Allocate and minimally initialize a vdev_t.
278789Sahrens  */
27910594SGeorge.Wilson@Sun.COM vdev_t *
280789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
281789Sahrens {
282789Sahrens 	vdev_t *vd;
283789Sahrens 
2841585Sbonwick 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
2851585Sbonwick 
2861585Sbonwick 	if (spa->spa_root_vdev == NULL) {
2871585Sbonwick 		ASSERT(ops == &vdev_root_ops);
2881585Sbonwick 		spa->spa_root_vdev = vd;
2891585Sbonwick 	}
290789Sahrens 
29110594SGeorge.Wilson@Sun.COM 	if (guid == 0 && ops != &vdev_hole_ops) {
2921585Sbonwick 		if (spa->spa_root_vdev == vd) {
2931585Sbonwick 			/*
2941585Sbonwick 			 * The root vdev's guid will also be the pool guid,
2951585Sbonwick 			 * which must be unique among all pools.
2961585Sbonwick 			 */
29711422SMark.Musante@Sun.COM 			guid = spa_generate_guid(NULL);
2981585Sbonwick 		} else {
2991585Sbonwick 			/*
3001585Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3011585Sbonwick 			 */
30211422SMark.Musante@Sun.COM 			guid = spa_generate_guid(spa);
3031585Sbonwick 		}
3041585Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3051585Sbonwick 	}
306789Sahrens 
307789Sahrens 	vd->vdev_spa = spa;
308789Sahrens 	vd->vdev_id = id;
309789Sahrens 	vd->vdev_guid = guid;
310789Sahrens 	vd->vdev_guid_sum = guid;
311789Sahrens 	vd->vdev_ops = ops;
312789Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
31310594SGeorge.Wilson@Sun.COM 	vd->vdev_ishole = (ops == &vdev_hole_ops);
314789Sahrens 
315789Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3162856Snd150628 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
3177754SJeff.Bonwick@Sun.COM 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3188241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
3198241SJeff.Bonwick@Sun.COM 		space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
3208241SJeff.Bonwick@Sun.COM 		    &vd->vdev_dtl_lock);
3218241SJeff.Bonwick@Sun.COM 	}
322789Sahrens 	txg_list_create(&vd->vdev_ms_list,
323789Sahrens 	    offsetof(struct metaslab, ms_txg_node));
324789Sahrens 	txg_list_create(&vd->vdev_dtl_list,
325789Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
326789Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3274451Seschrock 	vdev_queue_init(vd);
3284451Seschrock 	vdev_cache_init(vd);
329789Sahrens 
330789Sahrens 	return (vd);
331789Sahrens }
332789Sahrens 
333789Sahrens /*
334789Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
335789Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
336789Sahrens  * different for each case.
337789Sahrens  */
3382082Seschrock int
3392082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
3402082Seschrock     int alloctype)
341789Sahrens {
342789Sahrens 	vdev_ops_t *ops;
343789Sahrens 	char *type;
3444527Sperrin 	uint64_t guid = 0, islog, nparity;
345789Sahrens 	vdev_t *vd;
346789Sahrens 
3477754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
348789Sahrens 
349789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
3502082Seschrock 		return (EINVAL);
351789Sahrens 
352789Sahrens 	if ((ops = vdev_getops(type)) == NULL)
3532082Seschrock 		return (EINVAL);
354789Sahrens 
355789Sahrens 	/*
356789Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
357789Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
358789Sahrens 	 */
359789Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
360789Sahrens 		uint64_t label_id;
361789Sahrens 
362789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
363789Sahrens 		    label_id != id)
3642082Seschrock 			return (EINVAL);
365789Sahrens 
366789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3672082Seschrock 			return (EINVAL);
3682082Seschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
3692082Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3702082Seschrock 			return (EINVAL);
3715450Sbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
3725450Sbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3735450Sbrendan 			return (EINVAL);
3749790SLin.Ling@Sun.COM 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
3759790SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3769790SLin.Ling@Sun.COM 			return (EINVAL);
377789Sahrens 	}
378789Sahrens 
3792082Seschrock 	/*
3802082Seschrock 	 * The first allocated vdev must be of type 'root'.
3812082Seschrock 	 */
3822082Seschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
3832082Seschrock 		return (EINVAL);
3842082Seschrock 
3854527Sperrin 	/*
3864527Sperrin 	 * Determine whether we're a log vdev.
3874527Sperrin 	 */
3884527Sperrin 	islog = 0;
3894527Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
3905094Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
3914527Sperrin 		return (ENOTSUP);
3924527Sperrin 
39310594SGeorge.Wilson@Sun.COM 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
39410594SGeorge.Wilson@Sun.COM 		return (ENOTSUP);
39510594SGeorge.Wilson@Sun.COM 
3964527Sperrin 	/*
3974527Sperrin 	 * Set the nparity property for RAID-Z vdevs.
3984527Sperrin 	 */
3994527Sperrin 	nparity = -1ULL;
4004527Sperrin 	if (ops == &vdev_raidz_ops) {
4014527Sperrin 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4024527Sperrin 		    &nparity) == 0) {
40310922SJeff.Bonwick@Sun.COM 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
4044527Sperrin 				return (EINVAL);
4054527Sperrin 			/*
40610105Sadam.leventhal@sun.com 			 * Previous versions could only support 1 or 2 parity
40710105Sadam.leventhal@sun.com 			 * device.
4084527Sperrin 			 */
40910105Sadam.leventhal@sun.com 			if (nparity > 1 &&
41010105Sadam.leventhal@sun.com 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
41110105Sadam.leventhal@sun.com 				return (ENOTSUP);
41210105Sadam.leventhal@sun.com 			if (nparity > 2 &&
41310105Sadam.leventhal@sun.com 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
4144527Sperrin 				return (ENOTSUP);
4154527Sperrin 		} else {
4164527Sperrin 			/*
4174527Sperrin 			 * We require the parity to be specified for SPAs that
4184527Sperrin 			 * support multiple parity levels.
4194527Sperrin 			 */
42010105Sadam.leventhal@sun.com 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
4214527Sperrin 				return (EINVAL);
4224527Sperrin 			/*
4234527Sperrin 			 * Otherwise, we default to 1 parity device for RAID-Z.
4244527Sperrin 			 */
4254527Sperrin 			nparity = 1;
4264527Sperrin 		}
4274527Sperrin 	} else {
4284527Sperrin 		nparity = 0;
4294527Sperrin 	}
4304527Sperrin 	ASSERT(nparity != -1ULL);
4314527Sperrin 
432789Sahrens 	vd = vdev_alloc_common(spa, id, guid, ops);
433789Sahrens 
4344527Sperrin 	vd->vdev_islog = islog;
4354527Sperrin 	vd->vdev_nparity = nparity;
4364527Sperrin 
437789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
438789Sahrens 		vd->vdev_path = spa_strdup(vd->vdev_path);
439789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
440789Sahrens 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4414451Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4424451Seschrock 	    &vd->vdev_physpath) == 0)
4434451Seschrock 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4449425SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4459425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
446789Sahrens 
447789Sahrens 	/*
4481171Seschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
4491171Seschrock 	 * as -1.
4501171Seschrock 	 */
4511171Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
4521171Seschrock 	    &vd->vdev_wholedisk) != 0)
4531171Seschrock 		vd->vdev_wholedisk = -1ULL;
4541171Seschrock 
4551171Seschrock 	/*
4561544Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
4571544Seschrock 	 * was not present at the time of import.
4581544Seschrock 	 */
4599425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4609425SEric.Schrock@Sun.COM 	    &vd->vdev_not_present);
4611544Seschrock 
4621544Seschrock 	/*
4631732Sbonwick 	 * Get the alignment requirement.
4641732Sbonwick 	 */
4651732Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
4661732Sbonwick 
4671732Sbonwick 	/*
46810594SGeorge.Wilson@Sun.COM 	 * Retrieve the vdev creation time.
46910594SGeorge.Wilson@Sun.COM 	 */
47010594SGeorge.Wilson@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
47110594SGeorge.Wilson@Sun.COM 	    &vd->vdev_crtxg);
47210594SGeorge.Wilson@Sun.COM 
47310594SGeorge.Wilson@Sun.COM 	/*
474789Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
475789Sahrens 	 */
47611422SMark.Musante@Sun.COM 	if (parent && !parent->vdev_parent &&
47711422SMark.Musante@Sun.COM 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
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);
48412296SLin.Ling@Sun.COM 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
48512296SLin.Ling@Sun.COM 		    &vd->vdev_removing);
486789Sahrens 	}
487789Sahrens 
48810974SJeff.Bonwick@Sun.COM 	if (parent && !parent->vdev_parent) {
48910974SJeff.Bonwick@Sun.COM 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
49010982SGeorge.Wilson@Sun.COM 		    alloctype == VDEV_ALLOC_ADD ||
49111422SMark.Musante@Sun.COM 		    alloctype == VDEV_ALLOC_SPLIT ||
49210982SGeorge.Wilson@Sun.COM 		    alloctype == VDEV_ALLOC_ROOTPOOL);
49310974SJeff.Bonwick@Sun.COM 		vd->vdev_mg = metaslab_group_create(islog ?
49410974SJeff.Bonwick@Sun.COM 		    spa_log_class(spa) : spa_normal_class(spa), vd);
49510974SJeff.Bonwick@Sun.COM 	}
49610974SJeff.Bonwick@Sun.COM 
497789Sahrens 	/*
4984451Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
499789Sahrens 	 */
5006643Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
5019790SLin.Ling@Sun.COM 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
5029790SLin.Ling@Sun.COM 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
5036643Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
5046643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
5058241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl_smo.smo_object);
5066643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
5076643Seschrock 			    &vd->vdev_unspare);
5086643Seschrock 		}
5099790SLin.Ling@Sun.COM 
5109790SLin.Ling@Sun.COM 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
5119790SLin.Ling@Sun.COM 			uint64_t spare = 0;
5129790SLin.Ling@Sun.COM 
5139790SLin.Ling@Sun.COM 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
5149790SLin.Ling@Sun.COM 			    &spare) == 0 && spare)
5159790SLin.Ling@Sun.COM 				spa_spare_add(vd);
5169790SLin.Ling@Sun.COM 		}
5179790SLin.Ling@Sun.COM 
5181732Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
5191732Sbonwick 		    &vd->vdev_offline);
5206643Seschrock 
521*13037SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVERING,
522*13037SMark.Musante@Sun.COM 		    &vd->vdev_resilvering);
523*13037SMark.Musante@Sun.COM 
5244451Seschrock 		/*
5254451Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5264451Seschrock 		 * state, as the diagnosis made on another system may not be
52710817SEric.Schrock@Sun.COM 		 * valid in the current context.  Local vdevs will
52810817SEric.Schrock@Sun.COM 		 * remain in the faulted state.
5294451Seschrock 		 */
53011147SGeorge.Wilson@Sun.COM 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
5314451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5324451Seschrock 			    &vd->vdev_faulted);
5334451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5344451Seschrock 			    &vd->vdev_degraded);
5354451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5364451Seschrock 			    &vd->vdev_removed);
53710817SEric.Schrock@Sun.COM 
53810817SEric.Schrock@Sun.COM 			if (vd->vdev_faulted || vd->vdev_degraded) {
53910817SEric.Schrock@Sun.COM 				char *aux;
54010817SEric.Schrock@Sun.COM 
54110817SEric.Schrock@Sun.COM 				vd->vdev_label_aux =
54210817SEric.Schrock@Sun.COM 				    VDEV_AUX_ERR_EXCEEDED;
54310817SEric.Schrock@Sun.COM 				if (nvlist_lookup_string(nv,
54410817SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
54510817SEric.Schrock@Sun.COM 				    strcmp(aux, "external") == 0)
54610817SEric.Schrock@Sun.COM 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
54710817SEric.Schrock@Sun.COM 			}
5484451Seschrock 		}
549789Sahrens 	}
550789Sahrens 
551789Sahrens 	/*
552789Sahrens 	 * Add ourselves to the parent's list of children.
553789Sahrens 	 */
554789Sahrens 	vdev_add_child(parent, vd);
555789Sahrens 
5562082Seschrock 	*vdp = vd;
5572082Seschrock 
5582082Seschrock 	return (0);
559789Sahrens }
560789Sahrens 
561789Sahrens void
562789Sahrens vdev_free(vdev_t *vd)
563789Sahrens {
5644451Seschrock 	spa_t *spa = vd->vdev_spa;
565789Sahrens 
566789Sahrens 	/*
567789Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
568789Sahrens 	 * trying to ensure complicated semantics for all callers.
569789Sahrens 	 */
570789Sahrens 	vdev_close(vd);
571789Sahrens 
5727754SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
57310922SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
574789Sahrens 
575789Sahrens 	/*
576789Sahrens 	 * Free all children.
577789Sahrens 	 */
5789816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
579789Sahrens 		vdev_free(vd->vdev_child[c]);
580789Sahrens 
581789Sahrens 	ASSERT(vd->vdev_child == NULL);
582789Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
583789Sahrens 
584789Sahrens 	/*
585789Sahrens 	 * Discard allocation state.
586789Sahrens 	 */
58710974SJeff.Bonwick@Sun.COM 	if (vd->vdev_mg != NULL) {
588789Sahrens 		vdev_metaslab_fini(vd);
58910974SJeff.Bonwick@Sun.COM 		metaslab_group_destroy(vd->vdev_mg);
59010974SJeff.Bonwick@Sun.COM 	}
591789Sahrens 
592789Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
5932082Seschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
594789Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
595789Sahrens 
596789Sahrens 	/*
597789Sahrens 	 * Remove this vdev from its parent's child list.
598789Sahrens 	 */
599789Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
600789Sahrens 
601789Sahrens 	ASSERT(vd->vdev_parent == NULL);
602789Sahrens 
6034451Seschrock 	/*
6044451Seschrock 	 * Clean up vdev structure.
6054451Seschrock 	 */
6064451Seschrock 	vdev_queue_fini(vd);
6074451Seschrock 	vdev_cache_fini(vd);
6084451Seschrock 
6094451Seschrock 	if (vd->vdev_path)
6104451Seschrock 		spa_strfree(vd->vdev_path);
6114451Seschrock 	if (vd->vdev_devid)
6124451Seschrock 		spa_strfree(vd->vdev_devid);
6134451Seschrock 	if (vd->vdev_physpath)
6144451Seschrock 		spa_strfree(vd->vdev_physpath);
6159425SEric.Schrock@Sun.COM 	if (vd->vdev_fru)
6169425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_fru);
6174451Seschrock 
6184451Seschrock 	if (vd->vdev_isspare)
6194451Seschrock 		spa_spare_remove(vd);
6205450Sbrendan 	if (vd->vdev_isl2cache)
6215450Sbrendan 		spa_l2cache_remove(vd);
6224451Seschrock 
6234451Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
6244451Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
6258241SJeff.Bonwick@Sun.COM 
6264451Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
6278241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
6288241SJeff.Bonwick@Sun.COM 		space_map_unload(&vd->vdev_dtl[t]);
6298241SJeff.Bonwick@Sun.COM 		space_map_destroy(&vd->vdev_dtl[t]);
6308241SJeff.Bonwick@Sun.COM 	}
6314451Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
6328241SJeff.Bonwick@Sun.COM 
6334451Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6344451Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
6357754SJeff.Bonwick@Sun.COM 	mutex_destroy(&vd->vdev_probe_lock);
6364451Seschrock 
6374451Seschrock 	if (vd == spa->spa_root_vdev)
6384451Seschrock 		spa->spa_root_vdev = NULL;
6394451Seschrock 
6404451Seschrock 	kmem_free(vd, sizeof (vdev_t));
641789Sahrens }
642789Sahrens 
643789Sahrens /*
644789Sahrens  * Transfer top-level vdev state from svd to tvd.
645789Sahrens  */
646789Sahrens static void
647789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
648789Sahrens {
649789Sahrens 	spa_t *spa = svd->vdev_spa;
650789Sahrens 	metaslab_t *msp;
651789Sahrens 	vdev_t *vd;
652789Sahrens 	int t;
653789Sahrens 
654789Sahrens 	ASSERT(tvd == tvd->vdev_top);
655789Sahrens 
656789Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
657789Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
658789Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
659789Sahrens 
660789Sahrens 	svd->vdev_ms_array = 0;
661789Sahrens 	svd->vdev_ms_shift = 0;
662789Sahrens 	svd->vdev_ms_count = 0;
663789Sahrens 
664789Sahrens 	tvd->vdev_mg = svd->vdev_mg;
665789Sahrens 	tvd->vdev_ms = svd->vdev_ms;
666789Sahrens 
667789Sahrens 	svd->vdev_mg = NULL;
668789Sahrens 	svd->vdev_ms = NULL;
6691732Sbonwick 
6701732Sbonwick 	if (tvd->vdev_mg != NULL)
6711732Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
672789Sahrens 
673789Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
674789Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
6752082Seschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
676789Sahrens 
677789Sahrens 	svd->vdev_stat.vs_alloc = 0;
678789Sahrens 	svd->vdev_stat.vs_space = 0;
6792082Seschrock 	svd->vdev_stat.vs_dspace = 0;
680789Sahrens 
681789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
682789Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
683789Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
684789Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
685789Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
686789Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
687789Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
688789Sahrens 	}
689789Sahrens 
6907754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_config_dirty_node)) {
691789Sahrens 		vdev_config_clean(svd);
692789Sahrens 		vdev_config_dirty(tvd);
693789Sahrens 	}
694789Sahrens 
6957754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_state_dirty_node)) {
6967754SJeff.Bonwick@Sun.COM 		vdev_state_clean(svd);
6977754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(tvd);
6987754SJeff.Bonwick@Sun.COM 	}
6997754SJeff.Bonwick@Sun.COM 
7002082Seschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
7012082Seschrock 	svd->vdev_deflate_ratio = 0;
7024527Sperrin 
7034527Sperrin 	tvd->vdev_islog = svd->vdev_islog;
7044527Sperrin 	svd->vdev_islog = 0;
705789Sahrens }
706789Sahrens 
707789Sahrens static void
708789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
709789Sahrens {
710789Sahrens 	if (vd == NULL)
711789Sahrens 		return;
712789Sahrens 
713789Sahrens 	vd->vdev_top = tvd;
714789Sahrens 
7159816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
716789Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
717789Sahrens }
718789Sahrens 
719789Sahrens /*
720789Sahrens  * Add a mirror/replacing vdev above an existing vdev.
721789Sahrens  */
722789Sahrens vdev_t *
723789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
724789Sahrens {
725789Sahrens 	spa_t *spa = cvd->vdev_spa;
726789Sahrens 	vdev_t *pvd = cvd->vdev_parent;
727789Sahrens 	vdev_t *mvd;
728789Sahrens 
7297754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
730789Sahrens 
731789Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
7321732Sbonwick 
7331732Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
7349816SGeorge.Wilson@Sun.COM 	mvd->vdev_min_asize = cvd->vdev_min_asize;
7351732Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
7361732Sbonwick 	mvd->vdev_state = cvd->vdev_state;
73710594SGeorge.Wilson@Sun.COM 	mvd->vdev_crtxg = cvd->vdev_crtxg;
7381732Sbonwick 
739789Sahrens 	vdev_remove_child(pvd, cvd);
740789Sahrens 	vdev_add_child(pvd, mvd);
741789Sahrens 	cvd->vdev_id = mvd->vdev_children;
742789Sahrens 	vdev_add_child(mvd, cvd);
743789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
744789Sahrens 
745789Sahrens 	if (mvd == mvd->vdev_top)
746789Sahrens 		vdev_top_transfer(cvd, mvd);
747789Sahrens 
748789Sahrens 	return (mvd);
749789Sahrens }
750789Sahrens 
751789Sahrens /*
752789Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
753789Sahrens  */
754789Sahrens void
755789Sahrens vdev_remove_parent(vdev_t *cvd)
756789Sahrens {
757789Sahrens 	vdev_t *mvd = cvd->vdev_parent;
758789Sahrens 	vdev_t *pvd = mvd->vdev_parent;
759789Sahrens 
7607754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
761789Sahrens 
762789Sahrens 	ASSERT(mvd->vdev_children == 1);
763789Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
7642082Seschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
7652082Seschrock 	    mvd->vdev_ops == &vdev_spare_ops);
7661732Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
767789Sahrens 
768789Sahrens 	vdev_remove_child(mvd, cvd);
769789Sahrens 	vdev_remove_child(pvd, mvd);
7708241SJeff.Bonwick@Sun.COM 
7717754SJeff.Bonwick@Sun.COM 	/*
7727754SJeff.Bonwick@Sun.COM 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
7737754SJeff.Bonwick@Sun.COM 	 * Otherwise, we could have detached an offline device, and when we
7747754SJeff.Bonwick@Sun.COM 	 * go to import the pool we'll think we have two top-level vdevs,
7757754SJeff.Bonwick@Sun.COM 	 * instead of a different version of the same top-level vdev.
7767754SJeff.Bonwick@Sun.COM 	 */
7778241SJeff.Bonwick@Sun.COM 	if (mvd->vdev_top == mvd) {
7788241SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
77911422SMark.Musante@Sun.COM 		cvd->vdev_orig_guid = cvd->vdev_guid;
7808241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid += guid_delta;
7818241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid_sum += guid_delta;
7828241SJeff.Bonwick@Sun.COM 	}
783789Sahrens 	cvd->vdev_id = mvd->vdev_id;
784789Sahrens 	vdev_add_child(pvd, cvd);
785789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
786789Sahrens 
787789Sahrens 	if (cvd == cvd->vdev_top)
788789Sahrens 		vdev_top_transfer(mvd, cvd);
789789Sahrens 
790789Sahrens 	ASSERT(mvd->vdev_children == 0);
791789Sahrens 	vdev_free(mvd);
792789Sahrens }
793789Sahrens 
7941544Seschrock int
795789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
796789Sahrens {
797789Sahrens 	spa_t *spa = vd->vdev_spa;
7981732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
7991732Sbonwick 	uint64_t m;
800789Sahrens 	uint64_t oldc = vd->vdev_ms_count;
801789Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
8021732Sbonwick 	metaslab_t **mspp;
8031732Sbonwick 	int error;
804789Sahrens 
80510974SJeff.Bonwick@Sun.COM 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
80610974SJeff.Bonwick@Sun.COM 
80710594SGeorge.Wilson@Sun.COM 	/*
80810594SGeorge.Wilson@Sun.COM 	 * This vdev is not being allocated from yet or is a hole.
80910594SGeorge.Wilson@Sun.COM 	 */
81010594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms_shift == 0)
8111585Sbonwick 		return (0);
8121585Sbonwick 
81310594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
81410594SGeorge.Wilson@Sun.COM 
8159701SGeorge.Wilson@Sun.COM 	/*
8169701SGeorge.Wilson@Sun.COM 	 * Compute the raidz-deflation ratio.  Note, we hard-code
8179701SGeorge.Wilson@Sun.COM 	 * in 128k (1 << 17) because it is the current "typical" blocksize.
8189701SGeorge.Wilson@Sun.COM 	 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
8199701SGeorge.Wilson@Sun.COM 	 * or we will inconsistently account for existing bp's.
8209701SGeorge.Wilson@Sun.COM 	 */
8219701SGeorge.Wilson@Sun.COM 	vd->vdev_deflate_ratio = (1 << 17) /
8229701SGeorge.Wilson@Sun.COM 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
8239701SGeorge.Wilson@Sun.COM 
824789Sahrens 	ASSERT(oldc <= newc);
825789Sahrens 
8261732Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
8271732Sbonwick 
8281732Sbonwick 	if (oldc != 0) {
8291732Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
8301732Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
8311732Sbonwick 	}
8321732Sbonwick 
8331732Sbonwick 	vd->vdev_ms = mspp;
834789Sahrens 	vd->vdev_ms_count = newc;
835789Sahrens 
8361732Sbonwick 	for (m = oldc; m < newc; m++) {
8371732Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
838789Sahrens 		if (txg == 0) {
8391732Sbonwick 			uint64_t object = 0;
8401732Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8419512SNeil.Perrin@Sun.COM 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8429512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
8431732Sbonwick 			if (error)
8441732Sbonwick 				return (error);
8451732Sbonwick 			if (object != 0) {
8461732Sbonwick 				dmu_buf_t *db;
8471732Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
8481732Sbonwick 				if (error)
8491732Sbonwick 					return (error);
8504944Smaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
8514944Smaybee 				bcopy(db->db_data, &smo, sizeof (smo));
8521732Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
8531544Seschrock 				dmu_buf_rele(db, FTAG);
854789Sahrens 			}
855789Sahrens 		}
8561732Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
8571732Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
858789Sahrens 	}
859789Sahrens 
86010974SJeff.Bonwick@Sun.COM 	if (txg == 0)
86110974SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
86210974SJeff.Bonwick@Sun.COM 
86312296SLin.Ling@Sun.COM 	/*
86412296SLin.Ling@Sun.COM 	 * If the vdev is being removed we don't activate
86512296SLin.Ling@Sun.COM 	 * the metaslabs since we want to ensure that no new
86612296SLin.Ling@Sun.COM 	 * allocations are performed on this device.
86712296SLin.Ling@Sun.COM 	 */
86812296SLin.Ling@Sun.COM 	if (oldc == 0 && !vd->vdev_removing)
86910974SJeff.Bonwick@Sun.COM 		metaslab_group_activate(vd->vdev_mg);
87010974SJeff.Bonwick@Sun.COM 
87110974SJeff.Bonwick@Sun.COM 	if (txg == 0)
87210974SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALLOC, FTAG);
87310974SJeff.Bonwick@Sun.COM 
8741544Seschrock 	return (0);
875789Sahrens }
876789Sahrens 
877789Sahrens void
878789Sahrens vdev_metaslab_fini(vdev_t *vd)
879789Sahrens {
880789Sahrens 	uint64_t m;
881789Sahrens 	uint64_t count = vd->vdev_ms_count;
882789Sahrens 
883789Sahrens 	if (vd->vdev_ms != NULL) {
88410974SJeff.Bonwick@Sun.COM 		metaslab_group_passivate(vd->vdev_mg);
885789Sahrens 		for (m = 0; m < count; m++)
8861732Sbonwick 			if (vd->vdev_ms[m] != NULL)
8871732Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
888789Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
889789Sahrens 		vd->vdev_ms = NULL;
890789Sahrens 	}
891789Sahrens }
892789Sahrens 
8937754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats {
8947754SJeff.Bonwick@Sun.COM 	boolean_t	vps_readable;
8957754SJeff.Bonwick@Sun.COM 	boolean_t	vps_writeable;
8967754SJeff.Bonwick@Sun.COM 	int		vps_flags;
8977754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t;
8987754SJeff.Bonwick@Sun.COM 
8997754SJeff.Bonwick@Sun.COM static void
9007754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio)
9015329Sgw25295 {
9028241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
9038632SBill.Moore@Sun.COM 	vdev_t *vd = zio->io_vd;
9047754SJeff.Bonwick@Sun.COM 	vdev_probe_stats_t *vps = zio->io_private;
9058632SBill.Moore@Sun.COM 
9068632SBill.Moore@Sun.COM 	ASSERT(vd->vdev_probe_zio != NULL);
9077754SJeff.Bonwick@Sun.COM 
9087754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ) {
9097754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
9107754SJeff.Bonwick@Sun.COM 			vps->vps_readable = 1;
9118241SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0 && spa_writeable(spa)) {
9128632SBill.Moore@Sun.COM 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
9137754SJeff.Bonwick@Sun.COM 			    zio->io_offset, zio->io_size, zio->io_data,
9147754SJeff.Bonwick@Sun.COM 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
9157754SJeff.Bonwick@Sun.COM 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
9167754SJeff.Bonwick@Sun.COM 		} else {
9177754SJeff.Bonwick@Sun.COM 			zio_buf_free(zio->io_data, zio->io_size);
9187754SJeff.Bonwick@Sun.COM 		}
9197754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
9207754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
9217754SJeff.Bonwick@Sun.COM 			vps->vps_writeable = 1;
9227754SJeff.Bonwick@Sun.COM 		zio_buf_free(zio->io_data, zio->io_size);
9237754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_NULL) {
9248632SBill.Moore@Sun.COM 		zio_t *pio;
9257754SJeff.Bonwick@Sun.COM 
9267754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read |= !vps->vps_readable;
9277754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write |= !vps->vps_writeable;
9287754SJeff.Bonwick@Sun.COM 
9297754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) &&
9308241SJeff.Bonwick@Sun.COM 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
9317754SJeff.Bonwick@Sun.COM 			zio->io_error = 0;
9327754SJeff.Bonwick@Sun.COM 		} else {
9337754SJeff.Bonwick@Sun.COM 			ASSERT(zio->io_error != 0);
9347754SJeff.Bonwick@Sun.COM 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
9358241SJeff.Bonwick@Sun.COM 			    spa, vd, NULL, 0, 0);
9367754SJeff.Bonwick@Sun.COM 			zio->io_error = ENXIO;
9377754SJeff.Bonwick@Sun.COM 		}
9388632SBill.Moore@Sun.COM 
9398632SBill.Moore@Sun.COM 		mutex_enter(&vd->vdev_probe_lock);
9408632SBill.Moore@Sun.COM 		ASSERT(vd->vdev_probe_zio == zio);
9418632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = NULL;
9428632SBill.Moore@Sun.COM 		mutex_exit(&vd->vdev_probe_lock);
9438632SBill.Moore@Sun.COM 
9448632SBill.Moore@Sun.COM 		while ((pio = zio_walk_parents(zio)) != NULL)
9458632SBill.Moore@Sun.COM 			if (!vdev_accessible(vd, pio))
9468632SBill.Moore@Sun.COM 				pio->io_error = ENXIO;
9478632SBill.Moore@Sun.COM 
9487754SJeff.Bonwick@Sun.COM 		kmem_free(vps, sizeof (*vps));
9497754SJeff.Bonwick@Sun.COM 	}
9507754SJeff.Bonwick@Sun.COM }
9515329Sgw25295 
9527754SJeff.Bonwick@Sun.COM /*
9537754SJeff.Bonwick@Sun.COM  * Determine whether this device is accessible by reading and writing
9547754SJeff.Bonwick@Sun.COM  * to several known locations: the pad regions of each vdev label
9557754SJeff.Bonwick@Sun.COM  * but the first (which we leave alone in case it contains a VTOC).
9567754SJeff.Bonwick@Sun.COM  */
9577754SJeff.Bonwick@Sun.COM zio_t *
9588632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio)
9597754SJeff.Bonwick@Sun.COM {
9607754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
9618632SBill.Moore@Sun.COM 	vdev_probe_stats_t *vps = NULL;
9628632SBill.Moore@Sun.COM 	zio_t *pio;
9637754SJeff.Bonwick@Sun.COM 
9647754SJeff.Bonwick@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9657754SJeff.Bonwick@Sun.COM 
9668632SBill.Moore@Sun.COM 	/*
9678632SBill.Moore@Sun.COM 	 * Don't probe the probe.
9688632SBill.Moore@Sun.COM 	 */
9698632SBill.Moore@Sun.COM 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
9708632SBill.Moore@Sun.COM 		return (NULL);
9718632SBill.Moore@Sun.COM 
9728632SBill.Moore@Sun.COM 	/*
9738632SBill.Moore@Sun.COM 	 * To prevent 'probe storms' when a device fails, we create
9748632SBill.Moore@Sun.COM 	 * just one probe i/o at a time.  All zios that want to probe
9758632SBill.Moore@Sun.COM 	 * this vdev will become parents of the probe io.
9768632SBill.Moore@Sun.COM 	 */
9778632SBill.Moore@Sun.COM 	mutex_enter(&vd->vdev_probe_lock);
9788632SBill.Moore@Sun.COM 
9798632SBill.Moore@Sun.COM 	if ((pio = vd->vdev_probe_zio) == NULL) {
9808632SBill.Moore@Sun.COM 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
9818632SBill.Moore@Sun.COM 
9828632SBill.Moore@Sun.COM 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
9838632SBill.Moore@Sun.COM 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9849725SEric.Schrock@Sun.COM 		    ZIO_FLAG_TRYHARD;
9858632SBill.Moore@Sun.COM 
9868632SBill.Moore@Sun.COM 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
9878632SBill.Moore@Sun.COM 			/*
9888632SBill.Moore@Sun.COM 			 * vdev_cant_read and vdev_cant_write can only
9898632SBill.Moore@Sun.COM 			 * transition from TRUE to FALSE when we have the
9908632SBill.Moore@Sun.COM 			 * SCL_ZIO lock as writer; otherwise they can only
9918632SBill.Moore@Sun.COM 			 * transition from FALSE to TRUE.  This ensures that
9928632SBill.Moore@Sun.COM 			 * any zio looking at these values can assume that
9938632SBill.Moore@Sun.COM 			 * failures persist for the life of the I/O.  That's
9948632SBill.Moore@Sun.COM 			 * important because when a device has intermittent
9958632SBill.Moore@Sun.COM 			 * connectivity problems, we want to ensure that
9968632SBill.Moore@Sun.COM 			 * they're ascribed to the device (ENXIO) and not
9978632SBill.Moore@Sun.COM 			 * the zio (EIO).
9988632SBill.Moore@Sun.COM 			 *
9998632SBill.Moore@Sun.COM 			 * Since we hold SCL_ZIO as writer here, clear both
10008632SBill.Moore@Sun.COM 			 * values so the probe can reevaluate from first
10018632SBill.Moore@Sun.COM 			 * principles.
10028632SBill.Moore@Sun.COM 			 */
10038632SBill.Moore@Sun.COM 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
10048632SBill.Moore@Sun.COM 			vd->vdev_cant_read = B_FALSE;
10058632SBill.Moore@Sun.COM 			vd->vdev_cant_write = B_FALSE;
10068632SBill.Moore@Sun.COM 		}
10078632SBill.Moore@Sun.COM 
10088632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
10098632SBill.Moore@Sun.COM 		    vdev_probe_done, vps,
10108632SBill.Moore@Sun.COM 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
10118632SBill.Moore@Sun.COM 
101212247SGeorge.Wilson@Sun.COM 		/*
101312247SGeorge.Wilson@Sun.COM 		 * We can't change the vdev state in this context, so we
101412247SGeorge.Wilson@Sun.COM 		 * kick off an async task to do it on our behalf.
101512247SGeorge.Wilson@Sun.COM 		 */
10168632SBill.Moore@Sun.COM 		if (zio != NULL) {
10178632SBill.Moore@Sun.COM 			vd->vdev_probe_wanted = B_TRUE;
10188632SBill.Moore@Sun.COM 			spa_async_request(spa, SPA_ASYNC_PROBE);
10198632SBill.Moore@Sun.COM 		}
10208632SBill.Moore@Sun.COM 	}
10218632SBill.Moore@Sun.COM 
10228632SBill.Moore@Sun.COM 	if (zio != NULL)
10238632SBill.Moore@Sun.COM 		zio_add_child(zio, pio);
10248632SBill.Moore@Sun.COM 
10258632SBill.Moore@Sun.COM 	mutex_exit(&vd->vdev_probe_lock);
10268632SBill.Moore@Sun.COM 
10278632SBill.Moore@Sun.COM 	if (vps == NULL) {
10288632SBill.Moore@Sun.COM 		ASSERT(zio != NULL);
10298632SBill.Moore@Sun.COM 		return (NULL);
10308632SBill.Moore@Sun.COM 	}
10317754SJeff.Bonwick@Sun.COM 
10327754SJeff.Bonwick@Sun.COM 	for (int l = 1; l < VDEV_LABELS; l++) {
10338632SBill.Moore@Sun.COM 		zio_nowait(zio_read_phys(pio, vd,
10347754SJeff.Bonwick@Sun.COM 		    vdev_label_offset(vd->vdev_psize, l,
10359056SLin.Ling@Sun.COM 		    offsetof(vdev_label_t, vl_pad2)),
10369056SLin.Ling@Sun.COM 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
10377754SJeff.Bonwick@Sun.COM 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
10387754SJeff.Bonwick@Sun.COM 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
10397754SJeff.Bonwick@Sun.COM 	}
10407754SJeff.Bonwick@Sun.COM 
10418632SBill.Moore@Sun.COM 	if (zio == NULL)
10428632SBill.Moore@Sun.COM 		return (pio);
10438632SBill.Moore@Sun.COM 
10448632SBill.Moore@Sun.COM 	zio_nowait(pio);
10458632SBill.Moore@Sun.COM 	return (NULL);
10465329Sgw25295 }
10475329Sgw25295 
10489846SEric.Taylor@Sun.COM static void
10499846SEric.Taylor@Sun.COM vdev_open_child(void *arg)
10509846SEric.Taylor@Sun.COM {
10519846SEric.Taylor@Sun.COM 	vdev_t *vd = arg;
10529846SEric.Taylor@Sun.COM 
10539846SEric.Taylor@Sun.COM 	vd->vdev_open_thread = curthread;
10549846SEric.Taylor@Sun.COM 	vd->vdev_open_error = vdev_open(vd);
10559846SEric.Taylor@Sun.COM 	vd->vdev_open_thread = NULL;
10569846SEric.Taylor@Sun.COM }
10579846SEric.Taylor@Sun.COM 
105810588SEric.Taylor@Sun.COM boolean_t
105910588SEric.Taylor@Sun.COM vdev_uses_zvols(vdev_t *vd)
106010588SEric.Taylor@Sun.COM {
106110588SEric.Taylor@Sun.COM 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
106210588SEric.Taylor@Sun.COM 	    strlen(ZVOL_DIR)) == 0)
106310588SEric.Taylor@Sun.COM 		return (B_TRUE);
106410588SEric.Taylor@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
106510588SEric.Taylor@Sun.COM 		if (vdev_uses_zvols(vd->vdev_child[c]))
106610588SEric.Taylor@Sun.COM 			return (B_TRUE);
106710588SEric.Taylor@Sun.COM 	return (B_FALSE);
106810588SEric.Taylor@Sun.COM }
106910588SEric.Taylor@Sun.COM 
10709846SEric.Taylor@Sun.COM void
10719846SEric.Taylor@Sun.COM vdev_open_children(vdev_t *vd)
10729846SEric.Taylor@Sun.COM {
10739846SEric.Taylor@Sun.COM 	taskq_t *tq;
10749846SEric.Taylor@Sun.COM 	int children = vd->vdev_children;
10759846SEric.Taylor@Sun.COM 
107610588SEric.Taylor@Sun.COM 	/*
107710588SEric.Taylor@Sun.COM 	 * in order to handle pools on top of zvols, do the opens
107810588SEric.Taylor@Sun.COM 	 * in a single thread so that the same thread holds the
107910588SEric.Taylor@Sun.COM 	 * spa_namespace_lock
108010588SEric.Taylor@Sun.COM 	 */
108110588SEric.Taylor@Sun.COM 	if (vdev_uses_zvols(vd)) {
108210588SEric.Taylor@Sun.COM 		for (int c = 0; c < children; c++)
108310588SEric.Taylor@Sun.COM 			vd->vdev_child[c]->vdev_open_error =
108410588SEric.Taylor@Sun.COM 			    vdev_open(vd->vdev_child[c]);
108510588SEric.Taylor@Sun.COM 		return;
108610588SEric.Taylor@Sun.COM 	}
10879846SEric.Taylor@Sun.COM 	tq = taskq_create("vdev_open", children, minclsyspri,
10889846SEric.Taylor@Sun.COM 	    children, children, TASKQ_PREPOPULATE);
10899846SEric.Taylor@Sun.COM 
10909846SEric.Taylor@Sun.COM 	for (int c = 0; c < children; c++)
10919846SEric.Taylor@Sun.COM 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
10929846SEric.Taylor@Sun.COM 		    TQ_SLEEP) != NULL);
10939846SEric.Taylor@Sun.COM 
10949846SEric.Taylor@Sun.COM 	taskq_destroy(tq);
10959846SEric.Taylor@Sun.COM }
10969846SEric.Taylor@Sun.COM 
1097789Sahrens /*
1098789Sahrens  * Prepare a virtual device for access.
1099789Sahrens  */
1100789Sahrens int
1101789Sahrens vdev_open(vdev_t *vd)
1102789Sahrens {
11038241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
1104789Sahrens 	int error;
1105789Sahrens 	uint64_t osize = 0;
1106789Sahrens 	uint64_t asize, psize;
11071732Sbonwick 	uint64_t ashift = 0;
1108789Sahrens 
11099846SEric.Taylor@Sun.COM 	ASSERT(vd->vdev_open_thread == curthread ||
11109846SEric.Taylor@Sun.COM 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1111789Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1112789Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1113789Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1114789Sahrens 
1115789Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
11169701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_read = B_FALSE;
11179701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_write = B_FALSE;
11189816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1119789Sahrens 
112010817SEric.Schrock@Sun.COM 	/*
112110817SEric.Schrock@Sun.COM 	 * If this vdev is not removed, check its fault status.  If it's
112210817SEric.Schrock@Sun.COM 	 * faulted, bail out of the open.
112310817SEric.Schrock@Sun.COM 	 */
11244451Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
11254451Seschrock 		ASSERT(vd->vdev_children == 0);
112610817SEric.Schrock@Sun.COM 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
112710817SEric.Schrock@Sun.COM 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
11284451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
112910817SEric.Schrock@Sun.COM 		    vd->vdev_label_aux);
11304451Seschrock 		return (ENXIO);
11314451Seschrock 	} else if (vd->vdev_offline) {
1132789Sahrens 		ASSERT(vd->vdev_children == 0);
11331544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1134789Sahrens 		return (ENXIO);
1135789Sahrens 	}
1136789Sahrens 
1137789Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1138789Sahrens 
113910850SGeorge.Wilson@Sun.COM 	/*
114010850SGeorge.Wilson@Sun.COM 	 * Reset the vdev_reopening flag so that we actually close
114110850SGeorge.Wilson@Sun.COM 	 * the vdev on error.
114210850SGeorge.Wilson@Sun.COM 	 */
114310850SGeorge.Wilson@Sun.COM 	vd->vdev_reopening = B_FALSE;
11441544Seschrock 	if (zio_injection_enabled && error == 0)
11459725SEric.Schrock@Sun.COM 		error = zio_handle_device_injection(vd, NULL, ENXIO);
11461544Seschrock 
11474451Seschrock 	if (error) {
11484451Seschrock 		if (vd->vdev_removed &&
11494451Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
11504451Seschrock 			vd->vdev_removed = B_FALSE;
1151789Sahrens 
11521544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1153789Sahrens 		    vd->vdev_stat.vs_aux);
1154789Sahrens 		return (error);
1155789Sahrens 	}
1156789Sahrens 
11574451Seschrock 	vd->vdev_removed = B_FALSE;
11584451Seschrock 
115910830SEric.Schrock@Sun.COM 	/*
116010830SEric.Schrock@Sun.COM 	 * Recheck the faulted flag now that we have confirmed that
116110830SEric.Schrock@Sun.COM 	 * the vdev is accessible.  If we're faulted, bail.
116210830SEric.Schrock@Sun.COM 	 */
116310830SEric.Schrock@Sun.COM 	if (vd->vdev_faulted) {
116410830SEric.Schrock@Sun.COM 		ASSERT(vd->vdev_children == 0);
116510830SEric.Schrock@Sun.COM 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
116610830SEric.Schrock@Sun.COM 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
116710830SEric.Schrock@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
116810830SEric.Schrock@Sun.COM 		    vd->vdev_label_aux);
116910830SEric.Schrock@Sun.COM 		return (ENXIO);
117010830SEric.Schrock@Sun.COM 	}
117110830SEric.Schrock@Sun.COM 
11724451Seschrock 	if (vd->vdev_degraded) {
11734451Seschrock 		ASSERT(vd->vdev_children == 0);
11744451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11754451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
11764451Seschrock 	} else {
117710817SEric.Schrock@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
11784451Seschrock 	}
1179789Sahrens 
118010594SGeorge.Wilson@Sun.COM 	/*
118110594SGeorge.Wilson@Sun.COM 	 * For hole or missing vdevs we just return success.
118210594SGeorge.Wilson@Sun.COM 	 */
118310594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
118410594SGeorge.Wilson@Sun.COM 		return (0);
118510594SGeorge.Wilson@Sun.COM 
11869816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
11871544Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
11881544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11891544Seschrock 			    VDEV_AUX_NONE);
11901544Seschrock 			break;
11911544Seschrock 		}
11929816SGeorge.Wilson@Sun.COM 	}
1193789Sahrens 
1194789Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1195789Sahrens 
1196789Sahrens 	if (vd->vdev_children == 0) {
1197789Sahrens 		if (osize < SPA_MINDEVSIZE) {
11981544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11991544Seschrock 			    VDEV_AUX_TOO_SMALL);
1200789Sahrens 			return (EOVERFLOW);
1201789Sahrens 		}
1202789Sahrens 		psize = osize;
1203789Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1204789Sahrens 	} else {
12051732Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1206789Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
12071544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12081544Seschrock 			    VDEV_AUX_TOO_SMALL);
1209789Sahrens 			return (EOVERFLOW);
1210789Sahrens 		}
1211789Sahrens 		psize = 0;
1212789Sahrens 		asize = osize;
1213789Sahrens 	}
1214789Sahrens 
1215789Sahrens 	vd->vdev_psize = psize;
1216789Sahrens 
12179816SGeorge.Wilson@Sun.COM 	/*
12189816SGeorge.Wilson@Sun.COM 	 * Make sure the allocatable size hasn't shrunk.
12199816SGeorge.Wilson@Sun.COM 	 */
12209816SGeorge.Wilson@Sun.COM 	if (asize < vd->vdev_min_asize) {
12219816SGeorge.Wilson@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12229816SGeorge.Wilson@Sun.COM 		    VDEV_AUX_BAD_LABEL);
12239816SGeorge.Wilson@Sun.COM 		return (EINVAL);
12249816SGeorge.Wilson@Sun.COM 	}
12259816SGeorge.Wilson@Sun.COM 
1226789Sahrens 	if (vd->vdev_asize == 0) {
1227789Sahrens 		/*
1228789Sahrens 		 * This is the first-ever open, so use the computed values.
12291732Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1230789Sahrens 		 */
1231789Sahrens 		vd->vdev_asize = asize;
12321732Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1233789Sahrens 	} else {
1234789Sahrens 		/*
1235789Sahrens 		 * Make sure the alignment requirement hasn't increased.
1236789Sahrens 		 */
12371732Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
12381544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12391544Seschrock 			    VDEV_AUX_BAD_LABEL);
1240789Sahrens 			return (EINVAL);
1241789Sahrens 		}
1242789Sahrens 	}
1243789Sahrens 
12441544Seschrock 	/*
12459816SGeorge.Wilson@Sun.COM 	 * If all children are healthy and the asize has increased,
12469816SGeorge.Wilson@Sun.COM 	 * then we've experienced dynamic LUN growth.  If automatic
12479816SGeorge.Wilson@Sun.COM 	 * expansion is enabled then use the additional space.
12489816SGeorge.Wilson@Sun.COM 	 */
12499816SGeorge.Wilson@Sun.COM 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
12509816SGeorge.Wilson@Sun.COM 	    (vd->vdev_expanding || spa->spa_autoexpand))
12519816SGeorge.Wilson@Sun.COM 		vd->vdev_asize = asize;
12529816SGeorge.Wilson@Sun.COM 
12539816SGeorge.Wilson@Sun.COM 	vdev_set_min_asize(vd);
12549816SGeorge.Wilson@Sun.COM 
12559816SGeorge.Wilson@Sun.COM 	/*
12565329Sgw25295 	 * Ensure we can issue some IO before declaring the
12575329Sgw25295 	 * vdev open for business.
12585329Sgw25295 	 */
12597754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf &&
12607754SJeff.Bonwick@Sun.COM 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
126112247SGeorge.Wilson@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
126212247SGeorge.Wilson@Sun.COM 		    VDEV_AUX_ERR_EXCEEDED);
12635329Sgw25295 		return (error);
12645329Sgw25295 	}
12655329Sgw25295 
12665329Sgw25295 	/*
12677046Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
12688241SJeff.Bonwick@Sun.COM 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
12698241SJeff.Bonwick@Sun.COM 	 * since this would just restart the scrub we are already doing.
12707046Sahrens 	 */
12718241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
12728241SJeff.Bonwick@Sun.COM 	    vdev_resilver_needed(vd, NULL, NULL))
12738241SJeff.Bonwick@Sun.COM 		spa_async_request(spa, SPA_ASYNC_RESILVER);
12747046Sahrens 
1275789Sahrens 	return (0);
1276789Sahrens }
1277789Sahrens 
1278789Sahrens /*
12791986Seschrock  * Called once the vdevs are all opened, this routine validates the label
12801986Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
12814451Seschrock  * inadvertently do repair I/Os to the wrong device.
12821986Seschrock  *
12831986Seschrock  * This function will only return failure if one of the vdevs indicates that it
12841986Seschrock  * has since been destroyed or exported.  This is only possible if
12851986Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
12861986Seschrock  * will be updated but the function will return 0.
12871986Seschrock  */
12881986Seschrock int
12891986Seschrock vdev_validate(vdev_t *vd)
12901986Seschrock {
12911986Seschrock 	spa_t *spa = vd->vdev_spa;
12921986Seschrock 	nvlist_t *label;
129311422SMark.Musante@Sun.COM 	uint64_t guid = 0, top_guid;
12941986Seschrock 	uint64_t state;
12951986Seschrock 
12969816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
12971986Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
12984070Smc142369 			return (EBADF);
12991986Seschrock 
13002174Seschrock 	/*
13012174Seschrock 	 * If the device has already failed, or was marked offline, don't do
13022174Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
13032174Seschrock 	 * overwrite the previous state.
13042174Seschrock 	 */
13057754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
130611422SMark.Musante@Sun.COM 		uint64_t aux_guid = 0;
130711422SMark.Musante@Sun.COM 		nvlist_t *nvl;
13081986Seschrock 
13091986Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
13101986Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
13111986Seschrock 			    VDEV_AUX_BAD_LABEL);
13121986Seschrock 			return (0);
13131986Seschrock 		}
13141986Seschrock 
131511422SMark.Musante@Sun.COM 		/*
131611422SMark.Musante@Sun.COM 		 * Determine if this vdev has been split off into another
131711422SMark.Musante@Sun.COM 		 * pool.  If so, then refuse to open it.
131811422SMark.Musante@Sun.COM 		 */
131911422SMark.Musante@Sun.COM 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
132011422SMark.Musante@Sun.COM 		    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
132111422SMark.Musante@Sun.COM 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
132211422SMark.Musante@Sun.COM 			    VDEV_AUX_SPLIT_POOL);
132311422SMark.Musante@Sun.COM 			nvlist_free(label);
132411422SMark.Musante@Sun.COM 			return (0);
132511422SMark.Musante@Sun.COM 		}
132611422SMark.Musante@Sun.COM 
13271986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
13281986Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
13291986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13301986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13311986Seschrock 			nvlist_free(label);
13321986Seschrock 			return (0);
13331986Seschrock 		}
13341986Seschrock 
133511422SMark.Musante@Sun.COM 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
133611422SMark.Musante@Sun.COM 		    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
133711422SMark.Musante@Sun.COM 		    &aux_guid) != 0)
133811422SMark.Musante@Sun.COM 			aux_guid = 0;
133911422SMark.Musante@Sun.COM 
13407754SJeff.Bonwick@Sun.COM 		/*
13417754SJeff.Bonwick@Sun.COM 		 * If this vdev just became a top-level vdev because its
13427754SJeff.Bonwick@Sun.COM 		 * sibling was detached, it will have adopted the parent's
13437754SJeff.Bonwick@Sun.COM 		 * vdev guid -- but the label may or may not be on disk yet.
13447754SJeff.Bonwick@Sun.COM 		 * Fortunately, either version of the label will have the
13457754SJeff.Bonwick@Sun.COM 		 * same top guid, so if we're a top-level vdev, we can
13467754SJeff.Bonwick@Sun.COM 		 * safely compare to that instead.
134711422SMark.Musante@Sun.COM 		 *
134811422SMark.Musante@Sun.COM 		 * If we split this vdev off instead, then we also check the
134911422SMark.Musante@Sun.COM 		 * original pool's guid.  We don't want to consider the vdev
135011422SMark.Musante@Sun.COM 		 * corrupt if it is partway through a split operation.
13517754SJeff.Bonwick@Sun.COM 		 */
13521986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
13537754SJeff.Bonwick@Sun.COM 		    &guid) != 0 ||
13547754SJeff.Bonwick@Sun.COM 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
13557754SJeff.Bonwick@Sun.COM 		    &top_guid) != 0 ||
135611422SMark.Musante@Sun.COM 		    ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
13577754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
13581986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13591986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13601986Seschrock 			nvlist_free(label);
13611986Seschrock 			return (0);
13621986Seschrock 		}
13631986Seschrock 
13641986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
13651986Seschrock 		    &state) != 0) {
13661986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13671986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13681986Seschrock 			nvlist_free(label);
13691986Seschrock 			return (0);
13701986Seschrock 		}
13711986Seschrock 
13721986Seschrock 		nvlist_free(label);
13731986Seschrock 
137410100SLin.Ling@Sun.COM 		/*
137512949SGeorge.Wilson@Sun.COM 		 * If this is a verbatim import, no need to check the
137610100SLin.Ling@Sun.COM 		 * state of the pool.
137710100SLin.Ling@Sun.COM 		 */
137812949SGeorge.Wilson@Sun.COM 		if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
137911147SGeorge.Wilson@Sun.COM 		    spa_load_state(spa) == SPA_LOAD_OPEN &&
138010100SLin.Ling@Sun.COM 		    state != POOL_STATE_ACTIVE)
13814070Smc142369 			return (EBADF);
13826976Seschrock 
13836976Seschrock 		/*
13846976Seschrock 		 * If we were able to open and validate a vdev that was
13856976Seschrock 		 * previously marked permanently unavailable, clear that state
13866976Seschrock 		 * now.
13876976Seschrock 		 */
13886976Seschrock 		if (vd->vdev_not_present)
13896976Seschrock 			vd->vdev_not_present = 0;
13901986Seschrock 	}
13911986Seschrock 
13921986Seschrock 	return (0);
13931986Seschrock }
13941986Seschrock 
13951986Seschrock /*
1396789Sahrens  * Close a virtual device.
1397789Sahrens  */
1398789Sahrens void
1399789Sahrens vdev_close(vdev_t *vd)
1400789Sahrens {
14018241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
140210850SGeorge.Wilson@Sun.COM 	vdev_t *pvd = vd->vdev_parent;
14038241SJeff.Bonwick@Sun.COM 
14048241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
14058241SJeff.Bonwick@Sun.COM 
140611422SMark.Musante@Sun.COM 	/*
140711422SMark.Musante@Sun.COM 	 * If our parent is reopening, then we are as well, unless we are
140811422SMark.Musante@Sun.COM 	 * going offline.
140911422SMark.Musante@Sun.COM 	 */
141010850SGeorge.Wilson@Sun.COM 	if (pvd != NULL && pvd->vdev_reopening)
141111422SMark.Musante@Sun.COM 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
141210850SGeorge.Wilson@Sun.COM 
1413789Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1414789Sahrens 
14154451Seschrock 	vdev_cache_purge(vd);
1416789Sahrens 
14171986Seschrock 	/*
14189816SGeorge.Wilson@Sun.COM 	 * We record the previous state before we close it, so that if we are
14191986Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
14201986Seschrock 	 * it's still faulted.
14211986Seschrock 	 */
14221986Seschrock 	vd->vdev_prevstate = vd->vdev_state;
14231986Seschrock 
1424789Sahrens 	if (vd->vdev_offline)
1425789Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1426789Sahrens 	else
1427789Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
14281544Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1429789Sahrens }
1430789Sahrens 
143111958SGeorge.Wilson@Sun.COM void
143211958SGeorge.Wilson@Sun.COM vdev_hold(vdev_t *vd)
143311958SGeorge.Wilson@Sun.COM {
143411958SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
143511958SGeorge.Wilson@Sun.COM 
143611958SGeorge.Wilson@Sun.COM 	ASSERT(spa_is_root(spa));
143711958SGeorge.Wilson@Sun.COM 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
143811958SGeorge.Wilson@Sun.COM 		return;
143911958SGeorge.Wilson@Sun.COM 
144011958SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
144111958SGeorge.Wilson@Sun.COM 		vdev_hold(vd->vdev_child[c]);
144211958SGeorge.Wilson@Sun.COM 
144311958SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
144411958SGeorge.Wilson@Sun.COM 		vd->vdev_ops->vdev_op_hold(vd);
144511958SGeorge.Wilson@Sun.COM }
144611958SGeorge.Wilson@Sun.COM 
144711958SGeorge.Wilson@Sun.COM void
144811958SGeorge.Wilson@Sun.COM vdev_rele(vdev_t *vd)
144911958SGeorge.Wilson@Sun.COM {
145011958SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
145111958SGeorge.Wilson@Sun.COM 
145211958SGeorge.Wilson@Sun.COM 	ASSERT(spa_is_root(spa));
145311958SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
145411958SGeorge.Wilson@Sun.COM 		vdev_rele(vd->vdev_child[c]);
145511958SGeorge.Wilson@Sun.COM 
145611958SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
145711958SGeorge.Wilson@Sun.COM 		vd->vdev_ops->vdev_op_rele(vd);
145811958SGeorge.Wilson@Sun.COM }
145911958SGeorge.Wilson@Sun.COM 
146010850SGeorge.Wilson@Sun.COM /*
146110850SGeorge.Wilson@Sun.COM  * Reopen all interior vdevs and any unopened leaves.  We don't actually
146210850SGeorge.Wilson@Sun.COM  * reopen leaf vdevs which had previously been opened as they might deadlock
146310850SGeorge.Wilson@Sun.COM  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
146410850SGeorge.Wilson@Sun.COM  * If the leaf has never been opened then open it, as usual.
146510850SGeorge.Wilson@Sun.COM  */
1466789Sahrens void
14671544Seschrock vdev_reopen(vdev_t *vd)
1468789Sahrens {
14691544Seschrock 	spa_t *spa = vd->vdev_spa;
1470789Sahrens 
14717754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
14721544Seschrock 
147311422SMark.Musante@Sun.COM 	/* set the reopening flag unless we're taking the vdev offline */
147411422SMark.Musante@Sun.COM 	vd->vdev_reopening = !vd->vdev_offline;
1475789Sahrens 	vdev_close(vd);
1476789Sahrens 	(void) vdev_open(vd);
1477789Sahrens 
1478789Sahrens 	/*
14793377Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
14803377Seschrock 	 * Otherwise, a device with an invalid label could be successfully
14813377Seschrock 	 * opened in response to vdev_reopen().
14823377Seschrock 	 */
14836643Seschrock 	if (vd->vdev_aux) {
14846643Seschrock 		(void) vdev_validate_aux(vd);
14857754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) && vdev_writeable(vd) &&
14869425SEric.Schrock@Sun.COM 		    vd->vdev_aux == &spa->spa_l2cache &&
14879816SGeorge.Wilson@Sun.COM 		    !l2arc_vdev_present(vd))
14889816SGeorge.Wilson@Sun.COM 			l2arc_add_vdev(spa, vd);
14896643Seschrock 	} else {
14906643Seschrock 		(void) vdev_validate(vd);
14916643Seschrock 	}
14923377Seschrock 
14933377Seschrock 	/*
14944451Seschrock 	 * Reassess parent vdev's health.
1495789Sahrens 	 */
14964451Seschrock 	vdev_propagate_state(vd);
1497789Sahrens }
1498789Sahrens 
1499789Sahrens int
15002082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1501789Sahrens {
1502789Sahrens 	int error;
1503789Sahrens 
1504789Sahrens 	/*
1505789Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1506789Sahrens 	 * For a create, however, we want to fail the request if
1507789Sahrens 	 * there are any components we can't open.
1508789Sahrens 	 */
1509789Sahrens 	error = vdev_open(vd);
1510789Sahrens 
1511789Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1512789Sahrens 		vdev_close(vd);
1513789Sahrens 		return (error ? error : ENXIO);
1514789Sahrens 	}
1515789Sahrens 
1516789Sahrens 	/*
1517789Sahrens 	 * Recursively initialize all labels.
1518789Sahrens 	 */
15193377Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
15203377Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1521789Sahrens 		vdev_close(vd);
1522789Sahrens 		return (error);
1523789Sahrens 	}
1524789Sahrens 
1525789Sahrens 	return (0);
1526789Sahrens }
1527789Sahrens 
15281585Sbonwick void
15299816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(vdev_t *vd)
1530789Sahrens {
1531789Sahrens 	/*
1532789Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1533789Sahrens 	 */
1534789Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1535789Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1536789Sahrens }
1537789Sahrens 
1538789Sahrens void
15391732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1540789Sahrens {
15411732Sbonwick 	ASSERT(vd == vd->vdev_top);
154210594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
15431732Sbonwick 	ASSERT(ISP2(flags));
1544789Sahrens 
15451732Sbonwick 	if (flags & VDD_METASLAB)
15461732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
15471732Sbonwick 
15481732Sbonwick 	if (flags & VDD_DTL)
15491732Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
15501732Sbonwick 
15511732Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1552789Sahrens }
1553789Sahrens 
15548241SJeff.Bonwick@Sun.COM /*
15558241SJeff.Bonwick@Sun.COM  * DTLs.
15568241SJeff.Bonwick@Sun.COM  *
15578241SJeff.Bonwick@Sun.COM  * A vdev's DTL (dirty time log) is the set of transaction groups for which
155811815SEric.Taylor@Sun.COM  * the vdev has less than perfect replication.  There are four kinds of DTL:
15598241SJeff.Bonwick@Sun.COM  *
15608241SJeff.Bonwick@Sun.COM  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
15618241SJeff.Bonwick@Sun.COM  *
15628241SJeff.Bonwick@Sun.COM  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
15638241SJeff.Bonwick@Sun.COM  *
15648241SJeff.Bonwick@Sun.COM  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
15658241SJeff.Bonwick@Sun.COM  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
15668241SJeff.Bonwick@Sun.COM  *	txgs that was scrubbed.
15678241SJeff.Bonwick@Sun.COM  *
15688241SJeff.Bonwick@Sun.COM  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
15698241SJeff.Bonwick@Sun.COM  *	persistent errors or just some device being offline.
15708241SJeff.Bonwick@Sun.COM  *	Unlike the other three, the DTL_OUTAGE map is not generally
15718241SJeff.Bonwick@Sun.COM  *	maintained; it's only computed when needed, typically to
15728241SJeff.Bonwick@Sun.COM  *	determine whether a device can be detached.
15738241SJeff.Bonwick@Sun.COM  *
15748241SJeff.Bonwick@Sun.COM  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
15758241SJeff.Bonwick@Sun.COM  * either has the data or it doesn't.
15768241SJeff.Bonwick@Sun.COM  *
15778241SJeff.Bonwick@Sun.COM  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
15788241SJeff.Bonwick@Sun.COM  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
15798241SJeff.Bonwick@Sun.COM  * if any child is less than fully replicated, then so is its parent.
15808241SJeff.Bonwick@Sun.COM  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
15818241SJeff.Bonwick@Sun.COM  * comprising only those txgs which appear in 'maxfaults' or more children;
15828241SJeff.Bonwick@Sun.COM  * those are the txgs we don't have enough replication to read.  For example,
15838241SJeff.Bonwick@Sun.COM  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
15848241SJeff.Bonwick@Sun.COM  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
15858241SJeff.Bonwick@Sun.COM  * two child DTL_MISSING maps.
15868241SJeff.Bonwick@Sun.COM  *
15878241SJeff.Bonwick@Sun.COM  * It should be clear from the above that to compute the DTLs and outage maps
15888241SJeff.Bonwick@Sun.COM  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
15898241SJeff.Bonwick@Sun.COM  * Therefore, that is all we keep on disk.  When loading the pool, or after
15908241SJeff.Bonwick@Sun.COM  * a configuration change, we generate all other DTLs from first principles.
15918241SJeff.Bonwick@Sun.COM  */
1592789Sahrens void
15938241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1594789Sahrens {
15958241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
15968241SJeff.Bonwick@Sun.COM 
15978241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
15988241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
15998241SJeff.Bonwick@Sun.COM 
1600789Sahrens 	mutex_enter(sm->sm_lock);
1601789Sahrens 	if (!space_map_contains(sm, txg, size))
1602789Sahrens 		space_map_add(sm, txg, size);
1603789Sahrens 	mutex_exit(sm->sm_lock);
1604789Sahrens }
1605789Sahrens 
16068241SJeff.Bonwick@Sun.COM boolean_t
16078241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1608789Sahrens {
16098241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
16108241SJeff.Bonwick@Sun.COM 	boolean_t dirty = B_FALSE;
16118241SJeff.Bonwick@Sun.COM 
16128241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
16138241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1614789Sahrens 
1615789Sahrens 	mutex_enter(sm->sm_lock);
16168241SJeff.Bonwick@Sun.COM 	if (sm->sm_space != 0)
16178241SJeff.Bonwick@Sun.COM 		dirty = space_map_contains(sm, txg, size);
1618789Sahrens 	mutex_exit(sm->sm_lock);
1619789Sahrens 
1620789Sahrens 	return (dirty);
1621789Sahrens }
1622789Sahrens 
16238241SJeff.Bonwick@Sun.COM boolean_t
16248241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
16258241SJeff.Bonwick@Sun.COM {
16268241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
16278241SJeff.Bonwick@Sun.COM 	boolean_t empty;
16288241SJeff.Bonwick@Sun.COM 
16298241SJeff.Bonwick@Sun.COM 	mutex_enter(sm->sm_lock);
16308241SJeff.Bonwick@Sun.COM 	empty = (sm->sm_space == 0);
16318241SJeff.Bonwick@Sun.COM 	mutex_exit(sm->sm_lock);
16328241SJeff.Bonwick@Sun.COM 
16338241SJeff.Bonwick@Sun.COM 	return (empty);
16348241SJeff.Bonwick@Sun.COM }
16358241SJeff.Bonwick@Sun.COM 
1636789Sahrens /*
1637789Sahrens  * Reassess DTLs after a config change or scrub completion.
1638789Sahrens  */
1639789Sahrens void
1640789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1641789Sahrens {
16421544Seschrock 	spa_t *spa = vd->vdev_spa;
16438241SJeff.Bonwick@Sun.COM 	avl_tree_t reftree;
16448241SJeff.Bonwick@Sun.COM 	int minref;
16458241SJeff.Bonwick@Sun.COM 
16468241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
16478241SJeff.Bonwick@Sun.COM 
16488241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
16498241SJeff.Bonwick@Sun.COM 		vdev_dtl_reassess(vd->vdev_child[c], txg,
16508241SJeff.Bonwick@Sun.COM 		    scrub_txg, scrub_done);
16518241SJeff.Bonwick@Sun.COM 
165210922SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
16538241SJeff.Bonwick@Sun.COM 		return;
16548241SJeff.Bonwick@Sun.COM 
16558241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
165612296SLin.Ling@Sun.COM 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
165712296SLin.Ling@Sun.COM 
1658789Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
16597046Sahrens 		if (scrub_txg != 0 &&
166012296SLin.Ling@Sun.COM 		    (spa->spa_scrub_started ||
166112296SLin.Ling@Sun.COM 		    (scn && scn->scn_phys.scn_errors == 0))) {
16627046Sahrens 			/*
16637046Sahrens 			 * We completed a scrub up to scrub_txg.  If we
16647046Sahrens 			 * did it without rebooting, then the scrub dtl
16657046Sahrens 			 * will be valid, so excise the old region and
16667046Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
16677046Sahrens 			 * dtl as-is if there was an error.
16688241SJeff.Bonwick@Sun.COM 			 *
16698241SJeff.Bonwick@Sun.COM 			 * There's little trick here: to excise the beginning
16708241SJeff.Bonwick@Sun.COM 			 * of the DTL_MISSING map, we put it into a reference
16718241SJeff.Bonwick@Sun.COM 			 * tree and then add a segment with refcnt -1 that
16728241SJeff.Bonwick@Sun.COM 			 * covers the range [0, scrub_txg).  This means
16738241SJeff.Bonwick@Sun.COM 			 * that each txg in that range has refcnt -1 or 0.
16748241SJeff.Bonwick@Sun.COM 			 * We then add DTL_SCRUB with a refcnt of 2, so that
16758241SJeff.Bonwick@Sun.COM 			 * entries in the range [0, scrub_txg) will have a
16768241SJeff.Bonwick@Sun.COM 			 * positive refcnt -- either 1 or 2.  We then convert
16778241SJeff.Bonwick@Sun.COM 			 * the reference tree into the new DTL_MISSING map.
16787046Sahrens 			 */
16798241SJeff.Bonwick@Sun.COM 			space_map_ref_create(&reftree);
16808241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
16818241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
16828241SJeff.Bonwick@Sun.COM 			space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
16838241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
16848241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_SCRUB], 2);
16858241SJeff.Bonwick@Sun.COM 			space_map_ref_generate_map(&reftree,
16868241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
16878241SJeff.Bonwick@Sun.COM 			space_map_ref_destroy(&reftree);
1688789Sahrens 		}
16898241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
16908241SJeff.Bonwick@Sun.COM 		space_map_walk(&vd->vdev_dtl[DTL_MISSING],
16918241SJeff.Bonwick@Sun.COM 		    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1692789Sahrens 		if (scrub_done)
16938241SJeff.Bonwick@Sun.COM 			space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
16948241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
16958241SJeff.Bonwick@Sun.COM 		if (!vdev_readable(vd))
16968241SJeff.Bonwick@Sun.COM 			space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
16978241SJeff.Bonwick@Sun.COM 		else
16988241SJeff.Bonwick@Sun.COM 			space_map_walk(&vd->vdev_dtl[DTL_MISSING],
16998241SJeff.Bonwick@Sun.COM 			    space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1700789Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
17017046Sahrens 
17021732Sbonwick 		if (txg != 0)
17031732Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1704789Sahrens 		return;
1705789Sahrens 	}
1706789Sahrens 
1707789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17088241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
170910890SEric.Taylor@Sun.COM 		/* account for child's outage in parent's missing map */
171010890SEric.Taylor@Sun.COM 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
17118241SJeff.Bonwick@Sun.COM 		if (t == DTL_SCRUB)
17128241SJeff.Bonwick@Sun.COM 			continue;			/* leaf vdevs only */
17138241SJeff.Bonwick@Sun.COM 		if (t == DTL_PARTIAL)
17148241SJeff.Bonwick@Sun.COM 			minref = 1;			/* i.e. non-zero */
17158241SJeff.Bonwick@Sun.COM 		else if (vd->vdev_nparity != 0)
17168241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
17178241SJeff.Bonwick@Sun.COM 		else
17188241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_children;	/* any kind of mirror */
17198241SJeff.Bonwick@Sun.COM 		space_map_ref_create(&reftree);
17208241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
17218241SJeff.Bonwick@Sun.COM 			vdev_t *cvd = vd->vdev_child[c];
17228241SJeff.Bonwick@Sun.COM 			mutex_enter(&cvd->vdev_dtl_lock);
172310890SEric.Taylor@Sun.COM 			space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1);
17248241SJeff.Bonwick@Sun.COM 			mutex_exit(&cvd->vdev_dtl_lock);
17258241SJeff.Bonwick@Sun.COM 		}
17268241SJeff.Bonwick@Sun.COM 		space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
17278241SJeff.Bonwick@Sun.COM 		space_map_ref_destroy(&reftree);
17288241SJeff.Bonwick@Sun.COM 	}
1729789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1730789Sahrens }
1731789Sahrens 
1732789Sahrens static int
1733789Sahrens vdev_dtl_load(vdev_t *vd)
1734789Sahrens {
1735789Sahrens 	spa_t *spa = vd->vdev_spa;
17368241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
17371732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1738789Sahrens 	dmu_buf_t *db;
1739789Sahrens 	int error;
1740789Sahrens 
1741789Sahrens 	ASSERT(vd->vdev_children == 0);
1742789Sahrens 
1743789Sahrens 	if (smo->smo_object == 0)
1744789Sahrens 		return (0);
1745789Sahrens 
174610594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
174710594SGeorge.Wilson@Sun.COM 
17481732Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
17491544Seschrock 		return (error);
17501732Sbonwick 
17514944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
17524944Smaybee 	bcopy(db->db_data, smo, sizeof (*smo));
17531544Seschrock 	dmu_buf_rele(db, FTAG);
1754789Sahrens 
1755789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17568241SJeff.Bonwick@Sun.COM 	error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
17578241SJeff.Bonwick@Sun.COM 	    NULL, SM_ALLOC, smo, mos);
1758789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1759789Sahrens 
1760789Sahrens 	return (error);
1761789Sahrens }
1762789Sahrens 
1763789Sahrens void
1764789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1765789Sahrens {
1766789Sahrens 	spa_t *spa = vd->vdev_spa;
17678241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
17688241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
17691732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1770789Sahrens 	space_map_t smsync;
1771789Sahrens 	kmutex_t smlock;
1772789Sahrens 	dmu_buf_t *db;
1773789Sahrens 	dmu_tx_t *tx;
1774789Sahrens 
177510594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
177610594SGeorge.Wilson@Sun.COM 
1777789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1778789Sahrens 
1779789Sahrens 	if (vd->vdev_detached) {
1780789Sahrens 		if (smo->smo_object != 0) {
17811732Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1782789Sahrens 			ASSERT3U(err, ==, 0);
1783789Sahrens 			smo->smo_object = 0;
1784789Sahrens 		}
1785789Sahrens 		dmu_tx_commit(tx);
1786789Sahrens 		return;
1787789Sahrens 	}
1788789Sahrens 
1789789Sahrens 	if (smo->smo_object == 0) {
1790789Sahrens 		ASSERT(smo->smo_objsize == 0);
1791789Sahrens 		ASSERT(smo->smo_alloc == 0);
17921732Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1793789Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1794789Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1795789Sahrens 		ASSERT(smo->smo_object != 0);
1796789Sahrens 		vdev_config_dirty(vd->vdev_top);
1797789Sahrens 	}
1798789Sahrens 
1799789Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1800789Sahrens 
1801789Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1802789Sahrens 	    &smlock);
1803789Sahrens 
1804789Sahrens 	mutex_enter(&smlock);
1805789Sahrens 
1806789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
18071732Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1808789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1809789Sahrens 
18101732Sbonwick 	space_map_truncate(smo, mos, tx);
18111732Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1812789Sahrens 
1813789Sahrens 	space_map_destroy(&smsync);
1814789Sahrens 
1815789Sahrens 	mutex_exit(&smlock);
1816789Sahrens 	mutex_destroy(&smlock);
1817789Sahrens 
18181732Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1819789Sahrens 	dmu_buf_will_dirty(db, tx);
18204944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
18214944Smaybee 	bcopy(smo, db->db_data, sizeof (*smo));
18221544Seschrock 	dmu_buf_rele(db, FTAG);
1823789Sahrens 
1824789Sahrens 	dmu_tx_commit(tx);
1825789Sahrens }
1826789Sahrens 
18277046Sahrens /*
18288241SJeff.Bonwick@Sun.COM  * Determine whether the specified vdev can be offlined/detached/removed
18298241SJeff.Bonwick@Sun.COM  * without losing data.
18308241SJeff.Bonwick@Sun.COM  */
18318241SJeff.Bonwick@Sun.COM boolean_t
18328241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd)
18338241SJeff.Bonwick@Sun.COM {
18348241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
18358241SJeff.Bonwick@Sun.COM 	vdev_t *tvd = vd->vdev_top;
18368241SJeff.Bonwick@Sun.COM 	uint8_t cant_read = vd->vdev_cant_read;
18378241SJeff.Bonwick@Sun.COM 	boolean_t required;
18388241SJeff.Bonwick@Sun.COM 
18398241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
18408241SJeff.Bonwick@Sun.COM 
18418241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev || vd == tvd)
18428241SJeff.Bonwick@Sun.COM 		return (B_TRUE);
18438241SJeff.Bonwick@Sun.COM 
18448241SJeff.Bonwick@Sun.COM 	/*
18458241SJeff.Bonwick@Sun.COM 	 * Temporarily mark the device as unreadable, and then determine
18468241SJeff.Bonwick@Sun.COM 	 * whether this results in any DTL outages in the top-level vdev.
18478241SJeff.Bonwick@Sun.COM 	 * If not, we can safely offline/detach/remove the device.
18488241SJeff.Bonwick@Sun.COM 	 */
18498241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = B_TRUE;
18508241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
18518241SJeff.Bonwick@Sun.COM 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
18528241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = cant_read;
18538241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
18548241SJeff.Bonwick@Sun.COM 
1855*13037SMark.Musante@Sun.COM 	if (!required && zio_injection_enabled)
1856*13037SMark.Musante@Sun.COM 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
1857*13037SMark.Musante@Sun.COM 
18588241SJeff.Bonwick@Sun.COM 	return (required);
18598241SJeff.Bonwick@Sun.COM }
18608241SJeff.Bonwick@Sun.COM 
18618241SJeff.Bonwick@Sun.COM /*
18627046Sahrens  * Determine if resilver is needed, and if so the txg range.
18637046Sahrens  */
18647046Sahrens boolean_t
18657046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
18667046Sahrens {
18677046Sahrens 	boolean_t needed = B_FALSE;
18687046Sahrens 	uint64_t thismin = UINT64_MAX;
18697046Sahrens 	uint64_t thismax = 0;
18707046Sahrens 
18717046Sahrens 	if (vd->vdev_children == 0) {
18727046Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
18738241SJeff.Bonwick@Sun.COM 		if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
18748241SJeff.Bonwick@Sun.COM 		    vdev_writeable(vd)) {
18757046Sahrens 			space_seg_t *ss;
18767046Sahrens 
18778241SJeff.Bonwick@Sun.COM 			ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
18787046Sahrens 			thismin = ss->ss_start - 1;
18798241SJeff.Bonwick@Sun.COM 			ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
18807046Sahrens 			thismax = ss->ss_end;
18817046Sahrens 			needed = B_TRUE;
18827046Sahrens 		}
18837046Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
18847046Sahrens 	} else {
18858241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
18867046Sahrens 			vdev_t *cvd = vd->vdev_child[c];
18877046Sahrens 			uint64_t cmin, cmax;
18887046Sahrens 
18897046Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
18907046Sahrens 				thismin = MIN(thismin, cmin);
18917046Sahrens 				thismax = MAX(thismax, cmax);
18927046Sahrens 				needed = B_TRUE;
18937046Sahrens 			}
18947046Sahrens 		}
18957046Sahrens 	}
18967046Sahrens 
18977046Sahrens 	if (needed && minp) {
18987046Sahrens 		*minp = thismin;
18997046Sahrens 		*maxp = thismax;
19007046Sahrens 	}
19017046Sahrens 	return (needed);
19027046Sahrens }
19037046Sahrens 
19041986Seschrock void
19051544Seschrock vdev_load(vdev_t *vd)
1906789Sahrens {
1907789Sahrens 	/*
1908789Sahrens 	 * Recursively load all children.
1909789Sahrens 	 */
19108241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
19111986Seschrock 		vdev_load(vd->vdev_child[c]);
1912789Sahrens 
1913789Sahrens 	/*
19141585Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1915789Sahrens 	 */
191610594SGeorge.Wilson@Sun.COM 	if (vd == vd->vdev_top && !vd->vdev_ishole &&
19171986Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
19181986Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
19191986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
19201986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1921789Sahrens 
1922789Sahrens 	/*
1923789Sahrens 	 * If this is a leaf vdev, load its DTL.
1924789Sahrens 	 */
19251986Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
19261986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
19271986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1928789Sahrens }
1929789Sahrens 
19302082Seschrock /*
19315450Sbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
19325450Sbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
19335450Sbrendan  * we make sure that we can open the underlying device, then try to read the
19345450Sbrendan  * label, and make sure that the label is sane and that it hasn't been
19355450Sbrendan  * repurposed to another pool.
19362082Seschrock  */
19372082Seschrock int
19385450Sbrendan vdev_validate_aux(vdev_t *vd)
19392082Seschrock {
19402082Seschrock 	nvlist_t *label;
19412082Seschrock 	uint64_t guid, version;
19422082Seschrock 	uint64_t state;
19432082Seschrock 
19447754SJeff.Bonwick@Sun.COM 	if (!vdev_readable(vd))
19456643Seschrock 		return (0);
19466643Seschrock 
19472082Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
19482082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
19492082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
19502082Seschrock 		return (-1);
19512082Seschrock 	}
19522082Seschrock 
19532082Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
19544577Sahrens 	    version > SPA_VERSION ||
19552082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
19562082Seschrock 	    guid != vd->vdev_guid ||
19572082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
19582082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
19592082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
19602082Seschrock 		nvlist_free(label);
19612082Seschrock 		return (-1);
19622082Seschrock 	}
19632082Seschrock 
19642082Seschrock 	/*
19652082Seschrock 	 * We don't actually check the pool state here.  If it's in fact in
19662082Seschrock 	 * use by another pool, we update this fact on the fly when requested.
19672082Seschrock 	 */
19682082Seschrock 	nvlist_free(label);
19692082Seschrock 	return (0);
19702082Seschrock }
19712082Seschrock 
1972789Sahrens void
197310594SGeorge.Wilson@Sun.COM vdev_remove(vdev_t *vd, uint64_t txg)
197410594SGeorge.Wilson@Sun.COM {
197510594SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
197610594SGeorge.Wilson@Sun.COM 	objset_t *mos = spa->spa_meta_objset;
197710594SGeorge.Wilson@Sun.COM 	dmu_tx_t *tx;
197810594SGeorge.Wilson@Sun.COM 
197910594SGeorge.Wilson@Sun.COM 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
198010594SGeorge.Wilson@Sun.COM 
198110594SGeorge.Wilson@Sun.COM 	if (vd->vdev_dtl_smo.smo_object) {
198210594SGeorge.Wilson@Sun.COM 		ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0);
198310594SGeorge.Wilson@Sun.COM 		(void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
198410594SGeorge.Wilson@Sun.COM 		vd->vdev_dtl_smo.smo_object = 0;
198510594SGeorge.Wilson@Sun.COM 	}
198610594SGeorge.Wilson@Sun.COM 
198710594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms != NULL) {
198810594SGeorge.Wilson@Sun.COM 		for (int m = 0; m < vd->vdev_ms_count; m++) {
198910594SGeorge.Wilson@Sun.COM 			metaslab_t *msp = vd->vdev_ms[m];
199010594SGeorge.Wilson@Sun.COM 
199110594SGeorge.Wilson@Sun.COM 			if (msp == NULL || msp->ms_smo.smo_object == 0)
199210594SGeorge.Wilson@Sun.COM 				continue;
199310594SGeorge.Wilson@Sun.COM 
199410594SGeorge.Wilson@Sun.COM 			ASSERT3U(msp->ms_smo.smo_alloc, ==, 0);
199510594SGeorge.Wilson@Sun.COM 			(void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
199610594SGeorge.Wilson@Sun.COM 			msp->ms_smo.smo_object = 0;
199710594SGeorge.Wilson@Sun.COM 		}
199810594SGeorge.Wilson@Sun.COM 	}
199910594SGeorge.Wilson@Sun.COM 
200010594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms_array) {
200110594SGeorge.Wilson@Sun.COM 		(void) dmu_object_free(mos, vd->vdev_ms_array, tx);
200210594SGeorge.Wilson@Sun.COM 		vd->vdev_ms_array = 0;
200310594SGeorge.Wilson@Sun.COM 		vd->vdev_ms_shift = 0;
200410594SGeorge.Wilson@Sun.COM 	}
200510594SGeorge.Wilson@Sun.COM 	dmu_tx_commit(tx);
200610594SGeorge.Wilson@Sun.COM }
200710594SGeorge.Wilson@Sun.COM 
200810594SGeorge.Wilson@Sun.COM void
2009789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
2010789Sahrens {
2011789Sahrens 	metaslab_t *msp;
201211146SGeorge.Wilson@Sun.COM 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2013789Sahrens 
201410594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
201510594SGeorge.Wilson@Sun.COM 
2016789Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2017789Sahrens 		metaslab_sync_done(msp, txg);
201811146SGeorge.Wilson@Sun.COM 
201911146SGeorge.Wilson@Sun.COM 	if (reassess)
202011146SGeorge.Wilson@Sun.COM 		metaslab_sync_reassess(vd->vdev_mg);
2021789Sahrens }
2022789Sahrens 
2023789Sahrens void
2024789Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2025789Sahrens {
2026789Sahrens 	spa_t *spa = vd->vdev_spa;
2027789Sahrens 	vdev_t *lvd;
2028789Sahrens 	metaslab_t *msp;
20291732Sbonwick 	dmu_tx_t *tx;
2030789Sahrens 
203110594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
203210594SGeorge.Wilson@Sun.COM 
20331732Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
20341732Sbonwick 		ASSERT(vd == vd->vdev_top);
20351732Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
20361732Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
20371732Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
20381732Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
20391732Sbonwick 		vdev_config_dirty(vd);
20401732Sbonwick 		dmu_tx_commit(tx);
20411732Sbonwick 	}
2042789Sahrens 
204312296SLin.Ling@Sun.COM 	/*
204412296SLin.Ling@Sun.COM 	 * Remove the metadata associated with this vdev once it's empty.
204512296SLin.Ling@Sun.COM 	 */
204612296SLin.Ling@Sun.COM 	if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
204710594SGeorge.Wilson@Sun.COM 		vdev_remove(vd, txg);
204810594SGeorge.Wilson@Sun.COM 
20491732Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2050789Sahrens 		metaslab_sync(msp, txg);
20511732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
20521732Sbonwick 	}
2053789Sahrens 
2054789Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2055789Sahrens 		vdev_dtl_sync(lvd, txg);
2056789Sahrens 
2057789Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2058789Sahrens }
2059789Sahrens 
2060789Sahrens uint64_t
2061789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2062789Sahrens {
2063789Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2064789Sahrens }
2065789Sahrens 
20664451Seschrock /*
20674451Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
20684451Seschrock  * not be opened, and no I/O is attempted.
20694451Seschrock  */
2070789Sahrens int
207110817SEric.Schrock@Sun.COM vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
20724451Seschrock {
207312949SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd;
20744451Seschrock 
207510685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
20764451Seschrock 
20776643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
20787754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
20797754SJeff.Bonwick@Sun.COM 
20804451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
20817754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
20824451Seschrock 
208312949SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
208412949SGeorge.Wilson@Sun.COM 
20854451Seschrock 	/*
208610817SEric.Schrock@Sun.COM 	 * We don't directly use the aux state here, but if we do a
208710817SEric.Schrock@Sun.COM 	 * vdev_reopen(), we need this value to be present to remember why we
208810817SEric.Schrock@Sun.COM 	 * were faulted.
208910817SEric.Schrock@Sun.COM 	 */
209010817SEric.Schrock@Sun.COM 	vd->vdev_label_aux = aux;
209110817SEric.Schrock@Sun.COM 
209210817SEric.Schrock@Sun.COM 	/*
20934451Seschrock 	 * Faulted state takes precedence over degraded.
20944451Seschrock 	 */
209512247SGeorge.Wilson@Sun.COM 	vd->vdev_delayed_close = B_FALSE;
20964451Seschrock 	vd->vdev_faulted = 1ULL;
20974451Seschrock 	vd->vdev_degraded = 0ULL;
209810817SEric.Schrock@Sun.COM 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
20994451Seschrock 
21004451Seschrock 	/*
210111982SGeorge.Wilson@Sun.COM 	 * If this device has the only valid copy of the data, then
210211982SGeorge.Wilson@Sun.COM 	 * back off and simply mark the vdev as degraded instead.
21034451Seschrock 	 */
210412949SGeorge.Wilson@Sun.COM 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
21054451Seschrock 		vd->vdev_degraded = 1ULL;
21064451Seschrock 		vd->vdev_faulted = 0ULL;
21074451Seschrock 
21084451Seschrock 		/*
21094451Seschrock 		 * If we reopen the device and it's not dead, only then do we
21104451Seschrock 		 * mark it degraded.
21114451Seschrock 		 */
211212949SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
21134451Seschrock 
211410817SEric.Schrock@Sun.COM 		if (vdev_readable(vd))
211510817SEric.Schrock@Sun.COM 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
21164451Seschrock 	}
21174451Seschrock 
21187754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
21194451Seschrock }
21204451Seschrock 
21214451Seschrock /*
21224451Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
21234451Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
21244451Seschrock  * as I/O is concerned.
21254451Seschrock  */
21264451Seschrock int
212710817SEric.Schrock@Sun.COM vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
21284451Seschrock {
21296643Seschrock 	vdev_t *vd;
21304451Seschrock 
213110685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
21324451Seschrock 
21336643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
21347754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
21357754SJeff.Bonwick@Sun.COM 
21364451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
21377754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
21384451Seschrock 
21394451Seschrock 	/*
21404451Seschrock 	 * If the vdev is already faulted, then don't do anything.
21414451Seschrock 	 */
21427754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded)
21437754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, 0));
21444451Seschrock 
21454451Seschrock 	vd->vdev_degraded = 1ULL;
21464451Seschrock 	if (!vdev_is_dead(vd))
21474451Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
214810817SEric.Schrock@Sun.COM 		    aux);
21494451Seschrock 
21507754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
21514451Seschrock }
21524451Seschrock 
21534451Seschrock /*
21544451Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
21554451Seschrock  * any attached spare device should be detached when the device finishes
21564451Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
21574451Seschrock  * so no FMA events are generated if the device fails to open.
21584451Seschrock  */
21594451Seschrock int
21607754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2161789Sahrens {
21629816SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2163789Sahrens 
216410685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
21651485Slling 
21666643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
21677754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2168789Sahrens 
21691585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
21707754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
21711585Sbonwick 
21729816SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
2173789Sahrens 	vd->vdev_offline = B_FALSE;
21741485Slling 	vd->vdev_tmpoffline = B_FALSE;
21757754SJeff.Bonwick@Sun.COM 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
21767754SJeff.Bonwick@Sun.COM 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
21779816SGeorge.Wilson@Sun.COM 
21789816SGeorge.Wilson@Sun.COM 	/* XXX - L2ARC 1.0 does not support expansion */
21799816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
21809816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
21819816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
21829816SGeorge.Wilson@Sun.COM 	}
21839816SGeorge.Wilson@Sun.COM 
21849816SGeorge.Wilson@Sun.COM 	vdev_reopen(tvd);
21854451Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
21864451Seschrock 
21879816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
21889816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
21899816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = B_FALSE;
21909816SGeorge.Wilson@Sun.COM 	}
21919816SGeorge.Wilson@Sun.COM 
21924451Seschrock 	if (newstate)
21934451Seschrock 		*newstate = vd->vdev_state;
21944451Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
21954451Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
21964451Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
21974451Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
21984451Seschrock 		vd->vdev_unspare = B_TRUE;
2199789Sahrens 
22009816SGeorge.Wilson@Sun.COM 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
22019816SGeorge.Wilson@Sun.COM 
22029816SGeorge.Wilson@Sun.COM 		/* XXX - L2ARC 1.0 does not support expansion */
22039816SGeorge.Wilson@Sun.COM 		if (vd->vdev_aux)
22049816SGeorge.Wilson@Sun.COM 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
22059816SGeorge.Wilson@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
22069816SGeorge.Wilson@Sun.COM 	}
22078241SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
2208789Sahrens }
2209789Sahrens 
221010974SJeff.Bonwick@Sun.COM static int
221110974SJeff.Bonwick@Sun.COM vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2212789Sahrens {
22139701SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd;
221410685SGeorge.Wilson@Sun.COM 	int error = 0;
221510685SGeorge.Wilson@Sun.COM 	uint64_t generation;
221610685SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg;
221710685SGeorge.Wilson@Sun.COM 
221810685SGeorge.Wilson@Sun.COM top:
221910685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_ALLOC);
2220789Sahrens 
22216643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
22227754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2223789Sahrens 
22241585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
22257754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
22261585Sbonwick 
22279701SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
222810685SGeorge.Wilson@Sun.COM 	mg = tvd->vdev_mg;
222910685SGeorge.Wilson@Sun.COM 	generation = spa->spa_config_generation + 1;
22309701SGeorge.Wilson@Sun.COM 
2231789Sahrens 	/*
22321732Sbonwick 	 * If the device isn't already offline, try to offline it.
2233789Sahrens 	 */
22341732Sbonwick 	if (!vd->vdev_offline) {
22351732Sbonwick 		/*
22368241SJeff.Bonwick@Sun.COM 		 * If this device has the only valid copy of some data,
22379701SGeorge.Wilson@Sun.COM 		 * don't allow it to be offlined. Log devices are always
22389701SGeorge.Wilson@Sun.COM 		 * expendable.
22391732Sbonwick 		 */
22409701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
22419701SGeorge.Wilson@Sun.COM 		    vdev_dtl_required(vd))
22427754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2243789Sahrens 
22441732Sbonwick 		/*
224510922SJeff.Bonwick@Sun.COM 		 * If the top-level is a slog and it has had allocations
224610922SJeff.Bonwick@Sun.COM 		 * then proceed.  We check that the vdev's metaslab group
224710922SJeff.Bonwick@Sun.COM 		 * is not NULL since it's possible that we may have just
224810922SJeff.Bonwick@Sun.COM 		 * added this vdev but not yet initialized its metaslabs.
224910685SGeorge.Wilson@Sun.COM 		 */
225010685SGeorge.Wilson@Sun.COM 		if (tvd->vdev_islog && mg != NULL) {
225110685SGeorge.Wilson@Sun.COM 			/*
225210685SGeorge.Wilson@Sun.COM 			 * Prevent any future allocations.
225310685SGeorge.Wilson@Sun.COM 			 */
225410974SJeff.Bonwick@Sun.COM 			metaslab_group_passivate(mg);
225510685SGeorge.Wilson@Sun.COM 			(void) spa_vdev_state_exit(spa, vd, 0);
225610685SGeorge.Wilson@Sun.COM 
225711422SMark.Musante@Sun.COM 			error = spa_offline_log(spa);
225810685SGeorge.Wilson@Sun.COM 
225910685SGeorge.Wilson@Sun.COM 			spa_vdev_state_enter(spa, SCL_ALLOC);
226010685SGeorge.Wilson@Sun.COM 
226110685SGeorge.Wilson@Sun.COM 			/*
226210685SGeorge.Wilson@Sun.COM 			 * Check to see if the config has changed.
226310685SGeorge.Wilson@Sun.COM 			 */
226410685SGeorge.Wilson@Sun.COM 			if (error || generation != spa->spa_config_generation) {
226510974SJeff.Bonwick@Sun.COM 				metaslab_group_activate(mg);
226610685SGeorge.Wilson@Sun.COM 				if (error)
226710685SGeorge.Wilson@Sun.COM 					return (spa_vdev_state_exit(spa,
226810685SGeorge.Wilson@Sun.COM 					    vd, error));
226910685SGeorge.Wilson@Sun.COM 				(void) spa_vdev_state_exit(spa, vd, 0);
227010685SGeorge.Wilson@Sun.COM 				goto top;
227110685SGeorge.Wilson@Sun.COM 			}
227210685SGeorge.Wilson@Sun.COM 			ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0);
227310685SGeorge.Wilson@Sun.COM 		}
227410685SGeorge.Wilson@Sun.COM 
227510685SGeorge.Wilson@Sun.COM 		/*
22761732Sbonwick 		 * Offline this device and reopen its top-level vdev.
22779701SGeorge.Wilson@Sun.COM 		 * If the top-level vdev is a log device then just offline
22789701SGeorge.Wilson@Sun.COM 		 * it. Otherwise, if this action results in the top-level
22799701SGeorge.Wilson@Sun.COM 		 * vdev becoming unusable, undo it and fail the request.
22801732Sbonwick 		 */
22811732Sbonwick 		vd->vdev_offline = B_TRUE;
22829701SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
22839701SGeorge.Wilson@Sun.COM 
22849701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
22859701SGeorge.Wilson@Sun.COM 		    vdev_is_dead(tvd)) {
22861732Sbonwick 			vd->vdev_offline = B_FALSE;
22879701SGeorge.Wilson@Sun.COM 			vdev_reopen(tvd);
22887754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
22891732Sbonwick 		}
229010685SGeorge.Wilson@Sun.COM 
229110685SGeorge.Wilson@Sun.COM 		/*
229210685SGeorge.Wilson@Sun.COM 		 * Add the device back into the metaslab rotor so that
229310685SGeorge.Wilson@Sun.COM 		 * once we online the device it's open for business.
229410685SGeorge.Wilson@Sun.COM 		 */
229510685SGeorge.Wilson@Sun.COM 		if (tvd->vdev_islog && mg != NULL)
229610974SJeff.Bonwick@Sun.COM 			metaslab_group_activate(mg);
2297789Sahrens 	}
2298789Sahrens 
22997754SJeff.Bonwick@Sun.COM 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
23001732Sbonwick 
230110685SGeorge.Wilson@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
2302789Sahrens }
2303789Sahrens 
230410974SJeff.Bonwick@Sun.COM int
230510974SJeff.Bonwick@Sun.COM vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
230610974SJeff.Bonwick@Sun.COM {
230710974SJeff.Bonwick@Sun.COM 	int error;
230810974SJeff.Bonwick@Sun.COM 
230910974SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_vdev_top_lock);
231010974SJeff.Bonwick@Sun.COM 	error = vdev_offline_locked(spa, guid, flags);
231110974SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_vdev_top_lock);
231210974SJeff.Bonwick@Sun.COM 
231310974SJeff.Bonwick@Sun.COM 	return (error);
231410974SJeff.Bonwick@Sun.COM }
231510974SJeff.Bonwick@Sun.COM 
23161544Seschrock /*
23171544Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
23181544Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
23191544Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
23201544Seschrock  */
23211544Seschrock void
23227754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd)
2323789Sahrens {
23247754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
23257754SJeff.Bonwick@Sun.COM 
23267754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2327789Sahrens 
23281544Seschrock 	if (vd == NULL)
23297754SJeff.Bonwick@Sun.COM 		vd = rvd;
2330789Sahrens 
23311544Seschrock 	vd->vdev_stat.vs_read_errors = 0;
23321544Seschrock 	vd->vdev_stat.vs_write_errors = 0;
23331544Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2334789Sahrens 
23357754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
23367754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd->vdev_child[c]);
23374451Seschrock 
23384451Seschrock 	/*
23396959Sek110237 	 * If we're in the FAULTED state or have experienced failed I/O, then
23406959Sek110237 	 * clear the persistent state and attempt to reopen the device.  We
23416959Sek110237 	 * also mark the vdev config dirty, so that the new faulted state is
23426959Sek110237 	 * written out to disk.
23434451Seschrock 	 */
23447754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded ||
23457754SJeff.Bonwick@Sun.COM 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
23466959Sek110237 
234710830SEric.Schrock@Sun.COM 		/*
234810830SEric.Schrock@Sun.COM 		 * When reopening in reponse to a clear event, it may be due to
234910830SEric.Schrock@Sun.COM 		 * a fmadm repair request.  In this case, if the device is
235010830SEric.Schrock@Sun.COM 		 * still broken, we want to still post the ereport again.
235110830SEric.Schrock@Sun.COM 		 */
235210830SEric.Schrock@Sun.COM 		vd->vdev_forcefault = B_TRUE;
235310830SEric.Schrock@Sun.COM 
235412949SGeorge.Wilson@Sun.COM 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
23557754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read = B_FALSE;
23567754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write = B_FALSE;
23577754SJeff.Bonwick@Sun.COM 
23584451Seschrock 		vdev_reopen(vd);
23594451Seschrock 
236010830SEric.Schrock@Sun.COM 		vd->vdev_forcefault = B_FALSE;
236110830SEric.Schrock@Sun.COM 
23627754SJeff.Bonwick@Sun.COM 		if (vd != rvd)
23637754SJeff.Bonwick@Sun.COM 			vdev_state_dirty(vd->vdev_top);
23647754SJeff.Bonwick@Sun.COM 
23657754SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
23664808Sek110237 			spa_async_request(spa, SPA_ASYNC_RESILVER);
23674451Seschrock 
23684451Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
23694451Seschrock 	}
237010830SEric.Schrock@Sun.COM 
237110830SEric.Schrock@Sun.COM 	/*
237210830SEric.Schrock@Sun.COM 	 * When clearing a FMA-diagnosed fault, we always want to
237310830SEric.Schrock@Sun.COM 	 * unspare the device, as we assume that the original spare was
237410830SEric.Schrock@Sun.COM 	 * done in response to the FMA fault.
237510830SEric.Schrock@Sun.COM 	 */
237610830SEric.Schrock@Sun.COM 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
237710830SEric.Schrock@Sun.COM 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
237810830SEric.Schrock@Sun.COM 	    vd->vdev_parent->vdev_child[0] == vd)
237910830SEric.Schrock@Sun.COM 		vd->vdev_unspare = B_TRUE;
2380789Sahrens }
2381789Sahrens 
23827754SJeff.Bonwick@Sun.COM boolean_t
23837754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd)
23845329Sgw25295 {
238510594SGeorge.Wilson@Sun.COM 	/*
238610594SGeorge.Wilson@Sun.COM 	 * Holes and missing devices are always considered "dead".
238710594SGeorge.Wilson@Sun.COM 	 * This simplifies the code since we don't have to check for
238810594SGeorge.Wilson@Sun.COM 	 * these types of devices in the various code paths.
238910594SGeorge.Wilson@Sun.COM 	 * Instead we rely on the fact that we skip over dead devices
239010594SGeorge.Wilson@Sun.COM 	 * before issuing I/O to them.
239110594SGeorge.Wilson@Sun.COM 	 */
239210594SGeorge.Wilson@Sun.COM 	return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
239310594SGeorge.Wilson@Sun.COM 	    vd->vdev_ops == &vdev_missing_ops);
23945329Sgw25295 }
23955329Sgw25295 
23967754SJeff.Bonwick@Sun.COM boolean_t
23977754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd)
2398789Sahrens {
23997754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2400789Sahrens }
2401789Sahrens 
24027754SJeff.Bonwick@Sun.COM boolean_t
24037754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd)
2404789Sahrens {
24057754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
24067754SJeff.Bonwick@Sun.COM }
2407789Sahrens 
24087754SJeff.Bonwick@Sun.COM boolean_t
24097980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd)
24107980SGeorge.Wilson@Sun.COM {
24118241SJeff.Bonwick@Sun.COM 	uint64_t state = vd->vdev_state;
24128241SJeff.Bonwick@Sun.COM 
24137980SGeorge.Wilson@Sun.COM 	/*
24148241SJeff.Bonwick@Sun.COM 	 * We currently allow allocations from vdevs which may be in the
24157980SGeorge.Wilson@Sun.COM 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
24167980SGeorge.Wilson@Sun.COM 	 * fails to reopen then we'll catch it later when we're holding
24178241SJeff.Bonwick@Sun.COM 	 * the proper locks.  Note that we have to get the vdev state
24188241SJeff.Bonwick@Sun.COM 	 * in a local variable because although it changes atomically,
24198241SJeff.Bonwick@Sun.COM 	 * we're asking two separate questions about it.
24207980SGeorge.Wilson@Sun.COM 	 */
24218241SJeff.Bonwick@Sun.COM 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
242212296SLin.Ling@Sun.COM 	    !vd->vdev_cant_write && !vd->vdev_ishole);
24237980SGeorge.Wilson@Sun.COM }
24247980SGeorge.Wilson@Sun.COM 
24257980SGeorge.Wilson@Sun.COM boolean_t
24267754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio)
24277754SJeff.Bonwick@Sun.COM {
24287754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_vd == vd);
2429789Sahrens 
24307754SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
24317754SJeff.Bonwick@Sun.COM 		return (B_FALSE);
2432789Sahrens 
24337754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ)
24347754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_read);
2435789Sahrens 
24367754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_WRITE)
24377754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_write);
24387754SJeff.Bonwick@Sun.COM 
24397754SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2440789Sahrens }
2441789Sahrens 
2442789Sahrens /*
2443789Sahrens  * Get statistics for the given vdev.
2444789Sahrens  */
2445789Sahrens void
2446789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2447789Sahrens {
2448789Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2449789Sahrens 
2450789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2451789Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2452789Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2453789Sahrens 	vs->vs_state = vd->vdev_state;
24549816SGeorge.Wilson@Sun.COM 	vs->vs_rsize = vdev_get_min_asize(vd);
24559816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
24569816SGeorge.Wilson@Sun.COM 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2457789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2458789Sahrens 
2459789Sahrens 	/*
2460789Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2461789Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2462789Sahrens 	 */
2463789Sahrens 	if (vd == rvd) {
24647754SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
2465789Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2466789Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2467789Sahrens 
2468789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
24697754SJeff.Bonwick@Sun.COM 			for (int t = 0; t < ZIO_TYPES; t++) {
2470789Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2471789Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2472789Sahrens 			}
247312296SLin.Ling@Sun.COM 			cvs->vs_scan_removing = cvd->vdev_removing;
2474789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
2475789Sahrens 		}
2476789Sahrens 	}
2477789Sahrens }
2478789Sahrens 
2479789Sahrens void
24805450Sbrendan vdev_clear_stats(vdev_t *vd)
24815450Sbrendan {
24825450Sbrendan 	mutex_enter(&vd->vdev_stat_lock);
24835450Sbrendan 	vd->vdev_stat.vs_space = 0;
24845450Sbrendan 	vd->vdev_stat.vs_dspace = 0;
24855450Sbrendan 	vd->vdev_stat.vs_alloc = 0;
24865450Sbrendan 	mutex_exit(&vd->vdev_stat_lock);
24875450Sbrendan }
24885450Sbrendan 
24895450Sbrendan void
249012296SLin.Ling@Sun.COM vdev_scan_stat_init(vdev_t *vd)
249112296SLin.Ling@Sun.COM {
249212296SLin.Ling@Sun.COM 	vdev_stat_t *vs = &vd->vdev_stat;
249312296SLin.Ling@Sun.COM 
249412296SLin.Ling@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
249512296SLin.Ling@Sun.COM 		vdev_scan_stat_init(vd->vdev_child[c]);
249612296SLin.Ling@Sun.COM 
249712296SLin.Ling@Sun.COM 	mutex_enter(&vd->vdev_stat_lock);
249812296SLin.Ling@Sun.COM 	vs->vs_scan_processed = 0;
249912296SLin.Ling@Sun.COM 	mutex_exit(&vd->vdev_stat_lock);
250012296SLin.Ling@Sun.COM }
250112296SLin.Ling@Sun.COM 
250212296SLin.Ling@Sun.COM void
25037754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize)
2504789Sahrens {
25058241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
25068241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
25077754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2508789Sahrens 	vdev_t *pvd;
2509789Sahrens 	uint64_t txg = zio->io_txg;
2510789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2511789Sahrens 	zio_type_t type = zio->io_type;
2512789Sahrens 	int flags = zio->io_flags;
2513789Sahrens 
25147754SJeff.Bonwick@Sun.COM 	/*
25157754SJeff.Bonwick@Sun.COM 	 * If this i/o is a gang leader, it didn't do any actual work.
25167754SJeff.Bonwick@Sun.COM 	 */
25177754SJeff.Bonwick@Sun.COM 	if (zio->io_gang_tree)
25187754SJeff.Bonwick@Sun.COM 		return;
25197754SJeff.Bonwick@Sun.COM 
2520789Sahrens 	if (zio->io_error == 0) {
25217754SJeff.Bonwick@Sun.COM 		/*
25227754SJeff.Bonwick@Sun.COM 		 * If this is a root i/o, don't count it -- we've already
25237754SJeff.Bonwick@Sun.COM 		 * counted the top-level vdevs, and vdev_get_stats() will
25247754SJeff.Bonwick@Sun.COM 		 * aggregate them when asked.  This reduces contention on
25257754SJeff.Bonwick@Sun.COM 		 * the root vdev_stat_lock and implicitly handles blocks
25267754SJeff.Bonwick@Sun.COM 		 * that compress away to holes, for which there is no i/o.
25277754SJeff.Bonwick@Sun.COM 		 * (Holes never create vdev children, so all the counters
25287754SJeff.Bonwick@Sun.COM 		 * remain zero, which is what we want.)
25297754SJeff.Bonwick@Sun.COM 		 *
25307754SJeff.Bonwick@Sun.COM 		 * Note: this only applies to successful i/o (io_error == 0)
25317754SJeff.Bonwick@Sun.COM 		 * because unlike i/o counts, errors are not additive.
25327754SJeff.Bonwick@Sun.COM 		 * When reading a ditto block, for example, failure of
25337754SJeff.Bonwick@Sun.COM 		 * one top-level vdev does not imply a root-level error.
25347754SJeff.Bonwick@Sun.COM 		 */
25357754SJeff.Bonwick@Sun.COM 		if (vd == rvd)
25367754SJeff.Bonwick@Sun.COM 			return;
25377754SJeff.Bonwick@Sun.COM 
25387754SJeff.Bonwick@Sun.COM 		ASSERT(vd == zio->io_vd);
25398241SJeff.Bonwick@Sun.COM 
25408241SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_BYPASS)
25418241SJeff.Bonwick@Sun.COM 			return;
25428241SJeff.Bonwick@Sun.COM 
25438241SJeff.Bonwick@Sun.COM 		mutex_enter(&vd->vdev_stat_lock);
25448241SJeff.Bonwick@Sun.COM 
25457754SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_REPAIR) {
254612586SGeorge.Wilson@Sun.COM 			if (flags & ZIO_FLAG_SCAN_THREAD) {
254712296SLin.Ling@Sun.COM 				dsl_scan_phys_t *scn_phys =
254812296SLin.Ling@Sun.COM 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
254912296SLin.Ling@Sun.COM 				uint64_t *processed = &scn_phys->scn_processed;
255012296SLin.Ling@Sun.COM 
255112296SLin.Ling@Sun.COM 				/* XXX cleanup? */
255212296SLin.Ling@Sun.COM 				if (vd->vdev_ops->vdev_op_leaf)
255312296SLin.Ling@Sun.COM 					atomic_add_64(processed, psize);
255412296SLin.Ling@Sun.COM 				vs->vs_scan_processed += psize;
255512296SLin.Ling@Sun.COM 			}
255612296SLin.Ling@Sun.COM 
25578241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SELF_HEAL)
25587754SJeff.Bonwick@Sun.COM 				vs->vs_self_healed += psize;
2559789Sahrens 		}
25608241SJeff.Bonwick@Sun.COM 
25618241SJeff.Bonwick@Sun.COM 		vs->vs_ops[type]++;
25628241SJeff.Bonwick@Sun.COM 		vs->vs_bytes[type] += psize;
25638241SJeff.Bonwick@Sun.COM 
25648241SJeff.Bonwick@Sun.COM 		mutex_exit(&vd->vdev_stat_lock);
2565789Sahrens 		return;
2566789Sahrens 	}
2567789Sahrens 
2568789Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2569789Sahrens 		return;
2570789Sahrens 
25719725SEric.Schrock@Sun.COM 	/*
25729725SEric.Schrock@Sun.COM 	 * If this is an I/O error that is going to be retried, then ignore the
25739725SEric.Schrock@Sun.COM 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
25749725SEric.Schrock@Sun.COM 	 * hard errors, when in reality they can happen for any number of
25759725SEric.Schrock@Sun.COM 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
25769725SEric.Schrock@Sun.COM 	 */
25779725SEric.Schrock@Sun.COM 	if (zio->io_error == EIO &&
25789725SEric.Schrock@Sun.COM 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
25799725SEric.Schrock@Sun.COM 		return;
25809725SEric.Schrock@Sun.COM 
258110685SGeorge.Wilson@Sun.COM 	/*
258210685SGeorge.Wilson@Sun.COM 	 * Intent logs writes won't propagate their error to the root
258310685SGeorge.Wilson@Sun.COM 	 * I/O so don't mark these types of failures as pool-level
258410685SGeorge.Wilson@Sun.COM 	 * errors.
258510685SGeorge.Wilson@Sun.COM 	 */
258610685SGeorge.Wilson@Sun.COM 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
258710685SGeorge.Wilson@Sun.COM 		return;
258810685SGeorge.Wilson@Sun.COM 
25897754SJeff.Bonwick@Sun.COM 	mutex_enter(&vd->vdev_stat_lock);
25909230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
25917754SJeff.Bonwick@Sun.COM 		if (zio->io_error == ECKSUM)
25927754SJeff.Bonwick@Sun.COM 			vs->vs_checksum_errors++;
25937754SJeff.Bonwick@Sun.COM 		else
25947754SJeff.Bonwick@Sun.COM 			vs->vs_read_errors++;
2595789Sahrens 	}
25969230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
25977754SJeff.Bonwick@Sun.COM 		vs->vs_write_errors++;
25987754SJeff.Bonwick@Sun.COM 	mutex_exit(&vd->vdev_stat_lock);
2599789Sahrens 
26008241SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
26018241SJeff.Bonwick@Sun.COM 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
260212586SGeorge.Wilson@Sun.COM 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
260310922SJeff.Bonwick@Sun.COM 	    spa->spa_claiming)) {
26048241SJeff.Bonwick@Sun.COM 		/*
260510922SJeff.Bonwick@Sun.COM 		 * This is either a normal write (not a repair), or it's
260610922SJeff.Bonwick@Sun.COM 		 * a repair induced by the scrub thread, or it's a repair
260710922SJeff.Bonwick@Sun.COM 		 * made by zil_claim() during spa_load() in the first txg.
260810922SJeff.Bonwick@Sun.COM 		 * In the normal case, we commit the DTL change in the same
260910922SJeff.Bonwick@Sun.COM 		 * txg as the block was born.  In the scrub-induced repair
261010922SJeff.Bonwick@Sun.COM 		 * case, we know that scrubs run in first-pass syncing context,
261110922SJeff.Bonwick@Sun.COM 		 * so we commit the DTL change in spa_syncing_txg(spa).
261210922SJeff.Bonwick@Sun.COM 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
26138241SJeff.Bonwick@Sun.COM 		 *
26148241SJeff.Bonwick@Sun.COM 		 * We currently do not make DTL entries for failed spontaneous
26158241SJeff.Bonwick@Sun.COM 		 * self-healing writes triggered by normal (non-scrubbing)
26168241SJeff.Bonwick@Sun.COM 		 * reads, because we have no transactional context in which to
26178241SJeff.Bonwick@Sun.COM 		 * do so -- and it's not clear that it'd be desirable anyway.
26188241SJeff.Bonwick@Sun.COM 		 */
26198241SJeff.Bonwick@Sun.COM 		if (vd->vdev_ops->vdev_op_leaf) {
26208241SJeff.Bonwick@Sun.COM 			uint64_t commit_txg = txg;
262112586SGeorge.Wilson@Sun.COM 			if (flags & ZIO_FLAG_SCAN_THREAD) {
26228241SJeff.Bonwick@Sun.COM 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
26238241SJeff.Bonwick@Sun.COM 				ASSERT(spa_sync_pass(spa) == 1);
26248241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
262510922SJeff.Bonwick@Sun.COM 				commit_txg = spa_syncing_txg(spa);
262610922SJeff.Bonwick@Sun.COM 			} else if (spa->spa_claiming) {
262710922SJeff.Bonwick@Sun.COM 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
262810922SJeff.Bonwick@Sun.COM 				commit_txg = spa_first_txg(spa);
26298241SJeff.Bonwick@Sun.COM 			}
263010922SJeff.Bonwick@Sun.COM 			ASSERT(commit_txg >= spa_syncing_txg(spa));
26318241SJeff.Bonwick@Sun.COM 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
26328241SJeff.Bonwick@Sun.COM 				return;
26338241SJeff.Bonwick@Sun.COM 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
26348241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
26358241SJeff.Bonwick@Sun.COM 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2636789Sahrens 		}
26378241SJeff.Bonwick@Sun.COM 		if (vd != rvd)
26388241SJeff.Bonwick@Sun.COM 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2639789Sahrens 	}
2640789Sahrens }
2641789Sahrens 
2642789Sahrens /*
264310922SJeff.Bonwick@Sun.COM  * Update the in-core space usage stats for this vdev, its metaslab class,
264410922SJeff.Bonwick@Sun.COM  * and the root vdev.
2645789Sahrens  */
2646789Sahrens void
264710922SJeff.Bonwick@Sun.COM vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
264810922SJeff.Bonwick@Sun.COM     int64_t space_delta)
2649789Sahrens {
26504527Sperrin 	int64_t dspace_delta = space_delta;
26514527Sperrin 	spa_t *spa = vd->vdev_spa;
26524527Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
265310922SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg = vd->vdev_mg;
265410922SJeff.Bonwick@Sun.COM 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
26554527Sperrin 
2656789Sahrens 	ASSERT(vd == vd->vdev_top);
26574527Sperrin 
26584527Sperrin 	/*
26594527Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
26604527Sperrin 	 * factor.  We must calculate this here and not at the root vdev
26614527Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
26624527Sperrin 	 * childrens', thus not accurate enough for us.
26634527Sperrin 	 */
26644527Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
26659701SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
26664527Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
26674527Sperrin 	    vd->vdev_deflate_ratio;
2668789Sahrens 
26694527Sperrin 	mutex_enter(&vd->vdev_stat_lock);
267010922SJeff.Bonwick@Sun.COM 	vd->vdev_stat.vs_alloc += alloc_delta;
26714527Sperrin 	vd->vdev_stat.vs_space += space_delta;
26724527Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
26734527Sperrin 	mutex_exit(&vd->vdev_stat_lock);
26742082Seschrock 
267510922SJeff.Bonwick@Sun.COM 	if (mc == spa_normal_class(spa)) {
267610922SJeff.Bonwick@Sun.COM 		mutex_enter(&rvd->vdev_stat_lock);
267710922SJeff.Bonwick@Sun.COM 		rvd->vdev_stat.vs_alloc += alloc_delta;
267810922SJeff.Bonwick@Sun.COM 		rvd->vdev_stat.vs_space += space_delta;
267910922SJeff.Bonwick@Sun.COM 		rvd->vdev_stat.vs_dspace += dspace_delta;
268010922SJeff.Bonwick@Sun.COM 		mutex_exit(&rvd->vdev_stat_lock);
268110922SJeff.Bonwick@Sun.COM 	}
268210922SJeff.Bonwick@Sun.COM 
268310922SJeff.Bonwick@Sun.COM 	if (mc != NULL) {
26845450Sbrendan 		ASSERT(rvd == vd->vdev_parent);
26855450Sbrendan 		ASSERT(vd->vdev_ms_count != 0);
26864527Sperrin 
268710922SJeff.Bonwick@Sun.COM 		metaslab_class_space_update(mc,
268810922SJeff.Bonwick@Sun.COM 		    alloc_delta, defer_delta, space_delta, dspace_delta);
26895450Sbrendan 	}
2690789Sahrens }
2691789Sahrens 
2692789Sahrens /*
2693789Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2694789Sahrens  * so that it will be written out next time the vdev configuration is synced.
2695789Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2696789Sahrens  */
2697789Sahrens void
2698789Sahrens vdev_config_dirty(vdev_t *vd)
2699789Sahrens {
2700789Sahrens 	spa_t *spa = vd->vdev_spa;
2701789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2702789Sahrens 	int c;
2703789Sahrens 
27041601Sbonwick 	/*
27059425SEric.Schrock@Sun.COM 	 * If this is an aux vdev (as with l2cache and spare devices), then we
27069425SEric.Schrock@Sun.COM 	 * update the vdev config manually and set the sync flag.
27076643Seschrock 	 */
27086643Seschrock 	if (vd->vdev_aux != NULL) {
27096643Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
27106643Seschrock 		nvlist_t **aux;
27116643Seschrock 		uint_t naux;
27126643Seschrock 
27136643Seschrock 		for (c = 0; c < sav->sav_count; c++) {
27146643Seschrock 			if (sav->sav_vdevs[c] == vd)
27156643Seschrock 				break;
27166643Seschrock 		}
27176643Seschrock 
27187754SJeff.Bonwick@Sun.COM 		if (c == sav->sav_count) {
27197754SJeff.Bonwick@Sun.COM 			/*
27207754SJeff.Bonwick@Sun.COM 			 * We're being removed.  There's nothing more to do.
27217754SJeff.Bonwick@Sun.COM 			 */
27227754SJeff.Bonwick@Sun.COM 			ASSERT(sav->sav_sync == B_TRUE);
27237754SJeff.Bonwick@Sun.COM 			return;
27247754SJeff.Bonwick@Sun.COM 		}
27257754SJeff.Bonwick@Sun.COM 
27266643Seschrock 		sav->sav_sync = B_TRUE;
27276643Seschrock 
27289425SEric.Schrock@Sun.COM 		if (nvlist_lookup_nvlist_array(sav->sav_config,
27299425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
27309425SEric.Schrock@Sun.COM 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
27319425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
27329425SEric.Schrock@Sun.COM 		}
27336643Seschrock 
27346643Seschrock 		ASSERT(c < naux);
27356643Seschrock 
27366643Seschrock 		/*
27376643Seschrock 		 * Setting the nvlist in the middle if the array is a little
27386643Seschrock 		 * sketchy, but it will work.
27396643Seschrock 		 */
27406643Seschrock 		nvlist_free(aux[c]);
274112296SLin.Ling@Sun.COM 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
27426643Seschrock 
27436643Seschrock 		return;
27446643Seschrock 	}
27456643Seschrock 
27466643Seschrock 	/*
27477754SJeff.Bonwick@Sun.COM 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
27487754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
27497754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
27501601Sbonwick 	 * so this is sufficient to ensure mutual exclusion.
27511601Sbonwick 	 */
27527754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
27537754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
27547754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
27551601Sbonwick 
2756789Sahrens 	if (vd == rvd) {
2757789Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2758789Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2759789Sahrens 	} else {
2760789Sahrens 		ASSERT(vd == vd->vdev_top);
2761789Sahrens 
276210594SGeorge.Wilson@Sun.COM 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
276310594SGeorge.Wilson@Sun.COM 		    !vd->vdev_ishole)
27647754SJeff.Bonwick@Sun.COM 			list_insert_head(&spa->spa_config_dirty_list, vd);
2765789Sahrens 	}
2766789Sahrens }
2767789Sahrens 
2768789Sahrens void
2769789Sahrens vdev_config_clean(vdev_t *vd)
2770789Sahrens {
27711601Sbonwick 	spa_t *spa = vd->vdev_spa;
27721601Sbonwick 
27737754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
27747754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
27757754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
27767754SJeff.Bonwick@Sun.COM 
27777754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
27787754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_config_dirty_list, vd);
27797754SJeff.Bonwick@Sun.COM }
27807754SJeff.Bonwick@Sun.COM 
27817754SJeff.Bonwick@Sun.COM /*
27827754SJeff.Bonwick@Sun.COM  * Mark a top-level vdev's state as dirty, so that the next pass of
27837754SJeff.Bonwick@Sun.COM  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
27847754SJeff.Bonwick@Sun.COM  * the state changes from larger config changes because they require
27857754SJeff.Bonwick@Sun.COM  * much less locking, and are often needed for administrative actions.
27867754SJeff.Bonwick@Sun.COM  */
27877754SJeff.Bonwick@Sun.COM void
27887754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd)
27897754SJeff.Bonwick@Sun.COM {
27907754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
27917754SJeff.Bonwick@Sun.COM 
27927754SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
27931601Sbonwick 
27947754SJeff.Bonwick@Sun.COM 	/*
27957754SJeff.Bonwick@Sun.COM 	 * The state list is protected by the SCL_STATE lock.  The caller
27967754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_STATE as writer, or must be the sync thread
27977754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
27987754SJeff.Bonwick@Sun.COM 	 * so this is sufficient to ensure mutual exclusion.
27997754SJeff.Bonwick@Sun.COM 	 */
28007754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
28017754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
28027754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
28037754SJeff.Bonwick@Sun.COM 
280410922SJeff.Bonwick@Sun.COM 	if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
28057754SJeff.Bonwick@Sun.COM 		list_insert_head(&spa->spa_state_dirty_list, vd);
28067754SJeff.Bonwick@Sun.COM }
28077754SJeff.Bonwick@Sun.COM 
28087754SJeff.Bonwick@Sun.COM void
28097754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd)
28107754SJeff.Bonwick@Sun.COM {
28117754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
28127754SJeff.Bonwick@Sun.COM 
28137754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
28147754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
28157754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
28167754SJeff.Bonwick@Sun.COM 
28177754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
28187754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_state_dirty_list, vd);
2819789Sahrens }
2820789Sahrens 
28216523Sek110237 /*
28226523Sek110237  * Propagate vdev state up from children to parent.
28236523Sek110237  */
28241775Sbillm void
28251775Sbillm vdev_propagate_state(vdev_t *vd)
28261775Sbillm {
28278241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
28288241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
28291775Sbillm 	int degraded = 0, faulted = 0;
28301775Sbillm 	int corrupted = 0;
28311775Sbillm 	vdev_t *child;
28321775Sbillm 
28334451Seschrock 	if (vd->vdev_children > 0) {
28349816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
28354451Seschrock 			child = vd->vdev_child[c];
28366976Seschrock 
283710594SGeorge.Wilson@Sun.COM 			/*
283810594SGeorge.Wilson@Sun.COM 			 * Don't factor holes into the decision.
283910594SGeorge.Wilson@Sun.COM 			 */
284010594SGeorge.Wilson@Sun.COM 			if (child->vdev_ishole)
284110594SGeorge.Wilson@Sun.COM 				continue;
284210594SGeorge.Wilson@Sun.COM 
28437754SJeff.Bonwick@Sun.COM 			if (!vdev_readable(child) ||
28448241SJeff.Bonwick@Sun.COM 			    (!vdev_writeable(child) && spa_writeable(spa))) {
28456976Seschrock 				/*
28466976Seschrock 				 * Root special: if there is a top-level log
28476976Seschrock 				 * device, treat the root vdev as if it were
28486976Seschrock 				 * degraded.
28496976Seschrock 				 */
28506976Seschrock 				if (child->vdev_islog && vd == rvd)
28516976Seschrock 					degraded++;
28526976Seschrock 				else
28536976Seschrock 					faulted++;
28546976Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
28554451Seschrock 				degraded++;
28566976Seschrock 			}
28574451Seschrock 
28584451Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
28594451Seschrock 				corrupted++;
28604451Seschrock 		}
28611775Sbillm 
28624451Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
28634451Seschrock 
28644451Seschrock 		/*
28657754SJeff.Bonwick@Sun.COM 		 * Root special: if there is a top-level vdev that cannot be
28664451Seschrock 		 * opened due to corrupted metadata, then propagate the root
28674451Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
28684451Seschrock 		 * replicas'.
28694451Seschrock 		 */
28704451Seschrock 		if (corrupted && vd == rvd &&
28714451Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
28724451Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
28734451Seschrock 			    VDEV_AUX_CORRUPT_DATA);
28741775Sbillm 	}
28751775Sbillm 
28766976Seschrock 	if (vd->vdev_parent)
28774451Seschrock 		vdev_propagate_state(vd->vdev_parent);
28781775Sbillm }
28791775Sbillm 
2880789Sahrens /*
28811544Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
28821544Seschrock  * state, because we're in the process of opening children depth-first.
28831544Seschrock  * Otherwise, we propagate the change to the parent.
28841544Seschrock  *
28851544Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
28861544Seschrock  * generated.
2887789Sahrens  */
2888789Sahrens void
28891544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2890789Sahrens {
28911986Seschrock 	uint64_t save_state;
28926643Seschrock 	spa_t *spa = vd->vdev_spa;
28931544Seschrock 
28941544Seschrock 	if (state == vd->vdev_state) {
28951544Seschrock 		vd->vdev_stat.vs_aux = aux;
2896789Sahrens 		return;
28971544Seschrock 	}
28981544Seschrock 
28991986Seschrock 	save_state = vd->vdev_state;
2900789Sahrens 
2901789Sahrens 	vd->vdev_state = state;
2902789Sahrens 	vd->vdev_stat.vs_aux = aux;
2903789Sahrens 
29044451Seschrock 	/*
29054451Seschrock 	 * If we are setting the vdev state to anything but an open state, then
290612247SGeorge.Wilson@Sun.COM 	 * always close the underlying device unless the device has requested
290712247SGeorge.Wilson@Sun.COM 	 * a delayed close (i.e. we're about to remove or fault the device).
290812247SGeorge.Wilson@Sun.COM 	 * Otherwise, we keep accessible but invalid devices open forever.
290912247SGeorge.Wilson@Sun.COM 	 * We don't call vdev_close() itself, because that implies some extra
291012247SGeorge.Wilson@Sun.COM 	 * checks (offline, etc) that we don't want here.  This is limited to
291112247SGeorge.Wilson@Sun.COM 	 * leaf devices, because otherwise closing the device will affect other
291212247SGeorge.Wilson@Sun.COM 	 * children.
29134451Seschrock 	 */
291412247SGeorge.Wilson@Sun.COM 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
291512247SGeorge.Wilson@Sun.COM 	    vd->vdev_ops->vdev_op_leaf)
29164451Seschrock 		vd->vdev_ops->vdev_op_close(vd);
29174451Seschrock 
291810817SEric.Schrock@Sun.COM 	/*
291910817SEric.Schrock@Sun.COM 	 * If we have brought this vdev back into service, we need
292010817SEric.Schrock@Sun.COM 	 * to notify fmd so that it can gracefully repair any outstanding
292110817SEric.Schrock@Sun.COM 	 * cases due to a missing device.  We do this in all cases, even those
292210817SEric.Schrock@Sun.COM 	 * that probably don't correlate to a repaired fault.  This is sure to
292310817SEric.Schrock@Sun.COM 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
292410817SEric.Schrock@Sun.COM 	 * this is a transient state it's OK, as the retire agent will
292510817SEric.Schrock@Sun.COM 	 * double-check the state of the vdev before repairing it.
292610817SEric.Schrock@Sun.COM 	 */
292710817SEric.Schrock@Sun.COM 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
292810817SEric.Schrock@Sun.COM 	    vd->vdev_prevstate != state)
292910817SEric.Schrock@Sun.COM 		zfs_post_state_change(spa, vd);
293010817SEric.Schrock@Sun.COM 
29314451Seschrock 	if (vd->vdev_removed &&
29324451Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
29334451Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
29344451Seschrock 		/*
29354451Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
29364451Seschrock 		 * device was previously marked removed and someone attempted to
29374451Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
29384451Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
29394451Seschrock 		 * it is one of our special test online cases, which is only
29404451Seschrock 		 * attempting to online the device and shouldn't generate an FMA
29414451Seschrock 		 * fault.
29424451Seschrock 		 */
29434451Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
29444451Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
29454451Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
29464451Seschrock 		vd->vdev_removed = B_TRUE;
29474451Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
29481544Seschrock 		/*
2949*13037SMark.Musante@Sun.COM 		 * If we fail to open a vdev during an import or recovery, we
2950*13037SMark.Musante@Sun.COM 		 * mark it as "not available", which signifies that it was
2951*13037SMark.Musante@Sun.COM 		 * never there to begin with.  Failure to open such a device
2952*13037SMark.Musante@Sun.COM 		 * is not considered an error.
29531544Seschrock 		 */
2954*13037SMark.Musante@Sun.COM 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
2955*13037SMark.Musante@Sun.COM 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
29561986Seschrock 		    vd->vdev_ops->vdev_op_leaf)
29571986Seschrock 			vd->vdev_not_present = 1;
29581986Seschrock 
29591986Seschrock 		/*
29601986Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
29611986Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
29621986Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
29631986Seschrock 		 * want to post the ereport if the device was already in the
29641986Seschrock 		 * CANT_OPEN state beforehand.
29654451Seschrock 		 *
29664451Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
29674451Seschrock 		 * online the device in response to an insertion event.  If we
29684451Seschrock 		 * hit this case, then we have detected an insertion event for a
29694451Seschrock 		 * faulted or offline device that wasn't in the removed state.
29704451Seschrock 		 * In this scenario, we don't post an ereport because we are
29714451Seschrock 		 * about to replace the device, or attempt an online with
29724451Seschrock 		 * vdev_forcefault, which will generate the fault for us.
29731986Seschrock 		 */
29744451Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
29754451Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
29766643Seschrock 		    vd != spa->spa_root_vdev) {
29771544Seschrock 			const char *class;
29781544Seschrock 
29791544Seschrock 			switch (aux) {
29801544Seschrock 			case VDEV_AUX_OPEN_FAILED:
29811544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
29821544Seschrock 				break;
29831544Seschrock 			case VDEV_AUX_CORRUPT_DATA:
29841544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
29851544Seschrock 				break;
29861544Seschrock 			case VDEV_AUX_NO_REPLICAS:
29871544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
29881544Seschrock 				break;
29891544Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
29901544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
29911544Seschrock 				break;
29921544Seschrock 			case VDEV_AUX_TOO_SMALL:
29931544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
29941544Seschrock 				break;
29951544Seschrock 			case VDEV_AUX_BAD_LABEL:
29961544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
29971544Seschrock 				break;
29981544Seschrock 			default:
29991544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
30001544Seschrock 			}
30011544Seschrock 
30026643Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
30031544Seschrock 		}
30044451Seschrock 
30054451Seschrock 		/* Erase any notion of persistent removed state */
30064451Seschrock 		vd->vdev_removed = B_FALSE;
30074451Seschrock 	} else {
30084451Seschrock 		vd->vdev_removed = B_FALSE;
30091544Seschrock 	}
30101544Seschrock 
30119583STim.Haley@Sun.COM 	if (!isopen && vd->vdev_parent)
30129583STim.Haley@Sun.COM 		vdev_propagate_state(vd->vdev_parent);
3013789Sahrens }
30147042Sgw25295 
30157042Sgw25295 /*
30167042Sgw25295  * Check the vdev configuration to ensure that it's capable of supporting
30177042Sgw25295  * a root pool. Currently, we do not support RAID-Z or partial configuration.
30187042Sgw25295  * In addition, only a single top-level vdev is allowed and none of the leaves
30197042Sgw25295  * can be wholedisks.
30207042Sgw25295  */
30217042Sgw25295 boolean_t
30227042Sgw25295 vdev_is_bootable(vdev_t *vd)
30237042Sgw25295 {
30247042Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf) {
30257042Sgw25295 		char *vdev_type = vd->vdev_ops->vdev_op_type;
30267042Sgw25295 
30277042Sgw25295 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
30287042Sgw25295 		    vd->vdev_children > 1) {
30297042Sgw25295 			return (B_FALSE);
30307042Sgw25295 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
30317042Sgw25295 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
30327042Sgw25295 			return (B_FALSE);
30337042Sgw25295 		}
30347042Sgw25295 	} else if (vd->vdev_wholedisk == 1) {
30357042Sgw25295 		return (B_FALSE);
30367042Sgw25295 	}
30377042Sgw25295 
30389816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
30397042Sgw25295 		if (!vdev_is_bootable(vd->vdev_child[c]))
30407042Sgw25295 			return (B_FALSE);
30417042Sgw25295 	}
30427042Sgw25295 	return (B_TRUE);
30437042Sgw25295 }
30449701SGeorge.Wilson@Sun.COM 
304510594SGeorge.Wilson@Sun.COM /*
304610594SGeorge.Wilson@Sun.COM  * Load the state from the original vdev tree (ovd) which
304710594SGeorge.Wilson@Sun.COM  * we've retrieved from the MOS config object. If the original
304812949SGeorge.Wilson@Sun.COM  * vdev was offline or faulted then we transfer that state to the
304912949SGeorge.Wilson@Sun.COM  * device in the current vdev tree (nvd).
305010594SGeorge.Wilson@Sun.COM  */
30519701SGeorge.Wilson@Sun.COM void
305210594SGeorge.Wilson@Sun.COM vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
30539701SGeorge.Wilson@Sun.COM {
305410594SGeorge.Wilson@Sun.COM 	spa_t *spa = nvd->vdev_spa;
305510594SGeorge.Wilson@Sun.COM 
305612949SGeorge.Wilson@Sun.COM 	ASSERT(nvd->vdev_top->vdev_islog);
305710594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
305810594SGeorge.Wilson@Sun.COM 	ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
305910594SGeorge.Wilson@Sun.COM 
306010594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < nvd->vdev_children; c++)
306110594SGeorge.Wilson@Sun.COM 		vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
306210594SGeorge.Wilson@Sun.COM 
306312949SGeorge.Wilson@Sun.COM 	if (nvd->vdev_ops->vdev_op_leaf) {
30649701SGeorge.Wilson@Sun.COM 		/*
306512949SGeorge.Wilson@Sun.COM 		 * Restore the persistent vdev state
30669701SGeorge.Wilson@Sun.COM 		 */
306710594SGeorge.Wilson@Sun.COM 		nvd->vdev_offline = ovd->vdev_offline;
306812949SGeorge.Wilson@Sun.COM 		nvd->vdev_faulted = ovd->vdev_faulted;
306912949SGeorge.Wilson@Sun.COM 		nvd->vdev_degraded = ovd->vdev_degraded;
307012949SGeorge.Wilson@Sun.COM 		nvd->vdev_removed = ovd->vdev_removed;
30719701SGeorge.Wilson@Sun.COM 	}
30729701SGeorge.Wilson@Sun.COM }
30739816SGeorge.Wilson@Sun.COM 
30749816SGeorge.Wilson@Sun.COM /*
307512949SGeorge.Wilson@Sun.COM  * Determine if a log device has valid content.  If the vdev was
307612949SGeorge.Wilson@Sun.COM  * removed or faulted in the MOS config then we know that
307712949SGeorge.Wilson@Sun.COM  * the content on the log device has already been written to the pool.
307812949SGeorge.Wilson@Sun.COM  */
307912949SGeorge.Wilson@Sun.COM boolean_t
308012949SGeorge.Wilson@Sun.COM vdev_log_state_valid(vdev_t *vd)
308112949SGeorge.Wilson@Sun.COM {
308212949SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
308312949SGeorge.Wilson@Sun.COM 	    !vd->vdev_removed)
308412949SGeorge.Wilson@Sun.COM 		return (B_TRUE);
308512949SGeorge.Wilson@Sun.COM 
308612949SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
308712949SGeorge.Wilson@Sun.COM 		if (vdev_log_state_valid(vd->vdev_child[c]))
308812949SGeorge.Wilson@Sun.COM 			return (B_TRUE);
308912949SGeorge.Wilson@Sun.COM 
309012949SGeorge.Wilson@Sun.COM 	return (B_FALSE);
309112949SGeorge.Wilson@Sun.COM }
309212949SGeorge.Wilson@Sun.COM 
309312949SGeorge.Wilson@Sun.COM /*
30949816SGeorge.Wilson@Sun.COM  * Expand a vdev if possible.
30959816SGeorge.Wilson@Sun.COM  */
30969816SGeorge.Wilson@Sun.COM void
30979816SGeorge.Wilson@Sun.COM vdev_expand(vdev_t *vd, uint64_t txg)
30989816SGeorge.Wilson@Sun.COM {
30999816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_top == vd);
31009816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
31019816SGeorge.Wilson@Sun.COM 
31029816SGeorge.Wilson@Sun.COM 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
31039816SGeorge.Wilson@Sun.COM 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
31049816SGeorge.Wilson@Sun.COM 		vdev_config_dirty(vd);
31059816SGeorge.Wilson@Sun.COM 	}
31069816SGeorge.Wilson@Sun.COM }
310711422SMark.Musante@Sun.COM 
310811422SMark.Musante@Sun.COM /*
310911422SMark.Musante@Sun.COM  * Split a vdev.
311011422SMark.Musante@Sun.COM  */
311111422SMark.Musante@Sun.COM void
311211422SMark.Musante@Sun.COM vdev_split(vdev_t *vd)
311311422SMark.Musante@Sun.COM {
311411422SMark.Musante@Sun.COM 	vdev_t *cvd, *pvd = vd->vdev_parent;
311511422SMark.Musante@Sun.COM 
311611422SMark.Musante@Sun.COM 	vdev_remove_child(pvd, vd);
311711422SMark.Musante@Sun.COM 	vdev_compact_children(pvd);
311811422SMark.Musante@Sun.COM 
311911422SMark.Musante@Sun.COM 	cvd = pvd->vdev_child[0];
312011422SMark.Musante@Sun.COM 	if (pvd->vdev_children == 1) {
312111422SMark.Musante@Sun.COM 		vdev_remove_parent(cvd);
312211422SMark.Musante@Sun.COM 		cvd->vdev_splitting = B_TRUE;
312311422SMark.Musante@Sun.COM 	}
312411422SMark.Musante@Sun.COM 	vdev_propagate_state(cvd);
312511422SMark.Musante@Sun.COM }
3126