xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision bf3e216c)
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.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25bf16b11eSMatthew Ahrens  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
30fa9e4066Sahrens #include <sys/spa.h>
31fa9e4066Sahrens #include <sys/spa_impl.h>
32fa9e4066Sahrens #include <sys/dmu.h>
33fa9e4066Sahrens #include <sys/dmu_tx.h>
34fa9e4066Sahrens #include <sys/vdev_impl.h>
35fa9e4066Sahrens #include <sys/uberblock_impl.h>
36fa9e4066Sahrens #include <sys/metaslab.h>
37fa9e4066Sahrens #include <sys/metaslab_impl.h>
38fa9e4066Sahrens #include <sys/space_map.h>
390713e232SGeorge Wilson #include <sys/space_reftree.h>
40fa9e4066Sahrens #include <sys/zio.h>
41fa9e4066Sahrens #include <sys/zap.h>
42fa9e4066Sahrens #include <sys/fs/zfs.h>
43c5904d13Seschrock #include <sys/arc.h>
44e6ca193dSGeorge Wilson #include <sys/zil.h>
453f9d6ad7SLin Ling #include <sys/dsl_scan.h>
46fa9e4066Sahrens 
47fa9e4066Sahrens /*
48fa9e4066Sahrens  * Virtual device management.
49fa9e4066Sahrens  */
50fa9e4066Sahrens 
51fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = {
52fa9e4066Sahrens 	&vdev_root_ops,
53fa9e4066Sahrens 	&vdev_raidz_ops,
54fa9e4066Sahrens 	&vdev_mirror_ops,
55fa9e4066Sahrens 	&vdev_replacing_ops,
5699653d4eSeschrock 	&vdev_spare_ops,
57fa9e4066Sahrens 	&vdev_disk_ops,
58fa9e4066Sahrens 	&vdev_file_ops,
59fa9e4066Sahrens 	&vdev_missing_ops,
6088ecc943SGeorge Wilson 	&vdev_hole_ops,
61fa9e4066Sahrens 	NULL
62fa9e4066Sahrens };
63fa9e4066Sahrens 
64088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
65088f3894Sahrens int zfs_scrub_limit = 10;
6605b2b3b8Smishra 
67*bf3e216cSMatthew Ahrens /*
68*bf3e216cSMatthew Ahrens  * When a vdev is added, it will be divided into approximately (but no
69*bf3e216cSMatthew Ahrens  * more than) this number of metaslabs.
70*bf3e216cSMatthew Ahrens  */
71*bf3e216cSMatthew Ahrens int metaslabs_per_vdev = 200;
72*bf3e216cSMatthew Ahrens 
73fa9e4066Sahrens /*
74fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
75fa9e4066Sahrens  */
76fa9e4066Sahrens static vdev_ops_t *
77fa9e4066Sahrens vdev_getops(const char *type)
78fa9e4066Sahrens {
79fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
80fa9e4066Sahrens 
81fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
82fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
83fa9e4066Sahrens 			break;
84fa9e4066Sahrens 
85fa9e4066Sahrens 	return (ops);
86fa9e4066Sahrens }
87fa9e4066Sahrens 
88fa9e4066Sahrens /*
89fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
90fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
91fa9e4066Sahrens  */
92fa9e4066Sahrens uint64_t
93fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
94fa9e4066Sahrens {
95ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
96fa9e4066Sahrens 	uint64_t csize;
97fa9e4066Sahrens 
98573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
99fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
100fa9e4066Sahrens 		asize = MAX(asize, csize);
101fa9e4066Sahrens 	}
102fa9e4066Sahrens 
103fa9e4066Sahrens 	return (asize);
104fa9e4066Sahrens }
105fa9e4066Sahrens 
1062a79c5feSlling /*
107573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
108573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
109573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
110573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
1112a79c5feSlling  */
1122a79c5feSlling uint64_t
113573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
1142a79c5feSlling {
115573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
1162a79c5feSlling 
117573ca77eSGeorge Wilson 	/*
1184263d13fSGeorge Wilson 	 * If our parent is NULL (inactive spare or cache) or is the root,
119573ca77eSGeorge Wilson 	 * just return our own asize.
120573ca77eSGeorge Wilson 	 */
121573ca77eSGeorge Wilson 	if (pvd == NULL)
122573ca77eSGeorge Wilson 		return (vd->vdev_asize);
1232a79c5feSlling 
1242a79c5feSlling 	/*
125573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
126573ca77eSGeorge Wilson 	 * to the nearest metaslab.
1272a79c5feSlling 	 */
128573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
129573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
1302a79c5feSlling 
131573ca77eSGeorge Wilson 	/*
132573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
133573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
134573ca77eSGeorge Wilson 	 */
135573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
136573ca77eSGeorge Wilson 		return (pvd->vdev_min_asize / pvd->vdev_children);
1372a79c5feSlling 
138573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
139573ca77eSGeorge Wilson }
1402a79c5feSlling 
141573ca77eSGeorge Wilson void
142573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
143573ca77eSGeorge Wilson {
144573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
145573ca77eSGeorge Wilson 
146573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
147573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
1482a79c5feSlling }
1492a79c5feSlling 
150fa9e4066Sahrens vdev_t *
151fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
152fa9e4066Sahrens {
153fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
154fa9e4066Sahrens 
155e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
156e05725b1Sbonwick 
157088f3894Sahrens 	if (vdev < rvd->vdev_children) {
158088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
159fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
160088f3894Sahrens 	}
161fa9e4066Sahrens 
162fa9e4066Sahrens 	return (NULL);
163fa9e4066Sahrens }
164fa9e4066Sahrens 
165fa9e4066Sahrens vdev_t *
166fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
167fa9e4066Sahrens {
168fa9e4066Sahrens 	vdev_t *mvd;
169fa9e4066Sahrens 
1700e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
171fa9e4066Sahrens 		return (vd);
172fa9e4066Sahrens 
173573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
174fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
175fa9e4066Sahrens 		    NULL)
176fa9e4066Sahrens 			return (mvd);
177fa9e4066Sahrens 
178fa9e4066Sahrens 	return (NULL);
179fa9e4066Sahrens }
180fa9e4066Sahrens 
181fa9e4066Sahrens void
182fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
183fa9e4066Sahrens {
184fa9e4066Sahrens 	size_t oldsize, newsize;
185fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
186fa9e4066Sahrens 	vdev_t **newchild;
187fa9e4066Sahrens 
188e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
189fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
190fa9e4066Sahrens 
191fa9e4066Sahrens 	cvd->vdev_parent = pvd;
192fa9e4066Sahrens 
193fa9e4066Sahrens 	if (pvd == NULL)
194fa9e4066Sahrens 		return;
195fa9e4066Sahrens 
196fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
197fa9e4066Sahrens 
198fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
199fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
200fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
201fa9e4066Sahrens 
202fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
203fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
204fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
205fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
206fa9e4066Sahrens 	}
207fa9e4066Sahrens 
208fa9e4066Sahrens 	pvd->vdev_child = newchild;
209fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
210fa9e4066Sahrens 
211fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
212fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
213fa9e4066Sahrens 
214fa9e4066Sahrens 	/*
215fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
216fa9e4066Sahrens 	 */
217fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
218fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
219fa9e4066Sahrens }
220fa9e4066Sahrens 
221fa9e4066Sahrens void
222fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
223fa9e4066Sahrens {
224fa9e4066Sahrens 	int c;
225fa9e4066Sahrens 	uint_t id = cvd->vdev_id;
226fa9e4066Sahrens 
227fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == pvd);
228fa9e4066Sahrens 
229fa9e4066Sahrens 	if (pvd == NULL)
230fa9e4066Sahrens 		return;
231fa9e4066Sahrens 
232fa9e4066Sahrens 	ASSERT(id < pvd->vdev_children);
233fa9e4066Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
234fa9e4066Sahrens 
235fa9e4066Sahrens 	pvd->vdev_child[id] = NULL;
236fa9e4066Sahrens 	cvd->vdev_parent = NULL;
237fa9e4066Sahrens 
238fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
239fa9e4066Sahrens 		if (pvd->vdev_child[c])
240fa9e4066Sahrens 			break;
241fa9e4066Sahrens 
242fa9e4066Sahrens 	if (c == pvd->vdev_children) {
243fa9e4066Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
244fa9e4066Sahrens 		pvd->vdev_child = NULL;
245fa9e4066Sahrens 		pvd->vdev_children = 0;
246fa9e4066Sahrens 	}
247fa9e4066Sahrens 
248fa9e4066Sahrens 	/*
249fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
250fa9e4066Sahrens 	 */
251fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
252fa9e4066Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
253fa9e4066Sahrens }
254fa9e4066Sahrens 
255fa9e4066Sahrens /*
256fa9e4066Sahrens  * Remove any holes in the child array.
257fa9e4066Sahrens  */
258fa9e4066Sahrens void
259fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
260fa9e4066Sahrens {
261fa9e4066Sahrens 	vdev_t **newchild, *cvd;
262fa9e4066Sahrens 	int oldc = pvd->vdev_children;
263573ca77eSGeorge Wilson 	int newc;
264fa9e4066Sahrens 
265e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
266fa9e4066Sahrens 
267573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
268fa9e4066Sahrens 		if (pvd->vdev_child[c])
269fa9e4066Sahrens 			newc++;
270fa9e4066Sahrens 
271fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
272fa9e4066Sahrens 
273573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
274fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
275fa9e4066Sahrens 			newchild[newc] = cvd;
276fa9e4066Sahrens 			cvd->vdev_id = newc++;
277fa9e4066Sahrens 		}
278fa9e4066Sahrens 	}
279fa9e4066Sahrens 
280fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
281fa9e4066Sahrens 	pvd->vdev_child = newchild;
282fa9e4066Sahrens 	pvd->vdev_children = newc;
283fa9e4066Sahrens }
284fa9e4066Sahrens 
285fa9e4066Sahrens /*
286fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
287fa9e4066Sahrens  */
28888ecc943SGeorge Wilson vdev_t *
289fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
290fa9e4066Sahrens {
291fa9e4066Sahrens 	vdev_t *vd;
292fa9e4066Sahrens 
293fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
294fa9e4066Sahrens 
2950e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
2960e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
2970e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
298e9103aaeSGarrett D'Amore 		spa->spa_load_guid = spa_generate_guid(NULL);
2990e34b6a7Sbonwick 	}
3000e34b6a7Sbonwick 
30188ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
3020e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
3030e34b6a7Sbonwick 			/*
3040e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
3050e34b6a7Sbonwick 			 * which must be unique among all pools.
3060e34b6a7Sbonwick 			 */
3071195e687SMark J Musante 			guid = spa_generate_guid(NULL);
3080e34b6a7Sbonwick 		} else {
3090e34b6a7Sbonwick 			/*
3100e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
3110e34b6a7Sbonwick 			 */
3121195e687SMark J Musante 			guid = spa_generate_guid(spa);
3130e34b6a7Sbonwick 		}
3140e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
3150e34b6a7Sbonwick 	}
3160e34b6a7Sbonwick 
317fa9e4066Sahrens 	vd->vdev_spa = spa;
318fa9e4066Sahrens 	vd->vdev_id = id;
319fa9e4066Sahrens 	vd->vdev_guid = guid;
320fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
321fa9e4066Sahrens 	vd->vdev_ops = ops;
322fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
32388ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
324fa9e4066Sahrens 
325fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
3265ad82045Snd 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
327e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
3288ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
3290713e232SGeorge Wilson 		vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
3308ad4d6ddSJeff Bonwick 		    &vd->vdev_dtl_lock);
3318ad4d6ddSJeff Bonwick 	}
332fa9e4066Sahrens 	txg_list_create(&vd->vdev_ms_list,
333fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
334fa9e4066Sahrens 	txg_list_create(&vd->vdev_dtl_list,
335fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
336fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
3373d7072f8Seschrock 	vdev_queue_init(vd);
3383d7072f8Seschrock 	vdev_cache_init(vd);
339fa9e4066Sahrens 
340fa9e4066Sahrens 	return (vd);
341fa9e4066Sahrens }
342fa9e4066Sahrens 
343fa9e4066Sahrens /*
344fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
345fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
346fa9e4066Sahrens  * different for each case.
347fa9e4066Sahrens  */
34899653d4eSeschrock int
34999653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
35099653d4eSeschrock     int alloctype)
351fa9e4066Sahrens {
352fa9e4066Sahrens 	vdev_ops_t *ops;
353fa9e4066Sahrens 	char *type;
3548654d025Sperrin 	uint64_t guid = 0, islog, nparity;
355fa9e4066Sahrens 	vdev_t *vd;
356fa9e4066Sahrens 
357e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
358fa9e4066Sahrens 
359fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
360be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
361fa9e4066Sahrens 
362fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
363be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
364fa9e4066Sahrens 
365fa9e4066Sahrens 	/*
366fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
367fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
368fa9e4066Sahrens 	 */
369fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
370fa9e4066Sahrens 		uint64_t label_id;
371fa9e4066Sahrens 
372fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
373fa9e4066Sahrens 		    label_id != id)
374be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
375fa9e4066Sahrens 
376fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
377be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
37899653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
37999653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
380be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
381fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
382fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
383be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
38421ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
38521ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
386be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
387fa9e4066Sahrens 	}
388fa9e4066Sahrens 
38999653d4eSeschrock 	/*
39099653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
39199653d4eSeschrock 	 */
39299653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
393be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
39499653d4eSeschrock 
3958654d025Sperrin 	/*
3968654d025Sperrin 	 * Determine whether we're a log vdev.
3978654d025Sperrin 	 */
3988654d025Sperrin 	islog = 0;
3998654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
400990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
401be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
402fa9e4066Sahrens 
40388ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
404be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
40588ecc943SGeorge Wilson 
40699653d4eSeschrock 	/*
4078654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
40899653d4eSeschrock 	 */
4098654d025Sperrin 	nparity = -1ULL;
41099653d4eSeschrock 	if (ops == &vdev_raidz_ops) {
41199653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4128654d025Sperrin 		    &nparity) == 0) {
413b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
414be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
41599653d4eSeschrock 			/*
416f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
417f94275ceSAdam Leventhal 			 * device.
41899653d4eSeschrock 			 */
419f94275ceSAdam Leventhal 			if (nparity > 1 &&
420f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
421be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
422f94275ceSAdam Leventhal 			if (nparity > 2 &&
423f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
424be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
42599653d4eSeschrock 		} else {
42699653d4eSeschrock 			/*
42799653d4eSeschrock 			 * We require the parity to be specified for SPAs that
42899653d4eSeschrock 			 * support multiple parity levels.
42999653d4eSeschrock 			 */
430f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
431be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
43299653d4eSeschrock 			/*
43399653d4eSeschrock 			 * Otherwise, we default to 1 parity device for RAID-Z.
43499653d4eSeschrock 			 */
4358654d025Sperrin 			nparity = 1;
43699653d4eSeschrock 		}
43799653d4eSeschrock 	} else {
4388654d025Sperrin 		nparity = 0;
43999653d4eSeschrock 	}
4408654d025Sperrin 	ASSERT(nparity != -1ULL);
4418654d025Sperrin 
4428654d025Sperrin 	vd = vdev_alloc_common(spa, id, guid, ops);
4438654d025Sperrin 
4448654d025Sperrin 	vd->vdev_islog = islog;
4458654d025Sperrin 	vd->vdev_nparity = nparity;
4468654d025Sperrin 
4478654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
4488654d025Sperrin 		vd->vdev_path = spa_strdup(vd->vdev_path);
4498654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
4508654d025Sperrin 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
4518654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4528654d025Sperrin 	    &vd->vdev_physpath) == 0)
4538654d025Sperrin 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
4546809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
4556809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
45699653d4eSeschrock 
457afefbcddSeschrock 	/*
458afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
459afefbcddSeschrock 	 * as -1.
460afefbcddSeschrock 	 */
461afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
462afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
463afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
464afefbcddSeschrock 
465ea8dc4b6Seschrock 	/*
466ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
467ea8dc4b6Seschrock 	 * was not present at the time of import.
468ea8dc4b6Seschrock 	 */
4696809eb4eSEric Schrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
4706809eb4eSEric Schrock 	    &vd->vdev_not_present);
471ea8dc4b6Seschrock 
472ecc2d604Sbonwick 	/*
473ecc2d604Sbonwick 	 * Get the alignment requirement.
474ecc2d604Sbonwick 	 */
475ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
476ecc2d604Sbonwick 
47788ecc943SGeorge Wilson 	/*
47888ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
47988ecc943SGeorge Wilson 	 */
48088ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
48188ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
48288ecc943SGeorge Wilson 
483fa9e4066Sahrens 	/*
484fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
485fa9e4066Sahrens 	 */
4861195e687SMark J Musante 	if (parent && !parent->vdev_parent &&
4871195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
488fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
489fa9e4066Sahrens 		    &vd->vdev_ms_array);
490fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
491fa9e4066Sahrens 		    &vd->vdev_ms_shift);
492fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
493fa9e4066Sahrens 		    &vd->vdev_asize);
4943f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
4953f9d6ad7SLin Ling 		    &vd->vdev_removing);
496fa9e4066Sahrens 	}
497fa9e4066Sahrens 
498cd0837ccSGeorge Wilson 	if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
499a1521560SJeff Bonwick 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
5009f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ADD ||
5011195e687SMark J Musante 		    alloctype == VDEV_ALLOC_SPLIT ||
5029f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ROOTPOOL);
503a1521560SJeff Bonwick 		vd->vdev_mg = metaslab_group_create(islog ?
504a1521560SJeff Bonwick 		    spa_log_class(spa) : spa_normal_class(spa), vd);
505a1521560SJeff Bonwick 	}
506a1521560SJeff Bonwick 
507fa9e4066Sahrens 	/*
5083d7072f8Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
509fa9e4066Sahrens 	 */
510c5904d13Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
51121ecdf64SLin Ling 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
51221ecdf64SLin Ling 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
513c5904d13Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
514c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
5150713e232SGeorge Wilson 			    &vd->vdev_dtl_object);
516c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
517c5904d13Seschrock 			    &vd->vdev_unspare);
518c5904d13Seschrock 		}
51921ecdf64SLin Ling 
52021ecdf64SLin Ling 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
52121ecdf64SLin Ling 			uint64_t spare = 0;
52221ecdf64SLin Ling 
52321ecdf64SLin Ling 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
52421ecdf64SLin Ling 			    &spare) == 0 && spare)
52521ecdf64SLin Ling 				spa_spare_add(vd);
52621ecdf64SLin Ling 		}
52721ecdf64SLin Ling 
528ecc2d604Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
529ecc2d604Sbonwick 		    &vd->vdev_offline);
530c5904d13Seschrock 
531b4952e17SGeorge Wilson 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
532b4952e17SGeorge Wilson 		    &vd->vdev_resilver_txg);
533cb04b873SMark J Musante 
5343d7072f8Seschrock 		/*
5353d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
5363d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
537069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
538069f55e2SEric Schrock 		 * remain in the faulted state.
5393d7072f8Seschrock 		 */
540b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
5413d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
5423d7072f8Seschrock 			    &vd->vdev_faulted);
5433d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
5443d7072f8Seschrock 			    &vd->vdev_degraded);
5453d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
5463d7072f8Seschrock 			    &vd->vdev_removed);
547069f55e2SEric Schrock 
548069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
549069f55e2SEric Schrock 				char *aux;
550069f55e2SEric Schrock 
551069f55e2SEric Schrock 				vd->vdev_label_aux =
552069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
553069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
554069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
555069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
556069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
557069f55e2SEric Schrock 			}
5583d7072f8Seschrock 		}
559fa9e4066Sahrens 	}
560fa9e4066Sahrens 
561fa9e4066Sahrens 	/*
562fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
563fa9e4066Sahrens 	 */
564fa9e4066Sahrens 	vdev_add_child(parent, vd);
565fa9e4066Sahrens 
56699653d4eSeschrock 	*vdp = vd;
56799653d4eSeschrock 
56899653d4eSeschrock 	return (0);
569fa9e4066Sahrens }
570fa9e4066Sahrens 
571fa9e4066Sahrens void
572fa9e4066Sahrens vdev_free(vdev_t *vd)
573fa9e4066Sahrens {
5743d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
575fa9e4066Sahrens 
576fa9e4066Sahrens 	/*
577fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
578fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
579fa9e4066Sahrens 	 */
580fa9e4066Sahrens 	vdev_close(vd);
581fa9e4066Sahrens 
582e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
583b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
584fa9e4066Sahrens 
585fa9e4066Sahrens 	/*
586fa9e4066Sahrens 	 * Free all children.
587fa9e4066Sahrens 	 */
588573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
589fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
590fa9e4066Sahrens 
591fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
592fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
593fa9e4066Sahrens 
594fa9e4066Sahrens 	/*
595fa9e4066Sahrens 	 * Discard allocation state.
596fa9e4066Sahrens 	 */
597a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
598fa9e4066Sahrens 		vdev_metaslab_fini(vd);
599a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
600a1521560SJeff Bonwick 	}
601fa9e4066Sahrens 
602fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_space);
603fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_dspace);
604fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_alloc);
605fa9e4066Sahrens 
606fa9e4066Sahrens 	/*
607fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
608fa9e4066Sahrens 	 */
609fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
610fa9e4066Sahrens 
611fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
612fa9e4066Sahrens 
6133d7072f8Seschrock 	/*
6143d7072f8Seschrock 	 * Clean up vdev structure.
6153d7072f8Seschrock 	 */
6163d7072f8Seschrock 	vdev_queue_fini(vd);
6173d7072f8Seschrock 	vdev_cache_fini(vd);
6183d7072f8Seschrock 
6193d7072f8Seschrock 	if (vd->vdev_path)
6203d7072f8Seschrock 		spa_strfree(vd->vdev_path);
6213d7072f8Seschrock 	if (vd->vdev_devid)
6223d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
6233d7072f8Seschrock 	if (vd->vdev_physpath)
6243d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
6256809eb4eSEric Schrock 	if (vd->vdev_fru)
6266809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
6273d7072f8Seschrock 
6283d7072f8Seschrock 	if (vd->vdev_isspare)
6293d7072f8Seschrock 		spa_spare_remove(vd);
630fa94a07fSbrendan 	if (vd->vdev_isl2cache)
631fa94a07fSbrendan 		spa_l2cache_remove(vd);
6323d7072f8Seschrock 
6333d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
6343d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
6358ad4d6ddSJeff Bonwick 
6363d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
6370713e232SGeorge Wilson 	space_map_close(vd->vdev_dtl_sm);
6388ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
6390713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
6400713e232SGeorge Wilson 		range_tree_destroy(vd->vdev_dtl[t]);
6418ad4d6ddSJeff Bonwick 	}
6423d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
6438ad4d6ddSJeff Bonwick 
6443d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
6453d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
646e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
6473d7072f8Seschrock 
6483d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
6493d7072f8Seschrock 		spa->spa_root_vdev = NULL;
6503d7072f8Seschrock 
6513d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
652fa9e4066Sahrens }
653fa9e4066Sahrens 
654fa9e4066Sahrens /*
655fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
656fa9e4066Sahrens  */
657fa9e4066Sahrens static void
658fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
659fa9e4066Sahrens {
660fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
661fa9e4066Sahrens 	metaslab_t *msp;
662fa9e4066Sahrens 	vdev_t *vd;
663fa9e4066Sahrens 	int t;
664fa9e4066Sahrens 
665fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
666fa9e4066Sahrens 
667fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
668fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
669fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
670fa9e4066Sahrens 
671fa9e4066Sahrens 	svd->vdev_ms_array = 0;
672fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
673fa9e4066Sahrens 	svd->vdev_ms_count = 0;
674fa9e4066Sahrens 
675cd0837ccSGeorge Wilson 	if (tvd->vdev_mg)
676cd0837ccSGeorge Wilson 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
677fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
678fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
679fa9e4066Sahrens 
680fa9e4066Sahrens 	svd->vdev_mg = NULL;
681fa9e4066Sahrens 	svd->vdev_ms = NULL;
682ecc2d604Sbonwick 
683ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
684ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
685fa9e4066Sahrens 
686fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
687fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
68899653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
689fa9e4066Sahrens 
690fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
691fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
69299653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
693fa9e4066Sahrens 
694fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
695fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
696fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
697fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
698fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
699fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
700fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
701fa9e4066Sahrens 	}
702fa9e4066Sahrens 
703e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
704fa9e4066Sahrens 		vdev_config_clean(svd);
705fa9e4066Sahrens 		vdev_config_dirty(tvd);
706fa9e4066Sahrens 	}
707fa9e4066Sahrens 
708e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
709e14bb325SJeff Bonwick 		vdev_state_clean(svd);
710e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
711e14bb325SJeff Bonwick 	}
712e14bb325SJeff Bonwick 
71399653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
71499653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
7158654d025Sperrin 
7168654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
7178654d025Sperrin 	svd->vdev_islog = 0;
718fa9e4066Sahrens }
719fa9e4066Sahrens 
720fa9e4066Sahrens static void
721fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
722fa9e4066Sahrens {
723fa9e4066Sahrens 	if (vd == NULL)
724fa9e4066Sahrens 		return;
725fa9e4066Sahrens 
726fa9e4066Sahrens 	vd->vdev_top = tvd;
727fa9e4066Sahrens 
728573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
729fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
730fa9e4066Sahrens }
731fa9e4066Sahrens 
732fa9e4066Sahrens /*
733fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
734fa9e4066Sahrens  */
735fa9e4066Sahrens vdev_t *
736fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
737fa9e4066Sahrens {
738fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
739fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
740fa9e4066Sahrens 	vdev_t *mvd;
741fa9e4066Sahrens 
742e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
743fa9e4066Sahrens 
744fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
745ecc2d604Sbonwick 
746ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
747573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
7484263d13fSGeorge Wilson 	mvd->vdev_max_asize = cvd->vdev_max_asize;
749ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
750ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
75188ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
752ecc2d604Sbonwick 
753fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
754fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
755fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
756fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
757fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
758fa9e4066Sahrens 
759fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
760fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
761fa9e4066Sahrens 
762fa9e4066Sahrens 	return (mvd);
763fa9e4066Sahrens }
764fa9e4066Sahrens 
765fa9e4066Sahrens /*
766fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
767fa9e4066Sahrens  */
768fa9e4066Sahrens void
769fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
770fa9e4066Sahrens {
771fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
772fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
773fa9e4066Sahrens 
774e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
775fa9e4066Sahrens 
776fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
777fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
77899653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
77999653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
780ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
781fa9e4066Sahrens 
782fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
783fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
7848ad4d6ddSJeff Bonwick 
78599653d4eSeschrock 	/*
786e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
787e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
788e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
789e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
79099653d4eSeschrock 	 */
7918ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
7928ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
7931195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
7948ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
7958ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
7968ad4d6ddSJeff Bonwick 	}
797e14bb325SJeff Bonwick 	cvd->vdev_id = mvd->vdev_id;
798e14bb325SJeff Bonwick 	vdev_add_child(pvd, cvd);
799fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
800fa9e4066Sahrens 
801fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
802fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
803fa9e4066Sahrens 
804fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
805fa9e4066Sahrens 	vdev_free(mvd);
806fa9e4066Sahrens }
807fa9e4066Sahrens 
808ea8dc4b6Seschrock int
809fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
810fa9e4066Sahrens {
811fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
812ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
813ecc2d604Sbonwick 	uint64_t m;
814fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
815fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
816ecc2d604Sbonwick 	metaslab_t **mspp;
817ecc2d604Sbonwick 	int error;
818fa9e4066Sahrens 
819a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
820a1521560SJeff Bonwick 
82188ecc943SGeorge Wilson 	/*
82288ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
82388ecc943SGeorge Wilson 	 */
82488ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
8250e34b6a7Sbonwick 		return (0);
8260e34b6a7Sbonwick 
82788ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
82888ecc943SGeorge Wilson 
829e6ca193dSGeorge Wilson 	/*
830e6ca193dSGeorge Wilson 	 * Compute the raidz-deflation ratio.  Note, we hard-code
831e6ca193dSGeorge Wilson 	 * in 128k (1 << 17) because it is the current "typical" blocksize.
832e6ca193dSGeorge Wilson 	 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
833e6ca193dSGeorge Wilson 	 * or we will inconsistently account for existing bp's.
834e6ca193dSGeorge Wilson 	 */
835e6ca193dSGeorge Wilson 	vd->vdev_deflate_ratio = (1 << 17) /
836e6ca193dSGeorge Wilson 	    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
837e6ca193dSGeorge Wilson 
838fa9e4066Sahrens 	ASSERT(oldc <= newc);
839fa9e4066Sahrens 
840ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
841fa9e4066Sahrens 
842ecc2d604Sbonwick 	if (oldc != 0) {
843ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
844ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
845ecc2d604Sbonwick 	}
846fa9e4066Sahrens 
847ecc2d604Sbonwick 	vd->vdev_ms = mspp;
848ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
849fa9e4066Sahrens 
850ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
8510713e232SGeorge Wilson 		uint64_t object = 0;
8520713e232SGeorge Wilson 
853ecc2d604Sbonwick 		if (txg == 0) {
854ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
8557bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
8567bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
857ecc2d604Sbonwick 			if (error)
858ecc2d604Sbonwick 				return (error);
859fa9e4066Sahrens 		}
8600713e232SGeorge Wilson 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, m, object, txg);
861fa9e4066Sahrens 	}
862fa9e4066Sahrens 
863a1521560SJeff Bonwick 	if (txg == 0)
864a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
865a1521560SJeff Bonwick 
8663f9d6ad7SLin Ling 	/*
8673f9d6ad7SLin Ling 	 * If the vdev is being removed we don't activate
8683f9d6ad7SLin Ling 	 * the metaslabs since we want to ensure that no new
8693f9d6ad7SLin Ling 	 * allocations are performed on this device.
8703f9d6ad7SLin Ling 	 */
8713f9d6ad7SLin Ling 	if (oldc == 0 && !vd->vdev_removing)
872a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
873a1521560SJeff Bonwick 
874a1521560SJeff Bonwick 	if (txg == 0)
875a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
876a1521560SJeff Bonwick 
877ea8dc4b6Seschrock 	return (0);
878fa9e4066Sahrens }
879fa9e4066Sahrens 
880fa9e4066Sahrens void
881fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
882fa9e4066Sahrens {
883fa9e4066Sahrens 	uint64_t m;
884fa9e4066Sahrens 	uint64_t count = vd->vdev_ms_count;
885fa9e4066Sahrens 
886fa9e4066Sahrens 	if (vd->vdev_ms != NULL) {
887a1521560SJeff Bonwick 		metaslab_group_passivate(vd->vdev_mg);
8880713e232SGeorge Wilson 		for (m = 0; m < count; m++) {
8890713e232SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
8900713e232SGeorge Wilson 
8910713e232SGeorge Wilson 			if (msp != NULL)
8920713e232SGeorge Wilson 				metaslab_fini(msp);
8930713e232SGeorge Wilson 		}
894fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
895fa9e4066Sahrens 		vd->vdev_ms = NULL;
896fa9e4066Sahrens 	}
897fa9e4066Sahrens }
898fa9e4066Sahrens 
899e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
900e14bb325SJeff Bonwick 	boolean_t	vps_readable;
901e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
902e14bb325SJeff Bonwick 	int		vps_flags;
903e14bb325SJeff Bonwick } vdev_probe_stats_t;
904e14bb325SJeff Bonwick 
905e14bb325SJeff Bonwick static void
906e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
9070a4e9518Sgw {
9088ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
909a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
910e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
911a3f829aeSBill Moore 
912a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
913e14bb325SJeff Bonwick 
914e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
915e14bb325SJeff Bonwick 		if (zio->io_error == 0)
916e14bb325SJeff Bonwick 			vps->vps_readable = 1;
9178ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
918a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
919e14bb325SJeff Bonwick 			    zio->io_offset, zio->io_size, zio->io_data,
920e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
921e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
922e14bb325SJeff Bonwick 		} else {
923e14bb325SJeff Bonwick 			zio_buf_free(zio->io_data, zio->io_size);
924e14bb325SJeff Bonwick 		}
925e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
926e14bb325SJeff Bonwick 		if (zio->io_error == 0)
927e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
928e14bb325SJeff Bonwick 		zio_buf_free(zio->io_data, zio->io_size);
929e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
930a3f829aeSBill Moore 		zio_t *pio;
931e14bb325SJeff Bonwick 
932e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
933e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
934e14bb325SJeff Bonwick 
935e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
9368ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
937e14bb325SJeff Bonwick 			zio->io_error = 0;
938e14bb325SJeff Bonwick 		} else {
939e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
940e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
9418ad4d6ddSJeff Bonwick 			    spa, vd, NULL, 0, 0);
942be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
943e14bb325SJeff Bonwick 		}
944a3f829aeSBill Moore 
945a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
946a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
947a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
948a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
949a3f829aeSBill Moore 
950a3f829aeSBill Moore 		while ((pio = zio_walk_parents(zio)) != NULL)
951a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
952be6fd75aSMatthew Ahrens 				pio->io_error = SET_ERROR(ENXIO);
953a3f829aeSBill Moore 
954e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
955e14bb325SJeff Bonwick 	}
956e14bb325SJeff Bonwick }
9570a4e9518Sgw 
958e14bb325SJeff Bonwick /*
959f7170741SWill Andrews  * Determine whether this device is accessible.
960f7170741SWill Andrews  *
961f7170741SWill Andrews  * Read and write to several known locations: the pad regions of each
962f7170741SWill Andrews  * vdev label but the first, which we leave alone in case it contains
963f7170741SWill Andrews  * a VTOC.
964e14bb325SJeff Bonwick  */
965e14bb325SJeff Bonwick zio_t *
966a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
967e14bb325SJeff Bonwick {
968e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
969a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
970a3f829aeSBill Moore 	zio_t *pio;
971a3f829aeSBill Moore 
972a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
9730a4e9518Sgw 
974a3f829aeSBill Moore 	/*
975a3f829aeSBill Moore 	 * Don't probe the probe.
976a3f829aeSBill Moore 	 */
977a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
978a3f829aeSBill Moore 		return (NULL);
979e14bb325SJeff Bonwick 
980a3f829aeSBill Moore 	/*
981a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
982a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
983a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
984a3f829aeSBill Moore 	 */
985a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
986e14bb325SJeff Bonwick 
987a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
988a3f829aeSBill Moore 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
989a3f829aeSBill Moore 
990a3f829aeSBill Moore 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
991a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
9928956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
993a3f829aeSBill Moore 
994a3f829aeSBill Moore 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
995a3f829aeSBill Moore 			/*
996a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
997a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
998a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
999a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
1000a3f829aeSBill Moore 			 * any zio looking at these values can assume that
1001a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
1002a3f829aeSBill Moore 			 * important because when a device has intermittent
1003a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
1004a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
1005a3f829aeSBill Moore 			 * the zio (EIO).
1006a3f829aeSBill Moore 			 *
1007a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
1008a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
1009a3f829aeSBill Moore 			 * principles.
1010a3f829aeSBill Moore 			 */
1011a3f829aeSBill Moore 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1012a3f829aeSBill Moore 			vd->vdev_cant_read = B_FALSE;
1013a3f829aeSBill Moore 			vd->vdev_cant_write = B_FALSE;
1014a3f829aeSBill Moore 		}
1015a3f829aeSBill Moore 
1016a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1017a3f829aeSBill Moore 		    vdev_probe_done, vps,
1018a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1019a3f829aeSBill Moore 
102098d1cbfeSGeorge Wilson 		/*
102198d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
102298d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
102398d1cbfeSGeorge Wilson 		 */
1024a3f829aeSBill Moore 		if (zio != NULL) {
1025a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1026a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1027a3f829aeSBill Moore 		}
1028e14bb325SJeff Bonwick 	}
1029e14bb325SJeff Bonwick 
1030a3f829aeSBill Moore 	if (zio != NULL)
1031a3f829aeSBill Moore 		zio_add_child(zio, pio);
1032e14bb325SJeff Bonwick 
1033a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1034e14bb325SJeff Bonwick 
1035a3f829aeSBill Moore 	if (vps == NULL) {
1036a3f829aeSBill Moore 		ASSERT(zio != NULL);
1037a3f829aeSBill Moore 		return (NULL);
1038a3f829aeSBill Moore 	}
1039e14bb325SJeff Bonwick 
1040e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1041a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1042e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1043f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2)),
1044f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1045e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1046e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1047e14bb325SJeff Bonwick 	}
1048e14bb325SJeff Bonwick 
1049a3f829aeSBill Moore 	if (zio == NULL)
1050a3f829aeSBill Moore 		return (pio);
1051a3f829aeSBill Moore 
1052a3f829aeSBill Moore 	zio_nowait(pio);
1053a3f829aeSBill Moore 	return (NULL);
10540a4e9518Sgw }
10550a4e9518Sgw 
1056f64c0e34SEric Taylor static void
1057f64c0e34SEric Taylor vdev_open_child(void *arg)
1058f64c0e34SEric Taylor {
1059f64c0e34SEric Taylor 	vdev_t *vd = arg;
1060f64c0e34SEric Taylor 
1061f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1062f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1063f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1064f64c0e34SEric Taylor }
1065f64c0e34SEric Taylor 
1066681d9761SEric Taylor boolean_t
1067681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1068681d9761SEric Taylor {
1069681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1070681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1071681d9761SEric Taylor 		return (B_TRUE);
1072681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1073681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1074681d9761SEric Taylor 			return (B_TRUE);
1075681d9761SEric Taylor 	return (B_FALSE);
1076681d9761SEric Taylor }
1077681d9761SEric Taylor 
1078f64c0e34SEric Taylor void
1079f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1080f64c0e34SEric Taylor {
1081f64c0e34SEric Taylor 	taskq_t *tq;
1082f64c0e34SEric Taylor 	int children = vd->vdev_children;
1083f64c0e34SEric Taylor 
1084681d9761SEric Taylor 	/*
1085681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1086681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1087681d9761SEric Taylor 	 * spa_namespace_lock
1088681d9761SEric Taylor 	 */
1089681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
1090681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1091681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1092681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
1093681d9761SEric Taylor 		return;
1094681d9761SEric Taylor 	}
1095f64c0e34SEric Taylor 	tq = taskq_create("vdev_open", children, minclsyspri,
1096f64c0e34SEric Taylor 	    children, children, TASKQ_PREPOPULATE);
1097f64c0e34SEric Taylor 
1098f64c0e34SEric Taylor 	for (int c = 0; c < children; c++)
1099f64c0e34SEric Taylor 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1100f64c0e34SEric Taylor 		    TQ_SLEEP) != NULL);
1101f64c0e34SEric Taylor 
1102f64c0e34SEric Taylor 	taskq_destroy(tq);
1103f64c0e34SEric Taylor }
1104f64c0e34SEric Taylor 
1105fa9e4066Sahrens /*
1106fa9e4066Sahrens  * Prepare a virtual device for access.
1107fa9e4066Sahrens  */
1108fa9e4066Sahrens int
1109fa9e4066Sahrens vdev_open(vdev_t *vd)
1110fa9e4066Sahrens {
11118ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1112fa9e4066Sahrens 	int error;
1113fa9e4066Sahrens 	uint64_t osize = 0;
11144263d13fSGeorge Wilson 	uint64_t max_osize = 0;
11154263d13fSGeorge Wilson 	uint64_t asize, max_asize, psize;
1116ecc2d604Sbonwick 	uint64_t ashift = 0;
1117fa9e4066Sahrens 
1118f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1119f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1120fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1121fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1122fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1123fa9e4066Sahrens 
1124fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1125e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1126e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1127573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1128fa9e4066Sahrens 
1129069f55e2SEric Schrock 	/*
1130069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1131069f55e2SEric Schrock 	 * faulted, bail out of the open.
1132069f55e2SEric Schrock 	 */
11333d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
11343d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1135069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1136069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
11373d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1138069f55e2SEric Schrock 		    vd->vdev_label_aux);
1139be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
11403d7072f8Seschrock 	} else if (vd->vdev_offline) {
1141fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1142ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1143be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1144fa9e4066Sahrens 	}
1145fa9e4066Sahrens 
11464263d13fSGeorge Wilson 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1147fa9e4066Sahrens 
1148095bcd66SGeorge Wilson 	/*
1149095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1150095bcd66SGeorge Wilson 	 * the vdev on error.
1151095bcd66SGeorge Wilson 	 */
1152095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1153ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
11548956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1155ea8dc4b6Seschrock 
1156fa9e4066Sahrens 	if (error) {
11573d7072f8Seschrock 		if (vd->vdev_removed &&
11583d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
11593d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
11603d7072f8Seschrock 
1161ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1162fa9e4066Sahrens 		    vd->vdev_stat.vs_aux);
1163fa9e4066Sahrens 		return (error);
1164fa9e4066Sahrens 	}
1165fa9e4066Sahrens 
11663d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
11673d7072f8Seschrock 
1168096d22d4SEric Schrock 	/*
1169096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1170096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1171096d22d4SEric Schrock 	 */
1172096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1173096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1174096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1175096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1176096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1177096d22d4SEric Schrock 		    vd->vdev_label_aux);
1178be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1179096d22d4SEric Schrock 	}
1180096d22d4SEric Schrock 
11813d7072f8Seschrock 	if (vd->vdev_degraded) {
11823d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
11833d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
11843d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
11853d7072f8Seschrock 	} else {
1186069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
11873d7072f8Seschrock 	}
1188fa9e4066Sahrens 
118988ecc943SGeorge Wilson 	/*
119088ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
119188ecc943SGeorge Wilson 	 */
119288ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
119388ecc943SGeorge Wilson 		return (0);
119488ecc943SGeorge Wilson 
1195573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1196ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1197ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1198ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1199ea8dc4b6Seschrock 			break;
1200ea8dc4b6Seschrock 		}
1201573ca77eSGeorge Wilson 	}
1202fa9e4066Sahrens 
1203fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
12044263d13fSGeorge Wilson 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1205fa9e4066Sahrens 
1206fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1207fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1208ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1209ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1210be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1211fa9e4066Sahrens 		}
1212fa9e4066Sahrens 		psize = osize;
1213fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
12144263d13fSGeorge Wilson 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
12154263d13fSGeorge Wilson 		    VDEV_LABEL_END_SIZE);
1216fa9e4066Sahrens 	} else {
1217ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1218fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1219ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1220ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1221be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1222fa9e4066Sahrens 		}
1223fa9e4066Sahrens 		psize = 0;
1224fa9e4066Sahrens 		asize = osize;
12254263d13fSGeorge Wilson 		max_asize = max_osize;
1226fa9e4066Sahrens 	}
1227fa9e4066Sahrens 
1228fa9e4066Sahrens 	vd->vdev_psize = psize;
1229fa9e4066Sahrens 
1230573ca77eSGeorge Wilson 	/*
1231573ca77eSGeorge Wilson 	 * Make sure the allocatable size hasn't shrunk.
1232573ca77eSGeorge Wilson 	 */
1233573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1234573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1235573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1236be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1237573ca77eSGeorge Wilson 	}
1238573ca77eSGeorge Wilson 
1239fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1240fa9e4066Sahrens 		/*
1241fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
1242ecc2d604Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1243fa9e4066Sahrens 		 */
1244fa9e4066Sahrens 		vd->vdev_asize = asize;
12454263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1246ecc2d604Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1247fa9e4066Sahrens 	} else {
1248fa9e4066Sahrens 		/*
12492384d9f8SGeorge Wilson 		 * Detect if the alignment requirement has increased.
12502384d9f8SGeorge Wilson 		 * We don't want to make the pool unavailable, just
12512384d9f8SGeorge Wilson 		 * issue a warning instead.
1252fa9e4066Sahrens 		 */
12532384d9f8SGeorge Wilson 		if (ashift > vd->vdev_top->vdev_ashift &&
12542384d9f8SGeorge Wilson 		    vd->vdev_ops->vdev_op_leaf) {
12552384d9f8SGeorge Wilson 			cmn_err(CE_WARN,
12562384d9f8SGeorge Wilson 			    "Disk, '%s', has a block alignment that is "
12572384d9f8SGeorge Wilson 			    "larger than the pool's alignment\n",
12582384d9f8SGeorge Wilson 			    vd->vdev_path);
1259fa9e4066Sahrens 		}
12604263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1261573ca77eSGeorge Wilson 	}
1262fa9e4066Sahrens 
1263573ca77eSGeorge Wilson 	/*
1264573ca77eSGeorge Wilson 	 * If all children are healthy and the asize has increased,
1265573ca77eSGeorge Wilson 	 * then we've experienced dynamic LUN growth.  If automatic
1266573ca77eSGeorge Wilson 	 * expansion is enabled then use the additional space.
1267573ca77eSGeorge Wilson 	 */
1268573ca77eSGeorge Wilson 	if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1269573ca77eSGeorge Wilson 	    (vd->vdev_expanding || spa->spa_autoexpand))
1270573ca77eSGeorge Wilson 		vd->vdev_asize = asize;
1271fa9e4066Sahrens 
1272573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1273fa9e4066Sahrens 
12740a4e9518Sgw 	/*
12750a4e9518Sgw 	 * Ensure we can issue some IO before declaring the
12760a4e9518Sgw 	 * vdev open for business.
12770a4e9518Sgw 	 */
1278e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1279e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
128098d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
128198d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
12820a4e9518Sgw 		return (error);
12830a4e9518Sgw 	}
12840a4e9518Sgw 
1285088f3894Sahrens 	/*
1286088f3894Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
12878ad4d6ddSJeff Bonwick 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
12888ad4d6ddSJeff Bonwick 	 * since this would just restart the scrub we are already doing.
1289088f3894Sahrens 	 */
12908ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
12918ad4d6ddSJeff Bonwick 	    vdev_resilver_needed(vd, NULL, NULL))
12928ad4d6ddSJeff Bonwick 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1293088f3894Sahrens 
1294fa9e4066Sahrens 	return (0);
1295fa9e4066Sahrens }
1296fa9e4066Sahrens 
1297560e6e96Seschrock /*
1298560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
1299560e6e96Seschrock  * contents.  This needs to be done before vdev_load() so that we don't
13003d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1301560e6e96Seschrock  *
1302d7f601efSGeorge Wilson  * If 'strict' is false ignore the spa guid check. This is necessary because
1303d7f601efSGeorge Wilson  * if the machine crashed during a re-guid the new guid might have been written
1304d7f601efSGeorge Wilson  * to all of the vdev labels, but not the cached config. The strict check
1305d7f601efSGeorge Wilson  * will be performed when the pool is opened again using the mos config.
1306d7f601efSGeorge Wilson  *
1307560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1308560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1309560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1310560e6e96Seschrock  * will be updated but the function will return 0.
1311560e6e96Seschrock  */
1312560e6e96Seschrock int
1313d7f601efSGeorge Wilson vdev_validate(vdev_t *vd, boolean_t strict)
1314560e6e96Seschrock {
1315560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1316560e6e96Seschrock 	nvlist_t *label;
13171195e687SMark J Musante 	uint64_t guid = 0, top_guid;
1318560e6e96Seschrock 	uint64_t state;
1319560e6e96Seschrock 
1320573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1321d7f601efSGeorge Wilson 		if (vdev_validate(vd->vdev_child[c], strict) != 0)
1322be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1323560e6e96Seschrock 
1324b5989ec7Seschrock 	/*
1325b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1326b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1327b5989ec7Seschrock 	 * overwrite the previous state.
1328b5989ec7Seschrock 	 */
1329e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
13301195e687SMark J Musante 		uint64_t aux_guid = 0;
13311195e687SMark J Musante 		nvlist_t *nvl;
1332bda88194SGeorge Wilson 		uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1333bda88194SGeorge Wilson 		    spa_last_synced_txg(spa) : -1ULL;
1334560e6e96Seschrock 
1335dfbb9432SGeorge Wilson 		if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1336560e6e96Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1337560e6e96Seschrock 			    VDEV_AUX_BAD_LABEL);
1338560e6e96Seschrock 			return (0);
1339560e6e96Seschrock 		}
1340560e6e96Seschrock 
13411195e687SMark J Musante 		/*
13421195e687SMark J Musante 		 * Determine if this vdev has been split off into another
13431195e687SMark J Musante 		 * pool.  If so, then refuse to open it.
13441195e687SMark J Musante 		 */
13451195e687SMark J Musante 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
13461195e687SMark J Musante 		    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
13471195e687SMark J Musante 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13481195e687SMark J Musante 			    VDEV_AUX_SPLIT_POOL);
13491195e687SMark J Musante 			nvlist_free(label);
13501195e687SMark J Musante 			return (0);
13511195e687SMark J Musante 		}
13521195e687SMark J Musante 
1353d7f601efSGeorge Wilson 		if (strict && (nvlist_lookup_uint64(label,
1354d7f601efSGeorge Wilson 		    ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1355d7f601efSGeorge Wilson 		    guid != spa_guid(spa))) {
1356560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1357560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1358560e6e96Seschrock 			nvlist_free(label);
1359560e6e96Seschrock 			return (0);
1360560e6e96Seschrock 		}
1361560e6e96Seschrock 
13621195e687SMark J Musante 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
13631195e687SMark J Musante 		    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
13641195e687SMark J Musante 		    &aux_guid) != 0)
13651195e687SMark J Musante 			aux_guid = 0;
13661195e687SMark J Musante 
1367e14bb325SJeff Bonwick 		/*
1368e14bb325SJeff Bonwick 		 * If this vdev just became a top-level vdev because its
1369e14bb325SJeff Bonwick 		 * sibling was detached, it will have adopted the parent's
1370e14bb325SJeff Bonwick 		 * vdev guid -- but the label may or may not be on disk yet.
1371e14bb325SJeff Bonwick 		 * Fortunately, either version of the label will have the
1372e14bb325SJeff Bonwick 		 * same top guid, so if we're a top-level vdev, we can
1373e14bb325SJeff Bonwick 		 * safely compare to that instead.
13741195e687SMark J Musante 		 *
13751195e687SMark J Musante 		 * If we split this vdev off instead, then we also check the
13761195e687SMark J Musante 		 * original pool's guid.  We don't want to consider the vdev
13771195e687SMark J Musante 		 * corrupt if it is partway through a split operation.
1378e14bb325SJeff Bonwick 		 */
1379560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1380e14bb325SJeff Bonwick 		    &guid) != 0 ||
1381e14bb325SJeff Bonwick 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1382e14bb325SJeff Bonwick 		    &top_guid) != 0 ||
13831195e687SMark J Musante 		    ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1384e14bb325SJeff Bonwick 		    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1385560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1386560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1387560e6e96Seschrock 			nvlist_free(label);
1388560e6e96Seschrock 			return (0);
1389560e6e96Seschrock 		}
1390560e6e96Seschrock 
1391560e6e96Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1392560e6e96Seschrock 		    &state) != 0) {
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 
1399560e6e96Seschrock 		nvlist_free(label);
1400560e6e96Seschrock 
1401bc758434SLin Ling 		/*
14024b964adaSGeorge Wilson 		 * If this is a verbatim import, no need to check the
1403bc758434SLin Ling 		 * state of the pool.
1404bc758434SLin Ling 		 */
14054b964adaSGeorge Wilson 		if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1406b16da2e2SGeorge Wilson 		    spa_load_state(spa) == SPA_LOAD_OPEN &&
1407bc758434SLin Ling 		    state != POOL_STATE_ACTIVE)
1408be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1409560e6e96Seschrock 
141051ece835Seschrock 		/*
141151ece835Seschrock 		 * If we were able to open and validate a vdev that was
141251ece835Seschrock 		 * previously marked permanently unavailable, clear that state
141351ece835Seschrock 		 * now.
141451ece835Seschrock 		 */
141551ece835Seschrock 		if (vd->vdev_not_present)
141651ece835Seschrock 			vd->vdev_not_present = 0;
141751ece835Seschrock 	}
1418560e6e96Seschrock 
1419560e6e96Seschrock 	return (0);
1420560e6e96Seschrock }
1421560e6e96Seschrock 
1422fa9e4066Sahrens /*
1423fa9e4066Sahrens  * Close a virtual device.
1424fa9e4066Sahrens  */
1425fa9e4066Sahrens void
1426fa9e4066Sahrens vdev_close(vdev_t *vd)
1427fa9e4066Sahrens {
14288ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1429095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
14308ad4d6ddSJeff Bonwick 
14318ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
14328ad4d6ddSJeff Bonwick 
14331195e687SMark J Musante 	/*
14341195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
14351195e687SMark J Musante 	 * going offline.
14361195e687SMark J Musante 	 */
1437095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
14381195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1439095bcd66SGeorge Wilson 
1440fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1441fa9e4066Sahrens 
14423d7072f8Seschrock 	vdev_cache_purge(vd);
1443fa9e4066Sahrens 
1444560e6e96Seschrock 	/*
1445573ca77eSGeorge Wilson 	 * We record the previous state before we close it, so that if we are
1446560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1447560e6e96Seschrock 	 * it's still faulted.
1448560e6e96Seschrock 	 */
1449560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
1450560e6e96Seschrock 
1451fa9e4066Sahrens 	if (vd->vdev_offline)
1452fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1453fa9e4066Sahrens 	else
1454fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
1455ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1456fa9e4066Sahrens }
1457fa9e4066Sahrens 
1458dcba9f3fSGeorge Wilson void
1459dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
1460dcba9f3fSGeorge Wilson {
1461dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1462dcba9f3fSGeorge Wilson 
1463dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1464dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1465dcba9f3fSGeorge Wilson 		return;
1466dcba9f3fSGeorge Wilson 
1467dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1468dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
1469dcba9f3fSGeorge Wilson 
1470dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1471dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
1472dcba9f3fSGeorge Wilson }
1473dcba9f3fSGeorge Wilson 
1474dcba9f3fSGeorge Wilson void
1475dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
1476dcba9f3fSGeorge Wilson {
1477dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1478dcba9f3fSGeorge Wilson 
1479dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1480dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1481dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
1482dcba9f3fSGeorge Wilson 
1483dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1484dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
1485dcba9f3fSGeorge Wilson }
1486dcba9f3fSGeorge Wilson 
1487095bcd66SGeorge Wilson /*
1488095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1489095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
1490095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1491095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
1492095bcd66SGeorge Wilson  */
1493fa9e4066Sahrens void
1494ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
1495fa9e4066Sahrens {
1496ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
1497fa9e4066Sahrens 
1498e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1499ea8dc4b6Seschrock 
15001195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
15011195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
1502fa9e4066Sahrens 	vdev_close(vd);
1503fa9e4066Sahrens 	(void) vdev_open(vd);
1504fa9e4066Sahrens 
150539c23413Seschrock 	/*
150639c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
150739c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
150839c23413Seschrock 	 * opened in response to vdev_reopen().
150939c23413Seschrock 	 */
1510c5904d13Seschrock 	if (vd->vdev_aux) {
1511c5904d13Seschrock 		(void) vdev_validate_aux(vd);
1512e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
15136809eb4eSEric Schrock 		    vd->vdev_aux == &spa->spa_l2cache &&
1514573ca77eSGeorge Wilson 		    !l2arc_vdev_present(vd))
1515573ca77eSGeorge Wilson 			l2arc_add_vdev(spa, vd);
1516c5904d13Seschrock 	} else {
1517bda88194SGeorge Wilson 		(void) vdev_validate(vd, B_TRUE);
1518c5904d13Seschrock 	}
151939c23413Seschrock 
1520fa9e4066Sahrens 	/*
15213d7072f8Seschrock 	 * Reassess parent vdev's health.
1522fa9e4066Sahrens 	 */
15233d7072f8Seschrock 	vdev_propagate_state(vd);
1524fa9e4066Sahrens }
1525fa9e4066Sahrens 
1526fa9e4066Sahrens int
152799653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1528fa9e4066Sahrens {
1529fa9e4066Sahrens 	int error;
1530fa9e4066Sahrens 
1531fa9e4066Sahrens 	/*
1532fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1533fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
1534fa9e4066Sahrens 	 * there are any components we can't open.
1535fa9e4066Sahrens 	 */
1536fa9e4066Sahrens 	error = vdev_open(vd);
1537fa9e4066Sahrens 
1538fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1539fa9e4066Sahrens 		vdev_close(vd);
1540fa9e4066Sahrens 		return (error ? error : ENXIO);
1541fa9e4066Sahrens 	}
1542fa9e4066Sahrens 
1543fa9e4066Sahrens 	/*
15440713e232SGeorge Wilson 	 * Recursively load DTLs and initialize all labels.
1545fa9e4066Sahrens 	 */
15460713e232SGeorge Wilson 	if ((error = vdev_dtl_load(vd)) != 0 ||
15470713e232SGeorge Wilson 	    (error = vdev_label_init(vd, txg, isreplacing ?
154839c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1549fa9e4066Sahrens 		vdev_close(vd);
1550fa9e4066Sahrens 		return (error);
1551fa9e4066Sahrens 	}
1552fa9e4066Sahrens 
1553fa9e4066Sahrens 	return (0);
1554fa9e4066Sahrens }
1555fa9e4066Sahrens 
15560e34b6a7Sbonwick void
1557573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
1558fa9e4066Sahrens {
1559fa9e4066Sahrens 	/*
1560*bf3e216cSMatthew Ahrens 	 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1561fa9e4066Sahrens 	 */
1562*bf3e216cSMatthew Ahrens 	vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
1563fa9e4066Sahrens 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1564fa9e4066Sahrens }
1565fa9e4066Sahrens 
1566fa9e4066Sahrens void
1567ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1568fa9e4066Sahrens {
1569ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
157088ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
1571ecc2d604Sbonwick 	ASSERT(ISP2(flags));
1572f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
1573fa9e4066Sahrens 
1574ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
1575ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1576ecc2d604Sbonwick 
1577ecc2d604Sbonwick 	if (flags & VDD_DTL)
1578ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1579ecc2d604Sbonwick 
1580ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1581fa9e4066Sahrens }
1582fa9e4066Sahrens 
15830713e232SGeorge Wilson void
15840713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
15850713e232SGeorge Wilson {
15860713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
15870713e232SGeorge Wilson 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
15880713e232SGeorge Wilson 
15890713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
15900713e232SGeorge Wilson 		vdev_dirty(vd->vdev_top, flags, vd, txg);
15910713e232SGeorge Wilson }
15920713e232SGeorge Wilson 
15938ad4d6ddSJeff Bonwick /*
15948ad4d6ddSJeff Bonwick  * DTLs.
15958ad4d6ddSJeff Bonwick  *
15968ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
15979fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
15988ad4d6ddSJeff Bonwick  *
15998ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
16008ad4d6ddSJeff Bonwick  *
16018ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
16028ad4d6ddSJeff Bonwick  *
16038ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
16048ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
16058ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
16068ad4d6ddSJeff Bonwick  *
16078ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
16088ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
16098ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
16108ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
16118ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
16128ad4d6ddSJeff Bonwick  *
16138ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
16148ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
16158ad4d6ddSJeff Bonwick  *
16168ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
16178ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
16188ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
16198ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
16208ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
16218ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
16228ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
16238ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
16248ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
16258ad4d6ddSJeff Bonwick  *
16268ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
16278ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
16288ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
16298ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
16308ad4d6ddSJeff Bonwick  */
1631fa9e4066Sahrens void
16328ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1633fa9e4066Sahrens {
16340713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
16358ad4d6ddSJeff Bonwick 
16368ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
16378ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1638f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
16398ad4d6ddSJeff Bonwick 
16400713e232SGeorge Wilson 	mutex_enter(rt->rt_lock);
16410713e232SGeorge Wilson 	if (!range_tree_contains(rt, txg, size))
16420713e232SGeorge Wilson 		range_tree_add(rt, txg, size);
16430713e232SGeorge Wilson 	mutex_exit(rt->rt_lock);
1644fa9e4066Sahrens }
1645fa9e4066Sahrens 
16468ad4d6ddSJeff Bonwick boolean_t
16478ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1648fa9e4066Sahrens {
16490713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
16508ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
1651fa9e4066Sahrens 
16528ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
16538ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1654fa9e4066Sahrens 
16550713e232SGeorge Wilson 	mutex_enter(rt->rt_lock);
16560713e232SGeorge Wilson 	if (range_tree_space(rt) != 0)
16570713e232SGeorge Wilson 		dirty = range_tree_contains(rt, txg, size);
16580713e232SGeorge Wilson 	mutex_exit(rt->rt_lock);
1659fa9e4066Sahrens 
1660fa9e4066Sahrens 	return (dirty);
1661fa9e4066Sahrens }
1662fa9e4066Sahrens 
16638ad4d6ddSJeff Bonwick boolean_t
16648ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
16658ad4d6ddSJeff Bonwick {
16660713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
16678ad4d6ddSJeff Bonwick 	boolean_t empty;
16688ad4d6ddSJeff Bonwick 
16690713e232SGeorge Wilson 	mutex_enter(rt->rt_lock);
16700713e232SGeorge Wilson 	empty = (range_tree_space(rt) == 0);
16710713e232SGeorge Wilson 	mutex_exit(rt->rt_lock);
16728ad4d6ddSJeff Bonwick 
16738ad4d6ddSJeff Bonwick 	return (empty);
16748ad4d6ddSJeff Bonwick }
16758ad4d6ddSJeff Bonwick 
1676b4952e17SGeorge Wilson /*
1677b4952e17SGeorge Wilson  * Returns the lowest txg in the DTL range.
1678b4952e17SGeorge Wilson  */
1679b4952e17SGeorge Wilson static uint64_t
1680b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd)
1681b4952e17SGeorge Wilson {
16820713e232SGeorge Wilson 	range_seg_t *rs;
1683b4952e17SGeorge Wilson 
1684b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
16850713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1686b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
1687b4952e17SGeorge Wilson 
16880713e232SGeorge Wilson 	rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
16890713e232SGeorge Wilson 	return (rs->rs_start - 1);
1690b4952e17SGeorge Wilson }
1691b4952e17SGeorge Wilson 
1692b4952e17SGeorge Wilson /*
1693b4952e17SGeorge Wilson  * Returns the highest txg in the DTL.
1694b4952e17SGeorge Wilson  */
1695b4952e17SGeorge Wilson static uint64_t
1696b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd)
1697b4952e17SGeorge Wilson {
16980713e232SGeorge Wilson 	range_seg_t *rs;
1699b4952e17SGeorge Wilson 
1700b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
17010713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1702b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
1703b4952e17SGeorge Wilson 
17040713e232SGeorge Wilson 	rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
17050713e232SGeorge Wilson 	return (rs->rs_end);
1706b4952e17SGeorge Wilson }
1707b4952e17SGeorge Wilson 
1708b4952e17SGeorge Wilson /*
1709b4952e17SGeorge Wilson  * Determine if a resilvering vdev should remove any DTL entries from
1710b4952e17SGeorge Wilson  * its range. If the vdev was resilvering for the entire duration of the
1711b4952e17SGeorge Wilson  * scan then it should excise that range from its DTLs. Otherwise, this
1712b4952e17SGeorge Wilson  * vdev is considered partially resilvered and should leave its DTL
1713b4952e17SGeorge Wilson  * entries intact. The comment in vdev_dtl_reassess() describes how we
1714b4952e17SGeorge Wilson  * excise the DTLs.
1715b4952e17SGeorge Wilson  */
1716b4952e17SGeorge Wilson static boolean_t
1717b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd)
1718b4952e17SGeorge Wilson {
1719b4952e17SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1720b4952e17SGeorge Wilson 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1721b4952e17SGeorge Wilson 
1722b4952e17SGeorge Wilson 	ASSERT0(scn->scn_phys.scn_errors);
1723b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
1724b4952e17SGeorge Wilson 
1725b4952e17SGeorge Wilson 	if (vd->vdev_resilver_txg == 0 ||
17260713e232SGeorge Wilson 	    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1727b4952e17SGeorge Wilson 		return (B_TRUE);
1728b4952e17SGeorge Wilson 
1729b4952e17SGeorge Wilson 	/*
1730b4952e17SGeorge Wilson 	 * When a resilver is initiated the scan will assign the scn_max_txg
1731b4952e17SGeorge Wilson 	 * value to the highest txg value that exists in all DTLs. If this
1732b4952e17SGeorge Wilson 	 * device's max DTL is not part of this scan (i.e. it is not in
1733b4952e17SGeorge Wilson 	 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1734b4952e17SGeorge Wilson 	 * for excision.
1735b4952e17SGeorge Wilson 	 */
1736b4952e17SGeorge Wilson 	if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1737b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1738b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1739b4952e17SGeorge Wilson 		ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1740b4952e17SGeorge Wilson 		return (B_TRUE);
1741b4952e17SGeorge Wilson 	}
1742b4952e17SGeorge Wilson 	return (B_FALSE);
1743b4952e17SGeorge Wilson }
1744b4952e17SGeorge Wilson 
1745fa9e4066Sahrens /*
1746fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
1747fa9e4066Sahrens  */
1748fa9e4066Sahrens void
1749fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1750fa9e4066Sahrens {
1751ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
17528ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
17538ad4d6ddSJeff Bonwick 	int minref;
1754fa9e4066Sahrens 
17558ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1756fa9e4066Sahrens 
17578ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
17588ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
17598ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
17608ad4d6ddSJeff Bonwick 
1761b24ab676SJeff Bonwick 	if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
17628ad4d6ddSJeff Bonwick 		return;
17638ad4d6ddSJeff Bonwick 
17648ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
17653f9d6ad7SLin Ling 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
17663f9d6ad7SLin Ling 
1767fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
1768b4952e17SGeorge Wilson 
1769b4952e17SGeorge Wilson 		/*
1770b4952e17SGeorge Wilson 		 * If we've completed a scan cleanly then determine
1771b4952e17SGeorge Wilson 		 * if this vdev should remove any DTLs. We only want to
1772b4952e17SGeorge Wilson 		 * excise regions on vdevs that were available during
1773b4952e17SGeorge Wilson 		 * the entire duration of this scan.
1774b4952e17SGeorge Wilson 		 */
1775088f3894Sahrens 		if (scrub_txg != 0 &&
17763f9d6ad7SLin Ling 		    (spa->spa_scrub_started ||
1777b4952e17SGeorge Wilson 		    (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1778b4952e17SGeorge Wilson 		    vdev_dtl_should_excise(vd)) {
1779088f3894Sahrens 			/*
1780088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
1781088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
1782088f3894Sahrens 			 * will be valid, so excise the old region and
1783088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
1784088f3894Sahrens 			 * dtl as-is if there was an error.
17858ad4d6ddSJeff Bonwick 			 *
17868ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
17878ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
17888ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
17898ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
17908ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
17918ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
17928ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
17938ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
17948ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
1795088f3894Sahrens 			 */
17960713e232SGeorge Wilson 			space_reftree_create(&reftree);
17970713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
17980713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
17990713e232SGeorge Wilson 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
18000713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
18010713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_SCRUB], 2);
18020713e232SGeorge Wilson 			space_reftree_generate_map(&reftree,
18030713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
18040713e232SGeorge Wilson 			space_reftree_destroy(&reftree);
1805fa9e4066Sahrens 		}
18060713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
18070713e232SGeorge Wilson 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
18080713e232SGeorge Wilson 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
1809fa9e4066Sahrens 		if (scrub_done)
18100713e232SGeorge Wilson 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
18110713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
18128ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
18130713e232SGeorge Wilson 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
18148ad4d6ddSJeff Bonwick 		else
18150713e232SGeorge Wilson 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
18160713e232SGeorge Wilson 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
1817b4952e17SGeorge Wilson 
1818b4952e17SGeorge Wilson 		/*
1819b4952e17SGeorge Wilson 		 * If the vdev was resilvering and no longer has any
1820b4952e17SGeorge Wilson 		 * DTLs then reset its resilvering flag.
1821b4952e17SGeorge Wilson 		 */
1822b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0 &&
18230713e232SGeorge Wilson 		    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
18240713e232SGeorge Wilson 		    range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
1825b4952e17SGeorge Wilson 			vd->vdev_resilver_txg = 0;
1826b4952e17SGeorge Wilson 
1827fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
1828088f3894Sahrens 
1829ecc2d604Sbonwick 		if (txg != 0)
1830ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1831fa9e4066Sahrens 		return;
1832fa9e4066Sahrens 	}
1833fa9e4066Sahrens 
1834fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
18358ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
183699bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
183799bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
18388ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
18398ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
18408ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
18418ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
18428ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
18438ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
18448ad4d6ddSJeff Bonwick 		else
18458ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
18460713e232SGeorge Wilson 		space_reftree_create(&reftree);
18478ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
18488ad4d6ddSJeff Bonwick 			vdev_t *cvd = vd->vdev_child[c];
18498ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
18500713e232SGeorge Wilson 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
18518ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
18528ad4d6ddSJeff Bonwick 		}
18530713e232SGeorge Wilson 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
18540713e232SGeorge Wilson 		space_reftree_destroy(&reftree);
1855fa9e4066Sahrens 	}
18568ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
1857fa9e4066Sahrens }
1858fa9e4066Sahrens 
18590713e232SGeorge Wilson int
1860fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
1861fa9e4066Sahrens {
1862fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
1863ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
18640713e232SGeorge Wilson 	int error = 0;
1865fa9e4066Sahrens 
18660713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
18670713e232SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
1868fa9e4066Sahrens 
18690713e232SGeorge Wilson 		error = space_map_open(&vd->vdev_dtl_sm, mos,
18700713e232SGeorge Wilson 		    vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
18710713e232SGeorge Wilson 		if (error)
18720713e232SGeorge Wilson 			return (error);
18730713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
1874fa9e4066Sahrens 
18750713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
187688ecc943SGeorge Wilson 
18770713e232SGeorge Wilson 		/*
18780713e232SGeorge Wilson 		 * Now that we've opened the space_map we need to update
18790713e232SGeorge Wilson 		 * the in-core DTL.
18800713e232SGeorge Wilson 		 */
18810713e232SGeorge Wilson 		space_map_update(vd->vdev_dtl_sm);
1882ecc2d604Sbonwick 
18830713e232SGeorge Wilson 		error = space_map_load(vd->vdev_dtl_sm,
18840713e232SGeorge Wilson 		    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
18850713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
1886fa9e4066Sahrens 
18870713e232SGeorge Wilson 		return (error);
18880713e232SGeorge Wilson 	}
18890713e232SGeorge Wilson 
18900713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
18910713e232SGeorge Wilson 		error = vdev_dtl_load(vd->vdev_child[c]);
18920713e232SGeorge Wilson 		if (error != 0)
18930713e232SGeorge Wilson 			break;
18940713e232SGeorge Wilson 	}
1895fa9e4066Sahrens 
1896fa9e4066Sahrens 	return (error);
1897fa9e4066Sahrens }
1898fa9e4066Sahrens 
1899fa9e4066Sahrens void
1900fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1901fa9e4066Sahrens {
1902fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
19030713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
1904ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
19050713e232SGeorge Wilson 	range_tree_t *rtsync;
19060713e232SGeorge Wilson 	kmutex_t rtlock;
1907fa9e4066Sahrens 	dmu_tx_t *tx;
19080713e232SGeorge Wilson 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
1909fa9e4066Sahrens 
191088ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
19110713e232SGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
191288ecc943SGeorge Wilson 
1913fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1914fa9e4066Sahrens 
19150713e232SGeorge Wilson 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
19160713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
19170713e232SGeorge Wilson 		space_map_free(vd->vdev_dtl_sm, tx);
19180713e232SGeorge Wilson 		space_map_close(vd->vdev_dtl_sm);
19190713e232SGeorge Wilson 		vd->vdev_dtl_sm = NULL;
19200713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
1921fa9e4066Sahrens 		dmu_tx_commit(tx);
1922fa9e4066Sahrens 		return;
1923fa9e4066Sahrens 	}
1924fa9e4066Sahrens 
19250713e232SGeorge Wilson 	if (vd->vdev_dtl_sm == NULL) {
19260713e232SGeorge Wilson 		uint64_t new_object;
19270713e232SGeorge Wilson 
19280713e232SGeorge Wilson 		new_object = space_map_alloc(mos, tx);
19290713e232SGeorge Wilson 		VERIFY3U(new_object, !=, 0);
19300713e232SGeorge Wilson 
19310713e232SGeorge Wilson 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
19320713e232SGeorge Wilson 		    0, -1ULL, 0, &vd->vdev_dtl_lock));
19330713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
1934fa9e4066Sahrens 	}
1935fa9e4066Sahrens 
19360713e232SGeorge Wilson 	mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
1937fa9e4066Sahrens 
19380713e232SGeorge Wilson 	rtsync = range_tree_create(NULL, NULL, &rtlock);
1939fa9e4066Sahrens 
19400713e232SGeorge Wilson 	mutex_enter(&rtlock);
1941fa9e4066Sahrens 
1942fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
19430713e232SGeorge Wilson 	range_tree_walk(rt, range_tree_add, rtsync);
1944fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
1945fa9e4066Sahrens 
19460713e232SGeorge Wilson 	space_map_truncate(vd->vdev_dtl_sm, tx);
19470713e232SGeorge Wilson 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
19480713e232SGeorge Wilson 	range_tree_vacate(rtsync, NULL, NULL);
1949fa9e4066Sahrens 
19500713e232SGeorge Wilson 	range_tree_destroy(rtsync);
1951fa9e4066Sahrens 
19520713e232SGeorge Wilson 	mutex_exit(&rtlock);
19530713e232SGeorge Wilson 	mutex_destroy(&rtlock);
1954fa9e4066Sahrens 
19550713e232SGeorge Wilson 	/*
19560713e232SGeorge Wilson 	 * If the object for the space map has changed then dirty
19570713e232SGeorge Wilson 	 * the top level so that we update the config.
19580713e232SGeorge Wilson 	 */
19590713e232SGeorge Wilson 	if (object != space_map_object(vd->vdev_dtl_sm)) {
19600713e232SGeorge Wilson 		zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
19610713e232SGeorge Wilson 		    "new object %llu", txg, spa_name(spa), object,
19620713e232SGeorge Wilson 		    space_map_object(vd->vdev_dtl_sm));
19630713e232SGeorge Wilson 		vdev_config_dirty(vd->vdev_top);
19640713e232SGeorge Wilson 	}
1965fa9e4066Sahrens 
1966fa9e4066Sahrens 	dmu_tx_commit(tx);
19670713e232SGeorge Wilson 
19680713e232SGeorge Wilson 	mutex_enter(&vd->vdev_dtl_lock);
19690713e232SGeorge Wilson 	space_map_update(vd->vdev_dtl_sm);
19700713e232SGeorge Wilson 	mutex_exit(&vd->vdev_dtl_lock);
1971fa9e4066Sahrens }
1972fa9e4066Sahrens 
19738ad4d6ddSJeff Bonwick /*
19748ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
19758ad4d6ddSJeff Bonwick  * without losing data.
19768ad4d6ddSJeff Bonwick  */
19778ad4d6ddSJeff Bonwick boolean_t
19788ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
19798ad4d6ddSJeff Bonwick {
19808ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
19818ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
19828ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
19838ad4d6ddSJeff Bonwick 	boolean_t required;
19848ad4d6ddSJeff Bonwick 
19858ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
19868ad4d6ddSJeff Bonwick 
19878ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
19888ad4d6ddSJeff Bonwick 		return (B_TRUE);
19898ad4d6ddSJeff Bonwick 
19908ad4d6ddSJeff Bonwick 	/*
19918ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
19928ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
19938ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
19948ad4d6ddSJeff Bonwick 	 */
19958ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
19968ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
19978ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
19988ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
19998ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
20008ad4d6ddSJeff Bonwick 
2001cb04b873SMark J Musante 	if (!required && zio_injection_enabled)
2002cb04b873SMark J Musante 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2003cb04b873SMark J Musante 
20048ad4d6ddSJeff Bonwick 	return (required);
20058ad4d6ddSJeff Bonwick }
20068ad4d6ddSJeff Bonwick 
2007088f3894Sahrens /*
2008088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
2009088f3894Sahrens  */
2010088f3894Sahrens boolean_t
2011088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2012088f3894Sahrens {
2013088f3894Sahrens 	boolean_t needed = B_FALSE;
2014088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
2015088f3894Sahrens 	uint64_t thismax = 0;
2016088f3894Sahrens 
2017088f3894Sahrens 	if (vd->vdev_children == 0) {
2018088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
20190713e232SGeorge Wilson 		if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
20208ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
2021088f3894Sahrens 
2022b4952e17SGeorge Wilson 			thismin = vdev_dtl_min(vd);
2023b4952e17SGeorge Wilson 			thismax = vdev_dtl_max(vd);
2024088f3894Sahrens 			needed = B_TRUE;
2025088f3894Sahrens 		}
2026088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2027088f3894Sahrens 	} else {
20288ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
2029088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
2030088f3894Sahrens 			uint64_t cmin, cmax;
2031088f3894Sahrens 
2032088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2033088f3894Sahrens 				thismin = MIN(thismin, cmin);
2034088f3894Sahrens 				thismax = MAX(thismax, cmax);
2035088f3894Sahrens 				needed = B_TRUE;
2036088f3894Sahrens 			}
2037088f3894Sahrens 		}
2038088f3894Sahrens 	}
2039088f3894Sahrens 
2040088f3894Sahrens 	if (needed && minp) {
2041088f3894Sahrens 		*minp = thismin;
2042088f3894Sahrens 		*maxp = thismax;
2043088f3894Sahrens 	}
2044088f3894Sahrens 	return (needed);
2045088f3894Sahrens }
2046088f3894Sahrens 
2047560e6e96Seschrock void
2048ea8dc4b6Seschrock vdev_load(vdev_t *vd)
2049fa9e4066Sahrens {
2050fa9e4066Sahrens 	/*
2051fa9e4066Sahrens 	 * Recursively load all children.
2052fa9e4066Sahrens 	 */
20538ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2054560e6e96Seschrock 		vdev_load(vd->vdev_child[c]);
2055fa9e4066Sahrens 
2056fa9e4066Sahrens 	/*
20570e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
2058fa9e4066Sahrens 	 */
205988ecc943SGeorge Wilson 	if (vd == vd->vdev_top && !vd->vdev_ishole &&
2060560e6e96Seschrock 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2061560e6e96Seschrock 	    vdev_metaslab_init(vd, 0) != 0))
2062560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2063560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
2064fa9e4066Sahrens 
2065fa9e4066Sahrens 	/*
2066fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
2067fa9e4066Sahrens 	 */
2068560e6e96Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2069560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2070560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
2071fa9e4066Sahrens }
2072fa9e4066Sahrens 
207399653d4eSeschrock /*
2074fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
2075fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
2076fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
2077fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
2078fa94a07fSbrendan  * repurposed to another pool.
207999653d4eSeschrock  */
208099653d4eSeschrock int
2081fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
208299653d4eSeschrock {
208399653d4eSeschrock 	nvlist_t *label;
208499653d4eSeschrock 	uint64_t guid, version;
208599653d4eSeschrock 	uint64_t state;
208699653d4eSeschrock 
2087e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
2088c5904d13Seschrock 		return (0);
2089c5904d13Seschrock 
2090dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
209199653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
209299653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
209399653d4eSeschrock 		return (-1);
209499653d4eSeschrock 	}
209599653d4eSeschrock 
209699653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2097ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(version) ||
209899653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
209999653d4eSeschrock 	    guid != vd->vdev_guid ||
210099653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
210199653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
210299653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
210399653d4eSeschrock 		nvlist_free(label);
210499653d4eSeschrock 		return (-1);
210599653d4eSeschrock 	}
210699653d4eSeschrock 
210799653d4eSeschrock 	/*
210899653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
210999653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
211099653d4eSeschrock 	 */
211199653d4eSeschrock 	nvlist_free(label);
211299653d4eSeschrock 	return (0);
211399653d4eSeschrock }
211499653d4eSeschrock 
211588ecc943SGeorge Wilson void
211688ecc943SGeorge Wilson vdev_remove(vdev_t *vd, uint64_t txg)
211788ecc943SGeorge Wilson {
211888ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
211988ecc943SGeorge Wilson 	objset_t *mos = spa->spa_meta_objset;
212088ecc943SGeorge Wilson 	dmu_tx_t *tx;
212188ecc943SGeorge Wilson 
212288ecc943SGeorge Wilson 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
212388ecc943SGeorge Wilson 
212488ecc943SGeorge Wilson 	if (vd->vdev_ms != NULL) {
21252e4c9986SGeorge Wilson 		metaslab_group_t *mg = vd->vdev_mg;
21262e4c9986SGeorge Wilson 
21272e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
21282e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
21292e4c9986SGeorge Wilson 
213088ecc943SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
213188ecc943SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
213288ecc943SGeorge Wilson 
21330713e232SGeorge Wilson 			if (msp == NULL || msp->ms_sm == NULL)
213488ecc943SGeorge Wilson 				continue;
213588ecc943SGeorge Wilson 
21360713e232SGeorge Wilson 			mutex_enter(&msp->ms_lock);
21372e4c9986SGeorge Wilson 			/*
21382e4c9986SGeorge Wilson 			 * If the metaslab was not loaded when the vdev
21392e4c9986SGeorge Wilson 			 * was removed then the histogram accounting may
21402e4c9986SGeorge Wilson 			 * not be accurate. Update the histogram information
21412e4c9986SGeorge Wilson 			 * here so that we ensure that the metaslab group
21422e4c9986SGeorge Wilson 			 * and metaslab class are up-to-date.
21432e4c9986SGeorge Wilson 			 */
21442e4c9986SGeorge Wilson 			metaslab_group_histogram_remove(mg, msp);
21452e4c9986SGeorge Wilson 
21460713e232SGeorge Wilson 			VERIFY0(space_map_allocated(msp->ms_sm));
21470713e232SGeorge Wilson 			space_map_free(msp->ms_sm, tx);
21480713e232SGeorge Wilson 			space_map_close(msp->ms_sm);
21490713e232SGeorge Wilson 			msp->ms_sm = NULL;
21500713e232SGeorge Wilson 			mutex_exit(&msp->ms_lock);
215188ecc943SGeorge Wilson 		}
21522e4c9986SGeorge Wilson 
21532e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
21542e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
21552e4c9986SGeorge Wilson 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
21562e4c9986SGeorge Wilson 			ASSERT0(mg->mg_histogram[i]);
21572e4c9986SGeorge Wilson 
215888ecc943SGeorge Wilson 	}
215988ecc943SGeorge Wilson 
216088ecc943SGeorge Wilson 	if (vd->vdev_ms_array) {
216188ecc943SGeorge Wilson 		(void) dmu_object_free(mos, vd->vdev_ms_array, tx);
216288ecc943SGeorge Wilson 		vd->vdev_ms_array = 0;
216388ecc943SGeorge Wilson 	}
216488ecc943SGeorge Wilson 	dmu_tx_commit(tx);
216588ecc943SGeorge Wilson }
216688ecc943SGeorge Wilson 
2167fa9e4066Sahrens void
2168fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
2169fa9e4066Sahrens {
2170fa9e4066Sahrens 	metaslab_t *msp;
217180eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2172fa9e4066Sahrens 
217388ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
217488ecc943SGeorge Wilson 
2175fa9e4066Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2176fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
217780eb36f2SGeorge Wilson 
217880eb36f2SGeorge Wilson 	if (reassess)
217980eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
2180fa9e4066Sahrens }
2181fa9e4066Sahrens 
2182fa9e4066Sahrens void
2183fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2184fa9e4066Sahrens {
2185fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2186fa9e4066Sahrens 	vdev_t *lvd;
2187fa9e4066Sahrens 	metaslab_t *msp;
2188ecc2d604Sbonwick 	dmu_tx_t *tx;
2189fa9e4066Sahrens 
219088ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
219188ecc943SGeorge Wilson 
2192ecc2d604Sbonwick 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2193ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
2194ecc2d604Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2195ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2196ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2197ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
2198ecc2d604Sbonwick 		vdev_config_dirty(vd);
2199ecc2d604Sbonwick 		dmu_tx_commit(tx);
2200ecc2d604Sbonwick 	}
2201fa9e4066Sahrens 
22023f9d6ad7SLin Ling 	/*
22033f9d6ad7SLin Ling 	 * Remove the metadata associated with this vdev once it's empty.
22043f9d6ad7SLin Ling 	 */
22053f9d6ad7SLin Ling 	if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
220688ecc943SGeorge Wilson 		vdev_remove(vd, txg);
220788ecc943SGeorge Wilson 
2208ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2209fa9e4066Sahrens 		metaslab_sync(msp, txg);
2210ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2211ecc2d604Sbonwick 	}
2212fa9e4066Sahrens 
2213fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2214fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
2215fa9e4066Sahrens 
2216fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2217fa9e4066Sahrens }
2218fa9e4066Sahrens 
2219fa9e4066Sahrens uint64_t
2220fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2221fa9e4066Sahrens {
2222fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2223fa9e4066Sahrens }
2224fa9e4066Sahrens 
22253d7072f8Seschrock /*
22263d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
22273d7072f8Seschrock  * not be opened, and no I/O is attempted.
22283d7072f8Seschrock  */
2229fa9e4066Sahrens int
2230069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2231fa9e4066Sahrens {
22324b964adaSGeorge Wilson 	vdev_t *vd, *tvd;
2233fa9e4066Sahrens 
22348f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
2235fa9e4066Sahrens 
2236c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2237e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2238e14bb325SJeff Bonwick 
22393d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2240e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2241fa9e4066Sahrens 
22424b964adaSGeorge Wilson 	tvd = vd->vdev_top;
22434b964adaSGeorge Wilson 
2244069f55e2SEric Schrock 	/*
2245069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
2246069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
2247069f55e2SEric Schrock 	 * were faulted.
2248069f55e2SEric Schrock 	 */
2249069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
2250069f55e2SEric Schrock 
22513d7072f8Seschrock 	/*
22523d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
22533d7072f8Seschrock 	 */
225498d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
22553d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
22563d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
2257069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
22583d7072f8Seschrock 
22593d7072f8Seschrock 	/*
2260c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
2261c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
22623d7072f8Seschrock 	 */
22634b964adaSGeorge Wilson 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
22643d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
22653d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
22663d7072f8Seschrock 
22673d7072f8Seschrock 		/*
22683d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
22693d7072f8Seschrock 		 * mark it degraded.
22703d7072f8Seschrock 		 */
22714b964adaSGeorge Wilson 		vdev_reopen(tvd);
22723d7072f8Seschrock 
2273069f55e2SEric Schrock 		if (vdev_readable(vd))
2274069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
22753d7072f8Seschrock 	}
22763d7072f8Seschrock 
2277e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
22783d7072f8Seschrock }
22793d7072f8Seschrock 
22803d7072f8Seschrock /*
22813d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
22823d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
22833d7072f8Seschrock  * as I/O is concerned.
22843d7072f8Seschrock  */
22853d7072f8Seschrock int
2286069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
22873d7072f8Seschrock {
2288c5904d13Seschrock 	vdev_t *vd;
22890a4e9518Sgw 
22908f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
22913d7072f8Seschrock 
2292c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2293e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2294e14bb325SJeff Bonwick 
22950e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2296e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
22970e34b6a7Sbonwick 
22983d7072f8Seschrock 	/*
22993d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
23003d7072f8Seschrock 	 */
2301e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
2302e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
23033d7072f8Seschrock 
23043d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
23053d7072f8Seschrock 	if (!vdev_is_dead(vd))
23063d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2307069f55e2SEric Schrock 		    aux);
23083d7072f8Seschrock 
2309e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
23103d7072f8Seschrock }
23113d7072f8Seschrock 
23123d7072f8Seschrock /*
2313f7170741SWill Andrews  * Online the given vdev.
2314f7170741SWill Andrews  *
2315f7170741SWill Andrews  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
2316f7170741SWill Andrews  * spare device should be detached when the device finishes resilvering.
2317f7170741SWill Andrews  * Second, the online should be treated like a 'test' online case, so no FMA
2318f7170741SWill Andrews  * events are generated if the device fails to open.
23193d7072f8Seschrock  */
23203d7072f8Seschrock int
2321e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
23223d7072f8Seschrock {
2323573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
23243d7072f8Seschrock 
23258f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
23263d7072f8Seschrock 
2327c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2328e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
23293d7072f8Seschrock 
23303d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2331e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2332fa9e4066Sahrens 
2333573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
2334fa9e4066Sahrens 	vd->vdev_offline = B_FALSE;
2335441d80aaSlling 	vd->vdev_tmpoffline = B_FALSE;
2336e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2337e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2338573ca77eSGeorge Wilson 
2339573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
2340573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2341573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2342573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2343573ca77eSGeorge Wilson 	}
2344573ca77eSGeorge Wilson 
2345573ca77eSGeorge Wilson 	vdev_reopen(tvd);
23463d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
23473d7072f8Seschrock 
2348573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
2349573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2350573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
2351573ca77eSGeorge Wilson 	}
2352573ca77eSGeorge Wilson 
23533d7072f8Seschrock 	if (newstate)
23543d7072f8Seschrock 		*newstate = vd->vdev_state;
23553d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
23563d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
23573d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
23583d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
23593d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
2360fa9e4066Sahrens 
2361573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2362573ca77eSGeorge Wilson 
2363573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
2364573ca77eSGeorge Wilson 		if (vd->vdev_aux)
2365573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2366573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2367573ca77eSGeorge Wilson 	}
23688ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
2369fa9e4066Sahrens }
2370fa9e4066Sahrens 
2371a1521560SJeff Bonwick static int
2372a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2373fa9e4066Sahrens {
2374e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
23758f18d1faSGeorge Wilson 	int error = 0;
23768f18d1faSGeorge Wilson 	uint64_t generation;
23778f18d1faSGeorge Wilson 	metaslab_group_t *mg;
23780a4e9518Sgw 
23798f18d1faSGeorge Wilson top:
23808f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
2381fa9e4066Sahrens 
2382c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2383e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2384fa9e4066Sahrens 
23850e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2386e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
23870e34b6a7Sbonwick 
2388e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
23898f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
23908f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
2391e6ca193dSGeorge Wilson 
2392fa9e4066Sahrens 	/*
2393ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
2394fa9e4066Sahrens 	 */
2395ecc2d604Sbonwick 	if (!vd->vdev_offline) {
2396ecc2d604Sbonwick 		/*
23978ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
2398e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
2399e6ca193dSGeorge Wilson 		 * expendable.
2400ecc2d604Sbonwick 		 */
2401e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2402e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
2403e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2404fa9e4066Sahrens 
24058f18d1faSGeorge Wilson 		/*
2406b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
2407b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
2408b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
2409b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
24108f18d1faSGeorge Wilson 		 */
24118f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
24128f18d1faSGeorge Wilson 			/*
24138f18d1faSGeorge Wilson 			 * Prevent any future allocations.
24148f18d1faSGeorge Wilson 			 */
2415a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
24168f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
24178f18d1faSGeorge Wilson 
24181195e687SMark J Musante 			error = spa_offline_log(spa);
24198f18d1faSGeorge Wilson 
24208f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
24218f18d1faSGeorge Wilson 
24228f18d1faSGeorge Wilson 			/*
24238f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
24248f18d1faSGeorge Wilson 			 */
24258f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
2426a1521560SJeff Bonwick 				metaslab_group_activate(mg);
24278f18d1faSGeorge Wilson 				if (error)
24288f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
24298f18d1faSGeorge Wilson 					    vd, error));
24308f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
24318f18d1faSGeorge Wilson 				goto top;
24328f18d1faSGeorge Wilson 			}
2433fb09f5aaSMadhav Suresh 			ASSERT0(tvd->vdev_stat.vs_alloc);
24348f18d1faSGeorge Wilson 		}
24358f18d1faSGeorge Wilson 
2436ecc2d604Sbonwick 		/*
2437ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
2438e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
2439e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
2440e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
2441ecc2d604Sbonwick 		 */
2442ecc2d604Sbonwick 		vd->vdev_offline = B_TRUE;
2443e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
2444e6ca193dSGeorge Wilson 
2445e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2446e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
2447ecc2d604Sbonwick 			vd->vdev_offline = B_FALSE;
2448e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
2449e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
2450ecc2d604Sbonwick 		}
24518f18d1faSGeorge Wilson 
24528f18d1faSGeorge Wilson 		/*
24538f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
24548f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
24558f18d1faSGeorge Wilson 		 */
24568f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
2457a1521560SJeff Bonwick 			metaslab_group_activate(mg);
2458fa9e4066Sahrens 	}
2459fa9e4066Sahrens 
2460e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2461ecc2d604Sbonwick 
24628f18d1faSGeorge Wilson 	return (spa_vdev_state_exit(spa, vd, 0));
2463fa9e4066Sahrens }
2464fa9e4066Sahrens 
2465a1521560SJeff Bonwick int
2466a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2467a1521560SJeff Bonwick {
2468a1521560SJeff Bonwick 	int error;
2469a1521560SJeff Bonwick 
2470a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
2471a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
2472a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
2473a1521560SJeff Bonwick 
2474a1521560SJeff Bonwick 	return (error);
2475a1521560SJeff Bonwick }
2476a1521560SJeff Bonwick 
2477ea8dc4b6Seschrock /*
2478ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
2479ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
2480ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
2481ea8dc4b6Seschrock  */
2482ea8dc4b6Seschrock void
2483e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
2484fa9e4066Sahrens {
2485e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2486e14bb325SJeff Bonwick 
2487e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2488fa9e4066Sahrens 
2489ea8dc4b6Seschrock 	if (vd == NULL)
2490e14bb325SJeff Bonwick 		vd = rvd;
2491fa9e4066Sahrens 
2492ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
2493ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
2494ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
2495fa9e4066Sahrens 
2496e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2497e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
24983d7072f8Seschrock 
24993d7072f8Seschrock 	/*
25008a79c1b5Sek 	 * If we're in the FAULTED state or have experienced failed I/O, then
25018a79c1b5Sek 	 * clear the persistent state and attempt to reopen the device.  We
25028a79c1b5Sek 	 * also mark the vdev config dirty, so that the new faulted state is
25038a79c1b5Sek 	 * written out to disk.
25043d7072f8Seschrock 	 */
2505e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
2506e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
25078a79c1b5Sek 
2508096d22d4SEric Schrock 		/*
2509096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
2510096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
2511096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
2512096d22d4SEric Schrock 		 */
2513096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
2514096d22d4SEric Schrock 
25154b964adaSGeorge Wilson 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2516e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
2517e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
2518e14bb325SJeff Bonwick 
2519f9af39baSGeorge Wilson 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
25203d7072f8Seschrock 
2521096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
2522096d22d4SEric Schrock 
2523f9af39baSGeorge Wilson 		if (vd != rvd && vdev_writeable(vd->vdev_top))
2524e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
2525e14bb325SJeff Bonwick 
2526e14bb325SJeff Bonwick 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2527bb8b5132Sek 			spa_async_request(spa, SPA_ASYNC_RESILVER);
25283d7072f8Seschrock 
25293d7072f8Seschrock 		spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
25303d7072f8Seschrock 	}
2531096d22d4SEric Schrock 
2532096d22d4SEric Schrock 	/*
2533096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
2534096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
2535096d22d4SEric Schrock 	 * done in response to the FMA fault.
2536096d22d4SEric Schrock 	 */
2537096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2538096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2539096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
2540096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
2541fa9e4066Sahrens }
2542fa9e4066Sahrens 
2543e14bb325SJeff Bonwick boolean_t
2544e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd)
25450a4e9518Sgw {
254688ecc943SGeorge Wilson 	/*
254788ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
254888ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
254988ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
255088ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
255188ecc943SGeorge Wilson 	 * before issuing I/O to them.
255288ecc943SGeorge Wilson 	 */
255388ecc943SGeorge Wilson 	return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
255488ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
25550a4e9518Sgw }
25560a4e9518Sgw 
2557e14bb325SJeff Bonwick boolean_t
2558e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
25590a4e9518Sgw {
2560e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
25610a4e9518Sgw }
25620a4e9518Sgw 
2563e14bb325SJeff Bonwick boolean_t
2564e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
2565fa9e4066Sahrens {
2566e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2567fa9e4066Sahrens }
2568fa9e4066Sahrens 
2569a31e6787SGeorge Wilson boolean_t
2570a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
2571a31e6787SGeorge Wilson {
25728ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
25738ad4d6ddSJeff Bonwick 
2574a31e6787SGeorge Wilson 	/*
25758ad4d6ddSJeff Bonwick 	 * We currently allow allocations from vdevs which may be in the
2576a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2577a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
25788ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
25798ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
25808ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
2581a31e6787SGeorge Wilson 	 */
25828ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
25833f9d6ad7SLin Ling 	    !vd->vdev_cant_write && !vd->vdev_ishole);
2584a31e6787SGeorge Wilson }
2585a31e6787SGeorge Wilson 
2586e14bb325SJeff Bonwick boolean_t
2587e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
2588fa9e4066Sahrens {
2589e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
2590fa9e4066Sahrens 
2591e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2592e14bb325SJeff Bonwick 		return (B_FALSE);
2593fa9e4066Sahrens 
2594e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
2595e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
2596fa9e4066Sahrens 
2597e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
2598e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
2599fa9e4066Sahrens 
2600e14bb325SJeff Bonwick 	return (B_TRUE);
2601fa9e4066Sahrens }
2602fa9e4066Sahrens 
2603fa9e4066Sahrens /*
2604fa9e4066Sahrens  * Get statistics for the given vdev.
2605fa9e4066Sahrens  */
2606fa9e4066Sahrens void
2607fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2608fa9e4066Sahrens {
26092e4c9986SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
26102e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
26112e4c9986SGeorge Wilson 
26122e4c9986SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2613fa9e4066Sahrens 
2614fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
2615fa9e4066Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2616fa9e4066Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2617fa9e4066Sahrens 	vs->vs_state = vd->vdev_state;
2618573ca77eSGeorge Wilson 	vs->vs_rsize = vdev_get_min_asize(vd);
2619573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
2620573ca77eSGeorge Wilson 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
26214263d13fSGeorge Wilson 	vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize;
26222986efa8SAlex Reece 	if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) {
26232e4c9986SGeorge Wilson 		vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
26242986efa8SAlex Reece 	}
2625fa9e4066Sahrens 
2626fa9e4066Sahrens 	/*
2627fa9e4066Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
2628fa9e4066Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
2629fa9e4066Sahrens 	 */
2630fa9e4066Sahrens 	if (vd == rvd) {
2631e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2632fa9e4066Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
2633fa9e4066Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
2634fa9e4066Sahrens 
2635e14bb325SJeff Bonwick 			for (int t = 0; t < ZIO_TYPES; t++) {
2636fa9e4066Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
2637fa9e4066Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
2638fa9e4066Sahrens 			}
26393f9d6ad7SLin Ling 			cvs->vs_scan_removing = cvd->vdev_removing;
2640fa9e4066Sahrens 		}
2641fa9e4066Sahrens 	}
26422e4c9986SGeorge Wilson 	mutex_exit(&vd->vdev_stat_lock);
2643fa9e4066Sahrens }
2644fa9e4066Sahrens 
2645fa94a07fSbrendan void
2646fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
2647fa94a07fSbrendan {
2648fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
2649fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
2650fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
2651fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
2652fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
2653fa94a07fSbrendan }
2654fa94a07fSbrendan 
26553f9d6ad7SLin Ling void
26563f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd)
26573f9d6ad7SLin Ling {
26583f9d6ad7SLin Ling 	vdev_stat_t *vs = &vd->vdev_stat;
26593f9d6ad7SLin Ling 
26603f9d6ad7SLin Ling 	for (int c = 0; c < vd->vdev_children; c++)
26613f9d6ad7SLin Ling 		vdev_scan_stat_init(vd->vdev_child[c]);
26623f9d6ad7SLin Ling 
26633f9d6ad7SLin Ling 	mutex_enter(&vd->vdev_stat_lock);
26643f9d6ad7SLin Ling 	vs->vs_scan_processed = 0;
26653f9d6ad7SLin Ling 	mutex_exit(&vd->vdev_stat_lock);
26663f9d6ad7SLin Ling }
26673f9d6ad7SLin Ling 
2668fa9e4066Sahrens void
2669e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
2670fa9e4066Sahrens {
26718ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
26728ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2673e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2674fa9e4066Sahrens 	vdev_t *pvd;
2675fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
2676fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
2677fa9e4066Sahrens 	zio_type_t type = zio->io_type;
2678fa9e4066Sahrens 	int flags = zio->io_flags;
2679fa9e4066Sahrens 
2680e14bb325SJeff Bonwick 	/*
2681e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
2682e14bb325SJeff Bonwick 	 */
2683e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
2684e14bb325SJeff Bonwick 		return;
2685e14bb325SJeff Bonwick 
2686fa9e4066Sahrens 	if (zio->io_error == 0) {
2687e14bb325SJeff Bonwick 		/*
2688e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
2689e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
2690e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
2691e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
2692e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
2693e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
2694e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
2695e14bb325SJeff Bonwick 		 *
2696e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
2697e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
2698e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
2699e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
2700e14bb325SJeff Bonwick 		 */
2701e14bb325SJeff Bonwick 		if (vd == rvd)
2702e14bb325SJeff Bonwick 			return;
2703e14bb325SJeff Bonwick 
2704e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
27058ad4d6ddSJeff Bonwick 
27068ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
27078ad4d6ddSJeff Bonwick 			return;
27088ad4d6ddSJeff Bonwick 
27098ad4d6ddSJeff Bonwick 		mutex_enter(&vd->vdev_stat_lock);
27108ad4d6ddSJeff Bonwick 
2711e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
271244ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
27133f9d6ad7SLin Ling 				dsl_scan_phys_t *scn_phys =
27143f9d6ad7SLin Ling 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
27153f9d6ad7SLin Ling 				uint64_t *processed = &scn_phys->scn_processed;
27163f9d6ad7SLin Ling 
27173f9d6ad7SLin Ling 				/* XXX cleanup? */
27183f9d6ad7SLin Ling 				if (vd->vdev_ops->vdev_op_leaf)
27193f9d6ad7SLin Ling 					atomic_add_64(processed, psize);
27203f9d6ad7SLin Ling 				vs->vs_scan_processed += psize;
27213f9d6ad7SLin Ling 			}
27223f9d6ad7SLin Ling 
27238ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
2724e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
2725fa9e4066Sahrens 		}
27268ad4d6ddSJeff Bonwick 
27278ad4d6ddSJeff Bonwick 		vs->vs_ops[type]++;
27288ad4d6ddSJeff Bonwick 		vs->vs_bytes[type] += psize;
27298ad4d6ddSJeff Bonwick 
27308ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
2731fa9e4066Sahrens 		return;
2732fa9e4066Sahrens 	}
2733fa9e4066Sahrens 
2734fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
2735fa9e4066Sahrens 		return;
2736fa9e4066Sahrens 
27378956713aSEric Schrock 	/*
27388956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
27398956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
27408956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
27418956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
27428956713aSEric Schrock 	 */
27438956713aSEric Schrock 	if (zio->io_error == EIO &&
27448956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
27458956713aSEric Schrock 		return;
27468956713aSEric Schrock 
27478f18d1faSGeorge Wilson 	/*
27488f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
27498f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
27508f18d1faSGeorge Wilson 	 * errors.
27518f18d1faSGeorge Wilson 	 */
27528f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
27538f18d1faSGeorge Wilson 		return;
27548f18d1faSGeorge Wilson 
2755e14bb325SJeff Bonwick 	mutex_enter(&vd->vdev_stat_lock);
2756b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2757e14bb325SJeff Bonwick 		if (zio->io_error == ECKSUM)
2758e14bb325SJeff Bonwick 			vs->vs_checksum_errors++;
2759e14bb325SJeff Bonwick 		else
2760e14bb325SJeff Bonwick 			vs->vs_read_errors++;
2761fa9e4066Sahrens 	}
2762b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2763e14bb325SJeff Bonwick 		vs->vs_write_errors++;
2764e14bb325SJeff Bonwick 	mutex_exit(&vd->vdev_stat_lock);
2765fa9e4066Sahrens 
27668ad4d6ddSJeff Bonwick 	if (type == ZIO_TYPE_WRITE && txg != 0 &&
27678ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
276844ecc532SGeorge Wilson 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
2769b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
27708ad4d6ddSJeff Bonwick 		/*
2771b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
2772b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
2773b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
2774b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
2775b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
2776b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
2777b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
2778b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
27798ad4d6ddSJeff Bonwick 		 *
27808ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
27818ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
27828ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
27838ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
27848ad4d6ddSJeff Bonwick 		 */
27858ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
27868ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
278744ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
27888ad4d6ddSJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
27898ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
27908ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2791b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
2792b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
2793b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2794b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
27958ad4d6ddSJeff Bonwick 			}
2796b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
27978ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2798fa9e4066Sahrens 				return;
27998ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
28008ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
28018ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2802fa9e4066Sahrens 		}
28038ad4d6ddSJeff Bonwick 		if (vd != rvd)
28048ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2805fa9e4066Sahrens 	}
2806fa9e4066Sahrens }
2807fa9e4066Sahrens 
2808fa9e4066Sahrens /*
2809b24ab676SJeff Bonwick  * Update the in-core space usage stats for this vdev, its metaslab class,
2810b24ab676SJeff Bonwick  * and the root vdev.
2811fa9e4066Sahrens  */
2812fa9e4066Sahrens void
2813b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2814b24ab676SJeff Bonwick     int64_t space_delta)
2815fa9e4066Sahrens {
281699653d4eSeschrock 	int64_t dspace_delta = space_delta;
28178654d025Sperrin 	spa_t *spa = vd->vdev_spa;
28188654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
2819b24ab676SJeff Bonwick 	metaslab_group_t *mg = vd->vdev_mg;
2820b24ab676SJeff Bonwick 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2821fa9e4066Sahrens 
28228654d025Sperrin 	ASSERT(vd == vd->vdev_top);
282399653d4eSeschrock 
28248654d025Sperrin 	/*
28258654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
28268654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
28278654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
28288654d025Sperrin 	 * childrens', thus not accurate enough for us.
28298654d025Sperrin 	 */
28308654d025Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2831e6ca193dSGeorge Wilson 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
28328654d025Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
28338654d025Sperrin 	    vd->vdev_deflate_ratio;
28348654d025Sperrin 
28358654d025Sperrin 	mutex_enter(&vd->vdev_stat_lock);
28368654d025Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
2837b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
28388654d025Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
28398654d025Sperrin 	mutex_exit(&vd->vdev_stat_lock);
28408654d025Sperrin 
2841b24ab676SJeff Bonwick 	if (mc == spa_normal_class(spa)) {
2842fa94a07fSbrendan 		mutex_enter(&rvd->vdev_stat_lock);
2843fa94a07fSbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
2844b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
2845fa94a07fSbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
2846fa94a07fSbrendan 		mutex_exit(&rvd->vdev_stat_lock);
2847fa94a07fSbrendan 	}
2848b24ab676SJeff Bonwick 
2849b24ab676SJeff Bonwick 	if (mc != NULL) {
2850b24ab676SJeff Bonwick 		ASSERT(rvd == vd->vdev_parent);
2851b24ab676SJeff Bonwick 		ASSERT(vd->vdev_ms_count != 0);
2852b24ab676SJeff Bonwick 
2853b24ab676SJeff Bonwick 		metaslab_class_space_update(mc,
2854b24ab676SJeff Bonwick 		    alloc_delta, defer_delta, space_delta, dspace_delta);
2855b24ab676SJeff Bonwick 	}
2856fa9e4066Sahrens }
2857fa9e4066Sahrens 
2858fa9e4066Sahrens /*
2859fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
2860fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
2861fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2862fa9e4066Sahrens  */
2863fa9e4066Sahrens void
2864fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
2865fa9e4066Sahrens {
2866fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2867fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2868fa9e4066Sahrens 	int c;
2869fa9e4066Sahrens 
2870f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
2871f9af39baSGeorge Wilson 
2872c5904d13Seschrock 	/*
28736809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
28746809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
2875c5904d13Seschrock 	 */
2876c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
2877c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
2878c5904d13Seschrock 		nvlist_t **aux;
2879c5904d13Seschrock 		uint_t naux;
2880c5904d13Seschrock 
2881c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
2882c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
2883c5904d13Seschrock 				break;
2884c5904d13Seschrock 		}
2885c5904d13Seschrock 
2886e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
2887e14bb325SJeff Bonwick 			/*
2888e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
2889e14bb325SJeff Bonwick 			 */
2890e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
2891e14bb325SJeff Bonwick 			return;
2892e14bb325SJeff Bonwick 		}
2893e14bb325SJeff Bonwick 
2894c5904d13Seschrock 		sav->sav_sync = B_TRUE;
2895c5904d13Seschrock 
28966809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
28976809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
28986809eb4eSEric Schrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
28996809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
29006809eb4eSEric Schrock 		}
2901c5904d13Seschrock 
2902c5904d13Seschrock 		ASSERT(c < naux);
2903c5904d13Seschrock 
2904c5904d13Seschrock 		/*
2905c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
2906c5904d13Seschrock 		 * sketchy, but it will work.
2907c5904d13Seschrock 		 */
2908c5904d13Seschrock 		nvlist_free(aux[c]);
29093f9d6ad7SLin Ling 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
2910c5904d13Seschrock 
2911c5904d13Seschrock 		return;
2912c5904d13Seschrock 	}
2913c5904d13Seschrock 
29145dabedeeSbonwick 	/*
2915e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
2916e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
2917e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
29185dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
29195dabedeeSbonwick 	 */
2920e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2921e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2922e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
29235dabedeeSbonwick 
2924fa9e4066Sahrens 	if (vd == rvd) {
2925fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
2926fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
2927fa9e4066Sahrens 	} else {
2928fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
2929fa9e4066Sahrens 
293088ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
293188ecc943SGeorge Wilson 		    !vd->vdev_ishole)
2932e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
2933fa9e4066Sahrens 	}
2934fa9e4066Sahrens }
2935fa9e4066Sahrens 
2936fa9e4066Sahrens void
2937fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
2938fa9e4066Sahrens {
29395dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
29405dabedeeSbonwick 
2941e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2942e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2943e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
29445dabedeeSbonwick 
2945e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
2946e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
2947e14bb325SJeff Bonwick }
2948e14bb325SJeff Bonwick 
2949e14bb325SJeff Bonwick /*
2950e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
2951e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
2952e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
2953e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
2954e14bb325SJeff Bonwick  */
2955e14bb325SJeff Bonwick void
2956e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
2957e14bb325SJeff Bonwick {
2958e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
2959e14bb325SJeff Bonwick 
2960f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
2961e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
2962e14bb325SJeff Bonwick 
2963e14bb325SJeff Bonwick 	/*
2964e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
2965e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
2966e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
2967e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
2968e14bb325SJeff Bonwick 	 */
2969e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2970e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2971e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2972e14bb325SJeff Bonwick 
2973b24ab676SJeff Bonwick 	if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
2974e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
2975e14bb325SJeff Bonwick }
2976e14bb325SJeff Bonwick 
2977e14bb325SJeff Bonwick void
2978e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
2979e14bb325SJeff Bonwick {
2980e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
2981e14bb325SJeff Bonwick 
2982e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2983e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2984e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
2985e14bb325SJeff Bonwick 
2986e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
2987e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
2988fa9e4066Sahrens }
2989fa9e4066Sahrens 
299032b87932Sek /*
299132b87932Sek  * Propagate vdev state up from children to parent.
299232b87932Sek  */
299344cd46caSbillm void
299444cd46caSbillm vdev_propagate_state(vdev_t *vd)
299544cd46caSbillm {
29968ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
29978ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
299844cd46caSbillm 	int degraded = 0, faulted = 0;
299944cd46caSbillm 	int corrupted = 0;
300044cd46caSbillm 	vdev_t *child;
300144cd46caSbillm 
30023d7072f8Seschrock 	if (vd->vdev_children > 0) {
3003573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
30043d7072f8Seschrock 			child = vd->vdev_child[c];
300551ece835Seschrock 
300688ecc943SGeorge Wilson 			/*
300788ecc943SGeorge Wilson 			 * Don't factor holes into the decision.
300888ecc943SGeorge Wilson 			 */
300988ecc943SGeorge Wilson 			if (child->vdev_ishole)
301088ecc943SGeorge Wilson 				continue;
301188ecc943SGeorge Wilson 
3012e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
30138ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
301451ece835Seschrock 				/*
301551ece835Seschrock 				 * Root special: if there is a top-level log
301651ece835Seschrock 				 * device, treat the root vdev as if it were
301751ece835Seschrock 				 * degraded.
301851ece835Seschrock 				 */
301951ece835Seschrock 				if (child->vdev_islog && vd == rvd)
302051ece835Seschrock 					degraded++;
302151ece835Seschrock 				else
302251ece835Seschrock 					faulted++;
302351ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
30243d7072f8Seschrock 				degraded++;
302551ece835Seschrock 			}
302644cd46caSbillm 
30273d7072f8Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
30283d7072f8Seschrock 				corrupted++;
30293d7072f8Seschrock 		}
303044cd46caSbillm 
30313d7072f8Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
30323d7072f8Seschrock 
30333d7072f8Seschrock 		/*
3034e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
30353d7072f8Seschrock 		 * opened due to corrupted metadata, then propagate the root
30363d7072f8Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
30373d7072f8Seschrock 		 * replicas'.
30383d7072f8Seschrock 		 */
30393d7072f8Seschrock 		if (corrupted && vd == rvd &&
30403d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
30413d7072f8Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
30423d7072f8Seschrock 			    VDEV_AUX_CORRUPT_DATA);
30433d7072f8Seschrock 	}
30443d7072f8Seschrock 
304551ece835Seschrock 	if (vd->vdev_parent)
30463d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
304744cd46caSbillm }
304844cd46caSbillm 
3049fa9e4066Sahrens /*
3050ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
3051ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
3052ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
3053ea8dc4b6Seschrock  *
3054ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
3055ea8dc4b6Seschrock  * generated.
3056fa9e4066Sahrens  */
3057fa9e4066Sahrens void
3058ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3059fa9e4066Sahrens {
3060560e6e96Seschrock 	uint64_t save_state;
3061c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
3062ea8dc4b6Seschrock 
3063ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
3064ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
3065fa9e4066Sahrens 		return;
3066ea8dc4b6Seschrock 	}
3067ea8dc4b6Seschrock 
3068560e6e96Seschrock 	save_state = vd->vdev_state;
3069fa9e4066Sahrens 
3070fa9e4066Sahrens 	vd->vdev_state = state;
3071fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
3072fa9e4066Sahrens 
30733d7072f8Seschrock 	/*
30743d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
307598d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
307698d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
307798d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
307898d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
307998d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
308098d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
308198d1cbfeSGeorge Wilson 	 * children.
308298d1cbfeSGeorge Wilson 	 */
308398d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
308498d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
30853d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
30863d7072f8Seschrock 
3087069f55e2SEric Schrock 	/*
3088069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
3089069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
3090069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
3091069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
3092069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
3093069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
3094069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
3095069f55e2SEric Schrock 	 */
3096069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3097069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
3098069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
3099069f55e2SEric Schrock 
31003d7072f8Seschrock 	if (vd->vdev_removed &&
31013d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
31023d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
31033d7072f8Seschrock 		/*
31043d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
31053d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
31063d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
31073d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
31083d7072f8Seschrock 		 * it is one of our special test online cases, which is only
31093d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
31103d7072f8Seschrock 		 * fault.
31113d7072f8Seschrock 		 */
31123d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
31133d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
31143d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
31153d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
31163d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
3117ea8dc4b6Seschrock 		/*
3118cb04b873SMark J Musante 		 * If we fail to open a vdev during an import or recovery, we
3119cb04b873SMark J Musante 		 * mark it as "not available", which signifies that it was
3120cb04b873SMark J Musante 		 * never there to begin with.  Failure to open such a device
3121cb04b873SMark J Musante 		 * is not considered an error.
3122ea8dc4b6Seschrock 		 */
3123cb04b873SMark J Musante 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3124cb04b873SMark J Musante 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3125560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
3126560e6e96Seschrock 			vd->vdev_not_present = 1;
3127560e6e96Seschrock 
3128560e6e96Seschrock 		/*
3129560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
3130560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3131560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
3132560e6e96Seschrock 		 * want to post the ereport if the device was already in the
3133560e6e96Seschrock 		 * CANT_OPEN state beforehand.
31343d7072f8Seschrock 		 *
31353d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
31363d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
31373d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
31383d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
31393d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
31403d7072f8Seschrock 		 * about to replace the device, or attempt an online with
31413d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
3142560e6e96Seschrock 		 */
31433d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
31443d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
3145c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
3146ea8dc4b6Seschrock 			const char *class;
3147ea8dc4b6Seschrock 
3148ea8dc4b6Seschrock 			switch (aux) {
3149ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
3150ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3151ea8dc4b6Seschrock 				break;
3152ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
3153ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3154ea8dc4b6Seschrock 				break;
3155ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
3156ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3157ea8dc4b6Seschrock 				break;
3158ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
3159ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3160ea8dc4b6Seschrock 				break;
3161ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
3162ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3163ea8dc4b6Seschrock 				break;
3164ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
3165ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3166ea8dc4b6Seschrock 				break;
3167ea8dc4b6Seschrock 			default:
3168ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3169ea8dc4b6Seschrock 			}
3170ea8dc4b6Seschrock 
3171c5904d13Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3172ea8dc4b6Seschrock 		}
3173ea8dc4b6Seschrock 
31743d7072f8Seschrock 		/* Erase any notion of persistent removed state */
31753d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
31763d7072f8Seschrock 	} else {
31773d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
31783d7072f8Seschrock 	}
3179ea8dc4b6Seschrock 
31808b33d774STim Haley 	if (!isopen && vd->vdev_parent)
31818b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
3182fa9e4066Sahrens }
318315e6edf1Sgw 
318415e6edf1Sgw /*
318515e6edf1Sgw  * Check the vdev configuration to ensure that it's capable of supporting
318615e6edf1Sgw  * a root pool. Currently, we do not support RAID-Z or partial configuration.
318715e6edf1Sgw  * In addition, only a single top-level vdev is allowed and none of the leaves
318815e6edf1Sgw  * can be wholedisks.
318915e6edf1Sgw  */
319015e6edf1Sgw boolean_t
319115e6edf1Sgw vdev_is_bootable(vdev_t *vd)
319215e6edf1Sgw {
319315e6edf1Sgw 	if (!vd->vdev_ops->vdev_op_leaf) {
319415e6edf1Sgw 		char *vdev_type = vd->vdev_ops->vdev_op_type;
319515e6edf1Sgw 
319615e6edf1Sgw 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
319715e6edf1Sgw 		    vd->vdev_children > 1) {
319815e6edf1Sgw 			return (B_FALSE);
319915e6edf1Sgw 		} else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
320015e6edf1Sgw 		    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
320115e6edf1Sgw 			return (B_FALSE);
320215e6edf1Sgw 		}
320315e6edf1Sgw 	} else if (vd->vdev_wholedisk == 1) {
320415e6edf1Sgw 		return (B_FALSE);
320515e6edf1Sgw 	}
320615e6edf1Sgw 
3207573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
320815e6edf1Sgw 		if (!vdev_is_bootable(vd->vdev_child[c]))
320915e6edf1Sgw 			return (B_FALSE);
321015e6edf1Sgw 	}
321115e6edf1Sgw 	return (B_TRUE);
321215e6edf1Sgw }
3213e6ca193dSGeorge Wilson 
321488ecc943SGeorge Wilson /*
321588ecc943SGeorge Wilson  * Load the state from the original vdev tree (ovd) which
321688ecc943SGeorge Wilson  * we've retrieved from the MOS config object. If the original
32174b964adaSGeorge Wilson  * vdev was offline or faulted then we transfer that state to the
32184b964adaSGeorge Wilson  * device in the current vdev tree (nvd).
321988ecc943SGeorge Wilson  */
3220e6ca193dSGeorge Wilson void
322188ecc943SGeorge Wilson vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3222e6ca193dSGeorge Wilson {
322388ecc943SGeorge Wilson 	spa_t *spa = nvd->vdev_spa;
3224e6ca193dSGeorge Wilson 
32254b964adaSGeorge Wilson 	ASSERT(nvd->vdev_top->vdev_islog);
322688ecc943SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
322788ecc943SGeorge Wilson 	ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3228e6ca193dSGeorge Wilson 
322988ecc943SGeorge Wilson 	for (int c = 0; c < nvd->vdev_children; c++)
323088ecc943SGeorge Wilson 		vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3231e6ca193dSGeorge Wilson 
32324b964adaSGeorge Wilson 	if (nvd->vdev_ops->vdev_op_leaf) {
3233e6ca193dSGeorge Wilson 		/*
32344b964adaSGeorge Wilson 		 * Restore the persistent vdev state
3235e6ca193dSGeorge Wilson 		 */
323688ecc943SGeorge Wilson 		nvd->vdev_offline = ovd->vdev_offline;
32374b964adaSGeorge Wilson 		nvd->vdev_faulted = ovd->vdev_faulted;
32384b964adaSGeorge Wilson 		nvd->vdev_degraded = ovd->vdev_degraded;
32394b964adaSGeorge Wilson 		nvd->vdev_removed = ovd->vdev_removed;
3240e6ca193dSGeorge Wilson 	}
3241e6ca193dSGeorge Wilson }
3242573ca77eSGeorge Wilson 
32434b964adaSGeorge Wilson /*
32444b964adaSGeorge Wilson  * Determine if a log device has valid content.  If the vdev was
32454b964adaSGeorge Wilson  * removed or faulted in the MOS config then we know that
32464b964adaSGeorge Wilson  * the content on the log device has already been written to the pool.
32474b964adaSGeorge Wilson  */
32484b964adaSGeorge Wilson boolean_t
32494b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd)
32504b964adaSGeorge Wilson {
32514b964adaSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
32524b964adaSGeorge Wilson 	    !vd->vdev_removed)
32534b964adaSGeorge Wilson 		return (B_TRUE);
32544b964adaSGeorge Wilson 
32554b964adaSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
32564b964adaSGeorge Wilson 		if (vdev_log_state_valid(vd->vdev_child[c]))
32574b964adaSGeorge Wilson 			return (B_TRUE);
32584b964adaSGeorge Wilson 
32594b964adaSGeorge Wilson 	return (B_FALSE);
32604b964adaSGeorge Wilson }
32614b964adaSGeorge Wilson 
3262573ca77eSGeorge Wilson /*
3263573ca77eSGeorge Wilson  * Expand a vdev if possible.
3264573ca77eSGeorge Wilson  */
3265573ca77eSGeorge Wilson void
3266573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
3267573ca77eSGeorge Wilson {
3268573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
3269573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3270573ca77eSGeorge Wilson 
3271573ca77eSGeorge Wilson 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3272573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
3273573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
3274573ca77eSGeorge Wilson 	}
3275573ca77eSGeorge Wilson }
32761195e687SMark J Musante 
32771195e687SMark J Musante /*
32781195e687SMark J Musante  * Split a vdev.
32791195e687SMark J Musante  */
32801195e687SMark J Musante void
32811195e687SMark J Musante vdev_split(vdev_t *vd)
32821195e687SMark J Musante {
32831195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
32841195e687SMark J Musante 
32851195e687SMark J Musante 	vdev_remove_child(pvd, vd);
32861195e687SMark J Musante 	vdev_compact_children(pvd);
32871195e687SMark J Musante 
32881195e687SMark J Musante 	cvd = pvd->vdev_child[0];
32891195e687SMark J Musante 	if (pvd->vdev_children == 1) {
32901195e687SMark J Musante 		vdev_remove_parent(cvd);
32911195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
32921195e687SMark J Musante 	}
32931195e687SMark J Musante 	vdev_propagate_state(cvd);
32941195e687SMark J Musante }
3295283b8460SGeorge.Wilson 
3296283b8460SGeorge.Wilson void
3297283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd)
3298283b8460SGeorge.Wilson {
3299283b8460SGeorge.Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
3300283b8460SGeorge.Wilson 		vdev_t *cvd = vd->vdev_child[c];
3301283b8460SGeorge.Wilson 
3302283b8460SGeorge.Wilson 		vdev_deadman(cvd);
3303283b8460SGeorge.Wilson 	}
3304283b8460SGeorge.Wilson 
3305283b8460SGeorge.Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3306283b8460SGeorge.Wilson 		vdev_queue_t *vq = &vd->vdev_queue;
3307283b8460SGeorge.Wilson 
3308283b8460SGeorge.Wilson 		mutex_enter(&vq->vq_lock);
330969962b56SMatthew Ahrens 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
3310283b8460SGeorge.Wilson 			spa_t *spa = vd->vdev_spa;
3311283b8460SGeorge.Wilson 			zio_t *fio;
3312283b8460SGeorge.Wilson 			uint64_t delta;
3313283b8460SGeorge.Wilson 
3314283b8460SGeorge.Wilson 			/*
3315283b8460SGeorge.Wilson 			 * Look at the head of all the pending queues,
3316283b8460SGeorge.Wilson 			 * if any I/O has been outstanding for longer than
3317283b8460SGeorge.Wilson 			 * the spa_deadman_synctime we panic the system.
3318283b8460SGeorge.Wilson 			 */
331969962b56SMatthew Ahrens 			fio = avl_first(&vq->vq_active_tree);
3320c55e05cbSMatthew Ahrens 			delta = gethrtime() - fio->io_timestamp;
3321c55e05cbSMatthew Ahrens 			if (delta > spa_deadman_synctime(spa)) {
3322c55e05cbSMatthew Ahrens 				zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3323c55e05cbSMatthew Ahrens 				    "delta %lluns, last io %lluns",
3324283b8460SGeorge.Wilson 				    fio->io_timestamp, delta,
3325283b8460SGeorge.Wilson 				    vq->vq_io_complete_ts);
3326283b8460SGeorge.Wilson 				fm_panic("I/O to pool '%s' appears to be "
3327283b8460SGeorge.Wilson 				    "hung.", spa_name(spa));
3328283b8460SGeorge.Wilson 			}
3329283b8460SGeorge.Wilson 		}
3330283b8460SGeorge.Wilson 		mutex_exit(&vq->vq_lock);
3331283b8460SGeorge.Wilson 	}
3332283b8460SGeorge.Wilson }
3333