xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 9583)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
238632SBill.Moore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <sys/zfs_context.h>
281544Seschrock #include <sys/fm/fs/zfs.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/spa_impl.h>
31789Sahrens #include <sys/dmu.h>
32789Sahrens #include <sys/dmu_tx.h>
33789Sahrens #include <sys/vdev_impl.h>
34789Sahrens #include <sys/uberblock_impl.h>
35789Sahrens #include <sys/metaslab.h>
36789Sahrens #include <sys/metaslab_impl.h>
37789Sahrens #include <sys/space_map.h>
38789Sahrens #include <sys/zio.h>
39789Sahrens #include <sys/zap.h>
40789Sahrens #include <sys/fs/zfs.h>
416643Seschrock #include <sys/arc.h>
42789Sahrens 
43789Sahrens /*
44789Sahrens  * Virtual device management.
45789Sahrens  */
46789Sahrens 
47789Sahrens static vdev_ops_t *vdev_ops_table[] = {
48789Sahrens 	&vdev_root_ops,
49789Sahrens 	&vdev_raidz_ops,
50789Sahrens 	&vdev_mirror_ops,
51789Sahrens 	&vdev_replacing_ops,
522082Seschrock 	&vdev_spare_ops,
53789Sahrens 	&vdev_disk_ops,
54789Sahrens 	&vdev_file_ops,
55789Sahrens 	&vdev_missing_ops,
56789Sahrens 	NULL
57789Sahrens };
58789Sahrens 
597046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
607046Sahrens int zfs_scrub_limit = 10;
613697Smishra 
62789Sahrens /*
63789Sahrens  * Given a vdev type, return the appropriate ops vector.
64789Sahrens  */
65789Sahrens static vdev_ops_t *
66789Sahrens vdev_getops(const char *type)
67789Sahrens {
68789Sahrens 	vdev_ops_t *ops, **opspp;
69789Sahrens 
70789Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
71789Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
72789Sahrens 			break;
73789Sahrens 
74789Sahrens 	return (ops);
75789Sahrens }
76789Sahrens 
77789Sahrens /*
78789Sahrens  * Default asize function: return the MAX of psize with the asize of
79789Sahrens  * all children.  This is what's used by anything other than RAID-Z.
80789Sahrens  */
81789Sahrens uint64_t
82789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
83789Sahrens {
841732Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
85789Sahrens 	uint64_t csize;
86789Sahrens 	uint64_t c;
87789Sahrens 
88789Sahrens 	for (c = 0; c < vd->vdev_children; c++) {
89789Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
90789Sahrens 		asize = MAX(asize, csize);
91789Sahrens 	}
92789Sahrens 
93789Sahrens 	return (asize);
94789Sahrens }
95789Sahrens 
961175Slling /*
971175Slling  * Get the replaceable or attachable device size.
981175Slling  * If the parent is a mirror or raidz, the replaceable size is the minimum
991175Slling  * psize of all its children. For the rest, just return our own psize.
1001175Slling  *
1011175Slling  * e.g.
1021175Slling  *			psize	rsize
1031175Slling  * root			-	-
1041175Slling  *	mirror/raidz	-	-
1051175Slling  *	    disk1	20g	20g
1061175Slling  *	    disk2 	40g	20g
1071175Slling  *	disk3 		80g	80g
1081175Slling  */
1091175Slling uint64_t
1101175Slling vdev_get_rsize(vdev_t *vd)
1111175Slling {
1121175Slling 	vdev_t *pvd, *cvd;
1131175Slling 	uint64_t c, rsize;
1141175Slling 
1151175Slling 	pvd = vd->vdev_parent;
1161175Slling 
1171175Slling 	/*
1181175Slling 	 * If our parent is NULL or the root, just return our own psize.
1191175Slling 	 */
1201175Slling 	if (pvd == NULL || pvd->vdev_parent == NULL)
1211175Slling 		return (vd->vdev_psize);
1221175Slling 
1231175Slling 	rsize = 0;
1241175Slling 
1251175Slling 	for (c = 0; c < pvd->vdev_children; c++) {
1261175Slling 		cvd = pvd->vdev_child[c];
1271175Slling 		rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
1281175Slling 	}
1291175Slling 
1301175Slling 	return (rsize);
1311175Slling }
1321175Slling 
133789Sahrens vdev_t *
134789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
135789Sahrens {
136789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
137789Sahrens 
1387754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1395530Sbonwick 
1407046Sahrens 	if (vdev < rvd->vdev_children) {
1417046Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
142789Sahrens 		return (rvd->vdev_child[vdev]);
1437046Sahrens 	}
144789Sahrens 
145789Sahrens 	return (NULL);
146789Sahrens }
147789Sahrens 
148789Sahrens vdev_t *
149789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
150789Sahrens {
151789Sahrens 	int c;
152789Sahrens 	vdev_t *mvd;
153789Sahrens 
1541585Sbonwick 	if (vd->vdev_guid == guid)
155789Sahrens 		return (vd);
156789Sahrens 
157789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
158789Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
159789Sahrens 		    NULL)
160789Sahrens 			return (mvd);
161789Sahrens 
162789Sahrens 	return (NULL);
163789Sahrens }
164789Sahrens 
165789Sahrens void
166789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
167789Sahrens {
168789Sahrens 	size_t oldsize, newsize;
169789Sahrens 	uint64_t id = cvd->vdev_id;
170789Sahrens 	vdev_t **newchild;
171789Sahrens 
1727754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
173789Sahrens 	ASSERT(cvd->vdev_parent == NULL);
174789Sahrens 
175789Sahrens 	cvd->vdev_parent = pvd;
176789Sahrens 
177789Sahrens 	if (pvd == NULL)
178789Sahrens 		return;
179789Sahrens 
180789Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
181789Sahrens 
182789Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
183789Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
184789Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
185789Sahrens 
186789Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
187789Sahrens 	if (pvd->vdev_child != NULL) {
188789Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
189789Sahrens 		kmem_free(pvd->vdev_child, oldsize);
190789Sahrens 	}
191789Sahrens 
192789Sahrens 	pvd->vdev_child = newchild;
193789Sahrens 	pvd->vdev_child[id] = cvd;
194789Sahrens 
195789Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
196789Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
197789Sahrens 
198789Sahrens 	/*
199789Sahrens 	 * Walk up all ancestors to update guid sum.
200789Sahrens 	 */
201789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
202789Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
2033697Smishra 
2043697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2053697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
206789Sahrens }
207789Sahrens 
208789Sahrens void
209789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
210789Sahrens {
211789Sahrens 	int c;
212789Sahrens 	uint_t id = cvd->vdev_id;
213789Sahrens 
214789Sahrens 	ASSERT(cvd->vdev_parent == pvd);
215789Sahrens 
216789Sahrens 	if (pvd == NULL)
217789Sahrens 		return;
218789Sahrens 
219789Sahrens 	ASSERT(id < pvd->vdev_children);
220789Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
221789Sahrens 
222789Sahrens 	pvd->vdev_child[id] = NULL;
223789Sahrens 	cvd->vdev_parent = NULL;
224789Sahrens 
225789Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
226789Sahrens 		if (pvd->vdev_child[c])
227789Sahrens 			break;
228789Sahrens 
229789Sahrens 	if (c == pvd->vdev_children) {
230789Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
231789Sahrens 		pvd->vdev_child = NULL;
232789Sahrens 		pvd->vdev_children = 0;
233789Sahrens 	}
234789Sahrens 
235789Sahrens 	/*
236789Sahrens 	 * Walk up all ancestors to update guid sum.
237789Sahrens 	 */
238789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
239789Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
2403697Smishra 
2413697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2423697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
243789Sahrens }
244789Sahrens 
245789Sahrens /*
246789Sahrens  * Remove any holes in the child array.
247789Sahrens  */
248789Sahrens void
249789Sahrens vdev_compact_children(vdev_t *pvd)
250789Sahrens {
251789Sahrens 	vdev_t **newchild, *cvd;
252789Sahrens 	int oldc = pvd->vdev_children;
253789Sahrens 	int newc, c;
254789Sahrens 
2557754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
256789Sahrens 
257789Sahrens 	for (c = newc = 0; c < oldc; c++)
258789Sahrens 		if (pvd->vdev_child[c])
259789Sahrens 			newc++;
260789Sahrens 
261789Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
262789Sahrens 
263789Sahrens 	for (c = newc = 0; c < oldc; c++) {
264789Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
265789Sahrens 			newchild[newc] = cvd;
266789Sahrens 			cvd->vdev_id = newc++;
267789Sahrens 		}
268789Sahrens 	}
269789Sahrens 
270789Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
271789Sahrens 	pvd->vdev_child = newchild;
272789Sahrens 	pvd->vdev_children = newc;
273789Sahrens }
274789Sahrens 
275789Sahrens /*
276789Sahrens  * Allocate and minimally initialize a vdev_t.
277789Sahrens  */
278789Sahrens static vdev_t *
279789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
280789Sahrens {
281789Sahrens 	vdev_t *vd;
282789Sahrens 
2831585Sbonwick 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
2841585Sbonwick 
2851585Sbonwick 	if (spa->spa_root_vdev == NULL) {
2861585Sbonwick 		ASSERT(ops == &vdev_root_ops);
2871585Sbonwick 		spa->spa_root_vdev = vd;
2881585Sbonwick 	}
289789Sahrens 
2901585Sbonwick 	if (guid == 0) {
2911585Sbonwick 		if (spa->spa_root_vdev == vd) {
2921585Sbonwick 			/*
2931585Sbonwick 			 * The root vdev's guid will also be the pool guid,
2941585Sbonwick 			 * which must be unique among all pools.
2951585Sbonwick 			 */
2961585Sbonwick 			while (guid == 0 || spa_guid_exists(guid, 0))
2971585Sbonwick 				guid = spa_get_random(-1ULL);
2981585Sbonwick 		} else {
2991585Sbonwick 			/*
3001585Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3011585Sbonwick 			 */
3021585Sbonwick 			while (guid == 0 ||
3031585Sbonwick 			    spa_guid_exists(spa_guid(spa), guid))
3041585Sbonwick 				guid = spa_get_random(-1ULL);
3051585Sbonwick 		}
3061585Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3071585Sbonwick 	}
308789Sahrens 
309789Sahrens 	vd->vdev_spa = spa;
310789Sahrens 	vd->vdev_id = id;
311789Sahrens 	vd->vdev_guid = guid;
312789Sahrens 	vd->vdev_guid_sum = guid;
313789Sahrens 	vd->vdev_ops = ops;
314789Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
315789Sahrens 
316789Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3172856Snd150628 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
3187754SJeff.Bonwick@Sun.COM 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3198241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
3208241SJeff.Bonwick@Sun.COM 		space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
3218241SJeff.Bonwick@Sun.COM 		    &vd->vdev_dtl_lock);
3228241SJeff.Bonwick@Sun.COM 	}
323789Sahrens 	txg_list_create(&vd->vdev_ms_list,
324789Sahrens 	    offsetof(struct metaslab, ms_txg_node));
325789Sahrens 	txg_list_create(&vd->vdev_dtl_list,
326789Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
327789Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3284451Seschrock 	vdev_queue_init(vd);
3294451Seschrock 	vdev_cache_init(vd);
330789Sahrens 
331789Sahrens 	return (vd);
332789Sahrens }
333789Sahrens 
334789Sahrens /*
335789Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
336789Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
337789Sahrens  * different for each case.
338789Sahrens  */
3392082Seschrock int
3402082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
3412082Seschrock     int alloctype)
342789Sahrens {
343789Sahrens 	vdev_ops_t *ops;
344789Sahrens 	char *type;
3454527Sperrin 	uint64_t guid = 0, islog, nparity;
346789Sahrens 	vdev_t *vd;
347789Sahrens 
3487754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
349789Sahrens 
350789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
3512082Seschrock 		return (EINVAL);
352789Sahrens 
353789Sahrens 	if ((ops = vdev_getops(type)) == NULL)
3542082Seschrock 		return (EINVAL);
355789Sahrens 
356789Sahrens 	/*
357789Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
358789Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
359789Sahrens 	 */
360789Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
361789Sahrens 		uint64_t label_id;
362789Sahrens 
363789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
364789Sahrens 		    label_id != id)
3652082Seschrock 			return (EINVAL);
366789Sahrens 
367789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3682082Seschrock 			return (EINVAL);
3692082Seschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
3702082Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3712082Seschrock 			return (EINVAL);
3725450Sbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
3735450Sbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3745450Sbrendan 			return (EINVAL);
375789Sahrens 	}
376789Sahrens 
3772082Seschrock 	/*
3782082Seschrock 	 * The first allocated vdev must be of type 'root'.
3792082Seschrock 	 */
3802082Seschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
3812082Seschrock 		return (EINVAL);
3822082Seschrock 
3834527Sperrin 	/*
3844527Sperrin 	 * Determine whether we're a log vdev.
3854527Sperrin 	 */
3864527Sperrin 	islog = 0;
3874527Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
3885094Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
3894527Sperrin 		return (ENOTSUP);
3904527Sperrin 
3914527Sperrin 	/*
3924527Sperrin 	 * Set the nparity property for RAID-Z vdevs.
3934527Sperrin 	 */
3944527Sperrin 	nparity = -1ULL;
3954527Sperrin 	if (ops == &vdev_raidz_ops) {
3964527Sperrin 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3974527Sperrin 		    &nparity) == 0) {
3984527Sperrin 			/*
3994527Sperrin 			 * Currently, we can only support 2 parity devices.
4004527Sperrin 			 */
4014527Sperrin 			if (nparity == 0 || nparity > 2)
4024527Sperrin 				return (EINVAL);
4034527Sperrin 			/*
4044527Sperrin 			 * Older versions can only support 1 parity device.
4054527Sperrin 			 */
4064527Sperrin 			if (nparity == 2 &&
4074577Sahrens 			    spa_version(spa) < SPA_VERSION_RAID6)
4084527Sperrin 				return (ENOTSUP);
4094527Sperrin 		} else {
4104527Sperrin 			/*
4114527Sperrin 			 * We require the parity to be specified for SPAs that
4124527Sperrin 			 * support multiple parity levels.
4134527Sperrin 			 */
4144577Sahrens 			if (spa_version(spa) >= SPA_VERSION_RAID6)
4154527Sperrin 				return (EINVAL);
4164527Sperrin 			/*
4174527Sperrin 			 * Otherwise, we default to 1 parity device for RAID-Z.
4184527Sperrin 			 */
4194527Sperrin 			nparity = 1;
4204527Sperrin 		}
4214527Sperrin 	} else {
4224527Sperrin 		nparity = 0;
4234527Sperrin 	}
4244527Sperrin 	ASSERT(nparity != -1ULL);
4254527Sperrin 
426789Sahrens 	vd = vdev_alloc_common(spa, id, guid, ops);
427789Sahrens 
4284527Sperrin 	vd->vdev_islog = islog;
4294527Sperrin 	vd->vdev_nparity = nparity;
4304527Sperrin 
431789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
432789Sahrens 		vd->vdev_path = spa_strdup(vd->vdev_path);
433789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
434789Sahrens 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4354451Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4364451Seschrock 	    &vd->vdev_physpath) == 0)
4374451Seschrock 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4389425SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4399425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
440789Sahrens 
441789Sahrens 	/*
4421171Seschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
4431171Seschrock 	 * as -1.
4441171Seschrock 	 */
4451171Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
4461171Seschrock 	    &vd->vdev_wholedisk) != 0)
4471171Seschrock 		vd->vdev_wholedisk = -1ULL;
4481171Seschrock 
4491171Seschrock 	/*
4501544Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
4511544Seschrock 	 * was not present at the time of import.
4521544Seschrock 	 */
4539425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4549425SEric.Schrock@Sun.COM 	    &vd->vdev_not_present);
4551544Seschrock 
4561544Seschrock 	/*
4571732Sbonwick 	 * Get the alignment requirement.
4581732Sbonwick 	 */
4591732Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
4601732Sbonwick 
4611732Sbonwick 	/*
462789Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
463789Sahrens 	 */
464789Sahrens 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
465789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
466789Sahrens 		    &vd->vdev_ms_array);
467789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
468789Sahrens 		    &vd->vdev_ms_shift);
469789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
470789Sahrens 		    &vd->vdev_asize);
471789Sahrens 	}
472789Sahrens 
473789Sahrens 	/*
4744451Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
475789Sahrens 	 */
4766643Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
4776643Seschrock 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) {
4786643Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
4796643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
4808241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl_smo.smo_object);
4816643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
4826643Seschrock 			    &vd->vdev_unspare);
4836643Seschrock 		}
4841732Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
4851732Sbonwick 		    &vd->vdev_offline);
4866643Seschrock 
4874451Seschrock 		/*
4884451Seschrock 		 * When importing a pool, we want to ignore the persistent fault
4894451Seschrock 		 * state, as the diagnosis made on another system may not be
4904451Seschrock 		 * valid in the current context.
4914451Seschrock 		 */
4924451Seschrock 		if (spa->spa_load_state == SPA_LOAD_OPEN) {
4934451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
4944451Seschrock 			    &vd->vdev_faulted);
4954451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
4964451Seschrock 			    &vd->vdev_degraded);
4974451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
4984451Seschrock 			    &vd->vdev_removed);
4994451Seschrock 		}
500789Sahrens 	}
501789Sahrens 
502789Sahrens 	/*
503789Sahrens 	 * Add ourselves to the parent's list of children.
504789Sahrens 	 */
505789Sahrens 	vdev_add_child(parent, vd);
506789Sahrens 
5072082Seschrock 	*vdp = vd;
5082082Seschrock 
5092082Seschrock 	return (0);
510789Sahrens }
511789Sahrens 
512789Sahrens void
513789Sahrens vdev_free(vdev_t *vd)
514789Sahrens {
515789Sahrens 	int c;
5164451Seschrock 	spa_t *spa = vd->vdev_spa;
517789Sahrens 
518789Sahrens 	/*
519789Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
520789Sahrens 	 * trying to ensure complicated semantics for all callers.
521789Sahrens 	 */
522789Sahrens 	vdev_close(vd);
523789Sahrens 
5247754SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
525789Sahrens 
526789Sahrens 	/*
527789Sahrens 	 * Free all children.
528789Sahrens 	 */
529789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
530789Sahrens 		vdev_free(vd->vdev_child[c]);
531789Sahrens 
532789Sahrens 	ASSERT(vd->vdev_child == NULL);
533789Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
534789Sahrens 
535789Sahrens 	/*
536789Sahrens 	 * Discard allocation state.
537789Sahrens 	 */
538789Sahrens 	if (vd == vd->vdev_top)
539789Sahrens 		vdev_metaslab_fini(vd);
540789Sahrens 
541789Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
5422082Seschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
543789Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
544789Sahrens 
545789Sahrens 	/*
546789Sahrens 	 * Remove this vdev from its parent's child list.
547789Sahrens 	 */
548789Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
549789Sahrens 
550789Sahrens 	ASSERT(vd->vdev_parent == NULL);
551789Sahrens 
5524451Seschrock 	/*
5534451Seschrock 	 * Clean up vdev structure.
5544451Seschrock 	 */
5554451Seschrock 	vdev_queue_fini(vd);
5564451Seschrock 	vdev_cache_fini(vd);
5574451Seschrock 
5584451Seschrock 	if (vd->vdev_path)
5594451Seschrock 		spa_strfree(vd->vdev_path);
5604451Seschrock 	if (vd->vdev_devid)
5614451Seschrock 		spa_strfree(vd->vdev_devid);
5624451Seschrock 	if (vd->vdev_physpath)
5634451Seschrock 		spa_strfree(vd->vdev_physpath);
5649425SEric.Schrock@Sun.COM 	if (vd->vdev_fru)
5659425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_fru);
5664451Seschrock 
5674451Seschrock 	if (vd->vdev_isspare)
5684451Seschrock 		spa_spare_remove(vd);
5695450Sbrendan 	if (vd->vdev_isl2cache)
5705450Sbrendan 		spa_l2cache_remove(vd);
5714451Seschrock 
5724451Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
5734451Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
5748241SJeff.Bonwick@Sun.COM 
5754451Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
5768241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
5778241SJeff.Bonwick@Sun.COM 		space_map_unload(&vd->vdev_dtl[t]);
5788241SJeff.Bonwick@Sun.COM 		space_map_destroy(&vd->vdev_dtl[t]);
5798241SJeff.Bonwick@Sun.COM 	}
5804451Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
5818241SJeff.Bonwick@Sun.COM 
5824451Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
5834451Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
5847754SJeff.Bonwick@Sun.COM 	mutex_destroy(&vd->vdev_probe_lock);
5854451Seschrock 
5864451Seschrock 	if (vd == spa->spa_root_vdev)
5874451Seschrock 		spa->spa_root_vdev = NULL;
5884451Seschrock 
5894451Seschrock 	kmem_free(vd, sizeof (vdev_t));
590789Sahrens }
591789Sahrens 
592789Sahrens /*
593789Sahrens  * Transfer top-level vdev state from svd to tvd.
594789Sahrens  */
595789Sahrens static void
596789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
597789Sahrens {
598789Sahrens 	spa_t *spa = svd->vdev_spa;
599789Sahrens 	metaslab_t *msp;
600789Sahrens 	vdev_t *vd;
601789Sahrens 	int t;
602789Sahrens 
603789Sahrens 	ASSERT(tvd == tvd->vdev_top);
604789Sahrens 
605789Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
606789Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
607789Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
608789Sahrens 
609789Sahrens 	svd->vdev_ms_array = 0;
610789Sahrens 	svd->vdev_ms_shift = 0;
611789Sahrens 	svd->vdev_ms_count = 0;
612789Sahrens 
613789Sahrens 	tvd->vdev_mg = svd->vdev_mg;
614789Sahrens 	tvd->vdev_ms = svd->vdev_ms;
615789Sahrens 
616789Sahrens 	svd->vdev_mg = NULL;
617789Sahrens 	svd->vdev_ms = NULL;
6181732Sbonwick 
6191732Sbonwick 	if (tvd->vdev_mg != NULL)
6201732Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
621789Sahrens 
622789Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
623789Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
6242082Seschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
625789Sahrens 
626789Sahrens 	svd->vdev_stat.vs_alloc = 0;
627789Sahrens 	svd->vdev_stat.vs_space = 0;
6282082Seschrock 	svd->vdev_stat.vs_dspace = 0;
629789Sahrens 
630789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
631789Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
632789Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
633789Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
634789Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
635789Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
636789Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
637789Sahrens 	}
638789Sahrens 
6397754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_config_dirty_node)) {
640789Sahrens 		vdev_config_clean(svd);
641789Sahrens 		vdev_config_dirty(tvd);
642789Sahrens 	}
643789Sahrens 
6447754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_state_dirty_node)) {
6457754SJeff.Bonwick@Sun.COM 		vdev_state_clean(svd);
6467754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(tvd);
6477754SJeff.Bonwick@Sun.COM 	}
6487754SJeff.Bonwick@Sun.COM 
6492082Seschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
6502082Seschrock 	svd->vdev_deflate_ratio = 0;
6514527Sperrin 
6524527Sperrin 	tvd->vdev_islog = svd->vdev_islog;
6534527Sperrin 	svd->vdev_islog = 0;
654789Sahrens }
655789Sahrens 
656789Sahrens static void
657789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
658789Sahrens {
659789Sahrens 	int c;
660789Sahrens 
661789Sahrens 	if (vd == NULL)
662789Sahrens 		return;
663789Sahrens 
664789Sahrens 	vd->vdev_top = tvd;
665789Sahrens 
666789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
667789Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
668789Sahrens }
669789Sahrens 
670789Sahrens /*
671789Sahrens  * Add a mirror/replacing vdev above an existing vdev.
672789Sahrens  */
673789Sahrens vdev_t *
674789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
675789Sahrens {
676789Sahrens 	spa_t *spa = cvd->vdev_spa;
677789Sahrens 	vdev_t *pvd = cvd->vdev_parent;
678789Sahrens 	vdev_t *mvd;
679789Sahrens 
6807754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
681789Sahrens 
682789Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
6831732Sbonwick 
6841732Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
6851732Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
6861732Sbonwick 	mvd->vdev_state = cvd->vdev_state;
6871732Sbonwick 
688789Sahrens 	vdev_remove_child(pvd, cvd);
689789Sahrens 	vdev_add_child(pvd, mvd);
690789Sahrens 	cvd->vdev_id = mvd->vdev_children;
691789Sahrens 	vdev_add_child(mvd, cvd);
692789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
693789Sahrens 
694789Sahrens 	if (mvd == mvd->vdev_top)
695789Sahrens 		vdev_top_transfer(cvd, mvd);
696789Sahrens 
697789Sahrens 	return (mvd);
698789Sahrens }
699789Sahrens 
700789Sahrens /*
701789Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
702789Sahrens  */
703789Sahrens void
704789Sahrens vdev_remove_parent(vdev_t *cvd)
705789Sahrens {
706789Sahrens 	vdev_t *mvd = cvd->vdev_parent;
707789Sahrens 	vdev_t *pvd = mvd->vdev_parent;
708789Sahrens 
7097754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
710789Sahrens 
711789Sahrens 	ASSERT(mvd->vdev_children == 1);
712789Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
7132082Seschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
7142082Seschrock 	    mvd->vdev_ops == &vdev_spare_ops);
7151732Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
716789Sahrens 
717789Sahrens 	vdev_remove_child(mvd, cvd);
718789Sahrens 	vdev_remove_child(pvd, mvd);
7198241SJeff.Bonwick@Sun.COM 
7207754SJeff.Bonwick@Sun.COM 	/*
7217754SJeff.Bonwick@Sun.COM 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
7227754SJeff.Bonwick@Sun.COM 	 * Otherwise, we could have detached an offline device, and when we
7237754SJeff.Bonwick@Sun.COM 	 * go to import the pool we'll think we have two top-level vdevs,
7247754SJeff.Bonwick@Sun.COM 	 * instead of a different version of the same top-level vdev.
7257754SJeff.Bonwick@Sun.COM 	 */
7268241SJeff.Bonwick@Sun.COM 	if (mvd->vdev_top == mvd) {
7278241SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
7288241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid += guid_delta;
7298241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid_sum += guid_delta;
7308241SJeff.Bonwick@Sun.COM 	}
731789Sahrens 	cvd->vdev_id = mvd->vdev_id;
732789Sahrens 	vdev_add_child(pvd, cvd);
733789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
734789Sahrens 
735789Sahrens 	if (cvd == cvd->vdev_top)
736789Sahrens 		vdev_top_transfer(mvd, cvd);
737789Sahrens 
738789Sahrens 	ASSERT(mvd->vdev_children == 0);
739789Sahrens 	vdev_free(mvd);
740789Sahrens }
741789Sahrens 
7421544Seschrock int
743789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
744789Sahrens {
745789Sahrens 	spa_t *spa = vd->vdev_spa;
7461732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
7474527Sperrin 	metaslab_class_t *mc;
7481732Sbonwick 	uint64_t m;
749789Sahrens 	uint64_t oldc = vd->vdev_ms_count;
750789Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
7511732Sbonwick 	metaslab_t **mspp;
7521732Sbonwick 	int error;
753789Sahrens 
7541585Sbonwick 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
7551585Sbonwick 		return (0);
7561585Sbonwick 
757789Sahrens 	ASSERT(oldc <= newc);
758789Sahrens 
7594527Sperrin 	if (vd->vdev_islog)
7604527Sperrin 		mc = spa->spa_log_class;
7614527Sperrin 	else
7624527Sperrin 		mc = spa->spa_normal_class;
7634527Sperrin 
7641732Sbonwick 	if (vd->vdev_mg == NULL)
7651732Sbonwick 		vd->vdev_mg = metaslab_group_create(mc, vd);
7661732Sbonwick 
7671732Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
7681732Sbonwick 
7691732Sbonwick 	if (oldc != 0) {
7701732Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
7711732Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
7721732Sbonwick 	}
7731732Sbonwick 
7741732Sbonwick 	vd->vdev_ms = mspp;
775789Sahrens 	vd->vdev_ms_count = newc;
776789Sahrens 
7771732Sbonwick 	for (m = oldc; m < newc; m++) {
7781732Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
779789Sahrens 		if (txg == 0) {
7801732Sbonwick 			uint64_t object = 0;
7811732Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
7829512SNeil.Perrin@Sun.COM 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
7839512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
7841732Sbonwick 			if (error)
7851732Sbonwick 				return (error);
7861732Sbonwick 			if (object != 0) {
7871732Sbonwick 				dmu_buf_t *db;
7881732Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
7891732Sbonwick 				if (error)
7901732Sbonwick 					return (error);
7914944Smaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
7924944Smaybee 				bcopy(db->db_data, &smo, sizeof (smo));
7931732Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
7941544Seschrock 				dmu_buf_rele(db, FTAG);
795789Sahrens 			}
796789Sahrens 		}
7971732Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
7981732Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
799789Sahrens 	}
800789Sahrens 
8011544Seschrock 	return (0);
802789Sahrens }
803789Sahrens 
804789Sahrens void
805789Sahrens vdev_metaslab_fini(vdev_t *vd)
806789Sahrens {
807789Sahrens 	uint64_t m;
808789Sahrens 	uint64_t count = vd->vdev_ms_count;
809789Sahrens 
810789Sahrens 	if (vd->vdev_ms != NULL) {
811789Sahrens 		for (m = 0; m < count; m++)
8121732Sbonwick 			if (vd->vdev_ms[m] != NULL)
8131732Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
814789Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
815789Sahrens 		vd->vdev_ms = NULL;
816789Sahrens 	}
817789Sahrens }
818789Sahrens 
8197754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats {
8207754SJeff.Bonwick@Sun.COM 	boolean_t	vps_readable;
8217754SJeff.Bonwick@Sun.COM 	boolean_t	vps_writeable;
8227754SJeff.Bonwick@Sun.COM 	int		vps_flags;
8237754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t;
8247754SJeff.Bonwick@Sun.COM 
8257754SJeff.Bonwick@Sun.COM static void
8267754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio)
8275329Sgw25295 {
8288241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
8298632SBill.Moore@Sun.COM 	vdev_t *vd = zio->io_vd;
8307754SJeff.Bonwick@Sun.COM 	vdev_probe_stats_t *vps = zio->io_private;
8318632SBill.Moore@Sun.COM 
8328632SBill.Moore@Sun.COM 	ASSERT(vd->vdev_probe_zio != NULL);
8337754SJeff.Bonwick@Sun.COM 
8347754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ) {
8357754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8367754SJeff.Bonwick@Sun.COM 			vps->vps_readable = 1;
8378241SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0 && spa_writeable(spa)) {
8388632SBill.Moore@Sun.COM 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
8397754SJeff.Bonwick@Sun.COM 			    zio->io_offset, zio->io_size, zio->io_data,
8407754SJeff.Bonwick@Sun.COM 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
8417754SJeff.Bonwick@Sun.COM 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
8427754SJeff.Bonwick@Sun.COM 		} else {
8437754SJeff.Bonwick@Sun.COM 			zio_buf_free(zio->io_data, zio->io_size);
8447754SJeff.Bonwick@Sun.COM 		}
8457754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
8467754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
8477754SJeff.Bonwick@Sun.COM 			vps->vps_writeable = 1;
8487754SJeff.Bonwick@Sun.COM 		zio_buf_free(zio->io_data, zio->io_size);
8497754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_NULL) {
8508632SBill.Moore@Sun.COM 		zio_t *pio;
8517754SJeff.Bonwick@Sun.COM 
8527754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read |= !vps->vps_readable;
8537754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write |= !vps->vps_writeable;
8547754SJeff.Bonwick@Sun.COM 
8557754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) &&
8568241SJeff.Bonwick@Sun.COM 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
8577754SJeff.Bonwick@Sun.COM 			zio->io_error = 0;
8587754SJeff.Bonwick@Sun.COM 		} else {
8597754SJeff.Bonwick@Sun.COM 			ASSERT(zio->io_error != 0);
8607754SJeff.Bonwick@Sun.COM 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
8618241SJeff.Bonwick@Sun.COM 			    spa, vd, NULL, 0, 0);
8627754SJeff.Bonwick@Sun.COM 			zio->io_error = ENXIO;
8637754SJeff.Bonwick@Sun.COM 		}
8648632SBill.Moore@Sun.COM 
8658632SBill.Moore@Sun.COM 		mutex_enter(&vd->vdev_probe_lock);
8668632SBill.Moore@Sun.COM 		ASSERT(vd->vdev_probe_zio == zio);
8678632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = NULL;
8688632SBill.Moore@Sun.COM 		mutex_exit(&vd->vdev_probe_lock);
8698632SBill.Moore@Sun.COM 
8708632SBill.Moore@Sun.COM 		while ((pio = zio_walk_parents(zio)) != NULL)
8718632SBill.Moore@Sun.COM 			if (!vdev_accessible(vd, pio))
8728632SBill.Moore@Sun.COM 				pio->io_error = ENXIO;
8738632SBill.Moore@Sun.COM 
8747754SJeff.Bonwick@Sun.COM 		kmem_free(vps, sizeof (*vps));
8757754SJeff.Bonwick@Sun.COM 	}
8767754SJeff.Bonwick@Sun.COM }
8775329Sgw25295 
8787754SJeff.Bonwick@Sun.COM /*
8797754SJeff.Bonwick@Sun.COM  * Determine whether this device is accessible by reading and writing
8807754SJeff.Bonwick@Sun.COM  * to several known locations: the pad regions of each vdev label
8817754SJeff.Bonwick@Sun.COM  * but the first (which we leave alone in case it contains a VTOC).
8827754SJeff.Bonwick@Sun.COM  */
8837754SJeff.Bonwick@Sun.COM zio_t *
8848632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio)
8857754SJeff.Bonwick@Sun.COM {
8867754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
8878632SBill.Moore@Sun.COM 	vdev_probe_stats_t *vps = NULL;
8888632SBill.Moore@Sun.COM 	zio_t *pio;
8897754SJeff.Bonwick@Sun.COM 
8907754SJeff.Bonwick@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
8917754SJeff.Bonwick@Sun.COM 
8928632SBill.Moore@Sun.COM 	/*
8938632SBill.Moore@Sun.COM 	 * Don't probe the probe.
8948632SBill.Moore@Sun.COM 	 */
8958632SBill.Moore@Sun.COM 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
8968632SBill.Moore@Sun.COM 		return (NULL);
8978632SBill.Moore@Sun.COM 
8988632SBill.Moore@Sun.COM 	/*
8998632SBill.Moore@Sun.COM 	 * To prevent 'probe storms' when a device fails, we create
9008632SBill.Moore@Sun.COM 	 * just one probe i/o at a time.  All zios that want to probe
9018632SBill.Moore@Sun.COM 	 * this vdev will become parents of the probe io.
9028632SBill.Moore@Sun.COM 	 */
9038632SBill.Moore@Sun.COM 	mutex_enter(&vd->vdev_probe_lock);
9048632SBill.Moore@Sun.COM 
9058632SBill.Moore@Sun.COM 	if ((pio = vd->vdev_probe_zio) == NULL) {
9068632SBill.Moore@Sun.COM 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
9078632SBill.Moore@Sun.COM 
9088632SBill.Moore@Sun.COM 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
9098632SBill.Moore@Sun.COM 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9108632SBill.Moore@Sun.COM 		    ZIO_FLAG_DONT_RETRY;
9118632SBill.Moore@Sun.COM 
9128632SBill.Moore@Sun.COM 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
9138632SBill.Moore@Sun.COM 			/*
9148632SBill.Moore@Sun.COM 			 * vdev_cant_read and vdev_cant_write can only
9158632SBill.Moore@Sun.COM 			 * transition from TRUE to FALSE when we have the
9168632SBill.Moore@Sun.COM 			 * SCL_ZIO lock as writer; otherwise they can only
9178632SBill.Moore@Sun.COM 			 * transition from FALSE to TRUE.  This ensures that
9188632SBill.Moore@Sun.COM 			 * any zio looking at these values can assume that
9198632SBill.Moore@Sun.COM 			 * failures persist for the life of the I/O.  That's
9208632SBill.Moore@Sun.COM 			 * important because when a device has intermittent
9218632SBill.Moore@Sun.COM 			 * connectivity problems, we want to ensure that
9228632SBill.Moore@Sun.COM 			 * they're ascribed to the device (ENXIO) and not
9238632SBill.Moore@Sun.COM 			 * the zio (EIO).
9248632SBill.Moore@Sun.COM 			 *
9258632SBill.Moore@Sun.COM 			 * Since we hold SCL_ZIO as writer here, clear both
9268632SBill.Moore@Sun.COM 			 * values so the probe can reevaluate from first
9278632SBill.Moore@Sun.COM 			 * principles.
9288632SBill.Moore@Sun.COM 			 */
9298632SBill.Moore@Sun.COM 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
9308632SBill.Moore@Sun.COM 			vd->vdev_cant_read = B_FALSE;
9318632SBill.Moore@Sun.COM 			vd->vdev_cant_write = B_FALSE;
9328632SBill.Moore@Sun.COM 		}
9338632SBill.Moore@Sun.COM 
9348632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
9358632SBill.Moore@Sun.COM 		    vdev_probe_done, vps,
9368632SBill.Moore@Sun.COM 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
9378632SBill.Moore@Sun.COM 
9388632SBill.Moore@Sun.COM 		if (zio != NULL) {
9398632SBill.Moore@Sun.COM 			vd->vdev_probe_wanted = B_TRUE;
9408632SBill.Moore@Sun.COM 			spa_async_request(spa, SPA_ASYNC_PROBE);
9418632SBill.Moore@Sun.COM 		}
9428632SBill.Moore@Sun.COM 	}
9438632SBill.Moore@Sun.COM 
9448632SBill.Moore@Sun.COM 	if (zio != NULL)
9458632SBill.Moore@Sun.COM 		zio_add_child(zio, pio);
9468632SBill.Moore@Sun.COM 
9478632SBill.Moore@Sun.COM 	mutex_exit(&vd->vdev_probe_lock);
9488632SBill.Moore@Sun.COM 
9498632SBill.Moore@Sun.COM 	if (vps == NULL) {
9508632SBill.Moore@Sun.COM 		ASSERT(zio != NULL);
9518632SBill.Moore@Sun.COM 		return (NULL);
9528632SBill.Moore@Sun.COM 	}
9537754SJeff.Bonwick@Sun.COM 
9547754SJeff.Bonwick@Sun.COM 	for (int l = 1; l < VDEV_LABELS; l++) {
9558632SBill.Moore@Sun.COM 		zio_nowait(zio_read_phys(pio, vd,
9567754SJeff.Bonwick@Sun.COM 		    vdev_label_offset(vd->vdev_psize, l,
9579056SLin.Ling@Sun.COM 		    offsetof(vdev_label_t, vl_pad2)),
9589056SLin.Ling@Sun.COM 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
9597754SJeff.Bonwick@Sun.COM 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
9607754SJeff.Bonwick@Sun.COM 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
9617754SJeff.Bonwick@Sun.COM 	}
9627754SJeff.Bonwick@Sun.COM 
9638632SBill.Moore@Sun.COM 	if (zio == NULL)
9648632SBill.Moore@Sun.COM 		return (pio);
9658632SBill.Moore@Sun.COM 
9668632SBill.Moore@Sun.COM 	zio_nowait(pio);
9678632SBill.Moore@Sun.COM 	return (NULL);
9685329Sgw25295 }
9695329Sgw25295 
970789Sahrens /*
971789Sahrens  * Prepare a virtual device for access.
972789Sahrens  */
973789Sahrens int
974789Sahrens vdev_open(vdev_t *vd)
975789Sahrens {
9768241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
977789Sahrens 	int error;
978789Sahrens 	int c;
979789Sahrens 	uint64_t osize = 0;
980789Sahrens 	uint64_t asize, psize;
9811732Sbonwick 	uint64_t ashift = 0;
982789Sahrens 
9838241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
9848241SJeff.Bonwick@Sun.COM 
985789Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
986789Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
987789Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
988789Sahrens 
989789Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
990789Sahrens 
9914451Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
9924451Seschrock 		ASSERT(vd->vdev_children == 0);
9934451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
9944451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
9954451Seschrock 		return (ENXIO);
9964451Seschrock 	} else if (vd->vdev_offline) {
997789Sahrens 		ASSERT(vd->vdev_children == 0);
9981544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
999789Sahrens 		return (ENXIO);
1000789Sahrens 	}
1001789Sahrens 
1002789Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1003789Sahrens 
10041544Seschrock 	if (zio_injection_enabled && error == 0)
10051544Seschrock 		error = zio_handle_device_injection(vd, ENXIO);
10061544Seschrock 
10074451Seschrock 	if (error) {
10084451Seschrock 		if (vd->vdev_removed &&
10094451Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
10104451Seschrock 			vd->vdev_removed = B_FALSE;
1011789Sahrens 
10121544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1013789Sahrens 		    vd->vdev_stat.vs_aux);
1014789Sahrens 		return (error);
1015789Sahrens 	}
1016789Sahrens 
10174451Seschrock 	vd->vdev_removed = B_FALSE;
10184451Seschrock 
10194451Seschrock 	if (vd->vdev_degraded) {
10204451Seschrock 		ASSERT(vd->vdev_children == 0);
10214451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
10224451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
10234451Seschrock 	} else {
10244451Seschrock 		vd->vdev_state = VDEV_STATE_HEALTHY;
10254451Seschrock 	}
1026789Sahrens 
1027789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
10281544Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
10291544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
10301544Seschrock 			    VDEV_AUX_NONE);
10311544Seschrock 			break;
10321544Seschrock 		}
1033789Sahrens 
1034789Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1035789Sahrens 
1036789Sahrens 	if (vd->vdev_children == 0) {
1037789Sahrens 		if (osize < SPA_MINDEVSIZE) {
10381544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10391544Seschrock 			    VDEV_AUX_TOO_SMALL);
1040789Sahrens 			return (EOVERFLOW);
1041789Sahrens 		}
1042789Sahrens 		psize = osize;
1043789Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1044789Sahrens 	} else {
10451732Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1046789Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
10471544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10481544Seschrock 			    VDEV_AUX_TOO_SMALL);
1049789Sahrens 			return (EOVERFLOW);
1050789Sahrens 		}
1051789Sahrens 		psize = 0;
1052789Sahrens 		asize = osize;
1053789Sahrens 	}
1054789Sahrens 
1055789Sahrens 	vd->vdev_psize = psize;
1056789Sahrens 
1057789Sahrens 	if (vd->vdev_asize == 0) {
1058789Sahrens 		/*
1059789Sahrens 		 * This is the first-ever open, so use the computed values.
10601732Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1061789Sahrens 		 */
1062789Sahrens 		vd->vdev_asize = asize;
10631732Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1064789Sahrens 	} else {
1065789Sahrens 		/*
1066789Sahrens 		 * Make sure the alignment requirement hasn't increased.
1067789Sahrens 		 */
10681732Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
10691544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10701544Seschrock 			    VDEV_AUX_BAD_LABEL);
1071789Sahrens 			return (EINVAL);
1072789Sahrens 		}
1073789Sahrens 
1074789Sahrens 		/*
1075789Sahrens 		 * Make sure the device hasn't shrunk.
1076789Sahrens 		 */
1077789Sahrens 		if (asize < vd->vdev_asize) {
10781544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
10791544Seschrock 			    VDEV_AUX_BAD_LABEL);
1080789Sahrens 			return (EINVAL);
1081789Sahrens 		}
1082789Sahrens 
1083789Sahrens 		/*
1084789Sahrens 		 * If all children are healthy and the asize has increased,
1085789Sahrens 		 * then we've experienced dynamic LUN growth.
1086789Sahrens 		 */
1087789Sahrens 		if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1088789Sahrens 		    asize > vd->vdev_asize) {
1089789Sahrens 			vd->vdev_asize = asize;
1090789Sahrens 		}
1091789Sahrens 	}
1092789Sahrens 
10931544Seschrock 	/*
10945329Sgw25295 	 * Ensure we can issue some IO before declaring the
10955329Sgw25295 	 * vdev open for business.
10965329Sgw25295 	 */
10977754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf &&
10987754SJeff.Bonwick@Sun.COM 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
10995329Sgw25295 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11007754SJeff.Bonwick@Sun.COM 		    VDEV_AUX_IO_FAILURE);
11015329Sgw25295 		return (error);
11025329Sgw25295 	}
11035329Sgw25295 
11045329Sgw25295 	/*
11052082Seschrock 	 * If this is a top-level vdev, compute the raidz-deflation
11062082Seschrock 	 * ratio.  Note, we hard-code in 128k (1<<17) because it is the
11072082Seschrock 	 * current "typical" blocksize.  Even if SPA_MAXBLOCKSIZE
11082082Seschrock 	 * changes, this algorithm must never change, or we will
11092082Seschrock 	 * inconsistently account for existing bp's.
11102082Seschrock 	 */
11112082Seschrock 	if (vd->vdev_top == vd) {
11122082Seschrock 		vd->vdev_deflate_ratio = (1<<17) /
11132082Seschrock 		    (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
11142082Seschrock 	}
11152082Seschrock 
11167046Sahrens 	/*
11177046Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
11188241SJeff.Bonwick@Sun.COM 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
11198241SJeff.Bonwick@Sun.COM 	 * since this would just restart the scrub we are already doing.
11207046Sahrens 	 */
11218241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
11228241SJeff.Bonwick@Sun.COM 	    vdev_resilver_needed(vd, NULL, NULL))
11238241SJeff.Bonwick@Sun.COM 		spa_async_request(spa, SPA_ASYNC_RESILVER);
11247046Sahrens 
1125789Sahrens 	return (0);
1126789Sahrens }
1127789Sahrens 
1128789Sahrens /*
11291986Seschrock  * Called once the vdevs are all opened, this routine validates the label
11301986Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
11314451Seschrock  * inadvertently do repair I/Os to the wrong device.
11321986Seschrock  *
11331986Seschrock  * This function will only return failure if one of the vdevs indicates that it
11341986Seschrock  * has since been destroyed or exported.  This is only possible if
11351986Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
11361986Seschrock  * will be updated but the function will return 0.
11371986Seschrock  */
11381986Seschrock int
11391986Seschrock vdev_validate(vdev_t *vd)
11401986Seschrock {
11411986Seschrock 	spa_t *spa = vd->vdev_spa;
11421986Seschrock 	int c;
11431986Seschrock 	nvlist_t *label;
11447754SJeff.Bonwick@Sun.COM 	uint64_t guid, top_guid;
11451986Seschrock 	uint64_t state;
11461986Seschrock 
11471986Seschrock 	for (c = 0; c < vd->vdev_children; c++)
11481986Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
11494070Smc142369 			return (EBADF);
11501986Seschrock 
11512174Seschrock 	/*
11522174Seschrock 	 * If the device has already failed, or was marked offline, don't do
11532174Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
11542174Seschrock 	 * overwrite the previous state.
11552174Seschrock 	 */
11567754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
11571986Seschrock 
11581986Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
11591986Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11601986Seschrock 			    VDEV_AUX_BAD_LABEL);
11611986Seschrock 			return (0);
11621986Seschrock 		}
11631986Seschrock 
11641986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
11651986Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
11661986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
11671986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11681986Seschrock 			nvlist_free(label);
11691986Seschrock 			return (0);
11701986Seschrock 		}
11711986Seschrock 
11727754SJeff.Bonwick@Sun.COM 		/*
11737754SJeff.Bonwick@Sun.COM 		 * If this vdev just became a top-level vdev because its
11747754SJeff.Bonwick@Sun.COM 		 * sibling was detached, it will have adopted the parent's
11757754SJeff.Bonwick@Sun.COM 		 * vdev guid -- but the label may or may not be on disk yet.
11767754SJeff.Bonwick@Sun.COM 		 * Fortunately, either version of the label will have the
11777754SJeff.Bonwick@Sun.COM 		 * same top guid, so if we're a top-level vdev, we can
11787754SJeff.Bonwick@Sun.COM 		 * safely compare to that instead.
11797754SJeff.Bonwick@Sun.COM 		 */
11801986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
11817754SJeff.Bonwick@Sun.COM 		    &guid) != 0 ||
11827754SJeff.Bonwick@Sun.COM 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
11837754SJeff.Bonwick@Sun.COM 		    &top_guid) != 0 ||
11847754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != guid &&
11857754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
11861986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
11871986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11881986Seschrock 			nvlist_free(label);
11891986Seschrock 			return (0);
11901986Seschrock 		}
11911986Seschrock 
11921986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
11931986Seschrock 		    &state) != 0) {
11941986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
11951986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11961986Seschrock 			nvlist_free(label);
11971986Seschrock 			return (0);
11981986Seschrock 		}
11991986Seschrock 
12001986Seschrock 		nvlist_free(label);
12011986Seschrock 
12021986Seschrock 		if (spa->spa_load_state == SPA_LOAD_OPEN &&
12031986Seschrock 		    state != POOL_STATE_ACTIVE)
12044070Smc142369 			return (EBADF);
12056976Seschrock 
12066976Seschrock 		/*
12076976Seschrock 		 * If we were able to open and validate a vdev that was
12086976Seschrock 		 * previously marked permanently unavailable, clear that state
12096976Seschrock 		 * now.
12106976Seschrock 		 */
12116976Seschrock 		if (vd->vdev_not_present)
12126976Seschrock 			vd->vdev_not_present = 0;
12131986Seschrock 	}
12141986Seschrock 
12151986Seschrock 	return (0);
12161986Seschrock }
12171986Seschrock 
12181986Seschrock /*
1219789Sahrens  * Close a virtual device.
1220789Sahrens  */
1221789Sahrens void
1222789Sahrens vdev_close(vdev_t *vd)
1223789Sahrens {
12248241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
12258241SJeff.Bonwick@Sun.COM 
12268241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
12278241SJeff.Bonwick@Sun.COM 
1228789Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1229789Sahrens 
12304451Seschrock 	vdev_cache_purge(vd);
1231789Sahrens 
12321986Seschrock 	/*
12331986Seschrock 	 * We record the previous state before we close it, so  that if we are
12341986Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
12351986Seschrock 	 * it's still faulted.
12361986Seschrock 	 */
12371986Seschrock 	vd->vdev_prevstate = vd->vdev_state;
12381986Seschrock 
1239789Sahrens 	if (vd->vdev_offline)
1240789Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1241789Sahrens 	else
1242789Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
12431544Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1244789Sahrens }
1245789Sahrens 
1246789Sahrens void
12471544Seschrock vdev_reopen(vdev_t *vd)
1248789Sahrens {
12491544Seschrock 	spa_t *spa = vd->vdev_spa;
1250789Sahrens 
12517754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
12521544Seschrock 
1253789Sahrens 	vdev_close(vd);
1254789Sahrens 	(void) vdev_open(vd);
1255789Sahrens 
1256789Sahrens 	/*
12573377Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
12583377Seschrock 	 * Otherwise, a device with an invalid label could be successfully
12593377Seschrock 	 * opened in response to vdev_reopen().
12603377Seschrock 	 */
12616643Seschrock 	if (vd->vdev_aux) {
12626643Seschrock 		(void) vdev_validate_aux(vd);
12637754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) && vdev_writeable(vd) &&
12649425SEric.Schrock@Sun.COM 		    vd->vdev_aux == &spa->spa_l2cache &&
12656643Seschrock 		    !l2arc_vdev_present(vd)) {
12666643Seschrock 			uint64_t size = vdev_get_rsize(vd);
12676643Seschrock 			l2arc_add_vdev(spa, vd,
12686643Seschrock 			    VDEV_LABEL_START_SIZE,
12696643Seschrock 			    size - VDEV_LABEL_START_SIZE);
12706643Seschrock 		}
12716643Seschrock 	} else {
12726643Seschrock 		(void) vdev_validate(vd);
12736643Seschrock 	}
12743377Seschrock 
12753377Seschrock 	/*
12764451Seschrock 	 * Reassess parent vdev's health.
1277789Sahrens 	 */
12784451Seschrock 	vdev_propagate_state(vd);
1279789Sahrens }
1280789Sahrens 
1281789Sahrens int
12822082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1283789Sahrens {
1284789Sahrens 	int error;
1285789Sahrens 
1286789Sahrens 	/*
1287789Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1288789Sahrens 	 * For a create, however, we want to fail the request if
1289789Sahrens 	 * there are any components we can't open.
1290789Sahrens 	 */
1291789Sahrens 	error = vdev_open(vd);
1292789Sahrens 
1293789Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1294789Sahrens 		vdev_close(vd);
1295789Sahrens 		return (error ? error : ENXIO);
1296789Sahrens 	}
1297789Sahrens 
1298789Sahrens 	/*
1299789Sahrens 	 * Recursively initialize all labels.
1300789Sahrens 	 */
13013377Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
13023377Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1303789Sahrens 		vdev_close(vd);
1304789Sahrens 		return (error);
1305789Sahrens 	}
1306789Sahrens 
1307789Sahrens 	return (0);
1308789Sahrens }
1309789Sahrens 
1310789Sahrens /*
1311789Sahrens  * The is the latter half of vdev_create().  It is distinct because it
1312789Sahrens  * involves initiating transactions in order to do metaslab creation.
1313789Sahrens  * For creation, we want to try to create all vdevs at once and then undo it
1314789Sahrens  * if anything fails; this is much harder if we have pending transactions.
1315789Sahrens  */
13161585Sbonwick void
1317789Sahrens vdev_init(vdev_t *vd, uint64_t txg)
1318789Sahrens {
1319789Sahrens 	/*
1320789Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1321789Sahrens 	 */
1322789Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1323789Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1324789Sahrens 
1325789Sahrens 	/*
13261585Sbonwick 	 * Initialize the vdev's metaslabs.  This can't fail because
13271585Sbonwick 	 * there's nothing to read when creating all new metaslabs.
1328789Sahrens 	 */
13291585Sbonwick 	VERIFY(vdev_metaslab_init(vd, txg) == 0);
1330789Sahrens }
1331789Sahrens 
1332789Sahrens void
13331732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1334789Sahrens {
13351732Sbonwick 	ASSERT(vd == vd->vdev_top);
13361732Sbonwick 	ASSERT(ISP2(flags));
1337789Sahrens 
13381732Sbonwick 	if (flags & VDD_METASLAB)
13391732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
13401732Sbonwick 
13411732Sbonwick 	if (flags & VDD_DTL)
13421732Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
13431732Sbonwick 
13441732Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1345789Sahrens }
1346789Sahrens 
13478241SJeff.Bonwick@Sun.COM /*
13488241SJeff.Bonwick@Sun.COM  * DTLs.
13498241SJeff.Bonwick@Sun.COM  *
13508241SJeff.Bonwick@Sun.COM  * A vdev's DTL (dirty time log) is the set of transaction groups for which
13518241SJeff.Bonwick@Sun.COM  * the vdev has less than perfect replication.  There are three kinds of DTL:
13528241SJeff.Bonwick@Sun.COM  *
13538241SJeff.Bonwick@Sun.COM  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
13548241SJeff.Bonwick@Sun.COM  *
13558241SJeff.Bonwick@Sun.COM  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
13568241SJeff.Bonwick@Sun.COM  *
13578241SJeff.Bonwick@Sun.COM  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
13588241SJeff.Bonwick@Sun.COM  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
13598241SJeff.Bonwick@Sun.COM  *	txgs that was scrubbed.
13608241SJeff.Bonwick@Sun.COM  *
13618241SJeff.Bonwick@Sun.COM  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
13628241SJeff.Bonwick@Sun.COM  *	persistent errors or just some device being offline.
13638241SJeff.Bonwick@Sun.COM  *	Unlike the other three, the DTL_OUTAGE map is not generally
13648241SJeff.Bonwick@Sun.COM  *	maintained; it's only computed when needed, typically to
13658241SJeff.Bonwick@Sun.COM  *	determine whether a device can be detached.
13668241SJeff.Bonwick@Sun.COM  *
13678241SJeff.Bonwick@Sun.COM  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
13688241SJeff.Bonwick@Sun.COM  * either has the data or it doesn't.
13698241SJeff.Bonwick@Sun.COM  *
13708241SJeff.Bonwick@Sun.COM  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
13718241SJeff.Bonwick@Sun.COM  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
13728241SJeff.Bonwick@Sun.COM  * if any child is less than fully replicated, then so is its parent.
13738241SJeff.Bonwick@Sun.COM  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
13748241SJeff.Bonwick@Sun.COM  * comprising only those txgs which appear in 'maxfaults' or more children;
13758241SJeff.Bonwick@Sun.COM  * those are the txgs we don't have enough replication to read.  For example,
13768241SJeff.Bonwick@Sun.COM  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
13778241SJeff.Bonwick@Sun.COM  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
13788241SJeff.Bonwick@Sun.COM  * two child DTL_MISSING maps.
13798241SJeff.Bonwick@Sun.COM  *
13808241SJeff.Bonwick@Sun.COM  * It should be clear from the above that to compute the DTLs and outage maps
13818241SJeff.Bonwick@Sun.COM  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
13828241SJeff.Bonwick@Sun.COM  * Therefore, that is all we keep on disk.  When loading the pool, or after
13838241SJeff.Bonwick@Sun.COM  * a configuration change, we generate all other DTLs from first principles.
13848241SJeff.Bonwick@Sun.COM  */
1385789Sahrens void
13868241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1387789Sahrens {
13888241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
13898241SJeff.Bonwick@Sun.COM 
13908241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
13918241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
13928241SJeff.Bonwick@Sun.COM 
1393789Sahrens 	mutex_enter(sm->sm_lock);
1394789Sahrens 	if (!space_map_contains(sm, txg, size))
1395789Sahrens 		space_map_add(sm, txg, size);
1396789Sahrens 	mutex_exit(sm->sm_lock);
1397789Sahrens }
1398789Sahrens 
13998241SJeff.Bonwick@Sun.COM boolean_t
14008241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1401789Sahrens {
14028241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
14038241SJeff.Bonwick@Sun.COM 	boolean_t dirty = B_FALSE;
14048241SJeff.Bonwick@Sun.COM 
14058241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
14068241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1407789Sahrens 
1408789Sahrens 	mutex_enter(sm->sm_lock);
14098241SJeff.Bonwick@Sun.COM 	if (sm->sm_space != 0)
14108241SJeff.Bonwick@Sun.COM 		dirty = space_map_contains(sm, txg, size);
1411789Sahrens 	mutex_exit(sm->sm_lock);
1412789Sahrens 
1413789Sahrens 	return (dirty);
1414789Sahrens }
1415789Sahrens 
14168241SJeff.Bonwick@Sun.COM boolean_t
14178241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
14188241SJeff.Bonwick@Sun.COM {
14198241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
14208241SJeff.Bonwick@Sun.COM 	boolean_t empty;
14218241SJeff.Bonwick@Sun.COM 
14228241SJeff.Bonwick@Sun.COM 	mutex_enter(sm->sm_lock);
14238241SJeff.Bonwick@Sun.COM 	empty = (sm->sm_space == 0);
14248241SJeff.Bonwick@Sun.COM 	mutex_exit(sm->sm_lock);
14258241SJeff.Bonwick@Sun.COM 
14268241SJeff.Bonwick@Sun.COM 	return (empty);
14278241SJeff.Bonwick@Sun.COM }
14288241SJeff.Bonwick@Sun.COM 
1429789Sahrens /*
1430789Sahrens  * Reassess DTLs after a config change or scrub completion.
1431789Sahrens  */
1432789Sahrens void
1433789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1434789Sahrens {
14351544Seschrock 	spa_t *spa = vd->vdev_spa;
14368241SJeff.Bonwick@Sun.COM 	avl_tree_t reftree;
14378241SJeff.Bonwick@Sun.COM 	int minref;
14388241SJeff.Bonwick@Sun.COM 
14398241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
14408241SJeff.Bonwick@Sun.COM 
14418241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
14428241SJeff.Bonwick@Sun.COM 		vdev_dtl_reassess(vd->vdev_child[c], txg,
14438241SJeff.Bonwick@Sun.COM 		    scrub_txg, scrub_done);
14448241SJeff.Bonwick@Sun.COM 
14458241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev)
14468241SJeff.Bonwick@Sun.COM 		return;
14478241SJeff.Bonwick@Sun.COM 
14488241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
1449789Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
14507046Sahrens 		if (scrub_txg != 0 &&
14517046Sahrens 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
14527046Sahrens 			/* XXX should check scrub_done? */
14537046Sahrens 			/*
14547046Sahrens 			 * We completed a scrub up to scrub_txg.  If we
14557046Sahrens 			 * did it without rebooting, then the scrub dtl
14567046Sahrens 			 * will be valid, so excise the old region and
14577046Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
14587046Sahrens 			 * dtl as-is if there was an error.
14598241SJeff.Bonwick@Sun.COM 			 *
14608241SJeff.Bonwick@Sun.COM 			 * There's little trick here: to excise the beginning
14618241SJeff.Bonwick@Sun.COM 			 * of the DTL_MISSING map, we put it into a reference
14628241SJeff.Bonwick@Sun.COM 			 * tree and then add a segment with refcnt -1 that
14638241SJeff.Bonwick@Sun.COM 			 * covers the range [0, scrub_txg).  This means
14648241SJeff.Bonwick@Sun.COM 			 * that each txg in that range has refcnt -1 or 0.
14658241SJeff.Bonwick@Sun.COM 			 * We then add DTL_SCRUB with a refcnt of 2, so that
14668241SJeff.Bonwick@Sun.COM 			 * entries in the range [0, scrub_txg) will have a
14678241SJeff.Bonwick@Sun.COM 			 * positive refcnt -- either 1 or 2.  We then convert
14688241SJeff.Bonwick@Sun.COM 			 * the reference tree into the new DTL_MISSING map.
14697046Sahrens 			 */
14708241SJeff.Bonwick@Sun.COM 			space_map_ref_create(&reftree);
14718241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
14728241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
14738241SJeff.Bonwick@Sun.COM 			space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
14748241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
14758241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_SCRUB], 2);
14768241SJeff.Bonwick@Sun.COM 			space_map_ref_generate_map(&reftree,
14778241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
14788241SJeff.Bonwick@Sun.COM 			space_map_ref_destroy(&reftree);
1479789Sahrens 		}
14808241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
14818241SJeff.Bonwick@Sun.COM 		space_map_walk(&vd->vdev_dtl[DTL_MISSING],
14828241SJeff.Bonwick@Sun.COM 		    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1483789Sahrens 		if (scrub_done)
14848241SJeff.Bonwick@Sun.COM 			space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
14858241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
14868241SJeff.Bonwick@Sun.COM 		if (!vdev_readable(vd))
14878241SJeff.Bonwick@Sun.COM 			space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
14888241SJeff.Bonwick@Sun.COM 		else
14898241SJeff.Bonwick@Sun.COM 			space_map_walk(&vd->vdev_dtl[DTL_MISSING],
14908241SJeff.Bonwick@Sun.COM 			    space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1491789Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
14927046Sahrens 
14931732Sbonwick 		if (txg != 0)
14941732Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1495789Sahrens 		return;
1496789Sahrens 	}
1497789Sahrens 
1498789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
14998241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
15008241SJeff.Bonwick@Sun.COM 		if (t == DTL_SCRUB)
15018241SJeff.Bonwick@Sun.COM 			continue;			/* leaf vdevs only */
15028241SJeff.Bonwick@Sun.COM 		if (t == DTL_PARTIAL)
15038241SJeff.Bonwick@Sun.COM 			minref = 1;			/* i.e. non-zero */
15048241SJeff.Bonwick@Sun.COM 		else if (vd->vdev_nparity != 0)
15058241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
15068241SJeff.Bonwick@Sun.COM 		else
15078241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_children;	/* any kind of mirror */
15088241SJeff.Bonwick@Sun.COM 		space_map_ref_create(&reftree);
15098241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
15108241SJeff.Bonwick@Sun.COM 			vdev_t *cvd = vd->vdev_child[c];
15118241SJeff.Bonwick@Sun.COM 			mutex_enter(&cvd->vdev_dtl_lock);
15128241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree, &cvd->vdev_dtl[t], 1);
15138241SJeff.Bonwick@Sun.COM 			mutex_exit(&cvd->vdev_dtl_lock);
15148241SJeff.Bonwick@Sun.COM 		}
15158241SJeff.Bonwick@Sun.COM 		space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
15168241SJeff.Bonwick@Sun.COM 		space_map_ref_destroy(&reftree);
15178241SJeff.Bonwick@Sun.COM 	}
1518789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1519789Sahrens }
1520789Sahrens 
1521789Sahrens static int
1522789Sahrens vdev_dtl_load(vdev_t *vd)
1523789Sahrens {
1524789Sahrens 	spa_t *spa = vd->vdev_spa;
15258241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
15261732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1527789Sahrens 	dmu_buf_t *db;
1528789Sahrens 	int error;
1529789Sahrens 
1530789Sahrens 	ASSERT(vd->vdev_children == 0);
1531789Sahrens 
1532789Sahrens 	if (smo->smo_object == 0)
1533789Sahrens 		return (0);
1534789Sahrens 
15351732Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
15361544Seschrock 		return (error);
15371732Sbonwick 
15384944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
15394944Smaybee 	bcopy(db->db_data, smo, sizeof (*smo));
15401544Seschrock 	dmu_buf_rele(db, FTAG);
1541789Sahrens 
1542789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
15438241SJeff.Bonwick@Sun.COM 	error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
15448241SJeff.Bonwick@Sun.COM 	    NULL, SM_ALLOC, smo, mos);
1545789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1546789Sahrens 
1547789Sahrens 	return (error);
1548789Sahrens }
1549789Sahrens 
1550789Sahrens void
1551789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1552789Sahrens {
1553789Sahrens 	spa_t *spa = vd->vdev_spa;
15548241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
15558241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
15561732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1557789Sahrens 	space_map_t smsync;
1558789Sahrens 	kmutex_t smlock;
1559789Sahrens 	dmu_buf_t *db;
1560789Sahrens 	dmu_tx_t *tx;
1561789Sahrens 
1562789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1563789Sahrens 
1564789Sahrens 	if (vd->vdev_detached) {
1565789Sahrens 		if (smo->smo_object != 0) {
15661732Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1567789Sahrens 			ASSERT3U(err, ==, 0);
1568789Sahrens 			smo->smo_object = 0;
1569789Sahrens 		}
1570789Sahrens 		dmu_tx_commit(tx);
1571789Sahrens 		return;
1572789Sahrens 	}
1573789Sahrens 
1574789Sahrens 	if (smo->smo_object == 0) {
1575789Sahrens 		ASSERT(smo->smo_objsize == 0);
1576789Sahrens 		ASSERT(smo->smo_alloc == 0);
15771732Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1578789Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1579789Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1580789Sahrens 		ASSERT(smo->smo_object != 0);
1581789Sahrens 		vdev_config_dirty(vd->vdev_top);
1582789Sahrens 	}
1583789Sahrens 
1584789Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1585789Sahrens 
1586789Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1587789Sahrens 	    &smlock);
1588789Sahrens 
1589789Sahrens 	mutex_enter(&smlock);
1590789Sahrens 
1591789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
15921732Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1593789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1594789Sahrens 
15951732Sbonwick 	space_map_truncate(smo, mos, tx);
15961732Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1597789Sahrens 
1598789Sahrens 	space_map_destroy(&smsync);
1599789Sahrens 
1600789Sahrens 	mutex_exit(&smlock);
1601789Sahrens 	mutex_destroy(&smlock);
1602789Sahrens 
16031732Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1604789Sahrens 	dmu_buf_will_dirty(db, tx);
16054944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
16064944Smaybee 	bcopy(smo, db->db_data, sizeof (*smo));
16071544Seschrock 	dmu_buf_rele(db, FTAG);
1608789Sahrens 
1609789Sahrens 	dmu_tx_commit(tx);
1610789Sahrens }
1611789Sahrens 
16127046Sahrens /*
16138241SJeff.Bonwick@Sun.COM  * Determine whether the specified vdev can be offlined/detached/removed
16148241SJeff.Bonwick@Sun.COM  * without losing data.
16158241SJeff.Bonwick@Sun.COM  */
16168241SJeff.Bonwick@Sun.COM boolean_t
16178241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd)
16188241SJeff.Bonwick@Sun.COM {
16198241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
16208241SJeff.Bonwick@Sun.COM 	vdev_t *tvd = vd->vdev_top;
16218241SJeff.Bonwick@Sun.COM 	uint8_t cant_read = vd->vdev_cant_read;
16228241SJeff.Bonwick@Sun.COM 	boolean_t required;
16238241SJeff.Bonwick@Sun.COM 
16248241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
16258241SJeff.Bonwick@Sun.COM 
16268241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev || vd == tvd)
16278241SJeff.Bonwick@Sun.COM 		return (B_TRUE);
16288241SJeff.Bonwick@Sun.COM 
16298241SJeff.Bonwick@Sun.COM 	/*
16308241SJeff.Bonwick@Sun.COM 	 * Temporarily mark the device as unreadable, and then determine
16318241SJeff.Bonwick@Sun.COM 	 * whether this results in any DTL outages in the top-level vdev.
16328241SJeff.Bonwick@Sun.COM 	 * If not, we can safely offline/detach/remove the device.
16338241SJeff.Bonwick@Sun.COM 	 */
16348241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = B_TRUE;
16358241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
16368241SJeff.Bonwick@Sun.COM 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
16378241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = cant_read;
16388241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
16398241SJeff.Bonwick@Sun.COM 
16408241SJeff.Bonwick@Sun.COM 	return (required);
16418241SJeff.Bonwick@Sun.COM }
16428241SJeff.Bonwick@Sun.COM 
16438241SJeff.Bonwick@Sun.COM /*
16447046Sahrens  * Determine if resilver is needed, and if so the txg range.
16457046Sahrens  */
16467046Sahrens boolean_t
16477046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
16487046Sahrens {
16497046Sahrens 	boolean_t needed = B_FALSE;
16507046Sahrens 	uint64_t thismin = UINT64_MAX;
16517046Sahrens 	uint64_t thismax = 0;
16527046Sahrens 
16537046Sahrens 	if (vd->vdev_children == 0) {
16547046Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
16558241SJeff.Bonwick@Sun.COM 		if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
16568241SJeff.Bonwick@Sun.COM 		    vdev_writeable(vd)) {
16577046Sahrens 			space_seg_t *ss;
16587046Sahrens 
16598241SJeff.Bonwick@Sun.COM 			ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
16607046Sahrens 			thismin = ss->ss_start - 1;
16618241SJeff.Bonwick@Sun.COM 			ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
16627046Sahrens 			thismax = ss->ss_end;
16637046Sahrens 			needed = B_TRUE;
16647046Sahrens 		}
16657046Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
16667046Sahrens 	} else {
16678241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
16687046Sahrens 			vdev_t *cvd = vd->vdev_child[c];
16697046Sahrens 			uint64_t cmin, cmax;
16707046Sahrens 
16717046Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
16727046Sahrens 				thismin = MIN(thismin, cmin);
16737046Sahrens 				thismax = MAX(thismax, cmax);
16747046Sahrens 				needed = B_TRUE;
16757046Sahrens 			}
16767046Sahrens 		}
16777046Sahrens 	}
16787046Sahrens 
16797046Sahrens 	if (needed && minp) {
16807046Sahrens 		*minp = thismin;
16817046Sahrens 		*maxp = thismax;
16827046Sahrens 	}
16837046Sahrens 	return (needed);
16847046Sahrens }
16857046Sahrens 
16861986Seschrock void
16871544Seschrock vdev_load(vdev_t *vd)
1688789Sahrens {
1689789Sahrens 	/*
1690789Sahrens 	 * Recursively load all children.
1691789Sahrens 	 */
16928241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
16931986Seschrock 		vdev_load(vd->vdev_child[c]);
1694789Sahrens 
1695789Sahrens 	/*
16961585Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1697789Sahrens 	 */
16981986Seschrock 	if (vd == vd->vdev_top &&
16991986Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
17001986Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
17011986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17021986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1703789Sahrens 
1704789Sahrens 	/*
1705789Sahrens 	 * If this is a leaf vdev, load its DTL.
1706789Sahrens 	 */
17071986Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
17081986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17091986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1710789Sahrens }
1711789Sahrens 
17122082Seschrock /*
17135450Sbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
17145450Sbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
17155450Sbrendan  * we make sure that we can open the underlying device, then try to read the
17165450Sbrendan  * label, and make sure that the label is sane and that it hasn't been
17175450Sbrendan  * repurposed to another pool.
17182082Seschrock  */
17192082Seschrock int
17205450Sbrendan vdev_validate_aux(vdev_t *vd)
17212082Seschrock {
17222082Seschrock 	nvlist_t *label;
17232082Seschrock 	uint64_t guid, version;
17242082Seschrock 	uint64_t state;
17252082Seschrock 
17267754SJeff.Bonwick@Sun.COM 	if (!vdev_readable(vd))
17276643Seschrock 		return (0);
17286643Seschrock 
17292082Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
17302082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
17312082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
17322082Seschrock 		return (-1);
17332082Seschrock 	}
17342082Seschrock 
17352082Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
17364577Sahrens 	    version > SPA_VERSION ||
17372082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
17382082Seschrock 	    guid != vd->vdev_guid ||
17392082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
17402082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
17412082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
17422082Seschrock 		nvlist_free(label);
17432082Seschrock 		return (-1);
17442082Seschrock 	}
17452082Seschrock 
17462082Seschrock 	/*
17472082Seschrock 	 * We don't actually check the pool state here.  If it's in fact in
17482082Seschrock 	 * use by another pool, we update this fact on the fly when requested.
17492082Seschrock 	 */
17502082Seschrock 	nvlist_free(label);
17512082Seschrock 	return (0);
17522082Seschrock }
17532082Seschrock 
1754789Sahrens void
1755789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
1756789Sahrens {
1757789Sahrens 	metaslab_t *msp;
1758789Sahrens 
1759789Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1760789Sahrens 		metaslab_sync_done(msp, txg);
1761789Sahrens }
1762789Sahrens 
1763789Sahrens void
1764789Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
1765789Sahrens {
1766789Sahrens 	spa_t *spa = vd->vdev_spa;
1767789Sahrens 	vdev_t *lvd;
1768789Sahrens 	metaslab_t *msp;
17691732Sbonwick 	dmu_tx_t *tx;
1770789Sahrens 
17711732Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
17721732Sbonwick 		ASSERT(vd == vd->vdev_top);
17731732Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
17741732Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
17751732Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
17761732Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
17771732Sbonwick 		vdev_config_dirty(vd);
17781732Sbonwick 		dmu_tx_commit(tx);
17791732Sbonwick 	}
1780789Sahrens 
17811732Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1782789Sahrens 		metaslab_sync(msp, txg);
17831732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
17841732Sbonwick 	}
1785789Sahrens 
1786789Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1787789Sahrens 		vdev_dtl_sync(lvd, txg);
1788789Sahrens 
1789789Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1790789Sahrens }
1791789Sahrens 
1792789Sahrens uint64_t
1793789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1794789Sahrens {
1795789Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1796789Sahrens }
1797789Sahrens 
17984451Seschrock /*
17994451Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
18004451Seschrock  * not be opened, and no I/O is attempted.
18014451Seschrock  */
1802789Sahrens int
18034451Seschrock vdev_fault(spa_t *spa, uint64_t guid)
18044451Seschrock {
18056643Seschrock 	vdev_t *vd;
18064451Seschrock 
18077754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
18084451Seschrock 
18096643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
18107754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
18117754SJeff.Bonwick@Sun.COM 
18124451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
18137754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
18144451Seschrock 
18154451Seschrock 	/*
18164451Seschrock 	 * Faulted state takes precedence over degraded.
18174451Seschrock 	 */
18184451Seschrock 	vd->vdev_faulted = 1ULL;
18194451Seschrock 	vd->vdev_degraded = 0ULL;
18207754SJeff.Bonwick@Sun.COM 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
18214451Seschrock 
18224451Seschrock 	/*
18238123SDavid.Marker@sun.com 	 * If marking the vdev as faulted cause the top-level vdev to become
18244451Seschrock 	 * unavailable, then back off and simply mark the vdev as degraded
18254451Seschrock 	 * instead.
18264451Seschrock 	 */
18276643Seschrock 	if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
18284451Seschrock 		vd->vdev_degraded = 1ULL;
18294451Seschrock 		vd->vdev_faulted = 0ULL;
18304451Seschrock 
18314451Seschrock 		/*
18324451Seschrock 		 * If we reopen the device and it's not dead, only then do we
18334451Seschrock 		 * mark it degraded.
18344451Seschrock 		 */
18354451Seschrock 		vdev_reopen(vd);
18364451Seschrock 
18375329Sgw25295 		if (vdev_readable(vd)) {
18384451Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
18394451Seschrock 			    VDEV_AUX_ERR_EXCEEDED);
18404451Seschrock 		}
18414451Seschrock 	}
18424451Seschrock 
18437754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
18444451Seschrock }
18454451Seschrock 
18464451Seschrock /*
18474451Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
18484451Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
18494451Seschrock  * as I/O is concerned.
18504451Seschrock  */
18514451Seschrock int
18524451Seschrock vdev_degrade(spa_t *spa, uint64_t guid)
18534451Seschrock {
18546643Seschrock 	vdev_t *vd;
18554451Seschrock 
18567754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
18574451Seschrock 
18586643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
18597754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
18607754SJeff.Bonwick@Sun.COM 
18614451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
18627754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
18634451Seschrock 
18644451Seschrock 	/*
18654451Seschrock 	 * If the vdev is already faulted, then don't do anything.
18664451Seschrock 	 */
18677754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded)
18687754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, 0));
18694451Seschrock 
18704451Seschrock 	vd->vdev_degraded = 1ULL;
18714451Seschrock 	if (!vdev_is_dead(vd))
18724451Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
18734451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
18744451Seschrock 
18757754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
18764451Seschrock }
18774451Seschrock 
18784451Seschrock /*
18794451Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
18804451Seschrock  * any attached spare device should be detached when the device finishes
18814451Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
18824451Seschrock  * so no FMA events are generated if the device fails to open.
18834451Seschrock  */
18844451Seschrock int
18857754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1886789Sahrens {
18876643Seschrock 	vdev_t *vd;
1888789Sahrens 
18897754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
18901485Slling 
18916643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
18927754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1893789Sahrens 
18941585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
18957754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
18961585Sbonwick 
1897789Sahrens 	vd->vdev_offline = B_FALSE;
18981485Slling 	vd->vdev_tmpoffline = B_FALSE;
18997754SJeff.Bonwick@Sun.COM 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
19007754SJeff.Bonwick@Sun.COM 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
19011544Seschrock 	vdev_reopen(vd->vdev_top);
19024451Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
19034451Seschrock 
19044451Seschrock 	if (newstate)
19054451Seschrock 		*newstate = vd->vdev_state;
19064451Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
19074451Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
19084451Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
19094451Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
19104451Seschrock 		vd->vdev_unspare = B_TRUE;
1911789Sahrens 
19128241SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
1913789Sahrens }
1914789Sahrens 
1915789Sahrens int
19164451Seschrock vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1917789Sahrens {
19186643Seschrock 	vdev_t *vd;
1919789Sahrens 
19207754SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa);
1921789Sahrens 
19226643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
19237754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
1924789Sahrens 
19251585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
19267754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
19271585Sbonwick 
1928789Sahrens 	/*
19291732Sbonwick 	 * If the device isn't already offline, try to offline it.
1930789Sahrens 	 */
19311732Sbonwick 	if (!vd->vdev_offline) {
19321732Sbonwick 		/*
19338241SJeff.Bonwick@Sun.COM 		 * If this device has the only valid copy of some data,
19348241SJeff.Bonwick@Sun.COM 		 * don't allow it to be offlined.
19351732Sbonwick 		 */
19368241SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && vdev_dtl_required(vd))
19377754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
1938789Sahrens 
19391732Sbonwick 		/*
19401732Sbonwick 		 * Offline this device and reopen its top-level vdev.
19411732Sbonwick 		 * If this action results in the top-level vdev becoming
19421732Sbonwick 		 * unusable, undo it and fail the request.
19431732Sbonwick 		 */
19441732Sbonwick 		vd->vdev_offline = B_TRUE;
19451544Seschrock 		vdev_reopen(vd->vdev_top);
19468241SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && vdev_is_dead(vd->vdev_top)) {
19471732Sbonwick 			vd->vdev_offline = B_FALSE;
19481732Sbonwick 			vdev_reopen(vd->vdev_top);
19497754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
19501732Sbonwick 		}
1951789Sahrens 	}
1952789Sahrens 
19537754SJeff.Bonwick@Sun.COM 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
19541732Sbonwick 
19557754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
1956789Sahrens }
1957789Sahrens 
19581544Seschrock /*
19591544Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
19601544Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
19611544Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
19621544Seschrock  */
19631544Seschrock void
19647754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd)
1965789Sahrens {
19667754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
19677754SJeff.Bonwick@Sun.COM 
19687754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1969789Sahrens 
19701544Seschrock 	if (vd == NULL)
19717754SJeff.Bonwick@Sun.COM 		vd = rvd;
1972789Sahrens 
19731544Seschrock 	vd->vdev_stat.vs_read_errors = 0;
19741544Seschrock 	vd->vdev_stat.vs_write_errors = 0;
19751544Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
1976789Sahrens 
19777754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
19787754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd->vdev_child[c]);
19794451Seschrock 
19804451Seschrock 	/*
19816959Sek110237 	 * If we're in the FAULTED state or have experienced failed I/O, then
19826959Sek110237 	 * clear the persistent state and attempt to reopen the device.  We
19836959Sek110237 	 * also mark the vdev config dirty, so that the new faulted state is
19846959Sek110237 	 * written out to disk.
19854451Seschrock 	 */
19867754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded ||
19877754SJeff.Bonwick@Sun.COM 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
19886959Sek110237 
19894451Seschrock 		vd->vdev_faulted = vd->vdev_degraded = 0;
19907754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read = B_FALSE;
19917754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write = B_FALSE;
19927754SJeff.Bonwick@Sun.COM 
19934451Seschrock 		vdev_reopen(vd);
19944451Seschrock 
19957754SJeff.Bonwick@Sun.COM 		if (vd != rvd)
19967754SJeff.Bonwick@Sun.COM 			vdev_state_dirty(vd->vdev_top);
19977754SJeff.Bonwick@Sun.COM 
19987754SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
19994808Sek110237 			spa_async_request(spa, SPA_ASYNC_RESILVER);
20004451Seschrock 
20014451Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
20024451Seschrock 	}
2003789Sahrens }
2004789Sahrens 
20057754SJeff.Bonwick@Sun.COM boolean_t
20067754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd)
20075329Sgw25295 {
20087754SJeff.Bonwick@Sun.COM 	return (vd->vdev_state < VDEV_STATE_DEGRADED);
20095329Sgw25295 }
20105329Sgw25295 
20117754SJeff.Bonwick@Sun.COM boolean_t
20127754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd)
2013789Sahrens {
20147754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2015789Sahrens }
2016789Sahrens 
20177754SJeff.Bonwick@Sun.COM boolean_t
20187754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd)
2019789Sahrens {
20207754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
20217754SJeff.Bonwick@Sun.COM }
2022789Sahrens 
20237754SJeff.Bonwick@Sun.COM boolean_t
20247980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd)
20257980SGeorge.Wilson@Sun.COM {
20268241SJeff.Bonwick@Sun.COM 	uint64_t state = vd->vdev_state;
20278241SJeff.Bonwick@Sun.COM 
20287980SGeorge.Wilson@Sun.COM 	/*
20298241SJeff.Bonwick@Sun.COM 	 * We currently allow allocations from vdevs which may be in the
20307980SGeorge.Wilson@Sun.COM 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
20317980SGeorge.Wilson@Sun.COM 	 * fails to reopen then we'll catch it later when we're holding
20328241SJeff.Bonwick@Sun.COM 	 * the proper locks.  Note that we have to get the vdev state
20338241SJeff.Bonwick@Sun.COM 	 * in a local variable because although it changes atomically,
20348241SJeff.Bonwick@Sun.COM 	 * we're asking two separate questions about it.
20357980SGeorge.Wilson@Sun.COM 	 */
20368241SJeff.Bonwick@Sun.COM 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
20377980SGeorge.Wilson@Sun.COM 	    !vd->vdev_cant_write);
20387980SGeorge.Wilson@Sun.COM }
20397980SGeorge.Wilson@Sun.COM 
20407980SGeorge.Wilson@Sun.COM boolean_t
20417754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio)
20427754SJeff.Bonwick@Sun.COM {
20437754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_vd == vd);
2044789Sahrens 
20457754SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
20467754SJeff.Bonwick@Sun.COM 		return (B_FALSE);
2047789Sahrens 
20487754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ)
20497754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_read);
2050789Sahrens 
20517754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_WRITE)
20527754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_write);
20537754SJeff.Bonwick@Sun.COM 
20547754SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2055789Sahrens }
2056789Sahrens 
2057789Sahrens /*
2058789Sahrens  * Get statistics for the given vdev.
2059789Sahrens  */
2060789Sahrens void
2061789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2062789Sahrens {
2063789Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2064789Sahrens 
2065789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2066789Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
20677046Sahrens 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
2068789Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2069789Sahrens 	vs->vs_state = vd->vdev_state;
20701175Slling 	vs->vs_rsize = vdev_get_rsize(vd);
2071789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2072789Sahrens 
2073789Sahrens 	/*
2074789Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2075789Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2076789Sahrens 	 */
2077789Sahrens 	if (vd == rvd) {
20787754SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
2079789Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2080789Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2081789Sahrens 
2082789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
20837754SJeff.Bonwick@Sun.COM 			for (int t = 0; t < ZIO_TYPES; t++) {
2084789Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2085789Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2086789Sahrens 			}
2087789Sahrens 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
2088789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
2089789Sahrens 		}
2090789Sahrens 	}
2091789Sahrens }
2092789Sahrens 
2093789Sahrens void
20945450Sbrendan vdev_clear_stats(vdev_t *vd)
20955450Sbrendan {
20965450Sbrendan 	mutex_enter(&vd->vdev_stat_lock);
20975450Sbrendan 	vd->vdev_stat.vs_space = 0;
20985450Sbrendan 	vd->vdev_stat.vs_dspace = 0;
20995450Sbrendan 	vd->vdev_stat.vs_alloc = 0;
21005450Sbrendan 	mutex_exit(&vd->vdev_stat_lock);
21015450Sbrendan }
21025450Sbrendan 
21035450Sbrendan void
21047754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize)
2105789Sahrens {
21068241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
21078241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
21087754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2109789Sahrens 	vdev_t *pvd;
2110789Sahrens 	uint64_t txg = zio->io_txg;
2111789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2112789Sahrens 	zio_type_t type = zio->io_type;
2113789Sahrens 	int flags = zio->io_flags;
2114789Sahrens 
21157754SJeff.Bonwick@Sun.COM 	/*
21167754SJeff.Bonwick@Sun.COM 	 * If this i/o is a gang leader, it didn't do any actual work.
21177754SJeff.Bonwick@Sun.COM 	 */
21187754SJeff.Bonwick@Sun.COM 	if (zio->io_gang_tree)
21197754SJeff.Bonwick@Sun.COM 		return;
21207754SJeff.Bonwick@Sun.COM 
2121789Sahrens 	if (zio->io_error == 0) {
21227754SJeff.Bonwick@Sun.COM 		/*
21237754SJeff.Bonwick@Sun.COM 		 * If this is a root i/o, don't count it -- we've already
21247754SJeff.Bonwick@Sun.COM 		 * counted the top-level vdevs, and vdev_get_stats() will
21257754SJeff.Bonwick@Sun.COM 		 * aggregate them when asked.  This reduces contention on
21267754SJeff.Bonwick@Sun.COM 		 * the root vdev_stat_lock and implicitly handles blocks
21277754SJeff.Bonwick@Sun.COM 		 * that compress away to holes, for which there is no i/o.
21287754SJeff.Bonwick@Sun.COM 		 * (Holes never create vdev children, so all the counters
21297754SJeff.Bonwick@Sun.COM 		 * remain zero, which is what we want.)
21307754SJeff.Bonwick@Sun.COM 		 *
21317754SJeff.Bonwick@Sun.COM 		 * Note: this only applies to successful i/o (io_error == 0)
21327754SJeff.Bonwick@Sun.COM 		 * because unlike i/o counts, errors are not additive.
21337754SJeff.Bonwick@Sun.COM 		 * When reading a ditto block, for example, failure of
21347754SJeff.Bonwick@Sun.COM 		 * one top-level vdev does not imply a root-level error.
21357754SJeff.Bonwick@Sun.COM 		 */
21367754SJeff.Bonwick@Sun.COM 		if (vd == rvd)
21377754SJeff.Bonwick@Sun.COM 			return;
21387754SJeff.Bonwick@Sun.COM 
21397754SJeff.Bonwick@Sun.COM 		ASSERT(vd == zio->io_vd);
21408241SJeff.Bonwick@Sun.COM 
21418241SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_BYPASS)
21428241SJeff.Bonwick@Sun.COM 			return;
21438241SJeff.Bonwick@Sun.COM 
21448241SJeff.Bonwick@Sun.COM 		mutex_enter(&vd->vdev_stat_lock);
21458241SJeff.Bonwick@Sun.COM 
21467754SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_REPAIR) {
21471807Sbonwick 			if (flags & ZIO_FLAG_SCRUB_THREAD)
21487754SJeff.Bonwick@Sun.COM 				vs->vs_scrub_repaired += psize;
21498241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SELF_HEAL)
21507754SJeff.Bonwick@Sun.COM 				vs->vs_self_healed += psize;
2151789Sahrens 		}
21528241SJeff.Bonwick@Sun.COM 
21538241SJeff.Bonwick@Sun.COM 		vs->vs_ops[type]++;
21548241SJeff.Bonwick@Sun.COM 		vs->vs_bytes[type] += psize;
21558241SJeff.Bonwick@Sun.COM 
21568241SJeff.Bonwick@Sun.COM 		mutex_exit(&vd->vdev_stat_lock);
2157789Sahrens 		return;
2158789Sahrens 	}
2159789Sahrens 
2160789Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2161789Sahrens 		return;
2162789Sahrens 
21637754SJeff.Bonwick@Sun.COM 	mutex_enter(&vd->vdev_stat_lock);
21649230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
21657754SJeff.Bonwick@Sun.COM 		if (zio->io_error == ECKSUM)
21667754SJeff.Bonwick@Sun.COM 			vs->vs_checksum_errors++;
21677754SJeff.Bonwick@Sun.COM 		else
21687754SJeff.Bonwick@Sun.COM 			vs->vs_read_errors++;
2169789Sahrens 	}
21709230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
21717754SJeff.Bonwick@Sun.COM 		vs->vs_write_errors++;
21727754SJeff.Bonwick@Sun.COM 	mutex_exit(&vd->vdev_stat_lock);
2173789Sahrens 
21748241SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
21758241SJeff.Bonwick@Sun.COM 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
21768241SJeff.Bonwick@Sun.COM 	    (flags & ZIO_FLAG_SCRUB_THREAD))) {
21778241SJeff.Bonwick@Sun.COM 		/*
21788241SJeff.Bonwick@Sun.COM 		 * This is either a normal write (not a repair), or it's a
21798241SJeff.Bonwick@Sun.COM 		 * repair induced by the scrub thread.  In the normal case,
21808241SJeff.Bonwick@Sun.COM 		 * we commit the DTL change in the same txg as the block
21818241SJeff.Bonwick@Sun.COM 		 * was born.  In the scrub-induced repair case, we know that
21828241SJeff.Bonwick@Sun.COM 		 * scrubs run in first-pass syncing context, so we commit
21838241SJeff.Bonwick@Sun.COM 		 * the DTL change in spa->spa_syncing_txg.
21848241SJeff.Bonwick@Sun.COM 		 *
21858241SJeff.Bonwick@Sun.COM 		 * We currently do not make DTL entries for failed spontaneous
21868241SJeff.Bonwick@Sun.COM 		 * self-healing writes triggered by normal (non-scrubbing)
21878241SJeff.Bonwick@Sun.COM 		 * reads, because we have no transactional context in which to
21888241SJeff.Bonwick@Sun.COM 		 * do so -- and it's not clear that it'd be desirable anyway.
21898241SJeff.Bonwick@Sun.COM 		 */
21908241SJeff.Bonwick@Sun.COM 		if (vd->vdev_ops->vdev_op_leaf) {
21918241SJeff.Bonwick@Sun.COM 			uint64_t commit_txg = txg;
21928241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SCRUB_THREAD) {
21938241SJeff.Bonwick@Sun.COM 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
21948241SJeff.Bonwick@Sun.COM 				ASSERT(spa_sync_pass(spa) == 1);
21958241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
21968241SJeff.Bonwick@Sun.COM 				commit_txg = spa->spa_syncing_txg;
21978241SJeff.Bonwick@Sun.COM 			}
21988241SJeff.Bonwick@Sun.COM 			ASSERT(commit_txg >= spa->spa_syncing_txg);
21998241SJeff.Bonwick@Sun.COM 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
22008241SJeff.Bonwick@Sun.COM 				return;
22018241SJeff.Bonwick@Sun.COM 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
22028241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
22038241SJeff.Bonwick@Sun.COM 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2204789Sahrens 		}
22058241SJeff.Bonwick@Sun.COM 		if (vd != rvd)
22068241SJeff.Bonwick@Sun.COM 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2207789Sahrens 	}
2208789Sahrens }
2209789Sahrens 
2210789Sahrens void
2211789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2212789Sahrens {
2213789Sahrens 	int c;
2214789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2215789Sahrens 
2216789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
2217789Sahrens 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2218789Sahrens 
2219789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2220789Sahrens 
2221789Sahrens 	if (type == POOL_SCRUB_NONE) {
2222789Sahrens 		/*
2223789Sahrens 		 * Update completion and end time.  Leave everything else alone
2224789Sahrens 		 * so we can report what happened during the previous scrub.
2225789Sahrens 		 */
2226789Sahrens 		vs->vs_scrub_complete = complete;
2227789Sahrens 		vs->vs_scrub_end = gethrestime_sec();
2228789Sahrens 	} else {
2229789Sahrens 		vs->vs_scrub_type = type;
2230789Sahrens 		vs->vs_scrub_complete = 0;
2231789Sahrens 		vs->vs_scrub_examined = 0;
2232789Sahrens 		vs->vs_scrub_repaired = 0;
2233789Sahrens 		vs->vs_scrub_start = gethrestime_sec();
2234789Sahrens 		vs->vs_scrub_end = 0;
2235789Sahrens 	}
2236789Sahrens 
2237789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2238789Sahrens }
2239789Sahrens 
2240789Sahrens /*
2241789Sahrens  * Update the in-core space usage stats for this vdev and the root vdev.
2242789Sahrens  */
2243789Sahrens void
22445450Sbrendan vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
22455450Sbrendan     boolean_t update_root)
2246789Sahrens {
22474527Sperrin 	int64_t dspace_delta = space_delta;
22484527Sperrin 	spa_t *spa = vd->vdev_spa;
22494527Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
22504527Sperrin 
2251789Sahrens 	ASSERT(vd == vd->vdev_top);
22524527Sperrin 
22534527Sperrin 	/*
22544527Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
22554527Sperrin 	 * factor.  We must calculate this here and not at the root vdev
22564527Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
22574527Sperrin 	 * childrens', thus not accurate enough for us.
22584527Sperrin 	 */
22594527Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
22604527Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
22614527Sperrin 	    vd->vdev_deflate_ratio;
2262789Sahrens 
22634527Sperrin 	mutex_enter(&vd->vdev_stat_lock);
22644527Sperrin 	vd->vdev_stat.vs_space += space_delta;
22654527Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
22664527Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
22674527Sperrin 	mutex_exit(&vd->vdev_stat_lock);
22682082Seschrock 
22695450Sbrendan 	if (update_root) {
22705450Sbrendan 		ASSERT(rvd == vd->vdev_parent);
22715450Sbrendan 		ASSERT(vd->vdev_ms_count != 0);
22724527Sperrin 
22735450Sbrendan 		/*
22745450Sbrendan 		 * Don't count non-normal (e.g. intent log) space as part of
22755450Sbrendan 		 * the pool's capacity.
22765450Sbrendan 		 */
22775450Sbrendan 		if (vd->vdev_mg->mg_class != spa->spa_normal_class)
22785450Sbrendan 			return;
22795450Sbrendan 
22805450Sbrendan 		mutex_enter(&rvd->vdev_stat_lock);
22815450Sbrendan 		rvd->vdev_stat.vs_space += space_delta;
22825450Sbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
22835450Sbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
22845450Sbrendan 		mutex_exit(&rvd->vdev_stat_lock);
22855450Sbrendan 	}
2286789Sahrens }
2287789Sahrens 
2288789Sahrens /*
2289789Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2290789Sahrens  * so that it will be written out next time the vdev configuration is synced.
2291789Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2292789Sahrens  */
2293789Sahrens void
2294789Sahrens vdev_config_dirty(vdev_t *vd)
2295789Sahrens {
2296789Sahrens 	spa_t *spa = vd->vdev_spa;
2297789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2298789Sahrens 	int c;
2299789Sahrens 
23001601Sbonwick 	/*
23019425SEric.Schrock@Sun.COM 	 * If this is an aux vdev (as with l2cache and spare devices), then we
23029425SEric.Schrock@Sun.COM 	 * update the vdev config manually and set the sync flag.
23036643Seschrock 	 */
23046643Seschrock 	if (vd->vdev_aux != NULL) {
23056643Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
23066643Seschrock 		nvlist_t **aux;
23076643Seschrock 		uint_t naux;
23086643Seschrock 
23096643Seschrock 		for (c = 0; c < sav->sav_count; c++) {
23106643Seschrock 			if (sav->sav_vdevs[c] == vd)
23116643Seschrock 				break;
23126643Seschrock 		}
23136643Seschrock 
23147754SJeff.Bonwick@Sun.COM 		if (c == sav->sav_count) {
23157754SJeff.Bonwick@Sun.COM 			/*
23167754SJeff.Bonwick@Sun.COM 			 * We're being removed.  There's nothing more to do.
23177754SJeff.Bonwick@Sun.COM 			 */
23187754SJeff.Bonwick@Sun.COM 			ASSERT(sav->sav_sync == B_TRUE);
23197754SJeff.Bonwick@Sun.COM 			return;
23207754SJeff.Bonwick@Sun.COM 		}
23217754SJeff.Bonwick@Sun.COM 
23226643Seschrock 		sav->sav_sync = B_TRUE;
23236643Seschrock 
23249425SEric.Schrock@Sun.COM 		if (nvlist_lookup_nvlist_array(sav->sav_config,
23259425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
23269425SEric.Schrock@Sun.COM 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
23279425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
23289425SEric.Schrock@Sun.COM 		}
23296643Seschrock 
23306643Seschrock 		ASSERT(c < naux);
23316643Seschrock 
23326643Seschrock 		/*
23336643Seschrock 		 * Setting the nvlist in the middle if the array is a little
23346643Seschrock 		 * sketchy, but it will work.
23356643Seschrock 		 */
23366643Seschrock 		nvlist_free(aux[c]);
23376643Seschrock 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
23386643Seschrock 
23396643Seschrock 		return;
23406643Seschrock 	}
23416643Seschrock 
23426643Seschrock 	/*
23437754SJeff.Bonwick@Sun.COM 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
23447754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
23457754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
23461601Sbonwick 	 * so this is sufficient to ensure mutual exclusion.
23471601Sbonwick 	 */
23487754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
23497754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
23507754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
23511601Sbonwick 
2352789Sahrens 	if (vd == rvd) {
2353789Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2354789Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2355789Sahrens 	} else {
2356789Sahrens 		ASSERT(vd == vd->vdev_top);
2357789Sahrens 
23587754SJeff.Bonwick@Sun.COM 		if (!list_link_active(&vd->vdev_config_dirty_node))
23597754SJeff.Bonwick@Sun.COM 			list_insert_head(&spa->spa_config_dirty_list, vd);
2360789Sahrens 	}
2361789Sahrens }
2362789Sahrens 
2363789Sahrens void
2364789Sahrens vdev_config_clean(vdev_t *vd)
2365789Sahrens {
23661601Sbonwick 	spa_t *spa = vd->vdev_spa;
23671601Sbonwick 
23687754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
23697754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
23707754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
23717754SJeff.Bonwick@Sun.COM 
23727754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
23737754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_config_dirty_list, vd);
23747754SJeff.Bonwick@Sun.COM }
23757754SJeff.Bonwick@Sun.COM 
23767754SJeff.Bonwick@Sun.COM /*
23777754SJeff.Bonwick@Sun.COM  * Mark a top-level vdev's state as dirty, so that the next pass of
23787754SJeff.Bonwick@Sun.COM  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
23797754SJeff.Bonwick@Sun.COM  * the state changes from larger config changes because they require
23807754SJeff.Bonwick@Sun.COM  * much less locking, and are often needed for administrative actions.
23817754SJeff.Bonwick@Sun.COM  */
23827754SJeff.Bonwick@Sun.COM void
23837754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd)
23847754SJeff.Bonwick@Sun.COM {
23857754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
23867754SJeff.Bonwick@Sun.COM 
23877754SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
23881601Sbonwick 
23897754SJeff.Bonwick@Sun.COM 	/*
23907754SJeff.Bonwick@Sun.COM 	 * The state list is protected by the SCL_STATE lock.  The caller
23917754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_STATE as writer, or must be the sync thread
23927754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
23937754SJeff.Bonwick@Sun.COM 	 * so this is sufficient to ensure mutual exclusion.
23947754SJeff.Bonwick@Sun.COM 	 */
23957754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
23967754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
23977754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
23987754SJeff.Bonwick@Sun.COM 
23997754SJeff.Bonwick@Sun.COM 	if (!list_link_active(&vd->vdev_state_dirty_node))
24007754SJeff.Bonwick@Sun.COM 		list_insert_head(&spa->spa_state_dirty_list, vd);
24017754SJeff.Bonwick@Sun.COM }
24027754SJeff.Bonwick@Sun.COM 
24037754SJeff.Bonwick@Sun.COM void
24047754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd)
24057754SJeff.Bonwick@Sun.COM {
24067754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
24077754SJeff.Bonwick@Sun.COM 
24087754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
24097754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
24107754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
24117754SJeff.Bonwick@Sun.COM 
24127754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
24137754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_state_dirty_list, vd);
2414789Sahrens }
2415789Sahrens 
24166523Sek110237 /*
24176523Sek110237  * Propagate vdev state up from children to parent.
24186523Sek110237  */
24191775Sbillm void
24201775Sbillm vdev_propagate_state(vdev_t *vd)
24211775Sbillm {
24228241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
24238241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
24241775Sbillm 	int degraded = 0, faulted = 0;
24251775Sbillm 	int corrupted = 0;
24261775Sbillm 	int c;
24271775Sbillm 	vdev_t *child;
24281775Sbillm 
24294451Seschrock 	if (vd->vdev_children > 0) {
24304451Seschrock 		for (c = 0; c < vd->vdev_children; c++) {
24314451Seschrock 			child = vd->vdev_child[c];
24326976Seschrock 
24337754SJeff.Bonwick@Sun.COM 			if (!vdev_readable(child) ||
24348241SJeff.Bonwick@Sun.COM 			    (!vdev_writeable(child) && spa_writeable(spa))) {
24356976Seschrock 				/*
24366976Seschrock 				 * Root special: if there is a top-level log
24376976Seschrock 				 * device, treat the root vdev as if it were
24386976Seschrock 				 * degraded.
24396976Seschrock 				 */
24406976Seschrock 				if (child->vdev_islog && vd == rvd)
24416976Seschrock 					degraded++;
24426976Seschrock 				else
24436976Seschrock 					faulted++;
24446976Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
24454451Seschrock 				degraded++;
24466976Seschrock 			}
24474451Seschrock 
24484451Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
24494451Seschrock 				corrupted++;
24504451Seschrock 		}
24511775Sbillm 
24524451Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
24534451Seschrock 
24544451Seschrock 		/*
24557754SJeff.Bonwick@Sun.COM 		 * Root special: if there is a top-level vdev that cannot be
24564451Seschrock 		 * opened due to corrupted metadata, then propagate the root
24574451Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
24584451Seschrock 		 * replicas'.
24594451Seschrock 		 */
24604451Seschrock 		if (corrupted && vd == rvd &&
24614451Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
24624451Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
24634451Seschrock 			    VDEV_AUX_CORRUPT_DATA);
24641775Sbillm 	}
24651775Sbillm 
24666976Seschrock 	if (vd->vdev_parent)
24674451Seschrock 		vdev_propagate_state(vd->vdev_parent);
24681775Sbillm }
24691775Sbillm 
2470789Sahrens /*
24711544Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
24721544Seschrock  * state, because we're in the process of opening children depth-first.
24731544Seschrock  * Otherwise, we propagate the change to the parent.
24741544Seschrock  *
24751544Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
24761544Seschrock  * generated.
2477789Sahrens  */
2478789Sahrens void
24791544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2480789Sahrens {
24811986Seschrock 	uint64_t save_state;
24826643Seschrock 	spa_t *spa = vd->vdev_spa;
24831544Seschrock 
24841544Seschrock 	if (state == vd->vdev_state) {
24851544Seschrock 		vd->vdev_stat.vs_aux = aux;
2486789Sahrens 		return;
24871544Seschrock 	}
24881544Seschrock 
24891986Seschrock 	save_state = vd->vdev_state;
2490789Sahrens 
2491789Sahrens 	vd->vdev_state = state;
2492789Sahrens 	vd->vdev_stat.vs_aux = aux;
2493789Sahrens 
24944451Seschrock 	/*
24954451Seschrock 	 * If we are setting the vdev state to anything but an open state, then
24964451Seschrock 	 * always close the underlying device.  Otherwise, we keep accessible
24974451Seschrock 	 * but invalid devices open forever.  We don't call vdev_close() itself,
24984451Seschrock 	 * because that implies some extra checks (offline, etc) that we don't
24994451Seschrock 	 * want here.  This is limited to leaf devices, because otherwise
25004451Seschrock 	 * closing the device will affect other children.
25014451Seschrock 	 */
25027780SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
25034451Seschrock 		vd->vdev_ops->vdev_op_close(vd);
25044451Seschrock 
25054451Seschrock 	if (vd->vdev_removed &&
25064451Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
25074451Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
25084451Seschrock 		/*
25094451Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
25104451Seschrock 		 * device was previously marked removed and someone attempted to
25114451Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
25124451Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
25134451Seschrock 		 * it is one of our special test online cases, which is only
25144451Seschrock 		 * attempting to online the device and shouldn't generate an FMA
25154451Seschrock 		 * fault.
25164451Seschrock 		 */
25174451Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
25184451Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
25194451Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
25204451Seschrock 		/*
25214451Seschrock 		 * Indicate to the ZFS DE that this device has been removed, and
25224451Seschrock 		 * any recent errors should be ignored.
25234451Seschrock 		 */
25246643Seschrock 		zfs_post_remove(spa, vd);
25254451Seschrock 		vd->vdev_removed = B_TRUE;
25264451Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
25271544Seschrock 		/*
25281544Seschrock 		 * If we fail to open a vdev during an import, we mark it as
25291544Seschrock 		 * "not available", which signifies that it was never there to
25301544Seschrock 		 * begin with.  Failure to open such a device is not considered
25311544Seschrock 		 * an error.
25321544Seschrock 		 */
25336643Seschrock 		if (spa->spa_load_state == SPA_LOAD_IMPORT &&
25341986Seschrock 		    vd->vdev_ops->vdev_op_leaf)
25351986Seschrock 			vd->vdev_not_present = 1;
25361986Seschrock 
25371986Seschrock 		/*
25381986Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
25391986Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
25401986Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
25411986Seschrock 		 * want to post the ereport if the device was already in the
25421986Seschrock 		 * CANT_OPEN state beforehand.
25434451Seschrock 		 *
25444451Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
25454451Seschrock 		 * online the device in response to an insertion event.  If we
25464451Seschrock 		 * hit this case, then we have detected an insertion event for a
25474451Seschrock 		 * faulted or offline device that wasn't in the removed state.
25484451Seschrock 		 * In this scenario, we don't post an ereport because we are
25494451Seschrock 		 * about to replace the device, or attempt an online with
25504451Seschrock 		 * vdev_forcefault, which will generate the fault for us.
25511986Seschrock 		 */
25524451Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
25534451Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
25546643Seschrock 		    vd != spa->spa_root_vdev) {
25551544Seschrock 			const char *class;
25561544Seschrock 
25571544Seschrock 			switch (aux) {
25581544Seschrock 			case VDEV_AUX_OPEN_FAILED:
25591544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
25601544Seschrock 				break;
25611544Seschrock 			case VDEV_AUX_CORRUPT_DATA:
25621544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
25631544Seschrock 				break;
25641544Seschrock 			case VDEV_AUX_NO_REPLICAS:
25651544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
25661544Seschrock 				break;
25671544Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
25681544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
25691544Seschrock 				break;
25701544Seschrock 			case VDEV_AUX_TOO_SMALL:
25711544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
25721544Seschrock 				break;
25731544Seschrock 			case VDEV_AUX_BAD_LABEL:
25741544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
25751544Seschrock 				break;
25767754SJeff.Bonwick@Sun.COM 			case VDEV_AUX_IO_FAILURE:
25777754SJeff.Bonwick@Sun.COM 				class = FM_EREPORT_ZFS_IO_FAILURE;
25787754SJeff.Bonwick@Sun.COM 				break;
25791544Seschrock 			default:
25801544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
25811544Seschrock 			}
25821544Seschrock 
25836643Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
25841544Seschrock 		}
25854451Seschrock 
25864451Seschrock 		/* Erase any notion of persistent removed state */
25874451Seschrock 		vd->vdev_removed = B_FALSE;
25884451Seschrock 	} else {
25894451Seschrock 		vd->vdev_removed = B_FALSE;
25901544Seschrock 	}
25911544Seschrock 
2592*9583STim.Haley@Sun.COM 	if (!isopen && vd->vdev_parent)
2593*9583STim.Haley@Sun.COM 		vdev_propagate_state(vd->vdev_parent);
2594789Sahrens }
25957042Sgw25295 
25967042Sgw25295 /*
25977042Sgw25295  * Check the vdev configuration to ensure that it's capable of supporting
25987042Sgw25295  * a root pool. Currently, we do not support RAID-Z or partial configuration.
25997042Sgw25295  * In addition, only a single top-level vdev is allowed and none of the leaves
26007042Sgw25295  * can be wholedisks.
26017042Sgw25295  */
26027042Sgw25295 boolean_t
26037042Sgw25295 vdev_is_bootable(vdev_t *vd)
26047042Sgw25295 {
26057042Sgw25295 	int c;
26067042Sgw25295 
26077042Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf) {
26087042Sgw25295 		char *vdev_type = vd->vdev_ops->vdev_op_type;
26097042Sgw25295 
26107042Sgw25295 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
26117042Sgw25295 		    vd->vdev_children > 1) {
26127042Sgw25295 			return (B_FALSE);
26137042Sgw25295 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
26147042Sgw25295 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
26157042Sgw25295 			return (B_FALSE);
26167042Sgw25295 		}
26177042Sgw25295 	} else if (vd->vdev_wholedisk == 1) {
26187042Sgw25295 		return (B_FALSE);
26197042Sgw25295 	}
26207042Sgw25295 
26217042Sgw25295 	for (c = 0; c < vd->vdev_children; c++) {
26227042Sgw25295 		if (!vdev_is_bootable(vd->vdev_child[c]))
26237042Sgw25295 			return (B_FALSE);
26247042Sgw25295 	}
26257042Sgw25295 	return (B_TRUE);
26267042Sgw25295 }
2627