xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision e9103aaee0c546d4644791198c54abb03c89969e)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
2398d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24*e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25*e9103aaeSGarrett D'Amore  * Copyright (c) 2011 by Delphix. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
30fa9e4066Sahrens #include <sys/spa.h>
31fa9e4066Sahrens #include <sys/spa_impl.h>
32fa9e4066Sahrens #include <sys/dmu.h>
33fa9e4066Sahrens #include <sys/dmu_tx.h>
34fa9e4066Sahrens #include <sys/vdev_impl.h>
35fa9e4066Sahrens #include <sys/uberblock_impl.h>
36fa9e4066Sahrens #include <sys/metaslab.h>
37fa9e4066Sahrens #include <sys/metaslab_impl.h>
38fa9e4066Sahrens #include <sys/space_map.h>
39fa9e4066Sahrens #include <sys/zio.h>
40fa9e4066Sahrens #include <sys/zap.h>
41fa9e4066Sahrens #include <sys/fs/zfs.h>
42c5904d13Seschrock #include <sys/arc.h>
43e6ca193dSGeorge Wilson #include <sys/zil.h>
443f9d6ad7SLin Ling #include <sys/dsl_scan.h>
45fa9e4066Sahrens 
46fa9e4066Sahrens /*
47fa9e4066Sahrens  * Virtual device management.
48fa9e4066Sahrens  */
49fa9e4066Sahrens 
50fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = {
51fa9e4066Sahrens 	&vdev_root_ops,
52fa9e4066Sahrens 	&vdev_raidz_ops,
53fa9e4066Sahrens 	&vdev_mirror_ops,
54fa9e4066Sahrens 	&vdev_replacing_ops,
5599653d4eSeschrock 	&vdev_spare_ops,
56fa9e4066Sahrens 	&vdev_disk_ops,
57fa9e4066Sahrens 	&vdev_file_ops,
58fa9e4066Sahrens 	&vdev_missing_ops,
5988ecc943SGeorge Wilson 	&vdev_hole_ops,
60fa9e4066Sahrens 	NULL
61fa9e4066Sahrens };
62fa9e4066Sahrens 
63088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
64088f3894Sahrens int zfs_scrub_limit = 10;
6505b2b3b8Smishra 
66fa9e4066Sahrens /*
67fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
68fa9e4066Sahrens  */
69fa9e4066Sahrens static vdev_ops_t *
70fa9e4066Sahrens vdev_getops(const char *type)
71fa9e4066Sahrens {
72fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
73fa9e4066Sahrens 
74fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
75fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
76fa9e4066Sahrens 			break;
77fa9e4066Sahrens 
78fa9e4066Sahrens 	return (ops);
79fa9e4066Sahrens }
80fa9e4066Sahrens 
81fa9e4066Sahrens /*
82fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
83fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
84fa9e4066Sahrens  */
85fa9e4066Sahrens uint64_t
86fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
87fa9e4066Sahrens {
88ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
89fa9e4066Sahrens 	uint64_t csize;
90fa9e4066Sahrens 
91573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
92fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
93fa9e4066Sahrens 		asize = MAX(asize, csize);
94fa9e4066Sahrens 	}
95fa9e4066Sahrens 
96fa9e4066Sahrens 	return (asize);
97fa9e4066Sahrens }
98fa9e4066Sahrens 
992a79c5feSlling /*
100573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
101573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
102573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
103573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
1042a79c5feSlling  */
1052a79c5feSlling uint64_t
106573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
1072a79c5feSlling {
108573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
1092a79c5feSlling 
110573ca77eSGeorge Wilson 	/*
111573ca77eSGeorge Wilson 	 * The our parent is NULL (inactive spare or cache) or is the root,
112573ca77eSGeorge Wilson 	 * just return our own asize.
113573ca77eSGeorge Wilson 	 */
114573ca77eSGeorge Wilson 	if (pvd == NULL)
115573ca77eSGeorge Wilson 		return (vd->vdev_asize);
1162a79c5feSlling 
1172a79c5feSlling 	/*
118573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
119573ca77eSGeorge Wilson 	 * to the nearest metaslab.
1202a79c5feSlling 	 */
121573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
122573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
1232a79c5feSlling 
124573ca77eSGeorge Wilson 	/*
125573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
126573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
127573ca77eSGeorge Wilson 	 */
128573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
129573ca77eSGeorge Wilson 		return (pvd->vdev_min_asize / pvd->vdev_children);
1302a79c5feSlling 
131573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
132573ca77eSGeorge Wilson }
1332a79c5feSlling 
134573ca77eSGeorge Wilson void
135573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
136573ca77eSGeorge Wilson {
137573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
138573ca77eSGeorge Wilson 
139573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
140573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
1412a79c5feSlling }
1422a79c5feSlling 
143fa9e4066Sahrens vdev_t *
144fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
145fa9e4066Sahrens {
146fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
147fa9e4066Sahrens 
148e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
149e05725b1Sbonwick 
150088f3894Sahrens 	if (vdev < rvd->vdev_children) {
151088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
152fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
153088f3894Sahrens 	}
154fa9e4066Sahrens 
155fa9e4066Sahrens 	return (NULL);
156fa9e4066Sahrens }
157fa9e4066Sahrens 
158fa9e4066Sahrens vdev_t *
159fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
160fa9e4066Sahrens {
161fa9e4066Sahrens 	vdev_t *mvd;
162fa9e4066Sahrens 
1630e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
164fa9e4066Sahrens 		return (vd);
165fa9e4066Sahrens 
166573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
167fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
168fa9e4066Sahrens 		    NULL)
169fa9e4066Sahrens 			return (mvd);
170fa9e4066Sahrens 
171fa9e4066Sahrens 	return (NULL);
172fa9e4066Sahrens }
173fa9e4066Sahrens 
174fa9e4066Sahrens void
175fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
176fa9e4066Sahrens {
177fa9e4066Sahrens 	size_t oldsize, newsize;
178fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
179fa9e4066Sahrens 	vdev_t **newchild;
180fa9e4066Sahrens 
181e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
182fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
183fa9e4066Sahrens 
184fa9e4066Sahrens 	cvd->vdev_parent = pvd;
185fa9e4066Sahrens 
186fa9e4066Sahrens 	if (pvd == NULL)
187fa9e4066Sahrens 		return;
188fa9e4066Sahrens 
189fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
190fa9e4066Sahrens 
191fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
192fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
193fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
194fa9e4066Sahrens 
195fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
196fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
197fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
198fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
199fa9e4066Sahrens 	}
200fa9e4066Sahrens 
201fa9e4066Sahrens 	pvd->vdev_child = newchild;
202fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
203fa9e4066Sahrens 
204fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
205fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
206fa9e4066Sahrens 
207fa9e4066Sahrens 	/*
208fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
209fa9e4066Sahrens 	 */
210fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
211fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
212fa9e4066Sahrens }
213fa9e4066Sahrens 
214fa9e4066Sahrens void
215fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
216fa9e4066Sahrens {
217fa9e4066Sahrens 	int c;
218fa9e4066Sahrens 	uint_t id = cvd->vdev_id;
219fa9e4066Sahrens 
220fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == pvd);
221fa9e4066Sahrens 
222fa9e4066Sahrens 	if (pvd == NULL)
223fa9e4066Sahrens 		return;
224fa9e4066Sahrens 
225fa9e4066Sahrens 	ASSERT(id < pvd->vdev_children);
226fa9e4066Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
227fa9e4066Sahrens 
228fa9e4066Sahrens 	pvd->vdev_child[id] = NULL;
229fa9e4066Sahrens 	cvd->vdev_parent = NULL;
230fa9e4066Sahrens 
231fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
232fa9e4066Sahrens 		if (pvd->vdev_child[c])
233fa9e4066Sahrens 			break;
234fa9e4066Sahrens 
235fa9e4066Sahrens 	if (c == pvd->vdev_children) {
236fa9e4066Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
237fa9e4066Sahrens 		pvd->vdev_child = NULL;
238fa9e4066Sahrens 		pvd->vdev_children = 0;
239fa9e4066Sahrens 	}
240fa9e4066Sahrens 
241fa9e4066Sahrens 	/*
242fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
243fa9e4066Sahrens 	 */
244fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
245fa9e4066Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
246fa9e4066Sahrens }
247fa9e4066Sahrens 
248fa9e4066Sahrens /*
249fa9e4066Sahrens  * Remove any holes in the child array.
250fa9e4066Sahrens  */
251fa9e4066Sahrens void
252fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
253fa9e4066Sahrens {
254fa9e4066Sahrens 	vdev_t **newchild, *cvd;
255fa9e4066Sahrens 	int oldc = pvd->vdev_children;
256573ca77eSGeorge Wilson 	int newc;
257fa9e4066Sahrens 
258e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
259fa9e4066Sahrens 
260573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
261fa9e4066Sahrens 		if (pvd->vdev_child[c])
262fa9e4066Sahrens 			newc++;
263fa9e4066Sahrens 
264fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
265fa9e4066Sahrens 
266573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
267fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
268fa9e4066Sahrens 			newchild[newc] = cvd;
269fa9e4066Sahrens 			cvd->vdev_id = newc++;
270fa9e4066Sahrens 		}
271fa9e4066Sahrens 	}
272fa9e4066Sahrens 
273fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
274fa9e4066Sahrens 	pvd->vdev_child = newchild;
275fa9e4066Sahrens 	pvd->vdev_children = newc;
276fa9e4066Sahrens }
277fa9e4066Sahrens 
278fa9e4066Sahrens /*
279fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
280fa9e4066Sahrens  */
28188ecc943SGeorge Wilson vdev_t *
282fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
283fa9e4066Sahrens {
284fa9e4066Sahrens 	vdev_t *vd;
285fa9e4066Sahrens 
286fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
287fa9e4066Sahrens 
2880e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
2890e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
2900e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
291*e9103aaeSGarrett D'Amore 		spa->spa_load_guid = spa_generate_guid(NULL);
2920e34b6a7Sbonwick 	}
2930e34b6a7Sbonwick 
29488ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
2950e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
2960e34b6a7Sbonwick 			/*
2970e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
2980e34b6a7Sbonwick 			 * which must be unique among all pools.
2990e34b6a7Sbonwick 			 */
3001195e687SMark J Musante 			guid = spa_generate_guid(NULL);
3010e34b6a7Sbonwick 		} else {
3020e34b6a7Sbonwick 			/*
3030e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3040e34b6a7Sbonwick 			 */
3051195e687SMark J Musante 			guid = spa_generate_guid(spa);
3060e34b6a7Sbonwick 		}
3070e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3080e34b6a7Sbonwick 	}
3090e34b6a7Sbonwick 
310fa9e4066Sahrens 	vd->vdev_spa = spa;
311fa9e4066Sahrens 	vd->vdev_id = id;
312fa9e4066Sahrens 	vd->vdev_guid = guid;
313fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
314fa9e4066Sahrens 	vd->vdev_ops = ops;
315fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
31688ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
317fa9e4066Sahrens 
318fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3195ad82045Snd 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
320e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3218ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
3228ad4d6ddSJeff Bonwick 		space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
3238ad4d6ddSJeff Bonwick 		    &vd->vdev_dtl_lock);
3248ad4d6ddSJeff Bonwick 	}
325fa9e4066Sahrens 	txg_list_create(&vd->vdev_ms_list,
326fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
327fa9e4066Sahrens 	txg_list_create(&vd->vdev_dtl_list,
328fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
329fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3303d7072f8Seschrock 	vdev_queue_init(vd);
3313d7072f8Seschrock 	vdev_cache_init(vd);
332fa9e4066Sahrens 
333fa9e4066Sahrens 	return (vd);
334fa9e4066Sahrens }
335fa9e4066Sahrens 
336fa9e4066Sahrens /*
337fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
338fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
339fa9e4066Sahrens  * different for each case.
340fa9e4066Sahrens  */
34199653d4eSeschrock int
34299653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
34399653d4eSeschrock     int alloctype)
344fa9e4066Sahrens {
345fa9e4066Sahrens 	vdev_ops_t *ops;
346fa9e4066Sahrens 	char *type;
3478654d025Sperrin 	uint64_t guid = 0, islog, nparity;
348fa9e4066Sahrens 	vdev_t *vd;
349fa9e4066Sahrens 
350e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
351fa9e4066Sahrens 
352fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
35399653d4eSeschrock 		return (EINVAL);
354fa9e4066Sahrens 
355fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
35699653d4eSeschrock 		return (EINVAL);
357fa9e4066Sahrens 
358fa9e4066Sahrens 	/*
359fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
360fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
361fa9e4066Sahrens 	 */
362fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
363fa9e4066Sahrens 		uint64_t label_id;
364fa9e4066Sahrens 
365fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
366fa9e4066Sahrens 		    label_id != id)
36799653d4eSeschrock 			return (EINVAL);
368fa9e4066Sahrens 
369fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
37099653d4eSeschrock 			return (EINVAL);
37199653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
37299653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
37399653d4eSeschrock 			return (EINVAL);
374fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
375fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
376fa94a07fSbrendan 			return (EINVAL);
37721ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
37821ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
37921ecdf64SLin Ling 			return (EINVAL);
380fa9e4066Sahrens 	}
381fa9e4066Sahrens 
38299653d4eSeschrock 	/*
38399653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
38499653d4eSeschrock 	 */
38599653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
38699653d4eSeschrock 		return (EINVAL);
38799653d4eSeschrock 
3888654d025Sperrin 	/*
3898654d025Sperrin 	 * Determine whether we're a log vdev.
3908654d025Sperrin 	 */
3918654d025Sperrin 	islog = 0;
3928654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
393990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
3948654d025Sperrin 		return (ENOTSUP);
395fa9e4066Sahrens 
39688ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
39788ecc943SGeorge Wilson 		return (ENOTSUP);
39888ecc943SGeorge Wilson 
39999653d4eSeschrock 	/*
4008654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
40199653d4eSeschrock 	 */
4028654d025Sperrin 	nparity = -1ULL;
40399653d4eSeschrock 	if (ops == &vdev_raidz_ops) {
40499653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4058654d025Sperrin 		    &nparity) == 0) {
406b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
40799653d4eSeschrock 				return (EINVAL);
40899653d4eSeschrock 			/*
409f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
410f94275ceSAdam Leventhal 			 * device.
41199653d4eSeschrock 			 */
412f94275ceSAdam Leventhal 			if (nparity > 1 &&
413f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
414f94275ceSAdam Leventhal 				return (ENOTSUP);
415f94275ceSAdam Leventhal 			if (nparity > 2 &&
416f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
41799653d4eSeschrock 				return (ENOTSUP);
41899653d4eSeschrock 		} else {
41999653d4eSeschrock 			/*
42099653d4eSeschrock 			 * We require the parity to be specified for SPAs that
42199653d4eSeschrock 			 * support multiple parity levels.
42299653d4eSeschrock 			 */
423f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
42499653d4eSeschrock 				return (EINVAL);
42599653d4eSeschrock 			/*
42699653d4eSeschrock 			 * Otherwise, we default to 1 parity device for RAID-Z.
42799653d4eSeschrock 			 */
4288654d025Sperrin 			nparity = 1;
42999653d4eSeschrock 		}
43099653d4eSeschrock 	} else {
4318654d025Sperrin 		nparity = 0;
43299653d4eSeschrock 	}
4338654d025Sperrin 	ASSERT(nparity != -1ULL);
4348654d025Sperrin 
4358654d025Sperrin 	vd = vdev_alloc_common(spa, id, guid, ops);
4368654d025Sperrin 
4378654d025Sperrin 	vd->vdev_islog = islog;
4388654d025Sperrin 	vd->vdev_nparity = nparity;
4398654d025Sperrin 
4408654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
4418654d025Sperrin 		vd->vdev_path = spa_strdup(vd->vdev_path);
4428654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
4438654d025Sperrin 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4448654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4458654d025Sperrin 	    &vd->vdev_physpath) == 0)
4468654d025Sperrin 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4476809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4486809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
44999653d4eSeschrock 
450afefbcddSeschrock 	/*
451afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
452afefbcddSeschrock 	 * as -1.
453afefbcddSeschrock 	 */
454afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
455afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
456afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
457afefbcddSeschrock 
458ea8dc4b6Seschrock 	/*
459ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
460ea8dc4b6Seschrock 	 * was not present at the time of import.
461ea8dc4b6Seschrock 	 */
4626809eb4eSEric Schrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4636809eb4eSEric Schrock 	    &vd->vdev_not_present);
464ea8dc4b6Seschrock 
465ecc2d604Sbonwick 	/*
466ecc2d604Sbonwick 	 * Get the alignment requirement.
467ecc2d604Sbonwick 	 */
468ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
469ecc2d604Sbonwick 
47088ecc943SGeorge Wilson 	/*
47188ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
47288ecc943SGeorge Wilson 	 */
47388ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
47488ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
47588ecc943SGeorge Wilson 
476fa9e4066Sahrens 	/*
477fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
478fa9e4066Sahrens 	 */
4791195e687SMark J Musante 	if (parent && !parent->vdev_parent &&
4801195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
481fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
482fa9e4066Sahrens 		    &vd->vdev_ms_array);
483fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
484fa9e4066Sahrens 		    &vd->vdev_ms_shift);
485fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
486fa9e4066Sahrens 		    &vd->vdev_asize);
4873f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
4883f9d6ad7SLin Ling 		    &vd->vdev_removing);
489fa9e4066Sahrens 	}
490fa9e4066Sahrens 
491a1521560SJeff Bonwick 	if (parent && !parent->vdev_parent) {
492a1521560SJeff Bonwick 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
4939f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ADD ||
4941195e687SMark J Musante 		    alloctype == VDEV_ALLOC_SPLIT ||
4959f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ROOTPOOL);
496a1521560SJeff Bonwick 		vd->vdev_mg = metaslab_group_create(islog ?
497a1521560SJeff Bonwick 		    spa_log_class(spa) : spa_normal_class(spa), vd);
498a1521560SJeff Bonwick 	}
499a1521560SJeff Bonwick 
500fa9e4066Sahrens 	/*
5013d7072f8Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
502fa9e4066Sahrens 	 */
503c5904d13Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
50421ecdf64SLin Ling 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
50521ecdf64SLin Ling 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
506c5904d13Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
507c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
5088ad4d6ddSJeff Bonwick 			    &vd->vdev_dtl_smo.smo_object);
509c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
510c5904d13Seschrock 			    &vd->vdev_unspare);
511c5904d13Seschrock 		}
51221ecdf64SLin Ling 
51321ecdf64SLin Ling 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
51421ecdf64SLin Ling 			uint64_t spare = 0;
51521ecdf64SLin Ling 
51621ecdf64SLin Ling 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
51721ecdf64SLin Ling 			    &spare) == 0 && spare)
51821ecdf64SLin Ling 				spa_spare_add(vd);
51921ecdf64SLin Ling 		}
52021ecdf64SLin Ling 
521ecc2d604Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
522ecc2d604Sbonwick 		    &vd->vdev_offline);
523c5904d13Seschrock 
524cb04b873SMark J Musante 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVERING,
525cb04b873SMark J Musante 		    &vd->vdev_resilvering);
526cb04b873SMark J Musante 
5273d7072f8Seschrock 		/*
5283d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5293d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
530069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
531069f55e2SEric Schrock 		 * remain in the faulted state.
5323d7072f8Seschrock 		 */
533b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
5343d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5353d7072f8Seschrock 			    &vd->vdev_faulted);
5363d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5373d7072f8Seschrock 			    &vd->vdev_degraded);
5383d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5393d7072f8Seschrock 			    &vd->vdev_removed);
540069f55e2SEric Schrock 
541069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
542069f55e2SEric Schrock 				char *aux;
543069f55e2SEric Schrock 
544069f55e2SEric Schrock 				vd->vdev_label_aux =
545069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
546069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
547069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
548069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
549069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
550069f55e2SEric Schrock 			}
5513d7072f8Seschrock 		}
552fa9e4066Sahrens 	}
553fa9e4066Sahrens 
554fa9e4066Sahrens 	/*
555fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
556fa9e4066Sahrens 	 */
557fa9e4066Sahrens 	vdev_add_child(parent, vd);
558fa9e4066Sahrens 
55999653d4eSeschrock 	*vdp = vd;
56099653d4eSeschrock 
56199653d4eSeschrock 	return (0);
562fa9e4066Sahrens }
563fa9e4066Sahrens 
564fa9e4066Sahrens void
565fa9e4066Sahrens vdev_free(vdev_t *vd)
566fa9e4066Sahrens {
5673d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
568fa9e4066Sahrens 
569fa9e4066Sahrens 	/*
570fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
571fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
572fa9e4066Sahrens 	 */
573fa9e4066Sahrens 	vdev_close(vd);
574fa9e4066Sahrens 
575e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
576b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
577fa9e4066Sahrens 
578fa9e4066Sahrens 	/*
579fa9e4066Sahrens 	 * Free all children.
580fa9e4066Sahrens 	 */
581573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
582fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
583fa9e4066Sahrens 
584fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
585fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
586fa9e4066Sahrens 
587fa9e4066Sahrens 	/*
588fa9e4066Sahrens 	 * Discard allocation state.
589fa9e4066Sahrens 	 */
590a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
591fa9e4066Sahrens 		vdev_metaslab_fini(vd);
592a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
593a1521560SJeff Bonwick 	}
594fa9e4066Sahrens 
595fa9e4066Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
59699653d4eSeschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
597fa9e4066Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
598fa9e4066Sahrens 
599fa9e4066Sahrens 	/*
600fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
601fa9e4066Sahrens 	 */
602fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
603fa9e4066Sahrens 
604fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
605fa9e4066Sahrens 
6063d7072f8Seschrock 	/*
6073d7072f8Seschrock 	 * Clean up vdev structure.
6083d7072f8Seschrock 	 */
6093d7072f8Seschrock 	vdev_queue_fini(vd);
6103d7072f8Seschrock 	vdev_cache_fini(vd);
6113d7072f8Seschrock 
6123d7072f8Seschrock 	if (vd->vdev_path)
6133d7072f8Seschrock 		spa_strfree(vd->vdev_path);
6143d7072f8Seschrock 	if (vd->vdev_devid)
6153d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
6163d7072f8Seschrock 	if (vd->vdev_physpath)
6173d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
6186809eb4eSEric Schrock 	if (vd->vdev_fru)
6196809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
6203d7072f8Seschrock 
6213d7072f8Seschrock 	if (vd->vdev_isspare)
6223d7072f8Seschrock 		spa_spare_remove(vd);
623fa94a07fSbrendan 	if (vd->vdev_isl2cache)
624fa94a07fSbrendan 		spa_l2cache_remove(vd);
6253d7072f8Seschrock 
6263d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
6273d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
6288ad4d6ddSJeff Bonwick 
6293d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
6308ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
6318ad4d6ddSJeff Bonwick 		space_map_unload(&vd->vdev_dtl[t]);
6328ad4d6ddSJeff Bonwick 		space_map_destroy(&vd->vdev_dtl[t]);
6338ad4d6ddSJeff Bonwick 	}
6343d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
6358ad4d6ddSJeff Bonwick 
6363d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6373d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
638e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
6393d7072f8Seschrock 
6403d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
6413d7072f8Seschrock 		spa->spa_root_vdev = NULL;
6423d7072f8Seschrock 
6433d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
644fa9e4066Sahrens }
645fa9e4066Sahrens 
646fa9e4066Sahrens /*
647fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
648fa9e4066Sahrens  */
649fa9e4066Sahrens static void
650fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
651fa9e4066Sahrens {
652fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
653fa9e4066Sahrens 	metaslab_t *msp;
654fa9e4066Sahrens 	vdev_t *vd;
655fa9e4066Sahrens 	int t;
656fa9e4066Sahrens 
657fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
658fa9e4066Sahrens 
659fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
660fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
661fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
662fa9e4066Sahrens 
663fa9e4066Sahrens 	svd->vdev_ms_array = 0;
664fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
665fa9e4066Sahrens 	svd->vdev_ms_count = 0;
666fa9e4066Sahrens 
667fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
668fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
669fa9e4066Sahrens 
670fa9e4066Sahrens 	svd->vdev_mg = NULL;
671fa9e4066Sahrens 	svd->vdev_ms = NULL;
672ecc2d604Sbonwick 
673ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
674ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
675fa9e4066Sahrens 
676fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
677fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
67899653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
679fa9e4066Sahrens 
680fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
681fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
68299653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
683fa9e4066Sahrens 
684fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
685fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
686fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
687fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
688fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
689fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
690fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
691fa9e4066Sahrens 	}
692fa9e4066Sahrens 
693e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
694fa9e4066Sahrens 		vdev_config_clean(svd);
695fa9e4066Sahrens 		vdev_config_dirty(tvd);
696fa9e4066Sahrens 	}
697fa9e4066Sahrens 
698e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
699e14bb325SJeff Bonwick 		vdev_state_clean(svd);
700e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
701e14bb325SJeff Bonwick 	}
702e14bb325SJeff Bonwick 
70399653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
70499653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
7058654d025Sperrin 
7068654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
7078654d025Sperrin 	svd->vdev_islog = 0;
708fa9e4066Sahrens }
709fa9e4066Sahrens 
710fa9e4066Sahrens static void
711fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
712fa9e4066Sahrens {
713fa9e4066Sahrens 	if (vd == NULL)
714fa9e4066Sahrens 		return;
715fa9e4066Sahrens 
716fa9e4066Sahrens 	vd->vdev_top = tvd;
717fa9e4066Sahrens 
718573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
719fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
720fa9e4066Sahrens }
721fa9e4066Sahrens 
722fa9e4066Sahrens /*
723fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
724fa9e4066Sahrens  */
725fa9e4066Sahrens vdev_t *
726fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
727fa9e4066Sahrens {
728fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
729fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
730fa9e4066Sahrens 	vdev_t *mvd;
731fa9e4066Sahrens 
732e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
733fa9e4066Sahrens 
734fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
735ecc2d604Sbonwick 
736ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
737573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
738ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
739ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
74088ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
741ecc2d604Sbonwick 
742fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
743fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
744fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
745fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
746fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
747fa9e4066Sahrens 
748fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
749fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
750fa9e4066Sahrens 
751fa9e4066Sahrens 	return (mvd);
752fa9e4066Sahrens }
753fa9e4066Sahrens 
754fa9e4066Sahrens /*
755fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
756fa9e4066Sahrens  */
757fa9e4066Sahrens void
758fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
759fa9e4066Sahrens {
760fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
761fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
762fa9e4066Sahrens 
763e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
764fa9e4066Sahrens 
765fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
766fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
76799653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
76899653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
769ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
770fa9e4066Sahrens 
771fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
772fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
7738ad4d6ddSJeff Bonwick 
77499653d4eSeschrock 	/*
775e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
776e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
777e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
778e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
77999653d4eSeschrock 	 */
7808ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
7818ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
7821195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
7838ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
7848ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
7858ad4d6ddSJeff Bonwick 	}
786e14bb325SJeff Bonwick 	cvd->vdev_id = mvd->vdev_id;
787e14bb325SJeff Bonwick 	vdev_add_child(pvd, cvd);
788fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
789fa9e4066Sahrens 
790fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
791fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
792fa9e4066Sahrens 
793fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
794fa9e4066Sahrens 	vdev_free(mvd);
795fa9e4066Sahrens }
796fa9e4066Sahrens 
797ea8dc4b6Seschrock int
798fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
799fa9e4066Sahrens {
800fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
801ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
802ecc2d604Sbonwick 	uint64_t m;
803fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
804fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
805ecc2d604Sbonwick 	metaslab_t **mspp;
806ecc2d604Sbonwick 	int error;
807fa9e4066Sahrens 
808a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
809a1521560SJeff Bonwick 
81088ecc943SGeorge Wilson 	/*
81188ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
81288ecc943SGeorge Wilson 	 */
81388ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
8140e34b6a7Sbonwick 		return (0);
8150e34b6a7Sbonwick 
81688ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
81788ecc943SGeorge Wilson 
818e6ca193dSGeorge Wilson 	/*
819e6ca193dSGeorge Wilson 	 * Compute the raidz-deflation ratio.  Note, we hard-code
820e6ca193dSGeorge Wilson 	 * in 128k (1 << 17) because it is the current "typical" blocksize.
821e6ca193dSGeorge Wilson 	 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
822e6ca193dSGeorge Wilson 	 * or we will inconsistently account for existing bp's.
823e6ca193dSGeorge Wilson 	 */
824e6ca193dSGeorge Wilson 	vd->vdev_deflate_ratio = (1 << 17) /
825e6ca193dSGeorge Wilson 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
826e6ca193dSGeorge Wilson 
827fa9e4066Sahrens 	ASSERT(oldc <= newc);
828fa9e4066Sahrens 
829ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
830fa9e4066Sahrens 
831ecc2d604Sbonwick 	if (oldc != 0) {
832ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
833ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
834ecc2d604Sbonwick 	}
835fa9e4066Sahrens 
836ecc2d604Sbonwick 	vd->vdev_ms = mspp;
837ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
838fa9e4066Sahrens 
839ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
840ecc2d604Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
841ecc2d604Sbonwick 		if (txg == 0) {
842ecc2d604Sbonwick 			uint64_t object = 0;
843ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8447bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8457bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
846ecc2d604Sbonwick 			if (error)
847ecc2d604Sbonwick 				return (error);
848ecc2d604Sbonwick 			if (object != 0) {
849ecc2d604Sbonwick 				dmu_buf_t *db;
850ecc2d604Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
851ecc2d604Sbonwick 				if (error)
852ecc2d604Sbonwick 					return (error);
8531934e92fSmaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
8541934e92fSmaybee 				bcopy(db->db_data, &smo, sizeof (smo));
855ecc2d604Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
856ea8dc4b6Seschrock 				dmu_buf_rele(db, FTAG);
857fa9e4066Sahrens 			}
858fa9e4066Sahrens 		}
859ecc2d604Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
860ecc2d604Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
861fa9e4066Sahrens 	}
862fa9e4066Sahrens 
863a1521560SJeff Bonwick 	if (txg == 0)
864a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
865a1521560SJeff Bonwick 
8663f9d6ad7SLin Ling 	/*
8673f9d6ad7SLin Ling 	 * If the vdev is being removed we don't activate
8683f9d6ad7SLin Ling 	 * the metaslabs since we want to ensure that no new
8693f9d6ad7SLin Ling 	 * allocations are performed on this device.
8703f9d6ad7SLin Ling 	 */
8713f9d6ad7SLin Ling 	if (oldc == 0 && !vd->vdev_removing)
872a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
873a1521560SJeff Bonwick 
874a1521560SJeff Bonwick 	if (txg == 0)
875a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
876a1521560SJeff Bonwick 
877ea8dc4b6Seschrock 	return (0);
878fa9e4066Sahrens }
879fa9e4066Sahrens 
880fa9e4066Sahrens void
881fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
882fa9e4066Sahrens {
883fa9e4066Sahrens 	uint64_t m;
884fa9e4066Sahrens 	uint64_t count = vd->vdev_ms_count;
885fa9e4066Sahrens 
886fa9e4066Sahrens 	if (vd->vdev_ms != NULL) {
887a1521560SJeff Bonwick 		metaslab_group_passivate(vd->vdev_mg);
888fa9e4066Sahrens 		for (m = 0; m < count; m++)
889ecc2d604Sbonwick 			if (vd->vdev_ms[m] != NULL)
890ecc2d604Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
891fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
892fa9e4066Sahrens 		vd->vdev_ms = NULL;
893fa9e4066Sahrens 	}
894fa9e4066Sahrens }
895fa9e4066Sahrens 
896e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
897e14bb325SJeff Bonwick 	boolean_t	vps_readable;
898e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
899e14bb325SJeff Bonwick 	int		vps_flags;
900e14bb325SJeff Bonwick } vdev_probe_stats_t;
901e14bb325SJeff Bonwick 
902e14bb325SJeff Bonwick static void
903e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
9040a4e9518Sgw {
9058ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
906a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
907e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
908a3f829aeSBill Moore 
909a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
910e14bb325SJeff Bonwick 
911e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
912e14bb325SJeff Bonwick 		if (zio->io_error == 0)
913e14bb325SJeff Bonwick 			vps->vps_readable = 1;
9148ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
915a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
916e14bb325SJeff Bonwick 			    zio->io_offset, zio->io_size, zio->io_data,
917e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
918e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
919e14bb325SJeff Bonwick 		} else {
920e14bb325SJeff Bonwick 			zio_buf_free(zio->io_data, zio->io_size);
921e14bb325SJeff Bonwick 		}
922e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
923e14bb325SJeff Bonwick 		if (zio->io_error == 0)
924e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
925e14bb325SJeff Bonwick 		zio_buf_free(zio->io_data, zio->io_size);
926e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
927a3f829aeSBill Moore 		zio_t *pio;
928e14bb325SJeff Bonwick 
929e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
930e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
931e14bb325SJeff Bonwick 
932e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
9338ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
934e14bb325SJeff Bonwick 			zio->io_error = 0;
935e14bb325SJeff Bonwick 		} else {
936e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
937e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
9388ad4d6ddSJeff Bonwick 			    spa, vd, NULL, 0, 0);
939e14bb325SJeff Bonwick 			zio->io_error = ENXIO;
940e14bb325SJeff Bonwick 		}
941a3f829aeSBill Moore 
942a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
943a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
944a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
945a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
946a3f829aeSBill Moore 
947a3f829aeSBill Moore 		while ((pio = zio_walk_parents(zio)) != NULL)
948a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
949a3f829aeSBill Moore 				pio->io_error = ENXIO;
950a3f829aeSBill Moore 
951e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
952e14bb325SJeff Bonwick 	}
953e14bb325SJeff Bonwick }
9540a4e9518Sgw 
955e14bb325SJeff Bonwick /*
956e14bb325SJeff Bonwick  * Determine whether this device is accessible by reading and writing
957e14bb325SJeff Bonwick  * to several known locations: the pad regions of each vdev label
958e14bb325SJeff Bonwick  * but the first (which we leave alone in case it contains a VTOC).
959e14bb325SJeff Bonwick  */
960e14bb325SJeff Bonwick zio_t *
961a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
962e14bb325SJeff Bonwick {
963e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
964a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
965a3f829aeSBill Moore 	zio_t *pio;
966a3f829aeSBill Moore 
967a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9680a4e9518Sgw 
969a3f829aeSBill Moore 	/*
970a3f829aeSBill Moore 	 * Don't probe the probe.
971a3f829aeSBill Moore 	 */
972a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
973a3f829aeSBill Moore 		return (NULL);
974e14bb325SJeff Bonwick 
975a3f829aeSBill Moore 	/*
976a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
977a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
978a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
979a3f829aeSBill Moore 	 */
980a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
981e14bb325SJeff Bonwick 
982a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
983a3f829aeSBill Moore 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
984a3f829aeSBill Moore 
985a3f829aeSBill Moore 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
986a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9878956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
988a3f829aeSBill Moore 
989a3f829aeSBill Moore 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
990a3f829aeSBill Moore 			/*
991a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
992a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
993a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
994a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
995a3f829aeSBill Moore 			 * any zio looking at these values can assume that
996a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
997a3f829aeSBill Moore 			 * important because when a device has intermittent
998a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
999a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
1000a3f829aeSBill Moore 			 * the zio (EIO).
1001a3f829aeSBill Moore 			 *
1002a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
1003a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
1004a3f829aeSBill Moore 			 * principles.
1005a3f829aeSBill Moore 			 */
1006a3f829aeSBill Moore 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1007a3f829aeSBill Moore 			vd->vdev_cant_read = B_FALSE;
1008a3f829aeSBill Moore 			vd->vdev_cant_write = B_FALSE;
1009a3f829aeSBill Moore 		}
1010a3f829aeSBill Moore 
1011a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1012a3f829aeSBill Moore 		    vdev_probe_done, vps,
1013a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1014a3f829aeSBill Moore 
101598d1cbfeSGeorge Wilson 		/*
101698d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
101798d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
101898d1cbfeSGeorge Wilson 		 */
1019a3f829aeSBill Moore 		if (zio != NULL) {
1020a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1021a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1022a3f829aeSBill Moore 		}
1023e14bb325SJeff Bonwick 	}
1024e14bb325SJeff Bonwick 
1025a3f829aeSBill Moore 	if (zio != NULL)
1026a3f829aeSBill Moore 		zio_add_child(zio, pio);
1027e14bb325SJeff Bonwick 
1028a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1029e14bb325SJeff Bonwick 
1030a3f829aeSBill Moore 	if (vps == NULL) {
1031a3f829aeSBill Moore 		ASSERT(zio != NULL);
1032a3f829aeSBill Moore 		return (NULL);
1033a3f829aeSBill Moore 	}
1034e14bb325SJeff Bonwick 
1035e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1036a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1037e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1038f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2)),
1039f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1040e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1041e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1042e14bb325SJeff Bonwick 	}
1043e14bb325SJeff Bonwick 
1044a3f829aeSBill Moore 	if (zio == NULL)
1045a3f829aeSBill Moore 		return (pio);
1046a3f829aeSBill Moore 
1047a3f829aeSBill Moore 	zio_nowait(pio);
1048a3f829aeSBill Moore 	return (NULL);
10490a4e9518Sgw }
10500a4e9518Sgw 
1051f64c0e34SEric Taylor static void
1052f64c0e34SEric Taylor vdev_open_child(void *arg)
1053f64c0e34SEric Taylor {
1054f64c0e34SEric Taylor 	vdev_t *vd = arg;
1055f64c0e34SEric Taylor 
1056f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1057f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1058f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1059f64c0e34SEric Taylor }
1060f64c0e34SEric Taylor 
1061681d9761SEric Taylor boolean_t
1062681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1063681d9761SEric Taylor {
1064681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1065681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1066681d9761SEric Taylor 		return (B_TRUE);
1067681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1068681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1069681d9761SEric Taylor 			return (B_TRUE);
1070681d9761SEric Taylor 	return (B_FALSE);
1071681d9761SEric Taylor }
1072681d9761SEric Taylor 
1073f64c0e34SEric Taylor void
1074f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1075f64c0e34SEric Taylor {
1076f64c0e34SEric Taylor 	taskq_t *tq;
1077f64c0e34SEric Taylor 	int children = vd->vdev_children;
1078f64c0e34SEric Taylor 
1079681d9761SEric Taylor 	/*
1080681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1081681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1082681d9761SEric Taylor 	 * spa_namespace_lock
1083681d9761SEric Taylor 	 */
1084681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
1085681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1086681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1087681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
1088681d9761SEric Taylor 		return;
1089681d9761SEric Taylor 	}
1090f64c0e34SEric Taylor 	tq = taskq_create("vdev_open", children, minclsyspri,
1091f64c0e34SEric Taylor 	    children, children, TASKQ_PREPOPULATE);
1092f64c0e34SEric Taylor 
1093f64c0e34SEric Taylor 	for (int c = 0; c < children; c++)
1094f64c0e34SEric Taylor 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1095f64c0e34SEric Taylor 		    TQ_SLEEP) != NULL);
1096f64c0e34SEric Taylor 
1097f64c0e34SEric Taylor 	taskq_destroy(tq);
1098f64c0e34SEric Taylor }
1099f64c0e34SEric Taylor 
1100fa9e4066Sahrens /*
1101fa9e4066Sahrens  * Prepare a virtual device for access.
1102fa9e4066Sahrens  */
1103fa9e4066Sahrens int
1104fa9e4066Sahrens vdev_open(vdev_t *vd)
1105fa9e4066Sahrens {
11068ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1107fa9e4066Sahrens 	int error;
1108fa9e4066Sahrens 	uint64_t osize = 0;
1109fa9e4066Sahrens 	uint64_t asize, psize;
1110ecc2d604Sbonwick 	uint64_t ashift = 0;
1111fa9e4066Sahrens 
1112f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1113f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1114fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1115fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1116fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1117fa9e4066Sahrens 
1118fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1119e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1120e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1121573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1122fa9e4066Sahrens 
1123069f55e2SEric Schrock 	/*
1124069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1125069f55e2SEric Schrock 	 * faulted, bail out of the open.
1126069f55e2SEric Schrock 	 */
11273d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
11283d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1129069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1130069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
11313d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1132069f55e2SEric Schrock 		    vd->vdev_label_aux);
11333d7072f8Seschrock 		return (ENXIO);
11343d7072f8Seschrock 	} else if (vd->vdev_offline) {
1135fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1136ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1137fa9e4066Sahrens 		return (ENXIO);
1138fa9e4066Sahrens 	}
1139fa9e4066Sahrens 
1140fa9e4066Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1141fa9e4066Sahrens 
1142095bcd66SGeorge Wilson 	/*
1143095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1144095bcd66SGeorge Wilson 	 * the vdev on error.
1145095bcd66SGeorge Wilson 	 */
1146095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1147ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
11488956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1149ea8dc4b6Seschrock 
1150fa9e4066Sahrens 	if (error) {
11513d7072f8Seschrock 		if (vd->vdev_removed &&
11523d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
11533d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
11543d7072f8Seschrock 
1155ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1156fa9e4066Sahrens 		    vd->vdev_stat.vs_aux);
1157fa9e4066Sahrens 		return (error);
1158fa9e4066Sahrens 	}
1159fa9e4066Sahrens 
11603d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
11613d7072f8Seschrock 
1162096d22d4SEric Schrock 	/*
1163096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1164096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1165096d22d4SEric Schrock 	 */
1166096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1167096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1168096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1169096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1170096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1171096d22d4SEric Schrock 		    vd->vdev_label_aux);
1172096d22d4SEric Schrock 		return (ENXIO);
1173096d22d4SEric Schrock 	}
1174096d22d4SEric Schrock 
11753d7072f8Seschrock 	if (vd->vdev_degraded) {
11763d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
11773d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11783d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
11793d7072f8Seschrock 	} else {
1180069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
11813d7072f8Seschrock 	}
1182fa9e4066Sahrens 
118388ecc943SGeorge Wilson 	/*
118488ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
118588ecc943SGeorge Wilson 	 */
118688ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
118788ecc943SGeorge Wilson 		return (0);
118888ecc943SGeorge Wilson 
1189573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1190ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1191ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1192ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1193ea8dc4b6Seschrock 			break;
1194ea8dc4b6Seschrock 		}
1195573ca77eSGeorge Wilson 	}
1196fa9e4066Sahrens 
1197fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1198fa9e4066Sahrens 
1199fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1200fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1201ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1202ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1203fa9e4066Sahrens 			return (EOVERFLOW);
1204fa9e4066Sahrens 		}
1205fa9e4066Sahrens 		psize = osize;
1206fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1207fa9e4066Sahrens 	} else {
1208ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1209fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1210ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1211ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1212fa9e4066Sahrens 			return (EOVERFLOW);
1213fa9e4066Sahrens 		}
1214fa9e4066Sahrens 		psize = 0;
1215fa9e4066Sahrens 		asize = osize;
1216fa9e4066Sahrens 	}
1217fa9e4066Sahrens 
1218fa9e4066Sahrens 	vd->vdev_psize = psize;
1219fa9e4066Sahrens 
1220573ca77eSGeorge Wilson 	/*
1221573ca77eSGeorge Wilson 	 * Make sure the allocatable size hasn't shrunk.
1222573ca77eSGeorge Wilson 	 */
1223573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1224573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1225573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1226573ca77eSGeorge Wilson 		return (EINVAL);
1227573ca77eSGeorge Wilson 	}
1228573ca77eSGeorge Wilson 
1229fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1230fa9e4066Sahrens 		/*
1231fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
1232ecc2d604Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1233fa9e4066Sahrens 		 */
1234fa9e4066Sahrens 		vd->vdev_asize = asize;
1235ecc2d604Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1236fa9e4066Sahrens 	} else {
1237fa9e4066Sahrens 		/*
1238fa9e4066Sahrens 		 * Make sure the alignment requirement hasn't increased.
1239fa9e4066Sahrens 		 */
1240ecc2d604Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
1241ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1242ea8dc4b6Seschrock 			    VDEV_AUX_BAD_LABEL);
1243fa9e4066Sahrens 			return (EINVAL);
1244fa9e4066Sahrens 		}
1245573ca77eSGeorge Wilson 	}
1246fa9e4066Sahrens 
1247573ca77eSGeorge Wilson 	/*
1248573ca77eSGeorge Wilson 	 * If all children are healthy and the asize has increased,
1249573ca77eSGeorge Wilson 	 * then we've experienced dynamic LUN growth.  If automatic
1250573ca77eSGeorge Wilson 	 * expansion is enabled then use the additional space.
1251573ca77eSGeorge Wilson 	 */
1252573ca77eSGeorge Wilson 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1253573ca77eSGeorge Wilson 	    (vd->vdev_expanding || spa->spa_autoexpand))
1254573ca77eSGeorge Wilson 		vd->vdev_asize = asize;
1255fa9e4066Sahrens 
1256573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1257fa9e4066Sahrens 
12580a4e9518Sgw 	/*
12590a4e9518Sgw 	 * Ensure we can issue some IO before declaring the
12600a4e9518Sgw 	 * vdev open for business.
12610a4e9518Sgw 	 */
1262e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1263e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
126498d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
126598d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
12660a4e9518Sgw 		return (error);
12670a4e9518Sgw 	}
12680a4e9518Sgw 
1269088f3894Sahrens 	/*
1270088f3894Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
12718ad4d6ddSJeff Bonwick 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
12728ad4d6ddSJeff Bonwick 	 * since this would just restart the scrub we are already doing.
1273088f3894Sahrens 	 */
12748ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
12758ad4d6ddSJeff Bonwick 	    vdev_resilver_needed(vd, NULL, NULL))
12768ad4d6ddSJeff Bonwick 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1277088f3894Sahrens 
1278fa9e4066Sahrens 	return (0);
1279fa9e4066Sahrens }
1280fa9e4066Sahrens 
1281560e6e96Seschrock /*
1282560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
1283560e6e96Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
12843d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1285560e6e96Seschrock  *
1286560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1287560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1288560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1289560e6e96Seschrock  * will be updated but the function will return 0.
1290560e6e96Seschrock  */
1291560e6e96Seschrock int
1292560e6e96Seschrock vdev_validate(vdev_t *vd)
1293560e6e96Seschrock {
1294560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1295560e6e96Seschrock 	nvlist_t *label;
12961195e687SMark J Musante 	uint64_t guid = 0, top_guid;
1297560e6e96Seschrock 	uint64_t state;
1298560e6e96Seschrock 
1299573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1300560e6e96Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
13010bf246f5Smc 			return (EBADF);
1302560e6e96Seschrock 
1303b5989ec7Seschrock 	/*
1304b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1305b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1306b5989ec7Seschrock 	 * overwrite the previous state.
1307b5989ec7Seschrock 	 */
1308e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
13091195e687SMark J Musante 		uint64_t aux_guid = 0;
13101195e687SMark J Musante 		nvlist_t *nvl;
1311560e6e96Seschrock 
1312560e6e96Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
1313560e6e96Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1314560e6e96Seschrock 			    VDEV_AUX_BAD_LABEL);
1315560e6e96Seschrock 			return (0);
1316560e6e96Seschrock 		}
1317560e6e96Seschrock 
13181195e687SMark J Musante 		/*
13191195e687SMark J Musante 		 * Determine if this vdev has been split off into another
13201195e687SMark J Musante 		 * pool.  If so, then refuse to open it.
13211195e687SMark J Musante 		 */
13221195e687SMark J Musante 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
13231195e687SMark J Musante 		    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
13241195e687SMark J Musante 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13251195e687SMark J Musante 			    VDEV_AUX_SPLIT_POOL);
13261195e687SMark J Musante 			nvlist_free(label);
13271195e687SMark J Musante 			return (0);
13281195e687SMark J Musante 		}
13291195e687SMark J Musante 
1330560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
1331560e6e96Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
1332560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1333560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1334560e6e96Seschrock 			nvlist_free(label);
1335560e6e96Seschrock 			return (0);
1336560e6e96Seschrock 		}
1337560e6e96Seschrock 
13381195e687SMark J Musante 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
13391195e687SMark J Musante 		    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
13401195e687SMark J Musante 		    &aux_guid) != 0)
13411195e687SMark J Musante 			aux_guid = 0;
13421195e687SMark J Musante 
1343e14bb325SJeff Bonwick 		/*
1344e14bb325SJeff Bonwick 		 * If this vdev just became a top-level vdev because its
1345e14bb325SJeff Bonwick 		 * sibling was detached, it will have adopted the parent's
1346e14bb325SJeff Bonwick 		 * vdev guid -- but the label may or may not be on disk yet.
1347e14bb325SJeff Bonwick 		 * Fortunately, either version of the label will have the
1348e14bb325SJeff Bonwick 		 * same top guid, so if we're a top-level vdev, we can
1349e14bb325SJeff Bonwick 		 * safely compare to that instead.
13501195e687SMark J Musante 		 *
13511195e687SMark J Musante 		 * If we split this vdev off instead, then we also check the
13521195e687SMark J Musante 		 * original pool's guid.  We don't want to consider the vdev
13531195e687SMark J Musante 		 * corrupt if it is partway through a split operation.
1354e14bb325SJeff Bonwick 		 */
1355560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1356e14bb325SJeff Bonwick 		    &guid) != 0 ||
1357e14bb325SJeff Bonwick 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1358e14bb325SJeff Bonwick 		    &top_guid) != 0 ||
13591195e687SMark J Musante 		    ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1360e14bb325SJeff Bonwick 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1361560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1362560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1363560e6e96Seschrock 			nvlist_free(label);
1364560e6e96Seschrock 			return (0);
1365560e6e96Seschrock 		}
1366560e6e96Seschrock 
1367560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1368560e6e96Seschrock 		    &state) != 0) {
1369560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1370560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1371560e6e96Seschrock 			nvlist_free(label);
1372560e6e96Seschrock 			return (0);
1373560e6e96Seschrock 		}
1374560e6e96Seschrock 
1375560e6e96Seschrock 		nvlist_free(label);
1376560e6e96Seschrock 
1377bc758434SLin Ling 		/*
13784b964adaSGeorge Wilson 		 * If this is a verbatim import, no need to check the
1379bc758434SLin Ling 		 * state of the pool.
1380bc758434SLin Ling 		 */
13814b964adaSGeorge Wilson 		if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1382b16da2e2SGeorge Wilson 		    spa_load_state(spa) == SPA_LOAD_OPEN &&
1383bc758434SLin Ling 		    state != POOL_STATE_ACTIVE)
13840bf246f5Smc 			return (EBADF);
1385560e6e96Seschrock 
138651ece835Seschrock 		/*
138751ece835Seschrock 		 * If we were able to open and validate a vdev that was
138851ece835Seschrock 		 * previously marked permanently unavailable, clear that state
138951ece835Seschrock 		 * now.
139051ece835Seschrock 		 */
139151ece835Seschrock 		if (vd->vdev_not_present)
139251ece835Seschrock 			vd->vdev_not_present = 0;
139351ece835Seschrock 	}
1394560e6e96Seschrock 
1395560e6e96Seschrock 	return (0);
1396560e6e96Seschrock }
1397560e6e96Seschrock 
1398fa9e4066Sahrens /*
1399fa9e4066Sahrens  * Close a virtual device.
1400fa9e4066Sahrens  */
1401fa9e4066Sahrens void
1402fa9e4066Sahrens vdev_close(vdev_t *vd)
1403fa9e4066Sahrens {
14048ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1405095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
14068ad4d6ddSJeff Bonwick 
14078ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
14088ad4d6ddSJeff Bonwick 
14091195e687SMark J Musante 	/*
14101195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
14111195e687SMark J Musante 	 * going offline.
14121195e687SMark J Musante 	 */
1413095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
14141195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1415095bcd66SGeorge Wilson 
1416fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1417fa9e4066Sahrens 
14183d7072f8Seschrock 	vdev_cache_purge(vd);
1419fa9e4066Sahrens 
1420560e6e96Seschrock 	/*
1421573ca77eSGeorge Wilson 	 * We record the previous state before we close it, so that if we are
1422560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1423560e6e96Seschrock 	 * it's still faulted.
1424560e6e96Seschrock 	 */
1425560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
1426560e6e96Seschrock 
1427fa9e4066Sahrens 	if (vd->vdev_offline)
1428fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1429fa9e4066Sahrens 	else
1430fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
1431ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1432fa9e4066Sahrens }
1433fa9e4066Sahrens 
1434dcba9f3fSGeorge Wilson void
1435dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
1436dcba9f3fSGeorge Wilson {
1437dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1438dcba9f3fSGeorge Wilson 
1439dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1440dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1441dcba9f3fSGeorge Wilson 		return;
1442dcba9f3fSGeorge Wilson 
1443dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1444dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
1445dcba9f3fSGeorge Wilson 
1446dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1447dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
1448dcba9f3fSGeorge Wilson }
1449dcba9f3fSGeorge Wilson 
1450dcba9f3fSGeorge Wilson void
1451dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
1452dcba9f3fSGeorge Wilson {
1453dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1454dcba9f3fSGeorge Wilson 
1455dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1456dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1457dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
1458dcba9f3fSGeorge Wilson 
1459dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1460dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
1461dcba9f3fSGeorge Wilson }
1462dcba9f3fSGeorge Wilson 
1463095bcd66SGeorge Wilson /*
1464095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1465095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
1466095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1467095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
1468095bcd66SGeorge Wilson  */
1469fa9e4066Sahrens void
1470ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
1471fa9e4066Sahrens {
1472ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
1473fa9e4066Sahrens 
1474e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1475ea8dc4b6Seschrock 
14761195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
14771195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
1478fa9e4066Sahrens 	vdev_close(vd);
1479fa9e4066Sahrens 	(void) vdev_open(vd);
1480fa9e4066Sahrens 
148139c23413Seschrock 	/*
148239c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
148339c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
148439c23413Seschrock 	 * opened in response to vdev_reopen().
148539c23413Seschrock 	 */
1486c5904d13Seschrock 	if (vd->vdev_aux) {
1487c5904d13Seschrock 		(void) vdev_validate_aux(vd);
1488e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
14896809eb4eSEric Schrock 		    vd->vdev_aux == &spa->spa_l2cache &&
1490573ca77eSGeorge Wilson 		    !l2arc_vdev_present(vd))
1491573ca77eSGeorge Wilson 			l2arc_add_vdev(spa, vd);
1492c5904d13Seschrock 	} else {
1493c5904d13Seschrock 		(void) vdev_validate(vd);
1494c5904d13Seschrock 	}
149539c23413Seschrock 
1496fa9e4066Sahrens 	/*
14973d7072f8Seschrock 	 * Reassess parent vdev's health.
1498fa9e4066Sahrens 	 */
14993d7072f8Seschrock 	vdev_propagate_state(vd);
1500fa9e4066Sahrens }
1501fa9e4066Sahrens 
1502fa9e4066Sahrens int
150399653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1504fa9e4066Sahrens {
1505fa9e4066Sahrens 	int error;
1506fa9e4066Sahrens 
1507fa9e4066Sahrens 	/*
1508fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1509fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
1510fa9e4066Sahrens 	 * there are any components we can't open.
1511fa9e4066Sahrens 	 */
1512fa9e4066Sahrens 	error = vdev_open(vd);
1513fa9e4066Sahrens 
1514fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1515fa9e4066Sahrens 		vdev_close(vd);
1516fa9e4066Sahrens 		return (error ? error : ENXIO);
1517fa9e4066Sahrens 	}
1518fa9e4066Sahrens 
1519fa9e4066Sahrens 	/*
1520fa9e4066Sahrens 	 * Recursively initialize all labels.
1521fa9e4066Sahrens 	 */
152239c23413Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
152339c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1524fa9e4066Sahrens 		vdev_close(vd);
1525fa9e4066Sahrens 		return (error);
1526fa9e4066Sahrens 	}
1527fa9e4066Sahrens 
1528fa9e4066Sahrens 	return (0);
1529fa9e4066Sahrens }
1530fa9e4066Sahrens 
15310e34b6a7Sbonwick void
1532573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
1533fa9e4066Sahrens {
1534fa9e4066Sahrens 	/*
1535fa9e4066Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1536fa9e4066Sahrens 	 */
1537fa9e4066Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1538fa9e4066Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1539fa9e4066Sahrens }
1540fa9e4066Sahrens 
1541fa9e4066Sahrens void
1542ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1543fa9e4066Sahrens {
1544ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
154588ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
1546ecc2d604Sbonwick 	ASSERT(ISP2(flags));
1547f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
1548fa9e4066Sahrens 
1549ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
1550ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1551ecc2d604Sbonwick 
1552ecc2d604Sbonwick 	if (flags & VDD_DTL)
1553ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1554ecc2d604Sbonwick 
1555ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1556fa9e4066Sahrens }
1557fa9e4066Sahrens 
15588ad4d6ddSJeff Bonwick /*
15598ad4d6ddSJeff Bonwick  * DTLs.
15608ad4d6ddSJeff Bonwick  *
15618ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
15629fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
15638ad4d6ddSJeff Bonwick  *
15648ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
15658ad4d6ddSJeff Bonwick  *
15668ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
15678ad4d6ddSJeff Bonwick  *
15688ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
15698ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
15708ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
15718ad4d6ddSJeff Bonwick  *
15728ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
15738ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
15748ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
15758ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
15768ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
15778ad4d6ddSJeff Bonwick  *
15788ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
15798ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
15808ad4d6ddSJeff Bonwick  *
15818ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
15828ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
15838ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
15848ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
15858ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
15868ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
15878ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
15888ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
15898ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
15908ad4d6ddSJeff Bonwick  *
15918ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
15928ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
15938ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
15948ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
15958ad4d6ddSJeff Bonwick  */
1596fa9e4066Sahrens void
15978ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1598fa9e4066Sahrens {
15998ad4d6ddSJeff Bonwick 	space_map_t *sm = &vd->vdev_dtl[t];
16008ad4d6ddSJeff Bonwick 
16018ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
16028ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1603f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
16048ad4d6ddSJeff Bonwick 
1605fa9e4066Sahrens 	mutex_enter(sm->sm_lock);
1606fa9e4066Sahrens 	if (!space_map_contains(sm, txg, size))
1607fa9e4066Sahrens 		space_map_add(sm, txg, size);
1608fa9e4066Sahrens 	mutex_exit(sm->sm_lock);
1609fa9e4066Sahrens }
1610fa9e4066Sahrens 
16118ad4d6ddSJeff Bonwick boolean_t
16128ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1613fa9e4066Sahrens {
16148ad4d6ddSJeff Bonwick 	space_map_t *sm = &vd->vdev_dtl[t];
16158ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
1616fa9e4066Sahrens 
16178ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
16188ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1619fa9e4066Sahrens 
1620fa9e4066Sahrens 	mutex_enter(sm->sm_lock);
16218ad4d6ddSJeff Bonwick 	if (sm->sm_space != 0)
16228ad4d6ddSJeff Bonwick 		dirty = space_map_contains(sm, txg, size);
1623fa9e4066Sahrens 	mutex_exit(sm->sm_lock);
1624fa9e4066Sahrens 
1625fa9e4066Sahrens 	return (dirty);
1626fa9e4066Sahrens }
1627fa9e4066Sahrens 
16288ad4d6ddSJeff Bonwick boolean_t
16298ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
16308ad4d6ddSJeff Bonwick {
16318ad4d6ddSJeff Bonwick 	space_map_t *sm = &vd->vdev_dtl[t];
16328ad4d6ddSJeff Bonwick 	boolean_t empty;
16338ad4d6ddSJeff Bonwick 
16348ad4d6ddSJeff Bonwick 	mutex_enter(sm->sm_lock);
16358ad4d6ddSJeff Bonwick 	empty = (sm->sm_space == 0);
16368ad4d6ddSJeff Bonwick 	mutex_exit(sm->sm_lock);
16378ad4d6ddSJeff Bonwick 
16388ad4d6ddSJeff Bonwick 	return (empty);
16398ad4d6ddSJeff Bonwick }
16408ad4d6ddSJeff Bonwick 
1641fa9e4066Sahrens /*
1642fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
1643fa9e4066Sahrens  */
1644fa9e4066Sahrens void
1645fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1646fa9e4066Sahrens {
1647ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
16488ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
16498ad4d6ddSJeff Bonwick 	int minref;
1650fa9e4066Sahrens 
16518ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1652fa9e4066Sahrens 
16538ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
16548ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
16558ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
16568ad4d6ddSJeff Bonwick 
1657b24ab676SJeff Bonwick 	if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
16588ad4d6ddSJeff Bonwick 		return;
16598ad4d6ddSJeff Bonwick 
16608ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
16613f9d6ad7SLin Ling 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
16623f9d6ad7SLin Ling 
1663fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
1664088f3894Sahrens 		if (scrub_txg != 0 &&
16653f9d6ad7SLin Ling 		    (spa->spa_scrub_started ||
16663f9d6ad7SLin Ling 		    (scn && scn->scn_phys.scn_errors == 0))) {
1667088f3894Sahrens 			/*
1668088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
1669088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
1670088f3894Sahrens 			 * will be valid, so excise the old region and
1671088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
1672088f3894Sahrens 			 * dtl as-is if there was an error.
16738ad4d6ddSJeff Bonwick 			 *
16748ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
16758ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
16768ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
16778ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
16788ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
16798ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
16808ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
16818ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
16828ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
1683088f3894Sahrens 			 */
16848ad4d6ddSJeff Bonwick 			space_map_ref_create(&reftree);
16858ad4d6ddSJeff Bonwick 			space_map_ref_add_map(&reftree,
16868ad4d6ddSJeff Bonwick 			    &vd->vdev_dtl[DTL_MISSING], 1);
16878ad4d6ddSJeff Bonwick 			space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
16888ad4d6ddSJeff Bonwick 			space_map_ref_add_map(&reftree,
16898ad4d6ddSJeff Bonwick 			    &vd->vdev_dtl[DTL_SCRUB], 2);
16908ad4d6ddSJeff Bonwick 			space_map_ref_generate_map(&reftree,
16918ad4d6ddSJeff Bonwick 			    &vd->vdev_dtl[DTL_MISSING], 1);
16928ad4d6ddSJeff Bonwick 			space_map_ref_destroy(&reftree);
1693fa9e4066Sahrens 		}
16948ad4d6ddSJeff Bonwick 		space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
16958ad4d6ddSJeff Bonwick 		space_map_walk(&vd->vdev_dtl[DTL_MISSING],
16968ad4d6ddSJeff Bonwick 		    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1697fa9e4066Sahrens 		if (scrub_done)
16988ad4d6ddSJeff Bonwick 			space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
16998ad4d6ddSJeff Bonwick 		space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
17008ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
17018ad4d6ddSJeff Bonwick 			space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
17028ad4d6ddSJeff Bonwick 		else
17038ad4d6ddSJeff Bonwick 			space_map_walk(&vd->vdev_dtl[DTL_MISSING],
17048ad4d6ddSJeff Bonwick 			    space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1705fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
1706088f3894Sahrens 
1707ecc2d604Sbonwick 		if (txg != 0)
1708ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1709fa9e4066Sahrens 		return;
1710fa9e4066Sahrens 	}
1711fa9e4066Sahrens 
1712fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17138ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
171499bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
171599bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
17168ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
17178ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
17188ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
17198ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
17208ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
17218ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
17228ad4d6ddSJeff Bonwick 		else
17238ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
17248ad4d6ddSJeff Bonwick 		space_map_ref_create(&reftree);
17258ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
17268ad4d6ddSJeff Bonwick 			vdev_t *cvd = vd->vdev_child[c];
17278ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
172899bb17e2SEric Taylor 			space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1);
17298ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
17308ad4d6ddSJeff Bonwick 		}
17318ad4d6ddSJeff Bonwick 		space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
17328ad4d6ddSJeff Bonwick 		space_map_ref_destroy(&reftree);
1733fa9e4066Sahrens 	}
17348ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
1735fa9e4066Sahrens }
1736fa9e4066Sahrens 
1737fa9e4066Sahrens static int
1738fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
1739fa9e4066Sahrens {
1740fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
17418ad4d6ddSJeff Bonwick 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
1742ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1743fa9e4066Sahrens 	dmu_buf_t *db;
1744fa9e4066Sahrens 	int error;
1745fa9e4066Sahrens 
1746fa9e4066Sahrens 	ASSERT(vd->vdev_children == 0);
1747fa9e4066Sahrens 
1748fa9e4066Sahrens 	if (smo->smo_object == 0)
1749fa9e4066Sahrens 		return (0);
1750fa9e4066Sahrens 
175188ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
175288ecc943SGeorge Wilson 
1753ecc2d604Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1754ea8dc4b6Seschrock 		return (error);
1755ecc2d604Sbonwick 
17561934e92fSmaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
17571934e92fSmaybee 	bcopy(db->db_data, smo, sizeof (*smo));
1758ea8dc4b6Seschrock 	dmu_buf_rele(db, FTAG);
1759fa9e4066Sahrens 
1760fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17618ad4d6ddSJeff Bonwick 	error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
17628ad4d6ddSJeff Bonwick 	    NULL, SM_ALLOC, smo, mos);
1763fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1764fa9e4066Sahrens 
1765fa9e4066Sahrens 	return (error);
1766fa9e4066Sahrens }
1767fa9e4066Sahrens 
1768fa9e4066Sahrens void
1769fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1770fa9e4066Sahrens {
1771fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
17728ad4d6ddSJeff Bonwick 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
17738ad4d6ddSJeff Bonwick 	space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
1774ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1775fa9e4066Sahrens 	space_map_t smsync;
1776fa9e4066Sahrens 	kmutex_t smlock;
1777fa9e4066Sahrens 	dmu_buf_t *db;
1778fa9e4066Sahrens 	dmu_tx_t *tx;
1779fa9e4066Sahrens 
178088ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
178188ecc943SGeorge Wilson 
1782fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1783fa9e4066Sahrens 
1784fa9e4066Sahrens 	if (vd->vdev_detached) {
1785fa9e4066Sahrens 		if (smo->smo_object != 0) {
1786ecc2d604Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1787fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
1788fa9e4066Sahrens 			smo->smo_object = 0;
1789fa9e4066Sahrens 		}
1790fa9e4066Sahrens 		dmu_tx_commit(tx);
1791fa9e4066Sahrens 		return;
1792fa9e4066Sahrens 	}
1793fa9e4066Sahrens 
1794fa9e4066Sahrens 	if (smo->smo_object == 0) {
1795fa9e4066Sahrens 		ASSERT(smo->smo_objsize == 0);
1796fa9e4066Sahrens 		ASSERT(smo->smo_alloc == 0);
1797ecc2d604Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1798fa9e4066Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1799fa9e4066Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1800fa9e4066Sahrens 		ASSERT(smo->smo_object != 0);
1801fa9e4066Sahrens 		vdev_config_dirty(vd->vdev_top);
1802fa9e4066Sahrens 	}
1803fa9e4066Sahrens 
1804fa9e4066Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1805fa9e4066Sahrens 
1806fa9e4066Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1807fa9e4066Sahrens 	    &smlock);
1808fa9e4066Sahrens 
1809fa9e4066Sahrens 	mutex_enter(&smlock);
1810fa9e4066Sahrens 
1811fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
1812ecc2d604Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1813fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1814fa9e4066Sahrens 
1815ecc2d604Sbonwick 	space_map_truncate(smo, mos, tx);
1816ecc2d604Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1817fa9e4066Sahrens 
1818fa9e4066Sahrens 	space_map_destroy(&smsync);
1819fa9e4066Sahrens 
1820fa9e4066Sahrens 	mutex_exit(&smlock);
1821fa9e4066Sahrens 	mutex_destroy(&smlock);
1822fa9e4066Sahrens 
1823ecc2d604Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1824fa9e4066Sahrens 	dmu_buf_will_dirty(db, tx);
18251934e92fSmaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
18261934e92fSmaybee 	bcopy(smo, db->db_data, sizeof (*smo));
1827ea8dc4b6Seschrock 	dmu_buf_rele(db, FTAG);
1828fa9e4066Sahrens 
1829fa9e4066Sahrens 	dmu_tx_commit(tx);
1830fa9e4066Sahrens }
1831fa9e4066Sahrens 
18328ad4d6ddSJeff Bonwick /*
18338ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
18348ad4d6ddSJeff Bonwick  * without losing data.
18358ad4d6ddSJeff Bonwick  */
18368ad4d6ddSJeff Bonwick boolean_t
18378ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
18388ad4d6ddSJeff Bonwick {
18398ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
18408ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
18418ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
18428ad4d6ddSJeff Bonwick 	boolean_t required;
18438ad4d6ddSJeff Bonwick 
18448ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
18458ad4d6ddSJeff Bonwick 
18468ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
18478ad4d6ddSJeff Bonwick 		return (B_TRUE);
18488ad4d6ddSJeff Bonwick 
18498ad4d6ddSJeff Bonwick 	/*
18508ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
18518ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
18528ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
18538ad4d6ddSJeff Bonwick 	 */
18548ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
18558ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
18568ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
18578ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
18588ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
18598ad4d6ddSJeff Bonwick 
1860cb04b873SMark J Musante 	if (!required && zio_injection_enabled)
1861cb04b873SMark J Musante 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
1862cb04b873SMark J Musante 
18638ad4d6ddSJeff Bonwick 	return (required);
18648ad4d6ddSJeff Bonwick }
18658ad4d6ddSJeff Bonwick 
1866088f3894Sahrens /*
1867088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
1868088f3894Sahrens  */
1869088f3894Sahrens boolean_t
1870088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
1871088f3894Sahrens {
1872088f3894Sahrens 	boolean_t needed = B_FALSE;
1873088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
1874088f3894Sahrens 	uint64_t thismax = 0;
1875088f3894Sahrens 
1876088f3894Sahrens 	if (vd->vdev_children == 0) {
1877088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
18788ad4d6ddSJeff Bonwick 		if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
18798ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
1880088f3894Sahrens 			space_seg_t *ss;
1881088f3894Sahrens 
18828ad4d6ddSJeff Bonwick 			ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
1883088f3894Sahrens 			thismin = ss->ss_start - 1;
18848ad4d6ddSJeff Bonwick 			ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
1885088f3894Sahrens 			thismax = ss->ss_end;
1886088f3894Sahrens 			needed = B_TRUE;
1887088f3894Sahrens 		}
1888088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
1889088f3894Sahrens 	} else {
18908ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
1891088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
1892088f3894Sahrens 			uint64_t cmin, cmax;
1893088f3894Sahrens 
1894088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
1895088f3894Sahrens 				thismin = MIN(thismin, cmin);
1896088f3894Sahrens 				thismax = MAX(thismax, cmax);
1897088f3894Sahrens 				needed = B_TRUE;
1898088f3894Sahrens 			}
1899088f3894Sahrens 		}
1900088f3894Sahrens 	}
1901088f3894Sahrens 
1902088f3894Sahrens 	if (needed && minp) {
1903088f3894Sahrens 		*minp = thismin;
1904088f3894Sahrens 		*maxp = thismax;
1905088f3894Sahrens 	}
1906088f3894Sahrens 	return (needed);
1907088f3894Sahrens }
1908088f3894Sahrens 
1909560e6e96Seschrock void
1910ea8dc4b6Seschrock vdev_load(vdev_t *vd)
1911fa9e4066Sahrens {
1912fa9e4066Sahrens 	/*
1913fa9e4066Sahrens 	 * Recursively load all children.
1914fa9e4066Sahrens 	 */
19158ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1916560e6e96Seschrock 		vdev_load(vd->vdev_child[c]);
1917fa9e4066Sahrens 
1918fa9e4066Sahrens 	/*
19190e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1920fa9e4066Sahrens 	 */
192188ecc943SGeorge Wilson 	if (vd == vd->vdev_top && !vd->vdev_ishole &&
1922560e6e96Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1923560e6e96Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
1924560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1925560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1926fa9e4066Sahrens 
1927fa9e4066Sahrens 	/*
1928fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
1929fa9e4066Sahrens 	 */
1930560e6e96Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1931560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1932560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1933fa9e4066Sahrens }
1934fa9e4066Sahrens 
193599653d4eSeschrock /*
1936fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
1937fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
1938fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
1939fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
1940fa94a07fSbrendan  * repurposed to another pool.
194199653d4eSeschrock  */
194299653d4eSeschrock int
1943fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
194499653d4eSeschrock {
194599653d4eSeschrock 	nvlist_t *label;
194699653d4eSeschrock 	uint64_t guid, version;
194799653d4eSeschrock 	uint64_t state;
194899653d4eSeschrock 
1949e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
1950c5904d13Seschrock 		return (0);
1951c5904d13Seschrock 
195299653d4eSeschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
195399653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
195499653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
195599653d4eSeschrock 		return (-1);
195699653d4eSeschrock 	}
195799653d4eSeschrock 
195899653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1959e7437265Sahrens 	    version > SPA_VERSION ||
196099653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
196199653d4eSeschrock 	    guid != vd->vdev_guid ||
196299653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
196399653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
196499653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
196599653d4eSeschrock 		nvlist_free(label);
196699653d4eSeschrock 		return (-1);
196799653d4eSeschrock 	}
196899653d4eSeschrock 
196999653d4eSeschrock 	/*
197099653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
197199653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
197299653d4eSeschrock 	 */
197399653d4eSeschrock 	nvlist_free(label);
197499653d4eSeschrock 	return (0);
197599653d4eSeschrock }
197699653d4eSeschrock 
197788ecc943SGeorge Wilson void
197888ecc943SGeorge Wilson vdev_remove(vdev_t *vd, uint64_t txg)
197988ecc943SGeorge Wilson {
198088ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
198188ecc943SGeorge Wilson 	objset_t *mos = spa->spa_meta_objset;
198288ecc943SGeorge Wilson 	dmu_tx_t *tx;
198388ecc943SGeorge Wilson 
198488ecc943SGeorge Wilson 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
198588ecc943SGeorge Wilson 
198688ecc943SGeorge Wilson 	if (vd->vdev_dtl_smo.smo_object) {
198788ecc943SGeorge Wilson 		ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0);
198888ecc943SGeorge Wilson 		(void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
198988ecc943SGeorge Wilson 		vd->vdev_dtl_smo.smo_object = 0;
199088ecc943SGeorge Wilson 	}
199188ecc943SGeorge Wilson 
199288ecc943SGeorge Wilson 	if (vd->vdev_ms != NULL) {
199388ecc943SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
199488ecc943SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
199588ecc943SGeorge Wilson 
199688ecc943SGeorge Wilson 			if (msp == NULL || msp->ms_smo.smo_object == 0)
199788ecc943SGeorge Wilson 				continue;
199888ecc943SGeorge Wilson 
199988ecc943SGeorge Wilson 			ASSERT3U(msp->ms_smo.smo_alloc, ==, 0);
200088ecc943SGeorge Wilson 			(void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
200188ecc943SGeorge Wilson 			msp->ms_smo.smo_object = 0;
200288ecc943SGeorge Wilson 		}
200388ecc943SGeorge Wilson 	}
200488ecc943SGeorge Wilson 
200588ecc943SGeorge Wilson 	if (vd->vdev_ms_array) {
200688ecc943SGeorge Wilson 		(void) dmu_object_free(mos, vd->vdev_ms_array, tx);
200788ecc943SGeorge Wilson 		vd->vdev_ms_array = 0;
200888ecc943SGeorge Wilson 		vd->vdev_ms_shift = 0;
200988ecc943SGeorge Wilson 	}
201088ecc943SGeorge Wilson 	dmu_tx_commit(tx);
201188ecc943SGeorge Wilson }
201288ecc943SGeorge Wilson 
2013fa9e4066Sahrens void
2014fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
2015fa9e4066Sahrens {
2016fa9e4066Sahrens 	metaslab_t *msp;
201780eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2018fa9e4066Sahrens 
201988ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
202088ecc943SGeorge Wilson 
2021fa9e4066Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2022fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
202380eb36f2SGeorge Wilson 
202480eb36f2SGeorge Wilson 	if (reassess)
202580eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
2026fa9e4066Sahrens }
2027fa9e4066Sahrens 
2028fa9e4066Sahrens void
2029fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2030fa9e4066Sahrens {
2031fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2032fa9e4066Sahrens 	vdev_t *lvd;
2033fa9e4066Sahrens 	metaslab_t *msp;
2034ecc2d604Sbonwick 	dmu_tx_t *tx;
2035fa9e4066Sahrens 
203688ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
203788ecc943SGeorge Wilson 
2038ecc2d604Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2039ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
2040ecc2d604Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2041ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2042ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2043ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
2044ecc2d604Sbonwick 		vdev_config_dirty(vd);
2045ecc2d604Sbonwick 		dmu_tx_commit(tx);
2046ecc2d604Sbonwick 	}
2047fa9e4066Sahrens 
20483f9d6ad7SLin Ling 	/*
20493f9d6ad7SLin Ling 	 * Remove the metadata associated with this vdev once it's empty.
20503f9d6ad7SLin Ling 	 */
20513f9d6ad7SLin Ling 	if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
205288ecc943SGeorge Wilson 		vdev_remove(vd, txg);
205388ecc943SGeorge Wilson 
2054ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2055fa9e4066Sahrens 		metaslab_sync(msp, txg);
2056ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2057ecc2d604Sbonwick 	}
2058fa9e4066Sahrens 
2059fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2060fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
2061fa9e4066Sahrens 
2062fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2063fa9e4066Sahrens }
2064fa9e4066Sahrens 
2065fa9e4066Sahrens uint64_t
2066fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2067fa9e4066Sahrens {
2068fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2069fa9e4066Sahrens }
2070fa9e4066Sahrens 
20713d7072f8Seschrock /*
20723d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
20733d7072f8Seschrock  * not be opened, and no I/O is attempted.
20743d7072f8Seschrock  */
2075fa9e4066Sahrens int
2076069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2077fa9e4066Sahrens {
20784b964adaSGeorge Wilson 	vdev_t *vd, *tvd;
2079fa9e4066Sahrens 
20808f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
2081fa9e4066Sahrens 
2082c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2083e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2084e14bb325SJeff Bonwick 
20853d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2086e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2087fa9e4066Sahrens 
20884b964adaSGeorge Wilson 	tvd = vd->vdev_top;
20894b964adaSGeorge Wilson 
2090069f55e2SEric Schrock 	/*
2091069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
2092069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
2093069f55e2SEric Schrock 	 * were faulted.
2094069f55e2SEric Schrock 	 */
2095069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
2096069f55e2SEric Schrock 
20973d7072f8Seschrock 	/*
20983d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
20993d7072f8Seschrock 	 */
210098d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
21013d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
21023d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
2103069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
21043d7072f8Seschrock 
21053d7072f8Seschrock 	/*
2106c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
2107c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
21083d7072f8Seschrock 	 */
21094b964adaSGeorge Wilson 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
21103d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
21113d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
21123d7072f8Seschrock 
21133d7072f8Seschrock 		/*
21143d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
21153d7072f8Seschrock 		 * mark it degraded.
21163d7072f8Seschrock 		 */
21174b964adaSGeorge Wilson 		vdev_reopen(tvd);
21183d7072f8Seschrock 
2119069f55e2SEric Schrock 		if (vdev_readable(vd))
2120069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
21213d7072f8Seschrock 	}
21223d7072f8Seschrock 
2123e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
21243d7072f8Seschrock }
21253d7072f8Seschrock 
21263d7072f8Seschrock /*
21273d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
21283d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
21293d7072f8Seschrock  * as I/O is concerned.
21303d7072f8Seschrock  */
21313d7072f8Seschrock int
2132069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
21333d7072f8Seschrock {
2134c5904d13Seschrock 	vdev_t *vd;
21350a4e9518Sgw 
21368f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
21373d7072f8Seschrock 
2138c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2139e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2140e14bb325SJeff Bonwick 
21410e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2142e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
21430e34b6a7Sbonwick 
21443d7072f8Seschrock 	/*
21453d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
21463d7072f8Seschrock 	 */
2147e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
2148e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
21493d7072f8Seschrock 
21503d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
21513d7072f8Seschrock 	if (!vdev_is_dead(vd))
21523d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2153069f55e2SEric Schrock 		    aux);
21543d7072f8Seschrock 
2155e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
21563d7072f8Seschrock }
21573d7072f8Seschrock 
21583d7072f8Seschrock /*
21593d7072f8Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
21603d7072f8Seschrock  * any attached spare device should be detached when the device finishes
21613d7072f8Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
21623d7072f8Seschrock  * so no FMA events are generated if the device fails to open.
21633d7072f8Seschrock  */
21643d7072f8Seschrock int
2165e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
21663d7072f8Seschrock {
2167573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
21683d7072f8Seschrock 
21698f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
21703d7072f8Seschrock 
2171c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2172e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
21733d7072f8Seschrock 
21743d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2175e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2176fa9e4066Sahrens 
2177573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
2178fa9e4066Sahrens 	vd->vdev_offline = B_FALSE;
2179441d80aaSlling 	vd->vdev_tmpoffline = B_FALSE;
2180e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2181e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2182573ca77eSGeorge Wilson 
2183573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
2184573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2185573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2186573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2187573ca77eSGeorge Wilson 	}
2188573ca77eSGeorge Wilson 
2189573ca77eSGeorge Wilson 	vdev_reopen(tvd);
21903d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
21913d7072f8Seschrock 
2192573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2193573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2194573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
2195573ca77eSGeorge Wilson 	}
2196573ca77eSGeorge Wilson 
21973d7072f8Seschrock 	if (newstate)
21983d7072f8Seschrock 		*newstate = vd->vdev_state;
21993d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
22003d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
22013d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
22023d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
22033d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
2204fa9e4066Sahrens 
2205573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2206573ca77eSGeorge Wilson 
2207573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
2208573ca77eSGeorge Wilson 		if (vd->vdev_aux)
2209573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2210573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2211573ca77eSGeorge Wilson 	}
22128ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
2213fa9e4066Sahrens }
2214fa9e4066Sahrens 
2215a1521560SJeff Bonwick static int
2216a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2217fa9e4066Sahrens {
2218e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
22198f18d1faSGeorge Wilson 	int error = 0;
22208f18d1faSGeorge Wilson 	uint64_t generation;
22218f18d1faSGeorge Wilson 	metaslab_group_t *mg;
22220a4e9518Sgw 
22238f18d1faSGeorge Wilson top:
22248f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
2225fa9e4066Sahrens 
2226c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2227e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2228fa9e4066Sahrens 
22290e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2230e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
22310e34b6a7Sbonwick 
2232e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
22338f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
22348f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
2235e6ca193dSGeorge Wilson 
2236fa9e4066Sahrens 	/*
2237ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
2238fa9e4066Sahrens 	 */
2239ecc2d604Sbonwick 	if (!vd->vdev_offline) {
2240ecc2d604Sbonwick 		/*
22418ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
2242e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
2243e6ca193dSGeorge Wilson 		 * expendable.
2244ecc2d604Sbonwick 		 */
2245e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2246e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
2247e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2248fa9e4066Sahrens 
22498f18d1faSGeorge Wilson 		/*
2250b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
2251b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
2252b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
2253b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
22548f18d1faSGeorge Wilson 		 */
22558f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
22568f18d1faSGeorge Wilson 			/*
22578f18d1faSGeorge Wilson 			 * Prevent any future allocations.
22588f18d1faSGeorge Wilson 			 */
2259a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
22608f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
22618f18d1faSGeorge Wilson 
22621195e687SMark J Musante 			error = spa_offline_log(spa);
22638f18d1faSGeorge Wilson 
22648f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
22658f18d1faSGeorge Wilson 
22668f18d1faSGeorge Wilson 			/*
22678f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
22688f18d1faSGeorge Wilson 			 */
22698f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
2270a1521560SJeff Bonwick 				metaslab_group_activate(mg);
22718f18d1faSGeorge Wilson 				if (error)
22728f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
22738f18d1faSGeorge Wilson 					    vd, error));
22748f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
22758f18d1faSGeorge Wilson 				goto top;
22768f18d1faSGeorge Wilson 			}
22778f18d1faSGeorge Wilson 			ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0);
22788f18d1faSGeorge Wilson 		}
22798f18d1faSGeorge Wilson 
2280ecc2d604Sbonwick 		/*
2281ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
2282e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
2283e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
2284e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
2285ecc2d604Sbonwick 		 */
2286ecc2d604Sbonwick 		vd->vdev_offline = B_TRUE;
2287e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
2288e6ca193dSGeorge Wilson 
2289e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2290e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
2291ecc2d604Sbonwick 			vd->vdev_offline = B_FALSE;
2292e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
2293e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2294ecc2d604Sbonwick 		}
22958f18d1faSGeorge Wilson 
22968f18d1faSGeorge Wilson 		/*
22978f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
22988f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
22998f18d1faSGeorge Wilson 		 */
23008f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
2301a1521560SJeff Bonwick 			metaslab_group_activate(mg);
2302fa9e4066Sahrens 	}
2303fa9e4066Sahrens 
2304e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2305ecc2d604Sbonwick 
23068f18d1faSGeorge Wilson 	return (spa_vdev_state_exit(spa, vd, 0));
2307fa9e4066Sahrens }
2308fa9e4066Sahrens 
2309a1521560SJeff Bonwick int
2310a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2311a1521560SJeff Bonwick {
2312a1521560SJeff Bonwick 	int error;
2313a1521560SJeff Bonwick 
2314a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
2315a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
2316a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
2317a1521560SJeff Bonwick 
2318a1521560SJeff Bonwick 	return (error);
2319a1521560SJeff Bonwick }
2320a1521560SJeff Bonwick 
2321ea8dc4b6Seschrock /*
2322ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
2323ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
2324ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
2325ea8dc4b6Seschrock  */
2326ea8dc4b6Seschrock void
2327e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
2328fa9e4066Sahrens {
2329e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2330e14bb325SJeff Bonwick 
2331e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2332fa9e4066Sahrens 
2333ea8dc4b6Seschrock 	if (vd == NULL)
2334e14bb325SJeff Bonwick 		vd = rvd;
2335fa9e4066Sahrens 
2336ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
2337ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
2338ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2339fa9e4066Sahrens 
2340e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2341e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
23423d7072f8Seschrock 
23433d7072f8Seschrock 	/*
23448a79c1b5Sek 	 * If we're in the FAULTED state or have experienced failed I/O, then
23458a79c1b5Sek 	 * clear the persistent state and attempt to reopen the device.  We
23468a79c1b5Sek 	 * also mark the vdev config dirty, so that the new faulted state is
23478a79c1b5Sek 	 * written out to disk.
23483d7072f8Seschrock 	 */
2349e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
2350e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
23518a79c1b5Sek 
2352096d22d4SEric Schrock 		/*
2353096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
2354096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
2355096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
2356096d22d4SEric Schrock 		 */
2357096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
2358096d22d4SEric Schrock 
23594b964adaSGeorge Wilson 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2360e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
2361e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
2362e14bb325SJeff Bonwick 
2363f9af39baSGeorge Wilson 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
23643d7072f8Seschrock 
2365096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
2366096d22d4SEric Schrock 
2367f9af39baSGeorge Wilson 		if (vd != rvd && vdev_writeable(vd->vdev_top))
2368e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
2369e14bb325SJeff Bonwick 
2370e14bb325SJeff Bonwick 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2371bb8b5132Sek 			spa_async_request(spa, SPA_ASYNC_RESILVER);
23723d7072f8Seschrock 
23733d7072f8Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
23743d7072f8Seschrock 	}
2375096d22d4SEric Schrock 
2376096d22d4SEric Schrock 	/*
2377096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
2378096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
2379096d22d4SEric Schrock 	 * done in response to the FMA fault.
2380096d22d4SEric Schrock 	 */
2381096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2382096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2383096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
2384096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
2385fa9e4066Sahrens }
2386fa9e4066Sahrens 
2387e14bb325SJeff Bonwick boolean_t
2388e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd)
23890a4e9518Sgw {
239088ecc943SGeorge Wilson 	/*
239188ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
239288ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
239388ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
239488ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
239588ecc943SGeorge Wilson 	 * before issuing I/O to them.
239688ecc943SGeorge Wilson 	 */
239788ecc943SGeorge Wilson 	return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
239888ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
23990a4e9518Sgw }
24000a4e9518Sgw 
2401e14bb325SJeff Bonwick boolean_t
2402e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
24030a4e9518Sgw {
2404e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
24050a4e9518Sgw }
24060a4e9518Sgw 
2407e14bb325SJeff Bonwick boolean_t
2408e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
2409fa9e4066Sahrens {
2410e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2411fa9e4066Sahrens }
2412fa9e4066Sahrens 
2413a31e6787SGeorge Wilson boolean_t
2414a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
2415a31e6787SGeorge Wilson {
24168ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
24178ad4d6ddSJeff Bonwick 
2418a31e6787SGeorge Wilson 	/*
24198ad4d6ddSJeff Bonwick 	 * We currently allow allocations from vdevs which may be in the
2420a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2421a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
24228ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
24238ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
24248ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
2425a31e6787SGeorge Wilson 	 */
24268ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
24273f9d6ad7SLin Ling 	    !vd->vdev_cant_write && !vd->vdev_ishole);
2428a31e6787SGeorge Wilson }
2429a31e6787SGeorge Wilson 
2430e14bb325SJeff Bonwick boolean_t
2431e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
2432fa9e4066Sahrens {
2433e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
2434fa9e4066Sahrens 
2435e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2436e14bb325SJeff Bonwick 		return (B_FALSE);
2437fa9e4066Sahrens 
2438e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
2439e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
2440fa9e4066Sahrens 
2441e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
2442e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
2443fa9e4066Sahrens 
2444e14bb325SJeff Bonwick 	return (B_TRUE);
2445fa9e4066Sahrens }
2446fa9e4066Sahrens 
2447fa9e4066Sahrens /*
2448fa9e4066Sahrens  * Get statistics for the given vdev.
2449fa9e4066Sahrens  */
2450fa9e4066Sahrens void
2451fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2452fa9e4066Sahrens {
2453fa9e4066Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2454fa9e4066Sahrens 
2455fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2456fa9e4066Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2457fa9e4066Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2458fa9e4066Sahrens 	vs->vs_state = vd->vdev_state;
2459573ca77eSGeorge Wilson 	vs->vs_rsize = vdev_get_min_asize(vd);
2460573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
2461573ca77eSGeorge Wilson 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2462fa9e4066Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2463fa9e4066Sahrens 
2464fa9e4066Sahrens 	/*
2465fa9e4066Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2466fa9e4066Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2467fa9e4066Sahrens 	 */
2468fa9e4066Sahrens 	if (vd == rvd) {
2469e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2470fa9e4066Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2471fa9e4066Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2472fa9e4066Sahrens 
2473fa9e4066Sahrens 			mutex_enter(&vd->vdev_stat_lock);
2474e14bb325SJeff Bonwick 			for (int t = 0; t < ZIO_TYPES; t++) {
2475fa9e4066Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2476fa9e4066Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2477fa9e4066Sahrens 			}
24783f9d6ad7SLin Ling 			cvs->vs_scan_removing = cvd->vdev_removing;
2479fa9e4066Sahrens 			mutex_exit(&vd->vdev_stat_lock);
2480fa9e4066Sahrens 		}
2481fa9e4066Sahrens 	}
2482fa9e4066Sahrens }
2483fa9e4066Sahrens 
2484fa94a07fSbrendan void
2485fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
2486fa94a07fSbrendan {
2487fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
2488fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
2489fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
2490fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
2491fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
2492fa94a07fSbrendan }
2493fa94a07fSbrendan 
24943f9d6ad7SLin Ling void
24953f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd)
24963f9d6ad7SLin Ling {
24973f9d6ad7SLin Ling 	vdev_stat_t *vs = &vd->vdev_stat;
24983f9d6ad7SLin Ling 
24993f9d6ad7SLin Ling 	for (int c = 0; c < vd->vdev_children; c++)
25003f9d6ad7SLin Ling 		vdev_scan_stat_init(vd->vdev_child[c]);
25013f9d6ad7SLin Ling 
25023f9d6ad7SLin Ling 	mutex_enter(&vd->vdev_stat_lock);
25033f9d6ad7SLin Ling 	vs->vs_scan_processed = 0;
25043f9d6ad7SLin Ling 	mutex_exit(&vd->vdev_stat_lock);
25053f9d6ad7SLin Ling }
25063f9d6ad7SLin Ling 
2507fa9e4066Sahrens void
2508e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
2509fa9e4066Sahrens {
25108ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
25118ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2512e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2513fa9e4066Sahrens 	vdev_t *pvd;
2514fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
2515fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2516fa9e4066Sahrens 	zio_type_t type = zio->io_type;
2517fa9e4066Sahrens 	int flags = zio->io_flags;
2518fa9e4066Sahrens 
2519e14bb325SJeff Bonwick 	/*
2520e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
2521e14bb325SJeff Bonwick 	 */
2522e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
2523e14bb325SJeff Bonwick 		return;
2524e14bb325SJeff Bonwick 
2525fa9e4066Sahrens 	if (zio->io_error == 0) {
2526e14bb325SJeff Bonwick 		/*
2527e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
2528e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
2529e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
2530e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
2531e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
2532e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
2533e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
2534e14bb325SJeff Bonwick 		 *
2535e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
2536e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
2537e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
2538e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
2539e14bb325SJeff Bonwick 		 */
2540e14bb325SJeff Bonwick 		if (vd == rvd)
2541e14bb325SJeff Bonwick 			return;
2542e14bb325SJeff Bonwick 
2543e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
25448ad4d6ddSJeff Bonwick 
25458ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
25468ad4d6ddSJeff Bonwick 			return;
25478ad4d6ddSJeff Bonwick 
25488ad4d6ddSJeff Bonwick 		mutex_enter(&vd->vdev_stat_lock);
25498ad4d6ddSJeff Bonwick 
2550e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
255144ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
25523f9d6ad7SLin Ling 				dsl_scan_phys_t *scn_phys =
25533f9d6ad7SLin Ling 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
25543f9d6ad7SLin Ling 				uint64_t *processed = &scn_phys->scn_processed;
25553f9d6ad7SLin Ling 
25563f9d6ad7SLin Ling 				/* XXX cleanup? */
25573f9d6ad7SLin Ling 				if (vd->vdev_ops->vdev_op_leaf)
25583f9d6ad7SLin Ling 					atomic_add_64(processed, psize);
25593f9d6ad7SLin Ling 				vs->vs_scan_processed += psize;
25603f9d6ad7SLin Ling 			}
25613f9d6ad7SLin Ling 
25628ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
2563e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
2564fa9e4066Sahrens 		}
25658ad4d6ddSJeff Bonwick 
25668ad4d6ddSJeff Bonwick 		vs->vs_ops[type]++;
25678ad4d6ddSJeff Bonwick 		vs->vs_bytes[type] += psize;
25688ad4d6ddSJeff Bonwick 
25698ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
2570fa9e4066Sahrens 		return;
2571fa9e4066Sahrens 	}
2572fa9e4066Sahrens 
2573fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2574fa9e4066Sahrens 		return;
2575fa9e4066Sahrens 
25768956713aSEric Schrock 	/*
25778956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
25788956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
25798956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
25808956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
25818956713aSEric Schrock 	 */
25828956713aSEric Schrock 	if (zio->io_error == EIO &&
25838956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
25848956713aSEric Schrock 		return;
25858956713aSEric Schrock 
25868f18d1faSGeorge Wilson 	/*
25878f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
25888f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
25898f18d1faSGeorge Wilson 	 * errors.
25908f18d1faSGeorge Wilson 	 */
25918f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
25928f18d1faSGeorge Wilson 		return;
25938f18d1faSGeorge Wilson 
2594e14bb325SJeff Bonwick 	mutex_enter(&vd->vdev_stat_lock);
2595b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2596e14bb325SJeff Bonwick 		if (zio->io_error == ECKSUM)
2597e14bb325SJeff Bonwick 			vs->vs_checksum_errors++;
2598e14bb325SJeff Bonwick 		else
2599e14bb325SJeff Bonwick 			vs->vs_read_errors++;
2600fa9e4066Sahrens 	}
2601b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2602e14bb325SJeff Bonwick 		vs->vs_write_errors++;
2603e14bb325SJeff Bonwick 	mutex_exit(&vd->vdev_stat_lock);
2604fa9e4066Sahrens 
26058ad4d6ddSJeff Bonwick 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
26068ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
260744ecc532SGeorge Wilson 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
2608b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
26098ad4d6ddSJeff Bonwick 		/*
2610b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
2611b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
2612b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
2613b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
2614b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
2615b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
2616b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
2617b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
26188ad4d6ddSJeff Bonwick 		 *
26198ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
26208ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
26218ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
26228ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
26238ad4d6ddSJeff Bonwick 		 */
26248ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
26258ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
262644ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
26278ad4d6ddSJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
26288ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
26298ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2630b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
2631b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
2632b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2633b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
26348ad4d6ddSJeff Bonwick 			}
2635b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
26368ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2637fa9e4066Sahrens 				return;
26388ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
26398ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
26408ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2641fa9e4066Sahrens 		}
26428ad4d6ddSJeff Bonwick 		if (vd != rvd)
26438ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2644fa9e4066Sahrens 	}
2645fa9e4066Sahrens }
2646fa9e4066Sahrens 
2647fa9e4066Sahrens /*
2648b24ab676SJeff Bonwick  * Update the in-core space usage stats for this vdev, its metaslab class,
2649b24ab676SJeff Bonwick  * and the root vdev.
2650fa9e4066Sahrens  */
2651fa9e4066Sahrens void
2652b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2653b24ab676SJeff Bonwick     int64_t space_delta)
2654fa9e4066Sahrens {
265599653d4eSeschrock 	int64_t dspace_delta = space_delta;
26568654d025Sperrin 	spa_t *spa = vd->vdev_spa;
26578654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
2658b24ab676SJeff Bonwick 	metaslab_group_t *mg = vd->vdev_mg;
2659b24ab676SJeff Bonwick 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2660fa9e4066Sahrens 
26618654d025Sperrin 	ASSERT(vd == vd->vdev_top);
266299653d4eSeschrock 
26638654d025Sperrin 	/*
26648654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
26658654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
26668654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
26678654d025Sperrin 	 * childrens', thus not accurate enough for us.
26688654d025Sperrin 	 */
26698654d025Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2670e6ca193dSGeorge Wilson 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
26718654d025Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
26728654d025Sperrin 	    vd->vdev_deflate_ratio;
26738654d025Sperrin 
26748654d025Sperrin 	mutex_enter(&vd->vdev_stat_lock);
26758654d025Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
2676b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
26778654d025Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
26788654d025Sperrin 	mutex_exit(&vd->vdev_stat_lock);
26798654d025Sperrin 
2680b24ab676SJeff Bonwick 	if (mc == spa_normal_class(spa)) {
2681fa94a07fSbrendan 		mutex_enter(&rvd->vdev_stat_lock);
2682fa94a07fSbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
2683b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
2684fa94a07fSbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
2685fa94a07fSbrendan 		mutex_exit(&rvd->vdev_stat_lock);
2686fa94a07fSbrendan 	}
2687b24ab676SJeff Bonwick 
2688b24ab676SJeff Bonwick 	if (mc != NULL) {
2689b24ab676SJeff Bonwick 		ASSERT(rvd == vd->vdev_parent);
2690b24ab676SJeff Bonwick 		ASSERT(vd->vdev_ms_count != 0);
2691b24ab676SJeff Bonwick 
2692b24ab676SJeff Bonwick 		metaslab_class_space_update(mc,
2693b24ab676SJeff Bonwick 		    alloc_delta, defer_delta, space_delta, dspace_delta);
2694b24ab676SJeff Bonwick 	}
2695fa9e4066Sahrens }
2696fa9e4066Sahrens 
2697fa9e4066Sahrens /*
2698fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2699fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
2700fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2701fa9e4066Sahrens  */
2702fa9e4066Sahrens void
2703fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
2704fa9e4066Sahrens {
2705fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2706fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2707fa9e4066Sahrens 	int c;
2708fa9e4066Sahrens 
2709f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
2710f9af39baSGeorge Wilson 
2711c5904d13Seschrock 	/*
27126809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
27136809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
2714c5904d13Seschrock 	 */
2715c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
2716c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
2717c5904d13Seschrock 		nvlist_t **aux;
2718c5904d13Seschrock 		uint_t naux;
2719c5904d13Seschrock 
2720c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
2721c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
2722c5904d13Seschrock 				break;
2723c5904d13Seschrock 		}
2724c5904d13Seschrock 
2725e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
2726e14bb325SJeff Bonwick 			/*
2727e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
2728e14bb325SJeff Bonwick 			 */
2729e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
2730e14bb325SJeff Bonwick 			return;
2731e14bb325SJeff Bonwick 		}
2732e14bb325SJeff Bonwick 
2733c5904d13Seschrock 		sav->sav_sync = B_TRUE;
2734c5904d13Seschrock 
27356809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
27366809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
27376809eb4eSEric Schrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
27386809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
27396809eb4eSEric Schrock 		}
2740c5904d13Seschrock 
2741c5904d13Seschrock 		ASSERT(c < naux);
2742c5904d13Seschrock 
2743c5904d13Seschrock 		/*
2744c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
2745c5904d13Seschrock 		 * sketchy, but it will work.
2746c5904d13Seschrock 		 */
2747c5904d13Seschrock 		nvlist_free(aux[c]);
27483f9d6ad7SLin Ling 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
2749c5904d13Seschrock 
2750c5904d13Seschrock 		return;
2751c5904d13Seschrock 	}
2752c5904d13Seschrock 
27535dabedeeSbonwick 	/*
2754e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
2755e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
2756e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
27575dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
27585dabedeeSbonwick 	 */
2759e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2760e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2761e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
27625dabedeeSbonwick 
2763fa9e4066Sahrens 	if (vd == rvd) {
2764fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2765fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2766fa9e4066Sahrens 	} else {
2767fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
2768fa9e4066Sahrens 
276988ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
277088ecc943SGeorge Wilson 		    !vd->vdev_ishole)
2771e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
2772fa9e4066Sahrens 	}
2773fa9e4066Sahrens }
2774fa9e4066Sahrens 
2775fa9e4066Sahrens void
2776fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
2777fa9e4066Sahrens {
27785dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
27795dabedeeSbonwick 
2780e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2781e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2782e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
27835dabedeeSbonwick 
2784e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
2785e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
2786e14bb325SJeff Bonwick }
2787e14bb325SJeff Bonwick 
2788e14bb325SJeff Bonwick /*
2789e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
2790e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
2791e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
2792e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
2793e14bb325SJeff Bonwick  */
2794e14bb325SJeff Bonwick void
2795e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
2796e14bb325SJeff Bonwick {
2797e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
2798e14bb325SJeff Bonwick 
2799f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
2800e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
2801e14bb325SJeff Bonwick 
2802e14bb325SJeff Bonwick 	/*
2803e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
2804e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
2805e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
2806e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
2807e14bb325SJeff Bonwick 	 */
2808e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2809e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2810e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2811e14bb325SJeff Bonwick 
2812b24ab676SJeff Bonwick 	if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
2813e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
2814e14bb325SJeff Bonwick }
2815e14bb325SJeff Bonwick 
2816e14bb325SJeff Bonwick void
2817e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
2818e14bb325SJeff Bonwick {
2819e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
2820e14bb325SJeff Bonwick 
2821e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2822e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2823e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2824e14bb325SJeff Bonwick 
2825e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
2826e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
2827fa9e4066Sahrens }
2828fa9e4066Sahrens 
282932b87932Sek /*
283032b87932Sek  * Propagate vdev state up from children to parent.
283132b87932Sek  */
283244cd46caSbillm void
283344cd46caSbillm vdev_propagate_state(vdev_t *vd)
283444cd46caSbillm {
28358ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
28368ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
283744cd46caSbillm 	int degraded = 0, faulted = 0;
283844cd46caSbillm 	int corrupted = 0;
283944cd46caSbillm 	vdev_t *child;
284044cd46caSbillm 
28413d7072f8Seschrock 	if (vd->vdev_children > 0) {
2842573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
28433d7072f8Seschrock 			child = vd->vdev_child[c];
284451ece835Seschrock 
284588ecc943SGeorge Wilson 			/*
284688ecc943SGeorge Wilson 			 * Don't factor holes into the decision.
284788ecc943SGeorge Wilson 			 */
284888ecc943SGeorge Wilson 			if (child->vdev_ishole)
284988ecc943SGeorge Wilson 				continue;
285088ecc943SGeorge Wilson 
2851e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
28528ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
285351ece835Seschrock 				/*
285451ece835Seschrock 				 * Root special: if there is a top-level log
285551ece835Seschrock 				 * device, treat the root vdev as if it were
285651ece835Seschrock 				 * degraded.
285751ece835Seschrock 				 */
285851ece835Seschrock 				if (child->vdev_islog && vd == rvd)
285951ece835Seschrock 					degraded++;
286051ece835Seschrock 				else
286151ece835Seschrock 					faulted++;
286251ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
28633d7072f8Seschrock 				degraded++;
286451ece835Seschrock 			}
286544cd46caSbillm 
28663d7072f8Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
28673d7072f8Seschrock 				corrupted++;
28683d7072f8Seschrock 		}
286944cd46caSbillm 
28703d7072f8Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
28713d7072f8Seschrock 
28723d7072f8Seschrock 		/*
2873e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
28743d7072f8Seschrock 		 * opened due to corrupted metadata, then propagate the root
28753d7072f8Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
28763d7072f8Seschrock 		 * replicas'.
28773d7072f8Seschrock 		 */
28783d7072f8Seschrock 		if (corrupted && vd == rvd &&
28793d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
28803d7072f8Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
28813d7072f8Seschrock 			    VDEV_AUX_CORRUPT_DATA);
28823d7072f8Seschrock 	}
28833d7072f8Seschrock 
288451ece835Seschrock 	if (vd->vdev_parent)
28853d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
288644cd46caSbillm }
288744cd46caSbillm 
2888fa9e4066Sahrens /*
2889ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
2890ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
2891ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
2892ea8dc4b6Seschrock  *
2893ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
2894ea8dc4b6Seschrock  * generated.
2895fa9e4066Sahrens  */
2896fa9e4066Sahrens void
2897ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2898fa9e4066Sahrens {
2899560e6e96Seschrock 	uint64_t save_state;
2900c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
2901ea8dc4b6Seschrock 
2902ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
2903ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
2904fa9e4066Sahrens 		return;
2905ea8dc4b6Seschrock 	}
2906ea8dc4b6Seschrock 
2907560e6e96Seschrock 	save_state = vd->vdev_state;
2908fa9e4066Sahrens 
2909fa9e4066Sahrens 	vd->vdev_state = state;
2910fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
2911fa9e4066Sahrens 
29123d7072f8Seschrock 	/*
29133d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
291498d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
291598d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
291698d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
291798d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
291898d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
291998d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
292098d1cbfeSGeorge Wilson 	 * children.
292198d1cbfeSGeorge Wilson 	 */
292298d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
292398d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
29243d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
29253d7072f8Seschrock 
2926069f55e2SEric Schrock 	/*
2927069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
2928069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
2929069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
2930069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
2931069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
2932069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
2933069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
2934069f55e2SEric Schrock 	 */
2935069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
2936069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
2937069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
2938069f55e2SEric Schrock 
29393d7072f8Seschrock 	if (vd->vdev_removed &&
29403d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
29413d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
29423d7072f8Seschrock 		/*
29433d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
29443d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
29453d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
29463d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
29473d7072f8Seschrock 		 * it is one of our special test online cases, which is only
29483d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
29493d7072f8Seschrock 		 * fault.
29503d7072f8Seschrock 		 */
29513d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
29523d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
29533d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
29543d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
29553d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
2956ea8dc4b6Seschrock 		/*
2957cb04b873SMark J Musante 		 * If we fail to open a vdev during an import or recovery, we
2958cb04b873SMark J Musante 		 * mark it as "not available", which signifies that it was
2959cb04b873SMark J Musante 		 * never there to begin with.  Failure to open such a device
2960cb04b873SMark J Musante 		 * is not considered an error.
2961ea8dc4b6Seschrock 		 */
2962cb04b873SMark J Musante 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
2963cb04b873SMark J Musante 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
2964560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
2965560e6e96Seschrock 			vd->vdev_not_present = 1;
2966560e6e96Seschrock 
2967560e6e96Seschrock 		/*
2968560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
2969560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
2970560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
2971560e6e96Seschrock 		 * want to post the ereport if the device was already in the
2972560e6e96Seschrock 		 * CANT_OPEN state beforehand.
29733d7072f8Seschrock 		 *
29743d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
29753d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
29763d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
29773d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
29783d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
29793d7072f8Seschrock 		 * about to replace the device, or attempt an online with
29803d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
2981560e6e96Seschrock 		 */
29823d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
29833d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
2984c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
2985ea8dc4b6Seschrock 			const char *class;
2986ea8dc4b6Seschrock 
2987ea8dc4b6Seschrock 			switch (aux) {
2988ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
2989ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
2990ea8dc4b6Seschrock 				break;
2991ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
2992ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
2993ea8dc4b6Seschrock 				break;
2994ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
2995ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
2996ea8dc4b6Seschrock 				break;
2997ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
2998ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
2999ea8dc4b6Seschrock 				break;
3000ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
3001ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3002ea8dc4b6Seschrock 				break;
3003ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
3004ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3005ea8dc4b6Seschrock 				break;
3006ea8dc4b6Seschrock 			default:
3007ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3008ea8dc4b6Seschrock 			}
3009ea8dc4b6Seschrock 
3010c5904d13Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3011ea8dc4b6Seschrock 		}
3012ea8dc4b6Seschrock 
30133d7072f8Seschrock 		/* Erase any notion of persistent removed state */
30143d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
30153d7072f8Seschrock 	} else {
30163d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
30173d7072f8Seschrock 	}
3018ea8dc4b6Seschrock 
30198b33d774STim Haley 	if (!isopen && vd->vdev_parent)
30208b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
3021fa9e4066Sahrens }
302215e6edf1Sgw 
302315e6edf1Sgw /*
302415e6edf1Sgw  * Check the vdev configuration to ensure that it's capable of supporting
302515e6edf1Sgw  * a root pool. Currently, we do not support RAID-Z or partial configuration.
302615e6edf1Sgw  * In addition, only a single top-level vdev is allowed and none of the leaves
302715e6edf1Sgw  * can be wholedisks.
302815e6edf1Sgw  */
302915e6edf1Sgw boolean_t
303015e6edf1Sgw vdev_is_bootable(vdev_t *vd)
303115e6edf1Sgw {
303215e6edf1Sgw 	if (!vd->vdev_ops->vdev_op_leaf) {
303315e6edf1Sgw 		char *vdev_type = vd->vdev_ops->vdev_op_type;
303415e6edf1Sgw 
303515e6edf1Sgw 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
303615e6edf1Sgw 		    vd->vdev_children > 1) {
303715e6edf1Sgw 			return (B_FALSE);
303815e6edf1Sgw 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
303915e6edf1Sgw 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
304015e6edf1Sgw 			return (B_FALSE);
304115e6edf1Sgw 		}
304215e6edf1Sgw 	} else if (vd->vdev_wholedisk == 1) {
304315e6edf1Sgw 		return (B_FALSE);
304415e6edf1Sgw 	}
304515e6edf1Sgw 
3046573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
304715e6edf1Sgw 		if (!vdev_is_bootable(vd->vdev_child[c]))
304815e6edf1Sgw 			return (B_FALSE);
304915e6edf1Sgw 	}
305015e6edf1Sgw 	return (B_TRUE);
305115e6edf1Sgw }
3052e6ca193dSGeorge Wilson 
305388ecc943SGeorge Wilson /*
305488ecc943SGeorge Wilson  * Load the state from the original vdev tree (ovd) which
305588ecc943SGeorge Wilson  * we've retrieved from the MOS config object. If the original
30564b964adaSGeorge Wilson  * vdev was offline or faulted then we transfer that state to the
30574b964adaSGeorge Wilson  * device in the current vdev tree (nvd).
305888ecc943SGeorge Wilson  */
3059e6ca193dSGeorge Wilson void
306088ecc943SGeorge Wilson vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3061e6ca193dSGeorge Wilson {
306288ecc943SGeorge Wilson 	spa_t *spa = nvd->vdev_spa;
3063e6ca193dSGeorge Wilson 
30644b964adaSGeorge Wilson 	ASSERT(nvd->vdev_top->vdev_islog);
306588ecc943SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
306688ecc943SGeorge Wilson 	ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3067e6ca193dSGeorge Wilson 
306888ecc943SGeorge Wilson 	for (int c = 0; c < nvd->vdev_children; c++)
306988ecc943SGeorge Wilson 		vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3070e6ca193dSGeorge Wilson 
30714b964adaSGeorge Wilson 	if (nvd->vdev_ops->vdev_op_leaf) {
3072e6ca193dSGeorge Wilson 		/*
30734b964adaSGeorge Wilson 		 * Restore the persistent vdev state
3074e6ca193dSGeorge Wilson 		 */
307588ecc943SGeorge Wilson 		nvd->vdev_offline = ovd->vdev_offline;
30764b964adaSGeorge Wilson 		nvd->vdev_faulted = ovd->vdev_faulted;
30774b964adaSGeorge Wilson 		nvd->vdev_degraded = ovd->vdev_degraded;
30784b964adaSGeorge Wilson 		nvd->vdev_removed = ovd->vdev_removed;
3079e6ca193dSGeorge Wilson 	}
3080e6ca193dSGeorge Wilson }
3081573ca77eSGeorge Wilson 
30824b964adaSGeorge Wilson /*
30834b964adaSGeorge Wilson  * Determine if a log device has valid content.  If the vdev was
30844b964adaSGeorge Wilson  * removed or faulted in the MOS config then we know that
30854b964adaSGeorge Wilson  * the content on the log device has already been written to the pool.
30864b964adaSGeorge Wilson  */
30874b964adaSGeorge Wilson boolean_t
30884b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd)
30894b964adaSGeorge Wilson {
30904b964adaSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
30914b964adaSGeorge Wilson 	    !vd->vdev_removed)
30924b964adaSGeorge Wilson 		return (B_TRUE);
30934b964adaSGeorge Wilson 
30944b964adaSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
30954b964adaSGeorge Wilson 		if (vdev_log_state_valid(vd->vdev_child[c]))
30964b964adaSGeorge Wilson 			return (B_TRUE);
30974b964adaSGeorge Wilson 
30984b964adaSGeorge Wilson 	return (B_FALSE);
30994b964adaSGeorge Wilson }
31004b964adaSGeorge Wilson 
3101573ca77eSGeorge Wilson /*
3102573ca77eSGeorge Wilson  * Expand a vdev if possible.
3103573ca77eSGeorge Wilson  */
3104573ca77eSGeorge Wilson void
3105573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
3106573ca77eSGeorge Wilson {
3107573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
3108573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3109573ca77eSGeorge Wilson 
3110573ca77eSGeorge Wilson 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3111573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
3112573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
3113573ca77eSGeorge Wilson 	}
3114573ca77eSGeorge Wilson }
31151195e687SMark J Musante 
31161195e687SMark J Musante /*
31171195e687SMark J Musante  * Split a vdev.
31181195e687SMark J Musante  */
31191195e687SMark J Musante void
31201195e687SMark J Musante vdev_split(vdev_t *vd)
31211195e687SMark J Musante {
31221195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
31231195e687SMark J Musante 
31241195e687SMark J Musante 	vdev_remove_child(pvd, vd);
31251195e687SMark J Musante 	vdev_compact_children(pvd);
31261195e687SMark J Musante 
31271195e687SMark J Musante 	cvd = pvd->vdev_child[0];
31281195e687SMark J Musante 	if (pvd->vdev_children == 1) {
31291195e687SMark J Musante 		vdev_remove_parent(cvd);
31301195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
31311195e687SMark J Musante 	}
31321195e687SMark J Musante 	vdev_propagate_state(cvd);
31331195e687SMark J Musante }
3134