xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision c3d26abc9ee97b4f60233556aadeb57e0bd30bb9)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
2398d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2481cd5c55SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
251a902ef8SHans Rosenfeld  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
26*c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/zfs_context.h>
30ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
31fa9e4066Sahrens #include <sys/spa.h>
32fa9e4066Sahrens #include <sys/spa_impl.h>
33fa9e4066Sahrens #include <sys/dmu.h>
34fa9e4066Sahrens #include <sys/dmu_tx.h>
35fa9e4066Sahrens #include <sys/vdev_impl.h>
36fa9e4066Sahrens #include <sys/uberblock_impl.h>
37fa9e4066Sahrens #include <sys/metaslab.h>
38fa9e4066Sahrens #include <sys/metaslab_impl.h>
39fa9e4066Sahrens #include <sys/space_map.h>
400713e232SGeorge Wilson #include <sys/space_reftree.h>
41fa9e4066Sahrens #include <sys/zio.h>
42fa9e4066Sahrens #include <sys/zap.h>
43fa9e4066Sahrens #include <sys/fs/zfs.h>
44c5904d13Seschrock #include <sys/arc.h>
45e6ca193dSGeorge Wilson #include <sys/zil.h>
463f9d6ad7SLin Ling #include <sys/dsl_scan.h>
47fa9e4066Sahrens 
48fa9e4066Sahrens /*
49fa9e4066Sahrens  * Virtual device management.
50fa9e4066Sahrens  */
51fa9e4066Sahrens 
52fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = {
53fa9e4066Sahrens 	&vdev_root_ops,
54fa9e4066Sahrens 	&vdev_raidz_ops,
55fa9e4066Sahrens 	&vdev_mirror_ops,
56fa9e4066Sahrens 	&vdev_replacing_ops,
5799653d4eSeschrock 	&vdev_spare_ops,
58fa9e4066Sahrens 	&vdev_disk_ops,
59fa9e4066Sahrens 	&vdev_file_ops,
60fa9e4066Sahrens 	&vdev_missing_ops,
6188ecc943SGeorge Wilson 	&vdev_hole_ops,
62fa9e4066Sahrens 	NULL
63fa9e4066Sahrens };
64fa9e4066Sahrens 
65088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
66088f3894Sahrens int zfs_scrub_limit = 10;
6705b2b3b8Smishra 
68bf3e216cSMatthew Ahrens /*
69bf3e216cSMatthew Ahrens  * When a vdev is added, it will be divided into approximately (but no
70bf3e216cSMatthew Ahrens  * more than) this number of metaslabs.
71bf3e216cSMatthew Ahrens  */
72bf3e216cSMatthew Ahrens int metaslabs_per_vdev = 200;
73bf3e216cSMatthew Ahrens 
74fa9e4066Sahrens /*
75fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
76fa9e4066Sahrens  */
77fa9e4066Sahrens static vdev_ops_t *
78fa9e4066Sahrens vdev_getops(const char *type)
79fa9e4066Sahrens {
80fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
81fa9e4066Sahrens 
82fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
83fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
84fa9e4066Sahrens 			break;
85fa9e4066Sahrens 
86fa9e4066Sahrens 	return (ops);
87fa9e4066Sahrens }
88fa9e4066Sahrens 
89fa9e4066Sahrens /*
90fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
91fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
92fa9e4066Sahrens  */
93fa9e4066Sahrens uint64_t
94fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
95fa9e4066Sahrens {
96ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
97fa9e4066Sahrens 	uint64_t csize;
98fa9e4066Sahrens 
99573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
100fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
101fa9e4066Sahrens 		asize = MAX(asize, csize);
102fa9e4066Sahrens 	}
103fa9e4066Sahrens 
104fa9e4066Sahrens 	return (asize);
105fa9e4066Sahrens }
106fa9e4066Sahrens 
1072a79c5feSlling /*
108573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
109573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
110573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
111573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
1122a79c5feSlling  */
1132a79c5feSlling uint64_t
114573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
1152a79c5feSlling {
116573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
1172a79c5feSlling 
118573ca77eSGeorge Wilson 	/*
1194263d13fSGeorge Wilson 	 * If our parent is NULL (inactive spare or cache) or is the root,
120573ca77eSGeorge Wilson 	 * just return our own asize.
121573ca77eSGeorge Wilson 	 */
122573ca77eSGeorge Wilson 	if (pvd == NULL)
123573ca77eSGeorge Wilson 		return (vd->vdev_asize);
1242a79c5feSlling 
1252a79c5feSlling 	/*
126573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
127573ca77eSGeorge Wilson 	 * to the nearest metaslab.
1282a79c5feSlling 	 */
129573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
130573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
1312a79c5feSlling 
132573ca77eSGeorge Wilson 	/*
133573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
134573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
135573ca77eSGeorge Wilson 	 */
136573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
137573ca77eSGeorge Wilson 		return (pvd->vdev_min_asize / pvd->vdev_children);
1382a79c5feSlling 
139573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
140573ca77eSGeorge Wilson }
1412a79c5feSlling 
142573ca77eSGeorge Wilson void
143573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
144573ca77eSGeorge Wilson {
145573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
146573ca77eSGeorge Wilson 
147573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
148573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
1492a79c5feSlling }
1502a79c5feSlling 
151fa9e4066Sahrens vdev_t *
152fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
153fa9e4066Sahrens {
154fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
155fa9e4066Sahrens 
156e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
157e05725b1Sbonwick 
158088f3894Sahrens 	if (vdev < rvd->vdev_children) {
159088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
160fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
161088f3894Sahrens 	}
162fa9e4066Sahrens 
163fa9e4066Sahrens 	return (NULL);
164fa9e4066Sahrens }
165fa9e4066Sahrens 
166fa9e4066Sahrens vdev_t *
167fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
168fa9e4066Sahrens {
169fa9e4066Sahrens 	vdev_t *mvd;
170fa9e4066Sahrens 
1710e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
172fa9e4066Sahrens 		return (vd);
173fa9e4066Sahrens 
174573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
175fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
176fa9e4066Sahrens 		    NULL)
177fa9e4066Sahrens 			return (mvd);
178fa9e4066Sahrens 
179fa9e4066Sahrens 	return (NULL);
180fa9e4066Sahrens }
181fa9e4066Sahrens 
18212380e1eSArne Jansen static int
18312380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd)
18412380e1eSArne Jansen {
18512380e1eSArne Jansen 	int n = 0;
18612380e1eSArne Jansen 
18712380e1eSArne Jansen 	if (vd->vdev_ops->vdev_op_leaf)
18812380e1eSArne Jansen 		return (1);
18912380e1eSArne Jansen 
19012380e1eSArne Jansen 	for (int c = 0; c < vd->vdev_children; c++)
19112380e1eSArne Jansen 		n += vdev_count_leaves_impl(vd->vdev_child[c]);
19212380e1eSArne Jansen 
19312380e1eSArne Jansen 	return (n);
19412380e1eSArne Jansen }
19512380e1eSArne Jansen 
19612380e1eSArne Jansen int
19712380e1eSArne Jansen vdev_count_leaves(spa_t *spa)
19812380e1eSArne Jansen {
19912380e1eSArne Jansen 	return (vdev_count_leaves_impl(spa->spa_root_vdev));
20012380e1eSArne Jansen }
20112380e1eSArne Jansen 
202fa9e4066Sahrens void
203fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
204fa9e4066Sahrens {
205fa9e4066Sahrens 	size_t oldsize, newsize;
206fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
207fa9e4066Sahrens 	vdev_t **newchild;
20881cd5c55SMatthew Ahrens 	spa_t *spa = cvd->vdev_spa;
209fa9e4066Sahrens 
21081cd5c55SMatthew Ahrens 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
211fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
212fa9e4066Sahrens 
213fa9e4066Sahrens 	cvd->vdev_parent = pvd;
214fa9e4066Sahrens 
215fa9e4066Sahrens 	if (pvd == NULL)
216fa9e4066Sahrens 		return;
217fa9e4066Sahrens 
218fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
221fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
222fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
223fa9e4066Sahrens 
224fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
225fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
226fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
227fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
228fa9e4066Sahrens 	}
229fa9e4066Sahrens 
230fa9e4066Sahrens 	pvd->vdev_child = newchild;
231fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
232fa9e4066Sahrens 
233fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
234fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
235fa9e4066Sahrens 
236fa9e4066Sahrens 	/*
237fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
238fa9e4066Sahrens 	 */
239fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
240fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
241fa9e4066Sahrens }
242fa9e4066Sahrens 
243fa9e4066Sahrens void
244fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
245fa9e4066Sahrens {
246fa9e4066Sahrens 	int c;
247fa9e4066Sahrens 	uint_t id = cvd->vdev_id;
248fa9e4066Sahrens 
249fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == pvd);
250fa9e4066Sahrens 
251fa9e4066Sahrens 	if (pvd == NULL)
252fa9e4066Sahrens 		return;
253fa9e4066Sahrens 
254fa9e4066Sahrens 	ASSERT(id < pvd->vdev_children);
255fa9e4066Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
256fa9e4066Sahrens 
257fa9e4066Sahrens 	pvd->vdev_child[id] = NULL;
258fa9e4066Sahrens 	cvd->vdev_parent = NULL;
259fa9e4066Sahrens 
260fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
261fa9e4066Sahrens 		if (pvd->vdev_child[c])
262fa9e4066Sahrens 			break;
263fa9e4066Sahrens 
264fa9e4066Sahrens 	if (c == pvd->vdev_children) {
265fa9e4066Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
266fa9e4066Sahrens 		pvd->vdev_child = NULL;
267fa9e4066Sahrens 		pvd->vdev_children = 0;
268fa9e4066Sahrens 	}
269fa9e4066Sahrens 
270fa9e4066Sahrens 	/*
271fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
272fa9e4066Sahrens 	 */
273fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
274fa9e4066Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
275fa9e4066Sahrens }
276fa9e4066Sahrens 
277fa9e4066Sahrens /*
278fa9e4066Sahrens  * Remove any holes in the child array.
279fa9e4066Sahrens  */
280fa9e4066Sahrens void
281fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
282fa9e4066Sahrens {
283fa9e4066Sahrens 	vdev_t **newchild, *cvd;
284fa9e4066Sahrens 	int oldc = pvd->vdev_children;
285573ca77eSGeorge Wilson 	int newc;
286fa9e4066Sahrens 
287e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
288fa9e4066Sahrens 
289573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
290fa9e4066Sahrens 		if (pvd->vdev_child[c])
291fa9e4066Sahrens 			newc++;
292fa9e4066Sahrens 
293fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
294fa9e4066Sahrens 
295573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
296fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
297fa9e4066Sahrens 			newchild[newc] = cvd;
298fa9e4066Sahrens 			cvd->vdev_id = newc++;
299fa9e4066Sahrens 		}
300fa9e4066Sahrens 	}
301fa9e4066Sahrens 
302fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
303fa9e4066Sahrens 	pvd->vdev_child = newchild;
304fa9e4066Sahrens 	pvd->vdev_children = newc;
305fa9e4066Sahrens }
306fa9e4066Sahrens 
307fa9e4066Sahrens /*
308fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
309fa9e4066Sahrens  */
31088ecc943SGeorge Wilson vdev_t *
311fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
312fa9e4066Sahrens {
313fa9e4066Sahrens 	vdev_t *vd;
314fa9e4066Sahrens 
315fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
316fa9e4066Sahrens 
3170e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
3180e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
3190e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
320e9103aaeSGarrett D'Amore 		spa->spa_load_guid = spa_generate_guid(NULL);
3210e34b6a7Sbonwick 	}
3220e34b6a7Sbonwick 
32388ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
3240e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
3250e34b6a7Sbonwick 			/*
3260e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
3270e34b6a7Sbonwick 			 * which must be unique among all pools.
3280e34b6a7Sbonwick 			 */
3291195e687SMark J Musante 			guid = spa_generate_guid(NULL);
3300e34b6a7Sbonwick 		} else {
3310e34b6a7Sbonwick 			/*
3320e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3330e34b6a7Sbonwick 			 */
3341195e687SMark J Musante 			guid = spa_generate_guid(spa);
3350e34b6a7Sbonwick 		}
3360e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3370e34b6a7Sbonwick 	}
3380e34b6a7Sbonwick 
339fa9e4066Sahrens 	vd->vdev_spa = spa;
340fa9e4066Sahrens 	vd->vdev_id = id;
341fa9e4066Sahrens 	vd->vdev_guid = guid;
342fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
343fa9e4066Sahrens 	vd->vdev_ops = ops;
344fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
34588ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
346fa9e4066Sahrens 
347fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3485ad82045Snd 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
349e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3508ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
3510713e232SGeorge Wilson 		vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
3528ad4d6ddSJeff Bonwick 		    &vd->vdev_dtl_lock);
3538ad4d6ddSJeff Bonwick 	}
354fa9e4066Sahrens 	txg_list_create(&vd->vdev_ms_list,
355fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
356fa9e4066Sahrens 	txg_list_create(&vd->vdev_dtl_list,
357fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
358fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3593d7072f8Seschrock 	vdev_queue_init(vd);
3603d7072f8Seschrock 	vdev_cache_init(vd);
361fa9e4066Sahrens 
362fa9e4066Sahrens 	return (vd);
363fa9e4066Sahrens }
364fa9e4066Sahrens 
365fa9e4066Sahrens /*
366fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
367fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
368fa9e4066Sahrens  * different for each case.
369fa9e4066Sahrens  */
37099653d4eSeschrock int
37199653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
37299653d4eSeschrock     int alloctype)
373fa9e4066Sahrens {
374fa9e4066Sahrens 	vdev_ops_t *ops;
375fa9e4066Sahrens 	char *type;
3768654d025Sperrin 	uint64_t guid = 0, islog, nparity;
377fa9e4066Sahrens 	vdev_t *vd;
378fa9e4066Sahrens 
379e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
380fa9e4066Sahrens 
381fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
382be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
383fa9e4066Sahrens 
384fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
385be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
386fa9e4066Sahrens 
387fa9e4066Sahrens 	/*
388fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
389fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
390fa9e4066Sahrens 	 */
391fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
392fa9e4066Sahrens 		uint64_t label_id;
393fa9e4066Sahrens 
394fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
395fa9e4066Sahrens 		    label_id != id)
396be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
397fa9e4066Sahrens 
398fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
399be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
40099653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
40199653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
402be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
403fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
404fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
405be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
40621ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
40721ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
408be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
409fa9e4066Sahrens 	}
410fa9e4066Sahrens 
41199653d4eSeschrock 	/*
41299653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
41399653d4eSeschrock 	 */
41499653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
415be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
41699653d4eSeschrock 
4178654d025Sperrin 	/*
4188654d025Sperrin 	 * Determine whether we're a log vdev.
4198654d025Sperrin 	 */
4208654d025Sperrin 	islog = 0;
4218654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
422990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
423be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
424fa9e4066Sahrens 
42588ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
426be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
42788ecc943SGeorge Wilson 
42899653d4eSeschrock 	/*
4298654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
43099653d4eSeschrock 	 */
4318654d025Sperrin 	nparity = -1ULL;
43299653d4eSeschrock 	if (ops == &vdev_raidz_ops) {
43399653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4348654d025Sperrin 		    &nparity) == 0) {
435b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
436be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
43799653d4eSeschrock 			/*
438f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
439f94275ceSAdam Leventhal 			 * device.
44099653d4eSeschrock 			 */
441f94275ceSAdam Leventhal 			if (nparity > 1 &&
442f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
443be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
444f94275ceSAdam Leventhal 			if (nparity > 2 &&
445f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
446be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
44799653d4eSeschrock 		} else {
44899653d4eSeschrock 			/*
44999653d4eSeschrock 			 * We require the parity to be specified for SPAs that
45099653d4eSeschrock 			 * support multiple parity levels.
45199653d4eSeschrock 			 */
452f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
453be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
45499653d4eSeschrock 			/*
45599653d4eSeschrock 			 * Otherwise, we default to 1 parity device for RAID-Z.
45699653d4eSeschrock 			 */
4578654d025Sperrin 			nparity = 1;
45899653d4eSeschrock 		}
45999653d4eSeschrock 	} else {
4608654d025Sperrin 		nparity = 0;
46199653d4eSeschrock 	}
4628654d025Sperrin 	ASSERT(nparity != -1ULL);
4638654d025Sperrin 
4648654d025Sperrin 	vd = vdev_alloc_common(spa, id, guid, ops);
4658654d025Sperrin 
4668654d025Sperrin 	vd->vdev_islog = islog;
4678654d025Sperrin 	vd->vdev_nparity = nparity;
4688654d025Sperrin 
4698654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
4708654d025Sperrin 		vd->vdev_path = spa_strdup(vd->vdev_path);
4718654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
4728654d025Sperrin 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4738654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4748654d025Sperrin 	    &vd->vdev_physpath) == 0)
4758654d025Sperrin 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4766809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4776809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
47899653d4eSeschrock 
479afefbcddSeschrock 	/*
480afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
481afefbcddSeschrock 	 * as -1.
482afefbcddSeschrock 	 */
483afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
484afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
485afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
486afefbcddSeschrock 
487ea8dc4b6Seschrock 	/*
488ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
489ea8dc4b6Seschrock 	 * was not present at the time of import.
490ea8dc4b6Seschrock 	 */
4916809eb4eSEric Schrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4926809eb4eSEric Schrock 	    &vd->vdev_not_present);
493ea8dc4b6Seschrock 
494ecc2d604Sbonwick 	/*
495ecc2d604Sbonwick 	 * Get the alignment requirement.
496ecc2d604Sbonwick 	 */
497ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
498ecc2d604Sbonwick 
49988ecc943SGeorge Wilson 	/*
50088ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
50188ecc943SGeorge Wilson 	 */
50288ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
50388ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
50488ecc943SGeorge Wilson 
505fa9e4066Sahrens 	/*
506fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
507fa9e4066Sahrens 	 */
5081195e687SMark J Musante 	if (parent && !parent->vdev_parent &&
5091195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
510fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
511fa9e4066Sahrens 		    &vd->vdev_ms_array);
512fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
513fa9e4066Sahrens 		    &vd->vdev_ms_shift);
514fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
515fa9e4066Sahrens 		    &vd->vdev_asize);
5163f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
5173f9d6ad7SLin Ling 		    &vd->vdev_removing);
518fa9e4066Sahrens 	}
519fa9e4066Sahrens 
520cd0837ccSGeorge Wilson 	if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
521a1521560SJeff Bonwick 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
5229f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ADD ||
5231195e687SMark J Musante 		    alloctype == VDEV_ALLOC_SPLIT ||
5249f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ROOTPOOL);
525a1521560SJeff Bonwick 		vd->vdev_mg = metaslab_group_create(islog ?
526a1521560SJeff Bonwick 		    spa_log_class(spa) : spa_normal_class(spa), vd);
527a1521560SJeff Bonwick 	}
528a1521560SJeff Bonwick 
529fa9e4066Sahrens 	/*
5303d7072f8Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
531fa9e4066Sahrens 	 */
532c5904d13Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
53321ecdf64SLin Ling 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
53421ecdf64SLin Ling 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
535c5904d13Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
536c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
5370713e232SGeorge Wilson 			    &vd->vdev_dtl_object);
538c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
539c5904d13Seschrock 			    &vd->vdev_unspare);
540c5904d13Seschrock 		}
54121ecdf64SLin Ling 
54221ecdf64SLin Ling 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
54321ecdf64SLin Ling 			uint64_t spare = 0;
54421ecdf64SLin Ling 
54521ecdf64SLin Ling 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
54621ecdf64SLin Ling 			    &spare) == 0 && spare)
54721ecdf64SLin Ling 				spa_spare_add(vd);
54821ecdf64SLin Ling 		}
54921ecdf64SLin Ling 
550ecc2d604Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
551ecc2d604Sbonwick 		    &vd->vdev_offline);
552c5904d13Seschrock 
553b4952e17SGeorge Wilson 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
554b4952e17SGeorge Wilson 		    &vd->vdev_resilver_txg);
555cb04b873SMark J Musante 
5563d7072f8Seschrock 		/*
5573d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5583d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
559069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
560069f55e2SEric Schrock 		 * remain in the faulted state.
5613d7072f8Seschrock 		 */
562b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
5633d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5643d7072f8Seschrock 			    &vd->vdev_faulted);
5653d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5663d7072f8Seschrock 			    &vd->vdev_degraded);
5673d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5683d7072f8Seschrock 			    &vd->vdev_removed);
569069f55e2SEric Schrock 
570069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
571069f55e2SEric Schrock 				char *aux;
572069f55e2SEric Schrock 
573069f55e2SEric Schrock 				vd->vdev_label_aux =
574069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
575069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
576069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
577069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
578069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
579069f55e2SEric Schrock 			}
5803d7072f8Seschrock 		}
581fa9e4066Sahrens 	}
582fa9e4066Sahrens 
583fa9e4066Sahrens 	/*
584fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
585fa9e4066Sahrens 	 */
586fa9e4066Sahrens 	vdev_add_child(parent, vd);
587fa9e4066Sahrens 
58899653d4eSeschrock 	*vdp = vd;
58999653d4eSeschrock 
59099653d4eSeschrock 	return (0);
591fa9e4066Sahrens }
592fa9e4066Sahrens 
593fa9e4066Sahrens void
594fa9e4066Sahrens vdev_free(vdev_t *vd)
595fa9e4066Sahrens {
5963d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
597fa9e4066Sahrens 
598fa9e4066Sahrens 	/*
599fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
600fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
601fa9e4066Sahrens 	 */
602fa9e4066Sahrens 	vdev_close(vd);
603fa9e4066Sahrens 
604e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
605b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
606fa9e4066Sahrens 
607fa9e4066Sahrens 	/*
608fa9e4066Sahrens 	 * Free all children.
609fa9e4066Sahrens 	 */
610573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
611fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
612fa9e4066Sahrens 
613fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
614fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
615fa9e4066Sahrens 
616fa9e4066Sahrens 	/*
617fa9e4066Sahrens 	 * Discard allocation state.
618fa9e4066Sahrens 	 */
619a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
620fa9e4066Sahrens 		vdev_metaslab_fini(vd);
621a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
622a1521560SJeff Bonwick 	}
623fa9e4066Sahrens 
624fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_space);
625fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_dspace);
626fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_alloc);
627fa9e4066Sahrens 
628fa9e4066Sahrens 	/*
629fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
630fa9e4066Sahrens 	 */
631fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
632fa9e4066Sahrens 
633fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
634fa9e4066Sahrens 
6353d7072f8Seschrock 	/*
6363d7072f8Seschrock 	 * Clean up vdev structure.
6373d7072f8Seschrock 	 */
6383d7072f8Seschrock 	vdev_queue_fini(vd);
6393d7072f8Seschrock 	vdev_cache_fini(vd);
6403d7072f8Seschrock 
6413d7072f8Seschrock 	if (vd->vdev_path)
6423d7072f8Seschrock 		spa_strfree(vd->vdev_path);
6433d7072f8Seschrock 	if (vd->vdev_devid)
6443d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
6453d7072f8Seschrock 	if (vd->vdev_physpath)
6463d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
6476809eb4eSEric Schrock 	if (vd->vdev_fru)
6486809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
6493d7072f8Seschrock 
6503d7072f8Seschrock 	if (vd->vdev_isspare)
6513d7072f8Seschrock 		spa_spare_remove(vd);
652fa94a07fSbrendan 	if (vd->vdev_isl2cache)
653fa94a07fSbrendan 		spa_l2cache_remove(vd);
6543d7072f8Seschrock 
6553d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
6563d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
6578ad4d6ddSJeff Bonwick 
6583d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
6590713e232SGeorge Wilson 	space_map_close(vd->vdev_dtl_sm);
6608ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
6610713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
6620713e232SGeorge Wilson 		range_tree_destroy(vd->vdev_dtl[t]);
6638ad4d6ddSJeff Bonwick 	}
6643d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
6658ad4d6ddSJeff Bonwick 
6663d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6673d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
668e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
6693d7072f8Seschrock 
6703d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
6713d7072f8Seschrock 		spa->spa_root_vdev = NULL;
6723d7072f8Seschrock 
6733d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
674fa9e4066Sahrens }
675fa9e4066Sahrens 
676fa9e4066Sahrens /*
677fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
678fa9e4066Sahrens  */
679fa9e4066Sahrens static void
680fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
681fa9e4066Sahrens {
682fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
683fa9e4066Sahrens 	metaslab_t *msp;
684fa9e4066Sahrens 	vdev_t *vd;
685fa9e4066Sahrens 	int t;
686fa9e4066Sahrens 
687fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
688fa9e4066Sahrens 
689fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
690fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
691fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
692fa9e4066Sahrens 
693fa9e4066Sahrens 	svd->vdev_ms_array = 0;
694fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
695fa9e4066Sahrens 	svd->vdev_ms_count = 0;
696fa9e4066Sahrens 
697cd0837ccSGeorge Wilson 	if (tvd->vdev_mg)
698cd0837ccSGeorge Wilson 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
699fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
700fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
701fa9e4066Sahrens 
702fa9e4066Sahrens 	svd->vdev_mg = NULL;
703fa9e4066Sahrens 	svd->vdev_ms = NULL;
704ecc2d604Sbonwick 
705ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
706ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
707fa9e4066Sahrens 
708fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
709fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
71099653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
711fa9e4066Sahrens 
712fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
713fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
71499653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
715fa9e4066Sahrens 
716fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
717fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
718fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
719fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
720fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
721fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
722fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
723fa9e4066Sahrens 	}
724fa9e4066Sahrens 
725e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
726fa9e4066Sahrens 		vdev_config_clean(svd);
727fa9e4066Sahrens 		vdev_config_dirty(tvd);
728fa9e4066Sahrens 	}
729fa9e4066Sahrens 
730e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
731e14bb325SJeff Bonwick 		vdev_state_clean(svd);
732e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
733e14bb325SJeff Bonwick 	}
734e14bb325SJeff Bonwick 
73599653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
73699653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
7378654d025Sperrin 
7388654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
7398654d025Sperrin 	svd->vdev_islog = 0;
740fa9e4066Sahrens }
741fa9e4066Sahrens 
742fa9e4066Sahrens static void
743fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
744fa9e4066Sahrens {
745fa9e4066Sahrens 	if (vd == NULL)
746fa9e4066Sahrens 		return;
747fa9e4066Sahrens 
748fa9e4066Sahrens 	vd->vdev_top = tvd;
749fa9e4066Sahrens 
750573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
751fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
752fa9e4066Sahrens }
753fa9e4066Sahrens 
754fa9e4066Sahrens /*
755fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
756fa9e4066Sahrens  */
757fa9e4066Sahrens vdev_t *
758fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
759fa9e4066Sahrens {
760fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
761fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
762fa9e4066Sahrens 	vdev_t *mvd;
763fa9e4066Sahrens 
764e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
765fa9e4066Sahrens 
766fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
767ecc2d604Sbonwick 
768ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
769573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
7704263d13fSGeorge Wilson 	mvd->vdev_max_asize = cvd->vdev_max_asize;
771ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
772ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
77388ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
774ecc2d604Sbonwick 
775fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
776fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
777fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
778fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
779fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
780fa9e4066Sahrens 
781fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
782fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
783fa9e4066Sahrens 
784fa9e4066Sahrens 	return (mvd);
785fa9e4066Sahrens }
786fa9e4066Sahrens 
787fa9e4066Sahrens /*
788fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
789fa9e4066Sahrens  */
790fa9e4066Sahrens void
791fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
792fa9e4066Sahrens {
793fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
794fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
795fa9e4066Sahrens 
796e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
797fa9e4066Sahrens 
798fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
799fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
80099653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
80199653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
802ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
803fa9e4066Sahrens 
804fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
805fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
8068ad4d6ddSJeff Bonwick 
80799653d4eSeschrock 	/*
808e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
809e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
810e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
811e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
81299653d4eSeschrock 	 */
8138ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
8148ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
8151195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
8168ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
8178ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
8188ad4d6ddSJeff Bonwick 	}
819e14bb325SJeff Bonwick 	cvd->vdev_id = mvd->vdev_id;
820e14bb325SJeff Bonwick 	vdev_add_child(pvd, cvd);
821fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
822fa9e4066Sahrens 
823fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
824fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
825fa9e4066Sahrens 
826fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
827fa9e4066Sahrens 	vdev_free(mvd);
828fa9e4066Sahrens }
829fa9e4066Sahrens 
830ea8dc4b6Seschrock int
831fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
832fa9e4066Sahrens {
833fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
834ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
835ecc2d604Sbonwick 	uint64_t m;
836fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
837fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
838ecc2d604Sbonwick 	metaslab_t **mspp;
839ecc2d604Sbonwick 	int error;
840fa9e4066Sahrens 
841a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
842a1521560SJeff Bonwick 
84388ecc943SGeorge Wilson 	/*
84488ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
84588ecc943SGeorge Wilson 	 */
84688ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
8470e34b6a7Sbonwick 		return (0);
8480e34b6a7Sbonwick 
84988ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
85088ecc943SGeorge Wilson 
851e6ca193dSGeorge Wilson 	/*
852e6ca193dSGeorge Wilson 	 * Compute the raidz-deflation ratio.  Note, we hard-code
853b5152584SMatthew Ahrens 	 * in 128k (1 << 17) because it is the "typical" blocksize.
854b5152584SMatthew Ahrens 	 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
855b5152584SMatthew Ahrens 	 * otherwise it would inconsistently account for existing bp's.
856e6ca193dSGeorge Wilson 	 */
857e6ca193dSGeorge Wilson 	vd->vdev_deflate_ratio = (1 << 17) /
858e6ca193dSGeorge Wilson 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
859e6ca193dSGeorge Wilson 
860fa9e4066Sahrens 	ASSERT(oldc <= newc);
861fa9e4066Sahrens 
862ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
863fa9e4066Sahrens 
864ecc2d604Sbonwick 	if (oldc != 0) {
865ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
866ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
867ecc2d604Sbonwick 	}
868fa9e4066Sahrens 
869ecc2d604Sbonwick 	vd->vdev_ms = mspp;
870ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
871fa9e4066Sahrens 
872ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
8730713e232SGeorge Wilson 		uint64_t object = 0;
8740713e232SGeorge Wilson 
875ecc2d604Sbonwick 		if (txg == 0) {
876ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8777bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8787bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
879ecc2d604Sbonwick 			if (error)
880ecc2d604Sbonwick 				return (error);
881fa9e4066Sahrens 		}
8821e9bd7ecSPrakash Surya 
8831e9bd7ecSPrakash Surya 		error = metaslab_init(vd->vdev_mg, m, object, txg,
8841e9bd7ecSPrakash Surya 		    &(vd->vdev_ms[m]));
8851e9bd7ecSPrakash Surya 		if (error)
8861e9bd7ecSPrakash Surya 			return (error);
887fa9e4066Sahrens 	}
888fa9e4066Sahrens 
889a1521560SJeff Bonwick 	if (txg == 0)
890a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
891a1521560SJeff Bonwick 
8923f9d6ad7SLin Ling 	/*
8933f9d6ad7SLin Ling 	 * If the vdev is being removed we don't activate
8943f9d6ad7SLin Ling 	 * the metaslabs since we want to ensure that no new
8953f9d6ad7SLin Ling 	 * allocations are performed on this device.
8963f9d6ad7SLin Ling 	 */
8973f9d6ad7SLin Ling 	if (oldc == 0 && !vd->vdev_removing)
898a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
899a1521560SJeff Bonwick 
900a1521560SJeff Bonwick 	if (txg == 0)
901a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
902a1521560SJeff Bonwick 
903ea8dc4b6Seschrock 	return (0);
904fa9e4066Sahrens }
905fa9e4066Sahrens 
906fa9e4066Sahrens void
907fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
908fa9e4066Sahrens {
909fa9e4066Sahrens 	uint64_t m;
910fa9e4066Sahrens 	uint64_t count = vd->vdev_ms_count;
911fa9e4066Sahrens 
912fa9e4066Sahrens 	if (vd->vdev_ms != NULL) {
913a1521560SJeff Bonwick 		metaslab_group_passivate(vd->vdev_mg);
9140713e232SGeorge Wilson 		for (m = 0; m < count; m++) {
9150713e232SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
9160713e232SGeorge Wilson 
9170713e232SGeorge Wilson 			if (msp != NULL)
9180713e232SGeorge Wilson 				metaslab_fini(msp);
9190713e232SGeorge Wilson 		}
920fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
921fa9e4066Sahrens 		vd->vdev_ms = NULL;
922fa9e4066Sahrens 	}
923fa9e4066Sahrens }
924fa9e4066Sahrens 
925e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
926e14bb325SJeff Bonwick 	boolean_t	vps_readable;
927e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
928e14bb325SJeff Bonwick 	int		vps_flags;
929e14bb325SJeff Bonwick } vdev_probe_stats_t;
930e14bb325SJeff Bonwick 
931e14bb325SJeff Bonwick static void
932e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
9330a4e9518Sgw {
9348ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
935a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
936e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
937a3f829aeSBill Moore 
938a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
939e14bb325SJeff Bonwick 
940e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
941e14bb325SJeff Bonwick 		if (zio->io_error == 0)
942e14bb325SJeff Bonwick 			vps->vps_readable = 1;
9438ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
944a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
945e14bb325SJeff Bonwick 			    zio->io_offset, zio->io_size, zio->io_data,
946e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
947e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
948e14bb325SJeff Bonwick 		} else {
949e14bb325SJeff Bonwick 			zio_buf_free(zio->io_data, zio->io_size);
950e14bb325SJeff Bonwick 		}
951e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
952e14bb325SJeff Bonwick 		if (zio->io_error == 0)
953e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
954e14bb325SJeff Bonwick 		zio_buf_free(zio->io_data, zio->io_size);
955e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
956a3f829aeSBill Moore 		zio_t *pio;
957e14bb325SJeff Bonwick 
958e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
959e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
960e14bb325SJeff Bonwick 
961e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
9628ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
963e14bb325SJeff Bonwick 			zio->io_error = 0;
964e14bb325SJeff Bonwick 		} else {
965e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
966e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
9678ad4d6ddSJeff Bonwick 			    spa, vd, NULL, 0, 0);
968be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
969e14bb325SJeff Bonwick 		}
970a3f829aeSBill Moore 
971a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
972a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
973a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
974a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
975a3f829aeSBill Moore 
976a3f829aeSBill Moore 		while ((pio = zio_walk_parents(zio)) != NULL)
977a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
978be6fd75aSMatthew Ahrens 				pio->io_error = SET_ERROR(ENXIO);
979a3f829aeSBill Moore 
980e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
981e14bb325SJeff Bonwick 	}
982e14bb325SJeff Bonwick }
9830a4e9518Sgw 
984e14bb325SJeff Bonwick /*
985f7170741SWill Andrews  * Determine whether this device is accessible.
986f7170741SWill Andrews  *
987f7170741SWill Andrews  * Read and write to several known locations: the pad regions of each
988f7170741SWill Andrews  * vdev label but the first, which we leave alone in case it contains
989f7170741SWill Andrews  * a VTOC.
990e14bb325SJeff Bonwick  */
991e14bb325SJeff Bonwick zio_t *
992a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
993e14bb325SJeff Bonwick {
994e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
995a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
996a3f829aeSBill Moore 	zio_t *pio;
997a3f829aeSBill Moore 
998a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9990a4e9518Sgw 
1000a3f829aeSBill Moore 	/*
1001a3f829aeSBill Moore 	 * Don't probe the probe.
1002a3f829aeSBill Moore 	 */
1003a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1004a3f829aeSBill Moore 		return (NULL);
1005e14bb325SJeff Bonwick 
1006a3f829aeSBill Moore 	/*
1007a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
1008a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
1009a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
1010a3f829aeSBill Moore 	 */
1011a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
1012e14bb325SJeff Bonwick 
1013a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
1014a3f829aeSBill Moore 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1015a3f829aeSBill Moore 
1016a3f829aeSBill Moore 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1017a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
10188956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
1019a3f829aeSBill Moore 
1020a3f829aeSBill Moore 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1021a3f829aeSBill Moore 			/*
1022a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
1023a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
1024a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
1025a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
1026a3f829aeSBill Moore 			 * any zio looking at these values can assume that
1027a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
1028a3f829aeSBill Moore 			 * important because when a device has intermittent
1029a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
1030a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
1031a3f829aeSBill Moore 			 * the zio (EIO).
1032a3f829aeSBill Moore 			 *
1033a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
1034a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
1035a3f829aeSBill Moore 			 * principles.
1036a3f829aeSBill Moore 			 */
1037a3f829aeSBill Moore 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1038a3f829aeSBill Moore 			vd->vdev_cant_read = B_FALSE;
1039a3f829aeSBill Moore 			vd->vdev_cant_write = B_FALSE;
1040a3f829aeSBill Moore 		}
1041a3f829aeSBill Moore 
1042a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1043a3f829aeSBill Moore 		    vdev_probe_done, vps,
1044a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1045a3f829aeSBill Moore 
104698d1cbfeSGeorge Wilson 		/*
104798d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
104898d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
104998d1cbfeSGeorge Wilson 		 */
1050a3f829aeSBill Moore 		if (zio != NULL) {
1051a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1052a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1053a3f829aeSBill Moore 		}
1054e14bb325SJeff Bonwick 	}
1055e14bb325SJeff Bonwick 
1056a3f829aeSBill Moore 	if (zio != NULL)
1057a3f829aeSBill Moore 		zio_add_child(zio, pio);
1058e14bb325SJeff Bonwick 
1059a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1060e14bb325SJeff Bonwick 
1061a3f829aeSBill Moore 	if (vps == NULL) {
1062a3f829aeSBill Moore 		ASSERT(zio != NULL);
1063a3f829aeSBill Moore 		return (NULL);
1064a3f829aeSBill Moore 	}
1065e14bb325SJeff Bonwick 
1066e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1067a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1068e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1069f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2)),
1070f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1071e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1072e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1073e14bb325SJeff Bonwick 	}
1074e14bb325SJeff Bonwick 
1075a3f829aeSBill Moore 	if (zio == NULL)
1076a3f829aeSBill Moore 		return (pio);
1077a3f829aeSBill Moore 
1078a3f829aeSBill Moore 	zio_nowait(pio);
1079a3f829aeSBill Moore 	return (NULL);
10800a4e9518Sgw }
10810a4e9518Sgw 
1082f64c0e34SEric Taylor static void
1083f64c0e34SEric Taylor vdev_open_child(void *arg)
1084f64c0e34SEric Taylor {
1085f64c0e34SEric Taylor 	vdev_t *vd = arg;
1086f64c0e34SEric Taylor 
1087f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1088f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1089f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1090f64c0e34SEric Taylor }
1091f64c0e34SEric Taylor 
1092681d9761SEric Taylor boolean_t
1093681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1094681d9761SEric Taylor {
1095681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1096681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1097681d9761SEric Taylor 		return (B_TRUE);
1098681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1099681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1100681d9761SEric Taylor 			return (B_TRUE);
1101681d9761SEric Taylor 	return (B_FALSE);
1102681d9761SEric Taylor }
1103681d9761SEric Taylor 
1104f64c0e34SEric Taylor void
1105f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1106f64c0e34SEric Taylor {
1107f64c0e34SEric Taylor 	taskq_t *tq;
1108f64c0e34SEric Taylor 	int children = vd->vdev_children;
1109f64c0e34SEric Taylor 
1110681d9761SEric Taylor 	/*
1111681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1112681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1113681d9761SEric Taylor 	 * spa_namespace_lock
1114681d9761SEric Taylor 	 */
1115681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
1116681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1117681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1118681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
1119681d9761SEric Taylor 		return;
1120681d9761SEric Taylor 	}
1121f64c0e34SEric Taylor 	tq = taskq_create("vdev_open", children, minclsyspri,
1122f64c0e34SEric Taylor 	    children, children, TASKQ_PREPOPULATE);
1123f64c0e34SEric Taylor 
1124f64c0e34SEric Taylor 	for (int c = 0; c < children; c++)
1125f64c0e34SEric Taylor 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1126f64c0e34SEric Taylor 		    TQ_SLEEP) != NULL);
1127f64c0e34SEric Taylor 
1128f64c0e34SEric Taylor 	taskq_destroy(tq);
1129f64c0e34SEric Taylor }
1130f64c0e34SEric Taylor 
1131fa9e4066Sahrens /*
1132fa9e4066Sahrens  * Prepare a virtual device for access.
1133fa9e4066Sahrens  */
1134fa9e4066Sahrens int
1135fa9e4066Sahrens vdev_open(vdev_t *vd)
1136fa9e4066Sahrens {
11378ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1138fa9e4066Sahrens 	int error;
1139fa9e4066Sahrens 	uint64_t osize = 0;
11404263d13fSGeorge Wilson 	uint64_t max_osize = 0;
11414263d13fSGeorge Wilson 	uint64_t asize, max_asize, psize;
1142ecc2d604Sbonwick 	uint64_t ashift = 0;
1143fa9e4066Sahrens 
1144f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1145f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1146fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1147fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1148fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1149fa9e4066Sahrens 
1150fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1151e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1152e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1153573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1154fa9e4066Sahrens 
1155069f55e2SEric Schrock 	/*
1156069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1157069f55e2SEric Schrock 	 * faulted, bail out of the open.
1158069f55e2SEric Schrock 	 */
11593d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
11603d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1161069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1162069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
11633d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1164069f55e2SEric Schrock 		    vd->vdev_label_aux);
1165be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
11663d7072f8Seschrock 	} else if (vd->vdev_offline) {
1167fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1168ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1169be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1170fa9e4066Sahrens 	}
1171fa9e4066Sahrens 
11724263d13fSGeorge Wilson 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1173fa9e4066Sahrens 
1174095bcd66SGeorge Wilson 	/*
1175095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1176095bcd66SGeorge Wilson 	 * the vdev on error.
1177095bcd66SGeorge Wilson 	 */
1178095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1179ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
11808956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1181ea8dc4b6Seschrock 
1182fa9e4066Sahrens 	if (error) {
11833d7072f8Seschrock 		if (vd->vdev_removed &&
11843d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
11853d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
11863d7072f8Seschrock 
1187ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1188fa9e4066Sahrens 		    vd->vdev_stat.vs_aux);
1189fa9e4066Sahrens 		return (error);
1190fa9e4066Sahrens 	}
1191fa9e4066Sahrens 
11923d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
11933d7072f8Seschrock 
1194096d22d4SEric Schrock 	/*
1195096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1196096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1197096d22d4SEric Schrock 	 */
1198096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1199096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1200096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1201096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1202096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1203096d22d4SEric Schrock 		    vd->vdev_label_aux);
1204be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1205096d22d4SEric Schrock 	}
1206096d22d4SEric Schrock 
12073d7072f8Seschrock 	if (vd->vdev_degraded) {
12083d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
12093d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
12103d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
12113d7072f8Seschrock 	} else {
1212069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
12133d7072f8Seschrock 	}
1214fa9e4066Sahrens 
121588ecc943SGeorge Wilson 	/*
121688ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
121788ecc943SGeorge Wilson 	 */
121888ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
121988ecc943SGeorge Wilson 		return (0);
122088ecc943SGeorge Wilson 
1221573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1222ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1223ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1224ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1225ea8dc4b6Seschrock 			break;
1226ea8dc4b6Seschrock 		}
1227573ca77eSGeorge Wilson 	}
1228fa9e4066Sahrens 
1229fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
12304263d13fSGeorge Wilson 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1231fa9e4066Sahrens 
1232fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1233fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1234ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1235ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1236be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1237fa9e4066Sahrens 		}
1238fa9e4066Sahrens 		psize = osize;
1239fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
12404263d13fSGeorge Wilson 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
12414263d13fSGeorge Wilson 		    VDEV_LABEL_END_SIZE);
1242fa9e4066Sahrens 	} else {
1243ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1244fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1245ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1246ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1247be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1248fa9e4066Sahrens 		}
1249fa9e4066Sahrens 		psize = 0;
1250fa9e4066Sahrens 		asize = osize;
12514263d13fSGeorge Wilson 		max_asize = max_osize;
1252fa9e4066Sahrens 	}
1253fa9e4066Sahrens 
1254fa9e4066Sahrens 	vd->vdev_psize = psize;
1255fa9e4066Sahrens 
1256573ca77eSGeorge Wilson 	/*
1257573ca77eSGeorge Wilson 	 * Make sure the allocatable size hasn't shrunk.
1258573ca77eSGeorge Wilson 	 */
1259573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1260573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1261573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1262be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1263573ca77eSGeorge Wilson 	}
1264573ca77eSGeorge Wilson 
1265fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1266fa9e4066Sahrens 		/*
1267fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
1268ecc2d604Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1269fa9e4066Sahrens 		 */
1270fa9e4066Sahrens 		vd->vdev_asize = asize;
12714263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1272ecc2d604Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1273fa9e4066Sahrens 	} else {
1274fa9e4066Sahrens 		/*
12752384d9f8SGeorge Wilson 		 * Detect if the alignment requirement has increased.
12762384d9f8SGeorge Wilson 		 * We don't want to make the pool unavailable, just
12772384d9f8SGeorge Wilson 		 * issue a warning instead.
1278fa9e4066Sahrens 		 */
12792384d9f8SGeorge Wilson 		if (ashift > vd->vdev_top->vdev_ashift &&
12802384d9f8SGeorge Wilson 		    vd->vdev_ops->vdev_op_leaf) {
12812384d9f8SGeorge Wilson 			cmn_err(CE_WARN,
12822384d9f8SGeorge Wilson 			    "Disk, '%s', has a block alignment that is "
12832384d9f8SGeorge Wilson 			    "larger than the pool's alignment\n",
12842384d9f8SGeorge Wilson 			    vd->vdev_path);
1285fa9e4066Sahrens 		}
12864263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1287573ca77eSGeorge Wilson 	}
1288fa9e4066Sahrens 
1289573ca77eSGeorge Wilson 	/*
1290573ca77eSGeorge Wilson 	 * If all children are healthy and the asize has increased,
1291573ca77eSGeorge Wilson 	 * then we've experienced dynamic LUN growth.  If automatic
1292573ca77eSGeorge Wilson 	 * expansion is enabled then use the additional space.
1293573ca77eSGeorge Wilson 	 */
1294573ca77eSGeorge Wilson 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1295573ca77eSGeorge Wilson 	    (vd->vdev_expanding || spa->spa_autoexpand))
1296573ca77eSGeorge Wilson 		vd->vdev_asize = asize;
1297fa9e4066Sahrens 
1298573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1299fa9e4066Sahrens 
13000a4e9518Sgw 	/*
13010a4e9518Sgw 	 * Ensure we can issue some IO before declaring the
13020a4e9518Sgw 	 * vdev open for business.
13030a4e9518Sgw 	 */
1304e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1305e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
130698d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
130798d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
13080a4e9518Sgw 		return (error);
13090a4e9518Sgw 	}
13100a4e9518Sgw 
131181cd5c55SMatthew Ahrens 	/*
131281cd5c55SMatthew Ahrens 	 * Track the min and max ashift values for normal data devices.
131381cd5c55SMatthew Ahrens 	 */
131481cd5c55SMatthew Ahrens 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
131581cd5c55SMatthew Ahrens 	    !vd->vdev_islog && vd->vdev_aux == NULL) {
131681cd5c55SMatthew Ahrens 		if (vd->vdev_ashift > spa->spa_max_ashift)
131781cd5c55SMatthew Ahrens 			spa->spa_max_ashift = vd->vdev_ashift;
131881cd5c55SMatthew Ahrens 		if (vd->vdev_ashift < spa->spa_min_ashift)
131981cd5c55SMatthew Ahrens 			spa->spa_min_ashift = vd->vdev_ashift;
132081cd5c55SMatthew Ahrens 	}
132181cd5c55SMatthew Ahrens 
1322088f3894Sahrens 	/*
1323088f3894Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
13248ad4d6ddSJeff Bonwick 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
13258ad4d6ddSJeff Bonwick 	 * since this would just restart the scrub we are already doing.
1326088f3894Sahrens 	 */
13278ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
13288ad4d6ddSJeff Bonwick 	    vdev_resilver_needed(vd, NULL, NULL))
13298ad4d6ddSJeff Bonwick 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1330088f3894Sahrens 
1331fa9e4066Sahrens 	return (0);
1332fa9e4066Sahrens }
1333fa9e4066Sahrens 
1334560e6e96Seschrock /*
1335560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
1336560e6e96Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
13373d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1338560e6e96Seschrock  *
1339d7f601efSGeorge Wilson  * If 'strict' is false ignore the spa guid check. This is necessary because
1340d7f601efSGeorge Wilson  * if the machine crashed during a re-guid the new guid might have been written
1341d7f601efSGeorge Wilson  * to all of the vdev labels, but not the cached config. The strict check
1342d7f601efSGeorge Wilson  * will be performed when the pool is opened again using the mos config.
1343d7f601efSGeorge Wilson  *
1344560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1345560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1346560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1347560e6e96Seschrock  * will be updated but the function will return 0.
1348560e6e96Seschrock  */
1349560e6e96Seschrock int
1350d7f601efSGeorge Wilson vdev_validate(vdev_t *vd, boolean_t strict)
1351560e6e96Seschrock {
1352560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1353560e6e96Seschrock 	nvlist_t *label;
13541195e687SMark J Musante 	uint64_t guid = 0, top_guid;
1355560e6e96Seschrock 	uint64_t state;
1356560e6e96Seschrock 
1357573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1358d7f601efSGeorge Wilson 		if (vdev_validate(vd->vdev_child[c], strict) != 0)
1359be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1360560e6e96Seschrock 
1361b5989ec7Seschrock 	/*
1362b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1363b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1364b5989ec7Seschrock 	 * overwrite the previous state.
1365b5989ec7Seschrock 	 */
1366e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
13671195e687SMark J Musante 		uint64_t aux_guid = 0;
13681195e687SMark J Musante 		nvlist_t *nvl;
1369bda88194SGeorge Wilson 		uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1370bda88194SGeorge Wilson 		    spa_last_synced_txg(spa) : -1ULL;
1371560e6e96Seschrock 
1372dfbb9432SGeorge Wilson 		if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1373560e6e96Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1374560e6e96Seschrock 			    VDEV_AUX_BAD_LABEL);
1375560e6e96Seschrock 			return (0);
1376560e6e96Seschrock 		}
1377560e6e96Seschrock 
13781195e687SMark J Musante 		/*
13791195e687SMark J Musante 		 * Determine if this vdev has been split off into another
13801195e687SMark J Musante 		 * pool.  If so, then refuse to open it.
13811195e687SMark J Musante 		 */
13821195e687SMark J Musante 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
13831195e687SMark J Musante 		    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
13841195e687SMark J Musante 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13851195e687SMark J Musante 			    VDEV_AUX_SPLIT_POOL);
13861195e687SMark J Musante 			nvlist_free(label);
13871195e687SMark J Musante 			return (0);
13881195e687SMark J Musante 		}
13891195e687SMark J Musante 
1390d7f601efSGeorge Wilson 		if (strict && (nvlist_lookup_uint64(label,
1391d7f601efSGeorge Wilson 		    ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1392d7f601efSGeorge Wilson 		    guid != spa_guid(spa))) {
1393560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1394560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1395560e6e96Seschrock 			nvlist_free(label);
1396560e6e96Seschrock 			return (0);
1397560e6e96Seschrock 		}
1398560e6e96Seschrock 
13991195e687SMark J Musante 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
14001195e687SMark J Musante 		    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
14011195e687SMark J Musante 		    &aux_guid) != 0)
14021195e687SMark J Musante 			aux_guid = 0;
14031195e687SMark J Musante 
1404e14bb325SJeff Bonwick 		/*
1405e14bb325SJeff Bonwick 		 * If this vdev just became a top-level vdev because its
1406e14bb325SJeff Bonwick 		 * sibling was detached, it will have adopted the parent's
1407e14bb325SJeff Bonwick 		 * vdev guid -- but the label may or may not be on disk yet.
1408e14bb325SJeff Bonwick 		 * Fortunately, either version of the label will have the
1409e14bb325SJeff Bonwick 		 * same top guid, so if we're a top-level vdev, we can
1410e14bb325SJeff Bonwick 		 * safely compare to that instead.
14111195e687SMark J Musante 		 *
14121195e687SMark J Musante 		 * If we split this vdev off instead, then we also check the
14131195e687SMark J Musante 		 * original pool's guid.  We don't want to consider the vdev
14141195e687SMark J Musante 		 * corrupt if it is partway through a split operation.
1415e14bb325SJeff Bonwick 		 */
1416560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1417e14bb325SJeff Bonwick 		    &guid) != 0 ||
1418e14bb325SJeff Bonwick 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1419e14bb325SJeff Bonwick 		    &top_guid) != 0 ||
14201195e687SMark J Musante 		    ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1421e14bb325SJeff Bonwick 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1422560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1423560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1424560e6e96Seschrock 			nvlist_free(label);
1425560e6e96Seschrock 			return (0);
1426560e6e96Seschrock 		}
1427560e6e96Seschrock 
1428560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1429560e6e96Seschrock 		    &state) != 0) {
1430560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1431560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1432560e6e96Seschrock 			nvlist_free(label);
1433560e6e96Seschrock 			return (0);
1434560e6e96Seschrock 		}
1435560e6e96Seschrock 
1436560e6e96Seschrock 		nvlist_free(label);
1437560e6e96Seschrock 
1438bc758434SLin Ling 		/*
14394b964adaSGeorge Wilson 		 * If this is a verbatim import, no need to check the
1440bc758434SLin Ling 		 * state of the pool.
1441bc758434SLin Ling 		 */
14424b964adaSGeorge Wilson 		if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1443b16da2e2SGeorge Wilson 		    spa_load_state(spa) == SPA_LOAD_OPEN &&
1444bc758434SLin Ling 		    state != POOL_STATE_ACTIVE)
1445be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1446560e6e96Seschrock 
144751ece835Seschrock 		/*
144851ece835Seschrock 		 * If we were able to open and validate a vdev that was
144951ece835Seschrock 		 * previously marked permanently unavailable, clear that state
145051ece835Seschrock 		 * now.
145151ece835Seschrock 		 */
145251ece835Seschrock 		if (vd->vdev_not_present)
145351ece835Seschrock 			vd->vdev_not_present = 0;
145451ece835Seschrock 	}
1455560e6e96Seschrock 
1456560e6e96Seschrock 	return (0);
1457560e6e96Seschrock }
1458560e6e96Seschrock 
1459fa9e4066Sahrens /*
1460fa9e4066Sahrens  * Close a virtual device.
1461fa9e4066Sahrens  */
1462fa9e4066Sahrens void
1463fa9e4066Sahrens vdev_close(vdev_t *vd)
1464fa9e4066Sahrens {
14658ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1466095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
14678ad4d6ddSJeff Bonwick 
14688ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
14698ad4d6ddSJeff Bonwick 
14701195e687SMark J Musante 	/*
14711195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
14721195e687SMark J Musante 	 * going offline.
14731195e687SMark J Musante 	 */
1474095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
14751195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1476095bcd66SGeorge Wilson 
1477fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1478fa9e4066Sahrens 
14793d7072f8Seschrock 	vdev_cache_purge(vd);
1480fa9e4066Sahrens 
1481560e6e96Seschrock 	/*
1482573ca77eSGeorge Wilson 	 * We record the previous state before we close it, so that if we are
1483560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1484560e6e96Seschrock 	 * it's still faulted.
1485560e6e96Seschrock 	 */
1486560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
1487560e6e96Seschrock 
1488fa9e4066Sahrens 	if (vd->vdev_offline)
1489fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1490fa9e4066Sahrens 	else
1491fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
1492ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1493fa9e4066Sahrens }
1494fa9e4066Sahrens 
1495dcba9f3fSGeorge Wilson void
1496dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
1497dcba9f3fSGeorge Wilson {
1498dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1499dcba9f3fSGeorge Wilson 
1500dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1501dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1502dcba9f3fSGeorge Wilson 		return;
1503dcba9f3fSGeorge Wilson 
1504dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1505dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
1506dcba9f3fSGeorge Wilson 
1507dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1508dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
1509dcba9f3fSGeorge Wilson }
1510dcba9f3fSGeorge Wilson 
1511dcba9f3fSGeorge Wilson void
1512dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
1513dcba9f3fSGeorge Wilson {
1514dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1515dcba9f3fSGeorge Wilson 
1516dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1517dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1518dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
1519dcba9f3fSGeorge Wilson 
1520dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1521dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
1522dcba9f3fSGeorge Wilson }
1523dcba9f3fSGeorge Wilson 
1524095bcd66SGeorge Wilson /*
1525095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1526095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
1527095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1528095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
1529095bcd66SGeorge Wilson  */
1530fa9e4066Sahrens void
1531ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
1532fa9e4066Sahrens {
1533ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
1534fa9e4066Sahrens 
1535e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1536ea8dc4b6Seschrock 
15371195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
15381195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
1539fa9e4066Sahrens 	vdev_close(vd);
1540fa9e4066Sahrens 	(void) vdev_open(vd);
1541fa9e4066Sahrens 
154239c23413Seschrock 	/*
154339c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
154439c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
154539c23413Seschrock 	 * opened in response to vdev_reopen().
154639c23413Seschrock 	 */
1547c5904d13Seschrock 	if (vd->vdev_aux) {
1548c5904d13Seschrock 		(void) vdev_validate_aux(vd);
1549e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
15506809eb4eSEric Schrock 		    vd->vdev_aux == &spa->spa_l2cache &&
1551573ca77eSGeorge Wilson 		    !l2arc_vdev_present(vd))
1552573ca77eSGeorge Wilson 			l2arc_add_vdev(spa, vd);
1553c5904d13Seschrock 	} else {
1554bda88194SGeorge Wilson 		(void) vdev_validate(vd, B_TRUE);
1555c5904d13Seschrock 	}
155639c23413Seschrock 
1557fa9e4066Sahrens 	/*
15583d7072f8Seschrock 	 * Reassess parent vdev's health.
1559fa9e4066Sahrens 	 */
15603d7072f8Seschrock 	vdev_propagate_state(vd);
1561fa9e4066Sahrens }
1562fa9e4066Sahrens 
1563fa9e4066Sahrens int
156499653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1565fa9e4066Sahrens {
1566fa9e4066Sahrens 	int error;
1567fa9e4066Sahrens 
1568fa9e4066Sahrens 	/*
1569fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1570fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
1571fa9e4066Sahrens 	 * there are any components we can't open.
1572fa9e4066Sahrens 	 */
1573fa9e4066Sahrens 	error = vdev_open(vd);
1574fa9e4066Sahrens 
1575fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1576fa9e4066Sahrens 		vdev_close(vd);
1577fa9e4066Sahrens 		return (error ? error : ENXIO);
1578fa9e4066Sahrens 	}
1579fa9e4066Sahrens 
1580fa9e4066Sahrens 	/*
15810713e232SGeorge Wilson 	 * Recursively load DTLs and initialize all labels.
1582fa9e4066Sahrens 	 */
15830713e232SGeorge Wilson 	if ((error = vdev_dtl_load(vd)) != 0 ||
15840713e232SGeorge Wilson 	    (error = vdev_label_init(vd, txg, isreplacing ?
158539c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1586fa9e4066Sahrens 		vdev_close(vd);
1587fa9e4066Sahrens 		return (error);
1588fa9e4066Sahrens 	}
1589fa9e4066Sahrens 
1590fa9e4066Sahrens 	return (0);
1591fa9e4066Sahrens }
1592fa9e4066Sahrens 
15930e34b6a7Sbonwick void
1594573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
1595fa9e4066Sahrens {
1596fa9e4066Sahrens 	/*
1597bf3e216cSMatthew Ahrens 	 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1598fa9e4066Sahrens 	 */
1599bf3e216cSMatthew Ahrens 	vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
1600fa9e4066Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1601fa9e4066Sahrens }
1602fa9e4066Sahrens 
1603fa9e4066Sahrens void
1604ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1605fa9e4066Sahrens {
1606ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
160788ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
1608ecc2d604Sbonwick 	ASSERT(ISP2(flags));
1609f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
1610fa9e4066Sahrens 
1611ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
1612ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1613ecc2d604Sbonwick 
1614ecc2d604Sbonwick 	if (flags & VDD_DTL)
1615ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1616ecc2d604Sbonwick 
1617ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1618fa9e4066Sahrens }
1619fa9e4066Sahrens 
16200713e232SGeorge Wilson void
16210713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
16220713e232SGeorge Wilson {
16230713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
16240713e232SGeorge Wilson 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
16250713e232SGeorge Wilson 
16260713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
16270713e232SGeorge Wilson 		vdev_dirty(vd->vdev_top, flags, vd, txg);
16280713e232SGeorge Wilson }
16290713e232SGeorge Wilson 
16308ad4d6ddSJeff Bonwick /*
16318ad4d6ddSJeff Bonwick  * DTLs.
16328ad4d6ddSJeff Bonwick  *
16338ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
16349fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
16358ad4d6ddSJeff Bonwick  *
16368ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
16378ad4d6ddSJeff Bonwick  *
16388ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
16398ad4d6ddSJeff Bonwick  *
16408ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
16418ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
16428ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
16438ad4d6ddSJeff Bonwick  *
16448ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
16458ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
16468ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
16478ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
16488ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
16498ad4d6ddSJeff Bonwick  *
16508ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
16518ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
16528ad4d6ddSJeff Bonwick  *
16538ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
16548ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
16558ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
16568ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
16578ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
16588ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
16598ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
16608ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
16618ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
16628ad4d6ddSJeff Bonwick  *
16638ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
16648ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
16658ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
16668ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
16678ad4d6ddSJeff Bonwick  */
1668fa9e4066Sahrens void
16698ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1670fa9e4066Sahrens {
16710713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
16728ad4d6ddSJeff Bonwick 
16738ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
16748ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1675f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
16768ad4d6ddSJeff Bonwick 
16770713e232SGeorge Wilson 	mutex_enter(rt->rt_lock);
16780713e232SGeorge Wilson 	if (!range_tree_contains(rt, txg, size))
16790713e232SGeorge Wilson 		range_tree_add(rt, txg, size);
16800713e232SGeorge Wilson 	mutex_exit(rt->rt_lock);
1681fa9e4066Sahrens }
1682fa9e4066Sahrens 
16838ad4d6ddSJeff Bonwick boolean_t
16848ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1685fa9e4066Sahrens {
16860713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
16878ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
1688fa9e4066Sahrens 
16898ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
16908ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1691fa9e4066Sahrens 
16920713e232SGeorge Wilson 	mutex_enter(rt->rt_lock);
16930713e232SGeorge Wilson 	if (range_tree_space(rt) != 0)
16940713e232SGeorge Wilson 		dirty = range_tree_contains(rt, txg, size);
16950713e232SGeorge Wilson 	mutex_exit(rt->rt_lock);
1696fa9e4066Sahrens 
1697fa9e4066Sahrens 	return (dirty);
1698fa9e4066Sahrens }
1699fa9e4066Sahrens 
17008ad4d6ddSJeff Bonwick boolean_t
17018ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
17028ad4d6ddSJeff Bonwick {
17030713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
17048ad4d6ddSJeff Bonwick 	boolean_t empty;
17058ad4d6ddSJeff Bonwick 
17060713e232SGeorge Wilson 	mutex_enter(rt->rt_lock);
17070713e232SGeorge Wilson 	empty = (range_tree_space(rt) == 0);
17080713e232SGeorge Wilson 	mutex_exit(rt->rt_lock);
17098ad4d6ddSJeff Bonwick 
17108ad4d6ddSJeff Bonwick 	return (empty);
17118ad4d6ddSJeff Bonwick }
17128ad4d6ddSJeff Bonwick 
1713b4952e17SGeorge Wilson /*
1714b4952e17SGeorge Wilson  * Returns the lowest txg in the DTL range.
1715b4952e17SGeorge Wilson  */
1716b4952e17SGeorge Wilson static uint64_t
1717b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd)
1718b4952e17SGeorge Wilson {
17190713e232SGeorge Wilson 	range_seg_t *rs;
1720b4952e17SGeorge Wilson 
1721b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
17220713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1723b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
1724b4952e17SGeorge Wilson 
17250713e232SGeorge Wilson 	rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
17260713e232SGeorge Wilson 	return (rs->rs_start - 1);
1727b4952e17SGeorge Wilson }
1728b4952e17SGeorge Wilson 
1729b4952e17SGeorge Wilson /*
1730b4952e17SGeorge Wilson  * Returns the highest txg in the DTL.
1731b4952e17SGeorge Wilson  */
1732b4952e17SGeorge Wilson static uint64_t
1733b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd)
1734b4952e17SGeorge Wilson {
17350713e232SGeorge Wilson 	range_seg_t *rs;
1736b4952e17SGeorge Wilson 
1737b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
17380713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1739b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
1740b4952e17SGeorge Wilson 
17410713e232SGeorge Wilson 	rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
17420713e232SGeorge Wilson 	return (rs->rs_end);
1743b4952e17SGeorge Wilson }
1744b4952e17SGeorge Wilson 
1745b4952e17SGeorge Wilson /*
1746b4952e17SGeorge Wilson  * Determine if a resilvering vdev should remove any DTL entries from
1747b4952e17SGeorge Wilson  * its range. If the vdev was resilvering for the entire duration of the
1748b4952e17SGeorge Wilson  * scan then it should excise that range from its DTLs. Otherwise, this
1749b4952e17SGeorge Wilson  * vdev is considered partially resilvered and should leave its DTL
1750b4952e17SGeorge Wilson  * entries intact. The comment in vdev_dtl_reassess() describes how we
1751b4952e17SGeorge Wilson  * excise the DTLs.
1752b4952e17SGeorge Wilson  */
1753b4952e17SGeorge Wilson static boolean_t
1754b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd)
1755b4952e17SGeorge Wilson {
1756b4952e17SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1757b4952e17SGeorge Wilson 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1758b4952e17SGeorge Wilson 
1759b4952e17SGeorge Wilson 	ASSERT0(scn->scn_phys.scn_errors);
1760b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
1761b4952e17SGeorge Wilson 
1762b4952e17SGeorge Wilson 	if (vd->vdev_resilver_txg == 0 ||
17630713e232SGeorge Wilson 	    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1764b4952e17SGeorge Wilson 		return (B_TRUE);
1765b4952e17SGeorge Wilson 
1766b4952e17SGeorge Wilson 	/*
1767b4952e17SGeorge Wilson 	 * When a resilver is initiated the scan will assign the scn_max_txg
1768b4952e17SGeorge Wilson 	 * value to the highest txg value that exists in all DTLs. If this
1769b4952e17SGeorge Wilson 	 * device's max DTL is not part of this scan (i.e. it is not in
1770b4952e17SGeorge Wilson 	 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1771b4952e17SGeorge Wilson 	 * for excision.
1772b4952e17SGeorge Wilson 	 */
1773b4952e17SGeorge Wilson 	if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1774b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1775b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1776b4952e17SGeorge Wilson 		ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1777b4952e17SGeorge Wilson 		return (B_TRUE);
1778b4952e17SGeorge Wilson 	}
1779b4952e17SGeorge Wilson 	return (B_FALSE);
1780b4952e17SGeorge Wilson }
1781b4952e17SGeorge Wilson 
1782fa9e4066Sahrens /*
1783fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
1784fa9e4066Sahrens  */
1785fa9e4066Sahrens void
1786fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1787fa9e4066Sahrens {
1788ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
17898ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
17908ad4d6ddSJeff Bonwick 	int minref;
1791fa9e4066Sahrens 
17928ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1793fa9e4066Sahrens 
17948ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
17958ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
17968ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
17978ad4d6ddSJeff Bonwick 
1798b24ab676SJeff Bonwick 	if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
17998ad4d6ddSJeff Bonwick 		return;
18008ad4d6ddSJeff Bonwick 
18018ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
18023f9d6ad7SLin Ling 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
18033f9d6ad7SLin Ling 
1804fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
1805b4952e17SGeorge Wilson 
1806b4952e17SGeorge Wilson 		/*
1807b4952e17SGeorge Wilson 		 * If we've completed a scan cleanly then determine
1808b4952e17SGeorge Wilson 		 * if this vdev should remove any DTLs. We only want to
1809b4952e17SGeorge Wilson 		 * excise regions on vdevs that were available during
1810b4952e17SGeorge Wilson 		 * the entire duration of this scan.
1811b4952e17SGeorge Wilson 		 */
1812088f3894Sahrens 		if (scrub_txg != 0 &&
18133f9d6ad7SLin Ling 		    (spa->spa_scrub_started ||
1814b4952e17SGeorge Wilson 		    (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1815b4952e17SGeorge Wilson 		    vdev_dtl_should_excise(vd)) {
1816088f3894Sahrens 			/*
1817088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
1818088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
1819088f3894Sahrens 			 * will be valid, so excise the old region and
1820088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
1821088f3894Sahrens 			 * dtl as-is if there was an error.
18228ad4d6ddSJeff Bonwick 			 *
18238ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
18248ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
18258ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
18268ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
18278ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
18288ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
18298ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
18308ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
18318ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
1832088f3894Sahrens 			 */
18330713e232SGeorge Wilson 			space_reftree_create(&reftree);
18340713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
18350713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
18360713e232SGeorge Wilson 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
18370713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
18380713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_SCRUB], 2);
18390713e232SGeorge Wilson 			space_reftree_generate_map(&reftree,
18400713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
18410713e232SGeorge Wilson 			space_reftree_destroy(&reftree);
1842fa9e4066Sahrens 		}
18430713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
18440713e232SGeorge Wilson 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
18450713e232SGeorge Wilson 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
1846fa9e4066Sahrens 		if (scrub_done)
18470713e232SGeorge Wilson 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
18480713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
18498ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
18500713e232SGeorge Wilson 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
18518ad4d6ddSJeff Bonwick 		else
18520713e232SGeorge Wilson 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
18530713e232SGeorge Wilson 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
1854b4952e17SGeorge Wilson 
1855b4952e17SGeorge Wilson 		/*
1856b4952e17SGeorge Wilson 		 * If the vdev was resilvering and no longer has any
1857b4952e17SGeorge Wilson 		 * DTLs then reset its resilvering flag.
1858b4952e17SGeorge Wilson 		 */
1859b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0 &&
18600713e232SGeorge Wilson 		    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
18610713e232SGeorge Wilson 		    range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
1862b4952e17SGeorge Wilson 			vd->vdev_resilver_txg = 0;
1863b4952e17SGeorge Wilson 
1864fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
1865088f3894Sahrens 
1866ecc2d604Sbonwick 		if (txg != 0)
1867ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1868fa9e4066Sahrens 		return;
1869fa9e4066Sahrens 	}
1870fa9e4066Sahrens 
1871fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
18728ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
187399bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
187499bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
18758ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
18768ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
18778ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
18788ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
18798ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
18808ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
18818ad4d6ddSJeff Bonwick 		else
18828ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
18830713e232SGeorge Wilson 		space_reftree_create(&reftree);
18848ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
18858ad4d6ddSJeff Bonwick 			vdev_t *cvd = vd->vdev_child[c];
18868ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
18870713e232SGeorge Wilson 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
18888ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
18898ad4d6ddSJeff Bonwick 		}
18900713e232SGeorge Wilson 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
18910713e232SGeorge Wilson 		space_reftree_destroy(&reftree);
1892fa9e4066Sahrens 	}
18938ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
1894fa9e4066Sahrens }
1895fa9e4066Sahrens 
18960713e232SGeorge Wilson int
1897fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
1898fa9e4066Sahrens {
1899fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
1900ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
19010713e232SGeorge Wilson 	int error = 0;
1902fa9e4066Sahrens 
19030713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
19040713e232SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
1905fa9e4066Sahrens 
19060713e232SGeorge Wilson 		error = space_map_open(&vd->vdev_dtl_sm, mos,
19070713e232SGeorge Wilson 		    vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
19080713e232SGeorge Wilson 		if (error)
19090713e232SGeorge Wilson 			return (error);
19100713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
1911fa9e4066Sahrens 
19120713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
191388ecc943SGeorge Wilson 
19140713e232SGeorge Wilson 		/*
19150713e232SGeorge Wilson 		 * Now that we've opened the space_map we need to update
19160713e232SGeorge Wilson 		 * the in-core DTL.
19170713e232SGeorge Wilson 		 */
19180713e232SGeorge Wilson 		space_map_update(vd->vdev_dtl_sm);
1919ecc2d604Sbonwick 
19200713e232SGeorge Wilson 		error = space_map_load(vd->vdev_dtl_sm,
19210713e232SGeorge Wilson 		    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
19220713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
1923fa9e4066Sahrens 
19240713e232SGeorge Wilson 		return (error);
19250713e232SGeorge Wilson 	}
19260713e232SGeorge Wilson 
19270713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
19280713e232SGeorge Wilson 		error = vdev_dtl_load(vd->vdev_child[c]);
19290713e232SGeorge Wilson 		if (error != 0)
19300713e232SGeorge Wilson 			break;
19310713e232SGeorge Wilson 	}
1932fa9e4066Sahrens 
1933fa9e4066Sahrens 	return (error);
1934fa9e4066Sahrens }
1935fa9e4066Sahrens 
1936fa9e4066Sahrens void
1937fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1938fa9e4066Sahrens {
1939fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
19400713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
1941ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
19420713e232SGeorge Wilson 	range_tree_t *rtsync;
19430713e232SGeorge Wilson 	kmutex_t rtlock;
1944fa9e4066Sahrens 	dmu_tx_t *tx;
19450713e232SGeorge Wilson 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
1946fa9e4066Sahrens 
194788ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
19480713e232SGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
194988ecc943SGeorge Wilson 
1950fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1951fa9e4066Sahrens 
19520713e232SGeorge Wilson 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
19530713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
19540713e232SGeorge Wilson 		space_map_free(vd->vdev_dtl_sm, tx);
19550713e232SGeorge Wilson 		space_map_close(vd->vdev_dtl_sm);
19560713e232SGeorge Wilson 		vd->vdev_dtl_sm = NULL;
19570713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
1958fa9e4066Sahrens 		dmu_tx_commit(tx);
1959fa9e4066Sahrens 		return;
1960fa9e4066Sahrens 	}
1961fa9e4066Sahrens 
19620713e232SGeorge Wilson 	if (vd->vdev_dtl_sm == NULL) {
19630713e232SGeorge Wilson 		uint64_t new_object;
19640713e232SGeorge Wilson 
19650713e232SGeorge Wilson 		new_object = space_map_alloc(mos, tx);
19660713e232SGeorge Wilson 		VERIFY3U(new_object, !=, 0);
19670713e232SGeorge Wilson 
19680713e232SGeorge Wilson 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
19690713e232SGeorge Wilson 		    0, -1ULL, 0, &vd->vdev_dtl_lock));
19700713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
1971fa9e4066Sahrens 	}
1972fa9e4066Sahrens 
19730713e232SGeorge Wilson 	mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
1974fa9e4066Sahrens 
19750713e232SGeorge Wilson 	rtsync = range_tree_create(NULL, NULL, &rtlock);
1976fa9e4066Sahrens 
19770713e232SGeorge Wilson 	mutex_enter(&rtlock);
1978fa9e4066Sahrens 
1979fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
19800713e232SGeorge Wilson 	range_tree_walk(rt, range_tree_add, rtsync);
1981fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1982fa9e4066Sahrens 
19830713e232SGeorge Wilson 	space_map_truncate(vd->vdev_dtl_sm, tx);
19840713e232SGeorge Wilson 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
19850713e232SGeorge Wilson 	range_tree_vacate(rtsync, NULL, NULL);
1986fa9e4066Sahrens 
19870713e232SGeorge Wilson 	range_tree_destroy(rtsync);
1988fa9e4066Sahrens 
19890713e232SGeorge Wilson 	mutex_exit(&rtlock);
19900713e232SGeorge Wilson 	mutex_destroy(&rtlock);
1991fa9e4066Sahrens 
19920713e232SGeorge Wilson 	/*
19930713e232SGeorge Wilson 	 * If the object for the space map has changed then dirty
19940713e232SGeorge Wilson 	 * the top level so that we update the config.
19950713e232SGeorge Wilson 	 */
19960713e232SGeorge Wilson 	if (object != space_map_object(vd->vdev_dtl_sm)) {
19970713e232SGeorge Wilson 		zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
19980713e232SGeorge Wilson 		    "new object %llu", txg, spa_name(spa), object,
19990713e232SGeorge Wilson 		    space_map_object(vd->vdev_dtl_sm));
20000713e232SGeorge Wilson 		vdev_config_dirty(vd->vdev_top);
20010713e232SGeorge Wilson 	}
2002fa9e4066Sahrens 
2003fa9e4066Sahrens 	dmu_tx_commit(tx);
20040713e232SGeorge Wilson 
20050713e232SGeorge Wilson 	mutex_enter(&vd->vdev_dtl_lock);
20060713e232SGeorge Wilson 	space_map_update(vd->vdev_dtl_sm);
20070713e232SGeorge Wilson 	mutex_exit(&vd->vdev_dtl_lock);
2008fa9e4066Sahrens }
2009fa9e4066Sahrens 
20108ad4d6ddSJeff Bonwick /*
20118ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
20128ad4d6ddSJeff Bonwick  * without losing data.
20138ad4d6ddSJeff Bonwick  */
20148ad4d6ddSJeff Bonwick boolean_t
20158ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
20168ad4d6ddSJeff Bonwick {
20178ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
20188ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
20198ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
20208ad4d6ddSJeff Bonwick 	boolean_t required;
20218ad4d6ddSJeff Bonwick 
20228ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
20238ad4d6ddSJeff Bonwick 
20248ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
20258ad4d6ddSJeff Bonwick 		return (B_TRUE);
20268ad4d6ddSJeff Bonwick 
20278ad4d6ddSJeff Bonwick 	/*
20288ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
20298ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
20308ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
20318ad4d6ddSJeff Bonwick 	 */
20328ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
20338ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
20348ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
20358ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
20368ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
20378ad4d6ddSJeff Bonwick 
2038cb04b873SMark J Musante 	if (!required && zio_injection_enabled)
2039cb04b873SMark J Musante 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2040cb04b873SMark J Musante 
20418ad4d6ddSJeff Bonwick 	return (required);
20428ad4d6ddSJeff Bonwick }
20438ad4d6ddSJeff Bonwick 
2044088f3894Sahrens /*
2045088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
2046088f3894Sahrens  */
2047088f3894Sahrens boolean_t
2048088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2049088f3894Sahrens {
2050088f3894Sahrens 	boolean_t needed = B_FALSE;
2051088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
2052088f3894Sahrens 	uint64_t thismax = 0;
2053088f3894Sahrens 
2054088f3894Sahrens 	if (vd->vdev_children == 0) {
2055088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
20560713e232SGeorge Wilson 		if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
20578ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
2058088f3894Sahrens 
2059b4952e17SGeorge Wilson 			thismin = vdev_dtl_min(vd);
2060b4952e17SGeorge Wilson 			thismax = vdev_dtl_max(vd);
2061088f3894Sahrens 			needed = B_TRUE;
2062088f3894Sahrens 		}
2063088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2064088f3894Sahrens 	} else {
20658ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
2066088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
2067088f3894Sahrens 			uint64_t cmin, cmax;
2068088f3894Sahrens 
2069088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2070088f3894Sahrens 				thismin = MIN(thismin, cmin);
2071088f3894Sahrens 				thismax = MAX(thismax, cmax);
2072088f3894Sahrens 				needed = B_TRUE;
2073088f3894Sahrens 			}
2074088f3894Sahrens 		}
2075088f3894Sahrens 	}
2076088f3894Sahrens 
2077088f3894Sahrens 	if (needed && minp) {
2078088f3894Sahrens 		*minp = thismin;
2079088f3894Sahrens 		*maxp = thismax;
2080088f3894Sahrens 	}
2081088f3894Sahrens 	return (needed);
2082088f3894Sahrens }
2083088f3894Sahrens 
2084560e6e96Seschrock void
2085ea8dc4b6Seschrock vdev_load(vdev_t *vd)
2086fa9e4066Sahrens {
2087fa9e4066Sahrens 	/*
2088fa9e4066Sahrens 	 * Recursively load all children.
2089fa9e4066Sahrens 	 */
20908ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2091560e6e96Seschrock 		vdev_load(vd->vdev_child[c]);
2092fa9e4066Sahrens 
2093fa9e4066Sahrens 	/*
20940e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
2095fa9e4066Sahrens 	 */
209688ecc943SGeorge Wilson 	if (vd == vd->vdev_top && !vd->vdev_ishole &&
2097560e6e96Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2098560e6e96Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
2099560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2100560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
2101fa9e4066Sahrens 
2102fa9e4066Sahrens 	/*
2103fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
2104fa9e4066Sahrens 	 */
2105560e6e96Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2106560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2107560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
2108fa9e4066Sahrens }
2109fa9e4066Sahrens 
211099653d4eSeschrock /*
2111fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
2112fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
2113fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
2114fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
2115fa94a07fSbrendan  * repurposed to another pool.
211699653d4eSeschrock  */
211799653d4eSeschrock int
2118fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
211999653d4eSeschrock {
212099653d4eSeschrock 	nvlist_t *label;
212199653d4eSeschrock 	uint64_t guid, version;
212299653d4eSeschrock 	uint64_t state;
212399653d4eSeschrock 
2124e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
2125c5904d13Seschrock 		return (0);
2126c5904d13Seschrock 
2127dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
212899653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
212999653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
213099653d4eSeschrock 		return (-1);
213199653d4eSeschrock 	}
213299653d4eSeschrock 
213399653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2134ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(version) ||
213599653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
213699653d4eSeschrock 	    guid != vd->vdev_guid ||
213799653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
213899653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
213999653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
214099653d4eSeschrock 		nvlist_free(label);
214199653d4eSeschrock 		return (-1);
214299653d4eSeschrock 	}
214399653d4eSeschrock 
214499653d4eSeschrock 	/*
214599653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
214699653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
214799653d4eSeschrock 	 */
214899653d4eSeschrock 	nvlist_free(label);
214999653d4eSeschrock 	return (0);
215099653d4eSeschrock }
215199653d4eSeschrock 
215288ecc943SGeorge Wilson void
215388ecc943SGeorge Wilson vdev_remove(vdev_t *vd, uint64_t txg)
215488ecc943SGeorge Wilson {
215588ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
215688ecc943SGeorge Wilson 	objset_t *mos = spa->spa_meta_objset;
215788ecc943SGeorge Wilson 	dmu_tx_t *tx;
215888ecc943SGeorge Wilson 
215988ecc943SGeorge Wilson 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
216088ecc943SGeorge Wilson 
216188ecc943SGeorge Wilson 	if (vd->vdev_ms != NULL) {
21622e4c9986SGeorge Wilson 		metaslab_group_t *mg = vd->vdev_mg;
21632e4c9986SGeorge Wilson 
21642e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
21652e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
21662e4c9986SGeorge Wilson 
216788ecc943SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
216888ecc943SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
216988ecc943SGeorge Wilson 
21700713e232SGeorge Wilson 			if (msp == NULL || msp->ms_sm == NULL)
217188ecc943SGeorge Wilson 				continue;
217288ecc943SGeorge Wilson 
21730713e232SGeorge Wilson 			mutex_enter(&msp->ms_lock);
21742e4c9986SGeorge Wilson 			/*
21752e4c9986SGeorge Wilson 			 * If the metaslab was not loaded when the vdev
21762e4c9986SGeorge Wilson 			 * was removed then the histogram accounting may
21772e4c9986SGeorge Wilson 			 * not be accurate. Update the histogram information
21782e4c9986SGeorge Wilson 			 * here so that we ensure that the metaslab group
21792e4c9986SGeorge Wilson 			 * and metaslab class are up-to-date.
21802e4c9986SGeorge Wilson 			 */
21812e4c9986SGeorge Wilson 			metaslab_group_histogram_remove(mg, msp);
21822e4c9986SGeorge Wilson 
21830713e232SGeorge Wilson 			VERIFY0(space_map_allocated(msp->ms_sm));
21840713e232SGeorge Wilson 			space_map_free(msp->ms_sm, tx);
21850713e232SGeorge Wilson 			space_map_close(msp->ms_sm);
21860713e232SGeorge Wilson 			msp->ms_sm = NULL;
21870713e232SGeorge Wilson 			mutex_exit(&msp->ms_lock);
218888ecc943SGeorge Wilson 		}
21892e4c9986SGeorge Wilson 
21902e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
21912e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
21922e4c9986SGeorge Wilson 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
21932e4c9986SGeorge Wilson 			ASSERT0(mg->mg_histogram[i]);
21942e4c9986SGeorge Wilson 
219588ecc943SGeorge Wilson 	}
219688ecc943SGeorge Wilson 
219788ecc943SGeorge Wilson 	if (vd->vdev_ms_array) {
219888ecc943SGeorge Wilson 		(void) dmu_object_free(mos, vd->vdev_ms_array, tx);
219988ecc943SGeorge Wilson 		vd->vdev_ms_array = 0;
220088ecc943SGeorge Wilson 	}
220188ecc943SGeorge Wilson 	dmu_tx_commit(tx);
220288ecc943SGeorge Wilson }
220388ecc943SGeorge Wilson 
2204fa9e4066Sahrens void
2205fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
2206fa9e4066Sahrens {
2207fa9e4066Sahrens 	metaslab_t *msp;
220880eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2209fa9e4066Sahrens 
221088ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
221188ecc943SGeorge Wilson 
2212fa9e4066Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2213fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
221480eb36f2SGeorge Wilson 
221580eb36f2SGeorge Wilson 	if (reassess)
221680eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
2217fa9e4066Sahrens }
2218fa9e4066Sahrens 
2219fa9e4066Sahrens void
2220fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2221fa9e4066Sahrens {
2222fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2223fa9e4066Sahrens 	vdev_t *lvd;
2224fa9e4066Sahrens 	metaslab_t *msp;
2225ecc2d604Sbonwick 	dmu_tx_t *tx;
2226fa9e4066Sahrens 
222788ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
222888ecc943SGeorge Wilson 
2229ecc2d604Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2230ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
2231ecc2d604Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2232ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2233ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2234ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
2235ecc2d604Sbonwick 		vdev_config_dirty(vd);
2236ecc2d604Sbonwick 		dmu_tx_commit(tx);
2237ecc2d604Sbonwick 	}
2238fa9e4066Sahrens 
22393f9d6ad7SLin Ling 	/*
22403f9d6ad7SLin Ling 	 * Remove the metadata associated with this vdev once it's empty.
22413f9d6ad7SLin Ling 	 */
22423f9d6ad7SLin Ling 	if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
224388ecc943SGeorge Wilson 		vdev_remove(vd, txg);
224488ecc943SGeorge Wilson 
2245ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2246fa9e4066Sahrens 		metaslab_sync(msp, txg);
2247ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2248ecc2d604Sbonwick 	}
2249fa9e4066Sahrens 
2250fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2251fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
2252fa9e4066Sahrens 
2253fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2254fa9e4066Sahrens }
2255fa9e4066Sahrens 
2256fa9e4066Sahrens uint64_t
2257fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2258fa9e4066Sahrens {
2259fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2260fa9e4066Sahrens }
2261fa9e4066Sahrens 
22623d7072f8Seschrock /*
22633d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
22643d7072f8Seschrock  * not be opened, and no I/O is attempted.
22653d7072f8Seschrock  */
2266fa9e4066Sahrens int
2267069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2268fa9e4066Sahrens {
22694b964adaSGeorge Wilson 	vdev_t *vd, *tvd;
2270fa9e4066Sahrens 
22718f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
2272fa9e4066Sahrens 
2273c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2274e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2275e14bb325SJeff Bonwick 
22763d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2277e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2278fa9e4066Sahrens 
22794b964adaSGeorge Wilson 	tvd = vd->vdev_top;
22804b964adaSGeorge Wilson 
2281069f55e2SEric Schrock 	/*
2282069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
2283069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
2284069f55e2SEric Schrock 	 * were faulted.
2285069f55e2SEric Schrock 	 */
2286069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
2287069f55e2SEric Schrock 
22883d7072f8Seschrock 	/*
22893d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
22903d7072f8Seschrock 	 */
229198d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
22923d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
22933d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
2294069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
22953d7072f8Seschrock 
22963d7072f8Seschrock 	/*
2297c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
2298c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
22993d7072f8Seschrock 	 */
23004b964adaSGeorge Wilson 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
23013d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
23023d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
23033d7072f8Seschrock 
23043d7072f8Seschrock 		/*
23053d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
23063d7072f8Seschrock 		 * mark it degraded.
23073d7072f8Seschrock 		 */
23084b964adaSGeorge Wilson 		vdev_reopen(tvd);
23093d7072f8Seschrock 
2310069f55e2SEric Schrock 		if (vdev_readable(vd))
2311069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
23123d7072f8Seschrock 	}
23133d7072f8Seschrock 
2314e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
23153d7072f8Seschrock }
23163d7072f8Seschrock 
23173d7072f8Seschrock /*
23183d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
23193d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
23203d7072f8Seschrock  * as I/O is concerned.
23213d7072f8Seschrock  */
23223d7072f8Seschrock int
2323069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
23243d7072f8Seschrock {
2325c5904d13Seschrock 	vdev_t *vd;
23260a4e9518Sgw 
23278f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
23283d7072f8Seschrock 
2329c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2330e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2331e14bb325SJeff Bonwick 
23320e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2333e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
23340e34b6a7Sbonwick 
23353d7072f8Seschrock 	/*
23363d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
23373d7072f8Seschrock 	 */
2338e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
2339e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
23403d7072f8Seschrock 
23413d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
23423d7072f8Seschrock 	if (!vdev_is_dead(vd))
23433d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2344069f55e2SEric Schrock 		    aux);
23453d7072f8Seschrock 
2346e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
23473d7072f8Seschrock }
23483d7072f8Seschrock 
23493d7072f8Seschrock /*
2350f7170741SWill Andrews  * Online the given vdev.
2351f7170741SWill Andrews  *
2352f7170741SWill Andrews  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
2353f7170741SWill Andrews  * spare device should be detached when the device finishes resilvering.
2354f7170741SWill Andrews  * Second, the online should be treated like a 'test' online case, so no FMA
2355f7170741SWill Andrews  * events are generated if the device fails to open.
23563d7072f8Seschrock  */
23573d7072f8Seschrock int
2358e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
23593d7072f8Seschrock {
2360573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
236114372834SHans Rosenfeld 	boolean_t postevent = B_FALSE;
23623d7072f8Seschrock 
23638f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
23643d7072f8Seschrock 
2365c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2366e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
23673d7072f8Seschrock 
23683d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2369e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2370fa9e4066Sahrens 
237114372834SHans Rosenfeld 	postevent =
237214372834SHans Rosenfeld 	    (vd->vdev_offline == B_TRUE || vd->vdev_tmpoffline == B_TRUE) ?
237314372834SHans Rosenfeld 	    B_TRUE : B_FALSE;
237414372834SHans Rosenfeld 
2375573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
2376fa9e4066Sahrens 	vd->vdev_offline = B_FALSE;
2377441d80aaSlling 	vd->vdev_tmpoffline = B_FALSE;
2378e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2379e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2380573ca77eSGeorge Wilson 
2381573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
2382573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2383573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2384573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2385573ca77eSGeorge Wilson 	}
2386573ca77eSGeorge Wilson 
2387573ca77eSGeorge Wilson 	vdev_reopen(tvd);
23883d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
23893d7072f8Seschrock 
2390573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2391573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2392573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
2393573ca77eSGeorge Wilson 	}
2394573ca77eSGeorge Wilson 
23953d7072f8Seschrock 	if (newstate)
23963d7072f8Seschrock 		*newstate = vd->vdev_state;
23973d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
23983d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
23993d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
24003d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
24013d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
2402fa9e4066Sahrens 
2403573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2404573ca77eSGeorge Wilson 
2405573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
2406573ca77eSGeorge Wilson 		if (vd->vdev_aux)
2407573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2408573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2409573ca77eSGeorge Wilson 	}
241014372834SHans Rosenfeld 
241114372834SHans Rosenfeld 	if (postevent)
241214372834SHans Rosenfeld 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_ONLINE);
241314372834SHans Rosenfeld 
24148ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
2415fa9e4066Sahrens }
2416fa9e4066Sahrens 
2417a1521560SJeff Bonwick static int
2418a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2419fa9e4066Sahrens {
2420e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
24218f18d1faSGeorge Wilson 	int error = 0;
24228f18d1faSGeorge Wilson 	uint64_t generation;
24238f18d1faSGeorge Wilson 	metaslab_group_t *mg;
24240a4e9518Sgw 
24258f18d1faSGeorge Wilson top:
24268f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
2427fa9e4066Sahrens 
2428c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2429e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2430fa9e4066Sahrens 
24310e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2432e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
24330e34b6a7Sbonwick 
2434e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
24358f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
24368f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
2437e6ca193dSGeorge Wilson 
2438fa9e4066Sahrens 	/*
2439ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
2440fa9e4066Sahrens 	 */
2441ecc2d604Sbonwick 	if (!vd->vdev_offline) {
2442ecc2d604Sbonwick 		/*
24438ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
2444e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
2445e6ca193dSGeorge Wilson 		 * expendable.
2446ecc2d604Sbonwick 		 */
2447e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2448e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
2449e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2450fa9e4066Sahrens 
24518f18d1faSGeorge Wilson 		/*
2452b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
2453b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
2454b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
2455b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
24568f18d1faSGeorge Wilson 		 */
24578f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
24588f18d1faSGeorge Wilson 			/*
24598f18d1faSGeorge Wilson 			 * Prevent any future allocations.
24608f18d1faSGeorge Wilson 			 */
2461a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
24628f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
24638f18d1faSGeorge Wilson 
24641195e687SMark J Musante 			error = spa_offline_log(spa);
24658f18d1faSGeorge Wilson 
24668f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
24678f18d1faSGeorge Wilson 
24688f18d1faSGeorge Wilson 			/*
24698f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
24708f18d1faSGeorge Wilson 			 */
24718f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
2472a1521560SJeff Bonwick 				metaslab_group_activate(mg);
24738f18d1faSGeorge Wilson 				if (error)
24748f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
24758f18d1faSGeorge Wilson 					    vd, error));
24768f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
24778f18d1faSGeorge Wilson 				goto top;
24788f18d1faSGeorge Wilson 			}
2479fb09f5aaSMadhav Suresh 			ASSERT0(tvd->vdev_stat.vs_alloc);
24808f18d1faSGeorge Wilson 		}
24818f18d1faSGeorge Wilson 
2482ecc2d604Sbonwick 		/*
2483ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
2484e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
2485e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
2486e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
2487ecc2d604Sbonwick 		 */
2488ecc2d604Sbonwick 		vd->vdev_offline = B_TRUE;
2489e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
2490e6ca193dSGeorge Wilson 
2491e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2492e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
2493ecc2d604Sbonwick 			vd->vdev_offline = B_FALSE;
2494e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
2495e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2496ecc2d604Sbonwick 		}
24978f18d1faSGeorge Wilson 
24988f18d1faSGeorge Wilson 		/*
24998f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
25008f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
25018f18d1faSGeorge Wilson 		 */
25028f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
2503a1521560SJeff Bonwick 			metaslab_group_activate(mg);
2504fa9e4066Sahrens 	}
2505fa9e4066Sahrens 
2506e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2507ecc2d604Sbonwick 
25088f18d1faSGeorge Wilson 	return (spa_vdev_state_exit(spa, vd, 0));
2509fa9e4066Sahrens }
2510fa9e4066Sahrens 
2511a1521560SJeff Bonwick int
2512a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2513a1521560SJeff Bonwick {
2514a1521560SJeff Bonwick 	int error;
2515a1521560SJeff Bonwick 
2516a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
2517a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
2518a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
2519a1521560SJeff Bonwick 
2520a1521560SJeff Bonwick 	return (error);
2521a1521560SJeff Bonwick }
2522a1521560SJeff Bonwick 
2523ea8dc4b6Seschrock /*
2524ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
2525ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
2526ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
2527ea8dc4b6Seschrock  */
2528ea8dc4b6Seschrock void
2529e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
2530fa9e4066Sahrens {
2531e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2532e14bb325SJeff Bonwick 
2533e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2534fa9e4066Sahrens 
2535ea8dc4b6Seschrock 	if (vd == NULL)
2536e14bb325SJeff Bonwick 		vd = rvd;
2537fa9e4066Sahrens 
2538ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
2539ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
2540ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2541fa9e4066Sahrens 
2542e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2543e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
25443d7072f8Seschrock 
25453d7072f8Seschrock 	/*
25468a79c1b5Sek 	 * If we're in the FAULTED state or have experienced failed I/O, then
25478a79c1b5Sek 	 * clear the persistent state and attempt to reopen the device.  We
25488a79c1b5Sek 	 * also mark the vdev config dirty, so that the new faulted state is
25498a79c1b5Sek 	 * written out to disk.
25503d7072f8Seschrock 	 */
2551e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
2552e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
25538a79c1b5Sek 
2554096d22d4SEric Schrock 		/*
2555096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
2556096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
2557096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
2558096d22d4SEric Schrock 		 */
2559096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
2560096d22d4SEric Schrock 
25614b964adaSGeorge Wilson 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2562e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
2563e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
2564e14bb325SJeff Bonwick 
2565f9af39baSGeorge Wilson 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
25663d7072f8Seschrock 
2567096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
2568096d22d4SEric Schrock 
2569f9af39baSGeorge Wilson 		if (vd != rvd && vdev_writeable(vd->vdev_top))
2570e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
2571e14bb325SJeff Bonwick 
2572e14bb325SJeff Bonwick 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2573bb8b5132Sek 			spa_async_request(spa, SPA_ASYNC_RESILVER);
25743d7072f8Seschrock 
25753d7072f8Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
25763d7072f8Seschrock 	}
2577096d22d4SEric Schrock 
2578096d22d4SEric Schrock 	/*
2579096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
2580096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
2581096d22d4SEric Schrock 	 * done in response to the FMA fault.
2582096d22d4SEric Schrock 	 */
2583096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2584096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2585096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
2586096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
2587fa9e4066Sahrens }
2588fa9e4066Sahrens 
2589e14bb325SJeff Bonwick boolean_t
2590e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd)
25910a4e9518Sgw {
259288ecc943SGeorge Wilson 	/*
259388ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
259488ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
259588ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
259688ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
259788ecc943SGeorge Wilson 	 * before issuing I/O to them.
259888ecc943SGeorge Wilson 	 */
259988ecc943SGeorge Wilson 	return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
260088ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
26010a4e9518Sgw }
26020a4e9518Sgw 
2603e14bb325SJeff Bonwick boolean_t
2604e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
26050a4e9518Sgw {
2606e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
26070a4e9518Sgw }
26080a4e9518Sgw 
2609e14bb325SJeff Bonwick boolean_t
2610e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
2611fa9e4066Sahrens {
2612e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2613fa9e4066Sahrens }
2614fa9e4066Sahrens 
2615a31e6787SGeorge Wilson boolean_t
2616a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
2617a31e6787SGeorge Wilson {
26188ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
26198ad4d6ddSJeff Bonwick 
2620a31e6787SGeorge Wilson 	/*
26218ad4d6ddSJeff Bonwick 	 * We currently allow allocations from vdevs which may be in the
2622a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2623a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
26248ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
26258ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
26268ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
2627a31e6787SGeorge Wilson 	 */
26288ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
26293f9d6ad7SLin Ling 	    !vd->vdev_cant_write && !vd->vdev_ishole);
2630a31e6787SGeorge Wilson }
2631a31e6787SGeorge Wilson 
2632e14bb325SJeff Bonwick boolean_t
2633e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
2634fa9e4066Sahrens {
2635e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
2636fa9e4066Sahrens 
2637e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2638e14bb325SJeff Bonwick 		return (B_FALSE);
2639fa9e4066Sahrens 
2640e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
2641e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
2642fa9e4066Sahrens 
2643e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
2644e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
2645fa9e4066Sahrens 
2646e14bb325SJeff Bonwick 	return (B_TRUE);
2647fa9e4066Sahrens }
2648fa9e4066Sahrens 
2649fa9e4066Sahrens /*
2650fa9e4066Sahrens  * Get statistics for the given vdev.
2651fa9e4066Sahrens  */
2652fa9e4066Sahrens void
2653fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2654fa9e4066Sahrens {
26552e4c9986SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
26562e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
26572e4c9986SGeorge Wilson 
26582e4c9986SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2659fa9e4066Sahrens 
2660fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2661fa9e4066Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2662fa9e4066Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2663fa9e4066Sahrens 	vs->vs_state = vd->vdev_state;
2664573ca77eSGeorge Wilson 	vs->vs_rsize = vdev_get_min_asize(vd);
2665573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
2666573ca77eSGeorge Wilson 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
26674263d13fSGeorge Wilson 	vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize;
26682986efa8SAlex Reece 	if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) {
26692e4c9986SGeorge Wilson 		vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
26702986efa8SAlex Reece 	}
2671fa9e4066Sahrens 
2672fa9e4066Sahrens 	/*
2673fa9e4066Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2674fa9e4066Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2675fa9e4066Sahrens 	 */
2676fa9e4066Sahrens 	if (vd == rvd) {
2677e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2678fa9e4066Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2679fa9e4066Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2680fa9e4066Sahrens 
2681e14bb325SJeff Bonwick 			for (int t = 0; t < ZIO_TYPES; t++) {
2682fa9e4066Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2683fa9e4066Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2684fa9e4066Sahrens 			}
26853f9d6ad7SLin Ling 			cvs->vs_scan_removing = cvd->vdev_removing;
2686fa9e4066Sahrens 		}
2687fa9e4066Sahrens 	}
26882e4c9986SGeorge Wilson 	mutex_exit(&vd->vdev_stat_lock);
2689fa9e4066Sahrens }
2690fa9e4066Sahrens 
2691fa94a07fSbrendan void
2692fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
2693fa94a07fSbrendan {
2694fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
2695fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
2696fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
2697fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
2698fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
2699fa94a07fSbrendan }
2700fa94a07fSbrendan 
27013f9d6ad7SLin Ling void
27023f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd)
27033f9d6ad7SLin Ling {
27043f9d6ad7SLin Ling 	vdev_stat_t *vs = &vd->vdev_stat;
27053f9d6ad7SLin Ling 
27063f9d6ad7SLin Ling 	for (int c = 0; c < vd->vdev_children; c++)
27073f9d6ad7SLin Ling 		vdev_scan_stat_init(vd->vdev_child[c]);
27083f9d6ad7SLin Ling 
27093f9d6ad7SLin Ling 	mutex_enter(&vd->vdev_stat_lock);
27103f9d6ad7SLin Ling 	vs->vs_scan_processed = 0;
27113f9d6ad7SLin Ling 	mutex_exit(&vd->vdev_stat_lock);
27123f9d6ad7SLin Ling }
27133f9d6ad7SLin Ling 
2714fa9e4066Sahrens void
2715e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
2716fa9e4066Sahrens {
27178ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
27188ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2719e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2720fa9e4066Sahrens 	vdev_t *pvd;
2721fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
2722fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2723fa9e4066Sahrens 	zio_type_t type = zio->io_type;
2724fa9e4066Sahrens 	int flags = zio->io_flags;
2725fa9e4066Sahrens 
2726e14bb325SJeff Bonwick 	/*
2727e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
2728e14bb325SJeff Bonwick 	 */
2729e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
2730e14bb325SJeff Bonwick 		return;
2731e14bb325SJeff Bonwick 
2732fa9e4066Sahrens 	if (zio->io_error == 0) {
2733e14bb325SJeff Bonwick 		/*
2734e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
2735e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
2736e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
2737e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
2738e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
2739e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
2740e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
2741e14bb325SJeff Bonwick 		 *
2742e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
2743e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
2744e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
2745e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
2746e14bb325SJeff Bonwick 		 */
2747e14bb325SJeff Bonwick 		if (vd == rvd)
2748e14bb325SJeff Bonwick 			return;
2749e14bb325SJeff Bonwick 
2750e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
27518ad4d6ddSJeff Bonwick 
27528ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
27538ad4d6ddSJeff Bonwick 			return;
27548ad4d6ddSJeff Bonwick 
27558ad4d6ddSJeff Bonwick 		mutex_enter(&vd->vdev_stat_lock);
27568ad4d6ddSJeff Bonwick 
2757e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
275844ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
27593f9d6ad7SLin Ling 				dsl_scan_phys_t *scn_phys =
27603f9d6ad7SLin Ling 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
27613f9d6ad7SLin Ling 				uint64_t *processed = &scn_phys->scn_processed;
27623f9d6ad7SLin Ling 
27633f9d6ad7SLin Ling 				/* XXX cleanup? */
27643f9d6ad7SLin Ling 				if (vd->vdev_ops->vdev_op_leaf)
27653f9d6ad7SLin Ling 					atomic_add_64(processed, psize);
27663f9d6ad7SLin Ling 				vs->vs_scan_processed += psize;
27673f9d6ad7SLin Ling 			}
27683f9d6ad7SLin Ling 
27698ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
2770e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
2771fa9e4066Sahrens 		}
27728ad4d6ddSJeff Bonwick 
27738ad4d6ddSJeff Bonwick 		vs->vs_ops[type]++;
27748ad4d6ddSJeff Bonwick 		vs->vs_bytes[type] += psize;
27758ad4d6ddSJeff Bonwick 
27768ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
2777fa9e4066Sahrens 		return;
2778fa9e4066Sahrens 	}
2779fa9e4066Sahrens 
2780fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2781fa9e4066Sahrens 		return;
2782fa9e4066Sahrens 
27838956713aSEric Schrock 	/*
27848956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
27858956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
27868956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
27878956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
27888956713aSEric Schrock 	 */
27898956713aSEric Schrock 	if (zio->io_error == EIO &&
27908956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
27918956713aSEric Schrock 		return;
27928956713aSEric Schrock 
27938f18d1faSGeorge Wilson 	/*
27948f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
27958f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
27968f18d1faSGeorge Wilson 	 * errors.
27978f18d1faSGeorge Wilson 	 */
27988f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
27998f18d1faSGeorge Wilson 		return;
28008f18d1faSGeorge Wilson 
2801e14bb325SJeff Bonwick 	mutex_enter(&vd->vdev_stat_lock);
2802b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2803e14bb325SJeff Bonwick 		if (zio->io_error == ECKSUM)
2804e14bb325SJeff Bonwick 			vs->vs_checksum_errors++;
2805e14bb325SJeff Bonwick 		else
2806e14bb325SJeff Bonwick 			vs->vs_read_errors++;
2807fa9e4066Sahrens 	}
2808b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2809e14bb325SJeff Bonwick 		vs->vs_write_errors++;
2810e14bb325SJeff Bonwick 	mutex_exit(&vd->vdev_stat_lock);
2811fa9e4066Sahrens 
28128ad4d6ddSJeff Bonwick 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
28138ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
281444ecc532SGeorge Wilson 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
2815b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
28168ad4d6ddSJeff Bonwick 		/*
2817b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
2818b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
2819b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
2820b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
2821b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
2822b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
2823b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
2824b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
28258ad4d6ddSJeff Bonwick 		 *
28268ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
28278ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
28288ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
28298ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
28308ad4d6ddSJeff Bonwick 		 */
28318ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
28328ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
283344ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
28348ad4d6ddSJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
28358ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
28368ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2837b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
2838b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
2839b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2840b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
28418ad4d6ddSJeff Bonwick 			}
2842b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
28438ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2844fa9e4066Sahrens 				return;
28458ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
28468ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
28478ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2848fa9e4066Sahrens 		}
28498ad4d6ddSJeff Bonwick 		if (vd != rvd)
28508ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2851fa9e4066Sahrens 	}
2852fa9e4066Sahrens }
2853fa9e4066Sahrens 
2854fa9e4066Sahrens /*
2855b24ab676SJeff Bonwick  * Update the in-core space usage stats for this vdev, its metaslab class,
2856b24ab676SJeff Bonwick  * and the root vdev.
2857fa9e4066Sahrens  */
2858fa9e4066Sahrens void
2859b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2860b24ab676SJeff Bonwick     int64_t space_delta)
2861fa9e4066Sahrens {
286299653d4eSeschrock 	int64_t dspace_delta = space_delta;
28638654d025Sperrin 	spa_t *spa = vd->vdev_spa;
28648654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
2865b24ab676SJeff Bonwick 	metaslab_group_t *mg = vd->vdev_mg;
2866b24ab676SJeff Bonwick 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2867fa9e4066Sahrens 
28688654d025Sperrin 	ASSERT(vd == vd->vdev_top);
286999653d4eSeschrock 
28708654d025Sperrin 	/*
28718654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
28728654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
28738654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
28748654d025Sperrin 	 * childrens', thus not accurate enough for us.
28758654d025Sperrin 	 */
28768654d025Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2877e6ca193dSGeorge Wilson 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
28788654d025Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
28798654d025Sperrin 	    vd->vdev_deflate_ratio;
28808654d025Sperrin 
28818654d025Sperrin 	mutex_enter(&vd->vdev_stat_lock);
28828654d025Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
2883b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
28848654d025Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
28858654d025Sperrin 	mutex_exit(&vd->vdev_stat_lock);
28868654d025Sperrin 
2887b24ab676SJeff Bonwick 	if (mc == spa_normal_class(spa)) {
2888fa94a07fSbrendan 		mutex_enter(&rvd->vdev_stat_lock);
2889fa94a07fSbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
2890b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
2891fa94a07fSbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
2892fa94a07fSbrendan 		mutex_exit(&rvd->vdev_stat_lock);
2893fa94a07fSbrendan 	}
2894b24ab676SJeff Bonwick 
2895b24ab676SJeff Bonwick 	if (mc != NULL) {
2896b24ab676SJeff Bonwick 		ASSERT(rvd == vd->vdev_parent);
2897b24ab676SJeff Bonwick 		ASSERT(vd->vdev_ms_count != 0);
2898b24ab676SJeff Bonwick 
2899b24ab676SJeff Bonwick 		metaslab_class_space_update(mc,
2900b24ab676SJeff Bonwick 		    alloc_delta, defer_delta, space_delta, dspace_delta);
2901b24ab676SJeff Bonwick 	}
2902fa9e4066Sahrens }
2903fa9e4066Sahrens 
2904fa9e4066Sahrens /*
2905fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2906fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
2907fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2908fa9e4066Sahrens  */
2909fa9e4066Sahrens void
2910fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
2911fa9e4066Sahrens {
2912fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2913fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2914fa9e4066Sahrens 	int c;
2915fa9e4066Sahrens 
2916f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
2917f9af39baSGeorge Wilson 
2918c5904d13Seschrock 	/*
29196809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
29206809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
2921c5904d13Seschrock 	 */
2922c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
2923c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
2924c5904d13Seschrock 		nvlist_t **aux;
2925c5904d13Seschrock 		uint_t naux;
2926c5904d13Seschrock 
2927c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
2928c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
2929c5904d13Seschrock 				break;
2930c5904d13Seschrock 		}
2931c5904d13Seschrock 
2932e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
2933e14bb325SJeff Bonwick 			/*
2934e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
2935e14bb325SJeff Bonwick 			 */
2936e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
2937e14bb325SJeff Bonwick 			return;
2938e14bb325SJeff Bonwick 		}
2939e14bb325SJeff Bonwick 
2940c5904d13Seschrock 		sav->sav_sync = B_TRUE;
2941c5904d13Seschrock 
29426809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
29436809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
29446809eb4eSEric Schrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
29456809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
29466809eb4eSEric Schrock 		}
2947c5904d13Seschrock 
2948c5904d13Seschrock 		ASSERT(c < naux);
2949c5904d13Seschrock 
2950c5904d13Seschrock 		/*
2951c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
2952c5904d13Seschrock 		 * sketchy, but it will work.
2953c5904d13Seschrock 		 */
2954c5904d13Seschrock 		nvlist_free(aux[c]);
29553f9d6ad7SLin Ling 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
2956c5904d13Seschrock 
2957c5904d13Seschrock 		return;
2958c5904d13Seschrock 	}
2959c5904d13Seschrock 
29605dabedeeSbonwick 	/*
2961e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
2962e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
2963e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
29645dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
29655dabedeeSbonwick 	 */
2966e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2967e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2968e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
29695dabedeeSbonwick 
2970fa9e4066Sahrens 	if (vd == rvd) {
2971fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2972fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2973fa9e4066Sahrens 	} else {
2974fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
2975fa9e4066Sahrens 
297688ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
297788ecc943SGeorge Wilson 		    !vd->vdev_ishole)
2978e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
2979fa9e4066Sahrens 	}
2980fa9e4066Sahrens }
2981fa9e4066Sahrens 
2982fa9e4066Sahrens void
2983fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
2984fa9e4066Sahrens {
29855dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
29865dabedeeSbonwick 
2987e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2988e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2989e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
29905dabedeeSbonwick 
2991e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
2992e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
2993e14bb325SJeff Bonwick }
2994e14bb325SJeff Bonwick 
2995e14bb325SJeff Bonwick /*
2996e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
2997e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
2998e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
2999e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
3000e14bb325SJeff Bonwick  */
3001e14bb325SJeff Bonwick void
3002e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
3003e14bb325SJeff Bonwick {
3004e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3005e14bb325SJeff Bonwick 
3006f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3007e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
3008e14bb325SJeff Bonwick 
3009e14bb325SJeff Bonwick 	/*
3010e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
3011e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
3012e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
3013e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
3014e14bb325SJeff Bonwick 	 */
3015e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3016e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3017e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3018e14bb325SJeff Bonwick 
3019b24ab676SJeff Bonwick 	if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
3020e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
3021e14bb325SJeff Bonwick }
3022e14bb325SJeff Bonwick 
3023e14bb325SJeff Bonwick void
3024e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
3025e14bb325SJeff Bonwick {
3026e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3027e14bb325SJeff Bonwick 
3028e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3029e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3030e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3031e14bb325SJeff Bonwick 
3032e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3033e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
3034fa9e4066Sahrens }
3035fa9e4066Sahrens 
303632b87932Sek /*
303732b87932Sek  * Propagate vdev state up from children to parent.
303832b87932Sek  */
303944cd46caSbillm void
304044cd46caSbillm vdev_propagate_state(vdev_t *vd)
304144cd46caSbillm {
30428ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
30438ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
304444cd46caSbillm 	int degraded = 0, faulted = 0;
304544cd46caSbillm 	int corrupted = 0;
304644cd46caSbillm 	vdev_t *child;
304744cd46caSbillm 
30483d7072f8Seschrock 	if (vd->vdev_children > 0) {
3049573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
30503d7072f8Seschrock 			child = vd->vdev_child[c];
305151ece835Seschrock 
305288ecc943SGeorge Wilson 			/*
305388ecc943SGeorge Wilson 			 * Don't factor holes into the decision.
305488ecc943SGeorge Wilson 			 */
305588ecc943SGeorge Wilson 			if (child->vdev_ishole)
305688ecc943SGeorge Wilson 				continue;
305788ecc943SGeorge Wilson 
3058e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
30598ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
306051ece835Seschrock 				/*
306151ece835Seschrock 				 * Root special: if there is a top-level log
306251ece835Seschrock 				 * device, treat the root vdev as if it were
306351ece835Seschrock 				 * degraded.
306451ece835Seschrock 				 */
306551ece835Seschrock 				if (child->vdev_islog && vd == rvd)
306651ece835Seschrock 					degraded++;
306751ece835Seschrock 				else
306851ece835Seschrock 					faulted++;
306951ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
30703d7072f8Seschrock 				degraded++;
307151ece835Seschrock 			}
307244cd46caSbillm 
30733d7072f8Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
30743d7072f8Seschrock 				corrupted++;
30753d7072f8Seschrock 		}
307644cd46caSbillm 
30773d7072f8Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
30783d7072f8Seschrock 
30793d7072f8Seschrock 		/*
3080e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
30813d7072f8Seschrock 		 * opened due to corrupted metadata, then propagate the root
30823d7072f8Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
30833d7072f8Seschrock 		 * replicas'.
30843d7072f8Seschrock 		 */
30853d7072f8Seschrock 		if (corrupted && vd == rvd &&
30863d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
30873d7072f8Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
30883d7072f8Seschrock 			    VDEV_AUX_CORRUPT_DATA);
30893d7072f8Seschrock 	}
30903d7072f8Seschrock 
309151ece835Seschrock 	if (vd->vdev_parent)
30923d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
309344cd46caSbillm }
309444cd46caSbillm 
3095fa9e4066Sahrens /*
3096ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
3097ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
3098ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
3099ea8dc4b6Seschrock  *
3100ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
3101ea8dc4b6Seschrock  * generated.
3102fa9e4066Sahrens  */
3103fa9e4066Sahrens void
3104ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3105fa9e4066Sahrens {
3106560e6e96Seschrock 	uint64_t save_state;
3107c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
3108ea8dc4b6Seschrock 
3109ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
3110ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
3111fa9e4066Sahrens 		return;
3112ea8dc4b6Seschrock 	}
3113ea8dc4b6Seschrock 
3114560e6e96Seschrock 	save_state = vd->vdev_state;
3115fa9e4066Sahrens 
3116fa9e4066Sahrens 	vd->vdev_state = state;
3117fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
3118fa9e4066Sahrens 
31193d7072f8Seschrock 	/*
31203d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
312198d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
312298d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
312398d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
312498d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
312598d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
312698d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
312798d1cbfeSGeorge Wilson 	 * children.
312898d1cbfeSGeorge Wilson 	 */
312998d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
313098d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
31313d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
31323d7072f8Seschrock 
3133069f55e2SEric Schrock 	/*
3134069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
3135069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
3136069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
3137069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
3138069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
3139069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
3140069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
3141069f55e2SEric Schrock 	 */
3142069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3143069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
3144069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
3145069f55e2SEric Schrock 
31463d7072f8Seschrock 	if (vd->vdev_removed &&
31473d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
31483d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
31493d7072f8Seschrock 		/*
31503d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
31513d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
31523d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
31533d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
31543d7072f8Seschrock 		 * it is one of our special test online cases, which is only
31553d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
31563d7072f8Seschrock 		 * fault.
31573d7072f8Seschrock 		 */
31583d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
31593d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
31603d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
31613d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
31623d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
3163ea8dc4b6Seschrock 		/*
3164cb04b873SMark J Musante 		 * If we fail to open a vdev during an import or recovery, we
3165cb04b873SMark J Musante 		 * mark it as "not available", which signifies that it was
3166cb04b873SMark J Musante 		 * never there to begin with.  Failure to open such a device
3167cb04b873SMark J Musante 		 * is not considered an error.
3168ea8dc4b6Seschrock 		 */
3169cb04b873SMark J Musante 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3170cb04b873SMark J Musante 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3171560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
3172560e6e96Seschrock 			vd->vdev_not_present = 1;
3173560e6e96Seschrock 
3174560e6e96Seschrock 		/*
3175560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
3176560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3177560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
3178560e6e96Seschrock 		 * want to post the ereport if the device was already in the
3179560e6e96Seschrock 		 * CANT_OPEN state beforehand.
31803d7072f8Seschrock 		 *
31813d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
31823d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
31833d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
31843d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
31853d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
31863d7072f8Seschrock 		 * about to replace the device, or attempt an online with
31873d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
3188560e6e96Seschrock 		 */
31893d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
31903d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
3191c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
3192ea8dc4b6Seschrock 			const char *class;
3193ea8dc4b6Seschrock 
3194ea8dc4b6Seschrock 			switch (aux) {
3195ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
3196ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3197ea8dc4b6Seschrock 				break;
3198ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
3199ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3200ea8dc4b6Seschrock 				break;
3201ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
3202ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3203ea8dc4b6Seschrock 				break;
3204ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
3205ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3206ea8dc4b6Seschrock 				break;
3207ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
3208ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3209ea8dc4b6Seschrock 				break;
3210ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
3211ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3212ea8dc4b6Seschrock 				break;
3213ea8dc4b6Seschrock 			default:
3214ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3215ea8dc4b6Seschrock 			}
3216ea8dc4b6Seschrock 
3217c5904d13Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3218ea8dc4b6Seschrock 		}
3219ea8dc4b6Seschrock 
32203d7072f8Seschrock 		/* Erase any notion of persistent removed state */
32213d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
32223d7072f8Seschrock 	} else {
32233d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
32243d7072f8Seschrock 	}
3225ea8dc4b6Seschrock 
32268b33d774STim Haley 	if (!isopen && vd->vdev_parent)
32278b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
3228fa9e4066Sahrens }
322915e6edf1Sgw 
323015e6edf1Sgw /*
323115e6edf1Sgw  * Check the vdev configuration to ensure that it's capable of supporting
323215e6edf1Sgw  * a root pool. Currently, we do not support RAID-Z or partial configuration.
323315e6edf1Sgw  * In addition, only a single top-level vdev is allowed and none of the leaves
323415e6edf1Sgw  * can be wholedisks.
323515e6edf1Sgw  */
323615e6edf1Sgw boolean_t
323715e6edf1Sgw vdev_is_bootable(vdev_t *vd)
323815e6edf1Sgw {
323915e6edf1Sgw 	if (!vd->vdev_ops->vdev_op_leaf) {
324015e6edf1Sgw 		char *vdev_type = vd->vdev_ops->vdev_op_type;
324115e6edf1Sgw 
324215e6edf1Sgw 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
324315e6edf1Sgw 		    vd->vdev_children > 1) {
324415e6edf1Sgw 			return (B_FALSE);
324515e6edf1Sgw 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
324615e6edf1Sgw 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
324715e6edf1Sgw 			return (B_FALSE);
324815e6edf1Sgw 		}
324915e6edf1Sgw 	}
325015e6edf1Sgw 
3251573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
325215e6edf1Sgw 		if (!vdev_is_bootable(vd->vdev_child[c]))
325315e6edf1Sgw 			return (B_FALSE);
325415e6edf1Sgw 	}
325515e6edf1Sgw 	return (B_TRUE);
325615e6edf1Sgw }
3257e6ca193dSGeorge Wilson 
325888ecc943SGeorge Wilson /*
325988ecc943SGeorge Wilson  * Load the state from the original vdev tree (ovd) which
326088ecc943SGeorge Wilson  * we've retrieved from the MOS config object. If the original
32614b964adaSGeorge Wilson  * vdev was offline or faulted then we transfer that state to the
32624b964adaSGeorge Wilson  * device in the current vdev tree (nvd).
326388ecc943SGeorge Wilson  */
3264e6ca193dSGeorge Wilson void
326588ecc943SGeorge Wilson vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3266e6ca193dSGeorge Wilson {
326788ecc943SGeorge Wilson 	spa_t *spa = nvd->vdev_spa;
3268e6ca193dSGeorge Wilson 
32694b964adaSGeorge Wilson 	ASSERT(nvd->vdev_top->vdev_islog);
327088ecc943SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
327188ecc943SGeorge Wilson 	ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3272e6ca193dSGeorge Wilson 
327388ecc943SGeorge Wilson 	for (int c = 0; c < nvd->vdev_children; c++)
327488ecc943SGeorge Wilson 		vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3275e6ca193dSGeorge Wilson 
32764b964adaSGeorge Wilson 	if (nvd->vdev_ops->vdev_op_leaf) {
3277e6ca193dSGeorge Wilson 		/*
32784b964adaSGeorge Wilson 		 * Restore the persistent vdev state
3279e6ca193dSGeorge Wilson 		 */
328088ecc943SGeorge Wilson 		nvd->vdev_offline = ovd->vdev_offline;
32814b964adaSGeorge Wilson 		nvd->vdev_faulted = ovd->vdev_faulted;
32824b964adaSGeorge Wilson 		nvd->vdev_degraded = ovd->vdev_degraded;
32834b964adaSGeorge Wilson 		nvd->vdev_removed = ovd->vdev_removed;
3284e6ca193dSGeorge Wilson 	}
3285e6ca193dSGeorge Wilson }
3286573ca77eSGeorge Wilson 
32874b964adaSGeorge Wilson /*
32884b964adaSGeorge Wilson  * Determine if a log device has valid content.  If the vdev was
32894b964adaSGeorge Wilson  * removed or faulted in the MOS config then we know that
32904b964adaSGeorge Wilson  * the content on the log device has already been written to the pool.
32914b964adaSGeorge Wilson  */
32924b964adaSGeorge Wilson boolean_t
32934b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd)
32944b964adaSGeorge Wilson {
32954b964adaSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
32964b964adaSGeorge Wilson 	    !vd->vdev_removed)
32974b964adaSGeorge Wilson 		return (B_TRUE);
32984b964adaSGeorge Wilson 
32994b964adaSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
33004b964adaSGeorge Wilson 		if (vdev_log_state_valid(vd->vdev_child[c]))
33014b964adaSGeorge Wilson 			return (B_TRUE);
33024b964adaSGeorge Wilson 
33034b964adaSGeorge Wilson 	return (B_FALSE);
33044b964adaSGeorge Wilson }
33054b964adaSGeorge Wilson 
3306573ca77eSGeorge Wilson /*
3307573ca77eSGeorge Wilson  * Expand a vdev if possible.
3308573ca77eSGeorge Wilson  */
3309573ca77eSGeorge Wilson void
3310573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
3311573ca77eSGeorge Wilson {
3312573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
3313573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3314573ca77eSGeorge Wilson 
3315573ca77eSGeorge Wilson 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3316573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
3317573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
3318573ca77eSGeorge Wilson 	}
3319573ca77eSGeorge Wilson }
33201195e687SMark J Musante 
33211195e687SMark J Musante /*
33221195e687SMark J Musante  * Split a vdev.
33231195e687SMark J Musante  */
33241195e687SMark J Musante void
33251195e687SMark J Musante vdev_split(vdev_t *vd)
33261195e687SMark J Musante {
33271195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
33281195e687SMark J Musante 
33291195e687SMark J Musante 	vdev_remove_child(pvd, vd);
33301195e687SMark J Musante 	vdev_compact_children(pvd);
33311195e687SMark J Musante 
33321195e687SMark J Musante 	cvd = pvd->vdev_child[0];
33331195e687SMark J Musante 	if (pvd->vdev_children == 1) {
33341195e687SMark J Musante 		vdev_remove_parent(cvd);
33351195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
33361195e687SMark J Musante 	}
33371195e687SMark J Musante 	vdev_propagate_state(cvd);
33381195e687SMark J Musante }
3339283b8460SGeorge.Wilson 
3340283b8460SGeorge.Wilson void
3341283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd)
3342283b8460SGeorge.Wilson {
3343283b8460SGeorge.Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
3344283b8460SGeorge.Wilson 		vdev_t *cvd = vd->vdev_child[c];
3345283b8460SGeorge.Wilson 
3346283b8460SGeorge.Wilson 		vdev_deadman(cvd);
3347283b8460SGeorge.Wilson 	}
3348283b8460SGeorge.Wilson 
3349283b8460SGeorge.Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3350283b8460SGeorge.Wilson 		vdev_queue_t *vq = &vd->vdev_queue;
3351283b8460SGeorge.Wilson 
3352283b8460SGeorge.Wilson 		mutex_enter(&vq->vq_lock);
335369962b56SMatthew Ahrens 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
3354283b8460SGeorge.Wilson 			spa_t *spa = vd->vdev_spa;
3355283b8460SGeorge.Wilson 			zio_t *fio;
3356283b8460SGeorge.Wilson 			uint64_t delta;
3357283b8460SGeorge.Wilson 
3358283b8460SGeorge.Wilson 			/*
3359283b8460SGeorge.Wilson 			 * Look at the head of all the pending queues,
3360283b8460SGeorge.Wilson 			 * if any I/O has been outstanding for longer than
3361283b8460SGeorge.Wilson 			 * the spa_deadman_synctime we panic the system.
3362283b8460SGeorge.Wilson 			 */
336369962b56SMatthew Ahrens 			fio = avl_first(&vq->vq_active_tree);
3364c55e05cbSMatthew Ahrens 			delta = gethrtime() - fio->io_timestamp;
3365c55e05cbSMatthew Ahrens 			if (delta > spa_deadman_synctime(spa)) {
3366c55e05cbSMatthew Ahrens 				zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3367c55e05cbSMatthew Ahrens 				    "delta %lluns, last io %lluns",
3368283b8460SGeorge.Wilson 				    fio->io_timestamp, delta,
3369283b8460SGeorge.Wilson 				    vq->vq_io_complete_ts);
3370283b8460SGeorge.Wilson 				fm_panic("I/O to pool '%s' appears to be "
3371283b8460SGeorge.Wilson 				    "hung.", spa_name(spa));
3372283b8460SGeorge.Wilson 			}
3373283b8460SGeorge.Wilson 		}
3374283b8460SGeorge.Wilson 		mutex_exit(&vq->vq_lock);
3375283b8460SGeorge.Wilson 	}
3376283b8460SGeorge.Wilson }
3377