xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 8671400134a11c848244896ca51a7db4d0f69da4)
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.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
255f368aefSYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
26c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
27c8811bd3SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
28ce1577b0SDave Eddy  * Copyright 2017 Joyent, Inc.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/zfs_context.h>
32ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
355cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h>
36fa9e4066Sahrens #include <sys/dmu.h>
37fa9e4066Sahrens #include <sys/dmu_tx.h>
385cabbc6bSPrashanth Sreenivasa #include <sys/dsl_dir.h>
39fa9e4066Sahrens #include <sys/vdev_impl.h>
40fa9e4066Sahrens #include <sys/uberblock_impl.h>
41fa9e4066Sahrens #include <sys/metaslab.h>
42fa9e4066Sahrens #include <sys/metaslab_impl.h>
43fa9e4066Sahrens #include <sys/space_map.h>
440713e232SGeorge Wilson #include <sys/space_reftree.h>
45fa9e4066Sahrens #include <sys/zio.h>
46fa9e4066Sahrens #include <sys/zap.h>
47fa9e4066Sahrens #include <sys/fs/zfs.h>
48c5904d13Seschrock #include <sys/arc.h>
49e6ca193dSGeorge Wilson #include <sys/zil.h>
503f9d6ad7SLin Ling #include <sys/dsl_scan.h>
51770499e1SDan Kimmel #include <sys/abd.h>
52fa9e4066Sahrens 
53fa9e4066Sahrens /*
54fa9e4066Sahrens  * Virtual device management.
55fa9e4066Sahrens  */
56fa9e4066Sahrens 
57fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = {
58fa9e4066Sahrens 	&vdev_root_ops,
59fa9e4066Sahrens 	&vdev_raidz_ops,
60fa9e4066Sahrens 	&vdev_mirror_ops,
61fa9e4066Sahrens 	&vdev_replacing_ops,
6299653d4eSeschrock 	&vdev_spare_ops,
63fa9e4066Sahrens 	&vdev_disk_ops,
64fa9e4066Sahrens 	&vdev_file_ops,
65fa9e4066Sahrens 	&vdev_missing_ops,
6688ecc943SGeorge Wilson 	&vdev_hole_ops,
675cabbc6bSPrashanth Sreenivasa 	&vdev_indirect_ops,
68fa9e4066Sahrens 	NULL
69fa9e4066Sahrens };
70fa9e4066Sahrens 
71088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
72088f3894Sahrens int zfs_scrub_limit = 10;
7305b2b3b8Smishra 
74*86714001SSerapheim Dimitropoulos /* maximum number of metaslabs per top-level vdev */
75*86714001SSerapheim Dimitropoulos int vdev_max_ms_count = 200;
76*86714001SSerapheim Dimitropoulos 
77*86714001SSerapheim Dimitropoulos /* minimum amount of metaslabs per top-level vdev */
78*86714001SSerapheim Dimitropoulos int vdev_min_ms_count = 16;
79*86714001SSerapheim Dimitropoulos 
80*86714001SSerapheim Dimitropoulos /* see comment in vdev_metaslab_set_size() */
81*86714001SSerapheim Dimitropoulos int vdev_default_ms_shift = 29;
82*86714001SSerapheim Dimitropoulos 
83*86714001SSerapheim Dimitropoulos boolean_t vdev_validate_skip = B_FALSE;
84*86714001SSerapheim Dimitropoulos 
85bf3e216cSMatthew Ahrens /*
86*86714001SSerapheim Dimitropoulos  * Since the DTL space map of a vdev is not expected to have a lot of
87*86714001SSerapheim Dimitropoulos  * entries, we default its block size to 4K.
88bf3e216cSMatthew Ahrens  */
89*86714001SSerapheim Dimitropoulos int vdev_dtl_sm_blksz = (1 << 12);
90bf3e216cSMatthew Ahrens 
91*86714001SSerapheim Dimitropoulos /*
92*86714001SSerapheim Dimitropoulos  * vdev-wide space maps that have lots of entries written to them at
93*86714001SSerapheim Dimitropoulos  * the end of each transaction can benefit from a higher I/O bandwidth
94*86714001SSerapheim Dimitropoulos  * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
95*86714001SSerapheim Dimitropoulos  */
96*86714001SSerapheim Dimitropoulos int vdev_standard_sm_blksz = (1 << 17);
976f793812SPavel Zakharov 
983ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
993ee8c80cSPavel Zakharov void
1003ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
1013ee8c80cSPavel Zakharov {
1023ee8c80cSPavel Zakharov 	va_list adx;
1033ee8c80cSPavel Zakharov 	char buf[256];
1043ee8c80cSPavel Zakharov 
1053ee8c80cSPavel Zakharov 	va_start(adx, fmt);
1063ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
1073ee8c80cSPavel Zakharov 	va_end(adx);
1083ee8c80cSPavel Zakharov 
1093ee8c80cSPavel Zakharov 	if (vd->vdev_path != NULL) {
1103ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
1113ee8c80cSPavel Zakharov 		    vd->vdev_path, buf);
1123ee8c80cSPavel Zakharov 	} else {
1133ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
1143ee8c80cSPavel Zakharov 		    vd->vdev_ops->vdev_op_type,
1153ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_id,
1163ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_guid, buf);
1173ee8c80cSPavel Zakharov 	}
1183ee8c80cSPavel Zakharov }
1193ee8c80cSPavel Zakharov 
1206f793812SPavel Zakharov void
1216f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
1226f793812SPavel Zakharov {
1236f793812SPavel Zakharov 	char state[20];
1246f793812SPavel Zakharov 
1256f793812SPavel Zakharov 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
1266f793812SPavel Zakharov 		zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
1276f793812SPavel Zakharov 		    vd->vdev_ops->vdev_op_type);
1286f793812SPavel Zakharov 		return;
1296f793812SPavel Zakharov 	}
1306f793812SPavel Zakharov 
1316f793812SPavel Zakharov 	switch (vd->vdev_state) {
1326f793812SPavel Zakharov 	case VDEV_STATE_UNKNOWN:
1336f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "unknown");
1346f793812SPavel Zakharov 		break;
1356f793812SPavel Zakharov 	case VDEV_STATE_CLOSED:
1366f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "closed");
1376f793812SPavel Zakharov 		break;
1386f793812SPavel Zakharov 	case VDEV_STATE_OFFLINE:
1396f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "offline");
1406f793812SPavel Zakharov 		break;
1416f793812SPavel Zakharov 	case VDEV_STATE_REMOVED:
1426f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "removed");
1436f793812SPavel Zakharov 		break;
1446f793812SPavel Zakharov 	case VDEV_STATE_CANT_OPEN:
1456f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "can't open");
1466f793812SPavel Zakharov 		break;
1476f793812SPavel Zakharov 	case VDEV_STATE_FAULTED:
1486f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "faulted");
1496f793812SPavel Zakharov 		break;
1506f793812SPavel Zakharov 	case VDEV_STATE_DEGRADED:
1516f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "degraded");
1526f793812SPavel Zakharov 		break;
1536f793812SPavel Zakharov 	case VDEV_STATE_HEALTHY:
1546f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "healthy");
1556f793812SPavel Zakharov 		break;
1566f793812SPavel Zakharov 	default:
1576f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "<state %u>",
1586f793812SPavel Zakharov 		    (uint_t)vd->vdev_state);
1596f793812SPavel Zakharov 	}
1606f793812SPavel Zakharov 
1616f793812SPavel Zakharov 	zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
1626f793812SPavel Zakharov 	    "", vd->vdev_id, vd->vdev_ops->vdev_op_type,
1636f793812SPavel Zakharov 	    vd->vdev_islog ? " (log)" : "",
1646f793812SPavel Zakharov 	    (u_longlong_t)vd->vdev_guid,
1656f793812SPavel Zakharov 	    vd->vdev_path ? vd->vdev_path : "N/A", state);
1666f793812SPavel Zakharov 
1676f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++)
1686f793812SPavel Zakharov 		vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
1696f793812SPavel Zakharov }
1706f793812SPavel Zakharov 
171fa9e4066Sahrens /*
172fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
173fa9e4066Sahrens  */
174fa9e4066Sahrens static vdev_ops_t *
175fa9e4066Sahrens vdev_getops(const char *type)
176fa9e4066Sahrens {
177fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
178fa9e4066Sahrens 
179fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
180fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
181fa9e4066Sahrens 			break;
182fa9e4066Sahrens 
183fa9e4066Sahrens 	return (ops);
184fa9e4066Sahrens }
185fa9e4066Sahrens 
186fa9e4066Sahrens /*
187fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
188fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
189fa9e4066Sahrens  */
190fa9e4066Sahrens uint64_t
191fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
192fa9e4066Sahrens {
193ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
194fa9e4066Sahrens 	uint64_t csize;
195fa9e4066Sahrens 
196573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
197fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
198fa9e4066Sahrens 		asize = MAX(asize, csize);
199fa9e4066Sahrens 	}
200fa9e4066Sahrens 
201fa9e4066Sahrens 	return (asize);
202fa9e4066Sahrens }
203fa9e4066Sahrens 
2042a79c5feSlling /*
205573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
206573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
207573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
208573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
2092a79c5feSlling  */
2102a79c5feSlling uint64_t
211573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
2122a79c5feSlling {
213573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
2142a79c5feSlling 
215573ca77eSGeorge Wilson 	/*
2164263d13fSGeorge Wilson 	 * If our parent is NULL (inactive spare or cache) or is the root,
217573ca77eSGeorge Wilson 	 * just return our own asize.
218573ca77eSGeorge Wilson 	 */
219573ca77eSGeorge Wilson 	if (pvd == NULL)
220573ca77eSGeorge Wilson 		return (vd->vdev_asize);
2212a79c5feSlling 
2222a79c5feSlling 	/*
223573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
224573ca77eSGeorge Wilson 	 * to the nearest metaslab.
2252a79c5feSlling 	 */
226573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
227573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
2282a79c5feSlling 
229573ca77eSGeorge Wilson 	/*
230573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
231573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
232573ca77eSGeorge Wilson 	 */
233573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
234c040c10cSSteven Hartland 		return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
235c040c10cSSteven Hartland 		    pvd->vdev_children);
2362a79c5feSlling 
237573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
238573ca77eSGeorge Wilson }
2392a79c5feSlling 
240573ca77eSGeorge Wilson void
241573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
242573ca77eSGeorge Wilson {
243573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
244573ca77eSGeorge Wilson 
245573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
246573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
2472a79c5feSlling }
2482a79c5feSlling 
249fa9e4066Sahrens vdev_t *
250fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
251fa9e4066Sahrens {
252fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
253fa9e4066Sahrens 
254e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
255e05725b1Sbonwick 
256088f3894Sahrens 	if (vdev < rvd->vdev_children) {
257088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
258fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
259088f3894Sahrens 	}
260fa9e4066Sahrens 
261fa9e4066Sahrens 	return (NULL);
262fa9e4066Sahrens }
263fa9e4066Sahrens 
264fa9e4066Sahrens vdev_t *
265fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
266fa9e4066Sahrens {
267fa9e4066Sahrens 	vdev_t *mvd;
268fa9e4066Sahrens 
2690e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
270fa9e4066Sahrens 		return (vd);
271fa9e4066Sahrens 
272573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
273fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
274fa9e4066Sahrens 		    NULL)
275fa9e4066Sahrens 			return (mvd);
276fa9e4066Sahrens 
277fa9e4066Sahrens 	return (NULL);
278fa9e4066Sahrens }
279fa9e4066Sahrens 
28012380e1eSArne Jansen static int
28112380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd)
28212380e1eSArne Jansen {
28312380e1eSArne Jansen 	int n = 0;
28412380e1eSArne Jansen 
28512380e1eSArne Jansen 	if (vd->vdev_ops->vdev_op_leaf)
28612380e1eSArne Jansen 		return (1);
28712380e1eSArne Jansen 
28812380e1eSArne Jansen 	for (int c = 0; c < vd->vdev_children; c++)
28912380e1eSArne Jansen 		n += vdev_count_leaves_impl(vd->vdev_child[c]);
29012380e1eSArne Jansen 
29112380e1eSArne Jansen 	return (n);
29212380e1eSArne Jansen }
29312380e1eSArne Jansen 
29412380e1eSArne Jansen int
29512380e1eSArne Jansen vdev_count_leaves(spa_t *spa)
29612380e1eSArne Jansen {
29712380e1eSArne Jansen 	return (vdev_count_leaves_impl(spa->spa_root_vdev));
29812380e1eSArne Jansen }
29912380e1eSArne Jansen 
300fa9e4066Sahrens void
301fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
302fa9e4066Sahrens {
303fa9e4066Sahrens 	size_t oldsize, newsize;
304fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
305fa9e4066Sahrens 	vdev_t **newchild;
30681cd5c55SMatthew Ahrens 	spa_t *spa = cvd->vdev_spa;
307fa9e4066Sahrens 
30881cd5c55SMatthew Ahrens 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
309fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
310fa9e4066Sahrens 
311fa9e4066Sahrens 	cvd->vdev_parent = pvd;
312fa9e4066Sahrens 
313fa9e4066Sahrens 	if (pvd == NULL)
314fa9e4066Sahrens 		return;
315fa9e4066Sahrens 
316fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
317fa9e4066Sahrens 
318fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
319fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
320fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
321fa9e4066Sahrens 
322fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
323fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
324fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
325fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
326fa9e4066Sahrens 	}
327fa9e4066Sahrens 
328fa9e4066Sahrens 	pvd->vdev_child = newchild;
329fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
330fa9e4066Sahrens 
331fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
332fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
333fa9e4066Sahrens 
334fa9e4066Sahrens 	/*
335fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
336fa9e4066Sahrens 	 */
337fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
338fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
339fa9e4066Sahrens }
340fa9e4066Sahrens 
341fa9e4066Sahrens void
342fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
343fa9e4066Sahrens {
344fa9e4066Sahrens 	int c;
345fa9e4066Sahrens 	uint_t id = cvd->vdev_id;
346fa9e4066Sahrens 
347fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == pvd);
348fa9e4066Sahrens 
349fa9e4066Sahrens 	if (pvd == NULL)
350fa9e4066Sahrens 		return;
351fa9e4066Sahrens 
352fa9e4066Sahrens 	ASSERT(id < pvd->vdev_children);
353fa9e4066Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
354fa9e4066Sahrens 
355fa9e4066Sahrens 	pvd->vdev_child[id] = NULL;
356fa9e4066Sahrens 	cvd->vdev_parent = NULL;
357fa9e4066Sahrens 
358fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
359fa9e4066Sahrens 		if (pvd->vdev_child[c])
360fa9e4066Sahrens 			break;
361fa9e4066Sahrens 
362fa9e4066Sahrens 	if (c == pvd->vdev_children) {
363fa9e4066Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
364fa9e4066Sahrens 		pvd->vdev_child = NULL;
365fa9e4066Sahrens 		pvd->vdev_children = 0;
366fa9e4066Sahrens 	}
367fa9e4066Sahrens 
368fa9e4066Sahrens 	/*
369fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
370fa9e4066Sahrens 	 */
371fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
372fa9e4066Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
373fa9e4066Sahrens }
374fa9e4066Sahrens 
375fa9e4066Sahrens /*
376fa9e4066Sahrens  * Remove any holes in the child array.
377fa9e4066Sahrens  */
378fa9e4066Sahrens void
379fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
380fa9e4066Sahrens {
381fa9e4066Sahrens 	vdev_t **newchild, *cvd;
382fa9e4066Sahrens 	int oldc = pvd->vdev_children;
383573ca77eSGeorge Wilson 	int newc;
384fa9e4066Sahrens 
385e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
386fa9e4066Sahrens 
387573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
388fa9e4066Sahrens 		if (pvd->vdev_child[c])
389fa9e4066Sahrens 			newc++;
390fa9e4066Sahrens 
391fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
392fa9e4066Sahrens 
393573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
394fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
395fa9e4066Sahrens 			newchild[newc] = cvd;
396fa9e4066Sahrens 			cvd->vdev_id = newc++;
397fa9e4066Sahrens 		}
398fa9e4066Sahrens 	}
399fa9e4066Sahrens 
400fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
401fa9e4066Sahrens 	pvd->vdev_child = newchild;
402fa9e4066Sahrens 	pvd->vdev_children = newc;
403fa9e4066Sahrens }
404fa9e4066Sahrens 
405fa9e4066Sahrens /*
406fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
407fa9e4066Sahrens  */
40888ecc943SGeorge Wilson vdev_t *
409fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
410fa9e4066Sahrens {
411fa9e4066Sahrens 	vdev_t *vd;
4125cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
413fa9e4066Sahrens 
414fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
4155cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
416fa9e4066Sahrens 
4170e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
4180e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
4190e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
420e9103aaeSGarrett D'Amore 		spa->spa_load_guid = spa_generate_guid(NULL);
4210e34b6a7Sbonwick 	}
4220e34b6a7Sbonwick 
42388ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
4240e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
4250e34b6a7Sbonwick 			/*
4260e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
4270e34b6a7Sbonwick 			 * which must be unique among all pools.
4280e34b6a7Sbonwick 			 */
4291195e687SMark J Musante 			guid = spa_generate_guid(NULL);
4300e34b6a7Sbonwick 		} else {
4310e34b6a7Sbonwick 			/*
4320e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
4330e34b6a7Sbonwick 			 */
4341195e687SMark J Musante 			guid = spa_generate_guid(spa);
4350e34b6a7Sbonwick 		}
4360e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
4370e34b6a7Sbonwick 	}
4380e34b6a7Sbonwick 
439fa9e4066Sahrens 	vd->vdev_spa = spa;
440fa9e4066Sahrens 	vd->vdev_id = id;
441fa9e4066Sahrens 	vd->vdev_guid = guid;
442fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
443fa9e4066Sahrens 	vd->vdev_ops = ops;
444fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
44588ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
4465cabbc6bSPrashanth Sreenivasa 	vic->vic_prev_indirect_vdev = UINT64_MAX;
4475cabbc6bSPrashanth Sreenivasa 
4485cabbc6bSPrashanth Sreenivasa 	rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
4495cabbc6bSPrashanth Sreenivasa 	mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
4505cabbc6bSPrashanth Sreenivasa 	vd->vdev_obsolete_segments = range_tree_create(NULL, NULL);
451fa9e4066Sahrens 
452fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
4535ad82045Snd 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
454e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
4550f7643c7SGeorge Wilson 	mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
4568ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
4575cabbc6bSPrashanth Sreenivasa 		vd->vdev_dtl[t] = range_tree_create(NULL, NULL);
4588ad4d6ddSJeff Bonwick 	}
459b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_ms_list, spa,
460fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
461b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_dtl_list, spa,
462fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
463fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
4643d7072f8Seschrock 	vdev_queue_init(vd);
4653d7072f8Seschrock 	vdev_cache_init(vd);
466fa9e4066Sahrens 
467fa9e4066Sahrens 	return (vd);
468fa9e4066Sahrens }
469fa9e4066Sahrens 
470fa9e4066Sahrens /*
471fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
472fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
473fa9e4066Sahrens  * different for each case.
474fa9e4066Sahrens  */
47599653d4eSeschrock int
47699653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
47799653d4eSeschrock     int alloctype)
478fa9e4066Sahrens {
479fa9e4066Sahrens 	vdev_ops_t *ops;
480fa9e4066Sahrens 	char *type;
4818654d025Sperrin 	uint64_t guid = 0, islog, nparity;
482fa9e4066Sahrens 	vdev_t *vd;
4835cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
484fa9e4066Sahrens 
485e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
486fa9e4066Sahrens 
487fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
488be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
489fa9e4066Sahrens 
490fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
491be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
492fa9e4066Sahrens 
493fa9e4066Sahrens 	/*
494fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
495fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
496fa9e4066Sahrens 	 */
497fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
498fa9e4066Sahrens 		uint64_t label_id;
499fa9e4066Sahrens 
500fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
501fa9e4066Sahrens 		    label_id != id)
502be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
503fa9e4066Sahrens 
504fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
505be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
50699653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
50799653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
508be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
509fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
510fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
511be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
51221ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
51321ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
514be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
515fa9e4066Sahrens 	}
516fa9e4066Sahrens 
51799653d4eSeschrock 	/*
51899653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
51999653d4eSeschrock 	 */
52099653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
521be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
52299653d4eSeschrock 
5238654d025Sperrin 	/*
5248654d025Sperrin 	 * Determine whether we're a log vdev.
5258654d025Sperrin 	 */
5268654d025Sperrin 	islog = 0;
5278654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
528990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
529be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
530fa9e4066Sahrens 
53188ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
532be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
53388ecc943SGeorge Wilson 
53499653d4eSeschrock 	/*
5358654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
53699653d4eSeschrock 	 */
5378654d025Sperrin 	nparity = -1ULL;
53899653d4eSeschrock 	if (ops == &vdev_raidz_ops) {
53999653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
5408654d025Sperrin 		    &nparity) == 0) {
541b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
542be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
54399653d4eSeschrock 			/*
544f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
545f94275ceSAdam Leventhal 			 * device.
54699653d4eSeschrock 			 */
547f94275ceSAdam Leventhal 			if (nparity > 1 &&
548f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
549be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
550f94275ceSAdam Leventhal 			if (nparity > 2 &&
551f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
552be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
55399653d4eSeschrock 		} else {
55499653d4eSeschrock 			/*
55599653d4eSeschrock 			 * We require the parity to be specified for SPAs that
55699653d4eSeschrock 			 * support multiple parity levels.
55799653d4eSeschrock 			 */
558f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
559be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
56099653d4eSeschrock 			/*
56199653d4eSeschrock 			 * Otherwise, we default to 1 parity device for RAID-Z.
56299653d4eSeschrock 			 */
5638654d025Sperrin 			nparity = 1;
56499653d4eSeschrock 		}
56599653d4eSeschrock 	} else {
5668654d025Sperrin 		nparity = 0;
56799653d4eSeschrock 	}
5688654d025Sperrin 	ASSERT(nparity != -1ULL);
5698654d025Sperrin 
5708654d025Sperrin 	vd = vdev_alloc_common(spa, id, guid, ops);
5715cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
5728654d025Sperrin 
5738654d025Sperrin 	vd->vdev_islog = islog;
5748654d025Sperrin 	vd->vdev_nparity = nparity;
5758654d025Sperrin 
5768654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
5778654d025Sperrin 		vd->vdev_path = spa_strdup(vd->vdev_path);
5788654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
5798654d025Sperrin 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
5808654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
5818654d025Sperrin 	    &vd->vdev_physpath) == 0)
5828654d025Sperrin 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
5836809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
5846809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
58599653d4eSeschrock 
586afefbcddSeschrock 	/*
587afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
588afefbcddSeschrock 	 * as -1.
589afefbcddSeschrock 	 */
590afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
591afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
592afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
593afefbcddSeschrock 
5945cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_mapping_object);
5955cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
5965cabbc6bSPrashanth Sreenivasa 	    &vic->vic_mapping_object);
5975cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_births_object);
5985cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
5995cabbc6bSPrashanth Sreenivasa 	    &vic->vic_births_object);
6005cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
6015cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
6025cabbc6bSPrashanth Sreenivasa 	    &vic->vic_prev_indirect_vdev);
6035cabbc6bSPrashanth Sreenivasa 
604ea8dc4b6Seschrock 	/*
605ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
606ea8dc4b6Seschrock 	 * was not present at the time of import.
607ea8dc4b6Seschrock 	 */
6086809eb4eSEric Schrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
6096809eb4eSEric Schrock 	    &vd->vdev_not_present);
610ea8dc4b6Seschrock 
611ecc2d604Sbonwick 	/*
612ecc2d604Sbonwick 	 * Get the alignment requirement.
613ecc2d604Sbonwick 	 */
614ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
615ecc2d604Sbonwick 
61688ecc943SGeorge Wilson 	/*
61788ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
61888ecc943SGeorge Wilson 	 */
61988ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
62088ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
62188ecc943SGeorge Wilson 
622fa9e4066Sahrens 	/*
623fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
624fa9e4066Sahrens 	 */
6251195e687SMark J Musante 	if (parent && !parent->vdev_parent &&
6261195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
627fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
628fa9e4066Sahrens 		    &vd->vdev_ms_array);
629fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
630fa9e4066Sahrens 		    &vd->vdev_ms_shift);
631fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
632fa9e4066Sahrens 		    &vd->vdev_asize);
6333f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
6343f9d6ad7SLin Ling 		    &vd->vdev_removing);
635215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
636215198a6SJoe Stein 		    &vd->vdev_top_zap);
637215198a6SJoe Stein 	} else {
638215198a6SJoe Stein 		ASSERT0(vd->vdev_top_zap);
639fa9e4066Sahrens 	}
640fa9e4066Sahrens 
641cd0837ccSGeorge Wilson 	if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
642a1521560SJeff Bonwick 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
6439f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ADD ||
6441195e687SMark J Musante 		    alloctype == VDEV_ALLOC_SPLIT ||
6459f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ROOTPOOL);
646a1521560SJeff Bonwick 		vd->vdev_mg = metaslab_group_create(islog ?
647a1521560SJeff Bonwick 		    spa_log_class(spa) : spa_normal_class(spa), vd);
648a1521560SJeff Bonwick 	}
649a1521560SJeff Bonwick 
650215198a6SJoe Stein 	if (vd->vdev_ops->vdev_op_leaf &&
651215198a6SJoe Stein 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
652215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv,
653215198a6SJoe Stein 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
654215198a6SJoe Stein 	} else {
655215198a6SJoe Stein 		ASSERT0(vd->vdev_leaf_zap);
656215198a6SJoe Stein 	}
657215198a6SJoe Stein 
658fa9e4066Sahrens 	/*
6593d7072f8Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
660fa9e4066Sahrens 	 */
661215198a6SJoe Stein 
662c5904d13Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
66321ecdf64SLin Ling 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
66421ecdf64SLin Ling 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
665c5904d13Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
666c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
6670713e232SGeorge Wilson 			    &vd->vdev_dtl_object);
668c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
669c5904d13Seschrock 			    &vd->vdev_unspare);
670c5904d13Seschrock 		}
67121ecdf64SLin Ling 
67221ecdf64SLin Ling 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
67321ecdf64SLin Ling 			uint64_t spare = 0;
67421ecdf64SLin Ling 
67521ecdf64SLin Ling 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
67621ecdf64SLin Ling 			    &spare) == 0 && spare)
67721ecdf64SLin Ling 				spa_spare_add(vd);
67821ecdf64SLin Ling 		}
67921ecdf64SLin Ling 
680ecc2d604Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
681ecc2d604Sbonwick 		    &vd->vdev_offline);
682c5904d13Seschrock 
683b4952e17SGeorge Wilson 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
684b4952e17SGeorge Wilson 		    &vd->vdev_resilver_txg);
685cb04b873SMark J Musante 
6863d7072f8Seschrock 		/*
6873d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
6883d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
689069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
690069f55e2SEric Schrock 		 * remain in the faulted state.
6913d7072f8Seschrock 		 */
692b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
6933d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
6943d7072f8Seschrock 			    &vd->vdev_faulted);
6953d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
6963d7072f8Seschrock 			    &vd->vdev_degraded);
6973d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
6983d7072f8Seschrock 			    &vd->vdev_removed);
699069f55e2SEric Schrock 
700069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
701069f55e2SEric Schrock 				char *aux;
702069f55e2SEric Schrock 
703069f55e2SEric Schrock 				vd->vdev_label_aux =
704069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
705069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
706069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
707069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
708069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
709069f55e2SEric Schrock 			}
7103d7072f8Seschrock 		}
711fa9e4066Sahrens 	}
712fa9e4066Sahrens 
713fa9e4066Sahrens 	/*
714fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
715fa9e4066Sahrens 	 */
716fa9e4066Sahrens 	vdev_add_child(parent, vd);
717fa9e4066Sahrens 
71899653d4eSeschrock 	*vdp = vd;
71999653d4eSeschrock 
72099653d4eSeschrock 	return (0);
721fa9e4066Sahrens }
722fa9e4066Sahrens 
723fa9e4066Sahrens void
724fa9e4066Sahrens vdev_free(vdev_t *vd)
725fa9e4066Sahrens {
7263d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
727fa9e4066Sahrens 
728fa9e4066Sahrens 	/*
729fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
730fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
731fa9e4066Sahrens 	 */
732fa9e4066Sahrens 	vdev_close(vd);
733fa9e4066Sahrens 
734e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
735b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
736fa9e4066Sahrens 
737fa9e4066Sahrens 	/*
738fa9e4066Sahrens 	 * Free all children.
739fa9e4066Sahrens 	 */
740573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
741fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
742fa9e4066Sahrens 
743fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
744fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
745fa9e4066Sahrens 
746fa9e4066Sahrens 	/*
747fa9e4066Sahrens 	 * Discard allocation state.
748fa9e4066Sahrens 	 */
749a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
750fa9e4066Sahrens 		vdev_metaslab_fini(vd);
751a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
752a1521560SJeff Bonwick 	}
753fa9e4066Sahrens 
754fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_space);
755fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_dspace);
756fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_alloc);
757fa9e4066Sahrens 
758fa9e4066Sahrens 	/*
759fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
760fa9e4066Sahrens 	 */
761fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
762fa9e4066Sahrens 
763fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
764fa9e4066Sahrens 
7653d7072f8Seschrock 	/*
7663d7072f8Seschrock 	 * Clean up vdev structure.
7673d7072f8Seschrock 	 */
7683d7072f8Seschrock 	vdev_queue_fini(vd);
7693d7072f8Seschrock 	vdev_cache_fini(vd);
7703d7072f8Seschrock 
7713d7072f8Seschrock 	if (vd->vdev_path)
7723d7072f8Seschrock 		spa_strfree(vd->vdev_path);
7733d7072f8Seschrock 	if (vd->vdev_devid)
7743d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
7753d7072f8Seschrock 	if (vd->vdev_physpath)
7763d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
7776809eb4eSEric Schrock 	if (vd->vdev_fru)
7786809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
7793d7072f8Seschrock 
7803d7072f8Seschrock 	if (vd->vdev_isspare)
7813d7072f8Seschrock 		spa_spare_remove(vd);
782fa94a07fSbrendan 	if (vd->vdev_isl2cache)
783fa94a07fSbrendan 		spa_l2cache_remove(vd);
7843d7072f8Seschrock 
7853d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
7863d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
7878ad4d6ddSJeff Bonwick 
7883d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
7890713e232SGeorge Wilson 	space_map_close(vd->vdev_dtl_sm);
7908ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
7910713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
7920713e232SGeorge Wilson 		range_tree_destroy(vd->vdev_dtl[t]);
7938ad4d6ddSJeff Bonwick 	}
7943d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
7958ad4d6ddSJeff Bonwick 
7965cabbc6bSPrashanth Sreenivasa 	EQUIV(vd->vdev_indirect_births != NULL,
7975cabbc6bSPrashanth Sreenivasa 	    vd->vdev_indirect_mapping != NULL);
7985cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_indirect_births != NULL) {
7995cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
8005cabbc6bSPrashanth Sreenivasa 		vdev_indirect_births_close(vd->vdev_indirect_births);
8015cabbc6bSPrashanth Sreenivasa 	}
8025cabbc6bSPrashanth Sreenivasa 
8035cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_obsolete_sm != NULL) {
8045cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
8055cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
8065cabbc6bSPrashanth Sreenivasa 		space_map_close(vd->vdev_obsolete_sm);
8075cabbc6bSPrashanth Sreenivasa 		vd->vdev_obsolete_sm = NULL;
8085cabbc6bSPrashanth Sreenivasa 	}
8095cabbc6bSPrashanth Sreenivasa 	range_tree_destroy(vd->vdev_obsolete_segments);
8105cabbc6bSPrashanth Sreenivasa 	rw_destroy(&vd->vdev_indirect_rwlock);
8115cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&vd->vdev_obsolete_lock);
8125cabbc6bSPrashanth Sreenivasa 
8130f7643c7SGeorge Wilson 	mutex_destroy(&vd->vdev_queue_lock);
8143d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
8153d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
816e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
8173d7072f8Seschrock 
8183d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
8193d7072f8Seschrock 		spa->spa_root_vdev = NULL;
8203d7072f8Seschrock 
8213d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
822fa9e4066Sahrens }
823fa9e4066Sahrens 
824fa9e4066Sahrens /*
825fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
826fa9e4066Sahrens  */
827fa9e4066Sahrens static void
828fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
829fa9e4066Sahrens {
830fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
831fa9e4066Sahrens 	metaslab_t *msp;
832fa9e4066Sahrens 	vdev_t *vd;
833fa9e4066Sahrens 	int t;
834fa9e4066Sahrens 
835fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
836fa9e4066Sahrens 
837fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
838fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
839fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
840215198a6SJoe Stein 	tvd->vdev_top_zap = svd->vdev_top_zap;
841fa9e4066Sahrens 
842fa9e4066Sahrens 	svd->vdev_ms_array = 0;
843fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
844fa9e4066Sahrens 	svd->vdev_ms_count = 0;
845215198a6SJoe Stein 	svd->vdev_top_zap = 0;
846fa9e4066Sahrens 
847cd0837ccSGeorge Wilson 	if (tvd->vdev_mg)
848cd0837ccSGeorge Wilson 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
849fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
850fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
851fa9e4066Sahrens 
852fa9e4066Sahrens 	svd->vdev_mg = NULL;
853fa9e4066Sahrens 	svd->vdev_ms = NULL;
854ecc2d604Sbonwick 
855ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
856ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
857fa9e4066Sahrens 
858*86714001SSerapheim Dimitropoulos 	tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
859*86714001SSerapheim Dimitropoulos 	svd->vdev_checkpoint_sm = NULL;
860*86714001SSerapheim Dimitropoulos 
861fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
862fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
86399653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
864fa9e4066Sahrens 
865fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
866fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
86799653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
868fa9e4066Sahrens 
869fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
870fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
871fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
872fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
873fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
874fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
875fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
876fa9e4066Sahrens 	}
877fa9e4066Sahrens 
878e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
879fa9e4066Sahrens 		vdev_config_clean(svd);
880fa9e4066Sahrens 		vdev_config_dirty(tvd);
881fa9e4066Sahrens 	}
882fa9e4066Sahrens 
883e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
884e14bb325SJeff Bonwick 		vdev_state_clean(svd);
885e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
886e14bb325SJeff Bonwick 	}
887e14bb325SJeff Bonwick 
88899653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
88999653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
8908654d025Sperrin 
8918654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
8928654d025Sperrin 	svd->vdev_islog = 0;
893fa9e4066Sahrens }
894fa9e4066Sahrens 
895fa9e4066Sahrens static void
896fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
897fa9e4066Sahrens {
898fa9e4066Sahrens 	if (vd == NULL)
899fa9e4066Sahrens 		return;
900fa9e4066Sahrens 
901fa9e4066Sahrens 	vd->vdev_top = tvd;
902fa9e4066Sahrens 
903573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
904fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
905fa9e4066Sahrens }
906fa9e4066Sahrens 
907fa9e4066Sahrens /*
908fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
909fa9e4066Sahrens  */
910fa9e4066Sahrens vdev_t *
911fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
912fa9e4066Sahrens {
913fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
914fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
915fa9e4066Sahrens 	vdev_t *mvd;
916fa9e4066Sahrens 
917e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
918fa9e4066Sahrens 
919fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
920ecc2d604Sbonwick 
921ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
922573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
9234263d13fSGeorge Wilson 	mvd->vdev_max_asize = cvd->vdev_max_asize;
9245cabbc6bSPrashanth Sreenivasa 	mvd->vdev_psize = cvd->vdev_psize;
925ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
926ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
92788ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
928ecc2d604Sbonwick 
929fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
930fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
931fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
932fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
933fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
934fa9e4066Sahrens 
935fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
936fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
937fa9e4066Sahrens 
938fa9e4066Sahrens 	return (mvd);
939fa9e4066Sahrens }
940fa9e4066Sahrens 
941fa9e4066Sahrens /*
942fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
943fa9e4066Sahrens  */
944fa9e4066Sahrens void
945fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
946fa9e4066Sahrens {
947fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
948fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
949fa9e4066Sahrens 
950e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
951fa9e4066Sahrens 
952fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
953fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
95499653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
95599653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
956ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
957fa9e4066Sahrens 
958fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
959fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
9608ad4d6ddSJeff Bonwick 
96199653d4eSeschrock 	/*
962e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
963e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
964e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
965e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
96699653d4eSeschrock 	 */
9678ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
9688ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
9691195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
9708ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
9718ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
9728ad4d6ddSJeff Bonwick 	}
973e14bb325SJeff Bonwick 	cvd->vdev_id = mvd->vdev_id;
974e14bb325SJeff Bonwick 	vdev_add_child(pvd, cvd);
975fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
976fa9e4066Sahrens 
977fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
978fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
979fa9e4066Sahrens 
980fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
981fa9e4066Sahrens 	vdev_free(mvd);
982fa9e4066Sahrens }
983fa9e4066Sahrens 
984ea8dc4b6Seschrock int
985fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
986fa9e4066Sahrens {
987fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
988ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
989ecc2d604Sbonwick 	uint64_t m;
990fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
991fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
992ecc2d604Sbonwick 	metaslab_t **mspp;
993ecc2d604Sbonwick 	int error;
994fa9e4066Sahrens 
995a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
996a1521560SJeff Bonwick 
99788ecc943SGeorge Wilson 	/*
99888ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
99988ecc943SGeorge Wilson 	 */
100088ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
10010e34b6a7Sbonwick 		return (0);
10020e34b6a7Sbonwick 
100388ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
100488ecc943SGeorge Wilson 
1005fa9e4066Sahrens 	ASSERT(oldc <= newc);
1006fa9e4066Sahrens 
1007ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1008fa9e4066Sahrens 
1009ecc2d604Sbonwick 	if (oldc != 0) {
1010ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1011ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1012ecc2d604Sbonwick 	}
1013fa9e4066Sahrens 
1014ecc2d604Sbonwick 	vd->vdev_ms = mspp;
1015ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
1016fa9e4066Sahrens 
1017ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
10180713e232SGeorge Wilson 		uint64_t object = 0;
10190713e232SGeorge Wilson 
10205cabbc6bSPrashanth Sreenivasa 		/*
10215cabbc6bSPrashanth Sreenivasa 		 * vdev_ms_array may be 0 if we are creating the "fake"
10225cabbc6bSPrashanth Sreenivasa 		 * metaslabs for an indirect vdev for zdb's leak detection.
10235cabbc6bSPrashanth Sreenivasa 		 * See zdb_leak_init().
10245cabbc6bSPrashanth Sreenivasa 		 */
10255cabbc6bSPrashanth Sreenivasa 		if (txg == 0 && vd->vdev_ms_array != 0) {
1026ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
10277bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
10287bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
10293ee8c80cSPavel Zakharov 			if (error != 0) {
10303ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "unable to read the metaslab "
10313ee8c80cSPavel Zakharov 				    "array [error=%d]", error);
1032ecc2d604Sbonwick 				return (error);
10333ee8c80cSPavel Zakharov 			}
1034fa9e4066Sahrens 		}
10351e9bd7ecSPrakash Surya 
10361e9bd7ecSPrakash Surya 		error = metaslab_init(vd->vdev_mg, m, object, txg,
10371e9bd7ecSPrakash Surya 		    &(vd->vdev_ms[m]));
10383ee8c80cSPavel Zakharov 		if (error != 0) {
10393ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
10403ee8c80cSPavel Zakharov 			    error);
10411e9bd7ecSPrakash Surya 			return (error);
10423ee8c80cSPavel Zakharov 		}
1043fa9e4066Sahrens 	}
1044fa9e4066Sahrens 
1045a1521560SJeff Bonwick 	if (txg == 0)
1046a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1047a1521560SJeff Bonwick 
10483f9d6ad7SLin Ling 	/*
10493f9d6ad7SLin Ling 	 * If the vdev is being removed we don't activate
10503f9d6ad7SLin Ling 	 * the metaslabs since we want to ensure that no new
10513f9d6ad7SLin Ling 	 * allocations are performed on this device.
10523f9d6ad7SLin Ling 	 */
10533f9d6ad7SLin Ling 	if (oldc == 0 && !vd->vdev_removing)
1054a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
1055a1521560SJeff Bonwick 
1056a1521560SJeff Bonwick 	if (txg == 0)
1057a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
1058a1521560SJeff Bonwick 
1059ea8dc4b6Seschrock 	return (0);
1060fa9e4066Sahrens }
1061fa9e4066Sahrens 
1062fa9e4066Sahrens void
1063fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
1064fa9e4066Sahrens {
1065*86714001SSerapheim Dimitropoulos 	if (vd->vdev_checkpoint_sm != NULL) {
1066*86714001SSerapheim Dimitropoulos 		ASSERT(spa_feature_is_active(vd->vdev_spa,
1067*86714001SSerapheim Dimitropoulos 		    SPA_FEATURE_POOL_CHECKPOINT));
1068*86714001SSerapheim Dimitropoulos 		space_map_close(vd->vdev_checkpoint_sm);
1069*86714001SSerapheim Dimitropoulos 		/*
1070*86714001SSerapheim Dimitropoulos 		 * Even though we close the space map, we need to set its
1071*86714001SSerapheim Dimitropoulos 		 * pointer to NULL. The reason is that vdev_metaslab_fini()
1072*86714001SSerapheim Dimitropoulos 		 * may be called multiple times for certain operations
1073*86714001SSerapheim Dimitropoulos 		 * (i.e. when destroying a pool) so we need to ensure that
1074*86714001SSerapheim Dimitropoulos 		 * this clause never executes twice. This logic is similar
1075*86714001SSerapheim Dimitropoulos 		 * to the one used for the vdev_ms clause below.
1076*86714001SSerapheim Dimitropoulos 		 */
1077*86714001SSerapheim Dimitropoulos 		vd->vdev_checkpoint_sm = NULL;
1078*86714001SSerapheim Dimitropoulos 	}
1079*86714001SSerapheim Dimitropoulos 
1080fa9e4066Sahrens 	if (vd->vdev_ms != NULL) {
10815cabbc6bSPrashanth Sreenivasa 		uint64_t count = vd->vdev_ms_count;
10825cabbc6bSPrashanth Sreenivasa 
1083a1521560SJeff Bonwick 		metaslab_group_passivate(vd->vdev_mg);
10845cabbc6bSPrashanth Sreenivasa 		for (uint64_t m = 0; m < count; m++) {
10850713e232SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
10860713e232SGeorge Wilson 
10870713e232SGeorge Wilson 			if (msp != NULL)
10880713e232SGeorge Wilson 				metaslab_fini(msp);
10890713e232SGeorge Wilson 		}
1090fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1091fa9e4066Sahrens 		vd->vdev_ms = NULL;
10925cabbc6bSPrashanth Sreenivasa 
10935cabbc6bSPrashanth Sreenivasa 		vd->vdev_ms_count = 0;
1094fa9e4066Sahrens 	}
10955cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_ms_count);
1096fa9e4066Sahrens }
1097fa9e4066Sahrens 
1098e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
1099e14bb325SJeff Bonwick 	boolean_t	vps_readable;
1100e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
1101e14bb325SJeff Bonwick 	int		vps_flags;
1102e14bb325SJeff Bonwick } vdev_probe_stats_t;
1103e14bb325SJeff Bonwick 
1104e14bb325SJeff Bonwick static void
1105e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
11060a4e9518Sgw {
11078ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
1108a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
1109e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
1110a3f829aeSBill Moore 
1111a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
1112e14bb325SJeff Bonwick 
1113e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
1114e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1115e14bb325SJeff Bonwick 			vps->vps_readable = 1;
11168ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
1117a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1118770499e1SDan Kimmel 			    zio->io_offset, zio->io_size, zio->io_abd,
1119e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1120e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1121e14bb325SJeff Bonwick 		} else {
1122770499e1SDan Kimmel 			abd_free(zio->io_abd);
1123e14bb325SJeff Bonwick 		}
1124e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
1125e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1126e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
1127770499e1SDan Kimmel 		abd_free(zio->io_abd);
1128e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
1129a3f829aeSBill Moore 		zio_t *pio;
1130e14bb325SJeff Bonwick 
1131e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
1132e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
1133e14bb325SJeff Bonwick 
1134e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
11358ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
1136e14bb325SJeff Bonwick 			zio->io_error = 0;
1137e14bb325SJeff Bonwick 		} else {
1138e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
11393ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "failed probe");
1140e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
11418ad4d6ddSJeff Bonwick 			    spa, vd, NULL, 0, 0);
1142be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
1143e14bb325SJeff Bonwick 		}
1144a3f829aeSBill Moore 
1145a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
1146a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
1147a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
1148a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
1149a3f829aeSBill Moore 
11500f7643c7SGeorge Wilson 		zio_link_t *zl = NULL;
11510f7643c7SGeorge Wilson 		while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1152a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
1153be6fd75aSMatthew Ahrens 				pio->io_error = SET_ERROR(ENXIO);
1154a3f829aeSBill Moore 
1155e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
1156e14bb325SJeff Bonwick 	}
1157e14bb325SJeff Bonwick }
11580a4e9518Sgw 
1159e14bb325SJeff Bonwick /*
1160f7170741SWill Andrews  * Determine whether this device is accessible.
1161f7170741SWill Andrews  *
1162f7170741SWill Andrews  * Read and write to several known locations: the pad regions of each
1163f7170741SWill Andrews  * vdev label but the first, which we leave alone in case it contains
1164f7170741SWill Andrews  * a VTOC.
1165e14bb325SJeff Bonwick  */
1166e14bb325SJeff Bonwick zio_t *
1167a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
1168e14bb325SJeff Bonwick {
1169e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1170a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
1171a3f829aeSBill Moore 	zio_t *pio;
1172a3f829aeSBill Moore 
1173a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
11740a4e9518Sgw 
1175a3f829aeSBill Moore 	/*
1176a3f829aeSBill Moore 	 * Don't probe the probe.
1177a3f829aeSBill Moore 	 */
1178a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1179a3f829aeSBill Moore 		return (NULL);
1180e14bb325SJeff Bonwick 
1181a3f829aeSBill Moore 	/*
1182a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
1183a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
1184a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
1185a3f829aeSBill Moore 	 */
1186a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
1187e14bb325SJeff Bonwick 
1188a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
1189a3f829aeSBill Moore 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1190a3f829aeSBill Moore 
1191a3f829aeSBill Moore 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1192a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
11938956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
1194a3f829aeSBill Moore 
1195a3f829aeSBill Moore 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1196a3f829aeSBill Moore 			/*
1197a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
1198a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
1199a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
1200a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
1201a3f829aeSBill Moore 			 * any zio looking at these values can assume that
1202a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
1203a3f829aeSBill Moore 			 * important because when a device has intermittent
1204a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
1205a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
1206a3f829aeSBill Moore 			 * the zio (EIO).
1207a3f829aeSBill Moore 			 *
1208a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
1209a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
1210a3f829aeSBill Moore 			 * principles.
1211a3f829aeSBill Moore 			 */
1212a3f829aeSBill Moore 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1213a3f829aeSBill Moore 			vd->vdev_cant_read = B_FALSE;
1214a3f829aeSBill Moore 			vd->vdev_cant_write = B_FALSE;
1215a3f829aeSBill Moore 		}
1216a3f829aeSBill Moore 
1217a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1218a3f829aeSBill Moore 		    vdev_probe_done, vps,
1219a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1220a3f829aeSBill Moore 
122198d1cbfeSGeorge Wilson 		/*
122298d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
122398d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
122498d1cbfeSGeorge Wilson 		 */
1225a3f829aeSBill Moore 		if (zio != NULL) {
1226a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1227a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1228a3f829aeSBill Moore 		}
1229e14bb325SJeff Bonwick 	}
1230e14bb325SJeff Bonwick 
1231a3f829aeSBill Moore 	if (zio != NULL)
1232a3f829aeSBill Moore 		zio_add_child(zio, pio);
1233e14bb325SJeff Bonwick 
1234a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1235e14bb325SJeff Bonwick 
1236a3f829aeSBill Moore 	if (vps == NULL) {
1237a3f829aeSBill Moore 		ASSERT(zio != NULL);
1238a3f829aeSBill Moore 		return (NULL);
1239a3f829aeSBill Moore 	}
1240e14bb325SJeff Bonwick 
1241e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1242a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1243e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1244770499e1SDan Kimmel 		    offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1245770499e1SDan Kimmel 		    abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1246e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1247e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1248e14bb325SJeff Bonwick 	}
1249e14bb325SJeff Bonwick 
1250a3f829aeSBill Moore 	if (zio == NULL)
1251a3f829aeSBill Moore 		return (pio);
1252a3f829aeSBill Moore 
1253a3f829aeSBill Moore 	zio_nowait(pio);
1254a3f829aeSBill Moore 	return (NULL);
12550a4e9518Sgw }
12560a4e9518Sgw 
1257f64c0e34SEric Taylor static void
1258f64c0e34SEric Taylor vdev_open_child(void *arg)
1259f64c0e34SEric Taylor {
1260f64c0e34SEric Taylor 	vdev_t *vd = arg;
1261f64c0e34SEric Taylor 
1262f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1263f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1264f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1265f64c0e34SEric Taylor }
1266f64c0e34SEric Taylor 
1267681d9761SEric Taylor boolean_t
1268681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1269681d9761SEric Taylor {
1270681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1271681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1272681d9761SEric Taylor 		return (B_TRUE);
1273681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1274681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1275681d9761SEric Taylor 			return (B_TRUE);
1276681d9761SEric Taylor 	return (B_FALSE);
1277681d9761SEric Taylor }
1278681d9761SEric Taylor 
1279f64c0e34SEric Taylor void
1280f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1281f64c0e34SEric Taylor {
1282f64c0e34SEric Taylor 	taskq_t *tq;
1283f64c0e34SEric Taylor 	int children = vd->vdev_children;
1284f64c0e34SEric Taylor 
1285681d9761SEric Taylor 	/*
1286681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1287681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1288681d9761SEric Taylor 	 * spa_namespace_lock
1289681d9761SEric Taylor 	 */
1290681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
1291681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1292681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1293681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
1294681d9761SEric Taylor 		return;
1295681d9761SEric Taylor 	}
1296f64c0e34SEric Taylor 	tq = taskq_create("vdev_open", children, minclsyspri,
1297f64c0e34SEric Taylor 	    children, children, TASKQ_PREPOPULATE);
1298f64c0e34SEric Taylor 
1299f64c0e34SEric Taylor 	for (int c = 0; c < children; c++)
1300f64c0e34SEric Taylor 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1301f64c0e34SEric Taylor 		    TQ_SLEEP) != NULL);
1302f64c0e34SEric Taylor 
1303f64c0e34SEric Taylor 	taskq_destroy(tq);
1304f64c0e34SEric Taylor }
1305f64c0e34SEric Taylor 
13065cabbc6bSPrashanth Sreenivasa /*
13075cabbc6bSPrashanth Sreenivasa  * Compute the raidz-deflation ratio.  Note, we hard-code
13085cabbc6bSPrashanth Sreenivasa  * in 128k (1 << 17) because it is the "typical" blocksize.
13095cabbc6bSPrashanth Sreenivasa  * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
13105cabbc6bSPrashanth Sreenivasa  * otherwise it would inconsistently account for existing bp's.
13115cabbc6bSPrashanth Sreenivasa  */
13125cabbc6bSPrashanth Sreenivasa static void
13135cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd)
13145cabbc6bSPrashanth Sreenivasa {
13155cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
13165cabbc6bSPrashanth Sreenivasa 		vd->vdev_deflate_ratio = (1 << 17) /
13175cabbc6bSPrashanth Sreenivasa 		    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
13185cabbc6bSPrashanth Sreenivasa 	}
13195cabbc6bSPrashanth Sreenivasa }
13205cabbc6bSPrashanth Sreenivasa 
1321fa9e4066Sahrens /*
1322fa9e4066Sahrens  * Prepare a virtual device for access.
1323fa9e4066Sahrens  */
1324fa9e4066Sahrens int
1325fa9e4066Sahrens vdev_open(vdev_t *vd)
1326fa9e4066Sahrens {
13278ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1328fa9e4066Sahrens 	int error;
1329fa9e4066Sahrens 	uint64_t osize = 0;
13304263d13fSGeorge Wilson 	uint64_t max_osize = 0;
13314263d13fSGeorge Wilson 	uint64_t asize, max_asize, psize;
1332ecc2d604Sbonwick 	uint64_t ashift = 0;
1333fa9e4066Sahrens 
1334f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1335f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1336fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1337fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1338fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1339fa9e4066Sahrens 
1340fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1341e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1342e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1343573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1344fa9e4066Sahrens 
1345069f55e2SEric Schrock 	/*
1346069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1347069f55e2SEric Schrock 	 * faulted, bail out of the open.
1348069f55e2SEric Schrock 	 */
13493d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
13503d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1351069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1352069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
13533d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1354069f55e2SEric Schrock 		    vd->vdev_label_aux);
1355be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
13563d7072f8Seschrock 	} else if (vd->vdev_offline) {
1357fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1358ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1359be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1360fa9e4066Sahrens 	}
1361fa9e4066Sahrens 
13624263d13fSGeorge Wilson 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1363fa9e4066Sahrens 
1364095bcd66SGeorge Wilson 	/*
1365095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1366095bcd66SGeorge Wilson 	 * the vdev on error.
1367095bcd66SGeorge Wilson 	 */
1368095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1369ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
13708956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1371ea8dc4b6Seschrock 
1372fa9e4066Sahrens 	if (error) {
13733d7072f8Seschrock 		if (vd->vdev_removed &&
13743d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
13753d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
13763d7072f8Seschrock 
13776f793812SPavel Zakharov 		if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
13786f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
13796f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
13806f793812SPavel Zakharov 		} else {
13816f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
13826f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
13836f793812SPavel Zakharov 		}
1384fa9e4066Sahrens 		return (error);
1385fa9e4066Sahrens 	}
1386fa9e4066Sahrens 
13873d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
13883d7072f8Seschrock 
1389096d22d4SEric Schrock 	/*
1390096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1391096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1392096d22d4SEric Schrock 	 */
1393096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1394096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1395096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1396096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1397096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1398096d22d4SEric Schrock 		    vd->vdev_label_aux);
1399be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1400096d22d4SEric Schrock 	}
1401096d22d4SEric Schrock 
14023d7072f8Seschrock 	if (vd->vdev_degraded) {
14033d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
14043d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
14053d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
14063d7072f8Seschrock 	} else {
1407069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
14083d7072f8Seschrock 	}
1409fa9e4066Sahrens 
141088ecc943SGeorge Wilson 	/*
141188ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
141288ecc943SGeorge Wilson 	 */
141388ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
141488ecc943SGeorge Wilson 		return (0);
141588ecc943SGeorge Wilson 
1416573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1417ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1418ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1419ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1420ea8dc4b6Seschrock 			break;
1421ea8dc4b6Seschrock 		}
1422573ca77eSGeorge Wilson 	}
1423fa9e4066Sahrens 
1424fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
14254263d13fSGeorge Wilson 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1426fa9e4066Sahrens 
1427fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1428fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1429ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1430ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1431be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1432fa9e4066Sahrens 		}
1433fa9e4066Sahrens 		psize = osize;
1434fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
14354263d13fSGeorge Wilson 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
14364263d13fSGeorge Wilson 		    VDEV_LABEL_END_SIZE);
1437fa9e4066Sahrens 	} else {
1438ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1439fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1440ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1441ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1442be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1443fa9e4066Sahrens 		}
1444fa9e4066Sahrens 		psize = 0;
1445fa9e4066Sahrens 		asize = osize;
14464263d13fSGeorge Wilson 		max_asize = max_osize;
1447fa9e4066Sahrens 	}
1448fa9e4066Sahrens 
1449fa9e4066Sahrens 	vd->vdev_psize = psize;
1450fa9e4066Sahrens 
1451573ca77eSGeorge Wilson 	/*
1452c040c10cSSteven Hartland 	 * Make sure the allocatable size hasn't shrunk too much.
1453573ca77eSGeorge Wilson 	 */
1454573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1455573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1456573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1457be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1458573ca77eSGeorge Wilson 	}
1459573ca77eSGeorge Wilson 
1460fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1461fa9e4066Sahrens 		/*
1462fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
1463ecc2d604Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1464fa9e4066Sahrens 		 */
1465fa9e4066Sahrens 		vd->vdev_asize = asize;
14664263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1467ecc2d604Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1468fa9e4066Sahrens 	} else {
1469fa9e4066Sahrens 		/*
14702384d9f8SGeorge Wilson 		 * Detect if the alignment requirement has increased.
14712384d9f8SGeorge Wilson 		 * We don't want to make the pool unavailable, just
14722384d9f8SGeorge Wilson 		 * issue a warning instead.
1473fa9e4066Sahrens 		 */
14742384d9f8SGeorge Wilson 		if (ashift > vd->vdev_top->vdev_ashift &&
14752384d9f8SGeorge Wilson 		    vd->vdev_ops->vdev_op_leaf) {
14762384d9f8SGeorge Wilson 			cmn_err(CE_WARN,
14772384d9f8SGeorge Wilson 			    "Disk, '%s', has a block alignment that is "
14782384d9f8SGeorge Wilson 			    "larger than the pool's alignment\n",
14792384d9f8SGeorge Wilson 			    vd->vdev_path);
1480fa9e4066Sahrens 		}
14814263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1482573ca77eSGeorge Wilson 	}
1483fa9e4066Sahrens 
1484573ca77eSGeorge Wilson 	/*
1485c040c10cSSteven Hartland 	 * If all children are healthy we update asize if either:
1486c040c10cSSteven Hartland 	 * The asize has increased, due to a device expansion caused by dynamic
1487c040c10cSSteven Hartland 	 * LUN growth or vdev replacement, and automatic expansion is enabled;
1488c040c10cSSteven Hartland 	 * making the additional space available.
1489c040c10cSSteven Hartland 	 *
1490c040c10cSSteven Hartland 	 * The asize has decreased, due to a device shrink usually caused by a
1491c040c10cSSteven Hartland 	 * vdev replace with a smaller device. This ensures that calculations
1492c040c10cSSteven Hartland 	 * based of max_asize and asize e.g. esize are always valid. It's safe
1493c040c10cSSteven Hartland 	 * to do this as we've already validated that asize is greater than
1494c040c10cSSteven Hartland 	 * vdev_min_asize.
1495573ca77eSGeorge Wilson 	 */
1496c040c10cSSteven Hartland 	if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1497c040c10cSSteven Hartland 	    ((asize > vd->vdev_asize &&
1498c040c10cSSteven Hartland 	    (vd->vdev_expanding || spa->spa_autoexpand)) ||
1499c040c10cSSteven Hartland 	    (asize < vd->vdev_asize)))
1500573ca77eSGeorge Wilson 		vd->vdev_asize = asize;
1501fa9e4066Sahrens 
1502573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1503fa9e4066Sahrens 
15040a4e9518Sgw 	/*
15050a4e9518Sgw 	 * Ensure we can issue some IO before declaring the
15060a4e9518Sgw 	 * vdev open for business.
15070a4e9518Sgw 	 */
1508e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1509e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
151098d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
151198d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
15120a4e9518Sgw 		return (error);
15130a4e9518Sgw 	}
15140a4e9518Sgw 
151581cd5c55SMatthew Ahrens 	/*
151681cd5c55SMatthew Ahrens 	 * Track the min and max ashift values for normal data devices.
151781cd5c55SMatthew Ahrens 	 */
151881cd5c55SMatthew Ahrens 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
151981cd5c55SMatthew Ahrens 	    !vd->vdev_islog && vd->vdev_aux == NULL) {
152081cd5c55SMatthew Ahrens 		if (vd->vdev_ashift > spa->spa_max_ashift)
152181cd5c55SMatthew Ahrens 			spa->spa_max_ashift = vd->vdev_ashift;
152281cd5c55SMatthew Ahrens 		if (vd->vdev_ashift < spa->spa_min_ashift)
152381cd5c55SMatthew Ahrens 			spa->spa_min_ashift = vd->vdev_ashift;
152481cd5c55SMatthew Ahrens 	}
152581cd5c55SMatthew Ahrens 
1526088f3894Sahrens 	/*
1527088f3894Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
15288ad4d6ddSJeff Bonwick 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
15298ad4d6ddSJeff Bonwick 	 * since this would just restart the scrub we are already doing.
1530088f3894Sahrens 	 */
15318ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
15328ad4d6ddSJeff Bonwick 	    vdev_resilver_needed(vd, NULL, NULL))
15338ad4d6ddSJeff Bonwick 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1534088f3894Sahrens 
1535fa9e4066Sahrens 	return (0);
1536fa9e4066Sahrens }
1537fa9e4066Sahrens 
1538560e6e96Seschrock /*
1539560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
15406f793812SPavel Zakharov  * contents. This needs to be done before vdev_load() so that we don't
15413d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1542560e6e96Seschrock  *
1543560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1544560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1545560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1546560e6e96Seschrock  * will be updated but the function will return 0.
1547560e6e96Seschrock  */
1548560e6e96Seschrock int
15496f793812SPavel Zakharov vdev_validate(vdev_t *vd)
1550560e6e96Seschrock {
1551560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1552560e6e96Seschrock 	nvlist_t *label;
15536f793812SPavel Zakharov 	uint64_t guid = 0, aux_guid = 0, top_guid;
1554560e6e96Seschrock 	uint64_t state;
15556f793812SPavel Zakharov 	nvlist_t *nvl;
15566f793812SPavel Zakharov 	uint64_t txg;
1557560e6e96Seschrock 
15586f793812SPavel Zakharov 	if (vdev_validate_skip)
15596f793812SPavel Zakharov 		return (0);
15606f793812SPavel Zakharov 
15616f793812SPavel Zakharov 	for (uint64_t c = 0; c < vd->vdev_children; c++)
15626f793812SPavel Zakharov 		if (vdev_validate(vd->vdev_child[c]) != 0)
1563be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1564560e6e96Seschrock 
1565b5989ec7Seschrock 	/*
1566b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1567b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1568b5989ec7Seschrock 	 * overwrite the previous state.
1569b5989ec7Seschrock 	 */
15706f793812SPavel Zakharov 	if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
15716f793812SPavel Zakharov 		return (0);
1572560e6e96Seschrock 
15736f793812SPavel Zakharov 	/*
15746f793812SPavel Zakharov 	 * If we are performing an extreme rewind, we allow for a label that
15756f793812SPavel Zakharov 	 * was modified at a point after the current txg.
15766f793812SPavel Zakharov 	 */
15776f793812SPavel Zakharov 	if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0)
15786f793812SPavel Zakharov 		txg = UINT64_MAX;
15796f793812SPavel Zakharov 	else
15806f793812SPavel Zakharov 		txg = spa_last_synced_txg(spa);
1581560e6e96Seschrock 
15826f793812SPavel Zakharov 	if ((label = vdev_label_read_config(vd, txg)) == NULL) {
15836f793812SPavel Zakharov 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
15846f793812SPavel Zakharov 		    VDEV_AUX_BAD_LABEL);
15856f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: failed reading config");
15866f793812SPavel Zakharov 		return (0);
15876f793812SPavel Zakharov 	}
15881195e687SMark J Musante 
15896f793812SPavel Zakharov 	/*
15906f793812SPavel Zakharov 	 * Determine if this vdev has been split off into another
15916f793812SPavel Zakharov 	 * pool.  If so, then refuse to open it.
15926f793812SPavel Zakharov 	 */
15936f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
15946f793812SPavel Zakharov 	    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
15956f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
15966f793812SPavel Zakharov 		    VDEV_AUX_SPLIT_POOL);
15976f793812SPavel Zakharov 		nvlist_free(label);
15986f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
15996f793812SPavel Zakharov 		return (0);
16006f793812SPavel Zakharov 	}
1601560e6e96Seschrock 
16026f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
16036f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16046f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16056f793812SPavel Zakharov 		nvlist_free(label);
16066f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
16076f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_GUID);
16086f793812SPavel Zakharov 		return (0);
16096f793812SPavel Zakharov 	}
16101195e687SMark J Musante 
16116f793812SPavel Zakharov 	/*
16126f793812SPavel Zakharov 	 * If config is not trusted then ignore the spa guid check. This is
16136f793812SPavel Zakharov 	 * necessary because if the machine crashed during a re-guid the new
16146f793812SPavel Zakharov 	 * guid might have been written to all of the vdev labels, but not the
16156f793812SPavel Zakharov 	 * cached config. The check will be performed again once we have the
16166f793812SPavel Zakharov 	 * trusted config from the MOS.
16176f793812SPavel Zakharov 	 */
16186f793812SPavel Zakharov 	if (spa->spa_trust_config && guid != spa_guid(spa)) {
16196f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16206f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16216f793812SPavel Zakharov 		nvlist_free(label);
16226f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
16236f793812SPavel Zakharov 		    "match config (%llu != %llu)", (u_longlong_t)guid,
16246f793812SPavel Zakharov 		    (u_longlong_t)spa_guid(spa));
16256f793812SPavel Zakharov 		return (0);
16266f793812SPavel Zakharov 	}
16276f793812SPavel Zakharov 
16286f793812SPavel Zakharov 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
16296f793812SPavel Zakharov 	    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
16306f793812SPavel Zakharov 	    &aux_guid) != 0)
16316f793812SPavel Zakharov 		aux_guid = 0;
16326f793812SPavel Zakharov 
16336f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
16346f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16356f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16366f793812SPavel Zakharov 		nvlist_free(label);
16376f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
16386f793812SPavel Zakharov 		    ZPOOL_CONFIG_GUID);
16396f793812SPavel Zakharov 		return (0);
16406f793812SPavel Zakharov 	}
16416f793812SPavel Zakharov 
16426f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
16436f793812SPavel Zakharov 	    != 0) {
16446f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16456f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16466f793812SPavel Zakharov 		nvlist_free(label);
16476f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
16486f793812SPavel Zakharov 		    ZPOOL_CONFIG_TOP_GUID);
16496f793812SPavel Zakharov 		return (0);
16506f793812SPavel Zakharov 	}
16516f793812SPavel Zakharov 
16526f793812SPavel Zakharov 	/*
16536f793812SPavel Zakharov 	 * If this vdev just became a top-level vdev because its sibling was
16546f793812SPavel Zakharov 	 * detached, it will have adopted the parent's vdev guid -- but the
16556f793812SPavel Zakharov 	 * label may or may not be on disk yet. Fortunately, either version
16566f793812SPavel Zakharov 	 * of the label will have the same top guid, so if we're a top-level
16576f793812SPavel Zakharov 	 * vdev, we can safely compare to that instead.
16586f793812SPavel Zakharov 	 * However, if the config comes from a cachefile that failed to update
16596f793812SPavel Zakharov 	 * after the detach, a top-level vdev will appear as a non top-level
16606f793812SPavel Zakharov 	 * vdev in the config. Also relax the constraints if we perform an
16616f793812SPavel Zakharov 	 * extreme rewind.
16626f793812SPavel Zakharov 	 *
16636f793812SPavel Zakharov 	 * If we split this vdev off instead, then we also check the
16646f793812SPavel Zakharov 	 * original pool's guid. We don't want to consider the vdev
16656f793812SPavel Zakharov 	 * corrupt if it is partway through a split operation.
16666f793812SPavel Zakharov 	 */
16676f793812SPavel Zakharov 	if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
16686f793812SPavel Zakharov 		boolean_t mismatch = B_FALSE;
16696f793812SPavel Zakharov 		if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
16706f793812SPavel Zakharov 			if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
16716f793812SPavel Zakharov 				mismatch = B_TRUE;
16726f793812SPavel Zakharov 		} else {
16736f793812SPavel Zakharov 			if (vd->vdev_guid != top_guid &&
16746f793812SPavel Zakharov 			    vd->vdev_top->vdev_guid != guid)
16756f793812SPavel Zakharov 				mismatch = B_TRUE;
1676560e6e96Seschrock 		}
1677560e6e96Seschrock 
16786f793812SPavel Zakharov 		if (mismatch) {
1679560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1680560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1681560e6e96Seschrock 			nvlist_free(label);
16826f793812SPavel Zakharov 			vdev_dbgmsg(vd, "vdev_validate: config guid "
16836f793812SPavel Zakharov 			    "doesn't match label guid");
16846f793812SPavel Zakharov 			vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
16856f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_guid,
16866f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_top->vdev_guid);
16876f793812SPavel Zakharov 			vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
16886f793812SPavel Zakharov 			    "aux_guid %llu", (u_longlong_t)guid,
16896f793812SPavel Zakharov 			    (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
1690560e6e96Seschrock 			return (0);
1691560e6e96Seschrock 		}
16926f793812SPavel Zakharov 	}
1693560e6e96Seschrock 
16946f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
16956f793812SPavel Zakharov 	    &state) != 0) {
16966f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16976f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
1698560e6e96Seschrock 		nvlist_free(label);
16996f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
17006f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_STATE);
17016f793812SPavel Zakharov 		return (0);
17026f793812SPavel Zakharov 	}
1703560e6e96Seschrock 
17046f793812SPavel Zakharov 	nvlist_free(label);
17056f793812SPavel Zakharov 
17066f793812SPavel Zakharov 	/*
17076f793812SPavel Zakharov 	 * If this is a verbatim import, no need to check the
17086f793812SPavel Zakharov 	 * state of the pool.
17096f793812SPavel Zakharov 	 */
17106f793812SPavel Zakharov 	if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
17116f793812SPavel Zakharov 	    spa_load_state(spa) == SPA_LOAD_OPEN &&
17126f793812SPavel Zakharov 	    state != POOL_STATE_ACTIVE) {
17136f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
17146f793812SPavel Zakharov 		    "for spa %s", (u_longlong_t)state, spa->spa_name);
17156f793812SPavel Zakharov 		return (SET_ERROR(EBADF));
17166f793812SPavel Zakharov 	}
17176f793812SPavel Zakharov 
17186f793812SPavel Zakharov 	/*
17196f793812SPavel Zakharov 	 * If we were able to open and validate a vdev that was
17206f793812SPavel Zakharov 	 * previously marked permanently unavailable, clear that state
17216f793812SPavel Zakharov 	 * now.
17226f793812SPavel Zakharov 	 */
17236f793812SPavel Zakharov 	if (vd->vdev_not_present)
17246f793812SPavel Zakharov 		vd->vdev_not_present = 0;
17256f793812SPavel Zakharov 
17266f793812SPavel Zakharov 	return (0);
17276f793812SPavel Zakharov }
17286f793812SPavel Zakharov 
17296f793812SPavel Zakharov static void
17306f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
17316f793812SPavel Zakharov {
17326f793812SPavel Zakharov 	if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
17336f793812SPavel Zakharov 		if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
17346f793812SPavel Zakharov 			zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
17356f793812SPavel Zakharov 			    "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
17366f793812SPavel Zakharov 			    dvd->vdev_path, svd->vdev_path);
17376f793812SPavel Zakharov 			spa_strfree(dvd->vdev_path);
17386f793812SPavel Zakharov 			dvd->vdev_path = spa_strdup(svd->vdev_path);
17393ee8c80cSPavel Zakharov 		}
17406f793812SPavel Zakharov 	} else if (svd->vdev_path != NULL) {
17416f793812SPavel Zakharov 		dvd->vdev_path = spa_strdup(svd->vdev_path);
17426f793812SPavel Zakharov 		zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
17436f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
17446f793812SPavel Zakharov 	}
17456f793812SPavel Zakharov }
1746560e6e96Seschrock 
17476f793812SPavel Zakharov /*
17486f793812SPavel Zakharov  * Recursively copy vdev paths from one vdev to another. Source and destination
17496f793812SPavel Zakharov  * vdev trees must have same geometry otherwise return error. Intended to copy
17506f793812SPavel Zakharov  * paths from userland config into MOS config.
17516f793812SPavel Zakharov  */
17526f793812SPavel Zakharov int
17536f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
17546f793812SPavel Zakharov {
17556f793812SPavel Zakharov 	if ((svd->vdev_ops == &vdev_missing_ops) ||
17566f793812SPavel Zakharov 	    (svd->vdev_ishole && dvd->vdev_ishole) ||
17576f793812SPavel Zakharov 	    (dvd->vdev_ops == &vdev_indirect_ops))
17586f793812SPavel Zakharov 		return (0);
17596f793812SPavel Zakharov 
17606f793812SPavel Zakharov 	if (svd->vdev_ops != dvd->vdev_ops) {
17616f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
17626f793812SPavel Zakharov 		    svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
17636f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
17646f793812SPavel Zakharov 	}
17656f793812SPavel Zakharov 
17666f793812SPavel Zakharov 	if (svd->vdev_guid != dvd->vdev_guid) {
17676f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
17686f793812SPavel Zakharov 		    "%llu)", (u_longlong_t)svd->vdev_guid,
17696f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid);
17706f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
17716f793812SPavel Zakharov 	}
17726f793812SPavel Zakharov 
17736f793812SPavel Zakharov 	if (svd->vdev_children != dvd->vdev_children) {
17746f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
17756f793812SPavel Zakharov 		    "%llu != %llu", (u_longlong_t)svd->vdev_children,
17766f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_children);
17776f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
177851ece835Seschrock 	}
1779560e6e96Seschrock 
17806f793812SPavel Zakharov 	for (uint64_t i = 0; i < svd->vdev_children; i++) {
17816f793812SPavel Zakharov 		int error = vdev_copy_path_strict(svd->vdev_child[i],
17826f793812SPavel Zakharov 		    dvd->vdev_child[i]);
17836f793812SPavel Zakharov 		if (error != 0)
17846f793812SPavel Zakharov 			return (error);
17856f793812SPavel Zakharov 	}
17866f793812SPavel Zakharov 
17876f793812SPavel Zakharov 	if (svd->vdev_ops->vdev_op_leaf)
17886f793812SPavel Zakharov 		vdev_copy_path_impl(svd, dvd);
17896f793812SPavel Zakharov 
1790560e6e96Seschrock 	return (0);
1791560e6e96Seschrock }
1792560e6e96Seschrock 
17936f793812SPavel Zakharov static void
17946f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
17956f793812SPavel Zakharov {
17966f793812SPavel Zakharov 	ASSERT(stvd->vdev_top == stvd);
17976f793812SPavel Zakharov 	ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
17986f793812SPavel Zakharov 
17996f793812SPavel Zakharov 	for (uint64_t i = 0; i < dvd->vdev_children; i++) {
18006f793812SPavel Zakharov 		vdev_copy_path_search(stvd, dvd->vdev_child[i]);
18016f793812SPavel Zakharov 	}
18026f793812SPavel Zakharov 
18036f793812SPavel Zakharov 	if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
18046f793812SPavel Zakharov 		return;
18056f793812SPavel Zakharov 
18066f793812SPavel Zakharov 	/*
18076f793812SPavel Zakharov 	 * The idea here is that while a vdev can shift positions within
18086f793812SPavel Zakharov 	 * a top vdev (when replacing, attaching mirror, etc.) it cannot
18096f793812SPavel Zakharov 	 * step outside of it.
18106f793812SPavel Zakharov 	 */
18116f793812SPavel Zakharov 	vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
18126f793812SPavel Zakharov 
18136f793812SPavel Zakharov 	if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
18146f793812SPavel Zakharov 		return;
18156f793812SPavel Zakharov 
18166f793812SPavel Zakharov 	ASSERT(vd->vdev_ops->vdev_op_leaf);
18176f793812SPavel Zakharov 
18186f793812SPavel Zakharov 	vdev_copy_path_impl(vd, dvd);
18196f793812SPavel Zakharov }
18206f793812SPavel Zakharov 
18216f793812SPavel Zakharov /*
18226f793812SPavel Zakharov  * Recursively copy vdev paths from one root vdev to another. Source and
18236f793812SPavel Zakharov  * destination vdev trees may differ in geometry. For each destination leaf
18246f793812SPavel Zakharov  * vdev, search a vdev with the same guid and top vdev id in the source.
18256f793812SPavel Zakharov  * Intended to copy paths from userland config into MOS config.
18266f793812SPavel Zakharov  */
18276f793812SPavel Zakharov void
18286f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
18296f793812SPavel Zakharov {
18306f793812SPavel Zakharov 	uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
18316f793812SPavel Zakharov 	ASSERT(srvd->vdev_ops == &vdev_root_ops);
18326f793812SPavel Zakharov 	ASSERT(drvd->vdev_ops == &vdev_root_ops);
18336f793812SPavel Zakharov 
18346f793812SPavel Zakharov 	for (uint64_t i = 0; i < children; i++) {
18356f793812SPavel Zakharov 		vdev_copy_path_search(srvd->vdev_child[i],
18366f793812SPavel Zakharov 		    drvd->vdev_child[i]);
18376f793812SPavel Zakharov 	}
18386f793812SPavel Zakharov }
18396f793812SPavel Zakharov 
1840fa9e4066Sahrens /*
1841fa9e4066Sahrens  * Close a virtual device.
1842fa9e4066Sahrens  */
1843fa9e4066Sahrens void
1844fa9e4066Sahrens vdev_close(vdev_t *vd)
1845fa9e4066Sahrens {
18468ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1847095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
18488ad4d6ddSJeff Bonwick 
18498ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
18508ad4d6ddSJeff Bonwick 
18511195e687SMark J Musante 	/*
18521195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
18531195e687SMark J Musante 	 * going offline.
18541195e687SMark J Musante 	 */
1855095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
18561195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1857095bcd66SGeorge Wilson 
1858fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1859fa9e4066Sahrens 
18603d7072f8Seschrock 	vdev_cache_purge(vd);
1861fa9e4066Sahrens 
1862560e6e96Seschrock 	/*
1863573ca77eSGeorge Wilson 	 * We record the previous state before we close it, so that if we are
1864560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1865560e6e96Seschrock 	 * it's still faulted.
1866560e6e96Seschrock 	 */
1867560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
1868560e6e96Seschrock 
1869fa9e4066Sahrens 	if (vd->vdev_offline)
1870fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1871fa9e4066Sahrens 	else
1872fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
1873ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1874fa9e4066Sahrens }
1875fa9e4066Sahrens 
1876dcba9f3fSGeorge Wilson void
1877dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
1878dcba9f3fSGeorge Wilson {
1879dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1880dcba9f3fSGeorge Wilson 
1881dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1882dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1883dcba9f3fSGeorge Wilson 		return;
1884dcba9f3fSGeorge Wilson 
1885dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1886dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
1887dcba9f3fSGeorge Wilson 
1888dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1889dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
1890dcba9f3fSGeorge Wilson }
1891dcba9f3fSGeorge Wilson 
1892dcba9f3fSGeorge Wilson void
1893dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
1894dcba9f3fSGeorge Wilson {
1895dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1896dcba9f3fSGeorge Wilson 
1897dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1898dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1899dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
1900dcba9f3fSGeorge Wilson 
1901dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1902dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
1903dcba9f3fSGeorge Wilson }
1904dcba9f3fSGeorge Wilson 
1905095bcd66SGeorge Wilson /*
1906095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1907095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
1908095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1909095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
1910095bcd66SGeorge Wilson  */
1911fa9e4066Sahrens void
1912ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
1913fa9e4066Sahrens {
1914ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
1915fa9e4066Sahrens 
1916e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1917ea8dc4b6Seschrock 
19181195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
19191195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
1920fa9e4066Sahrens 	vdev_close(vd);
1921fa9e4066Sahrens 	(void) vdev_open(vd);
1922fa9e4066Sahrens 
192339c23413Seschrock 	/*
192439c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
192539c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
192639c23413Seschrock 	 * opened in response to vdev_reopen().
192739c23413Seschrock 	 */
1928c5904d13Seschrock 	if (vd->vdev_aux) {
1929c5904d13Seschrock 		(void) vdev_validate_aux(vd);
1930e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
19316809eb4eSEric Schrock 		    vd->vdev_aux == &spa->spa_l2cache &&
1932573ca77eSGeorge Wilson 		    !l2arc_vdev_present(vd))
1933573ca77eSGeorge Wilson 			l2arc_add_vdev(spa, vd);
1934c5904d13Seschrock 	} else {
19356f793812SPavel Zakharov 		(void) vdev_validate(vd);
1936c5904d13Seschrock 	}
193739c23413Seschrock 
1938fa9e4066Sahrens 	/*
19393d7072f8Seschrock 	 * Reassess parent vdev's health.
1940fa9e4066Sahrens 	 */
19413d7072f8Seschrock 	vdev_propagate_state(vd);
1942fa9e4066Sahrens }
1943fa9e4066Sahrens 
1944fa9e4066Sahrens int
194599653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1946fa9e4066Sahrens {
1947fa9e4066Sahrens 	int error;
1948fa9e4066Sahrens 
1949fa9e4066Sahrens 	/*
1950fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
1951fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
1952fa9e4066Sahrens 	 * there are any components we can't open.
1953fa9e4066Sahrens 	 */
1954fa9e4066Sahrens 	error = vdev_open(vd);
1955fa9e4066Sahrens 
1956fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1957fa9e4066Sahrens 		vdev_close(vd);
1958fa9e4066Sahrens 		return (error ? error : ENXIO);
1959fa9e4066Sahrens 	}
1960fa9e4066Sahrens 
1961fa9e4066Sahrens 	/*
19620713e232SGeorge Wilson 	 * Recursively load DTLs and initialize all labels.
1963fa9e4066Sahrens 	 */
19640713e232SGeorge Wilson 	if ((error = vdev_dtl_load(vd)) != 0 ||
19650713e232SGeorge Wilson 	    (error = vdev_label_init(vd, txg, isreplacing ?
196639c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1967fa9e4066Sahrens 		vdev_close(vd);
1968fa9e4066Sahrens 		return (error);
1969fa9e4066Sahrens 	}
1970fa9e4066Sahrens 
1971fa9e4066Sahrens 	return (0);
1972fa9e4066Sahrens }
1973fa9e4066Sahrens 
19740e34b6a7Sbonwick void
1975573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
1976fa9e4066Sahrens {
1977*86714001SSerapheim Dimitropoulos 	uint64_t asize = vd->vdev_asize;
1978*86714001SSerapheim Dimitropoulos 	uint64_t ms_shift = 0;
1979*86714001SSerapheim Dimitropoulos 
1980fa9e4066Sahrens 	/*
1981*86714001SSerapheim Dimitropoulos 	 * For vdevs that are bigger than 8G the metaslab size varies in
1982*86714001SSerapheim Dimitropoulos 	 * a way that the number of metaslabs increases in powers of two,
1983*86714001SSerapheim Dimitropoulos 	 * linearly in terms of vdev_asize, starting from 16 metaslabs.
1984*86714001SSerapheim Dimitropoulos 	 * So for vdev_asize of 8G we get 16 metaslabs, for 16G, we get 32,
1985*86714001SSerapheim Dimitropoulos 	 * and so on, until we hit the maximum metaslab count limit
1986*86714001SSerapheim Dimitropoulos 	 * [vdev_max_ms_count] from which point the metaslab count stays
1987*86714001SSerapheim Dimitropoulos 	 * the same.
1988fa9e4066Sahrens 	 */
1989*86714001SSerapheim Dimitropoulos 	ms_shift = vdev_default_ms_shift;
1990*86714001SSerapheim Dimitropoulos 
1991*86714001SSerapheim Dimitropoulos 	if ((asize >> ms_shift) < vdev_min_ms_count) {
1992*86714001SSerapheim Dimitropoulos 		/*
1993*86714001SSerapheim Dimitropoulos 		 * For devices that are less than 8G we want to have
1994*86714001SSerapheim Dimitropoulos 		 * exactly 16 metaslabs. We don't want less as integer
1995*86714001SSerapheim Dimitropoulos 		 * division rounds down, so less metaslabs mean more
1996*86714001SSerapheim Dimitropoulos 		 * wasted space. We don't want more as these vdevs are
1997*86714001SSerapheim Dimitropoulos 		 * small and in the likely event that we are running
1998*86714001SSerapheim Dimitropoulos 		 * out of space, the SPA will have a hard time finding
1999*86714001SSerapheim Dimitropoulos 		 * space due to fragmentation.
2000*86714001SSerapheim Dimitropoulos 		 */
2001*86714001SSerapheim Dimitropoulos 		ms_shift = highbit64(asize / vdev_min_ms_count);
2002*86714001SSerapheim Dimitropoulos 		ms_shift = MAX(ms_shift, SPA_MAXBLOCKSHIFT);
2003*86714001SSerapheim Dimitropoulos 
2004*86714001SSerapheim Dimitropoulos 	} else if ((asize >> ms_shift) > vdev_max_ms_count) {
2005*86714001SSerapheim Dimitropoulos 		ms_shift = highbit64(asize / vdev_max_ms_count);
2006*86714001SSerapheim Dimitropoulos 	}
2007*86714001SSerapheim Dimitropoulos 
2008*86714001SSerapheim Dimitropoulos 	vd->vdev_ms_shift = ms_shift;
2009*86714001SSerapheim Dimitropoulos 	ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2010fa9e4066Sahrens }
2011fa9e4066Sahrens 
2012fa9e4066Sahrens void
2013ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2014fa9e4066Sahrens {
2015ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
20165cabbc6bSPrashanth Sreenivasa 	/* indirect vdevs don't have metaslabs or dtls */
20175cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd) || flags == 0);
2018ecc2d604Sbonwick 	ASSERT(ISP2(flags));
2019f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
2020fa9e4066Sahrens 
2021ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
2022ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2023ecc2d604Sbonwick 
2024ecc2d604Sbonwick 	if (flags & VDD_DTL)
2025ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2026ecc2d604Sbonwick 
2027ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2028fa9e4066Sahrens }
2029fa9e4066Sahrens 
20300713e232SGeorge Wilson void
20310713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
20320713e232SGeorge Wilson {
20330713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
20340713e232SGeorge Wilson 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
20350713e232SGeorge Wilson 
20360713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
20370713e232SGeorge Wilson 		vdev_dirty(vd->vdev_top, flags, vd, txg);
20380713e232SGeorge Wilson }
20390713e232SGeorge Wilson 
20408ad4d6ddSJeff Bonwick /*
20418ad4d6ddSJeff Bonwick  * DTLs.
20428ad4d6ddSJeff Bonwick  *
20438ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
20449fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
20458ad4d6ddSJeff Bonwick  *
20468ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
20478ad4d6ddSJeff Bonwick  *
20488ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
20498ad4d6ddSJeff Bonwick  *
20508ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
20518ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
20528ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
20538ad4d6ddSJeff Bonwick  *
20548ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
20558ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
20568ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
20578ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
20588ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
20598ad4d6ddSJeff Bonwick  *
20608ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
20618ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
20628ad4d6ddSJeff Bonwick  *
20638ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
20648ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
20658ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
20668ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
20678ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
20688ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
20698ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
20708ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
20718ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
20728ad4d6ddSJeff Bonwick  *
20738ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
20748ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
20758ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
20768ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
20778ad4d6ddSJeff Bonwick  */
2078fa9e4066Sahrens void
20798ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2080fa9e4066Sahrens {
20810713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
20828ad4d6ddSJeff Bonwick 
20838ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
20848ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2085f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
20868ad4d6ddSJeff Bonwick 
20875cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
20880713e232SGeorge Wilson 	if (!range_tree_contains(rt, txg, size))
20890713e232SGeorge Wilson 		range_tree_add(rt, txg, size);
20905cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2091fa9e4066Sahrens }
2092fa9e4066Sahrens 
20938ad4d6ddSJeff Bonwick boolean_t
20948ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2095fa9e4066Sahrens {
20960713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
20978ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
2098fa9e4066Sahrens 
20998ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
21008ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2101fa9e4066Sahrens 
21025cabbc6bSPrashanth Sreenivasa 	/*
21035cabbc6bSPrashanth Sreenivasa 	 * While we are loading the pool, the DTLs have not been loaded yet.
21045cabbc6bSPrashanth Sreenivasa 	 * Ignore the DTLs and try all devices.  This avoids a recursive
21055cabbc6bSPrashanth Sreenivasa 	 * mutex enter on the vdev_dtl_lock, and also makes us try hard
21065cabbc6bSPrashanth Sreenivasa 	 * when loading the pool (relying on the checksum to ensure that
21075cabbc6bSPrashanth Sreenivasa 	 * we get the right data -- note that we while loading, we are
21085cabbc6bSPrashanth Sreenivasa 	 * only reading the MOS, which is always checksummed).
21095cabbc6bSPrashanth Sreenivasa 	 */
21105cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
21115cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
21125cabbc6bSPrashanth Sreenivasa 
21135cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
2114*86714001SSerapheim Dimitropoulos 	if (!range_tree_is_empty(rt))
21150713e232SGeorge Wilson 		dirty = range_tree_contains(rt, txg, size);
21165cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2117fa9e4066Sahrens 
2118fa9e4066Sahrens 	return (dirty);
2119fa9e4066Sahrens }
2120fa9e4066Sahrens 
21218ad4d6ddSJeff Bonwick boolean_t
21228ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
21238ad4d6ddSJeff Bonwick {
21240713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
21258ad4d6ddSJeff Bonwick 	boolean_t empty;
21268ad4d6ddSJeff Bonwick 
21275cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
2128*86714001SSerapheim Dimitropoulos 	empty = range_tree_is_empty(rt);
21295cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
21308ad4d6ddSJeff Bonwick 
21318ad4d6ddSJeff Bonwick 	return (empty);
21328ad4d6ddSJeff Bonwick }
21338ad4d6ddSJeff Bonwick 
2134b4952e17SGeorge Wilson /*
2135b4952e17SGeorge Wilson  * Returns the lowest txg in the DTL range.
2136b4952e17SGeorge Wilson  */
2137b4952e17SGeorge Wilson static uint64_t
2138b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd)
2139b4952e17SGeorge Wilson {
21400713e232SGeorge Wilson 	range_seg_t *rs;
2141b4952e17SGeorge Wilson 
2142b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
21430713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2144b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2145b4952e17SGeorge Wilson 
21460713e232SGeorge Wilson 	rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
21470713e232SGeorge Wilson 	return (rs->rs_start - 1);
2148b4952e17SGeorge Wilson }
2149b4952e17SGeorge Wilson 
2150b4952e17SGeorge Wilson /*
2151b4952e17SGeorge Wilson  * Returns the highest txg in the DTL.
2152b4952e17SGeorge Wilson  */
2153b4952e17SGeorge Wilson static uint64_t
2154b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd)
2155b4952e17SGeorge Wilson {
21560713e232SGeorge Wilson 	range_seg_t *rs;
2157b4952e17SGeorge Wilson 
2158b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
21590713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2160b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2161b4952e17SGeorge Wilson 
21620713e232SGeorge Wilson 	rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
21630713e232SGeorge Wilson 	return (rs->rs_end);
2164b4952e17SGeorge Wilson }
2165b4952e17SGeorge Wilson 
2166b4952e17SGeorge Wilson /*
2167b4952e17SGeorge Wilson  * Determine if a resilvering vdev should remove any DTL entries from
2168b4952e17SGeorge Wilson  * its range. If the vdev was resilvering for the entire duration of the
2169b4952e17SGeorge Wilson  * scan then it should excise that range from its DTLs. Otherwise, this
2170b4952e17SGeorge Wilson  * vdev is considered partially resilvered and should leave its DTL
2171b4952e17SGeorge Wilson  * entries intact. The comment in vdev_dtl_reassess() describes how we
2172b4952e17SGeorge Wilson  * excise the DTLs.
2173b4952e17SGeorge Wilson  */
2174b4952e17SGeorge Wilson static boolean_t
2175b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd)
2176b4952e17SGeorge Wilson {
2177b4952e17SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2178b4952e17SGeorge Wilson 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2179b4952e17SGeorge Wilson 
2180b4952e17SGeorge Wilson 	ASSERT0(scn->scn_phys.scn_errors);
2181b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2182b4952e17SGeorge Wilson 
21832d2f193aSMatthew Ahrens 	if (vd->vdev_state < VDEV_STATE_DEGRADED)
21842d2f193aSMatthew Ahrens 		return (B_FALSE);
21852d2f193aSMatthew Ahrens 
2186b4952e17SGeorge Wilson 	if (vd->vdev_resilver_txg == 0 ||
2187*86714001SSerapheim Dimitropoulos 	    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2188b4952e17SGeorge Wilson 		return (B_TRUE);
2189b4952e17SGeorge Wilson 
2190b4952e17SGeorge Wilson 	/*
2191b4952e17SGeorge Wilson 	 * When a resilver is initiated the scan will assign the scn_max_txg
2192b4952e17SGeorge Wilson 	 * value to the highest txg value that exists in all DTLs. If this
2193b4952e17SGeorge Wilson 	 * device's max DTL is not part of this scan (i.e. it is not in
2194b4952e17SGeorge Wilson 	 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2195b4952e17SGeorge Wilson 	 * for excision.
2196b4952e17SGeorge Wilson 	 */
2197b4952e17SGeorge Wilson 	if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2198b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2199b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2200b4952e17SGeorge Wilson 		ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2201b4952e17SGeorge Wilson 		return (B_TRUE);
2202b4952e17SGeorge Wilson 	}
2203b4952e17SGeorge Wilson 	return (B_FALSE);
2204b4952e17SGeorge Wilson }
2205b4952e17SGeorge Wilson 
2206fa9e4066Sahrens /*
2207fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
2208fa9e4066Sahrens  */
2209fa9e4066Sahrens void
2210fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2211fa9e4066Sahrens {
2212ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
22138ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
22148ad4d6ddSJeff Bonwick 	int minref;
2215fa9e4066Sahrens 
22168ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2217fa9e4066Sahrens 
22188ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
22198ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
22208ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
22218ad4d6ddSJeff Bonwick 
22225cabbc6bSPrashanth Sreenivasa 	if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
22238ad4d6ddSJeff Bonwick 		return;
22248ad4d6ddSJeff Bonwick 
22258ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
22263f9d6ad7SLin Ling 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
22273f9d6ad7SLin Ling 
2228fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
2229b4952e17SGeorge Wilson 
2230b4952e17SGeorge Wilson 		/*
2231b4952e17SGeorge Wilson 		 * If we've completed a scan cleanly then determine
2232b4952e17SGeorge Wilson 		 * if this vdev should remove any DTLs. We only want to
2233b4952e17SGeorge Wilson 		 * excise regions on vdevs that were available during
2234b4952e17SGeorge Wilson 		 * the entire duration of this scan.
2235b4952e17SGeorge Wilson 		 */
2236088f3894Sahrens 		if (scrub_txg != 0 &&
22373f9d6ad7SLin Ling 		    (spa->spa_scrub_started ||
2238b4952e17SGeorge Wilson 		    (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2239b4952e17SGeorge Wilson 		    vdev_dtl_should_excise(vd)) {
2240088f3894Sahrens 			/*
2241088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
2242088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
2243088f3894Sahrens 			 * will be valid, so excise the old region and
2244088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
2245088f3894Sahrens 			 * dtl as-is if there was an error.
22468ad4d6ddSJeff Bonwick 			 *
22478ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
22488ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
22498ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
22508ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
22518ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
22528ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
22538ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
22548ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
22558ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
2256088f3894Sahrens 			 */
22570713e232SGeorge Wilson 			space_reftree_create(&reftree);
22580713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
22590713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
22600713e232SGeorge Wilson 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
22610713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
22620713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_SCRUB], 2);
22630713e232SGeorge Wilson 			space_reftree_generate_map(&reftree,
22640713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
22650713e232SGeorge Wilson 			space_reftree_destroy(&reftree);
2266fa9e4066Sahrens 		}
22670713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
22680713e232SGeorge Wilson 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
22690713e232SGeorge Wilson 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2270fa9e4066Sahrens 		if (scrub_done)
22710713e232SGeorge Wilson 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
22720713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
22738ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
22740713e232SGeorge Wilson 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
22758ad4d6ddSJeff Bonwick 		else
22760713e232SGeorge Wilson 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
22770713e232SGeorge Wilson 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2278b4952e17SGeorge Wilson 
2279b4952e17SGeorge Wilson 		/*
2280b4952e17SGeorge Wilson 		 * If the vdev was resilvering and no longer has any
2281b4952e17SGeorge Wilson 		 * DTLs then reset its resilvering flag.
2282b4952e17SGeorge Wilson 		 */
2283b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0 &&
2284*86714001SSerapheim Dimitropoulos 		    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
2285*86714001SSerapheim Dimitropoulos 		    range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE]))
2286b4952e17SGeorge Wilson 			vd->vdev_resilver_txg = 0;
2287b4952e17SGeorge Wilson 
2288fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2289088f3894Sahrens 
2290ecc2d604Sbonwick 		if (txg != 0)
2291ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2292fa9e4066Sahrens 		return;
2293fa9e4066Sahrens 	}
2294fa9e4066Sahrens 
2295fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
22968ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
229799bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
229899bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
22998ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
23008ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
23018ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
23028ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
23038ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
23048ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
23058ad4d6ddSJeff Bonwick 		else
23068ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
23070713e232SGeorge Wilson 		space_reftree_create(&reftree);
23088ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
23098ad4d6ddSJeff Bonwick 			vdev_t *cvd = vd->vdev_child[c];
23108ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
23110713e232SGeorge Wilson 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
23128ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
23138ad4d6ddSJeff Bonwick 		}
23140713e232SGeorge Wilson 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
23150713e232SGeorge Wilson 		space_reftree_destroy(&reftree);
2316fa9e4066Sahrens 	}
23178ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
2318fa9e4066Sahrens }
2319fa9e4066Sahrens 
23200713e232SGeorge Wilson int
2321fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
2322fa9e4066Sahrens {
2323fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2324ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
23250713e232SGeorge Wilson 	int error = 0;
2326fa9e4066Sahrens 
23270713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
23285cabbc6bSPrashanth Sreenivasa 		ASSERT(vdev_is_concrete(vd));
2329fa9e4066Sahrens 
23300713e232SGeorge Wilson 		error = space_map_open(&vd->vdev_dtl_sm, mos,
23315cabbc6bSPrashanth Sreenivasa 		    vd->vdev_dtl_object, 0, -1ULL, 0);
23320713e232SGeorge Wilson 		if (error)
23330713e232SGeorge Wilson 			return (error);
23340713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2335fa9e4066Sahrens 
23360713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
233788ecc943SGeorge Wilson 
23380713e232SGeorge Wilson 		/*
23390713e232SGeorge Wilson 		 * Now that we've opened the space_map we need to update
23400713e232SGeorge Wilson 		 * the in-core DTL.
23410713e232SGeorge Wilson 		 */
23420713e232SGeorge Wilson 		space_map_update(vd->vdev_dtl_sm);
2343ecc2d604Sbonwick 
23440713e232SGeorge Wilson 		error = space_map_load(vd->vdev_dtl_sm,
23450713e232SGeorge Wilson 		    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
23460713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2347fa9e4066Sahrens 
23480713e232SGeorge Wilson 		return (error);
23490713e232SGeorge Wilson 	}
23500713e232SGeorge Wilson 
23510713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
23520713e232SGeorge Wilson 		error = vdev_dtl_load(vd->vdev_child[c]);
23530713e232SGeorge Wilson 		if (error != 0)
23540713e232SGeorge Wilson 			break;
23550713e232SGeorge Wilson 	}
2356fa9e4066Sahrens 
2357fa9e4066Sahrens 	return (error);
2358fa9e4066Sahrens }
2359fa9e4066Sahrens 
2360215198a6SJoe Stein void
2361215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2362215198a6SJoe Stein {
2363215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2364215198a6SJoe Stein 
2365215198a6SJoe Stein 	VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2366215198a6SJoe Stein 	VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2367215198a6SJoe Stein 	    zapobj, tx));
2368215198a6SJoe Stein }
2369215198a6SJoe Stein 
2370215198a6SJoe Stein uint64_t
2371215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2372215198a6SJoe Stein {
2373215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2374215198a6SJoe Stein 	uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2375215198a6SJoe Stein 	    DMU_OT_NONE, 0, tx);
2376215198a6SJoe Stein 
2377215198a6SJoe Stein 	ASSERT(zap != 0);
2378215198a6SJoe Stein 	VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2379215198a6SJoe Stein 	    zap, tx));
2380215198a6SJoe Stein 
2381215198a6SJoe Stein 	return (zap);
2382215198a6SJoe Stein }
2383215198a6SJoe Stein 
2384215198a6SJoe Stein void
2385215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2386215198a6SJoe Stein {
2387215198a6SJoe Stein 	if (vd->vdev_ops != &vdev_hole_ops &&
2388215198a6SJoe Stein 	    vd->vdev_ops != &vdev_missing_ops &&
2389215198a6SJoe Stein 	    vd->vdev_ops != &vdev_root_ops &&
2390215198a6SJoe Stein 	    !vd->vdev_top->vdev_removing) {
2391215198a6SJoe Stein 		if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2392215198a6SJoe Stein 			vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2393215198a6SJoe Stein 		}
2394215198a6SJoe Stein 		if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2395215198a6SJoe Stein 			vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2396215198a6SJoe Stein 		}
2397215198a6SJoe Stein 	}
2398215198a6SJoe Stein 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
2399215198a6SJoe Stein 		vdev_construct_zaps(vd->vdev_child[i], tx);
2400215198a6SJoe Stein 	}
2401215198a6SJoe Stein }
2402215198a6SJoe Stein 
2403fa9e4066Sahrens void
2404fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2405fa9e4066Sahrens {
2406fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
24070713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2408ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
24090713e232SGeorge Wilson 	range_tree_t *rtsync;
2410fa9e4066Sahrens 	dmu_tx_t *tx;
24110713e232SGeorge Wilson 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
2412fa9e4066Sahrens 
24135cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
24140713e232SGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
241588ecc943SGeorge Wilson 
2416fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2417fa9e4066Sahrens 
24180713e232SGeorge Wilson 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
24190713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
24200713e232SGeorge Wilson 		space_map_free(vd->vdev_dtl_sm, tx);
24210713e232SGeorge Wilson 		space_map_close(vd->vdev_dtl_sm);
24220713e232SGeorge Wilson 		vd->vdev_dtl_sm = NULL;
24230713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2424215198a6SJoe Stein 
2425215198a6SJoe Stein 		/*
2426215198a6SJoe Stein 		 * We only destroy the leaf ZAP for detached leaves or for
2427215198a6SJoe Stein 		 * removed log devices. Removed data devices handle leaf ZAP
2428215198a6SJoe Stein 		 * cleanup later, once cancellation is no longer possible.
2429215198a6SJoe Stein 		 */
2430215198a6SJoe Stein 		if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2431215198a6SJoe Stein 		    vd->vdev_top->vdev_islog)) {
2432215198a6SJoe Stein 			vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2433215198a6SJoe Stein 			vd->vdev_leaf_zap = 0;
2434215198a6SJoe Stein 		}
2435215198a6SJoe Stein 
2436fa9e4066Sahrens 		dmu_tx_commit(tx);
2437fa9e4066Sahrens 		return;
2438fa9e4066Sahrens 	}
2439fa9e4066Sahrens 
24400713e232SGeorge Wilson 	if (vd->vdev_dtl_sm == NULL) {
24410713e232SGeorge Wilson 		uint64_t new_object;
24420713e232SGeorge Wilson 
2443*86714001SSerapheim Dimitropoulos 		new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx);
24440713e232SGeorge Wilson 		VERIFY3U(new_object, !=, 0);
24450713e232SGeorge Wilson 
24460713e232SGeorge Wilson 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
24475cabbc6bSPrashanth Sreenivasa 		    0, -1ULL, 0));
24480713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2449fa9e4066Sahrens 	}
2450fa9e4066Sahrens 
24515cabbc6bSPrashanth Sreenivasa 	rtsync = range_tree_create(NULL, NULL);
2452fa9e4066Sahrens 
2453fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
24540713e232SGeorge Wilson 	range_tree_walk(rt, range_tree_add, rtsync);
2455fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
2456fa9e4066Sahrens 
2457*86714001SSerapheim Dimitropoulos 	space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx);
24580713e232SGeorge Wilson 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
24590713e232SGeorge Wilson 	range_tree_vacate(rtsync, NULL, NULL);
2460fa9e4066Sahrens 
24610713e232SGeorge Wilson 	range_tree_destroy(rtsync);
2462fa9e4066Sahrens 
24630713e232SGeorge Wilson 	/*
24640713e232SGeorge Wilson 	 * If the object for the space map has changed then dirty
24650713e232SGeorge Wilson 	 * the top level so that we update the config.
24660713e232SGeorge Wilson 	 */
24670713e232SGeorge Wilson 	if (object != space_map_object(vd->vdev_dtl_sm)) {
24683ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
24693ee8c80cSPavel Zakharov 		    "new object %llu", (u_longlong_t)txg, spa_name(spa),
24703ee8c80cSPavel Zakharov 		    (u_longlong_t)object,
24713ee8c80cSPavel Zakharov 		    (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
24720713e232SGeorge Wilson 		vdev_config_dirty(vd->vdev_top);
24730713e232SGeorge Wilson 	}
2474fa9e4066Sahrens 
2475fa9e4066Sahrens 	dmu_tx_commit(tx);
24760713e232SGeorge Wilson 
24770713e232SGeorge Wilson 	mutex_enter(&vd->vdev_dtl_lock);
24780713e232SGeorge Wilson 	space_map_update(vd->vdev_dtl_sm);
24790713e232SGeorge Wilson 	mutex_exit(&vd->vdev_dtl_lock);
2480fa9e4066Sahrens }
2481fa9e4066Sahrens 
24828ad4d6ddSJeff Bonwick /*
24838ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
24848ad4d6ddSJeff Bonwick  * without losing data.
24858ad4d6ddSJeff Bonwick  */
24868ad4d6ddSJeff Bonwick boolean_t
24878ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
24888ad4d6ddSJeff Bonwick {
24898ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
24908ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
24918ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
24928ad4d6ddSJeff Bonwick 	boolean_t required;
24938ad4d6ddSJeff Bonwick 
24948ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
24958ad4d6ddSJeff Bonwick 
24968ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
24978ad4d6ddSJeff Bonwick 		return (B_TRUE);
24988ad4d6ddSJeff Bonwick 
24998ad4d6ddSJeff Bonwick 	/*
25008ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
25018ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
25028ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
25038ad4d6ddSJeff Bonwick 	 */
25048ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
25058ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
25068ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
25078ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
25088ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
25098ad4d6ddSJeff Bonwick 
2510cb04b873SMark J Musante 	if (!required && zio_injection_enabled)
2511cb04b873SMark J Musante 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2512cb04b873SMark J Musante 
25138ad4d6ddSJeff Bonwick 	return (required);
25148ad4d6ddSJeff Bonwick }
25158ad4d6ddSJeff Bonwick 
2516088f3894Sahrens /*
2517088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
2518088f3894Sahrens  */
2519088f3894Sahrens boolean_t
2520088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2521088f3894Sahrens {
2522088f3894Sahrens 	boolean_t needed = B_FALSE;
2523088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
2524088f3894Sahrens 	uint64_t thismax = 0;
2525088f3894Sahrens 
2526088f3894Sahrens 	if (vd->vdev_children == 0) {
2527088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
2528*86714001SSerapheim Dimitropoulos 		if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
25298ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
2530088f3894Sahrens 
2531b4952e17SGeorge Wilson 			thismin = vdev_dtl_min(vd);
2532b4952e17SGeorge Wilson 			thismax = vdev_dtl_max(vd);
2533088f3894Sahrens 			needed = B_TRUE;
2534088f3894Sahrens 		}
2535088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2536088f3894Sahrens 	} else {
25378ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
2538088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
2539088f3894Sahrens 			uint64_t cmin, cmax;
2540088f3894Sahrens 
2541088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2542088f3894Sahrens 				thismin = MIN(thismin, cmin);
2543088f3894Sahrens 				thismax = MAX(thismax, cmax);
2544088f3894Sahrens 				needed = B_TRUE;
2545088f3894Sahrens 			}
2546088f3894Sahrens 		}
2547088f3894Sahrens 	}
2548088f3894Sahrens 
2549088f3894Sahrens 	if (needed && minp) {
2550088f3894Sahrens 		*minp = thismin;
2551088f3894Sahrens 		*maxp = thismax;
2552088f3894Sahrens 	}
2553088f3894Sahrens 	return (needed);
2554088f3894Sahrens }
2555088f3894Sahrens 
2556*86714001SSerapheim Dimitropoulos /*
2557*86714001SSerapheim Dimitropoulos  * Gets the checkpoint space map object from the vdev's ZAP.
2558*86714001SSerapheim Dimitropoulos  * Returns the spacemap object, or 0 if it wasn't in the ZAP
2559*86714001SSerapheim Dimitropoulos  * or the ZAP doesn't exist yet.
2560*86714001SSerapheim Dimitropoulos  */
2561*86714001SSerapheim Dimitropoulos int
2562*86714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd)
2563*86714001SSerapheim Dimitropoulos {
2564*86714001SSerapheim Dimitropoulos 	ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
2565*86714001SSerapheim Dimitropoulos 	if (vd->vdev_top_zap == 0) {
2566*86714001SSerapheim Dimitropoulos 		return (0);
2567*86714001SSerapheim Dimitropoulos 	}
2568*86714001SSerapheim Dimitropoulos 
2569*86714001SSerapheim Dimitropoulos 	uint64_t sm_obj = 0;
2570*86714001SSerapheim Dimitropoulos 	int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
2571*86714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj);
2572*86714001SSerapheim Dimitropoulos 
2573*86714001SSerapheim Dimitropoulos 	ASSERT(err == 0 || err == ENOENT);
2574*86714001SSerapheim Dimitropoulos 
2575*86714001SSerapheim Dimitropoulos 	return (sm_obj);
2576*86714001SSerapheim Dimitropoulos }
2577*86714001SSerapheim Dimitropoulos 
25785cabbc6bSPrashanth Sreenivasa int
2579ea8dc4b6Seschrock vdev_load(vdev_t *vd)
2580fa9e4066Sahrens {
25815cabbc6bSPrashanth Sreenivasa 	int error = 0;
2582fa9e4066Sahrens 	/*
2583fa9e4066Sahrens 	 * Recursively load all children.
2584fa9e4066Sahrens 	 */
25855cabbc6bSPrashanth Sreenivasa 	for (int c = 0; c < vd->vdev_children; c++) {
25865cabbc6bSPrashanth Sreenivasa 		error = vdev_load(vd->vdev_child[c]);
25875cabbc6bSPrashanth Sreenivasa 		if (error != 0) {
25885cabbc6bSPrashanth Sreenivasa 			return (error);
25895cabbc6bSPrashanth Sreenivasa 		}
25905cabbc6bSPrashanth Sreenivasa 	}
25915cabbc6bSPrashanth Sreenivasa 
25925cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
2593fa9e4066Sahrens 
2594fa9e4066Sahrens 	/*
25950e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
2596fa9e4066Sahrens 	 */
25975cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
25985cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
25995cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
26005cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
26013ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
26023ee8c80cSPavel Zakharov 			    "asize=%llu", (u_longlong_t)vd->vdev_ashift,
26033ee8c80cSPavel Zakharov 			    (u_longlong_t)vd->vdev_asize);
26045cabbc6bSPrashanth Sreenivasa 			return (SET_ERROR(ENXIO));
26055cabbc6bSPrashanth Sreenivasa 		} else if ((error = vdev_metaslab_init(vd, 0)) != 0) {
26063ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
26073ee8c80cSPavel Zakharov 			    "[error=%d]", error);
26085cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
26095cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
26105cabbc6bSPrashanth Sreenivasa 			return (error);
26115cabbc6bSPrashanth Sreenivasa 		}
2612*86714001SSerapheim Dimitropoulos 
2613*86714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd);
2614*86714001SSerapheim Dimitropoulos 		if (checkpoint_sm_obj != 0) {
2615*86714001SSerapheim Dimitropoulos 			objset_t *mos = spa_meta_objset(vd->vdev_spa);
2616*86714001SSerapheim Dimitropoulos 			ASSERT(vd->vdev_asize != 0);
2617*86714001SSerapheim Dimitropoulos 			ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
2618*86714001SSerapheim Dimitropoulos 
2619*86714001SSerapheim Dimitropoulos 			if ((error = space_map_open(&vd->vdev_checkpoint_sm,
2620*86714001SSerapheim Dimitropoulos 			    mos, checkpoint_sm_obj, 0, vd->vdev_asize,
2621*86714001SSerapheim Dimitropoulos 			    vd->vdev_ashift))) {
2622*86714001SSerapheim Dimitropoulos 				vdev_dbgmsg(vd, "vdev_load: space_map_open "
2623*86714001SSerapheim Dimitropoulos 				    "failed for checkpoint spacemap (obj %llu) "
2624*86714001SSerapheim Dimitropoulos 				    "[error=%d]",
2625*86714001SSerapheim Dimitropoulos 				    (u_longlong_t)checkpoint_sm_obj, error);
2626*86714001SSerapheim Dimitropoulos 				return (error);
2627*86714001SSerapheim Dimitropoulos 			}
2628*86714001SSerapheim Dimitropoulos 			ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
2629*86714001SSerapheim Dimitropoulos 			space_map_update(vd->vdev_checkpoint_sm);
2630*86714001SSerapheim Dimitropoulos 
2631*86714001SSerapheim Dimitropoulos 			/*
2632*86714001SSerapheim Dimitropoulos 			 * Since the checkpoint_sm contains free entries
2633*86714001SSerapheim Dimitropoulos 			 * exclusively we can use sm_alloc to indicate the
2634*86714001SSerapheim Dimitropoulos 			 * culmulative checkpointed space that has been freed.
2635*86714001SSerapheim Dimitropoulos 			 */
2636*86714001SSerapheim Dimitropoulos 			vd->vdev_stat.vs_checkpoint_space =
2637*86714001SSerapheim Dimitropoulos 			    -vd->vdev_checkpoint_sm->sm_alloc;
2638*86714001SSerapheim Dimitropoulos 			vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
2639*86714001SSerapheim Dimitropoulos 			    vd->vdev_stat.vs_checkpoint_space;
2640*86714001SSerapheim Dimitropoulos 		}
26415cabbc6bSPrashanth Sreenivasa 	}
2642fa9e4066Sahrens 
2643fa9e4066Sahrens 	/*
2644fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
2645fa9e4066Sahrens 	 */
26465cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
2647560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2648560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
26493ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
26503ee8c80cSPavel Zakharov 		    "[error=%d]", error);
26515cabbc6bSPrashanth Sreenivasa 		return (error);
26525cabbc6bSPrashanth Sreenivasa 	}
26535cabbc6bSPrashanth Sreenivasa 
26545cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
26555cabbc6bSPrashanth Sreenivasa 	if (obsolete_sm_object != 0) {
26565cabbc6bSPrashanth Sreenivasa 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
26575cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_asize != 0);
2658*86714001SSerapheim Dimitropoulos 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
26595cabbc6bSPrashanth Sreenivasa 
26605cabbc6bSPrashanth Sreenivasa 		if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
26615cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_object, 0, vd->vdev_asize, 0))) {
26625cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
26635cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
26643ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
26653ee8c80cSPavel Zakharov 			    "obsolete spacemap (obj %llu) [error=%d]",
26663ee8c80cSPavel Zakharov 			    (u_longlong_t)obsolete_sm_object, error);
26675cabbc6bSPrashanth Sreenivasa 			return (error);
26685cabbc6bSPrashanth Sreenivasa 		}
26695cabbc6bSPrashanth Sreenivasa 		space_map_update(vd->vdev_obsolete_sm);
26705cabbc6bSPrashanth Sreenivasa 	}
26715cabbc6bSPrashanth Sreenivasa 
26725cabbc6bSPrashanth Sreenivasa 	return (0);
2673fa9e4066Sahrens }
2674fa9e4066Sahrens 
267599653d4eSeschrock /*
2676fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
2677fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
2678fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
2679fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
2680fa94a07fSbrendan  * repurposed to another pool.
268199653d4eSeschrock  */
268299653d4eSeschrock int
2683fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
268499653d4eSeschrock {
268599653d4eSeschrock 	nvlist_t *label;
268699653d4eSeschrock 	uint64_t guid, version;
268799653d4eSeschrock 	uint64_t state;
268899653d4eSeschrock 
2689e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
2690c5904d13Seschrock 		return (0);
2691c5904d13Seschrock 
2692dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
269399653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
269499653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
269599653d4eSeschrock 		return (-1);
269699653d4eSeschrock 	}
269799653d4eSeschrock 
269899653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2699ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(version) ||
270099653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
270199653d4eSeschrock 	    guid != vd->vdev_guid ||
270299653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
270399653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
270499653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
270599653d4eSeschrock 		nvlist_free(label);
270699653d4eSeschrock 		return (-1);
270799653d4eSeschrock 	}
270899653d4eSeschrock 
270999653d4eSeschrock 	/*
271099653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
271199653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
271299653d4eSeschrock 	 */
271399653d4eSeschrock 	nvlist_free(label);
271499653d4eSeschrock 	return (0);
271599653d4eSeschrock }
271699653d4eSeschrock 
27175cabbc6bSPrashanth Sreenivasa /*
27185cabbc6bSPrashanth Sreenivasa  * Free the objects used to store this vdev's spacemaps, and the array
27195cabbc6bSPrashanth Sreenivasa  * that points to them.
27205cabbc6bSPrashanth Sreenivasa  */
272188ecc943SGeorge Wilson void
27225cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
27235cabbc6bSPrashanth Sreenivasa {
27245cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0)
27255cabbc6bSPrashanth Sreenivasa 		return;
27265cabbc6bSPrashanth Sreenivasa 
27275cabbc6bSPrashanth Sreenivasa 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
27285cabbc6bSPrashanth Sreenivasa 	uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
27295cabbc6bSPrashanth Sreenivasa 	size_t array_bytes = array_count * sizeof (uint64_t);
27305cabbc6bSPrashanth Sreenivasa 	uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
27315cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
27325cabbc6bSPrashanth Sreenivasa 	    array_bytes, smobj_array, 0));
27335cabbc6bSPrashanth Sreenivasa 
27345cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < array_count; i++) {
27355cabbc6bSPrashanth Sreenivasa 		uint64_t smobj = smobj_array[i];
27365cabbc6bSPrashanth Sreenivasa 		if (smobj == 0)
27375cabbc6bSPrashanth Sreenivasa 			continue;
27385cabbc6bSPrashanth Sreenivasa 
27395cabbc6bSPrashanth Sreenivasa 		space_map_free_obj(mos, smobj, tx);
27405cabbc6bSPrashanth Sreenivasa 	}
27415cabbc6bSPrashanth Sreenivasa 
27425cabbc6bSPrashanth Sreenivasa 	kmem_free(smobj_array, array_bytes);
27435cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
27445cabbc6bSPrashanth Sreenivasa 	vd->vdev_ms_array = 0;
27455cabbc6bSPrashanth Sreenivasa }
27465cabbc6bSPrashanth Sreenivasa 
27475cabbc6bSPrashanth Sreenivasa static void
27485cabbc6bSPrashanth Sreenivasa vdev_remove_empty(vdev_t *vd, uint64_t txg)
274988ecc943SGeorge Wilson {
275088ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
275188ecc943SGeorge Wilson 	dmu_tx_t *tx;
275288ecc943SGeorge Wilson 
2753215198a6SJoe Stein 	ASSERT(vd == vd->vdev_top);
2754215198a6SJoe Stein 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
275588ecc943SGeorge Wilson 
275688ecc943SGeorge Wilson 	if (vd->vdev_ms != NULL) {
27572e4c9986SGeorge Wilson 		metaslab_group_t *mg = vd->vdev_mg;
27582e4c9986SGeorge Wilson 
27592e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
27602e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
27612e4c9986SGeorge Wilson 
276288ecc943SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
276388ecc943SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
276488ecc943SGeorge Wilson 
27650713e232SGeorge Wilson 			if (msp == NULL || msp->ms_sm == NULL)
276688ecc943SGeorge Wilson 				continue;
276788ecc943SGeorge Wilson 
27680713e232SGeorge Wilson 			mutex_enter(&msp->ms_lock);
27692e4c9986SGeorge Wilson 			/*
27702e4c9986SGeorge Wilson 			 * If the metaslab was not loaded when the vdev
27712e4c9986SGeorge Wilson 			 * was removed then the histogram accounting may
27722e4c9986SGeorge Wilson 			 * not be accurate. Update the histogram information
27732e4c9986SGeorge Wilson 			 * here so that we ensure that the metaslab group
27742e4c9986SGeorge Wilson 			 * and metaslab class are up-to-date.
27752e4c9986SGeorge Wilson 			 */
27762e4c9986SGeorge Wilson 			metaslab_group_histogram_remove(mg, msp);
27772e4c9986SGeorge Wilson 
27780713e232SGeorge Wilson 			VERIFY0(space_map_allocated(msp->ms_sm));
27790713e232SGeorge Wilson 			space_map_close(msp->ms_sm);
27800713e232SGeorge Wilson 			msp->ms_sm = NULL;
27810713e232SGeorge Wilson 			mutex_exit(&msp->ms_lock);
278288ecc943SGeorge Wilson 		}
27832e4c9986SGeorge Wilson 
2784*86714001SSerapheim Dimitropoulos 		if (vd->vdev_checkpoint_sm != NULL) {
2785*86714001SSerapheim Dimitropoulos 			ASSERT(spa_has_checkpoint(spa));
2786*86714001SSerapheim Dimitropoulos 			space_map_close(vd->vdev_checkpoint_sm);
2787*86714001SSerapheim Dimitropoulos 			vd->vdev_checkpoint_sm = NULL;
2788*86714001SSerapheim Dimitropoulos 		}
2789*86714001SSerapheim Dimitropoulos 
27902e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
27912e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
27922e4c9986SGeorge Wilson 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
27932e4c9986SGeorge Wilson 			ASSERT0(mg->mg_histogram[i]);
279488ecc943SGeorge Wilson 	}
279588ecc943SGeorge Wilson 
27965cabbc6bSPrashanth Sreenivasa 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
27975cabbc6bSPrashanth Sreenivasa 	vdev_destroy_spacemaps(vd, tx);
2798215198a6SJoe Stein 
2799215198a6SJoe Stein 	if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2800215198a6SJoe Stein 		vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2801215198a6SJoe Stein 		vd->vdev_top_zap = 0;
2802215198a6SJoe Stein 	}
280388ecc943SGeorge Wilson 	dmu_tx_commit(tx);
280488ecc943SGeorge Wilson }
280588ecc943SGeorge Wilson 
2806fa9e4066Sahrens void
2807fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
2808fa9e4066Sahrens {
2809fa9e4066Sahrens 	metaslab_t *msp;
281080eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2811fa9e4066Sahrens 
28125cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
281388ecc943SGeorge Wilson 
2814fa9e4066Sahrens 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2815fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
281680eb36f2SGeorge Wilson 
281780eb36f2SGeorge Wilson 	if (reassess)
281880eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
2819fa9e4066Sahrens }
2820fa9e4066Sahrens 
2821fa9e4066Sahrens void
2822fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2823fa9e4066Sahrens {
2824fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2825fa9e4066Sahrens 	vdev_t *lvd;
2826fa9e4066Sahrens 	metaslab_t *msp;
2827ecc2d604Sbonwick 	dmu_tx_t *tx;
2828fa9e4066Sahrens 
28295cabbc6bSPrashanth Sreenivasa 	if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
28305cabbc6bSPrashanth Sreenivasa 		dmu_tx_t *tx;
28315cabbc6bSPrashanth Sreenivasa 
28325cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
28335cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
283488ecc943SGeorge Wilson 
28355cabbc6bSPrashanth Sreenivasa 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
28365cabbc6bSPrashanth Sreenivasa 		vdev_indirect_sync_obsolete(vd, tx);
28375cabbc6bSPrashanth Sreenivasa 		dmu_tx_commit(tx);
28385cabbc6bSPrashanth Sreenivasa 
28395cabbc6bSPrashanth Sreenivasa 		/*
28405cabbc6bSPrashanth Sreenivasa 		 * If the vdev is indirect, it can't have dirty
28415cabbc6bSPrashanth Sreenivasa 		 * metaslabs or DTLs.
28425cabbc6bSPrashanth Sreenivasa 		 */
28435cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ops == &vdev_indirect_ops) {
28445cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
28455cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
28465cabbc6bSPrashanth Sreenivasa 			return;
28475cabbc6bSPrashanth Sreenivasa 		}
28485cabbc6bSPrashanth Sreenivasa 	}
28495cabbc6bSPrashanth Sreenivasa 
28505cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
28515cabbc6bSPrashanth Sreenivasa 
28525cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
28535cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_removing) {
2854ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
28555cabbc6bSPrashanth Sreenivasa 		ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
2856ecc2d604Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2857ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2858ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2859ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
2860ecc2d604Sbonwick 		vdev_config_dirty(vd);
2861ecc2d604Sbonwick 		dmu_tx_commit(tx);
2862ecc2d604Sbonwick 	}
2863fa9e4066Sahrens 
2864ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2865fa9e4066Sahrens 		metaslab_sync(msp, txg);
2866ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2867ecc2d604Sbonwick 	}
2868fa9e4066Sahrens 
2869fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2870fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
2871fa9e4066Sahrens 
28725cabbc6bSPrashanth Sreenivasa 	/*
28735cabbc6bSPrashanth Sreenivasa 	 * Remove the metadata associated with this vdev once it's empty.
28745cabbc6bSPrashanth Sreenivasa 	 * Note that this is typically used for log/cache device removal;
28755cabbc6bSPrashanth Sreenivasa 	 * we don't empty toplevel vdevs when removing them.  But if
28765cabbc6bSPrashanth Sreenivasa 	 * a toplevel happens to be emptied, this is not harmful.
28775cabbc6bSPrashanth Sreenivasa 	 */
28785cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) {
28795cabbc6bSPrashanth Sreenivasa 		vdev_remove_empty(vd, txg);
28805cabbc6bSPrashanth Sreenivasa 	}
28815cabbc6bSPrashanth Sreenivasa 
2882fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2883fa9e4066Sahrens }
2884fa9e4066Sahrens 
2885fa9e4066Sahrens uint64_t
2886fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2887fa9e4066Sahrens {
2888fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2889fa9e4066Sahrens }
2890fa9e4066Sahrens 
28913d7072f8Seschrock /*
28923d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
28933d7072f8Seschrock  * not be opened, and no I/O is attempted.
28943d7072f8Seschrock  */
2895fa9e4066Sahrens int
2896069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2897fa9e4066Sahrens {
28984b964adaSGeorge Wilson 	vdev_t *vd, *tvd;
2899fa9e4066Sahrens 
29008f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
2901fa9e4066Sahrens 
2902c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2903e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2904e14bb325SJeff Bonwick 
29053d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2906e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2907fa9e4066Sahrens 
29084b964adaSGeorge Wilson 	tvd = vd->vdev_top;
29094b964adaSGeorge Wilson 
2910069f55e2SEric Schrock 	/*
2911069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
2912069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
2913069f55e2SEric Schrock 	 * were faulted.
2914069f55e2SEric Schrock 	 */
2915069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
2916069f55e2SEric Schrock 
29173d7072f8Seschrock 	/*
29183d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
29193d7072f8Seschrock 	 */
292098d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
29213d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
29223d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
2923069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
29243d7072f8Seschrock 
29253d7072f8Seschrock 	/*
2926c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
2927c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
29283d7072f8Seschrock 	 */
29294b964adaSGeorge Wilson 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
29303d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
29313d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
29323d7072f8Seschrock 
29333d7072f8Seschrock 		/*
29343d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
29353d7072f8Seschrock 		 * mark it degraded.
29363d7072f8Seschrock 		 */
29374b964adaSGeorge Wilson 		vdev_reopen(tvd);
29383d7072f8Seschrock 
2939069f55e2SEric Schrock 		if (vdev_readable(vd))
2940069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
29413d7072f8Seschrock 	}
29423d7072f8Seschrock 
2943e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
29443d7072f8Seschrock }
29453d7072f8Seschrock 
29463d7072f8Seschrock /*
29473d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
29483d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
29493d7072f8Seschrock  * as I/O is concerned.
29503d7072f8Seschrock  */
29513d7072f8Seschrock int
2952069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
29533d7072f8Seschrock {
2954c5904d13Seschrock 	vdev_t *vd;
29550a4e9518Sgw 
29568f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
29573d7072f8Seschrock 
2958c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2959e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2960e14bb325SJeff Bonwick 
29610e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
2962e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
29630e34b6a7Sbonwick 
29643d7072f8Seschrock 	/*
29653d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
29663d7072f8Seschrock 	 */
2967e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
2968e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
29693d7072f8Seschrock 
29703d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
29713d7072f8Seschrock 	if (!vdev_is_dead(vd))
29723d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2973069f55e2SEric Schrock 		    aux);
29743d7072f8Seschrock 
2975e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
29763d7072f8Seschrock }
29773d7072f8Seschrock 
29783d7072f8Seschrock /*
2979f7170741SWill Andrews  * Online the given vdev.
2980f7170741SWill Andrews  *
2981f7170741SWill Andrews  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
2982f7170741SWill Andrews  * spare device should be detached when the device finishes resilvering.
2983f7170741SWill Andrews  * Second, the online should be treated like a 'test' online case, so no FMA
2984f7170741SWill Andrews  * events are generated if the device fails to open.
29853d7072f8Seschrock  */
29863d7072f8Seschrock int
2987e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
29883d7072f8Seschrock {
2989573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
29905f368aefSYuri Pankov 	boolean_t wasoffline;
29915f368aefSYuri Pankov 	vdev_state_t oldstate;
29923d7072f8Seschrock 
29938f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
29943d7072f8Seschrock 
2995c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2996e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
29973d7072f8Seschrock 
29983d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2999e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3000fa9e4066Sahrens 
30015f368aefSYuri Pankov 	wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
30025f368aefSYuri Pankov 	oldstate = vd->vdev_state;
300314372834SHans Rosenfeld 
3004573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
3005fa9e4066Sahrens 	vd->vdev_offline = B_FALSE;
3006441d80aaSlling 	vd->vdev_tmpoffline = B_FALSE;
3007e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3008e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3009573ca77eSGeorge Wilson 
3010573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
3011573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
3012573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3013573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
3014573ca77eSGeorge Wilson 	}
3015573ca77eSGeorge Wilson 
3016573ca77eSGeorge Wilson 	vdev_reopen(tvd);
30173d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
30183d7072f8Seschrock 
3019573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
3020573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3021573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
3022573ca77eSGeorge Wilson 	}
3023573ca77eSGeorge Wilson 
30243d7072f8Seschrock 	if (newstate)
30253d7072f8Seschrock 		*newstate = vd->vdev_state;
30263d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
30273d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
30283d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
30293d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
30303d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
3031fa9e4066Sahrens 
3032573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3033573ca77eSGeorge Wilson 
3034573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
3035573ca77eSGeorge Wilson 		if (vd->vdev_aux)
3036573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3037573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3038573ca77eSGeorge Wilson 	}
303914372834SHans Rosenfeld 
30405f368aefSYuri Pankov 	if (wasoffline ||
30415f368aefSYuri Pankov 	    (oldstate < VDEV_STATE_DEGRADED &&
30425f368aefSYuri Pankov 	    vd->vdev_state >= VDEV_STATE_DEGRADED))
3043ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
304414372834SHans Rosenfeld 
30458ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
3046fa9e4066Sahrens }
3047fa9e4066Sahrens 
3048a1521560SJeff Bonwick static int
3049a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
3050fa9e4066Sahrens {
3051e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
30528f18d1faSGeorge Wilson 	int error = 0;
30538f18d1faSGeorge Wilson 	uint64_t generation;
30548f18d1faSGeorge Wilson 	metaslab_group_t *mg;
30550a4e9518Sgw 
30568f18d1faSGeorge Wilson top:
30578f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
3058fa9e4066Sahrens 
3059c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3060e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3061fa9e4066Sahrens 
30620e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
3063e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
30640e34b6a7Sbonwick 
3065e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
30668f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
30678f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
3068e6ca193dSGeorge Wilson 
3069fa9e4066Sahrens 	/*
3070ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
3071fa9e4066Sahrens 	 */
3072ecc2d604Sbonwick 	if (!vd->vdev_offline) {
3073ecc2d604Sbonwick 		/*
30748ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
3075e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
3076e6ca193dSGeorge Wilson 		 * expendable.
3077ecc2d604Sbonwick 		 */
3078e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3079e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
3080e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3081fa9e4066Sahrens 
30828f18d1faSGeorge Wilson 		/*
3083b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
3084b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
3085b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
3086b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
30878f18d1faSGeorge Wilson 		 */
30888f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
30898f18d1faSGeorge Wilson 			/*
30908f18d1faSGeorge Wilson 			 * Prevent any future allocations.
30918f18d1faSGeorge Wilson 			 */
3092a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
30938f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
30948f18d1faSGeorge Wilson 
30955cabbc6bSPrashanth Sreenivasa 			error = spa_reset_logs(spa);
30968f18d1faSGeorge Wilson 
3097*86714001SSerapheim Dimitropoulos 			/*
3098*86714001SSerapheim Dimitropoulos 			 * If the log device was successfully reset but has
3099*86714001SSerapheim Dimitropoulos 			 * checkpointed data, do not offline it.
3100*86714001SSerapheim Dimitropoulos 			 */
3101*86714001SSerapheim Dimitropoulos 			if (error == 0 &&
3102*86714001SSerapheim Dimitropoulos 			    tvd->vdev_checkpoint_sm != NULL) {
3103*86714001SSerapheim Dimitropoulos 				ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc,
3104*86714001SSerapheim Dimitropoulos 				    !=, 0);
3105*86714001SSerapheim Dimitropoulos 				error = ZFS_ERR_CHECKPOINT_EXISTS;
3106*86714001SSerapheim Dimitropoulos 			}
3107*86714001SSerapheim Dimitropoulos 
31088f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
31098f18d1faSGeorge Wilson 
31108f18d1faSGeorge Wilson 			/*
31118f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
31128f18d1faSGeorge Wilson 			 */
31138f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
3114a1521560SJeff Bonwick 				metaslab_group_activate(mg);
31158f18d1faSGeorge Wilson 				if (error)
31168f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
31178f18d1faSGeorge Wilson 					    vd, error));
31188f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
31198f18d1faSGeorge Wilson 				goto top;
31208f18d1faSGeorge Wilson 			}
3121fb09f5aaSMadhav Suresh 			ASSERT0(tvd->vdev_stat.vs_alloc);
31228f18d1faSGeorge Wilson 		}
31238f18d1faSGeorge Wilson 
3124ecc2d604Sbonwick 		/*
3125ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
3126e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
3127e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
3128e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
3129ecc2d604Sbonwick 		 */
3130ecc2d604Sbonwick 		vd->vdev_offline = B_TRUE;
3131e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
3132e6ca193dSGeorge Wilson 
3133e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3134e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
3135ecc2d604Sbonwick 			vd->vdev_offline = B_FALSE;
3136e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
3137e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3138ecc2d604Sbonwick 		}
31398f18d1faSGeorge Wilson 
31408f18d1faSGeorge Wilson 		/*
31418f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
31428f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
31438f18d1faSGeorge Wilson 		 */
31448f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
3145a1521560SJeff Bonwick 			metaslab_group_activate(mg);
3146fa9e4066Sahrens 	}
3147fa9e4066Sahrens 
3148e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3149ecc2d604Sbonwick 
31508f18d1faSGeorge Wilson 	return (spa_vdev_state_exit(spa, vd, 0));
3151fa9e4066Sahrens }
3152fa9e4066Sahrens 
3153a1521560SJeff Bonwick int
3154a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3155a1521560SJeff Bonwick {
3156a1521560SJeff Bonwick 	int error;
3157a1521560SJeff Bonwick 
3158a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
3159a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
3160a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
3161a1521560SJeff Bonwick 
3162a1521560SJeff Bonwick 	return (error);
3163a1521560SJeff Bonwick }
3164a1521560SJeff Bonwick 
3165ea8dc4b6Seschrock /*
3166ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
3167ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
3168ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
3169ea8dc4b6Seschrock  */
3170ea8dc4b6Seschrock void
3171e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
3172fa9e4066Sahrens {
3173e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3174e14bb325SJeff Bonwick 
3175e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3176fa9e4066Sahrens 
3177ea8dc4b6Seschrock 	if (vd == NULL)
3178e14bb325SJeff Bonwick 		vd = rvd;
3179fa9e4066Sahrens 
3180ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
3181ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
3182ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
3183fa9e4066Sahrens 
3184e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
3185e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
31863d7072f8Seschrock 
31875cabbc6bSPrashanth Sreenivasa 	/*
31885cabbc6bSPrashanth Sreenivasa 	 * It makes no sense to "clear" an indirect vdev.
31895cabbc6bSPrashanth Sreenivasa 	 */
31905cabbc6bSPrashanth Sreenivasa 	if (!vdev_is_concrete(vd))
31915cabbc6bSPrashanth Sreenivasa 		return;
31925cabbc6bSPrashanth Sreenivasa 
31933d7072f8Seschrock 	/*
31948a79c1b5Sek 	 * If we're in the FAULTED state or have experienced failed I/O, then
31958a79c1b5Sek 	 * clear the persistent state and attempt to reopen the device.  We
31968a79c1b5Sek 	 * also mark the vdev config dirty, so that the new faulted state is
31978a79c1b5Sek 	 * written out to disk.
31983d7072f8Seschrock 	 */
3199e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
3200e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
32018a79c1b5Sek 
3202096d22d4SEric Schrock 		/*
3203096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
3204096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
3205096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
3206096d22d4SEric Schrock 		 */
3207096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
3208096d22d4SEric Schrock 
32094b964adaSGeorge Wilson 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3210e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
3211e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
3212e14bb325SJeff Bonwick 
3213f9af39baSGeorge Wilson 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
32143d7072f8Seschrock 
3215096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
3216096d22d4SEric Schrock 
3217f9af39baSGeorge Wilson 		if (vd != rvd && vdev_writeable(vd->vdev_top))
3218e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
3219e14bb325SJeff Bonwick 
3220e14bb325SJeff Bonwick 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
3221bb8b5132Sek 			spa_async_request(spa, SPA_ASYNC_RESILVER);
32223d7072f8Seschrock 
3223ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
32243d7072f8Seschrock 	}
3225096d22d4SEric Schrock 
3226096d22d4SEric Schrock 	/*
3227096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
3228096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
3229096d22d4SEric Schrock 	 * done in response to the FMA fault.
3230096d22d4SEric Schrock 	 */
3231096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3232096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3233096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
3234096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
3235fa9e4066Sahrens }
3236fa9e4066Sahrens 
3237e14bb325SJeff Bonwick boolean_t
3238e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd)
32390a4e9518Sgw {
324088ecc943SGeorge Wilson 	/*
324188ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
324288ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
324388ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
324488ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
324588ecc943SGeorge Wilson 	 * before issuing I/O to them.
324688ecc943SGeorge Wilson 	 */
32475cabbc6bSPrashanth Sreenivasa 	return (vd->vdev_state < VDEV_STATE_DEGRADED ||
32485cabbc6bSPrashanth Sreenivasa 	    vd->vdev_ops == &vdev_hole_ops ||
324988ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
32500a4e9518Sgw }
32510a4e9518Sgw 
3252e14bb325SJeff Bonwick boolean_t
3253e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
32540a4e9518Sgw {
3255e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
32560a4e9518Sgw }
32570a4e9518Sgw 
3258e14bb325SJeff Bonwick boolean_t
3259e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
3260fa9e4066Sahrens {
32615cabbc6bSPrashanth Sreenivasa 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
32625cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd));
3263fa9e4066Sahrens }
3264fa9e4066Sahrens 
3265a31e6787SGeorge Wilson boolean_t
3266a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
3267a31e6787SGeorge Wilson {
32688ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
32698ad4d6ddSJeff Bonwick 
3270a31e6787SGeorge Wilson 	/*
32718ad4d6ddSJeff Bonwick 	 * We currently allow allocations from vdevs which may be in the
3272a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3273a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
32748ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
32758ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
32768ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
3277a31e6787SGeorge Wilson 	 */
32788ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
32795cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_cant_write && vdev_is_concrete(vd) &&
32800f7643c7SGeorge Wilson 	    vd->vdev_mg->mg_initialized);
3281a31e6787SGeorge Wilson }
3282a31e6787SGeorge Wilson 
3283e14bb325SJeff Bonwick boolean_t
3284e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
3285fa9e4066Sahrens {
3286e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
3287fa9e4066Sahrens 
3288e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3289e14bb325SJeff Bonwick 		return (B_FALSE);
3290fa9e4066Sahrens 
3291e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
3292e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
3293fa9e4066Sahrens 
3294e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
3295e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
3296fa9e4066Sahrens 
3297e14bb325SJeff Bonwick 	return (B_TRUE);
3298fa9e4066Sahrens }
3299fa9e4066Sahrens 
3300*86714001SSerapheim Dimitropoulos boolean_t
3301*86714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd)
3302*86714001SSerapheim Dimitropoulos {
3303*86714001SSerapheim Dimitropoulos 	/*
3304*86714001SSerapheim Dimitropoulos 	 * Assuming 47 bits of the space map entry dedicated for the entry's
3305*86714001SSerapheim Dimitropoulos 	 * offset (see description in space_map.h), we calculate the maximum
3306*86714001SSerapheim Dimitropoulos 	 * address that can be described by a space map entry for the given
3307*86714001SSerapheim Dimitropoulos 	 * device.
3308*86714001SSerapheim Dimitropoulos 	 */
3309*86714001SSerapheim Dimitropoulos 	uint64_t shift = vd->vdev_ashift + 47;
3310*86714001SSerapheim Dimitropoulos 
3311*86714001SSerapheim Dimitropoulos 	if (shift >= 63) /* detect potential overflow */
3312*86714001SSerapheim Dimitropoulos 		return (B_TRUE);
3313*86714001SSerapheim Dimitropoulos 
3314*86714001SSerapheim Dimitropoulos 	return (vd->vdev_asize < (1ULL << shift));
3315*86714001SSerapheim Dimitropoulos }
3316*86714001SSerapheim Dimitropoulos 
3317fa9e4066Sahrens /*
3318fa9e4066Sahrens  * Get statistics for the given vdev.
3319fa9e4066Sahrens  */
3320fa9e4066Sahrens void
3321fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3322fa9e4066Sahrens {
33232e4c9986SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
33242e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
3325c39a2aaeSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
33262e4c9986SGeorge Wilson 
33272e4c9986SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3328fa9e4066Sahrens 
3329fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
3330fa9e4066Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3331fa9e4066Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3332fa9e4066Sahrens 	vs->vs_state = vd->vdev_state;
3333573ca77eSGeorge Wilson 	vs->vs_rsize = vdev_get_min_asize(vd);
3334573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
3335573ca77eSGeorge Wilson 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
3336c39a2aaeSGeorge Wilson 	/*
3337c39a2aaeSGeorge Wilson 	 * Report expandable space on top-level, non-auxillary devices only.
3338c39a2aaeSGeorge Wilson 	 * The expandable space is reported in terms of metaslab sized units
3339c39a2aaeSGeorge Wilson 	 * since that determines how much space the pool can expand.
3340c39a2aaeSGeorge Wilson 	 */
3341c39a2aaeSGeorge Wilson 	if (vd->vdev_aux == NULL && tvd != NULL) {
33427855d95bSToomas Soome 		vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize -
33437855d95bSToomas Soome 		    spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift);
3344c39a2aaeSGeorge Wilson 	}
33455cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
33465cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd)) {
33472e4c9986SGeorge Wilson 		vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
33482986efa8SAlex Reece 	}
3349fa9e4066Sahrens 
3350fa9e4066Sahrens 	/*
3351fa9e4066Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
3352fa9e4066Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
3353fa9e4066Sahrens 	 */
3354fa9e4066Sahrens 	if (vd == rvd) {
3355e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
3356fa9e4066Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
3357fa9e4066Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
3358fa9e4066Sahrens 
3359e14bb325SJeff Bonwick 			for (int t = 0; t < ZIO_TYPES; t++) {
3360fa9e4066Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
3361fa9e4066Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
3362fa9e4066Sahrens 			}
33633f9d6ad7SLin Ling 			cvs->vs_scan_removing = cvd->vdev_removing;
3364fa9e4066Sahrens 		}
3365fa9e4066Sahrens 	}
33662e4c9986SGeorge Wilson 	mutex_exit(&vd->vdev_stat_lock);
3367fa9e4066Sahrens }
3368fa9e4066Sahrens 
3369fa94a07fSbrendan void
3370fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
3371fa94a07fSbrendan {
3372fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
3373fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
3374fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
3375fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
3376fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
3377fa94a07fSbrendan }
3378fa94a07fSbrendan 
33793f9d6ad7SLin Ling void
33803f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd)
33813f9d6ad7SLin Ling {
33823f9d6ad7SLin Ling 	vdev_stat_t *vs = &vd->vdev_stat;
33833f9d6ad7SLin Ling 
33843f9d6ad7SLin Ling 	for (int c = 0; c < vd->vdev_children; c++)
33853f9d6ad7SLin Ling 		vdev_scan_stat_init(vd->vdev_child[c]);
33863f9d6ad7SLin Ling 
33873f9d6ad7SLin Ling 	mutex_enter(&vd->vdev_stat_lock);
33883f9d6ad7SLin Ling 	vs->vs_scan_processed = 0;
33893f9d6ad7SLin Ling 	mutex_exit(&vd->vdev_stat_lock);
33903f9d6ad7SLin Ling }
33913f9d6ad7SLin Ling 
3392fa9e4066Sahrens void
3393e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
3394fa9e4066Sahrens {
33958ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
33968ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3397e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3398fa9e4066Sahrens 	vdev_t *pvd;
3399fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
3400fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
3401fa9e4066Sahrens 	zio_type_t type = zio->io_type;
3402fa9e4066Sahrens 	int flags = zio->io_flags;
3403fa9e4066Sahrens 
3404e14bb325SJeff Bonwick 	/*
3405e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
3406e14bb325SJeff Bonwick 	 */
3407e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
3408e14bb325SJeff Bonwick 		return;
3409e14bb325SJeff Bonwick 
3410fa9e4066Sahrens 	if (zio->io_error == 0) {
3411e14bb325SJeff Bonwick 		/*
3412e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
3413e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
3414e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
3415e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
3416e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
3417e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
3418e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
3419e14bb325SJeff Bonwick 		 *
3420e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
3421e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
3422e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
3423e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
3424e14bb325SJeff Bonwick 		 */
3425e14bb325SJeff Bonwick 		if (vd == rvd)
3426e14bb325SJeff Bonwick 			return;
3427e14bb325SJeff Bonwick 
3428e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
34298ad4d6ddSJeff Bonwick 
34308ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
34318ad4d6ddSJeff Bonwick 			return;
34328ad4d6ddSJeff Bonwick 
34338ad4d6ddSJeff Bonwick 		mutex_enter(&vd->vdev_stat_lock);
34348ad4d6ddSJeff Bonwick 
3435e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
343644ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
34373f9d6ad7SLin Ling 				dsl_scan_phys_t *scn_phys =
34383f9d6ad7SLin Ling 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
34393f9d6ad7SLin Ling 				uint64_t *processed = &scn_phys->scn_processed;
34403f9d6ad7SLin Ling 
34413f9d6ad7SLin Ling 				/* XXX cleanup? */
34423f9d6ad7SLin Ling 				if (vd->vdev_ops->vdev_op_leaf)
34433f9d6ad7SLin Ling 					atomic_add_64(processed, psize);
34443f9d6ad7SLin Ling 				vs->vs_scan_processed += psize;
34453f9d6ad7SLin Ling 			}
34463f9d6ad7SLin Ling 
34478ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
3448e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
3449fa9e4066Sahrens 		}
34508ad4d6ddSJeff Bonwick 
34518ad4d6ddSJeff Bonwick 		vs->vs_ops[type]++;
34528ad4d6ddSJeff Bonwick 		vs->vs_bytes[type] += psize;
34538ad4d6ddSJeff Bonwick 
34548ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
3455fa9e4066Sahrens 		return;
3456fa9e4066Sahrens 	}
3457fa9e4066Sahrens 
3458fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
3459fa9e4066Sahrens 		return;
3460fa9e4066Sahrens 
34618956713aSEric Schrock 	/*
34628956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
34638956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
34648956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
34658956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
34668956713aSEric Schrock 	 */
34678956713aSEric Schrock 	if (zio->io_error == EIO &&
34688956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
34698956713aSEric Schrock 		return;
34708956713aSEric Schrock 
34718f18d1faSGeorge Wilson 	/*
34728f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
34738f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
34748f18d1faSGeorge Wilson 	 * errors.
34758f18d1faSGeorge Wilson 	 */
34768f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
34778f18d1faSGeorge Wilson 		return;
34788f18d1faSGeorge Wilson 
3479e14bb325SJeff Bonwick 	mutex_enter(&vd->vdev_stat_lock);
3480b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3481e14bb325SJeff Bonwick 		if (zio->io_error == ECKSUM)
3482e14bb325SJeff Bonwick 			vs->vs_checksum_errors++;
3483e14bb325SJeff Bonwick 		else
3484e14bb325SJeff Bonwick 			vs->vs_read_errors++;
3485fa9e4066Sahrens 	}
3486b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3487e14bb325SJeff Bonwick 		vs->vs_write_errors++;
3488e14bb325SJeff Bonwick 	mutex_exit(&vd->vdev_stat_lock);
3489fa9e4066Sahrens 
34905cabbc6bSPrashanth Sreenivasa 	if (spa->spa_load_state == SPA_LOAD_NONE &&
34915cabbc6bSPrashanth Sreenivasa 	    type == ZIO_TYPE_WRITE && txg != 0 &&
34928ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
349344ecc532SGeorge Wilson 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
3494b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
34958ad4d6ddSJeff Bonwick 		/*
3496b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
3497b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
3498b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
3499b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
3500b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
3501b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
3502b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
3503b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
35048ad4d6ddSJeff Bonwick 		 *
35058ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
35068ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
35078ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
35088ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
35098ad4d6ddSJeff Bonwick 		 */
35108ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
35118ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
351244ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
35138ad4d6ddSJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
35148ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
35158ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3516b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
3517b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
3518b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3519b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
35208ad4d6ddSJeff Bonwick 			}
3521b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
35228ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3523fa9e4066Sahrens 				return;
35248ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
35258ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
35268ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3527fa9e4066Sahrens 		}
35288ad4d6ddSJeff Bonwick 		if (vd != rvd)
35298ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3530fa9e4066Sahrens 	}
3531fa9e4066Sahrens }
3532fa9e4066Sahrens 
3533fa9e4066Sahrens /*
3534b24ab676SJeff Bonwick  * Update the in-core space usage stats for this vdev, its metaslab class,
3535b24ab676SJeff Bonwick  * and the root vdev.
3536fa9e4066Sahrens  */
3537fa9e4066Sahrens void
3538b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3539b24ab676SJeff Bonwick     int64_t space_delta)
3540fa9e4066Sahrens {
354199653d4eSeschrock 	int64_t dspace_delta = space_delta;
35428654d025Sperrin 	spa_t *spa = vd->vdev_spa;
35438654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
3544b24ab676SJeff Bonwick 	metaslab_group_t *mg = vd->vdev_mg;
3545b24ab676SJeff Bonwick 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
3546fa9e4066Sahrens 
35478654d025Sperrin 	ASSERT(vd == vd->vdev_top);
354899653d4eSeschrock 
35498654d025Sperrin 	/*
35508654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
35518654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
35528654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
35538654d025Sperrin 	 * childrens', thus not accurate enough for us.
35548654d025Sperrin 	 */
35558654d025Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
3556e6ca193dSGeorge Wilson 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
35578654d025Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
35588654d025Sperrin 	    vd->vdev_deflate_ratio;
35598654d025Sperrin 
35608654d025Sperrin 	mutex_enter(&vd->vdev_stat_lock);
35618654d025Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
3562b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
35638654d025Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
35648654d025Sperrin 	mutex_exit(&vd->vdev_stat_lock);
35658654d025Sperrin 
3566b24ab676SJeff Bonwick 	if (mc == spa_normal_class(spa)) {
3567fa94a07fSbrendan 		mutex_enter(&rvd->vdev_stat_lock);
3568fa94a07fSbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
3569b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
3570fa94a07fSbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
3571fa94a07fSbrendan 		mutex_exit(&rvd->vdev_stat_lock);
3572fa94a07fSbrendan 	}
3573b24ab676SJeff Bonwick 
3574b24ab676SJeff Bonwick 	if (mc != NULL) {
3575b24ab676SJeff Bonwick 		ASSERT(rvd == vd->vdev_parent);
3576b24ab676SJeff Bonwick 		ASSERT(vd->vdev_ms_count != 0);
3577b24ab676SJeff Bonwick 
3578b24ab676SJeff Bonwick 		metaslab_class_space_update(mc,
3579b24ab676SJeff Bonwick 		    alloc_delta, defer_delta, space_delta, dspace_delta);
3580b24ab676SJeff Bonwick 	}
3581fa9e4066Sahrens }
3582fa9e4066Sahrens 
3583fa9e4066Sahrens /*
3584fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
3585fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
3586fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3587fa9e4066Sahrens  */
3588fa9e4066Sahrens void
3589fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
3590fa9e4066Sahrens {
3591fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
3592fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3593fa9e4066Sahrens 	int c;
3594fa9e4066Sahrens 
3595f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3596f9af39baSGeorge Wilson 
3597c5904d13Seschrock 	/*
35986809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
35996809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
3600c5904d13Seschrock 	 */
3601c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
3602c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
3603c5904d13Seschrock 		nvlist_t **aux;
3604c5904d13Seschrock 		uint_t naux;
3605c5904d13Seschrock 
3606c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
3607c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
3608c5904d13Seschrock 				break;
3609c5904d13Seschrock 		}
3610c5904d13Seschrock 
3611e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
3612e14bb325SJeff Bonwick 			/*
3613e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
3614e14bb325SJeff Bonwick 			 */
3615e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
3616e14bb325SJeff Bonwick 			return;
3617e14bb325SJeff Bonwick 		}
3618e14bb325SJeff Bonwick 
3619c5904d13Seschrock 		sav->sav_sync = B_TRUE;
3620c5904d13Seschrock 
36216809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
36226809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
36236809eb4eSEric Schrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
36246809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
36256809eb4eSEric Schrock 		}
3626c5904d13Seschrock 
3627c5904d13Seschrock 		ASSERT(c < naux);
3628c5904d13Seschrock 
3629c5904d13Seschrock 		/*
3630c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
3631c5904d13Seschrock 		 * sketchy, but it will work.
3632c5904d13Seschrock 		 */
3633c5904d13Seschrock 		nvlist_free(aux[c]);
36343f9d6ad7SLin Ling 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3635c5904d13Seschrock 
3636c5904d13Seschrock 		return;
3637c5904d13Seschrock 	}
3638c5904d13Seschrock 
36395dabedeeSbonwick 	/*
3640e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
3641e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
3642e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
36435dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
36445dabedeeSbonwick 	 */
3645e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3646e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3647e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
36485dabedeeSbonwick 
3649fa9e4066Sahrens 	if (vd == rvd) {
3650fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
3651fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
3652fa9e4066Sahrens 	} else {
3653fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
3654fa9e4066Sahrens 
365588ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
36565cabbc6bSPrashanth Sreenivasa 		    vdev_is_concrete(vd)) {
3657e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
36585cabbc6bSPrashanth Sreenivasa 		}
3659fa9e4066Sahrens 	}
3660fa9e4066Sahrens }
3661fa9e4066Sahrens 
3662fa9e4066Sahrens void
3663fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
3664fa9e4066Sahrens {
36655dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
36665dabedeeSbonwick 
3667e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3668e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3669e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
36705dabedeeSbonwick 
3671e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3672e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
3673e14bb325SJeff Bonwick }
3674e14bb325SJeff Bonwick 
3675e14bb325SJeff Bonwick /*
3676e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
3677e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
3678e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
3679e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
3680e14bb325SJeff Bonwick  */
3681e14bb325SJeff Bonwick void
3682e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
3683e14bb325SJeff Bonwick {
3684e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3685e14bb325SJeff Bonwick 
3686f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3687e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
3688e14bb325SJeff Bonwick 
3689e14bb325SJeff Bonwick 	/*
3690e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
3691e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
3692e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
3693e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
3694e14bb325SJeff Bonwick 	 */
3695e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3696e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3697e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3698e14bb325SJeff Bonwick 
36995cabbc6bSPrashanth Sreenivasa 	if (!list_link_active(&vd->vdev_state_dirty_node) &&
37005cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd))
3701e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
3702e14bb325SJeff Bonwick }
3703e14bb325SJeff Bonwick 
3704e14bb325SJeff Bonwick void
3705e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
3706e14bb325SJeff Bonwick {
3707e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3708e14bb325SJeff Bonwick 
3709e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3710e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3711e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3712e14bb325SJeff Bonwick 
3713e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3714e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
3715fa9e4066Sahrens }
3716fa9e4066Sahrens 
371732b87932Sek /*
371832b87932Sek  * Propagate vdev state up from children to parent.
371932b87932Sek  */
372044cd46caSbillm void
372144cd46caSbillm vdev_propagate_state(vdev_t *vd)
372244cd46caSbillm {
37238ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
37248ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
372544cd46caSbillm 	int degraded = 0, faulted = 0;
372644cd46caSbillm 	int corrupted = 0;
372744cd46caSbillm 	vdev_t *child;
372844cd46caSbillm 
37293d7072f8Seschrock 	if (vd->vdev_children > 0) {
3730573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
37313d7072f8Seschrock 			child = vd->vdev_child[c];
373251ece835Seschrock 
373388ecc943SGeorge Wilson 			/*
37345cabbc6bSPrashanth Sreenivasa 			 * Don't factor holes or indirect vdevs into the
37355cabbc6bSPrashanth Sreenivasa 			 * decision.
373688ecc943SGeorge Wilson 			 */
37375cabbc6bSPrashanth Sreenivasa 			if (!vdev_is_concrete(child))
373888ecc943SGeorge Wilson 				continue;
373988ecc943SGeorge Wilson 
3740e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
37418ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
374251ece835Seschrock 				/*
374351ece835Seschrock 				 * Root special: if there is a top-level log
374451ece835Seschrock 				 * device, treat the root vdev as if it were
374551ece835Seschrock 				 * degraded.
374651ece835Seschrock 				 */
374751ece835Seschrock 				if (child->vdev_islog && vd == rvd)
374851ece835Seschrock 					degraded++;
374951ece835Seschrock 				else
375051ece835Seschrock 					faulted++;
375151ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
37523d7072f8Seschrock 				degraded++;
375351ece835Seschrock 			}
375444cd46caSbillm 
37553d7072f8Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
37563d7072f8Seschrock 				corrupted++;
37573d7072f8Seschrock 		}
375844cd46caSbillm 
37593d7072f8Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
37603d7072f8Seschrock 
37613d7072f8Seschrock 		/*
3762e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
37633d7072f8Seschrock 		 * opened due to corrupted metadata, then propagate the root
37643d7072f8Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
37653d7072f8Seschrock 		 * replicas'.
37663d7072f8Seschrock 		 */
37673d7072f8Seschrock 		if (corrupted && vd == rvd &&
37683d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
37693d7072f8Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
37703d7072f8Seschrock 			    VDEV_AUX_CORRUPT_DATA);
37713d7072f8Seschrock 	}
37723d7072f8Seschrock 
377351ece835Seschrock 	if (vd->vdev_parent)
37743d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
377544cd46caSbillm }
377644cd46caSbillm 
3777fa9e4066Sahrens /*
3778ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
3779ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
3780ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
3781ea8dc4b6Seschrock  *
3782ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
3783ea8dc4b6Seschrock  * generated.
3784fa9e4066Sahrens  */
3785fa9e4066Sahrens void
3786ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3787fa9e4066Sahrens {
3788560e6e96Seschrock 	uint64_t save_state;
3789c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
3790ea8dc4b6Seschrock 
3791ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
3792ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
3793fa9e4066Sahrens 		return;
3794ea8dc4b6Seschrock 	}
3795ea8dc4b6Seschrock 
3796560e6e96Seschrock 	save_state = vd->vdev_state;
3797fa9e4066Sahrens 
3798fa9e4066Sahrens 	vd->vdev_state = state;
3799fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
3800fa9e4066Sahrens 
38013d7072f8Seschrock 	/*
38023d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
380398d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
380498d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
380598d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
380698d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
380798d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
380898d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
380998d1cbfeSGeorge Wilson 	 * children.
381098d1cbfeSGeorge Wilson 	 */
381198d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
381298d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
38133d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
38143d7072f8Seschrock 
3815069f55e2SEric Schrock 	/*
3816069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
3817069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
3818069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
3819069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
3820069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
3821069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
3822069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
3823069f55e2SEric Schrock 	 */
3824069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3825069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
3826069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
3827069f55e2SEric Schrock 
38283d7072f8Seschrock 	if (vd->vdev_removed &&
38293d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
38303d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
38313d7072f8Seschrock 		/*
38323d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
38333d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
38343d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
38353d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
38363d7072f8Seschrock 		 * it is one of our special test online cases, which is only
38373d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
38383d7072f8Seschrock 		 * fault.
38393d7072f8Seschrock 		 */
38403d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
38413d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
38423d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
38433d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
38443d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
3845ea8dc4b6Seschrock 		/*
3846cb04b873SMark J Musante 		 * If we fail to open a vdev during an import or recovery, we
3847cb04b873SMark J Musante 		 * mark it as "not available", which signifies that it was
3848cb04b873SMark J Musante 		 * never there to begin with.  Failure to open such a device
3849cb04b873SMark J Musante 		 * is not considered an error.
3850ea8dc4b6Seschrock 		 */
3851cb04b873SMark J Musante 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3852cb04b873SMark J Musante 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3853560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
3854560e6e96Seschrock 			vd->vdev_not_present = 1;
3855560e6e96Seschrock 
3856560e6e96Seschrock 		/*
3857560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
3858560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3859560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
3860560e6e96Seschrock 		 * want to post the ereport if the device was already in the
3861560e6e96Seschrock 		 * CANT_OPEN state beforehand.
38623d7072f8Seschrock 		 *
38633d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
38643d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
38653d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
38663d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
38673d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
38683d7072f8Seschrock 		 * about to replace the device, or attempt an online with
38693d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
3870560e6e96Seschrock 		 */
38713d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
38723d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
3873c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
3874ea8dc4b6Seschrock 			const char *class;
3875ea8dc4b6Seschrock 
3876ea8dc4b6Seschrock 			switch (aux) {
3877ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
3878ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3879ea8dc4b6Seschrock 				break;
3880ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
3881ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3882ea8dc4b6Seschrock 				break;
3883ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
3884ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3885ea8dc4b6Seschrock 				break;
3886ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
3887ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3888ea8dc4b6Seschrock 				break;
3889ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
3890ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3891ea8dc4b6Seschrock 				break;
3892ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
3893ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3894ea8dc4b6Seschrock 				break;
3895ea8dc4b6Seschrock 			default:
3896ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3897ea8dc4b6Seschrock 			}
3898ea8dc4b6Seschrock 
3899c5904d13Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3900ea8dc4b6Seschrock 		}
3901ea8dc4b6Seschrock 
39023d7072f8Seschrock 		/* Erase any notion of persistent removed state */
39033d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
39043d7072f8Seschrock 	} else {
39053d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
39063d7072f8Seschrock 	}
3907ea8dc4b6Seschrock 
39088b33d774STim Haley 	if (!isopen && vd->vdev_parent)
39098b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
3910fa9e4066Sahrens }
391115e6edf1Sgw 
39126f793812SPavel Zakharov boolean_t
39136f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd)
39146f793812SPavel Zakharov {
39156f793812SPavel Zakharov 	ASSERT(!vd->vdev_ops->vdev_op_leaf);
39166f793812SPavel Zakharov 
39176f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
39186f793812SPavel Zakharov 		if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
39196f793812SPavel Zakharov 			return (B_FALSE);
39206f793812SPavel Zakharov 	}
39216f793812SPavel Zakharov 
39226f793812SPavel Zakharov 	return (B_TRUE);
39236f793812SPavel Zakharov }
39246f793812SPavel Zakharov 
392515e6edf1Sgw /*
392615e6edf1Sgw  * Check the vdev configuration to ensure that it's capable of supporting
3927c8811bd3SToomas Soome  * a root pool. We do not support partial configuration.
3928c8811bd3SToomas Soome  * In addition, only a single top-level vdev is allowed.
392915e6edf1Sgw  */
393015e6edf1Sgw boolean_t
393115e6edf1Sgw vdev_is_bootable(vdev_t *vd)
393215e6edf1Sgw {
393315e6edf1Sgw 	if (!vd->vdev_ops->vdev_op_leaf) {
393415e6edf1Sgw 		char *vdev_type = vd->vdev_ops->vdev_op_type;
393515e6edf1Sgw 
393615e6edf1Sgw 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
393715e6edf1Sgw 		    vd->vdev_children > 1) {
393815e6edf1Sgw 			return (B_FALSE);
39395cabbc6bSPrashanth Sreenivasa 		} else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
39405cabbc6bSPrashanth Sreenivasa 		    strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
394115e6edf1Sgw 			return (B_FALSE);
394215e6edf1Sgw 		}
394315e6edf1Sgw 	}
394415e6edf1Sgw 
3945573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
394615e6edf1Sgw 		if (!vdev_is_bootable(vd->vdev_child[c]))
394715e6edf1Sgw 			return (B_FALSE);
394815e6edf1Sgw 	}
394915e6edf1Sgw 	return (B_TRUE);
395015e6edf1Sgw }
3951e6ca193dSGeorge Wilson 
39525cabbc6bSPrashanth Sreenivasa boolean_t
39535cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd)
39545cabbc6bSPrashanth Sreenivasa {
39555cabbc6bSPrashanth Sreenivasa 	vdev_ops_t *ops = vd->vdev_ops;
39565cabbc6bSPrashanth Sreenivasa 	if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
39575cabbc6bSPrashanth Sreenivasa 	    ops == &vdev_missing_ops || ops == &vdev_root_ops) {
39585cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
39595cabbc6bSPrashanth Sreenivasa 	} else {
39605cabbc6bSPrashanth Sreenivasa 		return (B_TRUE);
39615cabbc6bSPrashanth Sreenivasa 	}
39625cabbc6bSPrashanth Sreenivasa }
39635cabbc6bSPrashanth Sreenivasa 
39644b964adaSGeorge Wilson /*
39654b964adaSGeorge Wilson  * Determine if a log device has valid content.  If the vdev was
39664b964adaSGeorge Wilson  * removed or faulted in the MOS config then we know that
39674b964adaSGeorge Wilson  * the content on the log device has already been written to the pool.
39684b964adaSGeorge Wilson  */
39694b964adaSGeorge Wilson boolean_t
39704b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd)
39714b964adaSGeorge Wilson {
39724b964adaSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
39734b964adaSGeorge Wilson 	    !vd->vdev_removed)
39744b964adaSGeorge Wilson 		return (B_TRUE);
39754b964adaSGeorge Wilson 
39764b964adaSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
39774b964adaSGeorge Wilson 		if (vdev_log_state_valid(vd->vdev_child[c]))
39784b964adaSGeorge Wilson 			return (B_TRUE);
39794b964adaSGeorge Wilson 
39804b964adaSGeorge Wilson 	return (B_FALSE);
39814b964adaSGeorge Wilson }
39824b964adaSGeorge Wilson 
3983573ca77eSGeorge Wilson /*
3984573ca77eSGeorge Wilson  * Expand a vdev if possible.
3985573ca77eSGeorge Wilson  */
3986573ca77eSGeorge Wilson void
3987573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
3988573ca77eSGeorge Wilson {
3989573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
3990573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3991573ca77eSGeorge Wilson 
39925cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
39935cabbc6bSPrashanth Sreenivasa 
39945cabbc6bSPrashanth Sreenivasa 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
39955cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd)) {
3996573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
3997573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
3998573ca77eSGeorge Wilson 	}
3999573ca77eSGeorge Wilson }
40001195e687SMark J Musante 
40011195e687SMark J Musante /*
40021195e687SMark J Musante  * Split a vdev.
40031195e687SMark J Musante  */
40041195e687SMark J Musante void
40051195e687SMark J Musante vdev_split(vdev_t *vd)
40061195e687SMark J Musante {
40071195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
40081195e687SMark J Musante 
40091195e687SMark J Musante 	vdev_remove_child(pvd, vd);
40101195e687SMark J Musante 	vdev_compact_children(pvd);
40111195e687SMark J Musante 
40121195e687SMark J Musante 	cvd = pvd->vdev_child[0];
40131195e687SMark J Musante 	if (pvd->vdev_children == 1) {
40141195e687SMark J Musante 		vdev_remove_parent(cvd);
40151195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
40161195e687SMark J Musante 	}
40171195e687SMark J Musante 	vdev_propagate_state(cvd);
40181195e687SMark J Musante }
4019283b8460SGeorge.Wilson 
4020283b8460SGeorge.Wilson void
4021283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd)
4022283b8460SGeorge.Wilson {
4023283b8460SGeorge.Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
4024283b8460SGeorge.Wilson 		vdev_t *cvd = vd->vdev_child[c];
4025283b8460SGeorge.Wilson 
4026283b8460SGeorge.Wilson 		vdev_deadman(cvd);
4027283b8460SGeorge.Wilson 	}
4028283b8460SGeorge.Wilson 
4029283b8460SGeorge.Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
4030283b8460SGeorge.Wilson 		vdev_queue_t *vq = &vd->vdev_queue;
4031283b8460SGeorge.Wilson 
4032283b8460SGeorge.Wilson 		mutex_enter(&vq->vq_lock);
403369962b56SMatthew Ahrens 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
4034283b8460SGeorge.Wilson 			spa_t *spa = vd->vdev_spa;
4035283b8460SGeorge.Wilson 			zio_t *fio;
4036283b8460SGeorge.Wilson 			uint64_t delta;
4037283b8460SGeorge.Wilson 
4038283b8460SGeorge.Wilson 			/*
4039283b8460SGeorge.Wilson 			 * Look at the head of all the pending queues,
4040283b8460SGeorge.Wilson 			 * if any I/O has been outstanding for longer than
4041283b8460SGeorge.Wilson 			 * the spa_deadman_synctime we panic the system.
4042283b8460SGeorge.Wilson 			 */
404369962b56SMatthew Ahrens 			fio = avl_first(&vq->vq_active_tree);
4044c55e05cbSMatthew Ahrens 			delta = gethrtime() - fio->io_timestamp;
4045c55e05cbSMatthew Ahrens 			if (delta > spa_deadman_synctime(spa)) {
40463ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "SLOW IO: zio timestamp "
40473ee8c80cSPavel Zakharov 				    "%lluns, delta %lluns, last io %lluns",
40483ee8c80cSPavel Zakharov 				    fio->io_timestamp, (u_longlong_t)delta,
4049283b8460SGeorge.Wilson 				    vq->vq_io_complete_ts);
4050283b8460SGeorge.Wilson 				fm_panic("I/O to pool '%s' appears to be "
4051283b8460SGeorge.Wilson 				    "hung.", spa_name(spa));
4052283b8460SGeorge.Wilson 			}
4053283b8460SGeorge.Wilson 		}
4054283b8460SGeorge.Wilson 		mutex_exit(&vq->vq_lock);
4055283b8460SGeorge.Wilson 	}
4056283b8460SGeorge.Wilson }
4057