xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 98d1cbfec254295273b6a761bc1861c0374bdf02)
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 /*
23*98d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/zfs_context.h>
27ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
28fa9e4066Sahrens #include <sys/spa.h>
29fa9e4066Sahrens #include <sys/spa_impl.h>
30fa9e4066Sahrens #include <sys/dmu.h>
31fa9e4066Sahrens #include <sys/dmu_tx.h>
32fa9e4066Sahrens #include <sys/vdev_impl.h>
33fa9e4066Sahrens #include <sys/uberblock_impl.h>
34fa9e4066Sahrens #include <sys/metaslab.h>
35fa9e4066Sahrens #include <sys/metaslab_impl.h>
36fa9e4066Sahrens #include <sys/space_map.h>
37fa9e4066Sahrens #include <sys/zio.h>
38fa9e4066Sahrens #include <sys/zap.h>
39fa9e4066Sahrens #include <sys/fs/zfs.h>
40c5904d13Seschrock #include <sys/arc.h>
41e6ca193dSGeorge Wilson #include <sys/zil.h>
42fa9e4066Sahrens 
43fa9e4066Sahrens /*
44fa9e4066Sahrens  * Virtual device management.
45fa9e4066Sahrens  */
46fa9e4066Sahrens 
47fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = {
48fa9e4066Sahrens 	&vdev_root_ops,
49fa9e4066Sahrens 	&vdev_raidz_ops,
50fa9e4066Sahrens 	&vdev_mirror_ops,
51fa9e4066Sahrens 	&vdev_replacing_ops,
5299653d4eSeschrock 	&vdev_spare_ops,
53fa9e4066Sahrens 	&vdev_disk_ops,
54fa9e4066Sahrens 	&vdev_file_ops,
55fa9e4066Sahrens 	&vdev_missing_ops,
5688ecc943SGeorge Wilson 	&vdev_hole_ops,
57fa9e4066Sahrens 	NULL
58fa9e4066Sahrens };
59fa9e4066Sahrens 
60088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
61088f3894Sahrens int zfs_scrub_limit = 10;
6205b2b3b8Smishra 
63fa9e4066Sahrens /*
64fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
65fa9e4066Sahrens  */
66fa9e4066Sahrens static vdev_ops_t *
67fa9e4066Sahrens vdev_getops(const char *type)
68fa9e4066Sahrens {
69fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
70fa9e4066Sahrens 
71fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
72fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
73fa9e4066Sahrens 			break;
74fa9e4066Sahrens 
75fa9e4066Sahrens 	return (ops);
76fa9e4066Sahrens }
77fa9e4066Sahrens 
78fa9e4066Sahrens /*
79fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
80fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
81fa9e4066Sahrens  */
82fa9e4066Sahrens uint64_t
83fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
84fa9e4066Sahrens {
85ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
86fa9e4066Sahrens 	uint64_t csize;
87fa9e4066Sahrens 
88573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
89fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
90fa9e4066Sahrens 		asize = MAX(asize, csize);
91fa9e4066Sahrens 	}
92fa9e4066Sahrens 
93fa9e4066Sahrens 	return (asize);
94fa9e4066Sahrens }
95fa9e4066Sahrens 
962a79c5feSlling /*
97573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
98573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
99573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
100573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
1012a79c5feSlling  */
1022a79c5feSlling uint64_t
103573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
1042a79c5feSlling {
105573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
1062a79c5feSlling 
107573ca77eSGeorge Wilson 	/*
108573ca77eSGeorge Wilson 	 * The our parent is NULL (inactive spare or cache) or is the root,
109573ca77eSGeorge Wilson 	 * just return our own asize.
110573ca77eSGeorge Wilson 	 */
111573ca77eSGeorge Wilson 	if (pvd == NULL)
112573ca77eSGeorge Wilson 		return (vd->vdev_asize);
1132a79c5feSlling 
1142a79c5feSlling 	/*
115573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
116573ca77eSGeorge Wilson 	 * to the nearest metaslab.
1172a79c5feSlling 	 */
118573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
119573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
1202a79c5feSlling 
121573ca77eSGeorge Wilson 	/*
122573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
123573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
124573ca77eSGeorge Wilson 	 */
125573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
126573ca77eSGeorge Wilson 		return (pvd->vdev_min_asize / pvd->vdev_children);
1272a79c5feSlling 
128573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
129573ca77eSGeorge Wilson }
1302a79c5feSlling 
131573ca77eSGeorge Wilson void
132573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
133573ca77eSGeorge Wilson {
134573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
135573ca77eSGeorge Wilson 
136573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
137573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
1382a79c5feSlling }
1392a79c5feSlling 
140fa9e4066Sahrens vdev_t *
141fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
142fa9e4066Sahrens {
143fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
144fa9e4066Sahrens 
145e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
146e05725b1Sbonwick 
147088f3894Sahrens 	if (vdev < rvd->vdev_children) {
148088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
149fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
150088f3894Sahrens 	}
151fa9e4066Sahrens 
152fa9e4066Sahrens 	return (NULL);
153fa9e4066Sahrens }
154fa9e4066Sahrens 
155fa9e4066Sahrens vdev_t *
156fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
157fa9e4066Sahrens {
158fa9e4066Sahrens 	vdev_t *mvd;
159fa9e4066Sahrens 
1600e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
161fa9e4066Sahrens 		return (vd);
162fa9e4066Sahrens 
163573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
164fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
165fa9e4066Sahrens 		    NULL)
166fa9e4066Sahrens 			return (mvd);
167fa9e4066Sahrens 
168fa9e4066Sahrens 	return (NULL);
169fa9e4066Sahrens }
170fa9e4066Sahrens 
171fa9e4066Sahrens void
172fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
173fa9e4066Sahrens {
174fa9e4066Sahrens 	size_t oldsize, newsize;
175fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
176fa9e4066Sahrens 	vdev_t **newchild;
177fa9e4066Sahrens 
178e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
179fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
180fa9e4066Sahrens 
181fa9e4066Sahrens 	cvd->vdev_parent = pvd;
182fa9e4066Sahrens 
183fa9e4066Sahrens 	if (pvd == NULL)
184fa9e4066Sahrens 		return;
185fa9e4066Sahrens 
186fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
187fa9e4066Sahrens 
188fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
189fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
190fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
191fa9e4066Sahrens 
192fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
193fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
194fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
195fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
196fa9e4066Sahrens 	}
197fa9e4066Sahrens 
198fa9e4066Sahrens 	pvd->vdev_child = newchild;
199fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
200fa9e4066Sahrens 
201fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
202fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
203fa9e4066Sahrens 
204fa9e4066Sahrens 	/*
205fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
206fa9e4066Sahrens 	 */
207fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
208fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
20905b2b3b8Smishra 
21005b2b3b8Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
21105b2b3b8Smishra 		cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
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;
24605b2b3b8Smishra 
24705b2b3b8Smishra 	if (cvd->vdev_ops->vdev_op_leaf)
24805b2b3b8Smishra 		cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
249fa9e4066Sahrens }
250fa9e4066Sahrens 
251fa9e4066Sahrens /*
252fa9e4066Sahrens  * Remove any holes in the child array.
253fa9e4066Sahrens  */
254fa9e4066Sahrens void
255fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
256fa9e4066Sahrens {
257fa9e4066Sahrens 	vdev_t **newchild, *cvd;
258fa9e4066Sahrens 	int oldc = pvd->vdev_children;
259573ca77eSGeorge Wilson 	int newc;
260fa9e4066Sahrens 
261e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
262fa9e4066Sahrens 
263573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
264fa9e4066Sahrens 		if (pvd->vdev_child[c])
265fa9e4066Sahrens 			newc++;
266fa9e4066Sahrens 
267fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
268fa9e4066Sahrens 
269573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
270fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
271fa9e4066Sahrens 			newchild[newc] = cvd;
272fa9e4066Sahrens 			cvd->vdev_id = newc++;
273fa9e4066Sahrens 		}
274fa9e4066Sahrens 	}
275fa9e4066Sahrens 
276fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
277fa9e4066Sahrens 	pvd->vdev_child = newchild;
278fa9e4066Sahrens 	pvd->vdev_children = newc;
279fa9e4066Sahrens }
280fa9e4066Sahrens 
281fa9e4066Sahrens /*
282fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
283fa9e4066Sahrens  */
28488ecc943SGeorge Wilson vdev_t *
285fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
286fa9e4066Sahrens {
287fa9e4066Sahrens 	vdev_t *vd;
288fa9e4066Sahrens 
289fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
290fa9e4066Sahrens 
2910e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
2920e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
2930e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
2940e34b6a7Sbonwick 	}
2950e34b6a7Sbonwick 
29688ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
2970e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
2980e34b6a7Sbonwick 			/*
2990e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
3000e34b6a7Sbonwick 			 * which must be unique among all pools.
3010e34b6a7Sbonwick 			 */
3021195e687SMark J Musante 			guid = spa_generate_guid(NULL);
3030e34b6a7Sbonwick 		} else {
3040e34b6a7Sbonwick 			/*
3050e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3060e34b6a7Sbonwick 			 */
3071195e687SMark J Musante 			guid = spa_generate_guid(spa);
3080e34b6a7Sbonwick 		}
3090e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3100e34b6a7Sbonwick 	}
3110e34b6a7Sbonwick 
312fa9e4066Sahrens 	vd->vdev_spa = spa;
313fa9e4066Sahrens 	vd->vdev_id = id;
314fa9e4066Sahrens 	vd->vdev_guid = guid;
315fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
316fa9e4066Sahrens 	vd->vdev_ops = ops;
317fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
31888ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
319fa9e4066Sahrens 
320fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3215ad82045Snd 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
322e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3238ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
3248ad4d6ddSJeff Bonwick 		space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
3258ad4d6ddSJeff Bonwick 		    &vd->vdev_dtl_lock);
3268ad4d6ddSJeff Bonwick 	}
327fa9e4066Sahrens 	txg_list_create(&vd->vdev_ms_list,
328fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
329fa9e4066Sahrens 	txg_list_create(&vd->vdev_dtl_list,
330fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
331fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3323d7072f8Seschrock 	vdev_queue_init(vd);
3333d7072f8Seschrock 	vdev_cache_init(vd);
334fa9e4066Sahrens 
335fa9e4066Sahrens 	return (vd);
336fa9e4066Sahrens }
337fa9e4066Sahrens 
338fa9e4066Sahrens /*
339fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
340fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
341fa9e4066Sahrens  * different for each case.
342fa9e4066Sahrens  */
34399653d4eSeschrock int
34499653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
34599653d4eSeschrock     int alloctype)
346fa9e4066Sahrens {
347fa9e4066Sahrens 	vdev_ops_t *ops;
348fa9e4066Sahrens 	char *type;
3498654d025Sperrin 	uint64_t guid = 0, islog, nparity;
350fa9e4066Sahrens 	vdev_t *vd;
351fa9e4066Sahrens 
352e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
353fa9e4066Sahrens 
354fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
35599653d4eSeschrock 		return (EINVAL);
356fa9e4066Sahrens 
357fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
35899653d4eSeschrock 		return (EINVAL);
359fa9e4066Sahrens 
360fa9e4066Sahrens 	/*
361fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
362fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
363fa9e4066Sahrens 	 */
364fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
365fa9e4066Sahrens 		uint64_t label_id;
366fa9e4066Sahrens 
367fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
368fa9e4066Sahrens 		    label_id != id)
36999653d4eSeschrock 			return (EINVAL);
370fa9e4066Sahrens 
371fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
37299653d4eSeschrock 			return (EINVAL);
37399653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
37499653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
37599653d4eSeschrock 			return (EINVAL);
376fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
377fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
378fa94a07fSbrendan 			return (EINVAL);
37921ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
38021ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
38121ecdf64SLin Ling 			return (EINVAL);
382fa9e4066Sahrens 	}
383fa9e4066Sahrens 
38499653d4eSeschrock 	/*
38599653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
38699653d4eSeschrock 	 */
38799653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
38899653d4eSeschrock 		return (EINVAL);
38999653d4eSeschrock 
3908654d025Sperrin 	/*
3918654d025Sperrin 	 * Determine whether we're a log vdev.
3928654d025Sperrin 	 */
3938654d025Sperrin 	islog = 0;
3948654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
395990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
3968654d025Sperrin 		return (ENOTSUP);
397fa9e4066Sahrens 
39888ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
39988ecc943SGeorge Wilson 		return (ENOTSUP);
40088ecc943SGeorge Wilson 
40199653d4eSeschrock 	/*
4028654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
40399653d4eSeschrock 	 */
4048654d025Sperrin 	nparity = -1ULL;
40599653d4eSeschrock 	if (ops == &vdev_raidz_ops) {
40699653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4078654d025Sperrin 		    &nparity) == 0) {
408b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
40999653d4eSeschrock 				return (EINVAL);
41099653d4eSeschrock 			/*
411f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
412f94275ceSAdam Leventhal 			 * device.
41399653d4eSeschrock 			 */
414f94275ceSAdam Leventhal 			if (nparity > 1 &&
415f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
416f94275ceSAdam Leventhal 				return (ENOTSUP);
417f94275ceSAdam Leventhal 			if (nparity > 2 &&
418f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
41999653d4eSeschrock 				return (ENOTSUP);
42099653d4eSeschrock 		} else {
42199653d4eSeschrock 			/*
42299653d4eSeschrock 			 * We require the parity to be specified for SPAs that
42399653d4eSeschrock 			 * support multiple parity levels.
42499653d4eSeschrock 			 */
425f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
42699653d4eSeschrock 				return (EINVAL);
42799653d4eSeschrock 			/*
42899653d4eSeschrock 			 * Otherwise, we default to 1 parity device for RAID-Z.
42999653d4eSeschrock 			 */
4308654d025Sperrin 			nparity = 1;
43199653d4eSeschrock 		}
43299653d4eSeschrock 	} else {
4338654d025Sperrin 		nparity = 0;
43499653d4eSeschrock 	}
4358654d025Sperrin 	ASSERT(nparity != -1ULL);
4368654d025Sperrin 
4378654d025Sperrin 	vd = vdev_alloc_common(spa, id, guid, ops);
4388654d025Sperrin 
4398654d025Sperrin 	vd->vdev_islog = islog;
4408654d025Sperrin 	vd->vdev_nparity = nparity;
4418654d025Sperrin 
4428654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
4438654d025Sperrin 		vd->vdev_path = spa_strdup(vd->vdev_path);
4448654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
4458654d025Sperrin 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4468654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4478654d025Sperrin 	    &vd->vdev_physpath) == 0)
4488654d025Sperrin 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4496809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4506809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
45199653d4eSeschrock 
452afefbcddSeschrock 	/*
453afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
454afefbcddSeschrock 	 * as -1.
455afefbcddSeschrock 	 */
456afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
457afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
458afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
459afefbcddSeschrock 
460ea8dc4b6Seschrock 	/*
461ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
462ea8dc4b6Seschrock 	 * was not present at the time of import.
463ea8dc4b6Seschrock 	 */
4646809eb4eSEric Schrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4656809eb4eSEric Schrock 	    &vd->vdev_not_present);
466ea8dc4b6Seschrock 
467ecc2d604Sbonwick 	/*
468ecc2d604Sbonwick 	 * Get the alignment requirement.
469ecc2d604Sbonwick 	 */
470ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
471ecc2d604Sbonwick 
47288ecc943SGeorge Wilson 	/*
47388ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
47488ecc943SGeorge Wilson 	 */
47588ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
47688ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
47788ecc943SGeorge Wilson 
478fa9e4066Sahrens 	/*
479fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
480fa9e4066Sahrens 	 */
4811195e687SMark J Musante 	if (parent && !parent->vdev_parent &&
4821195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
483fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
484fa9e4066Sahrens 		    &vd->vdev_ms_array);
485fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
486fa9e4066Sahrens 		    &vd->vdev_ms_shift);
487fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
488fa9e4066Sahrens 		    &vd->vdev_asize);
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 
5243d7072f8Seschrock 		/*
5253d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5263d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
527069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
528069f55e2SEric Schrock 		 * remain in the faulted state.
5293d7072f8Seschrock 		 */
530b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
5313d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5323d7072f8Seschrock 			    &vd->vdev_faulted);
5333d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5343d7072f8Seschrock 			    &vd->vdev_degraded);
5353d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5363d7072f8Seschrock 			    &vd->vdev_removed);
537069f55e2SEric Schrock 
538069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
539069f55e2SEric Schrock 				char *aux;
540069f55e2SEric Schrock 
541069f55e2SEric Schrock 				vd->vdev_label_aux =
542069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
543069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
544069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
545069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
546069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
547069f55e2SEric Schrock 			}
5483d7072f8Seschrock 		}
549fa9e4066Sahrens 	}
550fa9e4066Sahrens 
551fa9e4066Sahrens 	/*
552fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
553fa9e4066Sahrens 	 */
554fa9e4066Sahrens 	vdev_add_child(parent, vd);
555fa9e4066Sahrens 
55699653d4eSeschrock 	*vdp = vd;
55799653d4eSeschrock 
55899653d4eSeschrock 	return (0);
559fa9e4066Sahrens }
560fa9e4066Sahrens 
561fa9e4066Sahrens void
562fa9e4066Sahrens vdev_free(vdev_t *vd)
563fa9e4066Sahrens {
5643d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
565fa9e4066Sahrens 
566fa9e4066Sahrens 	/*
567fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
568fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
569fa9e4066Sahrens 	 */
570fa9e4066Sahrens 	vdev_close(vd);
571fa9e4066Sahrens 
572e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
573b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
574fa9e4066Sahrens 
575fa9e4066Sahrens 	/*
576fa9e4066Sahrens 	 * Free all children.
577fa9e4066Sahrens 	 */
578573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
579fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
580fa9e4066Sahrens 
581fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
582fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
583fa9e4066Sahrens 
584fa9e4066Sahrens 	/*
585fa9e4066Sahrens 	 * Discard allocation state.
586fa9e4066Sahrens 	 */
587a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
588fa9e4066Sahrens 		vdev_metaslab_fini(vd);
589a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
590a1521560SJeff Bonwick 	}
591fa9e4066Sahrens 
592fa9e4066Sahrens 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
59399653d4eSeschrock 	ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
594fa9e4066Sahrens 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
595fa9e4066Sahrens 
596fa9e4066Sahrens 	/*
597fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
598fa9e4066Sahrens 	 */
599fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
600fa9e4066Sahrens 
601fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
602fa9e4066Sahrens 
6033d7072f8Seschrock 	/*
6043d7072f8Seschrock 	 * Clean up vdev structure.
6053d7072f8Seschrock 	 */
6063d7072f8Seschrock 	vdev_queue_fini(vd);
6073d7072f8Seschrock 	vdev_cache_fini(vd);
6083d7072f8Seschrock 
6093d7072f8Seschrock 	if (vd->vdev_path)
6103d7072f8Seschrock 		spa_strfree(vd->vdev_path);
6113d7072f8Seschrock 	if (vd->vdev_devid)
6123d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
6133d7072f8Seschrock 	if (vd->vdev_physpath)
6143d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
6156809eb4eSEric Schrock 	if (vd->vdev_fru)
6166809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
6173d7072f8Seschrock 
6183d7072f8Seschrock 	if (vd->vdev_isspare)
6193d7072f8Seschrock 		spa_spare_remove(vd);
620fa94a07fSbrendan 	if (vd->vdev_isl2cache)
621fa94a07fSbrendan 		spa_l2cache_remove(vd);
6223d7072f8Seschrock 
6233d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
6243d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
6258ad4d6ddSJeff Bonwick 
6263d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
6278ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
6288ad4d6ddSJeff Bonwick 		space_map_unload(&vd->vdev_dtl[t]);
6298ad4d6ddSJeff Bonwick 		space_map_destroy(&vd->vdev_dtl[t]);
6308ad4d6ddSJeff Bonwick 	}
6313d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
6328ad4d6ddSJeff Bonwick 
6333d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6343d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
635e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
6363d7072f8Seschrock 
6373d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
6383d7072f8Seschrock 		spa->spa_root_vdev = NULL;
6393d7072f8Seschrock 
6403d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
641fa9e4066Sahrens }
642fa9e4066Sahrens 
643fa9e4066Sahrens /*
644fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
645fa9e4066Sahrens  */
646fa9e4066Sahrens static void
647fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
648fa9e4066Sahrens {
649fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
650fa9e4066Sahrens 	metaslab_t *msp;
651fa9e4066Sahrens 	vdev_t *vd;
652fa9e4066Sahrens 	int t;
653fa9e4066Sahrens 
654fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
655fa9e4066Sahrens 
656fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
657fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
658fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
659fa9e4066Sahrens 
660fa9e4066Sahrens 	svd->vdev_ms_array = 0;
661fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
662fa9e4066Sahrens 	svd->vdev_ms_count = 0;
663fa9e4066Sahrens 
664fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
665fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
666fa9e4066Sahrens 
667fa9e4066Sahrens 	svd->vdev_mg = NULL;
668fa9e4066Sahrens 	svd->vdev_ms = NULL;
669ecc2d604Sbonwick 
670ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
671ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
672fa9e4066Sahrens 
673fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
674fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
67599653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
676fa9e4066Sahrens 
677fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
678fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
67999653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
680fa9e4066Sahrens 
681fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
682fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
683fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
684fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
685fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
686fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
687fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
688fa9e4066Sahrens 	}
689fa9e4066Sahrens 
690e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
691fa9e4066Sahrens 		vdev_config_clean(svd);
692fa9e4066Sahrens 		vdev_config_dirty(tvd);
693fa9e4066Sahrens 	}
694fa9e4066Sahrens 
695e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
696e14bb325SJeff Bonwick 		vdev_state_clean(svd);
697e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
698e14bb325SJeff Bonwick 	}
699e14bb325SJeff Bonwick 
70099653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
70199653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
7028654d025Sperrin 
7038654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
7048654d025Sperrin 	svd->vdev_islog = 0;
705fa9e4066Sahrens }
706fa9e4066Sahrens 
707fa9e4066Sahrens static void
708fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
709fa9e4066Sahrens {
710fa9e4066Sahrens 	if (vd == NULL)
711fa9e4066Sahrens 		return;
712fa9e4066Sahrens 
713fa9e4066Sahrens 	vd->vdev_top = tvd;
714fa9e4066Sahrens 
715573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
716fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
717fa9e4066Sahrens }
718fa9e4066Sahrens 
719fa9e4066Sahrens /*
720fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
721fa9e4066Sahrens  */
722fa9e4066Sahrens vdev_t *
723fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
724fa9e4066Sahrens {
725fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
726fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
727fa9e4066Sahrens 	vdev_t *mvd;
728fa9e4066Sahrens 
729e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
730fa9e4066Sahrens 
731fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
732ecc2d604Sbonwick 
733ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
734573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
735ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
736ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
73788ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
738ecc2d604Sbonwick 
739fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
740fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
741fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
742fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
743fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
744fa9e4066Sahrens 
745fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
746fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
747fa9e4066Sahrens 
748fa9e4066Sahrens 	return (mvd);
749fa9e4066Sahrens }
750fa9e4066Sahrens 
751fa9e4066Sahrens /*
752fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
753fa9e4066Sahrens  */
754fa9e4066Sahrens void
755fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
756fa9e4066Sahrens {
757fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
758fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
759fa9e4066Sahrens 
760e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
761fa9e4066Sahrens 
762fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
763fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
76499653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
76599653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
766ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
767fa9e4066Sahrens 
768fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
769fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
7708ad4d6ddSJeff Bonwick 
77199653d4eSeschrock 	/*
772e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
773e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
774e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
775e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
77699653d4eSeschrock 	 */
7778ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
7788ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
7791195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
7808ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
7818ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
7828ad4d6ddSJeff Bonwick 	}
783e14bb325SJeff Bonwick 	cvd->vdev_id = mvd->vdev_id;
784e14bb325SJeff Bonwick 	vdev_add_child(pvd, cvd);
785fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
786fa9e4066Sahrens 
787fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
788fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
789fa9e4066Sahrens 
790fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
791fa9e4066Sahrens 	vdev_free(mvd);
792fa9e4066Sahrens }
793fa9e4066Sahrens 
794ea8dc4b6Seschrock int
795fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
796fa9e4066Sahrens {
797fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
798ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
799ecc2d604Sbonwick 	uint64_t m;
800fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
801fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
802ecc2d604Sbonwick 	metaslab_t **mspp;
803ecc2d604Sbonwick 	int error;
804fa9e4066Sahrens 
805a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
806a1521560SJeff Bonwick 
80788ecc943SGeorge Wilson 	/*
80888ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
80988ecc943SGeorge Wilson 	 */
81088ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
8110e34b6a7Sbonwick 		return (0);
8120e34b6a7Sbonwick 
81388ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
81488ecc943SGeorge Wilson 
815e6ca193dSGeorge Wilson 	/*
816e6ca193dSGeorge Wilson 	 * Compute the raidz-deflation ratio.  Note, we hard-code
817e6ca193dSGeorge Wilson 	 * in 128k (1 << 17) because it is the current "typical" blocksize.
818e6ca193dSGeorge Wilson 	 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
819e6ca193dSGeorge Wilson 	 * or we will inconsistently account for existing bp's.
820e6ca193dSGeorge Wilson 	 */
821e6ca193dSGeorge Wilson 	vd->vdev_deflate_ratio = (1 << 17) /
822e6ca193dSGeorge Wilson 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
823e6ca193dSGeorge Wilson 
824fa9e4066Sahrens 	ASSERT(oldc <= newc);
825fa9e4066Sahrens 
826ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
827fa9e4066Sahrens 
828ecc2d604Sbonwick 	if (oldc != 0) {
829ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
830ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
831ecc2d604Sbonwick 	}
832fa9e4066Sahrens 
833ecc2d604Sbonwick 	vd->vdev_ms = mspp;
834ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
835fa9e4066Sahrens 
836ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
837ecc2d604Sbonwick 		space_map_obj_t smo = { 0, 0, 0 };
838ecc2d604Sbonwick 		if (txg == 0) {
839ecc2d604Sbonwick 			uint64_t object = 0;
840ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8417bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8427bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
843ecc2d604Sbonwick 			if (error)
844ecc2d604Sbonwick 				return (error);
845ecc2d604Sbonwick 			if (object != 0) {
846ecc2d604Sbonwick 				dmu_buf_t *db;
847ecc2d604Sbonwick 				error = dmu_bonus_hold(mos, object, FTAG, &db);
848ecc2d604Sbonwick 				if (error)
849ecc2d604Sbonwick 					return (error);
8501934e92fSmaybee 				ASSERT3U(db->db_size, >=, sizeof (smo));
8511934e92fSmaybee 				bcopy(db->db_data, &smo, sizeof (smo));
852ecc2d604Sbonwick 				ASSERT3U(smo.smo_object, ==, object);
853ea8dc4b6Seschrock 				dmu_buf_rele(db, FTAG);
854fa9e4066Sahrens 			}
855fa9e4066Sahrens 		}
856ecc2d604Sbonwick 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
857ecc2d604Sbonwick 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
858fa9e4066Sahrens 	}
859fa9e4066Sahrens 
860a1521560SJeff Bonwick 	if (txg == 0)
861a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
862a1521560SJeff Bonwick 
863a1521560SJeff Bonwick 	if (oldc == 0)
864a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
865a1521560SJeff Bonwick 
866a1521560SJeff Bonwick 	if (txg == 0)
867a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
868a1521560SJeff Bonwick 
869ea8dc4b6Seschrock 	return (0);
870fa9e4066Sahrens }
871fa9e4066Sahrens 
872fa9e4066Sahrens void
873fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
874fa9e4066Sahrens {
875fa9e4066Sahrens 	uint64_t m;
876fa9e4066Sahrens 	uint64_t count = vd->vdev_ms_count;
877fa9e4066Sahrens 
878fa9e4066Sahrens 	if (vd->vdev_ms != NULL) {
879a1521560SJeff Bonwick 		metaslab_group_passivate(vd->vdev_mg);
880fa9e4066Sahrens 		for (m = 0; m < count; m++)
881ecc2d604Sbonwick 			if (vd->vdev_ms[m] != NULL)
882ecc2d604Sbonwick 				metaslab_fini(vd->vdev_ms[m]);
883fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
884fa9e4066Sahrens 		vd->vdev_ms = NULL;
885fa9e4066Sahrens 	}
886fa9e4066Sahrens }
887fa9e4066Sahrens 
888e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
889e14bb325SJeff Bonwick 	boolean_t	vps_readable;
890e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
891e14bb325SJeff Bonwick 	int		vps_flags;
892e14bb325SJeff Bonwick } vdev_probe_stats_t;
893e14bb325SJeff Bonwick 
894e14bb325SJeff Bonwick static void
895e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
8960a4e9518Sgw {
8978ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
898a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
899e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
900a3f829aeSBill Moore 
901a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
902e14bb325SJeff Bonwick 
903e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
904e14bb325SJeff Bonwick 		if (zio->io_error == 0)
905e14bb325SJeff Bonwick 			vps->vps_readable = 1;
9068ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
907a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
908e14bb325SJeff Bonwick 			    zio->io_offset, zio->io_size, zio->io_data,
909e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
910e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
911e14bb325SJeff Bonwick 		} else {
912e14bb325SJeff Bonwick 			zio_buf_free(zio->io_data, zio->io_size);
913e14bb325SJeff Bonwick 		}
914e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
915e14bb325SJeff Bonwick 		if (zio->io_error == 0)
916e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
917e14bb325SJeff Bonwick 		zio_buf_free(zio->io_data, zio->io_size);
918e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
919a3f829aeSBill Moore 		zio_t *pio;
920e14bb325SJeff Bonwick 
921e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
922e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
923e14bb325SJeff Bonwick 
924e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
9258ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
926e14bb325SJeff Bonwick 			zio->io_error = 0;
927e14bb325SJeff Bonwick 		} else {
928e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
929e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
9308ad4d6ddSJeff Bonwick 			    spa, vd, NULL, 0, 0);
931e14bb325SJeff Bonwick 			zio->io_error = ENXIO;
932e14bb325SJeff Bonwick 		}
933a3f829aeSBill Moore 
934a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
935a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
936a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
937a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
938a3f829aeSBill Moore 
939a3f829aeSBill Moore 		while ((pio = zio_walk_parents(zio)) != NULL)
940a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
941a3f829aeSBill Moore 				pio->io_error = ENXIO;
942a3f829aeSBill Moore 
943e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
944e14bb325SJeff Bonwick 	}
945e14bb325SJeff Bonwick }
9460a4e9518Sgw 
947e14bb325SJeff Bonwick /*
948e14bb325SJeff Bonwick  * Determine whether this device is accessible by reading and writing
949e14bb325SJeff Bonwick  * to several known locations: the pad regions of each vdev label
950e14bb325SJeff Bonwick  * but the first (which we leave alone in case it contains a VTOC).
951e14bb325SJeff Bonwick  */
952e14bb325SJeff Bonwick zio_t *
953a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
954e14bb325SJeff Bonwick {
955e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
956a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
957a3f829aeSBill Moore 	zio_t *pio;
958a3f829aeSBill Moore 
959a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9600a4e9518Sgw 
961a3f829aeSBill Moore 	/*
962a3f829aeSBill Moore 	 * Don't probe the probe.
963a3f829aeSBill Moore 	 */
964a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
965a3f829aeSBill Moore 		return (NULL);
966e14bb325SJeff Bonwick 
967a3f829aeSBill Moore 	/*
968a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
969a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
970a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
971a3f829aeSBill Moore 	 */
972a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
973e14bb325SJeff Bonwick 
974a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
975a3f829aeSBill Moore 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
976a3f829aeSBill Moore 
977a3f829aeSBill Moore 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
978a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9798956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
980a3f829aeSBill Moore 
981a3f829aeSBill Moore 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
982a3f829aeSBill Moore 			/*
983a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
984a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
985a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
986a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
987a3f829aeSBill Moore 			 * any zio looking at these values can assume that
988a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
989a3f829aeSBill Moore 			 * important because when a device has intermittent
990a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
991a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
992a3f829aeSBill Moore 			 * the zio (EIO).
993a3f829aeSBill Moore 			 *
994a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
995a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
996a3f829aeSBill Moore 			 * principles.
997a3f829aeSBill Moore 			 */
998a3f829aeSBill Moore 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
999a3f829aeSBill Moore 			vd->vdev_cant_read = B_FALSE;
1000a3f829aeSBill Moore 			vd->vdev_cant_write = B_FALSE;
1001a3f829aeSBill Moore 		}
1002a3f829aeSBill Moore 
1003a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1004a3f829aeSBill Moore 		    vdev_probe_done, vps,
1005a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1006a3f829aeSBill Moore 
1007*98d1cbfeSGeorge Wilson 		/*
1008*98d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
1009*98d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
1010*98d1cbfeSGeorge Wilson 		 */
1011a3f829aeSBill Moore 		if (zio != NULL) {
1012a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1013a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1014a3f829aeSBill Moore 		}
1015e14bb325SJeff Bonwick 	}
1016e14bb325SJeff Bonwick 
1017a3f829aeSBill Moore 	if (zio != NULL)
1018a3f829aeSBill Moore 		zio_add_child(zio, pio);
1019e14bb325SJeff Bonwick 
1020a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1021e14bb325SJeff Bonwick 
1022a3f829aeSBill Moore 	if (vps == NULL) {
1023a3f829aeSBill Moore 		ASSERT(zio != NULL);
1024a3f829aeSBill Moore 		return (NULL);
1025a3f829aeSBill Moore 	}
1026e14bb325SJeff Bonwick 
1027e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1028a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1029e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1030f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2)),
1031f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1032e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1033e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1034e14bb325SJeff Bonwick 	}
1035e14bb325SJeff Bonwick 
1036a3f829aeSBill Moore 	if (zio == NULL)
1037a3f829aeSBill Moore 		return (pio);
1038a3f829aeSBill Moore 
1039a3f829aeSBill Moore 	zio_nowait(pio);
1040a3f829aeSBill Moore 	return (NULL);
10410a4e9518Sgw }
10420a4e9518Sgw 
1043f64c0e34SEric Taylor static void
1044f64c0e34SEric Taylor vdev_open_child(void *arg)
1045f64c0e34SEric Taylor {
1046f64c0e34SEric Taylor 	vdev_t *vd = arg;
1047f64c0e34SEric Taylor 
1048f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1049f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1050f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1051f64c0e34SEric Taylor }
1052f64c0e34SEric Taylor 
1053681d9761SEric Taylor boolean_t
1054681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1055681d9761SEric Taylor {
1056681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1057681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1058681d9761SEric Taylor 		return (B_TRUE);
1059681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1060681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1061681d9761SEric Taylor 			return (B_TRUE);
1062681d9761SEric Taylor 	return (B_FALSE);
1063681d9761SEric Taylor }
1064681d9761SEric Taylor 
1065f64c0e34SEric Taylor void
1066f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1067f64c0e34SEric Taylor {
1068f64c0e34SEric Taylor 	taskq_t *tq;
1069f64c0e34SEric Taylor 	int children = vd->vdev_children;
1070f64c0e34SEric Taylor 
1071681d9761SEric Taylor 	/*
1072681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1073681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1074681d9761SEric Taylor 	 * spa_namespace_lock
1075681d9761SEric Taylor 	 */
1076681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
1077681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1078681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1079681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
1080681d9761SEric Taylor 		return;
1081681d9761SEric Taylor 	}
1082f64c0e34SEric Taylor 	tq = taskq_create("vdev_open", children, minclsyspri,
1083f64c0e34SEric Taylor 	    children, children, TASKQ_PREPOPULATE);
1084f64c0e34SEric Taylor 
1085f64c0e34SEric Taylor 	for (int c = 0; c < children; c++)
1086f64c0e34SEric Taylor 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1087f64c0e34SEric Taylor 		    TQ_SLEEP) != NULL);
1088f64c0e34SEric Taylor 
1089f64c0e34SEric Taylor 	taskq_destroy(tq);
1090f64c0e34SEric Taylor }
1091f64c0e34SEric Taylor 
1092fa9e4066Sahrens /*
1093fa9e4066Sahrens  * Prepare a virtual device for access.
1094fa9e4066Sahrens  */
1095fa9e4066Sahrens int
1096fa9e4066Sahrens vdev_open(vdev_t *vd)
1097fa9e4066Sahrens {
10988ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1099fa9e4066Sahrens 	int error;
1100fa9e4066Sahrens 	uint64_t osize = 0;
1101fa9e4066Sahrens 	uint64_t asize, psize;
1102ecc2d604Sbonwick 	uint64_t ashift = 0;
1103fa9e4066Sahrens 
1104f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1105f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1106fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1107fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1108fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1109fa9e4066Sahrens 
1110fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1111e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1112e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1113573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1114fa9e4066Sahrens 
1115069f55e2SEric Schrock 	/*
1116069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1117069f55e2SEric Schrock 	 * faulted, bail out of the open.
1118069f55e2SEric Schrock 	 */
11193d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
11203d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1121069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1122069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
11233d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1124069f55e2SEric Schrock 		    vd->vdev_label_aux);
11253d7072f8Seschrock 		return (ENXIO);
11263d7072f8Seschrock 	} else if (vd->vdev_offline) {
1127fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1128ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1129fa9e4066Sahrens 		return (ENXIO);
1130fa9e4066Sahrens 	}
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1133fa9e4066Sahrens 
1134095bcd66SGeorge Wilson 	/*
1135095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1136095bcd66SGeorge Wilson 	 * the vdev on error.
1137095bcd66SGeorge Wilson 	 */
1138095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1139ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
11408956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1141ea8dc4b6Seschrock 
1142fa9e4066Sahrens 	if (error) {
11433d7072f8Seschrock 		if (vd->vdev_removed &&
11443d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
11453d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
11463d7072f8Seschrock 
1147ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1148fa9e4066Sahrens 		    vd->vdev_stat.vs_aux);
1149fa9e4066Sahrens 		return (error);
1150fa9e4066Sahrens 	}
1151fa9e4066Sahrens 
11523d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
11533d7072f8Seschrock 
1154096d22d4SEric Schrock 	/*
1155096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1156096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1157096d22d4SEric Schrock 	 */
1158096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1159096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1160096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1161096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1162096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1163096d22d4SEric Schrock 		    vd->vdev_label_aux);
1164096d22d4SEric Schrock 		return (ENXIO);
1165096d22d4SEric Schrock 	}
1166096d22d4SEric Schrock 
11673d7072f8Seschrock 	if (vd->vdev_degraded) {
11683d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
11693d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11703d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
11713d7072f8Seschrock 	} else {
1172069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
11733d7072f8Seschrock 	}
1174fa9e4066Sahrens 
117588ecc943SGeorge Wilson 	/*
117688ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
117788ecc943SGeorge Wilson 	 */
117888ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
117988ecc943SGeorge Wilson 		return (0);
118088ecc943SGeorge Wilson 
1181573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1182ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1183ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1184ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1185ea8dc4b6Seschrock 			break;
1186ea8dc4b6Seschrock 		}
1187573ca77eSGeorge Wilson 	}
1188fa9e4066Sahrens 
1189fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1190fa9e4066Sahrens 
1191fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1192fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1193ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1194ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1195fa9e4066Sahrens 			return (EOVERFLOW);
1196fa9e4066Sahrens 		}
1197fa9e4066Sahrens 		psize = osize;
1198fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1199fa9e4066Sahrens 	} else {
1200ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1201fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1202ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1203ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1204fa9e4066Sahrens 			return (EOVERFLOW);
1205fa9e4066Sahrens 		}
1206fa9e4066Sahrens 		psize = 0;
1207fa9e4066Sahrens 		asize = osize;
1208fa9e4066Sahrens 	}
1209fa9e4066Sahrens 
1210fa9e4066Sahrens 	vd->vdev_psize = psize;
1211fa9e4066Sahrens 
1212573ca77eSGeorge Wilson 	/*
1213573ca77eSGeorge Wilson 	 * Make sure the allocatable size hasn't shrunk.
1214573ca77eSGeorge Wilson 	 */
1215573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1216573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1217573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1218573ca77eSGeorge Wilson 		return (EINVAL);
1219573ca77eSGeorge Wilson 	}
1220573ca77eSGeorge Wilson 
1221fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1222fa9e4066Sahrens 		/*
1223fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
1224ecc2d604Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1225fa9e4066Sahrens 		 */
1226fa9e4066Sahrens 		vd->vdev_asize = asize;
1227ecc2d604Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1228fa9e4066Sahrens 	} else {
1229fa9e4066Sahrens 		/*
1230fa9e4066Sahrens 		 * Make sure the alignment requirement hasn't increased.
1231fa9e4066Sahrens 		 */
1232ecc2d604Sbonwick 		if (ashift > vd->vdev_top->vdev_ashift) {
1233ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1234ea8dc4b6Seschrock 			    VDEV_AUX_BAD_LABEL);
1235fa9e4066Sahrens 			return (EINVAL);
1236fa9e4066Sahrens 		}
1237573ca77eSGeorge Wilson 	}
1238fa9e4066Sahrens 
1239573ca77eSGeorge Wilson 	/*
1240573ca77eSGeorge Wilson 	 * If all children are healthy and the asize has increased,
1241573ca77eSGeorge Wilson 	 * then we've experienced dynamic LUN growth.  If automatic
1242573ca77eSGeorge Wilson 	 * expansion is enabled then use the additional space.
1243573ca77eSGeorge Wilson 	 */
1244573ca77eSGeorge Wilson 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1245573ca77eSGeorge Wilson 	    (vd->vdev_expanding || spa->spa_autoexpand))
1246573ca77eSGeorge Wilson 		vd->vdev_asize = asize;
1247fa9e4066Sahrens 
1248573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1249fa9e4066Sahrens 
12500a4e9518Sgw 	/*
12510a4e9518Sgw 	 * Ensure we can issue some IO before declaring the
12520a4e9518Sgw 	 * vdev open for business.
12530a4e9518Sgw 	 */
1254e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1255e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1256*98d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1257*98d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
12580a4e9518Sgw 		return (error);
12590a4e9518Sgw 	}
12600a4e9518Sgw 
1261088f3894Sahrens 	/*
1262088f3894Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
12638ad4d6ddSJeff Bonwick 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
12648ad4d6ddSJeff Bonwick 	 * since this would just restart the scrub we are already doing.
1265088f3894Sahrens 	 */
12668ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
12678ad4d6ddSJeff Bonwick 	    vdev_resilver_needed(vd, NULL, NULL))
12688ad4d6ddSJeff Bonwick 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1269088f3894Sahrens 
1270fa9e4066Sahrens 	return (0);
1271fa9e4066Sahrens }
1272fa9e4066Sahrens 
1273560e6e96Seschrock /*
1274560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
1275560e6e96Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
12763d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1277560e6e96Seschrock  *
1278560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1279560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1280560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1281560e6e96Seschrock  * will be updated but the function will return 0.
1282560e6e96Seschrock  */
1283560e6e96Seschrock int
1284560e6e96Seschrock vdev_validate(vdev_t *vd)
1285560e6e96Seschrock {
1286560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1287560e6e96Seschrock 	nvlist_t *label;
12881195e687SMark J Musante 	uint64_t guid = 0, top_guid;
1289560e6e96Seschrock 	uint64_t state;
1290560e6e96Seschrock 
1291573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1292560e6e96Seschrock 		if (vdev_validate(vd->vdev_child[c]) != 0)
12930bf246f5Smc 			return (EBADF);
1294560e6e96Seschrock 
1295b5989ec7Seschrock 	/*
1296b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1297b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1298b5989ec7Seschrock 	 * overwrite the previous state.
1299b5989ec7Seschrock 	 */
1300e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
13011195e687SMark J Musante 		uint64_t aux_guid = 0;
13021195e687SMark J Musante 		nvlist_t *nvl;
1303560e6e96Seschrock 
1304560e6e96Seschrock 		if ((label = vdev_label_read_config(vd)) == NULL) {
1305560e6e96Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1306560e6e96Seschrock 			    VDEV_AUX_BAD_LABEL);
1307560e6e96Seschrock 			return (0);
1308560e6e96Seschrock 		}
1309560e6e96Seschrock 
13101195e687SMark J Musante 		/*
13111195e687SMark J Musante 		 * Determine if this vdev has been split off into another
13121195e687SMark J Musante 		 * pool.  If so, then refuse to open it.
13131195e687SMark J Musante 		 */
13141195e687SMark J Musante 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
13151195e687SMark J Musante 		    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
13161195e687SMark J Musante 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13171195e687SMark J Musante 			    VDEV_AUX_SPLIT_POOL);
13181195e687SMark J Musante 			nvlist_free(label);
13191195e687SMark J Musante 			return (0);
13201195e687SMark J Musante 		}
13211195e687SMark J Musante 
1322560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
1323560e6e96Seschrock 		    &guid) != 0 || guid != spa_guid(spa)) {
1324560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1325560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1326560e6e96Seschrock 			nvlist_free(label);
1327560e6e96Seschrock 			return (0);
1328560e6e96Seschrock 		}
1329560e6e96Seschrock 
13301195e687SMark J Musante 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
13311195e687SMark J Musante 		    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
13321195e687SMark J Musante 		    &aux_guid) != 0)
13331195e687SMark J Musante 			aux_guid = 0;
13341195e687SMark J Musante 
1335e14bb325SJeff Bonwick 		/*
1336e14bb325SJeff Bonwick 		 * If this vdev just became a top-level vdev because its
1337e14bb325SJeff Bonwick 		 * sibling was detached, it will have adopted the parent's
1338e14bb325SJeff Bonwick 		 * vdev guid -- but the label may or may not be on disk yet.
1339e14bb325SJeff Bonwick 		 * Fortunately, either version of the label will have the
1340e14bb325SJeff Bonwick 		 * same top guid, so if we're a top-level vdev, we can
1341e14bb325SJeff Bonwick 		 * safely compare to that instead.
13421195e687SMark J Musante 		 *
13431195e687SMark J Musante 		 * If we split this vdev off instead, then we also check the
13441195e687SMark J Musante 		 * original pool's guid.  We don't want to consider the vdev
13451195e687SMark J Musante 		 * corrupt if it is partway through a split operation.
1346e14bb325SJeff Bonwick 		 */
1347560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1348e14bb325SJeff Bonwick 		    &guid) != 0 ||
1349e14bb325SJeff Bonwick 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1350e14bb325SJeff Bonwick 		    &top_guid) != 0 ||
13511195e687SMark J Musante 		    ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1352e14bb325SJeff Bonwick 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1353560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1354560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1355560e6e96Seschrock 			nvlist_free(label);
1356560e6e96Seschrock 			return (0);
1357560e6e96Seschrock 		}
1358560e6e96Seschrock 
1359560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1360560e6e96Seschrock 		    &state) != 0) {
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 		nvlist_free(label);
1368560e6e96Seschrock 
1369bc758434SLin Ling 		/*
1370bc758434SLin Ling 		 * If spa->spa_load_verbatim is true, no need to check the
1371bc758434SLin Ling 		 * state of the pool.
1372bc758434SLin Ling 		 */
1373bc758434SLin Ling 		if (!spa->spa_load_verbatim &&
1374b16da2e2SGeorge Wilson 		    spa_load_state(spa) == SPA_LOAD_OPEN &&
1375bc758434SLin Ling 		    state != POOL_STATE_ACTIVE)
13760bf246f5Smc 			return (EBADF);
1377560e6e96Seschrock 
137851ece835Seschrock 		/*
137951ece835Seschrock 		 * If we were able to open and validate a vdev that was
138051ece835Seschrock 		 * previously marked permanently unavailable, clear that state
138151ece835Seschrock 		 * now.
138251ece835Seschrock 		 */
138351ece835Seschrock 		if (vd->vdev_not_present)
138451ece835Seschrock 			vd->vdev_not_present = 0;
138551ece835Seschrock 	}
1386560e6e96Seschrock 
1387560e6e96Seschrock 	return (0);
1388560e6e96Seschrock }
1389560e6e96Seschrock 
1390fa9e4066Sahrens /*
1391fa9e4066Sahrens  * Close a virtual device.
1392fa9e4066Sahrens  */
1393fa9e4066Sahrens void
1394fa9e4066Sahrens vdev_close(vdev_t *vd)
1395fa9e4066Sahrens {
13968ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1397095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
13988ad4d6ddSJeff Bonwick 
13998ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
14008ad4d6ddSJeff Bonwick 
14011195e687SMark J Musante 	/*
14021195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
14031195e687SMark J Musante 	 * going offline.
14041195e687SMark J Musante 	 */
1405095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
14061195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1407095bcd66SGeorge Wilson 
1408fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1409fa9e4066Sahrens 
14103d7072f8Seschrock 	vdev_cache_purge(vd);
1411fa9e4066Sahrens 
1412560e6e96Seschrock 	/*
1413573ca77eSGeorge Wilson 	 * We record the previous state before we close it, so that if we are
1414560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1415560e6e96Seschrock 	 * it's still faulted.
1416560e6e96Seschrock 	 */
1417560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
1418560e6e96Seschrock 
1419fa9e4066Sahrens 	if (vd->vdev_offline)
1420fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1421fa9e4066Sahrens 	else
1422fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
1423ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1424fa9e4066Sahrens }
1425fa9e4066Sahrens 
1426dcba9f3fSGeorge Wilson void
1427dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
1428dcba9f3fSGeorge Wilson {
1429dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1430dcba9f3fSGeorge Wilson 
1431dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1432dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1433dcba9f3fSGeorge Wilson 		return;
1434dcba9f3fSGeorge Wilson 
1435dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1436dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
1437dcba9f3fSGeorge Wilson 
1438dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1439dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
1440dcba9f3fSGeorge Wilson }
1441dcba9f3fSGeorge Wilson 
1442dcba9f3fSGeorge Wilson void
1443dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
1444dcba9f3fSGeorge Wilson {
1445dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1446dcba9f3fSGeorge Wilson 
1447dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1448dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1449dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
1450dcba9f3fSGeorge Wilson 
1451dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1452dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
1453dcba9f3fSGeorge Wilson }
1454dcba9f3fSGeorge Wilson 
1455095bcd66SGeorge Wilson /*
1456095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1457095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
1458095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1459095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
1460095bcd66SGeorge Wilson  */
1461fa9e4066Sahrens void
1462ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
1463fa9e4066Sahrens {
1464ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
1465fa9e4066Sahrens 
1466e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1467ea8dc4b6Seschrock 
14681195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
14691195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
1470fa9e4066Sahrens 	vdev_close(vd);
1471fa9e4066Sahrens 	(void) vdev_open(vd);
1472fa9e4066Sahrens 
147339c23413Seschrock 	/*
147439c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
147539c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
147639c23413Seschrock 	 * opened in response to vdev_reopen().
147739c23413Seschrock 	 */
1478c5904d13Seschrock 	if (vd->vdev_aux) {
1479c5904d13Seschrock 		(void) vdev_validate_aux(vd);
1480e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
14816809eb4eSEric Schrock 		    vd->vdev_aux == &spa->spa_l2cache &&
1482573ca77eSGeorge Wilson 		    !l2arc_vdev_present(vd))
1483573ca77eSGeorge Wilson 			l2arc_add_vdev(spa, vd);
1484c5904d13Seschrock 	} else {
1485c5904d13Seschrock 		(void) vdev_validate(vd);
1486c5904d13Seschrock 	}
148739c23413Seschrock 
1488fa9e4066Sahrens 	/*
14893d7072f8Seschrock 	 * Reassess parent vdev's health.
1490fa9e4066Sahrens 	 */
14913d7072f8Seschrock 	vdev_propagate_state(vd);
1492fa9e4066Sahrens }
1493fa9e4066Sahrens 
1494fa9e4066Sahrens int
149599653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1496fa9e4066Sahrens {
1497fa9e4066Sahrens 	int error;
1498fa9e4066Sahrens 
1499fa9e4066Sahrens 	/*
1500fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1501fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
1502fa9e4066Sahrens 	 * there are any components we can't open.
1503fa9e4066Sahrens 	 */
1504fa9e4066Sahrens 	error = vdev_open(vd);
1505fa9e4066Sahrens 
1506fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1507fa9e4066Sahrens 		vdev_close(vd);
1508fa9e4066Sahrens 		return (error ? error : ENXIO);
1509fa9e4066Sahrens 	}
1510fa9e4066Sahrens 
1511fa9e4066Sahrens 	/*
1512fa9e4066Sahrens 	 * Recursively initialize all labels.
1513fa9e4066Sahrens 	 */
151439c23413Seschrock 	if ((error = vdev_label_init(vd, txg, isreplacing ?
151539c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1516fa9e4066Sahrens 		vdev_close(vd);
1517fa9e4066Sahrens 		return (error);
1518fa9e4066Sahrens 	}
1519fa9e4066Sahrens 
1520fa9e4066Sahrens 	return (0);
1521fa9e4066Sahrens }
1522fa9e4066Sahrens 
15230e34b6a7Sbonwick void
1524573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
1525fa9e4066Sahrens {
1526fa9e4066Sahrens 	/*
1527fa9e4066Sahrens 	 * Aim for roughly 200 metaslabs per vdev.
1528fa9e4066Sahrens 	 */
1529fa9e4066Sahrens 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1530fa9e4066Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1531fa9e4066Sahrens }
1532fa9e4066Sahrens 
1533fa9e4066Sahrens void
1534ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1535fa9e4066Sahrens {
1536ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
153788ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
1538ecc2d604Sbonwick 	ASSERT(ISP2(flags));
1539fa9e4066Sahrens 
1540ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
1541ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1542ecc2d604Sbonwick 
1543ecc2d604Sbonwick 	if (flags & VDD_DTL)
1544ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1545ecc2d604Sbonwick 
1546ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1547fa9e4066Sahrens }
1548fa9e4066Sahrens 
15498ad4d6ddSJeff Bonwick /*
15508ad4d6ddSJeff Bonwick  * DTLs.
15518ad4d6ddSJeff Bonwick  *
15528ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
15539fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
15548ad4d6ddSJeff Bonwick  *
15558ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
15568ad4d6ddSJeff Bonwick  *
15578ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
15588ad4d6ddSJeff Bonwick  *
15598ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
15608ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
15618ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
15628ad4d6ddSJeff Bonwick  *
15638ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
15648ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
15658ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
15668ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
15678ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
15688ad4d6ddSJeff Bonwick  *
15698ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
15708ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
15718ad4d6ddSJeff Bonwick  *
15728ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
15738ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
15748ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
15758ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
15768ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
15778ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
15788ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
15798ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
15808ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
15818ad4d6ddSJeff Bonwick  *
15828ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
15838ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
15848ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
15858ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
15868ad4d6ddSJeff Bonwick  */
1587fa9e4066Sahrens void
15888ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1589fa9e4066Sahrens {
15908ad4d6ddSJeff Bonwick 	space_map_t *sm = &vd->vdev_dtl[t];
15918ad4d6ddSJeff Bonwick 
15928ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
15938ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
15948ad4d6ddSJeff Bonwick 
1595fa9e4066Sahrens 	mutex_enter(sm->sm_lock);
1596fa9e4066Sahrens 	if (!space_map_contains(sm, txg, size))
1597fa9e4066Sahrens 		space_map_add(sm, txg, size);
1598fa9e4066Sahrens 	mutex_exit(sm->sm_lock);
1599fa9e4066Sahrens }
1600fa9e4066Sahrens 
16018ad4d6ddSJeff Bonwick boolean_t
16028ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1603fa9e4066Sahrens {
16048ad4d6ddSJeff Bonwick 	space_map_t *sm = &vd->vdev_dtl[t];
16058ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
1606fa9e4066Sahrens 
16078ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
16088ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1609fa9e4066Sahrens 
1610fa9e4066Sahrens 	mutex_enter(sm->sm_lock);
16118ad4d6ddSJeff Bonwick 	if (sm->sm_space != 0)
16128ad4d6ddSJeff Bonwick 		dirty = space_map_contains(sm, txg, size);
1613fa9e4066Sahrens 	mutex_exit(sm->sm_lock);
1614fa9e4066Sahrens 
1615fa9e4066Sahrens 	return (dirty);
1616fa9e4066Sahrens }
1617fa9e4066Sahrens 
16188ad4d6ddSJeff Bonwick boolean_t
16198ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
16208ad4d6ddSJeff Bonwick {
16218ad4d6ddSJeff Bonwick 	space_map_t *sm = &vd->vdev_dtl[t];
16228ad4d6ddSJeff Bonwick 	boolean_t empty;
16238ad4d6ddSJeff Bonwick 
16248ad4d6ddSJeff Bonwick 	mutex_enter(sm->sm_lock);
16258ad4d6ddSJeff Bonwick 	empty = (sm->sm_space == 0);
16268ad4d6ddSJeff Bonwick 	mutex_exit(sm->sm_lock);
16278ad4d6ddSJeff Bonwick 
16288ad4d6ddSJeff Bonwick 	return (empty);
16298ad4d6ddSJeff Bonwick }
16308ad4d6ddSJeff Bonwick 
1631fa9e4066Sahrens /*
1632fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
1633fa9e4066Sahrens  */
1634fa9e4066Sahrens void
1635fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1636fa9e4066Sahrens {
1637ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
16388ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
16398ad4d6ddSJeff Bonwick 	int minref;
1640fa9e4066Sahrens 
16418ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1642fa9e4066Sahrens 
16438ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
16448ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
16458ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
16468ad4d6ddSJeff Bonwick 
1647b24ab676SJeff Bonwick 	if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
16488ad4d6ddSJeff Bonwick 		return;
16498ad4d6ddSJeff Bonwick 
16508ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
1651fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
1652088f3894Sahrens 		if (scrub_txg != 0 &&
1653088f3894Sahrens 		    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
1654088f3894Sahrens 			/*
1655088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
1656088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
1657088f3894Sahrens 			 * will be valid, so excise the old region and
1658088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
1659088f3894Sahrens 			 * dtl as-is if there was an error.
16608ad4d6ddSJeff Bonwick 			 *
16618ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
16628ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
16638ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
16648ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
16658ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
16668ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
16678ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
16688ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
16698ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
1670088f3894Sahrens 			 */
16718ad4d6ddSJeff Bonwick 			space_map_ref_create(&reftree);
16728ad4d6ddSJeff Bonwick 			space_map_ref_add_map(&reftree,
16738ad4d6ddSJeff Bonwick 			    &vd->vdev_dtl[DTL_MISSING], 1);
16748ad4d6ddSJeff Bonwick 			space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
16758ad4d6ddSJeff Bonwick 			space_map_ref_add_map(&reftree,
16768ad4d6ddSJeff Bonwick 			    &vd->vdev_dtl[DTL_SCRUB], 2);
16778ad4d6ddSJeff Bonwick 			space_map_ref_generate_map(&reftree,
16788ad4d6ddSJeff Bonwick 			    &vd->vdev_dtl[DTL_MISSING], 1);
16798ad4d6ddSJeff Bonwick 			space_map_ref_destroy(&reftree);
1680fa9e4066Sahrens 		}
16818ad4d6ddSJeff Bonwick 		space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
16828ad4d6ddSJeff Bonwick 		space_map_walk(&vd->vdev_dtl[DTL_MISSING],
16838ad4d6ddSJeff Bonwick 		    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1684fa9e4066Sahrens 		if (scrub_done)
16858ad4d6ddSJeff Bonwick 			space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
16868ad4d6ddSJeff Bonwick 		space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
16878ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
16888ad4d6ddSJeff Bonwick 			space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
16898ad4d6ddSJeff Bonwick 		else
16908ad4d6ddSJeff Bonwick 			space_map_walk(&vd->vdev_dtl[DTL_MISSING],
16918ad4d6ddSJeff Bonwick 			    space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1692fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
1693088f3894Sahrens 
1694ecc2d604Sbonwick 		if (txg != 0)
1695ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1696fa9e4066Sahrens 		return;
1697fa9e4066Sahrens 	}
1698fa9e4066Sahrens 
1699fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17008ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
170199bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
170299bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
17038ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
17048ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
17058ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
17068ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
17078ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
17088ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
17098ad4d6ddSJeff Bonwick 		else
17108ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
17118ad4d6ddSJeff Bonwick 		space_map_ref_create(&reftree);
17128ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
17138ad4d6ddSJeff Bonwick 			vdev_t *cvd = vd->vdev_child[c];
17148ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
171599bb17e2SEric Taylor 			space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1);
17168ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
17178ad4d6ddSJeff Bonwick 		}
17188ad4d6ddSJeff Bonwick 		space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
17198ad4d6ddSJeff Bonwick 		space_map_ref_destroy(&reftree);
1720fa9e4066Sahrens 	}
17218ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
1722fa9e4066Sahrens }
1723fa9e4066Sahrens 
1724fa9e4066Sahrens static int
1725fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
1726fa9e4066Sahrens {
1727fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
17288ad4d6ddSJeff Bonwick 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
1729ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1730fa9e4066Sahrens 	dmu_buf_t *db;
1731fa9e4066Sahrens 	int error;
1732fa9e4066Sahrens 
1733fa9e4066Sahrens 	ASSERT(vd->vdev_children == 0);
1734fa9e4066Sahrens 
1735fa9e4066Sahrens 	if (smo->smo_object == 0)
1736fa9e4066Sahrens 		return (0);
1737fa9e4066Sahrens 
173888ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
173988ecc943SGeorge Wilson 
1740ecc2d604Sbonwick 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1741ea8dc4b6Seschrock 		return (error);
1742ecc2d604Sbonwick 
17431934e92fSmaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
17441934e92fSmaybee 	bcopy(db->db_data, smo, sizeof (*smo));
1745ea8dc4b6Seschrock 	dmu_buf_rele(db, FTAG);
1746fa9e4066Sahrens 
1747fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
17488ad4d6ddSJeff Bonwick 	error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
17498ad4d6ddSJeff Bonwick 	    NULL, SM_ALLOC, smo, mos);
1750fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1751fa9e4066Sahrens 
1752fa9e4066Sahrens 	return (error);
1753fa9e4066Sahrens }
1754fa9e4066Sahrens 
1755fa9e4066Sahrens void
1756fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1757fa9e4066Sahrens {
1758fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
17598ad4d6ddSJeff Bonwick 	space_map_obj_t *smo = &vd->vdev_dtl_smo;
17608ad4d6ddSJeff Bonwick 	space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
1761ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1762fa9e4066Sahrens 	space_map_t smsync;
1763fa9e4066Sahrens 	kmutex_t smlock;
1764fa9e4066Sahrens 	dmu_buf_t *db;
1765fa9e4066Sahrens 	dmu_tx_t *tx;
1766fa9e4066Sahrens 
176788ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
176888ecc943SGeorge Wilson 
1769fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1770fa9e4066Sahrens 
1771fa9e4066Sahrens 	if (vd->vdev_detached) {
1772fa9e4066Sahrens 		if (smo->smo_object != 0) {
1773ecc2d604Sbonwick 			int err = dmu_object_free(mos, smo->smo_object, tx);
1774fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
1775fa9e4066Sahrens 			smo->smo_object = 0;
1776fa9e4066Sahrens 		}
1777fa9e4066Sahrens 		dmu_tx_commit(tx);
1778fa9e4066Sahrens 		return;
1779fa9e4066Sahrens 	}
1780fa9e4066Sahrens 
1781fa9e4066Sahrens 	if (smo->smo_object == 0) {
1782fa9e4066Sahrens 		ASSERT(smo->smo_objsize == 0);
1783fa9e4066Sahrens 		ASSERT(smo->smo_alloc == 0);
1784ecc2d604Sbonwick 		smo->smo_object = dmu_object_alloc(mos,
1785fa9e4066Sahrens 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1786fa9e4066Sahrens 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1787fa9e4066Sahrens 		ASSERT(smo->smo_object != 0);
1788fa9e4066Sahrens 		vdev_config_dirty(vd->vdev_top);
1789fa9e4066Sahrens 	}
1790fa9e4066Sahrens 
1791fa9e4066Sahrens 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1792fa9e4066Sahrens 
1793fa9e4066Sahrens 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1794fa9e4066Sahrens 	    &smlock);
1795fa9e4066Sahrens 
1796fa9e4066Sahrens 	mutex_enter(&smlock);
1797fa9e4066Sahrens 
1798fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
1799ecc2d604Sbonwick 	space_map_walk(sm, space_map_add, &smsync);
1800fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1801fa9e4066Sahrens 
1802ecc2d604Sbonwick 	space_map_truncate(smo, mos, tx);
1803ecc2d604Sbonwick 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1804fa9e4066Sahrens 
1805fa9e4066Sahrens 	space_map_destroy(&smsync);
1806fa9e4066Sahrens 
1807fa9e4066Sahrens 	mutex_exit(&smlock);
1808fa9e4066Sahrens 	mutex_destroy(&smlock);
1809fa9e4066Sahrens 
1810ecc2d604Sbonwick 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1811fa9e4066Sahrens 	dmu_buf_will_dirty(db, tx);
18121934e92fSmaybee 	ASSERT3U(db->db_size, >=, sizeof (*smo));
18131934e92fSmaybee 	bcopy(smo, db->db_data, sizeof (*smo));
1814ea8dc4b6Seschrock 	dmu_buf_rele(db, FTAG);
1815fa9e4066Sahrens 
1816fa9e4066Sahrens 	dmu_tx_commit(tx);
1817fa9e4066Sahrens }
1818fa9e4066Sahrens 
18198ad4d6ddSJeff Bonwick /*
18208ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
18218ad4d6ddSJeff Bonwick  * without losing data.
18228ad4d6ddSJeff Bonwick  */
18238ad4d6ddSJeff Bonwick boolean_t
18248ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
18258ad4d6ddSJeff Bonwick {
18268ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
18278ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
18288ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
18298ad4d6ddSJeff Bonwick 	boolean_t required;
18308ad4d6ddSJeff Bonwick 
18318ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
18328ad4d6ddSJeff Bonwick 
18338ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
18348ad4d6ddSJeff Bonwick 		return (B_TRUE);
18358ad4d6ddSJeff Bonwick 
18368ad4d6ddSJeff Bonwick 	/*
18378ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
18388ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
18398ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
18408ad4d6ddSJeff Bonwick 	 */
18418ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
18428ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
18438ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
18448ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
18458ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
18468ad4d6ddSJeff Bonwick 
18478ad4d6ddSJeff Bonwick 	return (required);
18488ad4d6ddSJeff Bonwick }
18498ad4d6ddSJeff Bonwick 
1850088f3894Sahrens /*
1851088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
1852088f3894Sahrens  */
1853088f3894Sahrens boolean_t
1854088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
1855088f3894Sahrens {
1856088f3894Sahrens 	boolean_t needed = B_FALSE;
1857088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
1858088f3894Sahrens 	uint64_t thismax = 0;
1859088f3894Sahrens 
1860088f3894Sahrens 	if (vd->vdev_children == 0) {
1861088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
18628ad4d6ddSJeff Bonwick 		if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
18638ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
1864088f3894Sahrens 			space_seg_t *ss;
1865088f3894Sahrens 
18668ad4d6ddSJeff Bonwick 			ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
1867088f3894Sahrens 			thismin = ss->ss_start - 1;
18688ad4d6ddSJeff Bonwick 			ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
1869088f3894Sahrens 			thismax = ss->ss_end;
1870088f3894Sahrens 			needed = B_TRUE;
1871088f3894Sahrens 		}
1872088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
1873088f3894Sahrens 	} else {
18748ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
1875088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
1876088f3894Sahrens 			uint64_t cmin, cmax;
1877088f3894Sahrens 
1878088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
1879088f3894Sahrens 				thismin = MIN(thismin, cmin);
1880088f3894Sahrens 				thismax = MAX(thismax, cmax);
1881088f3894Sahrens 				needed = B_TRUE;
1882088f3894Sahrens 			}
1883088f3894Sahrens 		}
1884088f3894Sahrens 	}
1885088f3894Sahrens 
1886088f3894Sahrens 	if (needed && minp) {
1887088f3894Sahrens 		*minp = thismin;
1888088f3894Sahrens 		*maxp = thismax;
1889088f3894Sahrens 	}
1890088f3894Sahrens 	return (needed);
1891088f3894Sahrens }
1892088f3894Sahrens 
1893560e6e96Seschrock void
1894ea8dc4b6Seschrock vdev_load(vdev_t *vd)
1895fa9e4066Sahrens {
1896fa9e4066Sahrens 	/*
1897fa9e4066Sahrens 	 * Recursively load all children.
1898fa9e4066Sahrens 	 */
18998ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1900560e6e96Seschrock 		vdev_load(vd->vdev_child[c]);
1901fa9e4066Sahrens 
1902fa9e4066Sahrens 	/*
19030e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
1904fa9e4066Sahrens 	 */
190588ecc943SGeorge Wilson 	if (vd == vd->vdev_top && !vd->vdev_ishole &&
1906560e6e96Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1907560e6e96Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
1908560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1909560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1910fa9e4066Sahrens 
1911fa9e4066Sahrens 	/*
1912fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
1913fa9e4066Sahrens 	 */
1914560e6e96Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1915560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1916560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1917fa9e4066Sahrens }
1918fa9e4066Sahrens 
191999653d4eSeschrock /*
1920fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
1921fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
1922fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
1923fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
1924fa94a07fSbrendan  * repurposed to another pool.
192599653d4eSeschrock  */
192699653d4eSeschrock int
1927fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
192899653d4eSeschrock {
192999653d4eSeschrock 	nvlist_t *label;
193099653d4eSeschrock 	uint64_t guid, version;
193199653d4eSeschrock 	uint64_t state;
193299653d4eSeschrock 
1933e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
1934c5904d13Seschrock 		return (0);
1935c5904d13Seschrock 
193699653d4eSeschrock 	if ((label = vdev_label_read_config(vd)) == NULL) {
193799653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
193899653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
193999653d4eSeschrock 		return (-1);
194099653d4eSeschrock 	}
194199653d4eSeschrock 
194299653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1943e7437265Sahrens 	    version > SPA_VERSION ||
194499653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
194599653d4eSeschrock 	    guid != vd->vdev_guid ||
194699653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
194799653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
194899653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
194999653d4eSeschrock 		nvlist_free(label);
195099653d4eSeschrock 		return (-1);
195199653d4eSeschrock 	}
195299653d4eSeschrock 
195399653d4eSeschrock 	/*
195499653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
195599653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
195699653d4eSeschrock 	 */
195799653d4eSeschrock 	nvlist_free(label);
195899653d4eSeschrock 	return (0);
195999653d4eSeschrock }
196099653d4eSeschrock 
196188ecc943SGeorge Wilson void
196288ecc943SGeorge Wilson vdev_remove(vdev_t *vd, uint64_t txg)
196388ecc943SGeorge Wilson {
196488ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
196588ecc943SGeorge Wilson 	objset_t *mos = spa->spa_meta_objset;
196688ecc943SGeorge Wilson 	dmu_tx_t *tx;
196788ecc943SGeorge Wilson 
196888ecc943SGeorge Wilson 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
196988ecc943SGeorge Wilson 
197088ecc943SGeorge Wilson 	if (vd->vdev_dtl_smo.smo_object) {
197188ecc943SGeorge Wilson 		ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0);
197288ecc943SGeorge Wilson 		(void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
197388ecc943SGeorge Wilson 		vd->vdev_dtl_smo.smo_object = 0;
197488ecc943SGeorge Wilson 	}
197588ecc943SGeorge Wilson 
197688ecc943SGeorge Wilson 	if (vd->vdev_ms != NULL) {
197788ecc943SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
197888ecc943SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
197988ecc943SGeorge Wilson 
198088ecc943SGeorge Wilson 			if (msp == NULL || msp->ms_smo.smo_object == 0)
198188ecc943SGeorge Wilson 				continue;
198288ecc943SGeorge Wilson 
198388ecc943SGeorge Wilson 			ASSERT3U(msp->ms_smo.smo_alloc, ==, 0);
198488ecc943SGeorge Wilson 			(void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
198588ecc943SGeorge Wilson 			msp->ms_smo.smo_object = 0;
198688ecc943SGeorge Wilson 		}
198788ecc943SGeorge Wilson 	}
198888ecc943SGeorge Wilson 
198988ecc943SGeorge Wilson 	if (vd->vdev_ms_array) {
199088ecc943SGeorge Wilson 		(void) dmu_object_free(mos, vd->vdev_ms_array, tx);
199188ecc943SGeorge Wilson 		vd->vdev_ms_array = 0;
199288ecc943SGeorge Wilson 		vd->vdev_ms_shift = 0;
199388ecc943SGeorge Wilson 	}
199488ecc943SGeorge Wilson 	dmu_tx_commit(tx);
199588ecc943SGeorge Wilson }
199688ecc943SGeorge Wilson 
1997fa9e4066Sahrens void
1998fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
1999fa9e4066Sahrens {
2000fa9e4066Sahrens 	metaslab_t *msp;
200180eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2002fa9e4066Sahrens 
200388ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
200488ecc943SGeorge Wilson 
2005fa9e4066Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2006fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
200780eb36f2SGeorge Wilson 
200880eb36f2SGeorge Wilson 	if (reassess)
200980eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
2010fa9e4066Sahrens }
2011fa9e4066Sahrens 
2012fa9e4066Sahrens void
2013fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2014fa9e4066Sahrens {
2015fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2016fa9e4066Sahrens 	vdev_t *lvd;
2017fa9e4066Sahrens 	metaslab_t *msp;
2018ecc2d604Sbonwick 	dmu_tx_t *tx;
2019fa9e4066Sahrens 
202088ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
202188ecc943SGeorge Wilson 
2022ecc2d604Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2023ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
2024ecc2d604Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2025ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2026ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2027ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
2028ecc2d604Sbonwick 		vdev_config_dirty(vd);
2029ecc2d604Sbonwick 		dmu_tx_commit(tx);
2030ecc2d604Sbonwick 	}
2031fa9e4066Sahrens 
203288ecc943SGeorge Wilson 	if (vd->vdev_removing)
203388ecc943SGeorge Wilson 		vdev_remove(vd, txg);
203488ecc943SGeorge Wilson 
2035ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2036fa9e4066Sahrens 		metaslab_sync(msp, txg);
2037ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2038ecc2d604Sbonwick 	}
2039fa9e4066Sahrens 
2040fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2041fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
2042fa9e4066Sahrens 
2043fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2044fa9e4066Sahrens }
2045fa9e4066Sahrens 
2046fa9e4066Sahrens uint64_t
2047fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2048fa9e4066Sahrens {
2049fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2050fa9e4066Sahrens }
2051fa9e4066Sahrens 
20523d7072f8Seschrock /*
20533d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
20543d7072f8Seschrock  * not be opened, and no I/O is attempted.
20553d7072f8Seschrock  */
2056fa9e4066Sahrens int
2057069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2058fa9e4066Sahrens {
2059c5904d13Seschrock 	vdev_t *vd;
2060fa9e4066Sahrens 
20618f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
2062fa9e4066Sahrens 
2063c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2064e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2065e14bb325SJeff Bonwick 
20663d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2067e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2068fa9e4066Sahrens 
2069069f55e2SEric Schrock 	/*
2070069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
2071069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
2072069f55e2SEric Schrock 	 * were faulted.
2073069f55e2SEric Schrock 	 */
2074069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
2075069f55e2SEric Schrock 
20763d7072f8Seschrock 	/*
20773d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
20783d7072f8Seschrock 	 */
2079*98d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
20803d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
20813d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
2082069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
20833d7072f8Seschrock 
20843d7072f8Seschrock 	/*
2085c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
2086c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
20873d7072f8Seschrock 	 */
2088c79790bcSGeorge Wilson 	if (!vd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
20893d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
20903d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
20913d7072f8Seschrock 
20923d7072f8Seschrock 		/*
20933d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
20943d7072f8Seschrock 		 * mark it degraded.
20953d7072f8Seschrock 		 */
20963d7072f8Seschrock 		vdev_reopen(vd);
20973d7072f8Seschrock 
2098069f55e2SEric Schrock 		if (vdev_readable(vd))
2099069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
21003d7072f8Seschrock 	}
21013d7072f8Seschrock 
2102e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
21033d7072f8Seschrock }
21043d7072f8Seschrock 
21053d7072f8Seschrock /*
21063d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
21073d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
21083d7072f8Seschrock  * as I/O is concerned.
21093d7072f8Seschrock  */
21103d7072f8Seschrock int
2111069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
21123d7072f8Seschrock {
2113c5904d13Seschrock 	vdev_t *vd;
21140a4e9518Sgw 
21158f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
21163d7072f8Seschrock 
2117c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2118e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2119e14bb325SJeff Bonwick 
21200e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2121e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
21220e34b6a7Sbonwick 
21233d7072f8Seschrock 	/*
21243d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
21253d7072f8Seschrock 	 */
2126e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
2127e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
21283d7072f8Seschrock 
21293d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
21303d7072f8Seschrock 	if (!vdev_is_dead(vd))
21313d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2132069f55e2SEric Schrock 		    aux);
21333d7072f8Seschrock 
2134e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
21353d7072f8Seschrock }
21363d7072f8Seschrock 
21373d7072f8Seschrock /*
21383d7072f8Seschrock  * Online the given vdev.  If 'unspare' is set, it implies two things.  First,
21393d7072f8Seschrock  * any attached spare device should be detached when the device finishes
21403d7072f8Seschrock  * resilvering.  Second, the online should be treated like a 'test' online case,
21413d7072f8Seschrock  * so no FMA events are generated if the device fails to open.
21423d7072f8Seschrock  */
21433d7072f8Seschrock int
2144e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
21453d7072f8Seschrock {
2146573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
21473d7072f8Seschrock 
21488f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
21493d7072f8Seschrock 
2150c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2151e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
21523d7072f8Seschrock 
21533d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2154e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2155fa9e4066Sahrens 
2156573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
2157fa9e4066Sahrens 	vd->vdev_offline = B_FALSE;
2158441d80aaSlling 	vd->vdev_tmpoffline = B_FALSE;
2159e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2160e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2161573ca77eSGeorge Wilson 
2162573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
2163573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2164573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2165573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2166573ca77eSGeorge Wilson 	}
2167573ca77eSGeorge Wilson 
2168573ca77eSGeorge Wilson 	vdev_reopen(tvd);
21693d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
21703d7072f8Seschrock 
2171573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2172573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2173573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
2174573ca77eSGeorge Wilson 	}
2175573ca77eSGeorge Wilson 
21763d7072f8Seschrock 	if (newstate)
21773d7072f8Seschrock 		*newstate = vd->vdev_state;
21783d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
21793d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
21803d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
21813d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
21823d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
2183fa9e4066Sahrens 
2184573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2185573ca77eSGeorge Wilson 
2186573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
2187573ca77eSGeorge Wilson 		if (vd->vdev_aux)
2188573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2189573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2190573ca77eSGeorge Wilson 	}
21918ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
2192fa9e4066Sahrens }
2193fa9e4066Sahrens 
2194a1521560SJeff Bonwick static int
2195a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2196fa9e4066Sahrens {
2197e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
21988f18d1faSGeorge Wilson 	int error = 0;
21998f18d1faSGeorge Wilson 	uint64_t generation;
22008f18d1faSGeorge Wilson 	metaslab_group_t *mg;
22010a4e9518Sgw 
22028f18d1faSGeorge Wilson top:
22038f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
2204fa9e4066Sahrens 
2205c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2206e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2207fa9e4066Sahrens 
22080e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2209e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
22100e34b6a7Sbonwick 
2211e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
22128f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
22138f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
2214e6ca193dSGeorge Wilson 
2215fa9e4066Sahrens 	/*
2216ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
2217fa9e4066Sahrens 	 */
2218ecc2d604Sbonwick 	if (!vd->vdev_offline) {
2219ecc2d604Sbonwick 		/*
22208ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
2221e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
2222e6ca193dSGeorge Wilson 		 * expendable.
2223ecc2d604Sbonwick 		 */
2224e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2225e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
2226e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2227fa9e4066Sahrens 
22288f18d1faSGeorge Wilson 		/*
2229b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
2230b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
2231b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
2232b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
22338f18d1faSGeorge Wilson 		 */
22348f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
22358f18d1faSGeorge Wilson 			/*
22368f18d1faSGeorge Wilson 			 * Prevent any future allocations.
22378f18d1faSGeorge Wilson 			 */
2238a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
22398f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
22408f18d1faSGeorge Wilson 
22411195e687SMark J Musante 			error = spa_offline_log(spa);
22428f18d1faSGeorge Wilson 
22438f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
22448f18d1faSGeorge Wilson 
22458f18d1faSGeorge Wilson 			/*
22468f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
22478f18d1faSGeorge Wilson 			 */
22488f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
2249a1521560SJeff Bonwick 				metaslab_group_activate(mg);
22508f18d1faSGeorge Wilson 				if (error)
22518f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
22528f18d1faSGeorge Wilson 					    vd, error));
22538f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
22548f18d1faSGeorge Wilson 				goto top;
22558f18d1faSGeorge Wilson 			}
22568f18d1faSGeorge Wilson 			ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0);
22578f18d1faSGeorge Wilson 		}
22588f18d1faSGeorge Wilson 
2259ecc2d604Sbonwick 		/*
2260ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
2261e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
2262e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
2263e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
2264ecc2d604Sbonwick 		 */
2265ecc2d604Sbonwick 		vd->vdev_offline = B_TRUE;
2266e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
2267e6ca193dSGeorge Wilson 
2268e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2269e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
2270ecc2d604Sbonwick 			vd->vdev_offline = B_FALSE;
2271e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
2272e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2273ecc2d604Sbonwick 		}
22748f18d1faSGeorge Wilson 
22758f18d1faSGeorge Wilson 		/*
22768f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
22778f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
22788f18d1faSGeorge Wilson 		 */
22798f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
2280a1521560SJeff Bonwick 			metaslab_group_activate(mg);
2281fa9e4066Sahrens 	}
2282fa9e4066Sahrens 
2283e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2284ecc2d604Sbonwick 
22858f18d1faSGeorge Wilson 	return (spa_vdev_state_exit(spa, vd, 0));
2286fa9e4066Sahrens }
2287fa9e4066Sahrens 
2288a1521560SJeff Bonwick int
2289a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2290a1521560SJeff Bonwick {
2291a1521560SJeff Bonwick 	int error;
2292a1521560SJeff Bonwick 
2293a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
2294a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
2295a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
2296a1521560SJeff Bonwick 
2297a1521560SJeff Bonwick 	return (error);
2298a1521560SJeff Bonwick }
2299a1521560SJeff Bonwick 
2300ea8dc4b6Seschrock /*
2301ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
2302ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
2303ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
2304ea8dc4b6Seschrock  */
2305ea8dc4b6Seschrock void
2306e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
2307fa9e4066Sahrens {
2308e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2309e14bb325SJeff Bonwick 
2310e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2311fa9e4066Sahrens 
2312ea8dc4b6Seschrock 	if (vd == NULL)
2313e14bb325SJeff Bonwick 		vd = rvd;
2314fa9e4066Sahrens 
2315ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
2316ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
2317ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2318fa9e4066Sahrens 
2319e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2320e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
23213d7072f8Seschrock 
23223d7072f8Seschrock 	/*
23238a79c1b5Sek 	 * If we're in the FAULTED state or have experienced failed I/O, then
23248a79c1b5Sek 	 * clear the persistent state and attempt to reopen the device.  We
23258a79c1b5Sek 	 * also mark the vdev config dirty, so that the new faulted state is
23268a79c1b5Sek 	 * written out to disk.
23273d7072f8Seschrock 	 */
2328e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
2329e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
23308a79c1b5Sek 
2331096d22d4SEric Schrock 		/*
2332096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
2333096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
2334096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
2335096d22d4SEric Schrock 		 */
2336096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
2337096d22d4SEric Schrock 
23383d7072f8Seschrock 		vd->vdev_faulted = vd->vdev_degraded = 0;
2339e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
2340e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
2341e14bb325SJeff Bonwick 
23423d7072f8Seschrock 		vdev_reopen(vd);
23433d7072f8Seschrock 
2344096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
2345096d22d4SEric Schrock 
2346e14bb325SJeff Bonwick 		if (vd != rvd)
2347e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
2348e14bb325SJeff Bonwick 
2349e14bb325SJeff Bonwick 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2350bb8b5132Sek 			spa_async_request(spa, SPA_ASYNC_RESILVER);
23513d7072f8Seschrock 
23523d7072f8Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
23533d7072f8Seschrock 	}
2354096d22d4SEric Schrock 
2355096d22d4SEric Schrock 	/*
2356096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
2357096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
2358096d22d4SEric Schrock 	 * done in response to the FMA fault.
2359096d22d4SEric Schrock 	 */
2360096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2361096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2362096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
2363096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
2364fa9e4066Sahrens }
2365fa9e4066Sahrens 
2366e14bb325SJeff Bonwick boolean_t
2367e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd)
23680a4e9518Sgw {
236988ecc943SGeorge Wilson 	/*
237088ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
237188ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
237288ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
237388ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
237488ecc943SGeorge Wilson 	 * before issuing I/O to them.
237588ecc943SGeorge Wilson 	 */
237688ecc943SGeorge Wilson 	return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
237788ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
23780a4e9518Sgw }
23790a4e9518Sgw 
2380e14bb325SJeff Bonwick boolean_t
2381e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
23820a4e9518Sgw {
2383e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
23840a4e9518Sgw }
23850a4e9518Sgw 
2386e14bb325SJeff Bonwick boolean_t
2387e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
2388fa9e4066Sahrens {
2389e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2390fa9e4066Sahrens }
2391fa9e4066Sahrens 
2392a31e6787SGeorge Wilson boolean_t
2393a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
2394a31e6787SGeorge Wilson {
23958ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
23968ad4d6ddSJeff Bonwick 
2397a31e6787SGeorge Wilson 	/*
23988ad4d6ddSJeff Bonwick 	 * We currently allow allocations from vdevs which may be in the
2399a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2400a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
24018ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
24028ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
24038ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
2404a31e6787SGeorge Wilson 	 */
24058ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
240688ecc943SGeorge Wilson 	    !vd->vdev_cant_write && !vd->vdev_ishole && !vd->vdev_removing);
2407a31e6787SGeorge Wilson }
2408a31e6787SGeorge Wilson 
2409e14bb325SJeff Bonwick boolean_t
2410e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
2411fa9e4066Sahrens {
2412e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
2413fa9e4066Sahrens 
2414e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2415e14bb325SJeff Bonwick 		return (B_FALSE);
2416fa9e4066Sahrens 
2417e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
2418e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
2419fa9e4066Sahrens 
2420e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
2421e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
2422fa9e4066Sahrens 
2423e14bb325SJeff Bonwick 	return (B_TRUE);
2424fa9e4066Sahrens }
2425fa9e4066Sahrens 
2426fa9e4066Sahrens /*
2427fa9e4066Sahrens  * Get statistics for the given vdev.
2428fa9e4066Sahrens  */
2429fa9e4066Sahrens void
2430fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2431fa9e4066Sahrens {
2432fa9e4066Sahrens 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2433fa9e4066Sahrens 
2434fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2435fa9e4066Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2436088f3894Sahrens 	vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
2437fa9e4066Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2438fa9e4066Sahrens 	vs->vs_state = vd->vdev_state;
2439573ca77eSGeorge Wilson 	vs->vs_rsize = vdev_get_min_asize(vd);
2440573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
2441573ca77eSGeorge Wilson 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2442fa9e4066Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2443fa9e4066Sahrens 
2444fa9e4066Sahrens 	/*
2445fa9e4066Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2446fa9e4066Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2447fa9e4066Sahrens 	 */
2448fa9e4066Sahrens 	if (vd == rvd) {
2449e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2450fa9e4066Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2451fa9e4066Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2452fa9e4066Sahrens 
2453fa9e4066Sahrens 			mutex_enter(&vd->vdev_stat_lock);
2454e14bb325SJeff Bonwick 			for (int t = 0; t < ZIO_TYPES; t++) {
2455fa9e4066Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2456fa9e4066Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2457fa9e4066Sahrens 			}
2458fa9e4066Sahrens 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
2459fa9e4066Sahrens 			mutex_exit(&vd->vdev_stat_lock);
2460fa9e4066Sahrens 		}
2461fa9e4066Sahrens 	}
2462fa9e4066Sahrens }
2463fa9e4066Sahrens 
2464fa94a07fSbrendan void
2465fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
2466fa94a07fSbrendan {
2467fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
2468fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
2469fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
2470fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
2471fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
2472fa94a07fSbrendan }
2473fa94a07fSbrendan 
2474fa9e4066Sahrens void
2475e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
2476fa9e4066Sahrens {
24778ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
24788ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2479e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2480fa9e4066Sahrens 	vdev_t *pvd;
2481fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
2482fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2483fa9e4066Sahrens 	zio_type_t type = zio->io_type;
2484fa9e4066Sahrens 	int flags = zio->io_flags;
2485fa9e4066Sahrens 
2486e14bb325SJeff Bonwick 	/*
2487e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
2488e14bb325SJeff Bonwick 	 */
2489e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
2490e14bb325SJeff Bonwick 		return;
2491e14bb325SJeff Bonwick 
2492fa9e4066Sahrens 	if (zio->io_error == 0) {
2493e14bb325SJeff Bonwick 		/*
2494e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
2495e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
2496e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
2497e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
2498e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
2499e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
2500e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
2501e14bb325SJeff Bonwick 		 *
2502e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
2503e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
2504e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
2505e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
2506e14bb325SJeff Bonwick 		 */
2507e14bb325SJeff Bonwick 		if (vd == rvd)
2508e14bb325SJeff Bonwick 			return;
2509e14bb325SJeff Bonwick 
2510e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
25118ad4d6ddSJeff Bonwick 
25128ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
25138ad4d6ddSJeff Bonwick 			return;
25148ad4d6ddSJeff Bonwick 
25158ad4d6ddSJeff Bonwick 		mutex_enter(&vd->vdev_stat_lock);
25168ad4d6ddSJeff Bonwick 
2517e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
2518d80c45e0Sbonwick 			if (flags & ZIO_FLAG_SCRUB_THREAD)
2519e14bb325SJeff Bonwick 				vs->vs_scrub_repaired += psize;
25208ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
2521e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
2522fa9e4066Sahrens 		}
25238ad4d6ddSJeff Bonwick 
25248ad4d6ddSJeff Bonwick 		vs->vs_ops[type]++;
25258ad4d6ddSJeff Bonwick 		vs->vs_bytes[type] += psize;
25268ad4d6ddSJeff Bonwick 
25278ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
2528fa9e4066Sahrens 		return;
2529fa9e4066Sahrens 	}
2530fa9e4066Sahrens 
2531fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2532fa9e4066Sahrens 		return;
2533fa9e4066Sahrens 
25348956713aSEric Schrock 	/*
25358956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
25368956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
25378956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
25388956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
25398956713aSEric Schrock 	 */
25408956713aSEric Schrock 	if (zio->io_error == EIO &&
25418956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
25428956713aSEric Schrock 		return;
25438956713aSEric Schrock 
25448f18d1faSGeorge Wilson 	/*
25458f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
25468f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
25478f18d1faSGeorge Wilson 	 * errors.
25488f18d1faSGeorge Wilson 	 */
25498f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
25508f18d1faSGeorge Wilson 		return;
25518f18d1faSGeorge Wilson 
2552e14bb325SJeff Bonwick 	mutex_enter(&vd->vdev_stat_lock);
2553b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2554e14bb325SJeff Bonwick 		if (zio->io_error == ECKSUM)
2555e14bb325SJeff Bonwick 			vs->vs_checksum_errors++;
2556e14bb325SJeff Bonwick 		else
2557e14bb325SJeff Bonwick 			vs->vs_read_errors++;
2558fa9e4066Sahrens 	}
2559b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2560e14bb325SJeff Bonwick 		vs->vs_write_errors++;
2561e14bb325SJeff Bonwick 	mutex_exit(&vd->vdev_stat_lock);
2562fa9e4066Sahrens 
25638ad4d6ddSJeff Bonwick 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
25648ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
2565b24ab676SJeff Bonwick 	    (flags & ZIO_FLAG_SCRUB_THREAD) ||
2566b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
25678ad4d6ddSJeff Bonwick 		/*
2568b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
2569b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
2570b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
2571b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
2572b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
2573b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
2574b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
2575b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
25768ad4d6ddSJeff Bonwick 		 *
25778ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
25788ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
25798ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
25808ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
25818ad4d6ddSJeff Bonwick 		 */
25828ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
25838ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
25848ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SCRUB_THREAD) {
25858ad4d6ddSJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
25868ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
25878ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2588b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
2589b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
2590b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2591b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
25928ad4d6ddSJeff Bonwick 			}
2593b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
25948ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2595fa9e4066Sahrens 				return;
25968ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
25978ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
25988ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2599fa9e4066Sahrens 		}
26008ad4d6ddSJeff Bonwick 		if (vd != rvd)
26018ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2602fa9e4066Sahrens 	}
2603fa9e4066Sahrens }
2604fa9e4066Sahrens 
2605fa9e4066Sahrens void
2606fa9e4066Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2607fa9e4066Sahrens {
2608fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2609fa9e4066Sahrens 
2610573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
2611fa9e4066Sahrens 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2612fa9e4066Sahrens 
2613fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2614fa9e4066Sahrens 
2615fa9e4066Sahrens 	if (type == POOL_SCRUB_NONE) {
2616fa9e4066Sahrens 		/*
2617fa9e4066Sahrens 		 * Update completion and end time.  Leave everything else alone
2618fa9e4066Sahrens 		 * so we can report what happened during the previous scrub.
2619fa9e4066Sahrens 		 */
2620fa9e4066Sahrens 		vs->vs_scrub_complete = complete;
2621fa9e4066Sahrens 		vs->vs_scrub_end = gethrestime_sec();
2622fa9e4066Sahrens 	} else {
2623fa9e4066Sahrens 		vs->vs_scrub_type = type;
2624fa9e4066Sahrens 		vs->vs_scrub_complete = 0;
2625fa9e4066Sahrens 		vs->vs_scrub_examined = 0;
2626fa9e4066Sahrens 		vs->vs_scrub_repaired = 0;
2627fa9e4066Sahrens 		vs->vs_scrub_start = gethrestime_sec();
2628fa9e4066Sahrens 		vs->vs_scrub_end = 0;
2629fa9e4066Sahrens 	}
2630fa9e4066Sahrens 
2631fa9e4066Sahrens 	mutex_exit(&vd->vdev_stat_lock);
2632fa9e4066Sahrens }
2633fa9e4066Sahrens 
2634fa9e4066Sahrens /*
2635b24ab676SJeff Bonwick  * Update the in-core space usage stats for this vdev, its metaslab class,
2636b24ab676SJeff Bonwick  * and the root vdev.
2637fa9e4066Sahrens  */
2638fa9e4066Sahrens void
2639b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2640b24ab676SJeff Bonwick     int64_t space_delta)
2641fa9e4066Sahrens {
264299653d4eSeschrock 	int64_t dspace_delta = space_delta;
26438654d025Sperrin 	spa_t *spa = vd->vdev_spa;
26448654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
2645b24ab676SJeff Bonwick 	metaslab_group_t *mg = vd->vdev_mg;
2646b24ab676SJeff Bonwick 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2647fa9e4066Sahrens 
26488654d025Sperrin 	ASSERT(vd == vd->vdev_top);
264999653d4eSeschrock 
26508654d025Sperrin 	/*
26518654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
26528654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
26538654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
26548654d025Sperrin 	 * childrens', thus not accurate enough for us.
26558654d025Sperrin 	 */
26568654d025Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2657e6ca193dSGeorge Wilson 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
26588654d025Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
26598654d025Sperrin 	    vd->vdev_deflate_ratio;
26608654d025Sperrin 
26618654d025Sperrin 	mutex_enter(&vd->vdev_stat_lock);
26628654d025Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
2663b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
26648654d025Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
26658654d025Sperrin 	mutex_exit(&vd->vdev_stat_lock);
26668654d025Sperrin 
2667b24ab676SJeff Bonwick 	if (mc == spa_normal_class(spa)) {
2668fa94a07fSbrendan 		mutex_enter(&rvd->vdev_stat_lock);
2669fa94a07fSbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
2670b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
2671fa94a07fSbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
2672fa94a07fSbrendan 		mutex_exit(&rvd->vdev_stat_lock);
2673fa94a07fSbrendan 	}
2674b24ab676SJeff Bonwick 
2675b24ab676SJeff Bonwick 	if (mc != NULL) {
2676b24ab676SJeff Bonwick 		ASSERT(rvd == vd->vdev_parent);
2677b24ab676SJeff Bonwick 		ASSERT(vd->vdev_ms_count != 0);
2678b24ab676SJeff Bonwick 
2679b24ab676SJeff Bonwick 		metaslab_class_space_update(mc,
2680b24ab676SJeff Bonwick 		    alloc_delta, defer_delta, space_delta, dspace_delta);
2681b24ab676SJeff Bonwick 	}
2682fa9e4066Sahrens }
2683fa9e4066Sahrens 
2684fa9e4066Sahrens /*
2685fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2686fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
2687fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2688fa9e4066Sahrens  */
2689fa9e4066Sahrens void
2690fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
2691fa9e4066Sahrens {
2692fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2693fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2694fa9e4066Sahrens 	int c;
2695fa9e4066Sahrens 
2696c5904d13Seschrock 	/*
26976809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
26986809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
2699c5904d13Seschrock 	 */
2700c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
2701c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
2702c5904d13Seschrock 		nvlist_t **aux;
2703c5904d13Seschrock 		uint_t naux;
2704c5904d13Seschrock 
2705c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
2706c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
2707c5904d13Seschrock 				break;
2708c5904d13Seschrock 		}
2709c5904d13Seschrock 
2710e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
2711e14bb325SJeff Bonwick 			/*
2712e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
2713e14bb325SJeff Bonwick 			 */
2714e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
2715e14bb325SJeff Bonwick 			return;
2716e14bb325SJeff Bonwick 		}
2717e14bb325SJeff Bonwick 
2718c5904d13Seschrock 		sav->sav_sync = B_TRUE;
2719c5904d13Seschrock 
27206809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
27216809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
27226809eb4eSEric Schrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
27236809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
27246809eb4eSEric Schrock 		}
2725c5904d13Seschrock 
2726c5904d13Seschrock 		ASSERT(c < naux);
2727c5904d13Seschrock 
2728c5904d13Seschrock 		/*
2729c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
2730c5904d13Seschrock 		 * sketchy, but it will work.
2731c5904d13Seschrock 		 */
2732c5904d13Seschrock 		nvlist_free(aux[c]);
2733c5904d13Seschrock 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
2734c5904d13Seschrock 
2735c5904d13Seschrock 		return;
2736c5904d13Seschrock 	}
2737c5904d13Seschrock 
27385dabedeeSbonwick 	/*
2739e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
2740e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
2741e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
27425dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
27435dabedeeSbonwick 	 */
2744e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2745e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2746e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
27475dabedeeSbonwick 
2748fa9e4066Sahrens 	if (vd == rvd) {
2749fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2750fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2751fa9e4066Sahrens 	} else {
2752fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
2753fa9e4066Sahrens 
275488ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
275588ecc943SGeorge Wilson 		    !vd->vdev_ishole)
2756e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
2757fa9e4066Sahrens 	}
2758fa9e4066Sahrens }
2759fa9e4066Sahrens 
2760fa9e4066Sahrens void
2761fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
2762fa9e4066Sahrens {
27635dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
27645dabedeeSbonwick 
2765e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2766e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2767e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
27685dabedeeSbonwick 
2769e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
2770e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
2771e14bb325SJeff Bonwick }
2772e14bb325SJeff Bonwick 
2773e14bb325SJeff Bonwick /*
2774e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
2775e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
2776e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
2777e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
2778e14bb325SJeff Bonwick  */
2779e14bb325SJeff Bonwick void
2780e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
2781e14bb325SJeff Bonwick {
2782e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
2783e14bb325SJeff Bonwick 
2784e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
2785e14bb325SJeff Bonwick 
2786e14bb325SJeff Bonwick 	/*
2787e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
2788e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
2789e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
2790e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
2791e14bb325SJeff Bonwick 	 */
2792e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2793e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2794e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2795e14bb325SJeff Bonwick 
2796b24ab676SJeff Bonwick 	if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
2797e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
2798e14bb325SJeff Bonwick }
2799e14bb325SJeff Bonwick 
2800e14bb325SJeff Bonwick void
2801e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
2802e14bb325SJeff Bonwick {
2803e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
2804e14bb325SJeff Bonwick 
2805e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2806e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2807e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2808e14bb325SJeff Bonwick 
2809e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
2810e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
2811fa9e4066Sahrens }
2812fa9e4066Sahrens 
281332b87932Sek /*
281432b87932Sek  * Propagate vdev state up from children to parent.
281532b87932Sek  */
281644cd46caSbillm void
281744cd46caSbillm vdev_propagate_state(vdev_t *vd)
281844cd46caSbillm {
28198ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
28208ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
282144cd46caSbillm 	int degraded = 0, faulted = 0;
282244cd46caSbillm 	int corrupted = 0;
282344cd46caSbillm 	vdev_t *child;
282444cd46caSbillm 
28253d7072f8Seschrock 	if (vd->vdev_children > 0) {
2826573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
28273d7072f8Seschrock 			child = vd->vdev_child[c];
282851ece835Seschrock 
282988ecc943SGeorge Wilson 			/*
283088ecc943SGeorge Wilson 			 * Don't factor holes into the decision.
283188ecc943SGeorge Wilson 			 */
283288ecc943SGeorge Wilson 			if (child->vdev_ishole)
283388ecc943SGeorge Wilson 				continue;
283488ecc943SGeorge Wilson 
2835e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
28368ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
283751ece835Seschrock 				/*
283851ece835Seschrock 				 * Root special: if there is a top-level log
283951ece835Seschrock 				 * device, treat the root vdev as if it were
284051ece835Seschrock 				 * degraded.
284151ece835Seschrock 				 */
284251ece835Seschrock 				if (child->vdev_islog && vd == rvd)
284351ece835Seschrock 					degraded++;
284451ece835Seschrock 				else
284551ece835Seschrock 					faulted++;
284651ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
28473d7072f8Seschrock 				degraded++;
284851ece835Seschrock 			}
284944cd46caSbillm 
28503d7072f8Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
28513d7072f8Seschrock 				corrupted++;
28523d7072f8Seschrock 		}
285344cd46caSbillm 
28543d7072f8Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
28553d7072f8Seschrock 
28563d7072f8Seschrock 		/*
2857e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
28583d7072f8Seschrock 		 * opened due to corrupted metadata, then propagate the root
28593d7072f8Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
28603d7072f8Seschrock 		 * replicas'.
28613d7072f8Seschrock 		 */
28623d7072f8Seschrock 		if (corrupted && vd == rvd &&
28633d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
28643d7072f8Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
28653d7072f8Seschrock 			    VDEV_AUX_CORRUPT_DATA);
28663d7072f8Seschrock 	}
28673d7072f8Seschrock 
286851ece835Seschrock 	if (vd->vdev_parent)
28693d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
287044cd46caSbillm }
287144cd46caSbillm 
2872fa9e4066Sahrens /*
2873ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
2874ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
2875ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
2876ea8dc4b6Seschrock  *
2877ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
2878ea8dc4b6Seschrock  * generated.
2879fa9e4066Sahrens  */
2880fa9e4066Sahrens void
2881ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2882fa9e4066Sahrens {
2883560e6e96Seschrock 	uint64_t save_state;
2884c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
2885ea8dc4b6Seschrock 
2886ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
2887ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
2888fa9e4066Sahrens 		return;
2889ea8dc4b6Seschrock 	}
2890ea8dc4b6Seschrock 
2891560e6e96Seschrock 	save_state = vd->vdev_state;
2892fa9e4066Sahrens 
2893fa9e4066Sahrens 	vd->vdev_state = state;
2894fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
2895fa9e4066Sahrens 
28963d7072f8Seschrock 	/*
28973d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
2898*98d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
2899*98d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
2900*98d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
2901*98d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
2902*98d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
2903*98d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
2904*98d1cbfeSGeorge Wilson 	 * children.
2905*98d1cbfeSGeorge Wilson 	 */
2906*98d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
2907*98d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
29083d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
29093d7072f8Seschrock 
2910069f55e2SEric Schrock 	/*
2911069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
2912069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
2913069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
2914069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
2915069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
2916069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
2917069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
2918069f55e2SEric Schrock 	 */
2919069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
2920069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
2921069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
2922069f55e2SEric Schrock 
29233d7072f8Seschrock 	if (vd->vdev_removed &&
29243d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
29253d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
29263d7072f8Seschrock 		/*
29273d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
29283d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
29293d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
29303d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
29313d7072f8Seschrock 		 * it is one of our special test online cases, which is only
29323d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
29333d7072f8Seschrock 		 * fault.
29343d7072f8Seschrock 		 */
29353d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
29363d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
29373d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
29383d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
29393d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
2940ea8dc4b6Seschrock 		/*
2941ea8dc4b6Seschrock 		 * If we fail to open a vdev during an import, we mark it as
2942ea8dc4b6Seschrock 		 * "not available", which signifies that it was never there to
2943ea8dc4b6Seschrock 		 * begin with.  Failure to open such a device is not considered
2944ea8dc4b6Seschrock 		 * an error.
2945ea8dc4b6Seschrock 		 */
2946b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_IMPORT &&
2947560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
2948560e6e96Seschrock 			vd->vdev_not_present = 1;
2949560e6e96Seschrock 
2950560e6e96Seschrock 		/*
2951560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
2952560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
2953560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
2954560e6e96Seschrock 		 * want to post the ereport if the device was already in the
2955560e6e96Seschrock 		 * CANT_OPEN state beforehand.
29563d7072f8Seschrock 		 *
29573d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
29583d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
29593d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
29603d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
29613d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
29623d7072f8Seschrock 		 * about to replace the device, or attempt an online with
29633d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
2964560e6e96Seschrock 		 */
29653d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
29663d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
2967c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
2968ea8dc4b6Seschrock 			const char *class;
2969ea8dc4b6Seschrock 
2970ea8dc4b6Seschrock 			switch (aux) {
2971ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
2972ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
2973ea8dc4b6Seschrock 				break;
2974ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
2975ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
2976ea8dc4b6Seschrock 				break;
2977ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
2978ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
2979ea8dc4b6Seschrock 				break;
2980ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
2981ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
2982ea8dc4b6Seschrock 				break;
2983ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
2984ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
2985ea8dc4b6Seschrock 				break;
2986ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
2987ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
2988ea8dc4b6Seschrock 				break;
2989ea8dc4b6Seschrock 			default:
2990ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
2991ea8dc4b6Seschrock 			}
2992ea8dc4b6Seschrock 
2993c5904d13Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
2994ea8dc4b6Seschrock 		}
2995ea8dc4b6Seschrock 
29963d7072f8Seschrock 		/* Erase any notion of persistent removed state */
29973d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
29983d7072f8Seschrock 	} else {
29993d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
30003d7072f8Seschrock 	}
3001ea8dc4b6Seschrock 
30028b33d774STim Haley 	if (!isopen && vd->vdev_parent)
30038b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
3004fa9e4066Sahrens }
300515e6edf1Sgw 
300615e6edf1Sgw /*
300715e6edf1Sgw  * Check the vdev configuration to ensure that it's capable of supporting
300815e6edf1Sgw  * a root pool. Currently, we do not support RAID-Z or partial configuration.
300915e6edf1Sgw  * In addition, only a single top-level vdev is allowed and none of the leaves
301015e6edf1Sgw  * can be wholedisks.
301115e6edf1Sgw  */
301215e6edf1Sgw boolean_t
301315e6edf1Sgw vdev_is_bootable(vdev_t *vd)
301415e6edf1Sgw {
301515e6edf1Sgw 	if (!vd->vdev_ops->vdev_op_leaf) {
301615e6edf1Sgw 		char *vdev_type = vd->vdev_ops->vdev_op_type;
301715e6edf1Sgw 
301815e6edf1Sgw 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
301915e6edf1Sgw 		    vd->vdev_children > 1) {
302015e6edf1Sgw 			return (B_FALSE);
302115e6edf1Sgw 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
302215e6edf1Sgw 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
302315e6edf1Sgw 			return (B_FALSE);
302415e6edf1Sgw 		}
302515e6edf1Sgw 	} else if (vd->vdev_wholedisk == 1) {
302615e6edf1Sgw 		return (B_FALSE);
302715e6edf1Sgw 	}
302815e6edf1Sgw 
3029573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
303015e6edf1Sgw 		if (!vdev_is_bootable(vd->vdev_child[c]))
303115e6edf1Sgw 			return (B_FALSE);
303215e6edf1Sgw 	}
303315e6edf1Sgw 	return (B_TRUE);
303415e6edf1Sgw }
3035e6ca193dSGeorge Wilson 
303688ecc943SGeorge Wilson /*
303788ecc943SGeorge Wilson  * Load the state from the original vdev tree (ovd) which
303888ecc943SGeorge Wilson  * we've retrieved from the MOS config object. If the original
303988ecc943SGeorge Wilson  * vdev was offline then we transfer that state to the device
304088ecc943SGeorge Wilson  * in the current vdev tree (nvd).
304188ecc943SGeorge Wilson  */
3042e6ca193dSGeorge Wilson void
304388ecc943SGeorge Wilson vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3044e6ca193dSGeorge Wilson {
304588ecc943SGeorge Wilson 	spa_t *spa = nvd->vdev_spa;
3046e6ca193dSGeorge Wilson 
304788ecc943SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
304888ecc943SGeorge Wilson 	ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3049e6ca193dSGeorge Wilson 
305088ecc943SGeorge Wilson 	for (int c = 0; c < nvd->vdev_children; c++)
305188ecc943SGeorge Wilson 		vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3052e6ca193dSGeorge Wilson 
305388ecc943SGeorge Wilson 	if (nvd->vdev_ops->vdev_op_leaf && ovd->vdev_offline) {
3054e6ca193dSGeorge Wilson 		/*
3055e6ca193dSGeorge Wilson 		 * It would be nice to call vdev_offline()
3056e6ca193dSGeorge Wilson 		 * directly but the pool isn't fully loaded and
3057e6ca193dSGeorge Wilson 		 * the txg threads have not been started yet.
3058e6ca193dSGeorge Wilson 		 */
305988ecc943SGeorge Wilson 		nvd->vdev_offline = ovd->vdev_offline;
306088ecc943SGeorge Wilson 		vdev_reopen(nvd->vdev_top);
3061e6ca193dSGeorge Wilson 	}
3062e6ca193dSGeorge Wilson }
3063573ca77eSGeorge Wilson 
3064573ca77eSGeorge Wilson /*
3065573ca77eSGeorge Wilson  * Expand a vdev if possible.
3066573ca77eSGeorge Wilson  */
3067573ca77eSGeorge Wilson void
3068573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
3069573ca77eSGeorge Wilson {
3070573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
3071573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3072573ca77eSGeorge Wilson 
3073573ca77eSGeorge Wilson 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3074573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
3075573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
3076573ca77eSGeorge Wilson 	}
3077573ca77eSGeorge Wilson }
30781195e687SMark J Musante 
30791195e687SMark J Musante /*
30801195e687SMark J Musante  * Split a vdev.
30811195e687SMark J Musante  */
30821195e687SMark J Musante void
30831195e687SMark J Musante vdev_split(vdev_t *vd)
30841195e687SMark J Musante {
30851195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
30861195e687SMark J Musante 
30871195e687SMark J Musante 	vdev_remove_child(pvd, vd);
30881195e687SMark J Musante 	vdev_compact_children(pvd);
30891195e687SMark J Musante 
30901195e687SMark J Musante 	cvd = pvd->vdev_child[0];
30911195e687SMark J Musante 	if (pvd->vdev_children == 1) {
30921195e687SMark J Musante 		vdev_remove_parent(cvd);
30931195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
30941195e687SMark J Musante 	}
30951195e687SMark J Musante 	vdev_propagate_state(cvd);
30961195e687SMark J Musante }
3097