xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 12247)
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 /*
23*12247SGeorge.Wilson@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/zfs_context.h>
271544Seschrock #include <sys/fm/fs/zfs.h>
28789Sahrens #include <sys/spa.h>
29789Sahrens #include <sys/spa_impl.h>
30789Sahrens #include <sys/dmu.h>
31789Sahrens #include <sys/dmu_tx.h>
32789Sahrens #include <sys/vdev_impl.h>
33789Sahrens #include <sys/uberblock_impl.h>
34789Sahrens #include <sys/metaslab.h>
35789Sahrens #include <sys/metaslab_impl.h>
36789Sahrens #include <sys/space_map.h>
37789Sahrens #include <sys/zio.h>
38789Sahrens #include <sys/zap.h>
39789Sahrens #include <sys/fs/zfs.h>
406643Seschrock #include <sys/arc.h>
419701SGeorge.Wilson@Sun.COM #include <sys/zil.h>
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,
5610594SGeorge.Wilson@Sun.COM 	&vdev_hole_ops,
57789Sahrens 	NULL
58789Sahrens };
59789Sahrens 
607046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
617046Sahrens int zfs_scrub_limit = 10;
623697Smishra 
63789Sahrens /*
64789Sahrens  * Given a vdev type, return the appropriate ops vector.
65789Sahrens  */
66789Sahrens static vdev_ops_t *
67789Sahrens vdev_getops(const char *type)
68789Sahrens {
69789Sahrens 	vdev_ops_t *ops, **opspp;
70789Sahrens 
71789Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
72789Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
73789Sahrens 			break;
74789Sahrens 
75789Sahrens 	return (ops);
76789Sahrens }
77789Sahrens 
78789Sahrens /*
79789Sahrens  * Default asize function: return the MAX of psize with the asize of
80789Sahrens  * all children.  This is what's used by anything other than RAID-Z.
81789Sahrens  */
82789Sahrens uint64_t
83789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
84789Sahrens {
851732Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
86789Sahrens 	uint64_t csize;
879816SGeorge.Wilson@Sun.COM 
889816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
89789Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
90789Sahrens 		asize = MAX(asize, csize);
91789Sahrens 	}
92789Sahrens 
93789Sahrens 	return (asize);
94789Sahrens }
95789Sahrens 
961175Slling /*
979816SGeorge.Wilson@Sun.COM  * Get the minimum allocatable size. We define the allocatable size as
989816SGeorge.Wilson@Sun.COM  * the vdev's asize rounded to the nearest metaslab. This allows us to
999816SGeorge.Wilson@Sun.COM  * replace or attach devices which don't have the same physical size but
1009816SGeorge.Wilson@Sun.COM  * can still satisfy the same number of allocations.
1011175Slling  */
1021175Slling uint64_t
1039816SGeorge.Wilson@Sun.COM vdev_get_min_asize(vdev_t *vd)
1041175Slling {
1059816SGeorge.Wilson@Sun.COM 	vdev_t *pvd = vd->vdev_parent;
1069816SGeorge.Wilson@Sun.COM 
1079816SGeorge.Wilson@Sun.COM 	/*
1089816SGeorge.Wilson@Sun.COM 	 * The our parent is NULL (inactive spare or cache) or is the root,
1099816SGeorge.Wilson@Sun.COM 	 * just return our own asize.
1109816SGeorge.Wilson@Sun.COM 	 */
1119816SGeorge.Wilson@Sun.COM 	if (pvd == NULL)
1129816SGeorge.Wilson@Sun.COM 		return (vd->vdev_asize);
1131175Slling 
1141175Slling 	/*
1159816SGeorge.Wilson@Sun.COM 	 * The top-level vdev just returns the allocatable size rounded
1169816SGeorge.Wilson@Sun.COM 	 * to the nearest metaslab.
1179816SGeorge.Wilson@Sun.COM 	 */
1189816SGeorge.Wilson@Sun.COM 	if (vd == vd->vdev_top)
1199816SGeorge.Wilson@Sun.COM 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
1209816SGeorge.Wilson@Sun.COM 
1219816SGeorge.Wilson@Sun.COM 	/*
1229816SGeorge.Wilson@Sun.COM 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
1239816SGeorge.Wilson@Sun.COM 	 * so each child must provide at least 1/Nth of its asize.
1241175Slling 	 */
1259816SGeorge.Wilson@Sun.COM 	if (pvd->vdev_ops == &vdev_raidz_ops)
1269816SGeorge.Wilson@Sun.COM 		return (pvd->vdev_min_asize / pvd->vdev_children);
1279816SGeorge.Wilson@Sun.COM 
1289816SGeorge.Wilson@Sun.COM 	return (pvd->vdev_min_asize);
1299816SGeorge.Wilson@Sun.COM }
1309816SGeorge.Wilson@Sun.COM 
1319816SGeorge.Wilson@Sun.COM void
1329816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vdev_t *vd)
1339816SGeorge.Wilson@Sun.COM {
1349816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1359816SGeorge.Wilson@Sun.COM 
1369816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
1379816SGeorge.Wilson@Sun.COM 		vdev_set_min_asize(vd->vdev_child[c]);
1381175Slling }
1391175Slling 
140789Sahrens vdev_t *
141789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
142789Sahrens {
143789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
144789Sahrens 
1457754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1465530Sbonwick 
1477046Sahrens 	if (vdev < rvd->vdev_children) {
1487046Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
149789Sahrens 		return (rvd->vdev_child[vdev]);
1507046Sahrens 	}
151789Sahrens 
152789Sahrens 	return (NULL);
153789Sahrens }
154789Sahrens 
155789Sahrens vdev_t *
156789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
157789Sahrens {
158789Sahrens 	vdev_t *mvd;
159789Sahrens 
1601585Sbonwick 	if (vd->vdev_guid == guid)
161789Sahrens 		return (vd);
162789Sahrens 
1639816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
164789Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
165789Sahrens 		    NULL)
166789Sahrens 			return (mvd);
167789Sahrens 
168789Sahrens 	return (NULL);
169789Sahrens }
170789Sahrens 
171789Sahrens void
172789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
173789Sahrens {
174789Sahrens 	size_t oldsize, newsize;
175789Sahrens 	uint64_t id = cvd->vdev_id;
176789Sahrens 	vdev_t **newchild;
177789Sahrens 
1787754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
179789Sahrens 	ASSERT(cvd->vdev_parent == NULL);
180789Sahrens 
181789Sahrens 	cvd->vdev_parent = pvd;
182789Sahrens 
183789Sahrens 	if (pvd == NULL)
184789Sahrens 		return;
185789Sahrens 
186789Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
187789Sahrens 
188789Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
189789Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
190789Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
191789Sahrens 
192789Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
193789Sahrens 	if (pvd->vdev_child != NULL) {
194789Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
195789Sahrens 		kmem_free(pvd->vdev_child, oldsize);
196789Sahrens 	}
197789Sahrens 
198789Sahrens 	pvd->vdev_child = newchild;
199789Sahrens 	pvd->vdev_child[id] = cvd;
200789Sahrens 
201789Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
202789Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
203789Sahrens 
204789Sahrens 	/*
205789Sahrens 	 * Walk up all ancestors to update guid sum.
206789Sahrens 	 */
207789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
208789Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
2093697Smishra 
2103697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2113697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
212789Sahrens }
213789Sahrens 
214789Sahrens void
215789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
216789Sahrens {
217789Sahrens 	int c;
218789Sahrens 	uint_t id = cvd->vdev_id;
219789Sahrens 
220789Sahrens 	ASSERT(cvd->vdev_parent == pvd);
221789Sahrens 
222789Sahrens 	if (pvd == NULL)
223789Sahrens 		return;
224789Sahrens 
225789Sahrens 	ASSERT(id < pvd->vdev_children);
226789Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
227789Sahrens 
228789Sahrens 	pvd->vdev_child[id] = NULL;
229789Sahrens 	cvd->vdev_parent = NULL;
230789Sahrens 
231789Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
232789Sahrens 		if (pvd->vdev_child[c])
233789Sahrens 			break;
234789Sahrens 
235789Sahrens 	if (c == pvd->vdev_children) {
236789Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
237789Sahrens 		pvd->vdev_child = NULL;
238789Sahrens 		pvd->vdev_children = 0;
239789Sahrens 	}
240789Sahrens 
241789Sahrens 	/*
242789Sahrens 	 * Walk up all ancestors to update guid sum.
243789Sahrens 	 */
244789Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
245789Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
2463697Smishra 
2473697Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
2483697Smishra 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
249789Sahrens }
250789Sahrens 
251789Sahrens /*
252789Sahrens  * Remove any holes in the child array.
253789Sahrens  */
254789Sahrens void
255789Sahrens vdev_compact_children(vdev_t *pvd)
256789Sahrens {
257789Sahrens 	vdev_t **newchild, *cvd;
258789Sahrens 	int oldc = pvd->vdev_children;
2599816SGeorge.Wilson@Sun.COM 	int newc;
260789Sahrens 
2617754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
262789Sahrens 
2639816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++)
264789Sahrens 		if (pvd->vdev_child[c])
265789Sahrens 			newc++;
266789Sahrens 
267789Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
268789Sahrens 
2699816SGeorge.Wilson@Sun.COM 	for (int c = newc = 0; c < oldc; c++) {
270789Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
271789Sahrens 			newchild[newc] = cvd;
272789Sahrens 			cvd->vdev_id = newc++;
273789Sahrens 		}
274789Sahrens 	}
275789Sahrens 
276789Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
277789Sahrens 	pvd->vdev_child = newchild;
278789Sahrens 	pvd->vdev_children = newc;
279789Sahrens }
280789Sahrens 
281789Sahrens /*
282789Sahrens  * Allocate and minimally initialize a vdev_t.
283789Sahrens  */
28410594SGeorge.Wilson@Sun.COM vdev_t *
285789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
286789Sahrens {
287789Sahrens 	vdev_t *vd;
288789Sahrens 
2891585Sbonwick 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
2901585Sbonwick 
2911585Sbonwick 	if (spa->spa_root_vdev == NULL) {
2921585Sbonwick 		ASSERT(ops == &vdev_root_ops);
2931585Sbonwick 		spa->spa_root_vdev = vd;
2941585Sbonwick 	}
295789Sahrens 
29610594SGeorge.Wilson@Sun.COM 	if (guid == 0 && ops != &vdev_hole_ops) {
2971585Sbonwick 		if (spa->spa_root_vdev == vd) {
2981585Sbonwick 			/*
2991585Sbonwick 			 * The root vdev's guid will also be the pool guid,
3001585Sbonwick 			 * which must be unique among all pools.
3011585Sbonwick 			 */
30211422SMark.Musante@Sun.COM 			guid = spa_generate_guid(NULL);
3031585Sbonwick 		} else {
3041585Sbonwick 			/*
3051585Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3061585Sbonwick 			 */
30711422SMark.Musante@Sun.COM 			guid = spa_generate_guid(spa);
3081585Sbonwick 		}
3091585Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3101585Sbonwick 	}
311789Sahrens 
312789Sahrens 	vd->vdev_spa = spa;
313789Sahrens 	vd->vdev_id = id;
314789Sahrens 	vd->vdev_guid = guid;
315789Sahrens 	vd->vdev_guid_sum = guid;
316789Sahrens 	vd->vdev_ops = ops;
317789Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
31810594SGeorge.Wilson@Sun.COM 	vd->vdev_ishole = (ops == &vdev_hole_ops);
319789Sahrens 
320789Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3212856Snd150628 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
3227754SJeff.Bonwick@Sun.COM 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3238241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
3248241SJeff.Bonwick@Sun.COM 		space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
3258241SJeff.Bonwick@Sun.COM 		    &vd->vdev_dtl_lock);
3268241SJeff.Bonwick@Sun.COM 	}
327789Sahrens 	txg_list_create(&vd->vdev_ms_list,
328789Sahrens 	    offsetof(struct metaslab, ms_txg_node));
329789Sahrens 	txg_list_create(&vd->vdev_dtl_list,
330789Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
331789Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3324451Seschrock 	vdev_queue_init(vd);
3334451Seschrock 	vdev_cache_init(vd);
334789Sahrens 
335789Sahrens 	return (vd);
336789Sahrens }
337789Sahrens 
338789Sahrens /*
339789Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
340789Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
341789Sahrens  * different for each case.
342789Sahrens  */
3432082Seschrock int
3442082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
3452082Seschrock     int alloctype)
346789Sahrens {
347789Sahrens 	vdev_ops_t *ops;
348789Sahrens 	char *type;
3494527Sperrin 	uint64_t guid = 0, islog, nparity;
350789Sahrens 	vdev_t *vd;
351789Sahrens 
3527754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
353789Sahrens 
354789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
3552082Seschrock 		return (EINVAL);
356789Sahrens 
357789Sahrens 	if ((ops = vdev_getops(type)) == NULL)
3582082Seschrock 		return (EINVAL);
359789Sahrens 
360789Sahrens 	/*
361789Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
362789Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
363789Sahrens 	 */
364789Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
365789Sahrens 		uint64_t label_id;
366789Sahrens 
367789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
368789Sahrens 		    label_id != id)
3692082Seschrock 			return (EINVAL);
370789Sahrens 
371789Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3722082Seschrock 			return (EINVAL);
3732082Seschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
3742082Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3752082Seschrock 			return (EINVAL);
3765450Sbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
3775450Sbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3785450Sbrendan 			return (EINVAL);
3799790SLin.Ling@Sun.COM 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
3809790SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
3819790SLin.Ling@Sun.COM 			return (EINVAL);
382789Sahrens 	}
383789Sahrens 
3842082Seschrock 	/*
3852082Seschrock 	 * The first allocated vdev must be of type 'root'.
3862082Seschrock 	 */
3872082Seschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
3882082Seschrock 		return (EINVAL);
3892082Seschrock 
3904527Sperrin 	/*
3914527Sperrin 	 * Determine whether we're a log vdev.
3924527Sperrin 	 */
3934527Sperrin 	islog = 0;
3944527Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
3955094Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
3964527Sperrin 		return (ENOTSUP);
3974527Sperrin 
39810594SGeorge.Wilson@Sun.COM 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
39910594SGeorge.Wilson@Sun.COM 		return (ENOTSUP);
40010594SGeorge.Wilson@Sun.COM 
4014527Sperrin 	/*
4024527Sperrin 	 * Set the nparity property for RAID-Z vdevs.
4034527Sperrin 	 */
4044527Sperrin 	nparity = -1ULL;
4054527Sperrin 	if (ops == &vdev_raidz_ops) {
4064527Sperrin 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4074527Sperrin 		    &nparity) == 0) {
40810922SJeff.Bonwick@Sun.COM 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
4094527Sperrin 				return (EINVAL);
4104527Sperrin 			/*
41110105Sadam.leventhal@sun.com 			 * Previous versions could only support 1 or 2 parity
41210105Sadam.leventhal@sun.com 			 * device.
4134527Sperrin 			 */
41410105Sadam.leventhal@sun.com 			if (nparity > 1 &&
41510105Sadam.leventhal@sun.com 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
41610105Sadam.leventhal@sun.com 				return (ENOTSUP);
41710105Sadam.leventhal@sun.com 			if (nparity > 2 &&
41810105Sadam.leventhal@sun.com 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
4194527Sperrin 				return (ENOTSUP);
4204527Sperrin 		} else {
4214527Sperrin 			/*
4224527Sperrin 			 * We require the parity to be specified for SPAs that
4234527Sperrin 			 * support multiple parity levels.
4244527Sperrin 			 */
42510105Sadam.leventhal@sun.com 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
4264527Sperrin 				return (EINVAL);
4274527Sperrin 			/*
4284527Sperrin 			 * Otherwise, we default to 1 parity device for RAID-Z.
4294527Sperrin 			 */
4304527Sperrin 			nparity = 1;
4314527Sperrin 		}
4324527Sperrin 	} else {
4334527Sperrin 		nparity = 0;
4344527Sperrin 	}
4354527Sperrin 	ASSERT(nparity != -1ULL);
4364527Sperrin 
437789Sahrens 	vd = vdev_alloc_common(spa, id, guid, ops);
438789Sahrens 
4394527Sperrin 	vd->vdev_islog = islog;
4404527Sperrin 	vd->vdev_nparity = nparity;
4414527Sperrin 
442789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
443789Sahrens 		vd->vdev_path = spa_strdup(vd->vdev_path);
444789Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
445789Sahrens 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4464451Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4474451Seschrock 	    &vd->vdev_physpath) == 0)
4484451Seschrock 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4499425SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4509425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
451789Sahrens 
452789Sahrens 	/*
4531171Seschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
4541171Seschrock 	 * as -1.
4551171Seschrock 	 */
4561171Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
4571171Seschrock 	    &vd->vdev_wholedisk) != 0)
4581171Seschrock 		vd->vdev_wholedisk = -1ULL;
4591171Seschrock 
4601171Seschrock 	/*
4611544Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
4621544Seschrock 	 * was not present at the time of import.
4631544Seschrock 	 */
4649425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4659425SEric.Schrock@Sun.COM 	    &vd->vdev_not_present);
4661544Seschrock 
4671544Seschrock 	/*
4681732Sbonwick 	 * Get the alignment requirement.
4691732Sbonwick 	 */
4701732Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
4711732Sbonwick 
4721732Sbonwick 	/*
47310594SGeorge.Wilson@Sun.COM 	 * Retrieve the vdev creation time.
47410594SGeorge.Wilson@Sun.COM 	 */
47510594SGeorge.Wilson@Sun.COM 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
47610594SGeorge.Wilson@Sun.COM 	    &vd->vdev_crtxg);
47710594SGeorge.Wilson@Sun.COM 
47810594SGeorge.Wilson@Sun.COM 	/*
479789Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
480789Sahrens 	 */
48111422SMark.Musante@Sun.COM 	if (parent && !parent->vdev_parent &&
48211422SMark.Musante@Sun.COM 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
483789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
484789Sahrens 		    &vd->vdev_ms_array);
485789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
486789Sahrens 		    &vd->vdev_ms_shift);
487789Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
488789Sahrens 		    &vd->vdev_asize);
489789Sahrens 	}
490789Sahrens 
49110974SJeff.Bonwick@Sun.COM 	if (parent && !parent->vdev_parent) {
49210974SJeff.Bonwick@Sun.COM 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
49310982SGeorge.Wilson@Sun.COM 		    alloctype == VDEV_ALLOC_ADD ||
49411422SMark.Musante@Sun.COM 		    alloctype == VDEV_ALLOC_SPLIT ||
49510982SGeorge.Wilson@Sun.COM 		    alloctype == VDEV_ALLOC_ROOTPOOL);
49610974SJeff.Bonwick@Sun.COM 		vd->vdev_mg = metaslab_group_create(islog ?
49710974SJeff.Bonwick@Sun.COM 		    spa_log_class(spa) : spa_normal_class(spa), vd);
49810974SJeff.Bonwick@Sun.COM 	}
49910974SJeff.Bonwick@Sun.COM 
500789Sahrens 	/*
5014451Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
502789Sahrens 	 */
5036643Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
5049790SLin.Ling@Sun.COM 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
5059790SLin.Ling@Sun.COM 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
5066643Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
5076643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
5088241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl_smo.smo_object);
5096643Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
5106643Seschrock 			    &vd->vdev_unspare);
5116643Seschrock 		}
5129790SLin.Ling@Sun.COM 
5139790SLin.Ling@Sun.COM 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
5149790SLin.Ling@Sun.COM 			uint64_t spare = 0;
5159790SLin.Ling@Sun.COM 
5169790SLin.Ling@Sun.COM 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
5179790SLin.Ling@Sun.COM 			    &spare) == 0 && spare)
5189790SLin.Ling@Sun.COM 				spa_spare_add(vd);
5199790SLin.Ling@Sun.COM 		}
5209790SLin.Ling@Sun.COM 
5211732Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
5221732Sbonwick 		    &vd->vdev_offline);
5236643Seschrock 
5244451Seschrock 		/*
5254451Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5264451Seschrock 		 * state, as the diagnosis made on another system may not be
52710817SEric.Schrock@Sun.COM 		 * valid in the current context.  Local vdevs will
52810817SEric.Schrock@Sun.COM 		 * remain in the faulted state.
5294451Seschrock 		 */
53011147SGeorge.Wilson@Sun.COM 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
5314451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5324451Seschrock 			    &vd->vdev_faulted);
5334451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5344451Seschrock 			    &vd->vdev_degraded);
5354451Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5364451Seschrock 			    &vd->vdev_removed);
53710817SEric.Schrock@Sun.COM 
53810817SEric.Schrock@Sun.COM 			if (vd->vdev_faulted || vd->vdev_degraded) {
53910817SEric.Schrock@Sun.COM 				char *aux;
54010817SEric.Schrock@Sun.COM 
54110817SEric.Schrock@Sun.COM 				vd->vdev_label_aux =
54210817SEric.Schrock@Sun.COM 				    VDEV_AUX_ERR_EXCEEDED;
54310817SEric.Schrock@Sun.COM 				if (nvlist_lookup_string(nv,
54410817SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
54510817SEric.Schrock@Sun.COM 				    strcmp(aux, "external") == 0)
54610817SEric.Schrock@Sun.COM 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
54710817SEric.Schrock@Sun.COM 			}
5484451Seschrock 		}
549789Sahrens 	}
550789Sahrens 
551789Sahrens 	/*
552789Sahrens 	 * Add ourselves to the parent's list of children.
553789Sahrens 	 */
554789Sahrens 	vdev_add_child(parent, vd);
555789Sahrens 
5562082Seschrock 	*vdp = vd;
5572082Seschrock 
5582082Seschrock 	return (0);
559789Sahrens }
560789Sahrens 
561789Sahrens void
562789Sahrens vdev_free(vdev_t *vd)
563789Sahrens {
5644451Seschrock 	spa_t *spa = vd->vdev_spa;
565789Sahrens 
566789Sahrens 	/*
567789Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
568789Sahrens 	 * trying to ensure complicated semantics for all callers.
569789Sahrens 	 */
570789Sahrens 	vdev_close(vd);
571789Sahrens 
5727754SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
57310922SJeff.Bonwick@Sun.COM 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
574789Sahrens 
575789Sahrens 	/*
576789Sahrens 	 * Free all children.
577789Sahrens 	 */
5789816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
579789Sahrens 		vdev_free(vd->vdev_child[c]);
580789Sahrens 
581789Sahrens 	ASSERT(vd->vdev_child == NULL);
582789Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
583789Sahrens 
584789Sahrens 	/*
585789Sahrens 	 * Discard allocation state.
586789Sahrens 	 */
58710974SJeff.Bonwick@Sun.COM 	if (vd->vdev_mg != NULL) {
588789Sahrens 		vdev_metaslab_fini(vd);
58910974SJeff.Bonwick@Sun.COM 		metaslab_group_destroy(vd->vdev_mg);
59010974SJeff.Bonwick@Sun.COM 	}
591789Sahrens 
592789Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
5932082Seschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
594789Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
595789Sahrens 
596789Sahrens 	/*
597789Sahrens 	 * Remove this vdev from its parent's child list.
598789Sahrens 	 */
599789Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
600789Sahrens 
601789Sahrens 	ASSERT(vd->vdev_parent == NULL);
602789Sahrens 
6034451Seschrock 	/*
6044451Seschrock 	 * Clean up vdev structure.
6054451Seschrock 	 */
6064451Seschrock 	vdev_queue_fini(vd);
6074451Seschrock 	vdev_cache_fini(vd);
6084451Seschrock 
6094451Seschrock 	if (vd->vdev_path)
6104451Seschrock 		spa_strfree(vd->vdev_path);
6114451Seschrock 	if (vd->vdev_devid)
6124451Seschrock 		spa_strfree(vd->vdev_devid);
6134451Seschrock 	if (vd->vdev_physpath)
6144451Seschrock 		spa_strfree(vd->vdev_physpath);
6159425SEric.Schrock@Sun.COM 	if (vd->vdev_fru)
6169425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_fru);
6174451Seschrock 
6184451Seschrock 	if (vd->vdev_isspare)
6194451Seschrock 		spa_spare_remove(vd);
6205450Sbrendan 	if (vd->vdev_isl2cache)
6215450Sbrendan 		spa_l2cache_remove(vd);
6224451Seschrock 
6234451Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
6244451Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
6258241SJeff.Bonwick@Sun.COM 
6264451Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
6278241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
6288241SJeff.Bonwick@Sun.COM 		space_map_unload(&vd->vdev_dtl[t]);
6298241SJeff.Bonwick@Sun.COM 		space_map_destroy(&vd->vdev_dtl[t]);
6308241SJeff.Bonwick@Sun.COM 	}
6314451Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
6328241SJeff.Bonwick@Sun.COM 
6334451Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6344451Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
6357754SJeff.Bonwick@Sun.COM 	mutex_destroy(&vd->vdev_probe_lock);
6364451Seschrock 
6374451Seschrock 	if (vd == spa->spa_root_vdev)
6384451Seschrock 		spa->spa_root_vdev = NULL;
6394451Seschrock 
6404451Seschrock 	kmem_free(vd, sizeof (vdev_t));
641789Sahrens }
642789Sahrens 
643789Sahrens /*
644789Sahrens  * Transfer top-level vdev state from svd to tvd.
645789Sahrens  */
646789Sahrens static void
647789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
648789Sahrens {
649789Sahrens 	spa_t *spa = svd->vdev_spa;
650789Sahrens 	metaslab_t *msp;
651789Sahrens 	vdev_t *vd;
652789Sahrens 	int t;
653789Sahrens 
654789Sahrens 	ASSERT(tvd == tvd->vdev_top);
655789Sahrens 
656789Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
657789Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
658789Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
659789Sahrens 
660789Sahrens 	svd->vdev_ms_array = 0;
661789Sahrens 	svd->vdev_ms_shift = 0;
662789Sahrens 	svd->vdev_ms_count = 0;
663789Sahrens 
664789Sahrens 	tvd->vdev_mg = svd->vdev_mg;
665789Sahrens 	tvd->vdev_ms = svd->vdev_ms;
666789Sahrens 
667789Sahrens 	svd->vdev_mg = NULL;
668789Sahrens 	svd->vdev_ms = NULL;
6691732Sbonwick 
6701732Sbonwick 	if (tvd->vdev_mg != NULL)
6711732Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
672789Sahrens 
673789Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
674789Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
6752082Seschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
676789Sahrens 
677789Sahrens 	svd->vdev_stat.vs_alloc = 0;
678789Sahrens 	svd->vdev_stat.vs_space = 0;
6792082Seschrock 	svd->vdev_stat.vs_dspace = 0;
680789Sahrens 
681789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
682789Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
683789Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
684789Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
685789Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
686789Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
687789Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
688789Sahrens 	}
689789Sahrens 
6907754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_config_dirty_node)) {
691789Sahrens 		vdev_config_clean(svd);
692789Sahrens 		vdev_config_dirty(tvd);
693789Sahrens 	}
694789Sahrens 
6957754SJeff.Bonwick@Sun.COM 	if (list_link_active(&svd->vdev_state_dirty_node)) {
6967754SJeff.Bonwick@Sun.COM 		vdev_state_clean(svd);
6977754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(tvd);
6987754SJeff.Bonwick@Sun.COM 	}
6997754SJeff.Bonwick@Sun.COM 
7002082Seschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
7012082Seschrock 	svd->vdev_deflate_ratio = 0;
7024527Sperrin 
7034527Sperrin 	tvd->vdev_islog = svd->vdev_islog;
7044527Sperrin 	svd->vdev_islog = 0;
705789Sahrens }
706789Sahrens 
707789Sahrens static void
708789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
709789Sahrens {
710789Sahrens 	if (vd == NULL)
711789Sahrens 		return;
712789Sahrens 
713789Sahrens 	vd->vdev_top = tvd;
714789Sahrens 
7159816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
716789Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
717789Sahrens }
718789Sahrens 
719789Sahrens /*
720789Sahrens  * Add a mirror/replacing vdev above an existing vdev.
721789Sahrens  */
722789Sahrens vdev_t *
723789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
724789Sahrens {
725789Sahrens 	spa_t *spa = cvd->vdev_spa;
726789Sahrens 	vdev_t *pvd = cvd->vdev_parent;
727789Sahrens 	vdev_t *mvd;
728789Sahrens 
7297754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
730789Sahrens 
731789Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
7321732Sbonwick 
7331732Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
7349816SGeorge.Wilson@Sun.COM 	mvd->vdev_min_asize = cvd->vdev_min_asize;
7351732Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
7361732Sbonwick 	mvd->vdev_state = cvd->vdev_state;
73710594SGeorge.Wilson@Sun.COM 	mvd->vdev_crtxg = cvd->vdev_crtxg;
7381732Sbonwick 
739789Sahrens 	vdev_remove_child(pvd, cvd);
740789Sahrens 	vdev_add_child(pvd, mvd);
741789Sahrens 	cvd->vdev_id = mvd->vdev_children;
742789Sahrens 	vdev_add_child(mvd, cvd);
743789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
744789Sahrens 
745789Sahrens 	if (mvd == mvd->vdev_top)
746789Sahrens 		vdev_top_transfer(cvd, mvd);
747789Sahrens 
748789Sahrens 	return (mvd);
749789Sahrens }
750789Sahrens 
751789Sahrens /*
752789Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
753789Sahrens  */
754789Sahrens void
755789Sahrens vdev_remove_parent(vdev_t *cvd)
756789Sahrens {
757789Sahrens 	vdev_t *mvd = cvd->vdev_parent;
758789Sahrens 	vdev_t *pvd = mvd->vdev_parent;
759789Sahrens 
7607754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
761789Sahrens 
762789Sahrens 	ASSERT(mvd->vdev_children == 1);
763789Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
7642082Seschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
7652082Seschrock 	    mvd->vdev_ops == &vdev_spare_ops);
7661732Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
767789Sahrens 
768789Sahrens 	vdev_remove_child(mvd, cvd);
769789Sahrens 	vdev_remove_child(pvd, mvd);
7708241SJeff.Bonwick@Sun.COM 
7717754SJeff.Bonwick@Sun.COM 	/*
7727754SJeff.Bonwick@Sun.COM 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
7737754SJeff.Bonwick@Sun.COM 	 * Otherwise, we could have detached an offline device, and when we
7747754SJeff.Bonwick@Sun.COM 	 * go to import the pool we'll think we have two top-level vdevs,
7757754SJeff.Bonwick@Sun.COM 	 * instead of a different version of the same top-level vdev.
7767754SJeff.Bonwick@Sun.COM 	 */
7778241SJeff.Bonwick@Sun.COM 	if (mvd->vdev_top == mvd) {
7788241SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
77911422SMark.Musante@Sun.COM 		cvd->vdev_orig_guid = cvd->vdev_guid;
7808241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid += guid_delta;
7818241SJeff.Bonwick@Sun.COM 		cvd->vdev_guid_sum += guid_delta;
7828241SJeff.Bonwick@Sun.COM 	}
783789Sahrens 	cvd->vdev_id = mvd->vdev_id;
784789Sahrens 	vdev_add_child(pvd, cvd);
785789Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
786789Sahrens 
787789Sahrens 	if (cvd == cvd->vdev_top)
788789Sahrens 		vdev_top_transfer(mvd, cvd);
789789Sahrens 
790789Sahrens 	ASSERT(mvd->vdev_children == 0);
791789Sahrens 	vdev_free(mvd);
792789Sahrens }
793789Sahrens 
7941544Seschrock int
795789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
796789Sahrens {
797789Sahrens 	spa_t *spa = vd->vdev_spa;
7981732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
7991732Sbonwick 	uint64_t m;
800789Sahrens 	uint64_t oldc = vd->vdev_ms_count;
801789Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
8021732Sbonwick 	metaslab_t **mspp;
8031732Sbonwick 	int error;
804789Sahrens 
80510974SJeff.Bonwick@Sun.COM 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
80610974SJeff.Bonwick@Sun.COM 
80710594SGeorge.Wilson@Sun.COM 	/*
80810594SGeorge.Wilson@Sun.COM 	 * This vdev is not being allocated from yet or is a hole.
80910594SGeorge.Wilson@Sun.COM 	 */
81010594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms_shift == 0)
8111585Sbonwick 		return (0);
8121585Sbonwick 
81310594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
81410594SGeorge.Wilson@Sun.COM 
8159701SGeorge.Wilson@Sun.COM 	/*
8169701SGeorge.Wilson@Sun.COM 	 * Compute the raidz-deflation ratio.  Note, we hard-code
8179701SGeorge.Wilson@Sun.COM 	 * in 128k (1 << 17) because it is the current "typical" blocksize.
8189701SGeorge.Wilson@Sun.COM 	 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
8199701SGeorge.Wilson@Sun.COM 	 * or we will inconsistently account for existing bp's.
8209701SGeorge.Wilson@Sun.COM 	 */
8219701SGeorge.Wilson@Sun.COM 	vd->vdev_deflate_ratio = (1 << 17) /
8229701SGeorge.Wilson@Sun.COM 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
8239701SGeorge.Wilson@Sun.COM 
824789Sahrens 	ASSERT(oldc <= newc);
825789Sahrens 
8261732Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
8271732Sbonwick 
8281732Sbonwick 	if (oldc != 0) {
8291732Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
8301732Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
8311732Sbonwick 	}
8321732Sbonwick 
8331732Sbonwick 	vd->vdev_ms = mspp;
834789Sahrens 	vd->vdev_ms_count = newc;
835789Sahrens 
8361732Sbonwick 	for (m = oldc; m < newc; m++) {
8371732Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
838789Sahrens 		if (txg == 0) {
8391732Sbonwick 			uint64_t object = 0;
8401732Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8419512SNeil.Perrin@Sun.COM 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8429512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
8431732Sbonwick 			if (error)
8441732Sbonwick 				return (error);
8451732Sbonwick 			if (object != 0) {
8461732Sbonwick 				dmu_buf_t *db;
8471732Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
8481732Sbonwick 				if (error)
8491732Sbonwick 					return (error);
8504944Smaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
8514944Smaybee 				bcopy(db->db_data, &smo, sizeof (smo));
8521732Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
8531544Seschrock 				dmu_buf_rele(db, FTAG);
854789Sahrens 			}
855789Sahrens 		}
8561732Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
8571732Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
858789Sahrens 	}
859789Sahrens 
86010974SJeff.Bonwick@Sun.COM 	if (txg == 0)
86110974SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
86210974SJeff.Bonwick@Sun.COM 
86310974SJeff.Bonwick@Sun.COM 	if (oldc == 0)
86410974SJeff.Bonwick@Sun.COM 		metaslab_group_activate(vd->vdev_mg);
86510974SJeff.Bonwick@Sun.COM 
86610974SJeff.Bonwick@Sun.COM 	if (txg == 0)
86710974SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALLOC, FTAG);
86810974SJeff.Bonwick@Sun.COM 
8691544Seschrock 	return (0);
870789Sahrens }
871789Sahrens 
872789Sahrens void
873789Sahrens vdev_metaslab_fini(vdev_t *vd)
874789Sahrens {
875789Sahrens 	uint64_t m;
876789Sahrens 	uint64_t count = vd->vdev_ms_count;
877789Sahrens 
878789Sahrens 	if (vd->vdev_ms != NULL) {
87910974SJeff.Bonwick@Sun.COM 		metaslab_group_passivate(vd->vdev_mg);
880789Sahrens 		for (m = 0; m < count; m++)
8811732Sbonwick 			if (vd->vdev_ms[m] != NULL)
8821732Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
883789Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
884789Sahrens 		vd->vdev_ms = NULL;
885789Sahrens 	}
886789Sahrens }
887789Sahrens 
8887754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats {
8897754SJeff.Bonwick@Sun.COM 	boolean_t	vps_readable;
8907754SJeff.Bonwick@Sun.COM 	boolean_t	vps_writeable;
8917754SJeff.Bonwick@Sun.COM 	int		vps_flags;
8927754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t;
8937754SJeff.Bonwick@Sun.COM 
8947754SJeff.Bonwick@Sun.COM static void
8957754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio)
8965329Sgw25295 {
8978241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
8988632SBill.Moore@Sun.COM 	vdev_t *vd = zio->io_vd;
8997754SJeff.Bonwick@Sun.COM 	vdev_probe_stats_t *vps = zio->io_private;
9008632SBill.Moore@Sun.COM 
9018632SBill.Moore@Sun.COM 	ASSERT(vd->vdev_probe_zio != NULL);
9027754SJeff.Bonwick@Sun.COM 
9037754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ) {
9047754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
9057754SJeff.Bonwick@Sun.COM 			vps->vps_readable = 1;
9068241SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0 && spa_writeable(spa)) {
9078632SBill.Moore@Sun.COM 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
9087754SJeff.Bonwick@Sun.COM 			    zio->io_offset, zio->io_size, zio->io_data,
9097754SJeff.Bonwick@Sun.COM 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
9107754SJeff.Bonwick@Sun.COM 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
9117754SJeff.Bonwick@Sun.COM 		} else {
9127754SJeff.Bonwick@Sun.COM 			zio_buf_free(zio->io_data, zio->io_size);
9137754SJeff.Bonwick@Sun.COM 		}
9147754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
9157754SJeff.Bonwick@Sun.COM 		if (zio->io_error == 0)
9167754SJeff.Bonwick@Sun.COM 			vps->vps_writeable = 1;
9177754SJeff.Bonwick@Sun.COM 		zio_buf_free(zio->io_data, zio->io_size);
9187754SJeff.Bonwick@Sun.COM 	} else if (zio->io_type == ZIO_TYPE_NULL) {
9198632SBill.Moore@Sun.COM 		zio_t *pio;
9207754SJeff.Bonwick@Sun.COM 
9217754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read |= !vps->vps_readable;
9227754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write |= !vps->vps_writeable;
9237754SJeff.Bonwick@Sun.COM 
9247754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) &&
9258241SJeff.Bonwick@Sun.COM 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
9267754SJeff.Bonwick@Sun.COM 			zio->io_error = 0;
9277754SJeff.Bonwick@Sun.COM 		} else {
9287754SJeff.Bonwick@Sun.COM 			ASSERT(zio->io_error != 0);
9297754SJeff.Bonwick@Sun.COM 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
9308241SJeff.Bonwick@Sun.COM 			    spa, vd, NULL, 0, 0);
9317754SJeff.Bonwick@Sun.COM 			zio->io_error = ENXIO;
9327754SJeff.Bonwick@Sun.COM 		}
9338632SBill.Moore@Sun.COM 
9348632SBill.Moore@Sun.COM 		mutex_enter(&vd->vdev_probe_lock);
9358632SBill.Moore@Sun.COM 		ASSERT(vd->vdev_probe_zio == zio);
9368632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = NULL;
9378632SBill.Moore@Sun.COM 		mutex_exit(&vd->vdev_probe_lock);
9388632SBill.Moore@Sun.COM 
9398632SBill.Moore@Sun.COM 		while ((pio = zio_walk_parents(zio)) != NULL)
9408632SBill.Moore@Sun.COM 			if (!vdev_accessible(vd, pio))
9418632SBill.Moore@Sun.COM 				pio->io_error = ENXIO;
9428632SBill.Moore@Sun.COM 
9437754SJeff.Bonwick@Sun.COM 		kmem_free(vps, sizeof (*vps));
9447754SJeff.Bonwick@Sun.COM 	}
9457754SJeff.Bonwick@Sun.COM }
9465329Sgw25295 
9477754SJeff.Bonwick@Sun.COM /*
9487754SJeff.Bonwick@Sun.COM  * Determine whether this device is accessible by reading and writing
9497754SJeff.Bonwick@Sun.COM  * to several known locations: the pad regions of each vdev label
9507754SJeff.Bonwick@Sun.COM  * but the first (which we leave alone in case it contains a VTOC).
9517754SJeff.Bonwick@Sun.COM  */
9527754SJeff.Bonwick@Sun.COM zio_t *
9538632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio)
9547754SJeff.Bonwick@Sun.COM {
9557754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
9568632SBill.Moore@Sun.COM 	vdev_probe_stats_t *vps = NULL;
9578632SBill.Moore@Sun.COM 	zio_t *pio;
9587754SJeff.Bonwick@Sun.COM 
9597754SJeff.Bonwick@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9607754SJeff.Bonwick@Sun.COM 
9618632SBill.Moore@Sun.COM 	/*
9628632SBill.Moore@Sun.COM 	 * Don't probe the probe.
9638632SBill.Moore@Sun.COM 	 */
9648632SBill.Moore@Sun.COM 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
9658632SBill.Moore@Sun.COM 		return (NULL);
9668632SBill.Moore@Sun.COM 
9678632SBill.Moore@Sun.COM 	/*
9688632SBill.Moore@Sun.COM 	 * To prevent 'probe storms' when a device fails, we create
9698632SBill.Moore@Sun.COM 	 * just one probe i/o at a time.  All zios that want to probe
9708632SBill.Moore@Sun.COM 	 * this vdev will become parents of the probe io.
9718632SBill.Moore@Sun.COM 	 */
9728632SBill.Moore@Sun.COM 	mutex_enter(&vd->vdev_probe_lock);
9738632SBill.Moore@Sun.COM 
9748632SBill.Moore@Sun.COM 	if ((pio = vd->vdev_probe_zio) == NULL) {
9758632SBill.Moore@Sun.COM 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
9768632SBill.Moore@Sun.COM 
9778632SBill.Moore@Sun.COM 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
9788632SBill.Moore@Sun.COM 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9799725SEric.Schrock@Sun.COM 		    ZIO_FLAG_TRYHARD;
9808632SBill.Moore@Sun.COM 
9818632SBill.Moore@Sun.COM 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
9828632SBill.Moore@Sun.COM 			/*
9838632SBill.Moore@Sun.COM 			 * vdev_cant_read and vdev_cant_write can only
9848632SBill.Moore@Sun.COM 			 * transition from TRUE to FALSE when we have the
9858632SBill.Moore@Sun.COM 			 * SCL_ZIO lock as writer; otherwise they can only
9868632SBill.Moore@Sun.COM 			 * transition from FALSE to TRUE.  This ensures that
9878632SBill.Moore@Sun.COM 			 * any zio looking at these values can assume that
9888632SBill.Moore@Sun.COM 			 * failures persist for the life of the I/O.  That's
9898632SBill.Moore@Sun.COM 			 * important because when a device has intermittent
9908632SBill.Moore@Sun.COM 			 * connectivity problems, we want to ensure that
9918632SBill.Moore@Sun.COM 			 * they're ascribed to the device (ENXIO) and not
9928632SBill.Moore@Sun.COM 			 * the zio (EIO).
9938632SBill.Moore@Sun.COM 			 *
9948632SBill.Moore@Sun.COM 			 * Since we hold SCL_ZIO as writer here, clear both
9958632SBill.Moore@Sun.COM 			 * values so the probe can reevaluate from first
9968632SBill.Moore@Sun.COM 			 * principles.
9978632SBill.Moore@Sun.COM 			 */
9988632SBill.Moore@Sun.COM 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
9998632SBill.Moore@Sun.COM 			vd->vdev_cant_read = B_FALSE;
10008632SBill.Moore@Sun.COM 			vd->vdev_cant_write = B_FALSE;
10018632SBill.Moore@Sun.COM 		}
10028632SBill.Moore@Sun.COM 
10038632SBill.Moore@Sun.COM 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
10048632SBill.Moore@Sun.COM 		    vdev_probe_done, vps,
10058632SBill.Moore@Sun.COM 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
10068632SBill.Moore@Sun.COM 
1007*12247SGeorge.Wilson@Sun.COM 		/*
1008*12247SGeorge.Wilson@Sun.COM 		 * We can't change the vdev state in this context, so we
1009*12247SGeorge.Wilson@Sun.COM 		 * kick off an async task to do it on our behalf.
1010*12247SGeorge.Wilson@Sun.COM 		 */
10118632SBill.Moore@Sun.COM 		if (zio != NULL) {
10128632SBill.Moore@Sun.COM 			vd->vdev_probe_wanted = B_TRUE;
10138632SBill.Moore@Sun.COM 			spa_async_request(spa, SPA_ASYNC_PROBE);
10148632SBill.Moore@Sun.COM 		}
10158632SBill.Moore@Sun.COM 	}
10168632SBill.Moore@Sun.COM 
10178632SBill.Moore@Sun.COM 	if (zio != NULL)
10188632SBill.Moore@Sun.COM 		zio_add_child(zio, pio);
10198632SBill.Moore@Sun.COM 
10208632SBill.Moore@Sun.COM 	mutex_exit(&vd->vdev_probe_lock);
10218632SBill.Moore@Sun.COM 
10228632SBill.Moore@Sun.COM 	if (vps == NULL) {
10238632SBill.Moore@Sun.COM 		ASSERT(zio != NULL);
10248632SBill.Moore@Sun.COM 		return (NULL);
10258632SBill.Moore@Sun.COM 	}
10267754SJeff.Bonwick@Sun.COM 
10277754SJeff.Bonwick@Sun.COM 	for (int l = 1; l < VDEV_LABELS; l++) {
10288632SBill.Moore@Sun.COM 		zio_nowait(zio_read_phys(pio, vd,
10297754SJeff.Bonwick@Sun.COM 		    vdev_label_offset(vd->vdev_psize, l,
10309056SLin.Ling@Sun.COM 		    offsetof(vdev_label_t, vl_pad2)),
10319056SLin.Ling@Sun.COM 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
10327754SJeff.Bonwick@Sun.COM 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
10337754SJeff.Bonwick@Sun.COM 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
10347754SJeff.Bonwick@Sun.COM 	}
10357754SJeff.Bonwick@Sun.COM 
10368632SBill.Moore@Sun.COM 	if (zio == NULL)
10378632SBill.Moore@Sun.COM 		return (pio);
10388632SBill.Moore@Sun.COM 
10398632SBill.Moore@Sun.COM 	zio_nowait(pio);
10408632SBill.Moore@Sun.COM 	return (NULL);
10415329Sgw25295 }
10425329Sgw25295 
10439846SEric.Taylor@Sun.COM static void
10449846SEric.Taylor@Sun.COM vdev_open_child(void *arg)
10459846SEric.Taylor@Sun.COM {
10469846SEric.Taylor@Sun.COM 	vdev_t *vd = arg;
10479846SEric.Taylor@Sun.COM 
10489846SEric.Taylor@Sun.COM 	vd->vdev_open_thread = curthread;
10499846SEric.Taylor@Sun.COM 	vd->vdev_open_error = vdev_open(vd);
10509846SEric.Taylor@Sun.COM 	vd->vdev_open_thread = NULL;
10519846SEric.Taylor@Sun.COM }
10529846SEric.Taylor@Sun.COM 
105310588SEric.Taylor@Sun.COM boolean_t
105410588SEric.Taylor@Sun.COM vdev_uses_zvols(vdev_t *vd)
105510588SEric.Taylor@Sun.COM {
105610588SEric.Taylor@Sun.COM 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
105710588SEric.Taylor@Sun.COM 	    strlen(ZVOL_DIR)) == 0)
105810588SEric.Taylor@Sun.COM 		return (B_TRUE);
105910588SEric.Taylor@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
106010588SEric.Taylor@Sun.COM 		if (vdev_uses_zvols(vd->vdev_child[c]))
106110588SEric.Taylor@Sun.COM 			return (B_TRUE);
106210588SEric.Taylor@Sun.COM 	return (B_FALSE);
106310588SEric.Taylor@Sun.COM }
106410588SEric.Taylor@Sun.COM 
10659846SEric.Taylor@Sun.COM void
10669846SEric.Taylor@Sun.COM vdev_open_children(vdev_t *vd)
10679846SEric.Taylor@Sun.COM {
10689846SEric.Taylor@Sun.COM 	taskq_t *tq;
10699846SEric.Taylor@Sun.COM 	int children = vd->vdev_children;
10709846SEric.Taylor@Sun.COM 
107110588SEric.Taylor@Sun.COM 	/*
107210588SEric.Taylor@Sun.COM 	 * in order to handle pools on top of zvols, do the opens
107310588SEric.Taylor@Sun.COM 	 * in a single thread so that the same thread holds the
107410588SEric.Taylor@Sun.COM 	 * spa_namespace_lock
107510588SEric.Taylor@Sun.COM 	 */
107610588SEric.Taylor@Sun.COM 	if (vdev_uses_zvols(vd)) {
107710588SEric.Taylor@Sun.COM 		for (int c = 0; c < children; c++)
107810588SEric.Taylor@Sun.COM 			vd->vdev_child[c]->vdev_open_error =
107910588SEric.Taylor@Sun.COM 			    vdev_open(vd->vdev_child[c]);
108010588SEric.Taylor@Sun.COM 		return;
108110588SEric.Taylor@Sun.COM 	}
10829846SEric.Taylor@Sun.COM 	tq = taskq_create("vdev_open", children, minclsyspri,
10839846SEric.Taylor@Sun.COM 	    children, children, TASKQ_PREPOPULATE);
10849846SEric.Taylor@Sun.COM 
10859846SEric.Taylor@Sun.COM 	for (int c = 0; c < children; c++)
10869846SEric.Taylor@Sun.COM 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
10879846SEric.Taylor@Sun.COM 		    TQ_SLEEP) != NULL);
10889846SEric.Taylor@Sun.COM 
10899846SEric.Taylor@Sun.COM 	taskq_destroy(tq);
10909846SEric.Taylor@Sun.COM }
10919846SEric.Taylor@Sun.COM 
1092789Sahrens /*
1093789Sahrens  * Prepare a virtual device for access.
1094789Sahrens  */
1095789Sahrens int
1096789Sahrens vdev_open(vdev_t *vd)
1097789Sahrens {
10988241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
1099789Sahrens 	int error;
1100789Sahrens 	uint64_t osize = 0;
1101789Sahrens 	uint64_t asize, psize;
11021732Sbonwick 	uint64_t ashift = 0;
1103789Sahrens 
11049846SEric.Taylor@Sun.COM 	ASSERT(vd->vdev_open_thread == curthread ||
11059846SEric.Taylor@Sun.COM 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1106789Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1107789Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1108789Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1109789Sahrens 
1110789Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
11119701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_read = B_FALSE;
11129701SGeorge.Wilson@Sun.COM 	vd->vdev_cant_write = B_FALSE;
11139816SGeorge.Wilson@Sun.COM 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1114789Sahrens 
111510817SEric.Schrock@Sun.COM 	/*
111610817SEric.Schrock@Sun.COM 	 * If this vdev is not removed, check its fault status.  If it's
111710817SEric.Schrock@Sun.COM 	 * faulted, bail out of the open.
111810817SEric.Schrock@Sun.COM 	 */
11194451Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
11204451Seschrock 		ASSERT(vd->vdev_children == 0);
112110817SEric.Schrock@Sun.COM 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
112210817SEric.Schrock@Sun.COM 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
11234451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
112410817SEric.Schrock@Sun.COM 		    vd->vdev_label_aux);
11254451Seschrock 		return (ENXIO);
11264451Seschrock 	} else if (vd->vdev_offline) {
1127789Sahrens 		ASSERT(vd->vdev_children == 0);
11281544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1129789Sahrens 		return (ENXIO);
1130789Sahrens 	}
1131789Sahrens 
1132789Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1133789Sahrens 
113410850SGeorge.Wilson@Sun.COM 	/*
113510850SGeorge.Wilson@Sun.COM 	 * Reset the vdev_reopening flag so that we actually close
113610850SGeorge.Wilson@Sun.COM 	 * the vdev on error.
113710850SGeorge.Wilson@Sun.COM 	 */
113810850SGeorge.Wilson@Sun.COM 	vd->vdev_reopening = B_FALSE;
11391544Seschrock 	if (zio_injection_enabled && error == 0)
11409725SEric.Schrock@Sun.COM 		error = zio_handle_device_injection(vd, NULL, ENXIO);
11411544Seschrock 
11424451Seschrock 	if (error) {
11434451Seschrock 		if (vd->vdev_removed &&
11444451Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
11454451Seschrock 			vd->vdev_removed = B_FALSE;
1146789Sahrens 
11471544Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1148789Sahrens 		    vd->vdev_stat.vs_aux);
1149789Sahrens 		return (error);
1150789Sahrens 	}
1151789Sahrens 
11524451Seschrock 	vd->vdev_removed = B_FALSE;
11534451Seschrock 
115410830SEric.Schrock@Sun.COM 	/*
115510830SEric.Schrock@Sun.COM 	 * Recheck the faulted flag now that we have confirmed that
115610830SEric.Schrock@Sun.COM 	 * the vdev is accessible.  If we're faulted, bail.
115710830SEric.Schrock@Sun.COM 	 */
115810830SEric.Schrock@Sun.COM 	if (vd->vdev_faulted) {
115910830SEric.Schrock@Sun.COM 		ASSERT(vd->vdev_children == 0);
116010830SEric.Schrock@Sun.COM 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
116110830SEric.Schrock@Sun.COM 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
116210830SEric.Schrock@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
116310830SEric.Schrock@Sun.COM 		    vd->vdev_label_aux);
116410830SEric.Schrock@Sun.COM 		return (ENXIO);
116510830SEric.Schrock@Sun.COM 	}
116610830SEric.Schrock@Sun.COM 
11674451Seschrock 	if (vd->vdev_degraded) {
11684451Seschrock 		ASSERT(vd->vdev_children == 0);
11694451Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11704451Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
11714451Seschrock 	} else {
117210817SEric.Schrock@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
11734451Seschrock 	}
1174789Sahrens 
117510594SGeorge.Wilson@Sun.COM 	/*
117610594SGeorge.Wilson@Sun.COM 	 * For hole or missing vdevs we just return success.
117710594SGeorge.Wilson@Sun.COM 	 */
117810594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
117910594SGeorge.Wilson@Sun.COM 		return (0);
118010594SGeorge.Wilson@Sun.COM 
11819816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
11821544Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
11831544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11841544Seschrock 			    VDEV_AUX_NONE);
11851544Seschrock 			break;
11861544Seschrock 		}
11879816SGeorge.Wilson@Sun.COM 	}
1188789Sahrens 
1189789Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1190789Sahrens 
1191789Sahrens 	if (vd->vdev_children == 0) {
1192789Sahrens 		if (osize < SPA_MINDEVSIZE) {
11931544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
11941544Seschrock 			    VDEV_AUX_TOO_SMALL);
1195789Sahrens 			return (EOVERFLOW);
1196789Sahrens 		}
1197789Sahrens 		psize = osize;
1198789Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1199789Sahrens 	} else {
12001732Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1201789Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
12021544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12031544Seschrock 			    VDEV_AUX_TOO_SMALL);
1204789Sahrens 			return (EOVERFLOW);
1205789Sahrens 		}
1206789Sahrens 		psize = 0;
1207789Sahrens 		asize = osize;
1208789Sahrens 	}
1209789Sahrens 
1210789Sahrens 	vd->vdev_psize = psize;
1211789Sahrens 
12129816SGeorge.Wilson@Sun.COM 	/*
12139816SGeorge.Wilson@Sun.COM 	 * Make sure the allocatable size hasn't shrunk.
12149816SGeorge.Wilson@Sun.COM 	 */
12159816SGeorge.Wilson@Sun.COM 	if (asize < vd->vdev_min_asize) {
12169816SGeorge.Wilson@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12179816SGeorge.Wilson@Sun.COM 		    VDEV_AUX_BAD_LABEL);
12189816SGeorge.Wilson@Sun.COM 		return (EINVAL);
12199816SGeorge.Wilson@Sun.COM 	}
12209816SGeorge.Wilson@Sun.COM 
1221789Sahrens 	if (vd->vdev_asize == 0) {
1222789Sahrens 		/*
1223789Sahrens 		 * This is the first-ever open, so use the computed values.
12241732Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1225789Sahrens 		 */
1226789Sahrens 		vd->vdev_asize = asize;
12271732Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1228789Sahrens 	} else {
1229789Sahrens 		/*
1230789Sahrens 		 * Make sure the alignment requirement hasn't increased.
1231789Sahrens 		 */
12321732Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
12331544Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
12341544Seschrock 			    VDEV_AUX_BAD_LABEL);
1235789Sahrens 			return (EINVAL);
1236789Sahrens 		}
1237789Sahrens 	}
1238789Sahrens 
12391544Seschrock 	/*
12409816SGeorge.Wilson@Sun.COM 	 * If all children are healthy and the asize has increased,
12419816SGeorge.Wilson@Sun.COM 	 * then we've experienced dynamic LUN growth.  If automatic
12429816SGeorge.Wilson@Sun.COM 	 * expansion is enabled then use the additional space.
12439816SGeorge.Wilson@Sun.COM 	 */
12449816SGeorge.Wilson@Sun.COM 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
12459816SGeorge.Wilson@Sun.COM 	    (vd->vdev_expanding || spa->spa_autoexpand))
12469816SGeorge.Wilson@Sun.COM 		vd->vdev_asize = asize;
12479816SGeorge.Wilson@Sun.COM 
12489816SGeorge.Wilson@Sun.COM 	vdev_set_min_asize(vd);
12499816SGeorge.Wilson@Sun.COM 
12509816SGeorge.Wilson@Sun.COM 	/*
12515329Sgw25295 	 * Ensure we can issue some IO before declaring the
12525329Sgw25295 	 * vdev open for business.
12535329Sgw25295 	 */
12547754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf &&
12557754SJeff.Bonwick@Sun.COM 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1256*12247SGeorge.Wilson@Sun.COM 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1257*12247SGeorge.Wilson@Sun.COM 		    VDEV_AUX_ERR_EXCEEDED);
12585329Sgw25295 		return (error);
12595329Sgw25295 	}
12605329Sgw25295 
12615329Sgw25295 	/*
12627046Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
12638241SJeff.Bonwick@Sun.COM 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
12648241SJeff.Bonwick@Sun.COM 	 * since this would just restart the scrub we are already doing.
12657046Sahrens 	 */
12668241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
12678241SJeff.Bonwick@Sun.COM 	    vdev_resilver_needed(vd, NULL, NULL))
12688241SJeff.Bonwick@Sun.COM 		spa_async_request(spa, SPA_ASYNC_RESILVER);
12697046Sahrens 
1270789Sahrens 	return (0);
1271789Sahrens }
1272789Sahrens 
1273789Sahrens /*
12741986Seschrock  * Called once the vdevs are all opened, this routine validates the label
12751986Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
12764451Seschrock  * inadvertently do repair I/Os to the wrong device.
12771986Seschrock  *
12781986Seschrock  * This function will only return failure if one of the vdevs indicates that it
12791986Seschrock  * has since been destroyed or exported.  This is only possible if
12801986Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
12811986Seschrock  * will be updated but the function will return 0.
12821986Seschrock  */
12831986Seschrock int
12841986Seschrock vdev_validate(vdev_t *vd)
12851986Seschrock {
12861986Seschrock 	spa_t *spa = vd->vdev_spa;
12871986Seschrock 	nvlist_t *label;
128811422SMark.Musante@Sun.COM 	uint64_t guid = 0, top_guid;
12891986Seschrock 	uint64_t state;
12901986Seschrock 
12919816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
12921986Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
12934070Smc142369 			return (EBADF);
12941986Seschrock 
12952174Seschrock 	/*
12962174Seschrock 	 * If the device has already failed, or was marked offline, don't do
12972174Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
12982174Seschrock 	 * overwrite the previous state.
12992174Seschrock 	 */
13007754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
130111422SMark.Musante@Sun.COM 		uint64_t aux_guid = 0;
130211422SMark.Musante@Sun.COM 		nvlist_t *nvl;
13031986Seschrock 
13041986Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
13051986Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
13061986Seschrock 			    VDEV_AUX_BAD_LABEL);
13071986Seschrock 			return (0);
13081986Seschrock 		}
13091986Seschrock 
131011422SMark.Musante@Sun.COM 		/*
131111422SMark.Musante@Sun.COM 		 * Determine if this vdev has been split off into another
131211422SMark.Musante@Sun.COM 		 * pool.  If so, then refuse to open it.
131311422SMark.Musante@Sun.COM 		 */
131411422SMark.Musante@Sun.COM 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
131511422SMark.Musante@Sun.COM 		    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
131611422SMark.Musante@Sun.COM 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
131711422SMark.Musante@Sun.COM 			    VDEV_AUX_SPLIT_POOL);
131811422SMark.Musante@Sun.COM 			nvlist_free(label);
131911422SMark.Musante@Sun.COM 			return (0);
132011422SMark.Musante@Sun.COM 		}
132111422SMark.Musante@Sun.COM 
13221986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
13231986Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
13241986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13251986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13261986Seschrock 			nvlist_free(label);
13271986Seschrock 			return (0);
13281986Seschrock 		}
13291986Seschrock 
133011422SMark.Musante@Sun.COM 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
133111422SMark.Musante@Sun.COM 		    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
133211422SMark.Musante@Sun.COM 		    &aux_guid) != 0)
133311422SMark.Musante@Sun.COM 			aux_guid = 0;
133411422SMark.Musante@Sun.COM 
13357754SJeff.Bonwick@Sun.COM 		/*
13367754SJeff.Bonwick@Sun.COM 		 * If this vdev just became a top-level vdev because its
13377754SJeff.Bonwick@Sun.COM 		 * sibling was detached, it will have adopted the parent's
13387754SJeff.Bonwick@Sun.COM 		 * vdev guid -- but the label may or may not be on disk yet.
13397754SJeff.Bonwick@Sun.COM 		 * Fortunately, either version of the label will have the
13407754SJeff.Bonwick@Sun.COM 		 * same top guid, so if we're a top-level vdev, we can
13417754SJeff.Bonwick@Sun.COM 		 * safely compare to that instead.
134211422SMark.Musante@Sun.COM 		 *
134311422SMark.Musante@Sun.COM 		 * If we split this vdev off instead, then we also check the
134411422SMark.Musante@Sun.COM 		 * original pool's guid.  We don't want to consider the vdev
134511422SMark.Musante@Sun.COM 		 * corrupt if it is partway through a split operation.
13467754SJeff.Bonwick@Sun.COM 		 */
13471986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
13487754SJeff.Bonwick@Sun.COM 		    &guid) != 0 ||
13497754SJeff.Bonwick@Sun.COM 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
13507754SJeff.Bonwick@Sun.COM 		    &top_guid) != 0 ||
135111422SMark.Musante@Sun.COM 		    ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
13527754SJeff.Bonwick@Sun.COM 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
13531986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13541986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13551986Seschrock 			nvlist_free(label);
13561986Seschrock 			return (0);
13571986Seschrock 		}
13581986Seschrock 
13591986Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
13601986Seschrock 		    &state) != 0) {
13611986Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13621986Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13631986Seschrock 			nvlist_free(label);
13641986Seschrock 			return (0);
13651986Seschrock 		}
13661986Seschrock 
13671986Seschrock 		nvlist_free(label);
13681986Seschrock 
136910100SLin.Ling@Sun.COM 		/*
137010100SLin.Ling@Sun.COM 		 * If spa->spa_load_verbatim is true, no need to check the
137110100SLin.Ling@Sun.COM 		 * state of the pool.
137210100SLin.Ling@Sun.COM 		 */
137310100SLin.Ling@Sun.COM 		if (!spa->spa_load_verbatim &&
137411147SGeorge.Wilson@Sun.COM 		    spa_load_state(spa) == SPA_LOAD_OPEN &&
137510100SLin.Ling@Sun.COM 		    state != POOL_STATE_ACTIVE)
13764070Smc142369 			return (EBADF);
13776976Seschrock 
13786976Seschrock 		/*
13796976Seschrock 		 * If we were able to open and validate a vdev that was
13806976Seschrock 		 * previously marked permanently unavailable, clear that state
13816976Seschrock 		 * now.
13826976Seschrock 		 */
13836976Seschrock 		if (vd->vdev_not_present)
13846976Seschrock 			vd->vdev_not_present = 0;
13851986Seschrock 	}
13861986Seschrock 
13871986Seschrock 	return (0);
13881986Seschrock }
13891986Seschrock 
13901986Seschrock /*
1391789Sahrens  * Close a virtual device.
1392789Sahrens  */
1393789Sahrens void
1394789Sahrens vdev_close(vdev_t *vd)
1395789Sahrens {
13968241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
139710850SGeorge.Wilson@Sun.COM 	vdev_t *pvd = vd->vdev_parent;
13988241SJeff.Bonwick@Sun.COM 
13998241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
14008241SJeff.Bonwick@Sun.COM 
140111422SMark.Musante@Sun.COM 	/*
140211422SMark.Musante@Sun.COM 	 * If our parent is reopening, then we are as well, unless we are
140311422SMark.Musante@Sun.COM 	 * going offline.
140411422SMark.Musante@Sun.COM 	 */
140510850SGeorge.Wilson@Sun.COM 	if (pvd != NULL && pvd->vdev_reopening)
140611422SMark.Musante@Sun.COM 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
140710850SGeorge.Wilson@Sun.COM 
1408789Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1409789Sahrens 
14104451Seschrock 	vdev_cache_purge(vd);
1411789Sahrens 
14121986Seschrock 	/*
14139816SGeorge.Wilson@Sun.COM 	 * We record the previous state before we close it, so that if we are
14141986Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
14151986Seschrock 	 * it's still faulted.
14161986Seschrock 	 */
14171986Seschrock 	vd->vdev_prevstate = vd->vdev_state;
14181986Seschrock 
1419789Sahrens 	if (vd->vdev_offline)
1420789Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1421789Sahrens 	else
1422789Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
14231544Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1424789Sahrens }
1425789Sahrens 
142611958SGeorge.Wilson@Sun.COM void
142711958SGeorge.Wilson@Sun.COM vdev_hold(vdev_t *vd)
142811958SGeorge.Wilson@Sun.COM {
142911958SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
143011958SGeorge.Wilson@Sun.COM 
143111958SGeorge.Wilson@Sun.COM 	ASSERT(spa_is_root(spa));
143211958SGeorge.Wilson@Sun.COM 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
143311958SGeorge.Wilson@Sun.COM 		return;
143411958SGeorge.Wilson@Sun.COM 
143511958SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
143611958SGeorge.Wilson@Sun.COM 		vdev_hold(vd->vdev_child[c]);
143711958SGeorge.Wilson@Sun.COM 
143811958SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
143911958SGeorge.Wilson@Sun.COM 		vd->vdev_ops->vdev_op_hold(vd);
144011958SGeorge.Wilson@Sun.COM }
144111958SGeorge.Wilson@Sun.COM 
144211958SGeorge.Wilson@Sun.COM void
144311958SGeorge.Wilson@Sun.COM vdev_rele(vdev_t *vd)
144411958SGeorge.Wilson@Sun.COM {
144511958SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
144611958SGeorge.Wilson@Sun.COM 
144711958SGeorge.Wilson@Sun.COM 	ASSERT(spa_is_root(spa));
144811958SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
144911958SGeorge.Wilson@Sun.COM 		vdev_rele(vd->vdev_child[c]);
145011958SGeorge.Wilson@Sun.COM 
145111958SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
145211958SGeorge.Wilson@Sun.COM 		vd->vdev_ops->vdev_op_rele(vd);
145311958SGeorge.Wilson@Sun.COM }
145411958SGeorge.Wilson@Sun.COM 
145510850SGeorge.Wilson@Sun.COM /*
145610850SGeorge.Wilson@Sun.COM  * Reopen all interior vdevs and any unopened leaves.  We don't actually
145710850SGeorge.Wilson@Sun.COM  * reopen leaf vdevs which had previously been opened as they might deadlock
145810850SGeorge.Wilson@Sun.COM  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
145910850SGeorge.Wilson@Sun.COM  * If the leaf has never been opened then open it, as usual.
146010850SGeorge.Wilson@Sun.COM  */
1461789Sahrens void
14621544Seschrock vdev_reopen(vdev_t *vd)
1463789Sahrens {
14641544Seschrock 	spa_t *spa = vd->vdev_spa;
1465789Sahrens 
14667754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
14671544Seschrock 
146811422SMark.Musante@Sun.COM 	/* set the reopening flag unless we're taking the vdev offline */
146911422SMark.Musante@Sun.COM 	vd->vdev_reopening = !vd->vdev_offline;
1470789Sahrens 	vdev_close(vd);
1471789Sahrens 	(void) vdev_open(vd);
1472789Sahrens 
1473789Sahrens 	/*
14743377Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
14753377Seschrock 	 * Otherwise, a device with an invalid label could be successfully
14763377Seschrock 	 * opened in response to vdev_reopen().
14773377Seschrock 	 */
14786643Seschrock 	if (vd->vdev_aux) {
14796643Seschrock 		(void) vdev_validate_aux(vd);
14807754SJeff.Bonwick@Sun.COM 		if (vdev_readable(vd) && vdev_writeable(vd) &&
14819425SEric.Schrock@Sun.COM 		    vd->vdev_aux == &spa->spa_l2cache &&
14829816SGeorge.Wilson@Sun.COM 		    !l2arc_vdev_present(vd))
14839816SGeorge.Wilson@Sun.COM 			l2arc_add_vdev(spa, vd);
14846643Seschrock 	} else {
14856643Seschrock 		(void) vdev_validate(vd);
14866643Seschrock 	}
14873377Seschrock 
14883377Seschrock 	/*
14894451Seschrock 	 * Reassess parent vdev's health.
1490789Sahrens 	 */
14914451Seschrock 	vdev_propagate_state(vd);
1492789Sahrens }
1493789Sahrens 
1494789Sahrens int
14952082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1496789Sahrens {
1497789Sahrens 	int error;
1498789Sahrens 
1499789Sahrens 	/*
1500789Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1501789Sahrens 	 * For a create, however, we want to fail the request if
1502789Sahrens 	 * there are any components we can't open.
1503789Sahrens 	 */
1504789Sahrens 	error = vdev_open(vd);
1505789Sahrens 
1506789Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1507789Sahrens 		vdev_close(vd);
1508789Sahrens 		return (error ? error : ENXIO);
1509789Sahrens 	}
1510789Sahrens 
1511789Sahrens 	/*
1512789Sahrens 	 * Recursively initialize all labels.
1513789Sahrens 	 */
15143377Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
15153377Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1516789Sahrens 		vdev_close(vd);
1517789Sahrens 		return (error);
1518789Sahrens 	}
1519789Sahrens 
1520789Sahrens 	return (0);
1521789Sahrens }
1522789Sahrens 
15231585Sbonwick void
15249816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(vdev_t *vd)
1525789Sahrens {
1526789Sahrens 	/*
1527789Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1528789Sahrens 	 */
1529789Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1530789Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1531789Sahrens }
1532789Sahrens 
1533789Sahrens void
15341732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1535789Sahrens {
15361732Sbonwick 	ASSERT(vd == vd->vdev_top);
153710594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
15381732Sbonwick 	ASSERT(ISP2(flags));
1539789Sahrens 
15401732Sbonwick 	if (flags & VDD_METASLAB)
15411732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
15421732Sbonwick 
15431732Sbonwick 	if (flags & VDD_DTL)
15441732Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
15451732Sbonwick 
15461732Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1547789Sahrens }
1548789Sahrens 
15498241SJeff.Bonwick@Sun.COM /*
15508241SJeff.Bonwick@Sun.COM  * DTLs.
15518241SJeff.Bonwick@Sun.COM  *
15528241SJeff.Bonwick@Sun.COM  * A vdev's DTL (dirty time log) is the set of transaction groups for which
155311815SEric.Taylor@Sun.COM  * the vdev has less than perfect replication.  There are four kinds of DTL:
15548241SJeff.Bonwick@Sun.COM  *
15558241SJeff.Bonwick@Sun.COM  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
15568241SJeff.Bonwick@Sun.COM  *
15578241SJeff.Bonwick@Sun.COM  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
15588241SJeff.Bonwick@Sun.COM  *
15598241SJeff.Bonwick@Sun.COM  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
15608241SJeff.Bonwick@Sun.COM  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
15618241SJeff.Bonwick@Sun.COM  *	txgs that was scrubbed.
15628241SJeff.Bonwick@Sun.COM  *
15638241SJeff.Bonwick@Sun.COM  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
15648241SJeff.Bonwick@Sun.COM  *	persistent errors or just some device being offline.
15658241SJeff.Bonwick@Sun.COM  *	Unlike the other three, the DTL_OUTAGE map is not generally
15668241SJeff.Bonwick@Sun.COM  *	maintained; it's only computed when needed, typically to
15678241SJeff.Bonwick@Sun.COM  *	determine whether a device can be detached.
15688241SJeff.Bonwick@Sun.COM  *
15698241SJeff.Bonwick@Sun.COM  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
15708241SJeff.Bonwick@Sun.COM  * either has the data or it doesn't.
15718241SJeff.Bonwick@Sun.COM  *
15728241SJeff.Bonwick@Sun.COM  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
15738241SJeff.Bonwick@Sun.COM  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
15748241SJeff.Bonwick@Sun.COM  * if any child is less than fully replicated, then so is its parent.
15758241SJeff.Bonwick@Sun.COM  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
15768241SJeff.Bonwick@Sun.COM  * comprising only those txgs which appear in 'maxfaults' or more children;
15778241SJeff.Bonwick@Sun.COM  * those are the txgs we don't have enough replication to read.  For example,
15788241SJeff.Bonwick@Sun.COM  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
15798241SJeff.Bonwick@Sun.COM  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
15808241SJeff.Bonwick@Sun.COM  * two child DTL_MISSING maps.
15818241SJeff.Bonwick@Sun.COM  *
15828241SJeff.Bonwick@Sun.COM  * It should be clear from the above that to compute the DTLs and outage maps
15838241SJeff.Bonwick@Sun.COM  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
15848241SJeff.Bonwick@Sun.COM  * Therefore, that is all we keep on disk.  When loading the pool, or after
15858241SJeff.Bonwick@Sun.COM  * a configuration change, we generate all other DTLs from first principles.
15868241SJeff.Bonwick@Sun.COM  */
1587789Sahrens void
15888241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1589789Sahrens {
15908241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
15918241SJeff.Bonwick@Sun.COM 
15928241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
15938241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
15948241SJeff.Bonwick@Sun.COM 
1595789Sahrens 	mutex_enter(sm->sm_lock);
1596789Sahrens 	if (!space_map_contains(sm, txg, size))
1597789Sahrens 		space_map_add(sm, txg, size);
1598789Sahrens 	mutex_exit(sm->sm_lock);
1599789Sahrens }
1600789Sahrens 
16018241SJeff.Bonwick@Sun.COM boolean_t
16028241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1603789Sahrens {
16048241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
16058241SJeff.Bonwick@Sun.COM 	boolean_t dirty = B_FALSE;
16068241SJeff.Bonwick@Sun.COM 
16078241SJeff.Bonwick@Sun.COM 	ASSERT(t < DTL_TYPES);
16088241SJeff.Bonwick@Sun.COM 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1609789Sahrens 
1610789Sahrens 	mutex_enter(sm->sm_lock);
16118241SJeff.Bonwick@Sun.COM 	if (sm->sm_space != 0)
16128241SJeff.Bonwick@Sun.COM 		dirty = space_map_contains(sm, txg, size);
1613789Sahrens 	mutex_exit(sm->sm_lock);
1614789Sahrens 
1615789Sahrens 	return (dirty);
1616789Sahrens }
1617789Sahrens 
16188241SJeff.Bonwick@Sun.COM boolean_t
16198241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
16208241SJeff.Bonwick@Sun.COM {
16218241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[t];
16228241SJeff.Bonwick@Sun.COM 	boolean_t empty;
16238241SJeff.Bonwick@Sun.COM 
16248241SJeff.Bonwick@Sun.COM 	mutex_enter(sm->sm_lock);
16258241SJeff.Bonwick@Sun.COM 	empty = (sm->sm_space == 0);
16268241SJeff.Bonwick@Sun.COM 	mutex_exit(sm->sm_lock);
16278241SJeff.Bonwick@Sun.COM 
16288241SJeff.Bonwick@Sun.COM 	return (empty);
16298241SJeff.Bonwick@Sun.COM }
16308241SJeff.Bonwick@Sun.COM 
1631789Sahrens /*
1632789Sahrens  * Reassess DTLs after a config change or scrub completion.
1633789Sahrens  */
1634789Sahrens void
1635789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1636789Sahrens {
16371544Seschrock 	spa_t *spa = vd->vdev_spa;
16388241SJeff.Bonwick@Sun.COM 	avl_tree_t reftree;
16398241SJeff.Bonwick@Sun.COM 	int minref;
16408241SJeff.Bonwick@Sun.COM 
16418241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
16428241SJeff.Bonwick@Sun.COM 
16438241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
16448241SJeff.Bonwick@Sun.COM 		vdev_dtl_reassess(vd->vdev_child[c], txg,
16458241SJeff.Bonwick@Sun.COM 		    scrub_txg, scrub_done);
16468241SJeff.Bonwick@Sun.COM 
164710922SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
16488241SJeff.Bonwick@Sun.COM 		return;
16498241SJeff.Bonwick@Sun.COM 
16508241SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
1651789Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
16527046Sahrens 		if (scrub_txg != 0 &&
16537046Sahrens 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
16547046Sahrens 			/*
16557046Sahrens 			 * We completed a scrub up to scrub_txg.  If we
16567046Sahrens 			 * did it without rebooting, then the scrub dtl
16577046Sahrens 			 * will be valid, so excise the old region and
16587046Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
16597046Sahrens 			 * dtl as-is if there was an error.
16608241SJeff.Bonwick@Sun.COM 			 *
16618241SJeff.Bonwick@Sun.COM 			 * There's little trick here: to excise the beginning
16628241SJeff.Bonwick@Sun.COM 			 * of the DTL_MISSING map, we put it into a reference
16638241SJeff.Bonwick@Sun.COM 			 * tree and then add a segment with refcnt -1 that
16648241SJeff.Bonwick@Sun.COM 			 * covers the range [0, scrub_txg).  This means
16658241SJeff.Bonwick@Sun.COM 			 * that each txg in that range has refcnt -1 or 0.
16668241SJeff.Bonwick@Sun.COM 			 * We then add DTL_SCRUB with a refcnt of 2, so that
16678241SJeff.Bonwick@Sun.COM 			 * entries in the range [0, scrub_txg) will have a
16688241SJeff.Bonwick@Sun.COM 			 * positive refcnt -- either 1 or 2.  We then convert
16698241SJeff.Bonwick@Sun.COM 			 * the reference tree into the new DTL_MISSING map.
16707046Sahrens 			 */
16718241SJeff.Bonwick@Sun.COM 			space_map_ref_create(&reftree);
16728241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
16738241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
16748241SJeff.Bonwick@Sun.COM 			space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
16758241SJeff.Bonwick@Sun.COM 			space_map_ref_add_map(&reftree,
16768241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_SCRUB], 2);
16778241SJeff.Bonwick@Sun.COM 			space_map_ref_generate_map(&reftree,
16788241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl[DTL_MISSING], 1);
16798241SJeff.Bonwick@Sun.COM 			space_map_ref_destroy(&reftree);
1680789Sahrens 		}
16818241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
16828241SJeff.Bonwick@Sun.COM 		space_map_walk(&vd->vdev_dtl[DTL_MISSING],
16838241SJeff.Bonwick@Sun.COM 		    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1684789Sahrens 		if (scrub_done)
16858241SJeff.Bonwick@Sun.COM 			space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
16868241SJeff.Bonwick@Sun.COM 		space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
16878241SJeff.Bonwick@Sun.COM 		if (!vdev_readable(vd))
16888241SJeff.Bonwick@Sun.COM 			space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
16898241SJeff.Bonwick@Sun.COM 		else
16908241SJeff.Bonwick@Sun.COM 			space_map_walk(&vd->vdev_dtl[DTL_MISSING],
16918241SJeff.Bonwick@Sun.COM 			    space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1692789Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
16937046Sahrens 
16941732Sbonwick 		if (txg != 0)
16951732Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1696789Sahrens 		return;
1697789Sahrens 	}
1698789Sahrens 
1699789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17008241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
170110890SEric.Taylor@Sun.COM 		/* account for child's outage in parent's missing map */
170210890SEric.Taylor@Sun.COM 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
17038241SJeff.Bonwick@Sun.COM 		if (t == DTL_SCRUB)
17048241SJeff.Bonwick@Sun.COM 			continue;			/* leaf vdevs only */
17058241SJeff.Bonwick@Sun.COM 		if (t == DTL_PARTIAL)
17068241SJeff.Bonwick@Sun.COM 			minref = 1;			/* i.e. non-zero */
17078241SJeff.Bonwick@Sun.COM 		else if (vd->vdev_nparity != 0)
17088241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
17098241SJeff.Bonwick@Sun.COM 		else
17108241SJeff.Bonwick@Sun.COM 			minref = vd->vdev_children;	/* any kind of mirror */
17118241SJeff.Bonwick@Sun.COM 		space_map_ref_create(&reftree);
17128241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
17138241SJeff.Bonwick@Sun.COM 			vdev_t *cvd = vd->vdev_child[c];
17148241SJeff.Bonwick@Sun.COM 			mutex_enter(&cvd->vdev_dtl_lock);
171510890SEric.Taylor@Sun.COM 			space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1);
17168241SJeff.Bonwick@Sun.COM 			mutex_exit(&cvd->vdev_dtl_lock);
17178241SJeff.Bonwick@Sun.COM 		}
17188241SJeff.Bonwick@Sun.COM 		space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
17198241SJeff.Bonwick@Sun.COM 		space_map_ref_destroy(&reftree);
17208241SJeff.Bonwick@Sun.COM 	}
1721789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1722789Sahrens }
1723789Sahrens 
1724789Sahrens static int
1725789Sahrens vdev_dtl_load(vdev_t *vd)
1726789Sahrens {
1727789Sahrens 	spa_t *spa = vd->vdev_spa;
17288241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
17291732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1730789Sahrens 	dmu_buf_t *db;
1731789Sahrens 	int error;
1732789Sahrens 
1733789Sahrens 	ASSERT(vd->vdev_children == 0);
1734789Sahrens 
1735789Sahrens 	if (smo->smo_object == 0)
1736789Sahrens 		return (0);
1737789Sahrens 
173810594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
173910594SGeorge.Wilson@Sun.COM 
17401732Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
17411544Seschrock 		return (error);
17421732Sbonwick 
17434944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
17444944Smaybee 	bcopy(db->db_data, smo, sizeof (*smo));
17451544Seschrock 	dmu_buf_rele(db, FTAG);
1746789Sahrens 
1747789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17488241SJeff.Bonwick@Sun.COM 	error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
17498241SJeff.Bonwick@Sun.COM 	    NULL, SM_ALLOC, smo, mos);
1750789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1751789Sahrens 
1752789Sahrens 	return (error);
1753789Sahrens }
1754789Sahrens 
1755789Sahrens void
1756789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1757789Sahrens {
1758789Sahrens 	spa_t *spa = vd->vdev_spa;
17598241SJeff.Bonwick@Sun.COM 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
17608241SJeff.Bonwick@Sun.COM 	space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
17611732Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1762789Sahrens 	space_map_t smsync;
1763789Sahrens 	kmutex_t smlock;
1764789Sahrens 	dmu_buf_t *db;
1765789Sahrens 	dmu_tx_t *tx;
1766789Sahrens 
176710594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
176810594SGeorge.Wilson@Sun.COM 
1769789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1770789Sahrens 
1771789Sahrens 	if (vd->vdev_detached) {
1772789Sahrens 		if (smo->smo_object != 0) {
17731732Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1774789Sahrens 			ASSERT3U(err, ==, 0);
1775789Sahrens 			smo->smo_object = 0;
1776789Sahrens 		}
1777789Sahrens 		dmu_tx_commit(tx);
1778789Sahrens 		return;
1779789Sahrens 	}
1780789Sahrens 
1781789Sahrens 	if (smo->smo_object == 0) {
1782789Sahrens 		ASSERT(smo->smo_objsize == 0);
1783789Sahrens 		ASSERT(smo->smo_alloc == 0);
17841732Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1785789Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1786789Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1787789Sahrens 		ASSERT(smo->smo_object != 0);
1788789Sahrens 		vdev_config_dirty(vd->vdev_top);
1789789Sahrens 	}
1790789Sahrens 
1791789Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1792789Sahrens 
1793789Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1794789Sahrens 	    &smlock);
1795789Sahrens 
1796789Sahrens 	mutex_enter(&smlock);
1797789Sahrens 
1798789Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17991732Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1800789Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1801789Sahrens 
18021732Sbonwick 	space_map_truncate(smo, mos, tx);
18031732Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1804789Sahrens 
1805789Sahrens 	space_map_destroy(&smsync);
1806789Sahrens 
1807789Sahrens 	mutex_exit(&smlock);
1808789Sahrens 	mutex_destroy(&smlock);
1809789Sahrens 
18101732Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1811789Sahrens 	dmu_buf_will_dirty(db, tx);
18124944Smaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
18134944Smaybee 	bcopy(smo, db->db_data, sizeof (*smo));
18141544Seschrock 	dmu_buf_rele(db, FTAG);
1815789Sahrens 
1816789Sahrens 	dmu_tx_commit(tx);
1817789Sahrens }
1818789Sahrens 
18197046Sahrens /*
18208241SJeff.Bonwick@Sun.COM  * Determine whether the specified vdev can be offlined/detached/removed
18218241SJeff.Bonwick@Sun.COM  * without losing data.
18228241SJeff.Bonwick@Sun.COM  */
18238241SJeff.Bonwick@Sun.COM boolean_t
18248241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd)
18258241SJeff.Bonwick@Sun.COM {
18268241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
18278241SJeff.Bonwick@Sun.COM 	vdev_t *tvd = vd->vdev_top;
18288241SJeff.Bonwick@Sun.COM 	uint8_t cant_read = vd->vdev_cant_read;
18298241SJeff.Bonwick@Sun.COM 	boolean_t required;
18308241SJeff.Bonwick@Sun.COM 
18318241SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
18328241SJeff.Bonwick@Sun.COM 
18338241SJeff.Bonwick@Sun.COM 	if (vd == spa->spa_root_vdev || vd == tvd)
18348241SJeff.Bonwick@Sun.COM 		return (B_TRUE);
18358241SJeff.Bonwick@Sun.COM 
18368241SJeff.Bonwick@Sun.COM 	/*
18378241SJeff.Bonwick@Sun.COM 	 * Temporarily mark the device as unreadable, and then determine
18388241SJeff.Bonwick@Sun.COM 	 * whether this results in any DTL outages in the top-level vdev.
18398241SJeff.Bonwick@Sun.COM 	 * If not, we can safely offline/detach/remove the device.
18408241SJeff.Bonwick@Sun.COM 	 */
18418241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = B_TRUE;
18428241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
18438241SJeff.Bonwick@Sun.COM 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
18448241SJeff.Bonwick@Sun.COM 	vd->vdev_cant_read = cant_read;
18458241SJeff.Bonwick@Sun.COM 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
18468241SJeff.Bonwick@Sun.COM 
18478241SJeff.Bonwick@Sun.COM 	return (required);
18488241SJeff.Bonwick@Sun.COM }
18498241SJeff.Bonwick@Sun.COM 
18508241SJeff.Bonwick@Sun.COM /*
18517046Sahrens  * Determine if resilver is needed, and if so the txg range.
18527046Sahrens  */
18537046Sahrens boolean_t
18547046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
18557046Sahrens {
18567046Sahrens 	boolean_t needed = B_FALSE;
18577046Sahrens 	uint64_t thismin = UINT64_MAX;
18587046Sahrens 	uint64_t thismax = 0;
18597046Sahrens 
18607046Sahrens 	if (vd->vdev_children == 0) {
18617046Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
18628241SJeff.Bonwick@Sun.COM 		if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
18638241SJeff.Bonwick@Sun.COM 		    vdev_writeable(vd)) {
18647046Sahrens 			space_seg_t *ss;
18657046Sahrens 
18668241SJeff.Bonwick@Sun.COM 			ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
18677046Sahrens 			thismin = ss->ss_start - 1;
18688241SJeff.Bonwick@Sun.COM 			ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
18697046Sahrens 			thismax = ss->ss_end;
18707046Sahrens 			needed = B_TRUE;
18717046Sahrens 		}
18727046Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
18737046Sahrens 	} else {
18748241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
18757046Sahrens 			vdev_t *cvd = vd->vdev_child[c];
18767046Sahrens 			uint64_t cmin, cmax;
18777046Sahrens 
18787046Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
18797046Sahrens 				thismin = MIN(thismin, cmin);
18807046Sahrens 				thismax = MAX(thismax, cmax);
18817046Sahrens 				needed = B_TRUE;
18827046Sahrens 			}
18837046Sahrens 		}
18847046Sahrens 	}
18857046Sahrens 
18867046Sahrens 	if (needed && minp) {
18877046Sahrens 		*minp = thismin;
18887046Sahrens 		*maxp = thismax;
18897046Sahrens 	}
18907046Sahrens 	return (needed);
18917046Sahrens }
18927046Sahrens 
18931986Seschrock void
18941544Seschrock vdev_load(vdev_t *vd)
1895789Sahrens {
1896789Sahrens 	/*
1897789Sahrens 	 * Recursively load all children.
1898789Sahrens 	 */
18998241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
19001986Seschrock 		vdev_load(vd->vdev_child[c]);
1901789Sahrens 
1902789Sahrens 	/*
19031585Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1904789Sahrens 	 */
190510594SGeorge.Wilson@Sun.COM 	if (vd == vd->vdev_top && !vd->vdev_ishole &&
19061986Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
19071986Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
19081986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
19091986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1910789Sahrens 
1911789Sahrens 	/*
1912789Sahrens 	 * If this is a leaf vdev, load its DTL.
1913789Sahrens 	 */
19141986Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
19151986Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
19161986Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1917789Sahrens }
1918789Sahrens 
19192082Seschrock /*
19205450Sbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
19215450Sbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
19225450Sbrendan  * we make sure that we can open the underlying device, then try to read the
19235450Sbrendan  * label, and make sure that the label is sane and that it hasn't been
19245450Sbrendan  * repurposed to another pool.
19252082Seschrock  */
19262082Seschrock int
19275450Sbrendan vdev_validate_aux(vdev_t *vd)
19282082Seschrock {
19292082Seschrock 	nvlist_t *label;
19302082Seschrock 	uint64_t guid, version;
19312082Seschrock 	uint64_t state;
19322082Seschrock 
19337754SJeff.Bonwick@Sun.COM 	if (!vdev_readable(vd))
19346643Seschrock 		return (0);
19356643Seschrock 
19362082Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
19372082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
19382082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
19392082Seschrock 		return (-1);
19402082Seschrock 	}
19412082Seschrock 
19422082Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
19434577Sahrens 	    version > SPA_VERSION ||
19442082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
19452082Seschrock 	    guid != vd->vdev_guid ||
19462082Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
19472082Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
19482082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
19492082Seschrock 		nvlist_free(label);
19502082Seschrock 		return (-1);
19512082Seschrock 	}
19522082Seschrock 
19532082Seschrock 	/*
19542082Seschrock 	 * We don't actually check the pool state here.  If it's in fact in
19552082Seschrock 	 * use by another pool, we update this fact on the fly when requested.
19562082Seschrock 	 */
19572082Seschrock 	nvlist_free(label);
19582082Seschrock 	return (0);
19592082Seschrock }
19602082Seschrock 
1961789Sahrens void
196210594SGeorge.Wilson@Sun.COM vdev_remove(vdev_t *vd, uint64_t txg)
196310594SGeorge.Wilson@Sun.COM {
196410594SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
196510594SGeorge.Wilson@Sun.COM 	objset_t *mos = spa->spa_meta_objset;
196610594SGeorge.Wilson@Sun.COM 	dmu_tx_t *tx;
196710594SGeorge.Wilson@Sun.COM 
196810594SGeorge.Wilson@Sun.COM 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
196910594SGeorge.Wilson@Sun.COM 
197010594SGeorge.Wilson@Sun.COM 	if (vd->vdev_dtl_smo.smo_object) {
197110594SGeorge.Wilson@Sun.COM 		ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0);
197210594SGeorge.Wilson@Sun.COM 		(void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
197310594SGeorge.Wilson@Sun.COM 		vd->vdev_dtl_smo.smo_object = 0;
197410594SGeorge.Wilson@Sun.COM 	}
197510594SGeorge.Wilson@Sun.COM 
197610594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms != NULL) {
197710594SGeorge.Wilson@Sun.COM 		for (int m = 0; m < vd->vdev_ms_count; m++) {
197810594SGeorge.Wilson@Sun.COM 			metaslab_t *msp = vd->vdev_ms[m];
197910594SGeorge.Wilson@Sun.COM 
198010594SGeorge.Wilson@Sun.COM 			if (msp == NULL || msp->ms_smo.smo_object == 0)
198110594SGeorge.Wilson@Sun.COM 				continue;
198210594SGeorge.Wilson@Sun.COM 
198310594SGeorge.Wilson@Sun.COM 			ASSERT3U(msp->ms_smo.smo_alloc, ==, 0);
198410594SGeorge.Wilson@Sun.COM 			(void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
198510594SGeorge.Wilson@Sun.COM 			msp->ms_smo.smo_object = 0;
198610594SGeorge.Wilson@Sun.COM 		}
198710594SGeorge.Wilson@Sun.COM 	}
198810594SGeorge.Wilson@Sun.COM 
198910594SGeorge.Wilson@Sun.COM 	if (vd->vdev_ms_array) {
199010594SGeorge.Wilson@Sun.COM 		(void) dmu_object_free(mos, vd->vdev_ms_array, tx);
199110594SGeorge.Wilson@Sun.COM 		vd->vdev_ms_array = 0;
199210594SGeorge.Wilson@Sun.COM 		vd->vdev_ms_shift = 0;
199310594SGeorge.Wilson@Sun.COM 	}
199410594SGeorge.Wilson@Sun.COM 	dmu_tx_commit(tx);
199510594SGeorge.Wilson@Sun.COM }
199610594SGeorge.Wilson@Sun.COM 
199710594SGeorge.Wilson@Sun.COM void
1998789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
1999789Sahrens {
2000789Sahrens 	metaslab_t *msp;
200111146SGeorge.Wilson@Sun.COM 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2002789Sahrens 
200310594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
200410594SGeorge.Wilson@Sun.COM 
2005789Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2006789Sahrens 		metaslab_sync_done(msp, txg);
200711146SGeorge.Wilson@Sun.COM 
200811146SGeorge.Wilson@Sun.COM 	if (reassess)
200911146SGeorge.Wilson@Sun.COM 		metaslab_sync_reassess(vd->vdev_mg);
2010789Sahrens }
2011789Sahrens 
2012789Sahrens void
2013789Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2014789Sahrens {
2015789Sahrens 	spa_t *spa = vd->vdev_spa;
2016789Sahrens 	vdev_t *lvd;
2017789Sahrens 	metaslab_t *msp;
20181732Sbonwick 	dmu_tx_t *tx;
2019789Sahrens 
202010594SGeorge.Wilson@Sun.COM 	ASSERT(!vd->vdev_ishole);
202110594SGeorge.Wilson@Sun.COM 
20221732Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
20231732Sbonwick 		ASSERT(vd == vd->vdev_top);
20241732Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
20251732Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
20261732Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
20271732Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
20281732Sbonwick 		vdev_config_dirty(vd);
20291732Sbonwick 		dmu_tx_commit(tx);
20301732Sbonwick 	}
2031789Sahrens 
203210594SGeorge.Wilson@Sun.COM 	if (vd->vdev_removing)
203310594SGeorge.Wilson@Sun.COM 		vdev_remove(vd, txg);
203410594SGeorge.Wilson@Sun.COM 
20351732Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2036789Sahrens 		metaslab_sync(msp, txg);
20371732Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
20381732Sbonwick 	}
2039789Sahrens 
2040789Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2041789Sahrens 		vdev_dtl_sync(lvd, txg);
2042789Sahrens 
2043789Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2044789Sahrens }
2045789Sahrens 
2046789Sahrens uint64_t
2047789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2048789Sahrens {
2049789Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2050789Sahrens }
2051789Sahrens 
20524451Seschrock /*
20534451Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
20544451Seschrock  * not be opened, and no I/O is attempted.
20554451Seschrock  */
2056789Sahrens int
205710817SEric.Schrock@Sun.COM vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
20584451Seschrock {
20596643Seschrock 	vdev_t *vd;
20604451Seschrock 
206110685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
20624451Seschrock 
20636643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
20647754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
20657754SJeff.Bonwick@Sun.COM 
20664451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
20677754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
20684451Seschrock 
20694451Seschrock 	/*
207010817SEric.Schrock@Sun.COM 	 * We don't directly use the aux state here, but if we do a
207110817SEric.Schrock@Sun.COM 	 * vdev_reopen(), we need this value to be present to remember why we
207210817SEric.Schrock@Sun.COM 	 * were faulted.
207310817SEric.Schrock@Sun.COM 	 */
207410817SEric.Schrock@Sun.COM 	vd->vdev_label_aux = aux;
207510817SEric.Schrock@Sun.COM 
207610817SEric.Schrock@Sun.COM 	/*
20774451Seschrock 	 * Faulted state takes precedence over degraded.
20784451Seschrock 	 */
2079*12247SGeorge.Wilson@Sun.COM 	vd->vdev_delayed_close = B_FALSE;
20804451Seschrock 	vd->vdev_faulted = 1ULL;
20814451Seschrock 	vd->vdev_degraded = 0ULL;
208210817SEric.Schrock@Sun.COM 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
20834451Seschrock 
20844451Seschrock 	/*
208511982SGeorge.Wilson@Sun.COM 	 * If this device has the only valid copy of the data, then
208611982SGeorge.Wilson@Sun.COM 	 * back off and simply mark the vdev as degraded instead.
20874451Seschrock 	 */
208811982SGeorge.Wilson@Sun.COM 	if (!vd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
20894451Seschrock 		vd->vdev_degraded = 1ULL;
20904451Seschrock 		vd->vdev_faulted = 0ULL;
20914451Seschrock 
20924451Seschrock 		/*
20934451Seschrock 		 * If we reopen the device and it's not dead, only then do we
20944451Seschrock 		 * mark it degraded.
20954451Seschrock 		 */
20964451Seschrock 		vdev_reopen(vd);
20974451Seschrock 
209810817SEric.Schrock@Sun.COM 		if (vdev_readable(vd))
209910817SEric.Schrock@Sun.COM 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
21004451Seschrock 	}
21014451Seschrock 
21027754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
21034451Seschrock }
21044451Seschrock 
21054451Seschrock /*
21064451Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
21074451Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
21084451Seschrock  * as I/O is concerned.
21094451Seschrock  */
21104451Seschrock int
211110817SEric.Schrock@Sun.COM vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
21124451Seschrock {
21136643Seschrock 	vdev_t *vd;
21144451Seschrock 
211510685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
21164451Seschrock 
21176643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
21187754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
21197754SJeff.Bonwick@Sun.COM 
21204451Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
21217754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
21224451Seschrock 
21234451Seschrock 	/*
21244451Seschrock 	 * If the vdev is already faulted, then don't do anything.
21254451Seschrock 	 */
21267754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded)
21277754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, 0));
21284451Seschrock 
21294451Seschrock 	vd->vdev_degraded = 1ULL;
21304451Seschrock 	if (!vdev_is_dead(vd))
21314451Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
213210817SEric.Schrock@Sun.COM 		    aux);
21334451Seschrock 
21347754SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
21354451Seschrock }
21364451Seschrock 
21374451Seschrock /*
21384451Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
21394451Seschrock  * any attached spare device should be detached when the device finishes
21404451Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
21414451Seschrock  * so no FMA events are generated if the device fails to open.
21424451Seschrock  */
21434451Seschrock int
21447754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2145789Sahrens {
21469816SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2147789Sahrens 
214810685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
21491485Slling 
21506643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
21517754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2152789Sahrens 
21531585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
21547754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
21551585Sbonwick 
21569816SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
2157789Sahrens 	vd->vdev_offline = B_FALSE;
21581485Slling 	vd->vdev_tmpoffline = B_FALSE;
21597754SJeff.Bonwick@Sun.COM 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
21607754SJeff.Bonwick@Sun.COM 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
21619816SGeorge.Wilson@Sun.COM 
21629816SGeorge.Wilson@Sun.COM 	/* XXX - L2ARC 1.0 does not support expansion */
21639816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
21649816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
21659816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
21669816SGeorge.Wilson@Sun.COM 	}
21679816SGeorge.Wilson@Sun.COM 
21689816SGeorge.Wilson@Sun.COM 	vdev_reopen(tvd);
21694451Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
21704451Seschrock 
21719816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_aux) {
21729816SGeorge.Wilson@Sun.COM 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
21739816SGeorge.Wilson@Sun.COM 			pvd->vdev_expanding = B_FALSE;
21749816SGeorge.Wilson@Sun.COM 	}
21759816SGeorge.Wilson@Sun.COM 
21764451Seschrock 	if (newstate)
21774451Seschrock 		*newstate = vd->vdev_state;
21784451Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
21794451Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
21804451Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
21814451Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
21824451Seschrock 		vd->vdev_unspare = B_TRUE;
2183789Sahrens 
21849816SGeorge.Wilson@Sun.COM 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
21859816SGeorge.Wilson@Sun.COM 
21869816SGeorge.Wilson@Sun.COM 		/* XXX - L2ARC 1.0 does not support expansion */
21879816SGeorge.Wilson@Sun.COM 		if (vd->vdev_aux)
21889816SGeorge.Wilson@Sun.COM 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
21899816SGeorge.Wilson@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
21909816SGeorge.Wilson@Sun.COM 	}
21918241SJeff.Bonwick@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
2192789Sahrens }
2193789Sahrens 
219410974SJeff.Bonwick@Sun.COM static int
219510974SJeff.Bonwick@Sun.COM vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2196789Sahrens {
21979701SGeorge.Wilson@Sun.COM 	vdev_t *vd, *tvd;
219810685SGeorge.Wilson@Sun.COM 	int error = 0;
219910685SGeorge.Wilson@Sun.COM 	uint64_t generation;
220010685SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg;
220110685SGeorge.Wilson@Sun.COM 
220210685SGeorge.Wilson@Sun.COM top:
220310685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_ALLOC);
2204789Sahrens 
22056643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
22067754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2207789Sahrens 
22081585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
22097754SJeff.Bonwick@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
22101585Sbonwick 
22119701SGeorge.Wilson@Sun.COM 	tvd = vd->vdev_top;
221210685SGeorge.Wilson@Sun.COM 	mg = tvd->vdev_mg;
221310685SGeorge.Wilson@Sun.COM 	generation = spa->spa_config_generation + 1;
22149701SGeorge.Wilson@Sun.COM 
2215789Sahrens 	/*
22161732Sbonwick 	 * If the device isn't already offline, try to offline it.
2217789Sahrens 	 */
22181732Sbonwick 	if (!vd->vdev_offline) {
22191732Sbonwick 		/*
22208241SJeff.Bonwick@Sun.COM 		 * If this device has the only valid copy of some data,
22219701SGeorge.Wilson@Sun.COM 		 * don't allow it to be offlined. Log devices are always
22229701SGeorge.Wilson@Sun.COM 		 * expendable.
22231732Sbonwick 		 */
22249701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
22259701SGeorge.Wilson@Sun.COM 		    vdev_dtl_required(vd))
22267754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2227789Sahrens 
22281732Sbonwick 		/*
222910922SJeff.Bonwick@Sun.COM 		 * If the top-level is a slog and it has had allocations
223010922SJeff.Bonwick@Sun.COM 		 * then proceed.  We check that the vdev's metaslab group
223110922SJeff.Bonwick@Sun.COM 		 * is not NULL since it's possible that we may have just
223210922SJeff.Bonwick@Sun.COM 		 * added this vdev but not yet initialized its metaslabs.
223310685SGeorge.Wilson@Sun.COM 		 */
223410685SGeorge.Wilson@Sun.COM 		if (tvd->vdev_islog && mg != NULL) {
223510685SGeorge.Wilson@Sun.COM 			/*
223610685SGeorge.Wilson@Sun.COM 			 * Prevent any future allocations.
223710685SGeorge.Wilson@Sun.COM 			 */
223810974SJeff.Bonwick@Sun.COM 			metaslab_group_passivate(mg);
223910685SGeorge.Wilson@Sun.COM 			(void) spa_vdev_state_exit(spa, vd, 0);
224010685SGeorge.Wilson@Sun.COM 
224111422SMark.Musante@Sun.COM 			error = spa_offline_log(spa);
224210685SGeorge.Wilson@Sun.COM 
224310685SGeorge.Wilson@Sun.COM 			spa_vdev_state_enter(spa, SCL_ALLOC);
224410685SGeorge.Wilson@Sun.COM 
224510685SGeorge.Wilson@Sun.COM 			/*
224610685SGeorge.Wilson@Sun.COM 			 * Check to see if the config has changed.
224710685SGeorge.Wilson@Sun.COM 			 */
224810685SGeorge.Wilson@Sun.COM 			if (error || generation != spa->spa_config_generation) {
224910974SJeff.Bonwick@Sun.COM 				metaslab_group_activate(mg);
225010685SGeorge.Wilson@Sun.COM 				if (error)
225110685SGeorge.Wilson@Sun.COM 					return (spa_vdev_state_exit(spa,
225210685SGeorge.Wilson@Sun.COM 					    vd, error));
225310685SGeorge.Wilson@Sun.COM 				(void) spa_vdev_state_exit(spa, vd, 0);
225410685SGeorge.Wilson@Sun.COM 				goto top;
225510685SGeorge.Wilson@Sun.COM 			}
225610685SGeorge.Wilson@Sun.COM 			ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0);
225710685SGeorge.Wilson@Sun.COM 		}
225810685SGeorge.Wilson@Sun.COM 
225910685SGeorge.Wilson@Sun.COM 		/*
22601732Sbonwick 		 * Offline this device and reopen its top-level vdev.
22619701SGeorge.Wilson@Sun.COM 		 * If the top-level vdev is a log device then just offline
22629701SGeorge.Wilson@Sun.COM 		 * it. Otherwise, if this action results in the top-level
22639701SGeorge.Wilson@Sun.COM 		 * vdev becoming unusable, undo it and fail the request.
22641732Sbonwick 		 */
22651732Sbonwick 		vd->vdev_offline = B_TRUE;
22669701SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
22679701SGeorge.Wilson@Sun.COM 
22689701SGeorge.Wilson@Sun.COM 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
22699701SGeorge.Wilson@Sun.COM 		    vdev_is_dead(tvd)) {
22701732Sbonwick 			vd->vdev_offline = B_FALSE;
22719701SGeorge.Wilson@Sun.COM 			vdev_reopen(tvd);
22727754SJeff.Bonwick@Sun.COM 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
22731732Sbonwick 		}
227410685SGeorge.Wilson@Sun.COM 
227510685SGeorge.Wilson@Sun.COM 		/*
227610685SGeorge.Wilson@Sun.COM 		 * Add the device back into the metaslab rotor so that
227710685SGeorge.Wilson@Sun.COM 		 * once we online the device it's open for business.
227810685SGeorge.Wilson@Sun.COM 		 */
227910685SGeorge.Wilson@Sun.COM 		if (tvd->vdev_islog && mg != NULL)
228010974SJeff.Bonwick@Sun.COM 			metaslab_group_activate(mg);
2281789Sahrens 	}
2282789Sahrens 
22837754SJeff.Bonwick@Sun.COM 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
22841732Sbonwick 
228510685SGeorge.Wilson@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
2286789Sahrens }
2287789Sahrens 
228810974SJeff.Bonwick@Sun.COM int
228910974SJeff.Bonwick@Sun.COM vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
229010974SJeff.Bonwick@Sun.COM {
229110974SJeff.Bonwick@Sun.COM 	int error;
229210974SJeff.Bonwick@Sun.COM 
229310974SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_vdev_top_lock);
229410974SJeff.Bonwick@Sun.COM 	error = vdev_offline_locked(spa, guid, flags);
229510974SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_vdev_top_lock);
229610974SJeff.Bonwick@Sun.COM 
229710974SJeff.Bonwick@Sun.COM 	return (error);
229810974SJeff.Bonwick@Sun.COM }
229910974SJeff.Bonwick@Sun.COM 
23001544Seschrock /*
23011544Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
23021544Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
23031544Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
23041544Seschrock  */
23051544Seschrock void
23067754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd)
2307789Sahrens {
23087754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
23097754SJeff.Bonwick@Sun.COM 
23107754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2311789Sahrens 
23121544Seschrock 	if (vd == NULL)
23137754SJeff.Bonwick@Sun.COM 		vd = rvd;
2314789Sahrens 
23151544Seschrock 	vd->vdev_stat.vs_read_errors = 0;
23161544Seschrock 	vd->vdev_stat.vs_write_errors = 0;
23171544Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2318789Sahrens 
23197754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
23207754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd->vdev_child[c]);
23214451Seschrock 
23224451Seschrock 	/*
23236959Sek110237 	 * If we're in the FAULTED state or have experienced failed I/O, then
23246959Sek110237 	 * clear the persistent state and attempt to reopen the device.  We
23256959Sek110237 	 * also mark the vdev config dirty, so that the new faulted state is
23266959Sek110237 	 * written out to disk.
23274451Seschrock 	 */
23287754SJeff.Bonwick@Sun.COM 	if (vd->vdev_faulted || vd->vdev_degraded ||
23297754SJeff.Bonwick@Sun.COM 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
23306959Sek110237 
233110830SEric.Schrock@Sun.COM 		/*
233210830SEric.Schrock@Sun.COM 		 * When reopening in reponse to a clear event, it may be due to
233310830SEric.Schrock@Sun.COM 		 * a fmadm repair request.  In this case, if the device is
233410830SEric.Schrock@Sun.COM 		 * still broken, we want to still post the ereport again.
233510830SEric.Schrock@Sun.COM 		 */
233610830SEric.Schrock@Sun.COM 		vd->vdev_forcefault = B_TRUE;
233710830SEric.Schrock@Sun.COM 
23384451Seschrock 		vd->vdev_faulted = vd->vdev_degraded = 0;
23397754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_read = B_FALSE;
23407754SJeff.Bonwick@Sun.COM 		vd->vdev_cant_write = B_FALSE;
23417754SJeff.Bonwick@Sun.COM 
23424451Seschrock 		vdev_reopen(vd);
23434451Seschrock 
234410830SEric.Schrock@Sun.COM 		vd->vdev_forcefault = B_FALSE;
234510830SEric.Schrock@Sun.COM 
23467754SJeff.Bonwick@Sun.COM 		if (vd != rvd)
23477754SJeff.Bonwick@Sun.COM 			vdev_state_dirty(vd->vdev_top);
23487754SJeff.Bonwick@Sun.COM 
23497754SJeff.Bonwick@Sun.COM 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
23504808Sek110237 			spa_async_request(spa, SPA_ASYNC_RESILVER);
23514451Seschrock 
23524451Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
23534451Seschrock 	}
235410830SEric.Schrock@Sun.COM 
235510830SEric.Schrock@Sun.COM 	/*
235610830SEric.Schrock@Sun.COM 	 * When clearing a FMA-diagnosed fault, we always want to
235710830SEric.Schrock@Sun.COM 	 * unspare the device, as we assume that the original spare was
235810830SEric.Schrock@Sun.COM 	 * done in response to the FMA fault.
235910830SEric.Schrock@Sun.COM 	 */
236010830SEric.Schrock@Sun.COM 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
236110830SEric.Schrock@Sun.COM 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
236210830SEric.Schrock@Sun.COM 	    vd->vdev_parent->vdev_child[0] == vd)
236310830SEric.Schrock@Sun.COM 		vd->vdev_unspare = B_TRUE;
2364789Sahrens }
2365789Sahrens 
23667754SJeff.Bonwick@Sun.COM boolean_t
23677754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd)
23685329Sgw25295 {
236910594SGeorge.Wilson@Sun.COM 	/*
237010594SGeorge.Wilson@Sun.COM 	 * Holes and missing devices are always considered "dead".
237110594SGeorge.Wilson@Sun.COM 	 * This simplifies the code since we don't have to check for
237210594SGeorge.Wilson@Sun.COM 	 * these types of devices in the various code paths.
237310594SGeorge.Wilson@Sun.COM 	 * Instead we rely on the fact that we skip over dead devices
237410594SGeorge.Wilson@Sun.COM 	 * before issuing I/O to them.
237510594SGeorge.Wilson@Sun.COM 	 */
237610594SGeorge.Wilson@Sun.COM 	return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
237710594SGeorge.Wilson@Sun.COM 	    vd->vdev_ops == &vdev_missing_ops);
23785329Sgw25295 }
23795329Sgw25295 
23807754SJeff.Bonwick@Sun.COM boolean_t
23817754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd)
2382789Sahrens {
23837754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2384789Sahrens }
2385789Sahrens 
23867754SJeff.Bonwick@Sun.COM boolean_t
23877754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd)
2388789Sahrens {
23897754SJeff.Bonwick@Sun.COM 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
23907754SJeff.Bonwick@Sun.COM }
2391789Sahrens 
23927754SJeff.Bonwick@Sun.COM boolean_t
23937980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd)
23947980SGeorge.Wilson@Sun.COM {
23958241SJeff.Bonwick@Sun.COM 	uint64_t state = vd->vdev_state;
23968241SJeff.Bonwick@Sun.COM 
23977980SGeorge.Wilson@Sun.COM 	/*
23988241SJeff.Bonwick@Sun.COM 	 * We currently allow allocations from vdevs which may be in the
23997980SGeorge.Wilson@Sun.COM 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
24007980SGeorge.Wilson@Sun.COM 	 * fails to reopen then we'll catch it later when we're holding
24018241SJeff.Bonwick@Sun.COM 	 * the proper locks.  Note that we have to get the vdev state
24028241SJeff.Bonwick@Sun.COM 	 * in a local variable because although it changes atomically,
24038241SJeff.Bonwick@Sun.COM 	 * we're asking two separate questions about it.
24047980SGeorge.Wilson@Sun.COM 	 */
24058241SJeff.Bonwick@Sun.COM 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
240610594SGeorge.Wilson@Sun.COM 	    !vd->vdev_cant_write && !vd->vdev_ishole && !vd->vdev_removing);
24077980SGeorge.Wilson@Sun.COM }
24087980SGeorge.Wilson@Sun.COM 
24097980SGeorge.Wilson@Sun.COM boolean_t
24107754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio)
24117754SJeff.Bonwick@Sun.COM {
24127754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_vd == vd);
2413789Sahrens 
24147754SJeff.Bonwick@Sun.COM 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
24157754SJeff.Bonwick@Sun.COM 		return (B_FALSE);
2416789Sahrens 
24177754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_READ)
24187754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_read);
2419789Sahrens 
24207754SJeff.Bonwick@Sun.COM 	if (zio->io_type == ZIO_TYPE_WRITE)
24217754SJeff.Bonwick@Sun.COM 		return (!vd->vdev_cant_write);
24227754SJeff.Bonwick@Sun.COM 
24237754SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2424789Sahrens }
2425789Sahrens 
2426789Sahrens /*
2427789Sahrens  * Get statistics for the given vdev.
2428789Sahrens  */
2429789Sahrens void
2430789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2431789Sahrens {
2432789Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2433789Sahrens 
2434789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2435789Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
24367046Sahrens 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
2437789Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2438789Sahrens 	vs->vs_state = vd->vdev_state;
24399816SGeorge.Wilson@Sun.COM 	vs->vs_rsize = vdev_get_min_asize(vd);
24409816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf)
24419816SGeorge.Wilson@Sun.COM 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2442789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2443789Sahrens 
2444789Sahrens 	/*
2445789Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2446789Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2447789Sahrens 	 */
2448789Sahrens 	if (vd == rvd) {
24497754SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
2450789Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2451789Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2452789Sahrens 
2453789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
24547754SJeff.Bonwick@Sun.COM 			for (int t = 0; t < ZIO_TYPES; t++) {
2455789Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2456789Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2457789Sahrens 			}
2458789Sahrens 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
2459789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
2460789Sahrens 		}
2461789Sahrens 	}
2462789Sahrens }
2463789Sahrens 
2464789Sahrens void
24655450Sbrendan vdev_clear_stats(vdev_t *vd)
24665450Sbrendan {
24675450Sbrendan 	mutex_enter(&vd->vdev_stat_lock);
24685450Sbrendan 	vd->vdev_stat.vs_space = 0;
24695450Sbrendan 	vd->vdev_stat.vs_dspace = 0;
24705450Sbrendan 	vd->vdev_stat.vs_alloc = 0;
24715450Sbrendan 	mutex_exit(&vd->vdev_stat_lock);
24725450Sbrendan }
24735450Sbrendan 
24745450Sbrendan void
24757754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize)
2476789Sahrens {
24778241SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
24788241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
24797754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2480789Sahrens 	vdev_t *pvd;
2481789Sahrens 	uint64_t txg = zio->io_txg;
2482789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2483789Sahrens 	zio_type_t type = zio->io_type;
2484789Sahrens 	int flags = zio->io_flags;
2485789Sahrens 
24867754SJeff.Bonwick@Sun.COM 	/*
24877754SJeff.Bonwick@Sun.COM 	 * If this i/o is a gang leader, it didn't do any actual work.
24887754SJeff.Bonwick@Sun.COM 	 */
24897754SJeff.Bonwick@Sun.COM 	if (zio->io_gang_tree)
24907754SJeff.Bonwick@Sun.COM 		return;
24917754SJeff.Bonwick@Sun.COM 
2492789Sahrens 	if (zio->io_error == 0) {
24937754SJeff.Bonwick@Sun.COM 		/*
24947754SJeff.Bonwick@Sun.COM 		 * If this is a root i/o, don't count it -- we've already
24957754SJeff.Bonwick@Sun.COM 		 * counted the top-level vdevs, and vdev_get_stats() will
24967754SJeff.Bonwick@Sun.COM 		 * aggregate them when asked.  This reduces contention on
24977754SJeff.Bonwick@Sun.COM 		 * the root vdev_stat_lock and implicitly handles blocks
24987754SJeff.Bonwick@Sun.COM 		 * that compress away to holes, for which there is no i/o.
24997754SJeff.Bonwick@Sun.COM 		 * (Holes never create vdev children, so all the counters
25007754SJeff.Bonwick@Sun.COM 		 * remain zero, which is what we want.)
25017754SJeff.Bonwick@Sun.COM 		 *
25027754SJeff.Bonwick@Sun.COM 		 * Note: this only applies to successful i/o (io_error == 0)
25037754SJeff.Bonwick@Sun.COM 		 * because unlike i/o counts, errors are not additive.
25047754SJeff.Bonwick@Sun.COM 		 * When reading a ditto block, for example, failure of
25057754SJeff.Bonwick@Sun.COM 		 * one top-level vdev does not imply a root-level error.
25067754SJeff.Bonwick@Sun.COM 		 */
25077754SJeff.Bonwick@Sun.COM 		if (vd == rvd)
25087754SJeff.Bonwick@Sun.COM 			return;
25097754SJeff.Bonwick@Sun.COM 
25107754SJeff.Bonwick@Sun.COM 		ASSERT(vd == zio->io_vd);
25118241SJeff.Bonwick@Sun.COM 
25128241SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_BYPASS)
25138241SJeff.Bonwick@Sun.COM 			return;
25148241SJeff.Bonwick@Sun.COM 
25158241SJeff.Bonwick@Sun.COM 		mutex_enter(&vd->vdev_stat_lock);
25168241SJeff.Bonwick@Sun.COM 
25177754SJeff.Bonwick@Sun.COM 		if (flags & ZIO_FLAG_IO_REPAIR) {
25181807Sbonwick 			if (flags & ZIO_FLAG_SCRUB_THREAD)
25197754SJeff.Bonwick@Sun.COM 				vs->vs_scrub_repaired += psize;
25208241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SELF_HEAL)
25217754SJeff.Bonwick@Sun.COM 				vs->vs_self_healed += psize;
2522789Sahrens 		}
25238241SJeff.Bonwick@Sun.COM 
25248241SJeff.Bonwick@Sun.COM 		vs->vs_ops[type]++;
25258241SJeff.Bonwick@Sun.COM 		vs->vs_bytes[type] += psize;
25268241SJeff.Bonwick@Sun.COM 
25278241SJeff.Bonwick@Sun.COM 		mutex_exit(&vd->vdev_stat_lock);
2528789Sahrens 		return;
2529789Sahrens 	}
2530789Sahrens 
2531789Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2532789Sahrens 		return;
2533789Sahrens 
25349725SEric.Schrock@Sun.COM 	/*
25359725SEric.Schrock@Sun.COM 	 * If this is an I/O error that is going to be retried, then ignore the
25369725SEric.Schrock@Sun.COM 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
25379725SEric.Schrock@Sun.COM 	 * hard errors, when in reality they can happen for any number of
25389725SEric.Schrock@Sun.COM 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
25399725SEric.Schrock@Sun.COM 	 */
25409725SEric.Schrock@Sun.COM 	if (zio->io_error == EIO &&
25419725SEric.Schrock@Sun.COM 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
25429725SEric.Schrock@Sun.COM 		return;
25439725SEric.Schrock@Sun.COM 
254410685SGeorge.Wilson@Sun.COM 	/*
254510685SGeorge.Wilson@Sun.COM 	 * Intent logs writes won't propagate their error to the root
254610685SGeorge.Wilson@Sun.COM 	 * I/O so don't mark these types of failures as pool-level
254710685SGeorge.Wilson@Sun.COM 	 * errors.
254810685SGeorge.Wilson@Sun.COM 	 */
254910685SGeorge.Wilson@Sun.COM 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
255010685SGeorge.Wilson@Sun.COM 		return;
255110685SGeorge.Wilson@Sun.COM 
25527754SJeff.Bonwick@Sun.COM 	mutex_enter(&vd->vdev_stat_lock);
25539230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
25547754SJeff.Bonwick@Sun.COM 		if (zio->io_error == ECKSUM)
25557754SJeff.Bonwick@Sun.COM 			vs->vs_checksum_errors++;
25567754SJeff.Bonwick@Sun.COM 		else
25577754SJeff.Bonwick@Sun.COM 			vs->vs_read_errors++;
2558789Sahrens 	}
25599230SGeorge.Wilson@Sun.COM 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
25607754SJeff.Bonwick@Sun.COM 		vs->vs_write_errors++;
25617754SJeff.Bonwick@Sun.COM 	mutex_exit(&vd->vdev_stat_lock);
2562789Sahrens 
25638241SJeff.Bonwick@Sun.COM 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
25648241SJeff.Bonwick@Sun.COM 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
256510922SJeff.Bonwick@Sun.COM 	    (flags & ZIO_FLAG_SCRUB_THREAD) ||
256610922SJeff.Bonwick@Sun.COM 	    spa->spa_claiming)) {
25678241SJeff.Bonwick@Sun.COM 		/*
256810922SJeff.Bonwick@Sun.COM 		 * This is either a normal write (not a repair), or it's
256910922SJeff.Bonwick@Sun.COM 		 * a repair induced by the scrub thread, or it's a repair
257010922SJeff.Bonwick@Sun.COM 		 * made by zil_claim() during spa_load() in the first txg.
257110922SJeff.Bonwick@Sun.COM 		 * In the normal case, we commit the DTL change in the same
257210922SJeff.Bonwick@Sun.COM 		 * txg as the block was born.  In the scrub-induced repair
257310922SJeff.Bonwick@Sun.COM 		 * case, we know that scrubs run in first-pass syncing context,
257410922SJeff.Bonwick@Sun.COM 		 * so we commit the DTL change in spa_syncing_txg(spa).
257510922SJeff.Bonwick@Sun.COM 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
25768241SJeff.Bonwick@Sun.COM 		 *
25778241SJeff.Bonwick@Sun.COM 		 * We currently do not make DTL entries for failed spontaneous
25788241SJeff.Bonwick@Sun.COM 		 * self-healing writes triggered by normal (non-scrubbing)
25798241SJeff.Bonwick@Sun.COM 		 * reads, because we have no transactional context in which to
25808241SJeff.Bonwick@Sun.COM 		 * do so -- and it's not clear that it'd be desirable anyway.
25818241SJeff.Bonwick@Sun.COM 		 */
25828241SJeff.Bonwick@Sun.COM 		if (vd->vdev_ops->vdev_op_leaf) {
25838241SJeff.Bonwick@Sun.COM 			uint64_t commit_txg = txg;
25848241SJeff.Bonwick@Sun.COM 			if (flags & ZIO_FLAG_SCRUB_THREAD) {
25858241SJeff.Bonwick@Sun.COM 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
25868241SJeff.Bonwick@Sun.COM 				ASSERT(spa_sync_pass(spa) == 1);
25878241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
258810922SJeff.Bonwick@Sun.COM 				commit_txg = spa_syncing_txg(spa);
258910922SJeff.Bonwick@Sun.COM 			} else if (spa->spa_claiming) {
259010922SJeff.Bonwick@Sun.COM 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
259110922SJeff.Bonwick@Sun.COM 				commit_txg = spa_first_txg(spa);
25928241SJeff.Bonwick@Sun.COM 			}
259310922SJeff.Bonwick@Sun.COM 			ASSERT(commit_txg >= spa_syncing_txg(spa));
25948241SJeff.Bonwick@Sun.COM 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
25958241SJeff.Bonwick@Sun.COM 				return;
25968241SJeff.Bonwick@Sun.COM 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
25978241SJeff.Bonwick@Sun.COM 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
25988241SJeff.Bonwick@Sun.COM 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2599789Sahrens 		}
26008241SJeff.Bonwick@Sun.COM 		if (vd != rvd)
26018241SJeff.Bonwick@Sun.COM 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2602789Sahrens 	}
2603789Sahrens }
2604789Sahrens 
2605789Sahrens void
2606789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2607789Sahrens {
2608789Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2609789Sahrens 
26109816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
2611789Sahrens 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2612789Sahrens 
2613789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2614789Sahrens 
2615789Sahrens 	if (type == POOL_SCRUB_NONE) {
2616789Sahrens 		/*
2617789Sahrens 		 * Update completion and end time.  Leave everything else alone
2618789Sahrens 		 * so we can report what happened during the previous scrub.
2619789Sahrens 		 */
2620789Sahrens 		vs->vs_scrub_complete = complete;
2621789Sahrens 		vs->vs_scrub_end = gethrestime_sec();
2622789Sahrens 	} else {
2623789Sahrens 		vs->vs_scrub_type = type;
2624789Sahrens 		vs->vs_scrub_complete = 0;
2625789Sahrens 		vs->vs_scrub_examined = 0;
2626789Sahrens 		vs->vs_scrub_repaired = 0;
2627789Sahrens 		vs->vs_scrub_start = gethrestime_sec();
2628789Sahrens 		vs->vs_scrub_end = 0;
2629789Sahrens 	}
2630789Sahrens 
2631789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2632789Sahrens }
2633789Sahrens 
2634789Sahrens /*
263510922SJeff.Bonwick@Sun.COM  * Update the in-core space usage stats for this vdev, its metaslab class,
263610922SJeff.Bonwick@Sun.COM  * and the root vdev.
2637789Sahrens  */
2638789Sahrens void
263910922SJeff.Bonwick@Sun.COM vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
264010922SJeff.Bonwick@Sun.COM     int64_t space_delta)
2641789Sahrens {
26424527Sperrin 	int64_t dspace_delta = space_delta;
26434527Sperrin 	spa_t *spa = vd->vdev_spa;
26444527Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
264510922SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg = vd->vdev_mg;
264610922SJeff.Bonwick@Sun.COM 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
26474527Sperrin 
2648789Sahrens 	ASSERT(vd == vd->vdev_top);
26494527Sperrin 
26504527Sperrin 	/*
26514527Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
26524527Sperrin 	 * factor.  We must calculate this here and not at the root vdev
26534527Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
26544527Sperrin 	 * childrens', thus not accurate enough for us.
26554527Sperrin 	 */
26564527Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
26579701SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
26584527Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
26594527Sperrin 	    vd->vdev_deflate_ratio;
2660789Sahrens 
26614527Sperrin 	mutex_enter(&vd->vdev_stat_lock);
266210922SJeff.Bonwick@Sun.COM 	vd->vdev_stat.vs_alloc += alloc_delta;
26634527Sperrin 	vd->vdev_stat.vs_space += space_delta;
26644527Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
26654527Sperrin 	mutex_exit(&vd->vdev_stat_lock);
26662082Seschrock 
266710922SJeff.Bonwick@Sun.COM 	if (mc == spa_normal_class(spa)) {
266810922SJeff.Bonwick@Sun.COM 		mutex_enter(&rvd->vdev_stat_lock);
266910922SJeff.Bonwick@Sun.COM 		rvd->vdev_stat.vs_alloc += alloc_delta;
267010922SJeff.Bonwick@Sun.COM 		rvd->vdev_stat.vs_space += space_delta;
267110922SJeff.Bonwick@Sun.COM 		rvd->vdev_stat.vs_dspace += dspace_delta;
267210922SJeff.Bonwick@Sun.COM 		mutex_exit(&rvd->vdev_stat_lock);
267310922SJeff.Bonwick@Sun.COM 	}
267410922SJeff.Bonwick@Sun.COM 
267510922SJeff.Bonwick@Sun.COM 	if (mc != NULL) {
26765450Sbrendan 		ASSERT(rvd == vd->vdev_parent);
26775450Sbrendan 		ASSERT(vd->vdev_ms_count != 0);
26784527Sperrin 
267910922SJeff.Bonwick@Sun.COM 		metaslab_class_space_update(mc,
268010922SJeff.Bonwick@Sun.COM 		    alloc_delta, defer_delta, space_delta, dspace_delta);
26815450Sbrendan 	}
2682789Sahrens }
2683789Sahrens 
2684789Sahrens /*
2685789Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2686789Sahrens  * so that it will be written out next time the vdev configuration is synced.
2687789Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2688789Sahrens  */
2689789Sahrens void
2690789Sahrens vdev_config_dirty(vdev_t *vd)
2691789Sahrens {
2692789Sahrens 	spa_t *spa = vd->vdev_spa;
2693789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2694789Sahrens 	int c;
2695789Sahrens 
26961601Sbonwick 	/*
26979425SEric.Schrock@Sun.COM 	 * If this is an aux vdev (as with l2cache and spare devices), then we
26989425SEric.Schrock@Sun.COM 	 * update the vdev config manually and set the sync flag.
26996643Seschrock 	 */
27006643Seschrock 	if (vd->vdev_aux != NULL) {
27016643Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
27026643Seschrock 		nvlist_t **aux;
27036643Seschrock 		uint_t naux;
27046643Seschrock 
27056643Seschrock 		for (c = 0; c < sav->sav_count; c++) {
27066643Seschrock 			if (sav->sav_vdevs[c] == vd)
27076643Seschrock 				break;
27086643Seschrock 		}
27096643Seschrock 
27107754SJeff.Bonwick@Sun.COM 		if (c == sav->sav_count) {
27117754SJeff.Bonwick@Sun.COM 			/*
27127754SJeff.Bonwick@Sun.COM 			 * We're being removed.  There's nothing more to do.
27137754SJeff.Bonwick@Sun.COM 			 */
27147754SJeff.Bonwick@Sun.COM 			ASSERT(sav->sav_sync == B_TRUE);
27157754SJeff.Bonwick@Sun.COM 			return;
27167754SJeff.Bonwick@Sun.COM 		}
27177754SJeff.Bonwick@Sun.COM 
27186643Seschrock 		sav->sav_sync = B_TRUE;
27196643Seschrock 
27209425SEric.Schrock@Sun.COM 		if (nvlist_lookup_nvlist_array(sav->sav_config,
27219425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
27229425SEric.Schrock@Sun.COM 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
27239425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
27249425SEric.Schrock@Sun.COM 		}
27256643Seschrock 
27266643Seschrock 		ASSERT(c < naux);
27276643Seschrock 
27286643Seschrock 		/*
27296643Seschrock 		 * Setting the nvlist in the middle if the array is a little
27306643Seschrock 		 * sketchy, but it will work.
27316643Seschrock 		 */
27326643Seschrock 		nvlist_free(aux[c]);
27336643Seschrock 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
27346643Seschrock 
27356643Seschrock 		return;
27366643Seschrock 	}
27376643Seschrock 
27386643Seschrock 	/*
27397754SJeff.Bonwick@Sun.COM 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
27407754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
27417754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
27421601Sbonwick 	 * so this is sufficient to ensure mutual exclusion.
27431601Sbonwick 	 */
27447754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
27457754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
27467754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
27471601Sbonwick 
2748789Sahrens 	if (vd == rvd) {
2749789Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2750789Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2751789Sahrens 	} else {
2752789Sahrens 		ASSERT(vd == vd->vdev_top);
2753789Sahrens 
275410594SGeorge.Wilson@Sun.COM 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
275510594SGeorge.Wilson@Sun.COM 		    !vd->vdev_ishole)
27567754SJeff.Bonwick@Sun.COM 			list_insert_head(&spa->spa_config_dirty_list, vd);
2757789Sahrens 	}
2758789Sahrens }
2759789Sahrens 
2760789Sahrens void
2761789Sahrens vdev_config_clean(vdev_t *vd)
2762789Sahrens {
27631601Sbonwick 	spa_t *spa = vd->vdev_spa;
27641601Sbonwick 
27657754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
27667754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
27677754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
27687754SJeff.Bonwick@Sun.COM 
27697754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
27707754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_config_dirty_list, vd);
27717754SJeff.Bonwick@Sun.COM }
27727754SJeff.Bonwick@Sun.COM 
27737754SJeff.Bonwick@Sun.COM /*
27747754SJeff.Bonwick@Sun.COM  * Mark a top-level vdev's state as dirty, so that the next pass of
27757754SJeff.Bonwick@Sun.COM  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
27767754SJeff.Bonwick@Sun.COM  * the state changes from larger config changes because they require
27777754SJeff.Bonwick@Sun.COM  * much less locking, and are often needed for administrative actions.
27787754SJeff.Bonwick@Sun.COM  */
27797754SJeff.Bonwick@Sun.COM void
27807754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd)
27817754SJeff.Bonwick@Sun.COM {
27827754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
27837754SJeff.Bonwick@Sun.COM 
27847754SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
27851601Sbonwick 
27867754SJeff.Bonwick@Sun.COM 	/*
27877754SJeff.Bonwick@Sun.COM 	 * The state list is protected by the SCL_STATE lock.  The caller
27887754SJeff.Bonwick@Sun.COM 	 * must either hold SCL_STATE as writer, or must be the sync thread
27897754SJeff.Bonwick@Sun.COM 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
27907754SJeff.Bonwick@Sun.COM 	 * so this is sufficient to ensure mutual exclusion.
27917754SJeff.Bonwick@Sun.COM 	 */
27927754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
27937754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
27947754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
27957754SJeff.Bonwick@Sun.COM 
279610922SJeff.Bonwick@Sun.COM 	if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
27977754SJeff.Bonwick@Sun.COM 		list_insert_head(&spa->spa_state_dirty_list, vd);
27987754SJeff.Bonwick@Sun.COM }
27997754SJeff.Bonwick@Sun.COM 
28007754SJeff.Bonwick@Sun.COM void
28017754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd)
28027754SJeff.Bonwick@Sun.COM {
28037754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
28047754SJeff.Bonwick@Sun.COM 
28057754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
28067754SJeff.Bonwick@Sun.COM 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
28077754SJeff.Bonwick@Sun.COM 	    spa_config_held(spa, SCL_STATE, RW_READER)));
28087754SJeff.Bonwick@Sun.COM 
28097754SJeff.Bonwick@Sun.COM 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
28107754SJeff.Bonwick@Sun.COM 	list_remove(&spa->spa_state_dirty_list, vd);
2811789Sahrens }
2812789Sahrens 
28136523Sek110237 /*
28146523Sek110237  * Propagate vdev state up from children to parent.
28156523Sek110237  */
28161775Sbillm void
28171775Sbillm vdev_propagate_state(vdev_t *vd)
28181775Sbillm {
28198241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
28208241SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
28211775Sbillm 	int degraded = 0, faulted = 0;
28221775Sbillm 	int corrupted = 0;
28231775Sbillm 	vdev_t *child;
28241775Sbillm 
28254451Seschrock 	if (vd->vdev_children > 0) {
28269816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < vd->vdev_children; c++) {
28274451Seschrock 			child = vd->vdev_child[c];
28286976Seschrock 
282910594SGeorge.Wilson@Sun.COM 			/*
283010594SGeorge.Wilson@Sun.COM 			 * Don't factor holes into the decision.
283110594SGeorge.Wilson@Sun.COM 			 */
283210594SGeorge.Wilson@Sun.COM 			if (child->vdev_ishole)
283310594SGeorge.Wilson@Sun.COM 				continue;
283410594SGeorge.Wilson@Sun.COM 
28357754SJeff.Bonwick@Sun.COM 			if (!vdev_readable(child) ||
28368241SJeff.Bonwick@Sun.COM 			    (!vdev_writeable(child) && spa_writeable(spa))) {
28376976Seschrock 				/*
28386976Seschrock 				 * Root special: if there is a top-level log
28396976Seschrock 				 * device, treat the root vdev as if it were
28406976Seschrock 				 * degraded.
28416976Seschrock 				 */
28426976Seschrock 				if (child->vdev_islog && vd == rvd)
28436976Seschrock 					degraded++;
28446976Seschrock 				else
28456976Seschrock 					faulted++;
28466976Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
28474451Seschrock 				degraded++;
28486976Seschrock 			}
28494451Seschrock 
28504451Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
28514451Seschrock 				corrupted++;
28524451Seschrock 		}
28531775Sbillm 
28544451Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
28554451Seschrock 
28564451Seschrock 		/*
28577754SJeff.Bonwick@Sun.COM 		 * Root special: if there is a top-level vdev that cannot be
28584451Seschrock 		 * opened due to corrupted metadata, then propagate the root
28594451Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
28604451Seschrock 		 * replicas'.
28614451Seschrock 		 */
28624451Seschrock 		if (corrupted && vd == rvd &&
28634451Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
28644451Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
28654451Seschrock 			    VDEV_AUX_CORRUPT_DATA);
28661775Sbillm 	}
28671775Sbillm 
28686976Seschrock 	if (vd->vdev_parent)
28694451Seschrock 		vdev_propagate_state(vd->vdev_parent);
28701775Sbillm }
28711775Sbillm 
2872789Sahrens /*
28731544Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
28741544Seschrock  * state, because we're in the process of opening children depth-first.
28751544Seschrock  * Otherwise, we propagate the change to the parent.
28761544Seschrock  *
28771544Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
28781544Seschrock  * generated.
2879789Sahrens  */
2880789Sahrens void
28811544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2882789Sahrens {
28831986Seschrock 	uint64_t save_state;
28846643Seschrock 	spa_t *spa = vd->vdev_spa;
28851544Seschrock 
28861544Seschrock 	if (state == vd->vdev_state) {
28871544Seschrock 		vd->vdev_stat.vs_aux = aux;
2888789Sahrens 		return;
28891544Seschrock 	}
28901544Seschrock 
28911986Seschrock 	save_state = vd->vdev_state;
2892789Sahrens 
2893789Sahrens 	vd->vdev_state = state;
2894789Sahrens 	vd->vdev_stat.vs_aux = aux;
2895789Sahrens 
28964451Seschrock 	/*
28974451Seschrock 	 * If we are setting the vdev state to anything but an open state, then
2898*12247SGeorge.Wilson@Sun.COM 	 * always close the underlying device unless the device has requested
2899*12247SGeorge.Wilson@Sun.COM 	 * a delayed close (i.e. we're about to remove or fault the device).
2900*12247SGeorge.Wilson@Sun.COM 	 * Otherwise, we keep accessible but invalid devices open forever.
2901*12247SGeorge.Wilson@Sun.COM 	 * We don't call vdev_close() itself, because that implies some extra
2902*12247SGeorge.Wilson@Sun.COM 	 * checks (offline, etc) that we don't want here.  This is limited to
2903*12247SGeorge.Wilson@Sun.COM 	 * leaf devices, because otherwise closing the device will affect other
2904*12247SGeorge.Wilson@Sun.COM 	 * children.
29054451Seschrock 	 */
2906*12247SGeorge.Wilson@Sun.COM 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
2907*12247SGeorge.Wilson@Sun.COM 	    vd->vdev_ops->vdev_op_leaf)
29084451Seschrock 		vd->vdev_ops->vdev_op_close(vd);
29094451Seschrock 
291010817SEric.Schrock@Sun.COM 	/*
291110817SEric.Schrock@Sun.COM 	 * If we have brought this vdev back into service, we need
291210817SEric.Schrock@Sun.COM 	 * to notify fmd so that it can gracefully repair any outstanding
291310817SEric.Schrock@Sun.COM 	 * cases due to a missing device.  We do this in all cases, even those
291410817SEric.Schrock@Sun.COM 	 * that probably don't correlate to a repaired fault.  This is sure to
291510817SEric.Schrock@Sun.COM 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
291610817SEric.Schrock@Sun.COM 	 * this is a transient state it's OK, as the retire agent will
291710817SEric.Schrock@Sun.COM 	 * double-check the state of the vdev before repairing it.
291810817SEric.Schrock@Sun.COM 	 */
291910817SEric.Schrock@Sun.COM 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
292010817SEric.Schrock@Sun.COM 	    vd->vdev_prevstate != state)
292110817SEric.Schrock@Sun.COM 		zfs_post_state_change(spa, vd);
292210817SEric.Schrock@Sun.COM 
29234451Seschrock 	if (vd->vdev_removed &&
29244451Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
29254451Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
29264451Seschrock 		/*
29274451Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
29284451Seschrock 		 * device was previously marked removed and someone attempted to
29294451Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
29304451Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
29314451Seschrock 		 * it is one of our special test online cases, which is only
29324451Seschrock 		 * attempting to online the device and shouldn't generate an FMA
29334451Seschrock 		 * fault.
29344451Seschrock 		 */
29354451Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
29364451Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
29374451Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
29384451Seschrock 		vd->vdev_removed = B_TRUE;
29394451Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
29401544Seschrock 		/*
29411544Seschrock 		 * If we fail to open a vdev during an import, we mark it as
29421544Seschrock 		 * "not available", which signifies that it was never there to
29431544Seschrock 		 * begin with.  Failure to open such a device is not considered
29441544Seschrock 		 * an error.
29451544Seschrock 		 */
294611147SGeorge.Wilson@Sun.COM 		if (spa_load_state(spa) == SPA_LOAD_IMPORT &&
29471986Seschrock 		    vd->vdev_ops->vdev_op_leaf)
29481986Seschrock 			vd->vdev_not_present = 1;
29491986Seschrock 
29501986Seschrock 		/*
29511986Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
29521986Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
29531986Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
29541986Seschrock 		 * want to post the ereport if the device was already in the
29551986Seschrock 		 * CANT_OPEN state beforehand.
29564451Seschrock 		 *
29574451Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
29584451Seschrock 		 * online the device in response to an insertion event.  If we
29594451Seschrock 		 * hit this case, then we have detected an insertion event for a
29604451Seschrock 		 * faulted or offline device that wasn't in the removed state.
29614451Seschrock 		 * In this scenario, we don't post an ereport because we are
29624451Seschrock 		 * about to replace the device, or attempt an online with
29634451Seschrock 		 * vdev_forcefault, which will generate the fault for us.
29641986Seschrock 		 */
29654451Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
29664451Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
29676643Seschrock 		    vd != spa->spa_root_vdev) {
29681544Seschrock 			const char *class;
29691544Seschrock 
29701544Seschrock 			switch (aux) {
29711544Seschrock 			case VDEV_AUX_OPEN_FAILED:
29721544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
29731544Seschrock 				break;
29741544Seschrock 			case VDEV_AUX_CORRUPT_DATA:
29751544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
29761544Seschrock 				break;
29771544Seschrock 			case VDEV_AUX_NO_REPLICAS:
29781544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
29791544Seschrock 				break;
29801544Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
29811544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
29821544Seschrock 				break;
29831544Seschrock 			case VDEV_AUX_TOO_SMALL:
29841544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
29851544Seschrock 				break;
29861544Seschrock 			case VDEV_AUX_BAD_LABEL:
29871544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
29881544Seschrock 				break;
29891544Seschrock 			default:
29901544Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
29911544Seschrock 			}
29921544Seschrock 
29936643Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
29941544Seschrock 		}
29954451Seschrock 
29964451Seschrock 		/* Erase any notion of persistent removed state */
29974451Seschrock 		vd->vdev_removed = B_FALSE;
29984451Seschrock 	} else {
29994451Seschrock 		vd->vdev_removed = B_FALSE;
30001544Seschrock 	}
30011544Seschrock 
30029583STim.Haley@Sun.COM 	if (!isopen && vd->vdev_parent)
30039583STim.Haley@Sun.COM 		vdev_propagate_state(vd->vdev_parent);
3004789Sahrens }
30057042Sgw25295 
30067042Sgw25295 /*
30077042Sgw25295  * Check the vdev configuration to ensure that it's capable of supporting
30087042Sgw25295  * a root pool. Currently, we do not support RAID-Z or partial configuration.
30097042Sgw25295  * In addition, only a single top-level vdev is allowed and none of the leaves
30107042Sgw25295  * can be wholedisks.
30117042Sgw25295  */
30127042Sgw25295 boolean_t
30137042Sgw25295 vdev_is_bootable(vdev_t *vd)
30147042Sgw25295 {
30157042Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf) {
30167042Sgw25295 		char *vdev_type = vd->vdev_ops->vdev_op_type;
30177042Sgw25295 
30187042Sgw25295 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
30197042Sgw25295 		    vd->vdev_children > 1) {
30207042Sgw25295 			return (B_FALSE);
30217042Sgw25295 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
30227042Sgw25295 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
30237042Sgw25295 			return (B_FALSE);
30247042Sgw25295 		}
30257042Sgw25295 	} else if (vd->vdev_wholedisk == 1) {
30267042Sgw25295 		return (B_FALSE);
30277042Sgw25295 	}
30287042Sgw25295 
30299816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
30307042Sgw25295 		if (!vdev_is_bootable(vd->vdev_child[c]))
30317042Sgw25295 			return (B_FALSE);
30327042Sgw25295 	}
30337042Sgw25295 	return (B_TRUE);
30347042Sgw25295 }
30359701SGeorge.Wilson@Sun.COM 
303610594SGeorge.Wilson@Sun.COM /*
303710594SGeorge.Wilson@Sun.COM  * Load the state from the original vdev tree (ovd) which
303810594SGeorge.Wilson@Sun.COM  * we've retrieved from the MOS config object. If the original
303910594SGeorge.Wilson@Sun.COM  * vdev was offline then we transfer that state to the device
304010594SGeorge.Wilson@Sun.COM  * in the current vdev tree (nvd).
304110594SGeorge.Wilson@Sun.COM  */
30429701SGeorge.Wilson@Sun.COM void
304310594SGeorge.Wilson@Sun.COM vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
30449701SGeorge.Wilson@Sun.COM {
304510594SGeorge.Wilson@Sun.COM 	spa_t *spa = nvd->vdev_spa;
304610594SGeorge.Wilson@Sun.COM 
304710594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
304810594SGeorge.Wilson@Sun.COM 	ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
304910594SGeorge.Wilson@Sun.COM 
305010594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < nvd->vdev_children; c++)
305110594SGeorge.Wilson@Sun.COM 		vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
305210594SGeorge.Wilson@Sun.COM 
305310594SGeorge.Wilson@Sun.COM 	if (nvd->vdev_ops->vdev_op_leaf && ovd->vdev_offline) {
30549701SGeorge.Wilson@Sun.COM 		/*
30559701SGeorge.Wilson@Sun.COM 		 * It would be nice to call vdev_offline()
30569701SGeorge.Wilson@Sun.COM 		 * directly but the pool isn't fully loaded and
30579701SGeorge.Wilson@Sun.COM 		 * the txg threads have not been started yet.
30589701SGeorge.Wilson@Sun.COM 		 */
305910594SGeorge.Wilson@Sun.COM 		nvd->vdev_offline = ovd->vdev_offline;
306010594SGeorge.Wilson@Sun.COM 		vdev_reopen(nvd->vdev_top);
30619701SGeorge.Wilson@Sun.COM 	}
30629701SGeorge.Wilson@Sun.COM }
30639816SGeorge.Wilson@Sun.COM 
30649816SGeorge.Wilson@Sun.COM /*
30659816SGeorge.Wilson@Sun.COM  * Expand a vdev if possible.
30669816SGeorge.Wilson@Sun.COM  */
30679816SGeorge.Wilson@Sun.COM void
30689816SGeorge.Wilson@Sun.COM vdev_expand(vdev_t *vd, uint64_t txg)
30699816SGeorge.Wilson@Sun.COM {
30709816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_top == vd);
30719816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
30729816SGeorge.Wilson@Sun.COM 
30739816SGeorge.Wilson@Sun.COM 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
30749816SGeorge.Wilson@Sun.COM 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
30759816SGeorge.Wilson@Sun.COM 		vdev_config_dirty(vd);
30769816SGeorge.Wilson@Sun.COM 	}
30779816SGeorge.Wilson@Sun.COM }
307811422SMark.Musante@Sun.COM 
307911422SMark.Musante@Sun.COM /*
308011422SMark.Musante@Sun.COM  * Split a vdev.
308111422SMark.Musante@Sun.COM  */
308211422SMark.Musante@Sun.COM void
308311422SMark.Musante@Sun.COM vdev_split(vdev_t *vd)
308411422SMark.Musante@Sun.COM {
308511422SMark.Musante@Sun.COM 	vdev_t *cvd, *pvd = vd->vdev_parent;
308611422SMark.Musante@Sun.COM 
308711422SMark.Musante@Sun.COM 	vdev_remove_child(pvd, vd);
308811422SMark.Musante@Sun.COM 	vdev_compact_children(pvd);
308911422SMark.Musante@Sun.COM 
309011422SMark.Musante@Sun.COM 	cvd = pvd->vdev_child[0];
309111422SMark.Musante@Sun.COM 	if (pvd->vdev_children == 1) {
309211422SMark.Musante@Sun.COM 		vdev_remove_parent(cvd);
309311422SMark.Musante@Sun.COM 		cvd->vdev_splitting = B_TRUE;
309411422SMark.Musante@Sun.COM 	}
309511422SMark.Musante@Sun.COM 	vdev_propagate_state(cvd);
309611422SMark.Musante@Sun.COM }
3097